1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
33 #include "text_object.h"
47 #include <sys/param.h>
48 #ifdef HAVE_SYS_INOTIFY_H
49 #include <sys/inotify.h>
50 #endif /* HAVE_SYS_INOTIFY_H */
53 #include <X11/Xutil.h>
55 #include <X11/extensions/Xdamage.h>
61 #include <sys/types.h>
63 #include <netinet/in.h>
71 #include <libxml/parser.h>
89 #include "temphelper.h"
93 /* check for OS and include appropriate headers */
94 #if defined(__linux__)
96 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
98 #elif defined(__OpenBSD__)
102 #if defined(__FreeBSD_kernel__)
106 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
107 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
108 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
109 int apm_getinfo(int fd, apm_info_t aip);
110 char *get_apm_adapter(void);
111 char *get_apm_battery_life(void);
112 char *get_apm_battery_time(void);
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 static char *template[MAX_TEMPLATES];
386 char **get_templates(void)
391 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
392 unsigned int max_user_text;
394 /* maximum size of individual text buffers, ie $exec buffer size */
395 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
400 /* no buffers in used memory? */
403 /* pad percentages to decimals? */
404 static int pad_percents = 0;
406 static char *global_text = 0;
408 char *get_global_text(void)
413 long global_text_lines;
415 static int total_updates;
416 static int updatereset;
418 void set_updatereset(int i)
423 int get_updatereset(void)
428 int check_contains(char *f, char *s)
431 FILE *where = open_file(f, 0);
436 while (fgets(buf1, 256, where)) {
437 if (strstr(buf1, s)) {
444 NORM_ERR("Could not open the file");
449 #define SECRIT_MULTILINE_CHAR '\x02'
453 static inline int calc_text_width(const char *s, int l)
455 if ((output_methods & TO_X) == 0) {
463 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
464 (const FcChar8 *) s, l, &gi);
466 XftTextExtents8(display, fonts[selected_font].xftfont,
467 (const FcChar8 *) s, l, &gi);
473 return XTextWidth(fonts[selected_font].font, s, l);
478 /* formatted text to render on screen, generated in generate_text(),
479 * drawn in draw_stuff() */
481 static char *text_buffer;
483 /* quite boring functions */
485 static inline void for_each_line(char *b, int f(char *, int))
488 int special_index = 0; /* specials index */
490 for (ps = b, pe = b; *pe; pe++) {
493 special_index = f(ps, special_index);
500 f(ps, special_index);
504 static void convert_escapes(char *buf)
506 char *p = buf, *s = buf;
513 } else if (*s == '\\') {
524 /* Prints anything normally printed with snprintf according to the current value
525 * of use_spacer. Actually slightly more flexible than snprintf, as you can
526 * safely specify the destination buffer as one of your inputs. */
527 int spaced_print(char *buf, int size, const char *format, int width, ...)
536 tempbuf = malloc(size * sizeof(char));
538 // Passes the varargs along to vsnprintf
539 va_start(argp, width);
540 vsnprintf(tempbuf, size, format, argp);
543 switch (use_spacer) {
545 len = snprintf(buf, size, "%s", tempbuf);
548 len = snprintf(buf, size, "%*s", width, tempbuf);
551 len = snprintf(buf, size, "%-*s", width, tempbuf);
558 /* print percentage values
560 * - i.e., unsigned values between 0 and 100
561 * - respect the value of pad_percents */
562 static int percent_print(char *buf, int size, unsigned value)
564 return spaced_print(buf, size, "%u", pad_percents, value);
567 /* converts from bytes to human readable format (K, M, G, T)
569 * The algorithm always divides by 1024, as unit-conversion of byte
570 * counts suggests. But for output length determination we need to
571 * compare with 1000 here, as we print in decimal form. */
572 static void human_readable(long long num, char *buf, int size)
574 const char **suffix = suffixes;
580 /* Possibly just output as usual, for example for stdout usage */
581 if (!format_human_readable) {
582 spaced_print(buf, size, "%d", 6, round_to_int(num));
593 if (llabs(num) < 1000LL) {
594 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
598 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
606 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
608 * The goal is to always have a significance of 3, by
609 * adjusting the decimal part of the number. Sample output:
613 * so the point of alignment resides between number and unit. The
614 * upside of this is that there is minimal padding necessary, though
615 * there should be a way to make alignment take place at the decimal
616 * dot (then with fixed width decimal part).
618 * Note the repdigits below: when given a precision value, printf()
619 * rounds the float to it, not just cuts off the remaining digits. So
620 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
621 * printed with a precision of 0. Yay. */
623 precision = 0; /* print 100-999 without decimal part */
625 precision = 1; /* print 10-99 with one decimal place */
627 precision = 2; /* print 0-9 with two decimal places */
629 spaced_print(buf, size, format, width, precision, fnum, *suffix);
632 /* global object list root element */
633 static struct text_object global_root_object;
635 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
636 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
637 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
638 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
640 int parentend, childend;
642 //by running pipe after the strcmp's we make sure that we don't have to create a pipe
643 //and close the ends if mode is something illegal
644 if(strcmp(mode, "r") == 0) {
645 if(pipe(ends) != 0) {
650 } else if(strcmp(mode, "w") == 0) {
651 if(pipe(ends) != 0) {
664 } else if(*child > 0) {
666 waitpid(*child, NULL, 0);
668 //don't read from both stdin and pipe or write to both stdout and pipe
669 if(childend == ends[0]) {
674 dup(childend); //by dupping childend, the returned fd will have close-on-exec turned off
675 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
676 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail)
678 return fdopen(parentend, mode);
681 static inline void read_exec(const char *data, char *buf, const int size)
685 alarm(update_interval);
686 fp = pid_popen(data, "r", &childpid);
690 length = fread(buf, 1, size, fp);
693 if (length > 0 && buf[length - 1] == '\n') {
694 buf[length - 1] = '\0';
702 void do_read_exec(const char *data, char *buf, const int size)
704 read_exec(data, buf, size);
707 void *threaded_exec(void *) __attribute__((noreturn));
709 void *threaded_exec(void *arg)
712 struct text_object *obj = (struct text_object *)arg;
715 buff = malloc(text_buffer_size);
716 read_exec(obj->data.texeci.cmd, buff,
725 timed_thread_lock(obj->data.texeci.p_timed_thread);
726 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
727 timed_thread_unlock(obj->data.texeci.p_timed_thread);
729 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
730 timed_thread_exit(obj->data.texeci.p_timed_thread);
736 static long current_text_color;
738 void set_current_text_color(long colour)
740 current_text_color = colour;
743 long get_current_text_color(void)
745 return current_text_color;
748 //adds newstring to to the tree unless you can already see it when travelling back.
749 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
750 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
751 struct conftree* node;
752 struct conftree* node2;
754 for(node = previous; node != NULL; node = node->back) {
755 if(strcmp(node->string, newstring) == 0) {
759 node = malloc(sizeof(struct conftree));
760 if (previous != NULL) {
761 if(previous->vert_next == NULL) {
762 previous->vert_next = node;
764 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
765 node2->horz_next = node;
768 node->string = strdup(newstring);
769 node->horz_next = NULL;
770 node->vert_next = NULL;
771 node->back = previous;
775 void conftree_empty(struct conftree* tree) {
777 conftree_empty(tree->horz_next);
778 conftree_empty(tree->vert_next);
784 struct conftree *currentconffile;
786 static void extract_variable_text(const char *p)
788 free_text_objects(&global_root_object, 0);
802 extract_variable_text_internal(&global_root_object, p);
805 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
807 extract_variable_text_internal(root, txt);
808 generate_text_internal(p, max_user_text, *root, cur);
811 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
812 void *thread(void *), const char *text)
814 if (obj->char_b && info.mail) {
815 // this means we use info
816 if (!info.mail->p_timed_thread) {
817 info.mail->p_timed_thread =
818 timed_thread_create(thread,
819 (void *) info.mail, info.mail->interval * 1000000);
820 if (!info.mail->p_timed_thread) {
821 NORM_ERR("Error creating %s timed thread", text);
823 timed_thread_register(info.mail->p_timed_thread,
824 &info.mail->p_timed_thread);
825 if (timed_thread_run(info.mail->p_timed_thread)) {
826 NORM_ERR("Error running %s timed thread", text);
830 } else if (obj->data.mail) {
831 // this means we use obj
832 if (!obj->data.mail->p_timed_thread) {
833 obj->data.mail->p_timed_thread =
834 timed_thread_create(thread,
835 (void *) obj->data.mail,
836 obj->data.mail->interval * 1000000);
837 if (!obj->data.mail->p_timed_thread) {
838 NORM_ERR("Error creating %s timed thread", text);
840 timed_thread_register(obj->data.mail->p_timed_thread,
841 &obj->data.mail->p_timed_thread);
842 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
843 NORM_ERR("Error running %s timed thread", text);
846 return obj->data.mail;
847 } else if (!obj->a) {
848 // something is wrong, warn once then stop
849 NORM_ERR("There's a problem with your mail settings. "
850 "Check that the global mail settings are properly defined"
851 " (line %li).", obj->line);
857 char *format_time(unsigned long timeval, const int width)
860 unsigned long nt; // narrow time, for speed on 32-bit
861 unsigned cc; // centiseconds
862 unsigned nn; // multi-purpose whatever
865 cc = nt % 100; // centiseconds past second
866 nt /= 100; // total seconds
867 nn = nt % 60; // seconds past the minute
868 nt /= 60; // total minutes
869 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
871 return strndup(buf, text_buffer_size);
873 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
874 return strndup(buf, text_buffer_size);
876 nn = nt % 60; // minutes past the hour
877 nt /= 60; // total hours
878 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
879 return strndup(buf, text_buffer_size);
881 nn = nt; // now also hours
882 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
883 return strndup(buf, text_buffer_size);
885 nn /= 24; // now days
886 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
887 return strndup(buf, text_buffer_size);
889 nn /= 7; // now weeks
890 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
891 return strndup(buf, text_buffer_size);
893 // well shoot, this outta' fit...
894 return strndup("<inf>", text_buffer_size);
897 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
898 //string has to end with \0 and it's length should fit in a int
900 void remove_deleted_chars(char *string){
902 while(string[i] != 0){
903 if(string[i] == BACKSPACE){
905 strcpy( &(string[i-1]), &(string[i+1]) );
907 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
912 static inline void format_media_player_time(char *buf, const int size,
915 int days, hours, minutes;
917 days = seconds / (24 * 60 * 60);
918 seconds %= (24 * 60 * 60);
919 hours = seconds / (60 * 60);
920 seconds %= (60 * 60);
921 minutes = seconds / 60;
925 snprintf(buf, size, "%i days %i:%02i:%02i", days,
926 hours, minutes, seconds);
927 } else if (hours > 0) {
928 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
931 snprintf(buf, size, "%i:%02i", minutes, seconds);
935 static inline double get_barnum(char *buf)
947 if (sscanf(buf, "%lf", &barnum) == 0) {
948 NORM_ERR("reading exec value failed (perhaps it's not the "
952 if (barnum > 100.0 || barnum < 0.0) {
953 NORM_ERR("your exec value is not between 0 and 100, "
954 "therefore it will be ignored");
960 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
961 * multiline objects like $exec work with $align[rc] and friends
963 void substitute_newlines(char *p, long l)
967 while (p && *p && p < s + l) {
969 /* only substitute if it's not the last newline */
970 *p = SECRIT_MULTILINE_CHAR;
976 static void generate_text_internal(char *p, int p_max_size,
977 struct text_object root, struct information *cur)
979 struct text_object *obj;
981 int need_to_load_fonts = 0;
984 /* for the OBJ_top* handler */
985 struct process **needed = 0;
988 char buff_in[p_max_size];
990 set_iconv_converting(0);
991 #endif /* HAVE_ICONV */
995 while (obj && p_max_size > 0) {
996 needed = 0; /* reset for top stuff */
998 /* IFBLOCK jumping algorithm
1000 * This is easier as it looks like:
1001 * - each IF checks it's condition
1002 * - on FALSE: call DO_JUMP
1003 * - on TRUE: don't care
1004 * - each ELSE calls DO_JUMP unconditionally
1005 * - each ENDIF is silently being ignored
1008 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
1009 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
1010 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
1011 * regularly, "obj" is being updated to point to obj->next, so object parsing
1012 * continues right after the corresponding ELSE or ENDIF. This means that if we
1013 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
1014 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
1015 * jumped, and there is nothing to do.
1019 obj = obj->data.ifblock.next; \
1022 #define OBJ(a) break; case OBJ_##a:
1024 switch (obj->type) {
1026 NORM_ERR("not implemented obj type %d", obj->type);
1029 struct sockaddr_in addr;
1030 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
1032 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
1034 memset(&addr, 0, sizeof(addr));
1035 addr.sin_family = AF_INET;
1036 addr.sin_port = obj->data.read_tcp.port;
1037 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
1038 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
1042 FD_SET(sock, &readfds);
1045 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
1046 received = recv(sock, p, p_max_size, 0);
1051 NORM_ERR("read_tcp: Couldn't create a connection");
1054 NORM_ERR("read_tcp: Couldn't create a socket");
1057 NORM_ERR("read_tcp: Problem with resolving the hostname");
1062 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
1064 #endif /* !__OpenBSD__ */
1067 obj->a = get_freq(p, p_max_size, "%.0f", 1,
1068 obj->data.cpu_index);
1074 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
1075 obj->data.cpu_index);
1077 /* OpenBSD has no such flag (SUSv2) */
1078 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
1079 obj->data.cpu_index);
1080 #endif /* __OpenBSD */
1083 #if defined(__linux__)
1086 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
1087 obj->data.cpu_index);
1092 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
1093 obj->data.cpu_index);
1098 OBJ(wireless_essid) {
1099 snprintf(p, p_max_size, "%s", obj->data.net->essid);
1101 OBJ(wireless_mode) {
1102 snprintf(p, p_max_size, "%s", obj->data.net->mode);
1104 OBJ(wireless_bitrate) {
1105 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
1108 snprintf(p, p_max_size, "%s", obj->data.net->ap);
1110 OBJ(wireless_link_qual) {
1111 spaced_print(p, p_max_size, "%d", 4,
1112 obj->data.net->link_qual);
1114 OBJ(wireless_link_qual_max) {
1115 spaced_print(p, p_max_size, "%d", 4,
1116 obj->data.net->link_qual_max);
1118 OBJ(wireless_link_qual_perc) {
1119 if (obj->data.net->link_qual_max > 0) {
1120 spaced_print(p, p_max_size, "%.0f", 5,
1121 (double) obj->data.net->link_qual /
1122 obj->data.net->link_qual_max * 100);
1124 spaced_print(p, p_max_size, "unk", 5);
1127 OBJ(wireless_link_bar) {
1129 if(output_methods & TO_X) {
1130 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
1131 obj->data.net->link_qual_max) * 255.0);
1134 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1135 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
1136 obj->data.net->link_qual_max) * 100.0, obj->a);
1141 #endif /* HAVE_IWLIB */
1143 #endif /* __linux__ */
1147 get_adt746x_cpu(p, p_max_size);
1150 get_adt746x_fan(p, p_max_size);
1153 get_acpi_fan(p, p_max_size);
1155 OBJ(acpiacadapter) {
1156 get_acpi_ac_adapter(p, p_max_size);
1159 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
1162 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
1164 OBJ(battery_percent) {
1165 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
1169 if(output_methods & TO_X) {
1170 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
1173 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1174 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
1179 OBJ(battery_short) {
1180 get_battery_short_status(p, p_max_size, obj->data.s);
1182 #endif /* __OpenBSD__ */
1185 human_readable(cur->buffers * 1024, p, 255);
1188 human_readable(cur->cached * 1024, p, 255);
1191 if (obj->data.cpu_index > info.cpu_count) {
1192 NORM_ERR("obj->data.cpu_index %i info.cpu_count %i",
1193 obj->data.cpu_index, info.cpu_count);
1194 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
1196 percent_print(p, p_max_size,
1197 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
1201 new_gauge(p, obj->a, obj->b,
1202 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1206 if(output_methods & TO_X) {
1207 new_bar(p, obj->a, obj->b,
1208 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1211 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1212 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
1219 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1220 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
1221 100, 1, obj->char_a, obj->char_b);
1224 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
1225 obj->e, 1, obj->char_a, obj->char_b);
1229 new_fg(p, obj->data.l);
1263 OBJ(conky_version) {
1264 snprintf(p, p_max_size, "%s", VERSION);
1266 OBJ(conky_build_date) {
1267 snprintf(p, p_max_size, "%s", BUILD_DATE);
1269 OBJ(conky_build_arch) {
1270 snprintf(p, p_max_size, "%s", BUILD_ARCH);
1272 #if defined(__linux__)
1274 snprintf(p, p_max_size, "%s",
1275 get_disk_protect_queue(obj->data.s));
1278 snprintf(p, p_max_size, "%s", i8k.version);
1281 snprintf(p, p_max_size, "%s", i8k.bios);
1284 snprintf(p, p_max_size, "%s", i8k.serial);
1289 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1290 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1292 OBJ(i8k_left_fan_status) {
1293 int left_fan_status;
1295 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1296 if (left_fan_status == 0) {
1297 snprintf(p, p_max_size, "off");
1299 if (left_fan_status == 1) {
1300 snprintf(p, p_max_size, "low");
1302 if (left_fan_status == 2) {
1303 snprintf(p, p_max_size, "high");
1306 OBJ(i8k_right_fan_status) {
1307 int right_fan_status;
1309 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1310 if (right_fan_status == 0) {
1311 snprintf(p, p_max_size, "off");
1313 if (right_fan_status == 1) {
1314 snprintf(p, p_max_size, "low");
1316 if (right_fan_status == 2) {
1317 snprintf(p, p_max_size, "high");
1320 OBJ(i8k_left_fan_rpm) {
1321 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1323 OBJ(i8k_right_fan_rpm) {
1324 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1326 OBJ(i8k_ac_status) {
1329 sscanf(i8k.ac_status, "%d", &ac_status);
1330 if (ac_status == -1) {
1331 snprintf(p, p_max_size, "disabled (read i8k docs)");
1333 if (ac_status == 0) {
1334 snprintf(p, p_max_size, "off");
1336 if (ac_status == 1) {
1337 snprintf(p, p_max_size, "on");
1340 OBJ(i8k_buttons_status) {
1341 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1345 get_ibm_acpi_fan(p, p_max_size);
1348 get_ibm_acpi_temps();
1349 temp_print(p, p_max_size,
1350 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
1353 get_ibm_acpi_volume(p, p_max_size);
1355 OBJ(ibm_brightness) {
1356 get_ibm_acpi_brightness(p, p_max_size);
1359 /* information from sony_laptop kernel module
1360 * /sys/devices/platform/sony-laptop */
1361 OBJ(sony_fanspeed) {
1362 get_sony_fanspeed(p, p_max_size);
1365 if (!cur->gw_info.count) {
1370 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
1373 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
1376 snprintf(p, p_max_size, "%d", get_laptop_mode());
1379 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1381 #endif /* __linux__ */
1382 #if (defined(__FreeBSD__) || defined(__linux__))
1384 if ((obj->data.ifblock.s)
1385 && (!interface_up(obj->data.ifblock.s))) {
1391 OBJ(obsd_sensors_temp) {
1392 obsd_sensors.device = sensor_device;
1393 update_obsd_sensors();
1394 temp_print(p, p_max_size,
1395 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
1398 OBJ(obsd_sensors_fan) {
1399 obsd_sensors.device = sensor_device;
1400 update_obsd_sensors();
1401 snprintf(p, p_max_size, "%d",
1402 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
1404 OBJ(obsd_sensors_volt) {
1405 obsd_sensors.device = sensor_device;
1406 update_obsd_sensors();
1407 snprintf(p, p_max_size, "%.2f",
1408 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
1411 get_obsd_vendor(p, p_max_size);
1414 get_obsd_product(p, p_max_size);
1416 #endif /* __OpenBSD__ */
1419 new_font(p, obj->data.s);
1420 need_to_load_fonts = 1;
1423 /* TODO: move this correction from kB to kB/s elsewhere
1424 * (or get rid of it??) */
1426 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
1430 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
1434 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
1439 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1440 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
1442 OBJ(diskiograph_read) {
1443 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1444 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
1446 OBJ(diskiograph_write) {
1447 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1448 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
1452 human_readable(obj->data.net->recv_speed, p, 255);
1455 spaced_print(p, p_max_size, "%.1f", 8,
1456 obj->data.net->recv_speed / 1024.0);
1459 OBJ(downspeedgraph) {
1460 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1461 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
1465 /* Since we see you, you're if has not jumped.
1466 * Do Ninja jump here: without leaving traces.
1467 * This is to prevent us from stale jumped flags.
1469 obj = obj->data.ifblock.next;
1473 /* harmless object, just ignore */
1476 if ((obj->data.net->addr.sa_data[2] & 255) == 0
1477 && (obj->data.net->addr.sa_data[3] & 255) == 0
1478 && (obj->data.net->addr.sa_data[4] & 255) == 0
1479 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
1480 snprintf(p, p_max_size, "No Address");
1482 snprintf(p, p_max_size, "%u.%u.%u.%u",
1483 obj->data.net->addr.sa_data[2] & 255,
1484 obj->data.net->addr.sa_data[3] & 255,
1485 obj->data.net->addr.sa_data[4] & 255,
1486 obj->data.net->addr.sa_data[5] & 255);
1489 #if defined(__linux__)
1491 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
1492 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
1493 strcpy(p, obj->data.net->addrs);
1495 strcpy(p, "0.0.0.0");
1498 #endif /* __linux__ */
1499 #if defined(IMLIB2) && defined(X11)
1501 /* doesn't actually draw anything, just queues it omp. the
1502 * image will get drawn after the X event loop */
1503 cimlib_add_image(obj->data.s);
1507 evaluate(obj->data.s, p);
1510 read_exec(obj->data.s, p, text_buffer_size);
1511 remove_deleted_chars(p);
1514 struct information *tmp_info;
1515 struct text_object subroot;
1517 read_exec(obj->data.s, p, text_buffer_size);
1519 tmp_info = malloc(sizeof(struct information));
1520 memcpy(tmp_info, cur, sizeof(struct information));
1521 parse_conky_vars(&subroot, p, p, tmp_info);
1523 free_text_objects(&subroot, 1);
1530 read_exec(obj->data.s, p, text_buffer_size);
1531 barnum = get_barnum(p); /*using the same function*/
1533 if (barnum >= 0.0) {
1535 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1542 read_exec(obj->data.s, p, text_buffer_size);
1543 barnum = get_barnum(p);
1545 if (barnum >= 0.0) {
1547 if(output_methods & TO_X) {
1549 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1552 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1553 new_bar_in_shell(p, p_max_size, barnum, obj->a);
1561 char showaslog = FALSE;
1562 char tempgrad = FALSE;
1564 char *cmd = obj->data.s;
1566 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1568 cmd += strlen(" "TEMPGRAD);
1570 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1572 cmd += strlen(" "LOGGRAPH);
1574 read_exec(cmd, p, text_buffer_size);
1575 barnum = get_barnum(p);
1578 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
1579 100, 1, showaslog, tempgrad);
1584 if (current_update_time - obj->data.execi.last_update
1585 >= obj->data.execi.interval) {
1588 read_exec(obj->data.execi.cmd, p, text_buffer_size);
1589 barnum = get_barnum(p);
1591 if (barnum >= 0.0) {
1594 obj->data.execi.last_update = current_update_time;
1597 if(output_methods & TO_X) {
1598 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
1601 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1602 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
1609 if (current_update_time - obj->data.execi.last_update
1610 >= obj->data.execi.interval) {
1612 char showaslog = FALSE;
1613 char tempgrad = FALSE;
1614 char *cmd = obj->data.execi.cmd;
1616 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1618 cmd += strlen(" "TEMPGRAD);
1620 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1622 cmd += strlen(" "LOGGRAPH);
1624 obj->char_a = showaslog;
1625 obj->char_b = tempgrad;
1626 read_exec(cmd, p, text_buffer_size);
1627 barnum = get_barnum(p);
1629 if (barnum >= 0.0) {
1632 obj->data.execi.last_update = current_update_time;
1634 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
1637 if (current_update_time - obj->data.execi.last_update
1638 >= obj->data.execi.interval) {
1641 read_exec(obj->data.execi.cmd, p, text_buffer_size);
1642 barnum = get_barnum(p);
1644 if (barnum >= 0.0) {
1645 obj->f = 255 * barnum / 100.0;
1647 obj->data.execi.last_update = current_update_time;
1649 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
1653 if (current_update_time - obj->data.execi.last_update
1654 >= obj->data.execi.interval
1655 && obj->data.execi.interval != 0) {
1656 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
1658 obj->data.execi.last_update = current_update_time;
1660 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
1663 struct text_object subroot;
1664 struct information *tmp_info =
1665 malloc(sizeof(struct information));
1666 memcpy(tmp_info, cur, sizeof(struct information));
1668 if (current_update_time - obj->data.execi.last_update
1669 < obj->data.execi.interval
1670 || obj->data.execi.interval == 0) {
1671 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1673 char *output = obj->data.execi.buffer;
1674 FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
1675 int length = fread(output, 1, text_buffer_size, fp);
1679 output[length] = '\0';
1680 if (length > 0 && output[length - 1] == '\n') {
1681 output[length - 1] = '\0';
1684 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1685 obj->data.execi.last_update = current_update_time;
1687 free_text_objects(&subroot, 1);
1691 if (!obj->data.texeci.p_timed_thread) {
1692 obj->data.texeci.p_timed_thread =
1693 timed_thread_create(&threaded_exec,
1694 (void *) obj, obj->data.texeci.interval * 1000000);
1695 if (!obj->data.texeci.p_timed_thread) {
1696 NORM_ERR("Error creating texeci timed thread");
1699 * note that we don't register this thread with the
1700 * timed_thread list, because we destroy it manually
1702 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
1703 NORM_ERR("Error running texeci timed thread");
1706 timed_thread_lock(obj->data.texeci.p_timed_thread);
1707 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
1708 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1712 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1714 if (mail && mail->p_timed_thread) {
1715 timed_thread_lock(mail->p_timed_thread);
1716 snprintf(p, p_max_size, "%lu", mail->unseen);
1717 timed_thread_unlock(mail->p_timed_thread);
1720 OBJ(imap_messages) {
1721 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1723 if (mail && mail->p_timed_thread) {
1724 timed_thread_lock(mail->p_timed_thread);
1725 snprintf(p, p_max_size, "%lu", mail->messages);
1726 timed_thread_unlock(mail->p_timed_thread);
1730 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1732 if (mail && mail->p_timed_thread) {
1733 timed_thread_lock(mail->p_timed_thread);
1734 snprintf(p, p_max_size, "%lu", mail->unseen);
1735 timed_thread_unlock(mail->p_timed_thread);
1739 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1741 if (mail && mail->p_timed_thread) {
1742 timed_thread_lock(mail->p_timed_thread);
1743 snprintf(p, p_max_size, "%.1f",
1744 mail->used / 1024.0 / 1024.0);
1745 timed_thread_unlock(mail->p_timed_thread);
1749 if (obj->data.fs != NULL) {
1750 if (obj->data.fs->size == 0) {
1752 if(output_methods & TO_X) {
1753 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1756 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1757 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1763 if(output_methods & TO_X) {
1764 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1765 (int) (255 - obj->data.fsbar.fs->avail * 255 /
1766 obj->data.fs->size));
1769 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1770 new_bar_in_shell(p, p_max_size,
1771 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1779 if (obj->data.fs != NULL) {
1780 human_readable(obj->data.fs->avail, p, 255);
1784 if (obj->data.fs != NULL) {
1787 if (obj->data.fs->size) {
1788 val = obj->data.fs->avail * 100 / obj->data.fs->size;
1791 percent_print(p, p_max_size, val);
1795 if (obj->data.fs != NULL) {
1796 human_readable(obj->data.fs->size, p, 255);
1800 if (obj->data.fs != NULL)
1801 snprintf(p, p_max_size, "%s", obj->data.fs->type);
1804 if (obj->data.fs != NULL) {
1805 human_readable(obj->data.fs->size - obj->data.fs->free, p,
1810 if (obj->data.fs != NULL) {
1811 if (obj->data.fs->size == 0) {
1813 if(output_methods & TO_X) {
1814 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1817 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1818 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1824 if(output_methods & TO_X) {
1825 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1826 (int) (obj->data.fsbar.fs->avail * 255 /
1827 obj->data.fs->size));
1830 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1831 new_bar_in_shell(p, p_max_size,
1832 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1840 if (obj->data.fs != NULL) {
1843 if (obj->data.fs->size) {
1844 val = obj->data.fs->free
1849 percent_print(p, p_max_size, 100 - val);
1853 float *v = info.loadavg;
1855 if (obj->data.loadavg[2]) {
1856 snprintf(p, p_max_size, "%.2f %.2f %.2f",
1857 v[obj->data.loadavg[0] - 1],
1858 v[obj->data.loadavg[1] - 1],
1859 v[obj->data.loadavg[2] - 1]);
1860 } else if (obj->data.loadavg[1]) {
1861 snprintf(p, p_max_size, "%.2f %.2f",
1862 v[obj->data.loadavg[0] - 1],
1863 v[obj->data.loadavg[1] - 1]);
1864 } else if (obj->data.loadavg[0]) {
1865 snprintf(p, p_max_size, "%.2f",
1866 v[obj->data.loadavg[0] - 1]);
1870 new_goto(p, obj->data.i);
1873 new_tab(p, obj->data.pair.a, obj->data.pair.b);
1877 new_hr(p, obj->data.i);
1881 if (cur->nameserver_info.nscount > obj->data.i)
1882 snprintf(p, p_max_size, "%s",
1883 cur->nameserver_info.ns_list[obj->data.i]);
1887 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
1888 snprintf(p, p_max_size, "%s", skill);
1893 if (obj->data.curl.uri != NULL) {
1894 ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
1896 NORM_ERR("error processing Curl data");
1902 if (obj->data.rss.uri != NULL) {
1903 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);
1905 NORM_ERR("error processing RSS data");
1911 if (obj->data.weather.uri != NULL) {
1912 weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
1914 NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
1919 OBJ(weather_forecast) {
1920 if (obj->data.weather_forecast.uri != NULL) {
1921 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);
1923 NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
1929 char *str = llua_getstring(obj->data.s);
1931 snprintf(p, p_max_size, "%s", str);
1936 char *str = llua_getstring(obj->data.s);
1944 if (llua_getnumber(obj->data.s, &per)) {
1946 if(output_methods & TO_X) {
1947 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
1950 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1951 new_bar_in_shell(p, p_max_size, per, obj->a);
1960 if (llua_getnumber(obj->data.s, &per)) {
1961 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1962 per, obj->e, 1, obj->char_a, obj->char_b);
1967 if (llua_getnumber(obj->data.s, &per)) {
1968 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
1972 #endif /* HAVE_LUA */
1977 if (obj->data.hddtemp.update_time < current_update_time - 30) {
1978 if (obj->data.hddtemp.temp)
1979 free(obj->data.hddtemp.temp);
1980 obj->data.hddtemp.temp = get_hddtemp_info(obj->data.hddtemp.dev,
1981 obj->data.hddtemp.addr, obj->data.hddtemp.port);
1982 obj->data.hddtemp.update_time = current_update_time;
1984 if (!obj->data.hddtemp.temp) {
1985 snprintf(p, p_max_size, "N/A");
1987 val = strtol(obj->data.hddtemp.temp + 1, &endptr, 10);
1988 unit = obj->data.hddtemp.temp[0];
1990 if (*endptr != '\0')
1991 snprintf(p, p_max_size, "N/A");
1992 else if (unit == 'C')
1993 temp_print(p, p_max_size, (double)val, TEMP_CELSIUS);
1994 else if (unit == 'F')
1995 temp_print(p, p_max_size, (double)val, TEMP_FAHRENHEIT);
1997 snprintf(p, p_max_size, "N/A");
2002 new_offset(p, obj->data.i);
2005 new_voffset(p, obj->data.i);
2011 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2012 obj->data.sysfs.devtype, obj->data.sysfs.type);
2014 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2016 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2017 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2018 } else if (r >= 100.0 || r == 0) {
2019 snprintf(p, p_max_size, "%d", (int) r);
2021 snprintf(p, p_max_size, "%.1f", r);
2027 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2028 obj->data.sysfs.devtype, obj->data.sysfs.type);
2030 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2032 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2033 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2034 } else if (r >= 100.0 || r == 0) {
2035 snprintf(p, p_max_size, "%d", (int) r);
2037 snprintf(p, p_max_size, "%.1f", r);
2043 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2044 obj->data.sysfs.devtype, obj->data.sysfs.type);
2046 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2048 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2049 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2050 } else if (r >= 100.0 || r == 0) {
2051 snprintf(p, p_max_size, "%d", (int) r);
2053 snprintf(p, p_max_size, "%.1f", r);
2056 #endif /* __linux__ */
2058 new_alignr(p, obj->data.i);
2061 new_alignc(p, obj->data.i);
2064 char buf[max_user_text];
2065 struct information *tmp_info =
2066 malloc(sizeof(struct information));
2067 memcpy(tmp_info, cur, sizeof(struct information));
2068 generate_text_internal(buf, max_user_text,
2069 *obj->sub, tmp_info);
2071 if (strlen(buf) != 0) {
2077 char expression[max_user_text];
2079 struct information *tmp_info;
2081 tmp_info = malloc(sizeof(struct information));
2082 memcpy(tmp_info, cur, sizeof(struct information));
2083 generate_text_internal(expression, max_user_text,
2084 *obj->sub, tmp_info);
2085 DBGP("parsed arg into '%s'", expression);
2087 val = compare(expression);
2089 NORM_ERR("compare failed for expression '%s'",
2097 if (obj->data.ifblock.str
2098 && !check_contains(obj->data.ifblock.s,
2099 obj->data.ifblock.str)) {
2101 } else if (obj->data.ifblock.s
2102 && access(obj->data.ifblock.s, F_OK)) {
2107 if ((obj->data.ifblock.s)
2108 && (!check_mount(obj->data.ifblock.s))) {
2114 if (!get_process_by_name(obj->data.ifblock.s)) {
2116 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
2121 #if defined(__linux__)
2123 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
2127 snprintf(p, p_max_size, "%s", cur->uname_s.release);
2130 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
2135 human_readable(cur->mem * 1024, p, 255);
2138 human_readable(cur->memeasyfree * 1024, p, 255);
2141 human_readable(cur->memfree * 1024, p, 255);
2144 human_readable(cur->memmax * 1024, p, 255);
2148 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
2152 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
2153 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2158 if(output_methods & TO_X) {
2159 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2160 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2163 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2164 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
2171 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2172 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
2173 100, 1, obj->char_a, obj->char_b);
2178 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
2181 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
2184 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
2188 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2189 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
2192 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2193 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
2196 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2197 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
2200 OBJ(if_mixer_mute) {
2201 if (!mixer_is_mute(obj->data.ifblock.i)) {
2206 #define NOT_IN_X "Not running in X"
2208 if(x_initialised != YES) {
2209 strncpy(p, NOT_IN_X, p_max_size);
2211 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
2214 OBJ(monitor_number) {
2215 if(x_initialised != YES) {
2216 strncpy(p, NOT_IN_X, p_max_size);
2218 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
2222 if(x_initialised != YES) {
2223 strncpy(p, NOT_IN_X, p_max_size);
2225 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
2228 OBJ(desktop_number) {
2229 if(x_initialised != YES) {
2230 strncpy(p, NOT_IN_X, p_max_size);
2232 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
2236 if(x_initialised != YES) {
2237 strncpy(p, NOT_IN_X, p_max_size);
2238 }else if(cur->x11.desktop.name != NULL) {
2239 strncpy(p, cur->x11.desktop.name, p_max_size);
2246 update_mail_count(&obj->data.local_mail);
2247 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
2250 update_mail_count(&obj->data.local_mail);
2251 snprintf(p, p_max_size, "%d",
2252 obj->data.local_mail.new_mail_count);
2255 update_mail_count(&obj->data.local_mail);
2256 snprintf(p, p_max_size, "%d",
2257 obj->data.local_mail.seen_mail_count);
2260 update_mail_count(&obj->data.local_mail);
2261 snprintf(p, p_max_size, "%d",
2262 obj->data.local_mail.unseen_mail_count);
2264 OBJ(flagged_mails) {
2265 update_mail_count(&obj->data.local_mail);
2266 snprintf(p, p_max_size, "%d",
2267 obj->data.local_mail.flagged_mail_count);
2269 OBJ(unflagged_mails) {
2270 update_mail_count(&obj->data.local_mail);
2271 snprintf(p, p_max_size, "%d",
2272 obj->data.local_mail.unflagged_mail_count);
2274 OBJ(forwarded_mails) {
2275 update_mail_count(&obj->data.local_mail);
2276 snprintf(p, p_max_size, "%d",
2277 obj->data.local_mail.forwarded_mail_count);
2279 OBJ(unforwarded_mails) {
2280 update_mail_count(&obj->data.local_mail);
2281 snprintf(p, p_max_size, "%d",
2282 obj->data.local_mail.unforwarded_mail_count);
2284 OBJ(replied_mails) {
2285 update_mail_count(&obj->data.local_mail);
2286 snprintf(p, p_max_size, "%d",
2287 obj->data.local_mail.replied_mail_count);
2289 OBJ(unreplied_mails) {
2290 update_mail_count(&obj->data.local_mail);
2291 snprintf(p, p_max_size, "%d",
2292 obj->data.local_mail.unreplied_mail_count);
2295 update_mail_count(&obj->data.local_mail);
2296 snprintf(p, p_max_size, "%d",
2297 obj->data.local_mail.draft_mail_count);
2299 OBJ(trashed_mails) {
2300 update_mail_count(&obj->data.local_mail);
2301 snprintf(p, p_max_size, "%d",
2302 obj->data.local_mail.trashed_mail_count);
2305 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
2307 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
2310 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
2313 new_outline(p, obj->data.l);
2316 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
2318 OBJ(running_processes) {
2319 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
2322 snprintf(p, p_max_size, "%s", obj->data.s);
2326 new_bg(p, obj->data.l);
2329 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
2333 human_readable(cur->swap * 1024, p, 255);
2336 human_readable(cur->swapfree * 1024, p, 255);
2339 human_readable(cur->swapmax * 1024, p, 255);
2342 if (cur->swapmax == 0) {
2343 strncpy(p, "No swap", p_max_size);
2345 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
2350 if(output_methods & TO_X) {
2351 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2352 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
2355 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2356 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
2362 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
2365 time_t t = time(NULL);
2366 struct tm *tm = localtime(&t);
2368 setlocale(LC_TIME, "");
2369 strftime(p, p_max_size, obj->data.s, tm);
2372 time_t t = time(NULL);
2373 struct tm *tm = gmtime(&t);
2375 strftime(p, p_max_size, obj->data.s, tm);
2382 if (obj->data.tztime.tz) {
2383 oldTZ = getenv("TZ");
2384 setenv("TZ", obj->data.tztime.tz, 1);
2390 setlocale(LC_TIME, "");
2391 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
2393 setenv("TZ", oldTZ, 1);
2398 // Needless to free oldTZ since getenv gives ptr to static data
2401 human_readable(obj->data.net->recv, p, 255);
2404 human_readable(obj->data.net->trans, p, 255);
2407 snprintf(p, p_max_size, "%d", total_updates);
2410 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
2415 human_readable(obj->data.net->trans_speed, p, 255);
2418 spaced_print(p, p_max_size, "%.1f", 8,
2419 obj->data.net->trans_speed / 1024.0);
2423 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2424 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
2428 format_seconds_short(p, p_max_size, (int) cur->uptime);
2431 format_seconds(p, p_max_size, (int) cur->uptime);
2434 snprintf(p, p_max_size, "%s", cur->users.names);
2437 snprintf(p, p_max_size, "%s", cur->users.terms);
2440 snprintf(p, p_max_size, "%s", cur->users.times);
2443 snprintf(p, p_max_size, "%d", cur->users.number);
2445 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2446 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2450 msg = get_apm_adapter();
2451 snprintf(p, p_max_size, "%s", msg);
2454 OBJ(apm_battery_life) {
2457 msg = get_apm_battery_life();
2458 snprintf(p, p_max_size, "%s", msg);
2461 OBJ(apm_battery_time) {
2464 msg = get_apm_battery_time();
2465 snprintf(p, p_max_size, "%s", msg);
2468 #endif /* __FreeBSD__ __OpenBSD__ */
2471 #define mpd_printf(fmt, val) \
2472 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
2473 #define mpd_sprintf(val) { \
2474 if (!obj->data.i || obj->data.i > p_max_size) \
2475 mpd_printf("%s", val); \
2477 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2482 mpd_sprintf(artist);
2486 mpd_printf("%s", random);
2488 mpd_printf("%s", repeat);
2496 mpd_printf("%d", volume);
2498 mpd_printf("%d", bitrate);
2500 mpd_printf("%s", status);
2502 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2505 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2508 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2512 if(output_methods & TO_X) {
2513 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2514 (int) (mpd_get_info()->progress * 255.0f));
2517 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2518 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
2524 struct mpd_s *mpd = mpd_get_info();
2525 int len = obj->data.i;
2526 if (len == 0 || len > p_max_size)
2529 memset(p, 0, p_max_size);
2530 if (mpd->artist && *mpd->artist &&
2531 mpd->title && *mpd->title) {
2532 snprintf(p, len, "%s - %s", mpd->artist,
2534 } else if (mpd->title && *mpd->title) {
2535 snprintf(p, len, "%s", mpd->title);
2536 } else if (mpd->artist && *mpd->artist) {
2537 snprintf(p, len, "%s", mpd->artist);
2538 } else if (mpd->file && *mpd->file) {
2539 snprintf(p, len, "%s", mpd->file);
2544 OBJ(if_mpd_playing) {
2545 if (!mpd_get_info()->is_playing) {
2554 #define MOC_PRINT(t, a) \
2555 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2557 MOC_PRINT(state, "??");
2560 MOC_PRINT(file, "no file");
2563 MOC_PRINT(title, "no title");
2566 MOC_PRINT(artist, "no artist");
2569 MOC_PRINT(song, "no song");
2572 MOC_PRINT(album, "no album");
2574 OBJ(moc_totaltime) {
2575 MOC_PRINT(totaltime, "0:00");
2578 MOC_PRINT(timeleft, "0:00");
2581 MOC_PRINT(curtime, "0:00");
2584 MOC_PRINT(bitrate, "0Kbps");
2587 MOC_PRINT(rate, "0KHz");
2593 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2596 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2599 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2602 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2604 OBJ(xmms2_comment) {
2605 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2608 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2611 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2614 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2616 OBJ(xmms2_tracknr) {
2617 if (cur->xmms2.tracknr != -1) {
2618 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2621 OBJ(xmms2_bitrate) {
2622 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2625 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2628 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2630 OBJ(xmms2_elapsed) {
2631 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2632 (cur->xmms2.elapsed / 1000) % 60);
2634 OBJ(xmms2_duration) {
2635 snprintf(p, p_max_size, "%02d:%02d",
2636 cur->xmms2.duration / 60000,
2637 (cur->xmms2.duration / 1000) % 60);
2639 OBJ(xmms2_percent) {
2640 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2644 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2645 (int) (cur->xmms2.progress * 255.0f));
2648 OBJ(xmms2_playlist) {
2649 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2651 OBJ(xmms2_timesplayed) {
2652 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2655 if (strlen(cur->xmms2.title) < 2
2656 && strlen(cur->xmms2.title) < 2) {
2657 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2659 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2663 OBJ(if_xmms2_connected) {
2664 if (cur->xmms2.conn_state != 1) {
2670 OBJ(audacious_status) {
2671 snprintf(p, p_max_size, "%s",
2672 cur->audacious.items[AUDACIOUS_STATUS]);
2674 OBJ(audacious_title) {
2675 snprintf(p, cur->audacious.max_title_len > 0
2676 ? cur->audacious.max_title_len : p_max_size, "%s",
2677 cur->audacious.items[AUDACIOUS_TITLE]);
2679 OBJ(audacious_length) {
2680 snprintf(p, p_max_size, "%s",
2681 cur->audacious.items[AUDACIOUS_LENGTH]);
2683 OBJ(audacious_length_seconds) {
2684 snprintf(p, p_max_size, "%s",
2685 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2687 OBJ(audacious_position) {
2688 snprintf(p, p_max_size, "%s",
2689 cur->audacious.items[AUDACIOUS_POSITION]);
2691 OBJ(audacious_position_seconds) {
2692 snprintf(p, p_max_size, "%s",
2693 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2695 OBJ(audacious_bitrate) {
2696 snprintf(p, p_max_size, "%s",
2697 cur->audacious.items[AUDACIOUS_BITRATE]);
2699 OBJ(audacious_frequency) {
2700 snprintf(p, p_max_size, "%s",
2701 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2703 OBJ(audacious_channels) {
2704 snprintf(p, p_max_size, "%s",
2705 cur->audacious.items[AUDACIOUS_CHANNELS]);
2707 OBJ(audacious_filename) {
2708 snprintf(p, p_max_size, "%s",
2709 cur->audacious.items[AUDACIOUS_FILENAME]);
2711 OBJ(audacious_playlist_length) {
2712 snprintf(p, p_max_size, "%s",
2713 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2715 OBJ(audacious_playlist_position) {
2716 snprintf(p, p_max_size, "%s",
2717 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2719 OBJ(audacious_main_volume) {
2720 snprintf(p, p_max_size, "%s",
2721 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2724 OBJ(audacious_bar) {
2728 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2729 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2730 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
2733 #endif /* AUDACIOUS */
2737 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2740 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2743 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2746 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2749 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2752 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2755 /* we have four different types of top (top, top_mem,
2756 * top_time and top_io). To avoid having almost-same code four
2757 * times, we have this special handler. */
2760 parse_top_args("top", obj->data.top.s, obj);
2761 if (!needed) needed = cur->cpu;
2763 parse_top_args("top_mem", obj->data.top.s, obj);
2764 if (!needed) needed = cur->memu;
2766 parse_top_args("top_time", obj->data.top.s, obj);
2767 if (!needed) needed = cur->time;
2770 parse_top_args("top_io", obj->data.top.s, obj);
2771 if (!needed) needed = cur->io;
2774 if (needed[obj->data.top.num]) {
2777 switch (obj->data.top.type) {
2779 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
2780 needed[obj->data.top.num]->name);
2783 snprintf(p, 7, "%6.2f",
2784 needed[obj->data.top.num]->amount);
2787 snprintf(p, 6, "%5i",
2788 needed[obj->data.top.num]->pid);
2791 snprintf(p, 7, "%6.2f",
2792 needed[obj->data.top.num]->totalmem);
2795 timeval = format_time(
2796 needed[obj->data.top.num]->total_cpu_time, 9);
2797 snprintf(p, 10, "%9s", timeval);
2801 human_readable(needed[obj->data.top.num]->rss,
2805 human_readable(needed[obj->data.top.num]->vsize,
2809 case TOP_READ_BYTES:
2810 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
2813 case TOP_WRITE_BYTES:
2814 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
2818 snprintf(p, 7, "%6.2f",
2819 needed[obj->data.top.num]->io_perc);
2825 print_tailhead("tail", obj, p, p_max_size);
2828 print_tailhead("head", obj, p, p_max_size);
2831 FILE *fp = open_file(obj->data.s, &obj->a);
2834 /* FIXME: use something more general (see also tail.c, head.c */
2835 #define BUFSZ 0x1000
2840 while(fgets(buf, BUFSZ, fp) != NULL){
2841 for(j = 0; buf[j] != 0; j++) {
2842 if(buf[j] == '\n') {
2847 sprintf(p, "%d", lines);
2850 sprintf(p, "File Unreadable");
2855 FILE *fp = open_file(obj->data.s, &obj->a);
2860 char inword = FALSE;
2863 while(fgets(buf, BUFSZ, fp) != NULL){
2864 for(j = 0; buf[j] != 0; j++) {
2865 if(!isspace(buf[j])) {
2866 if(inword == FALSE) {
2875 sprintf(p, "%d", words);
2878 sprintf(p, "File Unreadable");
2881 #ifdef TCP_PORT_MONITOR
2883 tcp_portmon_action(p, p_max_size,
2884 &obj->data.tcp_port_monitor);
2886 #endif /* TCP_PORT_MONITOR */
2890 set_iconv_converting(1);
2891 set_iconv_selected(obj->a);
2894 set_iconv_converting(0);
2895 set_iconv_selected(0);
2897 #endif /* HAVE_ICONV */
2899 OBJ(entropy_avail) {
2900 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2903 percent_print(p, p_max_size,
2904 cur->entropy.entropy_avail *
2905 100 / cur->entropy.poolsize);
2907 OBJ(entropy_poolsize) {
2908 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2911 double entropy_perc;
2913 entropy_perc = (double) cur->entropy.entropy_avail /
2914 (double) cur->entropy.poolsize;
2916 if(output_methods & TO_X) {
2917 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
2920 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
2921 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
2930 s = smapi_get_val(obj->data.s);
2931 snprintf(p, p_max_size, "%s", s);
2935 OBJ(if_smapi_bat_installed) {
2937 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
2938 if(!smapi_bat_installed(idx)) {
2942 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2944 OBJ(smapi_bat_perc) {
2946 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2947 val = smapi_bat_installed(idx) ?
2948 smapi_get_bat_int(idx, "remaining_percent") : 0;
2949 percent_print(p, p_max_size, val);
2951 NORM_ERR("argument to smapi_bat_perc must be an integer");
2953 OBJ(smapi_bat_temp) {
2955 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2956 val = smapi_bat_installed(idx) ?
2957 smapi_get_bat_int(idx, "temperature") : 0;
2958 /* temperature is in milli degree celsius */
2959 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2961 NORM_ERR("argument to smapi_bat_temp must be an integer");
2963 OBJ(smapi_bat_power) {
2965 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2966 val = smapi_bat_installed(idx) ?
2967 smapi_get_bat_int(idx, "power_now") : 0;
2968 /* power_now is in mW, set to W with one digit precision */
2969 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2971 NORM_ERR("argument to smapi_bat_power must be an integer");
2974 OBJ(smapi_bat_bar) {
2975 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2976 new_bar(p, obj->a, obj->b, (int)
2977 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2979 new_bar(p, obj->a, obj->b, 0);
2985 char buf[max_user_text];
2987 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2988 snprintf(p, p_max_size, "%s", buf);
2994 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2995 char buf[max_user_text];
2998 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2999 snprintf(p, p_max_size, "%s", buf);
3000 if(total_updates % 2) {
3001 for(j=0; p[j] != 0; j++) {
3007 char buf[max_user_text];
3009 char unit[16]; // 16 because we can also have long names (like mega-bytes)
3011 generate_text_internal(buf, max_user_text, *obj->sub, cur);
3012 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
3013 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
3014 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
3015 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
3016 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
3017 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
3019 snprintf(p, p_max_size, "%s", buf);
3022 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
3024 char buf[max_user_text];
3025 generate_text_internal(buf, max_user_text,
3027 for(j = 0; buf[j] != 0; j++) {
3029 case '\n': //place all the lines behind each other with LINESEPARATOR between them
3030 #define LINESEPARATOR '|'
3031 buf[j]=LINESEPARATOR;
3038 //no scrolling necessary if the length of the text to scroll is too short
3039 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
3040 snprintf(p, p_max_size, "%s", buf);
3043 //make sure a colorchange at the front is not part of the string we are going to show
3044 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
3045 obj->data.scroll.start++;
3047 //place all chars that should be visible in p, including colorchanges
3048 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
3049 p[j] = *(buf + obj->data.scroll.start + j);
3050 if(p[j] == SPECIAL_CHAR) {
3051 visibcolorchanges++;
3053 //if there is still room fill it with spaces
3056 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
3060 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
3061 for(j = 0; j < obj->data.scroll.start; j++) {
3062 if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
3064 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
3065 for(j = 0; j < frontcolorchanges; j++) {
3066 pwithcolors[j] = SPECIAL_CHAR;
3069 strcat(pwithcolors,p);
3070 strend = strlen(pwithcolors);
3071 //and place the colorchanges not in front or in the visible part behind the visible part
3072 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
3073 pwithcolors[strend + j] = SPECIAL_CHAR;
3075 pwithcolors[strend + j] = 0;
3076 strcpy(p, pwithcolors);
3079 obj->data.scroll.start += obj->data.scroll.step;
3080 if(buf[obj->data.scroll.start] == 0){
3081 obj->data.scroll.start = 0;
3084 //reset color when scroll is finished
3085 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
3089 char buf[2][max_user_text];
3096 struct llrows* next;
3098 struct llrows *ll_rows[2], *current[2];
3099 struct text_object * objsub = obj->sub;
3102 for(i=0; i<2; i++) {
3105 ll_rows[i] = malloc(sizeof(struct llrows));
3106 current[i] = ll_rows[i];
3107 for(j=0; j<i; j++) objsub = objsub->sub;
3108 generate_text_internal(buf[i], max_user_text, *objsub, cur);
3109 for(j=0; buf[i][j] != 0; j++) {
3110 if(buf[i][j] == '\t') buf[i][j] = ' ';
3111 if(buf[i][j] == '\n') {
3113 current[i]->row = strdup(buf[i]+nextstart);
3114 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3115 current[i]->next = malloc(sizeof(struct llrows));
3116 current[i] = current[i]->next;
3121 current[i]->row = strdup(buf[i]+nextstart);
3122 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3123 current[i]->next = NULL;
3124 current[i] = ll_rows[i];
3126 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
3128 strcat(p, current[0]->row);
3129 i=strlen(current[0]->row);
3131 while(i < longest) {
3136 strcat(p, obj->data.combine.seperation);
3137 strcat(p, current[1]->row);
3141 #pragma omp parallel for schedule(dynamic,10)
3142 #endif /* HAVE_OPENMP */
3143 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
3146 #pragma omp parallel for schedule(dynamic,10)
3147 #endif /* HAVE_OPENMP */
3148 for(i=0; i<2; i++) {
3149 while(ll_rows[i] != NULL) {
3150 current[i]=ll_rows[i];
3151 free(current[i]->row);
3152 ll_rows[i]=current[i]->next;
3159 int value = get_nvidia_value(obj->data.nvidia.type, display);
3161 snprintf(p, p_max_size, "N/A");
3162 else if (obj->data.nvidia.type == NV_TEMP)
3163 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
3164 else if (obj->data.nvidia.print_as_float &&
3165 value > 0 && value < 100)
3166 snprintf(p, p_max_size, "%.1f", (float)value);
3168 snprintf(p, p_max_size, "%d", value);
3173 /* This is just a meta-object to set host:port */
3176 snprintf(p, p_max_size, "%s",
3177 cur->apcupsd.items[APCUPSD_NAME]);
3179 OBJ(apcupsd_model) {
3180 snprintf(p, p_max_size, "%s",
3181 cur->apcupsd.items[APCUPSD_MODEL]);
3183 OBJ(apcupsd_upsmode) {
3184 snprintf(p, p_max_size, "%s",
3185 cur->apcupsd.items[APCUPSD_UPSMODE]);
3187 OBJ(apcupsd_cable) {
3188 snprintf(p, p_max_size, "%s",
3189 cur->apcupsd.items[APCUPSD_CABLE]);
3191 OBJ(apcupsd_status) {
3192 snprintf(p, p_max_size, "%s",
3193 cur->apcupsd.items[APCUPSD_STATUS]);
3195 OBJ(apcupsd_linev) {
3196 snprintf(p, p_max_size, "%s",
3197 cur->apcupsd.items[APCUPSD_LINEV]);
3200 snprintf(p, p_max_size, "%s",
3201 cur->apcupsd.items[APCUPSD_LOAD]);
3203 OBJ(apcupsd_loadbar) {
3206 if(output_methods & TO_X) {
3207 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3208 new_bar(p, obj->a, obj->b, (int) progress);
3211 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3212 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3213 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
3219 OBJ(apcupsd_loadgraph) {
3221 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3222 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3223 (int)progress, 100, 1, obj->char_a, obj->char_b);
3225 OBJ(apcupsd_loadgauge) {
3227 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3228 new_gauge(p, obj->a, obj->b,
3232 OBJ(apcupsd_charge) {
3233 snprintf(p, p_max_size, "%s",
3234 cur->apcupsd.items[APCUPSD_CHARGE]);
3236 OBJ(apcupsd_timeleft) {
3237 snprintf(p, p_max_size, "%s",
3238 cur->apcupsd.items[APCUPSD_TIMELEFT]);
3241 snprintf(p, p_max_size, "%s",
3242 cur->apcupsd.items[APCUPSD_TEMP]);
3244 OBJ(apcupsd_lastxfer) {
3245 snprintf(p, p_max_size, "%s",
3246 cur->apcupsd.items[APCUPSD_LASTXFER]);
3248 #endif /* APCUPSD */
3255 size_t a = strlen(p);
3258 iconv_convert(a, buff_in, p, p_max_size);
3259 #endif /* HAVE_ICONV */
3260 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi) {
3261 substitute_newlines(p, a - 2);
3269 /* load any new fonts we may have had */
3270 if (need_to_load_fonts) {
3276 void evaluate(const char *text, char *buffer)
3278 struct information *tmp_info;
3279 struct text_object subroot;
3281 tmp_info = malloc(sizeof(struct information));
3282 memcpy(tmp_info, &info, sizeof(struct information));
3283 parse_conky_vars(&subroot, text, buffer, tmp_info);
3284 DBGP("evaluated '%s' to '%s'", text, buffer);
3286 free_text_objects(&subroot, 1);
3290 double current_update_time, next_update_time, last_update_time;
3292 static void generate_text(void)
3294 struct information *cur = &info;
3301 current_update_time = get_time();
3305 /* add things to the buffer */
3311 generate_text_internal(p, max_user_text, global_root_object, cur);
3313 if (stuff_in_uppercase) {
3316 tmp_p = text_buffer;
3318 *tmp_p = toupper(*tmp_p);
3323 next_update_time += update_interval;
3324 if (next_update_time < get_time()) {
3325 next_update_time = get_time() + update_interval;
3326 } else if (next_update_time > get_time() + update_interval) {
3327 next_update_time = get_time() + update_interval;
3329 last_update_time = current_update_time;
3333 void set_update_interval(double interval)
3335 update_interval = interval;
3336 update_interval_old = interval;
3339 static inline int get_string_width(const char *s)
3342 if (output_methods & TO_X) {
3343 return *s ? calc_text_width(s, strlen(s)) : 0;
3350 static int get_string_width_special(char *s, int special_index)
3357 if ((output_methods & TO_X) == 0) {
3358 return (s) ? strlen(s) : 0;
3365 p = strndup(s, text_buffer_size);
3369 if (*p == SPECIAL_CHAR) {
3370 /* shift everything over by 1 so that the special char
3371 * doesn't mess up the size calculation */
3372 for (i = 0; i < (long)strlen(p); i++) {
3373 *(p + i) = *(p + i + 1);
3375 if (specials[special_index + idx].type == GRAPH
3376 || specials[special_index + idx].type == GAUGE
3377 || specials[special_index + idx].type == BAR) {
3378 width += specials[special_index + idx].width;
3381 } else if (*p == SECRIT_MULTILINE_CHAR) {
3388 if (strlen(final) > 1) {
3389 width += calc_text_width(final, strlen(final));
3395 static int text_size_updater(char *s, int special_index);
3397 int last_font_height;
3398 static void update_text_area(void)
3402 if ((output_methods & TO_X) == 0)
3404 /* update text size if it isn't fixed */
3409 text_width = minimum_width;
3411 last_font_height = font_height();
3412 for_each_line(text_buffer, text_size_updater);
3414 if (text_height < minimum_height) {
3415 text_height = minimum_height;
3417 if (text_width > maximum_width && maximum_width > 0) {
3418 text_width = maximum_width;
3422 /* get text position on workarea */
3423 switch (text_alignment) {
3430 x = workarea[2] - text_width - gap_x;
3435 x = workarea[2] / 2 - text_width / 2 - gap_x;
3442 y = workarea[3] - text_height - gap_y;
3446 x = workarea[2] - text_width - gap_x;
3447 y = workarea[3] - text_height - gap_y;
3451 x = workarea[2] / 2 - text_width / 2 - gap_x;
3452 y = workarea[3] - text_height - gap_y;
3457 y = workarea[3] / 2 - text_height / 2 - gap_y;
3461 x = workarea[2] - text_width - gap_x;
3462 y = workarea[3] / 2 - text_height / 2 - gap_y;
3466 case NONE: // Let the WM manage the window
3477 if (own_window && !fixed_pos) {
3480 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
3481 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
3482 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3483 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3487 /* If window size doesn't match to workarea's size,
3488 * then window probably includes panels (gnome).
3489 * Blah, doesn't work on KDE. */
3490 if (workarea[2] != window.width || workarea[3] != window.height) {
3499 /* update lua window globals */
3500 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3501 #endif /* HAVE_LUA */
3506 static int cur_x, cur_y; /* current x and y for drawing */
3508 //draw_mode also without X11 because we only need to print to stdout with FG
3509 static int draw_mode; /* FG, BG or OUTLINE */
3511 static long current_color;
3513 static int text_size_updater(char *s, int special_index)
3518 if ((output_methods & TO_X) == 0)
3520 /* get string widths and skip specials */
3523 if (*p == SPECIAL_CHAR) {
3525 w += get_string_width(s);
3528 if (specials[special_index].type == BAR
3529 || specials[special_index].type == GAUGE
3530 || specials[special_index].type == GRAPH) {
3531 w += specials[special_index].width;
3532 if (specials[special_index].height > last_font_height) {
3533 last_font_height = specials[special_index].height;
3534 last_font_height += font_height();
3536 } else if (specials[special_index].type == OFFSET) {
3537 if (specials[special_index].arg > 0) {
3538 w += specials[special_index].arg;
3540 } else if (specials[special_index].type == VOFFSET) {
3541 last_font_height += specials[special_index].arg;
3542 } else if (specials[special_index].type == GOTO) {
3543 if (specials[special_index].arg > cur_x) {
3544 w = (int) specials[special_index].arg;
3546 } else if (specials[special_index].type == TAB) {
3547 int start = specials[special_index].arg;
3548 int step = specials[special_index].width;
3550 if (!step || step < 0) {
3553 w += step - (cur_x - text_start_x - start) % step;
3554 } else if (specials[special_index].type == FONT) {
3555 selected_font = specials[special_index].font_added;
3556 if (font_height() > last_font_height) {
3557 last_font_height = font_height();
3563 } else if (*p == SECRIT_MULTILINE_CHAR) {
3566 lw = get_string_width(s);
3567 *p = SECRIT_MULTILINE_CHAR;
3569 w = lw > w ? lw : w;
3570 text_height += last_font_height;
3574 w += get_string_width(s);
3575 if (w > text_width) {
3578 if (text_width > maximum_width && maximum_width) {
3579 text_width = maximum_width;
3582 text_height += last_font_height;
3583 last_font_height = font_height();
3584 return special_index;
3588 static inline void set_foreground_color(long c)
3591 if (output_methods & TO_X) {
3593 XSetForeground(display, window.gc, c);
3597 if (output_methods & TO_NCURSES) {
3598 attron(COLOR_PAIR(c));
3600 #endif /* NCURSES */
3605 static void draw_string(const char *s)
3607 int i, i2, pos, width_of_s;
3610 char *s_with_newlines;
3616 width_of_s = get_string_width(s);
3617 s_with_newlines = strdup(s);
3618 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
3619 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
3620 s_with_newlines[i] = '\n';
3623 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
3624 printf("%s\n", s_with_newlines);
3625 if (extra_newline) fputc('\n', stdout);
3626 fflush(stdout); /* output immediately, don't buffer */
3628 if ((output_methods & TO_STDERR) && draw_mode == FG) {
3629 fprintf(stderr, "%s\n", s_with_newlines);
3630 fflush(stderr); /* output immediately, don't buffer */
3632 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
3633 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
3635 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
3636 fprintf(append_fpointer, "%s\n", s_with_newlines);
3639 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
3640 printw("%s", s_with_newlines);
3643 free(s_with_newlines);
3644 memset(tmpstring1, 0, text_buffer_size);
3645 memset(tmpstring2, 0, text_buffer_size);
3646 strncpy(tmpstring1, s, text_buffer_size - 1);
3651 if (output_methods & TO_X) {
3652 max = ((text_width - width_of_s) / get_string_width(" "));
3655 /* This code looks for tabs in the text and coverts them to spaces.
3656 * The trick is getting the correct number of spaces, and not going
3657 * over the window's size without forcing the window larger. */
3658 for (i = 0; i < (int) text_buffer_size; i++) {
3659 if (tmpstring1[i] == '\t') {
3661 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
3662 /* guard against overrun */
3663 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
3668 /* guard against overrun */
3669 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
3674 if (output_methods & TO_X) {
3675 if (text_width == maximum_width) {
3676 /* this means the text is probably pushing the limit,
3677 * so we'll chop it */
3678 while (cur_x + get_string_width(tmpstring2) - text_start_x
3679 > maximum_width && strlen(tmpstring2) > 0) {
3680 tmpstring2[strlen(tmpstring2) - 1] = '\0';
3687 if (output_methods & TO_X) {
3693 c.pixel = current_color;
3694 XQueryColor(display, DefaultColormap(display, screen), &c);
3697 c2.color.red = c.red;
3698 c2.color.green = c.green;
3699 c2.color.blue = c.blue;
3700 c2.color.alpha = fonts[selected_font].font_alpha;
3702 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3703 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3705 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3706 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3711 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
3714 cur_x += width_of_s;
3717 memcpy(tmpstring1, s, text_buffer_size);
3720 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
3723 int font_h = font_height();
3728 int last_special_needed = -1;
3729 int orig_special_index = special_index;
3732 cur_x = text_start_x;
3733 cur_y += font_ascent();
3737 if (*p == SECRIT_MULTILINE_CHAR) {
3738 /* special newline marker for multiline objects */
3743 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3748 /* draw string before special, unless we're dealing multiline
3750 if (last_special_applied > -1) {
3751 special_index = last_special_applied;
3759 switch (specials[special_index].type) {
3761 case HORIZONTAL_LINE:
3763 int h = specials[special_index].height;
3764 int mid = font_ascent() / 2;
3766 w = text_start_x + text_width - cur_x;
3768 XSetLineAttributes(display, window.gc, h, LineSolid,
3769 CapButt, JoinMiter);
3770 XDrawLine(display, window.drawable, window.gc, cur_x,
3771 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3777 int h = specials[special_index].height;
3778 int tmp_s = specials[special_index].arg;
3779 int mid = font_ascent() / 2;
3780 char ss[2] = { tmp_s, tmp_s };
3782 w = text_start_x + text_width - cur_x - 1;
3783 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3784 CapButt, JoinMiter);
3785 XSetDashes(display, window.gc, 0, ss, 2);
3786 XDrawLine(display, window.drawable, window.gc, cur_x,
3787 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3793 int h, bar_usage, by;
3794 if (cur_x - text_start_x > maximum_width
3795 && maximum_width > 0) {
3798 h = specials[special_index].height;
3799 bar_usage = specials[special_index].arg;
3800 by = cur_y - (font_ascent() / 2) - 1;
3805 w = specials[special_index].width;
3807 w = text_start_x + text_width - cur_x - 1;
3813 XSetLineAttributes(display, window.gc, 1, LineSolid,
3814 CapButt, JoinMiter);
3816 XDrawRectangle(display, window.drawable, window.gc, cur_x,
3818 XFillRectangle(display, window.drawable, window.gc, cur_x,
3819 by, w * bar_usage / 255, h);
3827 case GAUGE: /* new GAUGE */
3830 unsigned long last_colour = current_color;
3832 float angle, px, py;
3836 if (cur_x - text_start_x > maximum_width
3837 && maximum_width > 0) {
3841 h = specials[special_index].height;
3842 by = cur_y - (font_ascent() / 2) - 1;
3847 w = specials[special_index].width;
3849 w = text_start_x + text_width - cur_x - 1;
3855 XSetLineAttributes(display, window.gc, 1, LineSolid,
3856 CapButt, JoinMiter);
3858 XDrawArc(display, window.drawable, window.gc,
3859 cur_x, by, w, h * 2, 0, 180*64);
3862 usage = specials[special_index].arg;
3863 angle = (M_PI)*(float)(usage)/255.;
3864 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3865 py = (float)(by+(h))-(float)(h)*sin(angle);
3867 XDrawLine(display, window.drawable, window.gc,
3868 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3876 set_foreground_color(last_colour);
3884 int h, by, i = 0, j = 0;
3886 unsigned long last_colour = current_color;
3887 unsigned long *tmpcolour = 0;
3888 if (cur_x - text_start_x > maximum_width
3889 && maximum_width > 0) {
3892 h = specials[special_index].height;
3893 by = cur_y - (font_ascent() / 2) - 1;
3898 w = specials[special_index].width;
3900 w = text_start_x + text_width - cur_x - 1;
3905 if (draw_graph_borders) {
3906 XSetLineAttributes(display, window.gc, 1, LineSolid,
3907 CapButt, JoinMiter);
3908 XDrawRectangle(display, window.drawable, window.gc,
3911 XSetLineAttributes(display, window.gc, 1, LineSolid,
3912 CapButt, JoinMiter);
3914 if (specials[special_index].last_colour != 0
3915 || specials[special_index].first_colour != 0) {
3916 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3919 for (i = w - 2; i > -1; i--) {
3920 if (specials[special_index].last_colour != 0
3921 || specials[special_index].first_colour != 0) {
3922 if (specials[special_index].tempgrad) {
3925 (int)((float)(w - 2) - specials[special_index].graph[j] *
3926 (w - 2) / (float)specials[special_index].graph_scale)
3930 (int)((float)(w - 2) - specials[special_index].graph[j] *
3931 (w - 2) / (float)specials[special_index].graph_scale)
3934 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3936 (int)((float)(w - 2) - specials[special_index].graph[j] *
3937 (w - 2) / (float)specials[special_index].graph_scale)
3941 #endif /* DEBUG_lol */
3942 XSetForeground(display, window.gc, tmpcolour[
3943 (int)((float)(w - 2) - specials[special_index].graph[j] *
3944 (w - 2) / (float)specials[special_index].graph_scale)
3947 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3950 /* this is mugfugly, but it works */
3951 XDrawLine(display, window.drawable, window.gc,
3952 cur_x + i + 1, by + h, cur_x + i + 1,
3953 round_to_int((double)by + h - specials[special_index].graph[j] *
3954 (h - 1) / specials[special_index].graph_scale));
3955 if ((w - i) / ((float) (w - 2) /
3956 (specials[special_index].graph_width)) > j
3957 && j < MAX_GRAPH_DEPTH - 3) {
3961 if (tmpcolour) free(tmpcolour);
3966 /* if (draw_mode == BG) {
3967 set_foreground_color(default_bg_color);
3968 } else if (draw_mode == OUTLINE) {
3969 set_foreground_color(default_out_color);
3971 set_foreground_color(default_fg_color);
3973 if (show_graph_range) {
3976 unsigned short int seconds = update_interval * w;
3982 unsigned short int timeunits;
3984 timeunits = seconds / 86400; seconds %= 86400;
3985 if (timeunits > 0) {
3986 asprintf(&tmp_day_str, "%dd", timeunits);
3988 tmp_day_str = strdup("");
3990 timeunits = seconds / 3600; seconds %= 3600;
3991 if (timeunits > 0) {
3992 asprintf(&tmp_hour_str, "%dh", timeunits);
3994 tmp_hour_str = strdup("");
3996 timeunits = seconds / 60; seconds %= 60;
3997 if (timeunits > 0) {
3998 asprintf(&tmp_min_str, "%dm", timeunits);
4000 tmp_min_str = strdup("");
4003 asprintf(&tmp_sec_str, "%ds", seconds);
4005 tmp_sec_str = strdup("");
4007 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
4008 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
4010 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
4012 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
4013 cur_y += font_h / 2;
4014 draw_string(tmp_str);
4020 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
4024 cur_x += font_ascent() / 2;
4025 cur_y += font_h / 2;
4027 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
4029 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
4030 draw_string(tmp_str);
4036 set_foreground_color(last_colour);
4042 int old = font_ascent();
4044 cur_y -= font_ascent();
4045 selected_font = specials[special_index].font_added;
4047 if (cur_y + font_ascent() < cur_y + old) {
4050 cur_y += font_ascent();
4052 font_h = font_height();
4057 if (draw_mode == FG) {
4058 set_foreground_color(specials[special_index].arg);
4064 if (draw_mode == BG) {
4065 set_foreground_color(specials[special_index].arg);
4070 if (draw_mode == OUTLINE) {
4071 set_foreground_color(specials[special_index].arg);
4076 w += specials[special_index].arg;
4077 last_special_needed = special_index;
4081 cur_y += specials[special_index].arg;
4085 if (specials[special_index].arg >= 0) {
4086 cur_x = (int) specials[special_index].arg;
4088 last_special_needed = special_index;
4093 int start = specials[special_index].arg;
4094 int step = specials[special_index].width;
4096 if (!step || step < 0) {
4099 w = step - (cur_x - text_start_x - start) % step;
4100 last_special_needed = special_index;
4106 /* TODO: add back in "+ window.border_inner_margin" to the end of
4108 int pos_x = text_start_x + text_width -
4109 get_string_width_special(s, special_index);
4111 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4112 "get_string_width(p) %i gap_x %i "
4113 "specials[special_index].arg %i window.border_inner_margin %i "
4114 "window.border_width %i\n", pos_x, text_start_x, text_width,
4115 cur_x, get_string_width_special(s), gap_x,
4116 specials[special_index].arg, window.border_inner_margin,
4117 window.border_width); */
4118 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
4119 cur_x = pos_x - specials[special_index].arg;
4121 last_special_needed = special_index;
4127 int pos_x = (text_width) / 2 - get_string_width_special(s,
4128 special_index) / 2 - (cur_x -
4130 /* int pos_x = text_start_x + text_width / 2 -
4131 get_string_width_special(s) / 2; */
4133 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4134 "get_string_width(p) %i gap_x %i "
4135 "specials[special_index].arg %i\n", pos_x, text_start_x,
4136 text_width, cur_x, get_string_width(s), gap_x,
4137 specials[special_index].arg); */
4138 if (pos_x > specials[special_index].arg) {
4139 w = pos_x - specials[special_index].arg;
4141 last_special_needed = special_index;
4151 if (special_index != last_special_applied) {
4154 special_index = orig_special_index;
4155 last_special_applied = -1;
4166 if (output_methods & TO_NCURSES) {
4169 #endif /* NCURSES */
4171 cur_y += font_descent();
4173 if (recurse && *recurse) {
4174 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
4175 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
4177 return special_index;
4180 static int draw_line(char *s, int special_index)
4183 if (output_methods & TO_X) {
4184 return draw_each_line_inner(s, special_index, -1);
4188 if (output_methods & TO_NCURSES) {
4189 return draw_each_line_inner(s, special_index, -1);
4191 #endif /* NCURSES */
4193 UNUSED(special_index);
4197 static void draw_text(void)
4201 llua_draw_pre_hook();
4202 #endif /* HAVE_LUA */
4203 if (output_methods & TO_X) {
4204 cur_y = text_start_y;
4207 if (draw_borders && window.border_width > 0) {
4208 if (stippled_borders) {
4209 char ss[2] = { stippled_borders, stippled_borders };
4210 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
4211 CapButt, JoinMiter);
4212 XSetDashes(display, window.gc, 0, ss, 2);
4214 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
4215 CapButt, JoinMiter);
4218 XDrawRectangle(display, window.drawable, window.gc,
4219 text_start_x - window.border_inner_margin - window.border_width,
4220 text_start_y - window.border_inner_margin - window.border_width,
4221 text_width + window.border_inner_margin * 2 + window.border_width * 2,
4222 text_height + window.border_inner_margin * 2 + window.border_width * 2);
4230 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
4231 attron(COLOR_PAIR(COLOR_WHITE));
4232 #endif /* NCURSES */
4233 for_each_line(text_buffer, draw_line);
4234 #if defined(HAVE_LUA) && defined(X11)
4235 llua_draw_post_hook();
4236 #endif /* HAVE_LUA */
4239 static void draw_stuff(void)
4242 cimlib_render(text_start_x, text_start_y, window.width, window.height);
4244 if (overwrite_file) {
4245 overwrite_fpointer = fopen(overwrite_file, "w");
4246 if(!overwrite_fpointer)
4247 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
4250 append_fpointer = fopen(append_file, "a");
4251 if(!append_fpointer)
4252 NORM_ERR("Can't append '%s' anymore", append_file);
4255 if (output_methods & TO_X) {
4257 if (draw_shades && !draw_outline) {
4260 set_foreground_color(default_bg_color);
4271 for (i = -1; i < 2; i++) {
4272 for (j = -1; j < 2; j++) {
4273 if (i == 0 && j == 0) {
4278 set_foreground_color(default_out_color);
4279 draw_mode = OUTLINE;
4287 set_foreground_color(default_fg_color);
4293 xdbe_swap_buffers();
4294 if (output_methods & TO_X) {
4299 if(overwrite_fpointer) {
4300 fclose(overwrite_fpointer);
4301 overwrite_fpointer = 0;
4303 if(append_fpointer) {
4304 fclose(append_fpointer);
4305 append_fpointer = 0;
4310 static void clear_text(int exposures)
4314 /* The swap action is XdbeBackground, which clears */
4318 if (display && window.window) { // make sure these are !null
4319 /* there is some extra space for borders and outlines */
4320 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4321 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4322 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4323 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
4328 static int need_to_update;
4330 /* update_text() generates new text and clears old text area */
4331 static void update_text(void)
4338 if (output_methods & TO_X)
4343 llua_update_info(&info, update_interval);
4344 #endif /* HAVE_LUA */
4347 #ifdef HAVE_SYS_INOTIFY_H
4351 static void main_loop(void)
4354 #ifdef SIGNAL_BLOCKING
4355 sigset_t newmask, oldmask;
4358 #ifdef HAVE_SYS_INOTIFY_H
4359 int inotify_config_wd = -1;
4360 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
4361 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
4362 char inotify_buff[INOTIFY_BUF_LEN];
4363 #endif /* HAVE_SYS_INOTIFY_H */
4366 #ifdef SIGNAL_BLOCKING
4367 sigemptyset(&newmask);
4368 sigaddset(&newmask, SIGINT);
4369 sigaddset(&newmask, SIGTERM);
4370 sigaddset(&newmask, SIGUSR1);
4373 last_update_time = 0.0;
4374 next_update_time = get_time();
4376 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
4377 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
4378 char buf[max_user_text];
4380 get_battery_short_status(buf, max_user_text, "BAT0");
4382 update_interval = update_interval_bat;
4384 update_interval = update_interval_old;
4389 #ifdef SIGNAL_BLOCKING
4390 /* block signals. we will inspect for pending signals later */
4391 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
4392 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4397 if (output_methods & TO_X) {
4400 /* wait for X event or timeout */
4402 if (!XPending(display)) {
4406 t = next_update_time - get_time();
4410 } else if (t > update_interval) {
4411 t = update_interval;
4414 tv.tv_sec = (long) t;
4415 tv.tv_usec = (long) (t * 1000000) % 1000000;
4417 FD_SET(ConnectionNumber(display), &fdsr);
4419 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
4421 if (errno != EINTR) {
4422 NORM_ERR("can't select(): %s", strerror(errno));
4432 if (need_to_update) {
4434 int wx = window.x, wy = window.y;
4444 /* resize window if it isn't right size */
4446 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
4447 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
4448 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4449 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4450 draw_stuff(); /* redraw everything in our newly sized window */
4451 XResizeWindow(display, window.window, window.width,
4452 window.height); /* resize window */
4453 set_transparent_background(window.window);
4456 xdbe_swap_buffers();
4461 /* update lua window globals */
4462 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
4463 #endif /* HAVE_LUA */
4466 /* move window if it isn't in right position */
4467 if (!fixed_pos && (window.x != wx || window.y != wy)) {
4468 XMoveWindow(display, window.window, window.x, window.y);
4473 if (changed && window.type == TYPE_PANEL) {
4476 fprintf(stderr, PACKAGE_NAME": defining struts\n");
4479 switch (text_alignment) {
4506 set_struts(sidenum);
4517 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4518 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4519 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4520 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4521 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4526 /* handle X events */
4527 while (XPending(display)) {
4530 XNextEvent(display, &ev);
4537 r.width = ev.xexpose.width;
4538 r.height = ev.xexpose.height;
4539 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4543 case PropertyNotify:
4545 if ( ev.xproperty.state == PropertyNewValue ) {
4546 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
4552 case ReparentNotify:
4553 /* set background to ParentRelative for all parents */
4555 set_transparent_background(window.window);
4559 case ConfigureNotify:
4561 /* if window size isn't what expected, set fixed size */
4562 if (ev.xconfigure.width != window.width
4563 || ev.xconfigure.height != window.height) {
4564 if (window.width != 0 && window.height != 0) {
4568 /* clear old stuff before screwing up
4573 XWindowAttributes attrs;
4574 if (XGetWindowAttributes(display,
4575 window.window, &attrs)) {
4576 window.width = attrs.width;
4577 window.height = attrs.height;
4581 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4582 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4583 if (text_width > maximum_width
4584 && maximum_width > 0) {
4585 text_width = maximum_width;
4589 /* if position isn't what expected, set fixed pos
4590 * total_updates avoids setting fixed_pos when window
4591 * is set to weird locations when started */
4592 /* // this is broken
4593 if (total_updates >= 2 && !fixed_pos
4594 && (window.x != ev.xconfigure.x
4595 || window.y != ev.xconfigure.y)
4596 && (ev.xconfigure.x != 0
4597 || ev.xconfigure.y != 0)) {
4605 /* if an ordinary window with decorations */
4606 if ((window.type == TYPE_NORMAL &&
4607 (!TEST_HINT(window.hints,
4608 HINT_UNDECORATED))) ||
4609 window.type == TYPE_DESKTOP) {
4610 /* allow conky to hold input focus. */
4613 /* forward the click to the desktop window */
4614 XUngrabPointer(display, ev.xbutton.time);
4615 ev.xbutton.window = window.desktop;
4616 ev.xbutton.x = ev.xbutton.x_root;
4617 ev.xbutton.y = ev.xbutton.y_root;
4618 XSendEvent(display, ev.xbutton.window, False,
4619 ButtonPressMask, &ev);
4620 XSetInputFocus(display, ev.xbutton.window,
4621 RevertToParent, ev.xbutton.time);
4628 /* if an ordinary window with decorations */
4629 if ((window.type == TYPE_NORMAL)
4630 && (!TEST_HINT(window.hints,
4631 HINT_UNDECORATED))) {
4632 /* allow conky to hold input focus. */
4635 /* forward the release to the desktop window */
4636 ev.xbutton.window = window.desktop;
4637 ev.xbutton.x = ev.xbutton.x_root;
4638 ev.xbutton.y = ev.xbutton.y_root;
4639 XSendEvent(display, ev.xbutton.window, False,
4640 ButtonReleaseMask, &ev);
4649 if (ev.type == x11_stuff.event_base + XDamageNotify) {
4650 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
4652 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
4653 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
4655 #endif /* HAVE_XDAMAGE */
4661 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
4662 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
4663 #endif /* HAVE_XDAMAGE */
4665 /* XDBE doesn't seem to provide a way to clear the back buffer
4666 * without interfering with the front buffer, other than passing
4667 * XdbeBackground to XdbeSwapBuffers. That means that if we're
4668 * using XDBE, we need to redraw the text even if it wasn't part of
4669 * the exposed area. OTOH, if we're not going to call draw_stuff at
4670 * all, then no swap happens and we can safely do nothing. */
4672 if (!XEmptyRegion(x11_stuff.region)) {
4677 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4678 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4679 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4680 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4681 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4684 XSetRegion(display, window.gc, x11_stuff.region);
4687 XftDrawSetClip(window.xftdraw, x11_stuff.region);
4691 XDestroyRegion(x11_stuff.region);
4692 x11_stuff.region = XCreateRegion();
4696 t = (next_update_time - get_time()) * 1000000;
4697 if(t > 0) usleep((useconds_t)t);
4701 if(output_methods & TO_NCURSES) {
4710 #ifdef SIGNAL_BLOCKING
4711 /* unblock signals of interest and let handler fly */
4712 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4713 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4717 switch (g_signal_pending) {
4720 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4725 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4728 if (output_methods & TO_X) {
4729 XDestroyRegion(x11_stuff.region);
4730 x11_stuff.region = NULL;
4732 XDamageDestroy(display, x11_stuff.damage);
4733 XFixesDestroyRegion(display, x11_stuff.region2);
4734 XFixesDestroyRegion(display, x11_stuff.part);
4735 #endif /* HAVE_XDAMAGE */
4741 if(overwrite_file) {
4742 free(overwrite_file);
4751 /* Reaching here means someone set a signal
4752 * (SIGXXXX, signal_handler), but didn't write any code
4754 * If you don't want to handle a signal, don't set a handler on
4755 * it in the first place. */
4756 if (g_signal_pending) {
4757 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4761 #ifdef HAVE_SYS_INOTIFY_H
4762 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4763 inotify_config_wd = inotify_add_watch(inotify_fd,
4767 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4768 int len = 0, idx = 0;
4770 struct timeval time_to_wait;
4772 FD_ZERO(&descriptors);
4773 FD_SET(inotify_fd, &descriptors);
4775 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4777 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4778 if (FD_ISSET(inotify_fd, &descriptors)) {
4779 /* process inotify events */
4780 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4781 while (len > 0 && idx < len) {
4782 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4783 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4784 /* current_config should be reloaded */
4785 NORM_ERR("'%s' modified, reloading...", current_config);
4787 if (ev->mask & IN_IGNORED) {
4788 /* for some reason we get IN_IGNORED here
4789 * sometimes, so we need to re-add the watch */
4790 inotify_config_wd = inotify_add_watch(inotify_fd,
4797 llua_inotify_query(ev->wd, ev->mask);
4799 #endif /* HAVE_LUA */
4800 idx += INOTIFY_EVENT_SIZE + ev->len;
4804 #endif /* HAVE_SYS_INOTIFY_H */
4807 llua_update_info(&info, update_interval);
4808 #endif /* HAVE_LUA */
4809 g_signal_pending = 0;
4811 clean_up(NULL, NULL);
4813 #ifdef HAVE_SYS_INOTIFY_H
4814 if (inotify_fd != -1) {
4815 inotify_rm_watch(inotify_fd, inotify_config_wd);
4817 inotify_fd = inotify_config_wd = 0;
4819 #endif /* HAVE_SYS_INOTIFY_H */
4823 static void load_config_file_x11(const char *);
4825 void initialisation(int argc, char** argv);
4827 /* reload the config file */
4828 static void reload_config(void)
4830 char *current_config_copy = strdup(current_config);
4831 clean_up(NULL, NULL);
4832 current_config = current_config_copy;
4833 initialisation(argc_copy, argv_copy);
4836 void clean_up(void *memtofree1, void* memtofree2)
4841 if(output_methods & TO_NCURSES) {
4845 conftree_empty(currentconffile);
4846 currentconffile = NULL;
4853 timed_thread_destroy_registered_threads();
4855 if (info.cpu_usage) {
4856 free(info.cpu_usage);
4857 info.cpu_usage = NULL;
4860 if (x_initialised == YES) {
4861 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4862 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4863 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4864 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4867 if(x11_stuff.region) {
4868 XDestroyRegion(x11_stuff.region);
4869 x11_stuff.region = NULL;
4871 XCloseDisplay(display);
4873 if(info.x11.desktop.all_names) {
4874 free(info.x11.desktop.all_names);
4875 info.x11.desktop.all_names = NULL;
4877 if (info.x11.desktop.name) {
4878 free(info.x11.desktop.name);
4879 info.x11.desktop.name = NULL;
4883 free(fonts); //in set_default_configurations a font is set but not loaded
4889 for (i = 0; i < MAX_TEMPLATES; i++) {
4896 free_text_objects(&global_root_object, 0);
4915 free(current_config);
4918 #ifdef TCP_PORT_MONITOR
4919 tcp_portmon_clear();
4928 weather_free_info();
4931 llua_shutdown_hook();
4933 #endif /* HAVE_LUA */
4942 for (i = 0; i < special_count; i++) {
4943 if (specials[i].type == GRAPH) {
4944 free(specials[i].graph);
4952 clear_diskio_stats();
4953 if(global_cpu != NULL) {
4959 static int string_to_bool(const char *s)
4962 // Assumes an option without a true/false means true
4964 } else if (strcasecmp(s, "yes") == EQUAL) {
4966 } else if (strcasecmp(s, "true") == EQUAL) {
4968 } else if (strcasecmp(s, "1") == EQUAL) {
4975 static enum alignment string_to_alignment(const char *s)
4977 if (strcasecmp(s, "top_left") == EQUAL) {
4979 } else if (strcasecmp(s, "top_right") == EQUAL) {
4981 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4983 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4985 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4986 return BOTTOM_RIGHT;
4987 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4988 return BOTTOM_MIDDLE;
4989 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4991 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4992 return MIDDLE_RIGHT;
4993 } else if (strcasecmp(s, "tl") == EQUAL) {
4995 } else if (strcasecmp(s, "tr") == EQUAL) {
4997 } else if (strcasecmp(s, "tm") == EQUAL) {
4999 } else if (strcasecmp(s, "bl") == EQUAL) {
5001 } else if (strcasecmp(s, "br") == EQUAL) {
5002 return BOTTOM_RIGHT;
5003 } else if (strcasecmp(s, "bm") == EQUAL) {
5004 return BOTTOM_MIDDLE;
5005 } else if (strcasecmp(s, "ml") == EQUAL) {
5007 } else if (strcasecmp(s, "mr") == EQUAL) {
5008 return MIDDLE_RIGHT;
5009 } else if (strcasecmp(s, "none") == EQUAL) {
5017 static void set_default_configurations_for_x(void)
5019 default_fg_color = WhitePixel(display, screen);
5020 default_bg_color = BlackPixel(display, screen);
5021 default_out_color = BlackPixel(display, screen);
5022 color0 = default_fg_color;
5023 color1 = default_fg_color;
5024 color2 = default_fg_color;
5025 color3 = default_fg_color;
5026 color4 = default_fg_color;
5027 color5 = default_fg_color;
5028 color6 = default_fg_color;
5029 color7 = default_fg_color;
5030 color8 = default_fg_color;
5031 color9 = default_fg_color;
5032 current_text_color = default_fg_color;
5036 static void set_default_configurations(void)
5044 fork_to_background = 0;
5045 total_run_times = 0;
5046 info.cpu_avg_samples = 2;
5047 info.net_avg_samples = 2;
5048 info.diskio_avg_samples = 2;
5053 format_human_readable = 1;
5063 mpd_env_host = getenv("MPD_HOST");
5064 mpd_env_port = getenv("MPD_PORT");
5066 if (!mpd_env_host || !strlen(mpd_env_host)) {
5067 mpd_set_host("localhost");
5069 /* MPD_HOST environment variable is set */
5070 char *mpd_hostpart = strchr(mpd_env_host, '@');
5071 if (!mpd_hostpart) {
5072 mpd_set_host(mpd_env_host);
5074 /* MPD_HOST contains a password */
5075 char mpd_password[mpd_hostpart - mpd_env_host + 1];
5076 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
5078 if (!strlen(mpd_hostpart + 1)) {
5079 mpd_set_host("localhost");
5081 mpd_set_host(mpd_hostpart + 1);
5084 mpd_set_password(mpd_password, 1);
5089 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
5090 /* failed to set port from environment variable */
5091 mpd_set_port("6600");
5095 info.xmms2.artist = NULL;
5096 info.xmms2.album = NULL;
5097 info.xmms2.title = NULL;
5098 info.xmms2.genre = NULL;
5099 info.xmms2.comment = NULL;
5100 info.xmms2.url = NULL;
5101 info.xmms2.status = NULL;
5102 info.xmms2.playlist = NULL;
5104 use_spacer = NO_SPACER;
5106 output_methods = TO_X;
5108 output_methods = TO_STDOUT;
5111 show_graph_scale = 0;
5112 show_graph_range = 0;
5115 draw_graph_borders = 1;
5117 set_first_font("6x10");
5125 window.type = TYPE_NORMAL;
5127 strcpy(window.class_name, PACKAGE_NAME);
5128 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
5130 stippled_borders = 0;
5131 window.border_inner_margin = 3;
5132 window.border_outer_margin = 1;
5133 window.border_width = 1;
5134 text_alignment = BOTTOM_LEFT;
5135 info.x11.monitor.number = 1;
5136 info.x11.monitor.current = 0;
5137 info.x11.desktop.current = 1;
5138 info.x11.desktop.number = 1;
5139 info.x11.desktop.nitems = 0;
5140 info.x11.desktop.all_names = NULL;
5141 info.x11.desktop.name = NULL;
5144 for (i = 0; i < MAX_TEMPLATES; i++) {
5147 template[i] = strdup("");
5150 free(current_mail_spool);
5154 variable_substitute(MAIL_FILE, buf, 256);
5155 if (buf[0] != '\0') {
5156 current_mail_spool = strndup(buf, text_buffer_size);
5161 set_update_interval(3);
5162 update_interval_bat = NOBATTERY;
5163 info.music_player_interval = 1.0;
5164 stuff_in_uppercase = 0;
5165 info.users.number = 1;
5167 #ifdef TCP_PORT_MONITOR
5168 /* set default connection limit */
5169 tcp_portmon_set_max_connections(0);
5173 /* returns 1 if you can overwrite or create the file at 'path' */
5174 static _Bool overwrite_works(const char *path)
5178 if (!(filepointer = fopen(path, "w")))
5180 fclose(filepointer);
5184 /* returns 1 if you can append or create the file at 'path' */
5185 static _Bool append_works(const char *path)
5189 if (!(filepointer = fopen(path, "a")))
5191 fclose(filepointer);
5197 /* WARNING, this type not in Xlib spec */
5198 int x11_error_handler(Display *d, XErrorEvent *err)
5199 __attribute__((noreturn));
5200 int x11_error_handler(Display *d, XErrorEvent *err)
5202 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",
5204 (long unsigned)err->display,
5205 (long)err->resourceid,
5215 int x11_ioerror_handler(Display *d)
5216 __attribute__((noreturn));
5217 int x11_ioerror_handler(Display *d)
5219 NORM_ERR("X Error: Display %lx\n",
5226 static void X11_initialisation(void)
5228 if (x_initialised == YES) return;
5229 output_methods |= TO_X;
5231 set_default_configurations_for_x();
5232 x_initialised = YES;
5235 /* WARNING, this type not in Xlib spec */
5236 XSetErrorHandler(&x11_error_handler);
5237 XSetIOErrorHandler(&x11_ioerror_handler);
5241 static char **xargv = 0;
5242 static int xargc = 0;
5244 static void X11_create_window(void)
5246 if (output_methods & TO_X) {
5248 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5249 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
5251 #else /* OWN_WINDOW */
5252 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5253 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
5255 #endif /* OWN_WINDOW */
5259 update_text_area(); /* to position text/window on screen */
5262 if (own_window && !fixed_pos) {
5263 XMoveWindow(display, window.window, window.x, window.y);
5266 set_transparent_background(window.window);
5274 x11_stuff.region = XCreateRegion();
5276 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
5277 NORM_ERR("Xdamage extension unavailable");
5279 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5280 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5281 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
5282 #endif /* HAVE_XDAMAGE */
5285 update_text_area(); /* to get initial size of the window */
5288 /* setup lua window globals */
5289 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
5290 #endif /* HAVE_LUA */
5294 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
5295 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
5296 #define CONF2(a) if (strcasecmp(name, a) == 0)
5297 #define CONF(a) else CONF2(a)
5298 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
5299 || strcasecmp(name, b) == 0)
5300 #define CONF_CONTINUE 1
5301 #define CONF_BREAK 2
5302 #define CONF_BUFF_SIZE 512
5304 static FILE *open_config_file(const char *f)
5306 #ifdef CONFIG_OUTPUT
5307 if (!strcmp(f, "==builtin==")) {
5308 return conf_cookie_open();
5310 #endif /* CONFIG_OUTPUT */
5311 return fopen(f, "r");
5314 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
5318 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5321 remove_comments(buf);
5326 while (*p && isspace((int) *p)) {
5330 return CONF_CONTINUE; /* empty line */
5337 while (*p2 && !isspace((int) *p2)) {
5341 *p2 = '\0'; /* break at name's end */
5348 while (*p && isspace((int) *p)) {
5354 p2 = *value + strlen(*value);
5355 while (isspace((int) *(p2 - 1))) {
5364 char load_config_file(const char *f)
5369 fp = open_config_file(f);
5373 DBGP("reading contents from config file '%s'", f);
5376 char buff[CONF_BUFF_SIZE], *name, *value;
5377 int ret = do_config_step(&line, fp, buff, &name, &value);
5378 if (ret == CONF_BREAK) {
5380 } else if (ret == CONF_CONTINUE) {
5386 /* don't listen if X is already initialised or
5387 * if we already know we don't want it */
5388 if(x_initialised != YES) {
5389 if (string_to_bool(value)) {
5390 output_methods &= TO_X;
5392 output_methods &= ~TO_X;
5393 x_initialised = NEVER;
5398 if (!value || x_initialised == YES) {
5403 disp = strdup(value);
5408 if (window.type == TYPE_DOCK)
5411 #endif /*OWN_WINDOW */
5413 int a = string_to_alignment(value);
5424 CONF("background") {
5425 fork_to_background = string_to_bool(value);
5428 CONF2("background") {
5429 fork_to_background = string_to_bool(value);
5433 CONF("show_graph_scale") {
5434 show_graph_scale = string_to_bool(value);
5436 CONF("show_graph_range") {
5437 show_graph_range = string_to_bool(value);
5439 CONF("border_inner_margin") {
5441 window.border_inner_margin = strtol(value, 0, 0);
5442 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
5447 CONF("border_outer_margin") {
5449 window.border_outer_margin = strtol(value, 0, 0);
5450 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
5455 CONF("border_width") {
5457 window.border_width = strtol(value, 0, 0);
5458 if (window.border_width < 0) window.border_width = 0;
5464 #define TEMPLATE_CONF(n) \
5465 CONF("template"#n) { \
5467 free(template[n]); \
5468 template[n] = strdup(value); \
5485 info.mail = parse_mail_args(IMAP_TYPE, value);
5492 info.mail = parse_mail_args(POP3_TYPE, value);
5497 CONF("default_bar_size") {
5500 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
5507 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
5511 CONF("default_graph_size") {
5514 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
5521 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
5524 CONF("default_gauge_size") {
5527 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
5534 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
5541 mpd_set_host(value);
5547 if (value && mpd_set_port(value)) {
5551 CONF("mpd_password") {
5553 mpd_set_password(value, 0);
5559 CONF("music_player_interval") {
5561 info.music_player_interval = strtod(value, 0);
5567 CONF("sensor_device") {
5569 sensor_device = strtol(value, 0, 0);
5575 CONF("cpu_avg_samples") {
5577 cpu_avg_samples = strtol(value, 0, 0);
5578 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
5581 info.cpu_avg_samples = cpu_avg_samples;
5587 CONF("net_avg_samples") {
5589 net_avg_samples = strtol(value, 0, 0);
5590 if (net_avg_samples < 1 || net_avg_samples > 14) {
5593 info.net_avg_samples = net_avg_samples;
5599 CONF("diskio_avg_samples") {
5601 diskio_avg_samples = strtol(value, 0, 0);
5602 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
5605 info.diskio_avg_samples = diskio_avg_samples;
5613 CONF("double_buffer") {
5614 use_xdbe = string_to_bool(value);
5618 CONF("override_utf8_locale") {
5619 utf8_mode = string_to_bool(value);
5621 CONF("draw_borders") {
5622 draw_borders = string_to_bool(value);
5624 CONF("draw_graph_borders") {
5625 draw_graph_borders = string_to_bool(value);
5627 CONF("draw_shades") {
5628 draw_shades = string_to_bool(value);
5630 CONF("draw_outline") {
5631 draw_outline = string_to_bool(value);
5634 CONF("out_to_console") {
5635 if(string_to_bool(value)) {
5636 output_methods |= TO_STDOUT;
5638 output_methods &= ~TO_STDOUT;
5641 CONF("extra_newline") {
5642 extra_newline = string_to_bool(value);
5644 CONF("out_to_stderr") {
5645 if(string_to_bool(value))
5646 output_methods |= TO_STDERR;
5649 CONF("out_to_ncurses") {
5650 if(string_to_bool(value)) {
5653 output_methods |= TO_NCURSES;
5657 CONF("overwrite_file") {
5658 if(overwrite_file) {
5659 free(overwrite_file);
5662 if(overwrite_works(value)) {
5663 overwrite_file = strdup(value);
5664 output_methods |= OVERWRITE_FILE;
5666 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
5668 CONF("append_file") {
5673 if(append_works(value)) {
5674 append_file = strdup(value);
5675 output_methods |= APPEND_FILE;
5677 NORM_ERR("append_file won't be able to create/append '%s'", value);
5679 CONF("use_spacer") {
5681 if (strcasecmp(value, "left") == EQUAL) {
5682 use_spacer = LEFT_SPACER;
5683 } else if (strcasecmp(value, "right") == EQUAL) {
5684 use_spacer = RIGHT_SPACER;
5685 } else if (strcasecmp(value, "none") == EQUAL) {
5686 use_spacer = NO_SPACER;
5688 use_spacer = string_to_bool(value);
5689 NORM_ERR("use_spacer should have an argument of left, right, or"
5690 " none. '%s' seems to be some form of '%s', so"
5691 " defaulting to %s.", value,
5692 use_spacer ? "true" : "false",
5693 use_spacer ? "right" : "none");
5695 use_spacer = RIGHT_SPACER;
5697 use_spacer = NO_SPACER;
5701 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5702 use_spacer = RIGHT_SPACER;
5708 use_xft = string_to_bool(value);
5712 set_first_font(value);
5716 if (value && font_count >= 0) {
5717 fonts[0].font_alpha = atof(value) * 65535.0;
5724 if (string_to_bool(value)) {
5725 NORM_ERR("Xft not enabled at compile time");
5729 /* xftfont silently ignored when no Xft */
5732 /* xftalpha is silently ignored when no Xft */
5737 set_first_font(value);
5745 gap_x = atoi(value);
5752 gap_y = atoi(value);
5758 CONF("mail_spool") {
5762 variable_substitute(value, buffer, 256);
5764 if (buffer[0] != '\0') {
5765 if (current_mail_spool) {
5766 free(current_mail_spool);
5768 current_mail_spool = strndup(buffer, text_buffer_size);
5775 CONF("minimum_size") {
5777 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5779 if (sscanf(value, "%d", &minimum_width) != 1) {
5787 CONF("maximum_width") {
5789 if (sscanf(value, "%d", &maximum_width) != 1) {
5797 CONF("no_buffers") {
5798 no_buffers = string_to_bool(value);
5800 CONF("top_name_width") {
5802 if (sscanf(value, "%u", &top_name_width) != 1) {
5808 if (top_name_width >= max_user_text) {
5809 top_name_width = max_user_text - 1;
5812 CONF("top_cpu_separate") {
5813 cpu_separate = string_to_bool(value);
5815 CONF("short_units") {
5816 short_units = string_to_bool(value);
5818 CONF("format_human_readable") {
5819 format_human_readable = string_to_bool(value);
5821 CONF("pad_percents") {
5822 pad_percents = atoi(value);
5826 CONF("own_window") {
5828 own_window = string_to_bool(value);
5831 CONF("own_window_class") {
5833 memset(window.class_name, 0, sizeof(window.class_name));
5834 strncpy(window.class_name, value,
5835 sizeof(window.class_name) - 1);
5838 CONF("own_window_title") {
5840 memset(window.title, 0, sizeof(window.title));
5841 strncpy(window.title, value, sizeof(window.title) - 1);
5844 CONF("own_window_transparent") {
5846 set_transparent = string_to_bool(value);
5849 CONF("own_window_hints") {
5851 char *p_hint, *p_save;
5852 char delim[] = ", ";
5854 /* tokenize the value into individual hints */
5855 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5857 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5858 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5859 SET_HINT(window.hints, HINT_UNDECORATED);
5860 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5861 SET_HINT(window.hints, HINT_BELOW);
5862 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5863 SET_HINT(window.hints, HINT_ABOVE);
5864 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5865 SET_HINT(window.hints, HINT_STICKY);
5866 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5867 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5868 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5869 SET_HINT(window.hints, HINT_SKIP_PAGER);
5874 p_hint = strtok_r(NULL, delim, &p_save);
5875 } while (p_hint != NULL);
5881 CONF("own_window_type") {
5883 if (strncmp(value, "normal", 6) == EQUAL) {
5884 window.type = TYPE_NORMAL;
5885 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5886 window.type = TYPE_DESKTOP;
5887 } else if (strncmp(value, "dock", 4) == EQUAL) {
5888 window.type = TYPE_DOCK;
5889 text_alignment = TOP_LEFT;
5890 } else if (strncmp(value, "panel", 5) == EQUAL) {
5891 window.type = TYPE_PANEL;
5892 } else if (strncmp(value, "override", 8) == EQUAL) {
5893 window.type = TYPE_OVERRIDE;
5902 CONF("stippled_borders") {
5904 stippled_borders = strtol(value, 0, 0);
5906 stippled_borders = 4;
5910 CONF("imlib_cache_size") {
5912 cimlib_set_cache_size(atoi(value));
5915 CONF("imlib_cache_flush_interval") {
5917 cimlib_set_cache_flush_interval(atoi(value));
5922 CONF("update_interval_on_battery") {
5924 update_interval_bat = strtod(value, 0);
5929 CONF("update_interval") {
5931 set_update_interval(strtod(value, 0));
5935 if (info.music_player_interval == 0) {
5936 // default to update_interval
5937 info.music_player_interval = update_interval;
5940 CONF("total_run_times") {
5942 total_run_times = strtod(value, 0);
5948 stuff_in_uppercase = string_to_bool(value);
5950 CONF("max_specials") {
5952 max_specials = atoi(value);
5957 CONF("max_user_text") {
5959 max_user_text = atoi(value);
5964 CONF("text_buffer_size") {
5966 text_buffer_size = atoi(value);
5967 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5968 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5969 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5977 if (output_methods & TO_X) {
5978 X11_initialisation();
5987 global_text = (char *) malloc(1);
5988 global_text[0] = '\0';
5991 unsigned int l = strlen(global_text);
5993 char buf[CONF_BUFF_SIZE];
5995 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
6001 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
6009 /* Check for continuation of \\-\n. */
6010 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
6011 global_text[l-1] = '\0';
6015 global_text = (char *) realloc(global_text, l + bl + 1);
6016 strcat(global_text, buf);
6018 if (strlen(global_text) > max_user_text) {
6023 if (strlen(global_text) < 1) {
6024 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
6026 global_text_lines = line + 1;
6029 #ifdef TCP_PORT_MONITOR
6030 CONF("max_port_monitor_connections") {
6032 if (!value || (sscanf(value, "%d", &max) != 1)) {
6033 /* an error. use default, warn and continue. */
6034 tcp_portmon_set_max_connections(0);
6036 } else if (tcp_portmon_set_max_connections(max)) {
6037 /* max is < 0, default has been set*/
6042 CONF("if_up_strictness") {
6044 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6045 ifup_strictness = IFUP_UP;
6046 } else if (strcasecmp(value, "up") == EQUAL) {
6047 ifup_strictness = IFUP_UP;
6048 } else if (strcasecmp(value, "link") == EQUAL) {
6049 ifup_strictness = IFUP_LINK;
6050 } else if (strcasecmp(value, "address") == EQUAL) {
6051 ifup_strictness = IFUP_ADDR;
6053 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6054 ifup_strictness = IFUP_UP;
6058 CONF("temperature_unit") {
6060 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
6061 } else if (set_temp_output_unit(value)) {
6062 NORM_ERR("temperature_unit: incorrect argument");
6069 char *ptr = strtok(value, " ");
6072 ptr = strtok(NULL, " ");
6079 CONF("lua_draw_hook_pre") {
6081 llua_set_draw_pre_hook(value);
6086 CONF("lua_draw_hook_post") {
6088 llua_set_draw_post_hook(value);
6093 CONF("lua_startup_hook") {
6095 llua_set_startup_hook(value);
6100 CONF("lua_shutdown_hook") {
6102 llua_set_shutdown_hook(value);
6108 #endif /* HAVE_LUA */
6120 CONF("default_color"){}
6121 CONF3("default_shade_color", "default_shadecolor"){}
6122 CONF3("default_outline_color", "default_outlinecolor") {}
6123 CONF("own_window_colour") {}
6126 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
6132 if (info.music_player_interval == 0) {
6133 // default to update_interval
6134 info.music_player_interval = update_interval;
6136 if (!global_text) { // didn't supply any text
6137 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
6143 static void load_config_file_x11(const char *f)
6148 fp = open_config_file(f);
6152 DBGP("reading contents from config file '%s'", f);
6155 char buff[CONF_BUFF_SIZE], *name, *value;
6156 int ret = do_config_step(&line, fp, buff, &name, &value);
6157 if (ret == CONF_BREAK) {
6159 } else if (ret == CONF_CONTINUE) {
6164 X11_initialisation();
6165 if (x_initialised == YES) {
6167 color0 = get_x11_color(value);
6174 X11_initialisation();
6175 if (x_initialised == YES) {
6177 color1 = get_x11_color(value);
6184 X11_initialisation();
6185 if (x_initialised == YES) {
6187 color2 = get_x11_color(value);
6194 X11_initialisation();
6195 if (x_initialised == YES) {
6197 color3 = get_x11_color(value);
6204 X11_initialisation();
6205 if (x_initialised == YES) {
6207 color4 = get_x11_color(value);
6214 X11_initialisation();
6215 if (x_initialised == YES) {
6217 color5 = get_x11_color(value);
6224 X11_initialisation();
6225 if (x_initialised == YES) {
6227 color6 = get_x11_color(value);
6234 X11_initialisation();
6235 if (x_initialised == YES) {
6237 color7 = get_x11_color(value);
6244 X11_initialisation();
6245 if (x_initialised == YES) {
6247 color8 = get_x11_color(value);
6254 X11_initialisation();
6255 if (x_initialised == YES) {
6257 color9 = get_x11_color(value);
6263 CONF("default_color") {
6264 X11_initialisation();
6265 if (x_initialised == YES) {
6267 default_fg_color = get_x11_color(value);
6273 CONF3("default_shade_color", "default_shadecolor") {
6274 X11_initialisation();
6275 if (x_initialised == YES) {
6277 default_bg_color = get_x11_color(value);
6283 CONF3("default_outline_color", "default_outlinecolor") {
6284 X11_initialisation();
6285 if (x_initialised == YES) {
6287 default_out_color = get_x11_color(value);
6294 CONF("own_window_colour") {
6295 X11_initialisation();
6296 if (x_initialised == YES) {
6298 background_colour = get_x11_color(value);
6300 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
6301 "'#' for hex colours");
6307 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
6308 if(output_methods & TO_X) {
6309 X11_initialisation();
6318 #undef CONF_CONTINUE
6319 #undef CONF_BUFF_SIZE
6327 static void print_help(const char *prog_name) {
6328 printf("Usage: %s [OPTION]...\n"
6329 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
6330 "window. Command line options will override configurations defined in config\n"
6332 " -v, --version version\n"
6333 " -q, --quiet quiet mode\n"
6334 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
6335 " -c, --config=FILE config file to load\n"
6336 #ifdef CONFIG_OUTPUT
6337 " -C, --print-config print the builtin default config to stdout\n"
6338 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
6340 " -d, --daemonize daemonize, fork to background\n"
6341 " -h, --help help\n"
6343 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
6344 " -f, --font=FONT font to use\n"
6345 " -X, --display=DISPLAY X11 display to use\n"
6347 " -o, --own-window create own window to draw\n"
6350 " -b, --double-buffer double buffer (prevents flickering)\n"
6352 " -w, --window-id=WIN_ID window id to draw\n"
6353 " -x X x position\n"
6354 " -y Y y position\n"
6356 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
6357 " -u, --interval=SECS update interval\n"
6358 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
6363 /* : means that character before that takes an argument */
6364 static const char *getopt_string = "vVqdDt:u:i:hc:"
6374 #ifdef CONFIG_OUTPUT
6379 static const struct option longopts[] = {
6380 { "help", 0, NULL, 'h' },
6381 { "version", 0, NULL, 'V' },
6382 { "debug", 0, NULL, 'D' },
6383 { "config", 1, NULL, 'c' },
6384 #ifdef CONFIG_OUTPUT
6385 { "print-config", 0, NULL, 'C' },
6387 { "daemonize", 0, NULL, 'd' },
6389 { "alignment", 1, NULL, 'a' },
6390 { "font", 1, NULL, 'f' },
6391 { "display", 1, NULL, 'X' },
6393 { "own-window", 0, NULL, 'o' },
6396 { "double-buffer", 0, NULL, 'b' },
6398 { "window-id", 1, NULL, 'w' },
6400 { "text", 1, NULL, 't' },
6401 { "interval", 0, NULL, 'u' },
6405 void initialisation(int argc, char **argv) {
6406 struct sigaction act, oact;
6408 set_default_configurations();
6409 load_config_file(current_config);
6410 currentconffile = conftree_add(currentconffile, current_config);
6412 /* init specials array */
6413 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
6414 NORM_ERR("failed to create specials array");
6418 if (current_mail_spool == NULL) {
6421 variable_substitute(MAIL_FILE, buf, 256);
6423 if (buf[0] != '\0') {
6424 current_mail_spool = strndup(buf, text_buffer_size);
6429 /* handle other command line arguments */
6431 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
6432 || defined(__NetBSD__)
6433 optind = optreset = 1;
6438 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6439 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
6440 "kvm_open")) == NULL) {
6441 CRIT_ERR(NULL, NULL, "cannot read kvm");
6446 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6454 fork_to_background = 1;
6457 global_debug_level++;
6461 set_first_font(optarg);
6464 text_alignment = string_to_alignment(optarg);
6483 global_text = strndup(optarg, max_user_text);
6484 convert_escapes(global_text);
6488 update_interval = strtod(optarg, 0);
6489 update_interval_old = update_interval;
6490 if (info.music_player_interval == 0) {
6491 // default to update_interval
6492 info.music_player_interval = update_interval;
6497 total_run_times = strtod(optarg, 0);
6501 gap_x = atoi(optarg);
6505 gap_y = atoi(optarg);
6516 if (output_methods & TO_X) {
6517 load_config_file_x11(current_config);
6521 /* generate text and get initial size */
6522 extract_variable_text(global_text);
6529 if (fork_to_background) {
6534 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
6541 fprintf(stderr, "\n");
6546 /* parent process */
6547 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
6554 text_buffer = malloc(max_user_text);
6555 memset(text_buffer, 0, max_user_text);
6556 tmpstring1 = malloc(text_buffer_size);
6557 memset(tmpstring1, 0, text_buffer_size);
6558 tmpstring2 = malloc(text_buffer_size);
6559 memset(tmpstring2, 0, text_buffer_size);
6564 X11_create_window();
6567 llua_setup_info(&info, update_interval);
6568 #endif /* HAVE_LUA */
6573 /* Set signal handlers */
6574 act.sa_handler = signal_handler;
6575 sigemptyset(&act.sa_mask);
6578 act.sa_flags |= SA_RESTART;
6581 if ( sigaction(SIGINT, &act, &oact) < 0
6582 || sigaction(SIGALRM, &act, &oact) < 0
6583 || sigaction(SIGUSR1, &act, &oact) < 0
6584 || sigaction(SIGHUP, &act, &oact) < 0
6585 || sigaction(SIGTERM, &act, &oact) < 0) {
6586 NORM_ERR("error setting signal handler: %s", strerror(errno));
6590 llua_startup_hook();
6591 #endif /* HAVE_LUA */
6594 int main(int argc, char **argv)
6603 g_signal_pending = 0;
6604 max_user_text = MAX_USER_TEXT_DEFAULT;
6606 memset(&info, 0, sizeof(info));
6607 memset(template, 0, sizeof(template));
6610 #ifdef TCP_PORT_MONITOR
6611 /* set default connection limit */
6612 tcp_portmon_set_max_connections(0);
6615 /* handle command line parameters that don't change configs */
6617 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
6618 || ((s = getenv("LANG")) && *s)) {
6619 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6621 NORM_ERR("malloc failed");
6623 for (x = 0; x < strlen(s); x++) {
6624 temp[x] = tolower(s[x]);
6627 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6633 if (!setlocale(LC_CTYPE, "")) {
6634 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6638 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6649 if (current_config) {
6650 free(current_config);
6652 current_config = strndup(optarg, max_user_text);
6655 freopen("/dev/null", "w", stderr);
6658 print_help(argv[0]);
6660 #ifdef CONFIG_OUTPUT
6667 window.window = strtol(optarg, 0, 0);
6672 disp = strdup(optarg);
6681 /* check if specified config file is valid */
6682 if (current_config) {
6684 if (stat(current_config, &sb) ||
6685 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
6686 NORM_ERR("invalid configuration file '%s'\n", current_config);
6687 free(current_config);
6692 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
6694 if (!current_config) {
6695 /* load default config file */
6696 char buf[DEFAULT_TEXT_BUFFER_SIZE];
6699 /* Try to use personal config file first */
6700 to_real_path(buf, CONFIG_FILE);
6701 if (buf[0] && (fp = fopen(buf, "r"))) {
6702 current_config = strndup(buf, max_user_text);
6706 /* Try to use system config file if personal config not readable */
6707 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
6708 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
6712 /* No readable config found */
6713 if (!current_config) {
6714 #ifdef CONFIG_OUTPUT
6715 current_config = strdup("==builtin==");
6716 NORM_ERR("no readable personal or system-wide config file found,"
6717 " using builtin default");
6719 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
6720 #endif /* ! CONF_OUTPUT */
6725 /* Load xoap keys, if existing */
6729 #ifdef HAVE_SYS_INOTIFY_H
6730 inotify_fd = inotify_init();
6731 #endif /* HAVE_SYS_INOTIFY_H */
6733 initialisation(argc, argv);
6737 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6745 void alarm_handler(void) {
6747 kill(childpid, SIGTERM);
6751 static void signal_handler(int sig)
6753 /* signal handler is light as a feather, as it should be.
6754 * we will poll g_signal_pending with each loop of conky
6755 * and do any signal processing there, NOT here (except
6756 * SIGALRM because this is caused when conky is hanging) */
6757 if(sig == SIGALRM) {
6760 g_signal_pending = sig;