1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "text_object.h"
46 #include <sys/param.h>
47 #ifdef HAVE_SYS_INOTIFY_H
48 #include <sys/inotify.h>
49 #endif /* HAVE_SYS_INOTIFY_H */
52 #include <X11/Xutil.h>
54 #include <X11/extensions/Xdamage.h>
60 #include <sys/types.h>
62 #include <netinet/in.h>
70 #include <libxml/parser.h>
88 #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);
116 #include "defconfig.h"
117 #include "conf_cookie.h"
121 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
124 #define MAIL_FILE "$MAIL"
125 #define MAX_IF_BLOCK_DEPTH 5
127 //#define SIGNAL_BLOCKING
128 #undef SIGNAL_BLOCKING
130 /* debugging level, used by logging.h */
131 int global_debug_level = 0;
133 /* two strings for internal use */
134 static char *tmpstring1, *tmpstring2;
136 /* variables holding various config settings */
138 int format_human_readable;
145 int top_cpu, top_mem, top_time;
152 static unsigned int top_name_width = 15;
154 static int extra_newline;
155 enum x_initialiser_state x_initialised = NO;
156 static volatile int g_signal_pending;
157 /* Update interval */
158 double update_interval;
159 double update_interval_old;
160 double update_interval_bat;
161 void *global_cpu = NULL;
167 /* prototypes for internally used functions */
168 static void signal_handler(int);
169 static void print_version(void) __attribute__((noreturn));
170 static void reload_config(void);
171 static void generate_text_internal(char *, int, struct text_object,
172 struct information *);
174 static void print_version(void)
176 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
178 printf("\nCompiled in features:\n\n"
179 "System config file: "SYSTEM_CONFIG_FILE"\n"
180 "Package library path: "PACKAGE_LIBDIR"\n\n"
184 " * Xdamage extension\n"
185 # endif /* HAVE_XDAMAGE */
187 " * XDBE (double buffer extension)\n"
188 # endif /* HAVE_XDBE */
193 "\n Music detection:\n"
196 #endif /* AUDACIOUS */
212 #endif /* HAVE_OPENMP */
219 #ifdef TCP_PORT_MONITOR
221 #endif /* TCP_PORT_MONITOR */
224 #endif /* HAVE_CURL */
229 " * Weather (METAR)\n"
231 " * Weather (XOAP)\n"
236 #endif /* HAVE_IWLIB */
238 " * support for IBM/Lenovo notebooks\n"
248 #endif /* CONFIG_OUTPUT */
253 " * ALSA mixer support\n"
254 #endif /* MIXER_IS_ALSA */
264 #ifdef HAVE_LUA_CAIRO
266 #endif /* HAVE_LUA_CAIRO */
267 #ifdef HAVE_LUA_IMLIB2
270 #endif /* HAVE_LUA */
276 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
281 static void X11_create_window(void);
282 static void X11_initialisation(void);
284 struct _x11_stuff_s {
288 XserverRegion region2, part;
289 int event_base, error_base;
295 static int text_start_x, text_start_y; /* text start position in window */
296 static int text_width, text_height;
311 /* display to connect to */
312 static char *disp = NULL;
316 /* struct that has all info to be shared between
317 * instances of the same text object */
318 struct information info;
320 /* path to config file */
321 char *current_config;
323 /* set to 1 if you want all text to be in uppercase */
324 static unsigned int stuff_in_uppercase;
326 /* Run how many times? */
327 static unsigned long total_run_times;
330 static int fork_to_background;
332 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
334 /* filenames for output */
335 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
336 char *append_file = NULL; FILE *append_fpointer = NULL;
340 static int show_graph_scale;
341 static int show_graph_range;
343 /* Position on the screen */
344 static int text_alignment;
345 static int gap_x, gap_y;
348 static int draw_borders;
349 static int draw_graph_borders;
350 static int stippled_borders;
352 int get_stippled_borders(void)
354 return stippled_borders;
357 static int draw_shades, draw_outline;
359 long default_fg_color, default_bg_color, default_out_color;
361 /* create own window or draw stuff to root? */
362 static int set_transparent = 0;
365 static int own_window = 0;
366 static int background_colour = 0;
368 /* fixed size/pos is set if wm/user changes them */
369 static int fixed_size = 0, fixed_pos = 0;
372 static int minimum_width, minimum_height;
373 static int maximum_width;
378 static int sensor_device;
381 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
384 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
385 unsigned int max_user_text;
387 /* maximum size of individual text buffers, ie $exec buffer size */
388 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
393 /* no buffers in used memory? */
396 /* pad percentages to decimals? */
397 static int pad_percents = 0;
399 static char *global_text = 0;
401 char *get_global_text(void)
406 long global_text_lines;
408 static int total_updates;
409 static int updatereset;
411 void set_updatereset(int i)
416 int get_updatereset(void)
421 int check_contains(char *f, char *s)
424 FILE *where = open_file(f, 0);
429 while (fgets(buf1, 256, where)) {
430 if (strstr(buf1, s)) {
437 NORM_ERR("Could not open the file");
442 #define SECRIT_MULTILINE_CHAR '\x02'
446 static inline int calc_text_width(const char *s)
448 size_t slen = strlen(s);
450 if ((output_methods & TO_X) == 0) {
458 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
459 (const FcChar8 *) s, slen, &gi);
461 XftTextExtents8(display, fonts[selected_font].xftfont,
462 (const FcChar8 *) s, slen, &gi);
468 return XTextWidth(fonts[selected_font].font, s, slen);
473 /* formatted text to render on screen, generated in generate_text(),
474 * drawn in draw_stuff() */
476 static char *text_buffer;
478 /* quite boring functions */
480 static inline void for_each_line(char *b, int f(char *, int))
483 int special_index = 0; /* specials index */
485 for (ps = b, pe = b; *pe; pe++) {
488 special_index = f(ps, special_index);
495 f(ps, special_index);
499 static void convert_escapes(char *buf)
501 char *p = buf, *s = buf;
508 } else if (*s == '\\') {
519 /* Prints anything normally printed with snprintf according to the current value
520 * of use_spacer. Actually slightly more flexible than snprintf, as you can
521 * safely specify the destination buffer as one of your inputs. */
522 int spaced_print(char *buf, int size, const char *format, int width, ...)
531 tempbuf = malloc(size * sizeof(char));
533 // Passes the varargs along to vsnprintf
534 va_start(argp, width);
535 vsnprintf(tempbuf, size, format, argp);
538 switch (use_spacer) {
540 len = snprintf(buf, size, "%s", tempbuf);
543 len = snprintf(buf, size, "%*s", width, tempbuf);
546 len = snprintf(buf, size, "%-*s", width, tempbuf);
553 /* print percentage values
555 * - i.e., unsigned values between 0 and 100
556 * - respect the value of pad_percents */
557 static int percent_print(char *buf, int size, unsigned value)
559 return spaced_print(buf, size, "%u", pad_percents, value);
562 /* converts from bytes to human readable format (K, M, G, T)
564 * The algorithm always divides by 1024, as unit-conversion of byte
565 * counts suggests. But for output length determination we need to
566 * compare with 1000 here, as we print in decimal form. */
567 static void human_readable(long long num, char *buf, int size)
569 const char **suffix = suffixes;
575 /* Possibly just output as usual, for example for stdout usage */
576 if (!format_human_readable) {
577 spaced_print(buf, size, "%d", 6, round_to_int(num));
588 if (llabs(num) < 1000LL) {
589 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
593 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
601 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
603 * The goal is to always have a significance of 3, by
604 * adjusting the decimal part of the number. Sample output:
608 * so the point of alignment resides between number and unit. The
609 * upside of this is that there is minimal padding necessary, though
610 * there should be a way to make alignment take place at the decimal
611 * dot (then with fixed width decimal part).
613 * Note the repdigits below: when given a precision value, printf()
614 * rounds the float to it, not just cuts off the remaining digits. So
615 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
616 * printed with a precision of 0. Yay. */
618 precision = 0; /* print 100-999 without decimal part */
620 precision = 1; /* print 10-99 with one decimal place */
622 precision = 2; /* print 0-9 with two decimal places */
624 spaced_print(buf, size, format, width, precision, fnum, *suffix);
627 /* global object list root element */
628 static struct text_object global_root_object;
630 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
631 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
632 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
633 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
635 int parentend, childend;
637 //by running pipe after the strcmp's we make sure that we don't have to create a pipe
638 //and close the ends if mode is something illegal
639 if(strcmp(mode, "r") == 0) {
640 if(pipe(ends) != 0) {
645 } else if(strcmp(mode, "w") == 0) {
646 if(pipe(ends) != 0) {
659 } else if(*child > 0) {
661 waitpid(*child, NULL, 0);
663 //don't read from both stdin and pipe or write to both stdout and pipe
664 if(childend == ends[0]) {
669 dup(childend); //by dupping childend, the returned fd will have close-on-exec turned off
670 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
671 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail)
673 return fdopen(parentend, mode);
676 static inline void read_exec(const char *data, char *buf, const int size)
680 alarm(update_interval);
681 fp = pid_popen(data, "r", &childpid);
685 length = fread(buf, 1, size, fp);
688 if (length > 0 && buf[length - 1] == '\n') {
689 buf[length - 1] = '\0';
697 void do_read_exec(const char *data, char *buf, const int size)
699 read_exec(data, buf, size);
702 void *threaded_exec(void *) __attribute__((noreturn));
704 void *threaded_exec(void *arg)
707 struct text_object *obj = (struct text_object *)arg;
710 buff = malloc(text_buffer_size);
711 read_exec(obj->data.texeci.cmd, buff,
720 timed_thread_lock(obj->data.texeci.p_timed_thread);
721 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
722 timed_thread_unlock(obj->data.texeci.p_timed_thread);
724 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
725 timed_thread_exit(obj->data.texeci.p_timed_thread);
731 static long current_text_color;
733 void set_current_text_color(long colour)
735 current_text_color = colour;
738 long get_current_text_color(void)
740 return current_text_color;
743 //adds newstring to to the tree unless you can already see it when travelling back.
744 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
745 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
746 struct conftree* node;
747 struct conftree* node2;
749 for(node = previous; node != NULL; node = node->back) {
750 if(strcmp(node->string, newstring) == 0) {
754 node = malloc(sizeof(struct conftree));
755 if (previous != NULL) {
756 if(previous->vert_next == NULL) {
757 previous->vert_next = node;
759 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
760 node2->horz_next = node;
763 node->string = strdup(newstring);
764 node->horz_next = NULL;
765 node->vert_next = NULL;
766 node->back = previous;
770 void conftree_empty(struct conftree* tree) {
772 conftree_empty(tree->horz_next);
773 conftree_empty(tree->vert_next);
779 struct conftree *currentconffile;
781 static void extract_variable_text(const char *p)
783 free_text_objects(&global_root_object, 0);
797 extract_variable_text_internal(&global_root_object, p);
800 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
802 extract_variable_text_internal(root, txt);
803 generate_text_internal(p, max_user_text, *root, cur);
806 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
807 void *thread(void *), const char *text)
809 if (obj->char_b && info.mail) {
810 // this means we use info
811 if (!info.mail->p_timed_thread) {
812 info.mail->p_timed_thread =
813 timed_thread_create(thread,
814 (void *) info.mail, info.mail->interval * 1000000);
815 if (!info.mail->p_timed_thread) {
816 NORM_ERR("Error creating %s timed thread", text);
818 timed_thread_register(info.mail->p_timed_thread,
819 &info.mail->p_timed_thread);
820 if (timed_thread_run(info.mail->p_timed_thread)) {
821 NORM_ERR("Error running %s timed thread", text);
825 } else if (obj->data.mail) {
826 // this means we use obj
827 if (!obj->data.mail->p_timed_thread) {
828 obj->data.mail->p_timed_thread =
829 timed_thread_create(thread,
830 (void *) obj->data.mail,
831 obj->data.mail->interval * 1000000);
832 if (!obj->data.mail->p_timed_thread) {
833 NORM_ERR("Error creating %s timed thread", text);
835 timed_thread_register(obj->data.mail->p_timed_thread,
836 &obj->data.mail->p_timed_thread);
837 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
838 NORM_ERR("Error running %s timed thread", text);
841 return obj->data.mail;
842 } else if (!obj->a) {
843 // something is wrong, warn once then stop
844 NORM_ERR("There's a problem with your mail settings. "
845 "Check that the global mail settings are properly defined"
846 " (line %li).", obj->line);
852 char *format_time(unsigned long timeval, const int width)
855 unsigned long nt; // narrow time, for speed on 32-bit
856 unsigned cc; // centiseconds
857 unsigned nn; // multi-purpose whatever
860 cc = nt % 100; // centiseconds past second
861 nt /= 100; // total seconds
862 nn = nt % 60; // seconds past the minute
863 nt /= 60; // total minutes
864 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
866 return strndup(buf, text_buffer_size);
868 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
869 return strndup(buf, text_buffer_size);
871 nn = nt % 60; // minutes past the hour
872 nt /= 60; // total hours
873 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
874 return strndup(buf, text_buffer_size);
876 nn = nt; // now also hours
877 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
878 return strndup(buf, text_buffer_size);
880 nn /= 24; // now days
881 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
882 return strndup(buf, text_buffer_size);
884 nn /= 7; // now weeks
885 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
886 return strndup(buf, text_buffer_size);
888 // well shoot, this outta' fit...
889 return strndup("<inf>", text_buffer_size);
892 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
893 //string has to end with \0 and it's length should fit in a int
895 void remove_deleted_chars(char *string){
897 while(string[i] != 0){
898 if(string[i] == BACKSPACE){
900 strcpy( &(string[i-1]), &(string[i+1]) );
902 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
907 static inline void format_media_player_time(char *buf, const int size,
910 int days, hours, minutes;
912 days = seconds / (24 * 60 * 60);
913 seconds %= (24 * 60 * 60);
914 hours = seconds / (60 * 60);
915 seconds %= (60 * 60);
916 minutes = seconds / 60;
920 snprintf(buf, size, "%i days %i:%02i:%02i", days,
921 hours, minutes, seconds);
922 } else if (hours > 0) {
923 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
926 snprintf(buf, size, "%i:%02i", minutes, seconds);
930 static inline double get_barnum(char *buf)
942 if (sscanf(buf, "%lf", &barnum) == 0) {
943 NORM_ERR("reading exec value failed (perhaps it's not the "
947 if (barnum > 100.0 || barnum < 0.0) {
948 NORM_ERR("your exec value is not between 0 and 100, "
949 "therefore it will be ignored");
955 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
956 * multiline objects like $exec work with $align[rc] and friends
958 void substitute_newlines(char *p, long l)
962 while (p && *p && p < s + l) {
964 /* only substitute if it's not the last newline */
965 *p = SECRIT_MULTILINE_CHAR;
971 static void generate_text_internal(char *p, int p_max_size,
972 struct text_object root, struct information *cur)
974 struct text_object *obj;
976 int need_to_load_fonts = 0;
979 /* for the OBJ_top* handler */
980 struct process **needed = 0;
983 char buff_in[p_max_size];
985 set_iconv_converting(0);
986 #endif /* HAVE_ICONV */
990 while (obj && p_max_size > 0) {
991 needed = 0; /* reset for top stuff */
993 /* IFBLOCK jumping algorithm
995 * This is easier as it looks like:
996 * - each IF checks it's condition
997 * - on FALSE: call DO_JUMP
998 * - on TRUE: don't care
999 * - each ELSE calls DO_JUMP unconditionally
1000 * - each ENDIF is silently being ignored
1003 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
1004 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
1005 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
1006 * regularly, "obj" is being updated to point to obj->next, so object parsing
1007 * continues right after the corresponding ELSE or ENDIF. This means that if we
1008 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
1009 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
1010 * jumped, and there is nothing to do.
1014 obj = obj->data.ifblock.next; \
1017 #define OBJ(a) break; case OBJ_##a:
1019 switch (obj->type) {
1021 NORM_ERR("not implemented obj type %d", obj->type);
1024 struct sockaddr_in addr;
1029 if (!(he = gethostbyname(obj->data.read_tcp.host))) {
1030 NORM_ERR("read_tcp: Problem with resolving the hostname");
1033 if ((sock = socket(he->h_addrtype, SOCK_STREAM, 0)) == -1) {
1034 NORM_ERR("read_tcp: Couldn't create a socket");
1037 memset(&addr, 0, sizeof(addr));
1038 addr.sin_family = AF_INET;
1039 addr.sin_port = obj->data.read_tcp.port;
1040 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
1041 if (!connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr))) {
1042 NORM_ERR("read_tcp: Couldn't create a connection");
1046 FD_SET(sock, &readfds);
1049 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
1050 received = recv(sock, p, p_max_size, 0);
1057 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
1059 #endif /* !__OpenBSD__ */
1062 obj->a = get_freq(p, p_max_size, "%.0f", 1,
1063 obj->data.cpu_index);
1069 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
1070 obj->data.cpu_index);
1072 /* OpenBSD has no such flag (SUSv2) */
1073 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
1074 obj->data.cpu_index);
1075 #endif /* __OpenBSD */
1078 #if defined(__linux__)
1081 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
1082 obj->data.cpu_index);
1087 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
1088 obj->data.cpu_index);
1093 OBJ(wireless_essid) {
1094 snprintf(p, p_max_size, "%s", obj->data.net->essid);
1096 OBJ(wireless_mode) {
1097 snprintf(p, p_max_size, "%s", obj->data.net->mode);
1099 OBJ(wireless_bitrate) {
1100 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
1103 snprintf(p, p_max_size, "%s", obj->data.net->ap);
1105 OBJ(wireless_link_qual) {
1106 spaced_print(p, p_max_size, "%d", 4,
1107 obj->data.net->link_qual);
1109 OBJ(wireless_link_qual_max) {
1110 spaced_print(p, p_max_size, "%d", 4,
1111 obj->data.net->link_qual_max);
1113 OBJ(wireless_link_qual_perc) {
1114 if (obj->data.net->link_qual_max > 0) {
1115 spaced_print(p, p_max_size, "%.0f", 5,
1116 (double) obj->data.net->link_qual /
1117 obj->data.net->link_qual_max * 100);
1119 spaced_print(p, p_max_size, "unk", 5);
1122 OBJ(wireless_link_bar) {
1124 if(output_methods & TO_X) {
1125 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
1126 obj->data.net->link_qual_max) * 255.0);
1129 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1130 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
1131 obj->data.net->link_qual_max) * 100.0, obj->a);
1136 #endif /* HAVE_IWLIB */
1138 #endif /* __linux__ */
1142 get_adt746x_cpu(p, p_max_size);
1145 get_adt746x_fan(p, p_max_size);
1148 get_acpi_fan(p, p_max_size);
1150 OBJ(acpiacadapter) {
1151 get_acpi_ac_adapter(p, p_max_size);
1154 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
1157 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
1159 OBJ(battery_percent) {
1160 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
1164 if(output_methods & TO_X) {
1165 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
1168 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1169 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
1174 OBJ(battery_short) {
1175 get_battery_short_status(p, p_max_size, obj->data.s);
1177 #endif /* __OpenBSD__ */
1180 human_readable(cur->buffers * 1024, p, 255);
1183 human_readable(cur->cached * 1024, p, 255);
1186 if (obj->data.cpu_index > info.cpu_count) {
1187 NORM_ERR("obj->data.cpu_index %i info.cpu_count %i",
1188 obj->data.cpu_index, info.cpu_count);
1189 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
1191 percent_print(p, p_max_size,
1192 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
1196 new_gauge(p, obj->a, obj->b,
1197 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1201 if(output_methods & TO_X) {
1202 new_bar(p, obj->a, obj->b,
1203 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1206 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1207 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
1214 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1215 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
1216 100, 1, obj->char_a, obj->char_b);
1219 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
1220 obj->e, 1, obj->char_a, obj->char_b);
1224 new_fg(p, obj->data.l);
1258 OBJ(conky_version) {
1259 snprintf(p, p_max_size, "%s", VERSION);
1261 OBJ(conky_build_date) {
1262 snprintf(p, p_max_size, "%s", BUILD_DATE);
1264 OBJ(conky_build_arch) {
1265 snprintf(p, p_max_size, "%s", BUILD_ARCH);
1267 #if defined(__linux__)
1269 snprintf(p, p_max_size, "%s",
1270 get_disk_protect_queue(obj->data.s));
1273 snprintf(p, p_max_size, "%s", i8k.version);
1276 snprintf(p, p_max_size, "%s", i8k.bios);
1279 snprintf(p, p_max_size, "%s", i8k.serial);
1284 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1285 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1287 OBJ(i8k_left_fan_status) {
1288 int left_fan_status;
1290 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1291 if (left_fan_status == 0) {
1292 snprintf(p, p_max_size, "off");
1294 if (left_fan_status == 1) {
1295 snprintf(p, p_max_size, "low");
1297 if (left_fan_status == 2) {
1298 snprintf(p, p_max_size, "high");
1301 OBJ(i8k_right_fan_status) {
1302 int right_fan_status;
1304 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1305 if (right_fan_status == 0) {
1306 snprintf(p, p_max_size, "off");
1308 if (right_fan_status == 1) {
1309 snprintf(p, p_max_size, "low");
1311 if (right_fan_status == 2) {
1312 snprintf(p, p_max_size, "high");
1315 OBJ(i8k_left_fan_rpm) {
1316 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1318 OBJ(i8k_right_fan_rpm) {
1319 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1321 OBJ(i8k_ac_status) {
1324 sscanf(i8k.ac_status, "%d", &ac_status);
1325 if (ac_status == -1) {
1326 snprintf(p, p_max_size, "disabled (read i8k docs)");
1328 if (ac_status == 0) {
1329 snprintf(p, p_max_size, "off");
1331 if (ac_status == 1) {
1332 snprintf(p, p_max_size, "on");
1335 OBJ(i8k_buttons_status) {
1336 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1340 get_ibm_acpi_fan(p, p_max_size);
1343 get_ibm_acpi_temps();
1344 temp_print(p, p_max_size,
1345 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
1348 get_ibm_acpi_volume(p, p_max_size);
1350 OBJ(ibm_brightness) {
1351 get_ibm_acpi_brightness(p, p_max_size);
1354 /* information from sony_laptop kernel module
1355 * /sys/devices/platform/sony-laptop */
1356 OBJ(sony_fanspeed) {
1357 get_sony_fanspeed(p, p_max_size);
1360 if (!cur->gw_info.count) {
1365 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
1368 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
1371 snprintf(p, p_max_size, "%d", get_laptop_mode());
1374 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1376 #endif /* __linux__ */
1377 #if (defined(__FreeBSD__) || defined(__linux__))
1379 if ((obj->data.ifblock.s)
1380 && (!interface_up(obj->data.ifblock.s))) {
1386 OBJ(obsd_sensors_temp) {
1387 obsd_sensors.device = sensor_device;
1388 update_obsd_sensors();
1389 temp_print(p, p_max_size,
1390 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
1393 OBJ(obsd_sensors_fan) {
1394 obsd_sensors.device = sensor_device;
1395 update_obsd_sensors();
1396 snprintf(p, p_max_size, "%d",
1397 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
1399 OBJ(obsd_sensors_volt) {
1400 obsd_sensors.device = sensor_device;
1401 update_obsd_sensors();
1402 snprintf(p, p_max_size, "%.2f",
1403 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
1406 get_obsd_vendor(p, p_max_size);
1409 get_obsd_product(p, p_max_size);
1411 #endif /* __OpenBSD__ */
1414 new_font(p, obj->data.s);
1415 need_to_load_fonts = 1;
1418 /* TODO: move this correction from kB to kB/s elsewhere
1419 * (or get rid of it??) */
1421 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
1425 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
1429 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
1434 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1435 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
1437 OBJ(diskiograph_read) {
1438 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1439 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
1441 OBJ(diskiograph_write) {
1442 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1443 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
1447 human_readable(obj->data.net->recv_speed, p, 255);
1450 spaced_print(p, p_max_size, "%.1f", 8,
1451 obj->data.net->recv_speed / 1024.0);
1454 OBJ(downspeedgraph) {
1455 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1456 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
1460 /* Since we see you, you're if has not jumped.
1461 * Do Ninja jump here: without leaving traces.
1462 * This is to prevent us from stale jumped flags.
1464 obj = obj->data.ifblock.next;
1468 /* harmless object, just ignore */
1471 if ((obj->data.net->addr.sa_data[2] & 255) == 0
1472 && (obj->data.net->addr.sa_data[3] & 255) == 0
1473 && (obj->data.net->addr.sa_data[4] & 255) == 0
1474 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
1475 snprintf(p, p_max_size, "No Address");
1477 snprintf(p, p_max_size, "%u.%u.%u.%u",
1478 obj->data.net->addr.sa_data[2] & 255,
1479 obj->data.net->addr.sa_data[3] & 255,
1480 obj->data.net->addr.sa_data[4] & 255,
1481 obj->data.net->addr.sa_data[5] & 255);
1484 #if defined(__linux__)
1486 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
1487 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
1488 strcpy(p, obj->data.net->addrs);
1490 strcpy(p, "0.0.0.0");
1493 #endif /* __linux__ */
1494 #if defined(IMLIB2) && defined(X11)
1496 /* doesn't actually draw anything, just queues it omp. the
1497 * image will get drawn after the X event loop */
1498 cimlib_add_image(obj->data.s);
1502 evaluate(obj->data.s, p);
1505 read_exec(obj->data.s, p, text_buffer_size);
1506 remove_deleted_chars(p);
1509 struct information *tmp_info;
1510 struct text_object subroot;
1512 read_exec(obj->data.s, p, text_buffer_size);
1514 tmp_info = malloc(sizeof(struct information));
1515 memcpy(tmp_info, cur, sizeof(struct information));
1516 parse_conky_vars(&subroot, p, p, tmp_info);
1518 free_text_objects(&subroot, 1);
1525 read_exec(obj->data.s, p, text_buffer_size);
1526 barnum = get_barnum(p); /*using the same function*/
1528 if (barnum >= 0.0) {
1530 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1537 read_exec(obj->data.s, p, text_buffer_size);
1538 barnum = get_barnum(p);
1540 if (barnum >= 0.0) {
1542 if(output_methods & TO_X) {
1544 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1547 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1548 new_bar_in_shell(p, p_max_size, barnum, obj->a);
1556 char showaslog = FALSE;
1557 char tempgrad = FALSE;
1559 char *cmd = obj->data.s;
1561 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1563 cmd += strlen(" "TEMPGRAD);
1565 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1567 cmd += strlen(" "LOGGRAPH);
1569 read_exec(cmd, p, text_buffer_size);
1570 barnum = get_barnum(p);
1573 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
1574 100, 1, showaslog, tempgrad);
1579 if (current_update_time - obj->data.execi.last_update
1580 >= obj->data.execi.interval) {
1583 read_exec(obj->data.execi.cmd, p, text_buffer_size);
1584 barnum = get_barnum(p);
1586 if (barnum >= 0.0) {
1589 obj->data.execi.last_update = current_update_time;
1592 if(output_methods & TO_X) {
1593 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
1596 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1597 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
1604 if (current_update_time - obj->data.execi.last_update
1605 >= obj->data.execi.interval) {
1607 char showaslog = FALSE;
1608 char tempgrad = FALSE;
1609 char *cmd = obj->data.execi.cmd;
1611 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1613 cmd += strlen(" "TEMPGRAD);
1615 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1617 cmd += strlen(" "LOGGRAPH);
1619 obj->char_a = showaslog;
1620 obj->char_b = tempgrad;
1621 read_exec(cmd, p, text_buffer_size);
1622 barnum = get_barnum(p);
1624 if (barnum >= 0.0) {
1627 obj->data.execi.last_update = current_update_time;
1629 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
1632 if (current_update_time - obj->data.execi.last_update
1633 >= obj->data.execi.interval) {
1636 read_exec(obj->data.execi.cmd, p, text_buffer_size);
1637 barnum = get_barnum(p);
1639 if (barnum >= 0.0) {
1640 obj->f = 255 * barnum / 100.0;
1642 obj->data.execi.last_update = current_update_time;
1644 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
1648 if (current_update_time - obj->data.execi.last_update
1649 >= obj->data.execi.interval
1650 && obj->data.execi.interval != 0) {
1651 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
1653 obj->data.execi.last_update = current_update_time;
1655 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
1658 struct text_object subroot;
1659 struct information *tmp_info =
1660 malloc(sizeof(struct information));
1661 memcpy(tmp_info, cur, sizeof(struct information));
1663 if (current_update_time - obj->data.execi.last_update
1664 < obj->data.execi.interval
1665 || obj->data.execi.interval == 0) {
1666 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1668 char *output = obj->data.execi.buffer;
1669 FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
1670 int length = fread(output, 1, text_buffer_size, fp);
1674 output[length] = '\0';
1675 if (length > 0 && output[length - 1] == '\n') {
1676 output[length - 1] = '\0';
1679 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1680 obj->data.execi.last_update = current_update_time;
1682 free_text_objects(&subroot, 1);
1686 if (!obj->data.texeci.p_timed_thread) {
1687 obj->data.texeci.p_timed_thread =
1688 timed_thread_create(&threaded_exec,
1689 (void *) obj, obj->data.texeci.interval * 1000000);
1690 if (!obj->data.texeci.p_timed_thread) {
1691 NORM_ERR("Error creating texeci timed thread");
1694 * note that we don't register this thread with the
1695 * timed_thread list, because we destroy it manually
1697 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
1698 NORM_ERR("Error running texeci timed thread");
1701 timed_thread_lock(obj->data.texeci.p_timed_thread);
1702 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
1703 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1707 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1709 if (mail && mail->p_timed_thread) {
1710 timed_thread_lock(mail->p_timed_thread);
1711 snprintf(p, p_max_size, "%lu", mail->unseen);
1712 timed_thread_unlock(mail->p_timed_thread);
1715 OBJ(imap_messages) {
1716 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1718 if (mail && mail->p_timed_thread) {
1719 timed_thread_lock(mail->p_timed_thread);
1720 snprintf(p, p_max_size, "%lu", mail->messages);
1721 timed_thread_unlock(mail->p_timed_thread);
1725 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1727 if (mail && mail->p_timed_thread) {
1728 timed_thread_lock(mail->p_timed_thread);
1729 snprintf(p, p_max_size, "%lu", mail->unseen);
1730 timed_thread_unlock(mail->p_timed_thread);
1734 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1736 if (mail && mail->p_timed_thread) {
1737 timed_thread_lock(mail->p_timed_thread);
1738 snprintf(p, p_max_size, "%.1f",
1739 mail->used / 1024.0 / 1024.0);
1740 timed_thread_unlock(mail->p_timed_thread);
1744 if (obj->data.fs != NULL) {
1745 if (obj->data.fs->size == 0) {
1747 if(output_methods & TO_X) {
1748 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1751 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1752 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1758 if(output_methods & TO_X) {
1759 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1760 (int) (255 - obj->data.fsbar.fs->avail * 255 /
1761 obj->data.fs->size));
1764 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1765 new_bar_in_shell(p, p_max_size,
1766 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1774 if (obj->data.fs != NULL) {
1775 human_readable(obj->data.fs->avail, p, 255);
1779 if (obj->data.fs != NULL) {
1782 if (obj->data.fs->size) {
1783 val = obj->data.fs->avail * 100 / obj->data.fs->size;
1786 percent_print(p, p_max_size, val);
1790 if (obj->data.fs != NULL) {
1791 human_readable(obj->data.fs->size, p, 255);
1795 if (obj->data.fs != NULL)
1796 snprintf(p, p_max_size, "%s", obj->data.fs->type);
1799 if (obj->data.fs != NULL) {
1800 human_readable(obj->data.fs->size - obj->data.fs->free, p,
1805 if (obj->data.fs != NULL) {
1806 if (obj->data.fs->size == 0) {
1808 if(output_methods & TO_X) {
1809 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1812 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1813 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1819 if(output_methods & TO_X) {
1820 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1821 (int) (obj->data.fsbar.fs->avail * 255 /
1822 obj->data.fs->size));
1825 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1826 new_bar_in_shell(p, p_max_size,
1827 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1835 if (obj->data.fs != NULL) {
1838 if (obj->data.fs->size) {
1839 val = obj->data.fs->free
1844 percent_print(p, p_max_size, 100 - val);
1848 float *v = info.loadavg;
1850 if (obj->data.loadavg[2]) {
1851 snprintf(p, p_max_size, "%.2f %.2f %.2f",
1852 v[obj->data.loadavg[0] - 1],
1853 v[obj->data.loadavg[1] - 1],
1854 v[obj->data.loadavg[2] - 1]);
1855 } else if (obj->data.loadavg[1]) {
1856 snprintf(p, p_max_size, "%.2f %.2f",
1857 v[obj->data.loadavg[0] - 1],
1858 v[obj->data.loadavg[1] - 1]);
1859 } else if (obj->data.loadavg[0]) {
1860 snprintf(p, p_max_size, "%.2f",
1861 v[obj->data.loadavg[0] - 1]);
1865 new_goto(p, obj->data.i);
1868 new_tab(p, obj->data.pair.a, obj->data.pair.b);
1872 new_hr(p, obj->data.i);
1876 if (cur->nameserver_info.nscount > obj->data.i)
1877 snprintf(p, p_max_size, "%s",
1878 cur->nameserver_info.ns_list[obj->data.i]);
1882 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
1883 snprintf(p, p_max_size, "%s", skill);
1888 if (obj->data.curl.uri != NULL) {
1889 ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
1891 NORM_ERR("error processing Curl data");
1897 if (obj->data.rss.uri != NULL) {
1898 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);
1900 NORM_ERR("error processing RSS data");
1906 if (obj->data.weather.uri != NULL) {
1907 weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
1909 NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
1914 OBJ(weather_forecast) {
1915 if (obj->data.weather_forecast.uri != NULL) {
1916 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);
1918 NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
1924 char *str = llua_getstring(obj->data.s);
1926 snprintf(p, p_max_size, "%s", str);
1931 char *str = llua_getstring(obj->data.s);
1939 if (llua_getnumber(obj->data.s, &per)) {
1941 if(output_methods & TO_X) {
1942 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
1945 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1946 new_bar_in_shell(p, p_max_size, per, obj->a);
1955 if (llua_getnumber(obj->data.s, &per)) {
1956 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1957 per, obj->e, 1, obj->char_a, obj->char_b);
1962 if (llua_getnumber(obj->data.s, &per)) {
1963 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
1967 #endif /* HAVE_LUA */
1973 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1974 snprintf(p, p_max_size, "N/A");
1976 temp_print(p, p_max_size, (double)val,
1977 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1982 new_offset(p, obj->data.i);
1985 new_voffset(p, obj->data.i);
1991 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
1992 obj->data.sysfs.devtype, obj->data.sysfs.type);
1994 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
1996 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
1997 temp_print(p, p_max_size, r, TEMP_CELSIUS);
1998 } else if (r >= 100.0 || r == 0) {
1999 snprintf(p, p_max_size, "%d", (int) r);
2001 snprintf(p, p_max_size, "%.1f", r);
2007 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2008 obj->data.sysfs.devtype, obj->data.sysfs.type);
2010 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2012 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2013 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2014 } else if (r >= 100.0 || r == 0) {
2015 snprintf(p, p_max_size, "%d", (int) r);
2017 snprintf(p, p_max_size, "%.1f", r);
2023 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2024 obj->data.sysfs.devtype, obj->data.sysfs.type);
2026 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2028 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2029 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2030 } else if (r >= 100.0 || r == 0) {
2031 snprintf(p, p_max_size, "%d", (int) r);
2033 snprintf(p, p_max_size, "%.1f", r);
2036 #endif /* __linux__ */
2038 new_alignr(p, obj->data.i);
2041 new_alignc(p, obj->data.i);
2044 char buf[max_user_text];
2045 struct information *tmp_info =
2046 malloc(sizeof(struct information));
2047 memcpy(tmp_info, cur, sizeof(struct information));
2048 generate_text_internal(buf, max_user_text,
2049 *obj->sub, tmp_info);
2051 if (strlen(buf) != 0) {
2057 char expression[max_user_text];
2059 struct information *tmp_info;
2061 tmp_info = malloc(sizeof(struct information));
2062 memcpy(tmp_info, cur, sizeof(struct information));
2063 generate_text_internal(expression, max_user_text,
2064 *obj->sub, tmp_info);
2065 DBGP("parsed arg into '%s'", expression);
2067 val = compare(expression);
2069 NORM_ERR("compare failed for expression '%s'",
2077 if (obj->data.ifblock.str
2078 && !check_contains(obj->data.ifblock.s,
2079 obj->data.ifblock.str)) {
2081 } else if (obj->data.ifblock.s
2082 && access(obj->data.ifblock.s, F_OK)) {
2087 if ((obj->data.ifblock.s)
2088 && (!check_mount(obj->data.ifblock.s))) {
2094 if (!get_process_by_name(obj->data.ifblock.s)) {
2096 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
2101 #if defined(__linux__)
2103 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
2107 snprintf(p, p_max_size, "%s", cur->uname_s.release);
2110 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
2115 human_readable(cur->mem * 1024, p, 255);
2118 human_readable(cur->memeasyfree * 1024, p, 255);
2121 human_readable(cur->memfree * 1024, p, 255);
2124 human_readable(cur->memmax * 1024, p, 255);
2128 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
2132 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
2133 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2138 if(output_methods & TO_X) {
2139 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2140 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2143 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2144 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
2151 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2152 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
2153 100, 1, obj->char_a, obj->char_b);
2158 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
2161 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
2164 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
2168 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2169 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
2172 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2173 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
2176 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2177 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
2180 OBJ(if_mixer_mute) {
2181 if (!mixer_is_mute(obj->data.ifblock.i)) {
2186 #define NOT_IN_X "Not running in X"
2188 if(x_initialised != YES) {
2189 strncpy(p, NOT_IN_X, p_max_size);
2191 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
2194 OBJ(monitor_number) {
2195 if(x_initialised != YES) {
2196 strncpy(p, NOT_IN_X, p_max_size);
2198 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
2202 if(x_initialised != YES) {
2203 strncpy(p, NOT_IN_X, p_max_size);
2205 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
2208 OBJ(desktop_number) {
2209 if(x_initialised != YES) {
2210 strncpy(p, NOT_IN_X, p_max_size);
2212 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
2216 if(x_initialised != YES) {
2217 strncpy(p, NOT_IN_X, p_max_size);
2218 }else if(cur->x11.desktop.name != NULL) {
2219 strncpy(p, cur->x11.desktop.name, p_max_size);
2226 update_mail_count(&obj->data.local_mail);
2227 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
2230 update_mail_count(&obj->data.local_mail);
2231 snprintf(p, p_max_size, "%d",
2232 obj->data.local_mail.new_mail_count);
2235 update_mail_count(&obj->data.local_mail);
2236 snprintf(p, p_max_size, "%d",
2237 obj->data.local_mail.seen_mail_count);
2240 update_mail_count(&obj->data.local_mail);
2241 snprintf(p, p_max_size, "%d",
2242 obj->data.local_mail.unseen_mail_count);
2244 OBJ(flagged_mails) {
2245 update_mail_count(&obj->data.local_mail);
2246 snprintf(p, p_max_size, "%d",
2247 obj->data.local_mail.flagged_mail_count);
2249 OBJ(unflagged_mails) {
2250 update_mail_count(&obj->data.local_mail);
2251 snprintf(p, p_max_size, "%d",
2252 obj->data.local_mail.unflagged_mail_count);
2254 OBJ(forwarded_mails) {
2255 update_mail_count(&obj->data.local_mail);
2256 snprintf(p, p_max_size, "%d",
2257 obj->data.local_mail.forwarded_mail_count);
2259 OBJ(unforwarded_mails) {
2260 update_mail_count(&obj->data.local_mail);
2261 snprintf(p, p_max_size, "%d",
2262 obj->data.local_mail.unforwarded_mail_count);
2264 OBJ(replied_mails) {
2265 update_mail_count(&obj->data.local_mail);
2266 snprintf(p, p_max_size, "%d",
2267 obj->data.local_mail.replied_mail_count);
2269 OBJ(unreplied_mails) {
2270 update_mail_count(&obj->data.local_mail);
2271 snprintf(p, p_max_size, "%d",
2272 obj->data.local_mail.unreplied_mail_count);
2275 update_mail_count(&obj->data.local_mail);
2276 snprintf(p, p_max_size, "%d",
2277 obj->data.local_mail.draft_mail_count);
2279 OBJ(trashed_mails) {
2280 update_mail_count(&obj->data.local_mail);
2281 snprintf(p, p_max_size, "%d",
2282 obj->data.local_mail.trashed_mail_count);
2285 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
2287 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
2290 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
2293 new_outline(p, obj->data.l);
2296 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
2298 OBJ(running_processes) {
2299 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
2302 snprintf(p, p_max_size, "%s", obj->data.s);
2306 new_bg(p, obj->data.l);
2309 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
2313 human_readable(cur->swap * 1024, p, 255);
2316 human_readable(cur->swapfree * 1024, p, 255);
2319 human_readable(cur->swapmax * 1024, p, 255);
2322 if (cur->swapmax == 0) {
2323 strncpy(p, "No swap", p_max_size);
2325 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
2330 if(output_methods & TO_X) {
2331 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2332 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
2335 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2336 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
2342 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
2345 time_t t = time(NULL);
2346 struct tm *tm = localtime(&t);
2348 setlocale(LC_TIME, "");
2349 strftime(p, p_max_size, obj->data.s, tm);
2352 time_t t = time(NULL);
2353 struct tm *tm = gmtime(&t);
2355 strftime(p, p_max_size, obj->data.s, tm);
2362 if (obj->data.tztime.tz) {
2363 oldTZ = getenv("TZ");
2364 setenv("TZ", obj->data.tztime.tz, 1);
2370 setlocale(LC_TIME, "");
2371 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
2373 setenv("TZ", oldTZ, 1);
2378 // Needless to free oldTZ since getenv gives ptr to static data
2381 human_readable(obj->data.net->recv, p, 255);
2384 human_readable(obj->data.net->trans, p, 255);
2387 snprintf(p, p_max_size, "%d", total_updates);
2390 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
2395 human_readable(obj->data.net->trans_speed, p, 255);
2398 spaced_print(p, p_max_size, "%.1f", 8,
2399 obj->data.net->trans_speed / 1024.0);
2403 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2404 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
2408 format_seconds_short(p, p_max_size, (int) cur->uptime);
2411 format_seconds(p, p_max_size, (int) cur->uptime);
2414 snprintf(p, p_max_size, "%s", cur->users.names);
2417 snprintf(p, p_max_size, "%s", cur->users.terms);
2420 snprintf(p, p_max_size, "%s", cur->users.times);
2423 snprintf(p, p_max_size, "%d", cur->users.number);
2425 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2426 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2430 msg = get_apm_adapter();
2431 snprintf(p, p_max_size, "%s", msg);
2434 OBJ(apm_battery_life) {
2437 msg = get_apm_battery_life();
2438 snprintf(p, p_max_size, "%s", msg);
2441 OBJ(apm_battery_time) {
2444 msg = get_apm_battery_time();
2445 snprintf(p, p_max_size, "%s", msg);
2448 #endif /* __FreeBSD__ __OpenBSD__ */
2451 #define mpd_printf(fmt, val) \
2452 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
2453 #define mpd_sprintf(val) { \
2454 if (!obj->data.i || obj->data.i > p_max_size) \
2455 mpd_printf("%s", val); \
2457 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2462 mpd_sprintf(artist);
2466 mpd_printf("%s", random);
2468 mpd_printf("%s", repeat);
2476 mpd_printf("%d", volume);
2478 mpd_printf("%d", bitrate);
2480 mpd_printf("%s", status);
2482 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2485 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2488 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2492 if(output_methods & TO_X) {
2493 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2494 (int) (mpd_get_info()->progress * 255.0f));
2497 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2498 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
2504 struct mpd_s *mpd = mpd_get_info();
2505 int len = obj->data.i;
2506 if (len == 0 || len > p_max_size)
2509 memset(p, 0, p_max_size);
2510 if (mpd->artist && *mpd->artist &&
2511 mpd->title && *mpd->title) {
2512 snprintf(p, len, "%s - %s", mpd->artist,
2514 } else if (mpd->title && *mpd->title) {
2515 snprintf(p, len, "%s", mpd->title);
2516 } else if (mpd->artist && *mpd->artist) {
2517 snprintf(p, len, "%s", mpd->artist);
2518 } else if (mpd->file && *mpd->file) {
2519 snprintf(p, len, "%s", mpd->file);
2524 OBJ(if_mpd_playing) {
2525 if (!mpd_get_info()->is_playing) {
2534 #define MOC_PRINT(t, a) \
2535 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2537 MOC_PRINT(state, "??");
2540 MOC_PRINT(file, "no file");
2543 MOC_PRINT(title, "no title");
2546 MOC_PRINT(artist, "no artist");
2549 MOC_PRINT(song, "no song");
2552 MOC_PRINT(album, "no album");
2554 OBJ(moc_totaltime) {
2555 MOC_PRINT(totaltime, "0:00");
2558 MOC_PRINT(timeleft, "0:00");
2561 MOC_PRINT(curtime, "0:00");
2564 MOC_PRINT(bitrate, "0Kbps");
2567 MOC_PRINT(rate, "0KHz");
2573 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2576 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2579 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2582 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2584 OBJ(xmms2_comment) {
2585 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2588 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2591 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2594 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2596 OBJ(xmms2_tracknr) {
2597 if (cur->xmms2.tracknr != -1) {
2598 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2601 OBJ(xmms2_bitrate) {
2602 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2605 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2608 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2610 OBJ(xmms2_elapsed) {
2611 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2612 (cur->xmms2.elapsed / 1000) % 60);
2614 OBJ(xmms2_duration) {
2615 snprintf(p, p_max_size, "%02d:%02d",
2616 cur->xmms2.duration / 60000,
2617 (cur->xmms2.duration / 1000) % 60);
2619 OBJ(xmms2_percent) {
2620 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2624 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2625 (int) (cur->xmms2.progress * 255.0f));
2628 OBJ(xmms2_playlist) {
2629 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2631 OBJ(xmms2_timesplayed) {
2632 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2635 if (strlen(cur->xmms2.title) < 2
2636 && strlen(cur->xmms2.title) < 2) {
2637 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2639 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2643 OBJ(if_xmms2_connected) {
2644 if (cur->xmms2.conn_state != 1) {
2650 OBJ(audacious_status) {
2651 snprintf(p, p_max_size, "%s",
2652 cur->audacious.items[AUDACIOUS_STATUS]);
2654 OBJ(audacious_title) {
2655 snprintf(p, cur->audacious.max_title_len > 0
2656 ? cur->audacious.max_title_len : p_max_size, "%s",
2657 cur->audacious.items[AUDACIOUS_TITLE]);
2659 OBJ(audacious_length) {
2660 snprintf(p, p_max_size, "%s",
2661 cur->audacious.items[AUDACIOUS_LENGTH]);
2663 OBJ(audacious_length_seconds) {
2664 snprintf(p, p_max_size, "%s",
2665 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2667 OBJ(audacious_position) {
2668 snprintf(p, p_max_size, "%s",
2669 cur->audacious.items[AUDACIOUS_POSITION]);
2671 OBJ(audacious_position_seconds) {
2672 snprintf(p, p_max_size, "%s",
2673 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2675 OBJ(audacious_bitrate) {
2676 snprintf(p, p_max_size, "%s",
2677 cur->audacious.items[AUDACIOUS_BITRATE]);
2679 OBJ(audacious_frequency) {
2680 snprintf(p, p_max_size, "%s",
2681 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2683 OBJ(audacious_channels) {
2684 snprintf(p, p_max_size, "%s",
2685 cur->audacious.items[AUDACIOUS_CHANNELS]);
2687 OBJ(audacious_filename) {
2688 snprintf(p, p_max_size, "%s",
2689 cur->audacious.items[AUDACIOUS_FILENAME]);
2691 OBJ(audacious_playlist_length) {
2692 snprintf(p, p_max_size, "%s",
2693 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2695 OBJ(audacious_playlist_position) {
2696 snprintf(p, p_max_size, "%s",
2697 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2699 OBJ(audacious_main_volume) {
2700 snprintf(p, p_max_size, "%s",
2701 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2704 OBJ(audacious_bar) {
2708 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2709 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2710 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
2713 #endif /* AUDACIOUS */
2717 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2720 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2723 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2726 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2729 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2732 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2735 /* we have four different types of top (top, top_mem,
2736 * top_time and top_io). To avoid having almost-same code four
2737 * times, we have this special handler. */
2740 parse_top_args("top", obj->data.top.s, obj);
2741 if (!needed) needed = cur->cpu;
2743 parse_top_args("top_mem", obj->data.top.s, obj);
2744 if (!needed) needed = cur->memu;
2746 parse_top_args("top_time", obj->data.top.s, obj);
2747 if (!needed) needed = cur->time;
2750 parse_top_args("top_io", obj->data.top.s, obj);
2751 if (!needed) needed = cur->io;
2754 if (needed[obj->data.top.num]) {
2757 switch (obj->data.top.type) {
2759 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
2760 needed[obj->data.top.num]->name);
2763 snprintf(p, 7, "%6.2f",
2764 needed[obj->data.top.num]->amount);
2767 snprintf(p, 6, "%5i",
2768 needed[obj->data.top.num]->pid);
2771 snprintf(p, 7, "%6.2f",
2772 needed[obj->data.top.num]->totalmem);
2775 timeval = format_time(
2776 needed[obj->data.top.num]->total_cpu_time, 9);
2777 snprintf(p, 10, "%9s", timeval);
2781 human_readable(needed[obj->data.top.num]->rss,
2785 human_readable(needed[obj->data.top.num]->vsize,
2789 case TOP_READ_BYTES:
2790 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
2793 case TOP_WRITE_BYTES:
2794 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
2798 snprintf(p, 7, "%6.2f",
2799 needed[obj->data.top.num]->io_perc);
2805 print_tailhead("tail", obj, p, p_max_size);
2808 print_tailhead("head", obj, p, p_max_size);
2811 FILE *fp = open_file(obj->data.s, &obj->a);
2814 /* FIXME: use something more general (see also tail.c, head.c */
2815 #define BUFSZ 0x1000
2820 while(fgets(buf, BUFSZ, fp) != NULL){
2821 for(j = 0; buf[j] != 0; j++) {
2822 if(buf[j] == '\n') {
2827 sprintf(p, "%d", lines);
2830 sprintf(p, "File Unreadable");
2835 FILE *fp = open_file(obj->data.s, &obj->a);
2840 char inword = FALSE;
2843 while(fgets(buf, BUFSZ, fp) != NULL){
2844 for(j = 0; buf[j] != 0; j++) {
2845 if(!isspace(buf[j])) {
2846 if(inword == FALSE) {
2855 sprintf(p, "%d", words);
2858 sprintf(p, "File Unreadable");
2861 #ifdef TCP_PORT_MONITOR
2863 tcp_portmon_action(p, p_max_size,
2864 &obj->data.tcp_port_monitor);
2866 #endif /* TCP_PORT_MONITOR */
2870 set_iconv_converting(1);
2871 set_iconv_selected(obj->a);
2874 set_iconv_converting(0);
2875 set_iconv_selected(0);
2877 #endif /* HAVE_ICONV */
2879 OBJ(entropy_avail) {
2880 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2883 percent_print(p, p_max_size,
2884 cur->entropy.entropy_avail *
2885 100 / cur->entropy.poolsize);
2887 OBJ(entropy_poolsize) {
2888 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2891 double entropy_perc;
2893 entropy_perc = (double) cur->entropy.entropy_avail /
2894 (double) cur->entropy.poolsize;
2896 if(output_methods & TO_X) {
2897 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
2900 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
2901 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
2910 s = smapi_get_val(obj->data.s);
2911 snprintf(p, p_max_size, "%s", s);
2915 OBJ(if_smapi_bat_installed) {
2917 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
2918 if(!smapi_bat_installed(idx)) {
2922 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2924 OBJ(smapi_bat_perc) {
2926 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2927 val = smapi_bat_installed(idx) ?
2928 smapi_get_bat_int(idx, "remaining_percent") : 0;
2929 percent_print(p, p_max_size, val);
2931 NORM_ERR("argument to smapi_bat_perc must be an integer");
2933 OBJ(smapi_bat_temp) {
2935 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2936 val = smapi_bat_installed(idx) ?
2937 smapi_get_bat_int(idx, "temperature") : 0;
2938 /* temperature is in milli degree celsius */
2939 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2941 NORM_ERR("argument to smapi_bat_temp must be an integer");
2943 OBJ(smapi_bat_power) {
2945 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2946 val = smapi_bat_installed(idx) ?
2947 smapi_get_bat_int(idx, "power_now") : 0;
2948 /* power_now is in mW, set to W with one digit precision */
2949 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2951 NORM_ERR("argument to smapi_bat_power must be an integer");
2954 OBJ(smapi_bat_bar) {
2955 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2956 new_bar(p, obj->a, obj->b, (int)
2957 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2959 new_bar(p, obj->a, obj->b, 0);
2965 char buf[max_user_text];
2967 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2968 snprintf(p, p_max_size, "%s", buf);
2974 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2975 char buf[max_user_text];
2978 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2979 snprintf(p, p_max_size, "%s", buf);
2980 if(total_updates % 2) {
2981 for(j=0; p[j] != 0; j++) {
2987 char buf[max_user_text];
2989 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2991 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2992 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2993 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2994 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2995 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2996 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2997 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2999 snprintf(p, p_max_size, "%s", buf);
3002 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
3004 char buf[max_user_text];
3005 generate_text_internal(buf, max_user_text,
3007 for(j = 0; buf[j] != 0; j++) {
3009 case '\n': //place all the lines behind each other with LINESEPARATOR between them
3010 #define LINESEPARATOR '|'
3011 buf[j]=LINESEPARATOR;
3018 //no scrolling necessary if the length of the text to scroll is too short
3019 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
3020 snprintf(p, p_max_size, "%s", buf);
3023 //make sure a colorchange at the front is not part of the string we are going to show
3024 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
3025 obj->data.scroll.start++;
3027 //place all chars that should be visible in p, including colorchanges
3028 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
3029 p[j] = *(buf + obj->data.scroll.start + j);
3030 if(p[j] == SPECIAL_CHAR) {
3031 visibcolorchanges++;
3033 //if there is still room fill it with spaces
3036 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
3040 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
3041 for(j = 0; j < obj->data.scroll.start; j++) {
3042 if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
3044 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
3045 for(j = 0; j < frontcolorchanges; j++) {
3046 pwithcolors[j] = SPECIAL_CHAR;
3049 strcat(pwithcolors,p);
3050 strend = strlen(pwithcolors);
3051 //and place the colorchanges not in front or in the visible part behind the visible part
3052 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
3053 pwithcolors[strend + j] = SPECIAL_CHAR;
3055 pwithcolors[strend + j] = 0;
3056 strcpy(p, pwithcolors);
3059 obj->data.scroll.start += obj->data.scroll.step;
3060 if(buf[obj->data.scroll.start] == 0){
3061 obj->data.scroll.start = 0;
3064 //reset color when scroll is finished
3065 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
3069 char buf[2][max_user_text];
3076 struct llrows* next;
3078 struct llrows *ll_rows[2], *current[2];
3079 struct text_object * objsub = obj->sub;
3082 for(i=0; i<2; i++) {
3085 ll_rows[i] = malloc(sizeof(struct llrows));
3086 current[i] = ll_rows[i];
3087 for(j=0; j<i; j++) objsub = objsub->sub;
3088 generate_text_internal(buf[i], max_user_text, *objsub, cur);
3089 for(j=0; buf[i][j] != 0; j++) {
3090 if(buf[i][j] == '\t') buf[i][j] = ' ';
3091 if(buf[i][j] == '\n') {
3093 current[i]->row = strdup(buf[i]+nextstart);
3094 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3095 current[i]->next = malloc(sizeof(struct llrows));
3096 current[i] = current[i]->next;
3101 current[i]->row = strdup(buf[i]+nextstart);
3102 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3103 current[i]->next = NULL;
3104 current[i] = ll_rows[i];
3106 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
3108 strcat(p, current[0]->row);
3109 i=strlen(current[0]->row);
3111 while(i < longest) {
3116 strcat(p, obj->data.combine.seperation);
3117 strcat(p, current[1]->row);
3121 #pragma omp parallel for schedule(dynamic,10)
3122 #endif /* HAVE_OPENMP */
3123 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
3126 #pragma omp parallel for schedule(dynamic,10)
3127 #endif /* HAVE_OPENMP */
3128 for(i=0; i<2; i++) {
3129 while(ll_rows[i] != NULL) {
3130 current[i]=ll_rows[i];
3131 free(current[i]->row);
3132 ll_rows[i]=current[i]->next;
3139 int value = get_nvidia_value(obj->data.nvidia.type, display);
3141 snprintf(p, p_max_size, "N/A");
3142 else if (obj->data.nvidia.type == NV_TEMP)
3143 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
3144 else if (obj->data.nvidia.print_as_float &&
3145 value > 0 && value < 100)
3146 snprintf(p, p_max_size, "%.1f", (float)value);
3148 snprintf(p, p_max_size, "%d", value);
3153 /* This is just a meta-object to set host:port */
3156 snprintf(p, p_max_size, "%s",
3157 cur->apcupsd.items[APCUPSD_NAME]);
3159 OBJ(apcupsd_model) {
3160 snprintf(p, p_max_size, "%s",
3161 cur->apcupsd.items[APCUPSD_MODEL]);
3163 OBJ(apcupsd_upsmode) {
3164 snprintf(p, p_max_size, "%s",
3165 cur->apcupsd.items[APCUPSD_UPSMODE]);
3167 OBJ(apcupsd_cable) {
3168 snprintf(p, p_max_size, "%s",
3169 cur->apcupsd.items[APCUPSD_CABLE]);
3171 OBJ(apcupsd_status) {
3172 snprintf(p, p_max_size, "%s",
3173 cur->apcupsd.items[APCUPSD_STATUS]);
3175 OBJ(apcupsd_linev) {
3176 snprintf(p, p_max_size, "%s",
3177 cur->apcupsd.items[APCUPSD_LINEV]);
3180 snprintf(p, p_max_size, "%s",
3181 cur->apcupsd.items[APCUPSD_LOAD]);
3183 OBJ(apcupsd_loadbar) {
3186 if(output_methods & TO_X) {
3187 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3188 new_bar(p, obj->a, obj->b, (int) progress);
3191 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3192 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3193 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
3199 OBJ(apcupsd_loadgraph) {
3201 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3202 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3203 (int)progress, 100, 1, obj->char_a, obj->char_b);
3205 OBJ(apcupsd_loadgauge) {
3207 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3208 new_gauge(p, obj->a, obj->b,
3212 OBJ(apcupsd_charge) {
3213 snprintf(p, p_max_size, "%s",
3214 cur->apcupsd.items[APCUPSD_CHARGE]);
3216 OBJ(apcupsd_timeleft) {
3217 snprintf(p, p_max_size, "%s",
3218 cur->apcupsd.items[APCUPSD_TIMELEFT]);
3221 snprintf(p, p_max_size, "%s",
3222 cur->apcupsd.items[APCUPSD_TEMP]);
3224 OBJ(apcupsd_lastxfer) {
3225 snprintf(p, p_max_size, "%s",
3226 cur->apcupsd.items[APCUPSD_LASTXFER]);
3228 #endif /* APCUPSD */
3235 size_t a = strlen(p);
3238 iconv_convert(a, buff_in, p, p_max_size);
3239 #endif /* HAVE_ICONV */
3240 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
3241 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse) {
3242 substitute_newlines(p, a - 2);
3250 /* load any new fonts we may have had */
3251 if (need_to_load_fonts) {
3257 void evaluate(const char *text, char *buffer)
3259 struct information *tmp_info;
3260 struct text_object subroot;
3262 tmp_info = malloc(sizeof(struct information));
3263 memcpy(tmp_info, &info, sizeof(struct information));
3264 parse_conky_vars(&subroot, text, buffer, tmp_info);
3265 DBGP("evaluated '%s' to '%s'", text, buffer);
3267 free_text_objects(&subroot, 1);
3271 double current_update_time, next_update_time, last_update_time;
3273 static void generate_text(void)
3275 struct information *cur = &info;
3282 current_update_time = get_time();
3286 /* add things to the buffer */
3292 generate_text_internal(p, max_user_text, global_root_object, cur);
3294 if (stuff_in_uppercase) {
3297 tmp_p = text_buffer;
3299 *tmp_p = toupper(*tmp_p);
3304 next_update_time += update_interval;
3305 if (next_update_time < get_time()) {
3306 next_update_time = get_time() + update_interval;
3307 } else if (next_update_time > get_time() + update_interval) {
3308 next_update_time = get_time() + update_interval;
3310 last_update_time = current_update_time;
3314 void set_update_interval(double interval)
3316 update_interval = interval;
3317 update_interval_old = interval;
3320 static inline int get_string_width(const char *s)
3322 return *s ? calc_text_width(s) : 0;
3326 static int get_string_width_special(char *s, int special_index)
3336 if ((output_methods & TO_X) == 0)
3339 p = strndup(s, text_buffer_size);
3343 if (*p == SPECIAL_CHAR) {
3344 /* shift everything over by 1 so that the special char
3345 * doesn't mess up the size calculation */
3346 for (i = 0; i < (long)strlen(p); i++) {
3347 *(p + i) = *(p + i + 1);
3349 if (specials[special_index + idx].type == GRAPH
3350 || specials[special_index + idx].type == GAUGE
3351 || specials[special_index + idx].type == BAR) {
3352 width += specials[special_index + idx].width;
3355 } else if (*p == SECRIT_MULTILINE_CHAR) {
3362 if (strlen(final) > 1) {
3363 width += calc_text_width(final);
3369 static int text_size_updater(char *s, int special_index);
3371 int last_font_height;
3372 static void update_text_area(void)
3376 if ((output_methods & TO_X) == 0)
3378 /* update text size if it isn't fixed */
3383 text_width = minimum_width;
3385 last_font_height = font_height();
3386 for_each_line(text_buffer, text_size_updater);
3388 if (text_height < minimum_height) {
3389 text_height = minimum_height;
3391 if (text_width > maximum_width && maximum_width > 0) {
3392 text_width = maximum_width;
3396 /* get text position on workarea */
3397 switch (text_alignment) {
3404 x = workarea[2] - text_width - gap_x;
3409 x = workarea[2] / 2 - text_width / 2 - gap_x;
3416 y = workarea[3] - text_height - gap_y;
3420 x = workarea[2] - text_width - gap_x;
3421 y = workarea[3] - text_height - gap_y;
3425 x = workarea[2] / 2 - text_width / 2 - gap_x;
3426 y = workarea[3] - text_height - gap_y;
3431 y = workarea[3] / 2 - text_height / 2 - gap_y;
3435 x = workarea[2] - text_width - gap_x;
3436 y = workarea[3] / 2 - text_height / 2 - gap_y;
3440 case NONE: // Let the WM manage the window
3451 if (own_window && !fixed_pos) {
3454 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
3455 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
3456 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3457 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3461 /* If window size doesn't match to workarea's size,
3462 * then window probably includes panels (gnome).
3463 * Blah, doesn't work on KDE. */
3464 if (workarea[2] != window.width || workarea[3] != window.height) {
3473 /* update lua window globals */
3474 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3475 #endif /* HAVE_LUA */
3480 static int cur_x, cur_y; /* current x and y for drawing */
3482 //draw_mode also without X11 because we only need to print to stdout with FG
3483 static int draw_mode; /* FG, BG or OUTLINE */
3485 static long current_color;
3487 static int text_size_updater(char *s, int special_index)
3492 if ((output_methods & TO_X) == 0)
3494 /* get string widths and skip specials */
3497 if (*p == SPECIAL_CHAR) {
3499 w += get_string_width(s);
3502 if (specials[special_index].type == BAR
3503 || specials[special_index].type == GAUGE
3504 || specials[special_index].type == GRAPH) {
3505 w += specials[special_index].width;
3506 if (specials[special_index].height > last_font_height) {
3507 last_font_height = specials[special_index].height;
3508 last_font_height += font_height();
3510 } else if (specials[special_index].type == OFFSET) {
3511 if (specials[special_index].arg > 0) {
3512 w += specials[special_index].arg;
3514 } else if (specials[special_index].type == VOFFSET) {
3515 last_font_height += specials[special_index].arg;
3516 } else if (specials[special_index].type == GOTO) {
3517 if (specials[special_index].arg > cur_x) {
3518 w = (int) specials[special_index].arg;
3520 } else if (specials[special_index].type == TAB) {
3521 int start = specials[special_index].arg;
3522 int step = specials[special_index].width;
3524 if (!step || step < 0) {
3527 w += step - (cur_x - text_start_x - start) % step;
3528 } else if (specials[special_index].type == FONT) {
3529 selected_font = specials[special_index].font_added;
3530 if (font_height() > last_font_height) {
3531 last_font_height = font_height();
3537 } else if (*p == SECRIT_MULTILINE_CHAR) {
3540 lw = get_string_width(s);
3541 *p = SECRIT_MULTILINE_CHAR;
3543 w = lw > w ? lw : w;
3544 text_height += last_font_height;
3548 w += get_string_width(s);
3549 if (w > text_width) {
3552 if (text_width > maximum_width && maximum_width) {
3553 text_width = maximum_width;
3556 text_height += last_font_height;
3557 last_font_height = font_height();
3558 return special_index;
3562 static inline void set_foreground_color(long c)
3565 if (output_methods & TO_X) {
3567 XSetForeground(display, window.gc, c);
3571 if (output_methods & TO_NCURSES) {
3572 attron(COLOR_PAIR(c));
3574 #endif /* NCURSES */
3579 static void draw_string(const char *s)
3581 int i, i2, pos, width_of_s;
3584 char *s_with_newlines;
3590 width_of_s = get_string_width(s);
3591 s_with_newlines = strdup(s);
3592 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
3593 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
3594 s_with_newlines[i] = '\n';
3597 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
3598 printf("%s\n", s_with_newlines);
3599 if (extra_newline) fputc('\n', stdout);
3600 fflush(stdout); /* output immediately, don't buffer */
3602 if ((output_methods & TO_STDERR) && draw_mode == FG) {
3603 fprintf(stderr, "%s\n", s_with_newlines);
3604 fflush(stderr); /* output immediately, don't buffer */
3606 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
3607 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
3609 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
3610 fprintf(append_fpointer, "%s\n", s_with_newlines);
3613 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
3614 printw("%s", s_with_newlines);
3617 free(s_with_newlines);
3618 memset(tmpstring1, 0, text_buffer_size);
3619 memset(tmpstring2, 0, text_buffer_size);
3620 strncpy(tmpstring1, s, text_buffer_size - 1);
3625 if (output_methods & TO_X) {
3626 max = ((text_width - width_of_s) / get_string_width(" "));
3629 /* This code looks for tabs in the text and coverts them to spaces.
3630 * The trick is getting the correct number of spaces, and not going
3631 * over the window's size without forcing the window larger. */
3632 for (i = 0; i < (int) text_buffer_size; i++) {
3633 if (tmpstring1[i] == '\t') {
3635 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
3636 /* guard against overrun */
3637 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
3642 /* guard against overrun */
3643 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
3648 if (output_methods & TO_X) {
3649 if (text_width == maximum_width) {
3650 /* this means the text is probably pushing the limit,
3651 * so we'll chop it */
3652 while (cur_x + get_string_width(tmpstring2) - text_start_x
3653 > maximum_width && strlen(tmpstring2) > 0) {
3654 tmpstring2[strlen(tmpstring2) - 1] = '\0';
3661 if (output_methods & TO_X) {
3667 c.pixel = current_color;
3668 XQueryColor(display, DefaultColormap(display, screen), &c);
3671 c2.color.red = c.red;
3672 c2.color.green = c.green;
3673 c2.color.blue = c.blue;
3674 c2.color.alpha = fonts[selected_font].font_alpha;
3676 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3677 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3679 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3680 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3685 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
3688 cur_x += width_of_s;
3691 memcpy(tmpstring1, s, text_buffer_size);
3694 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
3702 int last_special_needed = -1;
3703 int orig_special_index = special_index;
3706 if (output_methods & TO_X) {
3707 font_h = font_height();
3708 cur_y += font_ascent();
3710 cur_x = text_start_x;
3714 if (*p == SECRIT_MULTILINE_CHAR) {
3715 /* special newline marker for multiline objects */
3720 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3725 /* draw string before special, unless we're dealing multiline
3727 if (last_special_applied > -1) {
3728 special_index = last_special_applied;
3736 switch (specials[special_index].type) {
3738 case HORIZONTAL_LINE:
3740 int h = specials[special_index].height;
3741 int mid = font_ascent() / 2;
3743 w = text_start_x + text_width - cur_x;
3745 XSetLineAttributes(display, window.gc, h, LineSolid,
3746 CapButt, JoinMiter);
3747 XDrawLine(display, window.drawable, window.gc, cur_x,
3748 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3754 int h = specials[special_index].height;
3755 int tmp_s = specials[special_index].arg;
3756 int mid = font_ascent() / 2;
3757 char ss[2] = { tmp_s, tmp_s };
3759 w = text_start_x + text_width - cur_x - 1;
3760 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3761 CapButt, JoinMiter);
3762 XSetDashes(display, window.gc, 0, ss, 2);
3763 XDrawLine(display, window.drawable, window.gc, cur_x,
3764 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3770 int h, bar_usage, by;
3771 if (cur_x - text_start_x > maximum_width
3772 && maximum_width > 0) {
3775 h = specials[special_index].height;
3776 bar_usage = specials[special_index].arg;
3777 by = cur_y - (font_ascent() / 2) - 1;
3782 w = specials[special_index].width;
3784 w = text_start_x + text_width - cur_x - 1;
3790 XSetLineAttributes(display, window.gc, 1, LineSolid,
3791 CapButt, JoinMiter);
3793 XDrawRectangle(display, window.drawable, window.gc, cur_x,
3795 XFillRectangle(display, window.drawable, window.gc, cur_x,
3796 by, w * bar_usage / 255, h);
3804 case GAUGE: /* new GAUGE */
3807 unsigned long last_colour = current_color;
3809 float angle, px, py;
3813 if (cur_x - text_start_x > maximum_width
3814 && maximum_width > 0) {
3818 h = specials[special_index].height;
3819 by = cur_y - (font_ascent() / 2) - 1;
3824 w = specials[special_index].width;
3826 w = text_start_x + text_width - cur_x - 1;
3832 XSetLineAttributes(display, window.gc, 1, LineSolid,
3833 CapButt, JoinMiter);
3835 XDrawArc(display, window.drawable, window.gc,
3836 cur_x, by, w, h * 2, 0, 180*64);
3839 usage = specials[special_index].arg;
3840 angle = (M_PI)*(float)(usage)/255.;
3841 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3842 py = (float)(by+(h))-(float)(h)*sin(angle);
3844 XDrawLine(display, window.drawable, window.gc,
3845 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3853 set_foreground_color(last_colour);
3861 int h, by, i = 0, j = 0;
3863 unsigned long last_colour = current_color;
3864 unsigned long *tmpcolour = 0;
3865 if (cur_x - text_start_x > maximum_width
3866 && maximum_width > 0) {
3869 h = specials[special_index].height;
3870 by = cur_y - (font_ascent() / 2) - 1;
3875 w = specials[special_index].width;
3877 w = text_start_x + text_width - cur_x - 1;
3882 if (draw_graph_borders) {
3883 XSetLineAttributes(display, window.gc, 1, LineSolid,
3884 CapButt, JoinMiter);
3885 XDrawRectangle(display, window.drawable, window.gc,
3888 XSetLineAttributes(display, window.gc, 1, LineSolid,
3889 CapButt, JoinMiter);
3891 if (specials[special_index].last_colour != 0
3892 || specials[special_index].first_colour != 0) {
3893 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3896 for (i = w - 2; i > -1; i--) {
3897 if (specials[special_index].last_colour != 0
3898 || specials[special_index].first_colour != 0) {
3899 if (specials[special_index].tempgrad) {
3902 (int)((float)(w - 2) - specials[special_index].graph[j] *
3903 (w - 2) / (float)specials[special_index].graph_scale)
3907 (int)((float)(w - 2) - specials[special_index].graph[j] *
3908 (w - 2) / (float)specials[special_index].graph_scale)
3911 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3913 (int)((float)(w - 2) - specials[special_index].graph[j] *
3914 (w - 2) / (float)specials[special_index].graph_scale)
3918 #endif /* DEBUG_lol */
3919 XSetForeground(display, window.gc, tmpcolour[
3920 (int)((float)(w - 2) - specials[special_index].graph[j] *
3921 (w - 2) / (float)specials[special_index].graph_scale)
3924 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3927 /* this is mugfugly, but it works */
3928 XDrawLine(display, window.drawable, window.gc,
3929 cur_x + i + 1, by + h, cur_x + i + 1,
3930 round_to_int((double)by + h - specials[special_index].graph[j] *
3931 (h - 1) / specials[special_index].graph_scale));
3932 if ((w - i) / ((float) (w - 2) /
3933 (specials[special_index].graph_width)) > j
3934 && j < MAX_GRAPH_DEPTH - 3) {
3938 if (tmpcolour) free(tmpcolour);
3943 /* if (draw_mode == BG) {
3944 set_foreground_color(default_bg_color);
3945 } else if (draw_mode == OUTLINE) {
3946 set_foreground_color(default_out_color);
3948 set_foreground_color(default_fg_color);
3950 if (show_graph_range) {
3953 unsigned short int seconds = update_interval * w;
3959 unsigned short int timeunits;
3961 timeunits = seconds / 86400; seconds %= 86400;
3962 if (timeunits > 0) {
3963 asprintf(&tmp_day_str, "%dd", timeunits);
3965 tmp_day_str = strdup("");
3967 timeunits = seconds / 3600; seconds %= 3600;
3968 if (timeunits > 0) {
3969 asprintf(&tmp_hour_str, "%dh", timeunits);
3971 tmp_hour_str = strdup("");
3973 timeunits = seconds / 60; seconds %= 60;
3974 if (timeunits > 0) {
3975 asprintf(&tmp_min_str, "%dm", timeunits);
3977 tmp_min_str = strdup("");
3980 asprintf(&tmp_sec_str, "%ds", seconds);
3982 tmp_sec_str = strdup("");
3984 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3985 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3987 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3989 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3990 cur_y += font_h / 2;
3991 draw_string(tmp_str);
3997 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
4001 cur_x += font_ascent() / 2;
4002 cur_y += font_h / 2;
4004 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
4006 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
4007 draw_string(tmp_str);
4013 set_foreground_color(last_colour);
4019 int old = font_ascent();
4021 cur_y -= font_ascent();
4022 selected_font = specials[special_index].font_added;
4024 if (cur_y + font_ascent() < cur_y + old) {
4027 cur_y += font_ascent();
4029 font_h = font_height();
4034 if (draw_mode == FG) {
4035 set_foreground_color(specials[special_index].arg);
4041 if (draw_mode == BG) {
4042 set_foreground_color(specials[special_index].arg);
4047 if (draw_mode == OUTLINE) {
4048 set_foreground_color(specials[special_index].arg);
4053 w += specials[special_index].arg;
4054 last_special_needed = special_index;
4058 cur_y += specials[special_index].arg;
4062 if (specials[special_index].arg >= 0) {
4063 cur_x = (int) specials[special_index].arg;
4065 last_special_needed = special_index;
4070 int start = specials[special_index].arg;
4071 int step = specials[special_index].width;
4073 if (!step || step < 0) {
4076 w = step - (cur_x - text_start_x - start) % step;
4077 last_special_needed = special_index;
4083 /* TODO: add back in "+ window.border_inner_margin" to the end of
4085 int pos_x = text_start_x + text_width -
4086 get_string_width_special(s, special_index);
4088 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4089 "get_string_width(p) %i gap_x %i "
4090 "specials[special_index].arg %i window.border_inner_margin %i "
4091 "window.border_width %i\n", pos_x, text_start_x, text_width,
4092 cur_x, get_string_width_special(s), gap_x,
4093 specials[special_index].arg, window.border_inner_margin,
4094 window.border_width); */
4095 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
4096 cur_x = pos_x - specials[special_index].arg;
4098 last_special_needed = special_index;
4104 int pos_x = (text_width) / 2 - get_string_width_special(s,
4105 special_index) / 2 - (cur_x -
4107 /* int pos_x = text_start_x + text_width / 2 -
4108 get_string_width_special(s) / 2; */
4110 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4111 "get_string_width(p) %i gap_x %i "
4112 "specials[special_index].arg %i\n", pos_x, text_start_x,
4113 text_width, cur_x, get_string_width(s), gap_x,
4114 specials[special_index].arg); */
4115 if (pos_x > specials[special_index].arg) {
4116 w = pos_x - specials[special_index].arg;
4118 last_special_needed = special_index;
4128 if (special_index != last_special_applied) {
4131 special_index = orig_special_index;
4132 last_special_applied = -1;
4143 if (output_methods & TO_NCURSES) {
4146 #endif /* NCURSES */
4148 if (output_methods & TO_X)
4149 cur_y += font_descent();
4151 if (recurse && *recurse) {
4152 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
4153 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
4155 return special_index;
4158 static int draw_line(char *s, int special_index)
4161 if (output_methods & TO_X) {
4162 return draw_each_line_inner(s, special_index, -1);
4166 if (output_methods & TO_NCURSES) {
4167 return draw_each_line_inner(s, special_index, -1);
4169 #endif /* NCURSES */
4171 UNUSED(special_index);
4175 static void draw_text(void)
4179 llua_draw_pre_hook();
4180 #endif /* HAVE_LUA */
4181 if (output_methods & TO_X) {
4182 cur_y = text_start_y;
4185 if (draw_borders && window.border_width > 0) {
4186 if (stippled_borders) {
4187 char ss[2] = { stippled_borders, stippled_borders };
4188 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
4189 CapButt, JoinMiter);
4190 XSetDashes(display, window.gc, 0, ss, 2);
4192 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
4193 CapButt, JoinMiter);
4196 XDrawRectangle(display, window.drawable, window.gc,
4197 text_start_x - window.border_inner_margin - window.border_width,
4198 text_start_y - window.border_inner_margin - window.border_width,
4199 text_width + window.border_inner_margin * 2 + window.border_width * 2,
4200 text_height + window.border_inner_margin * 2 + window.border_width * 2);
4208 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
4209 attron(COLOR_PAIR(COLOR_WHITE));
4210 #endif /* NCURSES */
4211 for_each_line(text_buffer, draw_line);
4212 #if defined(HAVE_LUA) && defined(X11)
4213 llua_draw_post_hook();
4214 #endif /* HAVE_LUA */
4217 static void draw_stuff(void)
4220 cimlib_render(text_start_x, text_start_y, window.width, window.height);
4222 if (overwrite_file) {
4223 overwrite_fpointer = fopen(overwrite_file, "w");
4224 if(!overwrite_fpointer)
4225 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
4228 append_fpointer = fopen(append_file, "a");
4229 if(!append_fpointer)
4230 NORM_ERR("Can't append '%s' anymore", append_file);
4233 if (output_methods & TO_X) {
4235 if (draw_shades && !draw_outline) {
4238 set_foreground_color(default_bg_color);
4249 for (i = -1; i < 2; i++) {
4250 for (j = -1; j < 2; j++) {
4251 if (i == 0 && j == 0) {
4256 set_foreground_color(default_out_color);
4257 draw_mode = OUTLINE;
4265 set_foreground_color(default_fg_color);
4270 #if defined(X11) && defined(HAVE_XDBE)
4271 if (output_methods & TO_X) {
4272 xdbe_swap_buffers();
4274 #endif /* X11 && HAVE_XDBE */
4275 if(overwrite_fpointer) {
4276 fclose(overwrite_fpointer);
4277 overwrite_fpointer = 0;
4279 if(append_fpointer) {
4280 fclose(append_fpointer);
4281 append_fpointer = 0;
4286 static void clear_text(int exposures)
4290 /* The swap action is XdbeBackground, which clears */
4294 if (display && window.window) { // make sure these are !null
4295 /* there is some extra space for borders and outlines */
4296 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4297 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4298 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4299 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
4304 static int need_to_update;
4306 /* update_text() generates new text and clears old text area */
4307 static void update_text(void)
4314 if (output_methods & TO_X)
4319 llua_update_info(&info, update_interval);
4320 #endif /* HAVE_LUA */
4323 #ifdef HAVE_SYS_INOTIFY_H
4327 static void main_loop(void)
4330 #ifdef SIGNAL_BLOCKING
4331 sigset_t newmask, oldmask;
4334 #ifdef HAVE_SYS_INOTIFY_H
4335 int inotify_config_wd = -1;
4336 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
4337 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
4338 char inotify_buff[INOTIFY_BUF_LEN];
4339 #endif /* HAVE_SYS_INOTIFY_H */
4342 #ifdef SIGNAL_BLOCKING
4343 sigemptyset(&newmask);
4344 sigaddset(&newmask, SIGINT);
4345 sigaddset(&newmask, SIGTERM);
4346 sigaddset(&newmask, SIGUSR1);
4349 last_update_time = 0.0;
4350 next_update_time = get_time();
4352 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
4353 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
4354 char buf[max_user_text];
4356 get_battery_short_status(buf, max_user_text, "BAT0");
4358 update_interval = update_interval_bat;
4360 update_interval = update_interval_old;
4365 #ifdef SIGNAL_BLOCKING
4366 /* block signals. we will inspect for pending signals later */
4367 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
4368 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4373 if (output_methods & TO_X) {
4376 /* wait for X event or timeout */
4378 if (!XPending(display)) {
4382 t = next_update_time - get_time();
4386 } else if (t > update_interval) {
4387 t = update_interval;
4390 tv.tv_sec = (long) t;
4391 tv.tv_usec = (long) (t * 1000000) % 1000000;
4393 FD_SET(ConnectionNumber(display), &fdsr);
4395 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
4397 if (errno != EINTR) {
4398 NORM_ERR("can't select(): %s", strerror(errno));
4408 if (need_to_update) {
4410 int wx = window.x, wy = window.y;
4420 /* resize window if it isn't right size */
4422 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
4423 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
4424 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4425 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4426 draw_stuff(); /* redraw everything in our newly sized window */
4427 XResizeWindow(display, window.window, window.width,
4428 window.height); /* resize window */
4429 set_transparent_background(window.window);
4432 xdbe_swap_buffers();
4437 /* update lua window globals */
4438 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
4439 #endif /* HAVE_LUA */
4442 /* move window if it isn't in right position */
4443 if (!fixed_pos && (window.x != wx || window.y != wy)) {
4444 XMoveWindow(display, window.window, window.x, window.y);
4449 if (changed && window.type == TYPE_PANEL) {
4452 fprintf(stderr, PACKAGE_NAME": defining struts\n");
4455 switch (text_alignment) {
4482 set_struts(sidenum);
4493 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4494 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4495 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4496 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4497 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4502 /* handle X events */
4503 while (XPending(display)) {
4506 XNextEvent(display, &ev);
4513 r.width = ev.xexpose.width;
4514 r.height = ev.xexpose.height;
4515 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4519 case PropertyNotify:
4521 if ( ev.xproperty.state == PropertyNewValue ) {
4522 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
4528 case ReparentNotify:
4529 /* set background to ParentRelative for all parents */
4531 set_transparent_background(window.window);
4535 case ConfigureNotify:
4537 /* if window size isn't what expected, set fixed size */
4538 if (ev.xconfigure.width != window.width
4539 || ev.xconfigure.height != window.height) {
4540 if (window.width != 0 && window.height != 0) {
4544 /* clear old stuff before screwing up
4549 XWindowAttributes attrs;
4550 if (XGetWindowAttributes(display,
4551 window.window, &attrs)) {
4552 window.width = attrs.width;
4553 window.height = attrs.height;
4557 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4558 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4559 if (text_width > maximum_width
4560 && maximum_width > 0) {
4561 text_width = maximum_width;
4565 /* if position isn't what expected, set fixed pos
4566 * total_updates avoids setting fixed_pos when window
4567 * is set to weird locations when started */
4568 /* // this is broken
4569 if (total_updates >= 2 && !fixed_pos
4570 && (window.x != ev.xconfigure.x
4571 || window.y != ev.xconfigure.y)
4572 && (ev.xconfigure.x != 0
4573 || ev.xconfigure.y != 0)) {
4581 /* if an ordinary window with decorations */
4582 if ((window.type == TYPE_NORMAL &&
4583 (!TEST_HINT(window.hints,
4584 HINT_UNDECORATED))) ||
4585 window.type == TYPE_DESKTOP) {
4586 /* allow conky to hold input focus. */
4589 /* forward the click to the desktop window */
4590 XUngrabPointer(display, ev.xbutton.time);
4591 ev.xbutton.window = window.desktop;
4592 ev.xbutton.x = ev.xbutton.x_root;
4593 ev.xbutton.y = ev.xbutton.y_root;
4594 XSendEvent(display, ev.xbutton.window, False,
4595 ButtonPressMask, &ev);
4596 XSetInputFocus(display, ev.xbutton.window,
4597 RevertToParent, ev.xbutton.time);
4604 /* if an ordinary window with decorations */
4605 if ((window.type == TYPE_NORMAL)
4606 && (!TEST_HINT(window.hints,
4607 HINT_UNDECORATED))) {
4608 /* allow conky to hold input focus. */
4611 /* forward the release to the desktop window */
4612 ev.xbutton.window = window.desktop;
4613 ev.xbutton.x = ev.xbutton.x_root;
4614 ev.xbutton.y = ev.xbutton.y_root;
4615 XSendEvent(display, ev.xbutton.window, False,
4616 ButtonReleaseMask, &ev);
4625 if (ev.type == x11_stuff.event_base + XDamageNotify) {
4626 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
4628 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
4629 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
4631 #endif /* HAVE_XDAMAGE */
4637 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
4638 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
4639 #endif /* HAVE_XDAMAGE */
4641 /* XDBE doesn't seem to provide a way to clear the back buffer
4642 * without interfering with the front buffer, other than passing
4643 * XdbeBackground to XdbeSwapBuffers. That means that if we're
4644 * using XDBE, we need to redraw the text even if it wasn't part of
4645 * the exposed area. OTOH, if we're not going to call draw_stuff at
4646 * all, then no swap happens and we can safely do nothing. */
4648 if (!XEmptyRegion(x11_stuff.region)) {
4653 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4654 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4655 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4656 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4657 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4660 XSetRegion(display, window.gc, x11_stuff.region);
4663 XftDrawSetClip(window.xftdraw, x11_stuff.region);
4667 XDestroyRegion(x11_stuff.region);
4668 x11_stuff.region = XCreateRegion();
4672 t = (next_update_time - get_time()) * 1000000;
4673 if(t > 0) usleep((useconds_t)t);
4677 if(output_methods & TO_NCURSES) {
4686 #ifdef SIGNAL_BLOCKING
4687 /* unblock signals of interest and let handler fly */
4688 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4689 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4693 switch (g_signal_pending) {
4696 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4701 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4704 if (output_methods & TO_X) {
4705 XDestroyRegion(x11_stuff.region);
4706 x11_stuff.region = NULL;
4708 XDamageDestroy(display, x11_stuff.damage);
4709 XFixesDestroyRegion(display, x11_stuff.region2);
4710 XFixesDestroyRegion(display, x11_stuff.part);
4711 #endif /* HAVE_XDAMAGE */
4717 if(overwrite_file) {
4718 free(overwrite_file);
4727 /* Reaching here means someone set a signal
4728 * (SIGXXXX, signal_handler), but didn't write any code
4730 * If you don't want to handle a signal, don't set a handler on
4731 * it in the first place. */
4732 if (g_signal_pending) {
4733 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4737 #ifdef HAVE_SYS_INOTIFY_H
4738 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4739 inotify_config_wd = inotify_add_watch(inotify_fd,
4743 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4744 int len = 0, idx = 0;
4746 struct timeval time_to_wait;
4748 FD_ZERO(&descriptors);
4749 FD_SET(inotify_fd, &descriptors);
4751 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4753 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4754 if (FD_ISSET(inotify_fd, &descriptors)) {
4755 /* process inotify events */
4756 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4757 while (len > 0 && idx < len) {
4758 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4759 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4760 /* current_config should be reloaded */
4761 NORM_ERR("'%s' modified, reloading...", current_config);
4763 if (ev->mask & IN_IGNORED) {
4764 /* for some reason we get IN_IGNORED here
4765 * sometimes, so we need to re-add the watch */
4766 inotify_config_wd = inotify_add_watch(inotify_fd,
4773 llua_inotify_query(ev->wd, ev->mask);
4775 #endif /* HAVE_LUA */
4776 idx += INOTIFY_EVENT_SIZE + ev->len;
4780 #endif /* HAVE_SYS_INOTIFY_H */
4783 llua_update_info(&info, update_interval);
4784 #endif /* HAVE_LUA */
4785 g_signal_pending = 0;
4787 clean_up(NULL, NULL);
4789 #ifdef HAVE_SYS_INOTIFY_H
4790 if (inotify_fd != -1) {
4791 inotify_rm_watch(inotify_fd, inotify_config_wd);
4793 inotify_fd = inotify_config_wd = 0;
4795 #endif /* HAVE_SYS_INOTIFY_H */
4799 static void load_config_file_x11(const char *);
4801 void initialisation(int argc, char** argv);
4803 /* reload the config file */
4804 static void reload_config(void)
4806 char *current_config_copy = strdup(current_config);
4807 clean_up(NULL, NULL);
4808 current_config = current_config_copy;
4809 initialisation(argc_copy, argv_copy);
4812 void clean_up(void *memtofree1, void* memtofree2)
4817 if(output_methods & TO_NCURSES) {
4821 conftree_empty(currentconffile);
4822 currentconffile = NULL;
4829 timed_thread_destroy_registered_threads();
4831 if (info.cpu_usage) {
4832 free(info.cpu_usage);
4833 info.cpu_usage = NULL;
4836 if (x_initialised == YES) {
4837 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4838 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4839 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4840 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4843 if(x11_stuff.region) {
4844 XDestroyRegion(x11_stuff.region);
4845 x11_stuff.region = NULL;
4847 XCloseDisplay(display);
4849 if(info.x11.desktop.all_names) {
4850 free(info.x11.desktop.all_names);
4851 info.x11.desktop.all_names = NULL;
4853 if (info.x11.desktop.name) {
4854 free(info.x11.desktop.name);
4855 info.x11.desktop.name = NULL;
4859 free(fonts); //in set_default_configurations a font is set but not loaded
4865 free_update_callbacks();
4869 free_text_objects(&global_root_object, 0);
4888 free(current_config);
4891 #ifdef TCP_PORT_MONITOR
4892 tcp_portmon_clear();
4901 weather_free_info();
4904 llua_shutdown_hook();
4906 #endif /* HAVE_LUA */
4908 if (output_methods & TO_X)
4916 for (i = 0; i < special_count; i++) {
4917 if (specials[i].type == GRAPH) {
4918 free(specials[i].graph);
4926 clear_diskio_stats();
4927 if(global_cpu != NULL) {
4933 static int string_to_bool(const char *s)
4936 // Assumes an option without a true/false means true
4938 } else if (strcasecmp(s, "yes") == EQUAL) {
4940 } else if (strcasecmp(s, "true") == EQUAL) {
4942 } else if (strcasecmp(s, "1") == EQUAL) {
4949 static enum alignment string_to_alignment(const char *s)
4951 if (strcasecmp(s, "top_left") == EQUAL) {
4953 } else if (strcasecmp(s, "top_right") == EQUAL) {
4955 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4957 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4959 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4960 return BOTTOM_RIGHT;
4961 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4962 return BOTTOM_MIDDLE;
4963 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4965 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4966 return MIDDLE_RIGHT;
4967 } else if (strcasecmp(s, "tl") == EQUAL) {
4969 } else if (strcasecmp(s, "tr") == EQUAL) {
4971 } else if (strcasecmp(s, "tm") == EQUAL) {
4973 } else if (strcasecmp(s, "bl") == EQUAL) {
4975 } else if (strcasecmp(s, "br") == EQUAL) {
4976 return BOTTOM_RIGHT;
4977 } else if (strcasecmp(s, "bm") == EQUAL) {
4978 return BOTTOM_MIDDLE;
4979 } else if (strcasecmp(s, "ml") == EQUAL) {
4981 } else if (strcasecmp(s, "mr") == EQUAL) {
4982 return MIDDLE_RIGHT;
4983 } else if (strcasecmp(s, "none") == EQUAL) {
4991 static void set_default_configurations_for_x(void)
4993 default_fg_color = WhitePixel(display, screen);
4994 default_bg_color = BlackPixel(display, screen);
4995 default_out_color = BlackPixel(display, screen);
4996 color0 = default_fg_color;
4997 color1 = default_fg_color;
4998 color2 = default_fg_color;
4999 color3 = default_fg_color;
5000 color4 = default_fg_color;
5001 color5 = default_fg_color;
5002 color6 = default_fg_color;
5003 color7 = default_fg_color;
5004 color8 = default_fg_color;
5005 color9 = default_fg_color;
5006 current_text_color = default_fg_color;
5010 static void set_default_configurations(void)
5017 fork_to_background = 0;
5018 total_run_times = 0;
5019 info.cpu_avg_samples = 2;
5020 info.net_avg_samples = 2;
5021 info.diskio_avg_samples = 2;
5026 format_human_readable = 1;
5036 mpd_env_host = getenv("MPD_HOST");
5037 mpd_env_port = getenv("MPD_PORT");
5039 if (!mpd_env_host || !strlen(mpd_env_host)) {
5040 mpd_set_host("localhost");
5042 /* MPD_HOST environment variable is set */
5043 char *mpd_hostpart = strchr(mpd_env_host, '@');
5044 if (!mpd_hostpart) {
5045 mpd_set_host(mpd_env_host);
5047 /* MPD_HOST contains a password */
5048 char mpd_password[mpd_hostpart - mpd_env_host + 1];
5049 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
5051 if (!strlen(mpd_hostpart + 1)) {
5052 mpd_set_host("localhost");
5054 mpd_set_host(mpd_hostpart + 1);
5057 mpd_set_password(mpd_password, 1);
5062 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
5063 /* failed to set port from environment variable */
5064 mpd_set_port("6600");
5068 info.xmms2.artist = NULL;
5069 info.xmms2.album = NULL;
5070 info.xmms2.title = NULL;
5071 info.xmms2.genre = NULL;
5072 info.xmms2.comment = NULL;
5073 info.xmms2.url = NULL;
5074 info.xmms2.status = NULL;
5075 info.xmms2.playlist = NULL;
5077 use_spacer = NO_SPACER;
5079 output_methods = TO_X;
5081 output_methods = TO_STDOUT;
5084 show_graph_scale = 0;
5085 show_graph_range = 0;
5088 draw_graph_borders = 1;
5090 set_first_font("6x10");
5098 window.type = TYPE_NORMAL;
5100 strcpy(window.class_name, PACKAGE_NAME);
5101 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
5103 stippled_borders = 0;
5104 window.border_inner_margin = 3;
5105 window.border_outer_margin = 1;
5106 window.border_width = 1;
5107 text_alignment = BOTTOM_LEFT;
5108 info.x11.monitor.number = 1;
5109 info.x11.monitor.current = 0;
5110 info.x11.desktop.current = 1;
5111 info.x11.desktop.number = 1;
5112 info.x11.desktop.nitems = 0;
5113 info.x11.desktop.all_names = NULL;
5114 info.x11.desktop.name = NULL;
5119 free(current_mail_spool);
5123 variable_substitute(MAIL_FILE, buf, 256);
5124 if (buf[0] != '\0') {
5125 current_mail_spool = strndup(buf, text_buffer_size);
5130 set_update_interval(3);
5131 update_interval_bat = NOBATTERY;
5132 info.music_player_interval = 1.0;
5133 stuff_in_uppercase = 0;
5134 info.users.number = 1;
5136 #ifdef TCP_PORT_MONITOR
5137 /* set default connection limit */
5138 tcp_portmon_set_max_connections(0);
5142 /* returns 1 if you can overwrite or create the file at 'path' */
5143 static _Bool overwrite_works(const char *path)
5147 if (!(filepointer = fopen(path, "w")))
5149 fclose(filepointer);
5153 /* returns 1 if you can append or create the file at 'path' */
5154 static _Bool append_works(const char *path)
5158 if (!(filepointer = fopen(path, "a")))
5160 fclose(filepointer);
5166 /* WARNING, this type not in Xlib spec */
5167 int x11_error_handler(Display *d, XErrorEvent *err)
5168 __attribute__((noreturn));
5169 int x11_error_handler(Display *d, XErrorEvent *err)
5171 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",
5173 (long unsigned)err->display,
5174 (long)err->resourceid,
5184 int x11_ioerror_handler(Display *d)
5185 __attribute__((noreturn));
5186 int x11_ioerror_handler(Display *d)
5188 NORM_ERR("X Error: Display %lx\n",
5195 static void X11_initialisation(void)
5197 if (x_initialised == YES) return;
5198 output_methods |= TO_X;
5200 set_default_configurations_for_x();
5201 x_initialised = YES;
5204 /* WARNING, this type not in Xlib spec */
5205 XSetErrorHandler(&x11_error_handler);
5206 XSetIOErrorHandler(&x11_ioerror_handler);
5210 static char **xargv = 0;
5211 static int xargc = 0;
5213 static void X11_create_window(void)
5215 if (output_methods & TO_X) {
5217 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5218 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
5220 #else /* OWN_WINDOW */
5221 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5222 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
5224 #endif /* OWN_WINDOW */
5228 update_text_area(); /* to position text/window on screen */
5231 if (own_window && !fixed_pos) {
5232 XMoveWindow(display, window.window, window.x, window.y);
5235 set_transparent_background(window.window);
5243 x11_stuff.region = XCreateRegion();
5245 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
5246 NORM_ERR("Xdamage extension unavailable");
5248 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5249 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5250 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
5251 #endif /* HAVE_XDAMAGE */
5254 update_text_area(); /* to get initial size of the window */
5257 /* setup lua window globals */
5258 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
5259 #endif /* HAVE_LUA */
5263 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
5264 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
5265 #define CONF2(a) if (strcasecmp(name, a) == 0)
5266 #define CONF(a) else CONF2(a)
5267 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
5268 || strcasecmp(name, b) == 0)
5269 #define CONF_CONTINUE 1
5270 #define CONF_BREAK 2
5271 #define CONF_BUFF_SIZE 512
5273 static FILE *open_config_file(const char *f)
5275 #ifdef CONFIG_OUTPUT
5276 if (!strcmp(f, "==builtin==")) {
5277 return conf_cookie_open();
5279 #endif /* CONFIG_OUTPUT */
5280 return fopen(f, "r");
5283 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
5287 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5290 remove_comments(buf);
5295 while (*p && isspace((int) *p)) {
5299 return CONF_CONTINUE; /* empty line */
5306 while (*p2 && !isspace((int) *p2)) {
5310 *p2 = '\0'; /* break at name's end */
5317 while (*p && isspace((int) *p)) {
5323 p2 = *value + strlen(*value);
5324 while (isspace((int) *(p2 - 1))) {
5333 char load_config_file(const char *f)
5338 fp = open_config_file(f);
5342 DBGP("reading contents from config file '%s'", f);
5345 char buff[CONF_BUFF_SIZE], *name, *value;
5346 int ret = do_config_step(&line, fp, buff, &name, &value);
5347 if (ret == CONF_BREAK) {
5349 } else if (ret == CONF_CONTINUE) {
5355 /* don't listen if X is already initialised or
5356 * if we already know we don't want it */
5357 if(x_initialised != YES) {
5358 if (string_to_bool(value)) {
5359 output_methods &= TO_X;
5361 output_methods &= ~TO_X;
5362 x_initialised = NEVER;
5367 if (!value || x_initialised == YES) {
5372 disp = strdup(value);
5377 if (window.type == TYPE_DOCK)
5380 #endif /*OWN_WINDOW */
5382 int a = string_to_alignment(value);
5393 CONF("background") {
5394 fork_to_background = string_to_bool(value);
5397 CONF2("background") {
5398 fork_to_background = string_to_bool(value);
5402 CONF("show_graph_scale") {
5403 show_graph_scale = string_to_bool(value);
5405 CONF("show_graph_range") {
5406 show_graph_range = string_to_bool(value);
5408 CONF("border_inner_margin") {
5410 window.border_inner_margin = strtol(value, 0, 0);
5411 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
5416 CONF("border_outer_margin") {
5418 window.border_outer_margin = strtol(value, 0, 0);
5419 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
5424 CONF("border_width") {
5426 window.border_width = strtol(value, 0, 0);
5427 if (window.border_width < 0) window.border_width = 0;
5433 #define TEMPLATE_CONF(n) \
5434 CONF("template"#n) { \
5435 if (set_template(n, value)) \
5450 info.mail = parse_mail_args(IMAP_TYPE, value);
5457 info.mail = parse_mail_args(POP3_TYPE, value);
5462 CONF("default_bar_size") {
5465 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
5472 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
5476 CONF("default_graph_size") {
5479 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
5486 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
5489 CONF("default_gauge_size") {
5492 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
5499 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
5506 mpd_set_host(value);
5512 if (value && mpd_set_port(value)) {
5516 CONF("mpd_password") {
5518 mpd_set_password(value, 0);
5524 CONF("music_player_interval") {
5526 info.music_player_interval = strtod(value, 0);
5532 CONF("sensor_device") {
5534 sensor_device = strtol(value, 0, 0);
5540 CONF("cpu_avg_samples") {
5542 cpu_avg_samples = strtol(value, 0, 0);
5543 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
5546 info.cpu_avg_samples = cpu_avg_samples;
5552 CONF("net_avg_samples") {
5554 net_avg_samples = strtol(value, 0, 0);
5555 if (net_avg_samples < 1 || net_avg_samples > 14) {
5558 info.net_avg_samples = net_avg_samples;
5564 CONF("diskio_avg_samples") {
5566 diskio_avg_samples = strtol(value, 0, 0);
5567 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
5570 info.diskio_avg_samples = diskio_avg_samples;
5578 CONF("double_buffer") {
5579 use_xdbe = string_to_bool(value);
5583 CONF("override_utf8_locale") {
5584 utf8_mode = string_to_bool(value);
5586 CONF("draw_borders") {
5587 draw_borders = string_to_bool(value);
5589 CONF("draw_graph_borders") {
5590 draw_graph_borders = string_to_bool(value);
5592 CONF("draw_shades") {
5593 draw_shades = string_to_bool(value);
5595 CONF("draw_outline") {
5596 draw_outline = string_to_bool(value);
5599 CONF("out_to_console") {
5600 if(string_to_bool(value)) {
5601 output_methods |= TO_STDOUT;
5603 output_methods &= ~TO_STDOUT;
5606 CONF("extra_newline") {
5607 extra_newline = string_to_bool(value);
5609 CONF("out_to_stderr") {
5610 if(string_to_bool(value))
5611 output_methods |= TO_STDERR;
5614 CONF("out_to_ncurses") {
5615 if(string_to_bool(value)) {
5618 output_methods |= TO_NCURSES;
5622 CONF("overwrite_file") {
5623 if(overwrite_file) {
5624 free(overwrite_file);
5627 if(overwrite_works(value)) {
5628 overwrite_file = strdup(value);
5629 output_methods |= OVERWRITE_FILE;
5631 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
5633 CONF("append_file") {
5638 if(append_works(value)) {
5639 append_file = strdup(value);
5640 output_methods |= APPEND_FILE;
5642 NORM_ERR("append_file won't be able to create/append '%s'", value);
5644 CONF("use_spacer") {
5646 if (strcasecmp(value, "left") == EQUAL) {
5647 use_spacer = LEFT_SPACER;
5648 } else if (strcasecmp(value, "right") == EQUAL) {
5649 use_spacer = RIGHT_SPACER;
5650 } else if (strcasecmp(value, "none") == EQUAL) {
5651 use_spacer = NO_SPACER;
5653 use_spacer = string_to_bool(value);
5654 NORM_ERR("use_spacer should have an argument of left, right, or"
5655 " none. '%s' seems to be some form of '%s', so"
5656 " defaulting to %s.", value,
5657 use_spacer ? "true" : "false",
5658 use_spacer ? "right" : "none");
5660 use_spacer = RIGHT_SPACER;
5662 use_spacer = NO_SPACER;
5666 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5667 use_spacer = RIGHT_SPACER;
5673 use_xft = string_to_bool(value);
5677 set_first_font(value);
5681 if (value && font_count >= 0) {
5682 fonts[0].font_alpha = atof(value) * 65535.0;
5689 if (string_to_bool(value)) {
5690 NORM_ERR("Xft not enabled at compile time");
5694 /* xftfont silently ignored when no Xft */
5697 /* xftalpha is silently ignored when no Xft */
5702 set_first_font(value);
5710 gap_x = atoi(value);
5717 gap_y = atoi(value);
5723 CONF("mail_spool") {
5727 variable_substitute(value, buffer, 256);
5729 if (buffer[0] != '\0') {
5730 if (current_mail_spool) {
5731 free(current_mail_spool);
5733 current_mail_spool = strndup(buffer, text_buffer_size);
5740 CONF("minimum_size") {
5742 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5744 if (sscanf(value, "%d", &minimum_width) != 1) {
5752 CONF("maximum_width") {
5754 if (sscanf(value, "%d", &maximum_width) != 1) {
5762 CONF("no_buffers") {
5763 no_buffers = string_to_bool(value);
5765 CONF("top_name_width") {
5767 if (sscanf(value, "%u", &top_name_width) != 1) {
5773 if (top_name_width >= max_user_text) {
5774 top_name_width = max_user_text - 1;
5777 CONF("top_cpu_separate") {
5778 cpu_separate = string_to_bool(value);
5780 CONF("short_units") {
5781 short_units = string_to_bool(value);
5783 CONF("format_human_readable") {
5784 format_human_readable = string_to_bool(value);
5787 CONF("hddtemp_host") {
5788 set_hddtemp_host(value);
5790 CONF("hddtemp_port") {
5791 set_hddtemp_port(value);
5793 #endif /* HDDTEMP */
5794 CONF("pad_percents") {
5795 pad_percents = atoi(value);
5799 CONF("own_window") {
5801 own_window = string_to_bool(value);
5804 CONF("own_window_class") {
5806 memset(window.class_name, 0, sizeof(window.class_name));
5807 strncpy(window.class_name, value,
5808 sizeof(window.class_name) - 1);
5811 CONF("own_window_title") {
5813 memset(window.title, 0, sizeof(window.title));
5814 strncpy(window.title, value, sizeof(window.title) - 1);
5817 CONF("own_window_transparent") {
5819 set_transparent = string_to_bool(value);
5822 CONF("own_window_hints") {
5824 char *p_hint, *p_save;
5825 char delim[] = ", ";
5827 /* tokenize the value into individual hints */
5828 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5830 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5831 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5832 SET_HINT(window.hints, HINT_UNDECORATED);
5833 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5834 SET_HINT(window.hints, HINT_BELOW);
5835 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5836 SET_HINT(window.hints, HINT_ABOVE);
5837 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5838 SET_HINT(window.hints, HINT_STICKY);
5839 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5840 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5841 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5842 SET_HINT(window.hints, HINT_SKIP_PAGER);
5847 p_hint = strtok_r(NULL, delim, &p_save);
5848 } while (p_hint != NULL);
5854 CONF("own_window_type") {
5856 if (strncmp(value, "normal", 6) == EQUAL) {
5857 window.type = TYPE_NORMAL;
5858 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5859 window.type = TYPE_DESKTOP;
5860 } else if (strncmp(value, "dock", 4) == EQUAL) {
5861 window.type = TYPE_DOCK;
5862 text_alignment = TOP_LEFT;
5863 } else if (strncmp(value, "panel", 5) == EQUAL) {
5864 window.type = TYPE_PANEL;
5865 } else if (strncmp(value, "override", 8) == EQUAL) {
5866 window.type = TYPE_OVERRIDE;
5875 CONF("stippled_borders") {
5877 stippled_borders = strtol(value, 0, 0);
5879 stippled_borders = 4;
5883 CONF("imlib_cache_size") {
5885 cimlib_set_cache_size(atoi(value));
5888 CONF("imlib_cache_flush_interval") {
5890 cimlib_set_cache_flush_interval(atoi(value));
5895 CONF("update_interval_on_battery") {
5897 update_interval_bat = strtod(value, 0);
5902 CONF("update_interval") {
5904 set_update_interval(strtod(value, 0));
5908 if (info.music_player_interval == 0) {
5909 // default to update_interval
5910 info.music_player_interval = update_interval;
5913 CONF("total_run_times") {
5915 total_run_times = strtod(value, 0);
5921 stuff_in_uppercase = string_to_bool(value);
5923 CONF("max_specials") {
5925 max_specials = atoi(value);
5930 CONF("max_user_text") {
5932 max_user_text = atoi(value);
5937 CONF("text_buffer_size") {
5939 text_buffer_size = atoi(value);
5940 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5941 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5942 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5950 if (output_methods & TO_X) {
5951 X11_initialisation();
5960 global_text = (char *) malloc(1);
5961 global_text[0] = '\0';
5964 unsigned int l = strlen(global_text);
5966 char buf[CONF_BUFF_SIZE];
5968 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5974 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5982 /* Check for continuation of \\-\n. */
5983 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5984 global_text[l-1] = '\0';
5988 global_text = (char *) realloc(global_text, l + bl + 1);
5989 strcat(global_text, buf);
5991 if (strlen(global_text) > max_user_text) {
5995 global_text_lines = line + 1;
5998 #ifdef TCP_PORT_MONITOR
5999 CONF("max_port_monitor_connections") {
6001 if (!value || (sscanf(value, "%d", &max) != 1)) {
6002 /* an error. use default, warn and continue. */
6003 tcp_portmon_set_max_connections(0);
6005 } else if (tcp_portmon_set_max_connections(max)) {
6006 /* max is < 0, default has been set*/
6011 CONF("if_up_strictness") {
6013 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6014 ifup_strictness = IFUP_UP;
6015 } else if (strcasecmp(value, "up") == EQUAL) {
6016 ifup_strictness = IFUP_UP;
6017 } else if (strcasecmp(value, "link") == EQUAL) {
6018 ifup_strictness = IFUP_LINK;
6019 } else if (strcasecmp(value, "address") == EQUAL) {
6020 ifup_strictness = IFUP_ADDR;
6022 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6023 ifup_strictness = IFUP_UP;
6027 CONF("temperature_unit") {
6029 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
6030 } else if (set_temp_output_unit(value)) {
6031 NORM_ERR("temperature_unit: incorrect argument");
6038 char *ptr = strtok(value, " ");
6041 ptr = strtok(NULL, " ");
6048 CONF("lua_draw_hook_pre") {
6050 llua_set_draw_pre_hook(value);
6055 CONF("lua_draw_hook_post") {
6057 llua_set_draw_post_hook(value);
6062 CONF("lua_startup_hook") {
6064 llua_set_startup_hook(value);
6069 CONF("lua_shutdown_hook") {
6071 llua_set_shutdown_hook(value);
6077 #endif /* HAVE_LUA */
6089 CONF("default_color"){}
6090 CONF3("default_shade_color", "default_shadecolor"){}
6091 CONF3("default_outline_color", "default_outlinecolor") {}
6092 CONF("own_window_colour") {}
6095 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
6101 if (info.music_player_interval == 0) {
6102 // default to update_interval
6103 info.music_player_interval = update_interval;
6105 if (!global_text) { // didn't supply any text
6106 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
6108 if (!output_methods) {
6109 CRIT_ERR(0, 0, "no output_methods have been selected; exiting");
6111 #if defined(NCURSES)
6113 if ((output_methods & TO_X) && (output_methods & TO_NCURSES)) {
6114 NORM_ERR("out_to_x and out_to_ncurses are incompatible, turning out_to_ncurses off");
6115 output_methods &= ~TO_NCURSES;
6119 if ((output_methods & (TO_STDOUT | TO_STDERR)) && (output_methods & TO_NCURSES)) {
6120 NORM_ERR("out_to_ncurses conflicts with out_to_console and out_to_stderr, disabling the later ones");
6121 output_methods &= ~(TO_STDOUT | TO_STDERR);
6123 #endif /* NCURSES */
6128 static void load_config_file_x11(const char *f)
6133 fp = open_config_file(f);
6137 DBGP("reading contents from config file '%s'", f);
6140 char buff[CONF_BUFF_SIZE], *name, *value;
6141 int ret = do_config_step(&line, fp, buff, &name, &value);
6142 if (ret == CONF_BREAK) {
6144 } else if (ret == CONF_CONTINUE) {
6149 X11_initialisation();
6150 if (x_initialised == YES) {
6152 color0 = get_x11_color(value);
6159 X11_initialisation();
6160 if (x_initialised == YES) {
6162 color1 = get_x11_color(value);
6169 X11_initialisation();
6170 if (x_initialised == YES) {
6172 color2 = get_x11_color(value);
6179 X11_initialisation();
6180 if (x_initialised == YES) {
6182 color3 = get_x11_color(value);
6189 X11_initialisation();
6190 if (x_initialised == YES) {
6192 color4 = get_x11_color(value);
6199 X11_initialisation();
6200 if (x_initialised == YES) {
6202 color5 = get_x11_color(value);
6209 X11_initialisation();
6210 if (x_initialised == YES) {
6212 color6 = get_x11_color(value);
6219 X11_initialisation();
6220 if (x_initialised == YES) {
6222 color7 = get_x11_color(value);
6229 X11_initialisation();
6230 if (x_initialised == YES) {
6232 color8 = get_x11_color(value);
6239 X11_initialisation();
6240 if (x_initialised == YES) {
6242 color9 = get_x11_color(value);
6248 CONF("default_color") {
6249 X11_initialisation();
6250 if (x_initialised == YES) {
6252 default_fg_color = get_x11_color(value);
6258 CONF3("default_shade_color", "default_shadecolor") {
6259 X11_initialisation();
6260 if (x_initialised == YES) {
6262 default_bg_color = get_x11_color(value);
6268 CONF3("default_outline_color", "default_outlinecolor") {
6269 X11_initialisation();
6270 if (x_initialised == YES) {
6272 default_out_color = get_x11_color(value);
6279 CONF("own_window_colour") {
6280 X11_initialisation();
6281 if (x_initialised == YES) {
6283 background_colour = get_x11_color(value);
6285 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
6286 "'#' for hex colours");
6292 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
6293 if(output_methods & TO_X) {
6294 X11_initialisation();
6303 #undef CONF_CONTINUE
6304 #undef CONF_BUFF_SIZE
6312 static void print_help(const char *prog_name) {
6313 printf("Usage: %s [OPTION]...\n"
6314 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
6315 "window. Command line options will override configurations defined in config\n"
6317 " -v, --version version\n"
6318 " -q, --quiet quiet mode\n"
6319 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
6320 " -c, --config=FILE config file to load\n"
6321 #ifdef CONFIG_OUTPUT
6322 " -C, --print-config print the builtin default config to stdout\n"
6323 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
6325 " -d, --daemonize daemonize, fork to background\n"
6326 " -h, --help help\n"
6328 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
6329 " -f, --font=FONT font to use\n"
6330 " -X, --display=DISPLAY X11 display to use\n"
6332 " -o, --own-window create own window to draw\n"
6335 " -b, --double-buffer double buffer (prevents flickering)\n"
6337 " -w, --window-id=WIN_ID window id to draw\n"
6338 " -x X x position\n"
6339 " -y Y y position\n"
6341 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
6342 " -u, --interval=SECS update interval\n"
6343 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
6348 /* : means that character before that takes an argument */
6349 static const char *getopt_string = "vVqdDt:u:i:hc:"
6359 #ifdef CONFIG_OUTPUT
6364 static const struct option longopts[] = {
6365 { "help", 0, NULL, 'h' },
6366 { "version", 0, NULL, 'V' },
6367 { "debug", 0, NULL, 'D' },
6368 { "config", 1, NULL, 'c' },
6369 #ifdef CONFIG_OUTPUT
6370 { "print-config", 0, NULL, 'C' },
6372 { "daemonize", 0, NULL, 'd' },
6374 { "alignment", 1, NULL, 'a' },
6375 { "font", 1, NULL, 'f' },
6376 { "display", 1, NULL, 'X' },
6378 { "own-window", 0, NULL, 'o' },
6381 { "double-buffer", 0, NULL, 'b' },
6383 { "window-id", 1, NULL, 'w' },
6385 { "text", 1, NULL, 't' },
6386 { "interval", 0, NULL, 'u' },
6390 void initialisation(int argc, char **argv) {
6391 struct sigaction act, oact;
6393 set_default_configurations();
6394 load_config_file(current_config);
6395 currentconffile = conftree_add(currentconffile, current_config);
6397 /* init specials array */
6398 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
6399 NORM_ERR("failed to create specials array");
6403 if (current_mail_spool == NULL) {
6406 variable_substitute(MAIL_FILE, buf, 256);
6408 if (buf[0] != '\0') {
6409 current_mail_spool = strndup(buf, text_buffer_size);
6414 /* handle other command line arguments */
6416 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
6417 || defined(__NetBSD__)
6418 optind = optreset = 1;
6423 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6424 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
6425 "kvm_open")) == NULL) {
6426 CRIT_ERR(NULL, NULL, "cannot read kvm");
6431 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6439 fork_to_background = 1;
6442 global_debug_level++;
6446 set_first_font(optarg);
6449 text_alignment = string_to_alignment(optarg);
6468 global_text = strndup(optarg, max_user_text);
6469 convert_escapes(global_text);
6473 update_interval = strtod(optarg, 0);
6474 update_interval_old = update_interval;
6475 if (info.music_player_interval == 0) {
6476 // default to update_interval
6477 info.music_player_interval = update_interval;
6482 total_run_times = strtod(optarg, 0);
6486 gap_x = atoi(optarg);
6490 gap_y = atoi(optarg);
6501 if (output_methods & TO_X) {
6502 load_config_file_x11(current_config);
6506 /* generate text and get initial size */
6507 extract_variable_text(global_text);
6514 if (fork_to_background) {
6519 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
6526 fprintf(stderr, "\n");
6531 /* parent process */
6532 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
6539 start_update_threading();
6541 text_buffer = malloc(max_user_text);
6542 memset(text_buffer, 0, max_user_text);
6543 tmpstring1 = malloc(text_buffer_size);
6544 memset(tmpstring1, 0, text_buffer_size);
6545 tmpstring2 = malloc(text_buffer_size);
6546 memset(tmpstring2, 0, text_buffer_size);
6551 X11_create_window();
6554 llua_setup_info(&info, update_interval);
6555 #endif /* HAVE_LUA */
6560 /* Set signal handlers */
6561 act.sa_handler = signal_handler;
6562 sigemptyset(&act.sa_mask);
6565 act.sa_flags |= SA_RESTART;
6568 if ( sigaction(SIGINT, &act, &oact) < 0
6569 || sigaction(SIGALRM, &act, &oact) < 0
6570 || sigaction(SIGUSR1, &act, &oact) < 0
6571 || sigaction(SIGHUP, &act, &oact) < 0
6572 || sigaction(SIGTERM, &act, &oact) < 0) {
6573 NORM_ERR("error setting signal handler: %s", strerror(errno));
6577 llua_startup_hook();
6578 #endif /* HAVE_LUA */
6581 int main(int argc, char **argv)
6590 g_signal_pending = 0;
6591 max_user_text = MAX_USER_TEXT_DEFAULT;
6593 memset(&info, 0, sizeof(info));
6597 #ifdef TCP_PORT_MONITOR
6598 /* set default connection limit */
6599 tcp_portmon_set_max_connections(0);
6602 /* handle command line parameters that don't change configs */
6604 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
6605 || ((s = getenv("LANG")) && *s)) {
6606 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6608 NORM_ERR("malloc failed");
6610 for (x = 0; x < strlen(s); x++) {
6611 temp[x] = tolower(s[x]);
6614 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6620 if (!setlocale(LC_CTYPE, "")) {
6621 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6625 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6636 if (current_config) {
6637 free(current_config);
6639 current_config = strndup(optarg, max_user_text);
6642 freopen("/dev/null", "w", stderr);
6645 print_help(argv[0]);
6647 #ifdef CONFIG_OUTPUT
6654 window.window = strtol(optarg, 0, 0);
6659 disp = strdup(optarg);
6668 /* check if specified config file is valid */
6669 if (current_config) {
6671 if (stat(current_config, &sb) ||
6672 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
6673 NORM_ERR("invalid configuration file '%s'\n", current_config);
6674 free(current_config);
6679 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
6681 if (!current_config) {
6682 /* load default config file */
6683 char buf[DEFAULT_TEXT_BUFFER_SIZE];
6686 /* Try to use personal config file first */
6687 to_real_path(buf, CONFIG_FILE);
6688 if (buf[0] && (fp = fopen(buf, "r"))) {
6689 current_config = strndup(buf, max_user_text);
6693 /* Try to use system config file if personal config not readable */
6694 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
6695 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
6699 /* No readable config found */
6700 if (!current_config) {
6701 #ifdef CONFIG_OUTPUT
6702 current_config = strdup("==builtin==");
6703 NORM_ERR("no readable personal or system-wide config file found,"
6704 " using builtin default");
6706 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
6707 #endif /* ! CONF_OUTPUT */
6712 /* Load xoap keys, if existing */
6716 #ifdef HAVE_SYS_INOTIFY_H
6717 inotify_fd = inotify_init1(IN_NONBLOCK);
6718 #endif /* HAVE_SYS_INOTIFY_H */
6720 initialisation(argc, argv);
6724 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6732 void alarm_handler(void) {
6734 kill(childpid, SIGTERM);
6738 static void signal_handler(int sig)
6740 /* signal handler is light as a feather, as it should be.
6741 * we will poll g_signal_pending with each loop of conky
6742 * and do any signal processing there, NOT here (except
6743 * SIGALRM because this is caused when conky is hanging) */
6744 if(sig == SIGALRM) {
6747 g_signal_pending = sig;