2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
28 #include <X11/Xutil.h>
30 #include <sys/types.h>
33 #define CONFIG_FILE "$HOME/.conkyrc"
34 #define MAIL_FILE "$MAIL"
35 #define MAX_IF_BLOCK_DEPTH 5
37 /* #define SIGNAL_BLOCKING */
38 #undef SIGNAL_BLOCKING
55 char name[TEXT_BUFFER_SIZE];
65 static int selected_font = 0;
66 static int font_count = -1;
67 struct font_list *fonts = NULL;
71 #define font_height() use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
72 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
73 #define font_ascent() use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent
74 #define font_descent() use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent
78 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
79 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
80 #define font_descent() fonts[selected_font].font->max_bounds.descent
84 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
87 static void set_font();
89 int addfont(const char *data_in)
91 if (font_count > MAX_FONTS) {
92 CRIT_ERR("you don't need that many fonts, sorry.");
95 if (font_count == 0) {
99 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
103 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
105 CRIT_ERR("realloc in addfont");
107 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
108 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
110 fonts[font_count].font_alpha = 0xffff;
113 CRIT_ERR("Oops...looks like something overflowed in addfont().");
118 void set_first_font(const char *data_in)
120 if (font_count < 0) {
121 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
126 if (strlen(data_in) > 1) {
127 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
129 fonts[0].font_alpha = 0xffff;
137 for (i=0;i<=font_count;i++) {
140 XftFontClose(display, fonts[i].xftfont);
144 XFreeFont(display, fonts[i].font);
151 set_first_font("6x10");
155 static void load_fonts()
158 for (i=0;i<=font_count;i++) {
162 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
163 XftFontClose(display, fonts[i].xftfont);
165 if ((fonts[i].xftfont =
166 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
169 ERR("can't load Xft font '%s'", fonts[i].name);
170 if ((fonts[i].xftfont =
171 XftFontOpenName(display, screen,
172 "courier-12")) != NULL)
175 ERR("can't load Xft font '%s'", "courier-12");
177 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
178 CRIT_ERR("can't load font '%s'", "fixed");
185 /* load normal font */
186 /* if (fonts[i].font != NULL)
187 XFreeFont(display, fonts[i].font);*/
189 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
190 ERR("can't load font '%s'", fonts[i].name);
191 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
192 CRIT_ERR("can't load font '%s'", "fixed");
193 printf("loaded fixed?\n");
201 /* default config file */
202 static char *current_config;
204 /* set to 1 if you want all text to be in uppercase */
205 static unsigned int stuff_in_upper_case;
207 /* Update interval */
208 static double update_interval;
210 /* Run how many times? */
211 static unsigned long total_run_times;
214 static int fork_to_background;
216 static int cpu_avg_samples, net_avg_samples;
220 /* Always on bottom */
221 static int on_bottom;
223 /* Position on the screen */
224 static int text_alignment;
225 static int gap_x, gap_y;
228 static int draw_borders;
229 static int stippled_borders;
231 static int draw_shades, draw_outline;
233 static int border_margin, border_width;
235 static long default_fg_color, default_bg_color, default_out_color;
237 /* create own window or draw stuff to root? */
238 static int set_transparent = 0;
242 static int own_window = 0;
243 static int background_colour = 0;
244 static char wm_class_name[256];
245 /* fixed size/pos is set if wm/user changes them */
246 static int fixed_size = 0, fixed_pos = 0;
249 static int minimum_width, minimum_height;
250 static int maximum_width;
257 /* no buffers in used memory? */
260 /* pad percentages to decimals? */
261 static int pad_percents = 0;
263 #ifdef TCP_PORT_MONITOR
264 tcp_port_monitor_collection_args_t tcp_port_monitor_collection_args;
265 tcp_port_monitor_args_t tcp_port_monitor_args;
268 /* Text that is shown */
269 static char original_text[] =
270 "$nodename - $sysname $kernel on $machine\n"
272 "${color grey}Uptime:$color $uptime\n"
273 "${color grey}Frequency (in MHz):$color $freq\n"
274 "${color grey}Frequency (in GHz):$color $freq_g\n"
275 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
276 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
277 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
278 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
280 "${color grey}File systems:\n"
281 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
282 "${color grey}Networking:\n"
283 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
284 "${color grey}Temperatures:\n"
285 " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
288 "${color grey}SETI@Home Statistics:\n"
289 "${color grey}Seti Unit Number:$color $seti_credit\n"
290 "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
293 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
294 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
296 "${color grey}Name PID CPU% MEM%\n"
297 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
298 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
299 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
300 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
301 "${tail /var/log/Xorg.0.log 3}";
303 static char *text = original_text;
305 static int total_updates;
308 static int blockdepth = 0;
309 static int if_jumped = 0;
310 static int blockstart[MAX_IF_BLOCK_DEPTH];
312 int check_mount(char *s)
314 #if defined(__linux__)
316 FILE *mtab = fopen("/etc/mtab", "r");
318 char buf1[256], buf2[128];
319 while (fgets(buf1, 256, mtab)) {
320 sscanf(buf1, "%*s %128s", buf2);
321 if (!strcmp(s, buf2)) {
328 ERR("Could not open mtab");
331 #elif defined(__FreeBSD__)
332 struct statfs *mntbuf;
335 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
336 for (i = mntsize - 1; i >= 0; i--)
337 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
346 static inline int calc_text_width(const char *s, int l)
352 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
354 XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
360 return XTextWidth(fonts[selected_font].font, s, l);
365 /* formatted text to render on screen, generated in generate_text(),
366 * drawn in draw_stuff() */
368 static char text_buffer[TEXT_BUFFER_SIZE * 4];
370 /* special stuff in text_buffer */
372 #define SPECIAL_CHAR '\x01'
389 static struct special_t {
399 unsigned long first_colour; // for graph gradient
400 unsigned long last_colour;
403 static int special_count;
405 static int special_index; /* used when drawing */
408 #define MAX_GRAPH_DEPTH 256 /* why 256? cause an array of more then 256 doubles seems excessive, and who needs that kind of precision anyway? */
410 static struct special_t *new_special(char *buf, int t)
412 if (special_count >= 128)
413 CRIT_ERR("too many special things in text");
415 buf[0] = SPECIAL_CHAR;
417 specials[special_count].type = t;
418 return &specials[special_count++];
421 typedef struct tailstring_list {
422 char data[TEXT_BUFFER_SIZE];
423 struct tailstring_list *next;
424 struct tailstring_list *first;
427 void addtail(tailstring ** head, char *data_in)
430 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
436 tmp->first = (*head)->first;
438 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
443 void freetail(tailstring * head)
446 while (head != NULL) {
453 void freelasttail(tailstring * head)
455 tailstring * tmp = head;
457 if (tmp->next == head->first) {
464 while(head != NULL && tmp != NULL) {
470 static void new_bar(char *buf, int w, int h, int usage)
472 struct special_t *s = new_special(buf, BAR);
473 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
478 static const char *scan_bar(const char *args, int *w, int *h)
480 *w = 0; /* zero width means all space that is available */
482 /* bar's argument is either height or height,width */
485 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
486 sscanf(args, "%d %n", h, &n);
493 static char *scan_font(const char *args)
495 if (args && sizeof(args) < 127) {
499 ERR("font scan failed, lets hope it doesn't mess stuff up");
505 static void new_font(char *buf, char * args) {
506 struct special_t *s = new_special(buf, FONT);
507 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
508 int tmp = selected_font;
509 selected_font = s->font_added = addfont(args);
517 inline void graph_append(struct special_t *graph, double f)
519 if (!graph->scaled && f > graph->graph_scale) {
520 f = graph->graph_scale;
524 graph->graph_scale = 1;
526 graph->graph[graph->graph_width - 1] = f; /* add new data */
527 for (i = 0; i < graph->graph_width - 1; i++) { /* shift all the data by 1 */
528 graph->graph[i] = graph->graph[i + 1];
529 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
530 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
535 short colour_depth = 0;
536 void set_up_gradient();
538 /* precalculated: 31/255, and 63/255 */
539 #define CONST_8_TO_5_BITS 0.12156862745098
540 #define CONST_8_TO_6_BITS 0.247058823529412
542 /* adjust color values depending on color depth*/
543 static unsigned int adjust_colors(unsigned int color)
546 if (colour_depth == 0) {
549 if (colour_depth == 16) {
550 r = (color & 0xff0000) >> 16;
551 g = (color & 0xff00) >> 8;
553 color = (int)(r * CONST_8_TO_5_BITS) << 11;
554 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
555 color |= (int)(b * CONST_8_TO_5_BITS);
560 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
562 struct special_t *s = new_special(buf, GRAPH);
564 if (s->graph == NULL) {
565 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
566 s->graph_width = s->width - 3; // subtract 3 for the box
568 s->graph_width = MAX_GRAPH_DEPTH - 3;
570 s->graph = malloc(s->graph_width * sizeof(double));
571 memset(s->graph, 0, s->graph_width * sizeof(double));
572 s->graph_scale = 100;
575 s->first_colour = adjust_colors(first_colour);
576 s->last_colour = adjust_colors(second_colour);
583 s->graph_width = s->width - 3; // subtract 3 for rectangle around
588 s->graph_scale = scale;
595 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
597 *w = 0; /* zero width means all space that is available */
601 /* graph's argument is either height or height,width */
603 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
604 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
606 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
608 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
611 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
615 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
618 if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
622 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
625 if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
629 if (sscanf(args, "%d,%d", h, w) < 2) {
632 sscanf(args, "%*s %d,%d", h, w);
638 static inline void new_hr(char *buf, int a)
640 new_special(buf, HORIZONTAL_LINE)->height = a;
643 static inline void new_stippled_hr(char *buf, int a, int b)
645 struct special_t *s = new_special(buf, STIPPLED_HR);
650 static inline void new_fg(char *buf, long c)
652 new_special(buf, FG)->arg = c;
655 static inline void new_bg(char *buf, long c)
657 new_special(buf, BG)->arg = c;
660 static inline void new_outline(char *buf, long c)
662 new_special(buf, OUTLINE)->arg = c;
665 static inline void new_offset(char *buf, long c)
667 new_special(buf, OFFSET)->arg = c;
670 static inline void new_voffset(char *buf, long c)
672 new_special(buf, VOFFSET)->arg = c;
675 static inline void new_alignr(char *buf, long c)
677 new_special(buf, ALIGNR)->arg = c;
680 static inline void new_alignc(char *buf, long c)
682 new_special(buf, ALIGNC)->arg = c;
685 /* quite boring functions */
687 static inline void for_each_line(char *b, void (*f) (char *))
691 for (ps = b, pe = b; *pe; pe++) {
704 static void convert_escapes(char *buf)
706 char *p = buf, *s = buf;
722 /* converts from bytes to human readable format (k, M, G, T) */
723 static void human_readable(long long a, char *buf, int size)
725 // Strange conditional due to possible overflows
726 if(a / 1024 / 1024 / 1024.0 > 1024.0){
727 snprintf(buf, size, "%.2fT", (a / 1024 / 1024 / 1024) / 1024.0);
729 else if (a >= 1024 * 1024 * 1024) {
730 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
732 else if (a >= 1024 * 1024) {
733 double m = (a / 1024) / 1024.0;
735 snprintf(buf, size, "%.0fM", m);
737 snprintf(buf, size, "%.1fM", m);
738 } else if (a >= 1024)
739 snprintf(buf, size, "%Ldk", a / (long long) 1024);
741 snprintf(buf, size, "%Ld", a);
746 enum text_object_type {
794 #if defined(__linux__)
800 OBJ_i8k_left_fan_status,
801 OBJ_i8k_right_fan_status,
802 OBJ_i8k_left_fan_rpm,
803 OBJ_i8k_right_fan_rpm,
805 OBJ_i8k_buttons_status,
806 #endif /* __linux__ */
833 OBJ_ml_upload_counter,
834 OBJ_ml_download_counter,
835 OBJ_ml_nshared_files,
836 OBJ_ml_shared_counter,
837 OBJ_ml_tcp_upload_rate,
838 OBJ_ml_tcp_download_rate,
839 OBJ_ml_udp_upload_rate,
840 OBJ_ml_udp_download_rate,
841 OBJ_ml_ndownloaded_files,
842 OBJ_ml_ndownloading_files,
845 OBJ_running_processes,
854 OBJ_temp1, /* i2c is used instead in these */
867 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
869 OBJ_apm_battery_time,
870 OBJ_apm_battery_life,
871 #endif /* __FreeBSD__ */
902 #ifdef TCP_PORT_MONITOR
910 unsigned int c, d, e;
913 char *s; /* some string */
914 int i; /* some integer */
915 long l; /* some other integer */
916 struct net_stat *net;
918 unsigned char loadavg[3];
919 //unsigned int diskio;
920 unsigned int cpu_index;
966 #ifdef TCP_PORT_MONITOR
968 in_port_t port_range_begin; /* starting port to monitor */
969 in_port_t port_range_end; /* ending port to monitor */
970 int item; /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
971 int connection_index; /* 0 to n-1 connections. */
977 struct text_object_list {
978 unsigned int text_object_count;
979 struct text_object *text_objects;
982 static unsigned int text_object_count;
983 static struct text_object *text_objects;
984 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
986 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
988 void *threaded_exec( struct text_object *obj ) {
989 char *p2 = obj->data.execi.buffer;
990 FILE *fp = popen(obj->data.execi.cmd,"r");
991 pthread_mutex_lock( &mutex1 );
992 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
995 if (n2 && p2[n2 - 1] == '\n')
1003 pthread_mutex_unlock( &mutex1 );
1007 static struct text_object *new_text_object_internal()
1009 struct text_object *obj = malloc(sizeof(struct text_object));
1010 memset(obj, 0, sizeof(struct text_object));
1017 if (mlconfig.mldonkey_hostname) {
1018 free(mlconfig.mldonkey_hostname);
1019 mlconfig.mldonkey_hostname = NULL;
1024 static void free_text_objects(unsigned int count, struct text_object *objs)
1027 for (i = 0; i < count; i++) {
1028 switch (objs[i].type) {
1030 close(objs[i].data.i);
1033 close(objs[i].data.i);
1036 close(objs[i].data.i2c.fd);
1039 free(objs[i].data.s);
1042 case OBJ_if_existing:
1043 case OBJ_if_mounted:
1044 case OBJ_if_running:
1045 free(objs[i].data.ifblock.s);
1048 free(objs[i].data.tail.logfile);
1049 free(objs[i].data.tail.buffer);
1051 case OBJ_text: case OBJ_font:
1052 free(objs[i].data.s);
1055 free(objs[i].data.s);
1058 free(objs[i].data.s);
1061 free(objs[i].data.s);
1063 /* case OBJ_execibar:
1064 free(objs[i].data.s);
1066 case OBJ_execigraph:
1067 free(objs[i].data.s);
1071 case OBJ_mpd_artist:
1073 case OBJ_mpd_random:
1074 case OBJ_mpd_repeat:
1076 case OBJ_mpd_status:
1080 case OBJ_bmpx_title:
1081 case OBJ_bmpx_artist:
1082 case OBJ_bmpx_album:
1083 case OBJ_bmpx_track:
1085 case OBJ_bmpx_bitrate:
1089 free(objs[i].data.s);
1093 free(objs[i].data.execi.cmd);
1094 free(objs[i].data.execi.buffer);
1097 free(objs[i].data.execi.cmd);
1098 free(objs[i].data.execi.buffer);
1101 if (info.first_process) {
1102 free_all_processes();
1103 info.first_process = NULL;
1107 if (info.first_process) {
1108 free_all_processes();
1109 info.first_process = NULL;
1115 //text_objects = NULL;
1116 //text_object_count = 0;
1119 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1124 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1125 *a = mixer_init(buf1);
1126 (void) scan_bar(arg + n, w, h);
1129 (void) scan_bar(arg, w, h);
1134 /* construct_text_object() creates a new text_object */
1135 static struct text_object *construct_text_object(const char *s, const char *arg)
1137 //struct text_object *obj = new_text_object();
1138 struct text_object *obj = new_text_object_internal();
1140 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
1141 #define END ; } } else
1145 obj->type = OBJ_color;
1146 obj->data.l = get_x11_color(s);
1149 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
1150 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
1151 END OBJ(acpiacadapter, 0)
1154 END OBJ(freq_dyn, 0);
1155 END OBJ(freq_dyn_g, 0);
1156 END OBJ(acpifan, 0);
1157 END OBJ(battery, 0);
1160 sscanf(arg, "%63s", bat);
1162 strcpy(bat, "BAT0");
1163 obj->data.s = strdup(bat);
1164 #if defined(__linux__)
1165 END OBJ(i8k_version, INFO_I8K)
1166 END OBJ(i8k_bios, INFO_I8K)
1167 END OBJ(i8k_serial, INFO_I8K)
1168 END OBJ(i8k_cpu_temp, INFO_I8K)
1169 END OBJ(i8k_cpu_tempf, INFO_I8K)
1170 END OBJ(i8k_left_fan_status, INFO_I8K)
1171 END OBJ(i8k_right_fan_status, INFO_I8K)
1172 END OBJ(i8k_left_fan_rpm, INFO_I8K)
1173 END OBJ(i8k_right_fan_rpm, INFO_I8K)
1174 END OBJ(i8k_ac_status, INFO_I8K)
1175 END OBJ(i8k_buttons_status, INFO_I8K)
1176 #endif /* __linux__ */
1177 END OBJ(buffers, INFO_BUFFERS)
1178 END OBJ(cached, INFO_BUFFERS)
1179 END OBJ(cpu, INFO_CPU)
1181 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1182 obj->data.cpu_index = atoi(&arg[3]);
1184 } else {obj->data.cpu_index = 0; }
1186 obj->data.cpu_index = 0;
1188 END OBJ(cpubar, INFO_CPU)
1190 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1191 obj->data.cpu_index = atoi(&arg[3]);
1194 else {obj->data.cpu_index = 0;}
1195 (void) scan_bar(arg, &obj->a, &obj->b);
1197 (void) scan_bar(arg, &obj->a, &obj->b);
1198 obj->data.cpu_index = 0;
1200 END OBJ(cpugraph, INFO_CPU)
1202 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
1203 obj->data.cpu_index = atoi(&arg[3]);
1206 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1208 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1209 obj->data.cpu_index = 0;
1211 END OBJ(diskio, INFO_DISKIO)
1212 END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1215 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
1219 obj->data.s = scan_font(arg);
1221 OBJ(downspeed, INFO_NET)
1223 obj->data.net = get_net_stat(arg);
1226 CRIT_ERR("downspeed needs argument");
1228 END OBJ(downspeedf, INFO_NET)
1230 obj->data.net = get_net_stat(arg);
1233 CRIT_ERR("downspeedf needs argument");
1235 END OBJ(downspeedgraph, INFO_NET)
1236 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1238 sscanf(arg, "%63s %*i,%*i %*i", buf);
1239 obj->data.net = get_net_stat(buf);
1240 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1241 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
1250 text_objects[blockstart[blockdepth - 1] -
1251 1].data.ifblock.pos = text_object_count;
1252 blockstart[blockdepth - 1] = text_object_count;
1253 obj->data.ifblock.pos = text_object_count + 2;
1255 ERR("$else: no matching $if_*");
1260 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
1263 ERR("$endif: no matching $if_*");
1267 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
1268 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
1269 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
1270 END OBJ(execibar, 0) unsigned int n;
1271 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1273 ERR("${execibar <interval> command}");
1274 obj->type = OBJ_text;
1275 snprintf(buf, 256, "${%s}", s);
1276 obj->data.s = strdup(buf);
1278 obj->data.execi.cmd = strdup(arg + n);
1280 END OBJ(execigraph, 0) unsigned int n;
1281 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1283 ERR("${execigraph <interval> command}");
1284 obj->type = OBJ_text;
1285 snprintf(buf, 256, "${%s}", s);
1286 obj->data.s = strdup(buf);
1288 obj->data.execi.cmd = strdup(arg + n);
1290 END OBJ(execi, 0) unsigned int n;
1293 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1295 ERR("${execi <interval> command}");
1296 obj->type = OBJ_text;
1297 snprintf(buf, 256, "${%s}", s);
1298 obj->data.s = strdup(buf);
1300 obj->data.execi.cmd = strdup(arg + n);
1301 obj->data.execi.buffer =
1302 (char *) calloc(1, TEXT_BUFFER_SIZE);
1304 END OBJ(texeci, 0) unsigned int n;
1307 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1309 ERR("${texeci <interval> command}");
1310 obj->type = OBJ_text;
1311 snprintf(buf, 256, "${%s}", s);
1312 obj->data.s = strdup(buf);
1314 obj->data.execi.cmd = strdup(arg + n);
1315 obj->data.execi.buffer =
1316 (char *) calloc(1, TEXT_BUFFER_SIZE);
1318 END OBJ(pre_exec, 0) obj->type = OBJ_text;
1320 FILE *fp = popen(arg, "r");
1324 n = fread(buf, 1, 2048, fp);
1327 if (n && buf[n - 1] == '\n')
1332 obj->data.s = strdup(buf);
1334 obj->data.s = strdup("");
1337 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1338 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1340 while (isspace(*arg))
1346 obj->data.fsbar.fs = prepare_fs_stat(arg);
1347 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1350 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1354 obj->data.fsbar.fs = prepare_fs_stat(arg);
1355 END OBJ(fs_free, INFO_FS) if (!arg)
1357 obj->data.fs = prepare_fs_stat(arg);
1358 END OBJ(fs_used_perc, INFO_FS) if (!arg)
1360 obj->data.fs = prepare_fs_stat(arg);
1361 END OBJ(fs_free_perc, INFO_FS) if (!arg)
1363 obj->data.fs = prepare_fs_stat(arg);
1364 END OBJ(fs_size, INFO_FS) if (!arg)
1366 obj->data.fs = prepare_fs_stat(arg);
1367 END OBJ(fs_used, INFO_FS) if (!arg)
1369 obj->data.fs = prepare_fs_stat(arg);
1370 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
1371 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
1372 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
1373 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
1377 ERR("i2c needs arguments");
1378 obj->type = OBJ_text;
1379 //obj->data.s = strdup("${i2c}");
1383 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1384 /* if scanf couldn't read three values, read type and num and use
1386 sscanf(arg, "%63s %d", buf2, &n);
1388 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1389 obj->data.i2c.devtype);
1390 strncpy(obj->data.i2c.type, buf2, 63);
1393 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1394 obj->data.i2c.devtype);
1395 strncpy(obj->data.i2c.type, buf2, 63);
1398 END OBJ(top, INFO_TOP)
1402 ERR("top needs arguments");
1403 obj->type = OBJ_text;
1404 //obj->data.s = strdup("${top}");
1407 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1408 if (strcmp(buf, "name") == 0) {
1409 obj->data.top.type = TOP_NAME;
1410 } else if (strcmp(buf, "cpu") == 0) {
1411 obj->data.top.type = TOP_CPU;
1412 } else if (strcmp(buf, "pid") == 0) {
1413 obj->data.top.type = TOP_PID;
1414 } else if (strcmp(buf, "mem") == 0) {
1415 obj->data.top.type = TOP_MEM;
1417 ERR("invalid arg for top");
1420 if (n < 1 || n > 10) {
1421 CRIT_ERR("invalid arg for top");
1424 obj->data.top.num = n - 1;
1428 ERR("invalid args given for top");
1431 END OBJ(top_mem, INFO_TOP)
1435 ERR("top_mem needs arguments");
1436 obj->type = OBJ_text;
1437 obj->data.s = strdup("${top_mem}");
1440 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1441 if (strcmp(buf, "name") == 0) {
1442 obj->data.top.type = TOP_NAME;
1443 } else if (strcmp(buf, "cpu") == 0) {
1444 obj->data.top.type = TOP_CPU;
1445 } else if (strcmp(buf, "pid") == 0) {
1446 obj->data.top.type = TOP_PID;
1447 } else if (strcmp(buf, "mem") == 0) {
1448 obj->data.top.type = TOP_MEM;
1450 ERR("invalid arg for top");
1453 if (n < 1 || n > 10) {
1454 CRIT_ERR("invalid arg for top");
1457 obj->data.top.num = n - 1;
1461 ERR("invalid args given for top");
1464 END OBJ(addr, INFO_NET)
1466 obj->data.net = get_net_stat(arg);
1469 CRIT_ERR("addr needs argument");
1471 END OBJ(linkstatus, INFO_WIFI)
1473 obj->data.net = get_net_stat(arg);
1476 CRIT_ERR("linkstatus needs argument");
1482 ERR("tail needs arguments");
1483 obj->type = OBJ_text;
1484 obj->data.s = strdup("${tail}");
1487 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1488 if (n1 < 1 || n1 > 30) {
1489 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
1493 fp = fopen(buf, "r");
1495 obj->data.tail.logfile =
1496 malloc(TEXT_BUFFER_SIZE);
1497 strcpy(obj->data.tail.logfile, buf);
1498 obj->data.tail.wantedlines = n1 - 1;
1499 obj->data.tail.interval =
1500 update_interval * 2;
1504 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1507 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1508 if (n1 < 1 || n1 > 30) {
1510 ("invalid arg for tail, number of lines must be between 1 and 30");
1512 } else if (n2 < 1 || n2 < update_interval) {
1514 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1518 fp = fopen(buf, "r");
1520 obj->data.tail.logfile =
1521 malloc(TEXT_BUFFER_SIZE);
1522 strcpy(obj->data.tail.logfile, buf);
1523 obj->data.tail.wantedlines = n1 - 1;
1524 obj->data.tail.interval = n2;
1528 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1534 ERR("invalid args given for tail");
1537 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1542 ERR("head needs arguments");
1543 obj->type = OBJ_text;
1544 obj->data.s = strdup("${head}");
1547 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1548 if (n1 < 1 || n1 > 30) {
1549 CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
1553 fp = fopen(buf, "r");
1555 obj->data.tail.logfile =
1556 malloc(TEXT_BUFFER_SIZE);
1557 strcpy(obj->data.tail.logfile, buf);
1558 obj->data.tail.wantedlines = n1 - 1;
1559 obj->data.tail.interval =
1560 update_interval * 2;
1564 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1567 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1568 if (n1 < 1 || n1 > 30) {
1570 ("invalid arg for head, number of lines must be between 1 and 30");
1572 } else if (n2 < 1 || n2 < update_interval) {
1574 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
1578 fp = fopen(buf, "r");
1580 obj->data.tail.logfile =
1581 malloc(TEXT_BUFFER_SIZE);
1582 strcpy(obj->data.tail.logfile, buf);
1583 obj->data.tail.wantedlines = n1 - 1;
1584 obj->data.tail.interval = n2;
1588 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
1594 ERR("invalid args given for head");
1597 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
1598 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
1600 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1601 if (r >= 3 && (c < 1 || c > 3))
1603 if (r >= 2 && (b < 1 || b > 3))
1605 if (r >= 1 && (a < 1 || a > 3))
1608 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1609 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1610 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1611 END OBJ(if_existing, 0)
1612 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1613 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1616 ERR("if_existing needs an argument");
1617 obj->data.ifblock.s = 0;
1619 obj->data.ifblock.s = strdup(arg);
1620 blockstart[blockdepth] = text_object_count;
1621 obj->data.ifblock.pos = text_object_count + 2;
1623 END OBJ(if_mounted, 0)
1624 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1625 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1628 ERR("if_mounted needs an argument");
1629 obj->data.ifblock.s = 0;
1631 obj->data.ifblock.s = strdup(arg);
1632 blockstart[blockdepth] = text_object_count;
1633 obj->data.ifblock.pos = text_object_count + 2;
1635 END OBJ(if_running, 0)
1636 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1637 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1641 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1642 obj->data.ifblock.s = strdup(buf);
1644 ERR("if_running needs an argument");
1645 obj->data.ifblock.s = 0;
1647 blockstart[blockdepth] = text_object_count;
1648 obj->data.ifblock.pos = text_object_count + 2;
1652 END OBJ(mails, INFO_MAIL)
1653 END OBJ(mem, INFO_MEM)
1654 END OBJ(memmax, INFO_MEM)
1655 END OBJ(memperc, INFO_MEM)
1656 END OBJ(membar, INFO_MEM)
1657 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1658 END OBJ(memgraph, INFO_MEM)
1659 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1660 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
1661 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
1662 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
1663 END OBJ(mixerbar, INFO_MIXER)
1664 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1665 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1666 END OBJ(mixerlbar, INFO_MIXER)
1667 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1668 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1669 END OBJ(mixerrbar, INFO_MIXER)
1670 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1671 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1674 OBJ(ml_upload_counter, INFO_MLDONKEY)
1675 END OBJ(ml_download_counter, INFO_MLDONKEY)
1676 END OBJ(ml_nshared_files, INFO_MLDONKEY)
1677 END OBJ(ml_shared_counter, INFO_MLDONKEY)
1678 END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1679 END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1680 END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1681 END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1682 END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1683 END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
1685 OBJ(new_mails, INFO_MAIL)
1686 END OBJ(nodename, 0)
1687 END OBJ(processes, INFO_PROCS)
1688 END OBJ(running_processes, INFO_RUN_PROCS)
1689 END OBJ(shadecolor, 0)
1691 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1693 END OBJ(outlinecolor, 0)
1695 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1697 END OBJ(stippled_hr, 0)
1699 int a = stippled_borders, b = 1;
1701 if (sscanf(arg, "%d %d", &a, &b) != 2)
1702 sscanf(arg, "%d", &b);
1706 obj->data.pair.a = a;
1707 obj->data.pair.b = b;
1709 END OBJ(swap, INFO_MEM)
1710 END OBJ(swapmax, INFO_MEM)
1711 END OBJ(swapperc, INFO_MEM)
1712 END OBJ(swapbar, INFO_MEM)
1713 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1714 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1716 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1717 obj->data.i2c.devtype);
1718 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1720 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1721 obj->data.i2c.devtype);
1722 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1723 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1724 END OBJ(totaldown, INFO_NET)
1726 obj->data.net = get_net_stat(arg);
1729 CRIT_ERR("totaldown needs argument");
1731 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1733 obj->data.net = get_net_stat(arg);
1736 CRIT_ERR("totalup needs argument");
1739 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
1740 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
1741 END OBJ(upspeed, INFO_NET)
1743 obj->data.net = get_net_stat(arg);
1746 CRIT_ERR("upspeed needs argument");
1748 END OBJ(upspeedf, INFO_NET)
1750 obj->data.net = get_net_stat(arg);
1753 CRIT_ERR("upspeedf needs argument");
1756 END OBJ(upspeedgraph, INFO_NET)
1757 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
1759 sscanf(arg, "%63s %*i,%*i %*i", buf);
1760 obj->data.net = get_net_stat(buf);
1761 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1762 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
1767 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1768 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
1769 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
1770 OBJ(apm_adapter, 0) END
1771 OBJ(apm_battery_life, 0) END
1772 OBJ(apm_battery_time, 0) END
1773 #endif /* __FreeBSD__ */
1775 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1776 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1777 END OBJ(seti_credit, INFO_SETI) END
1780 OBJ(mpd_artist, INFO_MPD)
1781 END OBJ(mpd_title, INFO_MPD)
1782 END OBJ(mpd_random, INFO_MPD)
1783 END OBJ(mpd_repeat, INFO_MPD)
1784 END OBJ(mpd_elapsed, INFO_MPD)
1785 END OBJ(mpd_length, INFO_MPD)
1786 END OBJ(mpd_track, INFO_MPD)
1787 END OBJ(mpd_percent, INFO_MPD)
1788 END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol,
1789 INFO_MPD) END OBJ(mpd_bitrate,
1791 END OBJ(mpd_status, INFO_MPD)
1792 END OBJ(mpd_bar, INFO_MPD)
1793 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1797 OBJ(bmpx_title, INFO_BMPX)
1799 OBJ(bmpx_artist, INFO_BMPX)
1801 OBJ(bmpx_album, INFO_BMPX)
1803 OBJ(bmpx_track, INFO_BMPX)
1805 OBJ(bmpx_uri, INFO_BMPX)
1807 OBJ(bmpx_bitrate, INFO_BMPX)
1810 #ifdef TCP_PORT_MONITOR
1811 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
1812 int argc, port_begin, port_end, item, connection_index;
1814 memset(itembuf,0,sizeof(itembuf));
1816 /* massive argument checking */
1818 CRIT_ERR("tcp_portmon: needs arguments");
1820 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
1821 if ( (argc != 3) && (argc != 4) )
1823 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
1825 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
1827 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
1829 if ( port_begin > port_end )
1831 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
1833 if ( strncmp(itembuf,"count",31) == 0 )
1835 else if ( strncmp(itembuf,"rip",31) == 0 )
1837 else if ( strncmp(itembuf,"rhost",31) == 0 )
1839 else if ( strncmp(itembuf,"rport",31) == 0 )
1841 else if ( strncmp(itembuf,"lip",31) == 0 )
1843 else if ( strncmp(itembuf,"lhost",31) == 0 )
1845 else if ( strncmp(itembuf,"lport",31) == 0 )
1847 else if ( strncmp(itembuf,"lservice",31) == 0 )
1851 CRIT_ERR("tcp_portmon: invalid item specified");
1853 if ( (argc==3) && (item!=COUNT) )
1855 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
1857 if ( (argc==4) && (connection_index<0) )
1859 CRIT_ERR("tcp_portmon: connection index must be non-negative");
1861 /* ok, args looks good. save the text object data */
1862 obj->data.tcp_port_monitor.port_range_begin = (in_addr_t)port_begin;
1863 obj->data.tcp_port_monitor.port_range_end = (in_addr_t)port_end;
1864 obj->data.tcp_port_monitor.item = item;
1865 obj->data.tcp_port_monitor.connection_index = connection_index;
1867 /* if the port monitor collection hasn't been created, we must create it */
1868 if ( !info.p_tcp_port_monitor_collection )
1870 info.p_tcp_port_monitor_collection =
1871 create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
1872 if ( !info.p_tcp_port_monitor_collection )
1874 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
1878 /* if a port monitor for this port does not exist, create one and add it to the collection */
1879 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
1881 tcp_port_monitor_t * p_monitor =
1882 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
1885 CRIT_ERR("tcp_portmon: unable to create port monitor");
1887 /* add the newly created monitor to the collection */
1888 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
1891 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
1898 ERR("unknown variable %s", s);
1899 obj->type = OBJ_text;
1900 snprintf(buf, 256, "${%s}", s);
1901 obj->data.s = strdup(buf);
1908 static struct text_object *create_plain_text(const char *s)
1910 struct text_object *obj;
1912 if (s == NULL || *s == '\0') {
1916 obj = new_text_object_internal();
1918 obj->type = OBJ_text;
1919 obj->data.s = strdup(s);
1923 static struct text_object_list *extract_variable_text_internal(const char *p)
1925 struct text_object_list *retval;
1926 struct text_object *obj;
1929 retval = malloc(sizeof(struct text_object_list));
1930 memset(retval, 0, sizeof(struct text_object_list));
1931 retval->text_object_count = 0;
1936 obj = create_plain_text(s);
1938 // allocate memory for the object
1939 retval->text_objects = realloc(retval->text_objects,
1940 sizeof(struct text_object) * (retval->text_object_count+1));
1941 // assign the new object to the end of the list.
1942 memcpy(&retval->text_objects[retval->text_object_count++],
1943 obj, sizeof(struct text_object));
1955 /* variable is either $foo or ${foo} */
1959 while (*p && *p != '}')
1965 while (*p && (isalnum((int) *p)
1970 /* copy variable to buffer */
1971 len = (p - s > 255) ? 255 : (p - s);
1972 strncpy(buf, s, len);
1981 /* if variable wasn't found from environment, use some special */
1987 if (strchr(buf, ' ')) {
1988 arg = strchr(buf, ' ');
1991 while (isspace((int) *arg))
1997 /* lowercase variable name */
2004 // create new object
2005 obj = construct_text_object(buf, arg);
2007 // allocate memory for the object
2008 retval->text_objects = realloc(retval->text_objects,
2009 sizeof(struct text_object) * (retval->text_object_count+1));
2010 // assign the new object to the end of the list.
2011 memcpy(&retval->text_objects[retval->text_object_count++],
2012 obj, sizeof(struct text_object));
2018 obj = create_plain_text("$");
2020 // allocate memory for the object
2021 retval->text_objects = realloc(retval->text_objects,
2022 sizeof(struct text_object) * (retval->text_object_count+1));
2023 // assign the new object to the end of the list.
2024 memcpy(&retval->text_objects[retval->text_object_count++],
2025 obj, sizeof(struct text_object));
2032 obj = create_plain_text(s);
2034 // allocate memory for the object
2035 retval->text_objects = realloc(retval->text_objects,
2036 sizeof(struct text_object) * (retval->text_object_count+1));
2037 // assign the new object to the end of the list.
2038 memcpy(&retval->text_objects[retval->text_object_count++],
2039 obj, sizeof(struct text_object));
2044 ERR("one or more $endif's are missing");
2050 static void extract_variable_text(const char *p)
2052 struct text_object_list *list;
2054 free_text_objects(text_object_count, text_objects);
2055 text_object_count = 0;
2056 text_objects = NULL;
2060 #endif /* MLDONKEY */
2062 list = extract_variable_text_internal(p);
2063 text_objects = list->text_objects;
2064 text_object_count = list->text_object_count;
2071 void parse_conky_vars(char * text, char * p, struct information *cur) {
2072 struct text_object_list *object_list = extract_variable_text_internal(text);
2073 generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
2077 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
2081 for (i = 0; i < object_count; i++) {
2082 struct text_object *obj = &objs[i];
2084 #define OBJ(a) break; case OBJ_##a:
2086 switch (obj->type) {
2089 ERR("not implemented obj type %d",
2093 /* does anyone have decimals in acpi temperature? */
2095 snprintf(p, p_max_size, "%d", (int)
2096 get_acpi_temperature(obj->
2100 snprintf(p, 5, "%d ", (int)
2101 get_acpi_temperature(obj->
2106 /* does anyone have decimals in acpi temperature? */
2108 snprintf(p, p_max_size, "%d", (int)
2109 ((get_acpi_temperature(obj->
2111 i)+ 40) * 9.0 / 5 - 40));
2113 snprintf(p, 5, "%d ", (int)
2114 ((get_acpi_temperature(obj->
2116 i)+ 40) * 9.0 / 5 - 40));
2119 get_freq(p, p_max_size, "%.0f", 1); /* pk */
2122 get_freq(p, p_max_size, "%'.2f", 1000); /* pk */
2126 get_freq_dynamic(p, 6, "%.0f ", 1 ); /* pk */
2128 get_freq_dynamic(p, p_max_size, "%.0f", 1 ); /* pk */
2133 get_freq_dynamic(p, 6, "%'.2f ", 1000); /* pk */
2135 get_freq_dynamic(p, p_max_size, "%'.2f", 1000); /* pk */
2139 get_adt746x_cpu(p, p_max_size); /* pk */
2142 get_adt746x_fan(p, p_max_size); /* pk */
2145 get_acpi_fan(p, p_max_size); /* pk */
2147 OBJ(acpiacadapter) {
2148 get_acpi_ac_adapter(p, p_max_size); /* pk */
2151 get_battery_stuff(p, p_max_size, obj->data.s);
2154 human_readable(cur->buffers * 1024, p, 255);
2157 human_readable(cur->cached * 1024, p, 255);
2160 if (obj->data.cpu_index > info.cpu_count) {
2161 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
2162 CRIT_ERR("attempting to use more CPUs then you have!");
2165 snprintf(p, p_max_size, "%*d", pad_percents,
2166 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2169 snprintf(p, 4, "%*d ",
2171 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2177 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
2180 new_graph(p, obj->a,
2181 obj->b, obj->c, obj->d,
2182 (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
2186 new_fg(p, obj->data.l);
2188 #if defined(__linux__)
2190 snprintf(p, p_max_size, "%s", i8k.version);
2193 snprintf(p, p_max_size, "%s", i8k.bios);
2196 snprintf(p, p_max_size, "%s", i8k.serial);
2199 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
2201 OBJ(i8k_cpu_tempf) {
2203 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
2204 snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
2206 OBJ(i8k_left_fan_status) {
2207 int left_fan_status;
2208 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
2209 if(left_fan_status == 0) {
2210 snprintf(p, p_max_size,"off");
2211 } if(left_fan_status == 1) {
2212 snprintf(p, p_max_size, "low");
2213 } if(left_fan_status == 2) {
2214 snprintf(p, p_max_size, "high");
2218 OBJ(i8k_right_fan_status) {
2219 int right_fan_status;
2220 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
2221 if(right_fan_status == 0) {
2222 snprintf(p, p_max_size,"off");
2223 } if(right_fan_status == 1) {
2224 snprintf(p, p_max_size, "low");
2225 } if(right_fan_status == 2) {
2226 snprintf(p, p_max_size, "high");
2229 OBJ(i8k_left_fan_rpm) {
2230 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
2232 OBJ(i8k_right_fan_rpm) {
2233 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
2235 OBJ(i8k_ac_status) {
2237 sscanf(i8k.ac_status, "%d", &ac_status);
2238 if(ac_status == -1) {
2239 snprintf(p, p_max_size,"disabled (read i8k docs)");
2240 } if(ac_status == 0) {
2241 snprintf(p, p_max_size, "off");
2242 } if(ac_status == 1) {
2243 snprintf(p, p_max_size, "on");
2246 OBJ(i8k_buttons_status) {
2247 snprintf(p, p_max_size, "%s", i8k.buttons_status);
2250 #endif /* __linux__ */
2254 new_font(p, obj->data.s);
2259 if (diskio_value > 1024*1024) {
2260 snprintf(p, p_max_size, "%.1fG",
2261 (double)diskio_value/1024/1024);
2262 } else if (diskio_value > 1024) {
2263 snprintf(p, p_max_size, "%.1fM",
2264 (double)diskio_value/1024);
2265 } else if (diskio_value > 0) {
2266 snprintf(p, p_max_size, "%dK", diskio_value);
2268 snprintf(p, p_max_size, "%d", diskio_value);
2271 if (diskio_value > 1024*1024) {
2272 snprintf(p, 6, "%.1fG ",
2273 (double)diskio_value/1024/1024);
2274 } else if (diskio_value > 1024) {
2275 snprintf(p, 6, "%.1fM ",
2276 (double)diskio_value/1024);
2277 } else if (diskio_value > 0) {
2278 snprintf(p, 6, "%dK ", diskio_value);
2280 snprintf(p, 6, "%d ", diskio_value);
2285 new_graph(p, obj->a,
2286 obj->b, obj->c, obj->d,
2287 diskio_value, obj->e, 1);
2292 snprintf(p, p_max_size, "%d",
2293 (int) (obj->data.net->
2297 snprintf(p, 6, "%d ",
2298 (int) (obj->data.net->
2304 snprintf(p, p_max_size, "%.1f",
2306 recv_speed / 1024.0);
2308 snprintf(p, 8, "%.1f ",
2310 recv_speed / 1024.0);
2312 OBJ(downspeedgraph) {
2313 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
2314 obj->data.net->recv_speed = 0.01;
2315 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2316 (obj->data.net->recv_speed /
2317 1024.0), obj->e, 1);
2323 i = obj->data.ifblock.pos - 2;
2333 snprintf(p, p_max_size, "%u.%u.%u.%u",
2334 obj->data.net->addr.
2336 obj->data.net->addr.
2338 obj->data.net->addr.
2340 obj->data.net->addr.
2345 snprintf(p, p_max_size, "%d",
2346 obj->data.net->linkstatus);
2350 FILE *fp = popen(obj->data.s, "r");
2351 int length = fread(p, 1, p_max_size, fp);
2354 /*output[length] = '\0';
2355 if (length > 0 && output[length - 1] == '\n') {
2356 output[length - 1] = '\0';
2359 if (length > 0 && p[length - 1] == '\n') {
2360 p[length - 1] = '\0';
2363 //parse_conky_vars(output, p, cur);
2367 FILE *fp = popen(obj->data.s, "r");
2368 int n2 = fread(p, 1, p_max_size, fp);
2372 if (n2 && p[n2 - 1] == '\n')
2381 if (sscanf(p, "%lf", &barnum) == 0) {
2382 ERR("reading execbar value failed (perhaps it's not the correct format?)");
2384 if (barnum > 100 || barnum < 0) {
2385 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
2387 barnum = barnum / 100.0;
2388 new_bar(p, 0, 4, (int) (barnum * 255.0));
2394 FILE *fp = popen(obj->data.s, "r");
2395 int n2 = fread(p, 1, p_max_size, fp);
2399 if (n2 && p[n2 - 1] == '\n')
2408 if (sscanf(p, "%lf", &barnum) == 0) {
2409 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
2411 if (barnum > 100 || barnum < 0) {
2412 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
2415 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
2420 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2421 new_bar(p, 0, 4, (int) obj->f);
2424 FILE *fp = popen(obj->data.execi.cmd, "r");
2425 int n2 = fread(p, 1, p_max_size, fp);
2428 if (n2 && p[n2 - 1] == '\n')
2437 if (sscanf(p, "%f", &barnum) == 0) {
2438 ERR("reading execibar value failed (perhaps it's not the correct format?)");
2440 if (barnum > 100 || barnum < 0) {
2441 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
2443 obj->f = 255 * barnum / 100.0;
2444 new_bar(p, 0, 4, (int) obj->f);
2446 obj->data.execi.last_update =
2447 current_update_time;
2451 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2452 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
2455 FILE *fp = popen(obj->data.execi.cmd, "r");
2456 int n2 = fread(p, 1, p_max_size, fp);
2459 if (n2 && p[n2 - 1] == '\n')
2468 if (sscanf(p, "%f", &barnum) == 0) {
2469 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
2471 if (barnum > 100 || barnum < 0) {
2472 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
2475 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
2477 obj->data.execi.last_update = current_update_time;
2483 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
2484 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
2486 char *output = obj->data.execi.buffer;
2487 FILE *fp = popen(obj->data.execi.cmd, "r");
2488 //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
2489 int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
2492 output[length] = '\0';
2493 if (length > 0 && output[length - 1] == '\n') {
2494 output[length - 1] = '\0';
2496 obj->data.execi.last_update = current_update_time;
2497 snprintf(p, p_max_size, "%s", output);
2499 //parse_conky_vars(output, p, cur);
2502 static int running = 0;
2503 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
2504 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
2506 static pthread_t execthread;
2508 pthread_join( execthread, NULL);
2513 pthread_create( &execthread, NULL, (void*)threaded_exec, (void*) obj);
2514 pthread_mutex_lock( &mutex1 );
2515 obj->data.execi.last_update = current_update_time;
2516 pthread_mutex_unlock( &mutex1 );
2518 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
2520 //parse_conky_vars(obj->data.execi.buffer, p, cur);
2524 if (obj->data.fs != NULL) {
2525 if (obj->data.fs->size == 0)
2544 if (obj->data.fs != NULL)
2545 human_readable(obj->data.fs->avail,
2549 if (obj->data.fs != NULL) {
2550 if (obj->data.fs->size)
2551 snprintf(p, p_max_size, "%*d",
2560 snprintf(p, p_max_size, "0");
2564 if (obj->data.fs != NULL)
2565 human_readable(obj->data.fs->size,
2569 if (obj->data.fs != NULL)
2570 human_readable(obj->data.fs->size -
2571 (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
2575 if (obj->data.fs != NULL) {
2576 if (obj->data.fs->size == 0)
2594 if (obj->data.fs != NULL) {
2595 if (obj->data.fs->size)
2596 snprintf(p, 4, "%d",
2606 snprintf(p, p_max_size, "0");
2610 float *v = info.loadavg;
2612 if (obj->data.loadavg[2])
2613 snprintf(p, p_max_size, "%.2f %.2f %.2f",
2614 v[obj->data.loadavg[0] -
2616 v[obj->data.loadavg[1] -
2618 v[obj->data.loadavg[2] -
2620 else if (obj->data.loadavg[1])
2621 snprintf(p, p_max_size, "%.2f %.2f",
2622 v[obj->data.loadavg[0] -
2624 v[obj->data.loadavg[1] -
2626 else if (obj->data.loadavg[0])
2627 snprintf(p, p_max_size, "%.2f",
2628 v[obj->data.loadavg[0] -
2632 new_hr(p, obj->data.i);
2635 new_offset(p, obj->data.i);
2638 new_voffset(p, obj->data.i);
2643 r = get_i2c_info(&obj->data.i2c.fd,
2645 obj->data.i2c.devtype,
2646 obj->data.i2c.type);
2648 if (r >= 100.0 || r == 0)
2649 snprintf(p, p_max_size, "%d", (int) r);
2651 snprintf(p, p_max_size, "%.1f", r);
2654 new_alignr(p, obj->data.i);
2657 new_alignc(p, obj->data.i);
2661 if ((obj->data.ifblock.s)
2662 && (stat(obj->data.ifblock.s, &tmp) ==
2664 i = obj->data.ifblock.pos - 2;
2670 if ((obj->data.ifblock.s)
2671 && (!check_mount(obj->data.ifblock.s))) {
2672 i = obj->data.ifblock.pos - 2;
2678 if ((obj->data.ifblock.s)
2679 && system(obj->data.ifblock.s)) {
2680 i = obj->data.ifblock.pos - 2;
2686 snprintf(p, p_max_size, "%s", cur->uname_s.release);
2689 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
2694 human_readable(cur->mem * 1024, p, 6);
2697 human_readable(cur->memmax * 1024, p, 255);
2702 snprintf(p, p_max_size, "%*lu",
2707 snprintf(p, 4, "%*lu ",
2714 new_bar(p, obj->data.pair.a,
2716 cur->memmax ? (cur->mem * 255) /
2721 new_graph(p, obj->a,
2722 obj->b, obj->c, obj->d,
2723 cur->memmax ? (cur->mem * 100.0) /
2724 (cur->memmax) : 0.0, 100, 1);
2728 snprintf(p, p_max_size, "%d",
2729 mixer_get_avg(obj->data.l));
2732 snprintf(p, p_max_size, "%d",
2733 mixer_get_left(obj->data.l));
2736 snprintf(p, p_max_size, "%d",
2737 mixer_get_right(obj->data.l));
2740 new_bar(p, obj->data.mixerbar.w,
2741 obj->data.mixerbar.h,
2742 mixer_get_avg(obj->data.mixerbar.
2746 new_bar(p, obj->data.mixerbar.w,
2747 obj->data.mixerbar.h,
2748 mixer_get_left(obj->data.mixerbar.
2752 new_bar(p, obj->data.mixerbar.w,
2753 obj->data.mixerbar.h,
2754 mixer_get_right(obj->data.mixerbar.
2760 snprintf(p, p_max_size, "%d", cur->mail_count);
2763 snprintf(p, p_max_size, "%d", cur->new_mail_count);
2766 OBJ(ml_upload_counter) {
2767 snprintf(p, p_max_size, "%lld",
2768 mlinfo.upload_counter / 1048576);
2770 OBJ(ml_download_counter) {
2771 snprintf(p, p_max_size, "%lld",
2772 mlinfo.download_counter /
2775 OBJ(ml_nshared_files) {
2776 snprintf(p, p_max_size, "%i", mlinfo.nshared_files);
2778 OBJ(ml_shared_counter) {
2779 snprintf(p, p_max_size, "%lld",
2780 mlinfo.shared_counter / 1048576);
2782 OBJ(ml_tcp_upload_rate) {
2783 snprintf(p, p_max_size, "%.2f",
2784 (float) mlinfo.tcp_upload_rate /
2787 OBJ(ml_tcp_download_rate) {
2788 snprintf(p, p_max_size, "%.2f",
2789 (float) mlinfo.tcp_download_rate /
2792 OBJ(ml_udp_upload_rate) {
2793 snprintf(p, p_max_size, "%.2f",
2794 (float) mlinfo.udp_upload_rate /
2797 OBJ(ml_udp_download_rate) {
2798 snprintf(p, p_max_size, "%.2f",
2799 (float) mlinfo.udp_download_rate /
2802 OBJ(ml_ndownloaded_files) {
2803 snprintf(p, p_max_size, "%i",
2804 mlinfo.ndownloaded_files);
2806 OBJ(ml_ndownloading_files) {
2807 snprintf(p, p_max_size, "%i",
2808 mlinfo.ndownloading_files);
2813 snprintf(p, p_max_size, "%s",
2814 cur->uname_s.nodename);
2817 new_outline(p, obj->data.l);
2821 snprintf(p, p_max_size, "%hu", cur->procs);
2823 snprintf(p, 5, "%hu ",
2826 OBJ(running_processes) {
2828 snprintf(p, p_max_size, "%hu",
2831 snprintf(p, 3, "%hu ",
2835 snprintf(p, p_max_size, "%s", obj->data.s);
2838 new_bg(p, obj->data.l);
2841 new_stippled_hr(p, obj->data.pair.a,
2845 human_readable(cur->swap * 1024, p, 255);
2848 human_readable(cur->swapmax * 1024, p,
2852 if (cur->swapmax == 0) {
2853 strncpy(p, "No swap", 255);
2856 snprintf(p, 255, "%*lu",
2862 snprintf(p, 4, "%*lu ",
2870 new_bar(p, obj->data.pair.a,
2872 cur->swapmax ? (cur->swap * 255) /
2873 (cur->swapmax) : 0);
2876 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
2879 time_t t = time(NULL);
2880 struct tm *tm = localtime(&t);
2881 setlocale(LC_TIME, "");
2882 strftime(p, p_max_size, obj->data.s, tm);
2885 time_t t = time(NULL);
2886 struct tm *tm = gmtime(&t);
2887 strftime(p, p_max_size, obj->data.s, tm);
2890 human_readable(obj->data.net->recv, p,
2894 human_readable(obj->data.net->trans, p,
2898 snprintf(p, p_max_size, "%d", total_updates);
2902 snprintf(p, p_max_size, "%d",
2903 (int) (obj->data.net->
2907 snprintf(p, 5, "%d ",
2908 (int) (obj->data.net->
2914 snprintf(p, p_max_size, "%.1f",
2916 trans_speed / 1024.0);
2918 snprintf(p, 8, "%.1f ",
2920 trans_speed / 1024.0);
2923 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
2924 obj->data.net->trans_speed = 0.01;
2925 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2926 (obj->data.net->trans_speed /
2927 1024.0), obj->e, 1);
2930 format_seconds_short(p, p_max_size,
2934 format_seconds(p, p_max_size, (int) cur->uptime);
2937 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2939 snprintf(p, p_max_size, "%s", get_apm_adapter());
2941 OBJ(apm_battery_life) {
2943 msg = get_apm_battery_life();
2944 snprintf(p, p_max_size, "%s", msg);
2947 OBJ(apm_battery_time) {
2949 msg = get_apm_battery_time();
2950 snprintf(p, p_max_size, "%s", msg);
2953 #endif /* __FreeBSD__ */
2956 snprintf(p, p_max_size, "%.2f",
2957 cur->seti_prog * 100.0f);
2960 new_bar(p, obj->data.pair.a,
2962 (int) (cur->seti_prog * 255.0f));
2965 snprintf(p, p_max_size, "%.0f", cur->seti_credit);
2971 snprintf(p, p_max_size, "%s", cur->mpd.title);
2974 snprintf(p, p_max_size, "%s", cur->mpd.artist);
2977 snprintf(p, p_max_size, "%s", cur->mpd.album);
2980 snprintf(p, p_max_size, "%s", cur->mpd.random);
2983 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
2986 snprintf(p, p_max_size, "%s", cur->mpd.track);
2989 snprintf(p, p_max_size, "%i", cur->mpd.volume);
2992 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
2995 snprintf(p, p_max_size, "%s", cur->mpd.status);
2998 int days = 0, hours = 0, minutes =
3000 int tmp = cur->mpd.elapsed;
3001 while (tmp >= 86400) {
3005 while (tmp >= 3600) {
3015 snprintf(p, p_max_size, "%i days %i:%02i:%02i",
3016 days, hours, minutes,
3019 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
3022 snprintf(p, p_max_size, "%i:%02i", minutes,
3026 int days = 0, hours = 0, minutes =
3028 int tmp = cur->mpd.length;
3029 while (tmp >= 86400) {
3033 while (tmp >= 3600) {
3043 snprintf(p, p_max_size,
3044 "%i days %i:%02i:%02i",
3045 days, hours, minutes,
3048 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
3051 snprintf(p, p_max_size, "%i:%02i", minutes,
3055 snprintf(p, p_max_size, "%2.0f",
3056 cur->mpd.progress * 100);
3059 new_bar(p, obj->data.pair.a,
3061 (int) (cur->mpd.progress *
3067 snprintf(p, p_max_size, "%s", cur->bmpx.title);
3070 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
3073 snprintf(p, p_max_size, "%s", cur->bmpx.album);
3076 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
3079 snprintf(p, p_max_size, "%i", cur->bmpx.track);
3082 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
3086 if (obj->data.top.type == TOP_NAME
3087 && obj->data.top.num >= 0
3088 && obj->data.top.num < 10) {
3089 // if we limit the buffer and add a bunch of space after, it stops the thing from
3090 // moving other shit around, which is really fucking annoying
3091 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
3092 } else if (obj->data.top.type == TOP_CPU
3093 && obj->data.top.num >= 0
3094 && obj->data.top.num < 10) {
3095 snprintf(p, 7, "%3.2f ",
3096 cur->cpu[obj->data.top.
3098 } else if (obj->data.top.type == TOP_PID
3099 && obj->data.top.num >= 0
3100 && obj->data.top.num < 10) {
3101 snprintf(p, 8, "%i ",
3102 cur->cpu[obj->data.top.
3104 } else if (obj->data.top.type == TOP_MEM
3105 && obj->data.top.num >= 0
3106 && obj->data.top.num < 10) {
3107 snprintf(p, 7, "%3.2f ",
3108 cur->cpu[obj->data.top.
3113 if (obj->data.top.type == TOP_NAME
3114 && obj->data.top.num >= 0
3115 && obj->data.top.num < 10) {
3116 // if we limit the buffer and add a bunch of space after, it stops the thing from
3117 // moving other shit around, which is really fucking annoying
3120 cur->memu[obj->data.top.
3122 } else if (obj->data.top.type == TOP_CPU
3123 && obj->data.top.num >= 0
3124 && obj->data.top.num < 10) {
3125 snprintf(p, 7, "%3.2f ",
3126 cur->memu[obj->data.top.
3128 } else if (obj->data.top.type == TOP_PID
3129 && obj->data.top.num >= 0
3130 && obj->data.top.num < 10) {
3131 snprintf(p, 8, "%i ",
3132 cur->memu[obj->data.top.
3134 } else if (obj->data.top.type == TOP_MEM
3135 && obj->data.top.num >= 0
3136 && obj->data.top.num < 10) {
3137 snprintf(p, 7, "%3.2f ",
3138 cur->memu[obj->data.top.
3145 if (current_update_time -obj->data.tail.last_update >= obj->data.tail.interval) {
3146 obj->data.tail.last_update = current_update_time;
3150 tailstring *head = NULL;
3151 tailstring *headtmp = NULL;
3152 tailstring *freetmp = NULL;
3153 fp = fopen(obj->data.tail.logfile, "rt");
3155 ERR("tail logfile failed to open");
3158 obj->data.tail.readlines = 0;
3160 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
3167 addtail(&head, obj->data.tail.buffer);
3168 obj->data.tail.readlines++;
3174 if (obj->data.tail.readlines > 0) {
3175 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
3176 addtail(&headtmp, head->data);
3181 strcpy(obj->data.tail.buffer, headtmp->data);
3182 headtmp = headtmp->next;
3183 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
3185 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
3186 headtmp = headtmp->next;
3190 /* get rid of any ugly newlines at the end */
3191 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
3192 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
3197 strcpy(obj->data.tail.buffer, "Logfile Empty");
3198 } /* if readlines */
3200 } /* if cur_upd_time >= */
3202 //parse_conky_vars(obj->data.tail.buffer, p, cur);
3206 if (current_update_time -obj->data.tail.last_update >= obj->data.tail.interval) {
3207 obj->data.tail.last_update = current_update_time;
3209 tailstring *head = NULL;
3210 tailstring *headtmp = NULL;
3211 tailstring *freetmp = NULL;
3212 fp = fopen(obj->data.tail.logfile, "rt");
3214 ERR("head logfile failed to open");
3216 obj->data.tail.readlines = 0;
3217 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
3218 addtail(&head, obj->data.tail.buffer);
3219 obj->data.tail.readlines++;
3223 if (obj->data.tail.readlines > 0) {
3225 addtail(&headtmp, head->data);
3230 strcpy(obj->data.tail.buffer, headtmp->data);
3231 headtmp = headtmp->next;
3233 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
3234 headtmp = headtmp->next;
3237 /* get rid of any ugly newlines at the end */
3238 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
3239 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
3242 strcpy(obj->data.tail.buffer, "Logfile Empty");
3243 } /* if readlines > 0 */
3244 } /* if fp == null */
3245 } /* cur_upd_time >= */
3247 //parse_conky_vars(obj->data.tail.buffer, p, cur);
3250 #ifdef TCP_PORT_MONITOR
3253 /* grab a pointer to this port monitor */
3254 tcp_port_monitor_t * p_monitor =
3255 find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
3256 obj->data.tcp_port_monitor.port_range_begin,
3257 obj->data.tcp_port_monitor.port_range_end );
3259 snprintf(p, p_max_size, "monitor not found");
3263 /* now grab the text of interest */
3264 if ( peek_tcp_port_monitor( p_monitor,
3265 obj->data.tcp_port_monitor.item,
3266 obj->data.tcp_port_monitor.connection_index,
3267 p, p_max_size ) != 0 )
3269 snprintf(p, p_max_size, "monitor peek error");
3279 unsigned int a = strlen(p);
3287 double current_update_time, last_update_time;
3289 static void generate_text()
3291 struct information *cur = &info;
3298 current_update_time = get_time();
3302 /* add things to the buffer */
3308 generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
3310 if (stuff_in_upper_case) {
3320 last_update_time = current_update_time;
3327 static void set_font()
3331 if (window.xftdraw != NULL) {
3332 XftDrawDestroy(window.xftdraw);
3334 window.xftdraw = XftDrawCreate(display, window.drawable,
3335 DefaultVisual(display,
3337 DefaultColormap(display,
3342 XSetFont(display, window.gc, fonts[selected_font].font->fid);
3351 static int text_start_x, text_start_y; /* text start position in window */
3352 static int text_width, text_height;
3356 static inline int get_string_width(const char *s)
3359 return *s ? calc_text_width(s, strlen(s)) : 0;
3365 static inline int get_string_width_special(char *s)
3378 if (*p == SPECIAL_CHAR) {
3379 /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
3380 for (i = 0; i < strlen(p); i++) {
3381 *(p + i) = *(p + i + 1);
3383 if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
3384 width += specials[special_index+index].width;
3391 if (strlen(final) > 1) {
3392 width += calc_text_width(final, strlen(final));
3404 static void text_size_updater(char *s)
3408 int h = font_height();
3409 /* get string widths and skip specials */
3412 if (*p == SPECIAL_CHAR) {
3414 w += get_string_width(s);
3417 if (specials[special_index].type == BAR
3418 || specials[special_index].type == GRAPH) {
3419 w += specials[special_index].width;
3420 if (specials[special_index].height > h) {
3421 h = specials[special_index].height;
3426 else if (specials[special_index].type == OFFSET) {
3427 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
3429 else if (specials[special_index].type == VOFFSET) {
3430 h += specials[special_index].arg;
3432 else if (specials[special_index].type == FONT) {
3433 fontchange = specials[special_index].font_added;
3434 selected_font = specials[special_index].font_added;
3444 w += get_string_width(s);
3447 if (text_width > maximum_width && maximum_width)
3448 text_width = maximum_width;
3459 static void update_text_area()
3463 /* update text size if it isn't fixed */
3468 text_width = minimum_width;
3471 for_each_line(text_buffer, text_size_updater);
3473 if (text_height < minimum_height)
3474 text_height = minimum_height;
3475 if (text_width > maximum_width && maximum_width > 0)
3476 text_width = maximum_width;
3479 /* get text position on workarea */
3480 switch (text_alignment) {
3487 x = workarea[2] - text_width - gap_x;
3494 y = workarea[3] - text_height - gap_y;
3498 x = workarea[2] - text_width - gap_x;
3499 y = workarea[3] - text_height - gap_y;
3503 case NONE: // Let the WM manage the window
3514 if (own_window && !fixed_pos) {
3517 text_start_x = border_margin + 1;
3518 text_start_y = border_margin + 1;
3519 window.x = x - border_margin - 1;
3520 window.y = y - border_margin - 1;
3524 /* If window size doesn't match to workarea's size, then window
3525 * probably includes panels (gnome).
3526 * Blah, doesn't work on KDE. */
3527 if (workarea[2] != window.width
3528 || workarea[3] != window.height) {
3542 static int cur_x, cur_y; /* current x and y for drawing */
3543 static int draw_mode; /* FG, BG or OUTLINE */
3544 static long current_color;
3546 static inline void set_foreground_color(long c)
3549 XSetForeground(display, window.gc, c);
3553 static void draw_string(const char *s)
3557 int i, i2, pos, width_of_s;
3560 width_of_s = get_string_width(s);
3561 if (out_to_console) {
3563 fflush(stdout); /* output immediately, don't buffer */
3565 /* daemon_run(s); the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
3566 memset(tmpstring1,0,TEXT_BUFFER_SIZE);
3567 memset(tmpstring2,0,TEXT_BUFFER_SIZE);
3568 strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
3572 snprintf(space, 2, " ");
3574 max = ((text_width - width_of_s) / get_string_width(space));
3577 * This code looks for tabs in the text and coverts them to spaces.
3578 * The trick is getting the correct number of spaces,
3579 * and not going over the window's size without forcing
3580 * the window larger.
3582 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
3583 if (tmpstring1[i] == '\t') // 9 is ascii tab
3587 i2 < (8 - (1 + pos) % 8) && added <= max;
3590 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
3591 fprintf(stderr,"buffer overrun detected\n");
3593 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
3598 if (tmpstring1[i] != 9) {
3600 if ( pos > TEXT_BUFFER_SIZE-1 )
3601 fprintf(stderr,"buffer overrun detected\n");
3603 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
3609 if (text_width == maximum_width) {
3610 /* this means the text is probably pushing the limit, so we'll chop it */
3611 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
3612 tmpstring2[strlen(tmpstring2)-1] = '\0';
3622 c.pixel = current_color;
3623 XQueryColor(display, DefaultColormap(display, screen), &c);
3626 c2.color.red = c.red;
3627 c2.color.green = c.green;
3628 c2.color.blue = c.blue;
3629 c2.color.alpha = fonts[selected_font].font_alpha;
3631 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3632 cur_x, cur_y, (XftChar8 *) s,
3635 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
3636 cur_x, cur_y, (XftChar8 *) s,
3642 XDrawString(display, window.drawable, window.gc,
3643 cur_x, cur_y, s, strlen(s));
3645 cur_x += width_of_s;
3647 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
3650 long redmask, greenmask, bluemask;
3652 void set_up_gradient()
3655 colour_depth = DisplayPlanes(display, screen);
3659 if (colour_depth != 24 && colour_depth != 16) {
3660 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
3666 for(i = (colour_depth / 3)-1; i>=0; i--) {
3668 greenmask |= 1 << i;
3671 if (colour_depth%3 == 1) {
3672 greenmask |= 1 << (colour_depth / 3);
3674 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
3675 greenmask = greenmask << (colour_depth / 3);
3678 inline unsigned long do_gradient(unsigned long first_colour, unsigned long last_colour) { /* this function returns the next colour between two colours for a gradient */
3680 int red1, green1, blue1; // first colour
3681 int red2, green2, blue2; // second colour
3682 int red3 = 0, green3 = 0, blue3 = 0; // difference
3683 short redshift = (2*colour_depth / 3 + colour_depth%3);
3684 short greenshift = (colour_depth / 3);
3685 red1 = (first_colour & redmask) >> redshift;
3686 green1 = (first_colour & greenmask) >> greenshift;
3687 blue1 = first_colour & bluemask;
3688 red2 = (last_colour & redmask) >> redshift;
3689 green2 = (last_colour & greenmask) >> greenshift;
3690 blue2 = last_colour & bluemask;
3697 if (green1 > green2) {
3700 if (green1 < green2) {
3703 if (blue1 > blue2) {
3706 if (blue1 < blue2) {
3721 if (red1 > bluemask) {
3724 if (green1 > bluemask) {
3727 if (blue1 > bluemask) {
3730 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
3734 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
3735 if (colour_depth == 0) {
3738 int red1, green1, blue1; // first colour
3739 int red2, green2, blue2; // second colour
3740 long redshift = (2*colour_depth / 3 + colour_depth%3);
3741 long greenshift = (colour_depth / 3);
3742 int red3 = 0, green3 = 0, blue3 = 0; // difference
3743 red1 = (first_colour & redmask) >> redshift;
3744 green1 = (first_colour & greenmask) >> greenshift;
3745 blue1 = first_colour & bluemask;
3746 red2 = (last_colour & redmask) >> redshift;
3747 green2 = (last_colour & greenmask) >> greenshift;
3748 blue2 = last_colour & bluemask;
3749 red3 = abs(red1 - red2);
3750 green3 = abs(green1 - green2);
3751 blue3 = abs(blue1 - blue2);
3760 static void draw_line(char *s)
3764 cur_x = text_start_x;
3765 cur_y += font_ascent();
3767 short font_h = font_height();
3769 /* find specials and draw stuff */
3772 if (*p == SPECIAL_CHAR) {
3775 /* draw string before special */
3782 switch (specials[special_index].type) {
3783 case HORIZONTAL_LINE:
3786 specials[special_index].height;
3787 int mid = font_ascent() / 2;
3788 w = text_start_x + text_width -
3791 XSetLineAttributes(display,
3796 XDrawLine(display, window.drawable,
3807 specials[special_index].height;
3809 specials[special_index].arg;
3810 int mid = font_ascent() / 2;
3811 char ss[2] = { s, s };
3812 w = text_start_x + text_width -
3815 XSetLineAttributes(display,
3820 XSetDashes(display, window.gc, 0,
3822 XDrawLine(display, window.drawable,
3832 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3836 specials[special_index].height;
3838 specials[special_index].arg;
3843 by = cur_y - (font_ascent() + h) / 2 - 1;
3847 by = cur_y - (font_ascent()/2) - 1;
3849 if (h < (font_height())) {
3852 w = specials[special_index].width;
3855 text_width - cur_x - 1;
3859 XSetLineAttributes(display,
3865 XDrawRectangle(display,
3869 XFillRectangle(display,
3873 w * bar_usage / 255,
3875 if (specials[special_index].
3877 && specials[special_index].
3881 [special_index].height;
3888 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
3892 specials[special_index].height;
3896 by = cur_y - (font_ascent() + h) / 2 - 1;
3900 by = cur_y - (font_ascent()/2) - 1;
3902 if (h < (font_height())) {
3905 w = specials[special_index].width;
3907 w = text_start_x + text_width - cur_x - 1;
3910 XSetLineAttributes(display,
3915 XDrawRectangle(display,
3919 XSetLineAttributes(display,
3926 int gradient_size = 0;
3927 float gradient_factor = 0;
3928 float gradient_update = 0;
3929 unsigned long tmpcolour = current_color;
3930 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3931 tmpcolour = specials[special_index].first_colour;
3932 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3933 gradient_factor = (float)gradient_size / (w - 3);
3935 for (i = 0; i < w - 3; i++) {
3936 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3937 XSetForeground(display, window.gc, tmpcolour);
3938 gradient_update += gradient_factor;
3939 while (gradient_update > 0) {
3940 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3944 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3947 XDrawLine(display, window.drawable, window.gc, cur_x + i + 2, by + h, cur_x + i + 2, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale); /* this is mugfugly, but it works */
3949 if (specials[special_index].
3951 && specials[special_index].
3955 [special_index].height;
3958 if (draw_mode == BG) {
3959 set_foreground_color(default_bg_color);
3961 else if (draw_mode == OUTLINE) {
3962 set_foreground_color(default_out_color);
3964 set_foreground_color(default_fg_color);
3970 cur_y -= font_ascent();
3971 selected_font = specials[special_index].font_added;
3972 cur_y += font_ascent();
3974 if (!use_xft || use_xdbe)
3982 if (draw_mode == FG)
3983 set_foreground_color(specials
3989 if (draw_mode == BG)
3990 set_foreground_color(specials
3996 if (draw_mode == OUTLINE)
3997 set_foreground_color(specials
4004 w += specials[special_index].arg;
4009 cur_y += specials[special_index].arg;
4015 int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
4016 /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i border_margin %i border_width %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width_special(s), gap_x, specials[special_index].arg, border_margin, border_width);*/
4017 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
4018 cur_x = pos_x - specials[special_index].arg;
4025 int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
4026 /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
4027 /*printf("pos_x %i text_start_x %i text_width %i cur_x %i get_string_width(p) %i gap_x %i specials[special_index].arg %i\n", pos_x, text_start_x, text_width, cur_x, get_string_width(s), gap_x, specials[special_index].arg);*/
4029 specials[special_index].arg)
4032 [special_index].arg;
4049 if (cur_y_add > 0) {
4051 cur_y -= font_descent();
4056 cur_y += font_descent();
4063 static void draw_text()
4066 cur_y = text_start_y;
4069 if (draw_borders && border_width > 0) {
4070 unsigned int b = (border_width + 1) / 2;
4072 if (stippled_borders) {
4074 { stippled_borders, stippled_borders };
4075 XSetLineAttributes(display, window.gc,
4076 border_width, LineOnOffDash,
4077 CapButt, JoinMiter);
4078 XSetDashes(display, window.gc, 0, ss, 2);
4080 XSetLineAttributes(display, window.gc,
4081 border_width, LineSolid,
4082 CapButt, JoinMiter);
4085 XDrawRectangle(display, window.drawable, window.gc,
4086 text_start_x - border_margin + b,
4087 text_start_y - border_margin + b,
4088 text_width + border_margin * 2 - 1 - b * 2,
4089 text_height + border_margin * 2 - 1 -
4096 for_each_line(text_buffer, draw_line);
4099 static void draw_stuff()
4102 if (draw_shades && !draw_outline) {
4105 set_foreground_color(default_bg_color);
4114 for (i = -1; i < 2; i++)
4115 for (j = -1; j < 2; j++) {
4116 if (i == 0 && j == 0)
4120 set_foreground_color(default_out_color);
4121 draw_mode = OUTLINE;
4128 set_foreground_color(default_fg_color);
4136 swap.swap_window = window.window;
4137 swap.swap_action = XdbeBackground;
4138 XdbeSwapBuffers(display, &swap, 1);
4144 static void clear_text(int exposures)
4148 return; /* The swap action is XdbeBackground, which clears */
4152 /* there is some extra space for borders and outlines */
4153 XClearArea(display, window.drawable,
4154 text_start_x - border_margin - 1,
4155 text_start_y - border_margin - 1,
4156 text_width + border_margin * 2 + 2,
4157 text_height + border_margin * 2 + 2,
4158 exposures ? True : 0);
4163 static int need_to_update;
4165 /* update_text() generates new text and clears old text area */
4166 static void update_text()
4175 static void main_loop()
4177 #ifdef SIGNAL_BLOCKING
4178 sigset_t newmask, oldmask;
4180 sigemptyset(&newmask);
4181 sigaddset(&newmask,SIGINT);
4182 sigaddset(&newmask,SIGTERM);
4183 sigaddset(&newmask,SIGUSR1);
4187 Region region = XCreateRegion();
4191 while (total_run_times == 0 || info.looped < total_run_times - 1) {
4194 #ifdef SIGNAL_BLOCKING
4195 /* block signals. we will inspect for pending signals later */
4196 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
4197 CRIT_ERR("unable to sigprocmask()");
4203 /* wait for X event or timeout */
4205 if (!XPending(display)) {
4210 update_interval - (get_time() -
4216 tv.tv_sec = (long) t;
4217 tv.tv_usec = (long) (t * 1000000) % 1000000;
4219 FD_SET(ConnectionNumber(display), &fdsr);
4222 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
4225 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
4230 ERR("can't select(): %s",
4241 set_transparent_background(window.window);
4246 if (need_to_update) {
4248 int wx = window.x, wy = window.y;
4256 /* resize window if it isn't right size */
4258 (text_width + border_margin * 2 !=
4260 || text_height + border_margin * 2 !=
4264 border_margin * 2 + 1;
4267 border_margin * 2 + 1;
4268 XResizeWindow(display,
4274 /* move window if it isn't in right position */
4277 || window.y != wy)) {
4278 XMoveWindow(display, window.window,
4279 window.x, window.y);
4289 r.x = text_start_x - border_margin;
4290 r.y = text_start_y - border_margin;
4291 r.width = text_width + border_margin * 2;
4292 r.height = text_height + border_margin * 2;
4293 XUnionRectWithRegion(&r, region, region);
4298 /* handle X events */
4300 while (XPending(display)) {
4302 XNextEvent(display, &ev);
4309 r.width = ev.xexpose.width;
4310 r.height = ev.xexpose.height;
4311 XUnionRectWithRegion(&r, region,
4317 case ReparentNotify:
4318 /* set background to ParentRelative for all parents */
4320 set_transparent_background(window.
4325 case ConfigureNotify:
4327 /* if window size isn't what expected, set fixed size */
4328 if (ev.xconfigure.width !=
4330 || ev.xconfigure.height !=
4332 if (window.width != 0
4333 && window.height != 0)
4336 /* clear old stuff before screwing up size and pos */
4342 if (XGetWindowAttributes(display, window.window, &attrs)) {
4357 border_margin * 2 - 1;
4360 border_margin * 2 - 1;
4361 if (text_width > maximum_width && maximum_width > 0)
4362 text_width = maximum_width;
4365 /* if position isn't what expected, set fixed pos, total_updates
4366 * avoids setting fixed_pos when window is set to weird locations
4368 /*if (total_updates >= 2 this is broken
4370 && (window.x != ev.xconfigure.x
4373 && (ev.xconfigure.x != 0
4374 || ev.xconfigure.y != 0)) {
4387 /* XDBE doesn't seem to provide a way to clear the back buffer without
4388 * interfering with the front buffer, other than passing XdbeBackground
4389 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
4390 * redraw the text even if it wasn't part of the exposed area. OTOH,
4391 * if we're not going to call draw_stuff at all, then no swap happens
4392 * and we can safely do nothing.
4395 if (!XEmptyRegion(region)) {
4399 r.x = text_start_x - border_margin;
4400 r.y = text_start_y - border_margin;
4401 r.width = text_width + border_margin * 2;
4402 r.height = text_height + border_margin * 2;
4403 XUnionRectWithRegion(&r, region, region);
4406 XSetRegion(display, window.gc, region);
4409 XftDrawSetClip(window.xftdraw, region);
4415 XDestroyRegion(region);
4416 region = XCreateRegion();
4420 #ifdef SIGNAL_BLOCKING
4421 /* unblock signals of interest and let handler fly */
4422 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
4423 CRIT_ERR("unable to sigprocmask()");
4426 switch(g_signal_pending) {
4429 ERR("received SIGUSR1. reloading the config file.");
4436 ERR("received SIGINT or SIGTERM to terminate. bye!");
4439 XDestroyRegion(region);
4442 return; /* return from main_loop */
4447 /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
4448 * but didn't write any code to deal with it. if you don't want to
4449 * handle a signal, don't set a handler on it in the first place. */
4450 if (g_signal_pending)
4451 ERR("ignoring signal (%d)", g_signal_pending);
4459 XDestroyRegion(region);
4464 static void load_config_file(const char *);
4466 /* reload the config file */
4467 void reload_config(void)
4469 if (current_config) {
4471 load_config_file(current_config);
4475 XClearWindow(display, RootWindow(display, screen)); // clear the window first
4478 #ifdef TCP_PORT_MONITOR
4479 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
4480 info.p_tcp_port_monitor_collection = NULL;
4482 extract_variable_text(text);
4494 XdbeDeallocateBackBufferName(display, window.back_buffer);
4499 XDestroyWindow(display, window.window);
4503 XClearWindow(display, RootWindow(display, screen));
4508 XFreeGC(display, window.gc);
4512 /* it is really pointless to free() memory at the end of program but ak|ra
4513 * wants me to do this */
4515 free_text_objects(text_object_count, text_objects);
4516 text_object_count = 0;
4517 text_objects = NULL;
4521 #endif /* MLDONKEY */
4523 if (text != original_text)
4526 free(current_config);
4527 free(current_mail_spool);
4531 #ifdef TCP_PORT_MONITOR
4532 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
4533 info.p_tcp_port_monitor_collection = NULL;
4537 static int string_to_bool(const char *s)
4541 if (strcasecmp(s, "yes") == 0)
4543 if (strcasecmp(s, "true") == 0)
4545 if (strcasecmp(s, "1") == 0)
4550 static enum alignment string_to_alignment(const char *s)
4552 if (strcasecmp(s, "top_left") == 0)
4554 else if (strcasecmp(s, "top_right") == 0)
4556 else if (strcasecmp(s, "bottom_left") == 0)
4558 else if (strcasecmp(s, "bottom_right") == 0)
4559 return BOTTOM_RIGHT;
4560 else if (strcasecmp(s, "tl") == 0)
4562 else if (strcasecmp(s, "tr") == 0)
4564 else if (strcasecmp(s, "bl") == 0)
4566 else if (strcasecmp(s, "br") == 0)
4567 return BOTTOM_RIGHT;
4568 else if (strcasecmp(s, "none") == 0)
4575 static void set_default_configurations(void)
4577 fork_to_background = 0;
4578 total_run_times = 0;
4579 info.cpu_avg_samples = 2;
4580 info.net_avg_samples = 2;
4585 strcpy(info.mpd.host, "localhost");
4586 info.mpd.port = 6600;
4587 info.mpd.status = "Checking status...";
4596 default_fg_color = WhitePixel(display, screen);
4597 default_bg_color = BlackPixel(display, screen);
4598 default_out_color = BlackPixel(display, screen);
4602 set_first_font("6x10");
4610 strcpy(wm_class_name, "conky");
4612 stippled_borders = 0;
4615 text_alignment = BOTTOM_LEFT;
4619 free(current_mail_spool);
4622 variable_substitute(MAIL_FILE, buf, 256);
4624 current_mail_spool = strdup(buf);
4628 update_interval = 10.0;
4629 stuff_in_upper_case = 0;
4631 mlconfig.mldonkey_hostname = strdup("127.0.0.1");
4632 mlconfig.mldonkey_port = 4001;
4633 mlconfig.mldonkey_login = NULL;
4634 mlconfig.mldonkey_password = NULL;
4637 #ifdef TCP_PORT_MONITOR
4638 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
4639 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
4643 static void load_config_file(const char *f)
4645 #define CONF_ERR ERR("%s: %d: config file error", f, line)
4649 set_default_configurations();
4655 char buf[256], *p, *p2, *name, *value;
4657 if (fgets(buf, 256, fp) == NULL)
4662 /* break at comment */
4663 p2 = strchr(p, '#');
4668 while (*p && isspace((int) *p))
4671 continue; /* empty line */
4677 while (*p2 && !isspace((int) *p2))
4680 *p2 = '\0'; /* break at name's end */
4687 while (*p && isspace((int) *p))
4692 p2 = value + strlen(value);
4693 while (isspace((int) *(p2 - 1)))
4699 #define CONF2(a) if (strcasecmp(name, a) == 0)
4700 #define CONF(a) else CONF2(a)
4701 #define CONF3(a,b) \
4702 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
4706 CONF2("alignment") {
4708 int a = string_to_alignment(value);
4718 on_bottom = string_to_bool(value);
4722 CONF("background") {
4723 fork_to_background = string_to_bool(value);
4727 CONF2("background") {
4728 fork_to_background = string_to_bool(value);
4732 CONF("border_margin") {
4734 border_margin = strtol(value, 0, 0);
4738 CONF("border_width") {
4740 border_width = strtol(value, 0, 0);
4744 CONF("default_color") {
4746 default_fg_color = get_x11_color(value);
4750 CONF3("default_shade_color", "default_shadecolor") {
4752 default_bg_color = get_x11_color(value);
4756 CONF3("default_outline_color", "default_outlinecolor") {
4758 default_out_color = get_x11_color(value);
4766 strcpy(info.mpd.host, value);
4772 info.mpd.port = strtol(value, 0, 0);
4773 if (info.mpd.port < 1
4774 || info.mpd.port > 0xffff)
4779 CONF("cpu_avg_samples") {
4781 cpu_avg_samples = strtol(value, 0, 0);
4782 if (cpu_avg_samples < 1
4783 || cpu_avg_samples > 14)
4792 CONF("net_avg_samples") {
4794 net_avg_samples = strtol(value, 0, 0);
4795 if (net_avg_samples < 1
4796 || net_avg_samples > 14)
4812 CONF("double_buffer") {
4813 use_xdbe = string_to_bool(value);
4817 CONF("override_utf8_locale") {
4818 utf8_mode = string_to_bool(value);
4821 CONF("draw_borders") {
4822 draw_borders = string_to_bool(value);
4824 CONF("draw_shades") {
4825 draw_shades = string_to_bool(value);
4827 CONF("draw_outline") {
4828 draw_outline = string_to_bool(value);
4831 CONF("out_to_console") {
4832 out_to_console = string_to_bool(value);
4834 CONF("use_spacer") {
4835 use_spacer = string_to_bool(value);
4840 use_xft = string_to_bool(value);
4845 set_first_font(value);
4851 if (value && font_count >= 0)
4852 fonts[0].font_alpha = atof(value)
4860 if (string_to_bool(value))
4861 ERR("Xft not enabled");
4864 /* xftfont silently ignored when no Xft */
4867 /* xftalpha is silently ignored when no Xft */
4872 set_first_font(value);
4878 gap_x = atoi(value);
4884 gap_y = atoi(value);
4889 CONF("mail_spool") {
4892 variable_substitute(value, buf, 256);
4896 if (current_mail_spool)
4897 free(current_mail_spool);
4898 current_mail_spool = strdup(buf);
4904 CONF("minimum_size") {
4907 (value, "%d %d", &minimum_width,
4908 &minimum_height) != 2)
4911 &minimum_width) != 1)
4916 CONF("maximum_width") {
4918 if (sscanf(value, "%d", &maximum_width) != 1)
4924 CONF("no_buffers") {
4925 no_buffers = string_to_bool(value);
4928 CONF("mldonkey_hostname") {
4930 if (mlconfig.mldonkey_hostname != NULL) {
4931 free(mlconfig.mldonkey_hostname);
4933 mlconfig.mldonkey_hostname = strdup(value);
4938 CONF("mldonkey_port") {
4940 mlconfig.mldonkey_port = atoi(value);
4944 CONF("mldonkey_login") {
4946 if (mlconfig.mldonkey_login != NULL) {
4947 free(mlconfig.mldonkey_login);
4949 mlconfig.mldonkey_login = strdup(value);
4954 CONF("mldonkey_password") {
4956 if (mlconfig.mldonkey_password != NULL) {
4957 free(mlconfig.mldonkey_password);
4959 mlconfig.mldonkey_password = strdup(value);
4965 CONF("pad_percents") {
4966 pad_percents = atoi(value);
4970 CONF("own_window") {
4971 own_window = string_to_bool(value);
4973 CONF("wm_class_name") {
4974 strncpy(wm_class_name, value, sizeof(wm_class_name)-1);
4975 wm_class_name[sizeof(wm_class_name)-1] = 0;
4977 CONF("own_window_transparent") {
4978 set_transparent = string_to_bool(value);
4980 CONF("own_window_colour") {
4981 background_colour = get_x11_color(value);
4984 CONF("stippled_borders") {
4986 stippled_borders = strtol(value, 0, 0);
4988 stippled_borders = 4;
4992 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
4995 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
4997 CONF("update_interval") {
4999 update_interval = strtod(value, 0);
5003 CONF("total_run_times") {
5005 total_run_times = strtod(value, 0);
5010 stuff_in_upper_case = string_to_bool(value);
5015 malloc(strlen(value)
5017 strcpy(seti_dir, value);
5021 if (text != original_text)
5031 int l = strlen(text);
5032 if (fgets(buf, 256, fp) == NULL)
5035 realloc(text, l + strlen(buf)
5039 if (strlen(text) > 1024 * 8)
5045 #ifdef TCP_PORT_MONITOR
5046 CONF("min_port_monitors")
5049 (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) ||
5050 tcp_port_monitor_collection_args.min_port_monitors < 0 )
5052 /* an error. use default, warn and continue. */
5053 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5056 else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
5058 /* no error, just use default */
5059 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5061 /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
5063 CONF("min_port_monitor_connections")
5066 (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1)
5067 || tcp_port_monitor_args.min_port_monitor_connections < 0 )
5069 /* an error. use default, warni and continue. */
5070 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5073 else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
5075 /* no error, just use default */
5076 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5078 /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
5082 ERR("%s: %d: no such configuration: '%s'", f, line, name);
5092 /* : means that character before that takes an argument */
5093 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
5106 int main(int argc, char **argv)
5108 struct sigaction act, oact;
5111 memset(&info, 0, sizeof(info) );
5113 #ifdef TCP_PORT_MONITOR
5114 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
5115 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
5118 /* handle command line parameters that don't change configs */
5124 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
5125 *s) || ((s = getenv("LANG")) && *s)) {
5127 for(x = 0; x < strlen(s) ; x++) {
5128 temp[x] = tolower(s[x]);
5130 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
5134 if (!setlocale(LC_CTYPE, "")) {
5135 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
5139 int c = getopt(argc,
5149 ("Conky " VERSION " compiled " __DATE__ "\n");
5153 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
5154 * though free() does the NULL check itself;), then load optarg value */
5156 free(current_config);
5157 current_config = strdup(optarg);
5162 ("Usage: %s [OPTION]...\n"
5163 "Conky is a system monitor that renders text on desktop or to own transparent\n"
5164 "window. Command line options will override configurations defined in config\n"
5167 " -c FILE config file to load instead of "
5170 " -d daemonize, fork to background\n"
5173 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
5174 " -f FONT font to use\n"
5176 " -o create own window to draw\n"
5179 " -b double buffer (prevents flickering)\n"
5181 " -w WIN_ID window id to draw\n"
5182 " -x X x position\n"
5183 " -y Y y position\n"
5185 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
5186 " -u SECS update interval\n"
5187 " -i NUM number of times to update Conky\n", argv[0]);
5191 window.window = strtol(optarg, 0, 0);
5200 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
5204 /* load current_config or CONFIG_FILE */
5207 if (current_config == NULL) {
5208 /* load default config file */
5211 variable_substitute(CONFIG_FILE, buf, 256);
5214 current_config = strdup(buf);
5218 if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
5219 load_config_file(current_config);
5221 set_default_configurations();
5225 if (current_mail_spool == NULL) {
5227 variable_substitute(MAIL_FILE, buf, 256);
5230 current_mail_spool = strdup(buf);
5234 /* handle other command line arguments */
5236 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
5237 optind = optreset = 1;
5243 int c = getopt(argc,
5251 fork_to_background = 1;
5256 set_first_font(optarg);
5259 text_alignment = string_to_alignment(optarg);
5274 if (text != original_text)
5276 text = strdup(optarg);
5277 convert_escapes(text);
5281 update_interval = strtod(optarg, 0);
5285 total_run_times = strtod(optarg, 0);
5289 gap_x = atoi(optarg);
5293 gap_y = atoi(optarg);
5307 /* generate text and get initial size */
5308 extract_variable_text(text);
5309 if (text != original_text) {
5318 update_text_area(); /* to get initial size of the window */
5320 #if defined OWN_WINDOW
5324 text_width + border_margin * 2 + 1,
5325 text_height + border_margin * 2 + 1,
5326 on_bottom, fixed_pos, set_transparent, background_colour, info.uname_s.nodename);
5330 text_width + border_margin * 2 + 1,
5331 text_height + border_margin * 2 + 1,
5332 on_bottom, set_transparent, background_colour, info.uname_s.nodename);
5336 update_text_area(); /* to position text/window on screen */
5340 // why the fuck not?
5346 if (own_window && !fixed_pos) {
5347 XMoveWindow(display, window.window, window.x, window.y);
5350 set_transparent_background(window.window);
5361 if (fork_to_background) {
5365 ERR("can't fork() to background: %s",
5375 "Conky: forked to background, pid is %d\n",
5381 /* Set signal handlers */
5382 act.sa_handler = signal_handler;
5383 sigemptyset(&act.sa_mask);
5386 act.sa_flags |= SA_RESTART;
5389 if ( sigaction(SIGINT,&act,&oact) < 0 ||
5390 sigaction(SIGUSR1,&act,&oact) < 0 ||
5391 sigaction(SIGTERM,&act,&oact) < 0 )
5393 ERR("error setting signal handler: %s", strerror(errno) );
5401 void signal_handler(int sig)
5403 /* signal handler is light as a feather, as it should be.
5404 * we will poll g_signal_pending with each loop of conky
5405 * and do any signal processing there, NOT here. pkovacs. */
5406 g_signal_pending=sig;