1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
33 #include "text_object.h"
48 #include <sys/param.h>
49 #ifdef HAVE_SYS_INOTIFY_H
50 #include <sys/inotify.h>
51 #endif /* HAVE_SYS_INOTIFY_H */
54 #include <X11/Xutil.h>
56 #include <X11/extensions/Xdamage.h>
62 #include <sys/types.h>
64 #include <netinet/in.h>
72 #include <libxml/parser.h>
89 #include "temphelper.h"
93 /* check for OS and include appropriate headers */
94 #if defined(__linux__)
96 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
98 #elif defined(__OpenBSD__)
102 #if defined(__FreeBSD_kernel__)
106 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
107 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
108 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
109 int apm_getinfo(int fd, apm_info_t aip);
110 char *get_apm_adapter(void);
111 char *get_apm_battery_life(void);
112 char *get_apm_battery_time(void);
120 #include "defconfig.h"
121 #include "conf_cookie.h"
125 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
128 #define MAIL_FILE "$MAIL"
129 #define MAX_IF_BLOCK_DEPTH 5
131 //#define SIGNAL_BLOCKING
132 #undef SIGNAL_BLOCKING
134 /* debugging level, used by logging.h */
135 int global_debug_level = 0;
137 /* two strings for internal use */
138 static char *tmpstring1, *tmpstring2;
140 /* variables holding various config settings */
142 int format_human_readable;
149 int top_cpu, top_mem, top_time;
153 static unsigned int top_name_width = 15;
155 static int extra_newline;
156 enum x_initialiser_state x_initialised = NO;
157 static volatile int g_signal_pending;
158 /* Update interval */
159 double update_interval;
160 double update_interval_old;
161 double update_interval_bat;
162 void *global_cpu = NULL;
168 /* prototypes for internally used functions */
169 static void signal_handler(int);
170 static void print_version(void) __attribute__((noreturn));
171 static void reload_config(void);
172 static void generate_text_internal(char *, int, struct text_object,
173 struct information *);
174 static int extract_variable_text_internal(struct text_object *,
177 static void print_version(void)
179 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
181 printf("\nCompiled in features:\n\n"
182 "System config file: "SYSTEM_CONFIG_FILE"\n"
183 "Package library path: "PACKAGE_LIBDIR"\n\n"
187 " * Xdamage extension\n"
188 # endif /* HAVE_XDAMAGE */
190 " * XDBE (double buffer extension)\n"
191 # endif /* HAVE_XDBE */
196 "\n Music detection:\n"
199 #endif /* AUDACIOUS */
215 #endif /* HAVE_OPENMP */
222 #ifdef TCP_PORT_MONITOR
224 #endif /* TCP_PORT_MONITOR */
227 #endif /* HAVE_CURL */
232 " * Weather (METAR)\n"
234 " * Weather (XOAP)\n"
239 #endif /* HAVE_IWLIB */
241 " * support for IBM/Lenovo notebooks\n"
251 #endif /* CONFIG_OUTPUT */
256 " * ALSA mixer support\n"
257 #endif /* MIXER_IS_ALSA */
267 #ifdef HAVE_LUA_CAIRO
269 #endif /* HAVE_LUA_CAIRO */
270 #ifdef HAVE_LUA_IMLIB2
273 #endif /* HAVE_LUA */
279 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
284 static void X11_create_window(void);
285 static void X11_initialisation(void);
287 struct _x11_stuff_s {
291 XserverRegion region2, part;
292 int event_base, error_base;
298 static int text_start_x, text_start_y; /* text start position in window */
299 static int text_width, text_height;
314 /* display to connect to */
315 static char *disp = NULL;
319 /* struct that has all info to be shared between
320 * instances of the same text object */
321 struct information info;
323 /* path to config file */
324 char *current_config;
326 /* set to 1 if you want all text to be in uppercase */
327 static unsigned int stuff_in_uppercase;
329 /* Run how many times? */
330 static unsigned long total_run_times;
333 static int fork_to_background;
335 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
337 /* filenames for output */
338 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
339 char *append_file = NULL; FILE *append_fpointer = NULL;
343 static int show_graph_scale;
344 static int show_graph_range;
346 /* Position on the screen */
347 static int text_alignment;
348 static int gap_x, gap_y;
351 static int draw_borders;
352 static int draw_graph_borders;
353 static int stippled_borders;
355 static int draw_shades, draw_outline;
357 static long default_fg_color, default_bg_color, default_out_color;
359 /* create own window or draw stuff to root? */
360 static int set_transparent = 0;
363 static int own_window = 0;
364 static int background_colour = 0;
366 /* fixed size/pos is set if wm/user changes them */
367 static int fixed_size = 0, fixed_pos = 0;
370 static int minimum_width, minimum_height;
371 static int maximum_width;
376 static int sensor_device;
379 static long color0, color1, color2, color3, color4, color5, color6, color7,
382 #define MAX_TEMPLATES 10
383 static char *template[MAX_TEMPLATES];
385 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
386 unsigned int max_user_text;
388 /* maximum size of individual text buffers, ie $exec buffer size */
389 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
392 #define CODEPAGE_LENGTH 20
394 long iconv_count = 0;
395 char iconv_converting;
396 static iconv_t **iconv_cd = 0;
398 int register_iconv(iconv_t *new_iconv)
400 iconv_cd = realloc(iconv_cd, sizeof(iconv_t *) * (iconv_count + 1));
402 CRIT_ERR(NULL, NULL, "Out of memory");
404 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
405 if (!iconv_cd[iconv_count]) {
406 CRIT_ERR(NULL, NULL, "Out of memory");
408 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
413 void free_iconv(void)
418 for (i = 0; i < iconv_count; i++) {
420 iconv_close(*iconv_cd[i]);
434 /* no buffers in used memory? */
437 /* pad percentages to decimals? */
438 static int pad_percents = 0;
440 static char *global_text = 0;
441 long global_text_lines;
443 static int total_updates;
444 static int updatereset;
446 int check_contains(char *f, char *s)
449 FILE *where = open_file(f, 0);
454 while (fgets(buf1, 256, where)) {
455 if (strstr(buf1, s)) {
462 NORM_ERR("Could not open the file");
467 #define SECRIT_MULTILINE_CHAR '\x02'
471 static inline int calc_text_width(const char *s, int l)
473 if ((output_methods & TO_X) == 0) {
481 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
482 (const FcChar8 *) s, l, &gi);
484 XftTextExtents8(display, fonts[selected_font].xftfont,
485 (const FcChar8 *) s, l, &gi);
491 return XTextWidth(fonts[selected_font].font, s, l);
496 /* formatted text to render on screen, generated in generate_text(),
497 * drawn in draw_stuff() */
499 static char *text_buffer;
501 /* quite boring functions */
503 static inline void for_each_line(char *b, int f(char *, int))
506 int special_index = 0; /* specials index */
508 for (ps = b, pe = b; *pe; pe++) {
511 special_index = f(ps, special_index);
518 f(ps, special_index);
522 static void convert_escapes(char *buf)
524 char *p = buf, *s = buf;
531 } else if (*s == '\\') {
542 /* Prints anything normally printed with snprintf according to the current value
543 * of use_spacer. Actually slightly more flexible than snprintf, as you can
544 * safely specify the destination buffer as one of your inputs. */
545 int spaced_print(char *buf, int size, const char *format, int width, ...)
554 tempbuf = malloc(size * sizeof(char));
556 // Passes the varargs along to vsnprintf
557 va_start(argp, width);
558 vsnprintf(tempbuf, size, format, argp);
561 switch (use_spacer) {
563 len = snprintf(buf, size, "%s", tempbuf);
566 len = snprintf(buf, size, "%*s", width, tempbuf);
569 len = snprintf(buf, size, "%-*s", width, tempbuf);
576 /* print percentage values
578 * - i.e., unsigned values between 0 and 100
579 * - respect the value of pad_percents */
580 static int percent_print(char *buf, int size, unsigned value)
582 return spaced_print(buf, size, "%u", pad_percents, value);
585 /* converts from bytes to human readable format (K, M, G, T)
587 * The algorithm always divides by 1024, as unit-conversion of byte
588 * counts suggests. But for output length determination we need to
589 * compare with 1000 here, as we print in decimal form. */
590 static void human_readable(long long num, char *buf, int size)
592 const char **suffix = suffixes;
598 /* Possibly just output as usual, for example for stdout usage */
599 if (!format_human_readable) {
600 spaced_print(buf, size, "%d", 6, round_to_int(num));
611 if (llabs(num) < 1000LL) {
612 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
616 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
624 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
626 * The goal is to always have a significance of 3, by
627 * adjusting the decimal part of the number. Sample output:
631 * so the point of alignment resides between number and unit. The
632 * upside of this is that there is minimal padding necessary, though
633 * there should be a way to make alignment take place at the decimal
634 * dot (then with fixed width decimal part).
636 * Note the repdigits below: when given a precision value, printf()
637 * rounds the float to it, not just cuts off the remaining digits. So
638 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
639 * printed with a precision of 0. Yay. */
641 precision = 0; /* print 100-999 without decimal part */
643 precision = 1; /* print 10-99 with one decimal place */
645 precision = 2; /* print 0-9 with two decimal places */
647 spaced_print(buf, size, format, width, precision, fnum, *suffix);
650 /* global object list root element */
651 static struct text_object global_root_object;
653 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
654 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
655 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
656 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
658 int parentend, childend;
660 //by running pipe after the strcmp's we make sure that we don't have to create a pipe
661 //and close the ends if mode is something illegal
662 if(strcmp(mode, "r") == 0) {
663 if(pipe(ends) != 0) {
668 } else if(strcmp(mode, "w") == 0) {
669 if(pipe(ends) != 0) {
682 } else if(*child > 0) {
684 waitpid(*child, NULL, 0);
686 //don't read from both stdin and pipe or write to both stdout and pipe
687 if(childend == ends[0]) {
692 dup(childend); //by dupping childend, the returned fd will have close-on-exec turned off
693 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
694 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail)
696 return fdopen(parentend, mode);
699 static inline void read_exec(const char *data, char *buf, const int size)
703 alarm(update_interval);
704 fp = pid_popen(data, "r", &childpid);
708 length = fread(buf, 1, size, fp);
711 if (length > 0 && buf[length - 1] == '\n') {
712 buf[length - 1] = '\0';
720 void *threaded_exec(void *) __attribute__((noreturn));
722 void *threaded_exec(void *arg)
725 struct text_object *obj = (struct text_object *)arg;
728 buff = malloc(text_buffer_size);
729 read_exec(obj->data.texeci.cmd, buff,
738 timed_thread_lock(obj->data.texeci.p_timed_thread);
739 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
740 timed_thread_unlock(obj->data.texeci.p_timed_thread);
742 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
743 timed_thread_exit(obj->data.texeci.p_timed_thread);
749 static struct text_object *new_text_object_internal(void)
751 struct text_object *obj = malloc(sizeof(struct text_object));
752 memset(obj, 0, sizeof(struct text_object));
757 * Frees the list of text objects root points to. When internal = 1, it won't
758 * free global objects.
760 static void free_text_objects(struct text_object *root, int internal)
762 struct text_object *obj;
768 #define data obj->data
769 for (obj = root->prev; obj; obj = root->prev) {
770 root->prev = obj->prev;
776 #endif /* !__OpenBSD__ */
781 close(data.sysfs.fd);
783 #endif /* __linux__ */
785 free(data.read_tcp.host);
792 free(data.tztime.tz);
793 free(data.tztime.fmt);
796 free(data.mboxscan.args);
797 free(data.mboxscan.output);
802 case OBJ_unseen_mails:
803 case OBJ_flagged_mails:
804 case OBJ_unflagged_mails:
805 case OBJ_forwarded_mails:
806 case OBJ_unforwarded_mails:
807 case OBJ_replied_mails:
808 case OBJ_unreplied_mails:
809 case OBJ_draft_mails:
810 case OBJ_trashed_mails:
811 free(data.local_mail.mbox);
813 case OBJ_imap_unseen:
818 case OBJ_imap_messages:
823 case OBJ_pop3_unseen:
835 free_text_objects(obj->sub, 1);
838 case OBJ_if_existing:
841 free(data.ifblock.s);
842 free(data.ifblock.str);
846 free(data.headtail.logfile);
847 if(data.headtail.buffer) {
848 free(data.headtail.buffer);
865 case OBJ_iconv_start:
870 case OBJ_disk_protect:
874 free(data.ifblock.s);
875 free(data.ifblock.str);
878 if (info.gw_info.iface) {
879 free(info.gw_info.iface);
880 info.gw_info.iface = 0;
882 if (info.gw_info.ip) {
883 free(info.gw_info.ip);
887 case OBJ_ioscheduler:
892 #if (defined(__FreeBSD__) || defined(__linux__))
894 free(data.ifblock.s);
895 free(data.ifblock.str);
899 case OBJ_xmms2_artist:
900 if (info.xmms2.artist) {
901 free(info.xmms2.artist);
902 info.xmms2.artist = 0;
905 case OBJ_xmms2_album:
906 if (info.xmms2.album) {
907 free(info.xmms2.album);
908 info.xmms2.album = 0;
911 case OBJ_xmms2_title:
912 if (info.xmms2.title) {
913 free(info.xmms2.title);
914 info.xmms2.title = 0;
917 case OBJ_xmms2_genre:
918 if (info.xmms2.genre) {
919 free(info.xmms2.genre);
920 info.xmms2.genre = 0;
923 case OBJ_xmms2_comment:
924 if (info.xmms2.comment) {
925 free(info.xmms2.comment);
926 info.xmms2.comment = 0;
930 if (info.xmms2.url) {
931 free(info.xmms2.url);
936 if (info.xmms2.date) {
937 free(info.xmms2.date);
941 case OBJ_xmms2_status:
942 if (info.xmms2.status) {
943 free(info.xmms2.status);
944 info.xmms2.status = 0;
947 case OBJ_xmms2_playlist:
948 if (info.xmms2.playlist) {
949 free(info.xmms2.playlist);
950 info.xmms2.playlist = 0;
953 case OBJ_xmms2_smart:
954 if (info.xmms2.artist) {
955 free(info.xmms2.artist);
956 info.xmms2.artist = 0;
958 if (info.xmms2.title) {
959 free(info.xmms2.title);
960 info.xmms2.title = 0;
962 if (info.xmms2.url) {
963 free(info.xmms2.url);
970 case OBJ_bmpx_artist:
974 case OBJ_bmpx_bitrate:
989 free(data.rss.action);
994 free(data.weather.uri);
995 free(data.weather.data_type);
999 case OBJ_weather_forecast:
1000 free(data.weather_forecast.uri);
1001 free(data.weather_forecast.data_type);
1014 #endif /* HAVE_LUA */
1021 case OBJ_battery_short:
1024 case OBJ_battery_time:
1027 #endif /* !__OpenBSD__ */
1032 case OBJ_execigraph:
1033 case OBJ_execigauge:
1035 free(data.execi.cmd);
1036 free(data.execi.buffer);
1039 if (data.texeci.p_timed_thread) timed_thread_destroy(data.texeci.p_timed_thread, &data.texeci.p_timed_thread);
1040 free(data.texeci.cmd);
1041 free(data.texeci.buffer);
1043 case OBJ_nameserver:
1052 if (info.first_process && !internal) {
1053 free_all_processes();
1054 info.first_process = NULL;
1056 if (data.top.s) free(data.top.s);
1060 free(data.hddtemp.dev);
1061 free(data.hddtemp.addr);
1062 if (data.hddtemp.temp)
1063 free(data.hddtemp.temp);
1065 #endif /* HDDTEMP */
1066 case OBJ_entropy_avail:
1067 case OBJ_entropy_perc:
1068 case OBJ_entropy_poolsize:
1069 case OBJ_entropy_bar:
1071 case OBJ_user_names:
1072 if (info.users.names) {
1073 free(info.users.names);
1074 info.users.names = 0;
1077 case OBJ_user_terms:
1078 if (info.users.terms) {
1079 free(info.users.terms);
1080 info.users.terms = 0;
1083 case OBJ_user_times:
1084 if (info.users.times) {
1085 free(info.users.times);
1086 info.users.times = 0;
1091 case OBJ_smapi_bat_perc:
1092 case OBJ_smapi_bat_temp:
1093 case OBJ_smapi_bat_power:
1096 case OBJ_if_smapi_bat_installed:
1097 free(data.ifblock.s);
1098 free(data.ifblock.str);
1107 case OBJ_mpd_artist:
1109 case OBJ_mpd_random:
1110 case OBJ_mpd_repeat:
1112 case OBJ_mpd_bitrate:
1113 case OBJ_mpd_status:
1115 case OBJ_mpd_elapsed:
1116 case OBJ_mpd_length:
1120 case OBJ_mpd_percent:
1122 case OBJ_if_mpd_playing:
1130 case OBJ_moc_artist:
1133 case OBJ_moc_totaltime:
1134 case OBJ_moc_timeleft:
1135 case OBJ_moc_curtime:
1136 case OBJ_moc_bitrate:
1145 free_text_objects(obj->sub, 1);
1150 free(data.scroll.text);
1151 free_text_objects(obj->sub, 1);
1155 free(data.combine.left);
1156 free(data.combine.seperation);
1157 free(data.combine.right);
1158 free_text_objects(obj->sub, 1);
1163 case OBJ_apcupsd_name:
1164 case OBJ_apcupsd_model:
1165 case OBJ_apcupsd_upsmode:
1166 case OBJ_apcupsd_cable:
1167 case OBJ_apcupsd_status:
1168 case OBJ_apcupsd_linev:
1169 case OBJ_apcupsd_load:
1170 case OBJ_apcupsd_loadbar:
1172 case OBJ_apcupsd_loadgraph:
1173 case OBJ_apcupsd_loadgauge:
1175 case OBJ_apcupsd_charge:
1176 case OBJ_apcupsd_timeleft:
1177 case OBJ_apcupsd_temp:
1178 case OBJ_apcupsd_lastxfer:
1180 #endif /* APCUPSD */
1183 case OBJ_desktop_number:
1184 case OBJ_desktop_name:
1185 if(info.x11.desktop.name) {
1186 free(info.x11.desktop.name);
1187 info.x11.desktop.name = NULL;
1189 if(info.x11.desktop.all_names) {
1190 free(info.x11.desktop.all_names);
1191 info.x11.desktop.all_names = NULL;
1202 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1207 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1208 *a = mixer_init(buf1);
1209 scan_bar(arg + n, w, h);
1211 *a = mixer_init(NULL);
1212 scan_bar(arg, w, h);
1217 /* strip a leading /dev/ if any, following symlinks first
1219 * BEWARE: this function returns a pointer to static content
1220 * which gets overwritten in consecutive calls. I.e.:
1221 * this function is NOT reentrant.
1223 static const char *dev_name(const char *path)
1225 static char buf[255]; /* should be enough for pathnames */
1231 #define DEV_NAME(x) \
1232 x != NULL && strlen(x) > 5 && strncmp(x, "/dev/", 5) == 0 ? x + 5 : x
1233 if ((buflen = readlink(path, buf, 254)) == -1)
1234 return DEV_NAME(path);
1236 return DEV_NAME(buf);
1240 static int parse_top_args(const char *s, const char *arg, struct text_object *obj)
1245 if (obj->data.top.was_parsed) {
1248 obj->data.top.was_parsed = 1;
1250 if (arg && !obj->data.top.s) {
1251 obj->data.top.s = strndup(arg, text_buffer_size);
1254 need_mask |= (1 << INFO_TOP);
1257 obj->type = OBJ_top;
1259 } else if (strcmp(&s[3], "_mem") == EQUAL) {
1260 obj->type = OBJ_top_mem;
1262 } else if (strcmp(&s[3], "_time") == EQUAL) {
1263 obj->type = OBJ_top_time;
1266 } else if (strcmp(&s[3], "_io") == EQUAL) {
1267 obj->type = OBJ_top_io;
1272 NORM_ERR("Must be top, top_mem, top_time or top_io");
1274 NORM_ERR("Must be top, top_mem or top_time");
1280 NORM_ERR("top needs arguments");
1284 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1285 if (strcmp(buf, "name") == EQUAL) {
1286 obj->data.top.type = TOP_NAME;
1287 } else if (strcmp(buf, "cpu") == EQUAL) {
1288 obj->data.top.type = TOP_CPU;
1289 } else if (strcmp(buf, "pid") == EQUAL) {
1290 obj->data.top.type = TOP_PID;
1291 } else if (strcmp(buf, "mem") == EQUAL) {
1292 obj->data.top.type = TOP_MEM;
1293 } else if (strcmp(buf, "time") == EQUAL) {
1294 obj->data.top.type = TOP_TIME;
1295 } else if (strcmp(buf, "mem_res") == EQUAL) {
1296 obj->data.top.type = TOP_MEM_RES;
1297 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
1298 obj->data.top.type = TOP_MEM_VSIZE;
1300 } else if (strcmp(buf, "io_read") == EQUAL) {
1301 obj->data.top.type = TOP_READ_BYTES;
1302 } else if (strcmp(buf, "io_write") == EQUAL) {
1303 obj->data.top.type = TOP_WRITE_BYTES;
1304 } else if (strcmp(buf, "io_perc") == EQUAL) {
1305 obj->data.top.type = TOP_IO_PERC;
1308 NORM_ERR("invalid type arg for top");
1310 NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
1311 "io_read, io_write, io_perc");
1313 NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
1317 if (n < 1 || n > 10) {
1318 NORM_ERR("invalid num arg for top. Must be between 1 and 10.");
1321 obj->data.top.num = n - 1;
1324 NORM_ERR("invalid argument count for top");
1330 long current_text_color;
1334 struct conftree* horz_next;
1335 struct conftree* vert_next;
1336 struct conftree* back;
1339 //adds newstring to to the tree unless you can already see it when travelling back.
1340 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
1341 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
1342 struct conftree* node;
1343 struct conftree* node2;
1345 for(node = previous; node != NULL; node = node->back) {
1346 if(strcmp(node->string, newstring) == 0) {
1350 node = malloc(sizeof(struct conftree));
1351 if (previous != NULL) {
1352 if(previous->vert_next == NULL) {
1353 previous->vert_next = node;
1355 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
1356 node2->horz_next = node;
1359 node->string = strdup(newstring);
1360 node->horz_next = NULL;
1361 node->vert_next = NULL;
1362 node->back = previous;
1366 void conftree_empty(struct conftree* tree) {
1368 conftree_empty(tree->horz_next);
1369 conftree_empty(tree->vert_next);
1375 struct conftree *currentconffile;
1377 char load_config_file(const char *);
1379 /* construct_text_object() creates a new text_object */
1380 static struct text_object *construct_text_object(const char *s,
1381 const char *arg, long line, void **ifblock_opaque, void *free_at_crash)
1383 // struct text_object *obj = new_text_object();
1384 struct text_object *obj = new_text_object_internal();
1388 #define OBJ(a, n) if (strcmp(s, #a) == 0) { \
1389 obj->type = OBJ_##a; need_mask |= (1ULL << n); {
1390 #define OBJ_IF(a, n) if (strcmp(s, #a) == 0) { \
1391 obj->type = OBJ_##a; need_mask |= (1ULL << n); \
1392 obj_be_ifblock_if(ifblock_opaque, obj); {
1393 #define END } } else
1395 #define SIZE_DEFAULTS(arg) { \
1396 obj->a = default_##arg##_width; \
1397 obj->b = default_##arg##_height; \
1402 obj->type = OBJ_color;
1403 obj->data.l = get_x11_color(s);
1407 OBJ(freq, INFO_FREQ)
1410 obj->data.i = open_acpi_temperature(arg);
1411 END OBJ(acpiacadapter, 0)
1412 END OBJ(freq, INFO_FREQ)
1413 #endif /* !__OpenBSD__ */
1415 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1416 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1417 obj->data.cpu_index = 1;
1418 /* NORM_ERR("freq: Invalid CPU number or you don't have that many CPUs! "
1419 "Displaying the clock for CPU 1."); */
1421 obj->data.cpu_index = atoi(&arg[0]);
1424 END OBJ(freq_g, INFO_FREQ)
1426 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1427 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1428 obj->data.cpu_index = 1;
1429 /* NORM_ERR("freq_g: Invalid CPU number or you don't have that many "
1430 "CPUs! Displaying the clock for CPU 1."); */
1432 obj->data.cpu_index = atoi(&arg[0]);
1435 END OBJ(read_tcp, 0)
1437 obj->data.read_tcp.host = malloc(text_buffer_size);
1438 sscanf(arg, "%s", obj->data.read_tcp.host);
1439 sscanf(arg+strlen(obj->data.read_tcp.host), "%u", &(obj->data.read_tcp.port));
1440 if(obj->data.read_tcp.port == 0) {
1441 obj->data.read_tcp.port = atoi(obj->data.read_tcp.host);
1442 strcpy(obj->data.read_tcp.host,"localhost");
1444 obj->data.read_tcp.port = htons(obj->data.read_tcp.port);
1445 if(obj->data.read_tcp.port < 1 || obj->data.read_tcp.port > 65535) {
1446 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1449 CRIT_ERR(obj, free_at_crash, "read_tcp: Needs \"(host) port\" as argument(s)");
1451 #if defined(__linux__)
1452 END OBJ(voltage_mv, 0)
1454 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1455 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1456 obj->data.cpu_index = 1;
1457 /* NORM_ERR("voltage_mv: Invalid CPU number or you don't have that many "
1458 "CPUs! Displaying voltage for CPU 1."); */
1460 obj->data.cpu_index = atoi(&arg[0]);
1463 END OBJ(voltage_v, 0)
1465 if (!arg || !isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) == 0
1466 || (unsigned int) atoi(&arg[0]) > info.cpu_count) {
1467 obj->data.cpu_index = 1;
1468 /* NORM_ERR("voltage_v: Invalid CPU number or you don't have that many "
1469 "CPUs! Displaying voltage for CPU 1."); */
1471 obj->data.cpu_index = atoi(&arg[0]);
1476 END OBJ(wireless_essid, INFO_NET)
1478 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1480 // default to DEFAULTNETDEV
1481 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1482 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1485 END OBJ(wireless_mode, INFO_NET)
1487 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1489 // default to DEFAULTNETDEV
1490 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1491 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1494 END OBJ(wireless_bitrate, INFO_NET)
1496 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1498 // default to DEFAULTNETDEV
1499 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1500 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1503 END OBJ(wireless_ap, INFO_NET)
1505 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1507 // default to DEFAULTNETDEV
1508 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1509 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1512 END OBJ(wireless_link_qual, INFO_NET)
1514 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1516 // default to DEFAULTNETDEV
1517 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1518 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1521 END OBJ(wireless_link_qual_max, INFO_NET)
1523 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1525 // default to DEFAULTNETDEV
1526 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1527 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1530 END OBJ(wireless_link_qual_perc, INFO_NET)
1532 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1534 // default to DEFAULTNETDEV
1535 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1536 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1539 END OBJ(wireless_link_bar, INFO_NET)
1542 arg = scan_bar(arg, &obj->a, &obj->b);
1543 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1545 // default to DEFAULTNETDEV
1546 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1547 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1550 #endif /* HAVE_IWLIB */
1552 #endif /* __linux__ */
1560 sscanf(arg, "%63s", bat);
1562 strcpy(bat, "BAT0");
1564 obj->data.s = strndup(bat, text_buffer_size);
1565 END OBJ(battery_short, 0)
1569 sscanf(arg, "%63s", bat);
1571 strcpy(bat, "BAT0");
1573 obj->data.s = strndup(bat, text_buffer_size);
1574 END OBJ(battery_time, 0)
1578 sscanf(arg, "%63s", bat);
1580 strcpy(bat, "BAT0");
1582 obj->data.s = strndup(bat, text_buffer_size);
1583 END OBJ(battery_percent, 0)
1587 sscanf(arg, "%63s", bat);
1589 strcpy(bat, "BAT0");
1591 obj->data.s = strndup(bat, text_buffer_size);
1592 END OBJ(battery_bar, 0)
1597 arg = scan_bar(arg, &obj->a, &obj->b);
1598 sscanf(arg, "%63s", bat);
1600 strcpy(bat, "BAT0");
1602 obj->data.s = strndup(bat, text_buffer_size);
1603 #endif /* !__OpenBSD__ */
1605 #if defined(__linux__)
1606 END OBJ(disk_protect, 0)
1608 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1610 CRIT_ERR(obj, free_at_crash, "disk_protect needs an argument");
1611 END OBJ(i8k_version, INFO_I8K)
1612 END OBJ(i8k_bios, INFO_I8K)
1613 END OBJ(i8k_serial, INFO_I8K)
1614 END OBJ(i8k_cpu_temp, INFO_I8K)
1615 END OBJ(i8k_left_fan_status, INFO_I8K)
1616 END OBJ(i8k_right_fan_status, INFO_I8K)
1617 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1618 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1619 END OBJ(i8k_ac_status, INFO_I8K)
1620 END OBJ(i8k_buttons_status, INFO_I8K)
1623 END OBJ(ibm_temps, 0)
1625 CRIT_ERR(obj, free_at_crash, "ibm_temps: needs an argument");
1627 if (!isdigit(arg[0]) || strlen(arg) >= 2 || atoi(&arg[0]) >= 8) {
1628 obj->data.sensor = 0;
1629 NORM_ERR("Invalid temperature sensor! Sensor number must be 0 to 7. "
1630 "Using 0 (CPU temp sensor).");
1632 obj->data.sensor = atoi(&arg[0]);
1633 END OBJ(ibm_volume, 0)
1634 END OBJ(ibm_brightness, 0)
1636 /* information from sony_laptop kernel module
1637 * /sys/devices/platform/sony-laptop */
1638 END OBJ(sony_fanspeed, 0)
1639 END OBJ_IF(if_gw, INFO_GW)
1640 END OBJ(ioscheduler, 0)
1642 CRIT_ERR(obj, free_at_crash, "get_ioscheduler needs an argument (e.g. hda)");
1645 obj->data.s = strndup(dev_name(arg), text_buffer_size);
1646 END OBJ(laptop_mode, 0)
1647 END OBJ(pb_battery, 0)
1648 if (arg && strcmp(arg, "status") == EQUAL) {
1649 obj->data.i = PB_BATT_STATUS;
1650 } else if (arg && strcmp(arg, "percent") == EQUAL) {
1651 obj->data.i = PB_BATT_PERCENT;
1652 } else if (arg && strcmp(arg, "time") == EQUAL) {
1653 obj->data.i = PB_BATT_TIME;
1655 NORM_ERR("pb_battery: needs one argument: status, percent or time");
1660 #endif /* __linux__ */
1661 #if (defined(__FreeBSD__) || defined(__linux__))
1662 END OBJ_IF(if_up, 0)
1664 NORM_ERR("if_up needs an argument");
1665 obj->data.ifblock.s = 0;
1667 obj->data.ifblock.s = strndup(arg, text_buffer_size);
1670 #if defined(__OpenBSD__)
1671 END OBJ(obsd_sensors_temp, 0)
1673 CRIT_ERR(obj, free_at_crash, "obsd_sensors_temp: needs an argument");
1675 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1676 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1677 obj->data.sensor = 0;
1678 NORM_ERR("Invalid temperature sensor number!");
1680 obj->data.sensor = atoi(&arg[0]);
1681 END OBJ(obsd_sensors_fan, 0)
1683 CRIT_ERR(obj, free_at_crash, "obsd_sensors_fan: needs 2 arguments (device and sensor "
1686 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1687 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1688 obj->data.sensor = 0;
1689 NORM_ERR("Invalid fan sensor number!");
1691 obj->data.sensor = atoi(&arg[0]);
1692 END OBJ(obsd_sensors_volt, 0)
1694 CRIT_ERR(obj, free_at_crash, "obsd_sensors_volt: needs 2 arguments (device and sensor "
1697 if (!isdigit(arg[0]) || atoi(&arg[0]) < 0
1698 || atoi(&arg[0]) > OBSD_MAX_SENSORS - 1) {
1699 obj->data.sensor = 0;
1700 NORM_ERR("Invalid voltage sensor number!");
1702 obj->data.sensor = atoi(&arg[0]);
1703 END OBJ(obsd_vendor, 0)
1704 END OBJ(obsd_product, 0)
1705 #endif /* __OpenBSD__ */
1706 END OBJ(buffers, INFO_BUFFERS)
1707 END OBJ(cached, INFO_BUFFERS)
1708 #define SCAN_CPU(__arg, __var) { \
1710 if (__arg && sscanf(__arg, " cpu%u %n", &__var, &__offset) > 0) \
1711 __arg += __offset; \
1715 END OBJ(cpu, INFO_CPU)
1716 SCAN_CPU(arg, obj->data.cpu_index);
1717 DBGP2("Adding $cpu for CPU %d", obj->data.cpu_index);
1719 END OBJ(cpugauge, INFO_CPU)
1720 SIZE_DEFAULTS(gauge);
1721 SCAN_CPU(arg, obj->data.cpu_index);
1722 scan_gauge(arg, &obj->a, &obj->b);
1723 DBGP2("Adding $cpugauge for CPU %d", obj->data.cpu_index);
1725 END OBJ(cpubar, INFO_CPU)
1727 SCAN_CPU(arg, obj->data.cpu_index);
1728 scan_bar(arg, &obj->a, &obj->b);
1729 DBGP2("Adding $cpubar for CPU %d", obj->data.cpu_index);
1731 END OBJ(cpugraph, INFO_CPU)
1733 SIZE_DEFAULTS(graph);
1734 SCAN_CPU(arg, obj->data.cpu_index);
1735 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1736 &obj->e, &obj->char_a, &obj->char_b);
1737 DBGP2("Adding $cpugraph for CPU %d", obj->data.cpu_index);
1739 END OBJ(loadgraph, INFO_LOADAVG)
1741 SIZE_DEFAULTS(graph);
1742 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1743 &obj->e, &obj->char_a, &obj->char_b);
1747 r = sscanf(arg, "%d", &a);
1749 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1753 END OBJ(diskio, INFO_DISKIO)
1754 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1755 END OBJ(diskio_read, INFO_DISKIO)
1756 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1757 END OBJ(diskio_write, INFO_DISKIO)
1758 obj->data.diskio = prepare_diskio_stat(dev_name(arg));
1760 END OBJ(diskiograph, INFO_DISKIO)
1762 SIZE_DEFAULTS(graph);
1763 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1764 &obj->e, &obj->char_a, &obj->char_b);
1766 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1768 END OBJ(diskiograph_read, INFO_DISKIO)
1770 SIZE_DEFAULTS(graph);
1771 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1772 &obj->e, &obj->char_a, &obj->char_b);
1774 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1776 END OBJ(diskiograph_write, INFO_DISKIO)
1778 SIZE_DEFAULTS(graph);
1779 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1780 &obj->e, &obj->char_a, &obj->char_b);
1782 obj->data.diskio = prepare_diskio_stat(dev_name(buf));
1787 if (output_methods & TO_X) {
1788 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1789 current_text_color = obj->data.l;
1793 if (output_methods & TO_NCURSES) {
1794 obj->data.l = COLOR_WHITE;
1796 if(strcasecmp(arg, "red") == 0) {
1797 obj->data.l = COLOR_RED;
1798 }else if(strcasecmp(arg, "green") == 0) {
1799 obj->data.l = COLOR_GREEN;
1800 }else if(strcasecmp(arg, "yellow") == 0) {
1801 obj->data.l = COLOR_YELLOW;
1802 }else if(strcasecmp(arg, "blue") == 0) {
1803 obj->data.l = COLOR_BLUE;
1804 }else if(strcasecmp(arg, "magenta") == 0) {
1805 obj->data.l = COLOR_MAGENTA;
1806 }else if(strcasecmp(arg, "cyan") == 0) {
1807 obj->data.l = COLOR_CYAN;
1808 }else if(strcasecmp(arg, "black") == 0) {
1809 obj->data.l = COLOR_BLACK;
1812 current_text_color = obj->data.l;
1813 init_pair(obj->data.l, obj->data.l, COLOR_BLACK);
1815 #endif /* NCURSES */
1817 obj->data.l = color0;
1818 current_text_color = obj->data.l;
1820 obj->data.l = color1;
1821 current_text_color = obj->data.l;
1823 obj->data.l = color2;
1824 current_text_color = obj->data.l;
1826 obj->data.l = color3;
1827 current_text_color = obj->data.l;
1829 obj->data.l = color4;
1830 current_text_color = obj->data.l;
1832 obj->data.l = color5;
1833 current_text_color = obj->data.l;
1835 obj->data.l = color6;
1836 current_text_color = obj->data.l;
1838 obj->data.l = color7;
1839 current_text_color = obj->data.l;
1841 obj->data.l = color8;
1842 current_text_color = obj->data.l;
1844 obj->data.l = color9;
1845 current_text_color = obj->data.l;
1848 obj->data.s = scan_font(arg);
1850 END OBJ(conky_version, 0)
1851 END OBJ(conky_build_date, 0)
1852 END OBJ(conky_build_arch, 0)
1853 END OBJ(downspeed, INFO_NET)
1855 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1857 // default to DEFAULTNETDEV
1858 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1859 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1862 END OBJ(downspeedf, INFO_NET)
1864 obj->data.net = get_net_stat(arg, obj, free_at_crash);
1866 // default to DEFAULTNETDEV
1867 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
1868 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1872 END OBJ(downspeedgraph, INFO_NET)
1874 SIZE_DEFAULTS(graph);
1875 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
1876 &obj->e, &obj->char_a, &obj->char_b);
1878 // default to DEFAULTNETDEV
1879 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
1880 obj->data.net = get_net_stat(buf, obj, free_at_crash);
1884 obj_be_ifblock_else(ifblock_opaque, obj);
1886 obj_be_ifblock_endif(ifblock_opaque, obj);
1888 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1890 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1892 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1894 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1897 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1899 END OBJ(execgauge, 0)
1900 SIZE_DEFAULTS(gauge);
1901 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1902 END OBJ(execgraph, 0)
1903 SIZE_DEFAULTS(graph);
1904 obj->data.s = strndup(arg ? arg : "", text_buffer_size);
1906 END OBJ(execibar, 0)
1910 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1913 NORM_ERR("${execibar <interval> command}");
1914 obj->type = OBJ_text;
1915 snprintf(buf, 256, "${%s}", s);
1916 obj->data.s = strndup(buf, text_buffer_size);
1918 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1921 END OBJ(execigraph, 0)
1923 SIZE_DEFAULTS(graph);
1925 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1928 NORM_ERR("${execigraph <interval> command}");
1929 obj->type = OBJ_text;
1930 snprintf(buf, 256, "${%s}", s);
1931 obj->data.s = strndup(buf, text_buffer_size);
1933 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1935 END OBJ(execigauge, 0)
1937 SIZE_DEFAULTS(gauge);
1939 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1942 NORM_ERR("${execigauge <interval> command}");
1943 obj->type = OBJ_text;
1944 snprintf(buf, 256, "${%s}", s);
1945 obj->data.s = strndup(buf, text_buffer_size);
1947 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1953 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1956 NORM_ERR("${execi <interval> command}");
1957 obj->type = OBJ_text;
1958 snprintf(buf, 256, "${%s}", s);
1959 obj->data.s = strndup(buf, text_buffer_size);
1961 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1962 obj->data.execi.buffer = malloc(text_buffer_size);
1967 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1970 NORM_ERR("${execi <interval> command}");
1971 obj->type = OBJ_text;
1972 snprintf(buf, 256, "${%s}", s);
1973 obj->data.s = strndup(buf, text_buffer_size);
1975 obj->data.execi.cmd = strndup(arg + n, text_buffer_size);
1976 obj->data.execi.buffer = malloc(text_buffer_size);
1981 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
1984 NORM_ERR("${texeci <interval> command}");
1985 obj->type = OBJ_text;
1986 snprintf(buf, 256, "${%s}", s);
1987 obj->data.s = strndup(buf, text_buffer_size);
1989 obj->data.texeci.cmd = strndup(arg + n, text_buffer_size);
1990 obj->data.texeci.buffer = malloc(text_buffer_size);
1992 obj->data.texeci.p_timed_thread = NULL;
1993 END OBJ(pre_exec, 0)
1994 obj->type = OBJ_text;
1998 read_exec(arg, buf, sizeof(buf));
1999 obj->data.s = strndup(buf, text_buffer_size);
2001 obj->data.s = strndup("", text_buffer_size);
2003 END OBJ(fs_bar, INFO_FS)
2005 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2007 while (isspace(*arg)) {
2016 obj->data.fsbar.fs = prepare_fs_stat(arg);
2017 END OBJ(fs_bar_free, INFO_FS)
2019 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2021 while (isspace(*arg)) {
2031 obj->data.fsbar.fs = prepare_fs_stat(arg);
2032 END OBJ(fs_free, INFO_FS)
2036 obj->data.fs = prepare_fs_stat(arg);
2037 END OBJ(fs_used_perc, INFO_FS)
2041 obj->data.fs = prepare_fs_stat(arg);
2042 END OBJ(fs_free_perc, INFO_FS)
2046 obj->data.fs = prepare_fs_stat(arg);
2047 END OBJ(fs_size, INFO_FS)
2051 obj->data.fs = prepare_fs_stat(arg);
2052 END OBJ(fs_type, INFO_FS)
2056 obj->data.fs = prepare_fs_stat(arg);
2057 END OBJ(fs_used, INFO_FS)
2061 obj->data.fs = prepare_fs_stat(arg);
2063 obj->data.i = arg ? atoi(arg) : 1;
2064 END OBJ(nameserver, INFO_DNS)
2065 obj->data.i = arg ? atoi(arg) : 0;
2067 obj->data.i = arg ? atoi(arg) : 1;
2069 obj->data.i = arg ? atoi(arg) : 1;
2073 NORM_ERR("goto needs arguments");
2074 obj->type = OBJ_text;
2075 obj->data.s = strndup("${goto}", text_buffer_size);
2079 obj->data.i = atoi(arg);
2085 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2086 sscanf(arg, "%d", &b);
2092 obj->data.pair.a = a;
2093 obj->data.pair.b = b;
2096 END OBJ(i2c, INFO_SYSFS)
2097 char buf1[64], buf2[64];
2098 float factor, offset;
2102 NORM_ERR("i2c needs arguments");
2103 obj->type = OBJ_text;
2104 // obj->data.s = strndup("${i2c}", text_buffer_size);
2108 #define HWMON_RESET() {\
2113 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2114 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2115 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2116 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2119 NORM_ERR("i2c failed to parse arguments");
2120 obj->type = OBJ_text;
2123 DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2124 obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
2125 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2126 strncpy(obj->data.sysfs.type, buf2, 63);
2127 obj->data.sysfs.factor = factor;
2128 obj->data.sysfs.offset = offset;
2130 END OBJ(platform, INFO_SYSFS)
2131 char buf1[64], buf2[64];
2132 float factor, offset;
2136 NORM_ERR("platform needs arguments");
2137 obj->type = OBJ_text;
2141 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2142 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2143 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2144 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2147 NORM_ERR("platform failed to parse arguments");
2148 obj->type = OBJ_text;
2151 DBGP("parsed platform args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2152 obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
2153 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2154 strncpy(obj->data.sysfs.type, buf2, 63);
2155 obj->data.sysfs.factor = factor;
2156 obj->data.sysfs.offset = offset;
2158 END OBJ(hwmon, INFO_SYSFS)
2159 char buf1[64], buf2[64];
2160 float factor, offset;
2164 NORM_ERR("hwmon needs argumanets");
2165 obj->type = OBJ_text;
2169 if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
2170 if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
2171 if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
2172 if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
2177 NORM_ERR("hwmon failed to parse arguments");
2178 obj->type = OBJ_text;
2181 DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
2182 obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
2183 &obj->data.sysfs.arg, obj->data.sysfs.devtype);
2184 strncpy(obj->data.sysfs.type, buf2, 63);
2185 obj->data.sysfs.factor = factor;
2186 obj->data.sysfs.offset = offset;
2188 #endif /* !__OpenBSD__ */
2191 /* we have four different types of top (top, top_mem, top_time and top_io). To
2192 * avoid having almost-same code four times, we have this special
2194 if (strncmp(s, "top", 3) == EQUAL) {
2195 if (!parse_top_args(s, arg, obj)) {
2198 } else OBJ(addr, INFO_NET)
2200 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2202 // default to DEFAULTNETDEV
2203 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2204 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2207 #if defined(__linux__)
2208 END OBJ(addrs, INFO_NET)
2210 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2212 // default to DEFAULTNETDEV
2213 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2214 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2217 #endif /* __linux__ */
2219 init_tailhead("tail", arg, obj, free_at_crash);
2221 init_tailhead("head", arg, obj, free_at_crash);
2224 obj->data.s = strndup(arg, text_buffer_size);
2226 CRIT_ERR(obj, free_at_crash, "lines needs a argument");
2230 obj->data.s = strndup(arg, text_buffer_size);
2232 CRIT_ERR(obj, free_at_crash, "words needs a argument");
2234 END OBJ(loadavg, INFO_LOADAVG)
2235 int a = 1, b = 2, c = 3, r = 3;
2238 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2239 if (r >= 3 && (c < 1 || c > 3)) {
2242 if (r >= 2 && (b < 1 || b > 3)) {
2245 if (r >= 1 && (a < 1 || a > 3)) {
2249 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2250 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2251 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2252 END OBJ_IF(if_empty, 0)
2254 NORM_ERR("if_empty needs an argument");
2255 obj->data.ifblock.s = 0;
2257 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2258 obj->sub = malloc(sizeof(struct text_object));
2259 extract_variable_text_internal(obj->sub,
2260 obj->data.ifblock.s);
2262 END OBJ_IF(if_match, 0)
2264 NORM_ERR("if_match needs arguments");
2265 obj->data.ifblock.s = 0;
2267 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2268 obj->sub = malloc(sizeof(struct text_object));
2269 extract_variable_text_internal(obj->sub,
2270 obj->data.ifblock.s);
2272 END OBJ_IF(if_existing, 0)
2274 NORM_ERR("if_existing needs an argument or two");
2275 obj->data.ifblock.s = NULL;
2276 obj->data.ifblock.str = NULL;
2278 char buf1[256], buf2[256];
2279 int r = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2282 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2283 obj->data.ifblock.str = NULL;
2285 obj->data.ifblock.s = strndup(buf1, text_buffer_size);
2286 obj->data.ifblock.str = strndup(buf2, text_buffer_size);
2289 DBGP("if_existing: '%s' '%s'", obj->data.ifblock.s, obj->data.ifblock.str);
2290 END OBJ_IF(if_mounted, 0)
2292 NORM_ERR("if_mounted needs an argument");
2293 obj->data.ifblock.s = 0;
2295 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2298 END OBJ_IF(if_running, INFO_TOP)
2300 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2302 END OBJ_IF(if_running, 0)
2306 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2307 obj->data.ifblock.s = strndup(buf, text_buffer_size);
2310 NORM_ERR("if_running needs an argument");
2311 obj->data.ifblock.s = 0;
2317 char mbox[256], dst[256];
2321 /* Kapil: Changed from MAIL_FILE to
2322 current_mail_spool since the latter
2323 is a copy of the former if undefined
2324 but the latter should take precedence
2326 strncpy(mbox, current_mail_spool, sizeof(mbox));
2328 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2330 strncpy(mbox, arg, sizeof(mbox));
2334 variable_substitute(mbox, dst, sizeof(dst));
2335 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2336 obj->data.local_mail.interval = n1;
2337 END OBJ(new_mails, 0)
2339 char mbox[256], dst[256];
2343 strncpy(mbox, current_mail_spool, sizeof(mbox));
2345 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2347 strncpy(mbox, arg, sizeof(mbox));
2351 variable_substitute(mbox, dst, sizeof(dst));
2352 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2353 obj->data.local_mail.interval = n1;
2354 END OBJ(seen_mails, 0)
2356 char mbox[256], dst[256];
2360 strncpy(mbox, current_mail_spool, sizeof(mbox));
2362 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2364 strncpy(mbox, arg, sizeof(mbox));
2368 variable_substitute(mbox, dst, sizeof(dst));
2369 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2370 obj->data.local_mail.interval = n1;
2371 END OBJ(unseen_mails, 0)
2373 char mbox[256], dst[256];
2377 strncpy(mbox, current_mail_spool, sizeof(mbox));
2379 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2381 strncpy(mbox, arg, sizeof(mbox));
2385 variable_substitute(mbox, dst, sizeof(dst));
2386 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2387 obj->data.local_mail.interval = n1;
2388 END OBJ(flagged_mails, 0)
2390 char mbox[256], dst[256];
2394 strncpy(mbox, current_mail_spool, sizeof(mbox));
2396 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2398 strncpy(mbox, arg, sizeof(mbox));
2402 variable_substitute(mbox, dst, sizeof(dst));
2403 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2404 obj->data.local_mail.interval = n1;
2405 END OBJ(unflagged_mails, 0)
2407 char mbox[256], dst[256];
2411 strncpy(mbox, current_mail_spool, sizeof(mbox));
2413 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2415 strncpy(mbox, arg, sizeof(mbox));
2419 variable_substitute(mbox, dst, sizeof(dst));
2420 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2421 obj->data.local_mail.interval = n1;
2422 END OBJ(forwarded_mails, 0)
2424 char mbox[256], dst[256];
2428 strncpy(mbox, current_mail_spool, sizeof(mbox));
2430 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2432 strncpy(mbox, arg, sizeof(mbox));
2436 variable_substitute(mbox, dst, sizeof(dst));
2437 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2438 obj->data.local_mail.interval = n1;
2439 END OBJ(unforwarded_mails, 0)
2441 char mbox[256], dst[256];
2445 strncpy(mbox, current_mail_spool, sizeof(mbox));
2447 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2449 strncpy(mbox, arg, sizeof(mbox));
2453 variable_substitute(mbox, dst, sizeof(dst));
2454 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2455 obj->data.local_mail.interval = n1;
2456 END OBJ(replied_mails, 0)
2458 char mbox[256], dst[256];
2462 strncpy(mbox, current_mail_spool, sizeof(mbox));
2464 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2466 strncpy(mbox, arg, sizeof(mbox));
2470 variable_substitute(mbox, dst, sizeof(dst));
2471 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2472 obj->data.local_mail.interval = n1;
2473 END OBJ(unreplied_mails, 0)
2475 char mbox[256], dst[256];
2479 strncpy(mbox, current_mail_spool, sizeof(mbox));
2481 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2483 strncpy(mbox, arg, sizeof(mbox));
2487 variable_substitute(mbox, dst, sizeof(dst));
2488 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2489 obj->data.local_mail.interval = n1;
2490 END OBJ(draft_mails, 0)
2492 char mbox[256], dst[256];
2496 strncpy(mbox, current_mail_spool, sizeof(mbox));
2498 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2500 strncpy(mbox, arg, sizeof(mbox));
2504 variable_substitute(mbox, dst, sizeof(dst));
2505 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2506 obj->data.local_mail.interval = n1;
2507 END OBJ(trashed_mails, 0)
2509 char mbox[256], dst[256];
2513 strncpy(mbox, current_mail_spool, sizeof(mbox));
2515 if (sscanf(arg, "%s %f", mbox, &n1) != 2) {
2517 strncpy(mbox, arg, sizeof(mbox));
2521 variable_substitute(mbox, dst, sizeof(dst));
2522 obj->data.local_mail.mbox = strndup(dst, text_buffer_size);
2523 obj->data.local_mail.interval = n1;
2524 END OBJ(mboxscan, 0)
2525 obj->data.mboxscan.args = (char *) malloc(text_buffer_size);
2526 obj->data.mboxscan.output = (char *) malloc(text_buffer_size);
2527 /* if '1' (in mboxscan.c) then there was SIGUSR1, hmm */
2528 obj->data.mboxscan.output[0] = 1;
2529 strncpy(obj->data.mboxscan.args, arg, text_buffer_size);
2530 END OBJ(mem, INFO_MEM)
2531 END OBJ(memeasyfree, INFO_MEM)
2532 END OBJ(memfree, INFO_MEM)
2533 END OBJ(memmax, INFO_MEM)
2534 END OBJ(memperc, INFO_MEM)
2536 END OBJ(memgauge, INFO_MEM)
2537 SIZE_DEFAULTS(gauge);
2538 scan_gauge(arg, &obj->data.pair.a, &obj->data.pair.b);
2540 END OBJ(membar, INFO_MEM)
2542 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2544 END OBJ(memgraph, INFO_MEM)
2546 SIZE_DEFAULTS(graph);
2547 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2548 &obj->e, &obj->char_a, &obj->char_b);
2552 END OBJ(mixer, INFO_MIXER)
2553 obj->data.l = mixer_init(arg);
2554 END OBJ(mixerl, INFO_MIXER)
2555 obj->data.l = mixer_init(arg);
2556 END OBJ(mixerr, INFO_MIXER)
2557 obj->data.l = mixer_init(arg);
2559 END OBJ(mixerbar, INFO_MIXER)
2561 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2562 &obj->data.mixerbar.h);
2563 END OBJ(mixerlbar, INFO_MIXER)
2565 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2566 &obj->data.mixerbar.h);
2567 END OBJ(mixerrbar, INFO_MIXER)
2569 scan_mixer_bar(arg, &obj->data.mixerbar.l, &obj->data.mixerbar.w,
2570 &obj->data.mixerbar.h);
2572 END OBJ_IF(if_mixer_mute, INFO_MIXER)
2573 obj->data.ifblock.i = mixer_init(arg);
2575 END OBJ(monitor, INFO_X11)
2576 END OBJ(monitor_number, INFO_X11)
2577 END OBJ(desktop, INFO_X11)
2578 END OBJ(desktop_number, INFO_X11)
2579 END OBJ(desktop_name, INFO_X11)
2581 END OBJ(nodename, 0)
2582 END OBJ(processes, INFO_PROCS)
2583 END OBJ(running_processes, INFO_RUN_PROCS)
2584 END OBJ(shadecolor, 0)
2586 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2588 END OBJ(outlinecolor, 0)
2590 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2592 END OBJ(stippled_hr, 0)
2594 int a = stippled_borders, b = 1;
2597 if (sscanf(arg, "%d %d", &a, &b) != 2) {
2598 sscanf(arg, "%d", &b);
2604 obj->data.pair.a = a;
2605 obj->data.pair.b = b;
2607 END OBJ(swap, INFO_MEM)
2608 END OBJ(swapfree, INFO_MEM)
2609 END OBJ(swapmax, INFO_MEM)
2610 END OBJ(swapperc, INFO_MEM)
2611 END OBJ(swapbar, INFO_MEM)
2613 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2616 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2618 obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
2620 char buf1[256], buf2[256], *fmt, *tz;
2624 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2634 obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
2635 obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
2637 END OBJ(iconv_start, 0)
2638 if (iconv_converting) {
2639 CRIT_ERR(obj, free_at_crash, "You must stop your last iconv conversion before "
2640 "starting another");
2643 char iconv_from[CODEPAGE_LENGTH];
2644 char iconv_to[CODEPAGE_LENGTH];
2646 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2647 CRIT_ERR(obj, free_at_crash, "Invalid arguments for iconv_start");
2651 new_iconv = iconv_open(iconv_to, iconv_from);
2652 if (new_iconv == (iconv_t) (-1)) {
2653 NORM_ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2655 obj->a = register_iconv(&new_iconv);
2656 iconv_converting = 1;
2660 CRIT_ERR(obj, free_at_crash, "Iconv requires arguments");
2662 END OBJ(iconv_stop, 0)
2663 iconv_converting = 0;
2666 END OBJ(totaldown, INFO_NET)
2668 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2670 // default to DEFAULTNETDEV
2671 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2672 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2675 END OBJ(totalup, INFO_NET)
2676 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2678 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2680 // default to DEFAULTNETDEV
2681 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2682 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2686 END OBJ_IF(if_updatenr, 0)
2687 obj->data.ifblock.i = arg ? atoi(arg) : 0;
2688 if(obj->data.ifblock.i == 0) CRIT_ERR(obj, free_at_crash, "if_updatenr needs a number above 0 as argument");
2689 updatereset = obj->data.ifblock.i > updatereset ? obj->data.ifblock.i : updatereset;
2691 obj->data.i = arg ? atoi(arg) : 0;
2693 obj->data.i = arg ? atoi(arg) : 0;
2694 END OBJ(upspeed, INFO_NET)
2696 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2698 // default to DEFAULTNETDEV
2699 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2700 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2703 END OBJ(upspeedf, INFO_NET)
2705 obj->data.net = get_net_stat(arg, obj, free_at_crash);
2707 // default to DEFAULTNETDEV
2708 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
2709 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2714 END OBJ(upspeedgraph, INFO_NET)
2716 SIZE_DEFAULTS(graph);
2717 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
2718 &obj->e, &obj->char_a, &obj->char_b);
2720 // default to DEFAULTNETDEV
2721 buf = strndup(buf ? buf : DEFAULTNETDEV, text_buffer_size);
2722 obj->data.net = get_net_stat(buf, obj, free_at_crash);
2725 END OBJ(uptime_short, INFO_UPTIME)
2726 END OBJ(uptime, INFO_UPTIME)
2727 END OBJ(user_names, INFO_USERS)
2728 END OBJ(user_times, INFO_USERS)
2729 END OBJ(user_terms, INFO_USERS)
2730 END OBJ(user_number, INFO_USERS)
2731 #if defined(__linux__)
2732 END OBJ(gw_iface, INFO_GW)
2733 END OBJ(gw_ip, INFO_GW)
2734 #endif /* !__linux__ */
2736 END OBJ(adt746xcpu, 0)
2737 END OBJ(adt746xfan, 0)
2738 #endif /* !__OpenBSD__ */
2739 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2740 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2741 END OBJ(apm_adapter, 0)
2742 END OBJ(apm_battery_life, 0)
2743 END OBJ(apm_battery_time, 0)
2744 #endif /* __FreeBSD__ */
2745 END OBJ(imap_unseen, 0)
2748 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2753 END OBJ(imap_messages, 0)
2756 obj->data.mail = parse_mail_args(IMAP_TYPE, arg);
2761 END OBJ(pop3_unseen, 0)
2764 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2769 END OBJ(pop3_used, 0)
2772 obj->data.mail = parse_mail_args(POP3_TYPE, arg);
2780 obj->data.s = strndup(arg, text_buffer_size);
2782 NORM_ERR("smapi needs an argument");
2783 END OBJ_IF(if_smapi_bat_installed, 0)
2785 NORM_ERR("if_smapi_bat_installed needs an argument");
2786 obj->data.ifblock.s = 0;
2788 obj->data.ifblock.s = strndup(arg, text_buffer_size);
2789 END OBJ(smapi_bat_perc, 0)
2791 obj->data.s = strndup(arg, text_buffer_size);
2793 NORM_ERR("smapi_bat_perc needs an argument");
2794 END OBJ(smapi_bat_temp, 0)
2796 obj->data.s = strndup(arg, text_buffer_size);
2798 NORM_ERR("smapi_bat_temp needs an argument");
2799 END OBJ(smapi_bat_power, 0)
2801 obj->data.s = strndup(arg, text_buffer_size);
2803 NORM_ERR("smapi_bat_power needs an argument");
2805 END OBJ(smapi_bat_bar, 0)
2809 if(sscanf(arg, "%i %n", &obj->data.i, &cnt) <= 0) {
2810 NORM_ERR("first argument to smapi_bat_bar must be an integer value");
2814 arg = scan_bar(arg + cnt, &obj->a, &obj->b);
2817 NORM_ERR("smapi_bat_bar needs an argument");
2821 #define mpd_set_maxlen(name) \
2824 sscanf(arg, "%d", &i); \
2826 obj->data.i = i + 1; \
2828 NORM_ERR(#name ": invalid length argument"); \
2830 END OBJ(mpd_artist, INFO_MPD)
2831 mpd_set_maxlen(mpd_artist);
2833 END OBJ(mpd_title, INFO_MPD)
2834 mpd_set_maxlen(mpd_title);
2836 END OBJ(mpd_random, INFO_MPD) init_mpd();
2837 END OBJ(mpd_repeat, INFO_MPD) init_mpd();
2838 END OBJ(mpd_elapsed, INFO_MPD) init_mpd();
2839 END OBJ(mpd_length, INFO_MPD) init_mpd();
2840 END OBJ(mpd_track, INFO_MPD)
2841 mpd_set_maxlen(mpd_track);
2843 END OBJ(mpd_name, INFO_MPD)
2844 mpd_set_maxlen(mpd_name);
2846 END OBJ(mpd_file, INFO_MPD)
2847 mpd_set_maxlen(mpd_file);
2849 END OBJ(mpd_percent, INFO_MPD) init_mpd();
2850 END OBJ(mpd_album, INFO_MPD)
2851 mpd_set_maxlen(mpd_album);
2853 END OBJ(mpd_vol, INFO_MPD) init_mpd();
2854 END OBJ(mpd_bitrate, INFO_MPD) init_mpd();
2855 END OBJ(mpd_status, INFO_MPD) init_mpd();
2856 END OBJ(mpd_bar, INFO_MPD)
2858 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2860 END OBJ(mpd_smart, INFO_MPD)
2861 mpd_set_maxlen(mpd_smart);
2863 END OBJ_IF(if_mpd_playing, INFO_MPD)
2865 #undef mpd_set_maxlen
2868 END OBJ(moc_state, INFO_MOC)
2869 END OBJ(moc_file, INFO_MOC)
2870 END OBJ(moc_title, INFO_MOC)
2871 END OBJ(moc_artist, INFO_MOC)
2872 END OBJ(moc_song, INFO_MOC)
2873 END OBJ(moc_album, INFO_MOC)
2874 END OBJ(moc_totaltime, INFO_MOC)
2875 END OBJ(moc_timeleft, INFO_MOC)
2876 END OBJ(moc_curtime, INFO_MOC)
2877 END OBJ(moc_bitrate, INFO_MOC)
2878 END OBJ(moc_rate, INFO_MOC)
2881 END OBJ(xmms2_artist, INFO_XMMS2)
2882 END OBJ(xmms2_album, INFO_XMMS2)
2883 END OBJ(xmms2_title, INFO_XMMS2)
2884 END OBJ(xmms2_genre, INFO_XMMS2)
2885 END OBJ(xmms2_comment, INFO_XMMS2)
2886 END OBJ(xmms2_url, INFO_XMMS2)
2887 END OBJ(xmms2_tracknr, INFO_XMMS2)
2888 END OBJ(xmms2_bitrate, INFO_XMMS2)
2889 END OBJ(xmms2_date, INFO_XMMS2)
2890 END OBJ(xmms2_id, INFO_XMMS2)
2891 END OBJ(xmms2_duration, INFO_XMMS2)
2892 END OBJ(xmms2_elapsed, INFO_XMMS2)
2893 END OBJ(xmms2_size, INFO_XMMS2)
2894 END OBJ(xmms2_status, INFO_XMMS2)
2895 END OBJ(xmms2_percent, INFO_XMMS2)
2897 END OBJ(xmms2_bar, INFO_XMMS2)
2899 scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2901 END OBJ(xmms2_smart, INFO_XMMS2)
2902 END OBJ(xmms2_playlist, INFO_XMMS2)
2903 END OBJ(xmms2_timesplayed, INFO_XMMS2)
2904 END OBJ_IF(if_xmms2_connected, INFO_XMMS2)
2907 END OBJ(audacious_status, INFO_AUDACIOUS)
2908 END OBJ(audacious_title, INFO_AUDACIOUS)
2910 sscanf(arg, "%d", &info.audacious.max_title_len);
2911 if (info.audacious.max_title_len > 0) {
2912 info.audacious.max_title_len++;
2914 CRIT_ERR(obj, free_at_crash, "audacious_title: invalid length argument");
2917 END OBJ(audacious_length, INFO_AUDACIOUS)
2918 END OBJ(audacious_length_seconds, INFO_AUDACIOUS)
2919 END OBJ(audacious_position, INFO_AUDACIOUS)
2920 END OBJ(audacious_position_seconds, INFO_AUDACIOUS)
2921 END OBJ(audacious_bitrate, INFO_AUDACIOUS)
2922 END OBJ(audacious_frequency, INFO_AUDACIOUS)
2923 END OBJ(audacious_channels, INFO_AUDACIOUS)
2924 END OBJ(audacious_filename, INFO_AUDACIOUS)
2925 END OBJ(audacious_playlist_length, INFO_AUDACIOUS)
2926 END OBJ(audacious_playlist_position, INFO_AUDACIOUS)
2927 END OBJ(audacious_main_volume, INFO_AUDACIOUS)
2929 END OBJ(audacious_bar, INFO_AUDACIOUS)
2931 scan_bar(arg, &obj->a, &obj->b);
2935 END OBJ(bmpx_title, INFO_BMPX)
2936 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2937 END OBJ(bmpx_artist, INFO_BMPX)
2938 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2939 END OBJ(bmpx_album, INFO_BMPX)
2940 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2941 END OBJ(bmpx_track, INFO_BMPX)
2942 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2943 END OBJ(bmpx_uri, INFO_BMPX)
2944 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2945 END OBJ(bmpx_bitrate, INFO_BMPX)
2946 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2952 char *userid = (char *) malloc(20 * sizeof(char));
2953 char *apikey = (char *) malloc(64 * sizeof(char));
2954 char *charid = (char *) malloc(20 * sizeof(char));
2956 argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
2957 obj->data.eve.charid = charid;
2958 obj->data.eve.userid = userid;
2959 obj->data.eve.apikey = apikey;
2963 CRIT_ERR(obj, free_at_crash, "eve needs arguments: <userid> <apikey> <characterid>");
2971 char *uri = (char *) malloc(128 * sizeof(char));
2973 argc = sscanf(arg, "%127s %f", uri, &interval);
2975 obj->data.curl.uri = uri;
2976 obj->data.curl.interval = interval > 0 ? interval * 60 : 15*60;
2978 NORM_ERR("wrong number of arguments for $curl");
2981 CRIT_ERR(obj, free_at_crash, "curl needs arguments: <uri> <interval in minutes>");
2989 unsigned int nrspaces = 0;
2990 char *uri = (char *) malloc(128 * sizeof(char));
2991 char *action = (char *) malloc(64 * sizeof(char));
2993 argc = sscanf(arg, "%127s %f %63s %d %u", uri, &interval, action,
2994 &act_par, &nrspaces);
2996 obj->data.rss.uri = uri;
2997 obj->data.rss.interval = interval > 0 ? interval * 60 : 15*60;
2998 obj->data.rss.action = action;
2999 obj->data.rss.act_par = act_par;
3000 obj->data.rss.nrspaces = nrspaces;
3002 NORM_ERR("wrong number of arguments for $rss");
3005 CRIT_ERR(obj, free_at_crash, "rss needs arguments: <uri> <interval in minutes> <action> "
3006 "[act_par] [spaces in front]");
3014 char *locID = (char *) malloc(9 * sizeof(char));
3015 char *uri = (char *) malloc(128 * sizeof(char));
3016 char *data_type = (char *) malloc(32 * sizeof(char));
3018 argc = sscanf(arg, "%119s %8s %31s %f", uri, locID, data_type, &interval);
3021 if (process_weather_uri(uri, locID)) {
3025 CRIT_ERR(obj, free_at_crash, \
3026 "could not recognize the weather uri");
3029 obj->data.weather.uri = uri;
3030 obj->data.weather.data_type = data_type;
3032 /* Limit the data retrieval interval to half hour min */
3033 if (interval < 30) {
3037 /* Convert to seconds */
3038 obj->data.weather.interval = interval * 60;
3041 DBGP("weather: fetching %s from %s every %d seconds", \
3042 data_type, uri, obj->data.weather.interval);
3047 CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather");
3050 CRIT_ERR(obj, free_at_crash, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]");
3054 END OBJ(weather_forecast, 0)
3059 char *locID = (char *) malloc(9 * sizeof(char));
3060 char *uri = (char *) malloc(128 * sizeof(char));
3061 char *data_type = (char *) malloc(32 * sizeof(char));
3063 argc = sscanf(arg, "%119s %8s %1u %31s %f", uri, locID, &day, data_type, &interval);
3066 if (process_weather_uri(uri, locID)) {
3070 CRIT_ERR(obj, free_at_crash, \
3071 "could not recognize the weather forecast uri");
3074 obj->data.weather_forecast.uri = uri;
3075 obj->data.weather_forecast.data_type = data_type;
3077 /* Limit the day between 0 (today) and FORECAST_DAYS */
3078 if (day >= FORECAST_DAYS) {
3079 day = FORECAST_DAYS-1;
3081 obj->data.weather_forecast.day = day;
3083 /* Limit the data retrieval interval to 2 hours and an half */
3084 if (interval < 150) {
3088 /* Convert to seconds */
3089 obj->data.weather_forecast.interval = interval * 60;
3092 DBGP("weather_forecast: fetching %s for day %d from %s every %d seconds", \
3093 data_type, day, uri, obj->data.weather_forecast.interval);
3098 CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather_forecast");
3101 CRIT_ERR(obj, free_at_crash, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]");
3107 obj->data.s = strndup(arg, text_buffer_size);
3109 CRIT_ERR(obj, free_at_crash, "lua needs arguments: <function name> [function parameters]");
3111 END OBJ(lua_parse, 0)
3113 obj->data.s = strndup(arg, text_buffer_size);
3115 CRIT_ERR(obj, free_at_crash, "lua_parse needs arguments: <function name> [function parameters]");
3120 arg = scan_bar(arg, &obj->a, &obj->b);
3122 obj->data.s = strndup(arg, text_buffer_size);
3124 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
3127 CRIT_ERR(obj, free_at_crash, "lua_bar needs arguments: <height>,<width> <function name> [function parameters]");
3130 END OBJ(lua_graph, 0)
3131 SIZE_DEFAULTS(graph);
3134 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3135 &obj->e, &obj->char_a, &obj->char_b);
3139 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
3142 CRIT_ERR(obj, free_at_crash, "lua_graph needs arguments: <function name> [height],[width] [gradient colour 1] [gradient colour 2] [scale] [-t] [-l]");
3144 END OBJ(lua_gauge, 0)
3145 SIZE_DEFAULTS(gauge);
3147 arg = scan_gauge(arg, &obj->a, &obj->b);
3149 obj->data.s = strndup(arg, text_buffer_size);
3151 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
3154 CRIT_ERR(obj, free_at_crash, "lua_gauge needs arguments: <height>,<width> <function name> [function parameters]");
3157 #endif /* HAVE_LUA */
3160 if (scan_hddtemp(arg, &obj->data.hddtemp.dev,
3161 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
3162 NORM_ERR("hddtemp needs arguments");
3163 obj->type = OBJ_text;
3164 obj->data.s = strndup("${hddtemp}", text_buffer_size);
3165 obj->data.hddtemp.update_time = 0;
3167 obj->data.hddtemp.temp = NULL;
3168 #endif /* HDDTEMP */
3169 #ifdef TCP_PORT_MONITOR
3170 END OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
3171 tcp_portmon_init(arg, &obj->data.tcp_port_monitor);
3172 #endif /* TCP_PORT_MONITOR */
3173 END OBJ(entropy_avail, INFO_ENTROPY)
3174 END OBJ(entropy_perc, INFO_ENTROPY)
3175 END OBJ(entropy_poolsize, INFO_ENTROPY)
3176 END OBJ(entropy_bar, INFO_ENTROPY)
3178 scan_bar(arg, &obj->a, &obj->b);
3181 struct conftree *leaf = conftree_add(currentconffile, arg);
3183 if( load_config_file(arg) == TRUE) {
3184 obj->sub = malloc(sizeof(struct text_object));
3185 currentconffile = leaf;
3186 extract_variable_text_internal(obj->sub, global_text);
3187 currentconffile = leaf->back;
3189 NORM_ERR("Can't load configfile '%s'.", arg);
3192 NORM_ERR("You are trying to load '%s' recursively, I'm only going to load it once to prevent an infinite loop.", arg);
3195 CRIT_ERR(obj, free_at_crash, "include needs a argument");
3199 obj->sub = malloc(sizeof(struct text_object));
3200 extract_variable_text_internal(obj->sub, arg);
3202 CRIT_ERR(obj, free_at_crash, "blink needs a argument");
3204 END OBJ(to_bytes, 0)
3206 obj->sub = malloc(sizeof(struct text_object));
3207 extract_variable_text_internal(obj->sub, arg);
3209 CRIT_ERR(obj, free_at_crash, "to_bytes needs a argument");
3214 obj->data.scroll.resetcolor = current_text_color;
3215 obj->data.scroll.step = 1;
3216 if (arg && sscanf(arg, "%u %n", &obj->data.scroll.show, &n1) > 0) {
3217 sscanf(arg + n1, "%u %n", &obj->data.scroll.step, &n2);
3218 if (*(arg + n1 + n2)) {
3221 obj->data.scroll.step = 1;
3223 obj->data.scroll.text = malloc(strlen(arg + n1) + obj->data.scroll.show + 1);
3224 for(n2 = 0; (unsigned int) n2 < obj->data.scroll.show; n2++) {
3225 obj->data.scroll.text[n2] = ' ';
3227 obj->data.scroll.text[n2] = 0;
3228 strcat(obj->data.scroll.text, arg + n1);
3229 obj->data.scroll.start = 0;
3230 obj->sub = malloc(sizeof(struct text_object));
3231 extract_variable_text_internal(obj->sub,
3232 obj->data.scroll.text);
3234 CRIT_ERR(obj, free_at_crash, "scroll needs arguments: <length> [<step>] <text>");
3239 unsigned int indenting = 0; //vars can be used as args for other vars
3242 startvar[0] = endvar[0] = startvar[1] = endvar[1] = -1;
3244 for (i=0; arg[i] != 0 && j < 2; i++) {
3245 if(startvar[j] == -1) {
3249 }else if(endvar[j] == -1) {
3252 }else if(arg[i] == '}') {
3255 if (indenting == 0 && arg[i+1] < 48) { //<48 has 0, $, and the most used chars not used in varnames but not { or }
3261 if(startvar[0] >= 0 && endvar[0] >= 0 && startvar[1] >= 0 && endvar[1] >= 0) {
3262 obj->data.combine.left = malloc(endvar[0]-startvar[0] + 1);
3263 obj->data.combine.seperation = malloc(startvar[1] - endvar[0] + 1);
3264 obj->data.combine.right= malloc(endvar[1]-startvar[1] + 1);
3266 strncpy(obj->data.combine.left, arg + startvar[0], endvar[0] - startvar[0]);
3267 obj->data.combine.left[endvar[0] - startvar[0]] = 0;
3269 strncpy(obj->data.combine.seperation, arg + endvar[0], startvar[1] - endvar[0]);
3270 obj->data.combine.seperation[startvar[1] - endvar[0]] = 0;
3272 strncpy(obj->data.combine.right, arg + startvar[1], endvar[1] - startvar[1]);
3273 obj->data.combine.right[endvar[1] - startvar[1]] = 0;
3275 obj->sub = malloc(sizeof(struct text_object));
3276 extract_variable_text_internal(obj->sub, obj->data.combine.left);
3277 obj->sub->sub = malloc(sizeof(struct text_object));
3278 extract_variable_text_internal(obj->sub->sub, obj->data.combine.right);
3280 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3283 CRIT_ERR(obj, free_at_crash, "combine needs arguments: <text1> <text2>");
3288 CRIT_ERR(obj, free_at_crash, "nvidia needs an argument\n");
3289 } else if (set_nvidia_type(&obj->data.nvidia, arg)) {
3290 CRIT_ERR(obj, free_at_crash, "nvidia: invalid argument"
3291 " specified: '%s'\n", arg);
3296 END OBJ(apcupsd, INFO_APCUPSD)
3300 if (sscanf(arg, "%63s %d", host, &port) != 2) {
3301 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3303 info.apcupsd.port = htons(port);
3304 strncpy(info.apcupsd.host, host, sizeof(info.apcupsd.host));
3307 CRIT_ERR(obj, free_at_crash, "apcupsd needs arguments: <host> <port>");
3309 END OBJ(apcupsd_name, INFO_APCUPSD)
3310 END OBJ(apcupsd_model, INFO_APCUPSD)
3311 END OBJ(apcupsd_upsmode, INFO_APCUPSD)
3312 END OBJ(apcupsd_cable, INFO_APCUPSD)
3313 END OBJ(apcupsd_status, INFO_APCUPSD)
3314 END OBJ(apcupsd_linev, INFO_APCUPSD)
3315 END OBJ(apcupsd_load, INFO_APCUPSD)
3316 END OBJ(apcupsd_loadbar, INFO_APCUPSD)
3318 scan_bar(arg, &obj->a, &obj->b);
3320 END OBJ(apcupsd_loadgraph, INFO_APCUPSD)
3322 SIZE_DEFAULTS(graph);
3323 buf = scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d,
3324 &obj->e, &obj->char_a, &obj->char_b);
3326 END OBJ(apcupsd_loadgauge, INFO_APCUPSD)
3327 SIZE_DEFAULTS(gauge);
3328 scan_gauge(arg, &obj->a, &obj->b);
3330 END OBJ(apcupsd_charge, INFO_APCUPSD)
3331 END OBJ(apcupsd_timeleft, INFO_APCUPSD)
3332 END OBJ(apcupsd_temp, INFO_APCUPSD)
3333 END OBJ(apcupsd_lastxfer, INFO_APCUPSD)
3334 #endif /* APCUPSD */
3338 NORM_ERR("unknown variable %s", s);
3339 obj->type = OBJ_text;
3340 snprintf(buf, 256, "${%s}", s);
3341 obj->data.s = strndup(buf, text_buffer_size);
3348 static struct text_object *create_plain_text(const char *s)
3350 struct text_object *obj;
3352 if (s == NULL || *s == '\0') {
3356 obj = new_text_object_internal();
3358 obj->type = OBJ_text;
3359 obj->data.s = strndup(s, text_buffer_size);
3363 /* backslash_escape - do the actual substitution task for template objects
3365 * The field templates is used for substituting the \N occurences. Set it to
3366 * NULL to leave them as they are.
3368 static char *backslash_escape(const char *src, char **templates, unsigned int template_count)
3372 unsigned int dup_idx = 0, dup_len;
3374 dup_len = strlen(src) + 1;
3375 src_dup = malloc(dup_len * sizeof(char));
3383 if (*(p + 1) == '\\') {
3384 src_dup[dup_idx++] = '\\';
3386 } else if (*(p + 1) == ' ') {
3387 src_dup[dup_idx++] = ' ';
3389 } else if (*(p + 1) == 'n') {
3390 src_dup[dup_idx++] = '\n';
3392 } else if (templates) {
3393 unsigned int tmpl_num;
3395 if ((sscanf(p + 1, "%u%n", &tmpl_num, &digits) <= 0) ||
3396 (tmpl_num > template_count))
3398 dup_len += strlen(templates[tmpl_num - 1]);
3399 src_dup = realloc(src_dup, dup_len * sizeof(char));
3400 sprintf(src_dup + dup_idx, "%s", templates[tmpl_num - 1]);
3401 dup_idx += strlen(templates[tmpl_num - 1]);
3406 src_dup[dup_idx++] = *p;
3411 src_dup[dup_idx] = '\0';
3412 src_dup = realloc(src_dup, (strlen(src_dup) + 1) * sizeof(char));
3416 /* handle_template_object - core logic of the template object
3418 * use config variables like this:
3419 * template1 = "$\1\2"
3420 * template2 = "\1: ${fs_bar 4,100 \2} ${fs_used \2} / ${fs_size \2}"
3422 * and use them like this:
3423 * ${template1 node name}
3424 * ${template2 root /}
3425 * ${template2 cdrom /mnt/cdrom}
3427 static char *handle_template(const char *tmpl, const char *args)
3429 char *args_dup = NULL;
3431 char **argsp = NULL;
3432 unsigned int argcnt = 0, template_idx, i;
3435 if ((sscanf(tmpl, "template%u", &template_idx) != 1) ||
3436 (template_idx >= MAX_TEMPLATES))
3440 args_dup = strdup(args);
3443 while (*p && (*p == ' ' && (p == args_dup || *(p - 1) != '\\')))
3445 if (p > args_dup && *(p - 1) == '\\')
3448 while (*p && (*p != ' ' || (p > args_dup && *(p - 1) == '\\')))
3454 argsp = realloc(argsp, ++argcnt * sizeof(char *));
3455 argsp[argcnt - 1] = p_old;
3457 for (i = 0; i < argcnt; i++) {
3459 tmp = backslash_escape(argsp[i], NULL, 0);
3460 DBGP2("%s: substituted arg '%s' to '%s'", tmpl, argsp[i], tmp);
3465 eval_text = backslash_escape(template[template_idx], argsp, argcnt);
3466 DBGP("substituted %s, output is '%s'", tmpl, eval_text);
3468 for (i = 0; i < argcnt; i++)
3474 static char *find_and_replace_templates(const char *inbuf)
3476 char *outbuf, *indup, *p, *o, *templ, *args, *tmpl_out;
3479 outlen = strlen(inbuf) + 1;
3480 o = outbuf = calloc(outlen, sizeof(char));
3481 memset(outbuf, 0, outlen * sizeof(char));
3483 p = indup = strdup(inbuf);
3485 while (*p && *p != '$')
3491 if (strncmp(p, "$template", 9) && strncmp(p, "${template", 10)) {
3496 if (*(p + 1) == '{') {
3499 while (*p && !isspace(*p) && *p != '{' && *p != '}')
3507 while (*p && stack > 0) {
3515 // stack is empty. that means the previous char was }, so we zero it
3518 // we ran into the end of string without finding a closing }, bark
3519 CRIT_ERR(NULL, NULL, "cannot find a closing '}' in template expansion");
3523 while (*p && !isspace(*p))
3527 tmpl_out = handle_template(templ, args);
3529 outlen += strlen(tmpl_out);
3531 outbuf = realloc(outbuf, outlen * sizeof(char));
3532 strcat (outbuf, tmpl_out);
3534 o = outbuf + strlen(outbuf);
3536 NORM_ERR("failed to handle template '%s' with args '%s'", templ, args);
3540 outbuf = realloc(outbuf, (strlen(outbuf) + 1) * sizeof(char));
3545 static int text_contains_templates(const char *text)
3547 if (strcasestr(text, "${template") != NULL)
3549 if (strcasestr(text, "$template") != NULL)
3555 * - assumes that *string is '#'
3556 * - removes the part from '#' to the end of line ('\n' or '\0')
3557 * - it removes the '\n'
3558 * - copies the last char into 'char *last' argument, which should be a pointer
3559 * to a char rather than a string.
3561 static size_t remove_comment(char *string, char *last)
3564 while (*end != '\0' && *end != '\n') {
3567 if (last) *last = *end;
3568 if (*end == '\n') end++;
3569 strfold(string, end - string);
3570 return end - string;
3573 static size_t remove_comments(char *string)
3577 for (curplace = string; *curplace != 0; curplace++) {
3578 if (*curplace == '\\' && *(curplace + 1) == '#') {
3579 // strcpy can't be used for overlapping strings
3580 strfold(curplace, 1);
3582 } else if (*curplace == '#') {
3583 folded += remove_comment(curplace, 0);
3589 static int extract_variable_text_internal(struct text_object *retval, const char *const_p)
3591 struct text_object *obj;
3592 char *p, *s, *orig_p;
3594 void *ifblock_opaque = NULL;
3599 p = strndup(const_p, max_user_text - 1);
3600 while (text_contains_templates(p)) {
3602 tmp = find_and_replace_templates(p);
3608 if (strcmp(p, const_p)) {
3609 DBGP("replaced all templates in text: input is\n'%s'\noutput is\n'%s'", const_p, p);
3611 DBGP("no templates to replace");
3614 memset(retval, 0, sizeof(struct text_object));
3616 line = global_text_lines;
3624 obj = create_plain_text(s);
3626 append_object(retval, obj);
3636 /* variable is either $foo or ${foo} */
3638 unsigned int brl = 1, brr = 0;
3642 while (*p && brl != brr) {
3657 while (*p && (isalnum((int) *p) || *p == '_')) {
3662 /* copy variable to buffer */
3663 len = (p - s > 255) ? 255 : (p - s);
3664 strncpy(buf, s, len);
3672 /* search for variable in environment */
3676 obj = create_plain_text(var);
3678 append_object(retval, obj);
3683 /* if variable wasn't found in environment, use some special */
3688 if (strchr(buf, ' ')) {
3689 arg = strchr(buf, ' ');
3692 while (isspace((int) *arg)) {
3700 /* lowercase variable name */
3703 *tmp_p = tolower(*tmp_p);
3707 obj = construct_text_object(buf, arg,
3708 line, &ifblock_opaque, orig_p);
3710 append_object(retval, obj);
3714 obj = create_plain_text("$");
3717 append_object(retval, obj);
3720 } else if (*p == '\\' && *(p+1) == '#') {
3722 } else if (*p == '#') {
3724 if (remove_comment(p, &c) && p > orig_p && c == '\n') {
3725 /* if remove_comment removed a newline, we need to 'back up' with p */
3731 obj = create_plain_text(s);
3733 append_object(retval, obj);
3736 if (!ifblock_stack_empty(&ifblock_opaque)) {
3737 NORM_ERR("one or more $endif's are missing");
3744 static void extract_variable_text(const char *p)
3746 free_text_objects(&global_root_object, 0);
3760 extract_variable_text_internal(&global_root_object, p);
3763 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
3765 extract_variable_text_internal(root, txt);
3766 generate_text_internal(p, max_user_text, *root, cur);
3769 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
3770 void *thread(void *), const char *text)
3772 if (obj->char_b && info.mail) {
3773 // this means we use info
3774 if (!info.mail->p_timed_thread) {
3775 info.mail->p_timed_thread =
3776 timed_thread_create(thread,
3777 (void *) info.mail, info.mail->interval * 1000000);
3778 if (!info.mail->p_timed_thread) {
3779 NORM_ERR("Error creating %s timed thread", text);
3781 timed_thread_register(info.mail->p_timed_thread,
3782 &info.mail->p_timed_thread);
3783 if (timed_thread_run(info.mail->p_timed_thread)) {
3784 NORM_ERR("Error running %s timed thread", text);
3788 } else if (obj->data.mail) {
3789 // this means we use obj
3790 if (!obj->data.mail->p_timed_thread) {
3791 obj->data.mail->p_timed_thread =
3792 timed_thread_create(thread,
3793 (void *) obj->data.mail,
3794 obj->data.mail->interval * 1000000);
3795 if (!obj->data.mail->p_timed_thread) {
3796 NORM_ERR("Error creating %s timed thread", text);
3798 timed_thread_register(obj->data.mail->p_timed_thread,
3799 &obj->data.mail->p_timed_thread);
3800 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
3801 NORM_ERR("Error running %s timed thread", text);
3804 return obj->data.mail;
3805 } else if (!obj->a) {
3806 // something is wrong, warn once then stop
3807 NORM_ERR("There's a problem with your mail settings. "
3808 "Check that the global mail settings are properly defined"
3809 " (line %li).", obj->line);
3815 char *format_time(unsigned long timeval, const int width)
3818 unsigned long nt; // narrow time, for speed on 32-bit
3819 unsigned cc; // centiseconds
3820 unsigned nn; // multi-purpose whatever
3823 cc = nt % 100; // centiseconds past second
3824 nt /= 100; // total seconds
3825 nn = nt % 60; // seconds past the minute
3826 nt /= 60; // total minutes
3827 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
3829 return strndup(buf, text_buffer_size);
3831 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
3832 return strndup(buf, text_buffer_size);
3834 nn = nt % 60; // minutes past the hour
3835 nt /= 60; // total hours
3836 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
3837 return strndup(buf, text_buffer_size);
3839 nn = nt; // now also hours
3840 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
3841 return strndup(buf, text_buffer_size);
3843 nn /= 24; // now days
3844 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
3845 return strndup(buf, text_buffer_size);
3847 nn /= 7; // now weeks
3848 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
3849 return strndup(buf, text_buffer_size);
3851 // well shoot, this outta' fit...
3852 return strndup("<inf>", text_buffer_size);
3855 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
3856 //string has to end with \0 and it's length should fit in a int
3858 void remove_deleted_chars(char *string){
3860 while(string[i] != 0){
3861 if(string[i] == BACKSPACE){
3863 strcpy( &(string[i-1]), &(string[i+1]) );
3865 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
3870 static inline void format_media_player_time(char *buf, const int size,
3873 int days, hours, minutes;
3875 days = seconds / (24 * 60 * 60);
3876 seconds %= (24 * 60 * 60);
3877 hours = seconds / (60 * 60);
3878 seconds %= (60 * 60);
3879 minutes = seconds / 60;
3883 snprintf(buf, size, "%i days %i:%02i:%02i", days,
3884 hours, minutes, seconds);
3885 } else if (hours > 0) {
3886 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
3889 snprintf(buf, size, "%i:%02i", minutes, seconds);
3893 static inline double get_barnum(char *buf)
3905 if (sscanf(buf, "%lf", &barnum) == 0) {
3906 NORM_ERR("reading exec value failed (perhaps it's not the "
3907 "correct format?)");
3910 if (barnum > 100.0 || barnum < 0.0) {
3911 NORM_ERR("your exec value is not between 0 and 100, "
3912 "therefore it will be ignored");
3918 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
3919 * multiline objects like $exec work with $align[rc] and friends
3921 void substitute_newlines(char *p, long l)
3925 while (p && *p && p < s + l) {
3927 /* only substitute if it's not the last newline */
3928 *p = SECRIT_MULTILINE_CHAR;
3934 static void generate_text_internal(char *p, int p_max_size,
3935 struct text_object root, struct information *cur)
3937 struct text_object *obj;
3939 int need_to_load_fonts = 0;
3942 /* for the OBJ_top* handler */
3943 struct process **needed = 0;
3946 char buff_in[p_max_size];
3948 iconv_converting = 0;
3949 #endif /* HAVE_ICONV */
3953 while (obj && p_max_size > 0) {
3954 needed = 0; /* reset for top stuff */
3956 /* IFBLOCK jumping algorithm
3958 * This is easier as it looks like:
3959 * - each IF checks it's condition
3960 * - on FALSE: call DO_JUMP
3961 * - on TRUE: don't care
3962 * - each ELSE calls DO_JUMP unconditionally
3963 * - each ENDIF is silently being ignored
3966 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
3967 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
3968 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
3969 * regularly, "obj" is being updated to point to obj->next, so object parsing
3970 * continues right after the corresponding ELSE or ENDIF. This means that if we
3971 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
3972 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
3973 * jumped, and there is nothing to do.
3977 obj = obj->data.ifblock.next; \
3980 #define OBJ(a) break; case OBJ_##a:
3982 switch (obj->type) {
3984 NORM_ERR("not implemented obj type %d", obj->type);
3987 struct sockaddr_in addr;
3988 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
3990 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
3992 memset(&addr, 0, sizeof(addr));
3993 addr.sin_family = AF_INET;
3994 addr.sin_port = obj->data.read_tcp.port;
3995 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
3996 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
4000 FD_SET(sock, &readfds);
4003 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
4004 received = recv(sock, p, p_max_size, 0);
4009 NORM_ERR("read_tcp: Couldn't create a connection");
4012 NORM_ERR("read_tcp: Couldn't create a socket");
4015 NORM_ERR("read_tcp: Problem with resolving the hostname");
4020 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
4022 #endif /* !__OpenBSD__ */
4025 obj->a = get_freq(p, p_max_size, "%.0f", 1,
4026 obj->data.cpu_index);
4032 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
4033 obj->data.cpu_index);
4035 /* OpenBSD has no such flag (SUSv2) */
4036 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
4037 obj->data.cpu_index);
4038 #endif /* __OpenBSD */
4041 #if defined(__linux__)
4044 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
4045 obj->data.cpu_index);
4050 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
4051 obj->data.cpu_index);
4056 OBJ(wireless_essid) {
4057 snprintf(p, p_max_size, "%s", obj->data.net->essid);
4059 OBJ(wireless_mode) {
4060 snprintf(p, p_max_size, "%s", obj->data.net->mode);
4062 OBJ(wireless_bitrate) {
4063 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
4066 snprintf(p, p_max_size, "%s", obj->data.net->ap);
4068 OBJ(wireless_link_qual) {
4069 spaced_print(p, p_max_size, "%d", 4,
4070 obj->data.net->link_qual);
4072 OBJ(wireless_link_qual_max) {
4073 spaced_print(p, p_max_size, "%d", 4,
4074 obj->data.net->link_qual_max);
4076 OBJ(wireless_link_qual_perc) {
4077 if (obj->data.net->link_qual_max > 0) {
4078 spaced_print(p, p_max_size, "%.0f", 5,
4079 (double) obj->data.net->link_qual /
4080 obj->data.net->link_qual_max * 100);
4082 spaced_print(p, p_max_size, "unk", 5);
4085 OBJ(wireless_link_bar) {
4087 if(output_methods & TO_X) {
4088 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
4089 obj->data.net->link_qual_max) * 255.0);
4092 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4093 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
4094 obj->data.net->link_qual_max) * 100.0, obj->a);
4099 #endif /* HAVE_IWLIB */
4101 #endif /* __linux__ */
4105 get_adt746x_cpu(p, p_max_size);
4108 get_adt746x_fan(p, p_max_size);
4111 get_acpi_fan(p, p_max_size);
4113 OBJ(acpiacadapter) {
4114 get_acpi_ac_adapter(p, p_max_size);
4117 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
4120 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
4122 OBJ(battery_percent) {
4123 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
4127 if(output_methods & TO_X) {
4128 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
4131 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4132 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
4137 OBJ(battery_short) {
4138 get_battery_short_status(p, p_max_size, obj->data.s);
4140 #endif /* __OpenBSD__ */
4143 human_readable(cur->buffers * 1024, p, 255);
4146 human_readable(cur->cached * 1024, p, 255);
4149 if (obj->data.cpu_index > info.cpu_count) {
4150 NORM_ERR("obj->data.cpu_index %i info.cpu_count %i",
4151 obj->data.cpu_index, info.cpu_count);
4152 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
4154 percent_print(p, p_max_size,
4155 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
4159 new_gauge(p, obj->a, obj->b,
4160 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4164 if(output_methods & TO_X) {
4165 new_bar(p, obj->a, obj->b,
4166 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
4169 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4170 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
4177 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4178 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
4179 100, 1, obj->char_a, obj->char_b);
4182 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
4183 obj->e, 1, obj->char_a, obj->char_b);
4187 new_fg(p, obj->data.l);
4221 OBJ(conky_version) {
4222 snprintf(p, p_max_size, "%s", VERSION);
4224 OBJ(conky_build_date) {
4225 snprintf(p, p_max_size, "%s", BUILD_DATE);
4227 OBJ(conky_build_arch) {
4228 snprintf(p, p_max_size, "%s", BUILD_ARCH);
4230 #if defined(__linux__)
4232 snprintf(p, p_max_size, "%s",
4233 get_disk_protect_queue(obj->data.s));
4236 snprintf(p, p_max_size, "%s", i8k.version);
4239 snprintf(p, p_max_size, "%s", i8k.bios);
4242 snprintf(p, p_max_size, "%s", i8k.serial);
4247 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
4248 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
4250 OBJ(i8k_left_fan_status) {
4251 int left_fan_status;
4253 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
4254 if (left_fan_status == 0) {
4255 snprintf(p, p_max_size, "off");
4257 if (left_fan_status == 1) {
4258 snprintf(p, p_max_size, "low");
4260 if (left_fan_status == 2) {
4261 snprintf(p, p_max_size, "high");
4264 OBJ(i8k_right_fan_status) {
4265 int right_fan_status;
4267 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
4268 if (right_fan_status == 0) {
4269 snprintf(p, p_max_size, "off");
4271 if (right_fan_status == 1) {
4272 snprintf(p, p_max_size, "low");
4274 if (right_fan_status == 2) {
4275 snprintf(p, p_max_size, "high");
4278 OBJ(i8k_left_fan_rpm) {
4279 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
4281 OBJ(i8k_right_fan_rpm) {
4282 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
4284 OBJ(i8k_ac_status) {
4287 sscanf(i8k.ac_status, "%d", &ac_status);
4288 if (ac_status == -1) {
4289 snprintf(p, p_max_size, "disabled (read i8k docs)");
4291 if (ac_status == 0) {
4292 snprintf(p, p_max_size, "off");
4294 if (ac_status == 1) {
4295 snprintf(p, p_max_size, "on");
4298 OBJ(i8k_buttons_status) {
4299 snprintf(p, p_max_size, "%s", i8k.buttons_status);
4303 get_ibm_acpi_fan(p, p_max_size);
4306 get_ibm_acpi_temps();
4307 temp_print(p, p_max_size,
4308 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
4311 get_ibm_acpi_volume(p, p_max_size);
4313 OBJ(ibm_brightness) {
4314 get_ibm_acpi_brightness(p, p_max_size);
4317 /* information from sony_laptop kernel module
4318 * /sys/devices/platform/sony-laptop */
4319 OBJ(sony_fanspeed) {
4320 get_sony_fanspeed(p, p_max_size);
4323 if (!cur->gw_info.count) {
4328 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
4331 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
4334 snprintf(p, p_max_size, "%d", get_laptop_mode());
4337 get_powerbook_batt_info(p, p_max_size, obj->data.i);
4339 #endif /* __linux__ */
4340 #if (defined(__FreeBSD__) || defined(__linux__))
4342 if ((obj->data.ifblock.s)
4343 && (!interface_up(obj->data.ifblock.s))) {
4349 OBJ(obsd_sensors_temp) {
4350 obsd_sensors.device = sensor_device;
4351 update_obsd_sensors();
4352 temp_print(p, p_max_size,
4353 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
4356 OBJ(obsd_sensors_fan) {
4357 obsd_sensors.device = sensor_device;
4358 update_obsd_sensors();
4359 snprintf(p, p_max_size, "%d",
4360 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
4362 OBJ(obsd_sensors_volt) {
4363 obsd_sensors.device = sensor_device;
4364 update_obsd_sensors();
4365 snprintf(p, p_max_size, "%.2f",
4366 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
4369 get_obsd_vendor(p, p_max_size);
4372 get_obsd_product(p, p_max_size);
4374 #endif /* __OpenBSD__ */
4377 new_font(p, obj->data.s);
4378 need_to_load_fonts = 1;
4381 /* TODO: move this correction from kB to kB/s elsewhere
4382 * (or get rid of it??) */
4384 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
4388 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
4392 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
4397 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4398 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
4400 OBJ(diskiograph_read) {
4401 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4402 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
4404 OBJ(diskiograph_write) {
4405 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4406 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
4410 human_readable(obj->data.net->recv_speed, p, 255);
4413 spaced_print(p, p_max_size, "%.1f", 8,
4414 obj->data.net->recv_speed / 1024.0);
4417 OBJ(downspeedgraph) {
4418 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4419 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
4423 /* Since we see you, you're if has not jumped.
4424 * Do Ninja jump here: without leaving traces.
4425 * This is to prevent us from stale jumped flags.
4427 obj = obj->data.ifblock.next;
4431 /* harmless object, just ignore */
4434 if ((obj->data.net->addr.sa_data[2] & 255) == 0
4435 && (obj->data.net->addr.sa_data[3] & 255) == 0
4436 && (obj->data.net->addr.sa_data[4] & 255) == 0
4437 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
4438 snprintf(p, p_max_size, "No Address");
4440 snprintf(p, p_max_size, "%u.%u.%u.%u",
4441 obj->data.net->addr.sa_data[2] & 255,
4442 obj->data.net->addr.sa_data[3] & 255,
4443 obj->data.net->addr.sa_data[4] & 255,
4444 obj->data.net->addr.sa_data[5] & 255);
4447 #if defined(__linux__)
4449 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
4450 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
4451 strcpy(p, obj->data.net->addrs);
4453 strcpy(p, "0.0.0.0");
4456 #endif /* __linux__ */
4457 #if defined(IMLIB2) && defined(X11)
4459 /* doesn't actually draw anything, just queues it omp. the
4460 * image will get drawn after the X event loop */
4461 cimlib_add_image(obj->data.s);
4465 evaluate(obj->data.s, p);
4468 read_exec(obj->data.s, p, text_buffer_size);
4469 remove_deleted_chars(p);
4472 struct information *tmp_info;
4473 struct text_object subroot;
4475 read_exec(obj->data.s, p, text_buffer_size);
4477 tmp_info = malloc(sizeof(struct information));
4478 memcpy(tmp_info, cur, sizeof(struct information));
4479 parse_conky_vars(&subroot, p, p, tmp_info);
4481 free_text_objects(&subroot, 1);
4488 read_exec(obj->data.s, p, text_buffer_size);
4489 barnum = get_barnum(p); /*using the same function*/
4491 if (barnum >= 0.0) {
4493 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4500 read_exec(obj->data.s, p, text_buffer_size);
4501 barnum = get_barnum(p);
4503 if (barnum >= 0.0) {
4505 if(output_methods & TO_X) {
4507 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
4510 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4511 new_bar_in_shell(p, p_max_size, barnum, obj->a);
4519 char showaslog = FALSE;
4520 char tempgrad = FALSE;
4522 char *cmd = obj->data.s;
4524 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4526 cmd += strlen(" "TEMPGRAD);
4528 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4530 cmd += strlen(" "LOGGRAPH);
4532 read_exec(cmd, p, text_buffer_size);
4533 barnum = get_barnum(p);
4536 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
4537 100, 1, showaslog, tempgrad);
4542 if (current_update_time - obj->data.execi.last_update
4543 >= obj->data.execi.interval) {
4546 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4547 barnum = get_barnum(p);
4549 if (barnum >= 0.0) {
4552 obj->data.execi.last_update = current_update_time;
4555 if(output_methods & TO_X) {
4556 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
4559 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4560 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
4567 if (current_update_time - obj->data.execi.last_update
4568 >= obj->data.execi.interval) {
4570 char showaslog = FALSE;
4571 char tempgrad = FALSE;
4572 char *cmd = obj->data.execi.cmd;
4574 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
4576 cmd += strlen(" "TEMPGRAD);
4578 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
4580 cmd += strlen(" "LOGGRAPH);
4582 obj->char_a = showaslog;
4583 obj->char_b = tempgrad;
4584 read_exec(cmd, p, text_buffer_size);
4585 barnum = get_barnum(p);
4587 if (barnum >= 0.0) {
4590 obj->data.execi.last_update = current_update_time;
4592 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
4595 if (current_update_time - obj->data.execi.last_update
4596 >= obj->data.execi.interval) {
4599 read_exec(obj->data.execi.cmd, p, text_buffer_size);
4600 barnum = get_barnum(p);
4602 if (barnum >= 0.0) {
4603 obj->f = 255 * barnum / 100.0;
4605 obj->data.execi.last_update = current_update_time;
4607 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
4611 if (current_update_time - obj->data.execi.last_update
4612 >= obj->data.execi.interval
4613 && obj->data.execi.interval != 0) {
4614 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
4616 obj->data.execi.last_update = current_update_time;
4618 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
4621 struct text_object subroot;
4622 struct information *tmp_info =
4623 malloc(sizeof(struct information));
4624 memcpy(tmp_info, cur, sizeof(struct information));
4626 if (current_update_time - obj->data.execi.last_update
4627 < obj->data.execi.interval
4628 || obj->data.execi.interval == 0) {
4629 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4631 char *output = obj->data.execi.buffer;
4632 FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
4633 int length = fread(output, 1, text_buffer_size, fp);
4637 output[length] = '\0';
4638 if (length > 0 && output[length - 1] == '\n') {
4639 output[length - 1] = '\0';
4642 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
4643 obj->data.execi.last_update = current_update_time;
4645 free_text_objects(&subroot, 1);
4649 if (!obj->data.texeci.p_timed_thread) {
4650 obj->data.texeci.p_timed_thread =
4651 timed_thread_create(&threaded_exec,
4652 (void *) obj, obj->data.texeci.interval * 1000000);
4653 if (!obj->data.texeci.p_timed_thread) {
4654 NORM_ERR("Error creating texeci timed thread");
4657 * note that we don't register this thread with the
4658 * timed_thread list, because we destroy it manually
4660 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
4661 NORM_ERR("Error running texeci timed thread");
4664 timed_thread_lock(obj->data.texeci.p_timed_thread);
4665 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
4666 timed_thread_unlock(obj->data.texeci.p_timed_thread);
4670 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4672 if (mail && mail->p_timed_thread) {
4673 timed_thread_lock(mail->p_timed_thread);
4674 snprintf(p, p_max_size, "%lu", mail->unseen);
4675 timed_thread_unlock(mail->p_timed_thread);
4678 OBJ(imap_messages) {
4679 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
4681 if (mail && mail->p_timed_thread) {
4682 timed_thread_lock(mail->p_timed_thread);
4683 snprintf(p, p_max_size, "%lu", mail->messages);
4684 timed_thread_unlock(mail->p_timed_thread);
4688 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4690 if (mail && mail->p_timed_thread) {
4691 timed_thread_lock(mail->p_timed_thread);
4692 snprintf(p, p_max_size, "%lu", mail->unseen);
4693 timed_thread_unlock(mail->p_timed_thread);
4697 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
4699 if (mail && mail->p_timed_thread) {
4700 timed_thread_lock(mail->p_timed_thread);
4701 snprintf(p, p_max_size, "%.1f",
4702 mail->used / 1024.0 / 1024.0);
4703 timed_thread_unlock(mail->p_timed_thread);
4707 if (obj->data.fs != NULL) {
4708 if (obj->data.fs->size == 0) {
4710 if(output_methods & TO_X) {
4711 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4714 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4715 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4721 if(output_methods & TO_X) {
4722 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4723 (int) (255 - obj->data.fsbar.fs->avail * 255 /
4724 obj->data.fs->size));
4727 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4728 new_bar_in_shell(p, p_max_size,
4729 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4737 if (obj->data.fs != NULL) {
4738 human_readable(obj->data.fs->avail, p, 255);
4742 if (obj->data.fs != NULL) {
4745 if (obj->data.fs->size) {
4746 val = obj->data.fs->avail * 100 / obj->data.fs->size;
4749 percent_print(p, p_max_size, val);
4753 if (obj->data.fs != NULL) {
4754 human_readable(obj->data.fs->size, p, 255);
4758 if (obj->data.fs != NULL)
4759 snprintf(p, p_max_size, "%s", obj->data.fs->type);
4762 if (obj->data.fs != NULL) {
4763 human_readable(obj->data.fs->size - obj->data.fs->free, p,
4768 if (obj->data.fs != NULL) {
4769 if (obj->data.fs->size == 0) {
4771 if(output_methods & TO_X) {
4772 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
4775 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4776 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
4782 if(output_methods & TO_X) {
4783 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
4784 (int) (obj->data.fsbar.fs->avail * 255 /
4785 obj->data.fs->size));
4788 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
4789 new_bar_in_shell(p, p_max_size,
4790 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
4798 if (obj->data.fs != NULL) {
4801 if (obj->data.fs->size) {
4802 val = obj->data.fs->free
4807 percent_print(p, p_max_size, 100 - val);
4811 float *v = info.loadavg;
4813 if (obj->data.loadavg[2]) {
4814 snprintf(p, p_max_size, "%.2f %.2f %.2f",
4815 v[obj->data.loadavg[0] - 1],
4816 v[obj->data.loadavg[1] - 1],
4817 v[obj->data.loadavg[2] - 1]);
4818 } else if (obj->data.loadavg[1]) {
4819 snprintf(p, p_max_size, "%.2f %.2f",
4820 v[obj->data.loadavg[0] - 1],
4821 v[obj->data.loadavg[1] - 1]);
4822 } else if (obj->data.loadavg[0]) {
4823 snprintf(p, p_max_size, "%.2f",
4824 v[obj->data.loadavg[0] - 1]);
4828 new_goto(p, obj->data.i);
4831 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4835 new_hr(p, obj->data.i);
4839 if (cur->nameserver_info.nscount > obj->data.i)
4840 snprintf(p, p_max_size, "%s",
4841 cur->nameserver_info.ns_list[obj->data.i]);
4845 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
4846 snprintf(p, p_max_size, "%s", skill);
4851 if (obj->data.curl.uri != NULL) {
4852 ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
4854 NORM_ERR("error processing Curl data");
4860 if (obj->data.rss.uri != NULL) {
4861 rss_process_info(p, p_max_size, obj->data.rss.uri, obj->data.rss.action, obj->data.rss.act_par, obj->data.rss.interval, obj->data.rss.nrspaces);
4863 NORM_ERR("error processing RSS data");
4869 if (obj->data.weather.uri != NULL) {
4870 weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
4872 NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
4877 OBJ(weather_forecast) {
4878 if (obj->data.weather_forecast.uri != NULL) {
4879 weather_forecast_process_info(p, p_max_size, obj->data.weather_forecast.uri, obj->data.weather_forecast.day, obj->data.weather_forecast.data_type, obj->data.weather_forecast.interval);
4881 NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
4887 char *str = llua_getstring(obj->data.s);
4889 snprintf(p, p_max_size, "%s", str);
4894 char *str = llua_getstring(obj->data.s);
4902 if (llua_getnumber(obj->data.s, &per)) {
4904 if(output_methods & TO_X) {
4905 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
4908 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
4909 new_bar_in_shell(p, p_max_size, per, obj->a);
4918 if (llua_getnumber(obj->data.s, &per)) {
4919 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4920 per, obj->e, 1, obj->char_a, obj->char_b);
4925 if (llua_getnumber(obj->data.s, &per)) {
4926 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
4930 #endif /* HAVE_LUA */
4935 if (obj->data.hddtemp.update_time < current_update_time - 30) {
4936 if (obj->data.hddtemp.temp)
4937 free(obj->data.hddtemp.temp);
4938 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
4939 obj->data.hddtemp.addr, obj->data.hddtemp.port);
4940 obj->data.hddtemp.update_time = current_update_time;
4942 if (!obj->data.hddtemp.temp) {
4943 snprintf(p, p_max_size, "N/A");
4945 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
4946 unit = obj->data.hddtemp.temp[0];
4948 if (*endptr != '\0')
4949 snprintf(p, p_max_size, "N/A");
4950 else if (unit == 'C')
4951 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
4952 else if (unit == 'F')
4953 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
4955 snprintf(p, p_max_size, "N/A");
4960 new_offset(p, obj->data.i);
4963 new_voffset(p, obj->data.i);
4969 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4970 obj->data.sysfs.devtype, obj->data.sysfs.type);
4972 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4974 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4975 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4976 } else if (r >= 100.0 || r == 0) {
4977 snprintf(p, p_max_size, "%d", (int) r);
4979 snprintf(p, p_max_size, "%.1f", r);
4985 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
4986 obj->data.sysfs.devtype, obj->data.sysfs.type);
4988 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
4990 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
4991 temp_print(p, p_max_size, r, TEMP_CELSIUS);
4992 } else if (r >= 100.0 || r == 0) {
4993 snprintf(p, p_max_size, "%d", (int) r);
4995 snprintf(p, p_max_size, "%.1f", r);
5001 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
5002 obj->data.sysfs.devtype, obj->data.sysfs.type);
5004 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
5006 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
5007 temp_print(p, p_max_size, r, TEMP_CELSIUS);
5008 } else if (r >= 100.0 || r == 0) {
5009 snprintf(p, p_max_size, "%d", (int) r);
5011 snprintf(p, p_max_size, "%.1f", r);
5014 #endif /* __linux__ */
5016 new_alignr(p, obj->data.i);
5019 new_alignc(p, obj->data.i);
5022 char buf[max_user_text];
5023 struct information *tmp_info =
5024 malloc(sizeof(struct information));
5025 memcpy(tmp_info, cur, sizeof(struct information));
5026 generate_text_internal(buf, max_user_text,
5027 *obj->sub, tmp_info);
5029 if (strlen(buf) != 0) {
5035 char expression[max_user_text];
5037 struct information *tmp_info;
5039 tmp_info = malloc(sizeof(struct information));
5040 memcpy(tmp_info, cur, sizeof(struct information));
5041 generate_text_internal(expression, max_user_text,
5042 *obj->sub, tmp_info);
5043 DBGP("parsed arg into '%s'", expression);
5045 val = compare(expression);
5047 NORM_ERR("compare failed for expression '%s'",
5055 if (obj->data.ifblock.str
5056 && !check_contains(obj->data.ifblock.s,
5057 obj->data.ifblock.str)) {
5059 } else if (obj->data.ifblock.s
5060 && access(obj->data.ifblock.s, F_OK)) {
5065 if ((obj->data.ifblock.s)
5066 && (!check_mount(obj->data.ifblock.s))) {
5072 if (!get_process_by_name(obj->data.ifblock.s)) {
5074 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
5079 #if defined(__linux__)
5081 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
5085 snprintf(p, p_max_size, "%s", cur->uname_s.release);
5088 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
5093 human_readable(cur->mem * 1024, p, 255);
5096 human_readable(cur->memeasyfree * 1024, p, 255);
5099 human_readable(cur->memfree * 1024, p, 255);
5102 human_readable(cur->memmax * 1024, p, 255);
5106 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
5110 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
5111 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5116 if(output_methods & TO_X) {
5117 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5118 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
5121 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5122 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
5129 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5130 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
5131 100, 1, obj->char_a, obj->char_b);
5136 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
5139 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
5142 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
5146 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5147 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
5150 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5151 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
5154 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
5155 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
5158 OBJ(if_mixer_mute) {
5159 if (!mixer_is_mute(obj->data.ifblock.i)) {
5164 #define NOT_IN_X "Not running in X"
5166 if(x_initialised != YES) {
5167 strncpy(p, NOT_IN_X, p_max_size);
5169 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
5172 OBJ(monitor_number) {
5173 if(x_initialised != YES) {
5174 strncpy(p, NOT_IN_X, p_max_size);
5176 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
5180 if(x_initialised != YES) {
5181 strncpy(p, NOT_IN_X, p_max_size);
5183 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
5186 OBJ(desktop_number) {
5187 if(x_initialised != YES) {
5188 strncpy(p, NOT_IN_X, p_max_size);
5190 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
5194 if(x_initialised != YES) {
5195 strncpy(p, NOT_IN_X, p_max_size);
5196 }else if(cur->x11.desktop.name != NULL) {
5197 strncpy(p, cur->x11.desktop.name, p_max_size);
5204 update_mail_count(&obj->data.local_mail);
5205 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
5208 update_mail_count(&obj->data.local_mail);
5209 snprintf(p, p_max_size, "%d",
5210 obj->data.local_mail.new_mail_count);
5213 update_mail_count(&obj->data.local_mail);
5214 snprintf(p, p_max_size, "%d",
5215 obj->data.local_mail.seen_mail_count);
5218 update_mail_count(&obj->data.local_mail);
5219 snprintf(p, p_max_size, "%d",
5220 obj->data.local_mail.unseen_mail_count);
5222 OBJ(flagged_mails) {
5223 update_mail_count(&obj->data.local_mail);
5224 snprintf(p, p_max_size, "%d",
5225 obj->data.local_mail.flagged_mail_count);
5227 OBJ(unflagged_mails) {
5228 update_mail_count(&obj->data.local_mail);
5229 snprintf(p, p_max_size, "%d",
5230 obj->data.local_mail.unflagged_mail_count);
5232 OBJ(forwarded_mails) {
5233 update_mail_count(&obj->data.local_mail);
5234 snprintf(p, p_max_size, "%d",
5235 obj->data.local_mail.forwarded_mail_count);
5237 OBJ(unforwarded_mails) {
5238 update_mail_count(&obj->data.local_mail);
5239 snprintf(p, p_max_size, "%d",
5240 obj->data.local_mail.unforwarded_mail_count);
5242 OBJ(replied_mails) {
5243 update_mail_count(&obj->data.local_mail);
5244 snprintf(p, p_max_size, "%d",
5245 obj->data.local_mail.replied_mail_count);
5247 OBJ(unreplied_mails) {
5248 update_mail_count(&obj->data.local_mail);
5249 snprintf(p, p_max_size, "%d",
5250 obj->data.local_mail.unreplied_mail_count);
5253 update_mail_count(&obj->data.local_mail);
5254 snprintf(p, p_max_size, "%d",
5255 obj->data.local_mail.draft_mail_count);
5257 OBJ(trashed_mails) {
5258 update_mail_count(&obj->data.local_mail);
5259 snprintf(p, p_max_size, "%d",
5260 obj->data.local_mail.trashed_mail_count);
5263 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
5265 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
5268 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
5271 new_outline(p, obj->data.l);
5274 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
5276 OBJ(running_processes) {
5277 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
5280 snprintf(p, p_max_size, "%s", obj->data.s);
5284 new_bg(p, obj->data.l);
5287 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
5291 human_readable(cur->swap * 1024, p, 255);
5294 human_readable(cur->swapfree * 1024, p, 255);
5297 human_readable(cur->swapmax * 1024, p, 255);
5300 if (cur->swapmax == 0) {
5301 strncpy(p, "No swap", p_max_size);
5303 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
5308 if(output_methods & TO_X) {
5309 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5310 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
5313 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5314 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
5320 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
5323 time_t t = time(NULL);
5324 struct tm *tm = localtime(&t);
5326 setlocale(LC_TIME, "");
5327 strftime(p, p_max_size, obj->data.s, tm);
5330 time_t t = time(NULL);
5331 struct tm *tm = gmtime(&t);
5333 strftime(p, p_max_size, obj->data.s, tm);
5340 if (obj->data.tztime.tz) {
5341 oldTZ = getenv("TZ");
5342 setenv("TZ", obj->data.tztime.tz, 1);
5348 setlocale(LC_TIME, "");
5349 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
5351 setenv("TZ", oldTZ, 1);
5356 // Needless to free oldTZ since getenv gives ptr to static data
5359 human_readable(obj->data.net->recv, p, 255);
5362 human_readable(obj->data.net->trans, p, 255);
5365 snprintf(p, p_max_size, "%d", total_updates);
5368 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
5373 human_readable(obj->data.net->trans_speed, p, 255);
5376 spaced_print(p, p_max_size, "%.1f", 8,
5377 obj->data.net->trans_speed / 1024.0);
5381 new_graph(p, obj->a, obj->b, obj->c, obj->d,
5382 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
5386 format_seconds_short(p, p_max_size, (int) cur->uptime);
5389 format_seconds(p, p_max_size, (int) cur->uptime);
5392 snprintf(p, p_max_size, "%s", cur->users.names);
5395 snprintf(p, p_max_size, "%s", cur->users.terms);
5398 snprintf(p, p_max_size, "%s", cur->users.times);
5401 snprintf(p, p_max_size, "%d", cur->users.number);
5403 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
5404 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
5408 msg = get_apm_adapter();
5409 snprintf(p, p_max_size, "%s", msg);
5412 OBJ(apm_battery_life) {
5415 msg = get_apm_battery_life();
5416 snprintf(p, p_max_size, "%s", msg);
5419 OBJ(apm_battery_time) {
5422 msg = get_apm_battery_time();
5423 snprintf(p, p_max_size, "%s", msg);
5426 #endif /* __FreeBSD__ __OpenBSD__ */
5429 #define mpd_printf(fmt, val) \
5430 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
5431 #define mpd_sprintf(val) { \
5432 if (!obj->data.i || obj->data.i > p_max_size) \
5433 mpd_printf("%s", val); \
5435 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
5440 mpd_sprintf(artist);
5444 mpd_printf("%s", random);
5446 mpd_printf("%s", repeat);
5454 mpd_printf("%d", volume);
5456 mpd_printf("%d", bitrate);
5458 mpd_printf("%s", status);
5460 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
5463 format_media_player_time(p, p_max_size, mpd_get_info()->length);
5466 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
5470 if(output_methods & TO_X) {
5471 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5472 (int) (mpd_get_info()->progress * 255.0f));
5475 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
5476 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
5482 struct mpd_s *mpd = mpd_get_info();
5483 int len = obj->data.i;
5484 if (len == 0 || len > p_max_size)
5487 memset(p, 0, p_max_size);
5488 if (mpd->artist && *mpd->artist &&
5489 mpd->title && *mpd->title) {
5490 snprintf(p, len, "%s - %s", mpd->artist,
5492 } else if (mpd->title && *mpd->title) {
5493 snprintf(p, len, "%s", mpd->title);
5494 } else if (mpd->artist && *mpd->artist) {
5495 snprintf(p, len, "%s", mpd->artist);
5496 } else if (mpd->file && *mpd->file) {
5497 snprintf(p, len, "%s", mpd->file);
5502 OBJ(if_mpd_playing) {
5503 if (!mpd_get_info()->is_playing) {
5512 #define MOC_PRINT(t, a) \
5513 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
5515 MOC_PRINT(state, "??");
5518 MOC_PRINT(file, "no file");
5521 MOC_PRINT(title, "no title");
5524 MOC_PRINT(artist, "no artist");
5527 MOC_PRINT(song, "no song");
5530 MOC_PRINT(album, "no album");
5532 OBJ(moc_totaltime) {
5533 MOC_PRINT(totaltime, "0:00");
5536 MOC_PRINT(timeleft, "0:00");
5539 MOC_PRINT(curtime, "0:00");
5542 MOC_PRINT(bitrate, "0Kbps");
5545 MOC_PRINT(rate, "0KHz");
5551 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
5554 snprintf(p, p_max_size, "%s", cur->xmms2.album);
5557 snprintf(p, p_max_size, "%s", cur->xmms2.title);
5560 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
5562 OBJ(xmms2_comment) {
5563 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
5566 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5569 snprintf(p, p_max_size, "%s", cur->xmms2.status);
5572 snprintf(p, p_max_size, "%s", cur->xmms2.date);
5574 OBJ(xmms2_tracknr) {
5575 if (cur->xmms2.tracknr != -1) {
5576 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
5579 OBJ(xmms2_bitrate) {
5580 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
5583 snprintf(p, p_max_size, "%u", cur->xmms2.id);
5586 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
5588 OBJ(xmms2_elapsed) {
5589 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
5590 (cur->xmms2.elapsed / 1000) % 60);
5592 OBJ(xmms2_duration) {
5593 snprintf(p, p_max_size, "%02d:%02d",
5594 cur->xmms2.duration / 60000,
5595 (cur->xmms2.duration / 1000) % 60);
5597 OBJ(xmms2_percent) {
5598 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
5602 new_bar(p, obj->data.pair.a, obj->data.pair.b,
5603 (int) (cur->xmms2.progress * 255.0f));
5606 OBJ(xmms2_playlist) {
5607 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
5609 OBJ(xmms2_timesplayed) {
5610 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
5613 if (strlen(cur->xmms2.title) < 2
5614 && strlen(cur->xmms2.title) < 2) {
5615 snprintf(p, p_max_size, "%s", cur->xmms2.url);
5617 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
5621 OBJ(if_xmms2_connected) {
5622 if (cur->xmms2.conn_state != 1) {
5628 OBJ(audacious_status) {
5629 snprintf(p, p_max_size, "%s",
5630 cur->audacious.items[AUDACIOUS_STATUS]);
5632 OBJ(audacious_title) {
5633 snprintf(p, cur->audacious.max_title_len > 0
5634 ? cur->audacious.max_title_len : p_max_size, "%s",
5635 cur->audacious.items[AUDACIOUS_TITLE]);
5637 OBJ(audacious_length) {
5638 snprintf(p, p_max_size, "%s",
5639 cur->audacious.items[AUDACIOUS_LENGTH]);
5641 OBJ(audacious_length_seconds) {
5642 snprintf(p, p_max_size, "%s",
5643 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5645 OBJ(audacious_position) {
5646 snprintf(p, p_max_size, "%s",
5647 cur->audacious.items[AUDACIOUS_POSITION]);
5649 OBJ(audacious_position_seconds) {
5650 snprintf(p, p_max_size, "%s",
5651 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
5653 OBJ(audacious_bitrate) {
5654 snprintf(p, p_max_size, "%s",
5655 cur->audacious.items[AUDACIOUS_BITRATE]);
5657 OBJ(audacious_frequency) {
5658 snprintf(p, p_max_size, "%s",
5659 cur->audacious.items[AUDACIOUS_FREQUENCY]);
5661 OBJ(audacious_channels) {
5662 snprintf(p, p_max_size, "%s",
5663 cur->audacious.items[AUDACIOUS_CHANNELS]);
5665 OBJ(audacious_filename) {
5666 snprintf(p, p_max_size, "%s",
5667 cur->audacious.items[AUDACIOUS_FILENAME]);
5669 OBJ(audacious_playlist_length) {
5670 snprintf(p, p_max_size, "%s",
5671 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
5673 OBJ(audacious_playlist_position) {
5674 snprintf(p, p_max_size, "%s",
5675 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
5677 OBJ(audacious_main_volume) {
5678 snprintf(p, p_max_size, "%s",
5679 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
5682 OBJ(audacious_bar) {
5686 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
5687 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
5688 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
5691 #endif /* AUDACIOUS */
5695 snprintf(p, p_max_size, "%s", cur->bmpx.title);
5698 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
5701 snprintf(p, p_max_size, "%s", cur->bmpx.album);
5704 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
5707 snprintf(p, p_max_size, "%i", cur->bmpx.track);
5710 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
5713 /* we have four different types of top (top, top_mem,
5714 * top_time and top_io). To avoid having almost-same code four
5715 * times, we have this special handler. */
5718 parse_top_args("top", obj->data.top.s, obj);
5719 if (!needed) needed = cur->cpu;
5721 parse_top_args("top_mem", obj->data.top.s, obj);
5722 if (!needed) needed = cur->memu;
5724 parse_top_args("top_time", obj->data.top.s, obj);
5725 if (!needed) needed = cur->time;
5728 parse_top_args("top_io", obj->data.top.s, obj);
5729 if (!needed) needed = cur->io;
5732 if (needed[obj->data.top.num]) {
5735 switch (obj->data.top.type) {
5737 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
5738 needed[obj->data.top.num]->name);
5741 snprintf(p, 7, "%6.2f",
5742 needed[obj->data.top.num]->amount);
5745 snprintf(p, 6, "%5i",
5746 needed[obj->data.top.num]->pid);
5749 snprintf(p, 7, "%6.2f",
5750 needed[obj->data.top.num]->totalmem);
5753 timeval = format_time(
5754 needed[obj->data.top.num]->total_cpu_time, 9);
5755 snprintf(p, 10, "%9s", timeval);
5759 human_readable(needed[obj->data.top.num]->rss,
5763 human_readable(needed[obj->data.top.num]->vsize,
5767 case TOP_READ_BYTES:
5768 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
5771 case TOP_WRITE_BYTES:
5772 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
5776 snprintf(p, 7, "%6.2f",
5777 needed[obj->data.top.num]->io_perc);
5783 print_tailhead("tail", obj, p, p_max_size);
5786 print_tailhead("head", obj, p, p_max_size);
5789 FILE *fp = open_file(obj->data.s, &obj->a);
5792 /* FIXME: use something more general (see also tail.c, head.c */
5793 #define BUFSZ 0x1000
5798 while(fgets(buf, BUFSZ, fp) != NULL){
5799 for(j = 0; buf[j] != 0; j++) {
5800 if(buf[j] == '\n') {
5805 sprintf(p, "%d", lines);
5808 sprintf(p, "File Unreadable");
5813 FILE *fp = open_file(obj->data.s, &obj->a);
5818 char inword = FALSE;
5821 while(fgets(buf, BUFSZ, fp) != NULL){
5822 for(j = 0; buf[j] != 0; j++) {
5823 if(!isspace(buf[j])) {
5824 if(inword == FALSE) {
5833 sprintf(p, "%d", words);
5836 sprintf(p, "File Unreadable");
5839 #ifdef TCP_PORT_MONITOR
5841 tcp_portmon_action(p, p_max_size,
5842 &obj->data.tcp_port_monitor);
5844 #endif /* TCP_PORT_MONITOR */
5848 iconv_converting = 1;
5849 iconv_selected = obj->a;
5852 iconv_converting = 0;
5855 #endif /* HAVE_ICONV */
5857 OBJ(entropy_avail) {
5858 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
5861 percent_print(p, p_max_size,
5862 cur->entropy.entropy_avail *
5863 100 / cur->entropy.poolsize);
5865 OBJ(entropy_poolsize) {
5866 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
5869 double entropy_perc;
5871 entropy_perc = (double) cur->entropy.entropy_avail /
5872 (double) cur->entropy.poolsize;
5874 if(output_methods & TO_X) {
5875 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
5878 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
5879 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
5888 s = smapi_get_val(obj->data.s);
5889 snprintf(p, p_max_size, "%s", s);
5893 OBJ(if_smapi_bat_installed) {
5895 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
5896 if(!smapi_bat_installed(idx)) {
5900 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
5902 OBJ(smapi_bat_perc) {
5904 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5905 val = smapi_bat_installed(idx) ?
5906 smapi_get_bat_int(idx, "remaining_percent") : 0;
5907 percent_print(p, p_max_size, val);
5909 NORM_ERR("argument to smapi_bat_perc must be an integer");
5911 OBJ(smapi_bat_temp) {
5913 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5914 val = smapi_bat_installed(idx) ?
5915 smapi_get_bat_int(idx, "temperature") : 0;
5916 /* temperature is in milli degree celsius */
5917 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
5919 NORM_ERR("argument to smapi_bat_temp must be an integer");
5921 OBJ(smapi_bat_power) {
5923 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
5924 val = smapi_bat_installed(idx) ?
5925 smapi_get_bat_int(idx, "power_now") : 0;
5926 /* power_now is in mW, set to W with one digit precision */
5927 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
5929 NORM_ERR("argument to smapi_bat_power must be an integer");
5932 OBJ(smapi_bat_bar) {
5933 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
5934 new_bar(p, obj->a, obj->b, (int)
5935 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
5937 new_bar(p, obj->a, obj->b, 0);
5943 char buf[max_user_text];
5945 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5946 snprintf(p, p_max_size, "%s", buf);
5952 //blinking like this can look a bit ugly if the chars in the font don't have the same width
5953 char buf[max_user_text];
5956 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5957 snprintf(p, p_max_size, "%s", buf);
5958 if(total_updates % 2) {
5959 for(j=0; p[j] != 0; j++) {
5965 char buf[max_user_text];
5967 char unit[16]; // 16 because we can also have long names (like mega-bytes)
5969 generate_text_internal(buf, max_user_text, *obj->sub, cur);
5970 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
5971 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
5972 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
5973 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
5974 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
5975 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
5977 snprintf(p, p_max_size, "%s", buf);
5980 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
5982 char buf[max_user_text];
5983 generate_text_internal(buf, max_user_text,
5985 for(j = 0; buf[j] != 0; j++) {
5987 case '\n': //place all the lines behind each other with LINESEPARATOR between them
5988 #define LINESEPARATOR '|'
5989 buf[j]=LINESEPARATOR;
5996 //no scrolling necessary if the length of the text to scroll is too short
5997 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
5998 snprintf(p, p_max_size, "%s", buf);
6001 //make sure a colorchange at the front is not part of the string we are going to show
6002 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
6003 obj->data.scroll.start++;
6005 //place all chars that should be visible in p, including colorchanges
6006 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
6007 p[j] = *(buf + obj->data.scroll.start + j);
6008 if(p[j] == SPECIAL_CHAR) {
6009 visibcolorchanges++;
6011 //if there is still room fill it with spaces
6014 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
6018 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
6019 for(j = 0; j < obj->data.scroll.start; j++) {
6020 if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
6022 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
6023 for(j = 0; j < frontcolorchanges; j++) {
6024 pwithcolors[j] = SPECIAL_CHAR;
6027 strcat(pwithcolors,p);
6028 strend = strlen(pwithcolors);
6029 //and place the colorchanges not in front or in the visible part behind the visible part
6030 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
6031 pwithcolors[strend + j] = SPECIAL_CHAR;
6033 pwithcolors[strend + j] = 0;
6034 strcpy(p, pwithcolors);
6037 obj->data.scroll.start += obj->data.scroll.step;
6038 if(buf[obj->data.scroll.start] == 0){
6039 obj->data.scroll.start = 0;
6042 //reset color when scroll is finished
6043 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
6047 char buf[2][max_user_text];
6054 struct llrows* next;
6056 struct llrows *ll_rows[2], *current[2];
6057 struct text_object * objsub = obj->sub;
6060 for(i=0; i<2; i++) {
6063 ll_rows[i] = malloc(sizeof(struct llrows));
6064 current[i] = ll_rows[i];
6065 for(j=0; j<i; j++) objsub = objsub->sub;
6066 generate_text_internal(buf[i], max_user_text, *objsub, cur);
6067 for(j=0; buf[i][j] != 0; j++) {
6068 if(buf[i][j] == '\t') buf[i][j] = ' ';
6069 if(buf[i][j] == '\n') {
6071 current[i]->row = strdup(buf[i]+nextstart);
6072 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
6073 current[i]->next = malloc(sizeof(struct llrows));
6074 current[i] = current[i]->next;
6079 current[i]->row = strdup(buf[i]+nextstart);
6080 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
6081 current[i]->next = NULL;
6082 current[i] = ll_rows[i];
6084 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
6086 strcat(p, current[0]->row);
6087 i=strlen(current[0]->row);
6089 while(i < longest) {
6094 strcat(p, obj->data.combine.seperation);
6095 strcat(p, current[1]->row);
6099 #pragma omp parallel for schedule(dynamic,10)
6100 #endif /* HAVE_OPENMP */
6101 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
6104 #pragma omp parallel for schedule(dynamic,10)
6105 #endif /* HAVE_OPENMP */
6106 for(i=0; i<2; i++) {
6107 while(ll_rows[i] != NULL) {
6108 current[i]=ll_rows[i];
6109 free(current[i]->row);
6110 ll_rows[i]=current[i]->next;
6117 int value = get_nvidia_value(obj->data.nvidia.type, display);
6119 snprintf(p, p_max_size, "N/A");
6120 else if (obj->data.nvidia.type == NV_TEMP)
6121 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
6122 else if (obj->data.nvidia.print_as_float &&
6123 value > 0 && value < 100)
6124 snprintf(p, p_max_size, "%.1f", (float)value);
6126 snprintf(p, p_max_size, "%d", value);
6131 /* This is just a meta-object to set host:port */
6134 snprintf(p, p_max_size, "%s",
6135 cur->apcupsd.items[APCUPSD_NAME]);
6137 OBJ(apcupsd_model) {
6138 snprintf(p, p_max_size, "%s",
6139 cur->apcupsd.items[APCUPSD_MODEL]);
6141 OBJ(apcupsd_upsmode) {
6142 snprintf(p, p_max_size, "%s",
6143 cur->apcupsd.items[APCUPSD_UPSMODE]);
6145 OBJ(apcupsd_cable) {
6146 snprintf(p, p_max_size, "%s",
6147 cur->apcupsd.items[APCUPSD_CABLE]);
6149 OBJ(apcupsd_status) {
6150 snprintf(p, p_max_size, "%s",
6151 cur->apcupsd.items[APCUPSD_STATUS]);
6153 OBJ(apcupsd_linev) {
6154 snprintf(p, p_max_size, "%s",
6155 cur->apcupsd.items[APCUPSD_LINEV]);
6158 snprintf(p, p_max_size, "%s",
6159 cur->apcupsd.items[APCUPSD_LOAD]);
6161 OBJ(apcupsd_loadbar) {
6164 if(output_methods & TO_X) {
6165 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
6166 new_bar(p, obj->a, obj->b, (int) progress);
6169 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
6170 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
6171 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
6177 OBJ(apcupsd_loadgraph) {
6179 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
6180 new_graph(p, obj->a, obj->b, obj->c, obj->d,
6181 (int)progress, 100, 1, obj->char_a, obj->char_b);
6183 OBJ(apcupsd_loadgauge) {
6185 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
6186 new_gauge(p, obj->a, obj->b,
6190 OBJ(apcupsd_charge) {
6191 snprintf(p, p_max_size, "%s",
6192 cur->apcupsd.items[APCUPSD_CHARGE]);
6194 OBJ(apcupsd_timeleft) {
6195 snprintf(p, p_max_size, "%s",
6196 cur->apcupsd.items[APCUPSD_TIMELEFT]);
6199 snprintf(p, p_max_size, "%s",
6200 cur->apcupsd.items[APCUPSD_TEMP]);
6202 OBJ(apcupsd_lastxfer) {
6203 snprintf(p, p_max_size, "%s",
6204 cur->apcupsd.items[APCUPSD_LASTXFER]);
6206 #endif /* APCUPSD */
6213 unsigned int a = strlen(p);
6216 if (a > 0 && iconv_converting && iconv_selected > 0
6217 && (iconv_cd[iconv_selected - 1] != (iconv_t) (-1))) {
6219 size_t dummy1, dummy2;
6221 const char *ptr = buff_in;
6223 char *ptr = buff_in;
6227 dummy1 = dummy2 = a;
6229 strncpy(buff_in, p, p_max_size);
6231 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
6232 while (dummy1 > 0) {
6233 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1,
6236 NORM_ERR("Iconv codeset conversion failed");
6241 /* It is nessecary when we are converting from multibyte to
6242 * singlebyte codepage */
6245 #endif /* HAVE_ICONV */
6246 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi) {
6247 substitute_newlines(p, a - 2);
6255 /* load any new fonts we may have had */
6256 if (need_to_load_fonts) {
6262 void evaluate(const char *text, char *buffer)
6264 struct information *tmp_info;
6265 struct text_object subroot;
6267 tmp_info = malloc(sizeof(struct information));
6268 memcpy(tmp_info, &info, sizeof(struct information));
6269 parse_conky_vars(&subroot, text, buffer, tmp_info);
6270 DBGP("evaluated '%s' to '%s'", text, buffer);
6272 free_text_objects(&subroot, 1);
6276 double current_update_time, next_update_time, last_update_time;
6278 static void generate_text(void)
6280 struct information *cur = &info;
6287 current_update_time = get_time();
6291 /* add things to the buffer */
6297 generate_text_internal(p, max_user_text, global_root_object, cur);
6299 if (stuff_in_uppercase) {
6302 tmp_p = text_buffer;
6304 *tmp_p = toupper(*tmp_p);
6309 next_update_time += update_interval;
6310 if (next_update_time < get_time()) {
6311 next_update_time = get_time() + update_interval;
6312 } else if (next_update_time > get_time() + update_interval) {
6313 next_update_time = get_time() + update_interval;
6315 last_update_time = current_update_time;
6319 void set_update_interval(double interval)
6321 update_interval = interval;
6322 update_interval_old = interval;
6325 static inline int get_string_width(const char *s)
6328 if (output_methods & TO_X) {
6329 return *s ? calc_text_width(s, strlen(s)) : 0;
6336 static int get_string_width_special(char *s, int special_index)
6343 if ((output_methods & TO_X) == 0) {
6344 return (s) ? strlen(s) : 0;
6351 p = strndup(s, text_buffer_size);
6355 if (*p == SPECIAL_CHAR) {
6356 /* shift everything over by 1 so that the special char
6357 * doesn't mess up the size calculation */
6358 for (i = 0; i < (long)strlen(p); i++) {
6359 *(p + i) = *(p + i + 1);
6361 if (specials[special_index + idx].type == GRAPH
6362 || specials[special_index + idx].type == GAUGE
6363 || specials[special_index + idx].type == BAR) {
6364 width += specials[special_index + idx].width;
6367 } else if (*p == SECRIT_MULTILINE_CHAR) {
6374 if (strlen(final) > 1) {
6375 width += calc_text_width(final, strlen(final));
6381 static int text_size_updater(char *s, int special_index);
6383 int last_font_height;
6384 static void update_text_area(void)
6388 if ((output_methods & TO_X) == 0)
6390 /* update text size if it isn't fixed */
6395 text_width = minimum_width;
6397 last_font_height = font_height();
6398 for_each_line(text_buffer, text_size_updater);
6400 if (text_height < minimum_height) {
6401 text_height = minimum_height;
6403 if (text_width > maximum_width && maximum_width > 0) {
6404 text_width = maximum_width;
6408 /* get text position on workarea */
6409 switch (text_alignment) {
6416 x = workarea[2] - text_width - gap_x;
6421 x = workarea[2] / 2 - text_width / 2 - gap_x;
6428 y = workarea[3] - text_height - gap_y;
6432 x = workarea[2] - text_width - gap_x;
6433 y = workarea[3] - text_height - gap_y;
6437 x = workarea[2] / 2 - text_width / 2 - gap_x;
6438 y = workarea[3] - text_height - gap_y;
6443 y = workarea[3] / 2 - text_height / 2 - gap_y;
6447 x = workarea[2] - text_width - gap_x;
6448 y = workarea[3] / 2 - text_height / 2 - gap_y;
6452 case NONE: // Let the WM manage the window
6463 if (own_window && !fixed_pos) {
6466 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
6467 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
6468 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
6469 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
6473 /* If window size doesn't match to workarea's size,
6474 * then window probably includes panels (gnome).
6475 * Blah, doesn't work on KDE. */
6476 if (workarea[2] != window.width || workarea[3] != window.height) {
6485 /* update lua window globals */
6486 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
6487 #endif /* HAVE_LUA */
6492 static int cur_x, cur_y; /* current x and y for drawing */
6494 //draw_mode also without X11 because we only need to print to stdout with FG
6495 static int draw_mode; /* FG, BG or OUTLINE */
6497 static long current_color;
6499 static int text_size_updater(char *s, int special_index)
6504 if ((output_methods & TO_X) == 0)
6506 /* get string widths and skip specials */
6509 if (*p == SPECIAL_CHAR) {
6511 w += get_string_width(s);
6514 if (specials[special_index].type == BAR
6515 || specials[special_index].type == GAUGE
6516 || specials[special_index].type == GRAPH) {
6517 w += specials[special_index].width;
6518 if (specials[special_index].height > last_font_height) {
6519 last_font_height = specials[special_index].height;
6520 last_font_height += font_height();
6522 } else if (specials[special_index].type == OFFSET) {
6523 if (specials[special_index].arg > 0) {
6524 w += specials[special_index].arg;
6526 } else if (specials[special_index].type == VOFFSET) {
6527 last_font_height += specials[special_index].arg;
6528 } else if (specials[special_index].type == GOTO) {
6529 if (specials[special_index].arg > cur_x) {
6530 w = (int) specials[special_index].arg;
6532 } else if (specials[special_index].type == TAB) {
6533 int start = specials[special_index].arg;
6534 int step = specials[special_index].width;
6536 if (!step || step < 0) {
6539 w += step - (cur_x - text_start_x - start) % step;
6540 } else if (specials[special_index].type == FONT) {
6541 selected_font = specials[special_index].font_added;
6542 if (font_height() > last_font_height) {
6543 last_font_height = font_height();
6549 } else if (*p == SECRIT_MULTILINE_CHAR) {
6552 lw = get_string_width(s);
6553 *p = SECRIT_MULTILINE_CHAR;
6555 w = lw > w ? lw : w;
6556 text_height += last_font_height;
6560 w += get_string_width(s);
6561 if (w > text_width) {
6564 if (text_width > maximum_width && maximum_width) {
6565 text_width = maximum_width;
6568 text_height += last_font_height;
6569 last_font_height = font_height();
6570 return special_index;
6574 static inline void set_foreground_color(long c)
6577 if (output_methods & TO_X) {
6579 XSetForeground(display, window.gc, c);
6583 if (output_methods & TO_NCURSES) {
6584 attron(COLOR_PAIR(c));
6586 #endif /* NCURSES */
6590 static void draw_string(const char *s)
6592 int i, i2, pos, width_of_s;
6595 char *s_with_newlines;
6601 width_of_s = get_string_width(s);
6602 s_with_newlines = strdup(s);
6603 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
6604 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
6605 s_with_newlines[i] = '\n';
6608 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
6609 printf("%s\n", s_with_newlines);
6610 if (extra_newline) fputc('\n', stdout);
6611 fflush(stdout); /* output immediately, don't buffer */
6613 if ((output_methods & TO_STDERR) && draw_mode == FG) {
6614 fprintf(stderr, "%s\n", s_with_newlines);
6615 fflush(stderr); /* output immediately, don't buffer */
6617 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
6618 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
6620 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
6621 fprintf(append_fpointer, "%s\n", s_with_newlines);
6624 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
6625 printw("%s", s_with_newlines);
6628 free(s_with_newlines);
6629 memset(tmpstring1, 0, text_buffer_size);
6630 memset(tmpstring2, 0, text_buffer_size);
6631 strncpy(tmpstring1, s, text_buffer_size - 1);
6636 if (output_methods & TO_X) {
6637 max = ((text_width - width_of_s) / get_string_width(" "));
6640 /* This code looks for tabs in the text and coverts them to spaces.
6641 * The trick is getting the correct number of spaces, and not going
6642 * over the window's size without forcing the window larger. */
6643 for (i = 0; i < (int) text_buffer_size; i++) {
6644 if (tmpstring1[i] == '\t') {
6646 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
6647 /* guard against overrun */
6648 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
6653 /* guard against overrun */
6654 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
6659 if (output_methods & TO_X) {
6660 if (text_width == maximum_width) {
6661 /* this means the text is probably pushing the limit,
6662 * so we'll chop it */
6663 while (cur_x + get_string_width(tmpstring2) - text_start_x
6664 > maximum_width && strlen(tmpstring2) > 0) {
6665 tmpstring2[strlen(tmpstring2) - 1] = '\0';
6672 if (output_methods & TO_X) {
6678 c.pixel = current_color;
6679 XQueryColor(display, DefaultColormap(display, screen), &c);
6682 c2.color.red = c.red;
6683 c2.color.green = c.green;
6684 c2.color.blue = c.blue;
6685 c2.color.alpha = fonts[selected_font].font_alpha;
6687 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6688 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6690 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
6691 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
6696 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
6699 cur_x += width_of_s;
6702 memcpy(tmpstring1, s, text_buffer_size);
6705 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
6708 int font_h = font_height();
6713 int last_special_needed = -1;
6714 int orig_special_index = special_index;
6717 cur_x = text_start_x;
6718 cur_y += font_ascent();
6722 if (*p == SECRIT_MULTILINE_CHAR) {
6723 /* special newline marker for multiline objects */
6728 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
6733 /* draw string before special, unless we're dealing multiline
6735 if (last_special_applied > -1) {
6736 special_index = last_special_applied;
6744 switch (specials[special_index].type) {
6746 case HORIZONTAL_LINE:
6748 int h = specials[special_index].height;
6749 int mid = font_ascent() / 2;
6751 w = text_start_x + text_width - cur_x;
6753 XSetLineAttributes(display, window.gc, h, LineSolid,
6754 CapButt, JoinMiter);
6755 XDrawLine(display, window.drawable, window.gc, cur_x,
6756 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6762 int h = specials[special_index].height;
6763 int tmp_s = specials[special_index].arg;
6764 int mid = font_ascent() / 2;
6765 char ss[2] = { tmp_s, tmp_s };
6767 w = text_start_x + text_width - cur_x - 1;
6768 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
6769 CapButt, JoinMiter);
6770 XSetDashes(display, window.gc, 0, ss, 2);
6771 XDrawLine(display, window.drawable, window.gc, cur_x,
6772 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
6778 int h, bar_usage, by;
6779 if (cur_x - text_start_x > maximum_width
6780 && maximum_width > 0) {
6783 h = specials[special_index].height;
6784 bar_usage = specials[special_index].arg;
6785 by = cur_y - (font_ascent() / 2) - 1;
6790 w = specials[special_index].width;
6792 w = text_start_x + text_width - cur_x - 1;
6798 XSetLineAttributes(display, window.gc, 1, LineSolid,
6799 CapButt, JoinMiter);
6801 XDrawRectangle(display, window.drawable, window.gc, cur_x,
6803 XFillRectangle(display, window.drawable, window.gc, cur_x,
6804 by, w * bar_usage / 255, h);
6812 case GAUGE: /* new GAUGE */
6815 unsigned long last_colour = current_color;
6817 float angle, px, py;
6821 if (cur_x - text_start_x > maximum_width
6822 && maximum_width > 0) {
6826 h = specials[special_index].height;
6827 by = cur_y - (font_ascent() / 2) - 1;
6832 w = specials[special_index].width;
6834 w = text_start_x + text_width - cur_x - 1;
6840 XSetLineAttributes(display, window.gc, 1, LineSolid,
6841 CapButt, JoinMiter);
6843 XDrawArc(display, window.drawable, window.gc,
6844 cur_x, by, w, h * 2, 0, 180*64);
6847 usage = specials[special_index].arg;
6848 angle = (M_PI)*(float)(usage)/255.;
6849 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
6850 py = (float)(by+(h))-(float)(h)*sin(angle);
6852 XDrawLine(display, window.drawable, window.gc,
6853 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
6861 set_foreground_color(last_colour);
6869 int h, by, i = 0, j = 0;
6871 unsigned long last_colour = current_color;
6872 unsigned long *tmpcolour = 0;
6873 if (cur_x - text_start_x > maximum_width
6874 && maximum_width > 0) {
6877 h = specials[special_index].height;
6878 by = cur_y - (font_ascent() / 2) - 1;
6883 w = specials[special_index].width;
6885 w = text_start_x + text_width - cur_x - 1;
6890 if (draw_graph_borders) {
6891 XSetLineAttributes(display, window.gc, 1, LineSolid,
6892 CapButt, JoinMiter);
6893 XDrawRectangle(display, window.drawable, window.gc,
6896 XSetLineAttributes(display, window.gc, 1, LineSolid,
6897 CapButt, JoinMiter);
6899 if (specials[special_index].last_colour != 0
6900 || specials[special_index].first_colour != 0) {
6901 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
6904 for (i = w - 2; i > -1; i--) {
6905 if (specials[special_index].last_colour != 0
6906 || specials[special_index].first_colour != 0) {
6907 if (specials[special_index].tempgrad) {
6910 (int)((float)(w - 2) - specials[special_index].graph[j] *
6911 (w - 2) / (float)specials[special_index].graph_scale)
6915 (int)((float)(w - 2) - specials[special_index].graph[j] *
6916 (w - 2) / (float)specials[special_index].graph_scale)
6919 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
6921 (int)((float)(w - 2) - specials[special_index].graph[j] *
6922 (w - 2) / (float)specials[special_index].graph_scale)
6926 #endif /* DEBUG_lol */
6927 XSetForeground(display, window.gc, tmpcolour[
6928 (int)((float)(w - 2) - specials[special_index].graph[j] *
6929 (w - 2) / (float)specials[special_index].graph_scale)
6932 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
6935 /* this is mugfugly, but it works */
6936 XDrawLine(display, window.drawable, window.gc,
6937 cur_x + i + 1, by + h, cur_x + i + 1,
6938 round_to_int((double)by + h - specials[special_index].graph[j] *
6939 (h - 1) / specials[special_index].graph_scale));
6940 if ((w - i) / ((float) (w - 2) /
6941 (specials[special_index].graph_width)) > j
6942 && j < MAX_GRAPH_DEPTH - 3) {
6946 if (tmpcolour) free(tmpcolour);
6951 /* if (draw_mode == BG) {
6952 set_foreground_color(default_bg_color);
6953 } else if (draw_mode == OUTLINE) {
6954 set_foreground_color(default_out_color);
6956 set_foreground_color(default_fg_color);
6958 if (show_graph_range) {
6961 unsigned short int seconds = update_interval * w;
6967 unsigned short int timeunits;
6969 timeunits = seconds / 86400; seconds %= 86400;
6970 if (timeunits > 0) {
6971 asprintf(&tmp_day_str, "%dd", timeunits);
6973 tmp_day_str = strdup("");
6975 timeunits = seconds / 3600; seconds %= 3600;
6976 if (timeunits > 0) {
6977 asprintf(&tmp_hour_str, "%dh", timeunits);
6979 tmp_hour_str = strdup("");
6981 timeunits = seconds / 60; seconds %= 60;
6982 if (timeunits > 0) {
6983 asprintf(&tmp_min_str, "%dm", timeunits);
6985 tmp_min_str = strdup("");
6988 asprintf(&tmp_sec_str, "%ds", seconds);
6990 tmp_sec_str = strdup("");
6992 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
6993 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
6995 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
6997 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
6998 cur_y += font_h / 2;
6999 draw_string(tmp_str);
7005 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
7009 cur_x += font_ascent() / 2;
7010 cur_y += font_h / 2;
7012 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
7014 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
7015 draw_string(tmp_str);
7021 set_foreground_color(last_colour);
7027 int old = font_ascent();
7029 cur_y -= font_ascent();
7030 selected_font = specials[special_index].font_added;
7032 if (cur_y + font_ascent() < cur_y + old) {
7035 cur_y += font_ascent();
7037 font_h = font_height();
7042 if (draw_mode == FG) {
7043 set_foreground_color(specials[special_index].arg);
7049 if (draw_mode == BG) {
7050 set_foreground_color(specials[special_index].arg);
7055 if (draw_mode == OUTLINE) {
7056 set_foreground_color(specials[special_index].arg);
7061 w += specials[special_index].arg;
7062 last_special_needed = special_index;
7066 cur_y += specials[special_index].arg;
7070 if (specials[special_index].arg >= 0) {
7071 cur_x = (int) specials[special_index].arg;
7073 last_special_needed = special_index;
7078 int start = specials[special_index].arg;
7079 int step = specials[special_index].width;
7081 if (!step || step < 0) {
7084 w = step - (cur_x - text_start_x - start) % step;
7085 last_special_needed = special_index;
7091 /* TODO: add back in "+ window.border_inner_margin" to the end of
7093 int pos_x = text_start_x + text_width -
7094 get_string_width_special(s, special_index);
7096 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7097 "get_string_width(p) %i gap_x %i "
7098 "specials[special_index].arg %i window.border_inner_margin %i "
7099 "window.border_width %i\n", pos_x, text_start_x, text_width,
7100 cur_x, get_string_width_special(s), gap_x,
7101 specials[special_index].arg, window.border_inner_margin,
7102 window.border_width); */
7103 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
7104 cur_x = pos_x - specials[special_index].arg;
7106 last_special_needed = special_index;
7112 int pos_x = (text_width) / 2 - get_string_width_special(s,
7113 special_index) / 2 - (cur_x -
7115 /* int pos_x = text_start_x + text_width / 2 -
7116 get_string_width_special(s) / 2; */
7118 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
7119 "get_string_width(p) %i gap_x %i "
7120 "specials[special_index].arg %i\n", pos_x, text_start_x,
7121 text_width, cur_x, get_string_width(s), gap_x,
7122 specials[special_index].arg); */
7123 if (pos_x > specials[special_index].arg) {
7124 w = pos_x - specials[special_index].arg;
7126 last_special_needed = special_index;
7136 if (special_index != last_special_applied) {
7139 special_index = orig_special_index;
7140 last_special_applied = -1;
7151 if (output_methods & TO_NCURSES) {
7154 #endif /* NCURSES */
7156 cur_y += font_descent();
7158 if (recurse && *recurse) {
7159 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
7160 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
7162 return special_index;
7165 static int draw_line(char *s, int special_index)
7168 if (output_methods & TO_X) {
7169 return draw_each_line_inner(s, special_index, -1);
7173 if (output_methods & TO_NCURSES) {
7174 return draw_each_line_inner(s, special_index, -1);
7176 #endif /* NCURSES */
7178 //'special_index - special_index' instead of 0 otherwise gcc complains about not using special_index when build without X11
7179 return special_index - special_index;
7182 static void draw_text(void)
7186 llua_draw_pre_hook();
7187 #endif /* HAVE_LUA */
7188 if (output_methods & TO_X) {
7189 cur_y = text_start_y;
7192 if (draw_borders && window.border_width > 0) {
7193 if (stippled_borders) {
7194 char ss[2] = { stippled_borders, stippled_borders };
7195 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
7196 CapButt, JoinMiter);
7197 XSetDashes(display, window.gc, 0, ss, 2);
7199 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
7200 CapButt, JoinMiter);
7203 XDrawRectangle(display, window.drawable, window.gc,
7204 text_start_x - window.border_inner_margin - window.border_width,
7205 text_start_y - window.border_inner_margin - window.border_width,
7206 text_width + window.border_inner_margin * 2 + window.border_width * 2,
7207 text_height + window.border_inner_margin * 2 + window.border_width * 2);
7215 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
7216 attron(COLOR_PAIR(COLOR_WHITE));
7217 #endif /* NCURSES */
7218 for_each_line(text_buffer, draw_line);
7219 #if defined(HAVE_LUA) && defined(X11)
7220 llua_draw_post_hook();
7221 #endif /* HAVE_LUA */
7224 static void draw_stuff(void)
7226 if (overwrite_file) {
7227 overwrite_fpointer = fopen(overwrite_file, "w");
7228 if(!overwrite_fpointer)
7229 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
7232 append_fpointer = fopen(append_file, "a");
7233 if(!append_fpointer)
7234 NORM_ERR("Can't append '%s' anymore", append_file);
7237 if (output_methods & TO_X) {
7239 if (draw_shades && !draw_outline) {
7242 set_foreground_color(default_bg_color);
7253 for (i = -1; i < 2; i++) {
7254 for (j = -1; j < 2; j++) {
7255 if (i == 0 && j == 0) {
7260 set_foreground_color(default_out_color);
7261 draw_mode = OUTLINE;
7269 set_foreground_color(default_fg_color);
7275 if (output_methods & TO_X) {
7280 swap.swap_window = window.window;
7281 swap.swap_action = XdbeBackground;
7282 XdbeSwapBuffers(display, &swap, 1);
7287 if(overwrite_fpointer) {
7288 fclose(overwrite_fpointer);
7289 overwrite_fpointer = 0;
7291 if(append_fpointer) {
7292 fclose(append_fpointer);
7293 append_fpointer = 0;
7298 static void clear_text(int exposures)
7302 /* The swap action is XdbeBackground, which clears */
7306 if (display && window.window) { // make sure these are !null
7307 /* there is some extra space for borders and outlines */
7308 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
7309 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
7310 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
7311 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
7316 static int need_to_update;
7318 /* update_text() generates new text and clears old text area */
7319 static void update_text(void)
7326 if (output_methods & TO_X)
7331 llua_update_info(&info, update_interval);
7332 #endif /* HAVE_LUA */
7335 #ifdef HAVE_SYS_INOTIFY_H
7339 static void main_loop(void)
7342 #ifdef SIGNAL_BLOCKING
7343 sigset_t newmask, oldmask;
7346 #ifdef HAVE_SYS_INOTIFY_H
7347 int inotify_config_wd = -1;
7348 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
7349 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
7350 char inotify_buff[INOTIFY_BUF_LEN];
7351 #endif /* HAVE_SYS_INOTIFY_H */
7354 #ifdef SIGNAL_BLOCKING
7355 sigemptyset(&newmask);
7356 sigaddset(&newmask, SIGINT);
7357 sigaddset(&newmask, SIGTERM);
7358 sigaddset(&newmask, SIGUSR1);
7361 last_update_time = 0.0;
7362 next_update_time = get_time();
7364 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
7365 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
7366 char buf[max_user_text];
7368 get_battery_short_status(buf, max_user_text, "BAT0");
7370 update_interval = update_interval_bat;
7372 update_interval = update_interval_old;
7377 #ifdef SIGNAL_BLOCKING
7378 /* block signals. we will inspect for pending signals later */
7379 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
7380 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7385 if (output_methods & TO_X) {
7388 /* wait for X event or timeout */
7390 if (!XPending(display)) {
7394 t = next_update_time - get_time();
7398 } else if (t > update_interval) {
7399 t = update_interval;
7402 tv.tv_sec = (long) t;
7403 tv.tv_usec = (long) (t * 1000000) % 1000000;
7405 FD_SET(ConnectionNumber(display), &fdsr);
7407 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
7409 if (errno != EINTR) {
7410 NORM_ERR("can't select(): %s", strerror(errno));
7420 if (need_to_update) {
7422 int wx = window.x, wy = window.y;
7432 /* resize window if it isn't right size */
7434 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
7435 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
7436 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7437 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7438 XResizeWindow(display, window.window, window.width,
7440 set_transparent_background(window.window);
7444 /* update lua window globals */
7445 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
7446 #endif /* HAVE_LUA */
7449 /* move window if it isn't in right position */
7450 if (!fixed_pos && (window.x != wx || window.y != wy)) {
7451 XMoveWindow(display, window.window, window.x, window.y);
7456 if (changed && window.type == TYPE_PANEL) {
7459 fprintf(stderr, PACKAGE_NAME": defining struts\n");
7462 switch (text_alignment) {
7489 set_struts(sidenum);
7500 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7501 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7502 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7503 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7504 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7509 /* handle X events */
7510 while (XPending(display)) {
7513 XNextEvent(display, &ev);
7520 r.width = ev.xexpose.width;
7521 r.height = ev.xexpose.height;
7522 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7526 case PropertyNotify:
7528 if ( ev.xproperty.state == PropertyNewValue ) {
7529 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
7535 case ReparentNotify:
7536 /* set background to ParentRelative for all parents */
7538 set_transparent_background(window.window);
7542 case ConfigureNotify:
7544 /* if window size isn't what expected, set fixed size */
7545 if (ev.xconfigure.width != window.width
7546 || ev.xconfigure.height != window.height) {
7547 if (window.width != 0 && window.height != 0) {
7551 /* clear old stuff before screwing up
7556 XWindowAttributes attrs;
7557 if (XGetWindowAttributes(display,
7558 window.window, &attrs)) {
7559 window.width = attrs.width;
7560 window.height = attrs.height;
7564 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7565 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
7566 if (text_width > maximum_width
7567 && maximum_width > 0) {
7568 text_width = maximum_width;
7572 /* if position isn't what expected, set fixed pos
7573 * total_updates avoids setting fixed_pos when window
7574 * is set to weird locations when started */
7575 /* // this is broken
7576 if (total_updates >= 2 && !fixed_pos
7577 && (window.x != ev.xconfigure.x
7578 || window.y != ev.xconfigure.y)
7579 && (ev.xconfigure.x != 0
7580 || ev.xconfigure.y != 0)) {
7588 /* if an ordinary window with decorations */
7589 if ((window.type == TYPE_NORMAL)
7590 && (!TEST_HINT(window.hints,
7591 HINT_UNDECORATED))) {
7592 /* allow conky to hold input focus. */
7595 /* forward the click to the desktop window */
7596 XUngrabPointer(display, ev.xbutton.time);
7597 ev.xbutton.window = window.desktop;
7598 ev.xbutton.x = ev.xbutton.x_root;
7599 ev.xbutton.y = ev.xbutton.y_root;
7600 XSendEvent(display, ev.xbutton.window, False,
7601 ButtonPressMask, &ev);
7602 XSetInputFocus(display, ev.xbutton.window,
7603 RevertToParent, ev.xbutton.time);
7610 /* if an ordinary window with decorations */
7611 if ((window.type == TYPE_NORMAL)
7612 && (!TEST_HINT(window.hints,
7613 HINT_UNDECORATED))) {
7614 /* allow conky to hold input focus. */
7617 /* forward the release to the desktop window */
7618 ev.xbutton.window = window.desktop;
7619 ev.xbutton.x = ev.xbutton.x_root;
7620 ev.xbutton.y = ev.xbutton.y_root;
7621 XSendEvent(display, ev.xbutton.window, False,
7622 ButtonReleaseMask, &ev);
7631 if (ev.type == x11_stuff.event_base + XDamageNotify) {
7632 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
7634 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
7635 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
7637 #endif /* HAVE_XDAMAGE */
7643 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
7644 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
7645 #endif /* HAVE_XDAMAGE */
7647 /* XDBE doesn't seem to provide a way to clear the back buffer without
7648 * interfering with the front buffer, other than passing XdbeBackground
7649 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
7650 * redraw the text even if it wasn't part of the exposed area. OTOH,
7651 * if we're not going to call draw_stuff at all, then no swap happens
7652 * and we can safely do nothing. */
7654 if (!XEmptyRegion(x11_stuff.region)) {
7659 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
7660 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
7661 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7662 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
7663 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
7666 XSetRegion(display, window.gc, x11_stuff.region);
7669 XftDrawSetClip(window.xftdraw, x11_stuff.region);
7673 cimlib_render(text_start_x, text_start_y, window.width, window.height);
7676 XDestroyRegion(x11_stuff.region);
7677 x11_stuff.region = XCreateRegion();
7681 t = (next_update_time - get_time()) * 1000000;
7682 if(t > 0) usleep((useconds_t)t);
7686 if(output_methods & TO_NCURSES) {
7695 #ifdef SIGNAL_BLOCKING
7696 /* unblock signals of interest and let handler fly */
7697 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
7698 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
7702 switch (g_signal_pending) {
7705 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
7710 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
7713 if (output_methods & TO_X) {
7714 XDestroyRegion(x11_stuff.region);
7715 x11_stuff.region = NULL;
7717 XDamageDestroy(display, x11_stuff.damage);
7718 XFixesDestroyRegion(display, x11_stuff.region2);
7719 XFixesDestroyRegion(display, x11_stuff.part);
7720 #endif /* HAVE_XDAMAGE */
7726 if(overwrite_file) {
7727 free(overwrite_file);
7736 /* Reaching here means someone set a signal
7737 * (SIGXXXX, signal_handler), but didn't write any code
7739 * If you don't want to handle a signal, don't set a handler on
7740 * it in the first place. */
7741 if (g_signal_pending) {
7742 NORM_ERR("ignoring signal (%d)", g_signal_pending);
7746 #ifdef HAVE_SYS_INOTIFY_H
7747 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
7748 inotify_config_wd = inotify_add_watch(inotify_fd,
7752 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
7753 int len = 0, idx = 0;
7755 struct timeval time_to_wait;
7757 FD_ZERO(&descriptors);
7758 FD_SET(inotify_fd, &descriptors);
7760 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
7762 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
7763 if (FD_ISSET(inotify_fd, &descriptors)) {
7764 /* process inotify events */
7765 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
7766 while (len > 0 && idx < len) {
7767 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
7768 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
7769 /* current_config should be reloaded */
7770 NORM_ERR("'%s' modified, reloading...", current_config);
7772 if (ev->mask & IN_IGNORED) {
7773 /* for some reason we get IN_IGNORED here
7774 * sometimes, so we need to re-add the watch */
7775 inotify_config_wd = inotify_add_watch(inotify_fd,
7782 llua_inotify_query(ev->wd, ev->mask);
7784 #endif /* HAVE_LUA */
7785 idx += INOTIFY_EVENT_SIZE + ev->len;
7789 #endif /* HAVE_SYS_INOTIFY_H */
7792 llua_update_info(&info, update_interval);
7793 #endif /* HAVE_LUA */
7794 g_signal_pending = 0;
7796 clean_up(NULL, NULL);
7798 #ifdef HAVE_SYS_INOTIFY_H
7799 if (inotify_fd != -1) {
7800 inotify_rm_watch(inotify_fd, inotify_config_wd);
7802 inotify_fd = inotify_config_wd = 0;
7804 #endif /* HAVE_SYS_INOTIFY_H */
7808 static void load_config_file_x11(const char *);
7810 void initialisation(int argc, char** argv);
7812 /* reload the config file */
7813 static void reload_config(void)
7815 char *current_config_copy = strdup(current_config);
7816 clean_up(NULL, NULL);
7817 current_config = current_config_copy;
7818 initialisation(argc_copy, argv_copy);
7821 void clean_up(void *memtofree1, void* memtofree2)
7826 if(output_methods & TO_NCURSES) {
7831 conftree_empty(currentconffile);
7832 currentconffile = NULL;
7839 timed_thread_destroy_registered_threads();
7841 if (info.cpu_usage) {
7842 free(info.cpu_usage);
7843 info.cpu_usage = NULL;
7846 if (x_initialised == YES) {
7849 if(x11_stuff.region) {
7850 XDestroyRegion(x11_stuff.region);
7851 x11_stuff.region = NULL;
7853 XClearWindow(display, RootWindow(display, screen));
7854 XCloseDisplay(display);
7856 if(info.x11.desktop.all_names) {
7857 free(info.x11.desktop.all_names);
7858 info.x11.desktop.all_names = NULL;
7860 if (info.x11.desktop.name) {
7861 free(info.x11.desktop.name);
7862 info.x11.desktop.name = NULL;
7866 free(fonts); //in set_default_configurations a font is set but not loaded
7872 for (i = 0; i < MAX_TEMPLATES; i++) {
7879 free_text_objects(&global_root_object, 0);
7898 free(current_config);
7901 #ifdef TCP_PORT_MONITOR
7902 tcp_portmon_clear();
7911 weather_free_info();
7914 llua_shutdown_hook();
7916 #endif /* HAVE_LUA */
7925 for (i = 0; i < special_count; i++) {
7926 if (specials[i].type == GRAPH) {
7927 free(specials[i].graph);
7935 clear_diskio_stats();
7936 if(global_cpu != NULL) {
7942 static int string_to_bool(const char *s)
7945 // Assumes an option without a true/false means true
7947 } else if (strcasecmp(s, "yes") == EQUAL) {
7949 } else if (strcasecmp(s, "true") == EQUAL) {
7951 } else if (strcasecmp(s, "1") == EQUAL) {
7958 static enum alignment string_to_alignment(const char *s)
7960 if (strcasecmp(s, "top_left") == EQUAL) {
7962 } else if (strcasecmp(s, "top_right") == EQUAL) {
7964 } else if (strcasecmp(s, "top_middle") == EQUAL) {
7966 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
7968 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
7969 return BOTTOM_RIGHT;
7970 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
7971 return BOTTOM_MIDDLE;
7972 } else if (strcasecmp(s, "middle_left") == EQUAL) {
7974 } else if (strcasecmp(s, "middle_right") == EQUAL) {
7975 return MIDDLE_RIGHT;
7976 } else if (strcasecmp(s, "tl") == EQUAL) {
7978 } else if (strcasecmp(s, "tr") == EQUAL) {
7980 } else if (strcasecmp(s, "tm") == EQUAL) {
7982 } else if (strcasecmp(s, "bl") == EQUAL) {
7984 } else if (strcasecmp(s, "br") == EQUAL) {
7985 return BOTTOM_RIGHT;
7986 } else if (strcasecmp(s, "bm") == EQUAL) {
7987 return BOTTOM_MIDDLE;
7988 } else if (strcasecmp(s, "ml") == EQUAL) {
7990 } else if (strcasecmp(s, "mr") == EQUAL) {
7991 return MIDDLE_RIGHT;
7992 } else if (strcasecmp(s, "none") == EQUAL) {
8000 static void set_default_configurations_for_x(void)
8002 default_fg_color = WhitePixel(display, screen);
8003 default_bg_color = BlackPixel(display, screen);
8004 default_out_color = BlackPixel(display, screen);
8005 color0 = default_fg_color;
8006 color1 = default_fg_color;
8007 color2 = default_fg_color;
8008 color3 = default_fg_color;
8009 color4 = default_fg_color;
8010 color5 = default_fg_color;
8011 color6 = default_fg_color;
8012 color7 = default_fg_color;
8013 color8 = default_fg_color;
8014 color9 = default_fg_color;
8015 current_text_color = default_fg_color;
8019 static void set_default_configurations(void)
8027 fork_to_background = 0;
8028 total_run_times = 0;
8029 info.cpu_avg_samples = 2;
8030 info.net_avg_samples = 2;
8031 info.diskio_avg_samples = 2;
8036 format_human_readable = 1;
8043 mpd_env_host = getenv("MPD_HOST");
8044 mpd_env_port = getenv("MPD_PORT");
8046 if (!mpd_env_host || !strlen(mpd_env_host)) {
8047 mpd_set_host("localhost");
8049 /* MPD_HOST environment variable is set */
8050 char *mpd_hostpart = strchr(mpd_env_host, '@');
8051 if (!mpd_hostpart) {
8052 mpd_set_host(mpd_env_host);
8054 /* MPD_HOST contains a password */
8055 char mpd_password[mpd_hostpart - mpd_env_host + 1];
8056 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
8058 if (!strlen(mpd_hostpart + 1)) {
8059 mpd_set_host("localhost");
8061 mpd_set_host(mpd_hostpart + 1);
8064 mpd_set_password(mpd_password, 1);
8069 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
8070 /* failed to set port from environment variable */
8071 mpd_set_port("6600");
8075 info.xmms2.artist = NULL;
8076 info.xmms2.album = NULL;
8077 info.xmms2.title = NULL;
8078 info.xmms2.genre = NULL;
8079 info.xmms2.comment = NULL;
8080 info.xmms2.url = NULL;
8081 info.xmms2.status = NULL;
8082 info.xmms2.playlist = NULL;
8084 use_spacer = NO_SPACER;
8086 output_methods = TO_X;
8088 output_methods = TO_STDOUT;
8091 show_graph_scale = 0;
8092 show_graph_range = 0;
8095 draw_graph_borders = 1;
8097 set_first_font("6x10");
8105 window.type = TYPE_NORMAL;
8107 strcpy(window.class_name, PACKAGE_NAME);
8108 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
8110 stippled_borders = 0;
8111 window.border_inner_margin = 3;
8112 window.border_outer_margin = 1;
8113 window.border_width = 1;
8114 text_alignment = BOTTOM_LEFT;
8115 info.x11.monitor.number = 1;
8116 info.x11.monitor.current = 0;
8117 info.x11.desktop.current = 1;
8118 info.x11.desktop.number = 1;
8119 info.x11.desktop.nitems = 0;
8120 info.x11.desktop.all_names = NULL;
8121 info.x11.desktop.name = NULL;
8124 for (i = 0; i < MAX_TEMPLATES; i++) {
8127 template[i] = strdup("");
8130 free(current_mail_spool);
8134 variable_substitute(MAIL_FILE, buf, 256);
8135 if (buf[0] != '\0') {
8136 current_mail_spool = strndup(buf, text_buffer_size);
8141 set_update_interval(3);
8142 update_interval_bat = NOBATTERY;
8143 info.music_player_interval = 1.0;
8144 stuff_in_uppercase = 0;
8145 info.users.number = 1;
8147 #ifdef TCP_PORT_MONITOR
8148 /* set default connection limit */
8149 tcp_portmon_set_max_connections(0);
8153 /* returns 1 if you can overwrite or create the file at 'path' */
8154 static _Bool overwrite_works(const char *path)
8158 if (!(filepointer = fopen(path, "w")))
8160 fclose(filepointer);
8164 /* returns 1 if you can append or create the file at 'path' */
8165 static _Bool append_works(const char *path)
8169 if (!(filepointer = fopen(path, "a")))
8171 fclose(filepointer);
8177 /* WARNING, this type not in Xlib spec */
8178 int x11_error_handler(Display *d, XErrorEvent *err)
8179 __attribute__((noreturn));
8180 int x11_error_handler(Display *d, XErrorEvent *err)
8182 NORM_ERR("X Error: type %i Display %lx XID %li serial %lu error_code %i request_code %i minor_code %i other Display: %lx\n",
8184 (long unsigned)err->display,
8185 (long)err->resourceid,
8195 int x11_ioerror_handler(Display *d)
8196 __attribute__((noreturn));
8197 int x11_ioerror_handler(Display *d)
8199 NORM_ERR("X Error: Display %lx\n",
8206 static void X11_initialisation(void)
8208 if (x_initialised == YES) return;
8209 output_methods |= TO_X;
8211 set_default_configurations_for_x();
8212 x_initialised = YES;
8215 /* WARNING, this type not in Xlib spec */
8216 XSetErrorHandler(&x11_error_handler);
8217 XSetIOErrorHandler(&x11_ioerror_handler);
8221 static char **xargv = 0;
8222 static int xargc = 0;
8224 static void X11_create_window(void)
8226 if (output_methods & TO_X) {
8228 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
8229 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
8231 #else /* OWN_WINDOW */
8232 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
8233 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
8235 #endif /* OWN_WINDOW */
8239 update_text_area(); /* to position text/window on screen */
8242 if (own_window && !fixed_pos) {
8243 XMoveWindow(display, window.window, window.x, window.y);
8246 set_transparent_background(window.window);
8254 x11_stuff.region = XCreateRegion();
8256 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
8257 NORM_ERR("Xdamage extension unavailable");
8259 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
8260 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
8261 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
8262 #endif /* HAVE_XDAMAGE */
8265 update_text_area(); /* to get initial size of the window */
8268 /* setup lua window globals */
8269 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
8270 #endif /* HAVE_LUA */
8274 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
8275 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
8276 #define CONF2(a) if (strcasecmp(name, a) == 0)
8277 #define CONF(a) else CONF2(a)
8278 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
8279 || strcasecmp(name, b) == 0)
8280 #define CONF_CONTINUE 1
8281 #define CONF_BREAK 2
8282 #define CONF_BUFF_SIZE 512
8284 static FILE *open_config_file(const char *f)
8286 #ifdef CONFIG_OUTPUT
8287 if (!strcmp(f, "==builtin==")) {
8288 return conf_cookie_open();
8290 #endif /* CONFIG_OUTPUT */
8291 return fopen(f, "r");
8294 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
8298 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8301 remove_comments(buf);
8306 while (*p && isspace((int) *p)) {
8310 return CONF_CONTINUE; /* empty line */
8317 while (*p2 && !isspace((int) *p2)) {
8321 *p2 = '\0'; /* break at name's end */
8328 while (*p && isspace((int) *p)) {
8334 p2 = *value + strlen(*value);
8335 while (isspace((int) *(p2 - 1))) {
8344 char load_config_file(const char *f)
8349 fp = open_config_file(f);
8353 DBGP("reading contents from config file '%s'", f);
8356 char buff[CONF_BUFF_SIZE], *name, *value;
8357 int ret = do_config_step(&line, fp, buff, &name, &value);
8358 if (ret == CONF_BREAK) {
8360 } else if (ret == CONF_CONTINUE) {
8366 /* don't listen if X is already initialised or
8367 * if we already know we don't want it */
8368 if(x_initialised != YES) {
8369 if (string_to_bool(value)) {
8370 output_methods &= TO_X;
8372 output_methods &= ~TO_X;
8373 x_initialised = NEVER;
8378 if (!value || x_initialised == YES) {
8383 disp = strdup(value);
8388 if (window.type == TYPE_DOCK)
8391 #endif /*OWN_WINDOW */
8393 int a = string_to_alignment(value);
8404 CONF("background") {
8405 fork_to_background = string_to_bool(value);
8408 CONF2("background") {
8409 fork_to_background = string_to_bool(value);
8413 CONF("show_graph_scale") {
8414 show_graph_scale = string_to_bool(value);
8416 CONF("show_graph_range") {
8417 show_graph_range = string_to_bool(value);
8419 CONF("border_inner_margin") {
8421 window.border_inner_margin = strtol(value, 0, 0);
8422 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
8427 CONF("border_outer_margin") {
8429 window.border_outer_margin = strtol(value, 0, 0);
8430 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
8435 CONF("border_width") {
8437 window.border_width = strtol(value, 0, 0);
8438 if (window.border_width < 0) window.border_width = 0;
8444 #define TEMPLATE_CONF(n) \
8445 CONF("template"#n) { \
8447 free(template[n]); \
8448 template[n] = strdup(value); \
8465 info.mail = parse_mail_args(IMAP_TYPE, value);
8472 info.mail = parse_mail_args(POP3_TYPE, value);
8477 CONF("default_bar_size") {
8480 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
8487 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
8491 CONF("default_graph_size") {
8494 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
8501 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
8504 CONF("default_gauge_size") {
8507 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
8514 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
8521 mpd_set_host(value);
8527 if (value && mpd_set_port(value)) {
8531 CONF("mpd_password") {
8533 mpd_set_password(value, 0);
8539 CONF("music_player_interval") {
8541 info.music_player_interval = strtod(value, 0);
8547 CONF("sensor_device") {
8549 sensor_device = strtol(value, 0, 0);
8555 CONF("cpu_avg_samples") {
8557 cpu_avg_samples = strtol(value, 0, 0);
8558 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
8561 info.cpu_avg_samples = cpu_avg_samples;
8567 CONF("net_avg_samples") {
8569 net_avg_samples = strtol(value, 0, 0);
8570 if (net_avg_samples < 1 || net_avg_samples > 14) {
8573 info.net_avg_samples = net_avg_samples;
8579 CONF("diskio_avg_samples") {
8581 diskio_avg_samples = strtol(value, 0, 0);
8582 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
8585 info.diskio_avg_samples = diskio_avg_samples;
8593 CONF("double_buffer") {
8594 use_xdbe = string_to_bool(value);
8598 CONF("override_utf8_locale") {
8599 utf8_mode = string_to_bool(value);
8601 CONF("draw_borders") {
8602 draw_borders = string_to_bool(value);
8604 CONF("draw_graph_borders") {
8605 draw_graph_borders = string_to_bool(value);
8607 CONF("draw_shades") {
8608 draw_shades = string_to_bool(value);
8610 CONF("draw_outline") {
8611 draw_outline = string_to_bool(value);
8614 CONF("out_to_console") {
8615 if(string_to_bool(value)) {
8616 output_methods |= TO_STDOUT;
8618 output_methods &= ~TO_STDOUT;
8621 CONF("extra_newline") {
8622 extra_newline = string_to_bool(value);
8624 CONF("out_to_stderr") {
8625 if(string_to_bool(value))
8626 output_methods |= TO_STDERR;
8629 CONF("out_to_ncurses") {
8630 if(string_to_bool(value)) {
8633 output_methods |= TO_NCURSES;
8637 CONF("overwrite_file") {
8638 if(overwrite_file) {
8639 free(overwrite_file);
8642 if(overwrite_works(value)) {
8643 overwrite_file = strdup(value);
8644 output_methods |= OVERWRITE_FILE;
8646 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
8648 CONF("append_file") {
8653 if(append_works(value)) {
8654 append_file = strdup(value);
8655 output_methods |= APPEND_FILE;
8657 NORM_ERR("append_file won't be able to create/append '%s'", value);
8659 CONF("use_spacer") {
8661 if (strcasecmp(value, "left") == EQUAL) {
8662 use_spacer = LEFT_SPACER;
8663 } else if (strcasecmp(value, "right") == EQUAL) {
8664 use_spacer = RIGHT_SPACER;
8665 } else if (strcasecmp(value, "none") == EQUAL) {
8666 use_spacer = NO_SPACER;
8668 use_spacer = string_to_bool(value);
8669 NORM_ERR("use_spacer should have an argument of left, right, or"
8670 " none. '%s' seems to be some form of '%s', so"
8671 " defaulting to %s.", value,
8672 use_spacer ? "true" : "false",
8673 use_spacer ? "right" : "none");
8675 use_spacer = RIGHT_SPACER;
8677 use_spacer = NO_SPACER;
8681 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
8682 use_spacer = RIGHT_SPACER;
8688 use_xft = string_to_bool(value);
8692 set_first_font(value);
8696 if (value && font_count >= 0) {
8697 fonts[0].font_alpha = atof(value) * 65535.0;
8704 if (string_to_bool(value)) {
8705 NORM_ERR("Xft not enabled at compile time");
8709 /* xftfont silently ignored when no Xft */
8712 /* xftalpha is silently ignored when no Xft */
8717 set_first_font(value);
8725 gap_x = atoi(value);
8732 gap_y = atoi(value);
8738 CONF("mail_spool") {
8742 variable_substitute(value, buffer, 256);
8744 if (buffer[0] != '\0') {
8745 if (current_mail_spool) {
8746 free(current_mail_spool);
8748 current_mail_spool = strndup(buffer, text_buffer_size);
8755 CONF("minimum_size") {
8757 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
8759 if (sscanf(value, "%d", &minimum_width) != 1) {
8767 CONF("maximum_width") {
8769 if (sscanf(value, "%d", &maximum_width) != 1) {
8777 CONF("no_buffers") {
8778 no_buffers = string_to_bool(value);
8780 CONF("top_name_width") {
8782 if (sscanf(value, "%u", &top_name_width) != 1) {
8788 if (top_name_width >= max_user_text) {
8789 top_name_width = max_user_text - 1;
8792 CONF("top_cpu_separate") {
8793 cpu_separate = string_to_bool(value);
8795 CONF("short_units") {
8796 short_units = string_to_bool(value);
8798 CONF("format_human_readable") {
8799 format_human_readable = string_to_bool(value);
8801 CONF("pad_percents") {
8802 pad_percents = atoi(value);
8806 CONF("own_window") {
8808 own_window = string_to_bool(value);
8811 CONF("own_window_class") {
8813 memset(window.class_name, 0, sizeof(window.class_name));
8814 strncpy(window.class_name, value,
8815 sizeof(window.class_name) - 1);
8818 CONF("own_window_title") {
8820 memset(window.title, 0, sizeof(window.title));
8821 strncpy(window.title, value, sizeof(window.title) - 1);
8824 CONF("own_window_transparent") {
8826 set_transparent = string_to_bool(value);
8829 CONF("own_window_hints") {
8831 char *p_hint, *p_save;
8832 char delim[] = ", ";
8834 /* tokenize the value into individual hints */
8835 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
8837 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
8838 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
8839 SET_HINT(window.hints, HINT_UNDECORATED);
8840 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
8841 SET_HINT(window.hints, HINT_BELOW);
8842 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
8843 SET_HINT(window.hints, HINT_ABOVE);
8844 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
8845 SET_HINT(window.hints, HINT_STICKY);
8846 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
8847 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
8848 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
8849 SET_HINT(window.hints, HINT_SKIP_PAGER);
8854 p_hint = strtok_r(NULL, delim, &p_save);
8855 } while (p_hint != NULL);
8861 CONF("own_window_type") {
8863 if (strncmp(value, "normal", 6) == EQUAL) {
8864 window.type = TYPE_NORMAL;
8865 } else if (strncmp(value, "desktop", 7) == EQUAL) {
8866 window.type = TYPE_DESKTOP;
8867 } else if (strncmp(value, "dock", 4) == EQUAL) {
8868 window.type = TYPE_DOCK;
8869 text_alignment = TOP_LEFT;
8870 } else if (strncmp(value, "panel", 5) == EQUAL) {
8871 window.type = TYPE_PANEL;
8872 } else if (strncmp(value, "override", 8) == EQUAL) {
8873 window.type = TYPE_OVERRIDE;
8882 CONF("stippled_borders") {
8884 stippled_borders = strtol(value, 0, 0);
8886 stippled_borders = 4;
8890 CONF("imlib_cache_size") {
8892 cimlib_set_cache_size(atoi(value));
8895 CONF("imlib_cache_flush_interval") {
8897 cimlib_set_cache_flush_interval(atoi(value));
8902 CONF("update_interval_on_battery") {
8904 update_interval_bat = strtod(value, 0);
8909 CONF("update_interval") {
8911 set_update_interval(strtod(value, 0));
8915 if (info.music_player_interval == 0) {
8916 // default to update_interval
8917 info.music_player_interval = update_interval;
8920 CONF("total_run_times") {
8922 total_run_times = strtod(value, 0);
8928 stuff_in_uppercase = string_to_bool(value);
8930 CONF("max_specials") {
8932 max_specials = atoi(value);
8937 CONF("max_user_text") {
8939 max_user_text = atoi(value);
8944 CONF("text_buffer_size") {
8946 text_buffer_size = atoi(value);
8947 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
8948 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
8949 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
8957 if (output_methods & TO_X) {
8958 X11_initialisation();
8967 global_text = (char *) malloc(1);
8968 global_text[0] = '\0';
8971 unsigned int l = strlen(global_text);
8973 char buf[CONF_BUFF_SIZE];
8975 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
8981 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
8989 /* Check for continuation of \\-\n. */
8990 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
8991 global_text[l-1] = '\0';
8995 global_text = (char *) realloc(global_text, l + bl + 1);
8996 strcat(global_text, buf);
8998 if (strlen(global_text) > max_user_text) {
9003 if (strlen(global_text) < 1) {
9004 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
9006 global_text_lines = line + 1;
9009 #ifdef TCP_PORT_MONITOR
9010 CONF("max_port_monitor_connections") {
9012 if (!value || (sscanf(value, "%d", &max) != 1)) {
9013 /* an error. use default, warn and continue. */
9014 tcp_portmon_set_max_connections(0);
9016 } else if (tcp_portmon_set_max_connections(max)) {
9017 /* max is < 0, default has been set*/
9022 CONF("if_up_strictness") {
9024 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
9025 ifup_strictness = IFUP_UP;
9026 } else if (strcasecmp(value, "up") == EQUAL) {
9027 ifup_strictness = IFUP_UP;
9028 } else if (strcasecmp(value, "link") == EQUAL) {
9029 ifup_strictness = IFUP_LINK;
9030 } else if (strcasecmp(value, "address") == EQUAL) {
9031 ifup_strictness = IFUP_ADDR;
9033 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
9034 ifup_strictness = IFUP_UP;
9038 CONF("temperature_unit") {
9040 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
9041 } else if (set_temp_output_unit(value)) {
9042 NORM_ERR("temperature_unit: incorrect argument");
9049 char *ptr = strtok(value, " ");
9052 ptr = strtok(NULL, " ");
9059 CONF("lua_draw_hook_pre") {
9061 llua_set_draw_pre_hook(value);
9066 CONF("lua_draw_hook_post") {
9068 llua_set_draw_post_hook(value);
9073 CONF("lua_startup_hook") {
9075 llua_set_startup_hook(value);
9080 CONF("lua_shutdown_hook") {
9082 llua_set_shutdown_hook(value);
9088 #endif /* HAVE_LUA */
9100 CONF("default_color"){}
9101 CONF3("default_shade_color", "default_shadecolor"){}
9102 CONF3("default_outline_color", "default_outlinecolor") {}
9103 CONF("own_window_colour") {}
9106 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
9112 if (info.music_player_interval == 0) {
9113 // default to update_interval
9114 info.music_player_interval = update_interval;
9116 if (!global_text) { // didn't supply any text
9117 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
9123 static void load_config_file_x11(const char *f)
9128 fp = open_config_file(f);
9132 DBGP("reading contents from config file '%s'", f);
9135 char buff[CONF_BUFF_SIZE], *name, *value;
9136 int ret = do_config_step(&line, fp, buff, &name, &value);
9137 if (ret == CONF_BREAK) {
9139 } else if (ret == CONF_CONTINUE) {
9144 X11_initialisation();
9145 if (x_initialised == YES) {
9147 color0 = get_x11_color(value);
9154 X11_initialisation();
9155 if (x_initialised == YES) {
9157 color1 = get_x11_color(value);
9164 X11_initialisation();
9165 if (x_initialised == YES) {
9167 color2 = get_x11_color(value);
9174 X11_initialisation();
9175 if (x_initialised == YES) {
9177 color3 = get_x11_color(value);
9184 X11_initialisation();
9185 if (x_initialised == YES) {
9187 color4 = get_x11_color(value);
9194 X11_initialisation();
9195 if (x_initialised == YES) {
9197 color5 = get_x11_color(value);
9204 X11_initialisation();
9205 if (x_initialised == YES) {
9207 color6 = get_x11_color(value);
9214 X11_initialisation();
9215 if (x_initialised == YES) {
9217 color7 = get_x11_color(value);
9224 X11_initialisation();
9225 if (x_initialised == YES) {
9227 color8 = get_x11_color(value);
9234 X11_initialisation();
9235 if (x_initialised == YES) {
9237 color9 = get_x11_color(value);
9243 CONF("default_color") {
9244 X11_initialisation();
9245 if (x_initialised == YES) {
9247 default_fg_color = get_x11_color(value);
9253 CONF3("default_shade_color", "default_shadecolor") {
9254 X11_initialisation();
9255 if (x_initialised == YES) {
9257 default_bg_color = get_x11_color(value);
9263 CONF3("default_outline_color", "default_outlinecolor") {
9264 X11_initialisation();
9265 if (x_initialised == YES) {
9267 default_out_color = get_x11_color(value);
9274 CONF("own_window_colour") {
9275 X11_initialisation();
9276 if (x_initialised == YES) {
9278 background_colour = get_x11_color(value);
9280 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
9281 "'#' for hex colours");
9287 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
9288 if(output_methods & TO_X) {
9289 X11_initialisation();
9298 #undef CONF_CONTINUE
9299 #undef CONF_BUFF_SIZE
9307 static void print_help(const char *prog_name) {
9308 printf("Usage: %s [OPTION]...\n"
9309 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
9310 "window. Command line options will override configurations defined in config\n"
9312 " -v, --version version\n"
9313 " -q, --quiet quiet mode\n"
9314 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
9315 " -c, --config=FILE config file to load\n"
9316 #ifdef CONFIG_OUTPUT
9317 " -C, --print-config print the builtin default config to stdout\n"
9318 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
9320 " -d, --daemonize daemonize, fork to background\n"
9321 " -h, --help help\n"
9323 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
9324 " -f, --font=FONT font to use\n"
9325 " -X, --display=DISPLAY X11 display to use\n"
9327 " -o, --own-window create own window to draw\n"
9330 " -b, --double-buffer double buffer (prevents flickering)\n"
9332 " -w, --window-id=WIN_ID window id to draw\n"
9333 " -x X x position\n"
9334 " -y Y y position\n"
9336 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
9337 " -u, --interval=SECS update interval\n"
9338 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
9343 /* : means that character before that takes an argument */
9344 static const char *getopt_string = "vVqdDt:u:i:hc:"
9354 #ifdef CONFIG_OUTPUT
9359 static const struct option longopts[] = {
9360 { "help", 0, NULL, 'h' },
9361 { "version", 0, NULL, 'V' },
9362 { "debug", 0, NULL, 'D' },
9363 { "config", 1, NULL, 'c' },
9364 #ifdef CONFIG_OUTPUT
9365 { "print-config", 0, NULL, 'C' },
9367 { "daemonize", 0, NULL, 'd' },
9369 { "alignment", 1, NULL, 'a' },
9370 { "font", 1, NULL, 'f' },
9371 { "display", 1, NULL, 'X' },
9373 { "own-window", 0, NULL, 'o' },
9376 { "double-buffer", 0, NULL, 'b' },
9378 { "window-id", 1, NULL, 'w' },
9380 { "text", 1, NULL, 't' },
9381 { "interval", 0, NULL, 'u' },
9385 void initialisation(int argc, char **argv) {
9386 struct sigaction act, oact;
9388 set_default_configurations();
9389 load_config_file(current_config);
9390 currentconffile = conftree_add(currentconffile, current_config);
9392 /* init specials array */
9393 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
9394 NORM_ERR("failed to create specials array");
9398 if (current_mail_spool == NULL) {
9401 variable_substitute(MAIL_FILE, buf, 256);
9403 if (buf[0] != '\0') {
9404 current_mail_spool = strndup(buf, text_buffer_size);
9409 /* handle other command line arguments */
9411 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
9412 || defined(__NetBSD__)
9413 optind = optreset = 1;
9418 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9419 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
9420 "kvm_open")) == NULL) {
9421 CRIT_ERR(NULL, NULL, "cannot read kvm");
9426 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9434 fork_to_background = 1;
9437 global_debug_level++;
9441 set_first_font(optarg);
9444 text_alignment = string_to_alignment(optarg);
9463 global_text = strndup(optarg, max_user_text);
9464 convert_escapes(global_text);
9468 update_interval = strtod(optarg, 0);
9469 update_interval_old = update_interval;
9470 if (info.music_player_interval == 0) {
9471 // default to update_interval
9472 info.music_player_interval = update_interval;
9477 total_run_times = strtod(optarg, 0);
9481 gap_x = atoi(optarg);
9485 gap_y = atoi(optarg);
9496 if (output_methods & TO_X) {
9497 load_config_file_x11(current_config);
9501 /* generate text and get initial size */
9502 extract_variable_text(global_text);
9509 if (fork_to_background) {
9514 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
9521 fprintf(stderr, "\n");
9526 /* parent process */
9527 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
9534 text_buffer = malloc(max_user_text);
9535 memset(text_buffer, 0, max_user_text);
9536 tmpstring1 = malloc(text_buffer_size);
9537 memset(tmpstring1, 0, text_buffer_size);
9538 tmpstring2 = malloc(text_buffer_size);
9539 memset(tmpstring2, 0, text_buffer_size);
9544 X11_create_window();
9547 llua_setup_info(&info, update_interval);
9548 #endif /* HAVE_LUA */
9553 /* Set signal handlers */
9554 act.sa_handler = signal_handler;
9555 sigemptyset(&act.sa_mask);
9558 act.sa_flags |= SA_RESTART;
9561 if ( sigaction(SIGINT, &act, &oact) < 0
9562 || sigaction(SIGALRM, &act, &oact) < 0
9563 || sigaction(SIGUSR1, &act, &oact) < 0
9564 || sigaction(SIGHUP, &act, &oact) < 0
9565 || sigaction(SIGTERM, &act, &oact) < 0) {
9566 NORM_ERR("error setting signal handler: %s", strerror(errno));
9570 llua_startup_hook();
9571 #endif /* HAVE_LUA */
9574 int main(int argc, char **argv)
9583 g_signal_pending = 0;
9584 max_user_text = MAX_USER_TEXT_DEFAULT;
9586 memset(&info, 0, sizeof(info));
9587 memset(template, 0, sizeof(template));
9590 #ifdef TCP_PORT_MONITOR
9591 /* set default connection limit */
9592 tcp_portmon_set_max_connections(0);
9595 /* handle command line parameters that don't change configs */
9597 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
9598 || ((s = getenv("LANG")) && *s)) {
9599 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
9601 NORM_ERR("malloc failed");
9603 for (x = 0; x < strlen(s); x++) {
9604 temp[x] = tolower(s[x]);
9607 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
9613 if (!setlocale(LC_CTYPE, "")) {
9614 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
9618 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
9629 if (current_config) {
9630 free(current_config);
9632 current_config = strndup(optarg, max_user_text);
9635 freopen("/dev/null", "w", stderr);
9638 print_help(argv[0]);
9640 #ifdef CONFIG_OUTPUT
9647 window.window = strtol(optarg, 0, 0);
9652 disp = strdup(optarg);
9661 /* check if specified config file is valid */
9662 if (current_config) {
9664 if (stat(current_config, &sb) ||
9665 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
9666 NORM_ERR("invalid configuration file '%s'\n", current_config);
9667 free(current_config);
9672 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
9674 if (!current_config) {
9675 /* load default config file */
9676 char buf[DEFAULT_TEXT_BUFFER_SIZE];
9679 /* Try to use personal config file first */
9680 to_real_path(buf, CONFIG_FILE);
9681 if (buf[0] && (fp = fopen(buf, "r"))) {
9682 current_config = strndup(buf, max_user_text);
9686 /* Try to use system config file if personal config not readable */
9687 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
9688 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
9692 /* No readable config found */
9693 if (!current_config) {
9694 #ifdef CONFIG_OUTPUT
9695 current_config = strdup("==builtin==");
9696 NORM_ERR("no readable personal or system-wide config file found,"
9697 " using builtin default");
9699 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
9700 #endif /* ! CONF_OUTPUT */
9705 /* Load xoap keys, if existing */
9709 #ifdef HAVE_SYS_INOTIFY_H
9710 inotify_fd = inotify_init();
9711 #endif /* HAVE_SYS_INOTIFY_H */
9713 initialisation(argc, argv);
9717 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
9725 void alarm_handler(void) {
9727 kill(childpid, SIGTERM);
9731 static void signal_handler(int sig)
9733 /* signal handler is light as a feather, as it should be.
9734 * we will poll g_signal_pending with each loop of conky
9735 * and do any signal processing there, NOT here (except
9736 * SIGALRM because this is caused when conky is hanging) */
9737 if(sig == SIGALRM) {
9740 g_signal_pending = sig;