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 clean_up(void);
157 static void generate_text_internal(char *, int, struct text_object,
158 struct information *);
159 static int extract_variable_text_internal(struct text_object *,
162 static void print_version(void)
164 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
166 printf("\nCompiled in features:\n\n"
167 "System config file: "SYSTEM_CONFIG_FILE"\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 */
217 #endif /* HAVE_LUA */
220 #endif /* HAVE_IWLIB */
222 " * support for IBM/Lenovo notebooks\n"
232 #endif /* CONFIG_OUTPUT */
237 " * ALSA mixer support\n"
238 #endif /* MIXER_IS_ALSA */
250 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
255 static void X11_destroy_window(void);
256 static void X11_create_window(void);
257 static void X11_initialisation(void);
259 struct _x11_stuff_s {
263 XserverRegion region2, part;
264 int event_base, error_base;
270 static int text_start_x, text_start_y; /* text start position in window */
271 static int text_width, text_height;
286 /* display to connect to */
287 static char *disp = NULL;
291 /* struct that has all info to be shared between
292 * instances of the same text object */
293 struct information info;
295 /* default config file */
296 static char *current_config;
298 /* set to 1 if you want all text to be in uppercase */
299 static unsigned int stuff_in_upper_case;
301 /* Run how many times? */
302 static unsigned long total_run_times;
305 static int fork_to_background;
307 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
309 /* filenames for output */
310 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
311 char *append_file = NULL; FILE *append_fpointer = NULL;
315 static int show_graph_scale;
316 static int show_graph_range;
318 /* Position on the screen */
319 static int text_alignment;
320 static int gap_x, gap_y;
323 static int draw_borders;
324 static int draw_graph_borders;
325 static int stippled_borders;
327 static int draw_shades, draw_outline;
329 static long border_inner_margin, border_outer_margin, border_width;
331 static long default_fg_color, default_bg_color, default_out_color;
333 /* create own window or draw stuff to root? */
334 static int set_transparent = 0;
337 static int own_window = 0;
338 static int background_colour = 0;
340 /* fixed size/pos is set if wm/user changes them */
341 static int fixed_size = 0, fixed_pos = 0;
344 static int minimum_width, minimum_height;
345 static int maximum_width;
350 static int sensor_device;
353 static long color0, color1, color2, color3, color4, color5, color6, color7,
356 #define MAX_TEMPLATES 10
357 static char *template[MAX_TEMPLATES];
359 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
360 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
362 /* maximum size of individual text buffers, ie $exec buffer size */
363 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
366 #define CODEPAGE_LENGTH 20
368 long iconv_count = 0;
369 char iconv_converting;
370 static iconv_t **iconv_cd = 0;
372 int register_iconv(iconv_t *new_iconv)
374 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
376 CRIT_ERR("Out of memory");
378 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
379 if (!iconv_cd[iconv_count]) {
380 CRIT_ERR("Out of memory");
382 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
387 void free_iconv(void)
392 for (i = 0; i < iconv_count; i++) {
394 iconv_close(*iconv_cd[i]);
408 /* no buffers in used memory? */
411 /* pad percentages to decimals? */
412 static int pad_percents = 0;
414 static char *global_text = 0;
415 long global_text_lines;
417 static int total_updates;
418 static int updatereset;
420 int check_contains(char *f, char *s)
423 FILE *where = open_file(f, 0);
428 while (fgets(buf1, 256, where)) {
429 if (strstr(buf1, s)) {
436 ERR("Could not open the file");
442 static inline int calc_text_width(const char *s, int l)
444 if ((output_methods & TO_X) == 0)
451 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
452 (const FcChar8 *) s, l, &gi);
454 XftTextExtents8(display, fonts[selected_font].xftfont,
455 (const FcChar8 *) s, l, &gi);
461 return XTextWidth(fonts[selected_font].font, s, l);
466 /* formatted text to render on screen, generated in generate_text(),
467 * drawn in draw_stuff() */
469 static char *text_buffer;
472 static unsigned int special_index; /* used when drawing */
475 /* quite boring functions */
477 static inline void for_each_line(char *b, void f(char *))
481 for (ps = b, pe = b; *pe; pe++) {
495 static void convert_escapes(char *buf)
497 char *p = buf, *s = buf;
504 } else if (*s == '\\') {
515 /* Prints anything normally printed with snprintf according to the current value
516 * of use_spacer. Actually slightly more flexible than snprintf, as you can
517 * safely specify the destination buffer as one of your inputs. */
518 int spaced_print(char *buf, int size, const char *format, int width, ...)
527 tempbuf = malloc(size * sizeof(char));
529 // Passes the varargs along to vsnprintf
530 va_start(argp, width);
531 vsnprintf(tempbuf, size, format, argp);
534 switch (use_spacer) {
536 len = snprintf(buf, size, "%s", tempbuf);
539 len = snprintf(buf, size, "%*s", width, tempbuf);
542 len = snprintf(buf, size, "%-*s", width, tempbuf);
549 /* print percentage values
551 * - i.e., unsigned values between 0 and 100
552 * - respect the value of pad_percents */
553 static int percent_print(char *buf, int size, unsigned value)
555 return spaced_print(buf, size, "%u", pad_percents, value);
558 /* converts from bytes to human readable format (K, M, G, T)
560 * The algorithm always divides by 1024, as unit-conversion of byte
561 * counts suggests. But for output length determination we need to
562 * compare with 1000 here, as we print in decimal form. */
563 static void human_readable(long long num, char *buf, int size)
565 const char **suffix = suffixes;
571 /* Possibly just output as usual, for example for stdout usage */
572 if (!format_human_readable) {
573 spaced_print(buf, size, "%d", 6, round_to_int(num));
584 if (llabs(num) < 1000LL) {
585 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
589 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
597 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
599 * The goal is to always have a significance of 3, by
600 * adjusting the decimal part of the number. Sample output:
604 * so the point of alignment resides between number and unit. The
605 * upside of this is that there is minimal padding necessary, though
606 * there should be a way to make alignment take place at the decimal
607 * dot (then with fixed width decimal part).
609 * Note the repdigits below: when given a precision value, printf()
610 * rounds the float to it, not just cuts off the remaining digits. So
611 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
612 * printed with a precision of 0. Yay. */
614 precision = 0; /* print 100-999 without decimal part */
616 precision = 1; /* print 10-99 with one decimal place */
618 precision = 2; /* print 0-9 with two decimal places */
620 spaced_print(buf, size, format, width, precision, fnum, *suffix);
623 /* global object list root element */
624 static struct text_object global_root_object;
626 static inline void read_exec(const char *data, char *buf, const int size)
628 FILE *fp = popen(data, "r");
629 int length = fread(buf, 1, size, fp);
633 if (length > 0 && buf[length - 1] == '\n') {
634 buf[length - 1] = '\0';
638 void *threaded_exec(void *) __attribute__((noreturn));
640 void *threaded_exec(void *arg)
643 struct text_object *obj = (struct text_object *)arg;
646 buff = malloc(text_buffer_size);
647 read_exec(obj->data.texeci.cmd, buff,
656 timed_thread_lock(obj->data.texeci.p_timed_thread);
657 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
658 timed_thread_unlock(obj->data.texeci.p_timed_thread);
660 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
661 timed_thread_exit(obj->data.texeci.p_timed_thread);
667 static struct text_object *new_text_object_internal(void)
669 struct text_object *obj = malloc(sizeof(struct text_object));
670 memset(obj, 0, sizeof(struct text_object));
675 * Frees the list of text objects root points to. When internal = 1, it won't
676 * free global objects.
678 static void free_text_objects(struct text_object *root, int internal)
680 struct text_object *obj;
686 #define data obj->data
687 for (obj = root->prev; obj; obj = root->prev) {
688 root->prev = obj->prev;
694 #endif /* !__OpenBSD__ */
699 close(data.sysfs.fd);
701 #endif /* __linux__ */
703 free(data.read_tcp.host);
710 free(data.tztime.tz);
711 free(data.tztime.fmt);
714 free(data.mboxscan.args);
715 free(data.mboxscan.output);
720 case OBJ_unseen_mails:
721 case OBJ_flagged_mails:
722 case OBJ_unflagged_mails:
723 case OBJ_forwarded_mails:
724 case OBJ_unforwarded_mails:
725 case OBJ_replied_mails:
726 case OBJ_unreplied_mails:
727 case OBJ_draft_mails:
728 case OBJ_trashed_mails:
729 free(data.local_mail.box);
731 case OBJ_imap_unseen:
736 case OBJ_imap_messages:
741 case OBJ_pop3_unseen:
753 free_text_objects(obj->sub, 1);
756 case OBJ_if_existing:
759 free(data.ifblock.s);
760 free(data.ifblock.str);
763 free(data.tail.logfile);
764 free(data.tail.buffer);
780 case OBJ_iconv_start:
785 case OBJ_disk_protect:
789 free(data.ifblock.s);
790 free(data.ifblock.str);
793 if (info.gw_info.iface) {
794 free(info.gw_info.iface);
795 info.gw_info.iface = 0;
797 if (info.gw_info.ip) {
798 free(info.gw_info.ip);
802 case OBJ_ioscheduler:
807 #if (defined(__FreeBSD__) || defined(__linux__))
809 free(data.ifblock.s);
810 free(data.ifblock.str);
814 case OBJ_xmms2_artist:
815 if (info.xmms2.artist) {
816 free(info.xmms2.artist);
817 info.xmms2.artist = 0;
820 case OBJ_xmms2_album:
821 if (info.xmms2.album) {
822 free(info.xmms2.album);
823 info.xmms2.album = 0;
826 case OBJ_xmms2_title:
827 if (info.xmms2.title) {
828 free(info.xmms2.title);
829 info.xmms2.title = 0;
832 case OBJ_xmms2_genre:
833 if (info.xmms2.genre) {
834 free(info.xmms2.genre);
835 info.xmms2.genre = 0;
838 case OBJ_xmms2_comment:
839 if (info.xmms2.comment) {
840 free(info.xmms2.comment);
841 info.xmms2.comment = 0;
845 if (info.xmms2.url) {
846 free(info.xmms2.url);
851 if (info.xmms2.date) {
852 free(info.xmms2.date);
856 case OBJ_xmms2_status:
857 if (info.xmms2.status) {
858 free(info.xmms2.status);
859 info.xmms2.status = 0;
862 case OBJ_xmms2_playlist:
863 if (info.xmms2.playlist) {
864 free(info.xmms2.playlist);
865 info.xmms2.playlist = 0;
868 case OBJ_xmms2_smart:
869 if (info.xmms2.artist) {
870 free(info.xmms2.artist);
871 info.xmms2.artist = 0;
873 if (info.xmms2.title) {
874 free(info.xmms2.title);
875 info.xmms2.title = 0;
877 if (info.xmms2.url) {
878 free(info.xmms2.url);
885 case OBJ_bmpx_artist:
889 case OBJ_bmpx_bitrate:
899 free(data.rss.action);
904 free(data.weather.uri);
905 free(data.weather.data_type);
911 case OBJ_lua_read_parse:
919 #endif /* HAVE_LUA */
926 case OBJ_battery_short:
929 case OBJ_battery_time:
932 #endif /* !__OpenBSD__ */
940 free(data.execi.cmd);
941 free(data.execi.buffer);
944 free(data.texeci.cmd);
945 free(data.texeci.buffer);
956 if (info.first_process && !internal) {
957 free_all_processes();
958 info.first_process = NULL;
960 if (data.top.s) free(data.top.s);
964 free(data.hddtemp.dev);
965 free(data.hddtemp.addr);
966 if (data.hddtemp.temp)
967 free(data.hddtemp.temp);
970 case OBJ_entropy_avail:
971 case OBJ_entropy_perc:
972 case OBJ_entropy_poolsize:
973 case OBJ_entropy_bar:
976 if (info.users.names) {
977 free(info.users.names);
978 info.users.names = 0;
982 if (info.users.terms) {
983 free(info.users.terms);
984 info.users.terms = 0;
988 if (info.users.times) {
989 free(info.users.times);
990 info.users.times = 0;
995 case OBJ_smapi_bat_perc:
996 case OBJ_smapi_bat_temp:
997 case OBJ_smapi_bat_power:
1000 case OBJ_if_smapi_bat_installed:
1001 free(data.ifblock.s);
1002 free(data.ifblock.str);
1011 case OBJ_mpd_artist:
1013 case OBJ_mpd_random:
1014 case OBJ_mpd_repeat:
1016 case OBJ_mpd_bitrate:
1017 case OBJ_mpd_status:
1019 case OBJ_mpd_elapsed:
1020 case OBJ_mpd_length:
1024 case OBJ_mpd_percent:
1026 case OBJ_if_mpd_playing:
1034 case OBJ_moc_artist:
1037 case OBJ_moc_totaltime:
1038 case OBJ_moc_timeleft:
1039 case OBJ_moc_curtime:
1040 case OBJ_moc_bitrate:
1047 free_text_objects(obj->sub, 1);
1051 free(data.scroll.text);
1052 free_text_objects(obj->sub, 1);
1056 free(data.combine.left);
1057 free(data.combine.seperation);
1058 free(data.combine.right);
1059 free_text_objects(obj->sub, 1);
1064 case OBJ_apcupsd_name:
1065 case OBJ_apcupsd_model:
1066 case OBJ_apcupsd_upsmode:
1067 case OBJ_apcupsd_cable:
1068 case OBJ_apcupsd_status:
1069 case OBJ_apcupsd_linev:
1070 case OBJ_apcupsd_load:
1071 case OBJ_apcupsd_loadbar:
1073 case OBJ_apcupsd_loadgraph:
1074 case OBJ_apcupsd_loadgauge:
1076 case OBJ_apcupsd_charge:
1077 case OBJ_apcupsd_timeleft:
1078 case OBJ_apcupsd_temp:
1079 case OBJ_apcupsd_lastxfer:
1081 #endif /* APCUPSD */
1089 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1094 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1095 *a = mixer_init(buf1);
1096 scan_bar(arg + n, w, h);
1098 *a = mixer_init(NULL);
1099 scan_bar(arg, w, h);
1104 /* strip a leading /dev/ if any, following symlinks first
1106 * BEWARE: this function returns a pointer to static content
1107 * which gets overwritten in consecutive calls. I.e.:
1108 * this function is NOT reentrant.
1110 static const char *dev_name(const char *path)
1112 static char buf[255]; /* should be enough for pathnames */
1118 #define DEV_NAME(x) \
1119 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1120 if ((buflen = readlink(path, buf, 254)) == -1)
1121 return DEV_NAME(path);
1123 return DEV_NAME(buf);
1127 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1132 if (obj->data.top.was_parsed) {
1135 obj->data.top.was_parsed = 1;
1137 if (arg && !obj->data.top.s) {
1138 obj->data.top.s = strndup(arg, text_buffer_size);
1141 need_mask |= (1 << INFO_TOP);
1144 obj->type = OBJ_top;
1146 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1147 obj->type = OBJ_top_mem;
1149 } else if (strcmp(&s[3], "_time") == EQUAL) {
1150 obj->type = OBJ_top_time;
1153 } else if (strcmp(&s[3], "_io") == EQUAL) {
1154 obj->type = OBJ_top_io;
1159 ERR("Must be top, top_mem, top_time or top_io");
1161 ERR("Must be top, top_mem or top_time");
1167 ERR("top needs arguments");
1171 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1172 if (strcmp(buf, "name") == EQUAL) {
1173 obj->data.top.type = TOP_NAME;
1174 } else if (strcmp(buf, "cpu") == EQUAL) {
1175 obj->data.top.type = TOP_CPU;
1176 } else if (strcmp(buf, "pid") == EQUAL) {
1177 obj->data.top.type = TOP_PID;
1178 } else if (strcmp(buf, "mem") == EQUAL) {
1179 obj->data.top.type = TOP_MEM;
1180 } else if (strcmp(buf, "time") == EQUAL) {
1181 obj->data.top.type = TOP_TIME;
1182 } else if (strcmp(buf, "mem_res") == EQUAL) {
1183 obj->data.top.type = TOP_MEM_RES;
1184 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1185 obj->data.top.type = TOP_MEM_VSIZE;
1187 } else if (strcmp(buf, "io_read") == EQUAL) {
1188 obj->data.top.type = TOP_READ_BYTES;
1189 } else if (strcmp(buf, "io_write") == EQUAL) {
1190 obj->data.top.type = TOP_WRITE_BYTES;
1191 } else if (strcmp(buf, "io_perc") == EQUAL) {
1192 obj->data.top.type = TOP_IO_PERC;
1195 ERR("invalid type arg for top");
1197 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1198 "io_read, io_write, io_perc");
1200 ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1204 if (n < 1 || n > 10) {
1205 ERR("invalid num arg for top. Must be between 1 and 10.");
1208 obj->data.top.num = n - 1;
1211 ERR("invalid argument count for top");
1217 /* construct_text_object() creates a new text_object */
1218 static struct text_object *construct_text_object(const char *s,
1219 const char *arg, long line, char allow_threaded, void **ifblock_opaque)
1221 // struct text_object *obj = new_text_object();
1222 struct text_object *obj = new_text_object_internal();
1226 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1227 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1228 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1229 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1230 obj_be_ifblock_if(ifblock_opaque, obj); {
1231 #define OBJ_THREAD(a, n) if (strcmp(s, #a) == 0 && allow_threaded) { \
1232 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1233 #define END } } else
1235 #define SIZE_DEFAULTS(arg) { \
1236 obj->a = default_##arg##_width; \
1237 obj->b = default_##arg##_height; \
1242 obj->type = OBJ_color;
1243 obj->data.l = get_x11_color(s);
1247 OBJ(freq, INFO_FREQ)
1250 obj->data.i = open_acpi_temperature(arg);
1251 END OBJ(acpiacadapter, 0)
1252 END OBJ(freq, INFO_FREQ)
1253 #endif /* !__OpenBSD__ */
1255 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1256 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1257 obj->data.cpu_index = 1;
1258 /* ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1259 "Displaying the clock for CPU 1."); */
1261 obj->data.cpu_index = atoi(&arg[0]);
1264 END OBJ(freq_g, INFO_FREQ)
1266 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1267 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1268 obj->data.cpu_index = 1;
1269 /* ERR("freq_g: Invalid CPU number or you don't have that many "
1270 "CPUs! Displaying the clock for CPU 1."); */
1272 obj->data.cpu_index = atoi(&arg[0]);
1275 END OBJ(read_tcp, 0)
1277 obj->data.read_tcp.host = malloc(text_buffer_size);
1278 sscanf(arg, "%s", obj->data.read_tcp.host);
1279 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1280 if(obj->data.read_tcp.port == 0) {
1281 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1282 strcpy(obj->data.read_tcp.host,"localhost");
1284 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1285 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1286 CRIT_ERR("read_tcp: Needs \"(host) port\" as argument(s)");
1289 CRIT_ERR("read_tcp: Needs \"(host) port\" as argument(s)");
1291 #if defined(__linux__)
1292 END OBJ(voltage_mv, 0)
1294 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1295 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1296 obj->data.cpu_index = 1;
1297 /* ERR("voltage_mv: Invalid CPU number or you don't have that many "
1298 "CPUs! Displaying voltage for CPU 1."); */
1300 obj->data.cpu_index = atoi(&arg[0]);
1303 END OBJ(voltage_v, 0)
1305 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1306 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1307 obj->data.cpu_index = 1;
1308 /* ERR("voltage_v: Invalid CPU number or you don't have that many "
1309 "CPUs! Displaying voltage for CPU 1."); */
1311 obj->data.cpu_index = atoi(&arg[0]);
1316 END OBJ(wireless_essid, INFO_NET)
1318 obj->data.net = get_net_stat(arg);
1320 // default to DEFAULTNETDEV
1321 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1322 obj->data.net = get_net_stat(buf);
1325 END OBJ(wireless_mode, INFO_NET)
1327 obj->data.net = get_net_stat(arg);
1329 // default to DEFAULTNETDEV
1330 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1331 obj->data.net = get_net_stat(buf);
1334 END OBJ(wireless_bitrate, INFO_NET)
1336 obj->data.net = get_net_stat(arg);
1338 // default to DEFAULTNETDEV
1339 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1340 obj->data.net = get_net_stat(buf);
1343 END OBJ(wireless_ap, INFO_NET)
1345 obj->data.net = get_net_stat(arg);
1347 // default to DEFAULTNETDEV
1348 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1349 obj->data.net = get_net_stat(buf);
1352 END OBJ(wireless_link_qual, INFO_NET)
1354 obj->data.net = get_net_stat(arg);
1356 // default to DEFAULTNETDEV
1357 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1358 obj->data.net = get_net_stat(buf);
1361 END OBJ(wireless_link_qual_max, INFO_NET)
1363 obj->data.net = get_net_stat(arg);
1365 // default to DEFAULTNETDEV
1366 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1367 obj->data.net = get_net_stat(buf);
1370 END OBJ(wireless_link_qual_perc, INFO_NET)
1372 obj->data.net = get_net_stat(arg);
1374 // default to DEFAULTNETDEV
1375 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1376 obj->data.net = get_net_stat(buf);
1379 END OBJ(wireless_link_bar, INFO_NET)
1382 arg = scan_bar(arg, &obj->a, &obj->b);
1383 obj->data.net = get_net_stat(arg);
1385 // default to DEFAULTNETDEV
1386 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1387 obj->data.net = get_net_stat(buf);
1390 #endif /* HAVE_IWLIB */
1392 #endif /* __linux__ */
1400 sscanf(arg, "%63s", bat);
1402 strcpy(bat, "BAT0");
1404 obj->data.s = strndup(bat, text_buffer_size);
1405 END OBJ(battery_short, 0)
1409 sscanf(arg, "%63s", bat);
1411 strcpy(bat, "BAT0");
1413 obj->data.s = strndup(bat, text_buffer_size);
1414 END OBJ(battery_time, 0)
1418 sscanf(arg, "%63s", bat);
1420 strcpy(bat, "BAT0");
1422 obj->data.s = strndup(bat, text_buffer_size);
1423 END OBJ(battery_percent, 0)
1427 sscanf(arg, "%63s", bat);
1429 strcpy(bat, "BAT0");
1431 obj->data.s = strndup(bat, text_buffer_size);
1432 END OBJ(battery_bar, 0)
1437 arg = scan_bar(arg, &obj->a, &obj->b);
1438 sscanf(arg, "%63s", bat);
1440 strcpy(bat, "BAT0");
1442 obj->data.s = strndup(bat, text_buffer_size);
1443 #endif /* !__OpenBSD__ */
1445 #if defined(__linux__)
1446 END OBJ(disk_protect, 0)
1448 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1450 CRIT_ERR("disk_protect needs an argument");
1451 END OBJ(i8k_version, INFO_I8K)
1452 END OBJ(i8k_bios, INFO_I8K)
1453 END OBJ(i8k_serial, INFO_I8K)
1454 END OBJ(i8k_cpu_temp, INFO_I8K)
1455 END OBJ(i8k_left_fan_status, INFO_I8K)
1456 END OBJ(i8k_right_fan_status, INFO_I8K)
1457 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1458 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1459 END OBJ(i8k_ac_status, INFO_I8K)
1460 END OBJ(i8k_buttons_status, INFO_I8K)
1463 END OBJ(ibm_temps, 0)
1465 CRIT_ERR("ibm_temps: needs an argument");
1467 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1468 obj->data.sensor = 0;
1469 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1470 "Using 0 (CPU temp sensor).");
1472 obj->data.sensor = atoi(&arg[0]);
1473 END OBJ(ibm_volume, 0)
1474 END OBJ(ibm_brightness, 0)
1476 /* information from sony_laptop kernel module
1477 * /sys/devices/platform/sony-laptop */
1478 END OBJ(sony_fanspeed, 0)
1479 END OBJ_IF(if_gw, INFO_GW)
1480 END OBJ(ioscheduler, 0)
1482 CRIT_ERR("get_ioscheduler needs an argument (e.g. hda)");
1485 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1486 END OBJ(laptop_mode, 0)
1487 END OBJ(pb_battery, 0)
1488 if (arg && strcmp(arg, "status") == EQUAL) {
1489 obj->data.i = PB_BATT_STATUS;
1490 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1491 obj->data.i = PB_BATT_PERCENT;
1492 } else if (arg && strcmp(arg, "time") == EQUAL) {
1493 obj->data.i = PB_BATT_TIME;
1495 ERR("pb_battery: needs one argument: status, percent or time");
1500 #endif /* __linux__ */
1501 #if (defined(__FreeBSD__) || defined(__linux__))
1502 END OBJ_IF(if_up, 0)
1504 ERR("if_up needs an argument");
1505 obj->data.ifblock.s = 0;
1507 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1510 #if defined(__OpenBSD__)
1511 END OBJ(obsd_sensors_temp, 0)
1513 CRIT_ERR("obsd_sensors_temp: needs an argument");
1515 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1516 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1517 obj->data.sensor = 0;
1518 ERR("Invalid temperature sensor number!");
1520 obj->data.sensor = atoi(&arg[0]);
1521 END OBJ(obsd_sensors_fan, 0)
1523 CRIT_ERR("obsd_sensors_fan: needs 2 arguments (device and sensor "
1526 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1527 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1528 obj->data.sensor = 0;
1529 ERR("Invalid fan sensor number!");
1531 obj->data.sensor = atoi(&arg[0]);
1532 END OBJ(obsd_sensors_volt, 0)
1534 CRIT_ERR("obsd_sensors_volt: needs 2 arguments (device and sensor "
1537 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1538 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1539 obj->data.sensor = 0;
1540 ERR("Invalid voltage sensor number!");
1542 obj->data.sensor = atoi(&arg[0]);
1543 END OBJ(obsd_vendor, 0)
1544 END OBJ(obsd_product, 0)
1545 #endif /* __OpenBSD__ */
1546 END OBJ(buffers, INFO_BUFFERS)
1547 END OBJ(cached, INFO_BUFFERS)
1548 #define SCAN_CPU(__arg, __var) { \
1550 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1551 __arg += __offset; \
1555 END OBJ(cpu, INFO_CPU)
1556 SCAN_CPU(arg, obj->data.cpu_index);
1557 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1559 END OBJ(cpugauge, INFO_CPU)
1560 SIZE_DEFAULTS(gauge);
1561 SCAN_CPU(arg, obj->data.cpu_index);
1562 scan_gauge(arg, &obj->a, &obj->b);
1563 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1565 END OBJ(cpubar, INFO_CPU)
1567 SCAN_CPU(arg, obj->data.cpu_index);
1568 scan_bar(arg, &obj->a, &obj->b);
1569 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1571 END OBJ(cpugraph, INFO_CPU)
1573 SIZE_DEFAULTS(graph);
1574 SCAN_CPU(arg, obj->data.cpu_index);
1575 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1576 &obj->e, &obj->char_a, &obj->char_b);
1577 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1579 END OBJ(loadgraph, INFO_LOADAVG)
1581 SIZE_DEFAULTS(graph);
1582 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1583 &obj->e, &obj->char_a, &obj->char_b);
1587 r = sscanf(arg, "%d", &a);
1589 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1593 END OBJ(diskio, INFO_DISKIO)
1594 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1595 END OBJ(diskio_read, INFO_DISKIO)
1596 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1597 END OBJ(diskio_write, INFO_DISKIO)
1598 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1600 END OBJ(diskiograph, INFO_DISKIO)
1602 SIZE_DEFAULTS(graph);
1603 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1604 &obj->e, &obj->char_a, &obj->char_b);
1606 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1608 END OBJ(diskiograph_read, INFO_DISKIO)
1610 SIZE_DEFAULTS(graph);
1611 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1612 &obj->e, &obj->char_a, &obj->char_b);
1614 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1616 END OBJ(diskiograph_write, INFO_DISKIO)
1618 SIZE_DEFAULTS(graph);
1619 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1620 &obj->e, &obj->char_a, &obj->char_b);
1622 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1627 if (output_methods & TO_X) {
1628 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1632 obj->data.l = color0;
1634 obj->data.l = color1;
1636 obj->data.l = color2;
1638 obj->data.l = color3;
1640 obj->data.l = color4;
1642 obj->data.l = color5;
1644 obj->data.l = color6;
1646 obj->data.l = color7;
1648 obj->data.l = color8;
1650 obj->data.l = color9;
1653 obj->data.s = scan_font(arg);
1655 END OBJ(conky_version, 0)
1656 END OBJ(conky_build_date, 0)
1657 END OBJ(conky_build_arch, 0)
1658 END OBJ(downspeed, INFO_NET)
1660 obj->data.net = get_net_stat(arg);
1662 // default to DEFAULTNETDEV
1663 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1664 obj->data.net = get_net_stat(buf);
1667 END OBJ(downspeedf, INFO_NET)
1669 obj->data.net = get_net_stat(arg);
1671 // default to DEFAULTNETDEV
1672 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1673 obj->data.net = get_net_stat(buf);
1677 END OBJ(downspeedgraph, INFO_NET)
1679 SIZE_DEFAULTS(graph);
1680 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1681 &obj->e, &obj->char_a, &obj->char_b);
1683 // default to DEFAULTNETDEV
1684 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1685 obj->data.net = get_net_stat(buf);
1689 obj_be_ifblock_else(ifblock_opaque, obj);
1691 obj_be_ifblock_endif(ifblock_opaque, obj);
1693 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1695 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1698 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1700 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1703 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1705 END OBJ(execgauge, 0)
1706 SIZE_DEFAULTS(gauge);
1707 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1708 END OBJ(execgraph, 0)
1709 SIZE_DEFAULTS(graph);
1710 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1712 END OBJ(execibar, 0)
1716 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1719 ERR("${execibar <interval> command}");
1720 obj->type = OBJ_text;
1721 snprintf(buf, 256, "${%s}", s);
1722 obj->data.s = strndup(buf, text_buffer_size);
1724 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1727 END OBJ(execigraph, 0)
1729 SIZE_DEFAULTS(graph);
1731 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1734 ERR("${execigraph <interval> command}");
1735 obj->type = OBJ_text;
1736 snprintf(buf, 256, "${%s}", s);
1737 obj->data.s = strndup(buf, text_buffer_size);
1739 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1741 END OBJ(execigauge, 0)
1743 SIZE_DEFAULTS(gauge);
1745 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1748 ERR("${execigauge <interval> command}");
1749 obj->type = OBJ_text;
1750 snprintf(buf, 256, "${%s}", s);
1751 obj->data.s = strndup(buf, text_buffer_size);
1753 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1759 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1762 ERR("${execi <interval> command}");
1763 obj->type = OBJ_text;
1764 snprintf(buf, 256, "${%s}", s);
1765 obj->data.s = strndup(buf, text_buffer_size);
1767 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1768 obj->data.execi.buffer = malloc(text_buffer_size);
1773 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1776 ERR("${execi <interval> command}");
1777 obj->type = OBJ_text;
1778 snprintf(buf, 256, "${%s}", s);
1779 obj->data.s = strndup(buf, text_buffer_size);
1781 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1782 obj->data.execi.buffer = malloc(text_buffer_size);
1784 END OBJ_THREAD(texeci, 0)
1787 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1790 ERR("${texeci <interval> command}");
1791 obj->type = OBJ_text;
1792 snprintf(buf, 256, "${%s}", s);
1793 obj->data.s = strndup(buf, text_buffer_size);
1795 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1796 obj->data.texeci.buffer = malloc(text_buffer_size);
1798 obj->data.texeci.p_timed_thread = NULL;
1799 END OBJ(pre_exec, 0)
1800 obj->type = OBJ_text;
1804 read_exec(arg, buf, sizeof(buf));
1805 obj->data.s = strndup(buf, text_buffer_size);
1807 obj->data.s = strndup("", text_buffer_size);
1810 END OBJ(fs_bar, INFO_FS)
1812 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1814 while (isspace(*arg)) {
1823 obj->data.fsbar.fs = prepare_fs_stat(arg);
1824 END OBJ(fs_bar_free, INFO_FS)
1826 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1828 while (isspace(*arg)) {
1838 obj->data.fsbar.fs = prepare_fs_stat(arg);
1839 END OBJ(fs_free, INFO_FS)
1843 obj->data.fs = prepare_fs_stat(arg);
1844 END OBJ(fs_used_perc, INFO_FS)
1848 obj->data.fs = prepare_fs_stat(arg);
1849 END OBJ(fs_free_perc, INFO_FS)
1853 obj->data.fs = prepare_fs_stat(arg);
1854 END OBJ(fs_size, INFO_FS)
1858 obj->data.fs = prepare_fs_stat(arg);
1859 END OBJ(fs_type, INFO_FS)
1863 obj->data.fs = prepare_fs_stat(arg);
1864 END OBJ(fs_used, INFO_FS)
1868 obj->data.fs = prepare_fs_stat(arg);
1870 obj->data.i = arg ? atoi(arg) : 1;
1871 END OBJ(nameserver, INFO_DNS)
1872 obj->data.i = arg ? atoi(arg) : 0;
1874 obj->data.i = arg ? atoi(arg) : 1;
1876 obj->data.i = arg ? atoi(arg) : 1;
1880 ERR("goto needs arguments");
1881 obj->type = OBJ_text;
1882 obj->data.s = strndup("${goto}", text_buffer_size);
1886 obj->data.i = atoi(arg);
1892 if (sscanf(arg, "%d %d", &a, &b) != 2) {
1893 sscanf(arg, "%d", &b);
1899 obj->data.pair.a = a;
1900 obj->data.pair.b = b;
1903 END OBJ(i2c, INFO_SYSFS)
1904 char buf1[64], buf2[64];
1905 float factor, offset;
1909 ERR("i2c needs arguments");
1910 obj->type = OBJ_text;
1911 // obj->data.s = strndup("${i2c}", text_buffer_size);
1915 #define HWMON_RESET() {\
1920 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1921 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1922 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1923 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1926 ERR("i2c failed to parse arguments");
1927 obj->type = OBJ_text;
1930 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1931 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
1932 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1933 strncpy(obj->data.sysfs.type, buf2, 63);
1934 obj->data.sysfs.factor = factor;
1935 obj->data.sysfs.offset = offset;
1937 END OBJ(platform, INFO_SYSFS)
1938 char buf1[64], buf2[64];
1939 float factor, offset;
1943 ERR("platform needs arguments");
1944 obj->type = OBJ_text;
1948 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1949 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1950 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1951 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1954 ERR("platform failed to parse arguments");
1955 obj->type = OBJ_text;
1958 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1959 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
1960 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1961 strncpy(obj->data.sysfs.type, buf2, 63);
1962 obj->data.sysfs.factor = factor;
1963 obj->data.sysfs.offset = offset;
1965 END OBJ(hwmon, INFO_SYSFS)
1966 char buf1[64], buf2[64];
1967 float factor, offset;
1971 ERR("hwmon needs argumanets");
1972 obj->type = OBJ_text;
1976 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
1977 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
1978 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
1979 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
1984 ERR("hwmon failed to parse arguments");
1985 obj->type = OBJ_text;
1988 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
1989 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
1990 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
1991 strncpy(obj->data.sysfs.type, buf2, 63);
1992 obj->data.sysfs.factor = factor;
1993 obj->data.sysfs.offset = offset;
1995 #endif /* !__OpenBSD__ */
1998 /* we have four different types of top (top, top_mem, top_time and top_io). To
1999 * avoid having almost-same code four times, we have this special
2001 if (strncmp(s, "top", 3) == EQUAL) {
2002 if (!parse_top_args(s, arg, obj)) {
2005 } else OBJ(addr, INFO_NET)
2007 obj->data.net = get_net_stat(arg);
2009 // default to DEFAULTNETDEV
2010 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2011 obj->data.net = get_net_stat(buf);
2014 #if defined(__linux__)
2015 END OBJ(addrs, INFO_NET)
2017 obj->data.net = get_net_stat(arg);
2019 // default to DEFAULTNETDEV
2020 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2021 obj->data.net = get_net_stat(buf);
2024 #endif /* __linux__ */
2026 if (init_tail_object(obj, arg)) {
2027 obj->type = OBJ_text;
2028 obj->data.s = strndup("${tail}", text_buffer_size);
2031 if (init_head_object(obj, arg)) {
2032 obj->type = OBJ_text;
2033 obj->data.s = strndup("${head}", text_buffer_size);
2037 obj->data.s = strndup(arg, text_buffer_size);
2039 CRIT_ERR("lines needs a argument");
2043 obj->data.s = strndup(arg, text_buffer_size);
2045 CRIT_ERR("words needs a argument");
2047 END OBJ(loadavg, INFO_LOADAVG)
2048 int a = 1, b = 2, c = 3, r = 3;
2051 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2052 if (r >= 3 && (c < 1 || c > 3)) {
2055 if (r >= 2 && (b < 1 || b > 3)) {
2058 if (r >= 1 && (a < 1 || a > 3)) {
2062 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2063 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2064 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2065 END OBJ_IF(if_empty, 0)
2067 ERR("if_empty needs an argument");
2068 obj->data.ifblock.s = 0;
2070 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2071 obj->sub = malloc(sizeof(struct text_object));
2072 extract_variable_text_internal(obj->sub,
2073 obj->data.ifblock.s, 0);
2075 END OBJ_IF(if_match, 0)
2077 ERR("if_match needs arguments");
2078 obj->data.ifblock.s = 0;
2080 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2081 obj->sub = malloc(sizeof(struct text_object));
2082 extract_variable_text_internal(obj->sub,
2083 obj->data.ifblock.s, 0);
2085 END OBJ_IF(if_existing, 0)
2087 ERR("if_existing needs an argument or two");
2088 obj->data.ifblock.s = NULL;
2089 obj->data.ifblock.str = NULL;
2091 char buf1[256], buf2[256];
2092 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2095 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2096 obj->data.ifblock.str = NULL;
2098 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2099 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2102 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2103 END OBJ_IF(if_mounted, 0)
2105 ERR("if_mounted needs an argument");
2106 obj->data.ifblock.s = 0;
2108 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2111 END OBJ_IF(if_running, INFO_TOP)
2113 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2115 END OBJ_IF(if_running, 0)
2119 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2120 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2123 ERR("if_running needs an argument");
2124 obj->data.ifblock.s = 0;
2130 char box[256], dst[256];
2134 /* Kapil: Changed from MAIL_FILE to
2135 current_mail_spool since the latter
2136 is a copy of the former if undefined
2137 but the latter should take precedence
2139 strncpy(box, current_mail_spool, sizeof(box));
2141 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2143 strncpy(box, arg, sizeof(box));
2147 variable_substitute(box, dst, sizeof(dst));
2148 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2149 obj->data.local_mail.interval = n1;
2150 END OBJ(new_mails, 0)
2152 char box[256], dst[256];
2156 strncpy(box, current_mail_spool, sizeof(box));
2158 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2160 strncpy(box, arg, sizeof(box));
2164 variable_substitute(box, dst, sizeof(dst));
2165 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2166 obj->data.local_mail.interval = n1;
2167 END OBJ(seen_mails, 0)
2169 char box[256], dst[256];
2173 strncpy(box, current_mail_spool, sizeof(box));
2175 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2177 strncpy(box, arg, sizeof(box));
2181 variable_substitute(box, dst, sizeof(dst));
2182 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2183 obj->data.local_mail.interval = n1;
2184 END OBJ(unseen_mails, 0)
2186 char box[256], dst[256];
2190 strncpy(box, current_mail_spool, sizeof(box));
2192 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2194 strncpy(box, arg, sizeof(box));
2198 variable_substitute(box, dst, sizeof(dst));
2199 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2200 obj->data.local_mail.interval = n1;
2201 END OBJ(flagged_mails, 0)
2203 char box[256], dst[256];
2207 strncpy(box, current_mail_spool, sizeof(box));
2209 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2211 strncpy(box, arg, sizeof(box));
2215 variable_substitute(box, dst, sizeof(dst));
2216 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2217 obj->data.local_mail.interval = n1;
2218 END OBJ(unflagged_mails, 0)
2220 char box[256], dst[256];
2224 strncpy(box, current_mail_spool, sizeof(box));
2226 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2228 strncpy(box, arg, sizeof(box));
2232 variable_substitute(box, dst, sizeof(dst));
2233 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2234 obj->data.local_mail.interval = n1;
2235 END OBJ(forwarded_mails, 0)
2237 char box[256], dst[256];
2241 strncpy(box, current_mail_spool, sizeof(box));
2243 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2245 strncpy(box, arg, sizeof(box));
2249 variable_substitute(box, dst, sizeof(dst));
2250 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2251 obj->data.local_mail.interval = n1;
2252 END OBJ(unforwarded_mails, 0)
2254 char box[256], dst[256];
2258 strncpy(box, current_mail_spool, sizeof(box));
2260 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2262 strncpy(box, arg, sizeof(box));
2266 variable_substitute(box, dst, sizeof(dst));
2267 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2268 obj->data.local_mail.interval = n1;
2269 END OBJ(replied_mails, 0)
2271 char box[256], dst[256];
2275 strncpy(box, current_mail_spool, sizeof(box));
2277 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2279 strncpy(box, arg, sizeof(box));
2283 variable_substitute(box, dst, sizeof(dst));
2284 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2285 obj->data.local_mail.interval = n1;
2286 END OBJ(unreplied_mails, 0)
2288 char box[256], dst[256];
2292 strncpy(box, current_mail_spool, sizeof(box));
2294 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2296 strncpy(box, arg, sizeof(box));
2300 variable_substitute(box, dst, sizeof(dst));
2301 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2302 obj->data.local_mail.interval = n1;
2303 END OBJ(draft_mails, 0)
2305 char box[256], dst[256];
2309 strncpy(box, current_mail_spool, sizeof(box));
2311 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2313 strncpy(box, arg, sizeof(box));
2317 variable_substitute(box, dst, sizeof(dst));
2318 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2319 obj->data.local_mail.interval = n1;
2320 END OBJ(trashed_mails, 0)
2322 char box[256], dst[256];
2326 strncpy(box, current_mail_spool, sizeof(box));
2328 if (sscanf(arg, "%s %f", box, &n1) != 2) {
2330 strncpy(box, arg, sizeof(box));
2334 variable_substitute(box, dst, sizeof(dst));
2335 obj->data.local_mail.box = strndup(dst, text_buffer_size);
2336 obj->data.local_mail.interval = n1;
2337 END OBJ(mboxscan, 0)
2338 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2339 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2340 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2341 obj->data.mboxscan.output[0] = 1;
2342 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2343 END OBJ(mem, INFO_MEM)
2344 END OBJ(memeasyfree, INFO_MEM)
2345 END OBJ(memfree, INFO_MEM)
2346 END OBJ(memmax, INFO_MEM)
2347 END OBJ(memperc, INFO_MEM)
2349 END OBJ(memgauge, INFO_MEM)
2350 SIZE_DEFAULTS(gauge);
2351 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2353 END OBJ(membar, INFO_MEM)
2355 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2357 END OBJ(memgraph, INFO_MEM)
2359 SIZE_DEFAULTS(graph);
2360 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2361 &obj->e, &obj->char_a, &obj->char_b);
2365 END OBJ(mixer, INFO_MIXER)
2366 obj->data.l = mixer_init(arg);
2367 END OBJ(mixerl, INFO_MIXER)
2368 obj->data.l = mixer_init(arg);
2369 END OBJ(mixerr, INFO_MIXER)
2370 obj->data.l = mixer_init(arg);
2372 END OBJ(mixerbar, INFO_MIXER)
2374 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2375 &obj->data.mixerbar.h);
2376 END OBJ(mixerlbar, INFO_MIXER)
2378 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2379 &obj->data.mixerbar.h);
2380 END OBJ(mixerrbar, INFO_MIXER)
2382 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2383 &obj->data.mixerbar.h);
2385 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2386 obj->data.ifblock.i = mixer_init(arg);
2388 END OBJ(monitor, INFO_X11)
2389 END OBJ(monitor_number, INFO_X11)
2391 END OBJ(nodename, 0)
2392 END OBJ(processes, INFO_PROCS)
2393 END OBJ(running_processes, INFO_RUN_PROCS)
2394 END OBJ(shadecolor, 0)
2396 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2398 END OBJ(outlinecolor, 0)
2400 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2402 END OBJ(stippled_hr, 0)
2404 int a = stippled_borders, b = 1;
2407 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2408 sscanf(arg, "%d", &b);
2414 obj->data.pair.a = a;
2415 obj->data.pair.b = b;
2417 END OBJ(swap, INFO_MEM)
2418 END OBJ(swapmax, INFO_MEM)
2419 END OBJ(swapperc, INFO_MEM)
2420 END OBJ(swapbar, INFO_MEM)
2422 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2425 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2427 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2429 char buf1[256], buf2[256], *fmt, *tz;
2433 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2443 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2444 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2446 END OBJ(iconv_start, 0)
2447 if (iconv_converting) {
2448 CRIT_ERR("You must stop your last iconv conversion before "
2449 "starting another");
2452 char iconv_from[CODEPAGE_LENGTH];
2453 char iconv_to[CODEPAGE_LENGTH];
2455 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2456 CRIT_ERR("Invalid arguments for iconv_start");
2460 new_iconv = iconv_open(iconv_to, iconv_from);
2461 if (new_iconv == (iconv_t) (-1)) {
2462 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2464 obj->a = register_iconv(&new_iconv);
2465 iconv_converting = 1;
2469 CRIT_ERR("Iconv requires arguments");
2471 END OBJ(iconv_stop, 0)
2472 iconv_converting = 0;
2475 END OBJ(totaldown, INFO_NET)
2477 obj->data.net = get_net_stat(arg);
2479 // default to DEFAULTNETDEV
2480 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2481 obj->data.net = get_net_stat(buf);
2484 END OBJ(totalup, INFO_NET)
2485 obj->data.net = get_net_stat(arg);
2487 obj->data.net = get_net_stat(arg);
2489 // default to DEFAULTNETDEV
2490 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2491 obj->data.net = get_net_stat(buf);
2495 END OBJ_IF(if_updatenr, 0)
2496 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2497 if(obj->data.ifblock.i == 0) CRIT_ERR("if_updatenr needs a number above 0 as argument");
2498 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2500 obj->data.i = arg ? atoi(arg) : 0;
2502 obj->data.i = arg ? atoi(arg) : 0;
2503 END OBJ(upspeed, INFO_NET)
2505 obj->data.net = get_net_stat(arg);
2507 // default to DEFAULTNETDEV
2508 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2509 obj->data.net = get_net_stat(buf);
2512 END OBJ(upspeedf, INFO_NET)
2514 obj->data.net = get_net_stat(arg);
2516 // default to DEFAULTNETDEV
2517 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2518 obj->data.net = get_net_stat(buf);
2523 END OBJ(upspeedgraph, INFO_NET)
2525 SIZE_DEFAULTS(graph);
2526 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2527 &obj->e, &obj->char_a, &obj->char_b);
2529 // default to DEFAULTNETDEV
2530 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2531 obj->data.net = get_net_stat(buf);
2534 END OBJ(uptime_short, INFO_UPTIME)
2535 END OBJ(uptime, INFO_UPTIME)
2536 END OBJ(user_names, INFO_USERS)
2537 END OBJ(user_times, INFO_USERS)
2538 END OBJ(user_terms, INFO_USERS)
2539 END OBJ(user_number, INFO_USERS)
2540 #if defined(__linux__)
2541 END OBJ(gw_iface, INFO_GW)
2542 END OBJ(gw_ip, INFO_GW)
2543 #endif /* !__linux__ */
2545 END OBJ(adt746xcpu, 0)
2546 END OBJ(adt746xfan, 0)
2547 #endif /* !__OpenBSD__ */
2548 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2549 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2550 END OBJ(apm_adapter, 0)
2551 END OBJ(apm_battery_life, 0)
2552 END OBJ(apm_battery_time, 0)
2553 #endif /* __FreeBSD__ */
2554 END OBJ_THREAD(imap_unseen, 0)
2557 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2562 END OBJ_THREAD(imap_messages, 0)
2565 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2570 END OBJ_THREAD(pop3_unseen, 0)
2573 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2578 END OBJ_THREAD(pop3_used, 0)
2581 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2589 obj->data.s = strndup(arg, text_buffer_size);
2591 ERR("smapi needs an argument");
2592 END OBJ_IF(if_smapi_bat_installed, 0)
2594 ERR("if_smapi_bat_installed needs an argument");
2595 obj->data.ifblock.s = 0;
2597 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2598 END OBJ(smapi_bat_perc, 0)
2600 obj->data.s = strndup(arg, text_buffer_size);
2602 ERR("smapi_bat_perc needs an argument");
2603 END OBJ(smapi_bat_temp, 0)
2605 obj->data.s = strndup(arg, text_buffer_size);
2607 ERR("smapi_bat_temp needs an argument");
2608 END OBJ(smapi_bat_power, 0)
2610 obj->data.s = strndup(arg, text_buffer_size);
2612 ERR("smapi_bat_power needs an argument");
2614 END OBJ(smapi_bat_bar, 0)
2618 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2619 ERR("first argument to smapi_bat_bar must be an integer value");
2623 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2626 ERR("smapi_bat_bar needs an argument");
2630 #define mpd_set_maxlen(name) \
2633 sscanf(arg, "%d", &i); \
2635 obj->data.i = i + 1; \
2637 ERR(#name ": invalid length argument"); \
2639 END OBJ(mpd_artist, INFO_MPD)
2640 mpd_set_maxlen(mpd_artist);
2642 END OBJ(mpd_title, INFO_MPD)
2643 mpd_set_maxlen(mpd_title);
2645 END OBJ(mpd_random, INFO_MPD) init_mpd();
2646 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2647 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2648 END OBJ(mpd_length, INFO_MPD) init_mpd();
2649 END OBJ(mpd_track, INFO_MPD)
2650 mpd_set_maxlen(mpd_track);
2652 END OBJ(mpd_name, INFO_MPD)
2653 mpd_set_maxlen(mpd_name);
2655 END OBJ(mpd_file, INFO_MPD)
2656 mpd_set_maxlen(mpd_file);
2658 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2659 END OBJ(mpd_album, INFO_MPD)
2660 mpd_set_maxlen(mpd_album);
2662 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2663 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2664 END OBJ(mpd_status, INFO_MPD) init_mpd();
2665 END OBJ(mpd_bar, INFO_MPD)
2667 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2669 END OBJ(mpd_smart, INFO_MPD)
2670 mpd_set_maxlen(mpd_smart);
2672 END OBJ_IF(if_mpd_playing, INFO_MPD)
2674 #undef mpd_set_maxlen
2677 END OBJ(moc_state, INFO_MOC)
2678 END OBJ(moc_file, INFO_MOC)
2679 END OBJ(moc_title, INFO_MOC)
2680 END OBJ(moc_artist, INFO_MOC)
2681 END OBJ(moc_song, INFO_MOC)
2682 END OBJ(moc_album, INFO_MOC)
2683 END OBJ(moc_totaltime, INFO_MOC)
2684 END OBJ(moc_timeleft, INFO_MOC)
2685 END OBJ(moc_curtime, INFO_MOC)
2686 END OBJ(moc_bitrate, INFO_MOC)
2687 END OBJ(moc_rate, INFO_MOC)
2690 END OBJ(xmms2_artist, INFO_XMMS2)
2691 END OBJ(xmms2_album, INFO_XMMS2)
2692 END OBJ(xmms2_title, INFO_XMMS2)
2693 END OBJ(xmms2_genre, INFO_XMMS2)
2694 END OBJ(xmms2_comment, INFO_XMMS2)
2695 END OBJ(xmms2_url, INFO_XMMS2)
2696 END OBJ(xmms2_tracknr, INFO_XMMS2)
2697 END OBJ(xmms2_bitrate, INFO_XMMS2)
2698 END OBJ(xmms2_date, INFO_XMMS2)
2699 END OBJ(xmms2_id, INFO_XMMS2)
2700 END OBJ(xmms2_duration, INFO_XMMS2)
2701 END OBJ(xmms2_elapsed, INFO_XMMS2)
2702 END OBJ(xmms2_size, INFO_XMMS2)
2703 END OBJ(xmms2_status, INFO_XMMS2)
2704 END OBJ(xmms2_percent, INFO_XMMS2)
2706 END OBJ(xmms2_bar, INFO_XMMS2)
2708 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2710 END OBJ(xmms2_smart, INFO_XMMS2)
2711 END OBJ(xmms2_playlist, INFO_XMMS2)
2712 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2713 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2716 END OBJ(audacious_status, INFO_AUDACIOUS)
2717 END OBJ(audacious_title, INFO_AUDACIOUS)
2719 sscanf(arg, "%d", &info.audacious.max_title_len);
2720 if (info.audacious.max_title_len > 0) {
2721 info.audacious.max_title_len++;
2723 CRIT_ERR("audacious_title: invalid length argument");
2726 END OBJ(audacious_length, INFO_AUDACIOUS)
2727 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2728 END OBJ(audacious_position, INFO_AUDACIOUS)
2729 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2730 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2731 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2732 END OBJ(audacious_channels, INFO_AUDACIOUS)
2733 END OBJ(audacious_filename, INFO_AUDACIOUS)
2734 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2735 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2736 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2738 END OBJ(audacious_bar, INFO_AUDACIOUS)
2740 scan_bar(arg, &obj->a, &obj->b);
2744 END OBJ(bmpx_title, INFO_BMPX)
2745 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2746 END OBJ(bmpx_artist, INFO_BMPX)
2747 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2748 END OBJ(bmpx_album, INFO_BMPX)
2749 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2750 END OBJ(bmpx_track, INFO_BMPX)
2751 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2752 END OBJ(bmpx_uri, INFO_BMPX)
2753 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2754 END OBJ(bmpx_bitrate, INFO_BMPX)
2755 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2761 char *userid = (char *) malloc(20 * sizeof(char));
2762 char *apikey = (char *) malloc(64 * sizeof(char));
2763 char *charid = (char *) malloc(20 * sizeof(char));
2765 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2766 obj->data.eve.charid = charid;
2767 obj->data.eve.userid = userid;
2768 obj->data.eve.apikey = apikey;
2772 CRIT_ERR("eve needs arguments: <userid> <apikey> <characterid>");
2778 int argc, delay, act_par;
2779 unsigned int nrspaces = 0;
2780 char *uri = (char *) malloc(128 * sizeof(char));
2781 char *action = (char *) malloc(64 * sizeof(char));
2783 argc = sscanf(arg, "%127s %d %63s %d %u", uri, &delay, action,
2784 &act_par, &nrspaces);
2785 obj->data.rss.uri = uri;
2786 obj->data.rss.delay = delay;
2787 obj->data.rss.action = action;
2788 obj->data.rss.act_par = act_par;
2789 obj->data.rss.nrspaces = nrspaces;
2793 CRIT_ERR("rss needs arguments: <uri> <delay in minutes> <action> "
2794 "[act_par] [spaces in front]");
2801 char *icao = (char *) malloc(5 * sizeof(char));
2802 char *uri = (char *) malloc(128 * sizeof(char));
2803 char *data_type = (char *) malloc(32 * sizeof(char));
2806 argc = sscanf(arg, "%4s %31s %d", icao, data_type, &delay);
2808 //icao MUST BE upper-case
2811 *tmp_p = toupper(*tmp_p);
2815 strcpy(uri, "http://weather.noaa.gov/pub/data/observations/metar/stations/");
2817 strcat(uri, ".TXT");
2818 obj->data.weather.uri = uri;
2820 obj->data.weather.data_type = data_type;
2822 //The data retrieval interval is limited to half an hour
2826 obj->data.weather.delay = delay*60;
2828 init_weather_info();
2830 CRIT_ERR("weather needs arguments: <icao> <data_type> [delay in minutes]");
2836 obj->data.s = strndup(arg, text_buffer_size);
2838 CRIT_ERR("lua needs arguments: <function name> [function parameters]");
2840 END OBJ(lua_parse, 0)
2842 obj->data.s = strndup(arg, text_buffer_size);
2844 CRIT_ERR("lua_parse needs arguments: <function name> [function parameters]");
2846 END OBJ(lua_read_parse, 0)
2848 obj->data.s = strndup(arg, text_buffer_size);
2850 CRIT_ERR("lua_read_parse needs arguments: <function name> <string to pass>");
2855 arg = scan_bar(arg, &obj->a, &obj->b);
2857 obj->data.s = strndup(arg, text_buffer_size);
2859 CRIT_ERR("lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2862 CRIT_ERR("lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
2865 END OBJ(lua_graph, 0)
2866 SIZE_DEFAULTS(graph);
2869 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2870 &obj->e, &obj->char_a, &obj->char_b);
2874 CRIT_ERR("lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2877 CRIT_ERR("lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
2879 END OBJ(lua_gauge, 0)
2880 SIZE_DEFAULTS(gauge);
2882 arg = scan_gauge(arg, &obj->a, &obj->b);
2884 obj->data.s = strndup(arg, text_buffer_size);
2886 CRIT_ERR("lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2889 CRIT_ERR("lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
2892 #endif /* HAVE_LUA */
2895 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
2896 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2897 ERR("hddtemp needs arguments");
2898 obj->type = OBJ_text;
2899 obj->data.s = strndup("${hddtemp}", text_buffer_size);
2900 obj->data.hddtemp.update_time = 0;
2902 obj->data.hddtemp.temp = NULL;
2903 #endif /* HDDTEMP */
2904 #ifdef TCP_PORT_MONITOR
2905 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2906 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
2907 #endif /* TCP_PORT_MONITOR */
2908 END OBJ(entropy_avail, INFO_ENTROPY)
2909 END OBJ(entropy_perc, INFO_ENTROPY)
2910 END OBJ(entropy_poolsize, INFO_ENTROPY)
2911 END OBJ(entropy_bar, INFO_ENTROPY)
2913 scan_bar(arg, &obj->a, &obj->b);
2916 obj->sub = malloc(sizeof(struct text_object));
2917 extract_variable_text_internal(obj->sub, arg, 0);
2919 CRIT_ERR("blink needs a argument");
2921 END OBJ(to_bytes, 0)
2923 obj->sub = malloc(sizeof(struct text_object));
2924 extract_variable_text_internal(obj->sub, arg, 0);
2926 CRIT_ERR("to_bytes needs a argument");
2931 obj->data.scroll.step = 1;
2932 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
2933 if (sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2) > 0)
2935 obj->data.scroll.text = strndup(arg + n1, text_buffer_size);
2936 obj->data.scroll.start = 0;
2937 obj->sub = malloc(sizeof(struct text_object));
2938 extract_variable_text_internal(obj->sub,
2939 obj->data.scroll.text, 0);
2941 CRIT_ERR("scroll needs arguments: <length> [<step>] <text>");
2946 unsigned int indenting = 0; //vars can be used as args for other vars
2949 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
2951 for (i=0; arg[i] != 0 && j < 2; i++) {
2952 if(startvar[j] == -1) {
2956 }else if(endvar[j] == -1) {
2959 }else if(arg[i] == '}') {
2962 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
2968 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
2969 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
2970 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
2971 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
2973 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
2974 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
2976 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
2977 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
2979 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
2980 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
2982 obj->sub = malloc(sizeof(struct text_object));
2983 extract_variable_text_internal(obj->sub, obj->data.combine.left, 0);
2984 obj->sub->sub = malloc(sizeof(struct text_object));
2985 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right, 0);
2987 CRIT_ERR("combine needs arguments: <text1> <text2>");
2990 CRIT_ERR("combine needs arguments: <text1> <text2>");
2995 CRIT_ERR("nvidia needs an argument\n");
2996 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
2997 CRIT_ERR("nvidia: invalid argument"
2998 " specified: '%s'\n", arg);
3003 END OBJ(apcupsd, INFO_APCUPSD)
3007 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3008 CRIT_ERR("apcupsd needs arguments: <host> <port>");
3010 info.apcupsd.port = htons(port);
3011 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3014 CRIT_ERR("apcupsd needs arguments: <host> <port>");
3016 END OBJ(apcupsd_name, INFO_APCUPSD)
3017 END OBJ(apcupsd_model, INFO_APCUPSD)
3018 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3019 END OBJ(apcupsd_cable, INFO_APCUPSD)
3020 END OBJ(apcupsd_status, INFO_APCUPSD)
3021 END OBJ(apcupsd_linev, INFO_APCUPSD)
3022 END OBJ(apcupsd_load, INFO_APCUPSD)
3023 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3025 scan_bar(arg, &obj->a, &obj->b);
3027 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3029 SIZE_DEFAULTS(graph);
3030 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3031 &obj->e, &obj->char_a, &obj->char_b);
3033 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3034 SIZE_DEFAULTS(gauge);
3035 scan_gauge(arg, &obj->a, &obj->b);
3037 END OBJ(apcupsd_charge, INFO_APCUPSD)
3038 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3039 END OBJ(apcupsd_temp, INFO_APCUPSD)
3040 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3041 #endif /* APCUPSD */
3045 ERR("unknown variable %s", s);
3046 obj->type = OBJ_text;
3047 snprintf(buf, 256, "${%s}", s);
3048 obj->data.s = strndup(buf, text_buffer_size);
3055 static struct text_object *create_plain_text(const char *s)
3057 struct text_object *obj;
3059 if (s == NULL || *s == '\0') {
3063 obj = new_text_object_internal();
3065 obj->type = OBJ_text;
3066 obj->data.s = strndup(s, text_buffer_size);
3070 /* backslash_escape - do the actual substitution task for template objects
3072 * The field templates is used for substituting the \N occurences. Set it to
3073 * NULL to leave them as they are.
3075 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3079 unsigned int dup_idx = 0, dup_len;
3081 dup_len = strlen(src) + 1;
3082 src_dup = malloc(dup_len * sizeof(char));
3090 if (*(p + 1) == '\\') {
3091 src_dup[dup_idx++] = '\\';
3093 } else if (*(p + 1) == ' ') {
3094 src_dup[dup_idx++] = ' ';
3096 } else if (*(p + 1) == 'n') {
3097 src_dup[dup_idx++] = '\n';
3099 } else if (templates) {
3100 unsigned int tmpl_num;
3102 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3103 (tmpl_num > template_count))
3105 dup_len += strlen(templates[tmpl_num - 1]);
3106 src_dup = realloc(src_dup, dup_len * sizeof(char));
3107 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3108 dup_idx += strlen(templates[tmpl_num - 1]);
3113 src_dup[dup_idx++] = *p;
3118 src_dup[dup_idx] = '\0';
3119 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3123 /* handle_template_object - core logic of the template object
3125 * use config variables like this:
3126 * template1 = "$\1\2"
3127 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3129 * and use them like this:
3130 * ${template1 node name}
3131 * ${template2 root /}
3132 * ${template2 cdrom /mnt/cdrom}
3134 static char *handle_template(const char *tmpl, const char *args)
3136 char *args_dup = NULL;
3138 char **argsp = NULL;
3139 unsigned int argcnt = 0, template_idx, i;
3142 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3143 (template_idx >= MAX_TEMPLATES))
3147 args_dup = strdup(args);
3150 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3152 if (p > args_dup && *(p - 1) == '\\')
3155 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3161 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3162 argsp[argcnt - 1] = p_old;
3164 for (i = 0; i < argcnt; i++) {
3166 tmp = backslash_escape(argsp[i], NULL, 0);
3167 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3172 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3173 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3175 for (i = 0; i < argcnt; i++)
3181 static char *find_and_replace_templates(const char *inbuf)
3183 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3186 outlen = strlen(inbuf) + 1;
3187 o = outbuf = calloc(outlen, sizeof(char));
3188 memset(outbuf, 0, outlen * sizeof(char));
3190 p = indup = strdup(inbuf);
3192 while (*p && *p != '$')
3198 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3203 if (*(p + 1) == '{') {
3206 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3214 while (*p && stack > 0) {
3222 // stack is empty. that means the previous char was }, so we zero it
3225 // we ran into the end of string without finding a closing }, bark
3226 CRIT_ERR("cannot find a closing '}' in template expansion");
3230 while (*p && !isspace(*p))
3234 tmpl_out = handle_template(templ, args);
3236 outlen += strlen(tmpl_out);
3238 outbuf = realloc(outbuf, outlen * sizeof(char));
3239 strcat (outbuf, tmpl_out);
3241 o = outbuf + strlen(outbuf);
3243 ERR("failed to handle template '%s' with args '%s'", templ, args);
3247 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3252 static int text_contains_templates(const char *text)
3254 if (strcasestr(text, "${template") != NULL)
3256 if (strcasestr(text, "$template") != NULL)
3261 /* folds a string over top of itself, like so:
3263 * if start is "blah", and you call it with count = 1, the result will be "lah"
3265 static void strfold(char *start, int count)
3268 for (curplace = start + count; *curplace != 0; curplace++) {
3269 *(curplace - count) = *curplace;
3271 *(curplace - count) = 0;
3275 * - assumes that *string is '#'
3276 * - removes the part from '#' to the end of line ('\n' or '\0')
3277 * - it removes the '\n'
3278 * - copies the last char into 'char *last' argument, which should be a pointer
3279 * to a char rather than a string.
3281 static size_t remove_comment(char *string, char *last)
3284 while (*end != '\0' && *end != '\n') {
3287 if (last) *last = *end;
3288 if (*end == '\n') end++;
3289 strfold(string, end - string);
3290 return end - string;
3293 static size_t remove_comments(char *string)
3297 for (curplace = string; *curplace != 0; curplace++) {
3298 if (*curplace == '\\' && *(curplace + 1) == '#') {
3299 // strcpy can't be used for overlapping strings
3300 strfold(curplace, 1);
3302 } else if (*curplace == '#') {
3303 folded += remove_comment(curplace, 0);
3309 static int extract_variable_text_internal(struct text_object *retval, const char *const_p, char allow_threaded)
3311 struct text_object *obj;
3312 char *p, *s, *orig_p;
3314 void *ifblock_opaque = NULL;
3319 p = strndup(const_p, max_user_text - 1);
3320 while (text_contains_templates(p)) {
3322 tmp = find_and_replace_templates(p);
3328 if (strcmp(p, const_p)) {
3329 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3331 DBGP("no templates to replace");
3334 memset(retval, 0, sizeof(struct text_object));
3336 line = global_text_lines;
3344 obj = create_plain_text(s);
3346 append_object(retval, obj);
3356 /* variable is either $foo or ${foo} */
3358 unsigned int brl = 1, brr = 0;
3362 while (*p && brl != brr) {
3377 while (*p && (isalnum((int) *p) || *p == '_')) {
3382 /* copy variable to buffer */
3383 len = (p - s > 255) ? 255 : (p - s);
3384 strncpy(buf, s, len);
3392 /* search for variable in environment */
3396 obj = create_plain_text(var);
3398 append_object(retval, obj);
3403 /* if variable wasn't found in environment, use some special */
3408 if (strchr(buf, ' ')) {
3409 arg = strchr(buf, ' ');
3412 while (isspace((int) *arg)) {
3420 /* lowercase variable name */
3423 *tmp_p = tolower(*tmp_p);
3427 obj = construct_text_object(buf, arg,
3428 line, allow_threaded,
3431 append_object(retval, obj);
3435 obj = create_plain_text("$");
3438 append_object(retval, obj);
3441 } else if (*p == '\\' && *(p+1) == '#') {
3443 } else if (*p == '#') {
3445 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3446 /* if remove_comment removed a newline, we need to 'back up' with p */
3452 obj = create_plain_text(s);
3454 append_object(retval, obj);
3457 if (!ifblock_stack_empty(&ifblock_opaque)) {
3458 ERR("one or more $endif's are missing");
3465 static void extract_variable_text(const char *p)
3467 free_text_objects(&global_root_object, 0);
3481 extract_variable_text_internal(&global_root_object, p, 1);
3484 int parse_conky_vars(struct text_object *root, char *txt, char *p, struct information *cur)
3486 extract_variable_text_internal(root, txt, 0);
3487 generate_text_internal(p, max_user_text, *root, cur);
3491 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3492 void *thread(void *), const char *text)
3494 if (obj->char_b && info.mail) {
3495 // this means we use info
3496 if (!info.mail->p_timed_thread) {
3497 info.mail->p_timed_thread =
3498 timed_thread_create(thread,
3499 (void *) info.mail, info.mail->interval * 1000000);
3500 if (!info.mail->p_timed_thread) {
3501 ERR("Error creating %s timed thread", text);
3503 timed_thread_register(info.mail->p_timed_thread,
3504 &info.mail->p_timed_thread);
3505 if (timed_thread_run(info.mail->p_timed_thread)) {
3506 ERR("Error running %s timed thread", text);
3510 } else if (obj->data.mail) {
3511 // this means we use obj
3512 if (!obj->data.mail->p_timed_thread) {
3513 obj->data.mail->p_timed_thread =
3514 timed_thread_create(thread,
3515 (void *) obj->data.mail,
3516 obj->data.mail->interval * 1000000);
3517 if (!obj->data.mail->p_timed_thread) {
3518 ERR("Error creating %s timed thread", text);
3520 timed_thread_register(obj->data.mail->p_timed_thread,
3521 &obj->data.mail->p_timed_thread);
3522 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3523 ERR("Error running %s timed thread", text);
3526 return obj->data.mail;
3527 } else if (!obj->a) {
3528 // something is wrong, warn once then stop
3529 ERR("There's a problem with your mail settings. "
3530 "Check that the global mail settings are properly defined"
3531 " (line %li).", obj->line);
3537 char *format_time(unsigned long timeval, const int width)
3540 unsigned long nt; // narrow time, for speed on 32-bit
3541 unsigned cc; // centiseconds
3542 unsigned nn; // multi-purpose whatever
3545 cc = nt % 100; // centiseconds past second
3546 nt /= 100; // total seconds
3547 nn = nt % 60; // seconds past the minute
3548 nt /= 60; // total minutes
3549 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3551 return strndup(buf, text_buffer_size);
3553 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3554 return strndup(buf, text_buffer_size);
3556 nn = nt % 60; // minutes past the hour
3557 nt /= 60; // total hours
3558 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3559 return strndup(buf, text_buffer_size);
3561 nn = nt; // now also hours
3562 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3563 return strndup(buf, text_buffer_size);
3565 nn /= 24; // now days
3566 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3567 return strndup(buf, text_buffer_size);
3569 nn /= 7; // now weeks
3570 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3571 return strndup(buf, text_buffer_size);
3573 // well shoot, this outta' fit...
3574 return strndup("<inf>", text_buffer_size);
3577 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3578 //string has to end with \0 and it's length should fit in a int
3580 void remove_deleted_chars(char *string){
3582 while(string[i] != 0){
3583 if(string[i] == BACKSPACE){
3585 strcpy( &(string[i-1]), &(string[i+1]) );
3587 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3592 static inline void format_media_player_time(char *buf, const int size,
3595 int days, hours, minutes;
3597 days = seconds / (24 * 60 * 60);
3598 seconds %= (24 * 60 * 60);
3599 hours = seconds / (60 * 60);
3600 seconds %= (60 * 60);
3601 minutes = seconds / 60;
3605 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3606 hours, minutes, seconds);
3607 } else if (hours > 0) {
3608 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3611 snprintf(buf, size, "%i:%02i", minutes, seconds);
3615 static inline double get_barnum(char *buf)
3627 if (sscanf(buf, "%lf", &barnum) == 0) {
3628 ERR("reading exec value failed (perhaps it's not the "
3629 "correct format?)");
3632 if (barnum > 100.0 || barnum < 0.0) {
3633 ERR("your exec value is not between 0 and 100, "
3634 "therefore it will be ignored");
3640 static void generate_text_internal(char *p, int p_max_size,
3641 struct text_object root, struct information *cur)
3643 struct text_object *obj;
3645 int need_to_load_fonts = 0;
3648 /* for the OBJ_top* handler */
3649 struct process **needed = 0;
3652 char buff_in[p_max_size];
3654 iconv_converting = 0;
3655 #endif /* HAVE_ICONV */
3659 while (obj && p_max_size > 0) {
3660 needed = 0; // reset for top stuff
3662 /* IFBLOCK jumping algorithm
3664 * This is easier as it looks like:
3665 * - each IF checks it's condition
3666 * - on FALSE: call DO_JUMP
3667 * - on TRUE: don't care
3668 * - each ELSE calls DO_JUMP unconditionally
3669 * - each ENDIF is silently being ignored
3672 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3673 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3674 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3675 * regularly, "obj" is being updated to point to obj->next, so object parsing
3676 * continues right after the corresponding ELSE or ENDIF. This means that if we
3677 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3678 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3679 * jumped, and there is nothing to do.
3683 obj = obj->data.ifblock.next; \
3686 #define OBJ(a) break; case OBJ_##a:
3688 switch (obj->type) {
3690 ERR("not implemented obj type %d", obj->type);
3693 struct sockaddr_in addr;
3694 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3696 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3698 memset(&addr, 0, sizeof(addr));
3699 addr.sin_family = AF_INET;
3700 addr.sin_port = obj->data.read_tcp.port;
3701 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3702 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
3706 FD_SET(sock, &readfds);
3709 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
3710 received = recv(sock, p, p_max_size, 0);
3715 ERR("read_tcp: Couldn't create a connection");
3718 ERR("read_tcp: Couldn't create a socket");
3721 ERR("read_tcp: Problem with resolving the hostname");
3726 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
3728 #endif /* !__OpenBSD__ */
3731 obj->a = get_freq(p, p_max_size, "%.0f", 1,
3732 obj->data.cpu_index);
3738 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
3739 obj->data.cpu_index);
3741 /* OpenBSD has no such flag (SUSv2) */
3742 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
3743 obj->data.cpu_index);
3744 #endif /* __OpenBSD */
3747 #if defined(__linux__)
3750 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
3751 obj->data.cpu_index);
3756 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
3757 obj->data.cpu_index);
3762 OBJ(wireless_essid) {
3763 snprintf(p, p_max_size, "%s", obj->data.net->essid);
3765 OBJ(wireless_mode) {
3766 snprintf(p, p_max_size, "%s", obj->data.net->mode);
3768 OBJ(wireless_bitrate) {
3769 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
3772 snprintf(p, p_max_size, "%s", obj->data.net->ap);
3774 OBJ(wireless_link_qual) {
3775 spaced_print(p, p_max_size, "%d", 4,
3776 obj->data.net->link_qual);
3778 OBJ(wireless_link_qual_max) {
3779 spaced_print(p, p_max_size, "%d", 4,
3780 obj->data.net->link_qual_max);
3782 OBJ(wireless_link_qual_perc) {
3783 if (obj->data.net->link_qual_max > 0) {
3784 spaced_print(p, p_max_size, "%.0f", 5,
3785 (double) obj->data.net->link_qual /
3786 obj->data.net->link_qual_max * 100);
3788 spaced_print(p, p_max_size, "unk", 5);
3791 OBJ(wireless_link_bar) {
3793 if(output_methods & TO_X) {
3794 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
3795 obj->data.net->link_qual_max) * 255.0);
3798 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3799 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
3800 obj->data.net->link_qual_max) * 100.0, obj->a);
3805 #endif /* HAVE_IWLIB */
3807 #endif /* __linux__ */
3811 get_adt746x_cpu(p, p_max_size);
3814 get_adt746x_fan(p, p_max_size);
3817 get_acpi_fan(p, p_max_size);
3819 OBJ(acpiacadapter) {
3820 get_acpi_ac_adapter(p, p_max_size);
3823 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3826 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3828 OBJ(battery_percent) {
3829 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
3833 if(output_methods & TO_X) {
3834 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
3837 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3838 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
3843 OBJ(battery_short) {
3844 get_battery_short_status(p, p_max_size, obj->data.s);
3846 #endif /* __OpenBSD__ */
3849 human_readable(cur->buffers * 1024, p, 255);
3852 human_readable(cur->cached * 1024, p, 255);
3855 if (obj->data.cpu_index > info.cpu_count) {
3856 ERR("obj->data.cpu_index %i info.cpu_count %i",
3857 obj->data.cpu_index, info.cpu_count);
3858 CRIT_ERR("attempting to use more CPUs than you have!");
3860 percent_print(p, p_max_size,
3861 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
3865 new_gauge(p, obj->a, obj->b,
3866 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3870 if(output_methods & TO_X) {
3871 new_bar(p, obj->a, obj->b,
3872 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3875 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3876 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
3883 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3884 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
3885 100, 1, obj->char_a, obj->char_b);
3888 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
3889 obj->e, 1, obj->char_a, obj->char_b);
3892 new_fg(p, obj->data.l);
3925 OBJ(conky_version) {
3926 snprintf(p, p_max_size, "%s", VERSION);
3928 OBJ(conky_build_date) {
3929 snprintf(p, p_max_size, "%s", BUILD_DATE);
3931 OBJ(conky_build_arch) {
3932 snprintf(p, p_max_size, "%s", BUILD_ARCH);
3934 #if defined(__linux__)
3936 snprintf(p, p_max_size, "%s",
3937 get_disk_protect_queue(obj->data.s));
3940 snprintf(p, p_max_size, "%s", i8k.version);
3943 snprintf(p, p_max_size, "%s", i8k.bios);
3946 snprintf(p, p_max_size, "%s", i8k.serial);
3951 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3952 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
3954 OBJ(i8k_left_fan_status) {
3955 int left_fan_status;
3957 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3958 if (left_fan_status == 0) {
3959 snprintf(p, p_max_size, "off");
3961 if (left_fan_status == 1) {
3962 snprintf(p, p_max_size, "low");
3964 if (left_fan_status == 2) {
3965 snprintf(p, p_max_size, "high");
3968 OBJ(i8k_right_fan_status) {
3969 int right_fan_status;
3971 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3972 if (right_fan_status == 0) {
3973 snprintf(p, p_max_size, "off");
3975 if (right_fan_status == 1) {
3976 snprintf(p, p_max_size, "low");
3978 if (right_fan_status == 2) {
3979 snprintf(p, p_max_size, "high");
3982 OBJ(i8k_left_fan_rpm) {
3983 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3985 OBJ(i8k_right_fan_rpm) {
3986 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3988 OBJ(i8k_ac_status) {
3991 sscanf(i8k.ac_status, "%d", &ac_status);
3992 if (ac_status == -1) {
3993 snprintf(p, p_max_size, "disabled (read i8k docs)");
3995 if (ac_status == 0) {
3996 snprintf(p, p_max_size, "off");
3998 if (ac_status == 1) {
3999 snprintf(p, p_max_size, "on");
4002 OBJ(i8k_buttons_status) {
4003 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4007 get_ibm_acpi_fan(p, p_max_size);
4010 get_ibm_acpi_temps();
4011 temp_print(p, p_max_size,
4012 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4015 get_ibm_acpi_volume(p, p_max_size);
4017 OBJ(ibm_brightness) {
4018 get_ibm_acpi_brightness(p, p_max_size);
4021 /* information from sony_laptop kernel module
4022 * /sys/devices/platform/sony-laptop */
4023 OBJ(sony_fanspeed) {
4024 get_sony_fanspeed(p, p_max_size);
4027 if (!cur->gw_info.count) {
4032 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4035 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4038 snprintf(p, p_max_size, "%d", get_laptop_mode());
4041 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4043 #endif /* __linux__ */
4044 #if (defined(__FreeBSD__) || defined(__linux__))
4046 if ((obj->data.ifblock.s)
4047 && (!interface_up(obj->data.ifblock.s))) {
4053 OBJ(obsd_sensors_temp) {
4054 obsd_sensors.device = sensor_device;
4055 update_obsd_sensors();
4056 temp_print(p, p_max_size,
4057 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4060 OBJ(obsd_sensors_fan) {
4061 obsd_sensors.device = sensor_device;
4062 update_obsd_sensors();
4063 snprintf(p, p_max_size, "%d",
4064 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4066 OBJ(obsd_sensors_volt) {
4067 obsd_sensors.device = sensor_device;
4068 update_obsd_sensors();
4069 snprintf(p, p_max_size, "%.2f",
4070 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4073 get_obsd_vendor(p, p_max_size);
4076 get_obsd_product(p, p_max_size);
4078 #endif /* __OpenBSD__ */
4081 new_font(p, obj->data.s);
4082 need_to_load_fonts = 1;
4085 /* TODO: move this correction from kB to kB/s elsewhere
4086 * (or get rid of it??) */
4088 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4092 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4096 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4101 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4102 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4104 OBJ(diskiograph_read) {
4105 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4106 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4108 OBJ(diskiograph_write) {
4109 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4110 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4114 human_readable(obj->data.net->recv_speed, p, 255);
4117 spaced_print(p, p_max_size, "%.1f", 8,
4118 obj->data.net->recv_speed / 1024.0);
4121 OBJ(downspeedgraph) {
4122 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4123 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4127 /* Since we see you, you're if has not jumped.
4128 * Do Ninja jump here: without leaving traces.
4129 * This is to prevent us from stale jumped flags.
4131 obj = obj->data.ifblock.next;
4135 /* harmless object, just ignore */
4139 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4140 && (obj->data.net->addr.sa_data[3] & 255) == 0
4141 && (obj->data.net->addr.sa_data[4] & 255) == 0
4142 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4143 snprintf(p, p_max_size, "No Address");
4145 snprintf(p, p_max_size, "%u.%u.%u.%u",
4146 obj->data.net->addr.sa_data[2] & 255,
4147 obj->data.net->addr.sa_data[3] & 255,
4148 obj->data.net->addr.sa_data[4] & 255,
4149 obj->data.net->addr.sa_data[5] & 255);
4152 #if defined(__linux__)
4154 if(NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2)
4156 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4157 strcpy(p, obj->data.net->addrs);
4160 strcpy(p, "0.0.0.0");
4162 #endif /* __linux__ */
4163 #if defined(IMLIB2) && defined(X11)
4165 /* doesn't actually draw anything, just queues it omp. the
4166 * image will get drawn after the X event loop */
4167 cimlib_add_image(obj->data.s);
4171 struct information *tmp_info;
4172 struct text_object subroot, subroot2;
4174 tmp_info = malloc(sizeof(struct information));
4175 memcpy(tmp_info, cur, sizeof(struct information));
4176 parse_conky_vars(&subroot, obj->data.s, p, tmp_info);
4177 DBGP("evaluated '%s' to '%s'", obj->data.s, p);
4178 parse_conky_vars(&subroot2, p, p, tmp_info);
4180 free_text_objects(&subroot, 1);
4181 free_text_objects(&subroot2, 1);
4185 read_exec(obj->data.s, p, text_buffer_size);
4186 remove_deleted_chars(p);
4189 struct information *tmp_info;
4190 struct text_object subroot;
4192 read_exec(obj->data.s, p, text_buffer_size);
4194 tmp_info = malloc(sizeof(struct information));
4195 memcpy(tmp_info, cur, sizeof(struct information));
4196 parse_conky_vars(&subroot, p, p, tmp_info);
4198 free_text_objects(&subroot, 1);
4205 read_exec(obj->data.s, p, text_buffer_size);
4206 barnum = get_barnum(p); /*using the same function*/
4208 if (barnum >= 0.0) {
4210 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4217 read_exec(obj->data.s, p, text_buffer_size);
4218 barnum = get_barnum(p);
4220 if (barnum >= 0.0) {
4222 if(output_methods & TO_X) {
4224 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4227 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4228 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4236 char showaslog = FALSE;
4237 char tempgrad = FALSE;
4239 char *cmd = obj->data.s;
4241 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4243 cmd += strlen(" "TEMPGRAD);
4245 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4247 cmd += strlen(" "LOGGRAPH);
4249 read_exec(cmd, p, text_buffer_size);
4250 barnum = get_barnum(p);
4253 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4254 100, 1, showaslog, tempgrad);
4259 if (current_update_time - obj->data.execi.last_update
4260 >= obj->data.execi.interval) {
4263 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4264 barnum = get_barnum(p);
4266 if (barnum >= 0.0) {
4269 obj->data.execi.last_update = current_update_time;
4272 if(output_methods & TO_X) {
4273 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4276 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4277 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4284 if (current_update_time - obj->data.execi.last_update
4285 >= obj->data.execi.interval) {
4287 char showaslog = FALSE;
4288 char tempgrad = FALSE;
4289 char *cmd = obj->data.execi.cmd;
4291 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4293 cmd += strlen(" "TEMPGRAD);
4295 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4297 cmd += strlen(" "LOGGRAPH);
4299 obj->char_a = showaslog;
4300 obj->char_b = tempgrad;
4301 read_exec(cmd, p, text_buffer_size);
4302 barnum = get_barnum(p);
4304 if (barnum >= 0.0) {
4307 obj->data.execi.last_update = current_update_time;
4309 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4312 if (current_update_time - obj->data.execi.last_update
4313 >= obj->data.execi.interval) {
4316 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4317 barnum = get_barnum(p);
4319 if (barnum >= 0.0) {
4320 obj->f = 255 * barnum / 100.0;
4322 obj->data.execi.last_update = current_update_time;
4324 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4328 if (current_update_time - obj->data.execi.last_update
4329 >= obj->data.execi.interval
4330 && obj->data.execi.interval != 0) {
4331 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4333 obj->data.execi.last_update = current_update_time;
4335 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4338 struct text_object subroot;
4339 struct information *tmp_info =
4340 malloc(sizeof(struct information));
4341 memcpy(tmp_info, cur, sizeof(struct information));
4343 if (current_update_time - obj->data.execi.last_update
4344 < obj->data.execi.interval
4345 || obj->data.execi.interval == 0) {
4346 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4348 char *output = obj->data.execi.buffer;
4349 FILE *fp = popen(obj->data.execi.cmd, "r");
4350 int length = fread(output, 1, text_buffer_size, fp);
4354 output[length] = '\0';
4355 if (length > 0 && output[length - 1] == '\n') {
4356 output[length - 1] = '\0';
4359 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4360 obj->data.execi.last_update = current_update_time;
4362 free_text_objects(&subroot, 1);
4366 if (!obj->data.texeci.p_timed_thread) {
4367 obj->data.texeci.p_timed_thread =
4368 timed_thread_create(&threaded_exec,
4369 (void *) obj, obj->data.texeci.interval * 1000000);
4370 if (!obj->data.texeci.p_timed_thread) {
4371 ERR("Error creating texeci timed thread");
4373 timed_thread_register(obj->data.texeci.p_timed_thread,
4374 &obj->data.texeci.p_timed_thread);
4375 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4376 ERR("Error running texeci timed thread");
4379 timed_thread_lock(obj->data.texeci.p_timed_thread);
4380 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4381 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4384 #endif /* HAVE_POPEN */
4386 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4388 if (mail && mail->p_timed_thread) {
4389 timed_thread_lock(mail->p_timed_thread);
4390 snprintf(p, p_max_size, "%lu", mail->unseen);
4391 timed_thread_unlock(mail->p_timed_thread);
4394 OBJ(imap_messages) {
4395 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4397 if (mail && mail->p_timed_thread) {
4398 timed_thread_lock(mail->p_timed_thread);
4399 snprintf(p, p_max_size, "%lu", mail->messages);
4400 timed_thread_unlock(mail->p_timed_thread);
4404 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4406 if (mail && mail->p_timed_thread) {
4407 timed_thread_lock(mail->p_timed_thread);
4408 snprintf(p, p_max_size, "%lu", mail->unseen);
4409 timed_thread_unlock(mail->p_timed_thread);
4413 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4415 if (mail && mail->p_timed_thread) {
4416 timed_thread_lock(mail->p_timed_thread);
4417 snprintf(p, p_max_size, "%.1f",
4418 mail->used / 1024.0 / 1024.0);
4419 timed_thread_unlock(mail->p_timed_thread);
4423 if (obj->data.fs != NULL) {
4424 if (obj->data.fs->size == 0) {
4426 if(output_methods & TO_X) {
4427 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4430 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4431 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4437 if(output_methods & TO_X) {
4438 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4439 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4440 obj->data.fs->size));
4443 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4444 new_bar_in_shell(p, p_max_size,
4445 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4453 if (obj->data.fs != NULL) {
4454 human_readable(obj->data.fs->avail, p, 255);
4458 if (obj->data.fs != NULL) {
4461 if (obj->data.fs->size) {
4462 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4465 percent_print(p, p_max_size, val);
4469 if (obj->data.fs != NULL) {
4470 human_readable(obj->data.fs->size, p, 255);
4474 if (obj->data.fs != NULL)
4475 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4478 if (obj->data.fs != NULL) {
4479 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4484 if (obj->data.fs != NULL) {
4485 if (obj->data.fs->size == 0) {
4487 if(output_methods & TO_X) {
4488 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4491 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4492 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4498 if(output_methods & TO_X) {
4499 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4500 (int) (obj->data.fsbar.fs->avail * 255 /
4501 obj->data.fs->size));
4504 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4505 new_bar_in_shell(p, p_max_size,
4506 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4514 if (obj->data.fs != NULL) {
4517 if (obj->data.fs->size) {
4518 val = obj->data.fs->free
4523 percent_print(p, p_max_size, 100 - val);
4527 float *v = info.loadavg;
4529 if (obj->data.loadavg[2]) {
4530 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4531 v[obj->data.loadavg[0] - 1],
4532 v[obj->data.loadavg[1] - 1],
4533 v[obj->data.loadavg[2] - 1]);
4534 } else if (obj->data.loadavg[1]) {
4535 snprintf(p, p_max_size, "%.2f %.2f",
4536 v[obj->data.loadavg[0] - 1],
4537 v[obj->data.loadavg[1] - 1]);
4538 } else if (obj->data.loadavg[0]) {
4539 snprintf(p, p_max_size, "%.2f",
4540 v[obj->data.loadavg[0] - 1]);
4544 new_goto(p, obj->data.i);
4547 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4551 new_hr(p, obj->data.i);
4555 if (cur->nameserver_info.nscount > obj->data.i)
4556 snprintf(p, p_max_size, "%s",
4557 cur->nameserver_info.ns_list[obj->data.i]);
4561 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4562 snprintf(p, p_max_size, "%s", skill);
4567 PRSS *data = get_rss_info(obj->data.rss.uri,
4568 obj->data.rss.delay);
4572 snprintf(p, p_max_size, "prss: Error reading RSS data\n");
4574 if (strcmp(obj->data.rss.action, "feed_title") == EQUAL) {
4576 // remove trailing new line if one exists
4577 if (str[strlen(str) - 1] == '\n') {
4578 str[strlen(str) - 1] = 0;
4580 snprintf(p, p_max_size, "%s", str);
4581 } else if (strcmp(obj->data.rss.action, "item_title") == EQUAL) {
4582 if (obj->data.rss.act_par < data->item_count) {
4583 str = data->items[obj->data.rss.act_par].title;
4584 // remove trailing new line if one exists
4585 if (str[strlen(str) - 1] == '\n') {
4586 str[strlen(str) - 1] = 0;
4588 snprintf(p, p_max_size, "%s", str);
4590 } else if (strcmp(obj->data.rss.action, "item_desc") == EQUAL) {
4591 if (obj->data.rss.act_par < data->item_count) {
4593 data->items[obj->data.rss.act_par].description;
4594 // remove trailing new line if one exists
4595 if (str[strlen(str) - 1] == '\n') {
4596 str[strlen(str) - 1] = 0;
4598 snprintf(p, p_max_size, "%s", str);
4600 } else if (strcmp(obj->data.rss.action, "item_titles") == EQUAL) {
4601 if (data->item_count > 0) {
4604 //'tmpspaces' is a string with spaces too be placed in front of each title
4605 char *tmpspaces = malloc(obj->data.rss.nrspaces + 1);
4606 memset(tmpspaces, ' ', obj->data.rss.nrspaces);
4607 tmpspaces[obj->data.rss.nrspaces]=0;
4611 if (obj->data.rss.act_par > data->item_count) {
4612 show = data->item_count;
4614 show = obj->data.rss.act_par;
4616 for (itmp = 0; itmp < show; itmp++) {
4617 PRSS_Item *item = &data->items[itmp];
4621 // don't add new line before first item
4623 strncat(p, "\n", p_max_size);
4625 /* remove trailing new line if one exists,
4626 * we have our own */
4627 if (str[strlen(str) - 1] == '\n') {
4628 str[strlen(str) - 1] = 0;
4630 strncat(p, tmpspaces, p_max_size);
4631 strncat(p, str, p_max_size);
4642 PWEATHER *data = get_weather_info(obj->data.weather.uri, obj->data.weather.delay);
4644 static const char *wc[18] =
4645 {"", "drizzle", "rain", "hail", "soft hail",
4646 "snow", "snow grains", "fog", "haze", "smoke",
4647 "mist", "dust", "sand", "funnel cloud tornado",
4648 "dust/sand", "squall", "sand storm", "dust storm"};
4652 strncpy(p, "Error reading weather data", p_max_size);
4654 if (strcmp(obj->data.weather.data_type, "last_update") == EQUAL) {
4655 strncpy(p, data->lastupd, p_max_size);
4656 } else if (strcmp(obj->data.weather.data_type, "temperature_C") == EQUAL) {
4657 snprintf(p, p_max_size, "%d", data->tmpC);
4658 } else if (strcmp(obj->data.weather.data_type, "temperature_F") == EQUAL) {
4659 snprintf(p, p_max_size, "%d", data->tmpF);
4660 } else if (strcmp(obj->data.weather.data_type, "cloud_cover") == EQUAL) {
4661 if (data->cc == 0) {
4662 strncpy(p, "", p_max_size);
4663 } else if (data->cc < 3) {
4664 strncpy(p, "clear", p_max_size);
4665 } else if (data->cc < 5) {
4666 strncpy(p, "partly cloudy", p_max_size);
4667 } else if (data->cc == 5) {
4668 strncpy(p, "cloudy", p_max_size);
4669 } else if (data->cc == 6) {
4670 strncpy(p, "overcast", p_max_size);
4671 } else if (data->cc == 7) {
4672 strncpy(p, "towering cumulus", p_max_size);
4674 strncpy(p, "cumulonimbus", p_max_size);
4676 } else if (strcmp(obj->data.weather.data_type, "pressure") == EQUAL) {
4677 snprintf(p, p_max_size, "%d", data->bar);
4678 } else if (strcmp(obj->data.weather.data_type, "wind_speed") == EQUAL) {
4679 snprintf(p, p_max_size, "%d", data->wind_s);
4680 } else if (strcmp(obj->data.weather.data_type, "wind_dir") == EQUAL) {
4681 if ((data->wind_d >= 349) || (data->wind_d < 12)) {
4682 strncpy(p, "N", p_max_size);
4683 } else if (data->wind_d < 33) {
4684 strncpy(p, "NNE", p_max_size);
4685 } else if (data->wind_d < 57) {
4686 strncpy(p, "NE", p_max_size);
4687 } else if (data->wind_d < 79) {
4688 strncpy(p, "ENE", p_max_size);
4689 } else if (data->wind_d < 102) {
4690 strncpy(p, "E", p_max_size);
4691 } else if (data->wind_d < 124) {
4692 strncpy(p, "ESE", p_max_size);
4693 } else if (data->wind_d < 147) {
4694 strncpy(p, "SE", p_max_size);
4695 } else if (data->wind_d < 169) {
4696 strncpy(p, "SSE", p_max_size);
4697 } else if (data->wind_d < 192) {
4698 strncpy(p, "S", p_max_size);
4699 } else if (data->wind_d < 214) {
4700 strncpy(p, "SSW", p_max_size);
4701 } else if (data->wind_d < 237) {
4702 strncpy(p, "SW", p_max_size);
4703 } else if (data->wind_d < 259) {
4704 strncpy(p, "WSW", p_max_size);
4705 } else if (data->wind_d < 282) {
4706 strncpy(p, "W", p_max_size);
4707 } else if (data->wind_d < 304) {
4708 strncpy(p, "WNW", p_max_size);
4709 } else if (data->wind_d < 327) {
4710 strncpy(p, "NW", p_max_size);
4711 } else if (data->wind_d < 349) {
4712 strncpy(p, "NNW", p_max_size);
4714 } else if (strcmp(obj->data.weather.data_type, "wind_dir_DEG") == EQUAL) {
4715 snprintf(p, p_max_size, "%d", data->wind_d);
4717 } else if (strcmp(obj->data.weather.data_type, "humidity") == EQUAL) {
4718 snprintf(p, p_max_size, "%d", data->hmid);
4719 } else if (strcmp(obj->data.weather.data_type, "weather") == EQUAL) {
4720 strncpy(p, wc[data->wc], p_max_size);
4727 char *str = llua_getstring(obj->data.s);
4729 snprintf(p, p_max_size, "%s", str);
4734 char *str = llua_getstring(obj->data.s);
4736 struct information *tmp_info;
4737 struct text_object subroot;
4739 tmp_info = malloc(sizeof(struct information));
4740 memcpy(tmp_info, cur, sizeof(struct information));
4741 parse_conky_vars(&subroot, str, p, tmp_info);
4743 free_text_objects(&subroot, 1);
4748 OBJ(lua_read_parse) {
4749 struct information *tmp_info;
4750 struct text_object subroot, subroot2;
4753 sscanf(obj->data.s, "%64s", func);
4754 text = obj->data.s + strlen(func) + 1;
4756 tmp_info = malloc(sizeof(struct information));
4757 memcpy(tmp_info, cur, sizeof(struct information));
4758 parse_conky_vars(&subroot, text, p, tmp_info);
4759 DBGP("evaluated '%s' to '%s'", text, p);
4761 str = llua_getstring_read(func, p);
4763 parse_conky_vars(&subroot2, str, p, tmp_info);
4764 DBGP("evaluated '%s' to '%s'", str, p);
4767 free_text_objects(&subroot2, 1);
4769 free_text_objects(&subroot, 1);
4774 if (llua_getnumber(obj->data.s, &per)) {
4776 if(output_methods & TO_X) {
4777 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4780 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4781 new_bar_in_shell(p, p_max_size, per, obj->a);
4790 if (llua_getnumber(obj->data.s, &per)) {
4791 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4792 per, obj->e, 1, obj->char_a, obj->char_b);
4797 if (llua_getnumber(obj->data.s, &per)) {
4798 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4802 #endif /* HAVE_LUA */
4807 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4808 if (obj->data.hddtemp.temp)
4809 free(obj->data.hddtemp.temp);
4810 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4811 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4812 obj->data.hddtemp.update_time = current_update_time;
4814 if (!obj->data.hddtemp.temp) {
4815 snprintf(p, p_max_size, "N/A");
4817 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4818 unit = obj->data.hddtemp.temp[0];
4820 if (*endptr != '\0')
4821 snprintf(p, p_max_size, "N/A");
4822 else if (unit == 'C')
4823 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4824 else if (unit == 'F')
4825 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4827 snprintf(p, p_max_size, "N/A");
4832 new_offset(p, obj->data.i);
4835 new_voffset(p, obj->data.i);
4841 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4842 obj->data.sysfs.devtype, obj->data.sysfs.type);
4844 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4846 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4847 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4848 } else if (r >= 100.0 || r == 0) {
4849 snprintf(p, p_max_size, "%d", (int) r);
4851 snprintf(p, p_max_size, "%.1f", r);
4857 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4858 obj->data.sysfs.devtype, obj->data.sysfs.type);
4860 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4862 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4863 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4864 } else if (r >= 100.0 || r == 0) {
4865 snprintf(p, p_max_size, "%d", (int) r);
4867 snprintf(p, p_max_size, "%.1f", r);
4873 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4874 obj->data.sysfs.devtype, obj->data.sysfs.type);
4876 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4878 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4879 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4880 } else if (r >= 100.0 || r == 0) {
4881 snprintf(p, p_max_size, "%d", (int) r);
4883 snprintf(p, p_max_size, "%.1f", r);
4886 #endif /* __linux__ */
4888 new_alignr(p, obj->data.i);
4891 new_alignc(p, obj->data.i);
4894 char buf[max_user_text];
4895 struct information *tmp_info =
4896 malloc(sizeof(struct information));
4897 memcpy(tmp_info, cur, sizeof(struct information));
4898 generate_text_internal(buf, max_user_text,
4899 *obj->sub, tmp_info);
4901 if (strlen(buf) != 0) {
4907 char expression[max_user_text];
4909 struct information *tmp_info;
4911 tmp_info = malloc(sizeof(struct information));
4912 memcpy(tmp_info, cur, sizeof(struct information));
4913 generate_text_internal(expression, max_user_text,
4914 *obj->sub, tmp_info);
4915 DBGP("parsed arg into '%s'", expression);
4917 val = compare(expression);
4919 ERR("compare failed for expression '%s'",
4927 if (obj->data.ifblock.str
4928 && !check_contains(obj->data.ifblock.s,
4929 obj->data.ifblock.str)) {
4931 } else if (obj->data.ifblock.s
4932 && access(obj->data.ifblock.s, F_OK)) {
4937 if ((obj->data.ifblock.s)
4938 && (!check_mount(obj->data.ifblock.s))) {
4944 if (!get_process_by_name(obj->data.ifblock.s)) {
4946 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
4951 #if defined(__linux__)
4953 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
4957 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4960 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4965 human_readable(cur->mem * 1024, p, 255);
4968 human_readable(cur->memeasyfree * 1024, p, 255);
4971 human_readable(cur->memfree * 1024, p, 255);
4974 human_readable(cur->memmax * 1024, p, 255);
4978 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
4982 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
4983 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4988 if(output_methods & TO_X) {
4989 new_bar(p, obj->data.pair.a, obj->data.pair.b,
4990 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
4993 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
4994 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
5001 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5002 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5003 100, 1, obj->char_a, obj->char_b);
5008 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
5011 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
5014 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
5018 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5019 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
5022 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5023 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
5026 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5027 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
5030 OBJ(if_mixer_mute) {
5031 if (!mixer_is_mute(obj->data.ifblock.i)) {
5037 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5039 OBJ(monitor_number) {
5040 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5046 update_mail_count(&obj->data.local_mail);
5047 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5050 update_mail_count(&obj->data.local_mail);
5051 snprintf(p, p_max_size, "%d",
5052 obj->data.local_mail.new_mail_count);
5055 update_mail_count(&obj->data.local_mail);
5056 snprintf(p, p_max_size, "%d",
5057 obj->data.local_mail.seen_mail_count);
5060 update_mail_count(&obj->data.local_mail);
5061 snprintf(p, p_max_size, "%d",
5062 obj->data.local_mail.unseen_mail_count);
5064 OBJ(flagged_mails) {
5065 update_mail_count(&obj->data.local_mail);
5066 snprintf(p, p_max_size, "%d",
5067 obj->data.local_mail.flagged_mail_count);
5069 OBJ(unflagged_mails) {
5070 update_mail_count(&obj->data.local_mail);
5071 snprintf(p, p_max_size, "%d",
5072 obj->data.local_mail.unflagged_mail_count);
5074 OBJ(forwarded_mails) {
5075 update_mail_count(&obj->data.local_mail);
5076 snprintf(p, p_max_size, "%d",
5077 obj->data.local_mail.forwarded_mail_count);
5079 OBJ(unforwarded_mails) {
5080 update_mail_count(&obj->data.local_mail);
5081 snprintf(p, p_max_size, "%d",
5082 obj->data.local_mail.unforwarded_mail_count);
5084 OBJ(replied_mails) {
5085 update_mail_count(&obj->data.local_mail);
5086 snprintf(p, p_max_size, "%d",
5087 obj->data.local_mail.replied_mail_count);
5089 OBJ(unreplied_mails) {
5090 update_mail_count(&obj->data.local_mail);
5091 snprintf(p, p_max_size, "%d",
5092 obj->data.local_mail.unreplied_mail_count);
5095 update_mail_count(&obj->data.local_mail);
5096 snprintf(p, p_max_size, "%d",
5097 obj->data.local_mail.draft_mail_count);
5099 OBJ(trashed_mails) {
5100 update_mail_count(&obj->data.local_mail);
5101 snprintf(p, p_max_size, "%d",
5102 obj->data.local_mail.trashed_mail_count);
5105 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5107 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5110 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5113 new_outline(p, obj->data.l);
5116 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5118 OBJ(running_processes) {
5119 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5122 snprintf(p, p_max_size, "%s", obj->data.s);
5126 new_bg(p, obj->data.l);
5129 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5133 human_readable(cur->swap * 1024, p, 255);
5136 human_readable(cur->swapmax * 1024, p, 255);
5139 if (cur->swapmax == 0) {
5140 strncpy(p, "No swap", p_max_size);
5142 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5147 if(output_methods & TO_X) {
5148 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5149 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5152 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5153 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5159 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5162 time_t t = time(NULL);
5163 struct tm *tm = localtime(&t);
5165 setlocale(LC_TIME, "");
5166 strftime(p, p_max_size, obj->data.s, tm);
5169 time_t t = time(NULL);
5170 struct tm *tm = gmtime(&t);
5172 strftime(p, p_max_size, obj->data.s, tm);
5179 if (obj->data.tztime.tz) {
5180 oldTZ = getenv("TZ");
5181 setenv("TZ", obj->data.tztime.tz, 1);
5187 setlocale(LC_TIME, "");
5188 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5190 setenv("TZ", oldTZ, 1);
5195 // Needless to free oldTZ since getenv gives ptr to static data
5198 human_readable(obj->data.net->recv, p, 255);
5201 human_readable(obj->data.net->trans, p, 255);
5204 snprintf(p, p_max_size, "%d", total_updates);
5207 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5212 human_readable(obj->data.net->trans_speed, p, 255);
5215 spaced_print(p, p_max_size, "%.1f", 8,
5216 obj->data.net->trans_speed / 1024.0);
5220 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5221 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5225 format_seconds_short(p, p_max_size, (int) cur->uptime);
5228 format_seconds(p, p_max_size, (int) cur->uptime);
5231 snprintf(p, p_max_size, "%s", cur->users.names);
5234 snprintf(p, p_max_size, "%s", cur->users.terms);
5237 snprintf(p, p_max_size, "%s", cur->users.times);
5240 snprintf(p, p_max_size, "%d", cur->users.number);
5242 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5243 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5247 msg = get_apm_adapter();
5248 snprintf(p, p_max_size, "%s", msg);
5251 OBJ(apm_battery_life) {
5254 msg = get_apm_battery_life();
5255 snprintf(p, p_max_size, "%s", msg);
5258 OBJ(apm_battery_time) {
5261 msg = get_apm_battery_time();
5262 snprintf(p, p_max_size, "%s", msg);
5265 #endif /* __FreeBSD__ __OpenBSD__ */
5268 #define mpd_printf(fmt, val) \
5269 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5270 #define mpd_sprintf(val) { \
5271 if (!obj->data.i || obj->data.i > p_max_size) \
5272 mpd_printf("%s", val); \
5274 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5279 mpd_sprintf(artist);
5283 mpd_printf("%s", random);
5285 mpd_printf("%s", repeat);
5293 mpd_printf("%d", volume);
5295 mpd_printf("%d", bitrate);
5297 mpd_printf("%s", status);
5299 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5302 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5305 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5309 if(output_methods & TO_X) {
5310 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5311 (int) (mpd_get_info()->progress * 255.0f));
5314 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5315 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5321 struct mpd_s *mpd = mpd_get_info();
5322 int len = obj->data.i;
5323 if (len == 0 || len > p_max_size)
5326 memset(p, 0, p_max_size);
5327 if (mpd->artist && *mpd->artist &&
5328 mpd->title && *mpd->title) {
5329 snprintf(p, len, "%s - %s", mpd->artist,
5331 } else if (mpd->title && *mpd->title) {
5332 snprintf(p, len, "%s", mpd->title);
5333 } else if (mpd->artist && *mpd->artist) {
5334 snprintf(p, len, "%s", mpd->artist);
5335 } else if (mpd->file && *mpd->file) {
5336 snprintf(p, len, "%s", mpd->file);
5341 OBJ(if_mpd_playing) {
5342 if (!mpd_get_info()->is_playing) {
5351 #define MOC_PRINT(t, a) \
5352 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5354 MOC_PRINT(state, "??");
5357 MOC_PRINT(file, "no file");
5360 MOC_PRINT(title, "no title");
5363 MOC_PRINT(artist, "no artist");
5366 MOC_PRINT(song, "no song");
5369 MOC_PRINT(album, "no album");
5371 OBJ(moc_totaltime) {
5372 MOC_PRINT(totaltime, "0:00");
5375 MOC_PRINT(timeleft, "0:00");
5378 MOC_PRINT(curtime, "0:00");
5381 MOC_PRINT(bitrate, "0Kbps");
5384 MOC_PRINT(rate, "0KHz");
5390 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5393 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5396 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5399 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5401 OBJ(xmms2_comment) {
5402 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5405 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5408 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5411 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5413 OBJ(xmms2_tracknr) {
5414 if (cur->xmms2.tracknr != -1) {
5415 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5418 OBJ(xmms2_bitrate) {
5419 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5422 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5425 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5427 OBJ(xmms2_elapsed) {
5428 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5429 (cur->xmms2.elapsed / 1000) % 60);
5431 OBJ(xmms2_duration) {
5432 snprintf(p, p_max_size, "%02d:%02d",
5433 cur->xmms2.duration / 60000,
5434 (cur->xmms2.duration / 1000) % 60);
5436 OBJ(xmms2_percent) {
5437 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5441 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5442 (int) (cur->xmms2.progress * 255.0f));
5445 OBJ(xmms2_playlist) {
5446 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5448 OBJ(xmms2_timesplayed) {
5449 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5452 if (strlen(cur->xmms2.title) < 2
5453 && strlen(cur->xmms2.title) < 2) {
5454 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5456 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5460 OBJ(if_xmms2_connected) {
5461 if (cur->xmms2.conn_state != 1) {
5467 OBJ(audacious_status) {
5468 snprintf(p, p_max_size, "%s",
5469 cur->audacious.items[AUDACIOUS_STATUS]);
5471 OBJ(audacious_title) {
5472 snprintf(p, cur->audacious.max_title_len > 0
5473 ? cur->audacious.max_title_len : p_max_size, "%s",
5474 cur->audacious.items[AUDACIOUS_TITLE]);
5476 OBJ(audacious_length) {
5477 snprintf(p, p_max_size, "%s",
5478 cur->audacious.items[AUDACIOUS_LENGTH]);
5480 OBJ(audacious_length_seconds) {
5481 snprintf(p, p_max_size, "%s",
5482 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5484 OBJ(audacious_position) {
5485 snprintf(p, p_max_size, "%s",
5486 cur->audacious.items[AUDACIOUS_POSITION]);
5488 OBJ(audacious_position_seconds) {
5489 snprintf(p, p_max_size, "%s",
5490 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5492 OBJ(audacious_bitrate) {
5493 snprintf(p, p_max_size, "%s",
5494 cur->audacious.items[AUDACIOUS_BITRATE]);
5496 OBJ(audacious_frequency) {
5497 snprintf(p, p_max_size, "%s",
5498 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5500 OBJ(audacious_channels) {
5501 snprintf(p, p_max_size, "%s",
5502 cur->audacious.items[AUDACIOUS_CHANNELS]);
5504 OBJ(audacious_filename) {
5505 snprintf(p, p_max_size, "%s",
5506 cur->audacious.items[AUDACIOUS_FILENAME]);
5508 OBJ(audacious_playlist_length) {
5509 snprintf(p, p_max_size, "%s",
5510 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5512 OBJ(audacious_playlist_position) {
5513 snprintf(p, p_max_size, "%s",
5514 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5516 OBJ(audacious_main_volume) {
5517 snprintf(p, p_max_size, "%s",
5518 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5521 OBJ(audacious_bar) {
5525 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5526 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5527 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5530 #endif /* AUDACIOUS */
5534 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5537 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5540 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5543 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5546 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5549 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5552 /* we have four different types of top (top, top_mem,
5553 * top_time and top_io). To avoid having almost-same code four
5554 * times, we have this special handler. */
5557 parse_top_args("top", obj->data.top.s, obj);
5558 if (!needed) needed = cur->cpu;
5560 parse_top_args("top_mem", obj->data.top.s, obj);
5561 if (!needed) needed = cur->memu;
5563 parse_top_args("top_time", obj->data.top.s, obj);
5564 if (!needed) needed = cur->time;
5567 parse_top_args("top_io", obj->data.top.s, obj);
5568 if (!needed) needed = cur->io;
5571 if (needed[obj->data.top.num]) {
5574 switch (obj->data.top.type) {
5576 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5577 needed[obj->data.top.num]->name);
5580 snprintf(p, 7, "%6.2f",
5581 needed[obj->data.top.num]->amount);
5584 snprintf(p, 6, "%5i",
5585 needed[obj->data.top.num]->pid);
5588 snprintf(p, 7, "%6.2f",
5589 needed[obj->data.top.num]->totalmem);
5592 timeval = format_time(
5593 needed[obj->data.top.num]->total_cpu_time, 9);
5594 snprintf(p, 10, "%9s", timeval);
5598 human_readable(needed[obj->data.top.num]->rss,
5602 human_readable(needed[obj->data.top.num]->vsize,
5606 case TOP_READ_BYTES:
5607 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5610 case TOP_WRITE_BYTES:
5611 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5615 snprintf(p, 7, "%6.2f",
5616 needed[obj->data.top.num]->io_perc);
5622 print_tail_object(obj, p, p_max_size);
5624 print_head_object(obj, p, p_max_size);
5626 FILE *fp = open_file(obj->data.s, &obj->a);
5629 /* FIXME: use something more general (see also tail.c, head.c */
5630 #define BUFSZ 0x1000
5635 while(fgets(buf, BUFSZ, fp) != NULL){
5636 for(j = 0; buf[j] != 0; j++) {
5637 if(buf[j] == '\n') {
5642 sprintf(p, "%d", lines);
5645 sprintf(p, "File Unreadable");
5650 FILE *fp = open_file(obj->data.s, &obj->a);
5655 char inword = FALSE;
5658 while(fgets(buf, BUFSZ, fp) != NULL){
5659 for(j = 0; buf[j] != 0; j++) {
5660 if(!isspace(buf[j])) {
5661 if(inword == FALSE) {
5670 sprintf(p, "%d", words);
5673 sprintf(p, "File Unreadable");
5676 #ifdef TCP_PORT_MONITOR
5678 tcp_portmon_action(p, p_max_size,
5679 &obj->data.tcp_port_monitor);
5681 #endif /* TCP_PORT_MONITOR */
5685 iconv_converting = 1;
5686 iconv_selected = obj->a;
5689 iconv_converting = 0;
5692 #endif /* HAVE_ICONV */
5694 OBJ(entropy_avail) {
5695 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5698 percent_print(p, p_max_size,
5699 cur->entropy.entropy_avail *
5700 100 / cur->entropy.poolsize);
5702 OBJ(entropy_poolsize) {
5703 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5706 double entropy_perc;
5708 entropy_perc = (double) cur->entropy.entropy_avail /
5709 (double) cur->entropy.poolsize;
5711 if(output_methods & TO_X) {
5712 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5715 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5716 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5725 s = smapi_get_val(obj->data.s);
5726 snprintf(p, p_max_size, "%s", s);
5730 OBJ(if_smapi_bat_installed) {
5732 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5733 if(!smapi_bat_installed(idx)) {
5737 ERR("argument to if_smapi_bat_installed must be an integer");
5739 OBJ(smapi_bat_perc) {
5741 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5742 val = smapi_bat_installed(idx) ?
5743 smapi_get_bat_int(idx, "remaining_percent") : 0;
5744 percent_print(p, p_max_size, val);
5746 ERR("argument to smapi_bat_perc must be an integer");
5748 OBJ(smapi_bat_temp) {
5750 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5751 val = smapi_bat_installed(idx) ?
5752 smapi_get_bat_int(idx, "temperature") : 0;
5753 /* temperature is in milli degree celsius */
5754 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5756 ERR("argument to smapi_bat_temp must be an integer");
5758 OBJ(smapi_bat_power) {
5760 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5761 val = smapi_bat_installed(idx) ?
5762 smapi_get_bat_int(idx, "power_now") : 0;
5763 /* power_now is in mW, set to W with one digit precision */
5764 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5766 ERR("argument to smapi_bat_power must be an integer");
5769 OBJ(smapi_bat_bar) {
5770 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5771 new_bar(p, obj->a, obj->b, (int)
5772 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5774 new_bar(p, obj->a, obj->b, 0);
5779 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5780 char buf[max_user_text];
5783 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5784 snprintf(p, p_max_size, "%s", buf);
5785 if(total_updates % 2) {
5786 for(j=0; p[j] != 0; j++) {
5792 char buf[max_user_text];
5794 char unit[16]; //16 because we can also have long names (like mega-bytes)
5796 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5797 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5798 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5799 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5800 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5801 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5802 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5804 snprintf(p, p_max_size, "%s", buf);
5808 char *tmp, buf[max_user_text];
5809 generate_text_internal(buf, max_user_text,
5812 if (strlen(buf) <= obj->data.scroll.show) {
5813 snprintf(p, p_max_size, "%s", buf);
5816 #define LINESEPARATOR '|'
5817 //place all the lines behind each other with LINESEPARATOR between them
5818 for(j = 0; buf[j] != 0; j++) {
5820 buf[j]=LINESEPARATOR;
5823 //scroll the output obj->data.scroll.start places by copying that many chars from
5824 //the front of the string to tmp, scrolling the rest to the front and placing tmp
5825 //at the back of the string
5826 tmp = calloc(obj->data.scroll.start + 1, sizeof(char));
5827 strncpy(tmp, buf, obj->data.scroll.start); tmp[obj->data.scroll.start] = 0;
5828 for(j = obj->data.scroll.start; buf[j] != 0; j++){
5829 buf[j - obj->data.scroll.start] = buf[j];
5831 strcpy(&buf[j - obj->data.scroll.start], tmp);
5833 //only show the requested number of chars
5834 if(obj->data.scroll.show < j) {
5835 buf[obj->data.scroll.show] = 0;
5837 //next time, scroll a place more or reset scrolling if we are at the end
5838 obj->data.scroll.start += obj->data.scroll.step;
5839 if(obj->data.scroll.start >= j){
5840 obj->data.scroll.start = 0;
5842 snprintf(p, p_max_size, "%s", buf);
5845 char buf[2][max_user_text];
5852 struct llrows* next;
5854 struct llrows *ll_rows[2], *current[2];
5855 struct text_object * objsub = obj->sub;
5858 for(i=0; i<2; i++) {
5861 ll_rows[i] = malloc(sizeof(struct llrows));
5862 current[i] = ll_rows[i];
5863 for(j=0; j<i; j++) objsub = objsub->sub;
5864 generate_text_internal(buf[i], max_user_text, *objsub, cur);
5865 for(j=0; buf[i][j] != 0; j++) {
5866 if(buf[i][j] == '\t') buf[i][j] = ' ';
5867 if(buf[i][j] == '\n') {
5869 current[i]->row = strdup(buf[i]+nextstart);
5870 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5871 current[i]->next = malloc(sizeof(struct llrows));
5872 current[i] = current[i]->next;
5877 current[i]->row = strdup(buf[i]+nextstart);
5878 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
5879 current[i]->next = NULL;
5880 current[i] = ll_rows[i];
5882 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
5884 strcat(p, current[0]->row);
5885 i=strlen(current[0]->row);
5887 while(i < longest) {
5892 strcat(p, obj->data.combine.seperation);
5893 strcat(p, current[1]->row);
5897 #pragma omp parallel for
5898 #endif /* HAVE_OPENMP */
5899 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
5902 #pragma omp parallel for
5903 #endif /* HAVE_OPENMP */
5904 for(i=0; i<2; i++) {
5905 while(ll_rows[i] != NULL) {
5906 current[i]=ll_rows[i];
5907 free(current[i]->row);
5908 ll_rows[i]=current[i]->next;
5915 int value = get_nvidia_value(obj->data.nvidia.type, display);
5917 snprintf(p, p_max_size, "N/A");
5918 else if (obj->data.nvidia.type == NV_TEMP)
5919 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
5920 else if (obj->data.nvidia.print_as_float &&
5921 value > 0 && value < 100)
5922 snprintf(p, p_max_size, "%.1f", (float)value);
5924 snprintf(p, p_max_size, "%d", value);
5929 /* This is just a meta-object to set host:port */
5932 snprintf(p, p_max_size, "%s",
5933 cur->apcupsd.items[APCUPSD_NAME]);
5935 OBJ(apcupsd_model) {
5936 snprintf(p, p_max_size, "%s",
5937 cur->apcupsd.items[APCUPSD_MODEL]);
5939 OBJ(apcupsd_upsmode) {
5940 snprintf(p, p_max_size, "%s",
5941 cur->apcupsd.items[APCUPSD_UPSMODE]);
5943 OBJ(apcupsd_cable) {
5944 snprintf(p, p_max_size, "%s",
5945 cur->apcupsd.items[APCUPSD_CABLE]);
5947 OBJ(apcupsd_status) {
5948 snprintf(p, p_max_size, "%s",
5949 cur->apcupsd.items[APCUPSD_STATUS]);
5951 OBJ(apcupsd_linev) {
5952 snprintf(p, p_max_size, "%s",
5953 cur->apcupsd.items[APCUPSD_LINEV]);
5956 snprintf(p, p_max_size, "%s",
5957 cur->apcupsd.items[APCUPSD_LOAD]);
5959 OBJ(apcupsd_loadbar) {
5962 if(output_methods & TO_X) {
5963 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5964 new_bar(p, obj->a, obj->b, (int) progress);
5967 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5968 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5969 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
5975 OBJ(apcupsd_loadgraph) {
5977 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
5978 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5979 (int)progress, 100, 1, obj->char_a, obj->char_b);
5981 OBJ(apcupsd_loadgauge) {
5983 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
5984 new_gauge(p, obj->a, obj->b,
5988 OBJ(apcupsd_charge) {
5989 snprintf(p, p_max_size, "%s",
5990 cur->apcupsd.items[APCUPSD_CHARGE]);
5992 OBJ(apcupsd_timeleft) {
5993 snprintf(p, p_max_size, "%s",
5994 cur->apcupsd.items[APCUPSD_TIMELEFT]);
5997 snprintf(p, p_max_size, "%s",
5998 cur->apcupsd.items[APCUPSD_TEMP]);
6000 OBJ(apcupsd_lastxfer) {
6001 snprintf(p, p_max_size, "%s",
6002 cur->apcupsd.items[APCUPSD_LASTXFER]);
6004 #endif /* APCUPSD */
6011 unsigned int a = strlen(p);
6014 if (a > 0 && iconv_converting && iconv_selected > 0
6015 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6017 size_t dummy1, dummy2;
6019 const char *ptr = buff_in;
6021 char *ptr = buff_in;
6025 dummy1 = dummy2 = a;
6027 strncpy(buff_in, p, p_max_size);
6029 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6030 while (dummy1 > 0) {
6031 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6034 ERR("Iconv codeset conversion failed");
6039 /* It is nessecary when we are converting from multibyte to
6040 * singlebyte codepage */
6043 #endif /* HAVE_ICONV */
6050 /* load any new fonts we may have had */
6051 if (need_to_load_fonts) {
6057 double current_update_time, next_update_time, last_update_time;
6059 static void generate_text(void)
6061 struct information *cur = &info;
6068 current_update_time = get_time();
6072 /* add things to the buffer */
6078 generate_text_internal(p, max_user_text, global_root_object, cur);
6080 if (stuff_in_upper_case) {
6083 tmp_p = text_buffer;
6085 *tmp_p = toupper(*tmp_p);
6090 next_update_time += update_interval;
6091 if (next_update_time < get_time()) {
6092 next_update_time = get_time() + update_interval;
6093 } else if (next_update_time > get_time() + update_interval) {
6094 next_update_time = get_time() + update_interval;
6096 last_update_time = current_update_time;
6101 static inline int get_string_width(const char *s)
6104 if (output_methods & TO_X) {
6105 return *s ? calc_text_width(s, strlen(s)) : 0;
6111 static inline int get_string_width_special(char *s)
6119 if ((output_methods & TO_X) == 0) {
6121 return (s) ? strlen(s) : 0;
6129 p = strndup(s, text_buffer_size);
6133 if (*p == SPECIAL_CHAR) {
6134 /* shift everything over by 1 so that the special char
6135 * doesn't mess up the size calculation */
6136 for (i = 0; i < (long)strlen(p); i++) {
6137 *(p + i) = *(p + i + 1);
6139 if (specials[special_index + idx].type == GRAPH
6140 || specials[special_index + idx].type == GAUGE
6141 || specials[special_index + idx].type == BAR) {
6142 width += specials[special_index + idx].width;
6149 if (strlen(final) > 1) {
6150 width += calc_text_width(final, strlen(final));
6158 static void text_size_updater(char *s);
6160 int last_font_height;
6161 static void update_text_area(void)
6165 if ((output_methods & TO_X) == 0)
6167 /* update text size if it isn't fixed */
6172 text_width = minimum_width;
6175 last_font_height = font_height();
6176 for_each_line(text_buffer, text_size_updater);
6178 if (text_height < minimum_height) {
6179 text_height = minimum_height;
6181 if (text_width > maximum_width && maximum_width > 0) {
6182 text_width = maximum_width;
6186 /* get text position on workarea */
6187 switch (text_alignment) {
6194 x = workarea[2] - text_width - gap_x;
6199 x = workarea[2] / 2 - text_width / 2 - gap_x;
6206 y = workarea[3] - text_height - gap_y;
6210 x = workarea[2] - text_width - gap_x;
6211 y = workarea[3] - text_height - gap_y;
6215 x = workarea[2] / 2 - text_width / 2 - gap_x;
6216 y = workarea[3] - text_height - gap_y;
6221 y = workarea[3] / 2 - text_height / 2 - gap_y;
6225 x = workarea[2] - text_width - gap_x;
6226 y = workarea[3] / 2 - text_height / 2 - gap_y;
6230 case NONE: // Let the WM manage the window
6241 if (own_window && !fixed_pos) {
6244 text_start_x = border_inner_margin + border_outer_margin + border_width;
6245 text_start_y = border_inner_margin + border_outer_margin + border_width;
6246 window.x = x - border_inner_margin - border_outer_margin - border_width;
6247 window.y = y - border_inner_margin - border_outer_margin - border_width;
6251 /* If window size doesn't match to workarea's size,
6252 * then window probably includes panels (gnome).
6253 * Blah, doesn't work on KDE. */
6254 if (workarea[2] != window.width || workarea[3] != window.height) {
6266 static int cur_x, cur_y; /* current x and y for drawing */
6268 //draw_mode also without X11 because we only need to print to stdout with FG
6269 static int draw_mode; /* FG, BG or OUTLINE */
6271 static long current_color;
6273 static void text_size_updater(char *s)
6278 if ((output_methods & TO_X) == 0)
6280 /* get string widths and skip specials */
6283 if (*p == SPECIAL_CHAR) {
6285 w += get_string_width(s);
6288 if (specials[special_index].type == BAR
6289 || specials[special_index].type == GAUGE
6290 || specials[special_index].type == GRAPH) {
6291 w += specials[special_index].width;
6292 if (specials[special_index].height > last_font_height) {
6293 last_font_height = specials[special_index].height;
6294 last_font_height += font_height();
6296 } else if (specials[special_index].type == OFFSET) {
6297 if (specials[special_index].arg > 0) {
6298 w += specials[special_index].arg;
6300 } else if (specials[special_index].type == VOFFSET) {
6301 last_font_height += specials[special_index].arg;
6302 } else if (specials[special_index].type == GOTO) {
6303 if (specials[special_index].arg > cur_x) {
6304 w = (int) specials[special_index].arg;
6306 } else if (specials[special_index].type == TAB) {
6307 int start = specials[special_index].arg;
6308 int step = specials[special_index].width;
6310 if (!step || step < 0) {
6313 w += step - (cur_x - text_start_x - start) % step;
6314 } else if (specials[special_index].type == FONT) {
6315 selected_font = specials[special_index].font_added;
6316 if (font_height() > last_font_height) {
6317 last_font_height = font_height();
6326 w += get_string_width(s);
6327 if (w > text_width) {
6330 if (text_width > maximum_width && maximum_width) {
6331 text_width = maximum_width;
6334 text_height += last_font_height;
6335 last_font_height = font_height();
6338 static inline void set_foreground_color(long c)
6340 if ((output_methods & TO_X) == 0)
6343 XSetForeground(display, window.gc, c);
6347 static void draw_string(const char *s)
6349 int i, i2, pos, width_of_s;
6357 width_of_s = get_string_width(s);
6358 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6360 fflush(stdout); /* output immediately, don't buffer */
6362 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6363 fprintf(stderr, "%s\n", s);
6364 fflush(stderr); /* output immediately, don't buffer */
6366 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6367 fprintf(overwrite_fpointer, "%s\n", s);
6369 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6370 fprintf(append_fpointer, "%s\n", s);
6372 memset(tmpstring1, 0, text_buffer_size);
6373 memset(tmpstring2, 0, text_buffer_size);
6374 strncpy(tmpstring1, s, text_buffer_size - 1);
6379 if (output_methods & TO_X) {
6380 max = ((text_width - width_of_s) / get_string_width(" "));
6383 /* This code looks for tabs in the text and coverts them to spaces.
6384 * The trick is getting the correct number of spaces, and not going
6385 * over the window's size without forcing the window larger. */
6386 for (i = 0; i < (int) text_buffer_size; i++) {
6387 if (tmpstring1[i] == '\t') {
6389 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6390 /* guard against overrun */
6391 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6396 /* guard against overrun */
6397 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6402 if (output_methods & TO_X) {
6403 if (text_width == maximum_width) {
6404 /* this means the text is probably pushing the limit,
6405 * so we'll chop it */
6406 while (cur_x + get_string_width(tmpstring2) - text_start_x
6407 > maximum_width && strlen(tmpstring2) > 0) {
6408 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6415 if (output_methods & TO_X) {
6421 c.pixel = current_color;
6422 XQueryColor(display, DefaultColormap(display, screen), &c);
6425 c2.color.red = c.red;
6426 c2.color.green = c.green;
6427 c2.color.blue = c.blue;
6428 c2.color.alpha = fonts[selected_font].font_alpha;
6430 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6431 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6433 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6434 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6439 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6442 cur_x += width_of_s;
6445 memcpy(tmpstring1, s, text_buffer_size);
6448 static void draw_line(char *s)
6456 if ((output_methods & TO_X) == 0) {
6462 cur_x = text_start_x;
6463 cur_y += font_ascent();
6464 font_h = font_height();
6466 /* find specials and draw stuff */
6469 if (*p == SPECIAL_CHAR) {
6472 /* draw string before special */
6479 switch (specials[special_index].type) {
6480 case HORIZONTAL_LINE:
6482 int h = specials[special_index].height;
6483 int mid = font_ascent() / 2;
6485 w = text_start_x + text_width - cur_x;
6487 XSetLineAttributes(display, window.gc, h, LineSolid,
6488 CapButt, JoinMiter);
6489 XDrawLine(display, window.drawable, window.gc, cur_x,
6490 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6496 int h = specials[special_index].height;
6497 int tmp_s = specials[special_index].arg;
6498 int mid = font_ascent() / 2;
6499 char ss[2] = { tmp_s, tmp_s };
6501 w = text_start_x + text_width - cur_x - 1;
6502 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6503 CapButt, JoinMiter);
6504 XSetDashes(display, window.gc, 0, ss, 2);
6505 XDrawLine(display, window.drawable, window.gc, cur_x,
6506 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6512 int h, bar_usage, by;
6513 if (cur_x - text_start_x > maximum_width
6514 && maximum_width > 0) {
6517 h = specials[special_index].height;
6518 bar_usage = specials[special_index].arg;
6519 by = cur_y - (font_ascent() / 2) - 1;
6524 w = specials[special_index].width;
6526 w = text_start_x + text_width - cur_x - 1;
6532 XSetLineAttributes(display, window.gc, 1, LineSolid,
6533 CapButt, JoinMiter);
6535 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6537 XFillRectangle(display, window.drawable, window.gc, cur_x,
6538 by, w * bar_usage / 255, h);
6546 case GAUGE: /* new GAUGE */
6549 unsigned long last_colour = current_color;
6550 float angle, px, py;
6553 if (cur_x - text_start_x > maximum_width
6554 && maximum_width > 0) {
6558 h = specials[special_index].height;
6559 by = cur_y - (font_ascent() / 2) - 1;
6564 w = specials[special_index].width;
6566 w = text_start_x + text_width - cur_x - 1;
6572 XSetLineAttributes(display, window.gc, 1, LineSolid,
6573 CapButt, JoinMiter);
6575 XDrawArc(display, window.drawable, window.gc,
6576 cur_x, by, w, h * 2, 0, 180*64);
6579 usage = specials[special_index].arg;
6580 angle = (M_PI)*(float)(usage)/255.;
6581 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6582 py = (float)(by+(h))-(float)(h)*sin(angle);
6584 XDrawLine(display, window.drawable, window.gc,
6585 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6593 set_foreground_color(last_colour);
6601 int h, by, i = 0, j = 0;
6603 unsigned long last_colour = current_color;
6604 unsigned long *tmpcolour = 0;
6605 if (cur_x - text_start_x > maximum_width
6606 && maximum_width > 0) {
6609 h = specials[special_index].height;
6610 by = cur_y - (font_ascent() / 2) - 1;
6615 w = specials[special_index].width;
6617 w = text_start_x + text_width - cur_x - 1;
6622 if (draw_graph_borders) {
6623 XSetLineAttributes(display, window.gc, 1, LineSolid,
6624 CapButt, JoinMiter);
6625 XDrawRectangle(display, window.drawable, window.gc,
6628 XSetLineAttributes(display, window.gc, 1, LineSolid,
6629 CapButt, JoinMiter);
6631 if (specials[special_index].last_colour != 0
6632 || specials[special_index].first_colour != 0) {
6633 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6636 for (i = w - 2; i > -1; i--) {
6637 if (specials[special_index].last_colour != 0
6638 || specials[special_index].first_colour != 0) {
6639 if (specials[special_index].tempgrad) {
6642 (int)((float)(w - 2) - specials[special_index].graph[j] *
6643 (w - 2) / (float)specials[special_index].graph_scale)
6647 (int)((float)(w - 2) - specials[special_index].graph[j] *
6648 (w - 2) / (float)specials[special_index].graph_scale)
6651 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6653 (int)((float)(w - 2) - specials[special_index].graph[j] *
6654 (w - 2) / (float)specials[special_index].graph_scale)
6658 #endif /* DEBUG_lol */
6659 XSetForeground(display, window.gc, tmpcolour[
6660 (int)((float)(w - 2) - specials[special_index].graph[j] *
6661 (w - 2) / (float)specials[special_index].graph_scale)
6664 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6667 /* this is mugfugly, but it works */
6668 XDrawLine(display, window.drawable, window.gc,
6669 cur_x + i + 1, by + h, cur_x + i + 1,
6670 round_to_int((double)by + h - specials[special_index].graph[j] *
6671 (h - 1) / specials[special_index].graph_scale));
6672 if ((w - i) / ((float) (w - 2) /
6673 (specials[special_index].graph_width)) > j
6674 && j < MAX_GRAPH_DEPTH - 3) {
6678 if (tmpcolour) free(tmpcolour);
6683 /* if (draw_mode == BG) {
6684 set_foreground_color(default_bg_color);
6685 } else if (draw_mode == OUTLINE) {
6686 set_foreground_color(default_out_color);
6688 set_foreground_color(default_fg_color);
6690 if (show_graph_range) {
6693 unsigned short int seconds = update_interval * w;
6698 unsigned short int timeunits;
6700 timeunits = seconds / 86400; seconds %= 86400;
6701 if (timeunits > 0) {
6702 asprintf(&tmp_day_str, "%dd", timeunits);
6704 tmp_day_str = strdup("");
6706 timeunits = seconds / 3600; seconds %= 3600;
6707 if (timeunits > 0) {
6708 asprintf(&tmp_hour_str, "%dh", timeunits);
6710 tmp_hour_str = strdup("");
6712 timeunits = seconds / 60; seconds %= 60;
6713 if (timeunits > 0) {
6714 asprintf(&tmp_min_str, "%dm", timeunits);
6716 tmp_min_str = strdup("");
6719 asprintf(&tmp_sec_str, "%ds", seconds);
6721 tmp_sec_str = strdup("");
6723 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6724 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6726 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6728 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6729 cur_y += font_h / 2;
6730 draw_string(tmp_str);
6736 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
6739 cur_x += font_ascent() / 2;
6740 cur_y += font_h / 2;
6742 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
6744 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
6745 draw_string(tmp_str);
6751 set_foreground_color(last_colour);
6757 int old = font_ascent();
6759 cur_y -= font_ascent();
6760 selected_font = specials[special_index].font_added;
6762 if (cur_y + font_ascent() < cur_y + old) {
6765 cur_y += font_ascent();
6767 font_h = font_height();
6771 if (draw_mode == FG) {
6772 set_foreground_color(specials[special_index].arg);
6777 if (draw_mode == BG) {
6778 set_foreground_color(specials[special_index].arg);
6783 if (draw_mode == OUTLINE) {
6784 set_foreground_color(specials[special_index].arg);
6789 w += specials[special_index].arg;
6793 cur_y += specials[special_index].arg;
6797 if (specials[special_index].arg >= 0) {
6798 cur_x = (int) specials[special_index].arg;
6804 int start = specials[special_index].arg;
6805 int step = specials[special_index].width;
6807 if (!step || step < 0) {
6810 w = step - (cur_x - text_start_x - start) % step;
6816 /* TODO: add back in "+ border_inner_margin" to the end of
6818 int pos_x = text_start_x + text_width -
6819 get_string_width_special(s);
6821 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6822 "get_string_width(p) %i gap_x %i "
6823 "specials[special_index].arg %i border_inner_margin %i "
6824 "border_width %i\n", pos_x, text_start_x, text_width,
6825 cur_x, get_string_width_special(s), gap_x,
6826 specials[special_index].arg, border_inner_margin,
6828 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
6829 cur_x = pos_x - specials[special_index].arg;
6836 int pos_x = (text_width) / 2 - get_string_width_special(s) /
6837 2 - (cur_x - text_start_x);
6838 /* int pos_x = text_start_x + text_width / 2 -
6839 get_string_width_special(s) / 2; */
6841 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
6842 "get_string_width(p) %i gap_x %i "
6843 "specials[special_index].arg %i\n", pos_x, text_start_x,
6844 text_width, cur_x, get_string_width(s), gap_x,
6845 specials[special_index].arg); */
6846 if (pos_x > specials[special_index].arg) {
6847 w = pos_x - specials[special_index].arg;
6861 if (cur_y_add > 0) {
6865 cur_y += font_descent();
6870 static void draw_text(void)
6873 if (output_methods & TO_X) {
6874 cur_y = text_start_y;
6877 if (draw_borders && border_width > 0) {
6878 if (stippled_borders) {
6879 char ss[2] = { stippled_borders, stippled_borders };
6880 XSetLineAttributes(display, window.gc, border_width, LineOnOffDash,
6881 CapButt, JoinMiter);
6882 XSetDashes(display, window.gc, 0, ss, 2);
6884 XSetLineAttributes(display, window.gc, border_width, LineSolid,
6885 CapButt, JoinMiter);
6888 XDrawRectangle(display, window.drawable, window.gc,
6889 text_start_x - border_inner_margin - border_width,
6890 text_start_y - border_inner_margin - border_width,
6891 text_width + border_inner_margin * 2 + border_width * 2,
6892 text_height + border_inner_margin * 2 + border_width * 2);
6900 for_each_line(text_buffer, draw_line);
6903 static void draw_stuff(void)
6905 if (overwrite_file) {
6906 overwrite_fpointer = fopen(overwrite_file, "w");
6907 if(!overwrite_fpointer)
6908 ERR("Can't overwrite '%s' anymore", overwrite_file);
6911 append_fpointer = fopen(append_file, "a");
6912 if(!append_fpointer)
6913 ERR("Can't append '%s' anymore", append_file);
6916 if (output_methods & TO_X) {
6918 if (draw_shades && !draw_outline) {
6921 set_foreground_color(default_bg_color);
6932 for (i = -1; i < 2; i++) {
6933 for (j = -1; j < 2; j++) {
6934 if (i == 0 && j == 0) {
6939 set_foreground_color(default_out_color);
6940 draw_mode = OUTLINE;
6948 set_foreground_color(default_fg_color);
6954 if (output_methods & TO_X) {
6959 swap.swap_window = window.window;
6960 swap.swap_action = XdbeBackground;
6961 XdbeSwapBuffers(display, &swap, 1);
6966 if(overwrite_fpointer) {
6967 fclose(overwrite_fpointer);
6968 overwrite_fpointer = 0;
6970 if(append_fpointer) {
6971 fclose(append_fpointer);
6972 append_fpointer = 0;
6977 static void clear_text(int exposures)
6981 /* The swap action is XdbeBackground, which clears */
6985 if (display && window.window) { // make sure these are !null
6986 /* there is some extra space for borders and outlines */
6987 XClearArea(display, window.window, text_start_x - border_inner_margin - border_outer_margin - border_width,
6988 text_start_y - border_inner_margin - border_outer_margin - border_width,
6989 text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2,
6990 text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2, exposures ? True : 0);
6995 static int need_to_update;
6997 /* update_text() generates new text and clears old text area */
6998 static void update_text(void)
7005 if (output_methods & TO_X)
7011 #ifdef HAVE_SYS_INOTIFY_H
7015 static void main_loop(void)
7018 #ifdef SIGNAL_BLOCKING
7019 sigset_t newmask, oldmask;
7022 #ifdef HAVE_SYS_INOTIFY_H
7023 int inotify_config_wd = -1;
7024 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
7025 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
7026 char inotify_buff[INOTIFY_BUF_LEN];
7027 #endif /* HAVE_SYS_INOTIFY_H */
7030 #ifdef SIGNAL_BLOCKING
7031 sigemptyset(&newmask);
7032 sigaddset(&newmask, SIGINT);
7033 sigaddset(&newmask, SIGTERM);
7034 sigaddset(&newmask, SIGUSR1);
7037 last_update_time = 0.0;
7038 next_update_time = get_time();
7040 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7043 #ifdef SIGNAL_BLOCKING
7044 /* block signals. we will inspect for pending signals later */
7045 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7046 CRIT_ERR("unable to sigprocmask()");
7051 if (output_methods & TO_X) {
7054 /* wait for X event or timeout */
7056 if (!XPending(display)) {
7060 t = next_update_time - get_time();
7064 } else if (t > update_interval) {
7065 t = update_interval;
7068 tv.tv_sec = (long) t;
7069 tv.tv_usec = (long) (t * 1000000) % 1000000;
7071 FD_SET(ConnectionNumber(display), &fdsr);
7073 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7075 if (errno != EINTR) {
7076 ERR("can't select(): %s", strerror(errno));
7086 if (need_to_update) {
7088 int wx = window.x, wy = window.y;
7098 /* resize window if it isn't right size */
7100 && (text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2 != window.width
7101 || text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2 != window.height)) {
7102 window.width = text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2;
7103 window.height = text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2;
7104 XResizeWindow(display, window.window, window.width,
7106 set_transparent_background(window.window);
7111 /* move window if it isn't in right position */
7112 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7113 XMoveWindow(display, window.window, window.x, window.y);
7118 if (changed && window.type == TYPE_PANEL) {
7121 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7124 switch (text_alignment) {
7151 set_struts(sidenum);
7162 r.x = text_start_x - border_inner_margin - border_outer_margin - border_width;
7163 r.y = text_start_y - border_inner_margin - border_outer_margin - border_width;
7164 r.width = text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2;
7165 r.height = text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2;
7166 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7171 /* handle X events */
7172 while (XPending(display)) {
7175 XNextEvent(display, &ev);
7182 r.width = ev.xexpose.width;
7183 r.height = ev.xexpose.height;
7184 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7189 case ReparentNotify:
7190 /* set background to ParentRelative for all parents */
7192 set_transparent_background(window.window);
7196 case ConfigureNotify:
7198 /* if window size isn't what expected, set fixed size */
7199 if (ev.xconfigure.width != window.width
7200 || ev.xconfigure.height != window.height) {
7201 if (window.width != 0 && window.height != 0) {
7205 /* clear old stuff before screwing up
7210 XWindowAttributes attrs;
7211 if (XGetWindowAttributes(display,
7212 window.window, &attrs)) {
7213 window.width = attrs.width;
7214 window.height = attrs.height;
7218 text_width = window.width - border_inner_margin * 2 - border_outer_margin * 2 - border_width * 2;
7219 text_height = window.height - border_inner_margin * 2 - border_outer_margin * 2 - border_width * 2;
7220 if (text_width > maximum_width
7221 && maximum_width > 0) {
7222 text_width = maximum_width;
7226 /* if position isn't what expected, set fixed pos
7227 * total_updates avoids setting fixed_pos when window
7228 * is set to weird locations when started */
7229 /* // this is broken
7230 if (total_updates >= 2 && !fixed_pos
7231 && (window.x != ev.xconfigure.x
7232 || window.y != ev.xconfigure.y)
7233 && (ev.xconfigure.x != 0
7234 || ev.xconfigure.y != 0)) {
7242 /* if an ordinary window with decorations */
7243 if ((window.type == TYPE_NORMAL)
7244 && (!TEST_HINT(window.hints,
7245 HINT_UNDECORATED))) {
7246 /* allow conky to hold input focus. */
7249 /* forward the click to the desktop window */
7250 XUngrabPointer(display, ev.xbutton.time);
7251 ev.xbutton.window = window.desktop;
7252 ev.xbutton.x = ev.xbutton.x_root;
7253 ev.xbutton.y = ev.xbutton.y_root;
7254 XSendEvent(display, ev.xbutton.window, False,
7255 ButtonPressMask, &ev);
7256 XSetInputFocus(display, ev.xbutton.window,
7257 RevertToParent, ev.xbutton.time);
7264 /* if an ordinary window with decorations */
7265 if ((window.type == TYPE_NORMAL)
7266 && (!TEST_HINT(window.hints,
7267 HINT_UNDECORATED))) {
7268 /* allow conky to hold input focus. */
7271 /* forward the release to the desktop window */
7272 ev.xbutton.window = window.desktop;
7273 ev.xbutton.x = ev.xbutton.x_root;
7274 ev.xbutton.y = ev.xbutton.y_root;
7275 XSendEvent(display, ev.xbutton.window, False,
7276 ButtonReleaseMask, &ev);
7285 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7286 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7288 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7289 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7291 #endif /* HAVE_XDAMAGE */
7297 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7298 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7299 #endif /* HAVE_XDAMAGE */
7301 /* XDBE doesn't seem to provide a way to clear the back buffer without
7302 * interfering with the front buffer, other than passing XdbeBackground
7303 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7304 * redraw the text even if it wasn't part of the exposed area. OTOH,
7305 * if we're not going to call draw_stuff at all, then no swap happens
7306 * and we can safely do nothing. */
7308 if (!XEmptyRegion(x11_stuff.region)) {
7313 r.x = text_start_x - border_inner_margin - border_outer_margin - border_width;
7314 r.y = text_start_y - border_inner_margin - border_outer_margin - border_width;
7315 r.width = text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2;
7316 r.height = text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2;
7317 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7320 XSetRegion(display, window.gc, x11_stuff.region);
7323 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7327 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7330 XDestroyRegion(x11_stuff.region);
7331 x11_stuff.region = XCreateRegion();
7335 t = (next_update_time - get_time()) * 1000000;
7336 if(t > 0) usleep((useconds_t)t);
7343 #ifdef SIGNAL_BLOCKING
7344 /* unblock signals of interest and let handler fly */
7345 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7346 CRIT_ERR("unable to sigprocmask()");
7350 switch (g_signal_pending) {
7353 ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7358 ERR("received SIGINT or SIGTERM to terminate. bye!");
7362 if (output_methods & TO_X) {
7363 XDestroyRegion(x11_stuff.region);
7364 x11_stuff.region = NULL;
7366 XDamageDestroy(display, x11_stuff.damage);
7367 XFixesDestroyRegion(display, x11_stuff.region2);
7368 XFixesDestroyRegion(display, x11_stuff.part);
7369 #endif /* HAVE_XDAMAGE */
7375 if(overwrite_file) {
7376 free(overwrite_file);
7385 /* Reaching here means someone set a signal
7386 * (SIGXXXX, signal_handler), but didn't write any code
7388 * If you don't want to handle a signal, don't set a handler on
7389 * it in the first place. */
7390 if (g_signal_pending) {
7391 ERR("ignoring signal (%d)", g_signal_pending);
7395 #ifdef HAVE_SYS_INOTIFY_H
7396 if (inotify_fd != -1 && inotify_config_wd == -1) {
7397 inotify_config_wd = inotify_add_watch(inotify_fd,
7401 if (inotify_fd != -1 && inotify_config_wd != -1) {
7402 int len = 0, idx = 0;
7404 struct timeval time_to_wait;
7406 FD_ZERO(&descriptors);
7407 FD_SET(inotify_fd, &descriptors);
7409 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7411 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7412 if (FD_ISSET(inotify_fd, &descriptors)) {
7413 /* process inotify events */
7414 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7415 while (len > 0 && idx < len) {
7416 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7417 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7418 /* current_config should be reloaded */
7419 ERR("'%s' modified, reloading...", current_config);
7421 if (ev->mask & IN_IGNORED) {
7422 /* for some reason we get IN_IGNORED here
7423 * sometimes, so we need to re-add the watch */
7424 inotify_config_wd = inotify_add_watch(inotify_fd,
7431 llua_inotify_query(ev->wd, ev->mask);
7433 #endif /* HAVE_LUA */
7434 idx += INOTIFY_EVENT_SIZE + ev->len;
7438 #endif /* HAVE_SYS_INOTIFY_H */
7440 g_signal_pending = 0;
7443 #ifdef HAVE_SYS_INOTIFY_H
7444 if (inotify_fd != -1) {
7445 inotify_rm_watch(inotify_fd, inotify_config_wd);
7447 inotify_fd = inotify_config_wd = 0;
7449 #endif /* HAVE_SYS_INOTIFY_H */
7452 X11_destroy_window();
7456 static void load_config_file(const char *);
7457 static void load_config_file_x11(const char *);
7459 /* reload the config file */
7460 static void reload_config(void)
7462 timed_thread_destroy_registered_threads();
7464 if (info.cpu_usage) {
7465 free(info.cpu_usage);
7466 info.cpu_usage = NULL;
7477 #ifdef TCP_PORT_MONITOR
7478 tcp_portmon_clear();
7483 #endif /* HAVE_LUA */
7486 X11_destroy_window();
7489 if (current_config) {
7491 load_config_file(current_config);
7492 load_config_file_x11(current_config);
7494 /* re-init specials array */
7495 if ((specials = realloc((void *) specials,
7496 sizeof(struct special_t) * max_specials)) == 0) {
7497 ERR("failed to realloc specials array");
7501 if (output_methods & TO_X) {
7502 X11_initialisation();
7505 extract_variable_text(global_text);
7511 tmpstring1 = malloc(text_buffer_size);
7512 memset(tmpstring1, 0, text_buffer_size);
7516 tmpstring2 = malloc(text_buffer_size);
7517 memset(tmpstring2, 0, text_buffer_size);
7521 text_buffer = malloc(max_user_text);
7522 memset(text_buffer, 0, max_user_text);
7524 X11_create_window();
7530 static void clean_up(void)
7533 timed_thread_destroy_registered_threads();
7535 if (info.cpu_usage) {
7536 free(info.cpu_usage);
7537 info.cpu_usage = NULL;
7540 if (output_methods & TO_X) {
7543 XdbeDeallocateBackBufferName(display, window.back_buffer);
7548 XDestroyWindow(display, window.window);
7549 XClearWindow(display, RootWindow(display, screen));
7554 XClearWindow(display, RootWindow(display, screen));
7564 for (i = 0; i < MAX_TEMPLATES; i++) {
7571 free_text_objects(&global_root_object, 0);
7590 free(current_config);
7592 #ifdef TCP_PORT_MONITOR
7593 tcp_portmon_clear();
7599 free_weather_info();
7603 #endif /* HAVE_LUA */
7606 for (i = 0; i < special_count; i++) {
7607 if (specials[i].type == GRAPH) {
7608 free(specials[i].graph);
7615 clear_diskio_stats();
7616 if(global_cpu != NULL) free(global_cpu);
7619 static int string_to_bool(const char *s)
7622 // Assumes an option without a true/false means true
7624 } else if (strcasecmp(s, "yes") == EQUAL) {
7626 } else if (strcasecmp(s, "true") == EQUAL) {
7628 } else if (strcasecmp(s, "1") == EQUAL) {
7635 static enum alignment string_to_alignment(const char *s)
7637 if (strcasecmp(s, "top_left") == EQUAL) {
7639 } else if (strcasecmp(s, "top_right") == EQUAL) {
7641 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7643 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7645 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7646 return BOTTOM_RIGHT;
7647 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7648 return BOTTOM_MIDDLE;
7649 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7651 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7652 return MIDDLE_RIGHT;
7653 } else if (strcasecmp(s, "tl") == EQUAL) {
7655 } else if (strcasecmp(s, "tr") == EQUAL) {
7657 } else if (strcasecmp(s, "tm") == EQUAL) {
7659 } else if (strcasecmp(s, "bl") == EQUAL) {
7661 } else if (strcasecmp(s, "br") == EQUAL) {
7662 return BOTTOM_RIGHT;
7663 } else if (strcasecmp(s, "bm") == EQUAL) {
7664 return BOTTOM_MIDDLE;
7665 } else if (strcasecmp(s, "ml") == EQUAL) {
7667 } else if (strcasecmp(s, "mr") == EQUAL) {
7668 return MIDDLE_RIGHT;
7669 } else if (strcasecmp(s, "none") == EQUAL) {
7677 static void set_default_configurations_for_x(void)
7679 default_fg_color = WhitePixel(display, screen);
7680 default_bg_color = BlackPixel(display, screen);
7681 default_out_color = BlackPixel(display, screen);
7682 color0 = default_fg_color;
7683 color1 = default_fg_color;
7684 color2 = default_fg_color;
7685 color3 = default_fg_color;
7686 color4 = default_fg_color;
7687 color5 = default_fg_color;
7688 color6 = default_fg_color;
7689 color7 = default_fg_color;
7690 color8 = default_fg_color;
7691 color9 = default_fg_color;
7695 static void set_default_configurations(void)
7703 fork_to_background = 0;
7704 total_run_times = 0;
7705 info.cpu_avg_samples = 2;
7706 info.net_avg_samples = 2;
7707 info.diskio_avg_samples = 2;
7712 format_human_readable = 1;
7719 mpd_env_host = getenv("MPD_HOST");
7720 mpd_env_port = getenv("MPD_PORT");
7722 if (!mpd_env_host || !strlen(mpd_env_host)) {
7723 mpd_set_host("localhost");
7725 /* MPD_HOST environment variable is set */
7726 char *mpd_hostpart = strchr(mpd_env_host, '@');
7727 if (!mpd_hostpart) {
7728 mpd_set_host(mpd_env_host);
7730 /* MPD_HOST contains a password */
7731 char mpd_password[mpd_hostpart - mpd_env_host + 1];
7732 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
7734 if (!strlen(mpd_hostpart + 1)) {
7735 mpd_set_host("localhost");
7737 mpd_set_host(mpd_hostpart + 1);
7740 mpd_set_password(mpd_password, 1);
7745 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
7746 /* failed to set port from environment variable */
7747 mpd_set_port("6600");
7751 info.xmms2.artist = NULL;
7752 info.xmms2.album = NULL;
7753 info.xmms2.title = NULL;
7754 info.xmms2.genre = NULL;
7755 info.xmms2.comment = NULL;
7756 info.xmms2.url = NULL;
7757 info.xmms2.status = NULL;
7758 info.xmms2.playlist = NULL;
7760 use_spacer = NO_SPACER;
7762 output_methods = TO_X;
7764 output_methods = TO_STDOUT;
7767 show_graph_scale = 0;
7768 show_graph_range = 0;
7771 draw_graph_borders = 1;
7773 set_first_font("6x10");
7781 window.type = TYPE_NORMAL;
7783 strcpy(window.class_name, PACKAGE_NAME);
7784 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
7786 stippled_borders = 0;
7787 border_inner_margin = 3;
7788 border_outer_margin = 1;
7790 text_alignment = BOTTOM_LEFT;
7791 info.x11.monitor.number = 1;
7792 info.x11.monitor.current = 0;
7795 for (i = 0; i < MAX_TEMPLATES; i++) {
7798 template[i] = strdup("");
7801 free(current_mail_spool);
7805 variable_substitute(MAIL_FILE, buf, 256);
7806 if (buf[0] != '\0') {
7807 current_mail_spool = strndup(buf, text_buffer_size);
7812 update_interval = 3.0;
7813 info.music_player_interval = 1.0;
7814 stuff_in_upper_case = 0;
7815 info.users.number = 1;
7817 #ifdef TCP_PORT_MONITOR
7818 /* set default connection limit */
7819 tcp_portmon_set_max_connections(0);
7823 /* returns 1 if you can overwrite or create the file at 'path' */
7824 static _Bool overwrite_works(const char *path)
7828 if (!(filepointer = fopen(path, "w")))
7830 fclose(filepointer);
7834 /* returns 1 if you can append or create the file at 'path' */
7835 static _Bool append_works(const char *path)
7839 if (!(filepointer = fopen(path, "a")))
7841 fclose(filepointer);
7847 /* WARNING, this type not in Xlib spec */
7848 int x11_error_handler(Display *d, XErrorEvent *err)
7849 __attribute__((noreturn));
7850 int x11_error_handler(Display *d, XErrorEvent *err)
7852 ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
7854 (long unsigned)err->display,
7855 (long)err->resourceid,
7865 int x11_ioerror_handler(Display *d)
7866 __attribute__((noreturn));
7867 int x11_ioerror_handler(Display *d)
7869 ERR("X Error: Display %lx\n",
7876 static void X11_initialisation(void)
7878 if (x_initialised == YES) return;
7879 output_methods |= TO_X;
7881 set_default_configurations_for_x();
7882 x_initialised = YES;
7885 /* WARNING, this type not in Xlib spec */
7886 XSetErrorHandler(&x11_error_handler);
7887 XSetIOErrorHandler(&x11_ioerror_handler);
7891 static void X11_destroy_window(void)
7893 /* this function only exists for the sake of consistency */
7894 if (output_methods & TO_X) {
7896 XDamageDestroy(display, x11_stuff.damage);
7897 XFixesDestroyRegion(display, x11_stuff.region2);
7898 XFixesDestroyRegion(display, x11_stuff.part);
7899 if (x11_stuff.region) {
7900 XDestroyRegion(x11_stuff.region);
7902 x11_stuff.region = NULL;
7903 #endif /* HAVE_XDAMAGE */
7909 static char **xargv = 0;
7910 static int xargc = 0;
7912 static void X11_create_window(void)
7914 if (output_methods & TO_X) {
7916 init_window(own_window, text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2,
7917 text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2, set_transparent, background_colour,
7919 #else /* OWN_WINDOW */
7920 init_window(0, text_width + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2,
7921 text_height + border_inner_margin * 2 + border_outer_margin * 2 + border_width * 2, set_transparent, 0,
7923 #endif /* OWN_WINDOW */
7927 update_text_area(); /* to position text/window on screen */
7930 if (own_window && !fixed_pos) {
7931 XMoveWindow(display, window.window, window.x, window.y);
7934 set_transparent_background(window.window);
7942 x11_stuff.region = XCreateRegion();
7944 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
7945 ERR("Xdamage extension unavailable");
7947 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
7948 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
7949 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
7950 #endif /* HAVE_XDAMAGE */
7953 update_text_area(); /* to get initial size of the window */
7958 #define CONF_ERR ERR("%s: %d: config file error", f, line)
7959 #define CONF_ERR2(a) ERR("%s: %d: config file error: %s", f, line, a)
7960 #define CONF2(a) if (strcasecmp(name, a) == 0)
7961 #define CONF(a) else CONF2(a)
7962 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
7963 || strcasecmp(name, b) == 0)
7964 #define CONF_CONTINUE 1
7965 #define CONF_BREAK 2
7966 #define CONF_BUFF_SIZE 512
7968 static FILE *open_config_file(const char *f)
7970 #ifdef CONFIG_OUTPUT
7971 if (!strcmp(f, "==builtin==")) {
7972 return conf_cookie_open();
7974 #endif /* CONFIG_OUTPUT */
7975 return fopen(f, "r");
7978 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
7982 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
7985 remove_comments(buf);
7990 while (*p && isspace((int) *p)) {
7994 return CONF_CONTINUE; /* empty line */
8001 while (*p2 && !isspace((int) *p2)) {
8005 *p2 = '\0'; /* break at name's end */
8012 while (*p && isspace((int) *p)) {
8018 p2 = *value + strlen(*value);
8019 while (isspace((int) *(p2 - 1))) {
8028 static void load_config_file(const char *f)
8033 set_default_configurations();
8034 fp = open_config_file(f);
8038 DBGP("reading contents from config file '%s'", f);
8041 char buff[CONF_BUFF_SIZE], *name, *value;
8042 int ret = do_config_step(&line, fp, buff, &name, &value);
8043 if (ret == CONF_BREAK) {
8045 } else if (ret == CONF_CONTINUE) {
8051 /* don't listen if X is already initialised or
8052 * if we already know we don't want it */
8053 if(x_initialised == NO) {
8054 if (string_to_bool(value)) {
8055 output_methods &= TO_X;
8057 output_methods &= ~TO_X;
8058 x_initialised = NEVER;
8063 if (!value || x_initialised == YES) {
8068 disp = strdup(value);
8072 if (window.type == TYPE_DOCK)
8075 int a = string_to_alignment(value);
8086 CONF("background") {
8087 fork_to_background = string_to_bool(value);
8090 CONF2("background") {
8091 fork_to_background = string_to_bool(value);
8095 CONF("show_graph_scale") {
8096 show_graph_scale = string_to_bool(value);
8098 CONF("show_graph_range") {
8099 show_graph_range = string_to_bool(value);
8101 CONF("border_margin") {
8102 ERR("border_margin is deprecated, please use border_inner_margin instead");
8104 border_inner_margin = strtol(value, 0, 0);
8105 if (border_inner_margin < 0) border_inner_margin = 0;
8110 CONF("border_inner_margin") {
8112 border_inner_margin = strtol(value, 0, 0);
8113 if (border_inner_margin < 0) border_inner_margin = 0;
8118 CONF("border_outer_margin") {
8120 border_outer_margin = strtol(value, 0, 0);
8121 if (border_outer_margin < 0) border_outer_margin = 0;
8126 CONF("border_width") {
8128 border_width = strtol(value, 0, 0);
8129 if (border_width < 0) border_width = 0;
8135 #define TEMPLATE_CONF(n) \
8136 CONF("template"#n) { \
8138 free(template[n]); \
8139 template[n] = strdup(value); \
8156 info.mail = parse_mail_args(IMAP_TYPE, value);
8163 info.mail = parse_mail_args(POP3_TYPE, value);
8168 CONF("default_bar_size") {
8171 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8178 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8182 CONF("default_graph_size") {
8185 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8192 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8195 CONF("default_gauge_size") {
8198 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8205 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8212 mpd_set_host(value);
8218 if (value && mpd_set_port(value)) {
8222 CONF("mpd_password") {
8224 mpd_set_password(value, 0);
8230 CONF("music_player_interval") {
8232 info.music_player_interval = strtod(value, 0);
8238 CONF("sensor_device") {
8240 sensor_device = strtol(value, 0, 0);
8246 CONF("cpu_avg_samples") {
8248 cpu_avg_samples = strtol(value, 0, 0);
8249 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8252 info.cpu_avg_samples = cpu_avg_samples;
8258 CONF("net_avg_samples") {
8260 net_avg_samples = strtol(value, 0, 0);
8261 if (net_avg_samples < 1 || net_avg_samples > 14) {
8264 info.net_avg_samples = net_avg_samples;
8270 CONF("diskio_avg_samples") {
8272 diskio_avg_samples = strtol(value, 0, 0);
8273 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8276 info.diskio_avg_samples = diskio_avg_samples;
8284 CONF("double_buffer") {
8285 use_xdbe = string_to_bool(value);
8289 CONF("override_utf8_locale") {
8290 utf8_mode = string_to_bool(value);
8292 CONF("draw_borders") {
8293 draw_borders = string_to_bool(value);
8295 CONF("draw_graph_borders") {
8296 draw_graph_borders = string_to_bool(value);
8298 CONF("draw_shades") {
8299 draw_shades = string_to_bool(value);
8301 CONF("draw_outline") {
8302 draw_outline = string_to_bool(value);
8305 CONF("out_to_console") {
8306 if(string_to_bool(value))
8307 output_methods |= TO_STDOUT;
8309 CONF("out_to_stderr") {
8310 if(string_to_bool(value))
8311 output_methods |= TO_STDERR;
8313 CONF("overwrite_file") {
8314 if(overwrite_file) {
8315 free(overwrite_file);
8318 if(overwrite_works(value)) {
8319 overwrite_file = strdup(value);
8320 output_methods |= OVERWRITE_FILE;
8322 ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8324 CONF("append_file") {
8329 if(append_works(value)) {
8330 append_file = strdup(value);
8331 output_methods |= APPEND_FILE;
8333 ERR("append_file won't be able to create/append '%s'", value);
8335 CONF("use_spacer") {
8337 if (strcasecmp(value, "left") == EQUAL) {
8338 use_spacer = LEFT_SPACER;
8339 } else if (strcasecmp(value, "right") == EQUAL) {
8340 use_spacer = RIGHT_SPACER;
8341 } else if (strcasecmp(value, "none") == EQUAL) {
8342 use_spacer = NO_SPACER;
8344 use_spacer = string_to_bool(value);
8345 ERR("use_spacer should have an argument of left, right, or"
8346 " none. '%s' seems to be some form of '%s', so"
8347 " defaulting to %s.", value,
8348 use_spacer ? "true" : "false",
8349 use_spacer ? "right" : "none");
8351 use_spacer = RIGHT_SPACER;
8353 use_spacer = NO_SPACER;
8357 ERR("use_spacer should have an argument. Defaulting to right.");
8358 use_spacer = RIGHT_SPACER;
8364 use_xft = string_to_bool(value);
8368 set_first_font(value);
8372 if (value && font_count >= 0) {
8373 fonts[0].font_alpha = atof(value) * 65535.0;
8380 if (string_to_bool(value)) {
8381 ERR("Xft not enabled at compile time");
8385 /* xftfont silently ignored when no Xft */
8388 /* xftalpha is silently ignored when no Xft */
8393 set_first_font(value);
8401 gap_x = atoi(value);
8408 gap_y = atoi(value);
8414 CONF("mail_spool") {
8418 variable_substitute(value, buffer, 256);
8420 if (buffer[0] != '\0') {
8421 if (current_mail_spool) {
8422 free(current_mail_spool);
8424 current_mail_spool = strndup(buffer, text_buffer_size);
8431 CONF("minimum_size") {
8433 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8435 if (sscanf(value, "%d", &minimum_width) != 1) {
8443 CONF("maximum_width") {
8445 if (sscanf(value, "%d", &maximum_width) != 1) {
8453 CONF("no_buffers") {
8454 no_buffers = string_to_bool(value);
8456 CONF("top_name_width") {
8458 if (sscanf(value, "%u", &top_name_width) != 1) {
8464 if (top_name_width >= max_user_text) {
8465 top_name_width = max_user_text - 1;
8468 CONF("top_cpu_separate") {
8469 cpu_separate = string_to_bool(value);
8471 CONF("short_units") {
8472 short_units = string_to_bool(value);
8474 CONF("format_human_readable") {
8475 format_human_readable = string_to_bool(value);
8477 CONF("pad_percents") {
8478 pad_percents = atoi(value);
8482 CONF("own_window") {
8484 own_window = string_to_bool(value);
8487 CONF("own_window_class") {
8489 memset(window.class_name, 0, sizeof(window.class_name));
8490 strncpy(window.class_name, value,
8491 sizeof(window.class_name) - 1);
8494 CONF("own_window_title") {
8496 memset(window.title, 0, sizeof(window.title));
8497 strncpy(window.title, value, sizeof(window.title) - 1);
8500 CONF("own_window_transparent") {
8502 set_transparent = string_to_bool(value);
8505 CONF("own_window_hints") {
8507 char *p_hint, *p_save;
8508 char delim[] = ", ";
8510 /* tokenize the value into individual hints */
8511 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8513 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8514 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8515 SET_HINT(window.hints, HINT_UNDECORATED);
8516 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8517 SET_HINT(window.hints, HINT_BELOW);
8518 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8519 SET_HINT(window.hints, HINT_ABOVE);
8520 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8521 SET_HINT(window.hints, HINT_STICKY);
8522 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8523 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8524 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8525 SET_HINT(window.hints, HINT_SKIP_PAGER);
8530 p_hint = strtok_r(NULL, delim, &p_save);
8531 } while (p_hint != NULL);
8537 CONF("own_window_type") {
8539 if (strncmp(value, "normal", 6) == EQUAL) {
8540 window.type = TYPE_NORMAL;
8541 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8542 window.type = TYPE_DESKTOP;
8543 } else if (strncmp(value, "dock", 4) == EQUAL) {
8544 window.type = TYPE_DOCK;
8545 text_alignment = TOP_LEFT;
8546 } else if (strncmp(value, "panel", 5) == EQUAL) {
8547 window.type = TYPE_PANEL;
8548 } else if (strncmp(value, "override", 8) == EQUAL) {
8549 window.type = TYPE_OVERRIDE;
8558 CONF("stippled_borders") {
8560 stippled_borders = strtol(value, 0, 0);
8562 stippled_borders = 4;
8566 CONF("imlib_cache_size") {
8568 cimlib_set_cache_size(atoi(value));
8571 CONF("imlib_cache_flush_interval") {
8573 cimlib_set_cache_flush_interval(atoi(value));
8578 CONF("update_interval") {
8580 update_interval = strtod(value, 0);
8584 if (info.music_player_interval == 0) {
8585 // default to update_interval
8586 info.music_player_interval = update_interval;
8589 CONF("total_run_times") {
8591 total_run_times = strtod(value, 0);
8597 stuff_in_upper_case = string_to_bool(value);
8599 CONF("max_specials") {
8601 max_specials = atoi(value);
8606 CONF("max_user_text") {
8608 max_user_text = atoi(value);
8613 CONF("text_buffer_size") {
8615 text_buffer_size = atoi(value);
8616 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8617 ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8618 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8626 if (output_methods & TO_X) {
8627 X11_initialisation();
8636 global_text = (char *) malloc(1);
8637 global_text[0] = '\0';
8640 unsigned int l = strlen(global_text);
8642 char buf[CONF_BUFF_SIZE];
8644 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8650 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8658 /* Check for continuation of \\-\n. */
8659 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8660 global_text[l-1] = '\0';
8664 global_text = (char *) realloc(global_text, l + bl + 1);
8665 strcat(global_text, buf);
8667 if (strlen(global_text) > max_user_text) {
8672 if (strlen(global_text) < 1) {
8673 CRIT_ERR("no text supplied in configuration; exiting");
8675 global_text_lines = line + 1;
8678 #ifdef TCP_PORT_MONITOR
8679 CONF("max_port_monitor_connections") {
8681 if (!value || (sscanf(value, "%d", &max) != 1)) {
8682 /* an error. use default, warn and continue. */
8683 tcp_portmon_set_max_connections(0);
8685 } else if (tcp_portmon_set_max_connections(max)) {
8686 /* max is < 0, default has been set*/
8691 CONF("if_up_strictness") {
8693 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8694 ifup_strictness = IFUP_UP;
8695 } else if (strcasecmp(value, "up") == EQUAL) {
8696 ifup_strictness = IFUP_UP;
8697 } else if (strcasecmp(value, "link") == EQUAL) {
8698 ifup_strictness = IFUP_LINK;
8699 } else if (strcasecmp(value, "address") == EQUAL) {
8700 ifup_strictness = IFUP_ADDR;
8702 ERR("incorrect if_up_strictness value, defaulting to 'up'");
8703 ifup_strictness = IFUP_UP;
8707 CONF("temperature_unit") {
8709 ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
8710 } else if (set_temp_output_unit(value)) {
8711 ERR("temperature_unit: incorrect argument");
8717 size_t maxlength = strlen(value); //+1 for terminating 0 not needed, 'cause of the space in the middle of value
8718 char *skey = malloc(maxlength);
8719 char *svalue = malloc(maxlength);
8721 if (sscanf(value, "%[0-9a-zA-Z_] %[^\n]", skey, svalue) == 2) {
8722 oldvalue = getenv(skey);
8723 if (oldvalue == NULL) {
8724 setenv(skey, svalue, 0);
8726 //PS: Don't free oldvalue, it's the real envvar, not a copy
8740 char *ptr = strtok(value, " ");
8743 ptr = strtok(NULL, " ");
8749 #endif /* HAVE_LUA */
8761 CONF("default_color"){}
8762 CONF3("default_shade_color", "default_shadecolor"){}
8763 CONF3("default_outline_color", "default_outlinecolor") {}
8764 CONF("own_window_colour") {}
8767 ERR("%s: %d: no such configuration: '%s'", f, line, name);
8773 if (info.music_player_interval == 0) {
8774 // default to update_interval
8775 info.music_player_interval = update_interval;
8777 if (!global_text) { // didn't supply any text
8778 CRIT_ERR("missing text block in configuration; exiting");
8782 static void load_config_file_x11(const char *f)
8787 fp = open_config_file(f);
8791 DBGP("reading contents from config file '%s'", f);
8794 char buff[CONF_BUFF_SIZE], *name, *value;
8795 int ret = do_config_step(&line, fp, buff, &name, &value);
8796 if (ret == CONF_BREAK) {
8798 } else if (ret == CONF_CONTINUE) {
8804 X11_initialisation();
8805 if (x_initialised == YES) {
8807 color0 = get_x11_color(value);
8814 X11_initialisation();
8815 if (x_initialised == YES) {
8817 color1 = get_x11_color(value);
8824 X11_initialisation();
8825 if (x_initialised == YES) {
8827 color2 = get_x11_color(value);
8834 X11_initialisation();
8835 if (x_initialised == YES) {
8837 color3 = get_x11_color(value);
8844 X11_initialisation();
8845 if (x_initialised == YES) {
8847 color4 = get_x11_color(value);
8854 X11_initialisation();
8855 if (x_initialised == YES) {
8857 color5 = get_x11_color(value);
8864 X11_initialisation();
8865 if (x_initialised == YES) {
8867 color6 = get_x11_color(value);
8874 X11_initialisation();
8875 if (x_initialised == YES) {
8877 color7 = get_x11_color(value);
8884 X11_initialisation();
8885 if (x_initialised == YES) {
8887 color8 = get_x11_color(value);
8894 X11_initialisation();
8895 if (x_initialised == YES) {
8897 color9 = get_x11_color(value);
8903 CONF("default_color") {
8904 X11_initialisation();
8905 if (x_initialised == YES) {
8907 default_fg_color = get_x11_color(value);
8913 CONF3("default_shade_color", "default_shadecolor") {
8914 X11_initialisation();
8915 if (x_initialised == YES) {
8917 default_bg_color = get_x11_color(value);
8923 CONF3("default_outline_color", "default_outlinecolor") {
8924 X11_initialisation();
8925 if (x_initialised == YES) {
8927 default_out_color = get_x11_color(value);
8934 CONF("own_window_colour") {
8935 X11_initialisation();
8936 if (x_initialised == YES) {
8938 background_colour = get_x11_color(value);
8940 ERR("Invalid colour for own_window_colour (try omitting the "
8941 "'#' for hex colours");
8947 //initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod)
8948 if(output_methods & TO_X) {
8949 X11_initialisation();
8959 #undef CONF_CONTINUE
8960 #undef CONF_BUFF_SIZE
8967 static void print_help(const char *prog_name) {
8968 printf("Usage: %s [OPTION]...\n"
8969 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
8970 "window. Command line options will override configurations defined in config\n"
8972 " -v, --version version\n"
8973 " -q, --quiet quiet mode\n"
8974 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
8975 " -c, --config=FILE config file to load\n"
8976 #ifdef CONFIG_OUTPUT
8977 " -C, --print-config print the builtin default config to stdout\n"
8978 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
8980 " -d, --daemonize daemonize, fork to background\n"
8981 " -h, --help help\n"
8983 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
8984 " -f, --font=FONT font to use\n"
8985 " -X, --display=DISPLAY X11 display to use\n"
8987 " -o, --own-window create own window to draw\n"
8990 " -b, --double-buffer double buffer (prevents flickering)\n"
8992 " -w, --window-id=WIN_ID window id to draw\n"
8993 " -x X x position\n"
8994 " -y Y y position\n"
8996 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
8997 " -u, --interval=SECS update interval\n"
8998 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
9003 /* : means that character before that takes an argument */
9004 static const char *getopt_string = "vVqdDt:u:i:hc:"
9014 #ifdef CONFIG_OUTPUT
9019 static const struct option longopts[] = {
9020 { "help", 0, NULL, 'h' },
9021 { "version", 0, NULL, 'V' },
9022 { "debug", 0, NULL, 'D' },
9023 { "config", 1, NULL, 'c' },
9024 #ifdef CONFIG_OUTPUT
9025 { "print-config", 0, NULL, 'C' },
9027 { "daemonize", 0, NULL, 'd' },
9029 { "alignment", 1, NULL, 'a' },
9030 { "font", 1, NULL, 'f' },
9031 { "display", 1, NULL, 'X' },
9033 { "own-window", 0, NULL, 'o' },
9036 { "double-buffer", 0, NULL, 'b' },
9038 { "window-id", 1, NULL, 'w' },
9040 { "text", 1, NULL, 't' },
9041 { "interval", 0, NULL, 'u' },
9045 int main(int argc, char **argv)
9051 struct sigaction act, oact;
9053 g_signal_pending = 0;
9054 memset(&info, 0, sizeof(info));
9055 memset(template, 0, sizeof(template));
9058 #ifdef TCP_PORT_MONITOR
9059 /* set default connection limit */
9060 tcp_portmon_set_max_connections(0);
9063 /* handle command line parameters that don't change configs */
9065 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9066 || ((s = getenv("LANG")) && *s)) {
9067 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9069 ERR("malloc failed");
9071 for (x = 0; x < strlen(s); x++) {
9072 temp[x] = tolower(s[x]);
9075 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9081 if (!setlocale(LC_CTYPE, "")) {
9082 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9086 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9097 if (current_config) {
9098 free(current_config);
9100 current_config = strndup(optarg, max_user_text);
9103 freopen("/dev/null", "w", stderr);
9106 print_help(argv[0]);
9108 #ifdef CONFIG_OUTPUT
9115 window.window = strtol(optarg, 0, 0);
9124 /* check if specified config file is valid */
9125 if (current_config) {
9127 if (stat(current_config, &sb) ||
9128 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9129 ERR("invalid configuration file '%s'\n", current_config);
9130 free(current_config);
9135 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9137 if (!current_config) {
9138 /* load default config file */
9139 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9142 /* Try to use personal config file first */
9143 to_real_path(buf, CONFIG_FILE);
9144 if (buf[0] && (fp = fopen(buf, "r"))) {
9145 current_config = strndup(buf, max_user_text);
9149 /* Try to use system config file if personal config not readable */
9150 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9151 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9155 /* No readable config found */
9156 if (!current_config) {
9157 #ifdef CONFIG_OUTPUT
9158 current_config = strdup("==builtin==");
9159 ERR("no readable personal or system-wide config file found,"
9160 " using builtin default");
9162 CRIT_ERR("no readable personal or system-wide config file found");
9163 #endif /* ! CONF_OUTPUT */
9166 #ifdef HAVE_SYS_INOTIFY_H
9167 inotify_fd = inotify_init();
9168 #endif /* HAVE_SYS_INOTIFY_H */
9170 load_config_file(current_config);
9172 /* init specials array */
9173 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9174 ERR("failed to create specials array");
9178 if (current_mail_spool == NULL) {
9181 variable_substitute(MAIL_FILE, buf, 256);
9183 if (buf[0] != '\0') {
9184 current_mail_spool = strndup(buf, text_buffer_size);
9189 /* handle other command line arguments */
9191 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9192 || defined(__NetBSD__)
9193 optind = optreset = 1;
9198 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9199 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9200 "kvm_open")) == NULL) {
9201 CRIT_ERR("cannot read kvm");
9206 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9214 fork_to_background = 1;
9217 global_debug_level++;
9221 set_first_font(optarg);
9224 text_alignment = string_to_alignment(optarg);
9229 disp = strdup(optarg);
9248 global_text = strndup(optarg, max_user_text);
9249 convert_escapes(global_text);
9253 update_interval = strtod(optarg, 0);
9254 if (info.music_player_interval == 0) {
9255 // default to update_interval
9256 info.music_player_interval = update_interval;
9261 total_run_times = strtod(optarg, 0);
9265 gap_x = atoi(optarg);
9269 gap_y = atoi(optarg);
9280 if (output_methods & TO_X) {
9281 load_config_file_x11(current_config);
9285 /* generate text and get initial size */
9286 extract_variable_text(global_text);
9293 if (fork_to_background) {
9298 ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9305 fprintf(stderr, "\n");
9310 /* parent process */
9311 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9318 text_buffer = malloc(max_user_text);
9319 memset(text_buffer, 0, max_user_text);
9320 tmpstring1 = malloc(text_buffer_size);
9321 memset(tmpstring1, 0, text_buffer_size);
9322 tmpstring2 = malloc(text_buffer_size);
9323 memset(tmpstring2, 0, text_buffer_size);
9328 X11_create_window();
9331 /* Set signal handlers */
9332 act.sa_handler = signal_handler;
9333 sigemptyset(&act.sa_mask);
9336 act.sa_flags |= SA_RESTART;
9339 if ( sigaction(SIGINT, &act, &oact) < 0
9340 || sigaction(SIGUSR1, &act, &oact) < 0
9341 || sigaction(SIGHUP, &act, &oact) < 0
9342 || sigaction(SIGTERM, &act, &oact) < 0) {
9343 ERR("error setting signal handler: %s", strerror(errno));
9348 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9355 static void signal_handler(int sig)
9357 /* signal handler is light as a feather, as it should be.
9358 * we will poll g_signal_pending with each loop of conky
9359 * and do any signal processing there, NOT here. */
9360 g_signal_pending = sig;