2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
28 #include <X11/Xutil.h>
29 #include <X11/extensions/Xdamage.h>
34 #include <sys/types.h>
36 #include <netinet/in.h>
46 #define CONFIG_FILE "$HOME/.conkyrc"
47 #define MAIL_FILE "$MAIL"
48 #define MAX_IF_BLOCK_DEPTH 5
50 /* #define SIGNAL_BLOCKING */
51 #undef SIGNAL_BLOCKING
53 static void print_version()
55 printf("Conky %s compiled %s for %s\n",
56 VERSION, BUILD_DATE, BUILD_ARCH);
59 "\nCompiled in features:\n\n"
66 " * Xdamage extension\n"
67 # endif /* HAVE_XDAMAGE */
69 " * Xdbe extension (double buffer)\n"
70 # endif /* HAVE_XDBE */
72 "\n Music detection:\n"
75 #endif /* AUDACIOUS */
85 "\n General features:\n"
89 #ifdef TCP_PORT_MONITOR
91 #endif /* TCP_PORT_MONITOR */
103 static int text_start_x, text_start_y; /* text start position in window */
104 static int text_width, text_height;
119 char name[TEXT_BUFFER_SIZE];
129 static int selected_font = 0;
130 static int font_count = -1;
131 struct font_list *fonts = NULL;
135 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
136 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent))
137 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent)
138 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent)
142 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
143 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
144 #define font_descent() fonts[selected_font].font->max_bounds.descent
148 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
151 static void set_font();
153 int addfont(const char *data_in)
155 if (font_count > MAX_FONTS) {
156 CRIT_ERR("you don't need that many fonts, sorry.");
159 if (font_count == 0) {
163 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
167 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
169 CRIT_ERR("realloc in addfont");
171 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
172 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
174 fonts[font_count].font_alpha = 0xffff;
177 CRIT_ERR("Oops...looks like something overflowed in addfont().");
182 void set_first_font(const char *data_in)
184 if (font_count < 0) {
185 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
190 if (strlen(data_in) > 1) {
191 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
193 fonts[0].font_alpha = 0xffff;
201 for (i=0;i<=font_count;i++) {
204 XftFontClose(display, fonts[i].xftfont);
208 XFreeFont(display, fonts[i].font);
215 set_first_font("6x10");
219 static void load_fonts()
222 for (i=0; i <= font_count; i++) {
226 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
227 XftFontClose(display, fonts[i].xftfont);
229 if ((fonts[i].xftfont =
230 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
233 ERR("can't load Xft font '%s'", fonts[i].name);
234 if ((fonts[i].xftfont =
235 XftFontOpenName(display, screen,
236 "courier-12")) != NULL)
239 ERR("can't load Xft font '%s'", "courier-12");
241 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
242 CRIT_ERR("can't load font '%s'", "fixed");
249 /* load normal font */
250 /* if (fonts[i].font != NULL)
251 XFreeFont(display, fonts[i].font);*/
253 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
254 ERR("can't load font '%s'", fonts[i].name);
255 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
256 CRIT_ERR("can't load font '%s'", "fixed");
257 printf("loaded fixed?\n");
265 /* default config file */
266 static char *current_config;
268 /* set to 1 if you want all text to be in uppercase */
269 static unsigned int stuff_in_upper_case;
271 /* Update interval */
272 static double update_interval;
274 /* Run how many times? */
275 static unsigned long total_run_times;
278 static int fork_to_background;
280 static int cpu_avg_samples, net_avg_samples;
284 /* Always on bottom */
285 static int on_bottom;
287 /* Position on the screen */
288 static int text_alignment;
289 static int gap_x, gap_y;
292 static int draw_borders;
293 static int draw_graph_borders;
294 static int stippled_borders;
296 static int draw_shades, draw_outline;
298 static int border_margin, border_width;
300 static long default_fg_color, default_bg_color, default_out_color;
302 /* create own window or draw stuff to root? */
303 static int set_transparent = 0;
307 static int own_window = 0;
308 static int background_colour = 0;
309 /* fixed size/pos is set if wm/user changes them */
310 static int fixed_size = 0, fixed_pos = 0;
313 static int minimum_width, minimum_height;
314 static int maximum_width;
319 #define CODEPAGE_LENGTH 20
322 char iconv_converting;
323 static iconv_t **iconv_cd = 0;
325 int register_iconv(iconv_t *new_iconv)
328 iconv_cd = realloc(iconv, sizeof(iconv_t *) * (iconv_count + 1));
330 iconv_cd = malloc(sizeof(iconv_t *));
333 CRIT_ERR("Out of memory");
335 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
336 if (!iconv_cd[iconv_count]) {
337 CRIT_ERR("Out of memory");
339 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
344 void free_iconv(void)
348 for (i = iconv_count; i < 0; i++) {
364 /* no buffers in used memory? */
367 /* pad percentages to decimals? */
368 static int pad_percents = 0;
370 #ifdef TCP_PORT_MONITOR
371 tcp_port_monitor_collection_args_t tcp_port_monitor_collection_args;
372 tcp_port_monitor_args_t tcp_port_monitor_args;
375 /* Text that is shown */
376 static char original_text[] =
377 "$nodename - $sysname $kernel on $machine\n"
379 "${color grey}Uptime:$color $uptime\n"
380 "${color grey}Frequency (in MHz):$color $freq\n"
381 "${color grey}Frequency (in GHz):$color $freq_g\n"
382 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
383 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
384 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
385 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
387 "${color grey}File systems:\n"
388 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
389 "${color grey}Networking:\n"
390 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
393 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
394 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
397 "${color grey}XMMS2: $xmms2_status $xmms2_artist - $xmms2_title from $xmms2_album\n"
398 "Progress: $xmms2_bar\n"
400 "${color grey}Name PID CPU% MEM%\n"
401 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
402 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
403 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
404 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
405 "${tail /var/log/Xorg.0.log 3}";
407 static char *text = original_text;
410 static int total_updates;
413 static int blockdepth = 0;
414 static int if_jumped = 0;
415 static int blockstart[MAX_IF_BLOCK_DEPTH];
417 int check_mount(char *s)
419 #if defined(__linux__)
421 FILE *mtab = fopen("/etc/mtab", "r");
423 char buf1[256], buf2[128];
424 while (fgets(buf1, 256, mtab)) {
425 sscanf(buf1, "%*s %128s", buf2);
426 if (!strcmp(s, buf2)) {
433 ERR("Could not open mtab");
436 #elif defined(__FreeBSD__)
437 struct statfs *mntbuf;
440 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
441 for (i = mntsize - 1; i >= 0; i--)
442 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
451 static inline int calc_text_width(const char *s, int l)
457 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
459 XftTextExtents8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
465 return XTextWidth(fonts[selected_font].font, s, l);
470 /* formatted text to render on screen, generated in generate_text(),
471 * drawn in draw_stuff() */
473 static char text_buffer[TEXT_BUFFER_SIZE * 4];
475 /* special stuff in text_buffer */
477 #define SPECIAL_CHAR '\x01'
496 static struct special_t {
506 unsigned long first_colour; // for graph gradient
507 unsigned long last_colour;
510 static int special_count;
512 static int special_index; /* used when drawing */
515 #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? */
517 static struct special_t *new_special(char *buf, int t)
519 if (special_count >= 512)
520 CRIT_ERR("too many special things in text");
522 buf[0] = SPECIAL_CHAR;
524 specials[special_count].type = t;
525 return &specials[special_count++];
528 typedef struct tailstring_list {
529 char data[TEXT_BUFFER_SIZE];
530 struct tailstring_list *next;
531 struct tailstring_list *first;
534 void addtail(tailstring ** head, char *data_in)
537 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
543 tmp->first = (*head)->first;
545 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
550 void freetail(tailstring * head)
553 while (head != NULL) {
560 void freelasttail(tailstring * head)
562 tailstring * tmp = head;
564 if (tmp->next == head->first) {
571 while(head != NULL && tmp != NULL) {
577 static void new_bar(char *buf, int w, int h, int usage)
579 struct special_t *s = new_special(buf, BAR);
580 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
585 static const char *scan_bar(const char *args, int *w, int *h)
587 *w = 0; /* zero width means all space that is available */
589 /* bar's argument is either height or height,width */
592 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
593 sscanf(args, "%d %n", h, &n);
600 static char *scan_font(const char *args)
602 if (args && sizeof(args) < 127) {
609 static void new_font(char *buf, char * args) {
611 struct special_t *s = new_special(buf, FONT);
612 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
613 int tmp = selected_font;
614 selected_font = s->font_added = addfont(args);
619 struct special_t *s = new_special(buf, FONT);
620 int tmp = selected_font;
621 selected_font = s->font_added = 0;
628 inline void graph_append(struct special_t *graph, double f)
630 if (!graph->scaled && f > graph->graph_scale) {
631 f = graph->graph_scale;
635 graph->graph_scale = 1;
637 graph->graph[0] = f; /* add new data */
638 for (i = graph->graph_width - 1; i > 0; i--) { /* shift all the data by 1 */
639 graph->graph[i] = graph->graph[i - 1];
640 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
641 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
646 short colour_depth = 0;
647 void set_up_gradient();
649 /* precalculated: 31/255, and 63/255 */
650 #define CONST_8_TO_5_BITS 0.12156862745098
651 #define CONST_8_TO_6_BITS 0.247058823529412
653 /* adjust color values depending on color depth*/
654 static unsigned int adjust_colors(unsigned int color)
657 if (colour_depth == 0) {
660 if (colour_depth == 16) {
661 r = (color & 0xff0000) >> 16;
662 g = (color & 0xff00) >> 8;
664 color = (int)(r * CONST_8_TO_5_BITS) << 11;
665 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
666 color |= (int)(b * CONST_8_TO_5_BITS);
671 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
673 struct special_t *s = new_special(buf, GRAPH);
675 if (s->graph == NULL) {
676 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
677 s->graph_width = s->width/* - 2*/; // subtract 2 for the box
679 s->graph_width = MAX_GRAPH_DEPTH - 2;
681 s->graph = malloc(s->graph_width * sizeof(double));
682 memset(s->graph, 0, s->graph_width * sizeof(double));
683 s->graph_scale = 100;
686 s->first_colour = adjust_colors(first_colour);
687 s->last_colour = adjust_colors(second_colour);
694 s->graph_width = s->width - 2; // subtract 2 for rectangle around
699 s->graph_scale = scale;
706 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
708 *w = 0; /* zero width means all space that is available */
712 /* graph's argument is either height or height,width */
714 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
715 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
717 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
719 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
722 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
726 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
729 if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
733 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
736 if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
740 if (sscanf(args, "%d,%d", h, w) < 2) {
743 sscanf(args, "%*s %d,%d", h, w);
749 static inline void new_hr(char *buf, int a)
751 new_special(buf, HORIZONTAL_LINE)->height = a;
754 static inline void new_stippled_hr(char *buf, int a, int b)
756 struct special_t *s = new_special(buf, STIPPLED_HR);
761 static inline void new_fg(char *buf, long c)
763 new_special(buf, FG)->arg = c;
766 static inline void new_bg(char *buf, long c)
768 new_special(buf, BG)->arg = c;
771 static inline void new_outline(char *buf, long c)
773 new_special(buf, OUTLINE)->arg = c;
776 static inline void new_offset(char *buf, long c)
778 new_special(buf, OFFSET)->arg = c;
781 static inline void new_voffset(char *buf, long c)
783 new_special(buf, VOFFSET)->arg = c;
786 static inline void new_alignr(char *buf, long c)
788 new_special(buf, ALIGNR)->arg = c;
791 static inline void new_alignc(char *buf, long c)
793 new_special(buf, ALIGNC)->arg = c;
796 static inline void new_goto(char *buf, long c)
798 new_special(buf, GOTO)->arg = c;
801 static inline void new_tab(char *buf, int a, int b) {
802 struct special_t *s = new_special(buf, TAB);
807 /* quite boring functions */
809 static inline void for_each_line(char *b, void (*f) (char *))
813 for (ps = b, pe = b; *pe; pe++) {
826 static void convert_escapes(char *buf)
828 char *p = buf, *s = buf;
844 /* converts from bytes to human readable format (k, M, G, T) */
845 static void human_readable(long long a, char *buf, int size)
847 // Strange conditional due to possible overflows
848 if(a / 1024 / 1024 / 1024.0 > 1024.0){
849 snprintf(buf, size, "%.2fTiB", (a / 1024 / 1024 / 1024) / 1024.0);
851 else if (a >= 1024 * 1024 * 1024) {
852 snprintf(buf, size, "%.2fGiB", (a / 1024 / 1024) / 1024.0);
854 else if (a >= 1024 * 1024) {
855 double m = (a / 1024) / 1024.0;
857 snprintf(buf, size, "%.0fMiB", m);
859 snprintf(buf, size, "%.1fMiB", m);
860 } else if (a >= 1024)
861 snprintf(buf, size, "%LdKiB", a / (long long) 1024);
863 snprintf(buf, size, "%LdB", a);
868 enum text_object_type {
919 #if defined(__linux__)
925 OBJ_i8k_left_fan_status,
926 OBJ_i8k_right_fan_status,
927 OBJ_i8k_left_fan_rpm,
928 OBJ_i8k_right_fan_rpm,
930 OBJ_i8k_buttons_status,
938 #endif /* __linux__ */
965 OBJ_running_processes,
974 OBJ_temp1, /* i2c is used instead in these */
994 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
996 OBJ_apm_battery_time,
997 OBJ_apm_battery_life,
998 #endif /* __FreeBSD__ */
1027 OBJ_xmms2_transport,
1042 OBJ_audacious_status,
1043 OBJ_audacious_title,
1044 OBJ_audacious_length,
1045 OBJ_audacious_length_seconds,
1046 OBJ_audacious_position,
1047 OBJ_audacious_position_seconds,
1048 OBJ_audacious_bitrate,
1049 OBJ_audacious_frequency,
1050 OBJ_audacious_channels,
1051 OBJ_audacious_filename,
1052 OBJ_audacious_playlist_length,
1053 OBJ_audacious_playlist_position,
1064 #ifdef TCP_PORT_MONITOR
1077 struct text_object {
1081 unsigned int c, d, e;
1085 char *s; /* some string */
1086 int i; /* some integer */
1087 long l; /* some other integer */
1088 unsigned int sensor;
1089 struct net_stat *net;
1091 unsigned char loadavg[3];
1092 unsigned int cpu_index;
1093 struct mail_s *mail;
1096 char *tz; /* timezone variable */
1097 char *fmt; /* time display formatting */
1141 struct thread_info_s thread_info;
1147 #ifdef TCP_PORT_MONITOR
1149 in_port_t port_range_begin; /* starting port to monitor */
1150 in_port_t port_range_end; /* ending port to monitor */
1151 int item; /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
1152 int connection_index; /* 0 to n-1 connections. */
1165 struct text_object_list {
1166 unsigned int text_object_count;
1167 struct text_object *text_objects;
1170 static unsigned int text_object_count;
1171 static struct text_object *text_objects;
1172 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
1174 #define MAX_THREADS 512 // sure whatever
1175 typedef struct thread_info_s *thread_info;
1176 static thread_info thread_list[MAX_THREADS];
1177 static int thread_count = 0;
1178 static int threads_runnable = 1;
1180 int register_thread(struct thread_info_s *new_thread)
1182 if (thread_count >= MAX_THREADS) {
1183 CRIT_ERR("Uh oh, tried to register too many threads");
1185 thread_list[thread_count] = new_thread;
1187 // may as well fix the mutex for them as well
1188 pthread_mutex_init(&(new_thread->mutex), NULL);
1190 return thread_count - 1;
1193 #define MAXDATASIZE 1000
1197 struct mail_s* parse_mail_args(char type, const char *arg) {
1198 struct mail_s *mail;
1199 mail = malloc(sizeof(struct mail_s));
1200 memset(mail, 0, sizeof(struct mail_s));
1202 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass) != 3) {
1204 ERR("Scanning IMAP args failed");
1205 } else if (type == IMAP) {
1206 ERR("Scanning POP3 args failed");
1209 // see if password needs prompting
1210 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1211 int fp = fileno(stdin);
1212 struct termios term;
1213 tcgetattr(fp, &term);
1214 term.c_lflag &= ~ECHO;
1215 tcsetattr(fp, TCSANOW, &term);
1216 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1217 scanf("%128s", mail->pass);
1219 term.c_lflag |= ECHO;
1220 tcsetattr(fp, TCSANOW, &term);
1222 // now we check for optional args
1223 tmp = strstr(arg, "-i ");
1226 sscanf(tmp, "%f", &mail->interval);
1228 mail->interval = 300; // 5 minutes
1230 tmp = strstr(arg, "-p ");
1233 sscanf(tmp, "%lu", &mail->port);
1236 mail->port = 110; // default pop3 port
1237 } else if (type == IMAP) {
1238 mail->port = 143; // default imap port
1242 tmp = strstr(arg, "-f ");
1245 sscanf(tmp, "%s", mail->folder);
1247 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1250 tmp = strstr(arg, "-e ");
1254 if (tmp[0] == '\'') {
1255 len = strstr(tmp+1, "'") - tmp - 1;
1260 strncpy(mail->command, tmp+1, len);
1262 mail->command[0] = '\0';
1268 void *imap_thread(struct mail_s* mail)
1269 { // pthreads are really beginning to piss me off
1271 int run_code = threads_runnable;
1272 update_time = get_time();
1273 int sockfd, numbytes;
1274 char recvbuf[MAXDATASIZE];
1275 char sendbuf[MAXDATASIZE];
1278 unsigned int old_unseen = UINT_MAX;
1279 unsigned int old_messages = UINT_MAX;
1281 struct sockaddr_in their_addr; // connector's address information
1282 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1283 herror("gethostbyname");
1286 while (threads_runnable == run_code && fail < 5) {
1288 ERR("Trying IMAP connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1289 sleep((int)mail->interval);
1291 update_time = get_time();
1292 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1298 their_addr.sin_family = AF_INET; // host byte order
1299 their_addr.sin_port = htons(mail->port); // short, network byte order
1300 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1301 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1304 (sockfd, (struct sockaddr *) &their_addr,
1305 sizeof(struct sockaddr)) == -1) {
1310 struct timeval timeout;
1313 timeout.tv_sec = 60; // 60 second timeout i guess
1314 timeout.tv_usec = 0;
1316 FD_SET(sockfd, &fdset);
1317 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1320 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1327 ERR("IMAP connection failed: timeout");
1331 recvbuf[numbytes] = '\0';
1332 if (strstr(recvbuf, "* OK") != recvbuf) {
1333 ERR("IMAP connection failed, probably not an IMAP server");
1337 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1338 strncat(sendbuf, mail->user,
1339 MAXDATASIZE - strlen(sendbuf) - 1);
1340 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1341 strncat(sendbuf, mail->pass,
1342 MAXDATASIZE - strlen(sendbuf) - 1);
1343 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1344 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1349 timeout.tv_sec = 60; // 60 second timeout i guess
1350 timeout.tv_usec = 0;
1352 FD_SET(sockfd, &fdset);
1353 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1356 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1363 recvbuf[numbytes] = '\0';
1364 if (strstr(recvbuf, "a1 OK") == NULL) {
1365 ERR("IMAP server login failed: %s", recvbuf);
1369 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1370 strncat(sendbuf, mail->folder,
1371 MAXDATASIZE - strlen(sendbuf) - 1);
1372 strncat(sendbuf, " (MESSAGES UNSEEN)\n",
1373 MAXDATASIZE - strlen(sendbuf) - 1);
1374 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
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,
1393 recvbuf[numbytes] = '\0';
1394 if (strstr(recvbuf, "a2 OK") == NULL) {
1395 ERR("IMAP status failed: %s", recvbuf);
1399 // now we get the data
1400 reply = strstr(recvbuf, " (MESSAGES ");
1402 *strchr(reply, ')') = '\0';
1403 if (reply == NULL) {
1404 ERR("Error parsing IMAP response: %s", recvbuf);
1408 pthread_mutex_lock(&(mail->thread_info.mutex));
1409 sscanf(reply, "MESSAGES %lu UNSEEN %lu",
1412 pthread_mutex_unlock(&(mail->thread_info.mutex));
1414 strncpy(sendbuf, "a3 logout\n", MAXDATASIZE);
1415 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1420 timeout.tv_sec = 60; // 60 second timeout i guess
1421 timeout.tv_usec = 0;
1423 FD_SET(sockfd, &fdset);
1424 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1427 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1434 recvbuf[numbytes] = '\0';
1435 if (strstr(recvbuf, "a3 OK") == NULL) {
1436 ERR("IMAP logout failed: %s", recvbuf);
1441 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen || (mail->messages > old_messages && mail->unseen > 0))) { // new mail goodie
1442 if (system(mail->command) == -1) {
1447 old_unseen = mail->unseen;
1448 old_messages = mail->messages;
1449 mail->last_update = update_time;
1450 usleep(100); // prevent race condition
1451 if (get_time() - mail->last_update >
1455 unsigned int delay =
1456 1000000.0 * (mail->interval -
1458 mail->last_update));
1459 if (delay < update_interval * 500000) {
1460 delay = update_interval * 1000000;
1465 ERR("exiting imap thread");
1470 void *pop3_thread(struct mail_s *mail)
1471 { // pthreads are really beginning to piss me off
1473 int run_code = threads_runnable;
1474 update_time = get_time();
1475 int sockfd, numbytes;
1476 char recvbuf[MAXDATASIZE];
1477 char sendbuf[MAXDATASIZE];
1480 unsigned int old_unseen = UINT_MAX;
1482 struct sockaddr_in their_addr; // connector's address information
1483 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1484 herror("gethostbyname");
1487 while (threads_runnable == run_code && fail < 5) {
1489 ERR("Trying POP3 connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1490 sleep((int)mail->interval);
1492 update_time = get_time();
1493 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1499 their_addr.sin_family = AF_INET; // host byte order
1500 their_addr.sin_port = htons(mail->port); // short, network byte order
1501 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1502 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1505 (sockfd, (struct sockaddr *) &their_addr,
1506 sizeof(struct sockaddr)) == -1) {
1511 struct timeval timeout;
1514 timeout.tv_sec = 60; // 60 second timeout i guess
1515 timeout.tv_usec = 0;
1517 FD_SET(sockfd, &fdset);
1518 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1521 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1528 ERR("POP3 connection failed: timeout\n");
1532 recvbuf[numbytes] = '\0';
1533 if (strstr(recvbuf, "+OK ") != recvbuf) {
1534 ERR("POP3 connection failed, probably not a POP3 server");
1538 strncpy(sendbuf, "USER ", MAXDATASIZE);
1539 strncat(sendbuf, mail->user,
1540 MAXDATASIZE - strlen(sendbuf) - 1);
1541 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1542 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1543 perror("send USER");
1547 timeout.tv_sec = 60; // 60 second timeout i guess
1548 timeout.tv_usec = 0;
1550 FD_SET(sockfd, &fdset);
1551 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1554 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1556 perror("recv USER");
1561 recvbuf[numbytes] = '\0';
1562 if (strstr(recvbuf, "+OK ") == NULL) {
1563 ERR("POP3 server login failed: %s", recvbuf);
1567 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1568 strncat(sendbuf, mail->pass,
1569 MAXDATASIZE - strlen(sendbuf) - 1);
1570 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1571 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1572 perror("send PASS");
1576 timeout.tv_sec = 60; // 60 second timeout i guess
1577 timeout.tv_usec = 0;
1579 FD_SET(sockfd, &fdset);
1580 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1583 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1585 perror("recv PASS");
1590 recvbuf[numbytes] = '\0';
1591 if (strstr(recvbuf, "+OK ") == NULL) {
1592 ERR("POP3 server login failed: %s", recvbuf);
1596 strncpy(sendbuf, "STAT\n", MAXDATASIZE);
1597 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1598 perror("send STAT");
1602 timeout.tv_sec = 60; // 60 second timeout i guess
1603 timeout.tv_usec = 0;
1605 FD_SET(sockfd, &fdset);
1606 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1609 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1611 perror("recv STAT");
1616 recvbuf[numbytes] = '\0';
1617 if (strstr(recvbuf, "+OK ") == NULL) {
1618 ERR("POP3 status failed: %s", recvbuf);
1622 // now we get the data
1623 reply = recvbuf + 4;
1624 if (reply == NULL) {
1625 ERR("Error parsing POP3 response: %s", recvbuf);
1629 pthread_mutex_lock(&(mail->thread_info.mutex));
1630 sscanf(reply, "%lu %lu", &mail->unseen,
1633 pthread_mutex_unlock(&(mail->thread_info.mutex));
1635 strncpy(sendbuf, "QUIT\n", MAXDATASIZE);
1636 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1637 perror("send QUIT");
1641 timeout.tv_sec = 60; // 60 second timeout i guess
1642 timeout.tv_usec = 0;
1644 FD_SET(sockfd, &fdset);
1645 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1648 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1650 perror("recv QUIT");
1655 recvbuf[numbytes] = '\0';
1656 if (strstr(recvbuf, "+OK") == NULL) {
1657 ERR("POP3 logout failed: %s", recvbuf);
1662 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) { // new mail goodie
1663 if (system(mail->command) == -1) {
1668 old_unseen = mail->unseen;
1669 mail->last_update = update_time;
1670 usleep(100); // prevent race condition
1671 if (get_time() - mail->last_update >
1675 unsigned int delay =
1676 1000000.0 * (mail->interval -
1678 mail->last_update));
1679 if (delay < update_interval * 500000) {
1680 delay = update_interval * 1000000;
1685 ERR("exiting pop3 thread");
1691 void *threaded_exec(struct text_object *obj) { // pthreads are really beginning to piss me off
1693 int run_code = threads_runnable;
1694 while (threads_runnable == run_code) {
1695 update_time = get_time();
1696 char *p2 = obj->data.execi.buffer;
1697 FILE *fp = popen(obj->data.execi.cmd,"r");
1698 pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
1699 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
1702 if (n2 && p2[n2 - 1] == '\n') {
1706 if (*p2 == '\001') {
1711 pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
1712 obj->data.execi.last_update = update_time;
1713 usleep(100); // prevent race condition
1714 if (get_time() - obj->data.execi.last_update > obj->data.execi.interval) {
1717 unsigned int delay = 1000000.0 * (obj->data.execi.interval -(get_time() - obj->data.execi.last_update));
1718 if (delay < update_interval * 500000) {
1719 delay = update_interval * 1000000;
1724 ERR("exiting thread");
1729 static struct text_object *new_text_object_internal()
1731 struct text_object *obj = malloc(sizeof(struct text_object));
1732 memset(obj, 0, sizeof(struct text_object));
1736 static void free_text_objects(unsigned int count, struct text_object *objs)
1739 for (i = 0; i < count; i++) {
1740 switch (objs[i].type) {
1742 close(objs[i].data.i);
1745 close(objs[i].data.i);
1748 close(objs[i].data.i2c.fd);
1751 free(objs[i].data.s);
1754 free(objs[i].data.s);
1757 free(objs[i].data.tztime.tz);
1758 free(objs[i].data.tztime.fmt);
1763 case OBJ_imap_unseen:
1764 if (!objs[i].global_mode) {
1765 free(objs[i].data.mail);
1768 case OBJ_imap_messages:
1769 if (!objs[i].global_mode) {
1770 free(objs[i].data.mail);
1776 case OBJ_pop3_unseen:
1777 if (!objs[i].global_mode) {
1778 free(objs[i].data.mail);
1782 if (!objs[i].global_mode) {
1783 free(objs[i].data.mail);
1786 case OBJ_if_existing:
1787 case OBJ_if_mounted:
1788 case OBJ_if_running:
1789 free(objs[i].data.ifblock.s);
1792 free(objs[i].data.tail.logfile);
1793 free(objs[i].data.tail.buffer);
1795 case OBJ_text: case OBJ_font:
1796 free(objs[i].data.s);
1799 free(objs[i].data.s);
1802 free(objs[i].data.s);
1805 free(objs[i].data.s);
1808 free(objs[i].data.s);
1810 /* case OBJ_execibar:
1811 free(objs[i].data.s);
1813 case OBJ_execigraph:
1814 free(objs[i].data.s);
1817 case OBJ_iconv_start:
1823 if (info.mpd.title) {
1824 free(info.mpd.title);
1828 case OBJ_mpd_artist:
1829 if (info.mpd.artist) {
1830 free(info.mpd.artist);
1831 info.mpd.artist = 0;
1835 if (info.mpd.album) {
1836 free(info.mpd.album);
1840 case OBJ_mpd_random:
1841 if (info.mpd.random) {
1842 free(info.mpd.random);
1843 info.mpd.random = 0;
1846 case OBJ_mpd_repeat:
1847 if (info.mpd.repeat) {
1848 free(info.mpd.repeat);
1849 info.mpd.repeat = 0;
1853 if (info.mpd.track) {
1854 free(info.mpd.track);
1859 if (info.mpd.name) {
1860 free(info.mpd.name);
1865 if (info.mpd.file) {
1866 free(info.mpd.file);
1870 case OBJ_mpd_status:
1871 if (info.mpd.status) {
1872 free(info.mpd.status);
1873 info.mpd.status = 0;
1877 if (info.mpd.artist) {
1878 free(info.mpd.artist);
1879 info.mpd.artist = 0;
1881 if (info.mpd.title) {
1882 free(info.mpd.title);
1885 if (info.mpd.file) {
1886 free(info.mpd.file);
1893 case OBJ_xmms2_artist:
1894 if (info.xmms2.artist) {
1895 free(info.xmms2.artist);
1896 info.xmms2.artist = 0;
1899 case OBJ_xmms2_album:
1900 if (info.xmms2.album) {
1901 free(info.xmms2.album);
1902 info.xmms2.album = 0;
1905 case OBJ_xmms2_title:
1906 if (info.xmms2.title) {
1907 free(info.xmms2.title);
1908 info.xmms2.title = 0;
1911 case OBJ_xmms2_genre:
1912 if (info.xmms2.genre) {
1913 free(info.xmms2.genre);
1914 info.xmms2.genre = 0;
1917 case OBJ_xmms2_comment:
1918 if (info.xmms2.comment) {
1919 free(info.xmms2.comment);
1920 info.xmms2.comment = 0;
1923 case OBJ_xmms2_decoder:
1924 if (info.xmms2.decoder) {
1925 free(info.xmms2.decoder);
1929 case OBJ_xmms2_transport:
1930 if (info.xmms2.transport) {
1931 free(info.xmms2.transport);
1936 if (info.xmms2.url) {
1937 free(info.xmms2.url);
1941 case OBJ_xmms2_date:
1942 if (info.xmms2.date) {
1943 free(info.xmms2.date);
1944 info.xmms2.date = 0;
1947 case OBJ_xmms2_status:
1948 if (info.xmms2.status) {
1949 free(info.xmms2.status);
1950 info.xmms2.status = 0;
1953 case OBJ_xmms2_smart:
1954 if (info.xmms2.artist) {
1955 free(info.xmms2.artist);
1956 info.xmms2.artist = 0;
1958 if (info.xmms2.title) {
1959 free(info.xmms2.title);
1960 info.xmms2.title = 0;
1962 if (info.xmms2.url) {
1963 free(info.xmms2.url);
1969 case OBJ_bmpx_title:
1970 case OBJ_bmpx_artist:
1971 case OBJ_bmpx_album:
1972 case OBJ_bmpx_track:
1974 case OBJ_bmpx_bitrate:
1978 free(objs[i].data.s);
1982 free(objs[i].data.execi.cmd);
1983 free(objs[i].data.execi.buffer);
1986 free(objs[i].data.execi.cmd);
1987 free(objs[i].data.execi.buffer);
1990 if (info.first_process) {
1991 free_all_processes();
1992 info.first_process = NULL;
1996 if (info.first_process) {
1997 free_all_processes();
1998 info.first_process = NULL;
2003 free(objs[i].data.hddtemp.dev);
2004 free(objs[i].data.hddtemp.addr);
2010 //text_objects = NULL;
2011 //text_object_count = 0;
2014 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2019 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2020 *a = mixer_init(buf1);
2021 (void) scan_bar(arg + n, w, h);
2024 (void) scan_bar(arg, w, h);
2029 /* construct_text_object() creates a new text_object */
2030 static struct text_object *construct_text_object(const char *s, const char *arg, unsigned int object_count, struct text_object *text_objects, long line)
2032 //struct text_object *obj = new_text_object();
2033 struct text_object *obj = new_text_object_internal();
2036 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
2037 #define END ; } } else
2041 obj->type = OBJ_color;
2042 obj->data.l = get_x11_color(s);
2045 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
2046 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
2047 END OBJ(acpiacadapter, 0)
2048 #if defined(__linux__)
2055 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2057 obj->data.cpu_index=1;
2058 // ERR("freq: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2062 obj->data.cpu_index=atoi(&arg[0]);
2071 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2073 obj->data.cpu_index=1;
2074 // ERR("freq_g: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2078 obj->data.cpu_index=atoi(&arg[0]);
2081 END OBJ(voltage_mv, 0)
2087 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2089 obj->data.cpu_index=1;
2090 // ERR("voltage_mv: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2094 obj->data.cpu_index=atoi(&arg[0]);
2097 END OBJ(voltage_v, 0)
2103 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2105 obj->data.cpu_index=1;
2106 // ERR("voltage_v: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2110 obj->data.cpu_index=atoi(&arg[0]);
2116 #endif /* __linux__ */
2117 END OBJ(freq_dyn, 0);
2118 END OBJ(freq_dyn_g, 0);
2119 END OBJ(acpifan, 0);
2120 END OBJ(battery, 0);
2123 sscanf(arg, "%63s", bat);
2125 strcpy(bat, "BAT0");
2126 obj->data.s = strdup(bat);
2127 #if defined(__linux__)
2128 END OBJ(i8k_version, INFO_I8K)
2129 END OBJ(i8k_bios, INFO_I8K)
2130 END OBJ(i8k_serial, INFO_I8K)
2131 END OBJ(i8k_cpu_temp, INFO_I8K)
2132 END OBJ(i8k_cpu_tempf, INFO_I8K)
2133 END OBJ(i8k_left_fan_status, INFO_I8K)
2134 END OBJ(i8k_right_fan_status, INFO_I8K)
2135 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2136 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2137 END OBJ(i8k_ac_status, INFO_I8K)
2138 END OBJ(i8k_buttons_status, INFO_I8K)
2140 END OBJ(ibm_temps, 0)
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(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2203 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2207 obj->data.s = scan_font(arg);
2209 OBJ(downspeed, INFO_NET)
2211 obj->data.net = get_net_stat(arg);
2214 CRIT_ERR("downspeed needs argument");
2216 END OBJ(downspeedf, INFO_NET)
2218 obj->data.net = get_net_stat(arg);
2221 CRIT_ERR("downspeedf needs argument");
2223 END OBJ(downspeedgraph, INFO_NET)
2224 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2226 sscanf(arg, "%63s %*i,%*i %*i", buf);
2227 obj->data.net = get_net_stat(buf);
2228 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2229 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
2236 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos = object_count;
2237 blockstart[blockdepth - 1] = object_count;
2238 obj->data.ifblock.pos = object_count + 2;
2240 ERR("$else: no matching $if_*");
2245 text_objects[blockstart[blockdepth]].data.ifblock.pos = object_count;
2247 ERR("$endif: no matching $if_*");
2250 OBJ(image, 0) obj->data.s = strdup(arg ? arg : "");
2253 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
2254 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
2255 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
2256 END OBJ(execibar, 0) unsigned int n;
2257 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2259 ERR("${execibar <interval> command}");
2260 obj->type = OBJ_text;
2261 snprintf(buf, 256, "${%s}", s);
2262 obj->data.s = strdup(buf);
2264 obj->data.execi.cmd = strdup(arg + n);
2266 END OBJ(execigraph, 0) unsigned int n;
2267 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2269 ERR("${execigraph <interval> command}");
2270 obj->type = OBJ_text;
2271 snprintf(buf, 256, "${%s}", s);
2272 obj->data.s = strdup(buf);
2274 obj->data.execi.cmd = strdup(arg + n);
2276 END OBJ(execi, 0) unsigned int n;
2279 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2281 ERR("${execi <interval> command}");
2282 obj->type = OBJ_text;
2283 snprintf(buf, 256, "${%s}", s);
2284 obj->data.s = strdup(buf);
2286 obj->data.execi.cmd = strdup(arg + n);
2287 obj->data.execi.buffer =
2288 (char *) calloc(1, TEXT_BUFFER_SIZE);
2290 END OBJ(texeci, 0) unsigned int n;
2292 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2294 ERR("${texeci <interval> command}");
2295 obj->type = OBJ_text;
2296 snprintf(buf, 256, "${%s}", s);
2297 obj->data.s = strdup(buf);
2299 obj->data.execi.cmd = strdup(arg + n);
2300 obj->data.execi.buffer =
2301 (char *) calloc(1, TEXT_BUFFER_SIZE);
2303 obj->data.execi.pos = -1;
2304 END OBJ(pre_exec, 0) obj->type = OBJ_text;
2306 FILE *fp = popen(arg, "r");
2310 n = fread(buf, 1, 2048, fp);
2313 if (n && buf[n - 1] == '\n')
2318 obj->data.s = strdup(buf);
2320 obj->data.s = strdup("");
2323 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
2324 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2326 while (isspace(*arg))
2332 obj->data.fsbar.fs = prepare_fs_stat(arg);
2333 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
2336 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
2340 obj->data.fsbar.fs = prepare_fs_stat(arg);
2341 END OBJ(fs_free, INFO_FS) if (!arg)
2343 obj->data.fs = prepare_fs_stat(arg);
2344 END OBJ(fs_used_perc, INFO_FS) if (!arg)
2346 obj->data.fs = prepare_fs_stat(arg);
2347 END OBJ(fs_free_perc, INFO_FS) if (!arg)
2349 obj->data.fs = prepare_fs_stat(arg);
2350 END OBJ(fs_size, INFO_FS) if (!arg)
2352 obj->data.fs = prepare_fs_stat(arg);
2353 END OBJ(fs_used, INFO_FS) if (!arg)
2355 obj->data.fs = prepare_fs_stat(arg);
2356 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
2357 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
2358 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
2362 ERR("goto needs arguments");
2363 obj->type = OBJ_text;
2364 obj->data.s = strdup("${goto}");
2368 obj->data.i = atoi(arg);
2373 if (sscanf(arg, "%d %d", &a, &b) != 2)
2374 sscanf(arg, "%d", &b);
2378 obj->data.pair.a = a;
2379 obj->data.pair.b = b;
2381 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
2385 ERR("i2c needs arguments");
2386 obj->type = OBJ_text;
2387 //obj->data.s = strdup("${i2c}");
2391 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2392 /* if scanf couldn't read three values, read type and num and use
2394 sscanf(arg, "%63s %d", buf2, &n);
2396 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
2397 obj->data.i2c.devtype);
2398 strncpy(obj->data.i2c.type, buf2, 63);
2401 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
2402 obj->data.i2c.devtype);
2403 strncpy(obj->data.i2c.type, buf2, 63);
2406 END OBJ(top, INFO_TOP)
2410 ERR("top needs arguments");
2411 obj->type = OBJ_text;
2412 //obj->data.s = strdup("${top}");
2415 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2416 if (strcmp(buf, "name") == 0) {
2417 obj->data.top.type = TOP_NAME;
2418 } else if (strcmp(buf, "cpu") == 0) {
2419 obj->data.top.type = TOP_CPU;
2420 } else if (strcmp(buf, "pid") == 0) {
2421 obj->data.top.type = TOP_PID;
2422 } else if (strcmp(buf, "mem") == 0) {
2423 obj->data.top.type = TOP_MEM;
2425 ERR("invalid arg for top");
2428 if (n < 1 || n > 10) {
2429 CRIT_ERR("invalid arg for top");
2432 obj->data.top.num = n - 1;
2436 ERR("invalid args given for top");
2439 END OBJ(top_mem, INFO_TOP)
2443 ERR("top_mem needs arguments");
2444 obj->type = OBJ_text;
2445 obj->data.s = strdup("${top_mem}");
2448 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2449 if (strcmp(buf, "name") == 0) {
2450 obj->data.top.type = TOP_NAME;
2451 } else if (strcmp(buf, "cpu") == 0) {
2452 obj->data.top.type = TOP_CPU;
2453 } else if (strcmp(buf, "pid") == 0) {
2454 obj->data.top.type = TOP_PID;
2455 } else if (strcmp(buf, "mem") == 0) {
2456 obj->data.top.type = TOP_MEM;
2458 ERR("invalid arg for top");
2461 if (n < 1 || n > 10) {
2462 CRIT_ERR("invalid arg for top");
2465 obj->data.top.num = n - 1;
2469 ERR("invalid args given for top");
2472 END OBJ(addr, INFO_NET)
2474 obj->data.net = get_net_stat(arg);
2477 CRIT_ERR("addr needs argument");
2479 END OBJ(linkstatus, INFO_WIFI)
2481 obj->data.net = get_net_stat(arg);
2484 CRIT_ERR("linkstatus needs argument");
2490 ERR("tail needs arguments");
2491 obj->type = OBJ_text;
2492 obj->data.s = strdup("${tail}");
2495 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2496 if (n1 < 1 || n1 > 30) {
2497 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
2501 fp = fopen(buf, "r");
2503 obj->data.tail.logfile =
2504 malloc(TEXT_BUFFER_SIZE);
2505 strcpy(obj->data.tail.logfile, buf);
2506 obj->data.tail.wantedlines = n1 - 1;
2507 obj->data.tail.interval =
2508 update_interval * 2;
2512 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2515 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2516 if (n1 < 1 || n1 > 30) {
2518 ("invalid arg for tail, number of lines must be between 1 and 30");
2520 } else if (n2 < 1 || n2 < update_interval) {
2522 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
2526 fp = fopen(buf, "r");
2528 obj->data.tail.logfile =
2529 malloc(TEXT_BUFFER_SIZE);
2530 strcpy(obj->data.tail.logfile, buf);
2531 obj->data.tail.wantedlines = n1 - 1;
2532 obj->data.tail.interval = n2;
2536 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2542 ERR("invalid args given for tail");
2545 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2550 ERR("head needs arguments");
2551 obj->type = OBJ_text;
2552 obj->data.s = strdup("${head}");
2555 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2556 if (n1 < 1 || n1 > 30) {
2557 CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
2561 fp = fopen(buf, "r");
2563 obj->data.tail.logfile =
2564 malloc(TEXT_BUFFER_SIZE);
2565 strcpy(obj->data.tail.logfile, buf);
2566 obj->data.tail.wantedlines = n1 - 1;
2567 obj->data.tail.interval =
2568 update_interval * 2;
2572 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2575 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2576 if (n1 < 1 || n1 > 30) {
2578 ("invalid arg for head, number of lines must be between 1 and 30");
2580 } else if (n2 < 1 || n2 < update_interval) {
2582 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
2586 fp = fopen(buf, "r");
2588 obj->data.tail.logfile =
2589 malloc(TEXT_BUFFER_SIZE);
2590 strcpy(obj->data.tail.logfile, buf);
2591 obj->data.tail.wantedlines = n1 - 1;
2592 obj->data.tail.interval = n2;
2596 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2602 ERR("invalid args given for head");
2605 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2606 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
2608 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2609 if (r >= 3 && (c < 1 || c > 3))
2611 if (r >= 2 && (b < 1 || b > 3))
2613 if (r >= 1 && (a < 1 || a > 3))
2616 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2617 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2618 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2619 END OBJ(if_existing, 0)
2620 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2621 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2624 ERR("if_existing needs an argument");
2625 obj->data.ifblock.s = 0;
2627 obj->data.ifblock.s = strdup(arg);
2628 blockstart[blockdepth] = object_count;
2629 obj->data.ifblock.pos = object_count + 2;
2631 END OBJ(if_mounted, 0)
2632 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2633 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2636 ERR("if_mounted needs an argument");
2637 obj->data.ifblock.s = 0;
2639 obj->data.ifblock.s = strdup(arg);
2640 blockstart[blockdepth] = object_count;
2641 obj->data.ifblock.pos = object_count + 2;
2643 END OBJ(if_running, 0)
2644 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2645 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2649 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2650 obj->data.ifblock.s = strdup(buf);
2652 ERR("if_running needs an argument");
2653 obj->data.ifblock.s = 0;
2655 blockstart[blockdepth] = object_count;
2656 obj->data.ifblock.pos = object_count + 2;
2660 END OBJ(mails, INFO_MAIL)
2661 END OBJ(mem, INFO_MEM)
2662 END OBJ(memmax, INFO_MEM)
2663 END OBJ(memperc, INFO_MEM)
2664 END OBJ(membar, INFO_MEM)
2665 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2666 END OBJ(memgraph, INFO_MEM)
2667 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2668 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
2669 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
2670 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
2671 END OBJ(mixerbar, INFO_MIXER)
2672 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2673 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2674 END OBJ(mixerlbar, INFO_MIXER)
2675 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2676 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2677 END OBJ(mixerrbar, INFO_MIXER)
2678 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2679 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2681 OBJ(new_mails, INFO_MAIL)
2682 END OBJ(nodename, 0)
2683 END OBJ(processes, INFO_PROCS)
2684 END OBJ(running_processes, INFO_RUN_PROCS)
2685 END OBJ(shadecolor, 0)
2687 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2689 END OBJ(outlinecolor, 0)
2691 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2693 END OBJ(stippled_hr, 0)
2695 int a = stippled_borders, b = 1;
2697 if (sscanf(arg, "%d %d", &a, &b) != 2)
2698 sscanf(arg, "%d", &b);
2702 obj->data.pair.a = a;
2703 obj->data.pair.b = b;
2705 END OBJ(swap, INFO_MEM)
2706 END OBJ(swapmax, INFO_MEM)
2707 END OBJ(swapperc, INFO_MEM)
2708 END OBJ(swapbar, INFO_MEM)
2709 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2710 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
2712 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
2713 obj->data.i2c.devtype);
2714 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
2716 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
2717 obj->data.i2c.devtype);
2718 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2719 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2721 char buf1[256], buf2[256], *fmt, *tz;
2724 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2733 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
2734 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
2736 END OBJ(iconv_start, 0)
2737 if (iconv_converting) {
2738 CRIT_ERR("You must stop your last iconv conversion before starting another");
2741 char iconv_from[CODEPAGE_LENGTH];
2742 char iconv_to[CODEPAGE_LENGTH];
2743 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2744 CRIT_ERR("Invalid arguments for iconv_start");
2747 new_iconv = iconv_open(iconv_to, iconv_from);
2748 if (new_iconv == (iconv_t)(-1)) {
2749 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2751 obj->a = register_iconv(&new_iconv);
2752 iconv_converting = 1;
2756 CRIT_ERR("Iconv requires arguments");
2758 END OBJ(iconv_stop, 0)
2759 iconv_converting = 0;
2762 END OBJ(totaldown, INFO_NET)
2764 obj->data.net = get_net_stat(arg);
2767 CRIT_ERR("totaldown needs argument");
2769 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
2771 obj->data.net = get_net_stat(arg);
2774 CRIT_ERR("totalup needs argument");
2777 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
2778 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
2779 END OBJ(upspeed, INFO_NET)
2781 obj->data.net = get_net_stat(arg);
2784 CRIT_ERR("upspeed needs argument");
2786 END OBJ(upspeedf, INFO_NET)
2788 obj->data.net = get_net_stat(arg);
2791 CRIT_ERR("upspeedf needs argument");
2794 END OBJ(upspeedgraph, INFO_NET)
2795 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2797 sscanf(arg, "%63s %*i,%*i %*i", buf);
2798 obj->data.net = get_net_stat(buf);
2799 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2800 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
2805 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
2806 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
2807 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2808 OBJ(apm_adapter, 0) END
2809 OBJ(apm_battery_life, 0) END
2810 OBJ(apm_battery_time, 0) END
2811 #endif /* __FreeBSD__ */
2815 obj->data.mail = parse_mail_args(IMAP, arg);
2816 obj->global_mode = 0;
2818 obj->global_mode = 1;
2821 OBJ(imap_messages, 0)
2824 obj->data.mail = parse_mail_args(IMAP, arg);
2825 obj->global_mode = 0;
2827 obj->global_mode = 1;
2833 obj->data.mail = parse_mail_args(POP3, arg);
2834 obj->global_mode = 0;
2836 obj->global_mode = 1;
2842 obj->data.mail = parse_mail_args(POP3, arg);
2843 obj->global_mode = 0;
2845 obj->global_mode = 1;
2849 OBJ(mpd_artist, INFO_MPD)
2850 END OBJ(mpd_title, INFO_MPD)
2851 END OBJ(mpd_random, INFO_MPD)
2852 END OBJ(mpd_repeat, INFO_MPD)
2853 END OBJ(mpd_elapsed, INFO_MPD)
2854 END OBJ(mpd_length, INFO_MPD)
2855 END OBJ(mpd_track, INFO_MPD)
2856 END OBJ(mpd_name, INFO_MPD)
2857 END OBJ(mpd_file, INFO_MPD)
2858 END OBJ(mpd_percent, INFO_MPD)
2859 END OBJ(mpd_album, INFO_MPD)
2860 END OBJ(mpd_vol, INFO_MPD)
2861 END OBJ(mpd_bitrate, INFO_MPD)
2862 END OBJ(mpd_status, INFO_MPD)
2863 END OBJ(mpd_bar, INFO_MPD)
2864 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2865 END OBJ(mpd_smart, INFO_MPD) END
2868 OBJ(xmms2_artist, INFO_XMMS2)
2869 END OBJ(xmms2_album, INFO_XMMS2)
2870 END OBJ(xmms2_title, INFO_XMMS2)
2871 END OBJ(xmms2_genre, INFO_XMMS2)
2872 END OBJ(xmms2_comment, INFO_XMMS2)
2873 END OBJ(xmms2_decoder, INFO_XMMS2)
2874 END OBJ(xmms2_transport, INFO_XMMS2)
2875 END OBJ(xmms2_url, INFO_XMMS2)
2876 END OBJ(xmms2_tracknr, INFO_XMMS2)
2877 END OBJ(xmms2_bitrate, INFO_XMMS2)
2878 END OBJ(xmms2_date, INFO_XMMS2)
2879 END OBJ(xmms2_id, INFO_XMMS2)
2880 END OBJ(xmms2_duration, INFO_XMMS2)
2881 END OBJ(xmms2_elapsed, INFO_XMMS2)
2882 END OBJ(xmms2_size, INFO_XMMS2)
2883 END OBJ(xmms2_status, INFO_XMMS2)
2884 END OBJ(xmms2_percent, INFO_XMMS2)
2885 END OBJ(xmms2_bar, INFO_XMMS2)
2886 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2887 END OBJ(xmms2_smart, INFO_XMMS2)
2891 OBJ(audacious_status, INFO_AUDACIOUS) END
2892 OBJ(audacious_title, INFO_AUDACIOUS) END
2893 OBJ(audacious_length, INFO_AUDACIOUS) END
2894 OBJ(audacious_length_seconds, INFO_AUDACIOUS) END
2895 OBJ(audacious_position, INFO_AUDACIOUS) END
2896 OBJ(audacious_position_seconds, INFO_AUDACIOUS) END
2897 OBJ(audacious_bitrate, INFO_AUDACIOUS) END
2898 OBJ(audacious_frequency, INFO_AUDACIOUS) END
2899 OBJ(audacious_channels, INFO_AUDACIOUS) END
2900 OBJ(audacious_filename, INFO_AUDACIOUS) END
2901 OBJ(audacious_playlist_length, INFO_AUDACIOUS) END
2902 OBJ(audacious_playlist_position, INFO_AUDACIOUS) END
2903 OBJ(audacious_bar, INFO_AUDACIOUS)
2904 (void) scan_bar(arg, &obj->a, &obj->b);
2908 OBJ(bmpx_title, INFO_BMPX)
2909 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2911 OBJ(bmpx_artist, INFO_BMPX)
2912 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2914 OBJ(bmpx_album, INFO_BMPX)
2915 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2917 OBJ(bmpx_track, INFO_BMPX)
2918 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2920 OBJ(bmpx_uri, INFO_BMPX)
2921 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2923 OBJ(bmpx_bitrate, INFO_BMPX)
2924 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2929 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
2930 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2931 ERR("hddtemp needs arguments");
2932 obj->type = OBJ_text;
2933 obj->data.s = strdup("${hddtemp}");
2938 #ifdef TCP_PORT_MONITOR
2939 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2940 int argc, port_begin, port_end, item, connection_index;
2942 memset(itembuf,0,sizeof(itembuf));
2944 /* massive argument checking */
2946 CRIT_ERR("tcp_portmon: needs arguments");
2948 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
2949 if ( (argc != 3) && (argc != 4) )
2951 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
2953 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
2955 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
2957 if ( port_begin > port_end )
2959 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
2961 if ( strncmp(itembuf,"count",31) == 0 )
2963 else if ( strncmp(itembuf,"rip",31) == 0 )
2965 else if ( strncmp(itembuf,"rhost",31) == 0 )
2967 else if ( strncmp(itembuf,"rport",31) == 0 )
2969 else if ( strncmp(itembuf,"rservice",31) == 0 )
2971 else if ( strncmp(itembuf,"lip",31) == 0 )
2973 else if ( strncmp(itembuf,"lhost",31) == 0 )
2975 else if ( strncmp(itembuf,"lport",31) == 0 )
2977 else if ( strncmp(itembuf,"lservice",31) == 0 )
2981 CRIT_ERR("tcp_portmon: invalid item specified");
2983 if ( (argc==3) && (item!=COUNT) )
2985 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
2987 if ( (argc==4) && (connection_index<0) )
2989 CRIT_ERR("tcp_portmon: connection index must be non-negative");
2991 /* ok, args looks good. save the text object data */
2992 obj->data.tcp_port_monitor.port_range_begin = (in_port_t)port_begin;
2993 obj->data.tcp_port_monitor.port_range_end = (in_port_t)port_end;
2994 obj->data.tcp_port_monitor.item = item;
2995 obj->data.tcp_port_monitor.connection_index = connection_index;
2997 /* if the port monitor collection hasn't been created, we must create it */
2998 if ( !info.p_tcp_port_monitor_collection )
3000 info.p_tcp_port_monitor_collection =
3001 create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
3002 if ( !info.p_tcp_port_monitor_collection )
3004 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
3008 /* if a port monitor for this port does not exist, create one and add it to the collection */
3009 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
3011 tcp_port_monitor_t * p_monitor =
3012 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
3015 CRIT_ERR("tcp_portmon: unable to create port monitor");
3017 /* add the newly created monitor to the collection */
3018 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
3021 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
3028 ERR("unknown variable %s", s);
3029 obj->type = OBJ_text;
3030 snprintf(buf, 256, "${%s}", s);
3031 obj->data.s = strdup(buf);
3038 static struct text_object *create_plain_text(const char *s)
3040 struct text_object *obj;
3042 if (s == NULL || *s == '\0') {
3046 obj = new_text_object_internal();
3048 obj->type = OBJ_text;
3049 obj->data.s = strdup(s);
3053 static struct text_object_list *extract_variable_text_internal(const char *p)
3055 struct text_object_list *retval;
3056 struct text_object *obj;
3059 retval = malloc(sizeof(struct text_object_list));
3060 memset(retval, 0, sizeof(struct text_object_list));
3061 retval->text_object_count = 0;
3063 long line = text_lines;
3071 obj = create_plain_text(s);
3073 // allocate memory for the object
3074 retval->text_objects = realloc(retval->text_objects,
3075 sizeof(struct text_object) * (retval->text_object_count+1));
3076 // assign the new object to the end of the list.
3077 memcpy(&retval->text_objects[retval->text_object_count++],
3078 obj, sizeof(struct text_object));
3090 /* variable is either $foo or ${foo} */
3094 while (*p && *p != '}')
3100 while (*p && (isalnum((int) *p)
3105 /* copy variable to buffer */
3106 len = (p - s > 255) ? 255 : (p - s);
3107 strncpy(buf, s, len);
3116 /* if variable wasn't found from environment, use some special */
3122 if (strchr(buf, ' ')) {
3123 arg = strchr(buf, ' ');
3126 while (isspace((int) *arg))
3132 /* lowercase variable name */
3139 // create new object
3140 obj = construct_text_object(buf, arg, retval->text_object_count, retval->text_objects, line);
3142 // allocate memory for the object
3143 retval->text_objects = realloc(retval->text_objects,
3144 sizeof(struct text_object) * (retval->text_object_count+1));
3145 // assign the new object to the end of the list.
3146 memcpy(&retval->text_objects[retval->text_object_count++],
3147 obj, sizeof(struct text_object));
3153 obj = create_plain_text("$");
3155 // allocate memory for the object
3156 retval->text_objects = realloc(retval->text_objects,
3157 sizeof(struct text_object) * (retval->text_object_count+1));
3158 // assign the new object to the end of the list.
3159 memcpy(&retval->text_objects[retval->text_object_count++],
3160 obj, sizeof(struct text_object));
3167 obj = create_plain_text(s);
3169 // allocate memory for the object
3170 retval->text_objects = realloc(retval->text_objects,
3171 sizeof(struct text_object) * (retval->text_object_count+1));
3172 // assign the new object to the end of the list.
3173 memcpy(&retval->text_objects[retval->text_object_count++],
3174 obj, sizeof(struct text_object));
3179 ERR("one or more $endif's are missing");
3185 static void extract_variable_text(const char *p)
3187 struct text_object_list *list;
3189 free_text_objects(text_object_count, text_objects);
3190 text_object_count = 0;
3191 text_objects = NULL;
3194 list = extract_variable_text_internal(p);
3195 text_objects = list->text_objects;
3196 text_object_count = list->text_object_count;
3203 void parse_conky_vars(char * text, char * p, struct information *cur) {
3204 struct text_object_list *object_list = extract_variable_text_internal(text);
3205 generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
3209 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
3214 char buff_in[P_MAX_SIZE] = {0};
3215 iconv_converting = 0;
3218 for (i = 0; i < object_count; i++) {
3219 struct text_object *obj = &objs[i];
3221 #define OBJ(a) break; case OBJ_##a:
3223 switch (obj->type) {
3226 ERR("not implemented obj type %d",
3230 /* does anyone have decimals in acpi temperature? */
3232 snprintf(p, p_max_size, "%d", (int)
3233 get_acpi_temperature(obj->
3237 snprintf(p, 5, "%d ", (int)
3238 get_acpi_temperature(obj->
3243 /* does anyone have decimals in acpi temperature? */
3245 snprintf(p, p_max_size, "%d", (int)
3246 ((get_acpi_temperature(obj->
3248 i)+ 40) * 9.0 / 5 - 40));
3250 snprintf(p, 5, "%d ", (int)
3251 ((get_acpi_temperature(obj->
3253 i)+ 40) * 9.0 / 5 - 40));
3257 obj->a = get_freq(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3262 obj->a = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.cpu_index);
3265 #if defined(__linux__)
3268 obj->a = get_voltage(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3273 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.cpu_index);
3276 #endif /* __linux__ */
3280 get_freq_dynamic(p, 6, "%.0f ", 1 );
3282 get_freq_dynamic(p, p_max_size, "%.0f", 1 );
3287 get_freq_dynamic(p, 6, "%'.2f ", 1000);
3289 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
3293 get_adt746x_cpu(p, p_max_size);
3296 get_adt746x_fan(p, p_max_size);
3299 get_acpi_fan(p, p_max_size);
3301 OBJ(acpiacadapter) {
3302 get_acpi_ac_adapter(p, p_max_size);
3305 get_battery_stuff(p, p_max_size, obj->data.s);
3308 human_readable(cur->buffers * 1024, p, 255);
3311 human_readable(cur->cached * 1024, p, 255);
3314 if (obj->data.cpu_index > info.cpu_count) {
3315 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
3316 CRIT_ERR("attempting to use more CPUs then you have!");
3319 snprintf(p, p_max_size, "%*d", pad_percents,
3320 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3323 snprintf(p, 4, "%*d ",
3325 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3331 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3334 new_graph(p, obj->a,
3335 obj->b, obj->c, obj->d,
3336 (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3340 new_fg(p, obj->data.l);
3342 #if defined(__linux__)
3344 snprintf(p, p_max_size, "%s", i8k.version);
3347 snprintf(p, p_max_size, "%s", i8k.bios);
3350 snprintf(p, p_max_size, "%s", i8k.serial);
3353 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
3355 OBJ(i8k_cpu_tempf) {
3357 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3358 snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
3360 OBJ(i8k_left_fan_status) {
3361 int left_fan_status;
3362 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3363 if(left_fan_status == 0) {
3364 snprintf(p, p_max_size,"off");
3365 } if(left_fan_status == 1) {
3366 snprintf(p, p_max_size, "low");
3367 } if(left_fan_status == 2) {
3368 snprintf(p, p_max_size, "high");
3372 OBJ(i8k_right_fan_status) {
3373 int right_fan_status;
3374 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3375 if(right_fan_status == 0) {
3376 snprintf(p, p_max_size,"off");
3377 } if(right_fan_status == 1) {
3378 snprintf(p, p_max_size, "low");
3379 } if(right_fan_status == 2) {
3380 snprintf(p, p_max_size, "high");
3383 OBJ(i8k_left_fan_rpm) {
3384 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3386 OBJ(i8k_right_fan_rpm) {
3387 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3389 OBJ(i8k_ac_status) {
3391 sscanf(i8k.ac_status, "%d", &ac_status);
3392 if(ac_status == -1) {
3393 snprintf(p, p_max_size,"disabled (read i8k docs)");
3394 } if(ac_status == 0) {
3395 snprintf(p, p_max_size, "off");
3396 } if(ac_status == 1) {
3397 snprintf(p, p_max_size, "on");
3400 OBJ(i8k_buttons_status) {
3401 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3405 get_ibm_acpi_fan(p, p_max_size);
3408 get_ibm_acpi_temps();
3409 snprintf(p, p_max_size, "%d",
3410 ibm_acpi.temps[obj->data.sensor]);
3413 get_ibm_acpi_volume(p, p_max_size);
3415 OBJ(ibm_brightness) {
3416 get_ibm_acpi_brightness(p, p_max_size);
3419 get_powerbook_batt_info(p, p_max_size, obj->data.i);
3421 #endif /* __linux__ */
3425 new_font(p, obj->data.s);
3430 if (diskio_value > 1024*1024) {
3431 snprintf(p, p_max_size, "%.1fGiB",
3432 (double)diskio_value/1024/1024);
3433 } else if (diskio_value > 1024) {
3434 snprintf(p, p_max_size, "%.1fMiB",
3435 (double)diskio_value/1024);
3436 } else if (diskio_value > 0) {
3437 snprintf(p, p_max_size, "%dKiB", diskio_value);
3439 snprintf(p, p_max_size, "%dB", diskio_value);
3442 if (diskio_value > 1024*1024) {
3443 snprintf(p, 6, "%.1fGiB ",
3444 (double)diskio_value/1024/1024);
3445 } else if (diskio_value > 1024) {
3446 snprintf(p, 6, "%.1fMiB ",
3447 (double)diskio_value/1024);
3448 } else if (diskio_value > 0) {
3449 snprintf(p, 6, "%dKiB ", diskio_value);
3451 snprintf(p, 6, "%dB ", diskio_value);
3456 new_graph(p, obj->a,
3457 obj->b, obj->c, obj->d,
3458 diskio_value, obj->e, 1);
3463 snprintf(p, p_max_size, "%d",
3464 (int) (obj->data.net->
3468 snprintf(p, 6, "%d ",
3469 (int) (obj->data.net->
3475 snprintf(p, p_max_size, "%.1f",
3477 recv_speed / 1024.0);
3479 snprintf(p, 8, "%.1f ",
3481 recv_speed / 1024.0);
3483 OBJ(downspeedgraph) {
3484 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
3485 obj->data.net->recv_speed = 0.01;
3486 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3487 (obj->data.net->recv_speed /
3488 1024.0), obj->e, 1);
3492 i = obj->data.ifblock.pos - 1;
3502 snprintf(p, p_max_size, "%u.%u.%u.%u",
3503 obj->data.net->addr.
3505 obj->data.net->addr.
3507 obj->data.net->addr.
3509 obj->data.net->addr.
3514 snprintf(p, p_max_size, "%d",
3515 obj->data.net->linkstatus);
3517 #if defined(IMLIB2) && defined(X11)
3522 Imlib_Image image, buffer;
3523 image = imlib_load_image(obj->data.s);
3524 imlib_context_set_image(image);
3527 w = imlib_image_get_width();
3528 h = imlib_image_get_height();
3529 buffer = imlib_create_image(w, h);
3530 imlib_context_set_display(display);
3531 imlib_context_set_drawable(window.drawable);
3532 imlib_context_set_colormap(DefaultColormap(display, screen));
3533 imlib_context_set_visual(DefaultVisual(display, screen));
3534 imlib_context_set_image(buffer);
3535 imlib_blend_image_onto_image(image, 0, 0, 0, w, h, text_start_x, text_start_y, w, h);
3536 imlib_render_image_on_drawable(text_start_x, text_start_y);
3538 imlib_context_set_image(image);
3545 FILE *fp = popen(obj->data.s, "r");
3546 int length = fread(p, 1, p_max_size, fp);
3549 /*output[length] = '\0';
3550 if (length > 0 && output[length - 1] == '\n') {
3551 output[length - 1] = '\0';
3554 if (length > 0 && p[length - 1] == '\n') {
3555 p[length - 1] = '\0';
3558 //parse_conky_vars(output, p, cur);
3562 FILE *fp = popen(obj->data.s, "r");
3563 int n2 = fread(p, 1, p_max_size, fp);
3567 if (n2 && p[n2 - 1] == '\n')
3576 if (sscanf(p, "%lf", &barnum) == 0) {
3577 ERR("reading execbar value failed (perhaps it's not the correct format?)");
3579 if (barnum > 100 || barnum < 0) {
3580 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
3582 barnum = barnum / 100.0;
3583 new_bar(p, 0, 4, (int) (barnum * 255.0));
3589 FILE *fp = popen(obj->data.s, "r");
3590 int n2 = fread(p, 1, p_max_size, fp);
3594 if (n2 && p[n2 - 1] == '\n')
3603 if (sscanf(p, "%lf", &barnum) == 0) {
3604 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
3606 if (barnum > 100 || barnum < 0) {
3607 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
3609 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
3614 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3615 new_bar(p, 0, 4, (int) obj->f);
3618 FILE *fp = popen(obj->data.execi.cmd, "r");
3619 int n2 = fread(p, 1, p_max_size, fp);
3622 if (n2 && p[n2 - 1] == '\n')
3631 if (sscanf(p, "%f", &barnum) == 0) {
3632 ERR("reading execibar value failed (perhaps it's not the correct format?)");
3634 if (barnum > 100 || barnum < 0) {
3635 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
3637 obj->f = 255 * barnum / 100.0;
3638 new_bar(p, 0, 4, (int) obj->f);
3640 obj->data.execi.last_update =
3641 current_update_time;
3645 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3646 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
3649 FILE *fp = popen(obj->data.execi.cmd, "r");
3650 int n2 = fread(p, 1, p_max_size, fp);
3653 if (n2 && p[n2 - 1] == '\n')
3662 if (sscanf(p, "%f", &barnum) == 0) {
3663 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
3665 if (barnum > 100 || barnum < 0) {
3666 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
3669 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
3671 obj->data.execi.last_update = current_update_time;
3677 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
3678 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3680 char *output = obj->data.execi.buffer;
3681 FILE *fp = popen(obj->data.execi.cmd, "r");
3682 //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3683 int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3686 output[length] = '\0';
3687 if (length > 0 && output[length - 1] == '\n') {
3688 output[length - 1] = '\0';
3690 obj->data.execi.last_update = current_update_time;
3691 snprintf(p, p_max_size, "%s", output);
3693 //parse_conky_vars(output, p, cur);
3696 if (obj->data.execi.pos < 0) {
3697 obj->data.execi.last_update = current_update_time;
3698 if (pthread_create(&(obj->data.execi.thread_info.thread), NULL, (void*)threaded_exec, (void*) obj)) {
3699 ERR("Error starting thread");
3701 obj->data.execi.pos = register_thread(&(obj->data.execi.thread_info));
3703 pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
3704 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3705 pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
3707 #endif /* HAVE_POPEN */
3709 if (obj->global_mode && info.mail) { // this means we use info
3710 if (info.mail->pos < 0) {
3711 info.mail->last_update = current_update_time;
3712 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) info.mail)) {
3713 ERR("Error starting thread");
3715 info.mail->pos = register_thread(&(info.mail->thread_info));
3717 // get a lock before reading
3718 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3719 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3720 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3721 } else if (obj->data.mail) { // this means we use obj
3722 if (obj->data.mail->pos < 0) {
3723 obj->data.mail->last_update = current_update_time;
3724 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) obj->data.mail)) {
3725 ERR("Error starting thread");
3727 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3729 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3730 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3731 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3732 } else if (!obj->a) { // something is wrong, warn once then stop
3733 ERR("Theres a problem with your imap_unseen settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3737 OBJ(imap_messages) {
3738 if (obj->global_mode && info.mail) { // this means we use info
3739 if (info.mail->pos < 0) {
3740 info.mail->last_update = current_update_time;
3741 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) info.mail)) {
3742 ERR("Error starting thread");
3744 info.mail->pos = register_thread(&(info.mail->thread_info));
3746 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3747 snprintf(p, p_max_size, "%lu", info.mail->messages);
3748 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3749 } else if (obj->data.mail) { // this means we use obj
3750 if (obj->data.mail->pos < 0) {
3751 obj->data.mail->last_update = current_update_time;
3752 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) obj->data.mail)) {
3753 ERR("Error starting thread");
3755 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3757 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3758 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
3759 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3760 } else if (!obj->a) { // something is wrong, warn once then stop
3761 ERR("Theres a problem with your imap_messages settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3766 if (obj->global_mode && info.mail) { // this means we use info
3767 if (info.mail->pos < 0) {
3768 info.mail->last_update = current_update_time;
3769 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) info.mail)) {
3770 ERR("Error starting thread");
3772 info.mail->pos = register_thread(&(info.mail->thread_info));
3774 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3775 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3776 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3777 } else if (obj->data.mail) { // this means we use obj
3778 if (obj->data.mail->pos < 0) {
3779 obj->data.mail->last_update = current_update_time;
3780 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) obj->data.mail)) {
3781 ERR("Error starting thread");
3783 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3785 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3786 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3787 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3788 } else if (!obj->a) { // something is wrong, warn once then stop
3789 ERR("Theres a problem with your pop3_unseen settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3794 if (obj->global_mode && info.mail) { // this means we use info
3795 if (info.mail->pos < 0) {
3796 info.mail->last_update = current_update_time;
3797 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) info.mail)) {
3798 ERR("Error starting thread");
3800 info.mail->pos = register_thread(&(info.mail->thread_info));
3802 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3803 snprintf(p, p_max_size, "%.1f", info.mail->used/1024.0/1024.0);
3804 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3805 } else if (obj->data.mail) { // this means we use obj
3806 if (obj->data.mail->pos < 0) {
3807 obj->data.mail->last_update = current_update_time;
3808 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) obj->data.mail)) {
3809 ERR("Error starting thread");
3811 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3813 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3814 snprintf(p, p_max_size, "%.1f", obj->data.mail->used/1024.0/1024.0);
3815 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3816 } else if (!obj->a) { // something is wrong, warn once then stop
3817 ERR("Theres a problem with your pop3_used settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3822 if (obj->data.fs != NULL) {
3823 if (obj->data.fs->size == 0)
3842 if (obj->data.fs != NULL)
3843 human_readable(obj->data.fs->avail,
3847 if (obj->data.fs != NULL) {
3848 if (obj->data.fs->size)
3849 snprintf(p, p_max_size, "%*d",
3858 snprintf(p, p_max_size, "0");
3862 if (obj->data.fs != NULL)
3863 human_readable(obj->data.fs->size,
3867 if (obj->data.fs != NULL)
3868 human_readable(obj->data.fs->size -
3869 (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
3873 if (obj->data.fs != NULL) {
3874 if (obj->data.fs->size == 0)
3892 if (obj->data.fs != NULL) {
3893 if (obj->data.fs->size)
3894 snprintf(p, 4, "%d",
3904 snprintf(p, p_max_size, "0");
3908 float *v = info.loadavg;
3910 if (obj->data.loadavg[2])
3911 snprintf(p, p_max_size, "%.2f %.2f %.2f",
3912 v[obj->data.loadavg[0] -
3914 v[obj->data.loadavg[1] -
3916 v[obj->data.loadavg[2] -
3918 else if (obj->data.loadavg[1])
3919 snprintf(p, p_max_size, "%.2f %.2f",
3920 v[obj->data.loadavg[0] -
3922 v[obj->data.loadavg[1] -
3924 else if (obj->data.loadavg[0])
3925 snprintf(p, p_max_size, "%.2f",
3926 v[obj->data.loadavg[0] -
3930 new_goto(p, obj->data.i);
3933 new_tab(p, obj->data.pair.a, obj->data.pair.b);
3936 new_hr(p, obj->data.i);
3943 temp = get_hddtemp_info(obj->data.hddtemp.dev,
3944 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
3946 snprintf(p, p_max_size, "N/A");
3947 } else if (unit == '*') {
3948 snprintf(p, p_max_size, "%s", temp);
3950 snprintf(p, p_max_size, "%s°%c", temp, unit);
3955 new_offset(p, obj->data.i);
3958 new_voffset(p, obj->data.i);
3963 r = get_i2c_info(&obj->data.i2c.fd,
3965 obj->data.i2c.devtype,
3966 obj->data.i2c.type);
3968 if (r >= 100.0 || r == 0)
3969 snprintf(p, p_max_size, "%d", (int) r);
3971 snprintf(p, p_max_size, "%.1f", r);
3974 new_alignr(p, obj->data.i);
3977 new_alignc(p, obj->data.i);
3981 if ((obj->data.ifblock.s)
3982 && (stat(obj->data.ifblock.s, &tmp) ==
3984 i = obj->data.ifblock.pos;
3991 if ((obj->data.ifblock.s)
3992 && (!check_mount(obj->data.ifblock.s))) {
3993 i = obj->data.ifblock.pos;
4000 if ((obj->data.ifblock.s)
4001 && system(obj->data.ifblock.s)) {
4002 i = obj->data.ifblock.pos;
4009 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4012 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4017 human_readable(cur->mem * 1024, p, 255);
4020 human_readable(cur->memmax * 1024, p, 255);
4025 snprintf(p, p_max_size, "%*lu",
4030 snprintf(p, 4, "%*lu ",
4037 new_bar(p, obj->data.pair.a,
4039 cur->memmax ? (cur->mem * 255) /
4044 new_graph(p, obj->a,
4045 obj->b, obj->c, obj->d,
4046 cur->memmax ? (cur->mem * 100.0) /
4047 (cur->memmax) : 0.0, 100, 1);
4051 snprintf(p, p_max_size, "%d",
4052 mixer_get_avg(obj->data.l));
4055 snprintf(p, p_max_size, "%d",
4056 mixer_get_left(obj->data.l));
4059 snprintf(p, p_max_size, "%d",
4060 mixer_get_right(obj->data.l));
4063 new_bar(p, obj->data.mixerbar.w,
4064 obj->data.mixerbar.h,
4065 mixer_get_avg(obj->data.mixerbar.
4069 new_bar(p, obj->data.mixerbar.w,
4070 obj->data.mixerbar.h,
4071 mixer_get_left(obj->data.mixerbar.
4075 new_bar(p, obj->data.mixerbar.w,
4076 obj->data.mixerbar.h,
4077 mixer_get_right(obj->data.mixerbar.
4083 snprintf(p, p_max_size, "%d", cur->mail_count);
4086 snprintf(p, p_max_size, "%d", cur->new_mail_count);
4090 snprintf(p, p_max_size, "%s",
4091 cur->uname_s.nodename);
4094 new_outline(p, obj->data.l);
4098 snprintf(p, p_max_size, "%hu", cur->procs);
4100 snprintf(p, 5, "%hu ",
4103 OBJ(running_processes) {
4105 snprintf(p, p_max_size, "%hu",
4108 snprintf(p, 3, "%hu ",
4112 snprintf(p, p_max_size, "%s", obj->data.s);
4115 new_bg(p, obj->data.l);
4118 new_stippled_hr(p, obj->data.pair.a,
4122 human_readable(cur->swap * 1024, p, 255);
4125 human_readable(cur->swapmax * 1024, p,
4129 if (cur->swapmax == 0) {
4130 strncpy(p, "No swap", 255);
4133 snprintf(p, 255, "%*lu",
4139 snprintf(p, 4, "%*lu ",
4147 new_bar(p, obj->data.pair.a,
4149 cur->swapmax ? (cur->swap * 255) /
4150 (cur->swapmax) : 0);
4153 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4156 time_t t = time(NULL);
4157 struct tm *tm = localtime(&t);
4158 setlocale(LC_TIME, "");
4159 strftime(p, p_max_size, obj->data.s, tm);
4162 time_t t = time(NULL);
4163 struct tm *tm = gmtime(&t);
4164 strftime(p, p_max_size, obj->data.s, tm);
4168 if (obj->data.tztime.tz) {
4169 oldTZ = getenv("TZ");
4170 setenv("TZ", obj->data.tztime.tz, 1);
4173 time_t t = time(NULL);
4174 struct tm *tm = localtime(&t);
4175 setlocale(LC_TIME, "");
4176 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4178 setenv("TZ", oldTZ, 1);
4183 // Needless to free oldTZ since getenv gives ptr to static data
4186 human_readable(obj->data.net->recv, p,
4190 human_readable(obj->data.net->trans, p,
4194 snprintf(p, p_max_size, "%d", total_updates);
4198 snprintf(p, p_max_size, "%d",
4199 (int) (obj->data.net->
4203 snprintf(p, 5, "%d ",
4204 (int) (obj->data.net->
4210 snprintf(p, p_max_size, "%.1f",
4212 trans_speed / 1024.0);
4214 snprintf(p, 8, "%.1f ",
4216 trans_speed / 1024.0);
4219 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
4220 obj->data.net->trans_speed = 0.01;
4221 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4222 (obj->data.net->trans_speed /
4223 1024.0), obj->e, 1);
4226 format_seconds_short(p, p_max_size,
4230 format_seconds(p, p_max_size, (int) cur->uptime);
4233 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
4235 snprintf(p, p_max_size, "%s", get_apm_adapter());
4237 OBJ(apm_battery_life) {
4239 msg = get_apm_battery_life();
4240 snprintf(p, p_max_size, "%s", msg);
4243 OBJ(apm_battery_time) {
4245 msg = get_apm_battery_time();
4246 snprintf(p, p_max_size, "%s", msg);
4249 #endif /* __FreeBSD__ */
4253 snprintf(p, p_max_size, "%s", cur->mpd.title);
4256 snprintf(p, p_max_size, "%s", cur->mpd.artist);
4259 snprintf(p, p_max_size, "%s", cur->mpd.album);
4262 snprintf(p, p_max_size, "%s", cur->mpd.random);
4265 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4268 snprintf(p, p_max_size, "%s", cur->mpd.track);
4271 snprintf(p, p_max_size, "%s", cur->mpd.name);
4274 snprintf(p, p_max_size, "%s", cur->mpd.file);
4277 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4280 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4283 snprintf(p, p_max_size, "%s", cur->mpd.status);
4286 int days = 0, hours = 0, minutes =
4288 int tmp = cur->mpd.elapsed;
4289 while (tmp >= 86400) {
4293 while (tmp >= 3600) {
4303 snprintf(p, p_max_size, "%i days %i:%02i:%02i",
4304 days, hours, minutes,
4307 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4310 snprintf(p, p_max_size, "%i:%02i", minutes,
4314 int days = 0, hours = 0, minutes =
4316 int tmp = cur->mpd.length;
4317 while (tmp >= 86400) {
4321 while (tmp >= 3600) {
4331 snprintf(p, p_max_size,
4332 "%i days %i:%02i:%02i",
4333 days, hours, minutes,
4336 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4339 snprintf(p, p_max_size, "%i:%02i", minutes,
4343 snprintf(p, p_max_size, "%2.0f",
4344 cur->mpd.progress * 100);
4347 new_bar(p, obj->data.pair.a,
4349 (int) (cur->mpd.progress *
4353 if (strlen(cur->mpd.title) < 2 && strlen(cur->mpd.title) < 2) {
4354 snprintf(p, p_max_size, "%s", cur->mpd.file);
4356 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist, cur->mpd.title);
4362 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4365 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4368 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4371 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4373 OBJ(xmms2_comment) {
4374 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4376 OBJ(xmms2_decoder) {
4377 snprintf(p, p_max_size, "%s", cur->xmms2.decoder);
4379 OBJ(xmms2_transport) {
4380 snprintf(p, p_max_size, "%s", cur->xmms2.transport);
4383 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4386 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4389 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4391 OBJ(xmms2_tracknr) {
4392 if (cur->xmms2.tracknr != -1)
4393 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4395 OBJ(xmms2_bitrate) {
4396 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4399 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4402 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4404 OBJ(xmms2_elapsed) {
4405 int tmp = cur->xmms2.elapsed;
4406 snprintf(p, p_max_size, "%02d:%02d",
4407 tmp / 60000, (tmp / 1000) % 60);
4409 OBJ(xmms2_duration) {
4410 int tmp = cur->xmms2.duration;
4411 snprintf(p, p_max_size, "%02d:%02d",
4412 tmp / 60000, (tmp / 1000) % 60);
4414 OBJ(xmms2_percent) {
4415 snprintf(p, p_max_size, "%2.0f",
4416 cur->xmms2.progress * 100);
4419 new_bar(p, obj->data.pair.a,
4421 (int) (cur->xmms2.progress *
4425 if (strlen(cur->xmms2.title) < 2 && strlen(cur->xmms2.title) < 2) {
4426 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4428 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist, cur->xmms2.title);
4433 OBJ(audacious_status) {
4434 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_STATUS]);
4436 OBJ(audacious_title) {
4437 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_TITLE]);
4439 OBJ(audacious_length) {
4440 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH]);
4442 OBJ(audacious_length_seconds) {
4443 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4445 OBJ(audacious_position) {
4446 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION]);
4448 OBJ(audacious_position_seconds) {
4449 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4451 OBJ(audacious_bitrate) {
4452 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_BITRATE]);
4454 OBJ(audacious_frequency) {
4455 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FREQUENCY]);
4457 OBJ(audacious_channels) {
4458 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_CHANNELS]);
4460 OBJ(audacious_filename) {
4461 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FILENAME]);
4463 OBJ(audacious_playlist_length) {
4464 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4466 OBJ(audacious_playlist_position) {
4467 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4469 OBJ(audacious_bar) {
4471 progress= atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4472 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4473 new_bar(p,obj->a,obj->b,(int)(progress*255.0f));
4478 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4481 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4484 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4487 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4490 snprintf(p, p_max_size, "%i", cur->bmpx.track);
4493 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4497 if (obj->data.top.type == TOP_NAME
4498 && obj->data.top.num >= 0
4499 && obj->data.top.num < 10) {
4500 // if we limit the buffer and add a bunch of space after, it stops the thing from
4501 // moving other shit around, which is really fucking annoying
4502 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
4503 } else if (obj->data.top.type == TOP_CPU
4504 && obj->data.top.num >= 0
4505 && obj->data.top.num < 10) {
4506 snprintf(p, 7, "%3.2f ",
4507 cur->cpu[obj->data.top.
4509 } else if (obj->data.top.type == TOP_PID
4510 && obj->data.top.num >= 0
4511 && obj->data.top.num < 10) {
4512 snprintf(p, 8, "%i ",
4513 cur->cpu[obj->data.top.
4515 } else if (obj->data.top.type == TOP_MEM
4516 && obj->data.top.num >= 0
4517 && obj->data.top.num < 10) {
4518 snprintf(p, 7, "%3.2f ",
4519 cur->cpu[obj->data.top.
4524 if (obj->data.top.type == TOP_NAME
4525 && obj->data.top.num >= 0
4526 && obj->data.top.num < 10) {
4527 // if we limit the buffer and add a bunch of space after, it stops the thing from
4528 // moving other shit around, which is really fucking annoying
4531 cur->memu[obj->data.top.
4533 } else if (obj->data.top.type == TOP_CPU
4534 && obj->data.top.num >= 0
4535 && obj->data.top.num < 10) {
4536 snprintf(p, 7, "%3.2f ",
4537 cur->memu[obj->data.top.
4539 } else if (obj->data.top.type == TOP_PID
4540 && obj->data.top.num >= 0
4541 && obj->data.top.num < 10) {
4542 snprintf(p, 8, "%i ",
4543 cur->memu[obj->data.top.
4545 } else if (obj->data.top.type == TOP_MEM
4546 && obj->data.top.num >= 0
4547 && obj->data.top.num < 10) {
4548 snprintf(p, 7, "%3.2f ",
4549 cur->memu[obj->data.top.
4556 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4557 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4559 obj->data.tail.last_update = current_update_time;
4563 tailstring *head = NULL;
4564 tailstring *headtmp = NULL;
4565 tailstring *freetmp = NULL;
4566 fp = fopen(obj->data.tail.logfile, "rt");
4568 ERR("tail logfile failed to open");
4571 obj->data.tail.readlines = 0;
4573 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
4580 addtail(&head, obj->data.tail.buffer);
4581 obj->data.tail.readlines++;
4587 if (obj->data.tail.readlines > 0) {
4588 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4589 addtail(&headtmp, head->data);
4594 strcpy(obj->data.tail.buffer, headtmp->data);
4595 headtmp = headtmp->next;
4596 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4598 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
4599 headtmp = headtmp->next;
4603 /* get rid of any ugly newlines at the end */
4604 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4605 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4607 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4611 strcpy(obj->data.tail.buffer, "Logfile Empty");
4612 snprintf(p, p_max_size, "Logfile Empty");
4613 } /* if readlines */
4615 } /* if cur_upd_time >= */
4617 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4621 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4622 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4624 obj->data.tail.last_update = current_update_time;
4626 tailstring *head = NULL;
4627 tailstring *headtmp = NULL;
4628 tailstring *freetmp = NULL;
4629 fp = fopen(obj->data.tail.logfile, "rt");
4631 ERR("head logfile failed to open");
4633 obj->data.tail.readlines = 0;
4634 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
4635 addtail(&head, obj->data.tail.buffer);
4636 obj->data.tail.readlines++;
4640 if (obj->data.tail.readlines > 0) {
4642 addtail(&headtmp, head->data);
4647 strcpy(obj->data.tail.buffer, headtmp->data);
4648 headtmp = headtmp->next;
4650 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 20) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
4651 headtmp = headtmp->next;
4654 /* get rid of any ugly newlines at the end */
4655 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4656 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4658 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4660 strcpy(obj->data.tail.buffer, "Logfile Empty");
4661 snprintf(p, p_max_size, "Logfile Empty");
4662 } /* if readlines > 0 */
4663 } /* if fp == null */
4664 } /* cur_upd_time >= */
4666 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4669 #ifdef TCP_PORT_MONITOR
4672 /* grab a pointer to this port monitor */
4673 tcp_port_monitor_t * p_monitor =
4674 find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
4675 obj->data.tcp_port_monitor.port_range_begin,
4676 obj->data.tcp_port_monitor.port_range_end );
4678 snprintf(p, p_max_size, "monitor not found");
4682 /* now grab the text of interest */
4683 if ( peek_tcp_port_monitor( p_monitor,
4684 obj->data.tcp_port_monitor.item,
4685 obj->data.tcp_port_monitor.connection_index,
4686 p, p_max_size ) != 0 )
4688 snprintf(p, p_max_size, "monitor peek error");
4697 iconv_converting = 1;
4698 iconv_selected = obj->a;
4703 iconv_converting = 0;
4712 unsigned int a = strlen(p);
4715 if (a > 0 && iconv_converting && iconv_selected > 0 && (iconv_cd[iconv_selected - 1] != (iconv_t)(-1))) {
4717 size_t dummy1, dummy2;
4718 char *ptr = buff_in;
4721 dummy1 = dummy2 = a;
4723 strncpy(buff_in, p, P_MAX_SIZE);
4725 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
4726 while (dummy1 > 0) {
4727 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1, &outptr, &dummy2);
4729 ERR("Iconv codeset conversion failed");
4734 /* It is nessecary when we are converting from multibyte to singlebyte codepage */
4745 double current_update_time, last_update_time;
4747 static void generate_text()
4749 struct information *cur = &info;
4757 current_update_time = get_time();
4761 /* add things to the buffer */
4767 generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
4769 if (stuff_in_upper_case) {
4780 last_update_time = current_update_time;
4787 static void set_font()
4791 if (window.xftdraw != NULL) {
4792 XftDrawDestroy(window.xftdraw);
4794 window.xftdraw = XftDrawCreate(display, window.drawable,
4795 DefaultVisual(display,
4797 DefaultColormap(display,
4802 XSetFont(display, window.gc, fonts[selected_font].font->fid);
4808 static inline int get_string_width(const char *s)
4811 return *s ? calc_text_width(s, strlen(s)) : 0;
4817 static inline int get_string_width_special(char *s)
4830 if (*p == SPECIAL_CHAR) {
4831 /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
4832 for (i = 0; i < strlen(p); i++) {
4833 *(p + i) = *(p + i + 1);
4835 if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
4836 width += specials[special_index+index].width;
4843 if (strlen(final) > 1) {
4844 width += calc_text_width(final, strlen(final));
4854 static void text_size_updater(char *s);
4856 int last_font_height;
4857 static void update_text_area()
4861 /* update text size if it isn't fixed */
4866 text_width = minimum_width;
4869 last_font_height = font_height();
4870 for_each_line(text_buffer, text_size_updater);
4872 if (text_height < minimum_height)
4873 text_height = minimum_height;
4874 if (text_width > maximum_width && maximum_width > 0)
4875 text_width = maximum_width;
4878 /* get text position on workarea */
4879 switch (text_alignment) {
4886 x = workarea[2] - text_width - gap_x;
4893 y = workarea[3] - text_height - gap_y;
4897 x = workarea[2] - text_width - gap_x;
4898 y = workarea[3] - text_height - gap_y;
4902 case NONE: // Let the WM manage the window
4913 if (own_window && !fixed_pos) {
4916 text_start_x = border_margin + 1;
4917 text_start_y = border_margin + 1;
4918 window.x = x - border_margin - 1;
4919 window.y = y - border_margin - 1;
4923 /* If window size doesn't match to workarea's size, then window
4924 * probably includes panels (gnome).
4925 * Blah, doesn't work on KDE. */
4926 if (workarea[2] != window.width
4927 || workarea[3] != window.height) {
4941 static int cur_x, cur_y; /* current x and y for drawing */
4942 static int draw_mode; /* FG, BG or OUTLINE */
4943 static long current_color;
4946 static void text_size_updater(char *s)
4950 /* get string widths and skip specials */
4953 if (*p == SPECIAL_CHAR) {
4955 w += get_string_width(s);
4958 if (specials[special_index].type == BAR
4959 || specials[special_index].type == GRAPH) {
4960 w += specials[special_index].width;
4961 if (specials[special_index].height > last_font_height) {
4962 last_font_height = specials[special_index].height;
4963 last_font_height += font_ascent();
4965 } else if (specials[special_index].type == OFFSET) {
4966 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
4967 } else if (specials[special_index].type == VOFFSET) {
4968 last_font_height += specials[special_index].arg;
4969 } else if (specials[special_index].type == GOTO) {
4970 if (specials[special_index].arg >= 0)
4971 w += (int)specials[special_index].arg - cur_x;
4972 } else if (specials[special_index].type == TAB) {
4973 int start = specials[special_index].arg;
4974 int step = specials[special_index].width;
4975 if (!step || step < 0)
4977 w += step - (cur_x - text_start_x - start) % step;
4978 } else if (specials[special_index].type == FONT) {
4979 selected_font = specials[special_index].font_added;
4980 if (font_height() > last_font_height) {
4981 last_font_height = font_height();
4990 w += get_string_width(s);
4991 if (w > text_width) {
4994 if (text_width > maximum_width && maximum_width) {
4995 text_width = maximum_width;
4998 text_height += last_font_height;
4999 last_font_height = font_height();
5003 static inline void set_foreground_color(long c)
5006 XSetForeground(display, window.gc, c);
5010 static void draw_string(const char *s)
5014 int i, i2, pos, width_of_s;
5017 width_of_s = get_string_width(s);
5018 if (out_to_console) {
5020 fflush(stdout); /* output immediately, don't buffer */
5022 /* daemon_run(s); the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
5023 memset(tmpstring1,0,TEXT_BUFFER_SIZE);
5024 memset(tmpstring2,0,TEXT_BUFFER_SIZE);
5025 strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
5029 snprintf(space, 2, " ");
5031 max = ((text_width - width_of_s) / get_string_width(space));
5034 * This code looks for tabs in the text and coverts them to spaces.
5035 * The trick is getting the correct number of spaces,
5036 * and not going over the window's size without forcing
5037 * the window larger.
5039 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
5040 if (tmpstring1[i] == '\t') // 9 is ascii tab
5044 i2 < (8 - (1 + pos) % 8) && added <= max;
5047 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
5048 fprintf(stderr,"buffer overrun detected\n");
5050 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
5055 if (tmpstring1[i] != 9) {
5057 if ( pos > TEXT_BUFFER_SIZE-1 )
5058 fprintf(stderr,"buffer overrun detected\n");
5060 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
5066 if (text_width == maximum_width) {
5067 /* this means the text is probably pushing the limit, so we'll chop it */
5068 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
5069 tmpstring2[strlen(tmpstring2)-1] = '\0';
5079 c.pixel = current_color;
5080 XQueryColor(display, DefaultColormap(display, screen), &c);
5083 c2.color.red = c.red;
5084 c2.color.green = c.green;
5085 c2.color.blue = c.blue;
5086 c2.color.alpha = fonts[selected_font].font_alpha;
5088 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5089 cur_x, cur_y, (XftChar8 *) s,
5092 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5093 cur_x, cur_y, (XftChar8 *) s,
5099 XDrawString(display, window.drawable, window.gc,
5100 cur_x, cur_y, s, strlen(s));
5102 cur_x += width_of_s;
5104 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
5107 long redmask, greenmask, bluemask;
5109 void set_up_gradient()
5112 colour_depth = DisplayPlanes(display, screen);
5116 if (colour_depth != 24 && colour_depth != 16) {
5117 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
5123 for(i = (colour_depth / 3)-1; i>=0; i--) {
5125 greenmask |= 1 << i;
5128 if (colour_depth%3 == 1) {
5129 greenmask |= 1 << (colour_depth / 3);
5131 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
5132 greenmask = greenmask << (colour_depth / 3);
5135 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 */
5137 int red1, green1, blue1; // first colour
5138 int red2, green2, blue2; // second colour
5139 int red3 = 0, green3 = 0, blue3 = 0; // difference
5140 short redshift = (2*colour_depth / 3 + colour_depth%3);
5141 short greenshift = (colour_depth / 3);
5142 red1 = (first_colour & redmask) >> redshift;
5143 green1 = (first_colour & greenmask) >> greenshift;
5144 blue1 = first_colour & bluemask;
5145 red2 = (last_colour & redmask) >> redshift;
5146 green2 = (last_colour & greenmask) >> greenshift;
5147 blue2 = last_colour & bluemask;
5154 if (green1 > green2) {
5157 if (green1 < green2) {
5160 if (blue1 > blue2) {
5163 if (blue1 < blue2) {
5178 if (red1 > bluemask) {
5181 if (green1 > bluemask) {
5184 if (blue1 > bluemask) {
5187 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
5191 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
5192 if (colour_depth == 0) {
5195 int red1, green1, blue1; // first colour
5196 int red2, green2, blue2; // second colour
5197 long redshift = (2*colour_depth / 3 + colour_depth%3);
5198 long greenshift = (colour_depth / 3);
5199 int red3 = 0, green3 = 0, blue3 = 0; // difference
5200 red1 = (first_colour & redmask) >> redshift;
5201 green1 = (first_colour & greenmask) >> greenshift;
5202 blue1 = first_colour & bluemask;
5203 red2 = (last_colour & redmask) >> redshift;
5204 green2 = (last_colour & greenmask) >> greenshift;
5205 blue2 = last_colour & bluemask;
5206 red3 = abs(red1 - red2);
5207 green3 = abs(green1 - green2);
5208 blue3 = abs(blue1 - blue2);
5217 static void draw_line(char *s)
5221 cur_x = text_start_x;
5222 cur_y += font_ascent();
5224 short font_h = font_height();
5226 /* find specials and draw stuff */
5229 if (*p == SPECIAL_CHAR) {
5232 /* draw string before special */
5239 switch (specials[special_index].type) {
5240 case HORIZONTAL_LINE:
5243 specials[special_index].height;
5244 int mid = font_ascent() / 2;
5245 w = text_start_x + text_width -
5248 XSetLineAttributes(display,
5253 XDrawLine(display, window.drawable,
5264 specials[special_index].height;
5266 specials[special_index].arg;
5267 int mid = font_ascent() / 2;
5268 char ss[2] = { s, s };
5269 w = text_start_x + text_width -
5272 XSetLineAttributes(display,
5277 XSetDashes(display, window.gc, 0,
5279 XDrawLine(display, window.drawable,
5289 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5293 specials[special_index].height;
5295 specials[special_index].arg;
5296 int by = cur_y - (font_ascent() / 2) - 1;
5297 if (h < font_height()) {
5300 w = specials[special_index].width;
5303 text_width - cur_x - 1;
5307 XSetLineAttributes(display,
5313 XDrawRectangle(display,
5317 XFillRectangle(display,
5321 w * bar_usage / 255,
5323 if (specials[special_index].
5325 && specials[special_index].
5329 [special_index].height;
5336 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5340 specials[special_index].height;
5341 unsigned long last_colour = current_color;
5342 int by = cur_y - (font_ascent()/2) - 1;
5343 if (h < font_height()) {
5346 w = specials[special_index].width;
5348 w = text_start_x + text_width - cur_x - 1;
5351 if (draw_graph_borders) {
5352 XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
5353 XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
5355 XSetLineAttributes(display,
5362 int gradient_size = 0;
5363 float gradient_factor = 0;
5364 float gradient_update = 0;
5365 unsigned long tmpcolour = current_color;
5366 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5367 tmpcolour = specials[special_index].last_colour;
5368 gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
5369 gradient_factor = (float)gradient_size / (w - 2);
5371 for (i = w - 2; i > -1; i--) {
5372 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5373 XSetForeground(display, window.gc, tmpcolour);
5374 gradient_update += gradient_factor;
5375 while (gradient_update > 0) {
5376 tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
5380 if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
5383 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 */
5385 if (specials[special_index].
5387 && specials[special_index].
5391 [special_index].height;
5393 /* if (draw_mode == BG) {
5394 set_foreground_color(default_bg_color);
5396 else if (draw_mode == OUTLINE) {
5397 set_foreground_color(default_out_color);
5399 set_foreground_color(default_fg_color);
5401 set_foreground_color(last_colour);
5407 int old = font_ascent();
5408 cur_y -= font_ascent();
5409 selected_font = specials[special_index].font_added;
5410 if (cur_y + font_ascent() < cur_y + old) {
5413 cur_y += font_ascent();
5419 if (draw_mode == FG)
5420 set_foreground_color(specials
5426 if (draw_mode == BG)
5427 set_foreground_color(specials
5433 if (draw_mode == OUTLINE)
5434 set_foreground_color(specials
5440 w += specials[special_index].arg;
5444 cur_y += specials[special_index].arg;
5448 if (specials[special_index].arg >= 0)
5449 cur_x = (int)specials[special_index].arg;
5454 int start = specials[special_index].arg;
5455 int step = specials[special_index].width;
5456 if (!step || step < 0)
5458 w = step - (cur_x - text_start_x - start) % step;
5464 int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
5465 /*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);*/
5466 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5467 cur_x = pos_x - specials[special_index].arg;
5474 int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
5475 /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
5476 /*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);*/
5478 specials[special_index].arg)
5481 [special_index].arg;
5498 if (cur_y_add > 0) {
5500 cur_y -= font_descent();
5505 cur_y += font_descent();
5509 static void draw_text()
5512 cur_y = text_start_y;
5515 if (draw_borders && border_width > 0) {
5516 unsigned int b = (border_width + 1) / 2;
5518 if (stippled_borders) {
5520 { stippled_borders, stippled_borders };
5521 XSetLineAttributes(display, window.gc,
5522 border_width, LineOnOffDash,
5523 CapButt, JoinMiter);
5524 XSetDashes(display, window.gc, 0, ss, 2);
5526 XSetLineAttributes(display, window.gc,
5527 border_width, LineSolid,
5528 CapButt, JoinMiter);
5531 XDrawRectangle(display, window.drawable, window.gc,
5532 text_start_x - border_margin + b,
5533 text_start_y - border_margin + b,
5534 text_width + border_margin * 2 - 1 - b * 2,
5535 text_height + border_margin * 2 - 1 -
5542 for_each_line(text_buffer, draw_line);
5545 static void draw_stuff()
5549 if (draw_shades && !draw_outline) {
5552 set_foreground_color(default_bg_color);
5562 for (i = -1; i < 2; i++)
5563 for (j = -1; j < 2; j++) {
5564 if (i == 0 && j == 0)
5568 set_foreground_color(default_out_color);
5569 draw_mode = OUTLINE;
5576 set_foreground_color(default_fg_color);
5584 swap.swap_window = window.window;
5585 swap.swap_action = XdbeBackground;
5586 XdbeSwapBuffers(display, &swap, 1);
5592 static void clear_text(int exposures)
5596 return; /* The swap action is XdbeBackground, which clears */
5600 /* there is some extra space for borders and outlines */
5601 XClearArea(display, window.drawable,
5602 text_start_x - border_margin - 1,
5603 text_start_y - border_margin - 1,
5604 text_width + border_margin * 2 + 2,
5605 text_height + border_margin * 2 + 2,
5606 exposures ? True : 0);
5611 static int need_to_update;
5613 /* update_text() generates new text and clears old text area */
5614 static void update_text()
5623 static void main_loop()
5625 #ifdef SIGNAL_BLOCKING
5626 sigset_t newmask, oldmask;
5628 sigemptyset(&newmask);
5629 sigaddset(&newmask,SIGINT);
5630 sigaddset(&newmask,SIGTERM);
5631 sigaddset(&newmask,SIGUSR1);
5635 Region region = XCreateRegion();
5636 int event_base, error_base;
5638 if (!XDamageQueryExtension (display, &event_base, &error_base)) {
5639 ERR("Xdamage extension unavailable");
5641 Damage damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5642 XserverRegion region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5643 XserverRegion part = XFixesCreateRegionFromWindow(display, window.window, 0);
5644 #endif /* HAVE_XDAMAGE */
5648 while (total_run_times == 0 || info.looped < total_run_times) {
5651 #ifdef SIGNAL_BLOCKING
5652 /* block signals. we will inspect for pending signals later */
5653 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
5654 CRIT_ERR("unable to sigprocmask()");
5660 /* wait for X event or timeout */
5662 if (!XPending(display)) {
5667 update_interval - (get_time() -
5673 tv.tv_sec = (long) t;
5674 tv.tv_usec = (long) (t * 1000000) % 1000000;
5676 FD_SET(ConnectionNumber(display), &fdsr);
5679 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
5682 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
5687 ERR("can't select(): %s",
5698 if (need_to_update) {
5700 int wx = window.x, wy = window.y;
5708 /* resize window if it isn't right size */
5710 (text_width + border_margin * 2 + 1 !=
5712 || text_height + border_margin * 2 + 1 !=
5716 border_margin * 2 + 1;
5719 border_margin * 2 + 1;
5720 XResizeWindow(display,
5725 set_transparent_background(window.drawable);
5729 /* move window if it isn't in right position */
5732 || window.y != wy)) {
5733 XMoveWindow(display, window.window,
5734 window.x, window.y);
5744 r.x = text_start_x - border_margin;
5745 r.y = text_start_y - border_margin;
5746 r.width = text_width + border_margin * 2;
5747 r.height = text_height + border_margin * 2;
5748 XUnionRectWithRegion(&r, region, region);
5753 /* handle X events */
5755 while (XPending(display)) {
5757 XNextEvent(display, &ev);
5764 r.width = ev.xexpose.width;
5765 r.height = ev.xexpose.height;
5766 XUnionRectWithRegion(&r, region,
5772 case ReparentNotify:
5773 /* set background to ParentRelative for all parents */
5775 set_transparent_background(window.
5780 case ConfigureNotify:
5782 /* if window size isn't what expected, set fixed size */
5783 if (ev.xconfigure.width !=
5785 || ev.xconfigure.height !=
5787 if (window.width != 0
5788 && window.height != 0)
5791 /* clear old stuff before screwing up size and pos */
5797 if (XGetWindowAttributes(display, window.window, &attrs)) {
5812 border_margin * 2 - 1;
5815 border_margin * 2 - 1;
5816 if (text_width > maximum_width && maximum_width > 0)
5817 text_width = maximum_width;
5820 /* if position isn't what expected, set fixed pos, total_updates
5821 * avoids setting fixed_pos when window is set to weird locations
5823 /*if (total_updates >= 2 this is broken
5825 && (window.x != ev.xconfigure.x
5828 && (ev.xconfigure.x != 0
5829 || ev.xconfigure.y != 0)) {
5839 /* forward the click to the desktop window */
5840 XUngrabPointer(display, ev.xbutton.time);
5841 ev.xbutton.window = window.desktop;
5842 XSendEvent(display, ev.xbutton.window, False, ButtonPressMask, &ev);
5849 /* forward the release to the desktop window */
5850 ev.xbutton.window = window.desktop;
5851 XSendEvent(display, ev.xbutton.window, False, ButtonReleaseMask, &ev);
5859 if (ev.type == event_base + XDamageNotify) {
5860 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
5861 XFixesSetRegion(display, part, &dev->area, 1);
5862 XFixesUnionRegion(display, region2, region2, part);
5864 #endif /* HAVE_XDAMAGE */
5869 XDamageSubtract(display, damage, region2, None);
5870 XFixesSetRegion(display, region2, 0, 0);
5871 #endif /* HAVE_XDAMAGE */
5873 /* XDBE doesn't seem to provide a way to clear the back buffer without
5874 * interfering with the front buffer, other than passing XdbeBackground
5875 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
5876 * redraw the text even if it wasn't part of the exposed area. OTOH,
5877 * if we're not going to call draw_stuff at all, then no swap happens
5878 * and we can safely do nothing.
5881 if (!XEmptyRegion(region)) {
5885 r.x = text_start_x - border_margin;
5886 r.y = text_start_y - border_margin;
5887 r.width = text_width + border_margin * 2;
5888 r.height = text_height + border_margin * 2;
5889 XUnionRectWithRegion(&r, region, region);
5892 XSetRegion(display, window.gc, region);
5895 XftDrawSetClip(window.xftdraw, region);
5901 XDestroyRegion(region);
5902 region = XCreateRegion();
5906 #ifdef SIGNAL_BLOCKING
5907 /* unblock signals of interest and let handler fly */
5908 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
5909 CRIT_ERR("unable to sigprocmask()");
5912 switch(g_signal_pending) {
5915 ERR("received SIGUSR1. reloading the config file.");
5922 ERR("received SIGINT or SIGTERM to terminate. bye!");
5925 XDestroyRegion(region);
5928 return; /* return from main_loop */
5933 /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
5934 * but didn't write any code to deal with it. if you don't want to
5935 * handle a signal, don't set a handler on it in the first place. */
5936 if (g_signal_pending)
5937 ERR("ignoring signal (%d)", g_signal_pending);
5944 #if defined(X11) && defined(HAVE_XDAMAGE)
5945 XDamageDestroy(display, damage);
5946 XFixesDestroyRegion(display, region2);
5947 XFixesDestroyRegion(display, part);
5948 XDestroyRegion(region);
5950 #endif /* X11 && HAVE_XDAMAGE */
5953 static void load_config_file(const char *);
5955 /* reload the config file */
5956 void reload_config(void)
5958 //lock_all_threads();
5960 if (info.cpu_usage) {
5961 free(info.cpu_usage);
5962 info.cpu_usage = NULL;
5965 if ( (info.audacious.thread) && (destroy_audacious_thread()!=0) )
5966 ERR("error destroying audacious thread");
5968 #ifdef TCP_PORT_MONITOR
5969 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
5972 if (current_config) {
5974 load_config_file(current_config);
5978 XClearWindow(display, RootWindow(display, screen)); // clear the window first
5981 #ifdef TCP_PORT_MONITOR
5982 info.p_tcp_port_monitor_collection = NULL;
5985 if ( (!info.audacious.thread) && (create_audacious_thread() !=0) )
5986 CRIT_ERR("unable to create audacious thread!");
5988 extract_variable_text(text);
5998 //lock_all_threads();
6000 if (info.cpu_usage) {
6001 free(info.cpu_usage);
6002 info.cpu_usage = NULL;
6007 XdbeDeallocateBackBufferName(display, window.back_buffer);
6013 XDestroyWindow(display, window.window);
6014 XClearWindow(display, RootWindow(display, screen));
6020 XClearWindow(display, RootWindow(display, screen));
6025 XFreeGC(display, window.gc);
6029 /* it is really pointless to free() memory at the end of program but ak|ra
6030 * wants me to do this */
6032 free_text_objects(text_object_count, text_objects);
6033 text_object_count = 0;
6034 text_objects = NULL;
6036 if (text != original_text)
6039 free(current_config);
6040 free(current_mail_spool);
6041 #ifdef TCP_PORT_MONITOR
6042 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6043 info.p_tcp_port_monitor_collection = NULL;
6046 if ( info.audacious.thread && (destroy_audacious_thread()!=0) )
6047 ERR("error destroying audacious thread");
6051 static int string_to_bool(const char *s)
6055 if (strcasecmp(s, "yes") == 0)
6057 if (strcasecmp(s, "true") == 0)
6059 if (strcasecmp(s, "1") == 0)
6064 static enum alignment string_to_alignment(const char *s)
6066 if (strcasecmp(s, "top_left") == 0)
6068 else if (strcasecmp(s, "top_right") == 0)
6070 else if (strcasecmp(s, "bottom_left") == 0)
6072 else if (strcasecmp(s, "bottom_right") == 0)
6073 return BOTTOM_RIGHT;
6074 else if (strcasecmp(s, "tl") == 0)
6076 else if (strcasecmp(s, "tr") == 0)
6078 else if (strcasecmp(s, "bl") == 0)
6080 else if (strcasecmp(s, "br") == 0)
6081 return BOTTOM_RIGHT;
6082 else if (strcasecmp(s, "none") == 0)
6089 static void set_default_configurations(void)
6091 fork_to_background = 0;
6092 total_run_times = 0;
6093 info.cpu_avg_samples = 2;
6094 info.net_avg_samples = 2;
6099 strcpy(info.mpd.host, "localhost");
6100 info.mpd.port = 6600;
6101 info.mpd.status = NULL;
6102 info.mpd.artist = NULL;
6103 info.mpd.album = NULL;
6104 info.mpd.title = NULL;
6105 info.mpd.random = NULL;
6106 info.mpd.track = NULL;
6107 info.mpd.name = NULL;
6108 info.mpd.file = NULL;
6111 info.xmms2.artist = NULL;
6112 info.xmms2.album = NULL;
6113 info.xmms2.title = NULL;
6114 info.xmms2.genre = NULL;
6115 info.xmms2.comment = NULL;
6116 info.xmms2.decoder = NULL;
6117 info.xmms2.transport = NULL;
6118 info.xmms2.url = NULL;
6119 info.xmms2.status = NULL;
6128 default_fg_color = WhitePixel(display, screen);
6129 default_bg_color = BlackPixel(display, screen);
6130 default_out_color = BlackPixel(display, screen);
6133 draw_graph_borders = 1;
6135 set_first_font("6x10");
6143 window.type=TYPE_NORMAL;
6145 strcpy(window.wm_class_name, "conky");
6147 stippled_borders = 0;
6150 text_alignment = BOTTOM_LEFT;
6154 free(current_mail_spool);
6157 variable_substitute(MAIL_FILE, buf, 256);
6159 current_mail_spool = strdup(buf);
6163 update_interval = 10.0;
6164 stuff_in_upper_case = 0;
6166 #ifdef TCP_PORT_MONITOR
6167 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6168 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6172 static void load_config_file(const char *f)
6174 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6178 set_default_configurations();
6184 char buf[256], *p, *p2, *name, *value;
6186 if (fgets(buf, 256, fp) == NULL)
6191 /* break at comment */
6192 p2 = strchr(p, '#');
6197 while (*p && isspace((int) *p))
6200 continue; /* empty line */
6206 while (*p2 && !isspace((int) *p2))
6209 *p2 = '\0'; /* break at name's end */
6216 while (*p && isspace((int) *p))
6221 p2 = value + strlen(value);
6222 while (isspace((int) *(p2 - 1)))
6228 #define CONF2(a) if (strcasecmp(name, a) == 0)
6229 #define CONF(a) else CONF2(a)
6230 #define CONF3(a,b) \
6231 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
6235 CONF2("alignment") {
6237 int a = string_to_alignment(value);
6247 ERR("on_bottom is deprecated. use own_window_hints below");
6248 on_bottom = string_to_bool(value);
6250 SET_HINT(window.hints,HINT_BELOW);
6255 CONF("background") {
6256 fork_to_background = string_to_bool(value);
6260 CONF2("background") {
6261 fork_to_background = string_to_bool(value);
6265 CONF("border_margin") {
6267 border_margin = strtol(value, 0, 0);
6271 CONF("border_width") {
6273 border_width = strtol(value, 0, 0);
6277 CONF("default_color") {
6279 default_fg_color = get_x11_color(value);
6283 CONF3("default_shade_color", "default_shadecolor") {
6285 default_bg_color = get_x11_color(value);
6289 CONF3("default_outline_color", "default_outlinecolor") {
6291 default_out_color = get_x11_color(value);
6296 CONF("xmms_player") {
6298 ERR("xmms_player is deprecated. see ./configure --help");
6302 info.mail = parse_mail_args(IMAP, value);
6309 info.mail = parse_mail_args(POP3, value);
6317 strncpy(info.mpd.host, value, 127);
6323 info.mpd.port = strtol(value, 0, 0);
6324 if (info.mpd.port < 1
6325 || info.mpd.port > 0xffff)
6329 CONF("mpd_password") {
6331 strncpy(info.mpd.password, value, 127);
6336 CONF("cpu_avg_samples") {
6338 cpu_avg_samples = strtol(value, 0, 0);
6339 if (cpu_avg_samples < 1
6340 || cpu_avg_samples > 14)
6349 CONF("net_avg_samples") {
6351 net_avg_samples = strtol(value, 0, 0);
6352 if (net_avg_samples < 1
6353 || net_avg_samples > 14)
6369 CONF("double_buffer") {
6370 use_xdbe = string_to_bool(value);
6374 CONF("override_utf8_locale") {
6375 utf8_mode = string_to_bool(value);
6378 CONF("draw_borders") {
6379 draw_borders = string_to_bool(value);
6381 CONF("draw_graph_borders") {
6382 draw_graph_borders = string_to_bool(value);
6384 CONF("draw_shades") {
6385 draw_shades = string_to_bool(value);
6387 CONF("draw_outline") {
6388 draw_outline = string_to_bool(value);
6391 CONF("out_to_console") {
6392 out_to_console = string_to_bool(value);
6394 CONF("use_spacer") {
6395 use_spacer = string_to_bool(value);
6400 use_xft = string_to_bool(value);
6404 set_first_font(value);
6409 if (value && font_count >= 0)
6410 fonts[0].font_alpha = atof(value)
6419 if (string_to_bool(value))
6420 ERR("Xft not enabled");
6423 /* xftfont silently ignored when no Xft */
6426 /* xftalpha is silently ignored when no Xft */
6431 set_first_font(value);
6440 gap_x = atoi(value);
6446 gap_y = atoi(value);
6451 CONF("mail_spool") {
6454 variable_substitute(value, buf, 256);
6458 if (current_mail_spool)
6459 free(current_mail_spool);
6460 current_mail_spool = strdup(buf);
6466 CONF("minimum_size") {
6469 (value, "%d %d", &minimum_width,
6470 &minimum_height) != 2)
6473 &minimum_width) != 1)
6478 CONF("maximum_width") {
6480 if (sscanf(value, "%d", &maximum_width) != 1)
6486 CONF("no_buffers") {
6487 no_buffers = string_to_bool(value);
6489 CONF("pad_percents") {
6490 pad_percents = atoi(value);
6494 CONF("own_window") {
6495 own_window = string_to_bool(value);
6497 CONF("wm_class_name") {
6498 memset(window.wm_class_name,0,sizeof(window.wm_class_name));
6499 strncpy(window.wm_class_name, value, sizeof(window.wm_class_name)-1);
6501 CONF("own_window_transparent") {
6502 set_transparent = string_to_bool(value);
6504 CONF("own_window_colour") {
6506 background_colour = get_x11_color(value);
6508 ERR("Invalid colour for own_winder_colour (try omitting the '#' for hex colours");
6511 CONF("own_window_hints") {
6513 char *p_hint, *p_save;
6514 char delim[] = ", ";
6516 /* tokenize the value into individual hints */
6517 if ((p_hint=strtok_r(value, delim, &p_save)) != NULL)
6519 /*fprintf(stderr, "hint [%s] parsed\n", p_hint);*/
6520 if (strncmp(p_hint,"undecorate",10) == 0)
6521 SET_HINT(window.hints,HINT_UNDECORATED);
6522 else if (strncmp(p_hint,"below",5) == 0)
6523 SET_HINT(window.hints,HINT_BELOW);
6524 else if (strncmp(p_hint,"above",5) == 0)
6525 SET_HINT(window.hints,HINT_ABOVE);
6526 else if (strncmp(p_hint,"sticky",6) == 0)
6527 SET_HINT(window.hints,HINT_STICKY);
6528 else if (strncmp(p_hint,"skip_taskbar",12) == 0)
6529 SET_HINT(window.hints,HINT_SKIP_TASKBAR);
6530 else if (strncmp(p_hint,"skip_pager",10) == 0)
6531 SET_HINT(window.hints,HINT_SKIP_PAGER);
6535 p_hint=strtok_r(NULL, delim, &p_save);
6537 while (p_hint!=NULL);
6540 CONF("own_window_type") {
6542 if (strncmp(value,"normal",6)==0)
6543 window.type = TYPE_NORMAL;
6544 else if (strncmp(value,"desktop",7)==0)
6545 window.type = TYPE_DESKTOP;
6546 else if (strncmp(value,"override",8)==0)
6547 window.type = TYPE_OVERRIDE;
6553 CONF("stippled_borders") {
6555 stippled_borders = strtol(value, 0, 0);
6557 stippled_borders = 4;
6561 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
6564 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
6566 CONF("update_interval") {
6568 update_interval = strtod(value, 0);
6572 CONF("total_run_times") {
6574 total_run_times = strtod(value, 0);
6579 stuff_in_upper_case = string_to_bool(value);
6582 if (text != original_text)
6592 int l = strlen(text);
6593 if (fgets(buf, 256, fp) == NULL)
6596 realloc(text, l + strlen(buf)
6600 if (strlen(text) > 1024 * 8)
6604 text_lines = line + 1;
6607 #ifdef TCP_PORT_MONITOR
6608 CONF("min_port_monitors")
6611 (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) ||
6612 tcp_port_monitor_collection_args.min_port_monitors < 0 )
6614 /* an error. use default, warn and continue. */
6615 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6618 else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
6620 /* no error, just use default */
6621 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6623 /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
6625 CONF("min_port_monitor_connections")
6628 (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1)
6629 || tcp_port_monitor_args.min_port_monitor_connections < 0 )
6631 /* an error. use default, warni and continue. */
6632 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6635 else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
6637 /* no error, just use default */
6638 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6640 /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
6644 ERR("%s: %d: no such configuration: '%s'", f, line, name);
6656 /* : means that character before that takes an argument */
6657 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
6670 int main(int argc, char **argv)
6672 struct sigaction act, oact;
6675 memset(&info, 0, sizeof(info) );
6677 #ifdef TCP_PORT_MONITOR
6678 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6679 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6682 /* handle command line parameters that don't change configs */
6687 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
6688 *s) || ((s = getenv("LANG")) && *s)) {
6689 temp = (char *)malloc((strlen(s) + 1) * sizeof(char));
6691 ERR("malloc failed");
6693 for (x = 0; x < strlen(s); x++) {
6694 temp[x] = tolower(s[x]);
6696 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6702 if (!setlocale(LC_CTYPE, "")) {
6703 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6707 int c = getopt(argc,
6718 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
6719 * though free() does the NULL check itself;), then load optarg value */
6721 free(current_config);
6722 current_config = strdup(optarg);
6727 ("Usage: %s [OPTION]...\n"
6728 "Conky is a system monitor that renders text on desktop or to own transparent\n"
6729 "window. Command line options will override configurations defined in config\n"
6732 " -c FILE config file to load instead of "
6735 " -d daemonize, fork to background\n"
6738 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
6739 " -f FONT font to use\n"
6741 " -o create own window to draw\n"
6744 " -b double buffer (prevents flickering)\n"
6746 " -w WIN_ID window id to draw\n"
6747 " -x X x position\n"
6748 " -y Y y position\n"
6750 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
6751 " -u SECS update interval\n"
6752 " -i NUM number of times to update Conky\n", argv[0]);
6756 window.window = strtol(optarg, 0, 0);
6765 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
6769 /* load current_config or CONFIG_FILE */
6772 if (current_config == NULL) {
6773 /* load default config file */
6776 variable_substitute(CONFIG_FILE, buf, 256);
6779 current_config = strdup(buf);
6783 if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
6784 load_config_file(current_config);
6786 set_default_configurations();
6790 if (current_mail_spool == NULL) {
6792 variable_substitute(MAIL_FILE, buf, 256);
6795 current_mail_spool = strdup(buf);
6799 /* handle other command line arguments */
6801 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
6802 optind = optreset = 1;
6807 #if defined(__FreeBSD__)
6808 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
6809 O_RDONLY, "kvm_open")) == NULL)
6810 CRIT_ERR( "cannot read kvm");
6814 int c = getopt(argc,
6822 fork_to_background = 1;
6827 set_first_font(optarg);
6830 text_alignment = string_to_alignment(optarg);
6845 if (text != original_text)
6847 text = strdup(optarg);
6848 convert_escapes(text);
6852 update_interval = strtod(optarg, 0);
6856 total_run_times = strtod(optarg, 0);
6860 gap_x = atoi(optarg);
6864 gap_y = atoi(optarg);
6878 /* generate text and get initial size */
6879 extract_variable_text(text);
6880 if (text != original_text) {
6885 if (fork_to_background) {
6889 ERR("can't fork() to background: %s",
6896 fprintf(stderr,"\n");fflush(stderr);
6900 /* parent process */
6901 fprintf(stderr,"Conky: forked to background, pid is %d\n",pid);
6912 update_text_area(); /* to get initial size of the window */
6916 text_width + border_margin * 2 + 1,
6917 text_height + border_margin * 2 + 1,
6918 set_transparent, background_colour, info.uname_s.nodename, argv, argc);
6921 update_text_area(); /* to position text/window on screen */
6925 // why the fuck not?
6931 if (own_window && !fixed_pos) {
6932 XMoveWindow(display, window.window, window.x, window.y);
6935 set_transparent_background(window.window);
6946 /* Set signal handlers */
6947 act.sa_handler = signal_handler;
6948 sigemptyset(&act.sa_mask);
6951 act.sa_flags |= SA_RESTART;
6954 if ( sigaction(SIGINT,&act,&oact) < 0 ||
6955 sigaction(SIGUSR1,&act,&oact) < 0 ||
6956 sigaction(SIGTERM,&act,&oact) < 0 )
6958 ERR("error setting signal handler: %s", strerror(errno) );
6962 if ( (create_audacious_thread() !=0) )
6964 CRIT_ERR("unable to create audacious thread!");
6971 if ( info.audacious.thread && (destroy_audacious_thread()!=0) )
6972 ERR("error destroying audacious thread");
6975 #if defined(__FreeBSD__)
6982 void signal_handler(int sig)
6984 /* signal handler is light as a feather, as it should be.
6985 * we will poll g_signal_pending with each loop of conky
6986 * and do any signal processing there, NOT here. */
6987 g_signal_pending=sig;