2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
26 #include <X11/Xutil.h>
28 #include <X11/extensions/Xdamage.h>
34 #include <sys/types.h>
36 #include <netinet/in.h>
46 #define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
49 #define CONFIG_FILE "$HOME/.conkyrc"
50 #define MAIL_FILE "$MAIL"
51 #define MAX_IF_BLOCK_DEPTH 5
53 /* #define SIGNAL_BLOCKING */
54 #undef SIGNAL_BLOCKING
56 static void print_version()
58 printf("Conky %s compiled %s for %s\n",
59 VERSION, BUILD_DATE, BUILD_ARCH);
62 "\nCompiled in features:\n\n"
66 " * Xdamage extension\n"
67 # endif /* HAVE_XDAMAGE */
69 " * Xdbe extension (double buffer)\n"
70 # endif /* HAVE_XDBE */
76 "\n Music detection:\n"
79 #endif /* AUDACIOUS */
89 "\n General features:\n"
93 #ifdef TCP_PORT_MONITOR
95 #endif /* TCP_PORT_MONITOR */
110 static int text_start_x, text_start_y; /* text start position in window */
111 static int text_width, text_height;
126 char name[TEXT_BUFFER_SIZE];
136 static int selected_font = 0;
137 static int font_count = -1;
138 struct font_list *fonts = NULL;
142 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
143 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent))
144 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent)
145 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent)
149 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
150 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
151 #define font_descent() fonts[selected_font].font->max_bounds.descent
155 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
158 static void set_font();
160 int addfont(const char *data_in)
162 if (font_count > MAX_FONTS) {
163 CRIT_ERR("you don't need that many fonts, sorry.");
166 if (font_count == 0) {
170 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
174 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
176 CRIT_ERR("realloc in addfont");
178 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
179 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
181 fonts[font_count].font_alpha = 0xffff;
184 CRIT_ERR("Oops...looks like something overflowed in addfont().");
189 void set_first_font(const char *data_in)
191 if (font_count < 0) {
192 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
197 if (strlen(data_in) > 1) {
198 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
200 fonts[0].font_alpha = 0xffff;
208 for (i=0;i<=font_count;i++) {
211 XftFontClose(display, fonts[i].xftfont);
215 XFreeFont(display, fonts[i].font);
222 set_first_font("6x10");
226 static void load_fonts()
229 for (i=0; i <= font_count; i++) {
233 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
234 XftFontClose(display, fonts[i].xftfont);
236 if ((fonts[i].xftfont =
237 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
240 ERR("can't load Xft font '%s'", fonts[i].name);
241 if ((fonts[i].xftfont =
242 XftFontOpenName(display, screen,
243 "courier-12")) != NULL)
246 ERR("can't load Xft font '%s'", "courier-12");
248 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
249 CRIT_ERR("can't load font '%s'", "fixed");
256 /* load normal font */
257 /* if (fonts[i].font != NULL)
258 XFreeFont(display, fonts[i].font);*/
260 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
261 ERR("can't load font '%s'", fonts[i].name);
262 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
263 CRIT_ERR("can't load font '%s'", "fixed");
264 printf("loaded fixed?\n");
272 /* default config file */
273 static char *current_config;
275 /* set to 1 if you want all text to be in uppercase */
276 static unsigned int stuff_in_upper_case;
278 /* Run how many times? */
279 static unsigned long total_run_times;
282 static int fork_to_background;
284 static int cpu_avg_samples, net_avg_samples;
288 /* Position on the screen */
289 static int text_alignment;
290 static int gap_x, gap_y;
293 static int draw_borders;
294 static int draw_graph_borders;
295 static int stippled_borders;
297 static int draw_shades, draw_outline;
299 static int border_margin, border_width;
301 static long default_fg_color, default_bg_color, default_out_color;
303 /* create own window or draw stuff to root? */
304 static int set_transparent = 0;
308 static int own_window = 0;
309 static int background_colour = 0;
310 /* fixed size/pos is set if wm/user changes them */
311 static int fixed_size = 0, fixed_pos = 0;
314 static int minimum_width, minimum_height;
315 static int maximum_width;
319 /* maximum number of special things, e.g. fonts, offsets, aligns, etc. */
320 static unsigned int max_specials = MAX_SPECIALS_DEFAULT;
322 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
323 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
326 #define CODEPAGE_LENGTH 20
329 char iconv_converting;
330 static iconv_t **iconv_cd = 0;
332 int register_iconv(iconv_t *new_iconv)
335 iconv_cd = realloc(iconv, sizeof(iconv_t *) * (iconv_count + 1));
337 iconv_cd = malloc(sizeof(iconv_t *));
340 CRIT_ERR("Out of memory");
342 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
343 if (!iconv_cd[iconv_count]) {
344 CRIT_ERR("Out of memory");
346 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
351 void free_iconv(void)
355 for (i = iconv_count; i < 0; i++) {
371 /* no buffers in used memory? */
374 /* pad percentages to decimals? */
375 static int pad_percents = 0;
377 #ifdef TCP_PORT_MONITOR
378 tcp_port_monitor_args_t tcp_port_monitor_args;
381 #ifdef HAVE_LIBDEXTER
382 /* private config items for libdexter */
383 static char *dexter_config = NULL;
386 /* Text that is shown */
387 static char original_text[] =
388 "$nodename - $sysname $kernel on $machine\n"
390 "${color grey}Uptime:$color $uptime\n"
391 "${color grey}Frequency (in MHz):$color $freq\n"
392 "${color grey}Frequency (in GHz):$color $freq_g\n"
393 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
394 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
395 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
396 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
398 "${color grey}File systems:\n"
399 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
400 "${color grey}Networking:\n"
401 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
404 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
405 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
408 "${color grey}XMMS2: $xmms2_status $xmms2_artist - $xmms2_title from $xmms2_album\n"
409 "Progress: $xmms2_bar\n"
411 "${color grey}Name PID CPU% MEM%\n"
412 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
413 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
414 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
415 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
416 "${tail /var/log/Xorg.0.log 3}";
418 static char *text = original_text;
421 static int total_updates;
424 static int blockdepth = 0;
425 static int if_jumped = 0;
426 static int blockstart[MAX_IF_BLOCK_DEPTH];
428 int check_mount(char *s)
430 #if defined(__linux__)
432 FILE *mtab = fopen("/etc/mtab", "r");
434 char buf1[256], buf2[128];
435 while (fgets(buf1, 256, mtab)) {
436 sscanf(buf1, "%*s %128s", buf2);
437 if (!strcmp(s, buf2)) {
444 ERR("Could not open mtab");
447 #elif defined(__FreeBSD__)
448 struct statfs *mntbuf;
451 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
452 for (i = mntsize - 1; i >= 0; i--)
453 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
462 static inline int calc_text_width(const char *s, int l)
468 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
470 XftTextExtents8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
476 return XTextWidth(fonts[selected_font].font, s, l);
481 /* formatted text to render on screen, generated in generate_text(),
482 * drawn in draw_stuff() */
484 static char text_buffer[TEXT_BUFFER_SIZE * 4];
486 /* special stuff in text_buffer */
488 #define SPECIAL_CHAR '\x01'
517 unsigned long first_colour; // for graph gradient
518 unsigned long last_colour;
521 /* create specials array on heap instead of stack with introduction of max_specials */
522 static struct special_t *specials = NULL;
524 static unsigned int special_count;
526 static unsigned int special_index; /* used when drawing */
529 #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? */
531 static struct special_t *new_special(char *buf, int t)
533 if (special_count >= max_specials)
534 CRIT_ERR("too many special things in text");
536 buf[0] = SPECIAL_CHAR;
538 specials[special_count].type = t;
539 return &specials[special_count++];
542 long fwd_fcharfind(FILE* fp, char val, unsigned int step) {
545 unsigned int count = 0;
546 static char buf[BUFSZ];
547 long orig_pos = ftell(fp);
549 long buf_size = BUFSZ;
550 char* cur_found = NULL;
551 while(count < step) {
552 if(cur_found == NULL) {
553 buf_size = fread(buf, 1, buf_size, fp);
556 cur_found = memchr(buf+buf_pos, val, buf_size-buf_pos);
557 if(cur_found != NULL) {
558 buf_pos = cur_found-buf+1;
567 ret = ftell(fp) - buf_size + buf_pos - 1;
568 fseek(fp, orig_pos, SEEK_SET);
573 long rev_fcharfind(FILE* fp, char val, unsigned int step) {
576 unsigned int count = 0;
577 static char buf[BUFSZ];
578 long orig_pos = ftell(fp);
580 long file_pos = orig_pos;
581 long buf_size = BUFSZ;
583 while(count < step) {
585 if(file_pos > BUFSZ) {
586 fseek(fp, file_pos-BUFSZ, SEEK_SET);
590 fseek(fp, 0, SEEK_SET);
592 file_pos = ftell(fp);
593 buf_pos = fread(buf, 1, buf_size, fp);
600 if(cur_found != NULL) {
601 buf_pos = cur_found-buf;
610 fseek(fp, orig_pos, SEEK_SET);
612 ret = file_pos + buf_pos;
617 static void new_bar(char *buf, int w, int h, int usage)
619 struct special_t *s = new_special(buf, BAR);
620 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
625 static const char *scan_bar(const char *args, int *w, int *h)
627 *w = 0; /* zero width means all space that is available */
629 /* bar's argument is either height or height,width */
632 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
633 sscanf(args, "%d %n", h, &n);
640 static char *scan_font(const char *args)
642 if (args && sizeof(args) < 127) {
649 static void new_font(char *buf, char * args) {
651 struct special_t *s = new_special(buf, FONT);
652 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
653 int tmp = selected_font;
654 selected_font = s->font_added = addfont(args);
659 struct special_t *s = new_special(buf, FONT);
660 int tmp = selected_font;
661 selected_font = s->font_added = 0;
668 inline void graph_append(struct special_t *graph, double f)
670 if (!graph->scaled && f > graph->graph_scale) {
671 f = graph->graph_scale;
675 graph->graph_scale = 1;
677 graph->graph[0] = f; /* add new data */
678 for (i = graph->graph_width - 1; i > 0; i--) { /* shift all the data by 1 */
679 graph->graph[i] = graph->graph[i - 1];
680 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
681 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
686 short colour_depth = 0;
687 void set_up_gradient();
689 /* precalculated: 31/255, and 63/255 */
690 #define CONST_8_TO_5_BITS 0.12156862745098
691 #define CONST_8_TO_6_BITS 0.247058823529412
693 /* adjust color values depending on color depth*/
694 static unsigned int adjust_colors(unsigned int color)
697 if (colour_depth == 0) {
700 if (colour_depth == 16) {
701 r = (color & 0xff0000) >> 16;
702 g = (color & 0xff00) >> 8;
704 color = (int)(r * CONST_8_TO_5_BITS) << 11;
705 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
706 color |= (int)(b * CONST_8_TO_5_BITS);
711 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
713 struct special_t *s = new_special(buf, GRAPH);
715 if (s->graph == NULL) {
716 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
717 s->graph_width = s->width/* - 2*/; // subtract 2 for the box
719 s->graph_width = MAX_GRAPH_DEPTH - 2;
721 s->graph = malloc(s->graph_width * sizeof(double));
722 memset(s->graph, 0, s->graph_width * sizeof(double));
723 s->graph_scale = 100;
726 s->first_colour = adjust_colors(first_colour);
727 s->last_colour = adjust_colors(second_colour);
734 s->graph_width = s->width - 2; // subtract 2 for rectangle around
739 s->graph_scale = scale;
746 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
748 *w = 0; /* zero width means all space that is available */
752 /* graph's argument is either height or height,width */
754 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
755 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
757 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
759 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
762 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
766 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
769 if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
773 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
776 if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
780 if (sscanf(args, "%d,%d", h, w) < 2) {
783 sscanf(args, "%*s %d,%d", h, w);
789 static inline void new_hr(char *buf, int a)
791 new_special(buf, HORIZONTAL_LINE)->height = a;
794 static inline void new_stippled_hr(char *buf, int a, int b)
796 struct special_t *s = new_special(buf, STIPPLED_HR);
801 static inline void new_fg(char *buf, long c)
803 new_special(buf, FG)->arg = c;
806 static inline void new_bg(char *buf, long c)
808 new_special(buf, BG)->arg = c;
811 static inline void new_outline(char *buf, long c)
813 new_special(buf, OUTLINE)->arg = c;
816 static inline void new_offset(char *buf, long c)
818 new_special(buf, OFFSET)->arg = c;
821 static inline void new_voffset(char *buf, long c)
823 new_special(buf, VOFFSET)->arg = c;
826 static inline void new_alignr(char *buf, long c)
828 new_special(buf, ALIGNR)->arg = c;
831 static inline void new_alignc(char *buf, long c)
833 new_special(buf, ALIGNC)->arg = c;
836 static inline void new_goto(char *buf, long c)
838 new_special(buf, GOTO)->arg = c;
841 static inline void new_tab(char *buf, int a, int b) {
842 struct special_t *s = new_special(buf, TAB);
847 /* quite boring functions */
849 static inline void for_each_line(char *b, void (*f) (char *))
853 for (ps = b, pe = b; *pe; pe++) {
866 static void convert_escapes(char *buf)
868 char *p = buf, *s = buf;
884 /* converts from bytes to human readable format (k, M, G, T) */
885 static void human_readable(long long a, char *buf, int size)
887 // Strange conditional due to possible overflows
888 if(a / 1024 / 1024 / 1024.0 > 1024.0){
889 snprintf(buf, size, "%.2fTiB", (a / 1024 / 1024 / 1024) / 1024.0);
891 else if (a >= 1024 * 1024 * 1024) {
892 snprintf(buf, size, "%.2fGiB", (a / 1024 / 1024) / 1024.0);
894 else if (a >= 1024 * 1024) {
895 double m = (a / 1024) / 1024.0;
897 snprintf(buf, size, "%.0fMiB", m);
899 snprintf(buf, size, "%.1fMiB", m);
900 } else if (a >= 1024)
901 snprintf(buf, size, "%LdKiB", a / (long long) 1024);
903 snprintf(buf, size, "%LdB", a);
908 enum text_object_type {
930 OBJ_diskiograph_read,
931 OBJ_diskiograph_write,
964 #if defined(__linux__)
970 OBJ_i8k_left_fan_status,
971 OBJ_i8k_right_fan_status,
972 OBJ_i8k_left_fan_rpm,
973 OBJ_i8k_right_fan_rpm,
975 OBJ_i8k_buttons_status,
983 #endif /* __linux__ */
1010 OBJ_running_processes,
1019 OBJ_temp1, /* i2c is used instead in these */
1039 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
1041 OBJ_apm_battery_time,
1042 OBJ_apm_battery_life,
1043 #endif /* __FreeBSD__ */
1072 OBJ_xmms2_transport,
1087 OBJ_audacious_status,
1088 OBJ_audacious_title,
1089 OBJ_audacious_length,
1090 OBJ_audacious_length_seconds,
1091 OBJ_audacious_position,
1092 OBJ_audacious_position_seconds,
1093 OBJ_audacious_bitrate,
1094 OBJ_audacious_frequency,
1095 OBJ_audacious_channels,
1096 OBJ_audacious_filename,
1097 OBJ_audacious_playlist_length,
1098 OBJ_audacious_playlist_position,
1109 #ifdef TCP_PORT_MONITOR
1120 OBJ_entropy_poolsize,
1124 struct text_object {
1128 unsigned int c, d, e;
1132 char *s; /* some string */
1133 int i; /* some integer */
1134 long l; /* some other integer */
1135 unsigned int sensor;
1136 struct net_stat *net;
1138 unsigned char loadavg[3];
1139 unsigned int cpu_index;
1140 struct mail_s *mail;
1143 char *tz; /* timezone variable */
1144 char *fmt; /* time display formatting */
1194 timed_thread *p_timed_thread;
1200 #ifdef TCP_PORT_MONITOR
1202 in_port_t port_range_begin; /* starting port to monitor */
1203 in_port_t port_range_end; /* ending port to monitor */
1204 int item; /* enum from libtcp-portmon.h, e.g. COUNT, etc. */
1205 int connection_index; /* 0 to n-1 connections. */
1218 struct text_object_list {
1219 unsigned int text_object_count;
1220 struct text_object *text_objects;
1223 static unsigned int text_object_count;
1224 static struct text_object *text_objects;
1225 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
1227 #define MAXDATASIZE 1000
1231 struct mail_s* parse_mail_args(char type, const char *arg) {
1232 struct mail_s *mail;
1233 mail = malloc(sizeof(struct mail_s));
1234 memset(mail, 0, sizeof(struct mail_s));
1236 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass) != 3) {
1238 ERR("Scanning IMAP args failed");
1239 } else if (type == IMAP) {
1240 ERR("Scanning POP3 args failed");
1243 // see if password needs prompting
1244 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1245 int fp = fileno(stdin);
1246 struct termios term;
1247 tcgetattr(fp, &term);
1248 term.c_lflag &= ~ECHO;
1249 tcsetattr(fp, TCSANOW, &term);
1250 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1251 scanf("%128s", mail->pass);
1253 term.c_lflag |= ECHO;
1254 tcsetattr(fp, TCSANOW, &term);
1256 // now we check for optional args
1257 tmp = strstr(arg, "-i ");
1260 sscanf(tmp, "%f", &mail->interval);
1262 mail->interval = 300; // 5 minutes
1264 tmp = strstr(arg, "-p ");
1267 sscanf(tmp, "%lu", &mail->port);
1270 mail->port = 110; // default pop3 port
1271 } else if (type == IMAP) {
1272 mail->port = 143; // default imap port
1276 tmp = strstr(arg, "-f ");
1279 sscanf(tmp, "%s", mail->folder);
1281 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1284 tmp = strstr(arg, "-e ");
1288 if (tmp[0] == '\'') {
1289 len = strstr(tmp+1, "'") - tmp - 1;
1294 strncpy(mail->command, tmp+1, len);
1296 mail->command[0] = '\0';
1298 mail->p_timed_thread = NULL;
1302 void *imap_thread(struct mail_s* mail)
1304 int sockfd, numbytes;
1305 char recvbuf[MAXDATASIZE];
1306 char sendbuf[MAXDATASIZE];
1309 unsigned int old_unseen = UINT_MAX;
1310 unsigned int old_messages = UINT_MAX;
1311 struct stat stat_buf;
1313 struct sockaddr_in their_addr; // connector's address information
1314 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1315 herror("gethostbyname");
1320 ERR("Trying IMAP connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1322 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1325 goto next_iteration;
1328 their_addr.sin_family = AF_INET; // host byte order
1329 their_addr.sin_port = htons(mail->port); // short, network byte order
1330 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1331 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1334 (sockfd, (struct sockaddr *) &their_addr,
1335 sizeof(struct sockaddr)) == -1) {
1338 goto next_iteration;
1340 struct timeval timeout;
1343 timeout.tv_sec = 60; // 60 second timeout i guess
1344 timeout.tv_usec = 0;
1346 FD_SET(sockfd, &fdset);
1347 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1350 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1354 goto next_iteration;
1357 ERR("IMAP connection failed: timeout");
1359 goto next_iteration;
1361 recvbuf[numbytes] = '\0';
1362 if (strstr(recvbuf, "* OK") != recvbuf) {
1363 ERR("IMAP connection failed, probably not an IMAP server");
1365 goto next_iteration;
1367 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1368 strncat(sendbuf, mail->user,
1369 MAXDATASIZE - strlen(sendbuf) - 1);
1370 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1371 strncat(sendbuf, mail->pass,
1372 MAXDATASIZE - strlen(sendbuf) - 1);
1373 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1374 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1377 goto next_iteration;
1379 timeout.tv_sec = 60; // 60 second timeout i guess
1380 timeout.tv_usec = 0;
1382 FD_SET(sockfd, &fdset);
1383 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1386 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1390 goto next_iteration;
1393 recvbuf[numbytes] = '\0';
1394 if (strstr(recvbuf, "a1 OK") == NULL) {
1395 ERR("IMAP server login failed: %s", recvbuf);
1397 goto next_iteration;
1399 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1400 strncat(sendbuf, mail->folder,
1401 MAXDATASIZE - strlen(sendbuf) - 1);
1402 strncat(sendbuf, " (MESSAGES UNSEEN)\n",
1403 MAXDATASIZE - strlen(sendbuf) - 1);
1404 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1407 goto next_iteration;
1409 timeout.tv_sec = 60; // 60 second timeout i guess
1410 timeout.tv_usec = 0;
1412 FD_SET(sockfd, &fdset);
1413 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1416 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1420 goto next_iteration;
1423 recvbuf[numbytes] = '\0';
1424 if (strstr(recvbuf, "a2 OK") == NULL) {
1425 ERR("IMAP status failed: %s", recvbuf);
1427 goto next_iteration;
1429 // now we get the data
1430 reply = strstr(recvbuf, " (MESSAGES ");
1432 *strchr(reply, ')') = '\0';
1433 if (reply == NULL) {
1434 ERR("Error parsing IMAP response: %s", recvbuf);
1436 goto next_iteration;
1438 timed_thread_lock (mail->p_timed_thread);
1439 sscanf(reply, "MESSAGES %lu UNSEEN %lu",
1442 timed_thread_unlock (mail->p_timed_thread);
1444 strncpy(sendbuf, "a3 logout\n", MAXDATASIZE);
1445 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1448 goto next_iteration;
1450 timeout.tv_sec = 60; // 60 second timeout i guess
1451 timeout.tv_usec = 0;
1453 FD_SET(sockfd, &fdset);
1454 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1457 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1461 goto next_iteration;
1464 recvbuf[numbytes] = '\0';
1465 if (strstr(recvbuf, "a3 OK") == NULL) {
1466 ERR("IMAP logout failed: %s", recvbuf);
1468 goto next_iteration;
1470 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen || (mail->messages > old_messages && mail->unseen > 0))) { // new mail goodie
1471 if (system(mail->command) == -1) {
1476 old_unseen = mail->unseen;
1477 old_messages = mail->messages;
1479 if ((fstat(sockfd, &stat_buf)==0) && S_ISSOCK(stat_buf.st_mode))
1480 /* if a valid socket, close it */
1482 if (timed_thread_test (mail->p_timed_thread))
1483 timed_thread_exit (mail->p_timed_thread);
1488 void *pop3_thread(struct mail_s *mail)
1490 int sockfd, numbytes;
1491 char recvbuf[MAXDATASIZE];
1492 char sendbuf[MAXDATASIZE];
1495 unsigned int old_unseen = UINT_MAX;
1496 struct stat stat_buf;
1498 struct sockaddr_in their_addr; // connector's address information
1499 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1500 herror("gethostbyname");
1505 ERR("Trying POP3 connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1507 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1510 goto next_iteration;
1513 their_addr.sin_family = AF_INET; // host byte order
1514 their_addr.sin_port = htons(mail->port); // short, network byte order
1515 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1516 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1519 (sockfd, (struct sockaddr *) &their_addr,
1520 sizeof(struct sockaddr)) == -1) {
1523 goto next_iteration;
1525 struct timeval timeout;
1528 timeout.tv_sec = 60; // 60 second timeout i guess
1529 timeout.tv_usec = 0;
1531 FD_SET(sockfd, &fdset);
1532 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1535 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1539 goto next_iteration;
1542 ERR("POP3 connection failed: timeout\n");
1544 goto next_iteration;
1546 recvbuf[numbytes] = '\0';
1547 if (strstr(recvbuf, "+OK ") != recvbuf) {
1548 ERR("POP3 connection failed, probably not a POP3 server");
1550 goto next_iteration;
1552 strncpy(sendbuf, "USER ", MAXDATASIZE);
1553 strncat(sendbuf, mail->user,
1554 MAXDATASIZE - strlen(sendbuf) - 1);
1555 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1556 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1557 perror("send USER");
1559 goto next_iteration;
1561 timeout.tv_sec = 60; // 60 second timeout i guess
1562 timeout.tv_usec = 0;
1564 FD_SET(sockfd, &fdset);
1565 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1568 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1570 perror("recv USER");
1572 goto next_iteration;
1575 recvbuf[numbytes] = '\0';
1576 if (strstr(recvbuf, "+OK ") == NULL) {
1577 ERR("POP3 server login failed: %s", recvbuf);
1579 goto next_iteration;
1581 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1582 strncat(sendbuf, mail->pass,
1583 MAXDATASIZE - strlen(sendbuf) - 1);
1584 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1585 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1586 perror("send PASS");
1588 goto next_iteration;
1590 timeout.tv_sec = 60; // 60 second timeout i guess
1591 timeout.tv_usec = 0;
1593 FD_SET(sockfd, &fdset);
1594 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1597 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1599 perror("recv PASS");
1601 goto next_iteration;
1604 recvbuf[numbytes] = '\0';
1605 if (strstr(recvbuf, "+OK ") == NULL) {
1606 ERR("POP3 server login failed: %s", recvbuf);
1608 goto next_iteration;
1610 strncpy(sendbuf, "STAT\n", MAXDATASIZE);
1611 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1612 perror("send STAT");
1614 goto next_iteration;
1616 timeout.tv_sec = 60; // 60 second timeout i guess
1617 timeout.tv_usec = 0;
1619 FD_SET(sockfd, &fdset);
1620 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1623 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1625 perror("recv STAT");
1627 goto next_iteration;
1630 recvbuf[numbytes] = '\0';
1631 if (strstr(recvbuf, "+OK ") == NULL) {
1632 ERR("POP3 status failed: %s", recvbuf);
1634 goto next_iteration;
1636 // now we get the data
1637 reply = recvbuf + 4;
1638 if (reply == NULL) {
1639 ERR("Error parsing POP3 response: %s", recvbuf);
1641 goto next_iteration;
1643 timed_thread_lock (mail->p_timed_thread);
1644 sscanf(reply, "%lu %lu", &mail->unseen,
1646 timed_thread_unlock (mail->p_timed_thread);
1648 strncpy(sendbuf, "QUIT\n", MAXDATASIZE);
1649 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1650 perror("send QUIT");
1652 goto next_iteration;
1654 timeout.tv_sec = 60; // 60 second timeout i guess
1655 timeout.tv_usec = 0;
1657 FD_SET(sockfd, &fdset);
1658 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1661 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1663 perror("recv QUIT");
1665 goto next_iteration;
1668 recvbuf[numbytes] = '\0';
1669 if (strstr(recvbuf, "+OK") == NULL) {
1670 ERR("POP3 logout failed: %s", recvbuf);
1672 goto next_iteration;
1674 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) { // new mail goodie
1675 if (system(mail->command) == -1) {
1680 old_unseen = mail->unseen;
1682 if ((fstat(sockfd, &stat_buf)==0) && S_ISSOCK(stat_buf.st_mode))
1683 /* if a valid socket, close it */
1685 if (timed_thread_test (mail->p_timed_thread))
1686 timed_thread_exit (mail->p_timed_thread);
1692 void *threaded_exec(struct text_object *obj) {
1694 char *p2 = obj->data.texeci.buffer;
1695 FILE *fp = popen(obj->data.texeci.cmd,"r");
1696 timed_thread_lock (obj->data.texeci.p_timed_thread);
1697 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
1700 if (n2 && p2[n2 - 1] == '\n') {
1704 if (*p2 == '\001') {
1709 timed_thread_unlock (obj->data.texeci.p_timed_thread);
1710 if (timed_thread_test (obj->data.texeci.p_timed_thread))
1711 timed_thread_exit (obj->data.texeci.p_timed_thread);
1716 static struct text_object *new_text_object_internal()
1718 struct text_object *obj = malloc(sizeof(struct text_object));
1719 memset(obj, 0, sizeof(struct text_object));
1723 static void free_text_objects(unsigned int count, struct text_object *objs)
1726 for (i = 0; i < count; i++) {
1727 switch (objs[i].type) {
1729 close(objs[i].data.i);
1732 close(objs[i].data.i);
1735 close(objs[i].data.i2c.fd);
1738 free(objs[i].data.s);
1741 free(objs[i].data.s);
1744 free(objs[i].data.tztime.tz);
1745 free(objs[i].data.tztime.fmt);
1750 case OBJ_imap_unseen:
1751 if (!objs[i].global_mode) {
1752 free(objs[i].data.mail);
1755 case OBJ_imap_messages:
1756 if (!objs[i].global_mode) {
1757 free(objs[i].data.mail);
1763 case OBJ_pop3_unseen:
1764 if (!objs[i].global_mode) {
1765 free(objs[i].data.mail);
1769 if (!objs[i].global_mode) {
1770 free(objs[i].data.mail);
1773 case OBJ_if_existing:
1774 case OBJ_if_mounted:
1775 case OBJ_if_running:
1776 free(objs[i].data.ifblock.s);
1779 free(objs[i].data.tail.logfile);
1780 free(objs[i].data.tail.buffer);
1782 case OBJ_text: case OBJ_font:
1783 free(objs[i].data.s);
1786 free(objs[i].data.s);
1789 free(objs[i].data.s);
1792 free(objs[i].data.s);
1795 free(objs[i].data.s);
1797 /* case OBJ_execibar:
1798 free(objs[i].data.s);
1800 case OBJ_execigraph:
1801 free(objs[i].data.s);
1804 case OBJ_iconv_start:
1810 if (info.mpd.title) {
1811 free(info.mpd.title);
1815 case OBJ_mpd_artist:
1816 if (info.mpd.artist) {
1817 free(info.mpd.artist);
1818 info.mpd.artist = 0;
1822 if (info.mpd.album) {
1823 free(info.mpd.album);
1827 case OBJ_mpd_random:
1828 if (info.mpd.random) {
1829 free(info.mpd.random);
1830 info.mpd.random = 0;
1833 case OBJ_mpd_repeat:
1834 if (info.mpd.repeat) {
1835 free(info.mpd.repeat);
1836 info.mpd.repeat = 0;
1840 if (info.mpd.track) {
1841 free(info.mpd.track);
1846 if (info.mpd.name) {
1847 free(info.mpd.name);
1852 if (info.mpd.file) {
1853 free(info.mpd.file);
1857 case OBJ_mpd_status:
1858 if (info.mpd.status) {
1859 free(info.mpd.status);
1860 info.mpd.status = 0;
1864 if (info.mpd.artist) {
1865 free(info.mpd.artist);
1866 info.mpd.artist = 0;
1868 if (info.mpd.title) {
1869 free(info.mpd.title);
1872 if (info.mpd.file) {
1873 free(info.mpd.file);
1880 case OBJ_xmms2_artist:
1881 if (info.xmms2.artist) {
1882 free(info.xmms2.artist);
1883 info.xmms2.artist = 0;
1886 case OBJ_xmms2_album:
1887 if (info.xmms2.album) {
1888 free(info.xmms2.album);
1889 info.xmms2.album = 0;
1892 case OBJ_xmms2_title:
1893 if (info.xmms2.title) {
1894 free(info.xmms2.title);
1895 info.xmms2.title = 0;
1898 case OBJ_xmms2_genre:
1899 if (info.xmms2.genre) {
1900 free(info.xmms2.genre);
1901 info.xmms2.genre = 0;
1904 case OBJ_xmms2_comment:
1905 if (info.xmms2.comment) {
1906 free(info.xmms2.comment);
1907 info.xmms2.comment = 0;
1910 case OBJ_xmms2_decoder:
1911 if (info.xmms2.decoder) {
1912 free(info.xmms2.decoder);
1916 case OBJ_xmms2_transport:
1917 if (info.xmms2.transport) {
1918 free(info.xmms2.transport);
1923 if (info.xmms2.url) {
1924 free(info.xmms2.url);
1928 case OBJ_xmms2_date:
1929 if (info.xmms2.date) {
1930 free(info.xmms2.date);
1931 info.xmms2.date = 0;
1934 case OBJ_xmms2_status:
1935 if (info.xmms2.status) {
1936 free(info.xmms2.status);
1937 info.xmms2.status = 0;
1940 case OBJ_xmms2_smart:
1941 if (info.xmms2.artist) {
1942 free(info.xmms2.artist);
1943 info.xmms2.artist = 0;
1945 if (info.xmms2.title) {
1946 free(info.xmms2.title);
1947 info.xmms2.title = 0;
1949 if (info.xmms2.url) {
1950 free(info.xmms2.url);
1956 case OBJ_bmpx_title:
1957 case OBJ_bmpx_artist:
1958 case OBJ_bmpx_album:
1959 case OBJ_bmpx_track:
1961 case OBJ_bmpx_bitrate:
1965 free(objs[i].data.s);
1967 case OBJ_battery_time:
1968 free(objs[i].data.s);
1971 free(objs[i].data.execi.cmd);
1972 free(objs[i].data.execi.buffer);
1975 free(objs[i].data.texeci.cmd);
1976 free(objs[i].data.texeci.buffer);
1979 if (info.first_process) {
1980 free_all_processes();
1981 info.first_process = NULL;
1985 if (info.first_process) {
1986 free_all_processes();
1987 info.first_process = NULL;
1992 free(objs[i].data.hddtemp.dev);
1993 free(objs[i].data.hddtemp.addr);
1996 case OBJ_entropy_avail:
1997 case OBJ_entropy_poolsize:
1998 case OBJ_entropy_bar:
2003 //text_objects = NULL;
2004 //text_object_count = 0;
2007 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2012 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2013 *a = mixer_init(buf1);
2014 (void) scan_bar(arg + n, w, h);
2017 (void) scan_bar(arg, w, h);
2022 /* construct_text_object() creates a new text_object */
2023 static struct text_object *construct_text_object(const char *s, const char *arg, unsigned int object_count, struct text_object *text_objects, long line)
2025 //struct text_object *obj = new_text_object();
2026 struct text_object *obj = new_text_object_internal();
2029 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
2030 #define END ; } } else
2034 obj->type = OBJ_color;
2035 obj->data.l = get_x11_color(s);
2038 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
2039 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
2040 END OBJ(acpiacadapter, 0)
2041 END OBJ(freq, INFO_FREQ)
2047 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2049 obj->data.cpu_index=1;
2050 // ERR("freq: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2054 obj->data.cpu_index=atoi(&arg[0]);
2057 END OBJ(freq_g, INFO_FREQ)
2063 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2065 obj->data.cpu_index=1;
2066 // ERR("freq_g: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2070 obj->data.cpu_index=atoi(&arg[0]);
2073 #if defined(__linux__)
2074 END OBJ(voltage_mv, 0)
2080 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2082 obj->data.cpu_index=1;
2083 // ERR("voltage_mv: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2087 obj->data.cpu_index=atoi(&arg[0]);
2090 END OBJ(voltage_v, 0)
2096 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2098 obj->data.cpu_index=1;
2099 // ERR("voltage_v: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2103 obj->data.cpu_index=atoi(&arg[0]);
2106 #endif /* __linux__ */
2107 END OBJ(freq_dyn, 0);
2108 END OBJ(freq_dyn_g, 0);
2109 END OBJ(acpifan, 0);
2110 END OBJ(battery, 0);
2113 sscanf(arg, "%63s", bat);
2115 strcpy(bat, "BAT0");
2116 obj->data.s = strdup(bat);
2117 END OBJ(battery_time, 0);
2120 sscanf(arg, "%63s", bat);
2122 strcpy(bat, "BAT0");
2123 obj->data.s = strdup(bat);
2124 #if defined(__linux__)
2125 END OBJ(i8k_version, INFO_I8K)
2126 END OBJ(i8k_bios, INFO_I8K)
2127 END OBJ(i8k_serial, INFO_I8K)
2128 END OBJ(i8k_cpu_temp, INFO_I8K)
2129 END OBJ(i8k_cpu_tempf, INFO_I8K)
2130 END OBJ(i8k_left_fan_status, INFO_I8K)
2131 END OBJ(i8k_right_fan_status, INFO_I8K)
2132 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2133 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2134 END OBJ(i8k_ac_status, INFO_I8K)
2135 END OBJ(i8k_buttons_status, INFO_I8K)
2137 END OBJ(ibm_temps, 0)
2139 CRIT_ERR("ibm_temps: needs an argument");
2141 if (!isdigit(arg[0])
2143 || atoi(&arg[0]) >=8)
2146 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. Using 0 (CPU temp sensor).");
2148 obj->data.sensor = atoi(&arg[0]);
2149 END OBJ(ibm_volume, 0)
2150 END OBJ(ibm_brightness, 0)
2151 END OBJ(pb_battery, 0)
2152 if (arg && strcmp(arg, "status") == 0) {
2153 obj->data.i = PB_BATT_STATUS;
2154 } else if (arg && strcmp(arg, "percent") == 0) {
2155 obj->data.i = PB_BATT_PERCENT;
2156 } else if (arg && strcmp(arg, "time") == 0) {
2157 obj->data.i = PB_BATT_TIME;
2159 ERR("pb_battery: needs one argument: status, percent or time");
2164 #endif /* __linux__ */
2165 END OBJ(buffers, INFO_BUFFERS)
2166 END OBJ(cached, INFO_BUFFERS)
2167 END OBJ(cpu, INFO_CPU)
2169 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2170 obj->data.cpu_index = atoi(&arg[3]);
2172 } else {obj->data.cpu_index = 0; }
2174 obj->data.cpu_index = 0;
2176 END OBJ(cpubar, INFO_CPU)
2178 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2179 obj->data.cpu_index = atoi(&arg[3]);
2182 else {obj->data.cpu_index = 0;}
2183 (void) scan_bar(arg, &obj->a, &obj->b);
2185 (void) scan_bar(arg, &obj->a, &obj->b);
2186 obj->data.cpu_index = 0;
2188 END OBJ(cpugraph, INFO_CPU)
2190 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2191 obj->data.cpu_index = atoi(&arg[3]);
2194 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2196 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2197 obj->data.cpu_index = 0;
2199 END OBJ(diskio, INFO_DISKIO)
2200 END OBJ(diskio_read, INFO_DISKIO)
2201 END OBJ(diskio_write, INFO_DISKIO)
2202 END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2203 END OBJ(diskiograph_read, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2204 END OBJ(diskiograph_write, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2207 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2211 obj->data.s = scan_font(arg);
2213 OBJ(downspeed, INFO_NET)
2215 obj->data.net = get_net_stat(arg);
2218 CRIT_ERR("downspeed needs argument");
2220 END OBJ(downspeedf, INFO_NET)
2222 obj->data.net = get_net_stat(arg);
2225 CRIT_ERR("downspeedf needs argument");
2227 END OBJ(downspeedgraph, INFO_NET)
2228 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2230 sscanf(arg, "%63s %*i,%*i %*i", buf);
2231 obj->data.net = get_net_stat(buf);
2232 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2233 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
2240 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos = object_count;
2241 blockstart[blockdepth - 1] = object_count;
2242 obj->data.ifblock.pos = object_count + 2;
2244 ERR("$else: no matching $if_*");
2249 text_objects[blockstart[blockdepth]].data.ifblock.pos = object_count;
2251 ERR("$endif: no matching $if_*");
2254 OBJ(image, 0) obj->data.s = strdup(arg ? arg : "");
2257 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
2258 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
2259 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
2260 END OBJ(execibar, 0) unsigned int n;
2261 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2263 ERR("${execibar <interval> command}");
2264 obj->type = OBJ_text;
2265 snprintf(buf, 256, "${%s}", s);
2266 obj->data.s = strdup(buf);
2268 obj->data.execi.cmd = strdup(arg + n);
2270 END OBJ(execigraph, 0) unsigned int n;
2271 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2273 ERR("${execigraph <interval> command}");
2274 obj->type = OBJ_text;
2275 snprintf(buf, 256, "${%s}", s);
2276 obj->data.s = strdup(buf);
2278 obj->data.execi.cmd = strdup(arg + n);
2280 END OBJ(execi, 0) unsigned int n;
2281 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2283 ERR("${execi <interval> command}");
2284 obj->type = OBJ_text;
2285 snprintf(buf, 256, "${%s}", s);
2286 obj->data.s = strdup(buf);
2288 obj->data.execi.cmd = strdup(arg + n);
2289 obj->data.execi.buffer =
2290 (char *) calloc(1, TEXT_BUFFER_SIZE);
2292 END OBJ(texeci, 0) unsigned int n;
2293 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2295 ERR("${texeci <interval> command}");
2296 obj->type = OBJ_text;
2297 snprintf(buf, 256, "${%s}", s);
2298 obj->data.s = strdup(buf);
2300 obj->data.texeci.cmd = strdup(arg + n);
2301 obj->data.texeci.buffer =
2302 (char *) calloc(1, TEXT_BUFFER_SIZE);
2304 obj->data.texeci.p_timed_thread = NULL;
2305 END OBJ(pre_exec, 0) obj->type = OBJ_text;
2307 FILE *fp = popen(arg, "r");
2311 n = fread(buf, 1, 2048, fp);
2314 if (n && buf[n - 1] == '\n')
2319 obj->data.s = strdup(buf);
2321 obj->data.s = strdup("");
2324 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
2325 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2327 while (isspace(*arg))
2333 obj->data.fsbar.fs = prepare_fs_stat(arg);
2334 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
2337 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
2341 obj->data.fsbar.fs = prepare_fs_stat(arg);
2342 END OBJ(fs_free, INFO_FS) if (!arg)
2344 obj->data.fs = prepare_fs_stat(arg);
2345 END OBJ(fs_used_perc, INFO_FS) if (!arg)
2347 obj->data.fs = prepare_fs_stat(arg);
2348 END OBJ(fs_free_perc, INFO_FS) if (!arg)
2350 obj->data.fs = prepare_fs_stat(arg);
2351 END OBJ(fs_size, INFO_FS) if (!arg)
2353 obj->data.fs = prepare_fs_stat(arg);
2354 END OBJ(fs_used, INFO_FS) if (!arg)
2356 obj->data.fs = prepare_fs_stat(arg);
2357 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
2358 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
2359 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
2363 ERR("goto needs arguments");
2364 obj->type = OBJ_text;
2365 obj->data.s = strdup("${goto}");
2369 obj->data.i = atoi(arg);
2374 if (sscanf(arg, "%d %d", &a, &b) != 2)
2375 sscanf(arg, "%d", &b);
2379 obj->data.pair.a = a;
2380 obj->data.pair.b = b;
2382 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
2386 ERR("i2c needs arguments");
2387 obj->type = OBJ_text;
2388 //obj->data.s = strdup("${i2c}");
2392 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2393 /* if scanf couldn't read three values, read type and num and use
2395 sscanf(arg, "%63s %d", buf2, &n);
2397 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
2398 obj->data.i2c.devtype);
2399 strncpy(obj->data.i2c.type, buf2, 63);
2402 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
2403 obj->data.i2c.devtype);
2404 strncpy(obj->data.i2c.type, buf2, 63);
2407 END OBJ(top, INFO_TOP)
2411 ERR("top needs arguments");
2412 obj->type = OBJ_text;
2413 //obj->data.s = strdup("${top}");
2416 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2417 if (strcmp(buf, "name") == 0) {
2418 obj->data.top.type = TOP_NAME;
2419 } else if (strcmp(buf, "cpu") == 0) {
2420 obj->data.top.type = TOP_CPU;
2421 } else if (strcmp(buf, "pid") == 0) {
2422 obj->data.top.type = TOP_PID;
2423 } else if (strcmp(buf, "mem") == 0) {
2424 obj->data.top.type = TOP_MEM;
2426 ERR("invalid arg for top");
2429 if (n < 1 || n > 10) {
2430 CRIT_ERR("invalid arg for top");
2433 obj->data.top.num = n - 1;
2437 ERR("invalid args given for top");
2440 END OBJ(top_mem, INFO_TOP)
2444 ERR("top_mem needs arguments");
2445 obj->type = OBJ_text;
2446 obj->data.s = strdup("${top_mem}");
2449 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2450 if (strcmp(buf, "name") == 0) {
2451 obj->data.top.type = TOP_NAME;
2452 } else if (strcmp(buf, "cpu") == 0) {
2453 obj->data.top.type = TOP_CPU;
2454 } else if (strcmp(buf, "pid") == 0) {
2455 obj->data.top.type = TOP_PID;
2456 } else if (strcmp(buf, "mem") == 0) {
2457 obj->data.top.type = TOP_MEM;
2459 ERR("invalid arg for top");
2462 if (n < 1 || n > 10) {
2463 CRIT_ERR("invalid arg for top");
2466 obj->data.top.num = n - 1;
2470 ERR("invalid args given for top");
2473 END OBJ(addr, INFO_NET)
2475 obj->data.net = get_net_stat(arg);
2478 CRIT_ERR("addr needs argument");
2480 END OBJ(linkstatus, INFO_WIFI)
2482 obj->data.net = get_net_stat(arg);
2485 CRIT_ERR("linkstatus needs argument");
2491 ERR("tail needs arguments");
2492 obj->type = OBJ_text;
2493 obj->data.s = strdup("${tail}");
2496 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2497 if (n1 < 1 || n1 > 30) {
2498 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
2502 fp = fopen(buf, "r");
2504 obj->data.tail.logfile =
2505 malloc(TEXT_BUFFER_SIZE);
2506 strcpy(obj->data.tail.logfile, buf);
2507 obj->data.tail.wantedlines = n1;
2508 obj->data.tail.interval =
2509 update_interval * 2;
2513 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2516 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2517 if (n1 < 1 || n1 > 30) {
2519 ("invalid arg for tail, number of lines must be between 1 and 30");
2521 } else if (n2 < 1 || n2 < update_interval) {
2523 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
2527 fp = fopen(buf, "r");
2529 obj->data.tail.logfile =
2530 malloc(TEXT_BUFFER_SIZE);
2531 strcpy(obj->data.tail.logfile, buf);
2532 obj->data.tail.wantedlines = n1;
2533 obj->data.tail.interval = n2;
2537 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2543 ERR("invalid args given for tail");
2546 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2551 ERR("head needs arguments");
2552 obj->type = OBJ_text;
2553 obj->data.s = strdup("${head}");
2556 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2557 if (n1 < 1 || n1 > 30) {
2558 CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
2562 fp = fopen(buf, "r");
2564 obj->data.tail.logfile =
2565 malloc(TEXT_BUFFER_SIZE);
2566 strcpy(obj->data.tail.logfile, buf);
2567 obj->data.tail.wantedlines = n1;
2568 obj->data.tail.interval =
2569 update_interval * 2;
2573 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2576 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2577 if (n1 < 1 || n1 > 30) {
2579 ("invalid arg for head, number of lines must be between 1 and 30");
2581 } else if (n2 < 1 || n2 < update_interval) {
2583 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
2587 fp = fopen(buf, "r");
2589 obj->data.tail.logfile =
2590 malloc(TEXT_BUFFER_SIZE);
2591 strcpy(obj->data.tail.logfile, buf);
2592 obj->data.tail.wantedlines = n1;
2593 obj->data.tail.interval = n2;
2597 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2603 ERR("invalid args given for head");
2606 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2607 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
2609 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2610 if (r >= 3 && (c < 1 || c > 3))
2612 if (r >= 2 && (b < 1 || b > 3))
2614 if (r >= 1 && (a < 1 || a > 3))
2617 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2618 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2619 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2620 END OBJ(if_existing, 0)
2621 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2622 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2625 ERR("if_existing needs an argument");
2626 obj->data.ifblock.s = 0;
2628 obj->data.ifblock.s = strdup(arg);
2629 blockstart[blockdepth] = object_count;
2630 obj->data.ifblock.pos = object_count + 2;
2632 END OBJ(if_mounted, 0)
2633 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2634 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2637 ERR("if_mounted needs an argument");
2638 obj->data.ifblock.s = 0;
2640 obj->data.ifblock.s = strdup(arg);
2641 blockstart[blockdepth] = object_count;
2642 obj->data.ifblock.pos = object_count + 2;
2644 END OBJ(if_running, 0)
2645 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2646 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2650 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2651 obj->data.ifblock.s = strdup(buf);
2653 ERR("if_running needs an argument");
2654 obj->data.ifblock.s = 0;
2656 blockstart[blockdepth] = object_count;
2657 obj->data.ifblock.pos = object_count + 2;
2661 END OBJ(mails, INFO_MAIL)
2662 END OBJ(mem, INFO_MEM)
2663 END OBJ(memmax, INFO_MEM)
2664 END OBJ(memperc, INFO_MEM)
2665 END OBJ(membar, INFO_MEM)
2666 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2667 END OBJ(memgraph, INFO_MEM)
2668 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2669 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
2670 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
2671 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
2672 END OBJ(mixerbar, INFO_MIXER)
2673 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2674 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2675 END OBJ(mixerlbar, INFO_MIXER)
2676 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2677 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2678 END OBJ(mixerrbar, INFO_MIXER)
2679 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2680 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2682 OBJ(new_mails, INFO_MAIL)
2683 END OBJ(nodename, 0)
2684 END OBJ(processes, INFO_PROCS)
2685 END OBJ(running_processes, INFO_RUN_PROCS)
2686 END OBJ(shadecolor, 0)
2688 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2690 END OBJ(outlinecolor, 0)
2692 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2694 END OBJ(stippled_hr, 0)
2696 int a = stippled_borders, b = 1;
2698 if (sscanf(arg, "%d %d", &a, &b) != 2)
2699 sscanf(arg, "%d", &b);
2703 obj->data.pair.a = a;
2704 obj->data.pair.b = b;
2706 END OBJ(swap, INFO_MEM)
2707 END OBJ(swapmax, INFO_MEM)
2708 END OBJ(swapperc, INFO_MEM)
2709 END OBJ(swapbar, INFO_MEM)
2710 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2711 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
2713 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
2714 obj->data.i2c.devtype);
2715 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
2717 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
2718 obj->data.i2c.devtype);
2719 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2720 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2722 char buf1[256], buf2[256], *fmt, *tz;
2725 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2734 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
2735 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
2737 END OBJ(iconv_start, 0)
2738 if (iconv_converting) {
2739 CRIT_ERR("You must stop your last iconv conversion before starting another");
2742 char iconv_from[CODEPAGE_LENGTH];
2743 char iconv_to[CODEPAGE_LENGTH];
2744 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2745 CRIT_ERR("Invalid arguments for iconv_start");
2748 new_iconv = iconv_open(iconv_to, iconv_from);
2749 if (new_iconv == (iconv_t)(-1)) {
2750 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2752 obj->a = register_iconv(&new_iconv);
2753 iconv_converting = 1;
2757 CRIT_ERR("Iconv requires arguments");
2759 END OBJ(iconv_stop, 0)
2760 iconv_converting = 0;
2763 END OBJ(totaldown, INFO_NET)
2765 obj->data.net = get_net_stat(arg);
2768 CRIT_ERR("totaldown needs argument");
2770 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
2772 obj->data.net = get_net_stat(arg);
2775 CRIT_ERR("totalup needs argument");
2778 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
2779 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
2780 END OBJ(upspeed, INFO_NET)
2782 obj->data.net = get_net_stat(arg);
2785 CRIT_ERR("upspeed needs argument");
2787 END OBJ(upspeedf, INFO_NET)
2789 obj->data.net = get_net_stat(arg);
2792 CRIT_ERR("upspeedf needs argument");
2795 END OBJ(upspeedgraph, INFO_NET)
2796 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2798 sscanf(arg, "%63s %*i,%*i %*i", buf);
2799 obj->data.net = get_net_stat(buf);
2800 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2801 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
2806 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
2807 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
2808 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2809 OBJ(apm_adapter, 0) END
2810 OBJ(apm_battery_life, 0) END
2811 OBJ(apm_battery_time, 0) END
2812 #endif /* __FreeBSD__ */
2816 obj->data.mail = parse_mail_args(IMAP, arg);
2817 obj->global_mode = 0;
2819 obj->global_mode = 1;
2822 OBJ(imap_messages, 0)
2825 obj->data.mail = parse_mail_args(IMAP, arg);
2826 obj->global_mode = 0;
2828 obj->global_mode = 1;
2834 obj->data.mail = parse_mail_args(POP3, arg);
2835 obj->global_mode = 0;
2837 obj->global_mode = 1;
2843 obj->data.mail = parse_mail_args(POP3, arg);
2844 obj->global_mode = 0;
2846 obj->global_mode = 1;
2850 OBJ(mpd_artist, INFO_MPD)
2851 END OBJ(mpd_title, INFO_MPD)
2852 END OBJ(mpd_random, INFO_MPD)
2853 END OBJ(mpd_repeat, INFO_MPD)
2854 END OBJ(mpd_elapsed, INFO_MPD)
2855 END OBJ(mpd_length, INFO_MPD)
2856 END OBJ(mpd_track, INFO_MPD)
2857 END OBJ(mpd_name, INFO_MPD)
2858 END OBJ(mpd_file, INFO_MPD)
2859 END OBJ(mpd_percent, INFO_MPD)
2860 END OBJ(mpd_album, INFO_MPD)
2861 END OBJ(mpd_vol, INFO_MPD)
2862 END OBJ(mpd_bitrate, INFO_MPD)
2863 END OBJ(mpd_status, INFO_MPD)
2864 END OBJ(mpd_bar, INFO_MPD)
2865 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2866 END OBJ(mpd_smart, INFO_MPD) END
2869 OBJ(xmms2_artist, INFO_XMMS2)
2870 END OBJ(xmms2_album, INFO_XMMS2)
2871 END OBJ(xmms2_title, INFO_XMMS2)
2872 END OBJ(xmms2_genre, INFO_XMMS2)
2873 END OBJ(xmms2_comment, INFO_XMMS2)
2874 END OBJ(xmms2_decoder, INFO_XMMS2)
2875 END OBJ(xmms2_transport, INFO_XMMS2)
2876 END OBJ(xmms2_url, INFO_XMMS2)
2877 END OBJ(xmms2_tracknr, INFO_XMMS2)
2878 END OBJ(xmms2_bitrate, INFO_XMMS2)
2879 END OBJ(xmms2_date, INFO_XMMS2)
2880 END OBJ(xmms2_id, INFO_XMMS2)
2881 END OBJ(xmms2_duration, INFO_XMMS2)
2882 END OBJ(xmms2_elapsed, INFO_XMMS2)
2883 END OBJ(xmms2_size, INFO_XMMS2)
2884 END OBJ(xmms2_status, INFO_XMMS2)
2885 END OBJ(xmms2_percent, INFO_XMMS2)
2886 END OBJ(xmms2_bar, INFO_XMMS2)
2887 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2888 END OBJ(xmms2_smart, INFO_XMMS2)
2892 OBJ(audacious_status, INFO_AUDACIOUS) END
2893 OBJ(audacious_title, INFO_AUDACIOUS)
2897 sscanf (arg, "%d", &info.audacious.max_title_len);
2898 if (info.audacious.max_title_len > 0)
2899 info.audacious.max_title_len++;
2901 CRIT_ERR ("audacious_title: invalid length argument");
2905 OBJ(audacious_length, INFO_AUDACIOUS) END
2906 OBJ(audacious_length_seconds, INFO_AUDACIOUS) END
2907 OBJ(audacious_position, INFO_AUDACIOUS) END
2908 OBJ(audacious_position_seconds, INFO_AUDACIOUS) END
2909 OBJ(audacious_bitrate, INFO_AUDACIOUS) END
2910 OBJ(audacious_frequency, INFO_AUDACIOUS) END
2911 OBJ(audacious_channels, INFO_AUDACIOUS) END
2912 OBJ(audacious_filename, INFO_AUDACIOUS) END
2913 OBJ(audacious_playlist_length, INFO_AUDACIOUS) END
2914 OBJ(audacious_playlist_position, INFO_AUDACIOUS) END
2915 OBJ(audacious_bar, INFO_AUDACIOUS)
2916 (void) scan_bar(arg, &obj->a, &obj->b);
2920 OBJ(bmpx_title, INFO_BMPX)
2921 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2923 OBJ(bmpx_artist, INFO_BMPX)
2924 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2926 OBJ(bmpx_album, INFO_BMPX)
2927 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2929 OBJ(bmpx_track, INFO_BMPX)
2930 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2932 OBJ(bmpx_uri, INFO_BMPX)
2933 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2935 OBJ(bmpx_bitrate, INFO_BMPX)
2936 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2941 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
2942 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2943 ERR("hddtemp needs arguments");
2944 obj->type = OBJ_text;
2945 obj->data.s = strdup("${hddtemp}");
2950 #ifdef TCP_PORT_MONITOR
2951 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2952 int argc, port_begin, port_end, item, connection_index;
2954 memset(itembuf,0,sizeof(itembuf));
2956 /* massive argument checking */
2958 CRIT_ERR("tcp_portmon: needs arguments");
2960 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
2961 if ( (argc != 3) && (argc != 4) )
2963 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
2965 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
2967 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
2969 if ( port_begin > port_end )
2971 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
2973 if ( strncmp(itembuf,"count",31) == 0 )
2975 else if ( strncmp(itembuf,"rip",31) == 0 )
2977 else if ( strncmp(itembuf,"rhost",31) == 0 )
2979 else if ( strncmp(itembuf,"rport",31) == 0 )
2981 else if ( strncmp(itembuf,"rservice",31) == 0 )
2983 else if ( strncmp(itembuf,"lip",31) == 0 )
2985 else if ( strncmp(itembuf,"lhost",31) == 0 )
2987 else if ( strncmp(itembuf,"lport",31) == 0 )
2989 else if ( strncmp(itembuf,"lservice",31) == 0 )
2993 CRIT_ERR("tcp_portmon: invalid item specified");
2995 if ( (argc==3) && (item!=COUNT) )
2997 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
2999 if ( (argc==4) && (connection_index<0) )
3001 CRIT_ERR("tcp_portmon: connection index must be non-negative");
3003 /* ok, args looks good. save the text object data */
3004 obj->data.tcp_port_monitor.port_range_begin = (in_port_t)port_begin;
3005 obj->data.tcp_port_monitor.port_range_end = (in_port_t)port_end;
3006 obj->data.tcp_port_monitor.item = item;
3007 obj->data.tcp_port_monitor.connection_index = connection_index;
3009 /* if the port monitor collection hasn't been created, we must create it */
3010 if ( !info.p_tcp_port_monitor_collection )
3012 info.p_tcp_port_monitor_collection =
3013 create_tcp_port_monitor_collection ();
3014 if ( !info.p_tcp_port_monitor_collection )
3016 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
3020 /* if a port monitor for this port does not exist, create one and add it to the collection */
3021 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
3023 tcp_port_monitor_t * p_monitor =
3024 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
3027 CRIT_ERR("tcp_portmon: unable to create port monitor");
3029 /* add the newly created monitor to the collection */
3030 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
3033 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
3038 OBJ(entropy_avail, INFO_ENTROPY) END
3039 OBJ(entropy_poolsize, INFO_ENTROPY) END
3040 OBJ(entropy_bar, INFO_ENTROPY)
3041 (void) scan_bar(arg, &obj->a, &obj->b);
3046 ERR("unknown variable %s", s);
3047 obj->type = OBJ_text;
3048 snprintf(buf, 256, "${%s}", s);
3049 obj->data.s = strdup(buf);
3056 static struct text_object *create_plain_text(const char *s)
3058 struct text_object *obj;
3060 if (s == NULL || *s == '\0') {
3064 obj = new_text_object_internal();
3066 obj->type = OBJ_text;
3067 obj->data.s = strdup(s);
3071 static struct text_object_list *extract_variable_text_internal(const char *p)
3073 struct text_object_list *retval;
3074 struct text_object *obj;
3077 retval = malloc(sizeof(struct text_object_list));
3078 memset(retval, 0, sizeof(struct text_object_list));
3079 retval->text_object_count = 0;
3081 long line = text_lines;
3089 obj = create_plain_text(s);
3091 // allocate memory for the object
3092 retval->text_objects = realloc(retval->text_objects,
3093 sizeof(struct text_object) * (retval->text_object_count+1));
3094 // assign the new object to the end of the list.
3095 memcpy(&retval->text_objects[retval->text_object_count++],
3096 obj, sizeof(struct text_object));
3108 /* variable is either $foo or ${foo} */
3112 while (*p && *p != '}')
3118 while (*p && (isalnum((int) *p)
3123 /* copy variable to buffer */
3124 len = (p - s > 255) ? 255 : (p - s);
3125 strncpy(buf, s, len);
3134 /* if variable wasn't found from environment, use some special */
3140 if (strchr(buf, ' ')) {
3141 arg = strchr(buf, ' ');
3144 while (isspace((int) *arg))
3150 /* lowercase variable name */
3157 // create new object
3158 obj = construct_text_object(buf, arg, retval->text_object_count, retval->text_objects, line);
3160 // allocate memory for the object
3161 retval->text_objects = realloc(retval->text_objects,
3162 sizeof(struct text_object) * (retval->text_object_count+1));
3163 // assign the new object to the end of the list.
3164 memcpy(&retval->text_objects[retval->text_object_count++],
3165 obj, sizeof(struct text_object));
3171 obj = create_plain_text("$");
3173 // allocate memory for the object
3174 retval->text_objects = realloc(retval->text_objects,
3175 sizeof(struct text_object) * (retval->text_object_count+1));
3176 // assign the new object to the end of the list.
3177 memcpy(&retval->text_objects[retval->text_object_count++],
3178 obj, sizeof(struct text_object));
3185 obj = create_plain_text(s);
3187 // allocate memory for the object
3188 retval->text_objects = realloc(retval->text_objects,
3189 sizeof(struct text_object) * (retval->text_object_count+1));
3190 // assign the new object to the end of the list.
3191 memcpy(&retval->text_objects[retval->text_object_count++],
3192 obj, sizeof(struct text_object));
3197 ERR("one or more $endif's are missing");
3203 static void extract_variable_text(const char *p)
3205 struct text_object_list *list;
3207 free_text_objects(text_object_count, text_objects);
3208 text_object_count = 0;
3209 text_objects = NULL;
3212 list = extract_variable_text_internal(p);
3213 text_objects = list->text_objects;
3214 text_object_count = list->text_object_count;
3221 void parse_conky_vars(char * text, char * p, struct information *cur) {
3222 struct text_object_list *object_list = extract_variable_text_internal(text);
3223 generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
3227 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
3232 char buff_in[P_MAX_SIZE] = {0};
3233 iconv_converting = 0;
3236 for (i = 0; i < object_count; i++) {
3237 struct text_object *obj = &objs[i];
3239 #define OBJ(a) break; case OBJ_##a:
3241 switch (obj->type) {
3244 ERR("not implemented obj type %d",
3248 /* does anyone have decimals in acpi temperature? */
3250 snprintf(p, p_max_size, "%d", (int)
3251 get_acpi_temperature(obj->
3255 snprintf(p, 5, "%d ", (int)
3256 get_acpi_temperature(obj->
3261 /* does anyone have decimals in acpi temperature? */
3263 snprintf(p, p_max_size, "%d", (int)
3264 ((get_acpi_temperature(obj->
3266 i)+ 40) * 9.0 / 5 - 40));
3268 snprintf(p, 5, "%d ", (int)
3269 ((get_acpi_temperature(obj->
3271 i)+ 40) * 9.0 / 5 - 40));
3275 obj->a = get_freq(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3280 obj->a = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.cpu_index);
3283 #if defined(__linux__)
3286 obj->a = get_voltage(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3291 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.cpu_index);
3294 #endif /* __linux__ */
3298 get_freq_dynamic(p, 6, "%.0f ", 1 );
3300 get_freq_dynamic(p, p_max_size, "%.0f", 1 );
3305 get_freq_dynamic(p, 6, "%'.2f ", 1000);
3307 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
3311 get_adt746x_cpu(p, p_max_size);
3314 get_adt746x_fan(p, p_max_size);
3317 get_acpi_fan(p, p_max_size);
3319 OBJ(acpiacadapter) {
3320 get_acpi_ac_adapter(p, p_max_size);
3323 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_STATUS);
3326 get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME);
3329 human_readable(cur->buffers * 1024, p, 255);
3332 human_readable(cur->cached * 1024, p, 255);
3335 if (obj->data.cpu_index > info.cpu_count) {
3336 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
3337 CRIT_ERR("attempting to use more CPUs then you have!");
3340 snprintf(p, p_max_size, "%*d", pad_percents,
3341 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3344 snprintf(p, 4, "%*d ",
3346 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3352 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3355 new_graph(p, obj->a,
3356 obj->b, obj->c, obj->d,
3357 (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3361 new_fg(p, obj->data.l);
3363 #if defined(__linux__)
3365 snprintf(p, p_max_size, "%s", i8k.version);
3368 snprintf(p, p_max_size, "%s", i8k.bios);
3371 snprintf(p, p_max_size, "%s", i8k.serial);
3374 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
3376 OBJ(i8k_cpu_tempf) {
3378 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3379 snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
3381 OBJ(i8k_left_fan_status) {
3382 int left_fan_status;
3383 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3384 if(left_fan_status == 0) {
3385 snprintf(p, p_max_size,"off");
3386 } if(left_fan_status == 1) {
3387 snprintf(p, p_max_size, "low");
3388 } if(left_fan_status == 2) {
3389 snprintf(p, p_max_size, "high");
3393 OBJ(i8k_right_fan_status) {
3394 int right_fan_status;
3395 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3396 if(right_fan_status == 0) {
3397 snprintf(p, p_max_size,"off");
3398 } if(right_fan_status == 1) {
3399 snprintf(p, p_max_size, "low");
3400 } if(right_fan_status == 2) {
3401 snprintf(p, p_max_size, "high");
3404 OBJ(i8k_left_fan_rpm) {
3405 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3407 OBJ(i8k_right_fan_rpm) {
3408 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3410 OBJ(i8k_ac_status) {
3412 sscanf(i8k.ac_status, "%d", &ac_status);
3413 if(ac_status == -1) {
3414 snprintf(p, p_max_size,"disabled (read i8k docs)");
3415 } if(ac_status == 0) {
3416 snprintf(p, p_max_size, "off");
3417 } if(ac_status == 1) {
3418 snprintf(p, p_max_size, "on");
3421 OBJ(i8k_buttons_status) {
3422 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3426 get_ibm_acpi_fan(p, p_max_size);
3429 get_ibm_acpi_temps();
3430 snprintf(p, p_max_size, "%d",
3431 ibm_acpi.temps[obj->data.sensor]);
3434 get_ibm_acpi_volume(p, p_max_size);
3436 OBJ(ibm_brightness) {
3437 get_ibm_acpi_brightness(p, p_max_size);
3440 get_powerbook_batt_info(p, p_max_size, obj->data.i);
3442 #endif /* __linux__ */
3446 new_font(p, obj->data.s);
3449 void format_diskio(unsigned int diskio_value)
3452 if (diskio_value > 1024*1024) {
3453 snprintf(p, p_max_size, "%.1fGiB",
3454 (double)diskio_value/1024/1024);
3455 } else if (diskio_value > 1024) {
3456 snprintf(p, p_max_size, "%.1fMiB",
3457 (double)diskio_value/1024);
3458 } else if (diskio_value > 0) {
3459 snprintf(p, p_max_size, "%dKiB", diskio_value);
3461 snprintf(p, p_max_size, "%dB", diskio_value);
3464 if (diskio_value > 1024*1024) {
3465 snprintf(p, 12, "%.1fGiB ",
3466 (double)diskio_value/1024/1024);
3467 } else if (diskio_value > 1024) {
3468 snprintf(p, 12, "%.1fMiB ",
3469 (double)diskio_value/1024);
3470 } else if (diskio_value > 0) {
3471 snprintf(p, 12, "%dKiB ", diskio_value);
3473 snprintf(p, 12, "%dB ", diskio_value);
3478 format_diskio(diskio_value);
3481 format_diskio(diskio_write_value);
3484 format_diskio(diskio_read_value);
3487 new_graph(p, obj->a,
3488 obj->b, obj->c, obj->d,
3489 diskio_value, obj->e, 1);
3491 OBJ(diskiograph_read) {
3492 new_graph(p, obj->a,
3493 obj->b, obj->c, obj->d,
3494 diskio_read_value, obj->e, 1);
3496 OBJ(diskiograph_write) {
3497 new_graph(p, obj->a,
3498 obj->b, obj->c, obj->d,
3499 diskio_write_value, obj->e, 1);
3503 snprintf(p, p_max_size, "%d",
3504 (int) (obj->data.net->
3508 snprintf(p, 6, "%d ",
3509 (int) (obj->data.net->
3515 snprintf(p, p_max_size, "%.1f",
3517 recv_speed / 1024.0);
3519 snprintf(p, 8, "%.1f ",
3521 recv_speed / 1024.0);
3523 OBJ(downspeedgraph) {
3525 if (obj->data.net->recv_speed == 0)
3526 obj->data.net->recv_speed = 0.01;
3528 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3529 (obj->data.net->recv_speed /
3530 1024.0), obj->e, 1);
3534 i = obj->data.ifblock.pos - 1;
3544 snprintf(p, p_max_size, "%u.%u.%u.%u",
3545 obj->data.net->addr.
3547 obj->data.net->addr.
3549 obj->data.net->addr.
3551 obj->data.net->addr.
3557 snprintf(p, p_max_size, "%d",
3558 obj->data.net->linkstatus);
3561 snprintf(p, 5, "%4d ",
3562 obj->data.net->linkstatus);
3565 #if defined(IMLIB2) && defined(X11)
3570 Imlib_Image image, buffer;
3571 image = imlib_load_image(obj->data.s);
3572 imlib_context_set_image(image);
3575 w = imlib_image_get_width();
3576 h = imlib_image_get_height();
3577 buffer = imlib_create_image(w, h);
3578 imlib_context_set_display(display);
3579 imlib_context_set_drawable(window.drawable);
3580 imlib_context_set_colormap(DefaultColormap(display, screen));
3581 imlib_context_set_visual(DefaultVisual(display, screen));
3582 imlib_context_set_image(buffer);
3583 imlib_blend_image_onto_image(image, 0, 0, 0, w, h, text_start_x, text_start_y, w, h);
3584 imlib_render_image_on_drawable(text_start_x, text_start_y);
3586 imlib_context_set_image(image);
3593 FILE *fp = popen(obj->data.s, "r");
3594 int length = fread(p, 1, p_max_size, fp);
3597 /*output[length] = '\0';
3598 if (length > 0 && output[length - 1] == '\n') {
3599 output[length - 1] = '\0';
3602 if (length > 0 && p[length - 1] == '\n') {
3603 p[length - 1] = '\0';
3606 //parse_conky_vars(output, p, cur);
3610 FILE *fp = popen(obj->data.s, "r");
3611 int n2 = fread(p, 1, p_max_size, fp);
3615 if (n2 && p[n2 - 1] == '\n')
3624 if (sscanf(p, "%lf", &barnum) == 0) {
3625 ERR("reading execbar value failed (perhaps it's not the correct format?)");
3627 if (barnum > 100 || barnum < 0) {
3628 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
3630 barnum = barnum / 100.0;
3631 new_bar(p, 0, 4, (int) (barnum * 255.0));
3637 FILE *fp = popen(obj->data.s, "r");
3638 int n2 = fread(p, 1, p_max_size, fp);
3642 if (n2 && p[n2 - 1] == '\n')
3651 if (sscanf(p, "%lf", &barnum) == 0) {
3652 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
3654 if (barnum > 100 || barnum < 0) {
3655 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
3657 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
3662 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3663 new_bar(p, 0, 4, (int) obj->f);
3666 FILE *fp = popen(obj->data.execi.cmd, "r");
3667 int n2 = fread(p, 1, p_max_size, fp);
3670 if (n2 && p[n2 - 1] == '\n')
3679 if (sscanf(p, "%f", &barnum) == 0) {
3680 ERR("reading execibar value failed (perhaps it's not the correct format?)");
3682 if (barnum > 100 || barnum < 0) {
3683 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
3685 obj->f = 255 * barnum / 100.0;
3686 new_bar(p, 0, 4, (int) obj->f);
3688 obj->data.execi.last_update =
3689 current_update_time;
3693 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3694 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
3697 FILE *fp = popen(obj->data.execi.cmd, "r");
3698 int n2 = fread(p, 1, p_max_size, fp);
3701 if (n2 && p[n2 - 1] == '\n')
3710 if (sscanf(p, "%f", &barnum) == 0) {
3711 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
3713 if (barnum > 100 || barnum < 0) {
3714 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
3717 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
3719 obj->data.execi.last_update = current_update_time;
3725 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
3726 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3728 char *output = obj->data.execi.buffer;
3729 FILE *fp = popen(obj->data.execi.cmd, "r");
3730 //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3731 int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3734 output[length] = '\0';
3735 if (length > 0 && output[length - 1] == '\n') {
3736 output[length - 1] = '\0';
3738 obj->data.execi.last_update = current_update_time;
3739 snprintf(p, p_max_size, "%s", output);
3741 //parse_conky_vars(output, p, cur);
3744 if (!obj->data.texeci.p_timed_thread)
3746 obj->data.texeci.p_timed_thread=
3747 timed_thread_create ((void*)threaded_exec, (void*) obj,
3748 obj->data.texeci.interval * 1000000);
3749 if (!obj->data.texeci.p_timed_thread)
3750 ERR("Error starting texeci thread");
3751 timed_thread_register (obj->data.texeci.p_timed_thread,
3752 &obj->data.texeci.p_timed_thread);
3754 timed_thread_lock (obj->data.texeci.p_timed_thread);
3755 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
3756 timed_thread_unlock (obj->data.texeci.p_timed_thread);
3758 #endif /* HAVE_POPEN */
3760 if (obj->global_mode && info.mail) { // this means we use info
3761 if (!info.mail->p_timed_thread)
3763 info.mail->p_timed_thread =
3764 timed_thread_create ((void*)imap_thread,
3766 info.mail->interval * 1000000);
3767 if (!info.mail->p_timed_thread)
3768 ERR("Error starting imap thread");
3769 timed_thread_register (info.mail->p_timed_thread,
3770 &info.mail->p_timed_thread);
3772 timed_thread_lock (info.mail->p_timed_thread);
3773 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3774 timed_thread_unlock (info.mail->p_timed_thread);
3775 } else if (obj->data.mail) { // this means we use obj
3776 if (!obj->data.mail->p_timed_thread)
3778 obj->data.mail->p_timed_thread =
3779 timed_thread_create ((void*)imap_thread,
3780 (void*)obj->data.mail,
3781 obj->data.mail->interval * 1000000);
3782 if (!obj->data.mail->p_timed_thread)
3783 ERR("Error starting imap thread");
3784 timed_thread_register (obj->data.mail->p_timed_thread,
3785 &obj->data.mail->p_timed_thread);
3787 timed_thread_lock (obj->data.mail->p_timed_thread);
3788 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3789 timed_thread_unlock (obj->data.mail->p_timed_thread);
3790 } else if (!obj->a) { // something is wrong, warn once then stop
3791 ERR("Theres a problem with your imap_unseen settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3795 OBJ(imap_messages) {
3796 if (obj->global_mode && info.mail) { // this means we use info
3797 if (!info.mail->p_timed_thread)
3799 info.mail->p_timed_thread =
3800 timed_thread_create ((void*)imap_thread,
3802 info.mail->interval * 1000000);
3803 if (!info.mail->p_timed_thread)
3804 ERR("Error starting imap thread");
3805 timed_thread_register (info.mail->p_timed_thread,
3806 &info.mail->p_timed_thread);
3808 timed_thread_lock (info.mail->p_timed_thread);
3809 snprintf(p, p_max_size, "%lu", info.mail->messages);
3810 timed_thread_unlock (info.mail->p_timed_thread);
3811 } else if (obj->data.mail) { // this means we use obj
3812 if (!obj->data.mail->p_timed_thread)
3814 obj->data.mail->p_timed_thread =
3815 timed_thread_create ((void*)imap_thread,
3816 (void*)obj->data.mail,
3817 obj->data.mail->interval * 1000000);
3818 if (!obj->data.mail->p_timed_thread)
3819 ERR("Error starting imap thread");
3820 timed_thread_register (obj->data.mail->p_timed_thread,
3821 &obj->data.mail->p_timed_thread);
3823 timed_thread_lock (obj->data.mail->p_timed_thread);
3824 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
3825 timed_thread_lock (obj->data.mail->p_timed_thread);
3826 } else if (!obj->a) { // something is wrong, warn once then stop
3827 ERR("Theres a problem with your imap_messages settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3832 if (obj->global_mode && info.mail) { // this means we use info
3833 if (!info.mail->p_timed_thread)
3835 info.mail->p_timed_thread =
3836 timed_thread_create ((void*)pop3_thread,
3838 info.mail->interval * 1000000);
3839 if (!info.mail->p_timed_thread)
3840 ERR("Error starting pop3 thread");
3841 timed_thread_register (info.mail->p_timed_thread,
3842 &info.mail->p_timed_thread);
3844 timed_thread_lock (info.mail->p_timed_thread);
3845 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3846 timed_thread_unlock (info.mail->p_timed_thread);
3847 } else if (obj->data.mail) { // this means we use obj
3848 if (!obj->data.mail->p_timed_thread)
3850 obj->data.mail->p_timed_thread =
3851 timed_thread_create ((void*)pop3_thread,
3853 obj->data.mail->interval * 1000000);
3854 if (!obj->data.mail->p_timed_thread)
3855 ERR("Error starting pop3 thread");
3856 timed_thread_register (obj->data.mail->p_timed_thread,
3857 &obj->data.mail->p_timed_thread);
3859 timed_thread_lock (obj->data.mail->p_timed_thread);
3860 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3861 timed_thread_unlock (obj->data.mail->p_timed_thread);
3862 } else if (!obj->a) { // something is wrong, warn once then stop
3863 ERR("Theres a problem with your pop3_unseen settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3868 if (obj->global_mode && info.mail) { // this means we use info
3869 if (!info.mail->p_timed_thread)
3871 info.mail->p_timed_thread =
3872 timed_thread_create ((void*)pop3_thread,
3874 info.mail->interval * 1000000);
3875 if (!info.mail->p_timed_thread)
3876 ERR("Error starting pop3 thread");
3877 timed_thread_register (info.mail->p_timed_thread,
3878 &info.mail->p_timed_thread);
3880 timed_thread_lock (info.mail->p_timed_thread);
3881 snprintf(p, p_max_size, "%.1f", info.mail->used/1024.0/1024.0);
3882 timed_thread_unlock (info.mail->p_timed_thread);
3883 } else if (obj->data.mail) { // this means we use obj
3884 if (!obj->data.mail->p_timed_thread)
3886 obj->data.mail->p_timed_thread =
3887 timed_thread_create ((void*)pop3_thread,
3888 (void*)obj->data.mail,
3889 obj->data.mail->interval * 1000000);
3890 if (!obj->data.mail->p_timed_thread)
3891 ERR("Error starting pop3 thread");
3892 timed_thread_register (obj->data.mail->p_timed_thread,
3893 &obj->data.mail->p_timed_thread);
3895 timed_thread_lock (obj->data.mail->p_timed_thread);
3896 snprintf(p, p_max_size, "%.1f", obj->data.mail->used/1024.0/1024.0);
3897 timed_thread_unlock (obj->data.mail->p_timed_thread);
3898 } else if (!obj->a) { // something is wrong, warn once then stop
3899 ERR("Theres a problem with your pop3_used settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3904 if (obj->data.fs != NULL) {
3905 if (obj->data.fs->size == 0)
3924 if (obj->data.fs != NULL)
3925 human_readable(obj->data.fs->avail,
3929 if (obj->data.fs != NULL) {
3930 if (obj->data.fs->size)
3931 snprintf(p, p_max_size, "%*d",
3940 snprintf(p, p_max_size, "0");
3944 if (obj->data.fs != NULL)
3945 human_readable(obj->data.fs->size,
3949 if (obj->data.fs != NULL)
3950 human_readable(obj->data.fs->size -
3951 (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
3955 if (obj->data.fs != NULL) {
3956 if (obj->data.fs->size == 0)
3974 if (obj->data.fs != NULL) {
3975 if (obj->data.fs->size)
3976 snprintf(p, 4, "%d",
3986 snprintf(p, p_max_size, "0");
3990 float *v = info.loadavg;
3992 if (obj->data.loadavg[2])
3993 snprintf(p, p_max_size, "%.2f %.2f %.2f",
3994 v[obj->data.loadavg[0] -
3996 v[obj->data.loadavg[1] -
3998 v[obj->data.loadavg[2] -
4000 else if (obj->data.loadavg[1])
4001 snprintf(p, p_max_size, "%.2f %.2f",
4002 v[obj->data.loadavg[0] -
4004 v[obj->data.loadavg[1] -
4006 else if (obj->data.loadavg[0])
4007 snprintf(p, p_max_size, "%.2f",
4008 v[obj->data.loadavg[0] -
4012 new_goto(p, obj->data.i);
4015 new_tab(p, obj->data.pair.a, obj->data.pair.b);
4018 new_hr(p, obj->data.i);
4025 temp = get_hddtemp_info(obj->data.hddtemp.dev,
4026 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
4028 snprintf(p, p_max_size, "N/A");
4029 } else if (unit == '*') {
4030 snprintf(p, p_max_size, "%s", temp);
4032 snprintf(p, p_max_size, "%s°%c", temp, unit);
4037 new_offset(p, obj->data.i);
4040 new_voffset(p, obj->data.i);
4045 r = get_i2c_info(&obj->data.i2c.fd,
4047 obj->data.i2c.devtype,
4048 obj->data.i2c.type);
4050 if (r >= 100.0 || r == 0)
4051 snprintf(p, p_max_size, "%d", (int) r);
4053 snprintf(p, p_max_size, "%.1f", r);
4056 new_alignr(p, obj->data.i);
4059 new_alignc(p, obj->data.i);
4063 if ((obj->data.ifblock.s)
4064 && (stat(obj->data.ifblock.s, &tmp) ==
4066 i = obj->data.ifblock.pos;
4073 if ((obj->data.ifblock.s)
4074 && (!check_mount(obj->data.ifblock.s))) {
4075 i = obj->data.ifblock.pos;
4082 if ((obj->data.ifblock.s)
4083 && system(obj->data.ifblock.s)) {
4084 i = obj->data.ifblock.pos;
4091 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4094 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4099 human_readable(cur->mem * 1024, p, 255);
4102 human_readable(cur->memmax * 1024, p, 255);
4107 snprintf(p, p_max_size, "%*Lu",
4112 snprintf(p, 4, "%*Lu ",
4119 new_bar(p, obj->data.pair.a,
4121 cur->memmax ? (cur->mem * 255) /
4126 new_graph(p, obj->a,
4127 obj->b, obj->c, obj->d,
4128 cur->memmax ? (cur->mem * 100.0) /
4129 (cur->memmax) : 0.0, 100, 1);
4133 snprintf(p, p_max_size, "%d",
4134 mixer_get_avg(obj->data.l));
4137 snprintf(p, p_max_size, "%d",
4138 mixer_get_left(obj->data.l));
4141 snprintf(p, p_max_size, "%d",
4142 mixer_get_right(obj->data.l));
4145 new_bar(p, obj->data.mixerbar.w,
4146 obj->data.mixerbar.h,
4147 mixer_get_avg(obj->data.mixerbar.
4151 new_bar(p, obj->data.mixerbar.w,
4152 obj->data.mixerbar.h,
4153 mixer_get_left(obj->data.mixerbar.
4157 new_bar(p, obj->data.mixerbar.w,
4158 obj->data.mixerbar.h,
4159 mixer_get_right(obj->data.mixerbar.
4165 snprintf(p, p_max_size, "%d", cur->mail_count);
4168 snprintf(p, p_max_size, "%d", cur->new_mail_count);
4172 snprintf(p, p_max_size, "%s",
4173 cur->uname_s.nodename);
4176 new_outline(p, obj->data.l);
4180 snprintf(p, p_max_size, "%hu", cur->procs);
4182 snprintf(p, 5, "%hu ",
4185 OBJ(running_processes) {
4187 snprintf(p, p_max_size, "%hu",
4190 snprintf(p, 3, "%hu ",
4194 snprintf(p, p_max_size, "%s", obj->data.s);
4197 new_bg(p, obj->data.l);
4200 new_stippled_hr(p, obj->data.pair.a,
4204 human_readable(cur->swap * 1024, p, 255);
4207 human_readable(cur->swapmax * 1024, p,
4211 if (cur->swapmax == 0) {
4212 strncpy(p, "No swap", 255);
4215 snprintf(p, 255, "%*Lu",
4221 snprintf(p, 4, "%*Lu ",
4229 new_bar(p, obj->data.pair.a,
4231 cur->swapmax ? (cur->swap * 255) /
4232 (cur->swapmax) : 0);
4235 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4238 time_t t = time(NULL);
4239 struct tm *tm = localtime(&t);
4240 setlocale(LC_TIME, "");
4241 strftime(p, p_max_size, obj->data.s, tm);
4244 time_t t = time(NULL);
4245 struct tm *tm = gmtime(&t);
4246 strftime(p, p_max_size, obj->data.s, tm);
4250 if (obj->data.tztime.tz) {
4251 oldTZ = getenv("TZ");
4252 setenv("TZ", obj->data.tztime.tz, 1);
4255 time_t t = time(NULL);
4256 struct tm *tm = localtime(&t);
4257 setlocale(LC_TIME, "");
4258 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4260 setenv("TZ", oldTZ, 1);
4265 // Needless to free oldTZ since getenv gives ptr to static data
4268 human_readable(obj->data.net->recv, p,
4272 human_readable(obj->data.net->trans, p,
4276 snprintf(p, p_max_size, "%d", total_updates);
4280 snprintf(p, p_max_size, "%d",
4281 (int) (obj->data.net->
4285 snprintf(p, 5, "%d ",
4286 (int) (obj->data.net->
4292 snprintf(p, p_max_size, "%.1f",
4294 trans_speed / 1024.0);
4296 snprintf(p, 8, "%.1f ",
4298 trans_speed / 1024.0);
4302 if (obj->data.net->trans_speed == 0)
4303 obj->data.net->trans_speed = 0.01;
4305 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4306 (obj->data.net->trans_speed /
4307 1024.0), obj->e, 1);
4310 format_seconds_short(p, p_max_size,
4314 format_seconds(p, p_max_size, (int) cur->uptime);
4317 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
4319 snprintf(p, p_max_size, "%s", get_apm_adapter());
4321 OBJ(apm_battery_life) {
4323 msg = get_apm_battery_life();
4324 snprintf(p, p_max_size, "%s", msg);
4327 OBJ(apm_battery_time) {
4329 msg = get_apm_battery_time();
4330 snprintf(p, p_max_size, "%s", msg);
4333 #endif /* __FreeBSD__ */
4337 snprintf(p, p_max_size, "%s", cur->mpd.title);
4340 snprintf(p, p_max_size, "%s", cur->mpd.artist);
4343 snprintf(p, p_max_size, "%s", cur->mpd.album);
4346 snprintf(p, p_max_size, "%s", cur->mpd.random);
4349 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4352 snprintf(p, p_max_size, "%s", cur->mpd.track);
4355 snprintf(p, p_max_size, "%s", cur->mpd.name);
4358 snprintf(p, p_max_size, "%s", cur->mpd.file);
4361 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4364 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4367 snprintf(p, p_max_size, "%s", cur->mpd.status);
4370 int days = 0, hours = 0, minutes =
4372 int tmp = cur->mpd.elapsed;
4373 while (tmp >= 86400) {
4377 while (tmp >= 3600) {
4387 snprintf(p, p_max_size, "%i days %i:%02i:%02i",
4388 days, hours, minutes,
4391 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4394 snprintf(p, p_max_size, "%i:%02i", minutes,
4398 int days = 0, hours = 0, minutes =
4400 int tmp = cur->mpd.length;
4401 while (tmp >= 86400) {
4405 while (tmp >= 3600) {
4415 snprintf(p, p_max_size,
4416 "%i days %i:%02i:%02i",
4417 days, hours, minutes,
4420 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4423 snprintf(p, p_max_size, "%i:%02i", minutes,
4427 snprintf(p, p_max_size, "%2.0f",
4428 cur->mpd.progress * 100);
4431 new_bar(p, obj->data.pair.a,
4433 (int) (cur->mpd.progress *
4437 if (strlen(cur->mpd.title) < 2 && strlen(cur->mpd.title) < 2) {
4438 snprintf(p, p_max_size, "%s", cur->mpd.file);
4440 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist, cur->mpd.title);
4446 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4449 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4452 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4455 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4457 OBJ(xmms2_comment) {
4458 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4460 OBJ(xmms2_decoder) {
4461 snprintf(p, p_max_size, "%s", cur->xmms2.decoder);
4463 OBJ(xmms2_transport) {
4464 snprintf(p, p_max_size, "%s", cur->xmms2.transport);
4467 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4470 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4473 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4475 OBJ(xmms2_tracknr) {
4476 if (cur->xmms2.tracknr != -1)
4477 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4479 OBJ(xmms2_bitrate) {
4480 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4483 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4486 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4488 OBJ(xmms2_elapsed) {
4489 int tmp = cur->xmms2.elapsed;
4490 snprintf(p, p_max_size, "%02d:%02d",
4491 tmp / 60000, (tmp / 1000) % 60);
4493 OBJ(xmms2_duration) {
4494 int tmp = cur->xmms2.duration;
4495 snprintf(p, p_max_size, "%02d:%02d",
4496 tmp / 60000, (tmp / 1000) % 60);
4498 OBJ(xmms2_percent) {
4499 snprintf(p, p_max_size, "%2.0f",
4500 cur->xmms2.progress * 100);
4503 new_bar(p, obj->data.pair.a,
4505 (int) (cur->xmms2.progress *
4509 if (strlen(cur->xmms2.title) < 2 && strlen(cur->xmms2.title) < 2) {
4510 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4512 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist, cur->xmms2.title);
4517 OBJ(audacious_status) {
4518 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_STATUS]);
4520 OBJ(audacious_title) {
4521 snprintf(p, cur->audacious.max_title_len > 0 ?
4522 cur->audacious.max_title_len : p_max_size, "%s",
4523 cur->audacious.items[AUDACIOUS_TITLE]);
4525 OBJ(audacious_length) {
4526 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH]);
4528 OBJ(audacious_length_seconds) {
4529 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4531 OBJ(audacious_position) {
4532 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION]);
4534 OBJ(audacious_position_seconds) {
4535 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4537 OBJ(audacious_bitrate) {
4538 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_BITRATE]);
4540 OBJ(audacious_frequency) {
4541 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FREQUENCY]);
4543 OBJ(audacious_channels) {
4544 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_CHANNELS]);
4546 OBJ(audacious_filename) {
4547 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FILENAME]);
4549 OBJ(audacious_playlist_length) {
4550 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4552 OBJ(audacious_playlist_position) {
4553 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4555 OBJ(audacious_bar) {
4557 progress= atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4558 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4559 new_bar(p,obj->a,obj->b,(int)(progress*255.0f));
4564 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4567 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4570 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4573 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4576 snprintf(p, p_max_size, "%i", cur->bmpx.track);
4579 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4583 if (obj->data.top.type == TOP_NAME
4584 && obj->data.top.num >= 0
4585 && obj->data.top.num < 10) {
4586 // if we limit the buffer and add a bunch of space after, it stops the thing from
4587 // moving other shit around, which is really fucking annoying
4588 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
4589 } else if (obj->data.top.type == TOP_CPU
4590 && obj->data.top.num >= 0
4591 && obj->data.top.num < 10) {
4592 snprintf(p, 7, "%3.2f ",
4593 cur->cpu[obj->data.top.
4595 } else if (obj->data.top.type == TOP_PID
4596 && obj->data.top.num >= 0
4597 && obj->data.top.num < 10) {
4598 snprintf(p, 8, "%i ",
4599 cur->cpu[obj->data.top.
4601 } else if (obj->data.top.type == TOP_MEM
4602 && obj->data.top.num >= 0
4603 && obj->data.top.num < 10) {
4604 snprintf(p, 7, "%3.2f ",
4605 cur->cpu[obj->data.top.
4610 if (obj->data.top.type == TOP_NAME
4611 && obj->data.top.num >= 0
4612 && obj->data.top.num < 10) {
4613 // if we limit the buffer and add a bunch of space after, it stops the thing from
4614 // moving other shit around, which is really fucking annoying
4617 cur->memu[obj->data.top.
4619 } else if (obj->data.top.type == TOP_CPU
4620 && obj->data.top.num >= 0
4621 && obj->data.top.num < 10) {
4622 snprintf(p, 7, "%3.2f ",
4623 cur->memu[obj->data.top.
4625 } else if (obj->data.top.type == TOP_PID
4626 && obj->data.top.num >= 0
4627 && obj->data.top.num < 10) {
4628 snprintf(p, 8, "%i ",
4629 cur->memu[obj->data.top.
4631 } else if (obj->data.top.type == TOP_MEM
4632 && obj->data.top.num >= 0
4633 && obj->data.top.num < 10) {
4634 snprintf(p, 7, "%3.2f ",
4635 cur->memu[obj->data.top.
4642 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4643 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4645 obj->data.tail.last_update = current_update_time;
4649 fp = fopen(obj->data.tail.logfile, "rt");
4651 /* Send one message, but do not consistently spam on
4652 * missing logfiles. */
4653 if(obj->data.tail.readlines != 0) {
4654 ERR("tail logfile failed to open");
4655 strcpy(obj->data.tail.buffer, "Logfile Missing");
4657 obj->data.tail.readlines = 0;
4658 snprintf(p, p_max_size, "Logfile Missing");
4661 obj->data.tail.readlines = 0;
4662 /* -1 instead of 0 to avoid counting a trailing newline */
4663 fseek(fp, -1, SEEK_END);
4664 bsize = ftell(fp) + 1;
4665 for(iter = obj->data.tail.wantedlines; iter > 0; iter--) {
4666 nl = rev_fcharfind(fp, '\n', iter);
4670 obj->data.tail.readlines = iter;
4671 if(obj->data.tail.readlines < obj->data.tail.wantedlines) {
4672 fseek(fp, 0, SEEK_SET);
4675 fseek(fp, nl+1, SEEK_SET);
4678 /* Make sure bsize is at least 1 byte smaller than
4679 * the buffer max size. */
4680 if(bsize > TEXT_BUFFER_SIZE*20 - 1) {
4681 fseek(fp, bsize - TEXT_BUFFER_SIZE*20 - 1, SEEK_CUR);
4682 bsize = TEXT_BUFFER_SIZE*20 - 1;
4684 bsize = fread(obj->data.tail.buffer, 1, bsize, fp);
4687 /* Clean up trailing newline, make sure the buffer
4688 * is null terminated. */
4689 if(obj->data.tail.buffer[bsize-1] == '\n')
4690 obj->data.tail.buffer[bsize-1] = '\0';
4692 obj->data.tail.buffer[bsize] = '\0';
4693 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4696 strcpy(obj->data.tail.buffer, "Logfile Empty");
4697 snprintf(p, p_max_size, "Logfile Empty");
4700 } /* if cur_upd_time >= */
4702 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4706 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4707 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4709 obj->data.tail.last_update = current_update_time;
4713 fp = fopen(obj->data.tail.logfile, "rt");
4715 /* Send one message, but do not consistently spam on
4716 * missing logfiles. */
4717 if(obj->data.tail.readlines != 0) {
4718 ERR("head logfile failed to open");
4719 strcpy(obj->data.tail.buffer, "Logfile Missing");
4721 obj->data.tail.readlines = 0;
4722 snprintf(p, p_max_size, "Logfile Missing");
4724 obj->data.tail.readlines = 0;
4725 for(iter = obj->data.tail.wantedlines; iter > 0; iter--) {
4726 nl = fwd_fcharfind(fp, '\n', iter);
4730 obj->data.tail.readlines = iter;
4731 /* Make sure nl is at least 1 byte smaller than
4732 * the buffer max size. */
4733 if(nl > TEXT_BUFFER_SIZE*20 - 1) {
4734 nl = TEXT_BUFFER_SIZE*20 - 1;
4736 nl = fread(obj->data.tail.buffer, 1, nl, fp);
4739 /* Clean up trailing newline, make sure the buffer
4740 * is null terminated. */
4741 if (obj->data.tail.buffer[nl-1] == '\n') {
4742 obj->data.tail.buffer[nl-1] = '\0';
4745 obj->data.tail.buffer[nl] = '\0';
4747 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4750 strcpy(obj->data.tail.buffer, "Logfile Empty");
4751 snprintf(p, p_max_size, "Logfile Empty");
4753 } /* if fp == null */
4754 } /* cur_upd_time >= */
4756 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4759 #ifdef TCP_PORT_MONITOR
4762 /* grab a pointer to this port monitor */
4763 tcp_port_monitor_t * p_monitor =
4764 find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
4765 obj->data.tcp_port_monitor.port_range_begin,
4766 obj->data.tcp_port_monitor.port_range_end );
4768 snprintf(p, p_max_size, "monitor not found");
4772 /* now grab the text of interest */
4773 if ( peek_tcp_port_monitor( p_monitor,
4774 obj->data.tcp_port_monitor.item,
4775 obj->data.tcp_port_monitor.connection_index,
4776 p, p_max_size ) != 0 )
4778 snprintf(p, p_max_size, "monitor peek error");
4787 iconv_converting = 1;
4788 iconv_selected = obj->a;
4793 iconv_converting = 0;
4799 snprintf(p, p_max_size, "%d", cur->entropy.entropy_avail);
4801 OBJ(entropy_poolsize)
4803 snprintf(p, p_max_size, "%d", cur->entropy.poolsize);
4807 double entropy_perc;
4808 entropy_perc = (double)cur->entropy.entropy_avail /
4809 (double)cur->entropy.poolsize;
4810 new_bar(p,obj->a,obj->b,(int)(entropy_perc * 255.0f));
4818 unsigned int a = strlen(p);
4821 if (a > 0 && iconv_converting && iconv_selected > 0 && (iconv_cd[iconv_selected - 1] != (iconv_t)(-1))) {
4823 size_t dummy1, dummy2;
4824 char *ptr = buff_in;
4827 dummy1 = dummy2 = a;
4829 strncpy(buff_in, p, P_MAX_SIZE);
4831 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
4832 while (dummy1 > 0) {
4833 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1, &outptr, &dummy2);
4835 ERR("Iconv codeset conversion failed");
4840 /* It is nessecary when we are converting from multibyte to singlebyte codepage */
4851 double current_update_time, last_update_time;
4853 static void generate_text()
4855 struct information *cur = &info;
4863 current_update_time = get_time();
4866 /* fix diskio rates to b/s (use update_interval */
4867 diskio_read_value = diskio_read_value / update_interval;
4868 diskio_write_value = diskio_write_value / update_interval;
4869 diskio_value = diskio_value / update_interval;
4872 /* add things to the buffer */
4878 generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
4880 if (stuff_in_upper_case) {
4891 last_update_time = current_update_time;
4898 static void set_font()
4902 if (window.xftdraw != NULL) {
4903 XftDrawDestroy(window.xftdraw);
4905 window.xftdraw = XftDrawCreate(display, window.drawable,
4906 DefaultVisual(display,
4908 DefaultColormap(display,
4913 XSetFont(display, window.gc, fonts[selected_font].font->fid);
4919 static inline int get_string_width(const char *s)
4922 return *s ? calc_text_width(s, strlen(s)) : 0;
4928 static inline int get_string_width_special(char *s)
4941 if (*p == SPECIAL_CHAR) {
4942 /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
4943 for (i = 0; i < strlen(p); i++) {
4944 *(p + i) = *(p + i + 1);
4946 if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
4947 width += specials[special_index+index].width;
4954 if (strlen(final) > 1) {
4955 width += calc_text_width(final, strlen(final));
4965 static void text_size_updater(char *s);
4967 int last_font_height;
4968 static void update_text_area()
4972 /* update text size if it isn't fixed */
4977 text_width = minimum_width;
4980 last_font_height = font_height();
4981 for_each_line(text_buffer, text_size_updater);
4983 if (text_height < minimum_height)
4984 text_height = minimum_height;
4985 if (text_width > maximum_width && maximum_width > 0)
4986 text_width = maximum_width;
4989 /* get text position on workarea */
4990 switch (text_alignment) {
4997 x = workarea[2] - text_width - gap_x;
5004 y = workarea[3] - text_height - gap_y;
5008 x = workarea[2] - text_width - gap_x;
5009 y = workarea[3] - text_height - gap_y;
5013 case NONE: // Let the WM manage the window
5024 if (own_window && !fixed_pos) {
5027 text_start_x = border_margin + 1;
5028 text_start_y = border_margin + 1;
5029 window.x = x - border_margin - 1;
5030 window.y = y - border_margin - 1;
5034 /* If window size doesn't match to workarea's size, then window
5035 * probably includes panels (gnome).
5036 * Blah, doesn't work on KDE. */
5037 if (workarea[2] != window.width
5038 || workarea[3] != window.height) {
5052 static int cur_x, cur_y; /* current x and y for drawing */
5053 static int draw_mode; /* FG, BG or OUTLINE */
5054 static long current_color;
5057 static void text_size_updater(char *s)
5061 /* get string widths and skip specials */
5064 if (*p == SPECIAL_CHAR) {
5066 w += get_string_width(s);
5069 if (specials[special_index].type == BAR
5070 || specials[special_index].type == GRAPH) {
5071 w += specials[special_index].width;
5072 if (specials[special_index].height > last_font_height) {
5073 last_font_height = specials[special_index].height;
5074 last_font_height += font_ascent();
5076 } else if (specials[special_index].type == OFFSET) {
5077 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
5078 } else if (specials[special_index].type == VOFFSET) {
5079 last_font_height += specials[special_index].arg;
5080 } else if (specials[special_index].type == GOTO) {
5081 if (specials[special_index].arg >= 0)
5082 w += (int)specials[special_index].arg - cur_x;
5083 } else if (specials[special_index].type == TAB) {
5084 int start = specials[special_index].arg;
5085 int step = specials[special_index].width;
5086 if (!step || step < 0)
5088 w += step - (cur_x - text_start_x - start) % step;
5089 } else if (specials[special_index].type == FONT) {
5090 selected_font = specials[special_index].font_added;
5091 if (font_height() > last_font_height) {
5092 last_font_height = font_height();
5101 w += get_string_width(s);
5102 if (w > text_width) {
5105 if (text_width > maximum_width && maximum_width) {
5106 text_width = maximum_width;
5109 text_height += last_font_height;
5110 last_font_height = font_height();
5114 static inline void set_foreground_color(long c)
5117 XSetForeground(display, window.gc, c);
5121 static void draw_string(const char *s)
5125 int i, i2, pos, width_of_s;
5128 width_of_s = get_string_width(s);
5129 if (out_to_console) {
5131 fflush(stdout); /* output immediately, don't buffer */
5133 memset(tmpstring1,0,TEXT_BUFFER_SIZE);
5134 memset(tmpstring2,0,TEXT_BUFFER_SIZE);
5135 strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
5139 snprintf(space, 2, " ");
5141 max = ((text_width - width_of_s) / get_string_width(space));
5144 * This code looks for tabs in the text and coverts them to spaces.
5145 * The trick is getting the correct number of spaces,
5146 * and not going over the window's size without forcing
5147 * the window larger.
5149 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
5150 if (tmpstring1[i] == '\t') // 9 is ascii tab
5154 i2 < (8 - (1 + pos) % 8) && added <= max;
5157 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
5158 fprintf(stderr,"buffer overrun detected\n");
5160 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
5165 if (tmpstring1[i] != 9) {
5167 if ( pos > TEXT_BUFFER_SIZE-1 )
5168 fprintf(stderr,"buffer overrun detected\n");
5170 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
5176 if (text_width == maximum_width) {
5177 /* this means the text is probably pushing the limit, so we'll chop it */
5178 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
5179 tmpstring2[strlen(tmpstring2)-1] = '\0';
5189 c.pixel = current_color;
5190 XQueryColor(display, DefaultColormap(display, screen), &c);
5193 c2.color.red = c.red;
5194 c2.color.green = c.green;
5195 c2.color.blue = c.blue;
5196 c2.color.alpha = fonts[selected_font].font_alpha;
5198 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5199 cur_x, cur_y, (XftChar8 *) s,
5202 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5203 cur_x, cur_y, (XftChar8 *) s,
5209 XDrawString(display, window.drawable, window.gc,
5210 cur_x, cur_y, s, strlen(s));
5212 cur_x += width_of_s;
5214 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
5217 long redmask, greenmask, bluemask;
5219 void set_up_gradient()
5222 colour_depth = DisplayPlanes(display, screen);
5226 if (colour_depth != 24 && colour_depth != 16) {
5227 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
5233 for(i = (colour_depth / 3)-1; i>=0; i--) {
5235 greenmask |= 1 << i;
5238 if (colour_depth%3 == 1) {
5239 greenmask |= 1 << (colour_depth / 3);
5241 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
5242 greenmask = greenmask << (colour_depth / 3);
5245 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 */
5247 int red1, green1, blue1; // first colour
5248 int red2, green2, blue2; // second colour
5249 int red3 = 0, green3 = 0, blue3 = 0; // difference
5250 short redshift = (2*colour_depth / 3 + colour_depth%3);
5251 short greenshift = (colour_depth / 3);
5252 red1 = (first_colour & redmask) >> redshift;
5253 green1 = (first_colour & greenmask) >> greenshift;
5254 blue1 = first_colour & bluemask;
5255 red2 = (last_colour & redmask) >> redshift;
5256 green2 = (last_colour & greenmask) >> greenshift;
5257 blue2 = last_colour & bluemask;
5264 if (green1 > green2) {
5267 if (green1 < green2) {
5270 if (blue1 > blue2) {
5273 if (blue1 < blue2) {
5288 if (red1 > bluemask) {
5291 if (green1 > bluemask) {
5294 if (blue1 > bluemask) {
5297 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
5301 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
5302 if (colour_depth == 0) {
5305 int red1, green1, blue1; // first colour
5306 int red2, green2, blue2; // second colour
5307 long redshift = (2*colour_depth / 3 + colour_depth%3);
5308 long greenshift = (colour_depth / 3);
5309 int red3 = 0, green3 = 0, blue3 = 0; // difference
5310 red1 = (first_colour & redmask) >> redshift;
5311 green1 = (first_colour & greenmask) >> greenshift;
5312 blue1 = first_colour & bluemask;
5313 red2 = (last_colour & redmask) >> redshift;
5314 green2 = (last_colour & greenmask) >> greenshift;
5315 blue2 = last_colour & bluemask;
5316 red3 = abs(red1 - red2);
5317 green3 = abs(green1 - green2);
5318 blue3 = abs(blue1 - blue2);
5327 static void draw_line(char *s)
5331 cur_x = text_start_x;
5332 cur_y += font_ascent();
5334 short font_h = font_height();
5336 /* find specials and draw stuff */
5339 if (*p == SPECIAL_CHAR) {
5342 /* draw string before special */
5349 switch (specials[special_index].type) {
5350 case HORIZONTAL_LINE:
5353 specials[special_index].height;
5354 int mid = font_ascent() / 2;
5355 w = text_start_x + text_width -
5358 XSetLineAttributes(display,
5363 XDrawLine(display, window.drawable,
5374 specials[special_index].height;
5376 specials[special_index].arg;
5377 int mid = font_ascent() / 2;
5378 char ss[2] = { s, s };
5379 w = text_start_x + text_width -
5382 XSetLineAttributes(display,
5387 XSetDashes(display, window.gc, 0,
5389 XDrawLine(display, window.drawable,
5399 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5403 specials[special_index].height;
5405 specials[special_index].arg;
5406 int by = cur_y - (font_ascent() / 2) - 1;
5407 if (h < font_height()) {
5410 w = specials[special_index].width;
5413 text_width - cur_x - 1;
5417 XSetLineAttributes(display,
5423 XDrawRectangle(display,
5427 XFillRectangle(display,
5431 w * bar_usage / 255,
5433 if (specials[special_index].
5435 && specials[special_index].
5439 [special_index].height;
5446 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5450 specials[special_index].height;
5451 unsigned long last_colour = current_color;
5452 int by = cur_y - (font_ascent()/2) - 1;
5453 if (h < font_height()) {
5456 w = specials[special_index].width;
5458 w = text_start_x + text_width - cur_x - 1;
5461 if (draw_graph_borders) {
5462 XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
5463 XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
5465 XSetLineAttributes(display,
5472 int gradient_size = 0;
5473 float gradient_factor = 0;
5474 float gradient_update = 0;
5475 unsigned long tmpcolour = current_color;
5476 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5477 tmpcolour = specials[special_index].last_colour;
5478 gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
5479 gradient_factor = (float)gradient_size / (w - 2);
5481 for (i = w - 2; i > -1; i--) {
5482 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5483 XSetForeground(display, window.gc, tmpcolour);
5484 gradient_update += gradient_factor;
5485 while (gradient_update > 0) {
5486 tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
5490 if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
5493 XDrawLine(display, window.drawable, window.gc, cur_x + i + 1, by + h, cur_x + i + 1, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale); /* this is mugfugly, but it works */
5495 if (specials[special_index].
5497 && specials[special_index].
5501 [special_index].height;
5503 /* if (draw_mode == BG) {
5504 set_foreground_color(default_bg_color);
5506 else if (draw_mode == OUTLINE) {
5507 set_foreground_color(default_out_color);
5509 set_foreground_color(default_fg_color);
5511 set_foreground_color(last_colour);
5517 int old = font_ascent();
5518 cur_y -= font_ascent();
5519 selected_font = specials[special_index].font_added;
5520 if (cur_y + font_ascent() < cur_y + old) {
5523 cur_y += font_ascent();
5529 if (draw_mode == FG)
5530 set_foreground_color(specials
5536 if (draw_mode == BG)
5537 set_foreground_color(specials
5543 if (draw_mode == OUTLINE)
5544 set_foreground_color(specials
5550 w += specials[special_index].arg;
5554 cur_y += specials[special_index].arg;
5558 if (specials[special_index].arg >= 0)
5559 cur_x = (int)specials[special_index].arg;
5564 int start = specials[special_index].arg;
5565 int step = specials[special_index].width;
5566 if (!step || step < 0)
5568 w = step - (cur_x - text_start_x - start) % step;
5574 int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
5575 /*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);*/
5576 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5577 cur_x = pos_x - specials[special_index].arg;
5584 int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
5585 /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
5586 /*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);*/
5588 specials[special_index].arg)
5591 [special_index].arg;
5608 if (cur_y_add > 0) {
5610 cur_y -= font_descent();
5615 cur_y += font_descent();
5619 static void draw_text()
5622 cur_y = text_start_y;
5625 if (draw_borders && border_width > 0) {
5626 unsigned int b = (border_width + 1) / 2;
5628 if (stippled_borders) {
5630 { stippled_borders, stippled_borders };
5631 XSetLineAttributes(display, window.gc,
5632 border_width, LineOnOffDash,
5633 CapButt, JoinMiter);
5634 XSetDashes(display, window.gc, 0, ss, 2);
5636 XSetLineAttributes(display, window.gc,
5637 border_width, LineSolid,
5638 CapButt, JoinMiter);
5641 XDrawRectangle(display, window.drawable, window.gc,
5642 text_start_x - border_margin + b,
5643 text_start_y - border_margin + b,
5644 text_width + border_margin * 2 - 1 - b * 2,
5645 text_height + border_margin * 2 - 1 -
5652 for_each_line(text_buffer, draw_line);
5655 static void draw_stuff()
5659 if (draw_shades && !draw_outline) {
5662 set_foreground_color(default_bg_color);
5672 for (i = -1; i < 2; i++)
5673 for (j = -1; j < 2; j++) {
5674 if (i == 0 && j == 0)
5678 set_foreground_color(default_out_color);
5679 draw_mode = OUTLINE;
5686 set_foreground_color(default_fg_color);
5694 swap.swap_window = window.window;
5695 swap.swap_action = XdbeBackground;
5696 XdbeSwapBuffers(display, &swap, 1);
5702 static void clear_text(int exposures)
5706 return; /* The swap action is XdbeBackground, which clears */
5710 /* there is some extra space for borders and outlines */
5711 XClearArea(display, window.window,
5712 text_start_x - border_margin - 1,
5713 text_start_y - border_margin - 1,
5714 text_width + border_margin * 2 + 2,
5715 text_height + border_margin * 2 + 2,
5716 exposures ? True : 0);
5721 static int need_to_update;
5723 /* update_text() generates new text and clears old text area */
5724 static void update_text()
5733 static void main_loop()
5735 #ifdef SIGNAL_BLOCKING
5736 sigset_t newmask, oldmask;
5738 sigemptyset(&newmask);
5739 sigaddset(&newmask,SIGINT);
5740 sigaddset(&newmask,SIGTERM);
5741 sigaddset(&newmask,SIGUSR1);
5745 Region region = XCreateRegion();
5747 int event_base, error_base;
5748 if (!XDamageQueryExtension (display, &event_base, &error_base)) {
5749 ERR("Xdamage extension unavailable");
5751 Damage damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5752 XserverRegion region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5753 XserverRegion part = XFixesCreateRegionFromWindow(display, window.window, 0);
5754 #endif /* HAVE_XDAMAGE */
5758 while (total_run_times == 0 || info.looped < total_run_times) {
5761 #ifdef SIGNAL_BLOCKING
5762 /* block signals. we will inspect for pending signals later */
5763 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
5764 CRIT_ERR("unable to sigprocmask()");
5770 /* wait for X event or timeout */
5772 if (!XPending(display)) {
5777 update_interval - (get_time() -
5783 tv.tv_sec = (long) t;
5784 tv.tv_usec = (long) (t * 1000000) % 1000000;
5786 FD_SET(ConnectionNumber(display), &fdsr);
5789 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
5792 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
5797 ERR("can't select(): %s",
5808 if (need_to_update) {
5810 int wx = window.x, wy = window.y;
5818 /* resize window if it isn't right size */
5820 (text_width + border_margin * 2 + 1 !=
5822 || text_height + border_margin * 2 + 1 !=
5826 border_margin * 2 + 1;
5829 border_margin * 2 + 1;
5830 XResizeWindow(display,
5835 set_transparent_background(window.window);
5839 /* move window if it isn't in right position */
5842 || window.y != wy)) {
5843 XMoveWindow(display, window.window,
5844 window.x, window.y);
5854 r.x = text_start_x - border_margin;
5855 r.y = text_start_y - border_margin;
5856 r.width = text_width + border_margin * 2;
5857 r.height = text_height + border_margin * 2;
5858 XUnionRectWithRegion(&r, region, region);
5863 /* handle X events */
5865 while (XPending(display)) {
5867 XNextEvent(display, &ev);
5874 r.width = ev.xexpose.width;
5875 r.height = ev.xexpose.height;
5876 XUnionRectWithRegion(&r, region,
5882 case ReparentNotify:
5883 /* set background to ParentRelative for all parents */
5885 set_transparent_background(window.
5890 case ConfigureNotify:
5892 /* if window size isn't what expected, set fixed size */
5893 if (ev.xconfigure.width !=
5895 || ev.xconfigure.height !=
5897 if (window.width != 0
5898 && window.height != 0)
5901 /* clear old stuff before screwing up size and pos */
5907 if (XGetWindowAttributes(display, window.window, &attrs)) {
5922 border_margin * 2 - 1;
5925 border_margin * 2 - 1;
5926 if (text_width > maximum_width && maximum_width > 0)
5927 text_width = maximum_width;
5930 /* if position isn't what expected, set fixed pos, total_updates
5931 * avoids setting fixed_pos when window is set to weird locations
5933 /*if (total_updates >= 2 this is broken
5935 && (window.x != ev.xconfigure.x
5938 && (ev.xconfigure.x != 0
5939 || ev.xconfigure.y != 0)) {
5949 /* if an ordinary window with decorations */
5950 if ((window.type==TYPE_NORMAL) && (!TEST_HINT(window.hints,HINT_UNDECORATED)))
5952 /* allow conky to hold input focus.*/
5957 /* forward the click to the desktop window */
5958 XUngrabPointer(display, ev.xbutton.time);
5959 ev.xbutton.window = window.desktop;
5960 XSendEvent(display, ev.xbutton.window, False, ButtonPressMask, &ev);
5961 XSetInputFocus(display, ev.xbutton.window,RevertToParent,ev.xbutton.time);
5969 /* if an ordinary window with decorations */
5970 if ((window.type==TYPE_NORMAL) && (!TEST_HINT(window.hints,HINT_UNDECORATED)))
5972 /* allow conky to hold input focus.*/
5977 /* forward the release to the desktop window */
5978 ev.xbutton.window = window.desktop;
5979 XSendEvent(display, ev.xbutton.window, False, ButtonReleaseMask, &ev);
5988 if (ev.type == event_base + XDamageNotify) {
5989 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
5990 XFixesSetRegion(display, part, &dev->area, 1);
5991 XFixesUnionRegion(display, region2, region2, part);
5993 #endif /* HAVE_XDAMAGE */
5998 XDamageSubtract(display, damage, region2, None);
5999 XFixesSetRegion(display, region2, 0, 0);
6000 #endif /* HAVE_XDAMAGE */
6002 /* XDBE doesn't seem to provide a way to clear the back buffer without
6003 * interfering with the front buffer, other than passing XdbeBackground
6004 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
6005 * redraw the text even if it wasn't part of the exposed area. OTOH,
6006 * if we're not going to call draw_stuff at all, then no swap happens
6007 * and we can safely do nothing.
6010 if (!XEmptyRegion(region)) {
6014 r.x = text_start_x - border_margin;
6015 r.y = text_start_y - border_margin;
6016 r.width = text_width + border_margin * 2;
6017 r.height = text_height + border_margin * 2;
6018 XUnionRectWithRegion(&r, region, region);
6021 XSetRegion(display, window.gc, region);
6024 XftDrawSetClip(window.xftdraw, region);
6030 XDestroyRegion(region);
6031 region = XCreateRegion();
6035 #ifdef SIGNAL_BLOCKING
6036 /* unblock signals of interest and let handler fly */
6037 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
6038 CRIT_ERR("unable to sigprocmask()");
6041 switch(g_signal_pending) {
6044 ERR("received SIGUSR1. reloading the config file.");
6051 ERR("received SIGINT or SIGTERM to terminate. bye!");
6054 XDestroyRegion(region);
6057 return; /* return from main_loop */
6062 /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
6063 * but didn't write any code to deal with it. if you don't want to
6064 * handle a signal, don't set a handler on it in the first place. */
6065 if (g_signal_pending)
6066 ERR("ignoring signal (%d)", g_signal_pending);
6073 #if defined(X11) && defined(HAVE_XDAMAGE)
6074 XDamageDestroy(display, damage);
6075 XFixesDestroyRegion(display, region2);
6076 XFixesDestroyRegion(display, part);
6077 XDestroyRegion(region);
6079 #endif /* X11 && HAVE_XDAMAGE */
6082 static void load_config_file(const char *);
6084 /* reload the config file */
6085 void reload_config(void)
6087 timed_thread_destroy_registered_threads ();
6089 if (info.cpu_usage) {
6090 free(info.cpu_usage);
6091 info.cpu_usage = NULL;
6094 #ifdef TCP_PORT_MONITOR
6095 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6098 if (current_config) {
6100 load_config_file(current_config);
6102 /* re-init specials array */
6103 if ((specials = realloc ((void *)specials, sizeof(struct special_t)*max_specials)) == 0)
6104 ERR("failed to realloc specials array");
6109 XClearWindow(display, RootWindow(display, screen)); // clear the window first
6112 #ifdef TCP_PORT_MONITOR
6113 info.p_tcp_port_monitor_collection = NULL;
6116 if (create_audacious_thread() !=0)
6117 CRIT_ERR("unable to create audacious thread!");
6118 timed_thread_register (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
6120 extract_variable_text(text);
6129 timed_thread_destroy_registered_threads ();
6131 if (info.cpu_usage) {
6132 free(info.cpu_usage);
6133 info.cpu_usage = NULL;
6138 XdbeDeallocateBackBufferName(display, window.back_buffer);
6144 XDestroyWindow(display, window.window);
6145 XClearWindow(display, RootWindow(display, screen));
6151 XClearWindow(display, RootWindow(display, screen));
6156 XFreeGC(display, window.gc);
6159 free_text_objects(text_object_count, text_objects);
6160 text_object_count = 0;
6161 text_objects = NULL;
6163 if (text != original_text)
6166 free(current_config);
6167 free(current_mail_spool);
6169 #ifdef TCP_PORT_MONITOR
6170 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6171 info.p_tcp_port_monitor_collection = NULL;
6179 #ifdef HAVE_LIBDEXTER
6180 dexter_library_exit ();
6182 free (dexter_config);
6186 static int string_to_bool(const char *s)
6190 if (strcasecmp(s, "yes") == 0)
6192 if (strcasecmp(s, "true") == 0)
6194 if (strcasecmp(s, "1") == 0)
6199 static enum alignment string_to_alignment(const char *s)
6201 if (strcasecmp(s, "top_left") == 0)
6203 else if (strcasecmp(s, "top_right") == 0)
6205 else if (strcasecmp(s, "bottom_left") == 0)
6207 else if (strcasecmp(s, "bottom_right") == 0)
6208 return BOTTOM_RIGHT;
6209 else if (strcasecmp(s, "tl") == 0)
6211 else if (strcasecmp(s, "tr") == 0)
6213 else if (strcasecmp(s, "bl") == 0)
6215 else if (strcasecmp(s, "br") == 0)
6216 return BOTTOM_RIGHT;
6217 else if (strcasecmp(s, "none") == 0)
6224 static void set_default_configurations(void)
6226 fork_to_background = 0;
6227 total_run_times = 0;
6228 info.cpu_avg_samples = 2;
6229 info.net_avg_samples = 2;
6234 strcpy(info.mpd.host, "localhost");
6235 info.mpd.port = 6600;
6236 info.mpd.status = NULL;
6237 info.mpd.artist = NULL;
6238 info.mpd.album = NULL;
6239 info.mpd.title = NULL;
6240 info.mpd.random = NULL;
6241 info.mpd.track = NULL;
6242 info.mpd.name = NULL;
6243 info.mpd.file = NULL;
6246 info.xmms2.artist = NULL;
6247 info.xmms2.album = NULL;
6248 info.xmms2.title = NULL;
6249 info.xmms2.genre = NULL;
6250 info.xmms2.comment = NULL;
6251 info.xmms2.decoder = NULL;
6252 info.xmms2.transport = NULL;
6253 info.xmms2.url = NULL;
6254 info.xmms2.status = NULL;
6263 default_fg_color = WhitePixel(display, screen);
6264 default_bg_color = BlackPixel(display, screen);
6265 default_out_color = BlackPixel(display, screen);
6268 draw_graph_borders = 1;
6270 set_first_font("6x10");
6278 window.type=TYPE_NORMAL;
6280 strcpy(window.class_name, "Conky");
6282 sprintf(window.title,"%s - conky",info.uname_s.nodename);
6284 stippled_borders = 0;
6287 text_alignment = BOTTOM_LEFT;
6290 free(current_mail_spool);
6293 variable_substitute(MAIL_FILE, buf, 256);
6295 current_mail_spool = strdup(buf);
6299 update_interval = 10.0;
6300 stuff_in_upper_case = 0;
6302 #ifdef TCP_PORT_MONITOR
6303 tcp_port_monitor_args.max_port_monitor_connections = MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
6307 static void load_config_file(const char *f)
6309 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6313 set_default_configurations();
6319 char buf[256], *p, *p2, *name, *value;
6321 if (fgets(buf, 256, fp) == NULL)
6326 /* break at comment */
6327 p2 = strchr(p, '#');
6332 while (*p && isspace((int) *p))
6335 continue; /* empty line */
6341 while (*p2 && !isspace((int) *p2))
6344 *p2 = '\0'; /* break at name's end */
6351 while (*p && isspace((int) *p))
6356 p2 = value + strlen(value);
6357 while (isspace((int) *(p2 - 1)))
6363 #define CONF2(a) if (strcasecmp(name, a) == 0)
6364 #define CONF(a) else CONF2(a)
6365 #define CONF3(a,b) \
6366 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
6370 CONF2("alignment") {
6372 int a = string_to_alignment(value);
6380 CONF("background") {
6381 fork_to_background = string_to_bool(value);
6385 CONF2("background") {
6386 fork_to_background = string_to_bool(value);
6390 CONF("border_margin") {
6392 border_margin = strtol(value, 0, 0);
6396 CONF("border_width") {
6398 border_width = strtol(value, 0, 0);
6402 CONF("default_color") {
6404 default_fg_color = get_x11_color(value);
6408 CONF3("default_shade_color", "default_shadecolor") {
6410 default_bg_color = get_x11_color(value);
6414 CONF3("default_outline_color", "default_outlinecolor") {
6416 default_out_color = get_x11_color(value);
6423 info.mail = parse_mail_args(IMAP, value);
6430 info.mail = parse_mail_args(POP3, value);
6438 strncpy(info.mpd.host, value, 127);
6444 info.mpd.port = strtol(value, 0, 0);
6445 if (info.mpd.port < 1
6446 || info.mpd.port > 0xffff)
6450 CONF("mpd_password") {
6452 strncpy(info.mpd.password, value, 127);
6457 CONF("cpu_avg_samples") {
6459 cpu_avg_samples = strtol(value, 0, 0);
6460 if (cpu_avg_samples < 1
6461 || cpu_avg_samples > 14)
6470 CONF("net_avg_samples") {
6472 net_avg_samples = strtol(value, 0, 0);
6473 if (net_avg_samples < 1
6474 || net_avg_samples > 14)
6490 CONF("double_buffer") {
6491 use_xdbe = string_to_bool(value);
6495 CONF("override_utf8_locale") {
6496 utf8_mode = string_to_bool(value);
6499 CONF("draw_borders") {
6500 draw_borders = string_to_bool(value);
6502 CONF("draw_graph_borders") {
6503 draw_graph_borders = string_to_bool(value);
6505 CONF("draw_shades") {
6506 draw_shades = string_to_bool(value);
6508 CONF("draw_outline") {
6509 draw_outline = string_to_bool(value);
6512 CONF("out_to_console") {
6513 out_to_console = string_to_bool(value);
6515 CONF("use_spacer") {
6516 use_spacer = string_to_bool(value);
6521 use_xft = string_to_bool(value);
6525 set_first_font(value);
6530 if (value && font_count >= 0)
6531 fonts[0].font_alpha = atof(value)
6540 if (string_to_bool(value))
6541 ERR("Xft not enabled");
6544 /* xftfont silently ignored when no Xft */
6547 /* xftalpha is silently ignored when no Xft */
6552 set_first_font(value);
6561 gap_x = atoi(value);
6567 gap_y = atoi(value);
6572 CONF("mail_spool") {
6575 variable_substitute(value, buf, 256);
6579 if (current_mail_spool)
6580 free(current_mail_spool);
6581 current_mail_spool = strdup(buf);
6587 CONF("minimum_size") {
6590 (value, "%d %d", &minimum_width,
6591 &minimum_height) != 2)
6594 &minimum_width) != 1)
6599 CONF("maximum_width") {
6601 if (sscanf(value, "%d", &maximum_width) != 1)
6607 CONF("no_buffers") {
6608 no_buffers = string_to_bool(value);
6610 CONF("pad_percents") {
6611 pad_percents = atoi(value);
6615 CONF("own_window") {
6617 own_window = string_to_bool(value);
6621 CONF("own_window_class") {
6624 memset(window.class_name,0,sizeof(window.class_name));
6625 strncpy(window.class_name, value, sizeof(window.class_name)-1);
6630 CONF("own_window_title") {
6633 memset(window.title,0,sizeof(window.title));
6634 strncpy(window.title, value, sizeof(window.title)-1);
6639 CONF("own_window_transparent") {
6641 set_transparent = string_to_bool(value);
6645 CONF("own_window_colour") {
6647 background_colour = get_x11_color(value);
6649 ERR("Invalid colour for own_winder_colour (try omitting the '#' for hex colours");
6652 CONF("own_window_hints") {
6654 char *p_hint, *p_save;
6655 char delim[] = ", ";
6657 /* tokenize the value into individual hints */
6658 if ((p_hint=strtok_r(value, delim, &p_save)) != NULL)
6660 /*fprintf(stderr, "hint [%s] parsed\n", p_hint);*/
6661 if (strncmp(p_hint,"undecorate",10) == 0)
6662 SET_HINT(window.hints,HINT_UNDECORATED);
6663 else if (strncmp(p_hint,"below",5) == 0)
6664 SET_HINT(window.hints,HINT_BELOW);
6665 else if (strncmp(p_hint,"above",5) == 0)
6666 SET_HINT(window.hints,HINT_ABOVE);
6667 else if (strncmp(p_hint,"sticky",6) == 0)
6668 SET_HINT(window.hints,HINT_STICKY);
6669 else if (strncmp(p_hint,"skip_taskbar",12) == 0)
6670 SET_HINT(window.hints,HINT_SKIP_TASKBAR);
6671 else if (strncmp(p_hint,"skip_pager",10) == 0)
6672 SET_HINT(window.hints,HINT_SKIP_PAGER);
6676 p_hint=strtok_r(NULL, delim, &p_save);
6678 while (p_hint!=NULL);
6683 CONF("own_window_type") {
6685 if (strncmp(value,"normal",6)==0)
6686 window.type = TYPE_NORMAL;
6687 else if (strncmp(value,"desktop",7)==0)
6688 window.type = TYPE_DESKTOP;
6689 else if (strncmp(value,"override",8)==0)
6690 window.type = TYPE_OVERRIDE;
6698 CONF("stippled_borders") {
6700 stippled_borders = strtol(value, 0, 0);
6702 stippled_borders = 4;
6706 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
6709 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
6711 CONF("update_interval") {
6713 update_interval = strtod(value, 0);
6717 CONF("total_run_times") {
6719 total_run_times = strtod(value, 0);
6724 stuff_in_upper_case = string_to_bool(value);
6726 CONF("max_specials") {
6728 max_specials = atoi(value);
6732 CONF("max_user_text") {
6734 max_user_text = atoi(value);
6739 if (text != original_text)
6749 int l = strlen(text);
6750 if (fgets(buf, 256, fp) == NULL)
6753 realloc(text, l + strlen(buf)
6757 if (strlen(text) > max_user_text)
6761 text_lines = line + 1;
6764 #ifdef TCP_PORT_MONITOR
6765 CONF("max_port_monitor_connections")
6768 (sscanf(value, "%d", &tcp_port_monitor_args.max_port_monitor_connections) != 1)
6769 || tcp_port_monitor_args.max_port_monitor_connections < 0 )
6771 /* an error. use default, warn and continue. */
6772 tcp_port_monitor_args.max_port_monitor_connections =
6773 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
6776 else if ( tcp_port_monitor_args.max_port_monitor_connections == 0 )
6778 /* no error, just use default */
6779 tcp_port_monitor_args.max_port_monitor_connections =
6780 MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
6782 /* else tcp_port_monitor_args.max_port_monitor_connections > 0 as per config */
6785 #ifdef HAVE_LIBDEXTER
6786 CONF("dexter_client")
6789 dexter_client = string_to_bool (value);
6793 CONF("dexter_server")
6796 dexter_server = string_to_bool (value);
6800 CONF("dexter_config")
6803 dexter_config = strdup (value);
6810 ERR("%s: %d: no such configuration: '%s'", f, line, name);
6822 /* : means that character before that takes an argument */
6823 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
6836 int main(int argc, char **argv)
6838 struct sigaction act, oact;
6841 memset(&info, 0, sizeof(info) );
6843 #ifdef TCP_PORT_MONITOR
6844 tcp_port_monitor_args.max_port_monitor_connections = MAX_PORT_MONITOR_CONNECTIONS_DEFAULT;
6847 /* handle command line parameters that don't change configs */
6852 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
6853 *s) || ((s = getenv("LANG")) && *s)) {
6854 temp = (char *)malloc((strlen(s) + 1) * sizeof(char));
6856 ERR("malloc failed");
6858 for (x = 0; x < strlen(s); x++) {
6859 temp[x] = tolower(s[x]);
6861 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6867 if (!setlocale(LC_CTYPE, "")) {
6868 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6872 int c = getopt(argc,
6883 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
6884 * though free() does the NULL check itself;), then load optarg value */
6886 free(current_config);
6887 current_config = strdup(optarg);
6892 ("Usage: %s [OPTION]...\n"
6893 "Conky is a system monitor that renders text on desktop or to own transparent\n"
6894 "window. Command line options will override configurations defined in config\n"
6897 " -c FILE config file to load instead of "
6900 " -d daemonize, fork to background\n"
6903 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
6904 " -f FONT font to use\n"
6906 " -o create own window to draw\n"
6909 " -b double buffer (prevents flickering)\n"
6911 " -w WIN_ID window id to draw\n"
6912 " -x X x position\n"
6913 " -y Y y position\n"
6915 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
6916 " -u SECS update interval\n"
6917 " -i NUM number of times to update Conky\n", argv[0]);
6921 window.window = strtol(optarg, 0, 0);
6930 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
6934 /* load current_config or CONFIG_FILE */
6937 if (current_config == NULL) {
6938 /* load default config file */
6941 variable_substitute(CONFIG_FILE, buf, 256);
6944 current_config = strdup(buf);
6948 if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
6949 load_config_file(current_config);
6951 set_default_configurations();
6954 /* init specials array */
6955 if ((specials = calloc (sizeof(struct special_t), max_specials)) == 0)
6956 ERR("failed to create specials array");
6959 if (current_mail_spool == NULL) {
6961 variable_substitute(MAIL_FILE, buf, 256);
6964 current_mail_spool = strdup(buf);
6968 /* handle other command line arguments */
6970 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
6971 optind = optreset = 1;
6976 #if defined(__FreeBSD__)
6977 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
6978 O_RDONLY, "kvm_open")) == NULL)
6979 CRIT_ERR( "cannot read kvm");
6983 int c = getopt(argc,
6991 fork_to_background = 1;
6996 set_first_font(optarg);
6999 text_alignment = string_to_alignment(optarg);
7014 if (text != original_text)
7016 text = strdup(optarg);
7017 convert_escapes(text);
7021 update_interval = strtod(optarg, 0);
7025 total_run_times = strtod(optarg, 0);
7029 gap_x = atoi(optarg);
7033 gap_y = atoi(optarg);
7047 /* generate text and get initial size */
7048 extract_variable_text(text);
7049 if (text != original_text) {
7054 if (fork_to_background) {
7058 ERR("Conky: couldn't fork() to background: %s", strerror(errno));
7064 fprintf(stderr,"\n"); fflush(stderr);
7068 /* parent process */
7069 fprintf(stderr,"Conky: forked to background, pid is %d\n", pid); fflush(stderr);
7074 #ifdef HAVE_LIBDEXTER
7075 memset (&packet_arrival_time, 0, sizeof (struct timespec));
7076 dexter_library_init ();
7079 /* wait until some data packets arrive, else conky complains
7080 * bittery that /proc filesystems cannot be opened and, worse,
7081 * will likely segfault when it references unchecked pointers
7082 * in get_text_internal() -- those should be cleaned up.
7085 const int max_tries=30;
7086 gboolean got_packet;
7088 fprintf (stderr, "Conky: waiting for data packets to arrive ...\n");
7089 for (try=0;try<max_tries;try++)
7091 /* pause main thread for 1 sec */
7092 g_get_current_time (&timeval);
7093 g_time_val_add (&timeval, G_USEC_PER_SEC);
7094 g_mutex_lock (packet_mutex);
7095 /* mutex released before sleeping; re-acquired after time elapses */
7096 g_cond_timed_wait (packet_cond, packet_mutex, &timeval);
7097 got_packet = packet_arrival_time.tv_sec>0;
7098 g_mutex_unlock (packet_mutex);
7105 fprintf(stderr, "Conky: packets arriving ...\n");
7109 fprintf(stderr, "Conky: no data packets ...\n");
7110 dexter_library_exit ();
7118 update_text_area(); /* to get initial size of the window */
7122 text_width + border_margin * 2 + 1,
7123 text_height + border_margin * 2 + 1,
7124 set_transparent, background_colour, argv, argc);
7127 update_text_area(); /* to position text/window on screen */
7132 if (own_window && !fixed_pos) {
7133 XMoveWindow(display, window.window, window.x, window.y);
7136 set_transparent_background(window.window);
7147 /* Set signal handlers */
7148 act.sa_handler = signal_handler;
7149 sigemptyset(&act.sa_mask);
7152 act.sa_flags |= SA_RESTART;
7155 if ( sigaction(SIGINT,&act,&oact) < 0 ||
7156 sigaction(SIGUSR1,&act,&oact) < 0 ||
7157 sigaction(SIGTERM,&act,&oact) < 0 )
7159 ERR("error setting signal handler: %s", strerror(errno) );
7163 if (create_audacious_thread() !=0)
7164 CRIT_ERR("unable to create audacious thread!");
7165 timed_thread_register (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
7176 #if defined(__FreeBSD__)
7183 void signal_handler(int sig)
7185 /* signal handler is light as a feather, as it should be.
7186 * we will poll g_signal_pending with each loop of conky
7187 * and do any signal processing there, NOT here. */
7188 g_signal_pending=sig;
7191 #ifdef HAVE_LIBDEXTER
7192 void dexter_library_init (void)
7194 /* start libdexter */
7195 if (dexter_client || dexter_server)
7197 GError *error = NULL;
7198 dexter_init (dexter_config, &error);
7200 CRIT_ERR ("%s", error->message);
7204 if (!(info.dexter.channel = dexter_channel_new (NULL, NULL, NULL, dexter_channel_events)))
7205 CRIT_ERR("unable to create channel to server");
7206 dexter_channel_open (info.dexter.channel, &error);
7208 CRIT_ERR ("%s", error->message);
7209 dexter_channel_greet (info.dexter.channel, &error);
7211 CRIT_ERR ("%s", error->message);
7212 if (dexter_client_init () < 0)
7213 CRIT_ERR ("error initializing dexter client");
7215 packet_mutex = g_mutex_new ();
7216 packet_cond = g_cond_new ();
7221 if (!(info.dexter.server = dexter_server_new (NULL, NULL, NULL)))
7222 CRIT_ERR("unable to create server");
7223 dexter_server_start (info.dexter.server, &error);
7225 CRIT_ERR ("%s", error->message);
7231 void dexter_library_exit (void)
7233 /* shutdown libdexter */
7234 if (dexter_client || dexter_server)
7236 GError *error = NULL;
7240 if (dexter_client_exit () < 0)
7241 ERR ("error de-initializing dexter client");
7242 if (info.dexter.channel)
7244 dexter_channel_part (info.dexter.channel, &error);
7247 ERR("%s", error->message);
7248 g_clear_error (&error);
7250 dexter_channel_close (info.dexter.channel, &error);
7253 ERR("%s", error->message);
7254 g_clear_error (&error);
7256 dexter_channel_free (info.dexter.channel);
7257 info.dexter.channel=NULL;
7262 g_mutex_free (packet_mutex);
7267 g_cond_free (packet_cond);
7274 if (info.dexter.server)
7276 dexter_server_stop (info.dexter.server, &error);
7279 ERR("%s", error->message);
7280 g_clear_error (&error);
7282 dexter_server_free (info.dexter.server);
7283 info.dexter.server=NULL;
7291 void dexter_channel_events (DexterChannel *channel, gint event)
7296 /* if server disconnects we get this event. we also get it normally
7297 * following dexter_channel_part(). */
7298 if (event == DEXTER_CHANNEL_EVENT_NOCONN)
7300 fprintf (stderr, "Conky: channel to server closed\n");
7301 if (dexter_client && (dexter_client_exit () < 0))
7302 ERR ("error de-initializing dexter client");