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"
94 /* check for OS and include appropriate headers */
95 #if defined(__linux__)
97 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
99 #elif defined(__OpenBSD__)
103 #if defined(__FreeBSD_kernel__)
107 /* FIXME: apm_getinfo is unused here. maybe it's meant for common.c */
108 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
109 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
110 int apm_getinfo(int fd, apm_info_t aip);
111 char *get_apm_adapter(void);
112 char *get_apm_battery_life(void);
113 char *get_apm_battery_time(void);
117 #include "defconfig.h"
118 #include "conf_cookie.h"
122 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
125 #define MAIL_FILE "$MAIL"
126 #define MAX_IF_BLOCK_DEPTH 5
128 //#define SIGNAL_BLOCKING
129 #undef SIGNAL_BLOCKING
131 /* debugging level, used by logging.h */
132 int global_debug_level = 0;
134 /* two strings for internal use */
135 static char *tmpstring1, *tmpstring2;
137 /* variables holding various config settings */
139 int format_human_readable;
146 int top_cpu, top_mem, top_time;
153 static unsigned int top_name_width = 15;
155 static int extra_newline;
156 enum x_initialiser_state x_initialised = NO;
157 static volatile int g_signal_pending;
158 /* Update interval */
159 double update_interval;
160 double update_interval_old;
161 double update_interval_bat;
162 void *global_cpu = NULL;
168 /* prototypes for internally used functions */
169 static void signal_handler(int);
170 static void print_version(void) __attribute__((noreturn));
171 static void reload_config(void);
172 static void generate_text_internal(char *, int, struct text_object,
173 struct information *);
175 static void print_version(void)
177 printf(PACKAGE_NAME" "VERSION" compiled "BUILD_DATE" for "BUILD_ARCH"\n");
179 printf("\nCompiled in features:\n\n"
180 "System config file: "SYSTEM_CONFIG_FILE"\n"
181 "Package library path: "PACKAGE_LIBDIR"\n\n"
185 " * Xdamage extension\n"
186 # endif /* HAVE_XDAMAGE */
188 " * XDBE (double buffer extension)\n"
189 # endif /* HAVE_XDBE */
194 "\n Music detection:\n"
197 #endif /* AUDACIOUS */
213 #endif /* HAVE_OPENMP */
220 #ifdef TCP_PORT_MONITOR
222 #endif /* TCP_PORT_MONITOR */
225 #endif /* HAVE_CURL */
230 " * Weather (METAR)\n"
232 " * Weather (XOAP)\n"
237 #endif /* HAVE_IWLIB */
239 " * support for IBM/Lenovo notebooks\n"
249 #endif /* CONFIG_OUTPUT */
254 " * ALSA mixer support\n"
255 #endif /* MIXER_IS_ALSA */
265 #ifdef HAVE_LUA_CAIRO
267 #endif /* HAVE_LUA_CAIRO */
268 #ifdef HAVE_LUA_IMLIB2
271 #endif /* HAVE_LUA */
277 static const char *suffixes[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "" };
282 static void X11_create_window(void);
283 static void X11_initialisation(void);
285 struct _x11_stuff_s {
289 XserverRegion region2, part;
290 int event_base, error_base;
296 static int text_start_x, text_start_y; /* text start position in window */
297 static int text_width, text_height;
312 /* display to connect to */
313 static char *disp = NULL;
317 /* struct that has all info to be shared between
318 * instances of the same text object */
319 struct information info;
321 /* path to config file */
322 char *current_config;
324 /* set to 1 if you want all text to be in uppercase */
325 static unsigned int stuff_in_uppercase;
327 /* Run how many times? */
328 static unsigned long total_run_times;
331 static int fork_to_background;
333 static int cpu_avg_samples, net_avg_samples, diskio_avg_samples;
335 /* filenames for output */
336 char *overwrite_file = NULL; FILE *overwrite_fpointer = NULL;
337 char *append_file = NULL; FILE *append_fpointer = NULL;
341 static int show_graph_scale;
342 static int show_graph_range;
344 /* Position on the screen */
345 static int text_alignment;
346 static int gap_x, gap_y;
349 static int draw_borders;
350 static int draw_graph_borders;
351 static int stippled_borders;
353 int get_stippled_borders(void)
355 return stippled_borders;
358 static int draw_shades, draw_outline;
360 long default_fg_color, default_bg_color, default_out_color;
362 /* create own window or draw stuff to root? */
363 static int set_transparent = 0;
366 static int own_window = 0;
367 static int background_colour = 0;
369 /* fixed size/pos is set if wm/user changes them */
370 static int fixed_size = 0, fixed_pos = 0;
373 static int minimum_width, minimum_height;
374 static int maximum_width;
379 static int sensor_device;
382 long color0, color1, color2, color3, color4, color5, color6, color7, color8,
385 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
386 unsigned int max_user_text;
388 /* maximum size of individual text buffers, ie $exec buffer size */
389 unsigned int text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
394 /* no buffers in used memory? */
397 /* pad percentages to decimals? */
398 static int pad_percents = 0;
400 static char *global_text = 0;
402 char *get_global_text(void)
407 long global_text_lines;
409 static int total_updates;
410 static int updatereset;
412 void set_updatereset(int i)
417 int get_updatereset(void)
422 int check_contains(char *f, char *s)
425 FILE *where = open_file(f, 0);
430 while (fgets(buf1, 256, where)) {
431 if (strstr(buf1, s)) {
438 NORM_ERR("Could not open the file");
443 #define SECRIT_MULTILINE_CHAR '\x02'
447 static inline int calc_text_width(const char *s, int l)
449 if ((output_methods & TO_X) == 0) {
457 XftTextExtentsUtf8(display, fonts[selected_font].xftfont,
458 (const FcChar8 *) s, l, &gi);
460 XftTextExtents8(display, fonts[selected_font].xftfont,
461 (const FcChar8 *) s, l, &gi);
467 return XTextWidth(fonts[selected_font].font, s, l);
472 /* formatted text to render on screen, generated in generate_text(),
473 * drawn in draw_stuff() */
475 static char *text_buffer;
477 /* quite boring functions */
479 static inline void for_each_line(char *b, int f(char *, int))
482 int special_index = 0; /* specials index */
484 for (ps = b, pe = b; *pe; pe++) {
487 special_index = f(ps, special_index);
494 f(ps, special_index);
498 static void convert_escapes(char *buf)
500 char *p = buf, *s = buf;
507 } else if (*s == '\\') {
518 /* Prints anything normally printed with snprintf according to the current value
519 * of use_spacer. Actually slightly more flexible than snprintf, as you can
520 * safely specify the destination buffer as one of your inputs. */
521 int spaced_print(char *buf, int size, const char *format, int width, ...)
530 tempbuf = malloc(size * sizeof(char));
532 // Passes the varargs along to vsnprintf
533 va_start(argp, width);
534 vsnprintf(tempbuf, size, format, argp);
537 switch (use_spacer) {
539 len = snprintf(buf, size, "%s", tempbuf);
542 len = snprintf(buf, size, "%*s", width, tempbuf);
545 len = snprintf(buf, size, "%-*s", width, tempbuf);
552 /* print percentage values
554 * - i.e., unsigned values between 0 and 100
555 * - respect the value of pad_percents */
556 static int percent_print(char *buf, int size, unsigned value)
558 return spaced_print(buf, size, "%u", pad_percents, value);
561 /* converts from bytes to human readable format (K, M, G, T)
563 * The algorithm always divides by 1024, as unit-conversion of byte
564 * counts suggests. But for output length determination we need to
565 * compare with 1000 here, as we print in decimal form. */
566 static void human_readable(long long num, char *buf, int size)
568 const char **suffix = suffixes;
574 /* Possibly just output as usual, for example for stdout usage */
575 if (!format_human_readable) {
576 spaced_print(buf, size, "%d", 6, round_to_int(num));
587 if (llabs(num) < 1000LL) {
588 spaced_print(buf, size, format, width, 0, (float)num, *suffix);
592 while (llabs(num / 1024) >= 1000LL && **(suffix + 2)) {
600 /* fnum should now be < 1000, so looks like 'AAA.BBBBB'
602 * The goal is to always have a significance of 3, by
603 * adjusting the decimal part of the number. Sample output:
607 * so the point of alignment resides between number and unit. The
608 * upside of this is that there is minimal padding necessary, though
609 * there should be a way to make alignment take place at the decimal
610 * dot (then with fixed width decimal part).
612 * Note the repdigits below: when given a precision value, printf()
613 * rounds the float to it, not just cuts off the remaining digits. So
614 * e.g. 99.95 with a precision of 1 gets 100.0, which again should be
615 * printed with a precision of 0. Yay. */
617 precision = 0; /* print 100-999 without decimal part */
619 precision = 1; /* print 10-99 with one decimal place */
621 precision = 2; /* print 0-9 with two decimal places */
623 spaced_print(buf, size, format, width, precision, fnum, *suffix);
626 /* global object list root element */
627 static struct text_object global_root_object;
629 //our own implementation of popen, the difference : the value of 'childpid' will be filled with
630 //the pid of the running 'command'. This is useful if want to kill it when it hangs while reading
631 //or writing to it. We have to kill it because pclose will wait until the process dies by itself
632 FILE* pid_popen(const char *command, const char *mode, pid_t *child) {
634 int parentend, childend;
636 //by running pipe after the strcmp's we make sure that we don't have to create a pipe
637 //and close the ends if mode is something illegal
638 if(strcmp(mode, "r") == 0) {
639 if(pipe(ends) != 0) {
644 } else if(strcmp(mode, "w") == 0) {
645 if(pipe(ends) != 0) {
658 } else if(*child > 0) {
660 waitpid(*child, NULL, 0);
662 //don't read from both stdin and pipe or write to both stdout and pipe
663 if(childend == ends[0]) {
668 dup(childend); //by dupping childend, the returned fd will have close-on-exec turned off
669 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
670 _exit(EXIT_FAILURE); //child should die here, (normally execl will take care of this but it can fail)
672 return fdopen(parentend, mode);
675 static inline void read_exec(const char *data, char *buf, const int size)
679 alarm(update_interval);
680 fp = pid_popen(data, "r", &childpid);
684 length = fread(buf, 1, size, fp);
687 if (length > 0 && buf[length - 1] == '\n') {
688 buf[length - 1] = '\0';
696 void do_read_exec(const char *data, char *buf, const int size)
698 read_exec(data, buf, size);
701 void *threaded_exec(void *) __attribute__((noreturn));
703 void *threaded_exec(void *arg)
706 struct text_object *obj = (struct text_object *)arg;
709 buff = malloc(text_buffer_size);
710 read_exec(obj->data.texeci.cmd, buff,
719 timed_thread_lock(obj->data.texeci.p_timed_thread);
720 strncpy(obj->data.texeci.buffer, buff, text_buffer_size);
721 timed_thread_unlock(obj->data.texeci.p_timed_thread);
723 if (timed_thread_test(obj->data.texeci.p_timed_thread, 0)) {
724 timed_thread_exit(obj->data.texeci.p_timed_thread);
730 static long current_text_color;
732 void set_current_text_color(long colour)
734 current_text_color = colour;
737 long get_current_text_color(void)
739 return current_text_color;
742 //adds newstring to to the tree unless you can already see it when travelling back.
743 //if it's possible to attach it then it returns a pointer to the leaf, else it returns NULL
744 struct conftree* conftree_add(struct conftree* previous, const char* newstring) {
745 struct conftree* node;
746 struct conftree* node2;
748 for(node = previous; node != NULL; node = node->back) {
749 if(strcmp(node->string, newstring) == 0) {
753 node = malloc(sizeof(struct conftree));
754 if (previous != NULL) {
755 if(previous->vert_next == NULL) {
756 previous->vert_next = node;
758 for(node2 = previous->vert_next; node2->horz_next != NULL; node2 = node2->horz_next ) { }
759 node2->horz_next = node;
762 node->string = strdup(newstring);
763 node->horz_next = NULL;
764 node->vert_next = NULL;
765 node->back = previous;
769 void conftree_empty(struct conftree* tree) {
771 conftree_empty(tree->horz_next);
772 conftree_empty(tree->vert_next);
778 struct conftree *currentconffile;
780 static void extract_variable_text(const char *p)
782 free_text_objects(&global_root_object, 0);
796 extract_variable_text_internal(&global_root_object, p);
799 void parse_conky_vars(struct text_object *root, const char *txt, char *p, struct information *cur)
801 extract_variable_text_internal(root, txt);
802 generate_text_internal(p, max_user_text, *root, cur);
805 static inline struct mail_s *ensure_mail_thread(struct text_object *obj,
806 void *thread(void *), const char *text)
808 if (obj->char_b && info.mail) {
809 // this means we use info
810 if (!info.mail->p_timed_thread) {
811 info.mail->p_timed_thread =
812 timed_thread_create(thread,
813 (void *) info.mail, info.mail->interval * 1000000);
814 if (!info.mail->p_timed_thread) {
815 NORM_ERR("Error creating %s timed thread", text);
817 timed_thread_register(info.mail->p_timed_thread,
818 &info.mail->p_timed_thread);
819 if (timed_thread_run(info.mail->p_timed_thread)) {
820 NORM_ERR("Error running %s timed thread", text);
824 } else if (obj->data.mail) {
825 // this means we use obj
826 if (!obj->data.mail->p_timed_thread) {
827 obj->data.mail->p_timed_thread =
828 timed_thread_create(thread,
829 (void *) obj->data.mail,
830 obj->data.mail->interval * 1000000);
831 if (!obj->data.mail->p_timed_thread) {
832 NORM_ERR("Error creating %s timed thread", text);
834 timed_thread_register(obj->data.mail->p_timed_thread,
835 &obj->data.mail->p_timed_thread);
836 if (timed_thread_run(obj->data.mail->p_timed_thread)) {
837 NORM_ERR("Error running %s timed thread", text);
840 return obj->data.mail;
841 } else if (!obj->a) {
842 // something is wrong, warn once then stop
843 NORM_ERR("There's a problem with your mail settings. "
844 "Check that the global mail settings are properly defined"
845 " (line %li).", obj->line);
851 char *format_time(unsigned long timeval, const int width)
854 unsigned long nt; // narrow time, for speed on 32-bit
855 unsigned cc; // centiseconds
856 unsigned nn; // multi-purpose whatever
859 cc = nt % 100; // centiseconds past second
860 nt /= 100; // total seconds
861 nn = nt % 60; // seconds past the minute
862 nt /= 60; // total minutes
863 if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
865 return strndup(buf, text_buffer_size);
867 if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
868 return strndup(buf, text_buffer_size);
870 nn = nt % 60; // minutes past the hour
871 nt /= 60; // total hours
872 if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
873 return strndup(buf, text_buffer_size);
875 nn = nt; // now also hours
876 if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
877 return strndup(buf, text_buffer_size);
879 nn /= 24; // now days
880 if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
881 return strndup(buf, text_buffer_size);
883 nn /= 7; // now weeks
884 if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
885 return strndup(buf, text_buffer_size);
887 // well shoot, this outta' fit...
888 return strndup("<inf>", text_buffer_size);
891 //remove backspaced chars, example: "dog^H^H^Hcat" becomes "cat"
892 //string has to end with \0 and it's length should fit in a int
894 void remove_deleted_chars(char *string){
896 while(string[i] != 0){
897 if(string[i] == BACKSPACE){
899 strcpy( &(string[i-1]), &(string[i+1]) );
901 }else strcpy( &(string[i]), &(string[i+1]) ); //necessary for ^H's at the start of a string
906 static inline void format_media_player_time(char *buf, const int size,
909 int days, hours, minutes;
911 days = seconds / (24 * 60 * 60);
912 seconds %= (24 * 60 * 60);
913 hours = seconds / (60 * 60);
914 seconds %= (60 * 60);
915 minutes = seconds / 60;
919 snprintf(buf, size, "%i days %i:%02i:%02i", days,
920 hours, minutes, seconds);
921 } else if (hours > 0) {
922 snprintf(buf, size, "%i:%02i:%02i", hours, minutes,
925 snprintf(buf, size, "%i:%02i", minutes, seconds);
929 static inline double get_barnum(char *buf)
941 if (sscanf(buf, "%lf", &barnum) == 0) {
942 NORM_ERR("reading exec value failed (perhaps it's not the "
946 if (barnum > 100.0 || barnum < 0.0) {
947 NORM_ERR("your exec value is not between 0 and 100, "
948 "therefore it will be ignored");
954 /* substitutes all occurrences of '\n' with SECRIT_MULTILINE_CHAR, which allows
955 * multiline objects like $exec work with $align[rc] and friends
957 void substitute_newlines(char *p, long l)
961 while (p && *p && p < s + l) {
963 /* only substitute if it's not the last newline */
964 *p = SECRIT_MULTILINE_CHAR;
970 static void generate_text_internal(char *p, int p_max_size,
971 struct text_object root, struct information *cur)
973 struct text_object *obj;
975 int need_to_load_fonts = 0;
978 /* for the OBJ_top* handler */
979 struct process **needed = 0;
982 char buff_in[p_max_size];
984 set_iconv_converting(0);
985 #endif /* HAVE_ICONV */
989 while (obj && p_max_size > 0) {
990 needed = 0; /* reset for top stuff */
992 /* IFBLOCK jumping algorithm
994 * This is easier as it looks like:
995 * - each IF checks it's condition
996 * - on FALSE: call DO_JUMP
997 * - on TRUE: don't care
998 * - each ELSE calls DO_JUMP unconditionally
999 * - each ENDIF is silently being ignored
1002 * DO_JUMP overwrites the "obj" variable of the loop and sets it to the target
1003 * (i.e. the corresponding ELSE or ENDIF). After that, processing for the given
1004 * object can continue, free()ing stuff e.g., then the for-loop does the rest: as
1005 * regularly, "obj" is being updated to point to obj->next, so object parsing
1006 * continues right after the corresponding ELSE or ENDIF. This means that if we
1007 * find an ELSE, it's corresponding IF must not have jumped, so we need to jump
1008 * always. If we encounter an ENDIF, it's corresponding IF or ELSE has not
1009 * jumped, and there is nothing to do.
1013 obj = obj->data.ifblock.next; \
1016 #define OBJ(a) break; case OBJ_##a:
1018 switch (obj->type) {
1020 NORM_ERR("not implemented obj type %d", obj->type);
1023 struct sockaddr_in addr;
1024 struct hostent* he = gethostbyname(obj->data.read_tcp.host);
1026 sock = socket(he->h_addrtype, SOCK_STREAM, 0);
1028 memset(&addr, 0, sizeof(addr));
1029 addr.sin_family = AF_INET;
1030 addr.sin_port = obj->data.read_tcp.port;
1031 memcpy(&addr.sin_addr, he->h_addr, he->h_length);
1032 if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr)) == 0) {
1036 FD_SET(sock, &readfds);
1039 if(select(sock + 1, &readfds, NULL, NULL, &tv) > 0){
1040 received = recv(sock, p, p_max_size, 0);
1045 NORM_ERR("read_tcp: Couldn't create a connection");
1048 NORM_ERR("read_tcp: Couldn't create a socket");
1051 NORM_ERR("read_tcp: Problem with resolving the hostname");
1056 temp_print(p, p_max_size, get_acpi_temperature(obj->data.i), TEMP_CELSIUS);
1058 #endif /* !__OpenBSD__ */
1061 obj->a = get_freq(p, p_max_size, "%.0f", 1,
1062 obj->data.cpu_index);
1068 obj->a = get_freq(p, p_max_size, "%'.2f", 1000,
1069 obj->data.cpu_index);
1071 /* OpenBSD has no such flag (SUSv2) */
1072 obj->a = get_freq(p, p_max_size, "%.2f", 1000,
1073 obj->data.cpu_index);
1074 #endif /* __OpenBSD */
1077 #if defined(__linux__)
1080 obj->a = get_voltage(p, p_max_size, "%.0f", 1,
1081 obj->data.cpu_index);
1086 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000,
1087 obj->data.cpu_index);
1092 OBJ(wireless_essid) {
1093 snprintf(p, p_max_size, "%s", obj->data.net->essid);
1095 OBJ(wireless_mode) {
1096 snprintf(p, p_max_size, "%s", obj->data.net->mode);
1098 OBJ(wireless_bitrate) {
1099 snprintf(p, p_max_size, "%s", obj->data.net->bitrate);
1102 snprintf(p, p_max_size, "%s", obj->data.net->ap);
1104 OBJ(wireless_link_qual) {
1105 spaced_print(p, p_max_size, "%d", 4,
1106 obj->data.net->link_qual);
1108 OBJ(wireless_link_qual_max) {
1109 spaced_print(p, p_max_size, "%d", 4,
1110 obj->data.net->link_qual_max);
1112 OBJ(wireless_link_qual_perc) {
1113 if (obj->data.net->link_qual_max > 0) {
1114 spaced_print(p, p_max_size, "%.0f", 5,
1115 (double) obj->data.net->link_qual /
1116 obj->data.net->link_qual_max * 100);
1118 spaced_print(p, p_max_size, "unk", 5);
1121 OBJ(wireless_link_bar) {
1123 if(output_methods & TO_X) {
1124 new_bar(p, obj->a, obj->b, ((double) obj->data.net->link_qual /
1125 obj->data.net->link_qual_max) * 255.0);
1128 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1129 new_bar_in_shell(p, p_max_size, ((double) obj->data.net->link_qual /
1130 obj->data.net->link_qual_max) * 100.0, obj->a);
1135 #endif /* HAVE_IWLIB */
1137 #endif /* __linux__ */
1141 get_adt746x_cpu(p, p_max_size);
1144 get_adt746x_fan(p, p_max_size);
1147 get_acpi_fan(p, p_max_size);
1149 OBJ(acpiacadapter) {
1150 get_acpi_ac_adapter(p, p_max_size);
1153 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
1156 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
1158 OBJ(battery_percent) {
1159 percent_print(p, p_max_size, get_battery_perct(obj->data.s));
1163 if(output_methods & TO_X) {
1164 new_bar(p, obj->a, obj->b, get_battery_perct_bar(obj->data.s));
1167 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1168 new_bar_in_shell(p, p_max_size, get_battery_perct_bar(obj->data.s) / 2.55, obj->a);
1173 OBJ(battery_short) {
1174 get_battery_short_status(p, p_max_size, obj->data.s);
1176 #endif /* __OpenBSD__ */
1179 human_readable(cur->buffers * 1024, p, 255);
1182 human_readable(cur->cached * 1024, p, 255);
1185 if (obj->data.cpu_index > info.cpu_count) {
1186 NORM_ERR("obj->data.cpu_index %i info.cpu_count %i",
1187 obj->data.cpu_index, info.cpu_count);
1188 CRIT_ERR(NULL, NULL, "attempting to use more CPUs than you have!");
1190 percent_print(p, p_max_size,
1191 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100.0));
1195 new_gauge(p, obj->a, obj->b,
1196 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1200 if(output_methods & TO_X) {
1201 new_bar(p, obj->a, obj->b,
1202 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
1205 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1206 new_bar_in_shell(p, p_max_size, round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100), obj->a);
1213 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1214 round_to_int(cur->cpu_usage[obj->data.cpu_index] * 100),
1215 100, 1, obj->char_a, obj->char_b);
1218 new_graph(p, obj->a, obj->b, obj->c, obj->d, cur->loadavg[0],
1219 obj->e, 1, obj->char_a, obj->char_b);
1223 new_fg(p, obj->data.l);
1257 OBJ(conky_version) {
1258 snprintf(p, p_max_size, "%s", VERSION);
1260 OBJ(conky_build_date) {
1261 snprintf(p, p_max_size, "%s", BUILD_DATE);
1263 OBJ(conky_build_arch) {
1264 snprintf(p, p_max_size, "%s", BUILD_ARCH);
1266 #if defined(__linux__)
1268 snprintf(p, p_max_size, "%s",
1269 get_disk_protect_queue(obj->data.s));
1272 snprintf(p, p_max_size, "%s", i8k.version);
1275 snprintf(p, p_max_size, "%s", i8k.bios);
1278 snprintf(p, p_max_size, "%s", i8k.serial);
1283 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
1284 temp_print(p, p_max_size, (double)cpu_temp, TEMP_CELSIUS);
1286 OBJ(i8k_left_fan_status) {
1287 int left_fan_status;
1289 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
1290 if (left_fan_status == 0) {
1291 snprintf(p, p_max_size, "off");
1293 if (left_fan_status == 1) {
1294 snprintf(p, p_max_size, "low");
1296 if (left_fan_status == 2) {
1297 snprintf(p, p_max_size, "high");
1300 OBJ(i8k_right_fan_status) {
1301 int right_fan_status;
1303 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
1304 if (right_fan_status == 0) {
1305 snprintf(p, p_max_size, "off");
1307 if (right_fan_status == 1) {
1308 snprintf(p, p_max_size, "low");
1310 if (right_fan_status == 2) {
1311 snprintf(p, p_max_size, "high");
1314 OBJ(i8k_left_fan_rpm) {
1315 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
1317 OBJ(i8k_right_fan_rpm) {
1318 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
1320 OBJ(i8k_ac_status) {
1323 sscanf(i8k.ac_status, "%d", &ac_status);
1324 if (ac_status == -1) {
1325 snprintf(p, p_max_size, "disabled (read i8k docs)");
1327 if (ac_status == 0) {
1328 snprintf(p, p_max_size, "off");
1330 if (ac_status == 1) {
1331 snprintf(p, p_max_size, "on");
1334 OBJ(i8k_buttons_status) {
1335 snprintf(p, p_max_size, "%s", i8k.buttons_status);
1339 get_ibm_acpi_fan(p, p_max_size);
1342 get_ibm_acpi_temps();
1343 temp_print(p, p_max_size,
1344 ibm_acpi.temps[obj->data.sensor], TEMP_CELSIUS);
1347 get_ibm_acpi_volume(p, p_max_size);
1349 OBJ(ibm_brightness) {
1350 get_ibm_acpi_brightness(p, p_max_size);
1353 /* information from sony_laptop kernel module
1354 * /sys/devices/platform/sony-laptop */
1355 OBJ(sony_fanspeed) {
1356 get_sony_fanspeed(p, p_max_size);
1359 if (!cur->gw_info.count) {
1364 snprintf(p, p_max_size, "%s", cur->gw_info.iface);
1367 snprintf(p, p_max_size, "%s", cur->gw_info.ip);
1370 snprintf(p, p_max_size, "%d", get_laptop_mode());
1373 get_powerbook_batt_info(p, p_max_size, obj->data.i);
1375 #endif /* __linux__ */
1376 #if (defined(__FreeBSD__) || defined(__linux__))
1378 if ((obj->data.ifblock.s)
1379 && (!interface_up(obj->data.ifblock.s))) {
1385 OBJ(obsd_sensors_temp) {
1386 obsd_sensors.device = sensor_device;
1387 update_obsd_sensors();
1388 temp_print(p, p_max_size,
1389 obsd_sensors.temp[obsd_sensors.device][obj->data.sensor],
1392 OBJ(obsd_sensors_fan) {
1393 obsd_sensors.device = sensor_device;
1394 update_obsd_sensors();
1395 snprintf(p, p_max_size, "%d",
1396 obsd_sensors.fan[obsd_sensors.device][obj->data.sensor]);
1398 OBJ(obsd_sensors_volt) {
1399 obsd_sensors.device = sensor_device;
1400 update_obsd_sensors();
1401 snprintf(p, p_max_size, "%.2f",
1402 obsd_sensors.volt[obsd_sensors.device][obj->data.sensor]);
1405 get_obsd_vendor(p, p_max_size);
1408 get_obsd_product(p, p_max_size);
1410 #endif /* __OpenBSD__ */
1413 new_font(p, obj->data.s);
1414 need_to_load_fonts = 1;
1417 /* TODO: move this correction from kB to kB/s elsewhere
1418 * (or get rid of it??) */
1420 human_readable((obj->data.diskio->current / update_interval) * 1024LL,
1424 human_readable((obj->data.diskio->current_write / update_interval) * 1024LL,
1428 human_readable((obj->data.diskio->current_read / update_interval) * 1024LL,
1433 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1434 obj->data.diskio->current, obj->e, 1, obj->char_a, obj->char_b);
1436 OBJ(diskiograph_read) {
1437 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1438 obj->data.diskio->current_read, obj->e, 1, obj->char_a, obj->char_b);
1440 OBJ(diskiograph_write) {
1441 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1442 obj->data.diskio->current_write, obj->e, 1, obj->char_a, obj->char_b);
1446 human_readable(obj->data.net->recv_speed, p, 255);
1449 spaced_print(p, p_max_size, "%.1f", 8,
1450 obj->data.net->recv_speed / 1024.0);
1453 OBJ(downspeedgraph) {
1454 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1455 obj->data.net->recv_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
1459 /* Since we see you, you're if has not jumped.
1460 * Do Ninja jump here: without leaving traces.
1461 * This is to prevent us from stale jumped flags.
1463 obj = obj->data.ifblock.next;
1467 /* harmless object, just ignore */
1470 if ((obj->data.net->addr.sa_data[2] & 255) == 0
1471 && (obj->data.net->addr.sa_data[3] & 255) == 0
1472 && (obj->data.net->addr.sa_data[4] & 255) == 0
1473 && (obj->data.net->addr.sa_data[5] & 255) == 0) {
1474 snprintf(p, p_max_size, "No Address");
1476 snprintf(p, p_max_size, "%u.%u.%u.%u",
1477 obj->data.net->addr.sa_data[2] & 255,
1478 obj->data.net->addr.sa_data[3] & 255,
1479 obj->data.net->addr.sa_data[4] & 255,
1480 obj->data.net->addr.sa_data[5] & 255);
1483 #if defined(__linux__)
1485 if (NULL != obj->data.net->addrs && strlen(obj->data.net->addrs) > 2) {
1486 obj->data.net->addrs[strlen(obj->data.net->addrs) - 2] = 0; /* remove ", " from end of string */
1487 strcpy(p, obj->data.net->addrs);
1489 strcpy(p, "0.0.0.0");
1492 #endif /* __linux__ */
1493 #if defined(IMLIB2) && defined(X11)
1495 /* doesn't actually draw anything, just queues it omp. the
1496 * image will get drawn after the X event loop */
1497 cimlib_add_image(obj->data.s);
1501 evaluate(obj->data.s, p);
1504 read_exec(obj->data.s, p, text_buffer_size);
1505 remove_deleted_chars(p);
1508 struct information *tmp_info;
1509 struct text_object subroot;
1511 read_exec(obj->data.s, p, text_buffer_size);
1513 tmp_info = malloc(sizeof(struct information));
1514 memcpy(tmp_info, cur, sizeof(struct information));
1515 parse_conky_vars(&subroot, p, p, tmp_info);
1517 free_text_objects(&subroot, 1);
1524 read_exec(obj->data.s, p, text_buffer_size);
1525 barnum = get_barnum(p); /*using the same function*/
1527 if (barnum >= 0.0) {
1529 new_gauge(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1536 read_exec(obj->data.s, p, text_buffer_size);
1537 barnum = get_barnum(p);
1539 if (barnum >= 0.0) {
1541 if(output_methods & TO_X) {
1543 new_bar(p, obj->a, obj->b, round_to_int(barnum * 255.0));
1546 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1547 new_bar_in_shell(p, p_max_size, barnum, obj->a);
1555 char showaslog = FALSE;
1556 char tempgrad = FALSE;
1558 char *cmd = obj->data.s;
1560 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1562 cmd += strlen(" "TEMPGRAD);
1564 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1566 cmd += strlen(" "LOGGRAPH);
1568 read_exec(cmd, p, text_buffer_size);
1569 barnum = get_barnum(p);
1572 new_graph(p, obj->a, obj->b, obj->c, obj->d, round_to_int(barnum),
1573 100, 1, showaslog, tempgrad);
1578 if (current_update_time - obj->data.execi.last_update
1579 >= obj->data.execi.interval) {
1582 read_exec(obj->data.execi.cmd, p, text_buffer_size);
1583 barnum = get_barnum(p);
1585 if (barnum >= 0.0) {
1588 obj->data.execi.last_update = current_update_time;
1591 if(output_methods & TO_X) {
1592 new_bar(p, obj->a, obj->b, round_to_int(obj->f * 2.55));
1595 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1596 new_bar_in_shell(p, p_max_size, round_to_int(obj->f), obj->a);
1603 if (current_update_time - obj->data.execi.last_update
1604 >= obj->data.execi.interval) {
1606 char showaslog = FALSE;
1607 char tempgrad = FALSE;
1608 char *cmd = obj->data.execi.cmd;
1610 if (strstr(cmd, " "TEMPGRAD) && strlen(cmd) > strlen(" "TEMPGRAD)) {
1612 cmd += strlen(" "TEMPGRAD);
1614 if (strstr(cmd, " "LOGGRAPH) && strlen(cmd) > strlen(" "LOGGRAPH)) {
1616 cmd += strlen(" "LOGGRAPH);
1618 obj->char_a = showaslog;
1619 obj->char_b = tempgrad;
1620 read_exec(cmd, p, text_buffer_size);
1621 barnum = get_barnum(p);
1623 if (barnum >= 0.0) {
1626 obj->data.execi.last_update = current_update_time;
1628 new_graph(p, obj->a, obj->b, obj->c, obj->d, (int) (obj->f), 100, 1, obj->char_a, obj->char_b);
1631 if (current_update_time - obj->data.execi.last_update
1632 >= obj->data.execi.interval) {
1635 read_exec(obj->data.execi.cmd, p, text_buffer_size);
1636 barnum = get_barnum(p);
1638 if (barnum >= 0.0) {
1639 obj->f = 255 * barnum / 100.0;
1641 obj->data.execi.last_update = current_update_time;
1643 new_gauge(p, obj->a, obj->b, round_to_int(obj->f));
1647 if (current_update_time - obj->data.execi.last_update
1648 >= obj->data.execi.interval
1649 && obj->data.execi.interval != 0) {
1650 read_exec(obj->data.execi.cmd, obj->data.execi.buffer,
1652 obj->data.execi.last_update = current_update_time;
1654 snprintf(p, text_buffer_size, "%s", obj->data.execi.buffer);
1657 struct text_object subroot;
1658 struct information *tmp_info =
1659 malloc(sizeof(struct information));
1660 memcpy(tmp_info, cur, sizeof(struct information));
1662 if (current_update_time - obj->data.execi.last_update
1663 < obj->data.execi.interval
1664 || obj->data.execi.interval == 0) {
1665 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1667 char *output = obj->data.execi.buffer;
1668 FILE *fp = pid_popen(obj->data.execi.cmd, "r", &childpid);
1669 int length = fread(output, 1, text_buffer_size, fp);
1673 output[length] = '\0';
1674 if (length > 0 && output[length - 1] == '\n') {
1675 output[length - 1] = '\0';
1678 parse_conky_vars(&subroot, obj->data.execi.buffer, p, tmp_info);
1679 obj->data.execi.last_update = current_update_time;
1681 free_text_objects(&subroot, 1);
1685 if (!obj->data.texeci.p_timed_thread) {
1686 obj->data.texeci.p_timed_thread =
1687 timed_thread_create(&threaded_exec,
1688 (void *) obj, obj->data.texeci.interval * 1000000);
1689 if (!obj->data.texeci.p_timed_thread) {
1690 NORM_ERR("Error creating texeci timed thread");
1693 * note that we don't register this thread with the
1694 * timed_thread list, because we destroy it manually
1696 if (timed_thread_run(obj->data.texeci.p_timed_thread)) {
1697 NORM_ERR("Error running texeci timed thread");
1700 timed_thread_lock(obj->data.texeci.p_timed_thread);
1701 snprintf(p, text_buffer_size, "%s", obj->data.texeci.buffer);
1702 timed_thread_unlock(obj->data.texeci.p_timed_thread);
1706 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1708 if (mail && mail->p_timed_thread) {
1709 timed_thread_lock(mail->p_timed_thread);
1710 snprintf(p, p_max_size, "%lu", mail->unseen);
1711 timed_thread_unlock(mail->p_timed_thread);
1714 OBJ(imap_messages) {
1715 struct mail_s *mail = ensure_mail_thread(obj, imap_thread, "imap");
1717 if (mail && mail->p_timed_thread) {
1718 timed_thread_lock(mail->p_timed_thread);
1719 snprintf(p, p_max_size, "%lu", mail->messages);
1720 timed_thread_unlock(mail->p_timed_thread);
1724 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1726 if (mail && mail->p_timed_thread) {
1727 timed_thread_lock(mail->p_timed_thread);
1728 snprintf(p, p_max_size, "%lu", mail->unseen);
1729 timed_thread_unlock(mail->p_timed_thread);
1733 struct mail_s *mail = ensure_mail_thread(obj, pop3_thread, "pop3");
1735 if (mail && mail->p_timed_thread) {
1736 timed_thread_lock(mail->p_timed_thread);
1737 snprintf(p, p_max_size, "%.1f",
1738 mail->used / 1024.0 / 1024.0);
1739 timed_thread_unlock(mail->p_timed_thread);
1743 if (obj->data.fs != NULL) {
1744 if (obj->data.fs->size == 0) {
1746 if(output_methods & TO_X) {
1747 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1750 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1751 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1757 if(output_methods & TO_X) {
1758 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1759 (int) (255 - obj->data.fsbar.fs->avail * 255 /
1760 obj->data.fs->size));
1763 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1764 new_bar_in_shell(p, p_max_size,
1765 (int) (100 - obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1773 if (obj->data.fs != NULL) {
1774 human_readable(obj->data.fs->avail, p, 255);
1778 if (obj->data.fs != NULL) {
1781 if (obj->data.fs->size) {
1782 val = obj->data.fs->avail * 100 / obj->data.fs->size;
1785 percent_print(p, p_max_size, val);
1789 if (obj->data.fs != NULL) {
1790 human_readable(obj->data.fs->size, p, 255);
1794 if (obj->data.fs != NULL)
1795 snprintf(p, p_max_size, "%s", obj->data.fs->type);
1798 if (obj->data.fs != NULL) {
1799 human_readable(obj->data.fs->size - obj->data.fs->free, p,
1804 if (obj->data.fs != NULL) {
1805 if (obj->data.fs->size == 0) {
1807 if(output_methods & TO_X) {
1808 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h, 255);
1811 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1812 new_bar_in_shell(p, p_max_size, 100, obj->data.fsbar.w);
1818 if(output_methods & TO_X) {
1819 new_bar(p, obj->data.fsbar.w, obj->data.fsbar.h,
1820 (int) (obj->data.fsbar.fs->avail * 255 /
1821 obj->data.fs->size));
1824 if(!obj->data.fsbar.w) obj->data.fsbar.w = DEFAULT_BAR_WIDTH_NO_X;
1825 new_bar_in_shell(p, p_max_size,
1826 (int) (obj->data.fsbar.fs->avail * 100 / obj->data.fs->size), obj->data.fsbar.w);
1834 if (obj->data.fs != NULL) {
1837 if (obj->data.fs->size) {
1838 val = obj->data.fs->free
1843 percent_print(p, p_max_size, 100 - val);
1847 float *v = info.loadavg;
1849 if (obj->data.loadavg[2]) {
1850 snprintf(p, p_max_size, "%.2f %.2f %.2f",
1851 v[obj->data.loadavg[0] - 1],
1852 v[obj->data.loadavg[1] - 1],
1853 v[obj->data.loadavg[2] - 1]);
1854 } else if (obj->data.loadavg[1]) {
1855 snprintf(p, p_max_size, "%.2f %.2f",
1856 v[obj->data.loadavg[0] - 1],
1857 v[obj->data.loadavg[1] - 1]);
1858 } else if (obj->data.loadavg[0]) {
1859 snprintf(p, p_max_size, "%.2f",
1860 v[obj->data.loadavg[0] - 1]);
1864 new_goto(p, obj->data.i);
1867 new_tab(p, obj->data.pair.a, obj->data.pair.b);
1871 new_hr(p, obj->data.i);
1875 if (cur->nameserver_info.nscount > obj->data.i)
1876 snprintf(p, p_max_size, "%s",
1877 cur->nameserver_info.ns_list[obj->data.i]);
1881 char *skill = eve(obj->data.eve.userid, obj->data.eve.apikey, obj->data.eve.charid);
1882 snprintf(p, p_max_size, "%s", skill);
1887 if (obj->data.curl.uri != NULL) {
1888 ccurl_process_info(p, p_max_size, obj->data.curl.uri, obj->data.curl.interval);
1890 NORM_ERR("error processing Curl data");
1896 if (obj->data.rss.uri != NULL) {
1897 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);
1899 NORM_ERR("error processing RSS data");
1905 if (obj->data.weather.uri != NULL) {
1906 weather_process_info(p, p_max_size, obj->data.weather.uri, obj->data.weather.data_type, obj->data.weather.interval);
1908 NORM_ERR("error processing weather data, check that you have a valid XOAP key if using XOAP.");
1913 OBJ(weather_forecast) {
1914 if (obj->data.weather_forecast.uri != NULL) {
1915 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);
1917 NORM_ERR("error processing weather forecast data, check that you have a valid XOAP key if using XOAP.");
1923 char *str = llua_getstring(obj->data.s);
1925 snprintf(p, p_max_size, "%s", str);
1930 char *str = llua_getstring(obj->data.s);
1938 if (llua_getnumber(obj->data.s, &per)) {
1940 if(output_methods & TO_X) {
1941 new_bar(p, obj->a, obj->b, (per/100.0 * 255));
1944 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
1945 new_bar_in_shell(p, p_max_size, per, obj->a);
1954 if (llua_getnumber(obj->data.s, &per)) {
1955 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1956 per, obj->e, 1, obj->char_a, obj->char_b);
1961 if (llua_getnumber(obj->data.s, &per)) {
1962 new_gauge(p, obj->a, obj->b, (per/100.0 * 255));
1966 #endif /* HAVE_LUA */
1972 if (get_hddtemp_info(obj->data.s, &val, &unit)) {
1973 snprintf(p, p_max_size, "N/A");
1975 temp_print(p, p_max_size, (double)val,
1976 (unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
1981 new_offset(p, obj->data.i);
1984 new_voffset(p, obj->data.i);
1990 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
1991 obj->data.sysfs.devtype, obj->data.sysfs.type);
1993 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
1995 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
1996 temp_print(p, p_max_size, r, TEMP_CELSIUS);
1997 } else if (r >= 100.0 || r == 0) {
1998 snprintf(p, p_max_size, "%d", (int) r);
2000 snprintf(p, p_max_size, "%.1f", r);
2006 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2007 obj->data.sysfs.devtype, obj->data.sysfs.type);
2009 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2011 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2012 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2013 } else if (r >= 100.0 || r == 0) {
2014 snprintf(p, p_max_size, "%d", (int) r);
2016 snprintf(p, p_max_size, "%.1f", r);
2022 r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
2023 obj->data.sysfs.devtype, obj->data.sysfs.type);
2025 r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
2027 if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
2028 temp_print(p, p_max_size, r, TEMP_CELSIUS);
2029 } else if (r >= 100.0 || r == 0) {
2030 snprintf(p, p_max_size, "%d", (int) r);
2032 snprintf(p, p_max_size, "%.1f", r);
2035 #endif /* __linux__ */
2037 new_alignr(p, obj->data.i);
2040 new_alignc(p, obj->data.i);
2043 char buf[max_user_text];
2044 struct information *tmp_info =
2045 malloc(sizeof(struct information));
2046 memcpy(tmp_info, cur, sizeof(struct information));
2047 generate_text_internal(buf, max_user_text,
2048 *obj->sub, tmp_info);
2050 if (strlen(buf) != 0) {
2056 char expression[max_user_text];
2058 struct information *tmp_info;
2060 tmp_info = malloc(sizeof(struct information));
2061 memcpy(tmp_info, cur, sizeof(struct information));
2062 generate_text_internal(expression, max_user_text,
2063 *obj->sub, tmp_info);
2064 DBGP("parsed arg into '%s'", expression);
2066 val = compare(expression);
2068 NORM_ERR("compare failed for expression '%s'",
2076 if (obj->data.ifblock.str
2077 && !check_contains(obj->data.ifblock.s,
2078 obj->data.ifblock.str)) {
2080 } else if (obj->data.ifblock.s
2081 && access(obj->data.ifblock.s, F_OK)) {
2086 if ((obj->data.ifblock.s)
2087 && (!check_mount(obj->data.ifblock.s))) {
2093 if (!get_process_by_name(obj->data.ifblock.s)) {
2095 if ((obj->data.ifblock.s) && system(obj->data.ifblock.s)) {
2100 #if defined(__linux__)
2102 snprintf(p, p_max_size, "%s", get_ioscheduler(obj->data.s));
2106 snprintf(p, p_max_size, "%s", cur->uname_s.release);
2109 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
2114 human_readable(cur->mem * 1024, p, 255);
2117 human_readable(cur->memeasyfree * 1024, p, 255);
2120 human_readable(cur->memfree * 1024, p, 255);
2123 human_readable(cur->memmax * 1024, p, 255);
2127 percent_print(p, p_max_size, cur->mem * 100 / cur->memmax);
2131 new_gauge(p, obj->data.pair.a, obj->data.pair.b,
2132 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2137 if(output_methods & TO_X) {
2138 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2139 cur->memmax ? (cur->mem * 255) / (cur->memmax) : 0);
2142 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2143 new_bar_in_shell(p, p_max_size, cur->memmax ? (cur->mem * 100) / (cur->memmax) : 0, obj->data.pair.a);
2150 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2151 cur->memmax ? (cur->mem * 100.0) / (cur->memmax) : 0.0,
2152 100, 1, obj->char_a, obj->char_b);
2157 percent_print(p, p_max_size, mixer_get_avg(obj->data.l));
2160 percent_print(p, p_max_size, mixer_get_left(obj->data.l));
2163 percent_print(p, p_max_size, mixer_get_right(obj->data.l));
2167 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2168 mixer_to_255(obj->data.mixerbar.l,mixer_get_avg(obj->data.mixerbar.l)));
2171 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2172 mixer_to_255(obj->data.mixerbar.l,mixer_get_left(obj->data.mixerbar.l)));
2175 new_bar(p, obj->data.mixerbar.w, obj->data.mixerbar.h,
2176 mixer_to_255(obj->data.mixerbar.l,mixer_get_right(obj->data.mixerbar.l)));
2179 OBJ(if_mixer_mute) {
2180 if (!mixer_is_mute(obj->data.ifblock.i)) {
2185 #define NOT_IN_X "Not running in X"
2187 if(x_initialised != YES) {
2188 strncpy(p, NOT_IN_X, p_max_size);
2190 snprintf(p, p_max_size, "%d", cur->x11.monitor.current);
2193 OBJ(monitor_number) {
2194 if(x_initialised != YES) {
2195 strncpy(p, NOT_IN_X, p_max_size);
2197 snprintf(p, p_max_size, "%d", cur->x11.monitor.number);
2201 if(x_initialised != YES) {
2202 strncpy(p, NOT_IN_X, p_max_size);
2204 snprintf(p, p_max_size, "%d", cur->x11.desktop.current);
2207 OBJ(desktop_number) {
2208 if(x_initialised != YES) {
2209 strncpy(p, NOT_IN_X, p_max_size);
2211 snprintf(p, p_max_size, "%d", cur->x11.desktop.number);
2215 if(x_initialised != YES) {
2216 strncpy(p, NOT_IN_X, p_max_size);
2217 }else if(cur->x11.desktop.name != NULL) {
2218 strncpy(p, cur->x11.desktop.name, p_max_size);
2225 update_mail_count(&obj->data.local_mail);
2226 snprintf(p, p_max_size, "%d", obj->data.local_mail.mail_count);
2229 update_mail_count(&obj->data.local_mail);
2230 snprintf(p, p_max_size, "%d",
2231 obj->data.local_mail.new_mail_count);
2234 update_mail_count(&obj->data.local_mail);
2235 snprintf(p, p_max_size, "%d",
2236 obj->data.local_mail.seen_mail_count);
2239 update_mail_count(&obj->data.local_mail);
2240 snprintf(p, p_max_size, "%d",
2241 obj->data.local_mail.unseen_mail_count);
2243 OBJ(flagged_mails) {
2244 update_mail_count(&obj->data.local_mail);
2245 snprintf(p, p_max_size, "%d",
2246 obj->data.local_mail.flagged_mail_count);
2248 OBJ(unflagged_mails) {
2249 update_mail_count(&obj->data.local_mail);
2250 snprintf(p, p_max_size, "%d",
2251 obj->data.local_mail.unflagged_mail_count);
2253 OBJ(forwarded_mails) {
2254 update_mail_count(&obj->data.local_mail);
2255 snprintf(p, p_max_size, "%d",
2256 obj->data.local_mail.forwarded_mail_count);
2258 OBJ(unforwarded_mails) {
2259 update_mail_count(&obj->data.local_mail);
2260 snprintf(p, p_max_size, "%d",
2261 obj->data.local_mail.unforwarded_mail_count);
2263 OBJ(replied_mails) {
2264 update_mail_count(&obj->data.local_mail);
2265 snprintf(p, p_max_size, "%d",
2266 obj->data.local_mail.replied_mail_count);
2268 OBJ(unreplied_mails) {
2269 update_mail_count(&obj->data.local_mail);
2270 snprintf(p, p_max_size, "%d",
2271 obj->data.local_mail.unreplied_mail_count);
2274 update_mail_count(&obj->data.local_mail);
2275 snprintf(p, p_max_size, "%d",
2276 obj->data.local_mail.draft_mail_count);
2278 OBJ(trashed_mails) {
2279 update_mail_count(&obj->data.local_mail);
2280 snprintf(p, p_max_size, "%d",
2281 obj->data.local_mail.trashed_mail_count);
2284 mbox_scan(obj->data.mboxscan.args, obj->data.mboxscan.output,
2286 snprintf(p, p_max_size, "%s", obj->data.mboxscan.output);
2289 snprintf(p, p_max_size, "%s", cur->uname_s.nodename);
2292 new_outline(p, obj->data.l);
2295 spaced_print(p, p_max_size, "%hu", 4, cur->procs);
2297 OBJ(running_processes) {
2298 spaced_print(p, p_max_size, "%hu", 4, cur->run_procs);
2301 snprintf(p, p_max_size, "%s", obj->data.s);
2305 new_bg(p, obj->data.l);
2308 new_stippled_hr(p, obj->data.pair.a, obj->data.pair.b);
2312 human_readable(cur->swap * 1024, p, 255);
2315 human_readable(cur->swapfree * 1024, p, 255);
2318 human_readable(cur->swapmax * 1024, p, 255);
2321 if (cur->swapmax == 0) {
2322 strncpy(p, "No swap", p_max_size);
2324 percent_print(p, p_max_size, cur->swap * 100 / cur->swapmax);
2329 if(output_methods & TO_X) {
2330 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2331 cur->swapmax ? (cur->swap * 255) / (cur->swapmax) : 0);
2334 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2335 new_bar_in_shell(p, p_max_size, cur->swapmax ? (cur->swap * 100) / (cur->swapmax) : 0, obj->data.pair.a);
2341 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
2344 time_t t = time(NULL);
2345 struct tm *tm = localtime(&t);
2347 setlocale(LC_TIME, "");
2348 strftime(p, p_max_size, obj->data.s, tm);
2351 time_t t = time(NULL);
2352 struct tm *tm = gmtime(&t);
2354 strftime(p, p_max_size, obj->data.s, tm);
2361 if (obj->data.tztime.tz) {
2362 oldTZ = getenv("TZ");
2363 setenv("TZ", obj->data.tztime.tz, 1);
2369 setlocale(LC_TIME, "");
2370 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
2372 setenv("TZ", oldTZ, 1);
2377 // Needless to free oldTZ since getenv gives ptr to static data
2380 human_readable(obj->data.net->recv, p, 255);
2383 human_readable(obj->data.net->trans, p, 255);
2386 snprintf(p, p_max_size, "%d", total_updates);
2389 if(total_updates % updatereset != obj->data.ifblock.i - 1) {
2394 human_readable(obj->data.net->trans_speed, p, 255);
2397 spaced_print(p, p_max_size, "%.1f", 8,
2398 obj->data.net->trans_speed / 1024.0);
2402 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2403 obj->data.net->trans_speed / 1024.0, obj->e, 1, obj->char_a, obj->char_b);
2407 format_seconds_short(p, p_max_size, (int) cur->uptime);
2410 format_seconds(p, p_max_size, (int) cur->uptime);
2413 snprintf(p, p_max_size, "%s", cur->users.names);
2416 snprintf(p, p_max_size, "%s", cur->users.terms);
2419 snprintf(p, p_max_size, "%s", cur->users.times);
2422 snprintf(p, p_max_size, "%d", cur->users.number);
2424 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \
2425 || defined(__OpenBSD__)) && (defined(i386) || defined(__i386__))
2429 msg = get_apm_adapter();
2430 snprintf(p, p_max_size, "%s", msg);
2433 OBJ(apm_battery_life) {
2436 msg = get_apm_battery_life();
2437 snprintf(p, p_max_size, "%s", msg);
2440 OBJ(apm_battery_time) {
2443 msg = get_apm_battery_time();
2444 snprintf(p, p_max_size, "%s", msg);
2447 #endif /* __FreeBSD__ __OpenBSD__ */
2450 #define mpd_printf(fmt, val) \
2451 snprintf(p, p_max_size, fmt, mpd_get_info()->val)
2452 #define mpd_sprintf(val) { \
2453 if (!obj->data.i || obj->data.i > p_max_size) \
2454 mpd_printf("%s", val); \
2456 snprintf(p, obj->data.i, "%s", mpd_get_info()->val); \
2461 mpd_sprintf(artist);
2465 mpd_printf("%s", random);
2467 mpd_printf("%s", repeat);
2475 mpd_printf("%d", volume);
2477 mpd_printf("%d", bitrate);
2479 mpd_printf("%s", status);
2481 format_media_player_time(p, p_max_size, mpd_get_info()->elapsed);
2484 format_media_player_time(p, p_max_size, mpd_get_info()->length);
2487 percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100));
2491 if(output_methods & TO_X) {
2492 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2493 (int) (mpd_get_info()->progress * 255.0f));
2496 if(!obj->data.pair.a) obj->data.pair.a = DEFAULT_BAR_WIDTH_NO_X;
2497 new_bar_in_shell(p, p_max_size, (int) (mpd_get_info()->progress * 100.0f), obj->data.pair.a);
2503 struct mpd_s *mpd = mpd_get_info();
2504 int len = obj->data.i;
2505 if (len == 0 || len > p_max_size)
2508 memset(p, 0, p_max_size);
2509 if (mpd->artist && *mpd->artist &&
2510 mpd->title && *mpd->title) {
2511 snprintf(p, len, "%s - %s", mpd->artist,
2513 } else if (mpd->title && *mpd->title) {
2514 snprintf(p, len, "%s", mpd->title);
2515 } else if (mpd->artist && *mpd->artist) {
2516 snprintf(p, len, "%s", mpd->artist);
2517 } else if (mpd->file && *mpd->file) {
2518 snprintf(p, len, "%s", mpd->file);
2523 OBJ(if_mpd_playing) {
2524 if (!mpd_get_info()->is_playing) {
2533 #define MOC_PRINT(t, a) \
2534 snprintf(p, p_max_size, "%s", (moc.t ? moc.t : a))
2536 MOC_PRINT(state, "??");
2539 MOC_PRINT(file, "no file");
2542 MOC_PRINT(title, "no title");
2545 MOC_PRINT(artist, "no artist");
2548 MOC_PRINT(song, "no song");
2551 MOC_PRINT(album, "no album");
2553 OBJ(moc_totaltime) {
2554 MOC_PRINT(totaltime, "0:00");
2557 MOC_PRINT(timeleft, "0:00");
2560 MOC_PRINT(curtime, "0:00");
2563 MOC_PRINT(bitrate, "0Kbps");
2566 MOC_PRINT(rate, "0KHz");
2572 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
2575 snprintf(p, p_max_size, "%s", cur->xmms2.album);
2578 snprintf(p, p_max_size, "%s", cur->xmms2.title);
2581 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
2583 OBJ(xmms2_comment) {
2584 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
2587 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2590 snprintf(p, p_max_size, "%s", cur->xmms2.status);
2593 snprintf(p, p_max_size, "%s", cur->xmms2.date);
2595 OBJ(xmms2_tracknr) {
2596 if (cur->xmms2.tracknr != -1) {
2597 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
2600 OBJ(xmms2_bitrate) {
2601 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
2604 snprintf(p, p_max_size, "%u", cur->xmms2.id);
2607 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
2609 OBJ(xmms2_elapsed) {
2610 snprintf(p, p_max_size, "%02d:%02d", cur->xmms2.elapsed / 60000,
2611 (cur->xmms2.elapsed / 1000) % 60);
2613 OBJ(xmms2_duration) {
2614 snprintf(p, p_max_size, "%02d:%02d",
2615 cur->xmms2.duration / 60000,
2616 (cur->xmms2.duration / 1000) % 60);
2618 OBJ(xmms2_percent) {
2619 snprintf(p, p_max_size, "%2.0f", cur->xmms2.progress * 100);
2623 new_bar(p, obj->data.pair.a, obj->data.pair.b,
2624 (int) (cur->xmms2.progress * 255.0f));
2627 OBJ(xmms2_playlist) {
2628 snprintf(p, p_max_size, "%s", cur->xmms2.playlist);
2630 OBJ(xmms2_timesplayed) {
2631 snprintf(p, p_max_size, "%i", cur->xmms2.timesplayed);
2634 if (strlen(cur->xmms2.title) < 2
2635 && strlen(cur->xmms2.title) < 2) {
2636 snprintf(p, p_max_size, "%s", cur->xmms2.url);
2638 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist,
2642 OBJ(if_xmms2_connected) {
2643 if (cur->xmms2.conn_state != 1) {
2649 OBJ(audacious_status) {
2650 snprintf(p, p_max_size, "%s",
2651 cur->audacious.items[AUDACIOUS_STATUS]);
2653 OBJ(audacious_title) {
2654 snprintf(p, cur->audacious.max_title_len > 0
2655 ? cur->audacious.max_title_len : p_max_size, "%s",
2656 cur->audacious.items[AUDACIOUS_TITLE]);
2658 OBJ(audacious_length) {
2659 snprintf(p, p_max_size, "%s",
2660 cur->audacious.items[AUDACIOUS_LENGTH]);
2662 OBJ(audacious_length_seconds) {
2663 snprintf(p, p_max_size, "%s",
2664 cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2666 OBJ(audacious_position) {
2667 snprintf(p, p_max_size, "%s",
2668 cur->audacious.items[AUDACIOUS_POSITION]);
2670 OBJ(audacious_position_seconds) {
2671 snprintf(p, p_max_size, "%s",
2672 cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
2674 OBJ(audacious_bitrate) {
2675 snprintf(p, p_max_size, "%s",
2676 cur->audacious.items[AUDACIOUS_BITRATE]);
2678 OBJ(audacious_frequency) {
2679 snprintf(p, p_max_size, "%s",
2680 cur->audacious.items[AUDACIOUS_FREQUENCY]);
2682 OBJ(audacious_channels) {
2683 snprintf(p, p_max_size, "%s",
2684 cur->audacious.items[AUDACIOUS_CHANNELS]);
2686 OBJ(audacious_filename) {
2687 snprintf(p, p_max_size, "%s",
2688 cur->audacious.items[AUDACIOUS_FILENAME]);
2690 OBJ(audacious_playlist_length) {
2691 snprintf(p, p_max_size, "%s",
2692 cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
2694 OBJ(audacious_playlist_position) {
2695 snprintf(p, p_max_size, "%s",
2696 cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
2698 OBJ(audacious_main_volume) {
2699 snprintf(p, p_max_size, "%s",
2700 cur->audacious.items[AUDACIOUS_MAIN_VOLUME]);
2703 OBJ(audacious_bar) {
2707 atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
2708 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
2709 new_bar(p, obj->a, obj->b, (int) (progress * 255.0f));
2712 #endif /* AUDACIOUS */
2716 snprintf(p, p_max_size, "%s", cur->bmpx.title);
2719 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
2722 snprintf(p, p_max_size, "%s", cur->bmpx.album);
2725 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
2728 snprintf(p, p_max_size, "%i", cur->bmpx.track);
2731 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
2734 /* we have four different types of top (top, top_mem,
2735 * top_time and top_io). To avoid having almost-same code four
2736 * times, we have this special handler. */
2739 parse_top_args("top", obj->data.top.s, obj);
2740 if (!needed) needed = cur->cpu;
2742 parse_top_args("top_mem", obj->data.top.s, obj);
2743 if (!needed) needed = cur->memu;
2745 parse_top_args("top_time", obj->data.top.s, obj);
2746 if (!needed) needed = cur->time;
2749 parse_top_args("top_io", obj->data.top.s, obj);
2750 if (!needed) needed = cur->io;
2753 if (needed[obj->data.top.num]) {
2756 switch (obj->data.top.type) {
2758 snprintf(p, top_name_width + 1, "%-*s", top_name_width,
2759 needed[obj->data.top.num]->name);
2762 snprintf(p, 7, "%6.2f",
2763 needed[obj->data.top.num]->amount);
2766 snprintf(p, 6, "%5i",
2767 needed[obj->data.top.num]->pid);
2770 snprintf(p, 7, "%6.2f",
2771 needed[obj->data.top.num]->totalmem);
2774 timeval = format_time(
2775 needed[obj->data.top.num]->total_cpu_time, 9);
2776 snprintf(p, 10, "%9s", timeval);
2780 human_readable(needed[obj->data.top.num]->rss,
2784 human_readable(needed[obj->data.top.num]->vsize,
2788 case TOP_READ_BYTES:
2789 human_readable(needed[obj->data.top.num]->read_bytes / update_interval,
2792 case TOP_WRITE_BYTES:
2793 human_readable(needed[obj->data.top.num]->write_bytes / update_interval,
2797 snprintf(p, 7, "%6.2f",
2798 needed[obj->data.top.num]->io_perc);
2804 print_tailhead("tail", obj, p, p_max_size);
2807 print_tailhead("head", obj, p, p_max_size);
2810 FILE *fp = open_file(obj->data.s, &obj->a);
2813 /* FIXME: use something more general (see also tail.c, head.c */
2814 #define BUFSZ 0x1000
2819 while(fgets(buf, BUFSZ, fp) != NULL){
2820 for(j = 0; buf[j] != 0; j++) {
2821 if(buf[j] == '\n') {
2826 sprintf(p, "%d", lines);
2829 sprintf(p, "File Unreadable");
2834 FILE *fp = open_file(obj->data.s, &obj->a);
2839 char inword = FALSE;
2842 while(fgets(buf, BUFSZ, fp) != NULL){
2843 for(j = 0; buf[j] != 0; j++) {
2844 if(!isspace(buf[j])) {
2845 if(inword == FALSE) {
2854 sprintf(p, "%d", words);
2857 sprintf(p, "File Unreadable");
2860 #ifdef TCP_PORT_MONITOR
2862 tcp_portmon_action(p, p_max_size,
2863 &obj->data.tcp_port_monitor);
2865 #endif /* TCP_PORT_MONITOR */
2869 set_iconv_converting(1);
2870 set_iconv_selected(obj->a);
2873 set_iconv_converting(0);
2874 set_iconv_selected(0);
2876 #endif /* HAVE_ICONV */
2878 OBJ(entropy_avail) {
2879 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
2882 percent_print(p, p_max_size,
2883 cur->entropy.entropy_avail *
2884 100 / cur->entropy.poolsize);
2886 OBJ(entropy_poolsize) {
2887 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
2890 double entropy_perc;
2892 entropy_perc = (double) cur->entropy.entropy_avail /
2893 (double) cur->entropy.poolsize;
2895 if(output_methods & TO_X) {
2896 new_bar(p, obj->a, obj->b, (int) (entropy_perc * 255.0f));
2899 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
2900 new_bar_in_shell(p, p_max_size, (int) (entropy_perc * 100.0f), obj->a);
2909 s = smapi_get_val(obj->data.s);
2910 snprintf(p, p_max_size, "%s", s);
2914 OBJ(if_smapi_bat_installed) {
2916 if(obj->data.ifblock.s && sscanf(obj->data.ifblock.s, "%i", &idx) == 1) {
2917 if(!smapi_bat_installed(idx)) {
2921 NORM_ERR("argument to if_smapi_bat_installed must be an integer");
2923 OBJ(smapi_bat_perc) {
2925 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2926 val = smapi_bat_installed(idx) ?
2927 smapi_get_bat_int(idx, "remaining_percent") : 0;
2928 percent_print(p, p_max_size, val);
2930 NORM_ERR("argument to smapi_bat_perc must be an integer");
2932 OBJ(smapi_bat_temp) {
2934 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2935 val = smapi_bat_installed(idx) ?
2936 smapi_get_bat_int(idx, "temperature") : 0;
2937 /* temperature is in milli degree celsius */
2938 temp_print(p, p_max_size, val / 1000, TEMP_CELSIUS);
2940 NORM_ERR("argument to smapi_bat_temp must be an integer");
2942 OBJ(smapi_bat_power) {
2944 if(obj->data.s && sscanf(obj->data.s, "%i", &idx) == 1) {
2945 val = smapi_bat_installed(idx) ?
2946 smapi_get_bat_int(idx, "power_now") : 0;
2947 /* power_now is in mW, set to W with one digit precision */
2948 snprintf(p, p_max_size, "%.1f", ((double)val / 1000));
2950 NORM_ERR("argument to smapi_bat_power must be an integer");
2953 OBJ(smapi_bat_bar) {
2954 if(obj->data.i >= 0 && smapi_bat_installed(obj->data.i))
2955 new_bar(p, obj->a, obj->b, (int)
2956 (255 * smapi_get_bat_int(obj->data.i, "remaining_percent") / 100));
2958 new_bar(p, obj->a, obj->b, 0);
2964 char buf[max_user_text];
2966 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2967 snprintf(p, p_max_size, "%s", buf);
2973 //blinking like this can look a bit ugly if the chars in the font don't have the same width
2974 char buf[max_user_text];
2977 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2978 snprintf(p, p_max_size, "%s", buf);
2979 if(total_updates % 2) {
2980 for(j=0; p[j] != 0; j++) {
2986 char buf[max_user_text];
2988 char unit[16]; // 16 because we can also have long names (like mega-bytes)
2990 generate_text_internal(buf, max_user_text, *obj->sub, cur);
2991 if(sscanf(buf, "%lli%s", &bytes, unit) == 2 && strlen(unit) < 16){
2992 if(strncasecmp("b", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes);
2993 else if(strncasecmp("k", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024);
2994 else if(strncasecmp("m", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024);
2995 else if(strncasecmp("g", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024);
2996 else if(strncasecmp("t", unit, 1) == 0) snprintf(buf, max_user_text, "%lli", bytes * 1024 * 1024 * 1024 * 1024);
2998 snprintf(p, p_max_size, "%s", buf);
3001 unsigned int j, colorchanges = 0, frontcolorchanges = 0, visibcolorchanges = 0, strend;
3003 char buf[max_user_text];
3004 generate_text_internal(buf, max_user_text,
3006 for(j = 0; buf[j] != 0; j++) {
3008 case '\n': //place all the lines behind each other with LINESEPARATOR between them
3009 #define LINESEPARATOR '|'
3010 buf[j]=LINESEPARATOR;
3017 //no scrolling necessary if the length of the text to scroll is too short
3018 if (strlen(buf) - colorchanges <= obj->data.scroll.show) {
3019 snprintf(p, p_max_size, "%s", buf);
3022 //make sure a colorchange at the front is not part of the string we are going to show
3023 while(*(buf + obj->data.scroll.start) == SPECIAL_CHAR) {
3024 obj->data.scroll.start++;
3026 //place all chars that should be visible in p, including colorchanges
3027 for(j=0; j < obj->data.scroll.show + visibcolorchanges; j++) {
3028 p[j] = *(buf + obj->data.scroll.start + j);
3029 if(p[j] == SPECIAL_CHAR) {
3030 visibcolorchanges++;
3032 //if there is still room fill it with spaces
3035 for(; j < obj->data.scroll.show + visibcolorchanges; j++) {
3039 //count colorchanges in front of the visible part and place that many colorchanges in front of the visible part
3040 for(j = 0; j < obj->data.scroll.start; j++) {
3041 if(buf[j] == SPECIAL_CHAR) frontcolorchanges++;
3043 pwithcolors=malloc(strlen(p) + 1 + colorchanges - visibcolorchanges);
3044 for(j = 0; j < frontcolorchanges; j++) {
3045 pwithcolors[j] = SPECIAL_CHAR;
3048 strcat(pwithcolors,p);
3049 strend = strlen(pwithcolors);
3050 //and place the colorchanges not in front or in the visible part behind the visible part
3051 for(j = 0; j < colorchanges - frontcolorchanges - visibcolorchanges; j++) {
3052 pwithcolors[strend + j] = SPECIAL_CHAR;
3054 pwithcolors[strend + j] = 0;
3055 strcpy(p, pwithcolors);
3058 obj->data.scroll.start += obj->data.scroll.step;
3059 if(buf[obj->data.scroll.start] == 0){
3060 obj->data.scroll.start = 0;
3063 //reset color when scroll is finished
3064 new_fg(p + strlen(p), obj->data.scroll.resetcolor);
3068 char buf[2][max_user_text];
3075 struct llrows* next;
3077 struct llrows *ll_rows[2], *current[2];
3078 struct text_object * objsub = obj->sub;
3081 for(i=0; i<2; i++) {
3084 ll_rows[i] = malloc(sizeof(struct llrows));
3085 current[i] = ll_rows[i];
3086 for(j=0; j<i; j++) objsub = objsub->sub;
3087 generate_text_internal(buf[i], max_user_text, *objsub, cur);
3088 for(j=0; buf[i][j] != 0; j++) {
3089 if(buf[i][j] == '\t') buf[i][j] = ' ';
3090 if(buf[i][j] == '\n') {
3092 current[i]->row = strdup(buf[i]+nextstart);
3093 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3094 current[i]->next = malloc(sizeof(struct llrows));
3095 current[i] = current[i]->next;
3100 current[i]->row = strdup(buf[i]+nextstart);
3101 if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row);
3102 current[i]->next = NULL;
3103 current[i] = ll_rows[i];
3105 for(j=0; j < (nr_rows[0] > nr_rows[1] ? nr_rows[0] : nr_rows[1] ); j++) {
3107 strcat(p, current[0]->row);
3108 i=strlen(current[0]->row);
3110 while(i < longest) {
3115 strcat(p, obj->data.combine.seperation);
3116 strcat(p, current[1]->row);
3120 #pragma omp parallel for schedule(dynamic,10)
3121 #endif /* HAVE_OPENMP */
3122 for(i=0; i<2; i++) if(current[i]) current[i]=current[i]->next;
3125 #pragma omp parallel for schedule(dynamic,10)
3126 #endif /* HAVE_OPENMP */
3127 for(i=0; i<2; i++) {
3128 while(ll_rows[i] != NULL) {
3129 current[i]=ll_rows[i];
3130 free(current[i]->row);
3131 ll_rows[i]=current[i]->next;
3138 int value = get_nvidia_value(obj->data.nvidia.type, display);
3140 snprintf(p, p_max_size, "N/A");
3141 else if (obj->data.nvidia.type == NV_TEMP)
3142 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
3143 else if (obj->data.nvidia.print_as_float &&
3144 value > 0 && value < 100)
3145 snprintf(p, p_max_size, "%.1f", (float)value);
3147 snprintf(p, p_max_size, "%d", value);
3152 /* This is just a meta-object to set host:port */
3155 snprintf(p, p_max_size, "%s",
3156 cur->apcupsd.items[APCUPSD_NAME]);
3158 OBJ(apcupsd_model) {
3159 snprintf(p, p_max_size, "%s",
3160 cur->apcupsd.items[APCUPSD_MODEL]);
3162 OBJ(apcupsd_upsmode) {
3163 snprintf(p, p_max_size, "%s",
3164 cur->apcupsd.items[APCUPSD_UPSMODE]);
3166 OBJ(apcupsd_cable) {
3167 snprintf(p, p_max_size, "%s",
3168 cur->apcupsd.items[APCUPSD_CABLE]);
3170 OBJ(apcupsd_status) {
3171 snprintf(p, p_max_size, "%s",
3172 cur->apcupsd.items[APCUPSD_STATUS]);
3174 OBJ(apcupsd_linev) {
3175 snprintf(p, p_max_size, "%s",
3176 cur->apcupsd.items[APCUPSD_LINEV]);
3179 snprintf(p, p_max_size, "%s",
3180 cur->apcupsd.items[APCUPSD_LOAD]);
3182 OBJ(apcupsd_loadbar) {
3185 if(output_methods & TO_X) {
3186 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3187 new_bar(p, obj->a, obj->b, (int) progress);
3190 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3191 if(!obj->a) obj->a = DEFAULT_BAR_WIDTH_NO_X;
3192 new_bar_in_shell(p, p_max_size, (int) progress, obj->a);
3198 OBJ(apcupsd_loadgraph) {
3200 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]);
3201 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3202 (int)progress, 100, 1, obj->char_a, obj->char_b);
3204 OBJ(apcupsd_loadgauge) {
3206 progress = atof(cur->apcupsd.items[APCUPSD_LOAD]) / 100.0 * 255.0;
3207 new_gauge(p, obj->a, obj->b,
3211 OBJ(apcupsd_charge) {
3212 snprintf(p, p_max_size, "%s",
3213 cur->apcupsd.items[APCUPSD_CHARGE]);
3215 OBJ(apcupsd_timeleft) {
3216 snprintf(p, p_max_size, "%s",
3217 cur->apcupsd.items[APCUPSD_TIMELEFT]);
3220 snprintf(p, p_max_size, "%s",
3221 cur->apcupsd.items[APCUPSD_TEMP]);
3223 OBJ(apcupsd_lastxfer) {
3224 snprintf(p, p_max_size, "%s",
3225 cur->apcupsd.items[APCUPSD_LASTXFER]);
3227 #endif /* APCUPSD */
3234 size_t a = strlen(p);
3237 iconv_convert(a, buff_in, p, p_max_size);
3238 #endif /* HAVE_ICONV */
3239 if (obj->type != OBJ_text && obj->type != OBJ_execp && obj->type != OBJ_execpi
3240 && obj->type != OBJ_lua && obj->type != OBJ_lua_parse) {
3241 substitute_newlines(p, a - 2);
3249 /* load any new fonts we may have had */
3250 if (need_to_load_fonts) {
3256 void evaluate(const char *text, char *buffer)
3258 struct information *tmp_info;
3259 struct text_object subroot;
3261 tmp_info = malloc(sizeof(struct information));
3262 memcpy(tmp_info, &info, sizeof(struct information));
3263 parse_conky_vars(&subroot, text, buffer, tmp_info);
3264 DBGP("evaluated '%s' to '%s'", text, buffer);
3266 free_text_objects(&subroot, 1);
3270 double current_update_time, next_update_time, last_update_time;
3272 static void generate_text(void)
3274 struct information *cur = &info;
3281 current_update_time = get_time();
3285 /* add things to the buffer */
3291 generate_text_internal(p, max_user_text, global_root_object, cur);
3293 if (stuff_in_uppercase) {
3296 tmp_p = text_buffer;
3298 *tmp_p = toupper(*tmp_p);
3303 next_update_time += update_interval;
3304 if (next_update_time < get_time()) {
3305 next_update_time = get_time() + update_interval;
3306 } else if (next_update_time > get_time() + update_interval) {
3307 next_update_time = get_time() + update_interval;
3309 last_update_time = current_update_time;
3313 void set_update_interval(double interval)
3315 update_interval = interval;
3316 update_interval_old = interval;
3319 static inline int get_string_width(const char *s)
3322 if (output_methods & TO_X) {
3323 return *s ? calc_text_width(s, strlen(s)) : 0;
3330 static int get_string_width_special(char *s, int special_index)
3337 if ((output_methods & TO_X) == 0) {
3338 return (s) ? strlen(s) : 0;
3345 p = strndup(s, text_buffer_size);
3349 if (*p == SPECIAL_CHAR) {
3350 /* shift everything over by 1 so that the special char
3351 * doesn't mess up the size calculation */
3352 for (i = 0; i < (long)strlen(p); i++) {
3353 *(p + i) = *(p + i + 1);
3355 if (specials[special_index + idx].type == GRAPH
3356 || specials[special_index + idx].type == GAUGE
3357 || specials[special_index + idx].type == BAR) {
3358 width += specials[special_index + idx].width;
3361 } else if (*p == SECRIT_MULTILINE_CHAR) {
3368 if (strlen(final) > 1) {
3369 width += calc_text_width(final, strlen(final));
3375 static int text_size_updater(char *s, int special_index);
3377 int last_font_height;
3378 static void update_text_area(void)
3382 if ((output_methods & TO_X) == 0)
3384 /* update text size if it isn't fixed */
3389 text_width = minimum_width;
3391 last_font_height = font_height();
3392 for_each_line(text_buffer, text_size_updater);
3394 if (text_height < minimum_height) {
3395 text_height = minimum_height;
3397 if (text_width > maximum_width && maximum_width > 0) {
3398 text_width = maximum_width;
3402 /* get text position on workarea */
3403 switch (text_alignment) {
3410 x = workarea[2] - text_width - gap_x;
3415 x = workarea[2] / 2 - text_width / 2 - gap_x;
3422 y = workarea[3] - text_height - gap_y;
3426 x = workarea[2] - text_width - gap_x;
3427 y = workarea[3] - text_height - gap_y;
3431 x = workarea[2] / 2 - text_width / 2 - gap_x;
3432 y = workarea[3] - text_height - gap_y;
3437 y = workarea[3] / 2 - text_height / 2 - gap_y;
3441 x = workarea[2] - text_width - gap_x;
3442 y = workarea[3] / 2 - text_height / 2 - gap_y;
3446 case NONE: // Let the WM manage the window
3457 if (own_window && !fixed_pos) {
3460 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
3461 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
3462 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3463 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3467 /* If window size doesn't match to workarea's size,
3468 * then window probably includes panels (gnome).
3469 * Blah, doesn't work on KDE. */
3470 if (workarea[2] != window.width || workarea[3] != window.height) {
3479 /* update lua window globals */
3480 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3481 #endif /* HAVE_LUA */
3486 static int cur_x, cur_y; /* current x and y for drawing */
3488 //draw_mode also without X11 because we only need to print to stdout with FG
3489 static int draw_mode; /* FG, BG or OUTLINE */
3491 static long current_color;
3493 static int text_size_updater(char *s, int special_index)
3498 if ((output_methods & TO_X) == 0)
3500 /* get string widths and skip specials */
3503 if (*p == SPECIAL_CHAR) {
3505 w += get_string_width(s);
3508 if (specials[special_index].type == BAR
3509 || specials[special_index].type == GAUGE
3510 || specials[special_index].type == GRAPH) {
3511 w += specials[special_index].width;
3512 if (specials[special_index].height > last_font_height) {
3513 last_font_height = specials[special_index].height;
3514 last_font_height += font_height();
3516 } else if (specials[special_index].type == OFFSET) {
3517 if (specials[special_index].arg > 0) {
3518 w += specials[special_index].arg;
3520 } else if (specials[special_index].type == VOFFSET) {
3521 last_font_height += specials[special_index].arg;
3522 } else if (specials[special_index].type == GOTO) {
3523 if (specials[special_index].arg > cur_x) {
3524 w = (int) specials[special_index].arg;
3526 } else if (specials[special_index].type == TAB) {
3527 int start = specials[special_index].arg;
3528 int step = specials[special_index].width;
3530 if (!step || step < 0) {
3533 w += step - (cur_x - text_start_x - start) % step;
3534 } else if (specials[special_index].type == FONT) {
3535 selected_font = specials[special_index].font_added;
3536 if (font_height() > last_font_height) {
3537 last_font_height = font_height();
3543 } else if (*p == SECRIT_MULTILINE_CHAR) {
3546 lw = get_string_width(s);
3547 *p = SECRIT_MULTILINE_CHAR;
3549 w = lw > w ? lw : w;
3550 text_height += last_font_height;
3554 w += get_string_width(s);
3555 if (w > text_width) {
3558 if (text_width > maximum_width && maximum_width) {
3559 text_width = maximum_width;
3562 text_height += last_font_height;
3563 last_font_height = font_height();
3564 return special_index;
3568 static inline void set_foreground_color(long c)
3571 if (output_methods & TO_X) {
3573 XSetForeground(display, window.gc, c);
3577 if (output_methods & TO_NCURSES) {
3578 attron(COLOR_PAIR(c));
3580 #endif /* NCURSES */
3585 static void draw_string(const char *s)
3587 int i, i2, pos, width_of_s;
3590 char *s_with_newlines;
3596 width_of_s = get_string_width(s);
3597 s_with_newlines = strdup(s);
3598 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
3599 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
3600 s_with_newlines[i] = '\n';
3603 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
3604 printf("%s\n", s_with_newlines);
3605 if (extra_newline) fputc('\n', stdout);
3606 fflush(stdout); /* output immediately, don't buffer */
3608 if ((output_methods & TO_STDERR) && draw_mode == FG) {
3609 fprintf(stderr, "%s\n", s_with_newlines);
3610 fflush(stderr); /* output immediately, don't buffer */
3612 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
3613 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
3615 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
3616 fprintf(append_fpointer, "%s\n", s_with_newlines);
3619 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
3620 printw("%s", s_with_newlines);
3623 free(s_with_newlines);
3624 memset(tmpstring1, 0, text_buffer_size);
3625 memset(tmpstring2, 0, text_buffer_size);
3626 strncpy(tmpstring1, s, text_buffer_size - 1);
3631 if (output_methods & TO_X) {
3632 max = ((text_width - width_of_s) / get_string_width(" "));
3635 /* This code looks for tabs in the text and coverts them to spaces.
3636 * The trick is getting the correct number of spaces, and not going
3637 * over the window's size without forcing the window larger. */
3638 for (i = 0; i < (int) text_buffer_size; i++) {
3639 if (tmpstring1[i] == '\t') {
3641 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
3642 /* guard against overrun */
3643 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
3648 /* guard against overrun */
3649 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
3654 if (output_methods & TO_X) {
3655 if (text_width == maximum_width) {
3656 /* this means the text is probably pushing the limit,
3657 * so we'll chop it */
3658 while (cur_x + get_string_width(tmpstring2) - text_start_x
3659 > maximum_width && strlen(tmpstring2) > 0) {
3660 tmpstring2[strlen(tmpstring2) - 1] = '\0';
3667 if (output_methods & TO_X) {
3673 c.pixel = current_color;
3674 XQueryColor(display, DefaultColormap(display, screen), &c);
3677 c2.color.red = c.red;
3678 c2.color.green = c.green;
3679 c2.color.blue = c.blue;
3680 c2.color.alpha = fonts[selected_font].font_alpha;
3682 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3683 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3685 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3686 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3691 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
3694 cur_x += width_of_s;
3697 memcpy(tmpstring1, s, text_buffer_size);
3700 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
3703 int font_h = font_height();
3708 int last_special_needed = -1;
3709 int orig_special_index = special_index;
3712 cur_x = text_start_x;
3713 cur_y += font_ascent();
3717 if (*p == SECRIT_MULTILINE_CHAR) {
3718 /* special newline marker for multiline objects */
3723 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3728 /* draw string before special, unless we're dealing multiline
3730 if (last_special_applied > -1) {
3731 special_index = last_special_applied;
3739 switch (specials[special_index].type) {
3741 case HORIZONTAL_LINE:
3743 int h = specials[special_index].height;
3744 int mid = font_ascent() / 2;
3746 w = text_start_x + text_width - cur_x;
3748 XSetLineAttributes(display, window.gc, h, LineSolid,
3749 CapButt, JoinMiter);
3750 XDrawLine(display, window.drawable, window.gc, cur_x,
3751 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3757 int h = specials[special_index].height;
3758 int tmp_s = specials[special_index].arg;
3759 int mid = font_ascent() / 2;
3760 char ss[2] = { tmp_s, tmp_s };
3762 w = text_start_x + text_width - cur_x - 1;
3763 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3764 CapButt, JoinMiter);
3765 XSetDashes(display, window.gc, 0, ss, 2);
3766 XDrawLine(display, window.drawable, window.gc, cur_x,
3767 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3773 int h, bar_usage, by;
3774 if (cur_x - text_start_x > maximum_width
3775 && maximum_width > 0) {
3778 h = specials[special_index].height;
3779 bar_usage = specials[special_index].arg;
3780 by = cur_y - (font_ascent() / 2) - 1;
3785 w = specials[special_index].width;
3787 w = text_start_x + text_width - cur_x - 1;
3793 XSetLineAttributes(display, window.gc, 1, LineSolid,
3794 CapButt, JoinMiter);
3796 XDrawRectangle(display, window.drawable, window.gc, cur_x,
3798 XFillRectangle(display, window.drawable, window.gc, cur_x,
3799 by, w * bar_usage / 255, h);
3807 case GAUGE: /* new GAUGE */
3810 unsigned long last_colour = current_color;
3812 float angle, px, py;
3816 if (cur_x - text_start_x > maximum_width
3817 && maximum_width > 0) {
3821 h = specials[special_index].height;
3822 by = cur_y - (font_ascent() / 2) - 1;
3827 w = specials[special_index].width;
3829 w = text_start_x + text_width - cur_x - 1;
3835 XSetLineAttributes(display, window.gc, 1, LineSolid,
3836 CapButt, JoinMiter);
3838 XDrawArc(display, window.drawable, window.gc,
3839 cur_x, by, w, h * 2, 0, 180*64);
3842 usage = specials[special_index].arg;
3843 angle = (M_PI)*(float)(usage)/255.;
3844 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3845 py = (float)(by+(h))-(float)(h)*sin(angle);
3847 XDrawLine(display, window.drawable, window.gc,
3848 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3856 set_foreground_color(last_colour);
3864 int h, by, i = 0, j = 0;
3866 unsigned long last_colour = current_color;
3867 unsigned long *tmpcolour = 0;
3868 if (cur_x - text_start_x > maximum_width
3869 && maximum_width > 0) {
3872 h = specials[special_index].height;
3873 by = cur_y - (font_ascent() / 2) - 1;
3878 w = specials[special_index].width;
3880 w = text_start_x + text_width - cur_x - 1;
3885 if (draw_graph_borders) {
3886 XSetLineAttributes(display, window.gc, 1, LineSolid,
3887 CapButt, JoinMiter);
3888 XDrawRectangle(display, window.drawable, window.gc,
3891 XSetLineAttributes(display, window.gc, 1, LineSolid,
3892 CapButt, JoinMiter);
3894 if (specials[special_index].last_colour != 0
3895 || specials[special_index].first_colour != 0) {
3896 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3899 for (i = w - 2; i > -1; i--) {
3900 if (specials[special_index].last_colour != 0
3901 || specials[special_index].first_colour != 0) {
3902 if (specials[special_index].tempgrad) {
3905 (int)((float)(w - 2) - specials[special_index].graph[j] *
3906 (w - 2) / (float)specials[special_index].graph_scale)
3910 (int)((float)(w - 2) - specials[special_index].graph[j] *
3911 (w - 2) / (float)specials[special_index].graph_scale)
3914 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3916 (int)((float)(w - 2) - specials[special_index].graph[j] *
3917 (w - 2) / (float)specials[special_index].graph_scale)
3921 #endif /* DEBUG_lol */
3922 XSetForeground(display, window.gc, tmpcolour[
3923 (int)((float)(w - 2) - specials[special_index].graph[j] *
3924 (w - 2) / (float)specials[special_index].graph_scale)
3927 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3930 /* this is mugfugly, but it works */
3931 XDrawLine(display, window.drawable, window.gc,
3932 cur_x + i + 1, by + h, cur_x + i + 1,
3933 round_to_int((double)by + h - specials[special_index].graph[j] *
3934 (h - 1) / specials[special_index].graph_scale));
3935 if ((w - i) / ((float) (w - 2) /
3936 (specials[special_index].graph_width)) > j
3937 && j < MAX_GRAPH_DEPTH - 3) {
3941 if (tmpcolour) free(tmpcolour);
3946 /* if (draw_mode == BG) {
3947 set_foreground_color(default_bg_color);
3948 } else if (draw_mode == OUTLINE) {
3949 set_foreground_color(default_out_color);
3951 set_foreground_color(default_fg_color);
3953 if (show_graph_range) {
3956 unsigned short int seconds = update_interval * w;
3962 unsigned short int timeunits;
3964 timeunits = seconds / 86400; seconds %= 86400;
3965 if (timeunits > 0) {
3966 asprintf(&tmp_day_str, "%dd", timeunits);
3968 tmp_day_str = strdup("");
3970 timeunits = seconds / 3600; seconds %= 3600;
3971 if (timeunits > 0) {
3972 asprintf(&tmp_hour_str, "%dh", timeunits);
3974 tmp_hour_str = strdup("");
3976 timeunits = seconds / 60; seconds %= 60;
3977 if (timeunits > 0) {
3978 asprintf(&tmp_min_str, "%dm", timeunits);
3980 tmp_min_str = strdup("");
3983 asprintf(&tmp_sec_str, "%ds", seconds);
3985 tmp_sec_str = strdup("");
3987 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3988 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3990 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3992 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3993 cur_y += font_h / 2;
3994 draw_string(tmp_str);
4000 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
4004 cur_x += font_ascent() / 2;
4005 cur_y += font_h / 2;
4007 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
4009 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
4010 draw_string(tmp_str);
4016 set_foreground_color(last_colour);
4022 int old = font_ascent();
4024 cur_y -= font_ascent();
4025 selected_font = specials[special_index].font_added;
4027 if (cur_y + font_ascent() < cur_y + old) {
4030 cur_y += font_ascent();
4032 font_h = font_height();
4037 if (draw_mode == FG) {
4038 set_foreground_color(specials[special_index].arg);
4044 if (draw_mode == BG) {
4045 set_foreground_color(specials[special_index].arg);
4050 if (draw_mode == OUTLINE) {
4051 set_foreground_color(specials[special_index].arg);
4056 w += specials[special_index].arg;
4057 last_special_needed = special_index;
4061 cur_y += specials[special_index].arg;
4065 if (specials[special_index].arg >= 0) {
4066 cur_x = (int) specials[special_index].arg;
4068 last_special_needed = special_index;
4073 int start = specials[special_index].arg;
4074 int step = specials[special_index].width;
4076 if (!step || step < 0) {
4079 w = step - (cur_x - text_start_x - start) % step;
4080 last_special_needed = special_index;
4086 /* TODO: add back in "+ window.border_inner_margin" to the end of
4088 int pos_x = text_start_x + text_width -
4089 get_string_width_special(s, special_index);
4091 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4092 "get_string_width(p) %i gap_x %i "
4093 "specials[special_index].arg %i window.border_inner_margin %i "
4094 "window.border_width %i\n", pos_x, text_start_x, text_width,
4095 cur_x, get_string_width_special(s), gap_x,
4096 specials[special_index].arg, window.border_inner_margin,
4097 window.border_width); */
4098 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
4099 cur_x = pos_x - specials[special_index].arg;
4101 last_special_needed = special_index;
4107 int pos_x = (text_width) / 2 - get_string_width_special(s,
4108 special_index) / 2 - (cur_x -
4110 /* int pos_x = text_start_x + text_width / 2 -
4111 get_string_width_special(s) / 2; */
4113 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4114 "get_string_width(p) %i gap_x %i "
4115 "specials[special_index].arg %i\n", pos_x, text_start_x,
4116 text_width, cur_x, get_string_width(s), gap_x,
4117 specials[special_index].arg); */
4118 if (pos_x > specials[special_index].arg) {
4119 w = pos_x - specials[special_index].arg;
4121 last_special_needed = special_index;
4131 if (special_index != last_special_applied) {
4134 special_index = orig_special_index;
4135 last_special_applied = -1;
4146 if (output_methods & TO_NCURSES) {
4149 #endif /* NCURSES */
4151 cur_y += font_descent();
4153 if (recurse && *recurse) {
4154 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
4155 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
4157 return special_index;
4160 static int draw_line(char *s, int special_index)
4163 if (output_methods & TO_X) {
4164 return draw_each_line_inner(s, special_index, -1);
4168 if (output_methods & TO_NCURSES) {
4169 return draw_each_line_inner(s, special_index, -1);
4171 #endif /* NCURSES */
4173 UNUSED(special_index);
4177 static void draw_text(void)
4181 llua_draw_pre_hook();
4182 #endif /* HAVE_LUA */
4183 if (output_methods & TO_X) {
4184 cur_y = text_start_y;
4187 if (draw_borders && window.border_width > 0) {
4188 if (stippled_borders) {
4189 char ss[2] = { stippled_borders, stippled_borders };
4190 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
4191 CapButt, JoinMiter);
4192 XSetDashes(display, window.gc, 0, ss, 2);
4194 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
4195 CapButt, JoinMiter);
4198 XDrawRectangle(display, window.drawable, window.gc,
4199 text_start_x - window.border_inner_margin - window.border_width,
4200 text_start_y - window.border_inner_margin - window.border_width,
4201 text_width + window.border_inner_margin * 2 + window.border_width * 2,
4202 text_height + window.border_inner_margin * 2 + window.border_width * 2);
4210 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
4211 attron(COLOR_PAIR(COLOR_WHITE));
4212 #endif /* NCURSES */
4213 for_each_line(text_buffer, draw_line);
4214 #if defined(HAVE_LUA) && defined(X11)
4215 llua_draw_post_hook();
4216 #endif /* HAVE_LUA */
4219 static void draw_stuff(void)
4222 cimlib_render(text_start_x, text_start_y, window.width, window.height);
4224 if (overwrite_file) {
4225 overwrite_fpointer = fopen(overwrite_file, "w");
4226 if(!overwrite_fpointer)
4227 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
4230 append_fpointer = fopen(append_file, "a");
4231 if(!append_fpointer)
4232 NORM_ERR("Can't append '%s' anymore", append_file);
4235 if (output_methods & TO_X) {
4237 if (draw_shades && !draw_outline) {
4240 set_foreground_color(default_bg_color);
4251 for (i = -1; i < 2; i++) {
4252 for (j = -1; j < 2; j++) {
4253 if (i == 0 && j == 0) {
4258 set_foreground_color(default_out_color);
4259 draw_mode = OUTLINE;
4267 set_foreground_color(default_fg_color);
4273 xdbe_swap_buffers();
4274 if (output_methods & TO_X) {
4279 if(overwrite_fpointer) {
4280 fclose(overwrite_fpointer);
4281 overwrite_fpointer = 0;
4283 if(append_fpointer) {
4284 fclose(append_fpointer);
4285 append_fpointer = 0;
4290 static void clear_text(int exposures)
4294 /* The swap action is XdbeBackground, which clears */
4298 if (display && window.window) { // make sure these are !null
4299 /* there is some extra space for borders and outlines */
4300 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4301 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4302 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4303 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
4308 static int need_to_update;
4310 /* update_text() generates new text and clears old text area */
4311 static void update_text(void)
4318 if (output_methods & TO_X)
4323 llua_update_info(&info, update_interval);
4324 #endif /* HAVE_LUA */
4327 #ifdef HAVE_SYS_INOTIFY_H
4331 static void main_loop(void)
4334 #ifdef SIGNAL_BLOCKING
4335 sigset_t newmask, oldmask;
4338 #ifdef HAVE_SYS_INOTIFY_H
4339 int inotify_config_wd = -1;
4340 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
4341 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
4342 char inotify_buff[INOTIFY_BUF_LEN];
4343 #endif /* HAVE_SYS_INOTIFY_H */
4346 #ifdef SIGNAL_BLOCKING
4347 sigemptyset(&newmask);
4348 sigaddset(&newmask, SIGINT);
4349 sigaddset(&newmask, SIGTERM);
4350 sigaddset(&newmask, SIGUSR1);
4353 last_update_time = 0.0;
4354 next_update_time = get_time();
4356 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
4357 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
4358 char buf[max_user_text];
4360 get_battery_short_status(buf, max_user_text, "BAT0");
4362 update_interval = update_interval_bat;
4364 update_interval = update_interval_old;
4369 #ifdef SIGNAL_BLOCKING
4370 /* block signals. we will inspect for pending signals later */
4371 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
4372 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4377 if (output_methods & TO_X) {
4380 /* wait for X event or timeout */
4382 if (!XPending(display)) {
4386 t = next_update_time - get_time();
4390 } else if (t > update_interval) {
4391 t = update_interval;
4394 tv.tv_sec = (long) t;
4395 tv.tv_usec = (long) (t * 1000000) % 1000000;
4397 FD_SET(ConnectionNumber(display), &fdsr);
4399 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
4401 if (errno != EINTR) {
4402 NORM_ERR("can't select(): %s", strerror(errno));
4412 if (need_to_update) {
4414 int wx = window.x, wy = window.y;
4424 /* resize window if it isn't right size */
4426 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
4427 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
4428 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4429 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4430 draw_stuff(); /* redraw everything in our newly sized window */
4431 XResizeWindow(display, window.window, window.width,
4432 window.height); /* resize window */
4433 set_transparent_background(window.window);
4436 xdbe_swap_buffers();
4441 /* update lua window globals */
4442 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
4443 #endif /* HAVE_LUA */
4446 /* move window if it isn't in right position */
4447 if (!fixed_pos && (window.x != wx || window.y != wy)) {
4448 XMoveWindow(display, window.window, window.x, window.y);
4453 if (changed && window.type == TYPE_PANEL) {
4456 fprintf(stderr, PACKAGE_NAME": defining struts\n");
4459 switch (text_alignment) {
4486 set_struts(sidenum);
4497 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4498 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4499 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4500 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4501 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4506 /* handle X events */
4507 while (XPending(display)) {
4510 XNextEvent(display, &ev);
4517 r.width = ev.xexpose.width;
4518 r.height = ev.xexpose.height;
4519 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4523 case PropertyNotify:
4525 if ( ev.xproperty.state == PropertyNewValue ) {
4526 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
4532 case ReparentNotify:
4533 /* set background to ParentRelative for all parents */
4535 set_transparent_background(window.window);
4539 case ConfigureNotify:
4541 /* if window size isn't what expected, set fixed size */
4542 if (ev.xconfigure.width != window.width
4543 || ev.xconfigure.height != window.height) {
4544 if (window.width != 0 && window.height != 0) {
4548 /* clear old stuff before screwing up
4553 XWindowAttributes attrs;
4554 if (XGetWindowAttributes(display,
4555 window.window, &attrs)) {
4556 window.width = attrs.width;
4557 window.height = attrs.height;
4561 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4562 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4563 if (text_width > maximum_width
4564 && maximum_width > 0) {
4565 text_width = maximum_width;
4569 /* if position isn't what expected, set fixed pos
4570 * total_updates avoids setting fixed_pos when window
4571 * is set to weird locations when started */
4572 /* // this is broken
4573 if (total_updates >= 2 && !fixed_pos
4574 && (window.x != ev.xconfigure.x
4575 || window.y != ev.xconfigure.y)
4576 && (ev.xconfigure.x != 0
4577 || ev.xconfigure.y != 0)) {
4585 /* if an ordinary window with decorations */
4586 if ((window.type == TYPE_NORMAL &&
4587 (!TEST_HINT(window.hints,
4588 HINT_UNDECORATED))) ||
4589 window.type == TYPE_DESKTOP) {
4590 /* allow conky to hold input focus. */
4593 /* forward the click to the desktop window */
4594 XUngrabPointer(display, ev.xbutton.time);
4595 ev.xbutton.window = window.desktop;
4596 ev.xbutton.x = ev.xbutton.x_root;
4597 ev.xbutton.y = ev.xbutton.y_root;
4598 XSendEvent(display, ev.xbutton.window, False,
4599 ButtonPressMask, &ev);
4600 XSetInputFocus(display, ev.xbutton.window,
4601 RevertToParent, ev.xbutton.time);
4608 /* if an ordinary window with decorations */
4609 if ((window.type == TYPE_NORMAL)
4610 && (!TEST_HINT(window.hints,
4611 HINT_UNDECORATED))) {
4612 /* allow conky to hold input focus. */
4615 /* forward the release to the desktop window */
4616 ev.xbutton.window = window.desktop;
4617 ev.xbutton.x = ev.xbutton.x_root;
4618 ev.xbutton.y = ev.xbutton.y_root;
4619 XSendEvent(display, ev.xbutton.window, False,
4620 ButtonReleaseMask, &ev);
4629 if (ev.type == x11_stuff.event_base + XDamageNotify) {
4630 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
4632 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
4633 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
4635 #endif /* HAVE_XDAMAGE */
4641 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
4642 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
4643 #endif /* HAVE_XDAMAGE */
4645 /* XDBE doesn't seem to provide a way to clear the back buffer
4646 * without interfering with the front buffer, other than passing
4647 * XdbeBackground to XdbeSwapBuffers. That means that if we're
4648 * using XDBE, we need to redraw the text even if it wasn't part of
4649 * the exposed area. OTOH, if we're not going to call draw_stuff at
4650 * all, then no swap happens and we can safely do nothing. */
4652 if (!XEmptyRegion(x11_stuff.region)) {
4657 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4658 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4659 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4660 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4661 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4664 XSetRegion(display, window.gc, x11_stuff.region);
4667 XftDrawSetClip(window.xftdraw, x11_stuff.region);
4671 XDestroyRegion(x11_stuff.region);
4672 x11_stuff.region = XCreateRegion();
4676 t = (next_update_time - get_time()) * 1000000;
4677 if(t > 0) usleep((useconds_t)t);
4681 if(output_methods & TO_NCURSES) {
4690 #ifdef SIGNAL_BLOCKING
4691 /* unblock signals of interest and let handler fly */
4692 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4693 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4697 switch (g_signal_pending) {
4700 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4705 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4708 if (output_methods & TO_X) {
4709 XDestroyRegion(x11_stuff.region);
4710 x11_stuff.region = NULL;
4712 XDamageDestroy(display, x11_stuff.damage);
4713 XFixesDestroyRegion(display, x11_stuff.region2);
4714 XFixesDestroyRegion(display, x11_stuff.part);
4715 #endif /* HAVE_XDAMAGE */
4721 if(overwrite_file) {
4722 free(overwrite_file);
4731 /* Reaching here means someone set a signal
4732 * (SIGXXXX, signal_handler), but didn't write any code
4734 * If you don't want to handle a signal, don't set a handler on
4735 * it in the first place. */
4736 if (g_signal_pending) {
4737 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4741 #ifdef HAVE_SYS_INOTIFY_H
4742 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4743 inotify_config_wd = inotify_add_watch(inotify_fd,
4747 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4748 int len = 0, idx = 0;
4750 struct timeval time_to_wait;
4752 FD_ZERO(&descriptors);
4753 FD_SET(inotify_fd, &descriptors);
4755 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4757 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4758 if (FD_ISSET(inotify_fd, &descriptors)) {
4759 /* process inotify events */
4760 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4761 while (len > 0 && idx < len) {
4762 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4763 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4764 /* current_config should be reloaded */
4765 NORM_ERR("'%s' modified, reloading...", current_config);
4767 if (ev->mask & IN_IGNORED) {
4768 /* for some reason we get IN_IGNORED here
4769 * sometimes, so we need to re-add the watch */
4770 inotify_config_wd = inotify_add_watch(inotify_fd,
4777 llua_inotify_query(ev->wd, ev->mask);
4779 #endif /* HAVE_LUA */
4780 idx += INOTIFY_EVENT_SIZE + ev->len;
4784 #endif /* HAVE_SYS_INOTIFY_H */
4787 llua_update_info(&info, update_interval);
4788 #endif /* HAVE_LUA */
4789 g_signal_pending = 0;
4791 clean_up(NULL, NULL);
4793 #ifdef HAVE_SYS_INOTIFY_H
4794 if (inotify_fd != -1) {
4795 inotify_rm_watch(inotify_fd, inotify_config_wd);
4797 inotify_fd = inotify_config_wd = 0;
4799 #endif /* HAVE_SYS_INOTIFY_H */
4803 static void load_config_file_x11(const char *);
4805 void initialisation(int argc, char** argv);
4807 /* reload the config file */
4808 static void reload_config(void)
4810 char *current_config_copy = strdup(current_config);
4811 clean_up(NULL, NULL);
4812 current_config = current_config_copy;
4813 initialisation(argc_copy, argv_copy);
4816 void clean_up(void *memtofree1, void* memtofree2)
4821 if(output_methods & TO_NCURSES) {
4825 conftree_empty(currentconffile);
4826 currentconffile = NULL;
4833 timed_thread_destroy_registered_threads();
4835 if (info.cpu_usage) {
4836 free(info.cpu_usage);
4837 info.cpu_usage = NULL;
4840 if (x_initialised == YES) {
4841 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4842 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4843 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4844 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4847 if(x11_stuff.region) {
4848 XDestroyRegion(x11_stuff.region);
4849 x11_stuff.region = NULL;
4851 XCloseDisplay(display);
4853 if(info.x11.desktop.all_names) {
4854 free(info.x11.desktop.all_names);
4855 info.x11.desktop.all_names = NULL;
4857 if (info.x11.desktop.name) {
4858 free(info.x11.desktop.name);
4859 info.x11.desktop.name = NULL;
4863 free(fonts); //in set_default_configurations a font is set but not loaded
4869 free_update_callbacks();
4873 free_text_objects(&global_root_object, 0);
4892 free(current_config);
4895 #ifdef TCP_PORT_MONITOR
4896 tcp_portmon_clear();
4905 weather_free_info();
4908 llua_shutdown_hook();
4910 #endif /* HAVE_LUA */
4919 for (i = 0; i < special_count; i++) {
4920 if (specials[i].type == GRAPH) {
4921 free(specials[i].graph);
4929 clear_diskio_stats();
4930 if(global_cpu != NULL) {
4936 static int string_to_bool(const char *s)
4939 // Assumes an option without a true/false means true
4941 } else if (strcasecmp(s, "yes") == EQUAL) {
4943 } else if (strcasecmp(s, "true") == EQUAL) {
4945 } else if (strcasecmp(s, "1") == EQUAL) {
4952 static enum alignment string_to_alignment(const char *s)
4954 if (strcasecmp(s, "top_left") == EQUAL) {
4956 } else if (strcasecmp(s, "top_right") == EQUAL) {
4958 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4960 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4962 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4963 return BOTTOM_RIGHT;
4964 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4965 return BOTTOM_MIDDLE;
4966 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4968 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4969 return MIDDLE_RIGHT;
4970 } else if (strcasecmp(s, "tl") == EQUAL) {
4972 } else if (strcasecmp(s, "tr") == EQUAL) {
4974 } else if (strcasecmp(s, "tm") == EQUAL) {
4976 } else if (strcasecmp(s, "bl") == EQUAL) {
4978 } else if (strcasecmp(s, "br") == EQUAL) {
4979 return BOTTOM_RIGHT;
4980 } else if (strcasecmp(s, "bm") == EQUAL) {
4981 return BOTTOM_MIDDLE;
4982 } else if (strcasecmp(s, "ml") == EQUAL) {
4984 } else if (strcasecmp(s, "mr") == EQUAL) {
4985 return MIDDLE_RIGHT;
4986 } else if (strcasecmp(s, "none") == EQUAL) {
4994 static void set_default_configurations_for_x(void)
4996 default_fg_color = WhitePixel(display, screen);
4997 default_bg_color = BlackPixel(display, screen);
4998 default_out_color = BlackPixel(display, screen);
4999 color0 = default_fg_color;
5000 color1 = default_fg_color;
5001 color2 = default_fg_color;
5002 color3 = default_fg_color;
5003 color4 = default_fg_color;
5004 color5 = default_fg_color;
5005 color6 = default_fg_color;
5006 color7 = default_fg_color;
5007 color8 = default_fg_color;
5008 color9 = default_fg_color;
5009 current_text_color = default_fg_color;
5013 static void set_default_configurations(void)
5020 fork_to_background = 0;
5021 total_run_times = 0;
5022 info.cpu_avg_samples = 2;
5023 info.net_avg_samples = 2;
5024 info.diskio_avg_samples = 2;
5029 format_human_readable = 1;
5039 mpd_env_host = getenv("MPD_HOST");
5040 mpd_env_port = getenv("MPD_PORT");
5042 if (!mpd_env_host || !strlen(mpd_env_host)) {
5043 mpd_set_host("localhost");
5045 /* MPD_HOST environment variable is set */
5046 char *mpd_hostpart = strchr(mpd_env_host, '@');
5047 if (!mpd_hostpart) {
5048 mpd_set_host(mpd_env_host);
5050 /* MPD_HOST contains a password */
5051 char mpd_password[mpd_hostpart - mpd_env_host + 1];
5052 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
5054 if (!strlen(mpd_hostpart + 1)) {
5055 mpd_set_host("localhost");
5057 mpd_set_host(mpd_hostpart + 1);
5060 mpd_set_password(mpd_password, 1);
5065 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
5066 /* failed to set port from environment variable */
5067 mpd_set_port("6600");
5071 info.xmms2.artist = NULL;
5072 info.xmms2.album = NULL;
5073 info.xmms2.title = NULL;
5074 info.xmms2.genre = NULL;
5075 info.xmms2.comment = NULL;
5076 info.xmms2.url = NULL;
5077 info.xmms2.status = NULL;
5078 info.xmms2.playlist = NULL;
5080 use_spacer = NO_SPACER;
5082 output_methods = TO_X;
5084 output_methods = TO_STDOUT;
5087 show_graph_scale = 0;
5088 show_graph_range = 0;
5091 draw_graph_borders = 1;
5093 set_first_font("6x10");
5101 window.type = TYPE_NORMAL;
5103 strcpy(window.class_name, PACKAGE_NAME);
5104 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
5106 stippled_borders = 0;
5107 window.border_inner_margin = 3;
5108 window.border_outer_margin = 1;
5109 window.border_width = 1;
5110 text_alignment = BOTTOM_LEFT;
5111 info.x11.monitor.number = 1;
5112 info.x11.monitor.current = 0;
5113 info.x11.desktop.current = 1;
5114 info.x11.desktop.number = 1;
5115 info.x11.desktop.nitems = 0;
5116 info.x11.desktop.all_names = NULL;
5117 info.x11.desktop.name = NULL;
5122 free(current_mail_spool);
5126 variable_substitute(MAIL_FILE, buf, 256);
5127 if (buf[0] != '\0') {
5128 current_mail_spool = strndup(buf, text_buffer_size);
5133 set_update_interval(3);
5134 update_interval_bat = NOBATTERY;
5135 info.music_player_interval = 1.0;
5136 stuff_in_uppercase = 0;
5137 info.users.number = 1;
5139 #ifdef TCP_PORT_MONITOR
5140 /* set default connection limit */
5141 tcp_portmon_set_max_connections(0);
5145 /* returns 1 if you can overwrite or create the file at 'path' */
5146 static _Bool overwrite_works(const char *path)
5150 if (!(filepointer = fopen(path, "w")))
5152 fclose(filepointer);
5156 /* returns 1 if you can append or create the file at 'path' */
5157 static _Bool append_works(const char *path)
5161 if (!(filepointer = fopen(path, "a")))
5163 fclose(filepointer);
5169 /* WARNING, this type not in Xlib spec */
5170 int x11_error_handler(Display *d, XErrorEvent *err)
5171 __attribute__((noreturn));
5172 int x11_error_handler(Display *d, XErrorEvent *err)
5174 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",
5176 (long unsigned)err->display,
5177 (long)err->resourceid,
5187 int x11_ioerror_handler(Display *d)
5188 __attribute__((noreturn));
5189 int x11_ioerror_handler(Display *d)
5191 NORM_ERR("X Error: Display %lx\n",
5198 static void X11_initialisation(void)
5200 if (x_initialised == YES) return;
5201 output_methods |= TO_X;
5203 set_default_configurations_for_x();
5204 x_initialised = YES;
5207 /* WARNING, this type not in Xlib spec */
5208 XSetErrorHandler(&x11_error_handler);
5209 XSetIOErrorHandler(&x11_ioerror_handler);
5213 static char **xargv = 0;
5214 static int xargc = 0;
5216 static void X11_create_window(void)
5218 if (output_methods & TO_X) {
5220 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5221 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
5223 #else /* OWN_WINDOW */
5224 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5225 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
5227 #endif /* OWN_WINDOW */
5231 update_text_area(); /* to position text/window on screen */
5234 if (own_window && !fixed_pos) {
5235 XMoveWindow(display, window.window, window.x, window.y);
5238 set_transparent_background(window.window);
5246 x11_stuff.region = XCreateRegion();
5248 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
5249 NORM_ERR("Xdamage extension unavailable");
5251 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5252 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5253 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
5254 #endif /* HAVE_XDAMAGE */
5257 update_text_area(); /* to get initial size of the window */
5260 /* setup lua window globals */
5261 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
5262 #endif /* HAVE_LUA */
5266 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
5267 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
5268 #define CONF2(a) if (strcasecmp(name, a) == 0)
5269 #define CONF(a) else CONF2(a)
5270 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
5271 || strcasecmp(name, b) == 0)
5272 #define CONF_CONTINUE 1
5273 #define CONF_BREAK 2
5274 #define CONF_BUFF_SIZE 512
5276 static FILE *open_config_file(const char *f)
5278 #ifdef CONFIG_OUTPUT
5279 if (!strcmp(f, "==builtin==")) {
5280 return conf_cookie_open();
5282 #endif /* CONFIG_OUTPUT */
5283 return fopen(f, "r");
5286 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
5290 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5293 remove_comments(buf);
5298 while (*p && isspace((int) *p)) {
5302 return CONF_CONTINUE; /* empty line */
5309 while (*p2 && !isspace((int) *p2)) {
5313 *p2 = '\0'; /* break at name's end */
5320 while (*p && isspace((int) *p)) {
5326 p2 = *value + strlen(*value);
5327 while (isspace((int) *(p2 - 1))) {
5336 char load_config_file(const char *f)
5341 fp = open_config_file(f);
5345 DBGP("reading contents from config file '%s'", f);
5348 char buff[CONF_BUFF_SIZE], *name, *value;
5349 int ret = do_config_step(&line, fp, buff, &name, &value);
5350 if (ret == CONF_BREAK) {
5352 } else if (ret == CONF_CONTINUE) {
5358 /* don't listen if X is already initialised or
5359 * if we already know we don't want it */
5360 if(x_initialised != YES) {
5361 if (string_to_bool(value)) {
5362 output_methods &= TO_X;
5364 output_methods &= ~TO_X;
5365 x_initialised = NEVER;
5370 if (!value || x_initialised == YES) {
5375 disp = strdup(value);
5380 if (window.type == TYPE_DOCK)
5383 #endif /*OWN_WINDOW */
5385 int a = string_to_alignment(value);
5396 CONF("background") {
5397 fork_to_background = string_to_bool(value);
5400 CONF2("background") {
5401 fork_to_background = string_to_bool(value);
5405 CONF("show_graph_scale") {
5406 show_graph_scale = string_to_bool(value);
5408 CONF("show_graph_range") {
5409 show_graph_range = string_to_bool(value);
5411 CONF("border_inner_margin") {
5413 window.border_inner_margin = strtol(value, 0, 0);
5414 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
5419 CONF("border_outer_margin") {
5421 window.border_outer_margin = strtol(value, 0, 0);
5422 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
5427 CONF("border_width") {
5429 window.border_width = strtol(value, 0, 0);
5430 if (window.border_width < 0) window.border_width = 0;
5436 #define TEMPLATE_CONF(n) \
5437 CONF("template"#n) { \
5438 if (set_template(n, value)) \
5453 info.mail = parse_mail_args(IMAP_TYPE, value);
5460 info.mail = parse_mail_args(POP3_TYPE, value);
5465 CONF("default_bar_size") {
5468 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
5475 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
5479 CONF("default_graph_size") {
5482 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
5489 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
5492 CONF("default_gauge_size") {
5495 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
5502 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
5509 mpd_set_host(value);
5515 if (value && mpd_set_port(value)) {
5519 CONF("mpd_password") {
5521 mpd_set_password(value, 0);
5527 CONF("music_player_interval") {
5529 info.music_player_interval = strtod(value, 0);
5535 CONF("sensor_device") {
5537 sensor_device = strtol(value, 0, 0);
5543 CONF("cpu_avg_samples") {
5545 cpu_avg_samples = strtol(value, 0, 0);
5546 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
5549 info.cpu_avg_samples = cpu_avg_samples;
5555 CONF("net_avg_samples") {
5557 net_avg_samples = strtol(value, 0, 0);
5558 if (net_avg_samples < 1 || net_avg_samples > 14) {
5561 info.net_avg_samples = net_avg_samples;
5567 CONF("diskio_avg_samples") {
5569 diskio_avg_samples = strtol(value, 0, 0);
5570 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
5573 info.diskio_avg_samples = diskio_avg_samples;
5581 CONF("double_buffer") {
5582 use_xdbe = string_to_bool(value);
5586 CONF("override_utf8_locale") {
5587 utf8_mode = string_to_bool(value);
5589 CONF("draw_borders") {
5590 draw_borders = string_to_bool(value);
5592 CONF("draw_graph_borders") {
5593 draw_graph_borders = string_to_bool(value);
5595 CONF("draw_shades") {
5596 draw_shades = string_to_bool(value);
5598 CONF("draw_outline") {
5599 draw_outline = string_to_bool(value);
5602 CONF("out_to_console") {
5603 if(string_to_bool(value)) {
5604 output_methods |= TO_STDOUT;
5606 output_methods &= ~TO_STDOUT;
5609 CONF("extra_newline") {
5610 extra_newline = string_to_bool(value);
5612 CONF("out_to_stderr") {
5613 if(string_to_bool(value))
5614 output_methods |= TO_STDERR;
5617 CONF("out_to_ncurses") {
5618 if(string_to_bool(value)) {
5621 output_methods |= TO_NCURSES;
5625 CONF("overwrite_file") {
5626 if(overwrite_file) {
5627 free(overwrite_file);
5630 if(overwrite_works(value)) {
5631 overwrite_file = strdup(value);
5632 output_methods |= OVERWRITE_FILE;
5634 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
5636 CONF("append_file") {
5641 if(append_works(value)) {
5642 append_file = strdup(value);
5643 output_methods |= APPEND_FILE;
5645 NORM_ERR("append_file won't be able to create/append '%s'", value);
5647 CONF("use_spacer") {
5649 if (strcasecmp(value, "left") == EQUAL) {
5650 use_spacer = LEFT_SPACER;
5651 } else if (strcasecmp(value, "right") == EQUAL) {
5652 use_spacer = RIGHT_SPACER;
5653 } else if (strcasecmp(value, "none") == EQUAL) {
5654 use_spacer = NO_SPACER;
5656 use_spacer = string_to_bool(value);
5657 NORM_ERR("use_spacer should have an argument of left, right, or"
5658 " none. '%s' seems to be some form of '%s', so"
5659 " defaulting to %s.", value,
5660 use_spacer ? "true" : "false",
5661 use_spacer ? "right" : "none");
5663 use_spacer = RIGHT_SPACER;
5665 use_spacer = NO_SPACER;
5669 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5670 use_spacer = RIGHT_SPACER;
5676 use_xft = string_to_bool(value);
5680 set_first_font(value);
5684 if (value && font_count >= 0) {
5685 fonts[0].font_alpha = atof(value) * 65535.0;
5692 if (string_to_bool(value)) {
5693 NORM_ERR("Xft not enabled at compile time");
5697 /* xftfont silently ignored when no Xft */
5700 /* xftalpha is silently ignored when no Xft */
5705 set_first_font(value);
5713 gap_x = atoi(value);
5720 gap_y = atoi(value);
5726 CONF("mail_spool") {
5730 variable_substitute(value, buffer, 256);
5732 if (buffer[0] != '\0') {
5733 if (current_mail_spool) {
5734 free(current_mail_spool);
5736 current_mail_spool = strndup(buffer, text_buffer_size);
5743 CONF("minimum_size") {
5745 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5747 if (sscanf(value, "%d", &minimum_width) != 1) {
5755 CONF("maximum_width") {
5757 if (sscanf(value, "%d", &maximum_width) != 1) {
5765 CONF("no_buffers") {
5766 no_buffers = string_to_bool(value);
5768 CONF("top_name_width") {
5770 if (sscanf(value, "%u", &top_name_width) != 1) {
5776 if (top_name_width >= max_user_text) {
5777 top_name_width = max_user_text - 1;
5780 CONF("top_cpu_separate") {
5781 cpu_separate = string_to_bool(value);
5783 CONF("short_units") {
5784 short_units = string_to_bool(value);
5786 CONF("format_human_readable") {
5787 format_human_readable = string_to_bool(value);
5790 CONF("hddtemp_host") {
5791 set_hddtemp_host(value);
5793 CONF("hddtemp_port") {
5794 set_hddtemp_port(value);
5796 #endif /* HDDTEMP */
5797 CONF("pad_percents") {
5798 pad_percents = atoi(value);
5802 CONF("own_window") {
5804 own_window = string_to_bool(value);
5807 CONF("own_window_class") {
5809 memset(window.class_name, 0, sizeof(window.class_name));
5810 strncpy(window.class_name, value,
5811 sizeof(window.class_name) - 1);
5814 CONF("own_window_title") {
5816 memset(window.title, 0, sizeof(window.title));
5817 strncpy(window.title, value, sizeof(window.title) - 1);
5820 CONF("own_window_transparent") {
5822 set_transparent = string_to_bool(value);
5825 CONF("own_window_hints") {
5827 char *p_hint, *p_save;
5828 char delim[] = ", ";
5830 /* tokenize the value into individual hints */
5831 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5833 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5834 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5835 SET_HINT(window.hints, HINT_UNDECORATED);
5836 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5837 SET_HINT(window.hints, HINT_BELOW);
5838 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5839 SET_HINT(window.hints, HINT_ABOVE);
5840 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5841 SET_HINT(window.hints, HINT_STICKY);
5842 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5843 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5844 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5845 SET_HINT(window.hints, HINT_SKIP_PAGER);
5850 p_hint = strtok_r(NULL, delim, &p_save);
5851 } while (p_hint != NULL);
5857 CONF("own_window_type") {
5859 if (strncmp(value, "normal", 6) == EQUAL) {
5860 window.type = TYPE_NORMAL;
5861 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5862 window.type = TYPE_DESKTOP;
5863 } else if (strncmp(value, "dock", 4) == EQUAL) {
5864 window.type = TYPE_DOCK;
5865 text_alignment = TOP_LEFT;
5866 } else if (strncmp(value, "panel", 5) == EQUAL) {
5867 window.type = TYPE_PANEL;
5868 } else if (strncmp(value, "override", 8) == EQUAL) {
5869 window.type = TYPE_OVERRIDE;
5878 CONF("stippled_borders") {
5880 stippled_borders = strtol(value, 0, 0);
5882 stippled_borders = 4;
5886 CONF("imlib_cache_size") {
5888 cimlib_set_cache_size(atoi(value));
5891 CONF("imlib_cache_flush_interval") {
5893 cimlib_set_cache_flush_interval(atoi(value));
5898 CONF("update_interval_on_battery") {
5900 update_interval_bat = strtod(value, 0);
5905 CONF("update_interval") {
5907 set_update_interval(strtod(value, 0));
5911 if (info.music_player_interval == 0) {
5912 // default to update_interval
5913 info.music_player_interval = update_interval;
5916 CONF("total_run_times") {
5918 total_run_times = strtod(value, 0);
5924 stuff_in_uppercase = string_to_bool(value);
5926 CONF("max_specials") {
5928 max_specials = atoi(value);
5933 CONF("max_user_text") {
5935 max_user_text = atoi(value);
5940 CONF("text_buffer_size") {
5942 text_buffer_size = atoi(value);
5943 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5944 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5945 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5953 if (output_methods & TO_X) {
5954 X11_initialisation();
5963 global_text = (char *) malloc(1);
5964 global_text[0] = '\0';
5967 unsigned int l = strlen(global_text);
5969 char buf[CONF_BUFF_SIZE];
5971 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5977 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5985 /* Check for continuation of \\-\n. */
5986 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5987 global_text[l-1] = '\0';
5991 global_text = (char *) realloc(global_text, l + bl + 1);
5992 strcat(global_text, buf);
5994 if (strlen(global_text) > max_user_text) {
5999 if (strlen(global_text) < 1) {
6000 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
6002 global_text_lines = line + 1;
6005 #ifdef TCP_PORT_MONITOR
6006 CONF("max_port_monitor_connections") {
6008 if (!value || (sscanf(value, "%d", &max) != 1)) {
6009 /* an error. use default, warn and continue. */
6010 tcp_portmon_set_max_connections(0);
6012 } else if (tcp_portmon_set_max_connections(max)) {
6013 /* max is < 0, default has been set*/
6018 CONF("if_up_strictness") {
6020 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6021 ifup_strictness = IFUP_UP;
6022 } else if (strcasecmp(value, "up") == EQUAL) {
6023 ifup_strictness = IFUP_UP;
6024 } else if (strcasecmp(value, "link") == EQUAL) {
6025 ifup_strictness = IFUP_LINK;
6026 } else if (strcasecmp(value, "address") == EQUAL) {
6027 ifup_strictness = IFUP_ADDR;
6029 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6030 ifup_strictness = IFUP_UP;
6034 CONF("temperature_unit") {
6036 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
6037 } else if (set_temp_output_unit(value)) {
6038 NORM_ERR("temperature_unit: incorrect argument");
6045 char *ptr = strtok(value, " ");
6048 ptr = strtok(NULL, " ");
6055 CONF("lua_draw_hook_pre") {
6057 llua_set_draw_pre_hook(value);
6062 CONF("lua_draw_hook_post") {
6064 llua_set_draw_post_hook(value);
6069 CONF("lua_startup_hook") {
6071 llua_set_startup_hook(value);
6076 CONF("lua_shutdown_hook") {
6078 llua_set_shutdown_hook(value);
6084 #endif /* HAVE_LUA */
6096 CONF("default_color"){}
6097 CONF3("default_shade_color", "default_shadecolor"){}
6098 CONF3("default_outline_color", "default_outlinecolor") {}
6099 CONF("own_window_colour") {}
6102 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
6108 if (info.music_player_interval == 0) {
6109 // default to update_interval
6110 info.music_player_interval = update_interval;
6112 if (!global_text) { // didn't supply any text
6113 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
6119 static void load_config_file_x11(const char *f)
6124 fp = open_config_file(f);
6128 DBGP("reading contents from config file '%s'", f);
6131 char buff[CONF_BUFF_SIZE], *name, *value;
6132 int ret = do_config_step(&line, fp, buff, &name, &value);
6133 if (ret == CONF_BREAK) {
6135 } else if (ret == CONF_CONTINUE) {
6140 X11_initialisation();
6141 if (x_initialised == YES) {
6143 color0 = get_x11_color(value);
6150 X11_initialisation();
6151 if (x_initialised == YES) {
6153 color1 = get_x11_color(value);
6160 X11_initialisation();
6161 if (x_initialised == YES) {
6163 color2 = get_x11_color(value);
6170 X11_initialisation();
6171 if (x_initialised == YES) {
6173 color3 = get_x11_color(value);
6180 X11_initialisation();
6181 if (x_initialised == YES) {
6183 color4 = get_x11_color(value);
6190 X11_initialisation();
6191 if (x_initialised == YES) {
6193 color5 = get_x11_color(value);
6200 X11_initialisation();
6201 if (x_initialised == YES) {
6203 color6 = get_x11_color(value);
6210 X11_initialisation();
6211 if (x_initialised == YES) {
6213 color7 = get_x11_color(value);
6220 X11_initialisation();
6221 if (x_initialised == YES) {
6223 color8 = get_x11_color(value);
6230 X11_initialisation();
6231 if (x_initialised == YES) {
6233 color9 = get_x11_color(value);
6239 CONF("default_color") {
6240 X11_initialisation();
6241 if (x_initialised == YES) {
6243 default_fg_color = get_x11_color(value);
6249 CONF3("default_shade_color", "default_shadecolor") {
6250 X11_initialisation();
6251 if (x_initialised == YES) {
6253 default_bg_color = get_x11_color(value);
6259 CONF3("default_outline_color", "default_outlinecolor") {
6260 X11_initialisation();
6261 if (x_initialised == YES) {
6263 default_out_color = get_x11_color(value);
6270 CONF("own_window_colour") {
6271 X11_initialisation();
6272 if (x_initialised == YES) {
6274 background_colour = get_x11_color(value);
6276 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
6277 "'#' for hex colours");
6283 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
6284 if(output_methods & TO_X) {
6285 X11_initialisation();
6294 #undef CONF_CONTINUE
6295 #undef CONF_BUFF_SIZE
6303 static void print_help(const char *prog_name) {
6304 printf("Usage: %s [OPTION]...\n"
6305 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
6306 "window. Command line options will override configurations defined in config\n"
6308 " -v, --version version\n"
6309 " -q, --quiet quiet mode\n"
6310 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
6311 " -c, --config=FILE config file to load\n"
6312 #ifdef CONFIG_OUTPUT
6313 " -C, --print-config print the builtin default config to stdout\n"
6314 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
6316 " -d, --daemonize daemonize, fork to background\n"
6317 " -h, --help help\n"
6319 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
6320 " -f, --font=FONT font to use\n"
6321 " -X, --display=DISPLAY X11 display to use\n"
6323 " -o, --own-window create own window to draw\n"
6326 " -b, --double-buffer double buffer (prevents flickering)\n"
6328 " -w, --window-id=WIN_ID window id to draw\n"
6329 " -x X x position\n"
6330 " -y Y y position\n"
6332 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
6333 " -u, --interval=SECS update interval\n"
6334 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
6339 /* : means that character before that takes an argument */
6340 static const char *getopt_string = "vVqdDt:u:i:hc:"
6350 #ifdef CONFIG_OUTPUT
6355 static const struct option longopts[] = {
6356 { "help", 0, NULL, 'h' },
6357 { "version", 0, NULL, 'V' },
6358 { "debug", 0, NULL, 'D' },
6359 { "config", 1, NULL, 'c' },
6360 #ifdef CONFIG_OUTPUT
6361 { "print-config", 0, NULL, 'C' },
6363 { "daemonize", 0, NULL, 'd' },
6365 { "alignment", 1, NULL, 'a' },
6366 { "font", 1, NULL, 'f' },
6367 { "display", 1, NULL, 'X' },
6369 { "own-window", 0, NULL, 'o' },
6372 { "double-buffer", 0, NULL, 'b' },
6374 { "window-id", 1, NULL, 'w' },
6376 { "text", 1, NULL, 't' },
6377 { "interval", 0, NULL, 'u' },
6381 void initialisation(int argc, char **argv) {
6382 struct sigaction act, oact;
6384 set_default_configurations();
6385 load_config_file(current_config);
6386 currentconffile = conftree_add(currentconffile, current_config);
6388 /* init specials array */
6389 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
6390 NORM_ERR("failed to create specials array");
6394 if (current_mail_spool == NULL) {
6397 variable_substitute(MAIL_FILE, buf, 256);
6399 if (buf[0] != '\0') {
6400 current_mail_spool = strndup(buf, text_buffer_size);
6405 /* handle other command line arguments */
6407 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
6408 || defined(__NetBSD__)
6409 optind = optreset = 1;
6414 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6415 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
6416 "kvm_open")) == NULL) {
6417 CRIT_ERR(NULL, NULL, "cannot read kvm");
6422 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6430 fork_to_background = 1;
6433 global_debug_level++;
6437 set_first_font(optarg);
6440 text_alignment = string_to_alignment(optarg);
6459 global_text = strndup(optarg, max_user_text);
6460 convert_escapes(global_text);
6464 update_interval = strtod(optarg, 0);
6465 update_interval_old = update_interval;
6466 if (info.music_player_interval == 0) {
6467 // default to update_interval
6468 info.music_player_interval = update_interval;
6473 total_run_times = strtod(optarg, 0);
6477 gap_x = atoi(optarg);
6481 gap_y = atoi(optarg);
6492 if (output_methods & TO_X) {
6493 load_config_file_x11(current_config);
6497 /* generate text and get initial size */
6498 extract_variable_text(global_text);
6505 if (fork_to_background) {
6510 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
6517 fprintf(stderr, "\n");
6522 /* parent process */
6523 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
6530 text_buffer = malloc(max_user_text);
6531 memset(text_buffer, 0, max_user_text);
6532 tmpstring1 = malloc(text_buffer_size);
6533 memset(tmpstring1, 0, text_buffer_size);
6534 tmpstring2 = malloc(text_buffer_size);
6535 memset(tmpstring2, 0, text_buffer_size);
6540 X11_create_window();
6543 llua_setup_info(&info, update_interval);
6544 #endif /* HAVE_LUA */
6549 /* Set signal handlers */
6550 act.sa_handler = signal_handler;
6551 sigemptyset(&act.sa_mask);
6554 act.sa_flags |= SA_RESTART;
6557 if ( sigaction(SIGINT, &act, &oact) < 0
6558 || sigaction(SIGALRM, &act, &oact) < 0
6559 || sigaction(SIGUSR1, &act, &oact) < 0
6560 || sigaction(SIGHUP, &act, &oact) < 0
6561 || sigaction(SIGTERM, &act, &oact) < 0) {
6562 NORM_ERR("error setting signal handler: %s", strerror(errno));
6566 llua_startup_hook();
6567 #endif /* HAVE_LUA */
6570 int main(int argc, char **argv)
6579 g_signal_pending = 0;
6580 max_user_text = MAX_USER_TEXT_DEFAULT;
6582 memset(&info, 0, sizeof(info));
6586 #ifdef TCP_PORT_MONITOR
6587 /* set default connection limit */
6588 tcp_portmon_set_max_connections(0);
6591 /* handle command line parameters that don't change configs */
6593 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
6594 || ((s = getenv("LANG")) && *s)) {
6595 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6597 NORM_ERR("malloc failed");
6599 for (x = 0; x < strlen(s); x++) {
6600 temp[x] = tolower(s[x]);
6603 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6609 if (!setlocale(LC_CTYPE, "")) {
6610 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6614 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6625 if (current_config) {
6626 free(current_config);
6628 current_config = strndup(optarg, max_user_text);
6631 freopen("/dev/null", "w", stderr);
6634 print_help(argv[0]);
6636 #ifdef CONFIG_OUTPUT
6643 window.window = strtol(optarg, 0, 0);
6648 disp = strdup(optarg);
6657 /* check if specified config file is valid */
6658 if (current_config) {
6660 if (stat(current_config, &sb) ||
6661 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
6662 NORM_ERR("invalid configuration file '%s'\n", current_config);
6663 free(current_config);
6668 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
6670 if (!current_config) {
6671 /* load default config file */
6672 char buf[DEFAULT_TEXT_BUFFER_SIZE];
6675 /* Try to use personal config file first */
6676 to_real_path(buf, CONFIG_FILE);
6677 if (buf[0] && (fp = fopen(buf, "r"))) {
6678 current_config = strndup(buf, max_user_text);
6682 /* Try to use system config file if personal config not readable */
6683 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
6684 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
6688 /* No readable config found */
6689 if (!current_config) {
6690 #ifdef CONFIG_OUTPUT
6691 current_config = strdup("==builtin==");
6692 NORM_ERR("no readable personal or system-wide config file found,"
6693 " using builtin default");
6695 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
6696 #endif /* ! CONF_OUTPUT */
6701 /* Load xoap keys, if existing */
6705 #ifdef HAVE_SYS_INOTIFY_H
6706 inotify_fd = inotify_init();
6707 #endif /* HAVE_SYS_INOTIFY_H */
6709 initialisation(argc, argv);
6713 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6721 void alarm_handler(void) {
6723 kill(childpid, SIGTERM);
6727 static void signal_handler(int sig)
6729 /* signal handler is light as a feather, as it should be.
6730 * we will poll g_signal_pending with each loop of conky
6731 * and do any signal processing there, NOT here (except
6732 * SIGALRM because this is caused when conky is hanging) */
6733 if(sig == SIGALRM) {
6736 g_signal_pending = sig;