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 substitute_newlines(p, a - 2);
3248 /* load any new fonts we may have had */
3249 if (need_to_load_fonts) {
3255 void evaluate(const char *text, char *buffer)
3257 struct information *tmp_info;
3258 struct text_object subroot;
3260 tmp_info = malloc(sizeof(struct information));
3261 memcpy(tmp_info, &info, sizeof(struct information));
3262 parse_conky_vars(&subroot, text, buffer, tmp_info);
3263 DBGP("evaluated '%s' to '%s'", text, buffer);
3265 free_text_objects(&subroot, 1);
3269 double current_update_time, next_update_time, last_update_time;
3271 static void generate_text(void)
3273 struct information *cur = &info;
3280 current_update_time = get_time();
3284 /* add things to the buffer */
3290 generate_text_internal(p, max_user_text, global_root_object, cur);
3292 if (stuff_in_uppercase) {
3295 tmp_p = text_buffer;
3297 *tmp_p = toupper(*tmp_p);
3302 next_update_time += update_interval;
3303 if (next_update_time < get_time()) {
3304 next_update_time = get_time() + update_interval;
3305 } else if (next_update_time > get_time() + update_interval) {
3306 next_update_time = get_time() + update_interval;
3308 last_update_time = current_update_time;
3312 void set_update_interval(double interval)
3314 update_interval = interval;
3315 update_interval_old = interval;
3318 static inline int get_string_width(const char *s)
3321 if (output_methods & TO_X) {
3322 return *s ? calc_text_width(s, strlen(s)) : 0;
3329 static int get_string_width_special(char *s, int special_index)
3336 if ((output_methods & TO_X) == 0) {
3337 return (s) ? strlen(s) : 0;
3344 p = strndup(s, text_buffer_size);
3348 if (*p == SPECIAL_CHAR) {
3349 /* shift everything over by 1 so that the special char
3350 * doesn't mess up the size calculation */
3351 for (i = 0; i < (long)strlen(p); i++) {
3352 *(p + i) = *(p + i + 1);
3354 if (specials[special_index + idx].type == GRAPH
3355 || specials[special_index + idx].type == GAUGE
3356 || specials[special_index + idx].type == BAR) {
3357 width += specials[special_index + idx].width;
3360 } else if (*p == SECRIT_MULTILINE_CHAR) {
3367 if (strlen(final) > 1) {
3368 width += calc_text_width(final, strlen(final));
3374 static int text_size_updater(char *s, int special_index);
3376 int last_font_height;
3377 static void update_text_area(void)
3381 if ((output_methods & TO_X) == 0)
3383 /* update text size if it isn't fixed */
3388 text_width = minimum_width;
3390 last_font_height = font_height();
3391 for_each_line(text_buffer, text_size_updater);
3393 if (text_height < minimum_height) {
3394 text_height = minimum_height;
3396 if (text_width > maximum_width && maximum_width > 0) {
3397 text_width = maximum_width;
3401 /* get text position on workarea */
3402 switch (text_alignment) {
3409 x = workarea[2] - text_width - gap_x;
3414 x = workarea[2] / 2 - text_width / 2 - gap_x;
3421 y = workarea[3] - text_height - gap_y;
3425 x = workarea[2] - text_width - gap_x;
3426 y = workarea[3] - text_height - gap_y;
3430 x = workarea[2] / 2 - text_width / 2 - gap_x;
3431 y = workarea[3] - text_height - gap_y;
3436 y = workarea[3] / 2 - text_height / 2 - gap_y;
3440 x = workarea[2] - text_width - gap_x;
3441 y = workarea[3] / 2 - text_height / 2 - gap_y;
3445 case NONE: // Let the WM manage the window
3456 if (own_window && !fixed_pos) {
3459 text_start_x = window.border_inner_margin + window.border_outer_margin + window.border_width;
3460 text_start_y = window.border_inner_margin + window.border_outer_margin + window.border_width;
3461 window.x = x - window.border_inner_margin - window.border_outer_margin - window.border_width;
3462 window.y = y - window.border_inner_margin - window.border_outer_margin - window.border_width;
3466 /* If window size doesn't match to workarea's size,
3467 * then window probably includes panels (gnome).
3468 * Blah, doesn't work on KDE. */
3469 if (workarea[2] != window.width || workarea[3] != window.height) {
3478 /* update lua window globals */
3479 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
3480 #endif /* HAVE_LUA */
3485 static int cur_x, cur_y; /* current x and y for drawing */
3487 //draw_mode also without X11 because we only need to print to stdout with FG
3488 static int draw_mode; /* FG, BG or OUTLINE */
3490 static long current_color;
3492 static int text_size_updater(char *s, int special_index)
3497 if ((output_methods & TO_X) == 0)
3499 /* get string widths and skip specials */
3502 if (*p == SPECIAL_CHAR) {
3504 w += get_string_width(s);
3507 if (specials[special_index].type == BAR
3508 || specials[special_index].type == GAUGE
3509 || specials[special_index].type == GRAPH) {
3510 w += specials[special_index].width;
3511 if (specials[special_index].height > last_font_height) {
3512 last_font_height = specials[special_index].height;
3513 last_font_height += font_height();
3515 } else if (specials[special_index].type == OFFSET) {
3516 if (specials[special_index].arg > 0) {
3517 w += specials[special_index].arg;
3519 } else if (specials[special_index].type == VOFFSET) {
3520 last_font_height += specials[special_index].arg;
3521 } else if (specials[special_index].type == GOTO) {
3522 if (specials[special_index].arg > cur_x) {
3523 w = (int) specials[special_index].arg;
3525 } else if (specials[special_index].type == TAB) {
3526 int start = specials[special_index].arg;
3527 int step = specials[special_index].width;
3529 if (!step || step < 0) {
3532 w += step - (cur_x - text_start_x - start) % step;
3533 } else if (specials[special_index].type == FONT) {
3534 selected_font = specials[special_index].font_added;
3535 if (font_height() > last_font_height) {
3536 last_font_height = font_height();
3542 } else if (*p == SECRIT_MULTILINE_CHAR) {
3545 lw = get_string_width(s);
3546 *p = SECRIT_MULTILINE_CHAR;
3548 w = lw > w ? lw : w;
3549 text_height += last_font_height;
3553 w += get_string_width(s);
3554 if (w > text_width) {
3557 if (text_width > maximum_width && maximum_width) {
3558 text_width = maximum_width;
3561 text_height += last_font_height;
3562 last_font_height = font_height();
3563 return special_index;
3567 static inline void set_foreground_color(long c)
3570 if (output_methods & TO_X) {
3572 XSetForeground(display, window.gc, c);
3576 if (output_methods & TO_NCURSES) {
3577 attron(COLOR_PAIR(c));
3579 #endif /* NCURSES */
3584 static void draw_string(const char *s)
3586 int i, i2, pos, width_of_s;
3589 char *s_with_newlines;
3595 width_of_s = get_string_width(s);
3596 s_with_newlines = strdup(s);
3597 for(i = 0; i < (int) strlen(s_with_newlines); i++) {
3598 if(s_with_newlines[i] == SECRIT_MULTILINE_CHAR) {
3599 s_with_newlines[i] = '\n';
3602 if ((output_methods & TO_STDOUT) && draw_mode == FG) {
3603 printf("%s\n", s_with_newlines);
3604 if (extra_newline) fputc('\n', stdout);
3605 fflush(stdout); /* output immediately, don't buffer */
3607 if ((output_methods & TO_STDERR) && draw_mode == FG) {
3608 fprintf(stderr, "%s\n", s_with_newlines);
3609 fflush(stderr); /* output immediately, don't buffer */
3611 if ((output_methods & OVERWRITE_FILE) && draw_mode == FG && overwrite_fpointer) {
3612 fprintf(overwrite_fpointer, "%s\n", s_with_newlines);
3614 if ((output_methods & APPEND_FILE) && draw_mode == FG && append_fpointer) {
3615 fprintf(append_fpointer, "%s\n", s_with_newlines);
3618 if ((output_methods & TO_NCURSES) && draw_mode == FG) {
3619 printw("%s", s_with_newlines);
3622 free(s_with_newlines);
3623 memset(tmpstring1, 0, text_buffer_size);
3624 memset(tmpstring2, 0, text_buffer_size);
3625 strncpy(tmpstring1, s, text_buffer_size - 1);
3630 if (output_methods & TO_X) {
3631 max = ((text_width - width_of_s) / get_string_width(" "));
3634 /* This code looks for tabs in the text and coverts them to spaces.
3635 * The trick is getting the correct number of spaces, and not going
3636 * over the window's size without forcing the window larger. */
3637 for (i = 0; i < (int) text_buffer_size; i++) {
3638 if (tmpstring1[i] == '\t') {
3640 for (i2 = 0; i2 < (8 - (1 + pos) % 8) && added <= max; i2++) {
3641 /* guard against overrun */
3642 tmpstring2[MIN(pos + i2, (int)text_buffer_size - 1)] = ' ';
3647 /* guard against overrun */
3648 tmpstring2[MIN(pos, (int) text_buffer_size - 1)] = tmpstring1[i];
3653 if (output_methods & TO_X) {
3654 if (text_width == maximum_width) {
3655 /* this means the text is probably pushing the limit,
3656 * so we'll chop it */
3657 while (cur_x + get_string_width(tmpstring2) - text_start_x
3658 > maximum_width && strlen(tmpstring2) > 0) {
3659 tmpstring2[strlen(tmpstring2) - 1] = '\0';
3666 if (output_methods & TO_X) {
3672 c.pixel = current_color;
3673 XQueryColor(display, DefaultColormap(display, screen), &c);
3676 c2.color.red = c.red;
3677 c2.color.green = c.green;
3678 c2.color.blue = c.blue;
3679 c2.color.alpha = fonts[selected_font].font_alpha;
3681 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3682 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3684 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3685 cur_x, cur_y, (const XftChar8 *) s, strlen(s));
3690 XDrawString(display, window.drawable, window.gc, cur_x, cur_y, s,
3693 cur_x += width_of_s;
3696 memcpy(tmpstring1, s, text_buffer_size);
3699 int draw_each_line_inner(char *s, int special_index, int last_special_applied)
3702 int font_h = font_height();
3707 int last_special_needed = -1;
3708 int orig_special_index = special_index;
3711 cur_x = text_start_x;
3712 cur_y += font_ascent();
3716 if (*p == SECRIT_MULTILINE_CHAR) {
3717 /* special newline marker for multiline objects */
3722 if (*p == SPECIAL_CHAR || last_special_applied > -1) {
3727 /* draw string before special, unless we're dealing multiline
3729 if (last_special_applied > -1) {
3730 special_index = last_special_applied;
3738 switch (specials[special_index].type) {
3740 case HORIZONTAL_LINE:
3742 int h = specials[special_index].height;
3743 int mid = font_ascent() / 2;
3745 w = text_start_x + text_width - cur_x;
3747 XSetLineAttributes(display, window.gc, h, LineSolid,
3748 CapButt, JoinMiter);
3749 XDrawLine(display, window.drawable, window.gc, cur_x,
3750 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3756 int h = specials[special_index].height;
3757 int tmp_s = specials[special_index].arg;
3758 int mid = font_ascent() / 2;
3759 char ss[2] = { tmp_s, tmp_s };
3761 w = text_start_x + text_width - cur_x - 1;
3762 XSetLineAttributes(display, window.gc, h, LineOnOffDash,
3763 CapButt, JoinMiter);
3764 XSetDashes(display, window.gc, 0, ss, 2);
3765 XDrawLine(display, window.drawable, window.gc, cur_x,
3766 cur_y - mid / 2, cur_x + w, cur_y - mid / 2);
3772 int h, bar_usage, by;
3773 if (cur_x - text_start_x > maximum_width
3774 && maximum_width > 0) {
3777 h = specials[special_index].height;
3778 bar_usage = specials[special_index].arg;
3779 by = cur_y - (font_ascent() / 2) - 1;
3784 w = specials[special_index].width;
3786 w = text_start_x + text_width - cur_x - 1;
3792 XSetLineAttributes(display, window.gc, 1, LineSolid,
3793 CapButt, JoinMiter);
3795 XDrawRectangle(display, window.drawable, window.gc, cur_x,
3797 XFillRectangle(display, window.drawable, window.gc, cur_x,
3798 by, w * bar_usage / 255, h);
3806 case GAUGE: /* new GAUGE */
3809 unsigned long last_colour = current_color;
3811 float angle, px, py;
3815 if (cur_x - text_start_x > maximum_width
3816 && maximum_width > 0) {
3820 h = specials[special_index].height;
3821 by = cur_y - (font_ascent() / 2) - 1;
3826 w = specials[special_index].width;
3828 w = text_start_x + text_width - cur_x - 1;
3834 XSetLineAttributes(display, window.gc, 1, LineSolid,
3835 CapButt, JoinMiter);
3837 XDrawArc(display, window.drawable, window.gc,
3838 cur_x, by, w, h * 2, 0, 180*64);
3841 usage = specials[special_index].arg;
3842 angle = (M_PI)*(float)(usage)/255.;
3843 px = (float)(cur_x+(w/2.))-(float)(w/2.)*cos(angle);
3844 py = (float)(by+(h))-(float)(h)*sin(angle);
3846 XDrawLine(display, window.drawable, window.gc,
3847 cur_x + (w/2.), by+(h), (int)(px), (int)(py));
3855 set_foreground_color(last_colour);
3863 int h, by, i = 0, j = 0;
3865 unsigned long last_colour = current_color;
3866 unsigned long *tmpcolour = 0;
3867 if (cur_x - text_start_x > maximum_width
3868 && maximum_width > 0) {
3871 h = specials[special_index].height;
3872 by = cur_y - (font_ascent() / 2) - 1;
3877 w = specials[special_index].width;
3879 w = text_start_x + text_width - cur_x - 1;
3884 if (draw_graph_borders) {
3885 XSetLineAttributes(display, window.gc, 1, LineSolid,
3886 CapButt, JoinMiter);
3887 XDrawRectangle(display, window.drawable, window.gc,
3890 XSetLineAttributes(display, window.gc, 1, LineSolid,
3891 CapButt, JoinMiter);
3893 if (specials[special_index].last_colour != 0
3894 || specials[special_index].first_colour != 0) {
3895 tmpcolour = do_gradient(w - 1, specials[special_index].last_colour, specials[special_index].first_colour);
3898 for (i = w - 2; i > -1; i--) {
3899 if (specials[special_index].last_colour != 0
3900 || specials[special_index].first_colour != 0) {
3901 if (specials[special_index].tempgrad) {
3904 (int)((float)(w - 2) - specials[special_index].graph[j] *
3905 (w - 2) / (float)specials[special_index].graph_scale)
3909 (int)((float)(w - 2) - specials[special_index].graph[j] *
3910 (w - 2) / (float)specials[special_index].graph_scale)
3913 if (specials[special_index].graph[j] == specials[special_index].graph_scale) {
3915 (int)((float)(w - 2) - specials[special_index].graph[j] *
3916 (w - 2) / (float)specials[special_index].graph_scale)
3920 #endif /* DEBUG_lol */
3921 XSetForeground(display, window.gc, tmpcolour[
3922 (int)((float)(w - 2) - specials[special_index].graph[j] *
3923 (w - 2) / (float)specials[special_index].graph_scale)
3926 XSetForeground(display, window.gc, tmpcolour[colour_idx++]);
3929 /* this is mugfugly, but it works */
3930 XDrawLine(display, window.drawable, window.gc,
3931 cur_x + i + 1, by + h, cur_x + i + 1,
3932 round_to_int((double)by + h - specials[special_index].graph[j] *
3933 (h - 1) / specials[special_index].graph_scale));
3934 if ((w - i) / ((float) (w - 2) /
3935 (specials[special_index].graph_width)) > j
3936 && j < MAX_GRAPH_DEPTH - 3) {
3940 if (tmpcolour) free(tmpcolour);
3945 /* if (draw_mode == BG) {
3946 set_foreground_color(default_bg_color);
3947 } else if (draw_mode == OUTLINE) {
3948 set_foreground_color(default_out_color);
3950 set_foreground_color(default_fg_color);
3952 if (show_graph_range) {
3955 unsigned short int seconds = update_interval * w;
3961 unsigned short int timeunits;
3963 timeunits = seconds / 86400; seconds %= 86400;
3964 if (timeunits > 0) {
3965 asprintf(&tmp_day_str, "%dd", timeunits);
3967 tmp_day_str = strdup("");
3969 timeunits = seconds / 3600; seconds %= 3600;
3970 if (timeunits > 0) {
3971 asprintf(&tmp_hour_str, "%dh", timeunits);
3973 tmp_hour_str = strdup("");
3975 timeunits = seconds / 60; seconds %= 60;
3976 if (timeunits > 0) {
3977 asprintf(&tmp_min_str, "%dm", timeunits);
3979 tmp_min_str = strdup("");
3982 asprintf(&tmp_sec_str, "%ds", seconds);
3984 tmp_sec_str = strdup("");
3986 asprintf(&tmp_str, "%s%s%s%s", tmp_day_str, tmp_hour_str, tmp_min_str, tmp_sec_str);
3987 free(tmp_day_str); free(tmp_hour_str); free(tmp_min_str); free(tmp_sec_str);
3989 asprintf(&tmp_str, "Range not possible"); // should never happen, but better safe then sorry
3991 cur_x += (w / 2) - (font_ascent() * (strlen(tmp_str) / 2));
3992 cur_y += font_h / 2;
3993 draw_string(tmp_str);
3999 if (show_graph_scale && (specials[special_index].show_scale == 1)) {
4003 cur_x += font_ascent() / 2;
4004 cur_y += font_h / 2;
4006 calloc(log10(floor(specials[special_index].graph_scale)) + 4,
4008 sprintf(tmp_str, "%.1f", specials[special_index].graph_scale);
4009 draw_string(tmp_str);
4015 set_foreground_color(last_colour);
4021 int old = font_ascent();
4023 cur_y -= font_ascent();
4024 selected_font = specials[special_index].font_added;
4026 if (cur_y + font_ascent() < cur_y + old) {
4029 cur_y += font_ascent();
4031 font_h = font_height();
4036 if (draw_mode == FG) {
4037 set_foreground_color(specials[special_index].arg);
4043 if (draw_mode == BG) {
4044 set_foreground_color(specials[special_index].arg);
4049 if (draw_mode == OUTLINE) {
4050 set_foreground_color(specials[special_index].arg);
4055 w += specials[special_index].arg;
4056 last_special_needed = special_index;
4060 cur_y += specials[special_index].arg;
4064 if (specials[special_index].arg >= 0) {
4065 cur_x = (int) specials[special_index].arg;
4067 last_special_needed = special_index;
4072 int start = specials[special_index].arg;
4073 int step = specials[special_index].width;
4075 if (!step || step < 0) {
4078 w = step - (cur_x - text_start_x - start) % step;
4079 last_special_needed = special_index;
4085 /* TODO: add back in "+ window.border_inner_margin" to the end of
4087 int pos_x = text_start_x + text_width -
4088 get_string_width_special(s, special_index);
4090 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4091 "get_string_width(p) %i gap_x %i "
4092 "specials[special_index].arg %i window.border_inner_margin %i "
4093 "window.border_width %i\n", pos_x, text_start_x, text_width,
4094 cur_x, get_string_width_special(s), gap_x,
4095 specials[special_index].arg, window.border_inner_margin,
4096 window.border_width); */
4097 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
4098 cur_x = pos_x - specials[special_index].arg;
4100 last_special_needed = special_index;
4106 int pos_x = (text_width) / 2 - get_string_width_special(s,
4107 special_index) / 2 - (cur_x -
4109 /* int pos_x = text_start_x + text_width / 2 -
4110 get_string_width_special(s) / 2; */
4112 /* printf("pos_x %i text_start_x %i text_width %i cur_x %i "
4113 "get_string_width(p) %i gap_x %i "
4114 "specials[special_index].arg %i\n", pos_x, text_start_x,
4115 text_width, cur_x, get_string_width(s), gap_x,
4116 specials[special_index].arg); */
4117 if (pos_x > specials[special_index].arg) {
4118 w = pos_x - specials[special_index].arg;
4120 last_special_needed = special_index;
4130 if (special_index != last_special_applied) {
4133 special_index = orig_special_index;
4134 last_special_applied = -1;
4145 if (output_methods & TO_NCURSES) {
4148 #endif /* NCURSES */
4150 cur_y += font_descent();
4152 if (recurse && *recurse) {
4153 special_index = draw_each_line_inner(recurse, special_index, last_special_needed);
4154 *(recurse - 1) = SECRIT_MULTILINE_CHAR;
4156 return special_index;
4159 static int draw_line(char *s, int special_index)
4162 if (output_methods & TO_X) {
4163 return draw_each_line_inner(s, special_index, -1);
4167 if (output_methods & TO_NCURSES) {
4168 return draw_each_line_inner(s, special_index, -1);
4170 #endif /* NCURSES */
4172 UNUSED(special_index);
4176 static void draw_text(void)
4180 llua_draw_pre_hook();
4181 #endif /* HAVE_LUA */
4182 if (output_methods & TO_X) {
4183 cur_y = text_start_y;
4186 if (draw_borders && window.border_width > 0) {
4187 if (stippled_borders) {
4188 char ss[2] = { stippled_borders, stippled_borders };
4189 XSetLineAttributes(display, window.gc, window.border_width, LineOnOffDash,
4190 CapButt, JoinMiter);
4191 XSetDashes(display, window.gc, 0, ss, 2);
4193 XSetLineAttributes(display, window.gc, window.border_width, LineSolid,
4194 CapButt, JoinMiter);
4197 XDrawRectangle(display, window.drawable, window.gc,
4198 text_start_x - window.border_inner_margin - window.border_width,
4199 text_start_y - window.border_inner_margin - window.border_width,
4200 text_width + window.border_inner_margin * 2 + window.border_width * 2,
4201 text_height + window.border_inner_margin * 2 + window.border_width * 2);
4209 init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
4210 attron(COLOR_PAIR(COLOR_WHITE));
4211 #endif /* NCURSES */
4212 for_each_line(text_buffer, draw_line);
4213 #if defined(HAVE_LUA) && defined(X11)
4214 llua_draw_post_hook();
4215 #endif /* HAVE_LUA */
4218 static void draw_stuff(void)
4221 cimlib_render(text_start_x, text_start_y, window.width, window.height);
4223 if (overwrite_file) {
4224 overwrite_fpointer = fopen(overwrite_file, "w");
4225 if(!overwrite_fpointer)
4226 NORM_ERR("Can't overwrite '%s' anymore", overwrite_file);
4229 append_fpointer = fopen(append_file, "a");
4230 if(!append_fpointer)
4231 NORM_ERR("Can't append '%s' anymore", append_file);
4234 if (output_methods & TO_X) {
4236 if (draw_shades && !draw_outline) {
4239 set_foreground_color(default_bg_color);
4250 for (i = -1; i < 2; i++) {
4251 for (j = -1; j < 2; j++) {
4252 if (i == 0 && j == 0) {
4257 set_foreground_color(default_out_color);
4258 draw_mode = OUTLINE;
4266 set_foreground_color(default_fg_color);
4272 xdbe_swap_buffers();
4273 if (output_methods & TO_X) {
4278 if(overwrite_fpointer) {
4279 fclose(overwrite_fpointer);
4280 overwrite_fpointer = 0;
4282 if(append_fpointer) {
4283 fclose(append_fpointer);
4284 append_fpointer = 0;
4289 static void clear_text(int exposures)
4293 /* The swap action is XdbeBackground, which clears */
4297 if (display && window.window) { // make sure these are !null
4298 /* there is some extra space for borders and outlines */
4299 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4300 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4301 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4302 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, exposures ? True : 0);
4307 static int need_to_update;
4309 /* update_text() generates new text and clears old text area */
4310 static void update_text(void)
4317 if (output_methods & TO_X)
4322 llua_update_info(&info, update_interval);
4323 #endif /* HAVE_LUA */
4326 #ifdef HAVE_SYS_INOTIFY_H
4330 static void main_loop(void)
4333 #ifdef SIGNAL_BLOCKING
4334 sigset_t newmask, oldmask;
4337 #ifdef HAVE_SYS_INOTIFY_H
4338 int inotify_config_wd = -1;
4339 #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
4340 #define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16))
4341 char inotify_buff[INOTIFY_BUF_LEN];
4342 #endif /* HAVE_SYS_INOTIFY_H */
4345 #ifdef SIGNAL_BLOCKING
4346 sigemptyset(&newmask);
4347 sigaddset(&newmask, SIGINT);
4348 sigaddset(&newmask, SIGTERM);
4349 sigaddset(&newmask, SIGUSR1);
4352 last_update_time = 0.0;
4353 next_update_time = get_time();
4355 while (terminate == 0 && (total_run_times == 0 || info.looped < total_run_times)) {
4356 if(update_interval_bat != NOBATTERY && update_interval_bat != update_interval_old) {
4357 char buf[max_user_text];
4359 get_battery_short_status(buf, max_user_text, "BAT0");
4361 update_interval = update_interval_bat;
4363 update_interval = update_interval_old;
4368 #ifdef SIGNAL_BLOCKING
4369 /* block signals. we will inspect for pending signals later */
4370 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
4371 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4376 if (output_methods & TO_X) {
4379 /* wait for X event or timeout */
4381 if (!XPending(display)) {
4385 t = next_update_time - get_time();
4389 } else if (t > update_interval) {
4390 t = update_interval;
4393 tv.tv_sec = (long) t;
4394 tv.tv_usec = (long) (t * 1000000) % 1000000;
4396 FD_SET(ConnectionNumber(display), &fdsr);
4398 s = select(ConnectionNumber(display) + 1, &fdsr, 0, 0, &tv);
4400 if (errno != EINTR) {
4401 NORM_ERR("can't select(): %s", strerror(errno));
4411 if (need_to_update) {
4413 int wx = window.x, wy = window.y;
4423 /* resize window if it isn't right size */
4425 && (text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.width
4426 || text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2 != window.height)) {
4427 window.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4428 window.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4429 draw_stuff(); /* redraw everything in our newly sized window */
4430 XResizeWindow(display, window.window, window.width,
4431 window.height); /* resize window */
4432 set_transparent_background(window.window);
4435 xdbe_swap_buffers();
4440 /* update lua window globals */
4441 llua_update_window_table(text_start_x, text_start_y, text_width, text_height);
4442 #endif /* HAVE_LUA */
4445 /* move window if it isn't in right position */
4446 if (!fixed_pos && (window.x != wx || window.y != wy)) {
4447 XMoveWindow(display, window.window, window.x, window.y);
4452 if (changed && window.type == TYPE_PANEL) {
4455 fprintf(stderr, PACKAGE_NAME": defining struts\n");
4458 switch (text_alignment) {
4485 set_struts(sidenum);
4496 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4497 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4498 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4499 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4500 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4505 /* handle X events */
4506 while (XPending(display)) {
4509 XNextEvent(display, &ev);
4516 r.width = ev.xexpose.width;
4517 r.height = ev.xexpose.height;
4518 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4522 case PropertyNotify:
4524 if ( ev.xproperty.state == PropertyNewValue ) {
4525 get_x11_desktop_info( ev.xproperty.display, ev.xproperty.atom );
4531 case ReparentNotify:
4532 /* set background to ParentRelative for all parents */
4534 set_transparent_background(window.window);
4538 case ConfigureNotify:
4540 /* if window size isn't what expected, set fixed size */
4541 if (ev.xconfigure.width != window.width
4542 || ev.xconfigure.height != window.height) {
4543 if (window.width != 0 && window.height != 0) {
4547 /* clear old stuff before screwing up
4552 XWindowAttributes attrs;
4553 if (XGetWindowAttributes(display,
4554 window.window, &attrs)) {
4555 window.width = attrs.width;
4556 window.height = attrs.height;
4560 text_width = window.width - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4561 text_height = window.height - window.border_inner_margin * 2 - window.border_outer_margin * 2 - window.border_width * 2;
4562 if (text_width > maximum_width
4563 && maximum_width > 0) {
4564 text_width = maximum_width;
4568 /* if position isn't what expected, set fixed pos
4569 * total_updates avoids setting fixed_pos when window
4570 * is set to weird locations when started */
4571 /* // this is broken
4572 if (total_updates >= 2 && !fixed_pos
4573 && (window.x != ev.xconfigure.x
4574 || window.y != ev.xconfigure.y)
4575 && (ev.xconfigure.x != 0
4576 || ev.xconfigure.y != 0)) {
4584 /* if an ordinary window with decorations */
4585 if ((window.type == TYPE_NORMAL &&
4586 (!TEST_HINT(window.hints,
4587 HINT_UNDECORATED))) ||
4588 window.type == TYPE_DESKTOP) {
4589 /* allow conky to hold input focus. */
4592 /* forward the click to the desktop window */
4593 XUngrabPointer(display, ev.xbutton.time);
4594 ev.xbutton.window = window.desktop;
4595 ev.xbutton.x = ev.xbutton.x_root;
4596 ev.xbutton.y = ev.xbutton.y_root;
4597 XSendEvent(display, ev.xbutton.window, False,
4598 ButtonPressMask, &ev);
4599 XSetInputFocus(display, ev.xbutton.window,
4600 RevertToParent, ev.xbutton.time);
4607 /* if an ordinary window with decorations */
4608 if ((window.type == TYPE_NORMAL)
4609 && (!TEST_HINT(window.hints,
4610 HINT_UNDECORATED))) {
4611 /* allow conky to hold input focus. */
4614 /* forward the release to the desktop window */
4615 ev.xbutton.window = window.desktop;
4616 ev.xbutton.x = ev.xbutton.x_root;
4617 ev.xbutton.y = ev.xbutton.y_root;
4618 XSendEvent(display, ev.xbutton.window, False,
4619 ButtonReleaseMask, &ev);
4628 if (ev.type == x11_stuff.event_base + XDamageNotify) {
4629 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
4631 XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
4632 XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2, x11_stuff.part);
4634 #endif /* HAVE_XDAMAGE */
4640 XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
4641 XFixesSetRegion(display, x11_stuff.region2, 0, 0);
4642 #endif /* HAVE_XDAMAGE */
4644 /* XDBE doesn't seem to provide a way to clear the back buffer
4645 * without interfering with the front buffer, other than passing
4646 * XdbeBackground to XdbeSwapBuffers. That means that if we're
4647 * using XDBE, we need to redraw the text even if it wasn't part of
4648 * the exposed area. OTOH, if we're not going to call draw_stuff at
4649 * all, then no swap happens and we can safely do nothing. */
4651 if (!XEmptyRegion(x11_stuff.region)) {
4656 r.x = text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width;
4657 r.y = text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width;
4658 r.width = text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4659 r.height = text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2;
4660 XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
4663 XSetRegion(display, window.gc, x11_stuff.region);
4666 XftDrawSetClip(window.xftdraw, x11_stuff.region);
4670 XDestroyRegion(x11_stuff.region);
4671 x11_stuff.region = XCreateRegion();
4675 t = (next_update_time - get_time()) * 1000000;
4676 if(t > 0) usleep((useconds_t)t);
4680 if(output_methods & TO_NCURSES) {
4689 #ifdef SIGNAL_BLOCKING
4690 /* unblock signals of interest and let handler fly */
4691 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
4692 CRIT_ERR(NULL, NULL, "unable to sigprocmask()");
4696 switch (g_signal_pending) {
4699 NORM_ERR("received SIGHUP or SIGUSR1. reloading the config file.");
4704 NORM_ERR("received SIGINT or SIGTERM to terminate. bye!");
4707 if (output_methods & TO_X) {
4708 XDestroyRegion(x11_stuff.region);
4709 x11_stuff.region = NULL;
4711 XDamageDestroy(display, x11_stuff.damage);
4712 XFixesDestroyRegion(display, x11_stuff.region2);
4713 XFixesDestroyRegion(display, x11_stuff.part);
4714 #endif /* HAVE_XDAMAGE */
4720 if(overwrite_file) {
4721 free(overwrite_file);
4730 /* Reaching here means someone set a signal
4731 * (SIGXXXX, signal_handler), but didn't write any code
4733 * If you don't want to handle a signal, don't set a handler on
4734 * it in the first place. */
4735 if (g_signal_pending) {
4736 NORM_ERR("ignoring signal (%d)", g_signal_pending);
4740 #ifdef HAVE_SYS_INOTIFY_H
4741 if (inotify_fd != -1 && inotify_config_wd == -1 && current_config != 0) {
4742 inotify_config_wd = inotify_add_watch(inotify_fd,
4746 if (inotify_fd != -1 && inotify_config_wd != -1 && current_config != 0) {
4747 int len = 0, idx = 0;
4749 struct timeval time_to_wait;
4751 FD_ZERO(&descriptors);
4752 FD_SET(inotify_fd, &descriptors);
4754 time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
4756 select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
4757 if (FD_ISSET(inotify_fd, &descriptors)) {
4758 /* process inotify events */
4759 len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
4760 while (len > 0 && idx < len) {
4761 struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
4762 if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {
4763 /* current_config should be reloaded */
4764 NORM_ERR("'%s' modified, reloading...", current_config);
4766 if (ev->mask & IN_IGNORED) {
4767 /* for some reason we get IN_IGNORED here
4768 * sometimes, so we need to re-add the watch */
4769 inotify_config_wd = inotify_add_watch(inotify_fd,
4776 llua_inotify_query(ev->wd, ev->mask);
4778 #endif /* HAVE_LUA */
4779 idx += INOTIFY_EVENT_SIZE + ev->len;
4783 #endif /* HAVE_SYS_INOTIFY_H */
4786 llua_update_info(&info, update_interval);
4787 #endif /* HAVE_LUA */
4788 g_signal_pending = 0;
4790 clean_up(NULL, NULL);
4792 #ifdef HAVE_SYS_INOTIFY_H
4793 if (inotify_fd != -1) {
4794 inotify_rm_watch(inotify_fd, inotify_config_wd);
4796 inotify_fd = inotify_config_wd = 0;
4798 #endif /* HAVE_SYS_INOTIFY_H */
4802 static void load_config_file_x11(const char *);
4804 void initialisation(int argc, char** argv);
4806 /* reload the config file */
4807 static void reload_config(void)
4809 char *current_config_copy = strdup(current_config);
4810 clean_up(NULL, NULL);
4811 current_config = current_config_copy;
4812 initialisation(argc_copy, argv_copy);
4815 void clean_up(void *memtofree1, void* memtofree2)
4820 if(output_methods & TO_NCURSES) {
4824 conftree_empty(currentconffile);
4825 currentconffile = NULL;
4832 timed_thread_destroy_registered_threads();
4834 if (info.cpu_usage) {
4835 free(info.cpu_usage);
4836 info.cpu_usage = NULL;
4839 if (x_initialised == YES) {
4840 XClearArea(display, window.window, text_start_x - window.border_inner_margin - window.border_outer_margin - window.border_width,
4841 text_start_y - window.border_inner_margin - window.border_outer_margin - window.border_width,
4842 text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
4843 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, 0);
4846 if(x11_stuff.region) {
4847 XDestroyRegion(x11_stuff.region);
4848 x11_stuff.region = NULL;
4850 XCloseDisplay(display);
4852 if(info.x11.desktop.all_names) {
4853 free(info.x11.desktop.all_names);
4854 info.x11.desktop.all_names = NULL;
4856 if (info.x11.desktop.name) {
4857 free(info.x11.desktop.name);
4858 info.x11.desktop.name = NULL;
4862 free(fonts); //in set_default_configurations a font is set but not loaded
4868 free_update_callbacks();
4872 free_text_objects(&global_root_object, 0);
4891 free(current_config);
4894 #ifdef TCP_PORT_MONITOR
4895 tcp_portmon_clear();
4904 weather_free_info();
4907 llua_shutdown_hook();
4909 #endif /* HAVE_LUA */
4918 for (i = 0; i < special_count; i++) {
4919 if (specials[i].type == GRAPH) {
4920 free(specials[i].graph);
4928 clear_diskio_stats();
4929 if(global_cpu != NULL) {
4935 static int string_to_bool(const char *s)
4938 // Assumes an option without a true/false means true
4940 } else if (strcasecmp(s, "yes") == EQUAL) {
4942 } else if (strcasecmp(s, "true") == EQUAL) {
4944 } else if (strcasecmp(s, "1") == EQUAL) {
4951 static enum alignment string_to_alignment(const char *s)
4953 if (strcasecmp(s, "top_left") == EQUAL) {
4955 } else if (strcasecmp(s, "top_right") == EQUAL) {
4957 } else if (strcasecmp(s, "top_middle") == EQUAL) {
4959 } else if (strcasecmp(s, "bottom_left") == EQUAL) {
4961 } else if (strcasecmp(s, "bottom_right") == EQUAL) {
4962 return BOTTOM_RIGHT;
4963 } else if (strcasecmp(s, "bottom_middle") == EQUAL) {
4964 return BOTTOM_MIDDLE;
4965 } else if (strcasecmp(s, "middle_left") == EQUAL) {
4967 } else if (strcasecmp(s, "middle_right") == EQUAL) {
4968 return MIDDLE_RIGHT;
4969 } else if (strcasecmp(s, "tl") == EQUAL) {
4971 } else if (strcasecmp(s, "tr") == EQUAL) {
4973 } else if (strcasecmp(s, "tm") == EQUAL) {
4975 } else if (strcasecmp(s, "bl") == EQUAL) {
4977 } else if (strcasecmp(s, "br") == EQUAL) {
4978 return BOTTOM_RIGHT;
4979 } else if (strcasecmp(s, "bm") == EQUAL) {
4980 return BOTTOM_MIDDLE;
4981 } else if (strcasecmp(s, "ml") == EQUAL) {
4983 } else if (strcasecmp(s, "mr") == EQUAL) {
4984 return MIDDLE_RIGHT;
4985 } else if (strcasecmp(s, "none") == EQUAL) {
4993 static void set_default_configurations_for_x(void)
4995 default_fg_color = WhitePixel(display, screen);
4996 default_bg_color = BlackPixel(display, screen);
4997 default_out_color = BlackPixel(display, screen);
4998 color0 = default_fg_color;
4999 color1 = default_fg_color;
5000 color2 = default_fg_color;
5001 color3 = default_fg_color;
5002 color4 = default_fg_color;
5003 color5 = default_fg_color;
5004 color6 = default_fg_color;
5005 color7 = default_fg_color;
5006 color8 = default_fg_color;
5007 color9 = default_fg_color;
5008 current_text_color = default_fg_color;
5012 static void set_default_configurations(void)
5019 fork_to_background = 0;
5020 total_run_times = 0;
5021 info.cpu_avg_samples = 2;
5022 info.net_avg_samples = 2;
5023 info.diskio_avg_samples = 2;
5028 format_human_readable = 1;
5038 mpd_env_host = getenv("MPD_HOST");
5039 mpd_env_port = getenv("MPD_PORT");
5041 if (!mpd_env_host || !strlen(mpd_env_host)) {
5042 mpd_set_host("localhost");
5044 /* MPD_HOST environment variable is set */
5045 char *mpd_hostpart = strchr(mpd_env_host, '@');
5046 if (!mpd_hostpart) {
5047 mpd_set_host(mpd_env_host);
5049 /* MPD_HOST contains a password */
5050 char mpd_password[mpd_hostpart - mpd_env_host + 1];
5051 snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
5053 if (!strlen(mpd_hostpart + 1)) {
5054 mpd_set_host("localhost");
5056 mpd_set_host(mpd_hostpart + 1);
5059 mpd_set_password(mpd_password, 1);
5064 if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
5065 /* failed to set port from environment variable */
5066 mpd_set_port("6600");
5070 info.xmms2.artist = NULL;
5071 info.xmms2.album = NULL;
5072 info.xmms2.title = NULL;
5073 info.xmms2.genre = NULL;
5074 info.xmms2.comment = NULL;
5075 info.xmms2.url = NULL;
5076 info.xmms2.status = NULL;
5077 info.xmms2.playlist = NULL;
5079 use_spacer = NO_SPACER;
5081 output_methods = TO_X;
5083 output_methods = TO_STDOUT;
5086 show_graph_scale = 0;
5087 show_graph_range = 0;
5090 draw_graph_borders = 1;
5092 set_first_font("6x10");
5100 window.type = TYPE_NORMAL;
5102 strcpy(window.class_name, PACKAGE_NAME);
5103 sprintf(window.title, PACKAGE_NAME" (%s)", info.uname_s.nodename);
5105 stippled_borders = 0;
5106 window.border_inner_margin = 3;
5107 window.border_outer_margin = 1;
5108 window.border_width = 1;
5109 text_alignment = BOTTOM_LEFT;
5110 info.x11.monitor.number = 1;
5111 info.x11.monitor.current = 0;
5112 info.x11.desktop.current = 1;
5113 info.x11.desktop.number = 1;
5114 info.x11.desktop.nitems = 0;
5115 info.x11.desktop.all_names = NULL;
5116 info.x11.desktop.name = NULL;
5121 free(current_mail_spool);
5125 variable_substitute(MAIL_FILE, buf, 256);
5126 if (buf[0] != '\0') {
5127 current_mail_spool = strndup(buf, text_buffer_size);
5132 set_update_interval(3);
5133 update_interval_bat = NOBATTERY;
5134 info.music_player_interval = 1.0;
5135 stuff_in_uppercase = 0;
5136 info.users.number = 1;
5138 #ifdef TCP_PORT_MONITOR
5139 /* set default connection limit */
5140 tcp_portmon_set_max_connections(0);
5144 /* returns 1 if you can overwrite or create the file at 'path' */
5145 static _Bool overwrite_works(const char *path)
5149 if (!(filepointer = fopen(path, "w")))
5151 fclose(filepointer);
5155 /* returns 1 if you can append or create the file at 'path' */
5156 static _Bool append_works(const char *path)
5160 if (!(filepointer = fopen(path, "a")))
5162 fclose(filepointer);
5168 /* WARNING, this type not in Xlib spec */
5169 int x11_error_handler(Display *d, XErrorEvent *err)
5170 __attribute__((noreturn));
5171 int x11_error_handler(Display *d, XErrorEvent *err)
5173 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",
5175 (long unsigned)err->display,
5176 (long)err->resourceid,
5186 int x11_ioerror_handler(Display *d)
5187 __attribute__((noreturn));
5188 int x11_ioerror_handler(Display *d)
5190 NORM_ERR("X Error: Display %lx\n",
5197 static void X11_initialisation(void)
5199 if (x_initialised == YES) return;
5200 output_methods |= TO_X;
5202 set_default_configurations_for_x();
5203 x_initialised = YES;
5206 /* WARNING, this type not in Xlib spec */
5207 XSetErrorHandler(&x11_error_handler);
5208 XSetIOErrorHandler(&x11_ioerror_handler);
5212 static char **xargv = 0;
5213 static int xargc = 0;
5215 static void X11_create_window(void)
5217 if (output_methods & TO_X) {
5219 init_window(own_window, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5220 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, background_colour,
5222 #else /* OWN_WINDOW */
5223 init_window(0, text_width + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2,
5224 text_height + window.border_inner_margin * 2 + window.border_outer_margin * 2 + window.border_width * 2, set_transparent, 0,
5226 #endif /* OWN_WINDOW */
5230 update_text_area(); /* to position text/window on screen */
5233 if (own_window && !fixed_pos) {
5234 XMoveWindow(display, window.window, window.x, window.y);
5237 set_transparent_background(window.window);
5245 x11_stuff.region = XCreateRegion();
5247 if (!XDamageQueryExtension(display, &x11_stuff.event_base, &x11_stuff.error_base)) {
5248 NORM_ERR("Xdamage extension unavailable");
5250 x11_stuff.damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5251 x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5252 x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
5253 #endif /* HAVE_XDAMAGE */
5256 update_text_area(); /* to get initial size of the window */
5259 /* setup lua window globals */
5260 llua_setup_window_table(text_start_x, text_start_y, text_width, text_height);
5261 #endif /* HAVE_LUA */
5265 #define CONF_ERR NORM_ERR("%s: %d: config file error", f, line)
5266 #define CONF_ERR2(a) NORM_ERR("%s: %d: config file error: %s", f, line, a)
5267 #define CONF2(a) if (strcasecmp(name, a) == 0)
5268 #define CONF(a) else CONF2(a)
5269 #define CONF3(a, b) else if (strcasecmp(name, a) == 0 \
5270 || strcasecmp(name, b) == 0)
5271 #define CONF_CONTINUE 1
5272 #define CONF_BREAK 2
5273 #define CONF_BUFF_SIZE 512
5275 static FILE *open_config_file(const char *f)
5277 #ifdef CONFIG_OUTPUT
5278 if (!strcmp(f, "==builtin==")) {
5279 return conf_cookie_open();
5281 #endif /* CONFIG_OUTPUT */
5282 return fopen(f, "r");
5285 static int do_config_step(int *line, FILE *fp, char *buf, char **name, char **value)
5289 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5292 remove_comments(buf);
5297 while (*p && isspace((int) *p)) {
5301 return CONF_CONTINUE; /* empty line */
5308 while (*p2 && !isspace((int) *p2)) {
5312 *p2 = '\0'; /* break at name's end */
5319 while (*p && isspace((int) *p)) {
5325 p2 = *value + strlen(*value);
5326 while (isspace((int) *(p2 - 1))) {
5335 char load_config_file(const char *f)
5340 fp = open_config_file(f);
5344 DBGP("reading contents from config file '%s'", f);
5347 char buff[CONF_BUFF_SIZE], *name, *value;
5348 int ret = do_config_step(&line, fp, buff, &name, &value);
5349 if (ret == CONF_BREAK) {
5351 } else if (ret == CONF_CONTINUE) {
5357 /* don't listen if X is already initialised or
5358 * if we already know we don't want it */
5359 if(x_initialised != YES) {
5360 if (string_to_bool(value)) {
5361 output_methods &= TO_X;
5363 output_methods &= ~TO_X;
5364 x_initialised = NEVER;
5369 if (!value || x_initialised == YES) {
5374 disp = strdup(value);
5379 if (window.type == TYPE_DOCK)
5382 #endif /*OWN_WINDOW */
5384 int a = string_to_alignment(value);
5395 CONF("background") {
5396 fork_to_background = string_to_bool(value);
5399 CONF2("background") {
5400 fork_to_background = string_to_bool(value);
5404 CONF("show_graph_scale") {
5405 show_graph_scale = string_to_bool(value);
5407 CONF("show_graph_range") {
5408 show_graph_range = string_to_bool(value);
5410 CONF("border_inner_margin") {
5412 window.border_inner_margin = strtol(value, 0, 0);
5413 if (window.border_inner_margin < 0) window.border_inner_margin = 0;
5418 CONF("border_outer_margin") {
5420 window.border_outer_margin = strtol(value, 0, 0);
5421 if (window.border_outer_margin < 0) window.border_outer_margin = 0;
5426 CONF("border_width") {
5428 window.border_width = strtol(value, 0, 0);
5429 if (window.border_width < 0) window.border_width = 0;
5435 #define TEMPLATE_CONF(n) \
5436 CONF("template"#n) { \
5437 if (set_template(n, value)) \
5452 info.mail = parse_mail_args(IMAP_TYPE, value);
5459 info.mail = parse_mail_args(POP3_TYPE, value);
5464 CONF("default_bar_size") {
5467 if (sscanf(value, "%d %d", &default_bar_width, &default_bar_height) != 2) {
5474 CONF_ERR2("default_bar_size takes 2 integer arguments (ie. 'default_bar_size 0 6')")
5478 CONF("default_graph_size") {
5481 if (sscanf(value, "%d %d", &default_graph_width, &default_graph_height) != 2) {
5488 CONF_ERR2("default_graph_size takes 2 integer arguments (ie. 'default_graph_size 0 6')")
5491 CONF("default_gauge_size") {
5494 if (sscanf(value, "%d %d", &default_gauge_width, &default_gauge_height) != 2) {
5501 CONF_ERR2("default_gauge_size takes 2 integer arguments (ie. 'default_gauge_size 0 6')")
5508 mpd_set_host(value);
5514 if (value && mpd_set_port(value)) {
5518 CONF("mpd_password") {
5520 mpd_set_password(value, 0);
5526 CONF("music_player_interval") {
5528 info.music_player_interval = strtod(value, 0);
5534 CONF("sensor_device") {
5536 sensor_device = strtol(value, 0, 0);
5542 CONF("cpu_avg_samples") {
5544 cpu_avg_samples = strtol(value, 0, 0);
5545 if (cpu_avg_samples < 1 || cpu_avg_samples > 14) {
5548 info.cpu_avg_samples = cpu_avg_samples;
5554 CONF("net_avg_samples") {
5556 net_avg_samples = strtol(value, 0, 0);
5557 if (net_avg_samples < 1 || net_avg_samples > 14) {
5560 info.net_avg_samples = net_avg_samples;
5566 CONF("diskio_avg_samples") {
5568 diskio_avg_samples = strtol(value, 0, 0);
5569 if (diskio_avg_samples < 1 || diskio_avg_samples > 14) {
5572 info.diskio_avg_samples = diskio_avg_samples;
5580 CONF("double_buffer") {
5581 use_xdbe = string_to_bool(value);
5585 CONF("override_utf8_locale") {
5586 utf8_mode = string_to_bool(value);
5588 CONF("draw_borders") {
5589 draw_borders = string_to_bool(value);
5591 CONF("draw_graph_borders") {
5592 draw_graph_borders = string_to_bool(value);
5594 CONF("draw_shades") {
5595 draw_shades = string_to_bool(value);
5597 CONF("draw_outline") {
5598 draw_outline = string_to_bool(value);
5601 CONF("out_to_console") {
5602 if(string_to_bool(value)) {
5603 output_methods |= TO_STDOUT;
5605 output_methods &= ~TO_STDOUT;
5608 CONF("extra_newline") {
5609 extra_newline = string_to_bool(value);
5611 CONF("out_to_stderr") {
5612 if(string_to_bool(value))
5613 output_methods |= TO_STDERR;
5616 CONF("out_to_ncurses") {
5617 if(string_to_bool(value)) {
5620 output_methods |= TO_NCURSES;
5624 CONF("overwrite_file") {
5625 if(overwrite_file) {
5626 free(overwrite_file);
5629 if(overwrite_works(value)) {
5630 overwrite_file = strdup(value);
5631 output_methods |= OVERWRITE_FILE;
5633 NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value);
5635 CONF("append_file") {
5640 if(append_works(value)) {
5641 append_file = strdup(value);
5642 output_methods |= APPEND_FILE;
5644 NORM_ERR("append_file won't be able to create/append '%s'", value);
5646 CONF("use_spacer") {
5648 if (strcasecmp(value, "left") == EQUAL) {
5649 use_spacer = LEFT_SPACER;
5650 } else if (strcasecmp(value, "right") == EQUAL) {
5651 use_spacer = RIGHT_SPACER;
5652 } else if (strcasecmp(value, "none") == EQUAL) {
5653 use_spacer = NO_SPACER;
5655 use_spacer = string_to_bool(value);
5656 NORM_ERR("use_spacer should have an argument of left, right, or"
5657 " none. '%s' seems to be some form of '%s', so"
5658 " defaulting to %s.", value,
5659 use_spacer ? "true" : "false",
5660 use_spacer ? "right" : "none");
5662 use_spacer = RIGHT_SPACER;
5664 use_spacer = NO_SPACER;
5668 NORM_ERR("use_spacer should have an argument. Defaulting to right.");
5669 use_spacer = RIGHT_SPACER;
5675 use_xft = string_to_bool(value);
5679 set_first_font(value);
5683 if (value && font_count >= 0) {
5684 fonts[0].font_alpha = atof(value) * 65535.0;
5691 if (string_to_bool(value)) {
5692 NORM_ERR("Xft not enabled at compile time");
5696 /* xftfont silently ignored when no Xft */
5699 /* xftalpha is silently ignored when no Xft */
5704 set_first_font(value);
5712 gap_x = atoi(value);
5719 gap_y = atoi(value);
5725 CONF("mail_spool") {
5729 variable_substitute(value, buffer, 256);
5731 if (buffer[0] != '\0') {
5732 if (current_mail_spool) {
5733 free(current_mail_spool);
5735 current_mail_spool = strndup(buffer, text_buffer_size);
5742 CONF("minimum_size") {
5744 if (sscanf(value, "%d %d", &minimum_width, &minimum_height)
5746 if (sscanf(value, "%d", &minimum_width) != 1) {
5754 CONF("maximum_width") {
5756 if (sscanf(value, "%d", &maximum_width) != 1) {
5764 CONF("no_buffers") {
5765 no_buffers = string_to_bool(value);
5767 CONF("top_name_width") {
5769 if (sscanf(value, "%u", &top_name_width) != 1) {
5775 if (top_name_width >= max_user_text) {
5776 top_name_width = max_user_text - 1;
5779 CONF("top_cpu_separate") {
5780 cpu_separate = string_to_bool(value);
5782 CONF("short_units") {
5783 short_units = string_to_bool(value);
5785 CONF("format_human_readable") {
5786 format_human_readable = string_to_bool(value);
5789 CONF("hddtemp_host") {
5790 set_hddtemp_host(value);
5792 CONF("hddtemp_port") {
5793 set_hddtemp_port(value);
5795 #endif /* HDDTEMP */
5796 CONF("pad_percents") {
5797 pad_percents = atoi(value);
5801 CONF("own_window") {
5803 own_window = string_to_bool(value);
5806 CONF("own_window_class") {
5808 memset(window.class_name, 0, sizeof(window.class_name));
5809 strncpy(window.class_name, value,
5810 sizeof(window.class_name) - 1);
5813 CONF("own_window_title") {
5815 memset(window.title, 0, sizeof(window.title));
5816 strncpy(window.title, value, sizeof(window.title) - 1);
5819 CONF("own_window_transparent") {
5821 set_transparent = string_to_bool(value);
5824 CONF("own_window_hints") {
5826 char *p_hint, *p_save;
5827 char delim[] = ", ";
5829 /* tokenize the value into individual hints */
5830 if ((p_hint = strtok_r(value, delim, &p_save)) != NULL) {
5832 /* fprintf(stderr, "hint [%s] parsed\n", p_hint); */
5833 if (strncmp(p_hint, "undecorate", 10) == EQUAL) {
5834 SET_HINT(window.hints, HINT_UNDECORATED);
5835 } else if (strncmp(p_hint, "below", 5) == EQUAL) {
5836 SET_HINT(window.hints, HINT_BELOW);
5837 } else if (strncmp(p_hint, "above", 5) == EQUAL) {
5838 SET_HINT(window.hints, HINT_ABOVE);
5839 } else if (strncmp(p_hint, "sticky", 6) == EQUAL) {
5840 SET_HINT(window.hints, HINT_STICKY);
5841 } else if (strncmp(p_hint, "skip_taskbar", 12) == EQUAL) {
5842 SET_HINT(window.hints, HINT_SKIP_TASKBAR);
5843 } else if (strncmp(p_hint, "skip_pager", 10) == EQUAL) {
5844 SET_HINT(window.hints, HINT_SKIP_PAGER);
5849 p_hint = strtok_r(NULL, delim, &p_save);
5850 } while (p_hint != NULL);
5856 CONF("own_window_type") {
5858 if (strncmp(value, "normal", 6) == EQUAL) {
5859 window.type = TYPE_NORMAL;
5860 } else if (strncmp(value, "desktop", 7) == EQUAL) {
5861 window.type = TYPE_DESKTOP;
5862 } else if (strncmp(value, "dock", 4) == EQUAL) {
5863 window.type = TYPE_DOCK;
5864 text_alignment = TOP_LEFT;
5865 } else if (strncmp(value, "panel", 5) == EQUAL) {
5866 window.type = TYPE_PANEL;
5867 } else if (strncmp(value, "override", 8) == EQUAL) {
5868 window.type = TYPE_OVERRIDE;
5877 CONF("stippled_borders") {
5879 stippled_borders = strtol(value, 0, 0);
5881 stippled_borders = 4;
5885 CONF("imlib_cache_size") {
5887 cimlib_set_cache_size(atoi(value));
5890 CONF("imlib_cache_flush_interval") {
5892 cimlib_set_cache_flush_interval(atoi(value));
5897 CONF("update_interval_on_battery") {
5899 update_interval_bat = strtod(value, 0);
5904 CONF("update_interval") {
5906 set_update_interval(strtod(value, 0));
5910 if (info.music_player_interval == 0) {
5911 // default to update_interval
5912 info.music_player_interval = update_interval;
5915 CONF("total_run_times") {
5917 total_run_times = strtod(value, 0);
5923 stuff_in_uppercase = string_to_bool(value);
5925 CONF("max_specials") {
5927 max_specials = atoi(value);
5932 CONF("max_user_text") {
5934 max_user_text = atoi(value);
5939 CONF("text_buffer_size") {
5941 text_buffer_size = atoi(value);
5942 if (text_buffer_size < DEFAULT_TEXT_BUFFER_SIZE) {
5943 NORM_ERR("text_buffer_size must be >=%i bytes", DEFAULT_TEXT_BUFFER_SIZE);
5944 text_buffer_size = DEFAULT_TEXT_BUFFER_SIZE;
5952 if (output_methods & TO_X) {
5953 X11_initialisation();
5962 global_text = (char *) malloc(1);
5963 global_text[0] = '\0';
5966 unsigned int l = strlen(global_text);
5968 char buf[CONF_BUFF_SIZE];
5970 if (fgets(buf, CONF_BUFF_SIZE, fp) == NULL) {
5976 if (bl >= 2 && buf[bl-2] == '\\' && buf[bl-1] == '\n') {
5984 /* Check for continuation of \\-\n. */
5985 if (l > 0 && buf[0] == '\n' && global_text[l-1] == '\\') {
5986 global_text[l-1] = '\0';
5990 global_text = (char *) realloc(global_text, l + bl + 1);
5991 strcat(global_text, buf);
5993 if (strlen(global_text) > max_user_text) {
5998 if (strlen(global_text) < 1) {
5999 CRIT_ERR(NULL, NULL, "no text supplied in configuration; exiting");
6001 global_text_lines = line + 1;
6004 #ifdef TCP_PORT_MONITOR
6005 CONF("max_port_monitor_connections") {
6007 if (!value || (sscanf(value, "%d", &max) != 1)) {
6008 /* an error. use default, warn and continue. */
6009 tcp_portmon_set_max_connections(0);
6011 } else if (tcp_portmon_set_max_connections(max)) {
6012 /* max is < 0, default has been set*/
6017 CONF("if_up_strictness") {
6019 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6020 ifup_strictness = IFUP_UP;
6021 } else if (strcasecmp(value, "up") == EQUAL) {
6022 ifup_strictness = IFUP_UP;
6023 } else if (strcasecmp(value, "link") == EQUAL) {
6024 ifup_strictness = IFUP_LINK;
6025 } else if (strcasecmp(value, "address") == EQUAL) {
6026 ifup_strictness = IFUP_ADDR;
6028 NORM_ERR("incorrect if_up_strictness value, defaulting to 'up'");
6029 ifup_strictness = IFUP_UP;
6033 CONF("temperature_unit") {
6035 NORM_ERR("config option 'temperature_unit' needs an argument, either 'celsius' or 'fahrenheit'");
6036 } else if (set_temp_output_unit(value)) {
6037 NORM_ERR("temperature_unit: incorrect argument");
6044 char *ptr = strtok(value, " ");
6047 ptr = strtok(NULL, " ");
6054 CONF("lua_draw_hook_pre") {
6056 llua_set_draw_pre_hook(value);
6061 CONF("lua_draw_hook_post") {
6063 llua_set_draw_post_hook(value);
6068 CONF("lua_startup_hook") {
6070 llua_set_startup_hook(value);
6075 CONF("lua_shutdown_hook") {
6077 llua_set_shutdown_hook(value);
6083 #endif /* HAVE_LUA */
6095 CONF("default_color"){}
6096 CONF3("default_shade_color", "default_shadecolor"){}
6097 CONF3("default_outline_color", "default_outlinecolor") {}
6098 CONF("own_window_colour") {}
6101 NORM_ERR("%s: %d: no such configuration: '%s'", f, line, name);
6107 if (info.music_player_interval == 0) {
6108 // default to update_interval
6109 info.music_player_interval = update_interval;
6111 if (!global_text) { // didn't supply any text
6112 CRIT_ERR(NULL, NULL, "missing text block in configuration; exiting");
6118 static void load_config_file_x11(const char *f)
6123 fp = open_config_file(f);
6127 DBGP("reading contents from config file '%s'", f);
6130 char buff[CONF_BUFF_SIZE], *name, *value;
6131 int ret = do_config_step(&line, fp, buff, &name, &value);
6132 if (ret == CONF_BREAK) {
6134 } else if (ret == CONF_CONTINUE) {
6139 X11_initialisation();
6140 if (x_initialised == YES) {
6142 color0 = get_x11_color(value);
6149 X11_initialisation();
6150 if (x_initialised == YES) {
6152 color1 = get_x11_color(value);
6159 X11_initialisation();
6160 if (x_initialised == YES) {
6162 color2 = get_x11_color(value);
6169 X11_initialisation();
6170 if (x_initialised == YES) {
6172 color3 = get_x11_color(value);
6179 X11_initialisation();
6180 if (x_initialised == YES) {
6182 color4 = get_x11_color(value);
6189 X11_initialisation();
6190 if (x_initialised == YES) {
6192 color5 = get_x11_color(value);
6199 X11_initialisation();
6200 if (x_initialised == YES) {
6202 color6 = get_x11_color(value);
6209 X11_initialisation();
6210 if (x_initialised == YES) {
6212 color7 = get_x11_color(value);
6219 X11_initialisation();
6220 if (x_initialised == YES) {
6222 color8 = get_x11_color(value);
6229 X11_initialisation();
6230 if (x_initialised == YES) {
6232 color9 = get_x11_color(value);
6238 CONF("default_color") {
6239 X11_initialisation();
6240 if (x_initialised == YES) {
6242 default_fg_color = get_x11_color(value);
6248 CONF3("default_shade_color", "default_shadecolor") {
6249 X11_initialisation();
6250 if (x_initialised == YES) {
6252 default_bg_color = get_x11_color(value);
6258 CONF3("default_outline_color", "default_outlinecolor") {
6259 X11_initialisation();
6260 if (x_initialised == YES) {
6262 default_out_color = get_x11_color(value);
6269 CONF("own_window_colour") {
6270 X11_initialisation();
6271 if (x_initialised == YES) {
6273 background_colour = get_x11_color(value);
6275 NORM_ERR("Invalid colour for own_window_colour (try omitting the "
6276 "'#' for hex colours");
6282 /* initialize X11 if nothing X11-related is mentioned before TEXT (and if X11 is the default outputmethod) */
6283 if(output_methods & TO_X) {
6284 X11_initialisation();
6293 #undef CONF_CONTINUE
6294 #undef CONF_BUFF_SIZE
6302 static void print_help(const char *prog_name) {
6303 printf("Usage: %s [OPTION]...\n"
6304 PACKAGE_NAME" is a system monitor that renders text on desktop or to own transparent\n"
6305 "window. Command line options will override configurations defined in config\n"
6307 " -v, --version version\n"
6308 " -q, --quiet quiet mode\n"
6309 " -D, --debug increase debugging output, ie. -DD for more debugging\n"
6310 " -c, --config=FILE config file to load\n"
6311 #ifdef CONFIG_OUTPUT
6312 " -C, --print-config print the builtin default config to stdout\n"
6313 " e.g. 'conky -C > ~/.conkyrc' will create a new default config\n"
6315 " -d, --daemonize daemonize, fork to background\n"
6316 " -h, --help help\n"
6318 " -a, --alignment=ALIGNMENT text alignment on screen, {top,bottom,middle}_{left,right,middle}\n"
6319 " -f, --font=FONT font to use\n"
6320 " -X, --display=DISPLAY X11 display to use\n"
6322 " -o, --own-window create own window to draw\n"
6325 " -b, --double-buffer double buffer (prevents flickering)\n"
6327 " -w, --window-id=WIN_ID window id to draw\n"
6328 " -x X x position\n"
6329 " -y Y y position\n"
6331 " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n"
6332 " -u, --interval=SECS update interval\n"
6333 " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n",
6338 /* : means that character before that takes an argument */
6339 static const char *getopt_string = "vVqdDt:u:i:hc:"
6349 #ifdef CONFIG_OUTPUT
6354 static const struct option longopts[] = {
6355 { "help", 0, NULL, 'h' },
6356 { "version", 0, NULL, 'V' },
6357 { "debug", 0, NULL, 'D' },
6358 { "config", 1, NULL, 'c' },
6359 #ifdef CONFIG_OUTPUT
6360 { "print-config", 0, NULL, 'C' },
6362 { "daemonize", 0, NULL, 'd' },
6364 { "alignment", 1, NULL, 'a' },
6365 { "font", 1, NULL, 'f' },
6366 { "display", 1, NULL, 'X' },
6368 { "own-window", 0, NULL, 'o' },
6371 { "double-buffer", 0, NULL, 'b' },
6373 { "window-id", 1, NULL, 'w' },
6375 { "text", 1, NULL, 't' },
6376 { "interval", 0, NULL, 'u' },
6380 void initialisation(int argc, char **argv) {
6381 struct sigaction act, oact;
6383 set_default_configurations();
6384 load_config_file(current_config);
6385 currentconffile = conftree_add(currentconffile, current_config);
6387 /* init specials array */
6388 if ((specials = calloc(sizeof(struct special_t), max_specials)) == 0) {
6389 NORM_ERR("failed to create specials array");
6393 if (current_mail_spool == NULL) {
6396 variable_substitute(MAIL_FILE, buf, 256);
6398 if (buf[0] != '\0') {
6399 current_mail_spool = strndup(buf, text_buffer_size);
6404 /* handle other command line arguments */
6406 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \
6407 || defined(__NetBSD__)
6408 optind = optreset = 1;
6413 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6414 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY,
6415 "kvm_open")) == NULL) {
6416 CRIT_ERR(NULL, NULL, "cannot read kvm");
6421 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6429 fork_to_background = 1;
6432 global_debug_level++;
6436 set_first_font(optarg);
6439 text_alignment = string_to_alignment(optarg);
6458 global_text = strndup(optarg, max_user_text);
6459 convert_escapes(global_text);
6463 update_interval = strtod(optarg, 0);
6464 update_interval_old = update_interval;
6465 if (info.music_player_interval == 0) {
6466 // default to update_interval
6467 info.music_player_interval = update_interval;
6472 total_run_times = strtod(optarg, 0);
6476 gap_x = atoi(optarg);
6480 gap_y = atoi(optarg);
6491 if (output_methods & TO_X) {
6492 load_config_file_x11(current_config);
6496 /* generate text and get initial size */
6497 extract_variable_text(global_text);
6504 if (fork_to_background) {
6509 NORM_ERR(PACKAGE_NAME": couldn't fork() to background: %s",
6516 fprintf(stderr, "\n");
6521 /* parent process */
6522 fprintf(stderr, PACKAGE_NAME": forked to background, pid is %d\n",
6529 text_buffer = malloc(max_user_text);
6530 memset(text_buffer, 0, max_user_text);
6531 tmpstring1 = malloc(text_buffer_size);
6532 memset(tmpstring1, 0, text_buffer_size);
6533 tmpstring2 = malloc(text_buffer_size);
6534 memset(tmpstring2, 0, text_buffer_size);
6539 X11_create_window();
6542 llua_setup_info(&info, update_interval);
6543 #endif /* HAVE_LUA */
6548 /* Set signal handlers */
6549 act.sa_handler = signal_handler;
6550 sigemptyset(&act.sa_mask);
6553 act.sa_flags |= SA_RESTART;
6556 if ( sigaction(SIGINT, &act, &oact) < 0
6557 || sigaction(SIGALRM, &act, &oact) < 0
6558 || sigaction(SIGUSR1, &act, &oact) < 0
6559 || sigaction(SIGHUP, &act, &oact) < 0
6560 || sigaction(SIGTERM, &act, &oact) < 0) {
6561 NORM_ERR("error setting signal handler: %s", strerror(errno));
6565 llua_startup_hook();
6566 #endif /* HAVE_LUA */
6569 int main(int argc, char **argv)
6578 g_signal_pending = 0;
6579 max_user_text = MAX_USER_TEXT_DEFAULT;
6581 memset(&info, 0, sizeof(info));
6585 #ifdef TCP_PORT_MONITOR
6586 /* set default connection limit */
6587 tcp_portmon_set_max_connections(0);
6590 /* handle command line parameters that don't change configs */
6592 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) && *s)
6593 || ((s = getenv("LANG")) && *s)) {
6594 temp = (char *) malloc((strlen(s) + 1) * sizeof(char));
6596 NORM_ERR("malloc failed");
6598 for (x = 0; x < strlen(s); x++) {
6599 temp[x] = tolower(s[x]);
6602 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6608 if (!setlocale(LC_CTYPE, "")) {
6609 NORM_ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6613 int c = getopt_long(argc, argv, getopt_string, longopts, NULL);
6624 if (current_config) {
6625 free(current_config);
6627 current_config = strndup(optarg, max_user_text);
6630 freopen("/dev/null", "w", stderr);
6633 print_help(argv[0]);
6635 #ifdef CONFIG_OUTPUT
6642 window.window = strtol(optarg, 0, 0);
6647 disp = strdup(optarg);
6656 /* check if specified config file is valid */
6657 if (current_config) {
6659 if (stat(current_config, &sb) ||
6660 (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) {
6661 NORM_ERR("invalid configuration file '%s'\n", current_config);
6662 free(current_config);
6667 /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */
6669 if (!current_config) {
6670 /* load default config file */
6671 char buf[DEFAULT_TEXT_BUFFER_SIZE];
6674 /* Try to use personal config file first */
6675 to_real_path(buf, CONFIG_FILE);
6676 if (buf[0] && (fp = fopen(buf, "r"))) {
6677 current_config = strndup(buf, max_user_text);
6681 /* Try to use system config file if personal config not readable */
6682 if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) {
6683 current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text);
6687 /* No readable config found */
6688 if (!current_config) {
6689 #ifdef CONFIG_OUTPUT
6690 current_config = strdup("==builtin==");
6691 NORM_ERR("no readable personal or system-wide config file found,"
6692 " using builtin default");
6694 CRIT_ERR(NULL, NULL, "no readable personal or system-wide config file found");
6695 #endif /* ! CONF_OUTPUT */
6700 /* Load xoap keys, if existing */
6704 #ifdef HAVE_SYS_INOTIFY_H
6705 inotify_fd = inotify_init();
6706 #endif /* HAVE_SYS_INOTIFY_H */
6708 initialisation(argc, argv);
6712 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
6720 void alarm_handler(void) {
6722 kill(childpid, SIGTERM);
6726 static void signal_handler(int sig)
6728 /* signal handler is light as a feather, as it should be.
6729 * we will poll g_signal_pending with each loop of conky
6730 * and do any signal processing there, NOT here (except
6731 * SIGALRM because this is caused when conky is hanging) */
6732 if(sig == SIGALRM) {
6735 g_signal_pending = sig;