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"
63 " * Xdamage extension\n"
64 # endif /* HAVE_XDAMAGE */
66 " * Xdbe extension (double buffer)\n"
67 # endif /* HAVE_XDBE */
73 "\n Music detection:\n"
76 #endif /* AUDACIOUS */
86 "\n General features:\n"
90 #ifdef TCP_PORT_MONITOR
92 #endif /* TCP_PORT_MONITOR */
104 static int text_start_x, text_start_y; /* text start position in window */
105 static int text_width, text_height;
120 char name[TEXT_BUFFER_SIZE];
130 static int selected_font = 0;
131 static int font_count = -1;
132 struct font_list *fonts = NULL;
136 #define font_height() (use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
137 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent))
138 #define font_ascent() (use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent)
139 #define font_descent() (use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent)
143 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
144 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
145 #define font_descent() fonts[selected_font].font->max_bounds.descent
149 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
152 static void set_font();
154 int addfont(const char *data_in)
156 if (font_count > MAX_FONTS) {
157 CRIT_ERR("you don't need that many fonts, sorry.");
160 if (font_count == 0) {
164 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
168 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
170 CRIT_ERR("realloc in addfont");
172 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
173 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
175 fonts[font_count].font_alpha = 0xffff;
178 CRIT_ERR("Oops...looks like something overflowed in addfont().");
183 void set_first_font(const char *data_in)
185 if (font_count < 0) {
186 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
191 if (strlen(data_in) > 1) {
192 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
194 fonts[0].font_alpha = 0xffff;
202 for (i=0;i<=font_count;i++) {
205 XftFontClose(display, fonts[i].xftfont);
209 XFreeFont(display, fonts[i].font);
216 set_first_font("6x10");
220 static void load_fonts()
223 for (i=0; i <= font_count; i++) {
227 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
228 XftFontClose(display, fonts[i].xftfont);
230 if ((fonts[i].xftfont =
231 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
234 ERR("can't load Xft font '%s'", fonts[i].name);
235 if ((fonts[i].xftfont =
236 XftFontOpenName(display, screen,
237 "courier-12")) != NULL)
240 ERR("can't load Xft font '%s'", "courier-12");
242 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
243 CRIT_ERR("can't load font '%s'", "fixed");
250 /* load normal font */
251 /* if (fonts[i].font != NULL)
252 XFreeFont(display, fonts[i].font);*/
254 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
255 ERR("can't load font '%s'", fonts[i].name);
256 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
257 CRIT_ERR("can't load font '%s'", "fixed");
258 printf("loaded fixed?\n");
266 /* default config file */
267 static char *current_config;
269 /* set to 1 if you want all text to be in uppercase */
270 static unsigned int stuff_in_upper_case;
272 /* Update interval */
273 static double update_interval;
275 /* Run how many times? */
276 static unsigned long total_run_times;
279 static int fork_to_background;
281 static int cpu_avg_samples, net_avg_samples;
285 /* Always on bottom */
286 static int on_bottom;
288 /* Position on the screen */
289 static int text_alignment;
290 static int gap_x, gap_y;
293 static int draw_borders;
294 static int draw_graph_borders;
295 static int stippled_borders;
297 static int draw_shades, draw_outline;
299 static int border_margin, border_width;
301 static long default_fg_color, default_bg_color, default_out_color;
303 /* create own window or draw stuff to root? */
304 static int set_transparent = 0;
308 static int own_window = 0;
309 static int background_colour = 0;
310 /* fixed size/pos is set if wm/user changes them */
311 static int fixed_size = 0, fixed_pos = 0;
314 static int minimum_width, minimum_height;
315 static int maximum_width;
320 #define CODEPAGE_LENGTH 20
323 char iconv_converting;
324 static iconv_t **iconv_cd = 0;
326 int register_iconv(iconv_t *new_iconv)
329 iconv_cd = realloc(iconv, sizeof(iconv_t *) * (iconv_count + 1));
331 iconv_cd = malloc(sizeof(iconv_t *));
334 CRIT_ERR("Out of memory");
336 iconv_cd[iconv_count] = malloc(sizeof(iconv_t));
337 if (!iconv_cd[iconv_count]) {
338 CRIT_ERR("Out of memory");
340 memcpy(iconv_cd[iconv_count], new_iconv, sizeof(iconv_t));
345 void free_iconv(void)
349 for (i = iconv_count; i < 0; i++) {
365 /* no buffers in used memory? */
368 /* pad percentages to decimals? */
369 static int pad_percents = 0;
371 #ifdef TCP_PORT_MONITOR
372 tcp_port_monitor_collection_args_t tcp_port_monitor_collection_args;
373 tcp_port_monitor_args_t tcp_port_monitor_args;
376 /* Text that is shown */
377 static char original_text[] =
378 "$nodename - $sysname $kernel on $machine\n"
380 "${color grey}Uptime:$color $uptime\n"
381 "${color grey}Frequency (in MHz):$color $freq\n"
382 "${color grey}Frequency (in GHz):$color $freq_g\n"
383 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
384 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
385 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
386 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
388 "${color grey}File systems:\n"
389 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
390 "${color grey}Networking:\n"
391 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
394 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
395 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
398 "${color grey}XMMS2: $xmms2_status $xmms2_artist - $xmms2_title from $xmms2_album\n"
399 "Progress: $xmms2_bar\n"
401 "${color grey}Name PID CPU% MEM%\n"
402 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
403 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
404 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
405 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
406 "${tail /var/log/Xorg.0.log 3}";
408 static char *text = original_text;
411 static int total_updates;
414 static int blockdepth = 0;
415 static int if_jumped = 0;
416 static int blockstart[MAX_IF_BLOCK_DEPTH];
418 int check_mount(char *s)
420 #if defined(__linux__)
422 FILE *mtab = fopen("/etc/mtab", "r");
424 char buf1[256], buf2[128];
425 while (fgets(buf1, 256, mtab)) {
426 sscanf(buf1, "%*s %128s", buf2);
427 if (!strcmp(s, buf2)) {
434 ERR("Could not open mtab");
437 #elif defined(__FreeBSD__)
438 struct statfs *mntbuf;
441 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
442 for (i = mntsize - 1; i >= 0; i--)
443 if (strcmp(mntbuf[i].f_mntonname, s) == 0)
452 static inline int calc_text_width(const char *s, int l)
458 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
460 XftTextExtents8(display, fonts[selected_font].xftfont, (FcChar8 *)s, l, &gi);
466 return XTextWidth(fonts[selected_font].font, s, l);
471 /* formatted text to render on screen, generated in generate_text(),
472 * drawn in draw_stuff() */
474 static char text_buffer[TEXT_BUFFER_SIZE * 4];
476 /* special stuff in text_buffer */
478 #define SPECIAL_CHAR '\x01'
497 static struct special_t {
507 unsigned long first_colour; // for graph gradient
508 unsigned long last_colour;
511 static int special_count;
513 static int special_index; /* used when drawing */
516 #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? */
518 static struct special_t *new_special(char *buf, int t)
520 if (special_count >= 512)
521 CRIT_ERR("too many special things in text");
523 buf[0] = SPECIAL_CHAR;
525 specials[special_count].type = t;
526 return &specials[special_count++];
529 typedef struct tailstring_list {
530 char data[TEXT_BUFFER_SIZE];
531 struct tailstring_list *next;
532 struct tailstring_list *first;
535 void addtail(tailstring ** head, char *data_in)
538 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
544 tmp->first = (*head)->first;
546 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
551 void freetail(tailstring * head)
554 while (head != NULL) {
561 void freelasttail(tailstring * head)
563 tailstring * tmp = head;
565 if (tmp->next == head->first) {
572 while(head != NULL && tmp != NULL) {
578 static void new_bar(char *buf, int w, int h, int usage)
580 struct special_t *s = new_special(buf, BAR);
581 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
586 static const char *scan_bar(const char *args, int *w, int *h)
588 *w = 0; /* zero width means all space that is available */
590 /* bar's argument is either height or height,width */
593 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
594 sscanf(args, "%d %n", h, &n);
601 static char *scan_font(const char *args)
603 if (args && sizeof(args) < 127) {
610 static void new_font(char *buf, char * args) {
612 struct special_t *s = new_special(buf, FONT);
613 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
614 int tmp = selected_font;
615 selected_font = s->font_added = addfont(args);
620 struct special_t *s = new_special(buf, FONT);
621 int tmp = selected_font;
622 selected_font = s->font_added = 0;
629 inline void graph_append(struct special_t *graph, double f)
631 if (!graph->scaled && f > graph->graph_scale) {
632 f = graph->graph_scale;
636 graph->graph_scale = 1;
638 graph->graph[0] = f; /* add new data */
639 for (i = graph->graph_width - 1; i > 0; i--) { /* shift all the data by 1 */
640 graph->graph[i] = graph->graph[i - 1];
641 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
642 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
647 short colour_depth = 0;
648 void set_up_gradient();
650 /* precalculated: 31/255, and 63/255 */
651 #define CONST_8_TO_5_BITS 0.12156862745098
652 #define CONST_8_TO_6_BITS 0.247058823529412
654 /* adjust color values depending on color depth*/
655 static unsigned int adjust_colors(unsigned int color)
658 if (colour_depth == 0) {
661 if (colour_depth == 16) {
662 r = (color & 0xff0000) >> 16;
663 g = (color & 0xff00) >> 8;
665 color = (int)(r * CONST_8_TO_5_BITS) << 11;
666 color |= (int)(g * CONST_8_TO_6_BITS) << 5;
667 color |= (int)(b * CONST_8_TO_5_BITS);
672 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scale, int append)
674 struct special_t *s = new_special(buf, GRAPH);
676 if (s->graph == NULL) {
677 if (s->width > 0 && s->width < MAX_GRAPH_DEPTH) {
678 s->graph_width = s->width/* - 2*/; // subtract 2 for the box
680 s->graph_width = MAX_GRAPH_DEPTH - 2;
682 s->graph = malloc(s->graph_width * sizeof(double));
683 memset(s->graph, 0, s->graph_width * sizeof(double));
684 s->graph_scale = 100;
687 s->first_colour = adjust_colors(first_colour);
688 s->last_colour = adjust_colors(second_colour);
695 s->graph_width = s->width - 2; // subtract 2 for rectangle around
700 s->graph_scale = scale;
707 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour, unsigned int *scale)
709 *w = 0; /* zero width means all space that is available */
713 /* graph's argument is either height or height,width */
715 if (sscanf(args, "%*s %d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
716 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
718 if (sscanf(args, "%d,%d %x %x %i", h, w, first_colour, last_colour, scale) < 5) {
720 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
723 if (sscanf(args, "%*s %x %x %i", first_colour, last_colour, scale) < 3) {
727 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
730 if (sscanf(args, "%x %x %i", first_colour, last_colour, scale) < 3) {
734 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
737 if (sscanf(args, "%d,%d %i", h, w, scale) < 3) {
741 if (sscanf(args, "%d,%d", h, w) < 2) {
744 sscanf(args, "%*s %d,%d", h, w);
750 static inline void new_hr(char *buf, int a)
752 new_special(buf, HORIZONTAL_LINE)->height = a;
755 static inline void new_stippled_hr(char *buf, int a, int b)
757 struct special_t *s = new_special(buf, STIPPLED_HR);
762 static inline void new_fg(char *buf, long c)
764 new_special(buf, FG)->arg = c;
767 static inline void new_bg(char *buf, long c)
769 new_special(buf, BG)->arg = c;
772 static inline void new_outline(char *buf, long c)
774 new_special(buf, OUTLINE)->arg = c;
777 static inline void new_offset(char *buf, long c)
779 new_special(buf, OFFSET)->arg = c;
782 static inline void new_voffset(char *buf, long c)
784 new_special(buf, VOFFSET)->arg = c;
787 static inline void new_alignr(char *buf, long c)
789 new_special(buf, ALIGNR)->arg = c;
792 static inline void new_alignc(char *buf, long c)
794 new_special(buf, ALIGNC)->arg = c;
797 static inline void new_goto(char *buf, long c)
799 new_special(buf, GOTO)->arg = c;
802 static inline void new_tab(char *buf, int a, int b) {
803 struct special_t *s = new_special(buf, TAB);
808 /* quite boring functions */
810 static inline void for_each_line(char *b, void (*f) (char *))
814 for (ps = b, pe = b; *pe; pe++) {
827 static void convert_escapes(char *buf)
829 char *p = buf, *s = buf;
845 /* converts from bytes to human readable format (k, M, G, T) */
846 static void human_readable(long long a, char *buf, int size)
848 // Strange conditional due to possible overflows
849 if(a / 1024 / 1024 / 1024.0 > 1024.0){
850 snprintf(buf, size, "%.2fTiB", (a / 1024 / 1024 / 1024) / 1024.0);
852 else if (a >= 1024 * 1024 * 1024) {
853 snprintf(buf, size, "%.2fGiB", (a / 1024 / 1024) / 1024.0);
855 else if (a >= 1024 * 1024) {
856 double m = (a / 1024) / 1024.0;
858 snprintf(buf, size, "%.0fMiB", m);
860 snprintf(buf, size, "%.1fMiB", m);
861 } else if (a >= 1024)
862 snprintf(buf, size, "%LdKiB", a / (long long) 1024);
864 snprintf(buf, size, "%LdB", a);
869 enum text_object_type {
920 #if defined(__linux__)
926 OBJ_i8k_left_fan_status,
927 OBJ_i8k_right_fan_status,
928 OBJ_i8k_left_fan_rpm,
929 OBJ_i8k_right_fan_rpm,
931 OBJ_i8k_buttons_status,
939 #endif /* __linux__ */
966 OBJ_running_processes,
975 OBJ_temp1, /* i2c is used instead in these */
995 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
997 OBJ_apm_battery_time,
998 OBJ_apm_battery_life,
999 #endif /* __FreeBSD__ */
1028 OBJ_xmms2_transport,
1043 OBJ_audacious_status,
1044 OBJ_audacious_title,
1045 OBJ_audacious_length,
1046 OBJ_audacious_length_seconds,
1047 OBJ_audacious_position,
1048 OBJ_audacious_position_seconds,
1049 OBJ_audacious_bitrate,
1050 OBJ_audacious_frequency,
1051 OBJ_audacious_channels,
1052 OBJ_audacious_filename,
1053 OBJ_audacious_playlist_length,
1054 OBJ_audacious_playlist_position,
1065 #ifdef TCP_PORT_MONITOR
1078 struct text_object {
1082 unsigned int c, d, e;
1086 char *s; /* some string */
1087 int i; /* some integer */
1088 long l; /* some other integer */
1089 unsigned int sensor;
1090 struct net_stat *net;
1092 unsigned char loadavg[3];
1093 unsigned int cpu_index;
1094 struct mail_s *mail;
1097 char *tz; /* timezone variable */
1098 char *fmt; /* time display formatting */
1142 struct thread_info_s thread_info;
1148 #ifdef TCP_PORT_MONITOR
1150 in_port_t port_range_begin; /* starting port to monitor */
1151 in_port_t port_range_end; /* ending port to monitor */
1152 int item; /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
1153 int connection_index; /* 0 to n-1 connections. */
1166 struct text_object_list {
1167 unsigned int text_object_count;
1168 struct text_object *text_objects;
1171 static unsigned int text_object_count;
1172 static struct text_object *text_objects;
1173 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
1175 #define MAX_THREADS 512 // sure whatever
1176 typedef struct thread_info_s *thread_info;
1177 static thread_info thread_list[MAX_THREADS];
1178 static int thread_count = 0;
1179 static int threads_runnable = 1;
1181 int register_thread(struct thread_info_s *new_thread)
1183 if (thread_count >= MAX_THREADS) {
1184 CRIT_ERR("Uh oh, tried to register too many threads");
1186 thread_list[thread_count] = new_thread;
1188 // may as well fix the mutex for them as well
1189 pthread_mutex_init(&(new_thread->mutex), NULL);
1191 return thread_count - 1;
1194 #define MAXDATASIZE 1000
1198 struct mail_s* parse_mail_args(char type, const char *arg) {
1199 struct mail_s *mail;
1200 mail = malloc(sizeof(struct mail_s));
1201 memset(mail, 0, sizeof(struct mail_s));
1203 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass) != 3) {
1205 ERR("Scanning IMAP args failed");
1206 } else if (type == IMAP) {
1207 ERR("Scanning POP3 args failed");
1210 // see if password needs prompting
1211 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1212 int fp = fileno(stdin);
1213 struct termios term;
1214 tcgetattr(fp, &term);
1215 term.c_lflag &= ~ECHO;
1216 tcsetattr(fp, TCSANOW, &term);
1217 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1218 scanf("%128s", mail->pass);
1220 term.c_lflag |= ECHO;
1221 tcsetattr(fp, TCSANOW, &term);
1223 // now we check for optional args
1224 tmp = strstr(arg, "-i ");
1227 sscanf(tmp, "%f", &mail->interval);
1229 mail->interval = 300; // 5 minutes
1231 tmp = strstr(arg, "-p ");
1234 sscanf(tmp, "%lu", &mail->port);
1237 mail->port = 110; // default pop3 port
1238 } else if (type == IMAP) {
1239 mail->port = 143; // default imap port
1243 tmp = strstr(arg, "-f ");
1246 sscanf(tmp, "%s", mail->folder);
1248 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1251 tmp = strstr(arg, "-e ");
1255 if (tmp[0] == '\'') {
1256 len = strstr(tmp+1, "'") - tmp - 1;
1261 strncpy(mail->command, tmp+1, len);
1263 mail->command[0] = '\0';
1269 void *imap_thread(struct mail_s* mail)
1270 { // pthreads are really beginning to piss me off
1272 int run_code = threads_runnable;
1273 update_time = get_time();
1274 int sockfd, numbytes;
1275 char recvbuf[MAXDATASIZE];
1276 char sendbuf[MAXDATASIZE];
1279 unsigned int old_unseen = UINT_MAX;
1280 unsigned int old_messages = UINT_MAX;
1282 struct sockaddr_in their_addr; // connector's address information
1283 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1284 herror("gethostbyname");
1287 while (threads_runnable == run_code && fail < 5) {
1289 ERR("Trying IMAP connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1290 sleep((int)mail->interval);
1292 update_time = get_time();
1293 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1299 their_addr.sin_family = AF_INET; // host byte order
1300 their_addr.sin_port = htons(mail->port); // short, network byte order
1301 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1302 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1305 (sockfd, (struct sockaddr *) &their_addr,
1306 sizeof(struct sockaddr)) == -1) {
1311 struct timeval timeout;
1314 timeout.tv_sec = 60; // 60 second timeout i guess
1315 timeout.tv_usec = 0;
1317 FD_SET(sockfd, &fdset);
1318 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1321 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1328 ERR("IMAP connection failed: timeout");
1332 recvbuf[numbytes] = '\0';
1333 if (strstr(recvbuf, "* OK") != recvbuf) {
1334 ERR("IMAP connection failed, probably not an IMAP server");
1338 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1339 strncat(sendbuf, mail->user,
1340 MAXDATASIZE - strlen(sendbuf) - 1);
1341 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1342 strncat(sendbuf, mail->pass,
1343 MAXDATASIZE - strlen(sendbuf) - 1);
1344 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1345 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1350 timeout.tv_sec = 60; // 60 second timeout i guess
1351 timeout.tv_usec = 0;
1353 FD_SET(sockfd, &fdset);
1354 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1357 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1364 recvbuf[numbytes] = '\0';
1365 if (strstr(recvbuf, "a1 OK") == NULL) {
1366 ERR("IMAP server login failed: %s", recvbuf);
1370 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1371 strncat(sendbuf, mail->folder,
1372 MAXDATASIZE - strlen(sendbuf) - 1);
1373 strncat(sendbuf, " (MESSAGES UNSEEN)\n",
1374 MAXDATASIZE - strlen(sendbuf) - 1);
1375 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1380 timeout.tv_sec = 60; // 60 second timeout i guess
1381 timeout.tv_usec = 0;
1383 FD_SET(sockfd, &fdset);
1384 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1387 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1394 recvbuf[numbytes] = '\0';
1395 if (strstr(recvbuf, "a2 OK") == NULL) {
1396 ERR("IMAP status failed: %s", recvbuf);
1400 // now we get the data
1401 reply = strstr(recvbuf, " (MESSAGES ");
1403 *strchr(reply, ')') = '\0';
1404 if (reply == NULL) {
1405 ERR("Error parsing IMAP response: %s", recvbuf);
1409 pthread_mutex_lock(&(mail->thread_info.mutex));
1410 sscanf(reply, "MESSAGES %lu UNSEEN %lu",
1413 pthread_mutex_unlock(&(mail->thread_info.mutex));
1415 strncpy(sendbuf, "a3 logout\n", MAXDATASIZE);
1416 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1421 timeout.tv_sec = 60; // 60 second timeout i guess
1422 timeout.tv_usec = 0;
1424 FD_SET(sockfd, &fdset);
1425 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1428 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1435 recvbuf[numbytes] = '\0';
1436 if (strstr(recvbuf, "a3 OK") == NULL) {
1437 ERR("IMAP logout failed: %s", recvbuf);
1442 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen || (mail->messages > old_messages && mail->unseen > 0))) { // new mail goodie
1443 if (system(mail->command) == -1) {
1448 old_unseen = mail->unseen;
1449 old_messages = mail->messages;
1450 mail->last_update = update_time;
1451 usleep(100); // prevent race condition
1452 if (get_time() - mail->last_update >
1456 unsigned int delay =
1457 1000000.0 * (mail->interval -
1459 mail->last_update));
1460 if (delay < update_interval * 500000) {
1461 delay = update_interval * 1000000;
1466 ERR("exiting imap thread");
1471 void *pop3_thread(struct mail_s *mail)
1472 { // pthreads are really beginning to piss me off
1474 int run_code = threads_runnable;
1475 update_time = get_time();
1476 int sockfd, numbytes;
1477 char recvbuf[MAXDATASIZE];
1478 char sendbuf[MAXDATASIZE];
1481 unsigned int old_unseen = UINT_MAX;
1483 struct sockaddr_in their_addr; // connector's address information
1484 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1485 herror("gethostbyname");
1488 while (threads_runnable == run_code && fail < 5) {
1490 ERR("Trying POP3 connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1491 sleep((int)mail->interval);
1493 update_time = get_time();
1494 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1500 their_addr.sin_family = AF_INET; // host byte order
1501 their_addr.sin_port = htons(mail->port); // short, network byte order
1502 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1503 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1506 (sockfd, (struct sockaddr *) &their_addr,
1507 sizeof(struct sockaddr)) == -1) {
1512 struct timeval timeout;
1515 timeout.tv_sec = 60; // 60 second timeout i guess
1516 timeout.tv_usec = 0;
1518 FD_SET(sockfd, &fdset);
1519 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1522 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1529 ERR("POP3 connection failed: timeout\n");
1533 recvbuf[numbytes] = '\0';
1534 if (strstr(recvbuf, "+OK ") != recvbuf) {
1535 ERR("POP3 connection failed, probably not a POP3 server");
1539 strncpy(sendbuf, "USER ", MAXDATASIZE);
1540 strncat(sendbuf, mail->user,
1541 MAXDATASIZE - strlen(sendbuf) - 1);
1542 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1543 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1544 perror("send USER");
1548 timeout.tv_sec = 60; // 60 second timeout i guess
1549 timeout.tv_usec = 0;
1551 FD_SET(sockfd, &fdset);
1552 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1555 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1557 perror("recv USER");
1562 recvbuf[numbytes] = '\0';
1563 if (strstr(recvbuf, "+OK ") == NULL) {
1564 ERR("POP3 server login failed: %s", recvbuf);
1568 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1569 strncat(sendbuf, mail->pass,
1570 MAXDATASIZE - strlen(sendbuf) - 1);
1571 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1572 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1573 perror("send PASS");
1577 timeout.tv_sec = 60; // 60 second timeout i guess
1578 timeout.tv_usec = 0;
1580 FD_SET(sockfd, &fdset);
1581 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1584 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1586 perror("recv PASS");
1591 recvbuf[numbytes] = '\0';
1592 if (strstr(recvbuf, "+OK ") == NULL) {
1593 ERR("POP3 server login failed: %s", recvbuf);
1597 strncpy(sendbuf, "STAT\n", MAXDATASIZE);
1598 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1599 perror("send STAT");
1603 timeout.tv_sec = 60; // 60 second timeout i guess
1604 timeout.tv_usec = 0;
1606 FD_SET(sockfd, &fdset);
1607 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1610 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1612 perror("recv STAT");
1617 recvbuf[numbytes] = '\0';
1618 if (strstr(recvbuf, "+OK ") == NULL) {
1619 ERR("POP3 status failed: %s", recvbuf);
1623 // now we get the data
1624 reply = recvbuf + 4;
1625 if (reply == NULL) {
1626 ERR("Error parsing POP3 response: %s", recvbuf);
1630 pthread_mutex_lock(&(mail->thread_info.mutex));
1631 sscanf(reply, "%lu %lu", &mail->unseen,
1634 pthread_mutex_unlock(&(mail->thread_info.mutex));
1636 strncpy(sendbuf, "QUIT\n", MAXDATASIZE);
1637 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1638 perror("send QUIT");
1642 timeout.tv_sec = 60; // 60 second timeout i guess
1643 timeout.tv_usec = 0;
1645 FD_SET(sockfd, &fdset);
1646 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1649 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1651 perror("recv QUIT");
1656 recvbuf[numbytes] = '\0';
1657 if (strstr(recvbuf, "+OK") == NULL) {
1658 ERR("POP3 logout failed: %s", recvbuf);
1663 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) { // new mail goodie
1664 if (system(mail->command) == -1) {
1669 old_unseen = mail->unseen;
1670 mail->last_update = update_time;
1671 usleep(100); // prevent race condition
1672 if (get_time() - mail->last_update >
1676 unsigned int delay =
1677 1000000.0 * (mail->interval -
1679 mail->last_update));
1680 if (delay < update_interval * 500000) {
1681 delay = update_interval * 1000000;
1686 ERR("exiting pop3 thread");
1692 void *threaded_exec(struct text_object *obj) { // pthreads are really beginning to piss me off
1694 int run_code = threads_runnable;
1695 while (threads_runnable == run_code) {
1696 update_time = get_time();
1697 char *p2 = obj->data.execi.buffer;
1698 FILE *fp = popen(obj->data.execi.cmd,"r");
1699 pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
1700 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
1703 if (n2 && p2[n2 - 1] == '\n') {
1707 if (*p2 == '\001') {
1712 pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
1713 obj->data.execi.last_update = update_time;
1714 usleep(100); // prevent race condition
1715 if (get_time() - obj->data.execi.last_update > obj->data.execi.interval) {
1718 unsigned int delay = 1000000.0 * (obj->data.execi.interval -(get_time() - obj->data.execi.last_update));
1719 if (delay < update_interval * 500000) {
1720 delay = update_interval * 1000000;
1725 ERR("exiting thread");
1730 static struct text_object *new_text_object_internal()
1732 struct text_object *obj = malloc(sizeof(struct text_object));
1733 memset(obj, 0, sizeof(struct text_object));
1737 static void free_text_objects(unsigned int count, struct text_object *objs)
1740 for (i = 0; i < count; i++) {
1741 switch (objs[i].type) {
1743 close(objs[i].data.i);
1746 close(objs[i].data.i);
1749 close(objs[i].data.i2c.fd);
1752 free(objs[i].data.s);
1755 free(objs[i].data.s);
1758 free(objs[i].data.tztime.tz);
1759 free(objs[i].data.tztime.fmt);
1764 case OBJ_imap_unseen:
1765 if (!objs[i].global_mode) {
1766 free(objs[i].data.mail);
1769 case OBJ_imap_messages:
1770 if (!objs[i].global_mode) {
1771 free(objs[i].data.mail);
1777 case OBJ_pop3_unseen:
1778 if (!objs[i].global_mode) {
1779 free(objs[i].data.mail);
1783 if (!objs[i].global_mode) {
1784 free(objs[i].data.mail);
1787 case OBJ_if_existing:
1788 case OBJ_if_mounted:
1789 case OBJ_if_running:
1790 free(objs[i].data.ifblock.s);
1793 free(objs[i].data.tail.logfile);
1794 free(objs[i].data.tail.buffer);
1796 case OBJ_text: case OBJ_font:
1797 free(objs[i].data.s);
1800 free(objs[i].data.s);
1803 free(objs[i].data.s);
1806 free(objs[i].data.s);
1809 free(objs[i].data.s);
1811 /* case OBJ_execibar:
1812 free(objs[i].data.s);
1814 case OBJ_execigraph:
1815 free(objs[i].data.s);
1818 case OBJ_iconv_start:
1824 if (info.mpd.title) {
1825 free(info.mpd.title);
1829 case OBJ_mpd_artist:
1830 if (info.mpd.artist) {
1831 free(info.mpd.artist);
1832 info.mpd.artist = 0;
1836 if (info.mpd.album) {
1837 free(info.mpd.album);
1841 case OBJ_mpd_random:
1842 if (info.mpd.random) {
1843 free(info.mpd.random);
1844 info.mpd.random = 0;
1847 case OBJ_mpd_repeat:
1848 if (info.mpd.repeat) {
1849 free(info.mpd.repeat);
1850 info.mpd.repeat = 0;
1854 if (info.mpd.track) {
1855 free(info.mpd.track);
1860 if (info.mpd.name) {
1861 free(info.mpd.name);
1866 if (info.mpd.file) {
1867 free(info.mpd.file);
1871 case OBJ_mpd_status:
1872 if (info.mpd.status) {
1873 free(info.mpd.status);
1874 info.mpd.status = 0;
1878 if (info.mpd.artist) {
1879 free(info.mpd.artist);
1880 info.mpd.artist = 0;
1882 if (info.mpd.title) {
1883 free(info.mpd.title);
1886 if (info.mpd.file) {
1887 free(info.mpd.file);
1894 case OBJ_xmms2_artist:
1895 if (info.xmms2.artist) {
1896 free(info.xmms2.artist);
1897 info.xmms2.artist = 0;
1900 case OBJ_xmms2_album:
1901 if (info.xmms2.album) {
1902 free(info.xmms2.album);
1903 info.xmms2.album = 0;
1906 case OBJ_xmms2_title:
1907 if (info.xmms2.title) {
1908 free(info.xmms2.title);
1909 info.xmms2.title = 0;
1912 case OBJ_xmms2_genre:
1913 if (info.xmms2.genre) {
1914 free(info.xmms2.genre);
1915 info.xmms2.genre = 0;
1918 case OBJ_xmms2_comment:
1919 if (info.xmms2.comment) {
1920 free(info.xmms2.comment);
1921 info.xmms2.comment = 0;
1924 case OBJ_xmms2_decoder:
1925 if (info.xmms2.decoder) {
1926 free(info.xmms2.decoder);
1930 case OBJ_xmms2_transport:
1931 if (info.xmms2.transport) {
1932 free(info.xmms2.transport);
1937 if (info.xmms2.url) {
1938 free(info.xmms2.url);
1942 case OBJ_xmms2_date:
1943 if (info.xmms2.date) {
1944 free(info.xmms2.date);
1945 info.xmms2.date = 0;
1948 case OBJ_xmms2_status:
1949 if (info.xmms2.status) {
1950 free(info.xmms2.status);
1951 info.xmms2.status = 0;
1954 case OBJ_xmms2_smart:
1955 if (info.xmms2.artist) {
1956 free(info.xmms2.artist);
1957 info.xmms2.artist = 0;
1959 if (info.xmms2.title) {
1960 free(info.xmms2.title);
1961 info.xmms2.title = 0;
1963 if (info.xmms2.url) {
1964 free(info.xmms2.url);
1970 case OBJ_bmpx_title:
1971 case OBJ_bmpx_artist:
1972 case OBJ_bmpx_album:
1973 case OBJ_bmpx_track:
1975 case OBJ_bmpx_bitrate:
1979 free(objs[i].data.s);
1983 free(objs[i].data.execi.cmd);
1984 free(objs[i].data.execi.buffer);
1987 free(objs[i].data.execi.cmd);
1988 free(objs[i].data.execi.buffer);
1991 if (info.first_process) {
1992 free_all_processes();
1993 info.first_process = NULL;
1997 if (info.first_process) {
1998 free_all_processes();
1999 info.first_process = NULL;
2004 free(objs[i].data.hddtemp.dev);
2005 free(objs[i].data.hddtemp.addr);
2011 //text_objects = NULL;
2012 //text_object_count = 0;
2015 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
2020 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
2021 *a = mixer_init(buf1);
2022 (void) scan_bar(arg + n, w, h);
2025 (void) scan_bar(arg, w, h);
2030 /* construct_text_object() creates a new text_object */
2031 static struct text_object *construct_text_object(const char *s, const char *arg, unsigned int object_count, struct text_object *text_objects, long line)
2033 //struct text_object *obj = new_text_object();
2034 struct text_object *obj = new_text_object_internal();
2037 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
2038 #define END ; } } else
2042 obj->type = OBJ_color;
2043 obj->data.l = get_x11_color(s);
2046 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
2047 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
2048 END OBJ(acpiacadapter, 0)
2049 #if defined(__linux__)
2056 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2058 obj->data.cpu_index=1;
2059 // ERR("freq: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2063 obj->data.cpu_index=atoi(&arg[0]);
2072 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2074 obj->data.cpu_index=1;
2075 // ERR("freq_g: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2079 obj->data.cpu_index=atoi(&arg[0]);
2082 END OBJ(voltage_mv, 0)
2088 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2090 obj->data.cpu_index=1;
2091 // ERR("voltage_mv: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2095 obj->data.cpu_index=atoi(&arg[0]);
2098 END OBJ(voltage_v, 0)
2104 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2106 obj->data.cpu_index=1;
2107 // ERR("voltage_v: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2111 obj->data.cpu_index=atoi(&arg[0]);
2117 #endif /* __linux__ */
2118 END OBJ(freq_dyn, 0);
2119 END OBJ(freq_dyn_g, 0);
2120 END OBJ(acpifan, 0);
2121 END OBJ(battery, 0);
2124 sscanf(arg, "%63s", bat);
2126 strcpy(bat, "BAT0");
2127 obj->data.s = strdup(bat);
2128 #if defined(__linux__)
2129 END OBJ(i8k_version, INFO_I8K)
2130 END OBJ(i8k_bios, INFO_I8K)
2131 END OBJ(i8k_serial, INFO_I8K)
2132 END OBJ(i8k_cpu_temp, INFO_I8K)
2133 END OBJ(i8k_cpu_tempf, INFO_I8K)
2134 END OBJ(i8k_left_fan_status, INFO_I8K)
2135 END OBJ(i8k_right_fan_status, INFO_I8K)
2136 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2137 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2138 END OBJ(i8k_ac_status, INFO_I8K)
2139 END OBJ(i8k_buttons_status, INFO_I8K)
2141 END OBJ(ibm_temps, 0)
2142 if (!isdigit(arg[0])
2144 || atoi(&arg[0]) >=8)
2147 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. Using 0 (CPU temp sensor).");
2149 obj->data.sensor = atoi(&arg[0]);
2150 END OBJ(ibm_volume, 0)
2151 END OBJ(ibm_brightness, 0)
2152 END OBJ(pb_battery, 0)
2153 if (arg && strcmp(arg, "status") == 0) {
2154 obj->data.i = PB_BATT_STATUS;
2155 } else if (arg && strcmp(arg, "percent") == 0) {
2156 obj->data.i = PB_BATT_PERCENT;
2157 } else if (arg && strcmp(arg, "time") == 0) {
2158 obj->data.i = PB_BATT_TIME;
2160 ERR("pb_battery: needs one argument: status, percent or time");
2165 #endif /* __linux__ */
2166 END OBJ(buffers, INFO_BUFFERS)
2167 END OBJ(cached, INFO_BUFFERS)
2168 END OBJ(cpu, INFO_CPU)
2170 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2171 obj->data.cpu_index = atoi(&arg[3]);
2173 } else {obj->data.cpu_index = 0; }
2175 obj->data.cpu_index = 0;
2177 END OBJ(cpubar, INFO_CPU)
2179 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2180 obj->data.cpu_index = atoi(&arg[3]);
2183 else {obj->data.cpu_index = 0;}
2184 (void) scan_bar(arg, &obj->a, &obj->b);
2186 (void) scan_bar(arg, &obj->a, &obj->b);
2187 obj->data.cpu_index = 0;
2189 END OBJ(cpugraph, INFO_CPU)
2191 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2192 obj->data.cpu_index = atoi(&arg[3]);
2195 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2197 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2198 obj->data.cpu_index = 0;
2200 END OBJ(diskio, INFO_DISKIO)
2201 END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2204 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2208 obj->data.s = scan_font(arg);
2210 OBJ(downspeed, INFO_NET)
2212 obj->data.net = get_net_stat(arg);
2215 CRIT_ERR("downspeed needs argument");
2217 END OBJ(downspeedf, INFO_NET)
2219 obj->data.net = get_net_stat(arg);
2222 CRIT_ERR("downspeedf needs argument");
2224 END OBJ(downspeedgraph, INFO_NET)
2225 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2227 sscanf(arg, "%63s %*i,%*i %*i", buf);
2228 obj->data.net = get_net_stat(buf);
2229 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2230 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
2237 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos = object_count;
2238 blockstart[blockdepth - 1] = object_count;
2239 obj->data.ifblock.pos = object_count + 2;
2241 ERR("$else: no matching $if_*");
2246 text_objects[blockstart[blockdepth]].data.ifblock.pos = object_count;
2248 ERR("$endif: no matching $if_*");
2251 OBJ(image, 0) obj->data.s = strdup(arg ? arg : "");
2254 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
2255 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
2256 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
2257 END OBJ(execibar, 0) unsigned int n;
2258 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2260 ERR("${execibar <interval> command}");
2261 obj->type = OBJ_text;
2262 snprintf(buf, 256, "${%s}", s);
2263 obj->data.s = strdup(buf);
2265 obj->data.execi.cmd = strdup(arg + n);
2267 END OBJ(execigraph, 0) unsigned int n;
2268 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2270 ERR("${execigraph <interval> command}");
2271 obj->type = OBJ_text;
2272 snprintf(buf, 256, "${%s}", s);
2273 obj->data.s = strdup(buf);
2275 obj->data.execi.cmd = strdup(arg + n);
2277 END OBJ(execi, 0) unsigned int n;
2280 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2282 ERR("${execi <interval> command}");
2283 obj->type = OBJ_text;
2284 snprintf(buf, 256, "${%s}", s);
2285 obj->data.s = strdup(buf);
2287 obj->data.execi.cmd = strdup(arg + n);
2288 obj->data.execi.buffer =
2289 (char *) calloc(1, TEXT_BUFFER_SIZE);
2291 END OBJ(texeci, 0) unsigned int n;
2293 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2295 ERR("${texeci <interval> command}");
2296 obj->type = OBJ_text;
2297 snprintf(buf, 256, "${%s}", s);
2298 obj->data.s = strdup(buf);
2300 obj->data.execi.cmd = strdup(arg + n);
2301 obj->data.execi.buffer =
2302 (char *) calloc(1, TEXT_BUFFER_SIZE);
2304 obj->data.execi.pos = -1;
2305 END OBJ(pre_exec, 0) obj->type = OBJ_text;
2307 FILE *fp = popen(arg, "r");
2311 n = fread(buf, 1, 2048, fp);
2314 if (n && buf[n - 1] == '\n')
2319 obj->data.s = strdup(buf);
2321 obj->data.s = strdup("");
2324 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
2325 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2327 while (isspace(*arg))
2333 obj->data.fsbar.fs = prepare_fs_stat(arg);
2334 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
2337 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
2341 obj->data.fsbar.fs = prepare_fs_stat(arg);
2342 END OBJ(fs_free, INFO_FS) if (!arg)
2344 obj->data.fs = prepare_fs_stat(arg);
2345 END OBJ(fs_used_perc, INFO_FS) if (!arg)
2347 obj->data.fs = prepare_fs_stat(arg);
2348 END OBJ(fs_free_perc, INFO_FS) if (!arg)
2350 obj->data.fs = prepare_fs_stat(arg);
2351 END OBJ(fs_size, INFO_FS) if (!arg)
2353 obj->data.fs = prepare_fs_stat(arg);
2354 END OBJ(fs_used, INFO_FS) if (!arg)
2356 obj->data.fs = prepare_fs_stat(arg);
2357 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
2358 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
2359 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
2363 ERR("goto needs arguments");
2364 obj->type = OBJ_text;
2365 obj->data.s = strdup("${goto}");
2369 obj->data.i = atoi(arg);
2374 if (sscanf(arg, "%d %d", &a, &b) != 2)
2375 sscanf(arg, "%d", &b);
2379 obj->data.pair.a = a;
2380 obj->data.pair.b = b;
2382 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
2386 ERR("i2c needs arguments");
2387 obj->type = OBJ_text;
2388 //obj->data.s = strdup("${i2c}");
2392 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2393 /* if scanf couldn't read three values, read type and num and use
2395 sscanf(arg, "%63s %d", buf2, &n);
2397 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
2398 obj->data.i2c.devtype);
2399 strncpy(obj->data.i2c.type, buf2, 63);
2402 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
2403 obj->data.i2c.devtype);
2404 strncpy(obj->data.i2c.type, buf2, 63);
2407 END OBJ(top, INFO_TOP)
2411 ERR("top needs arguments");
2412 obj->type = OBJ_text;
2413 //obj->data.s = strdup("${top}");
2416 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2417 if (strcmp(buf, "name") == 0) {
2418 obj->data.top.type = TOP_NAME;
2419 } else if (strcmp(buf, "cpu") == 0) {
2420 obj->data.top.type = TOP_CPU;
2421 } else if (strcmp(buf, "pid") == 0) {
2422 obj->data.top.type = TOP_PID;
2423 } else if (strcmp(buf, "mem") == 0) {
2424 obj->data.top.type = TOP_MEM;
2426 ERR("invalid arg for top");
2429 if (n < 1 || n > 10) {
2430 CRIT_ERR("invalid arg for top");
2433 obj->data.top.num = n - 1;
2437 ERR("invalid args given for top");
2440 END OBJ(top_mem, INFO_TOP)
2444 ERR("top_mem needs arguments");
2445 obj->type = OBJ_text;
2446 obj->data.s = strdup("${top_mem}");
2449 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2450 if (strcmp(buf, "name") == 0) {
2451 obj->data.top.type = TOP_NAME;
2452 } else if (strcmp(buf, "cpu") == 0) {
2453 obj->data.top.type = TOP_CPU;
2454 } else if (strcmp(buf, "pid") == 0) {
2455 obj->data.top.type = TOP_PID;
2456 } else if (strcmp(buf, "mem") == 0) {
2457 obj->data.top.type = TOP_MEM;
2459 ERR("invalid arg for top");
2462 if (n < 1 || n > 10) {
2463 CRIT_ERR("invalid arg for top");
2466 obj->data.top.num = n - 1;
2470 ERR("invalid args given for top");
2473 END OBJ(addr, INFO_NET)
2475 obj->data.net = get_net_stat(arg);
2478 CRIT_ERR("addr needs argument");
2480 END OBJ(linkstatus, INFO_WIFI)
2482 obj->data.net = get_net_stat(arg);
2485 CRIT_ERR("linkstatus needs argument");
2491 ERR("tail needs arguments");
2492 obj->type = OBJ_text;
2493 obj->data.s = strdup("${tail}");
2496 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2497 if (n1 < 1 || n1 > 30) {
2498 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
2502 fp = fopen(buf, "r");
2504 obj->data.tail.logfile =
2505 malloc(TEXT_BUFFER_SIZE);
2506 strcpy(obj->data.tail.logfile, buf);
2507 obj->data.tail.wantedlines = n1 - 1;
2508 obj->data.tail.interval =
2509 update_interval * 2;
2513 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2516 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2517 if (n1 < 1 || n1 > 30) {
2519 ("invalid arg for tail, number of lines must be between 1 and 30");
2521 } else if (n2 < 1 || n2 < update_interval) {
2523 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
2527 fp = fopen(buf, "r");
2529 obj->data.tail.logfile =
2530 malloc(TEXT_BUFFER_SIZE);
2531 strcpy(obj->data.tail.logfile, buf);
2532 obj->data.tail.wantedlines = n1 - 1;
2533 obj->data.tail.interval = n2;
2537 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2543 ERR("invalid args given for tail");
2546 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2551 ERR("head needs arguments");
2552 obj->type = OBJ_text;
2553 obj->data.s = strdup("${head}");
2556 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2557 if (n1 < 1 || n1 > 30) {
2558 CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
2562 fp = fopen(buf, "r");
2564 obj->data.tail.logfile =
2565 malloc(TEXT_BUFFER_SIZE);
2566 strcpy(obj->data.tail.logfile, buf);
2567 obj->data.tail.wantedlines = n1 - 1;
2568 obj->data.tail.interval =
2569 update_interval * 2;
2573 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2576 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2577 if (n1 < 1 || n1 > 30) {
2579 ("invalid arg for head, number of lines must be between 1 and 30");
2581 } else if (n2 < 1 || n2 < update_interval) {
2583 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
2587 fp = fopen(buf, "r");
2589 obj->data.tail.logfile =
2590 malloc(TEXT_BUFFER_SIZE);
2591 strcpy(obj->data.tail.logfile, buf);
2592 obj->data.tail.wantedlines = n1 - 1;
2593 obj->data.tail.interval = n2;
2597 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2603 ERR("invalid args given for head");
2606 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2607 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
2609 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2610 if (r >= 3 && (c < 1 || c > 3))
2612 if (r >= 2 && (b < 1 || b > 3))
2614 if (r >= 1 && (a < 1 || a > 3))
2617 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2618 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2619 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2620 END OBJ(if_existing, 0)
2621 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2622 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2625 ERR("if_existing needs an argument");
2626 obj->data.ifblock.s = 0;
2628 obj->data.ifblock.s = strdup(arg);
2629 blockstart[blockdepth] = object_count;
2630 obj->data.ifblock.pos = object_count + 2;
2632 END OBJ(if_mounted, 0)
2633 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2634 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2637 ERR("if_mounted needs an argument");
2638 obj->data.ifblock.s = 0;
2640 obj->data.ifblock.s = strdup(arg);
2641 blockstart[blockdepth] = object_count;
2642 obj->data.ifblock.pos = object_count + 2;
2644 END OBJ(if_running, 0)
2645 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2646 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2650 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2651 obj->data.ifblock.s = strdup(buf);
2653 ERR("if_running needs an argument");
2654 obj->data.ifblock.s = 0;
2656 blockstart[blockdepth] = object_count;
2657 obj->data.ifblock.pos = object_count + 2;
2661 END OBJ(mails, INFO_MAIL)
2662 END OBJ(mem, INFO_MEM)
2663 END OBJ(memmax, INFO_MEM)
2664 END OBJ(memperc, INFO_MEM)
2665 END OBJ(membar, INFO_MEM)
2666 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2667 END OBJ(memgraph, INFO_MEM)
2668 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2669 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
2670 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
2671 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
2672 END OBJ(mixerbar, INFO_MIXER)
2673 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2674 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2675 END OBJ(mixerlbar, INFO_MIXER)
2676 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2677 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2678 END OBJ(mixerrbar, INFO_MIXER)
2679 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2680 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2682 OBJ(new_mails, INFO_MAIL)
2683 END OBJ(nodename, 0)
2684 END OBJ(processes, INFO_PROCS)
2685 END OBJ(running_processes, INFO_RUN_PROCS)
2686 END OBJ(shadecolor, 0)
2688 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2690 END OBJ(outlinecolor, 0)
2692 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2694 END OBJ(stippled_hr, 0)
2696 int a = stippled_borders, b = 1;
2698 if (sscanf(arg, "%d %d", &a, &b) != 2)
2699 sscanf(arg, "%d", &b);
2703 obj->data.pair.a = a;
2704 obj->data.pair.b = b;
2706 END OBJ(swap, INFO_MEM)
2707 END OBJ(swapmax, INFO_MEM)
2708 END OBJ(swapperc, INFO_MEM)
2709 END OBJ(swapbar, INFO_MEM)
2710 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2711 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
2713 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
2714 obj->data.i2c.devtype);
2715 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
2717 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
2718 obj->data.i2c.devtype);
2719 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2720 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2722 char buf1[256], buf2[256], *fmt, *tz;
2725 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2734 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
2735 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
2737 END OBJ(iconv_start, 0)
2738 if (iconv_converting) {
2739 CRIT_ERR("You must stop your last iconv conversion before starting another");
2742 char iconv_from[CODEPAGE_LENGTH];
2743 char iconv_to[CODEPAGE_LENGTH];
2744 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2745 CRIT_ERR("Invalid arguments for iconv_start");
2748 new_iconv = iconv_open(iconv_to, iconv_from);
2749 if (new_iconv == (iconv_t)(-1)) {
2750 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2752 obj->a = register_iconv(&new_iconv);
2753 iconv_converting = 1;
2757 CRIT_ERR("Iconv requires arguments");
2759 END OBJ(iconv_stop, 0)
2760 iconv_converting = 0;
2763 END OBJ(totaldown, INFO_NET)
2765 obj->data.net = get_net_stat(arg);
2768 CRIT_ERR("totaldown needs argument");
2770 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
2772 obj->data.net = get_net_stat(arg);
2775 CRIT_ERR("totalup needs argument");
2778 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
2779 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
2780 END OBJ(upspeed, INFO_NET)
2782 obj->data.net = get_net_stat(arg);
2785 CRIT_ERR("upspeed needs argument");
2787 END OBJ(upspeedf, INFO_NET)
2789 obj->data.net = get_net_stat(arg);
2792 CRIT_ERR("upspeedf needs argument");
2795 END OBJ(upspeedgraph, INFO_NET)
2796 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2798 sscanf(arg, "%63s %*i,%*i %*i", buf);
2799 obj->data.net = get_net_stat(buf);
2800 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2801 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
2806 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
2807 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
2808 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2809 OBJ(apm_adapter, 0) END
2810 OBJ(apm_battery_life, 0) END
2811 OBJ(apm_battery_time, 0) END
2812 #endif /* __FreeBSD__ */
2816 obj->data.mail = parse_mail_args(IMAP, arg);
2817 obj->global_mode = 0;
2819 obj->global_mode = 1;
2822 OBJ(imap_messages, 0)
2825 obj->data.mail = parse_mail_args(IMAP, arg);
2826 obj->global_mode = 0;
2828 obj->global_mode = 1;
2834 obj->data.mail = parse_mail_args(POP3, arg);
2835 obj->global_mode = 0;
2837 obj->global_mode = 1;
2843 obj->data.mail = parse_mail_args(POP3, arg);
2844 obj->global_mode = 0;
2846 obj->global_mode = 1;
2850 OBJ(mpd_artist, INFO_MPD)
2851 END OBJ(mpd_title, INFO_MPD)
2852 END OBJ(mpd_random, INFO_MPD)
2853 END OBJ(mpd_repeat, INFO_MPD)
2854 END OBJ(mpd_elapsed, INFO_MPD)
2855 END OBJ(mpd_length, INFO_MPD)
2856 END OBJ(mpd_track, INFO_MPD)
2857 END OBJ(mpd_name, INFO_MPD)
2858 END OBJ(mpd_file, INFO_MPD)
2859 END OBJ(mpd_percent, INFO_MPD)
2860 END OBJ(mpd_album, INFO_MPD)
2861 END OBJ(mpd_vol, INFO_MPD)
2862 END OBJ(mpd_bitrate, INFO_MPD)
2863 END OBJ(mpd_status, INFO_MPD)
2864 END OBJ(mpd_bar, INFO_MPD)
2865 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2866 END OBJ(mpd_smart, INFO_MPD) END
2869 OBJ(xmms2_artist, INFO_XMMS2)
2870 END OBJ(xmms2_album, INFO_XMMS2)
2871 END OBJ(xmms2_title, INFO_XMMS2)
2872 END OBJ(xmms2_genre, INFO_XMMS2)
2873 END OBJ(xmms2_comment, INFO_XMMS2)
2874 END OBJ(xmms2_decoder, INFO_XMMS2)
2875 END OBJ(xmms2_transport, INFO_XMMS2)
2876 END OBJ(xmms2_url, INFO_XMMS2)
2877 END OBJ(xmms2_tracknr, INFO_XMMS2)
2878 END OBJ(xmms2_bitrate, INFO_XMMS2)
2879 END OBJ(xmms2_date, INFO_XMMS2)
2880 END OBJ(xmms2_id, INFO_XMMS2)
2881 END OBJ(xmms2_duration, INFO_XMMS2)
2882 END OBJ(xmms2_elapsed, INFO_XMMS2)
2883 END OBJ(xmms2_size, INFO_XMMS2)
2884 END OBJ(xmms2_status, INFO_XMMS2)
2885 END OBJ(xmms2_percent, INFO_XMMS2)
2886 END OBJ(xmms2_bar, INFO_XMMS2)
2887 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2888 END OBJ(xmms2_smart, INFO_XMMS2)
2892 OBJ(audacious_status, INFO_AUDACIOUS) END
2893 OBJ(audacious_title, INFO_AUDACIOUS) END
2894 OBJ(audacious_length, INFO_AUDACIOUS) END
2895 OBJ(audacious_length_seconds, INFO_AUDACIOUS) END
2896 OBJ(audacious_position, INFO_AUDACIOUS) END
2897 OBJ(audacious_position_seconds, INFO_AUDACIOUS) END
2898 OBJ(audacious_bitrate, INFO_AUDACIOUS) END
2899 OBJ(audacious_frequency, INFO_AUDACIOUS) END
2900 OBJ(audacious_channels, INFO_AUDACIOUS) END
2901 OBJ(audacious_filename, INFO_AUDACIOUS) END
2902 OBJ(audacious_playlist_length, INFO_AUDACIOUS) END
2903 OBJ(audacious_playlist_position, INFO_AUDACIOUS) END
2904 OBJ(audacious_bar, INFO_AUDACIOUS)
2905 (void) scan_bar(arg, &obj->a, &obj->b);
2909 OBJ(bmpx_title, INFO_BMPX)
2910 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2912 OBJ(bmpx_artist, INFO_BMPX)
2913 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2915 OBJ(bmpx_album, INFO_BMPX)
2916 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2918 OBJ(bmpx_track, INFO_BMPX)
2919 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2921 OBJ(bmpx_uri, INFO_BMPX)
2922 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2924 OBJ(bmpx_bitrate, INFO_BMPX)
2925 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2930 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
2931 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2932 ERR("hddtemp needs arguments");
2933 obj->type = OBJ_text;
2934 obj->data.s = strdup("${hddtemp}");
2939 #ifdef TCP_PORT_MONITOR
2940 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2941 int argc, port_begin, port_end, item, connection_index;
2943 memset(itembuf,0,sizeof(itembuf));
2945 /* massive argument checking */
2947 CRIT_ERR("tcp_portmon: needs arguments");
2949 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
2950 if ( (argc != 3) && (argc != 4) )
2952 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
2954 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
2956 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
2958 if ( port_begin > port_end )
2960 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
2962 if ( strncmp(itembuf,"count",31) == 0 )
2964 else if ( strncmp(itembuf,"rip",31) == 0 )
2966 else if ( strncmp(itembuf,"rhost",31) == 0 )
2968 else if ( strncmp(itembuf,"rport",31) == 0 )
2970 else if ( strncmp(itembuf,"rservice",31) == 0 )
2972 else if ( strncmp(itembuf,"lip",31) == 0 )
2974 else if ( strncmp(itembuf,"lhost",31) == 0 )
2976 else if ( strncmp(itembuf,"lport",31) == 0 )
2978 else if ( strncmp(itembuf,"lservice",31) == 0 )
2982 CRIT_ERR("tcp_portmon: invalid item specified");
2984 if ( (argc==3) && (item!=COUNT) )
2986 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
2988 if ( (argc==4) && (connection_index<0) )
2990 CRIT_ERR("tcp_portmon: connection index must be non-negative");
2992 /* ok, args looks good. save the text object data */
2993 obj->data.tcp_port_monitor.port_range_begin = (in_port_t)port_begin;
2994 obj->data.tcp_port_monitor.port_range_end = (in_port_t)port_end;
2995 obj->data.tcp_port_monitor.item = item;
2996 obj->data.tcp_port_monitor.connection_index = connection_index;
2998 /* if the port monitor collection hasn't been created, we must create it */
2999 if ( !info.p_tcp_port_monitor_collection )
3001 info.p_tcp_port_monitor_collection =
3002 create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
3003 if ( !info.p_tcp_port_monitor_collection )
3005 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
3009 /* if a port monitor for this port does not exist, create one and add it to the collection */
3010 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
3012 tcp_port_monitor_t * p_monitor =
3013 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
3016 CRIT_ERR("tcp_portmon: unable to create port monitor");
3018 /* add the newly created monitor to the collection */
3019 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
3022 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
3029 ERR("unknown variable %s", s);
3030 obj->type = OBJ_text;
3031 snprintf(buf, 256, "${%s}", s);
3032 obj->data.s = strdup(buf);
3039 static struct text_object *create_plain_text(const char *s)
3041 struct text_object *obj;
3043 if (s == NULL || *s == '\0') {
3047 obj = new_text_object_internal();
3049 obj->type = OBJ_text;
3050 obj->data.s = strdup(s);
3054 static struct text_object_list *extract_variable_text_internal(const char *p)
3056 struct text_object_list *retval;
3057 struct text_object *obj;
3060 retval = malloc(sizeof(struct text_object_list));
3061 memset(retval, 0, sizeof(struct text_object_list));
3062 retval->text_object_count = 0;
3064 long line = text_lines;
3072 obj = create_plain_text(s);
3074 // allocate memory for the object
3075 retval->text_objects = realloc(retval->text_objects,
3076 sizeof(struct text_object) * (retval->text_object_count+1));
3077 // assign the new object to the end of the list.
3078 memcpy(&retval->text_objects[retval->text_object_count++],
3079 obj, sizeof(struct text_object));
3091 /* variable is either $foo or ${foo} */
3095 while (*p && *p != '}')
3101 while (*p && (isalnum((int) *p)
3106 /* copy variable to buffer */
3107 len = (p - s > 255) ? 255 : (p - s);
3108 strncpy(buf, s, len);
3117 /* if variable wasn't found from environment, use some special */
3123 if (strchr(buf, ' ')) {
3124 arg = strchr(buf, ' ');
3127 while (isspace((int) *arg))
3133 /* lowercase variable name */
3140 // create new object
3141 obj = construct_text_object(buf, arg, retval->text_object_count, retval->text_objects, line);
3143 // allocate memory for the object
3144 retval->text_objects = realloc(retval->text_objects,
3145 sizeof(struct text_object) * (retval->text_object_count+1));
3146 // assign the new object to the end of the list.
3147 memcpy(&retval->text_objects[retval->text_object_count++],
3148 obj, sizeof(struct text_object));
3154 obj = create_plain_text("$");
3156 // allocate memory for the object
3157 retval->text_objects = realloc(retval->text_objects,
3158 sizeof(struct text_object) * (retval->text_object_count+1));
3159 // assign the new object to the end of the list.
3160 memcpy(&retval->text_objects[retval->text_object_count++],
3161 obj, sizeof(struct text_object));
3168 obj = create_plain_text(s);
3170 // allocate memory for the object
3171 retval->text_objects = realloc(retval->text_objects,
3172 sizeof(struct text_object) * (retval->text_object_count+1));
3173 // assign the new object to the end of the list.
3174 memcpy(&retval->text_objects[retval->text_object_count++],
3175 obj, sizeof(struct text_object));
3180 ERR("one or more $endif's are missing");
3186 static void extract_variable_text(const char *p)
3188 struct text_object_list *list;
3190 free_text_objects(text_object_count, text_objects);
3191 text_object_count = 0;
3192 text_objects = NULL;
3195 list = extract_variable_text_internal(p);
3196 text_objects = list->text_objects;
3197 text_object_count = list->text_object_count;
3204 void parse_conky_vars(char * text, char * p, struct information *cur) {
3205 struct text_object_list *object_list = extract_variable_text_internal(text);
3206 generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
3210 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
3215 char buff_in[P_MAX_SIZE] = {0};
3216 iconv_converting = 0;
3219 for (i = 0; i < object_count; i++) {
3220 struct text_object *obj = &objs[i];
3222 #define OBJ(a) break; case OBJ_##a:
3224 switch (obj->type) {
3227 ERR("not implemented obj type %d",
3231 /* does anyone have decimals in acpi temperature? */
3233 snprintf(p, p_max_size, "%d", (int)
3234 get_acpi_temperature(obj->
3238 snprintf(p, 5, "%d ", (int)
3239 get_acpi_temperature(obj->
3244 /* does anyone have decimals in acpi temperature? */
3246 snprintf(p, p_max_size, "%d", (int)
3247 ((get_acpi_temperature(obj->
3249 i)+ 40) * 9.0 / 5 - 40));
3251 snprintf(p, 5, "%d ", (int)
3252 ((get_acpi_temperature(obj->
3254 i)+ 40) * 9.0 / 5 - 40));
3258 obj->a = get_freq(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3263 obj->a = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.cpu_index);
3266 #if defined(__linux__)
3269 obj->a = get_voltage(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3274 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.cpu_index);
3277 #endif /* __linux__ */
3281 get_freq_dynamic(p, 6, "%.0f ", 1 );
3283 get_freq_dynamic(p, p_max_size, "%.0f", 1 );
3288 get_freq_dynamic(p, 6, "%'.2f ", 1000);
3290 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
3294 get_adt746x_cpu(p, p_max_size);
3297 get_adt746x_fan(p, p_max_size);
3300 get_acpi_fan(p, p_max_size);
3302 OBJ(acpiacadapter) {
3303 get_acpi_ac_adapter(p, p_max_size);
3306 get_battery_stuff(p, p_max_size, obj->data.s);
3309 human_readable(cur->buffers * 1024, p, 255);
3312 human_readable(cur->cached * 1024, p, 255);
3315 if (obj->data.cpu_index > info.cpu_count) {
3316 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
3317 CRIT_ERR("attempting to use more CPUs then you have!");
3320 snprintf(p, p_max_size, "%*d", pad_percents,
3321 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3324 snprintf(p, 4, "%*d ",
3326 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3332 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3335 new_graph(p, obj->a,
3336 obj->b, obj->c, obj->d,
3337 (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3341 new_fg(p, obj->data.l);
3343 #if defined(__linux__)
3345 snprintf(p, p_max_size, "%s", i8k.version);
3348 snprintf(p, p_max_size, "%s", i8k.bios);
3351 snprintf(p, p_max_size, "%s", i8k.serial);
3354 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
3356 OBJ(i8k_cpu_tempf) {
3358 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3359 snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
3361 OBJ(i8k_left_fan_status) {
3362 int left_fan_status;
3363 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3364 if(left_fan_status == 0) {
3365 snprintf(p, p_max_size,"off");
3366 } if(left_fan_status == 1) {
3367 snprintf(p, p_max_size, "low");
3368 } if(left_fan_status == 2) {
3369 snprintf(p, p_max_size, "high");
3373 OBJ(i8k_right_fan_status) {
3374 int right_fan_status;
3375 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3376 if(right_fan_status == 0) {
3377 snprintf(p, p_max_size,"off");
3378 } if(right_fan_status == 1) {
3379 snprintf(p, p_max_size, "low");
3380 } if(right_fan_status == 2) {
3381 snprintf(p, p_max_size, "high");
3384 OBJ(i8k_left_fan_rpm) {
3385 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3387 OBJ(i8k_right_fan_rpm) {
3388 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3390 OBJ(i8k_ac_status) {
3392 sscanf(i8k.ac_status, "%d", &ac_status);
3393 if(ac_status == -1) {
3394 snprintf(p, p_max_size,"disabled (read i8k docs)");
3395 } if(ac_status == 0) {
3396 snprintf(p, p_max_size, "off");
3397 } if(ac_status == 1) {
3398 snprintf(p, p_max_size, "on");
3401 OBJ(i8k_buttons_status) {
3402 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3406 get_ibm_acpi_fan(p, p_max_size);
3409 get_ibm_acpi_temps();
3410 snprintf(p, p_max_size, "%d",
3411 ibm_acpi.temps[obj->data.sensor]);
3414 get_ibm_acpi_volume(p, p_max_size);
3416 OBJ(ibm_brightness) {
3417 get_ibm_acpi_brightness(p, p_max_size);
3420 get_powerbook_batt_info(p, p_max_size, obj->data.i);
3422 #endif /* __linux__ */
3426 new_font(p, obj->data.s);
3431 if (diskio_value > 1024*1024) {
3432 snprintf(p, p_max_size, "%.1fGiB",
3433 (double)diskio_value/1024/1024);
3434 } else if (diskio_value > 1024) {
3435 snprintf(p, p_max_size, "%.1fMiB",
3436 (double)diskio_value/1024);
3437 } else if (diskio_value > 0) {
3438 snprintf(p, p_max_size, "%dKiB", diskio_value);
3440 snprintf(p, p_max_size, "%dB", diskio_value);
3443 if (diskio_value > 1024*1024) {
3444 snprintf(p, 6, "%.1fGiB ",
3445 (double)diskio_value/1024/1024);
3446 } else if (diskio_value > 1024) {
3447 snprintf(p, 6, "%.1fMiB ",
3448 (double)diskio_value/1024);
3449 } else if (diskio_value > 0) {
3450 snprintf(p, 6, "%dKiB ", diskio_value);
3452 snprintf(p, 6, "%dB ", diskio_value);
3457 new_graph(p, obj->a,
3458 obj->b, obj->c, obj->d,
3459 diskio_value, obj->e, 1);
3464 snprintf(p, p_max_size, "%d",
3465 (int) (obj->data.net->
3469 snprintf(p, 6, "%d ",
3470 (int) (obj->data.net->
3476 snprintf(p, p_max_size, "%.1f",
3478 recv_speed / 1024.0);
3480 snprintf(p, 8, "%.1f ",
3482 recv_speed / 1024.0);
3484 OBJ(downspeedgraph) {
3485 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
3486 obj->data.net->recv_speed = 0.01;
3487 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3488 (obj->data.net->recv_speed /
3489 1024.0), obj->e, 1);
3493 i = obj->data.ifblock.pos - 1;
3503 snprintf(p, p_max_size, "%u.%u.%u.%u",
3504 obj->data.net->addr.
3506 obj->data.net->addr.
3508 obj->data.net->addr.
3510 obj->data.net->addr.
3515 snprintf(p, p_max_size, "%d",
3516 obj->data.net->linkstatus);
3518 #if defined(IMLIB2) && defined(X11)
3523 Imlib_Image image, buffer;
3524 image = imlib_load_image(obj->data.s);
3525 imlib_context_set_image(image);
3528 w = imlib_image_get_width();
3529 h = imlib_image_get_height();
3530 buffer = imlib_create_image(w, h);
3531 imlib_context_set_display(display);
3532 imlib_context_set_drawable(window.drawable);
3533 imlib_context_set_colormap(DefaultColormap(display, screen));
3534 imlib_context_set_visual(DefaultVisual(display, screen));
3535 imlib_context_set_image(buffer);
3536 imlib_blend_image_onto_image(image, 0, 0, 0, w, h, text_start_x, text_start_y, w, h);
3537 imlib_render_image_on_drawable(text_start_x, text_start_y);
3539 imlib_context_set_image(image);
3546 FILE *fp = popen(obj->data.s, "r");
3547 int length = fread(p, 1, p_max_size, fp);
3550 /*output[length] = '\0';
3551 if (length > 0 && output[length - 1] == '\n') {
3552 output[length - 1] = '\0';
3555 if (length > 0 && p[length - 1] == '\n') {
3556 p[length - 1] = '\0';
3559 //parse_conky_vars(output, p, cur);
3563 FILE *fp = popen(obj->data.s, "r");
3564 int n2 = fread(p, 1, p_max_size, fp);
3568 if (n2 && p[n2 - 1] == '\n')
3577 if (sscanf(p, "%lf", &barnum) == 0) {
3578 ERR("reading execbar value failed (perhaps it's not the correct format?)");
3580 if (barnum > 100 || barnum < 0) {
3581 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
3583 barnum = barnum / 100.0;
3584 new_bar(p, 0, 4, (int) (barnum * 255.0));
3590 FILE *fp = popen(obj->data.s, "r");
3591 int n2 = fread(p, 1, p_max_size, fp);
3595 if (n2 && p[n2 - 1] == '\n')
3604 if (sscanf(p, "%lf", &barnum) == 0) {
3605 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
3607 if (barnum > 100 || barnum < 0) {
3608 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
3610 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
3615 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3616 new_bar(p, 0, 4, (int) obj->f);
3619 FILE *fp = popen(obj->data.execi.cmd, "r");
3620 int n2 = fread(p, 1, p_max_size, fp);
3623 if (n2 && p[n2 - 1] == '\n')
3632 if (sscanf(p, "%f", &barnum) == 0) {
3633 ERR("reading execibar value failed (perhaps it's not the correct format?)");
3635 if (barnum > 100 || barnum < 0) {
3636 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
3638 obj->f = 255 * barnum / 100.0;
3639 new_bar(p, 0, 4, (int) obj->f);
3641 obj->data.execi.last_update =
3642 current_update_time;
3646 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3647 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
3650 FILE *fp = popen(obj->data.execi.cmd, "r");
3651 int n2 = fread(p, 1, p_max_size, fp);
3654 if (n2 && p[n2 - 1] == '\n')
3663 if (sscanf(p, "%f", &barnum) == 0) {
3664 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
3666 if (barnum > 100 || barnum < 0) {
3667 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
3670 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
3672 obj->data.execi.last_update = current_update_time;
3678 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
3679 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3681 char *output = obj->data.execi.buffer;
3682 FILE *fp = popen(obj->data.execi.cmd, "r");
3683 //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3684 int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3687 output[length] = '\0';
3688 if (length > 0 && output[length - 1] == '\n') {
3689 output[length - 1] = '\0';
3691 obj->data.execi.last_update = current_update_time;
3692 snprintf(p, p_max_size, "%s", output);
3694 //parse_conky_vars(output, p, cur);
3697 if (obj->data.execi.pos < 0) {
3698 obj->data.execi.last_update = current_update_time;
3699 if (pthread_create(&(obj->data.execi.thread_info.thread), NULL, (void*)threaded_exec, (void*) obj)) {
3700 ERR("Error starting thread");
3702 obj->data.execi.pos = register_thread(&(obj->data.execi.thread_info));
3704 pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
3705 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3706 pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
3708 #endif /* HAVE_POPEN */
3710 if (obj->global_mode && info.mail) { // this means we use info
3711 if (info.mail->pos < 0) {
3712 info.mail->last_update = current_update_time;
3713 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) info.mail)) {
3714 ERR("Error starting thread");
3716 info.mail->pos = register_thread(&(info.mail->thread_info));
3718 // get a lock before reading
3719 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3720 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3721 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3722 } else if (obj->data.mail) { // this means we use obj
3723 if (obj->data.mail->pos < 0) {
3724 obj->data.mail->last_update = current_update_time;
3725 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) obj->data.mail)) {
3726 ERR("Error starting thread");
3728 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3730 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3731 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3732 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3733 } else if (!obj->a) { // something is wrong, warn once then stop
3734 ERR("Theres a problem with your imap_unseen settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3738 OBJ(imap_messages) {
3739 if (obj->global_mode && info.mail) { // this means we use info
3740 if (info.mail->pos < 0) {
3741 info.mail->last_update = current_update_time;
3742 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) info.mail)) {
3743 ERR("Error starting thread");
3745 info.mail->pos = register_thread(&(info.mail->thread_info));
3747 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3748 snprintf(p, p_max_size, "%lu", info.mail->messages);
3749 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3750 } else if (obj->data.mail) { // this means we use obj
3751 if (obj->data.mail->pos < 0) {
3752 obj->data.mail->last_update = current_update_time;
3753 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)imap_thread, (void*) obj->data.mail)) {
3754 ERR("Error starting thread");
3756 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3758 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3759 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
3760 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3761 } else if (!obj->a) { // something is wrong, warn once then stop
3762 ERR("Theres a problem with your imap_messages settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3767 if (obj->global_mode && info.mail) { // this means we use info
3768 if (info.mail->pos < 0) {
3769 info.mail->last_update = current_update_time;
3770 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) info.mail)) {
3771 ERR("Error starting thread");
3773 info.mail->pos = register_thread(&(info.mail->thread_info));
3775 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3776 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3777 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3778 } else if (obj->data.mail) { // this means we use obj
3779 if (obj->data.mail->pos < 0) {
3780 obj->data.mail->last_update = current_update_time;
3781 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) obj->data.mail)) {
3782 ERR("Error starting thread");
3784 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3786 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3787 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3788 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3789 } else if (!obj->a) { // something is wrong, warn once then stop
3790 ERR("Theres a problem with your pop3_unseen settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3795 if (obj->global_mode && info.mail) { // this means we use info
3796 if (info.mail->pos < 0) {
3797 info.mail->last_update = current_update_time;
3798 if (pthread_create(&(info.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) info.mail)) {
3799 ERR("Error starting thread");
3801 info.mail->pos = register_thread(&(info.mail->thread_info));
3803 pthread_mutex_lock(&(info.mail->thread_info.mutex));
3804 snprintf(p, p_max_size, "%.1f", info.mail->used/1024.0/1024.0);
3805 pthread_mutex_unlock(&(info.mail->thread_info.mutex));
3806 } else if (obj->data.mail) { // this means we use obj
3807 if (obj->data.mail->pos < 0) {
3808 obj->data.mail->last_update = current_update_time;
3809 if (pthread_create(&(obj->data.mail->thread_info.thread), NULL, (void*)pop3_thread, (void*) obj->data.mail)) {
3810 ERR("Error starting thread");
3812 obj->data.mail->pos = register_thread(&(obj->data.mail->thread_info));
3814 pthread_mutex_lock(&(obj->data.mail->thread_info.mutex));
3815 snprintf(p, p_max_size, "%.1f", obj->data.mail->used/1024.0/1024.0);
3816 pthread_mutex_unlock(&(obj->data.mail->thread_info.mutex));
3817 } else if (!obj->a) { // something is wrong, warn once then stop
3818 ERR("Theres a problem with your pop3_used settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3823 if (obj->data.fs != NULL) {
3824 if (obj->data.fs->size == 0)
3843 if (obj->data.fs != NULL)
3844 human_readable(obj->data.fs->avail,
3848 if (obj->data.fs != NULL) {
3849 if (obj->data.fs->size)
3850 snprintf(p, p_max_size, "%*d",
3859 snprintf(p, p_max_size, "0");
3863 if (obj->data.fs != NULL)
3864 human_readable(obj->data.fs->size,
3868 if (obj->data.fs != NULL)
3869 human_readable(obj->data.fs->size -
3870 (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
3874 if (obj->data.fs != NULL) {
3875 if (obj->data.fs->size == 0)
3893 if (obj->data.fs != NULL) {
3894 if (obj->data.fs->size)
3895 snprintf(p, 4, "%d",
3905 snprintf(p, p_max_size, "0");
3909 float *v = info.loadavg;
3911 if (obj->data.loadavg[2])
3912 snprintf(p, p_max_size, "%.2f %.2f %.2f",
3913 v[obj->data.loadavg[0] -
3915 v[obj->data.loadavg[1] -
3917 v[obj->data.loadavg[2] -
3919 else if (obj->data.loadavg[1])
3920 snprintf(p, p_max_size, "%.2f %.2f",
3921 v[obj->data.loadavg[0] -
3923 v[obj->data.loadavg[1] -
3925 else if (obj->data.loadavg[0])
3926 snprintf(p, p_max_size, "%.2f",
3927 v[obj->data.loadavg[0] -
3931 new_goto(p, obj->data.i);
3934 new_tab(p, obj->data.pair.a, obj->data.pair.b);
3937 new_hr(p, obj->data.i);
3944 temp = get_hddtemp_info(obj->data.hddtemp.dev,
3945 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
3947 snprintf(p, p_max_size, "N/A");
3948 } else if (unit == '*') {
3949 snprintf(p, p_max_size, "%s", temp);
3951 snprintf(p, p_max_size, "%s°%c", temp, unit);
3956 new_offset(p, obj->data.i);
3959 new_voffset(p, obj->data.i);
3964 r = get_i2c_info(&obj->data.i2c.fd,
3966 obj->data.i2c.devtype,
3967 obj->data.i2c.type);
3969 if (r >= 100.0 || r == 0)
3970 snprintf(p, p_max_size, "%d", (int) r);
3972 snprintf(p, p_max_size, "%.1f", r);
3975 new_alignr(p, obj->data.i);
3978 new_alignc(p, obj->data.i);
3982 if ((obj->data.ifblock.s)
3983 && (stat(obj->data.ifblock.s, &tmp) ==
3985 i = obj->data.ifblock.pos;
3992 if ((obj->data.ifblock.s)
3993 && (!check_mount(obj->data.ifblock.s))) {
3994 i = obj->data.ifblock.pos;
4001 if ((obj->data.ifblock.s)
4002 && system(obj->data.ifblock.s)) {
4003 i = obj->data.ifblock.pos;
4010 snprintf(p, p_max_size, "%s", cur->uname_s.release);
4013 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
4018 human_readable(cur->mem * 1024, p, 255);
4021 human_readable(cur->memmax * 1024, p, 255);
4026 snprintf(p, p_max_size, "%*lu",
4031 snprintf(p, 4, "%*lu ",
4038 new_bar(p, obj->data.pair.a,
4040 cur->memmax ? (cur->mem * 255) /
4045 new_graph(p, obj->a,
4046 obj->b, obj->c, obj->d,
4047 cur->memmax ? (cur->mem * 100.0) /
4048 (cur->memmax) : 0.0, 100, 1);
4052 snprintf(p, p_max_size, "%d",
4053 mixer_get_avg(obj->data.l));
4056 snprintf(p, p_max_size, "%d",
4057 mixer_get_left(obj->data.l));
4060 snprintf(p, p_max_size, "%d",
4061 mixer_get_right(obj->data.l));
4064 new_bar(p, obj->data.mixerbar.w,
4065 obj->data.mixerbar.h,
4066 mixer_get_avg(obj->data.mixerbar.
4070 new_bar(p, obj->data.mixerbar.w,
4071 obj->data.mixerbar.h,
4072 mixer_get_left(obj->data.mixerbar.
4076 new_bar(p, obj->data.mixerbar.w,
4077 obj->data.mixerbar.h,
4078 mixer_get_right(obj->data.mixerbar.
4084 snprintf(p, p_max_size, "%d", cur->mail_count);
4087 snprintf(p, p_max_size, "%d", cur->new_mail_count);
4091 snprintf(p, p_max_size, "%s",
4092 cur->uname_s.nodename);
4095 new_outline(p, obj->data.l);
4099 snprintf(p, p_max_size, "%hu", cur->procs);
4101 snprintf(p, 5, "%hu ",
4104 OBJ(running_processes) {
4106 snprintf(p, p_max_size, "%hu",
4109 snprintf(p, 3, "%hu ",
4113 snprintf(p, p_max_size, "%s", obj->data.s);
4116 new_bg(p, obj->data.l);
4119 new_stippled_hr(p, obj->data.pair.a,
4123 human_readable(cur->swap * 1024, p, 255);
4126 human_readable(cur->swapmax * 1024, p,
4130 if (cur->swapmax == 0) {
4131 strncpy(p, "No swap", 255);
4134 snprintf(p, 255, "%*lu",
4140 snprintf(p, 4, "%*lu ",
4148 new_bar(p, obj->data.pair.a,
4150 cur->swapmax ? (cur->swap * 255) /
4151 (cur->swapmax) : 0);
4154 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4157 time_t t = time(NULL);
4158 struct tm *tm = localtime(&t);
4159 setlocale(LC_TIME, "");
4160 strftime(p, p_max_size, obj->data.s, tm);
4163 time_t t = time(NULL);
4164 struct tm *tm = gmtime(&t);
4165 strftime(p, p_max_size, obj->data.s, tm);
4169 if (obj->data.tztime.tz) {
4170 oldTZ = getenv("TZ");
4171 setenv("TZ", obj->data.tztime.tz, 1);
4174 time_t t = time(NULL);
4175 struct tm *tm = localtime(&t);
4176 setlocale(LC_TIME, "");
4177 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4179 setenv("TZ", oldTZ, 1);
4184 // Needless to free oldTZ since getenv gives ptr to static data
4187 human_readable(obj->data.net->recv, p,
4191 human_readable(obj->data.net->trans, p,
4195 snprintf(p, p_max_size, "%d", total_updates);
4199 snprintf(p, p_max_size, "%d",
4200 (int) (obj->data.net->
4204 snprintf(p, 5, "%d ",
4205 (int) (obj->data.net->
4211 snprintf(p, p_max_size, "%.1f",
4213 trans_speed / 1024.0);
4215 snprintf(p, 8, "%.1f ",
4217 trans_speed / 1024.0);
4220 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
4221 obj->data.net->trans_speed = 0.01;
4222 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4223 (obj->data.net->trans_speed /
4224 1024.0), obj->e, 1);
4227 format_seconds_short(p, p_max_size,
4231 format_seconds(p, p_max_size, (int) cur->uptime);
4234 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
4236 snprintf(p, p_max_size, "%s", get_apm_adapter());
4238 OBJ(apm_battery_life) {
4240 msg = get_apm_battery_life();
4241 snprintf(p, p_max_size, "%s", msg);
4244 OBJ(apm_battery_time) {
4246 msg = get_apm_battery_time();
4247 snprintf(p, p_max_size, "%s", msg);
4250 #endif /* __FreeBSD__ */
4254 snprintf(p, p_max_size, "%s", cur->mpd.title);
4257 snprintf(p, p_max_size, "%s", cur->mpd.artist);
4260 snprintf(p, p_max_size, "%s", cur->mpd.album);
4263 snprintf(p, p_max_size, "%s", cur->mpd.random);
4266 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4269 snprintf(p, p_max_size, "%s", cur->mpd.track);
4272 snprintf(p, p_max_size, "%s", cur->mpd.name);
4275 snprintf(p, p_max_size, "%s", cur->mpd.file);
4278 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4281 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4284 snprintf(p, p_max_size, "%s", cur->mpd.status);
4287 int days = 0, hours = 0, minutes =
4289 int tmp = cur->mpd.elapsed;
4290 while (tmp >= 86400) {
4294 while (tmp >= 3600) {
4304 snprintf(p, p_max_size, "%i days %i:%02i:%02i",
4305 days, hours, minutes,
4308 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4311 snprintf(p, p_max_size, "%i:%02i", minutes,
4315 int days = 0, hours = 0, minutes =
4317 int tmp = cur->mpd.length;
4318 while (tmp >= 86400) {
4322 while (tmp >= 3600) {
4332 snprintf(p, p_max_size,
4333 "%i days %i:%02i:%02i",
4334 days, hours, minutes,
4337 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4340 snprintf(p, p_max_size, "%i:%02i", minutes,
4344 snprintf(p, p_max_size, "%2.0f",
4345 cur->mpd.progress * 100);
4348 new_bar(p, obj->data.pair.a,
4350 (int) (cur->mpd.progress *
4354 if (strlen(cur->mpd.title) < 2 && strlen(cur->mpd.title) < 2) {
4355 snprintf(p, p_max_size, "%s", cur->mpd.file);
4357 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist, cur->mpd.title);
4363 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4366 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4369 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4372 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4374 OBJ(xmms2_comment) {
4375 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4377 OBJ(xmms2_decoder) {
4378 snprintf(p, p_max_size, "%s", cur->xmms2.decoder);
4380 OBJ(xmms2_transport) {
4381 snprintf(p, p_max_size, "%s", cur->xmms2.transport);
4384 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4387 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4390 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4392 OBJ(xmms2_tracknr) {
4393 if (cur->xmms2.tracknr != -1)
4394 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4396 OBJ(xmms2_bitrate) {
4397 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4400 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4403 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4405 OBJ(xmms2_elapsed) {
4406 int tmp = cur->xmms2.elapsed;
4407 snprintf(p, p_max_size, "%02d:%02d",
4408 tmp / 60000, (tmp / 1000) % 60);
4410 OBJ(xmms2_duration) {
4411 int tmp = cur->xmms2.duration;
4412 snprintf(p, p_max_size, "%02d:%02d",
4413 tmp / 60000, (tmp / 1000) % 60);
4415 OBJ(xmms2_percent) {
4416 snprintf(p, p_max_size, "%2.0f",
4417 cur->xmms2.progress * 100);
4420 new_bar(p, obj->data.pair.a,
4422 (int) (cur->xmms2.progress *
4426 if (strlen(cur->xmms2.title) < 2 && strlen(cur->xmms2.title) < 2) {
4427 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4429 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist, cur->xmms2.title);
4434 OBJ(audacious_status) {
4435 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_STATUS]);
4437 OBJ(audacious_title) {
4438 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_TITLE]);
4440 OBJ(audacious_length) {
4441 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH]);
4443 OBJ(audacious_length_seconds) {
4444 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4446 OBJ(audacious_position) {
4447 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION]);
4449 OBJ(audacious_position_seconds) {
4450 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4452 OBJ(audacious_bitrate) {
4453 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_BITRATE]);
4455 OBJ(audacious_frequency) {
4456 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FREQUENCY]);
4458 OBJ(audacious_channels) {
4459 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_CHANNELS]);
4461 OBJ(audacious_filename) {
4462 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FILENAME]);
4464 OBJ(audacious_playlist_length) {
4465 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4467 OBJ(audacious_playlist_position) {
4468 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4470 OBJ(audacious_bar) {
4472 progress= atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4473 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4474 new_bar(p,obj->a,obj->b,(int)(progress*255.0f));
4479 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4482 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4485 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4488 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4491 snprintf(p, p_max_size, "%i", cur->bmpx.track);
4494 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4498 if (obj->data.top.type == TOP_NAME
4499 && obj->data.top.num >= 0
4500 && obj->data.top.num < 10) {
4501 // if we limit the buffer and add a bunch of space after, it stops the thing from
4502 // moving other shit around, which is really fucking annoying
4503 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
4504 } else if (obj->data.top.type == TOP_CPU
4505 && obj->data.top.num >= 0
4506 && obj->data.top.num < 10) {
4507 snprintf(p, 7, "%3.2f ",
4508 cur->cpu[obj->data.top.
4510 } else if (obj->data.top.type == TOP_PID
4511 && obj->data.top.num >= 0
4512 && obj->data.top.num < 10) {
4513 snprintf(p, 8, "%i ",
4514 cur->cpu[obj->data.top.
4516 } else if (obj->data.top.type == TOP_MEM
4517 && obj->data.top.num >= 0
4518 && obj->data.top.num < 10) {
4519 snprintf(p, 7, "%3.2f ",
4520 cur->cpu[obj->data.top.
4525 if (obj->data.top.type == TOP_NAME
4526 && obj->data.top.num >= 0
4527 && obj->data.top.num < 10) {
4528 // if we limit the buffer and add a bunch of space after, it stops the thing from
4529 // moving other shit around, which is really fucking annoying
4532 cur->memu[obj->data.top.
4534 } else if (obj->data.top.type == TOP_CPU
4535 && obj->data.top.num >= 0
4536 && obj->data.top.num < 10) {
4537 snprintf(p, 7, "%3.2f ",
4538 cur->memu[obj->data.top.
4540 } else if (obj->data.top.type == TOP_PID
4541 && obj->data.top.num >= 0
4542 && obj->data.top.num < 10) {
4543 snprintf(p, 8, "%i ",
4544 cur->memu[obj->data.top.
4546 } else if (obj->data.top.type == TOP_MEM
4547 && obj->data.top.num >= 0
4548 && obj->data.top.num < 10) {
4549 snprintf(p, 7, "%3.2f ",
4550 cur->memu[obj->data.top.
4557 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4558 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4560 obj->data.tail.last_update = current_update_time;
4564 tailstring *head = NULL;
4565 tailstring *headtmp = NULL;
4566 tailstring *freetmp = NULL;
4567 fp = fopen(obj->data.tail.logfile, "rt");
4569 ERR("tail logfile failed to open");
4572 obj->data.tail.readlines = 0;
4574 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
4581 addtail(&head, obj->data.tail.buffer);
4582 obj->data.tail.readlines++;
4588 if (obj->data.tail.readlines > 0) {
4589 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4590 addtail(&headtmp, head->data);
4595 strcpy(obj->data.tail.buffer, headtmp->data);
4596 headtmp = headtmp->next;
4597 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4599 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 */
4600 headtmp = headtmp->next;
4604 /* get rid of any ugly newlines at the end */
4605 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4606 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4608 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4612 strcpy(obj->data.tail.buffer, "Logfile Empty");
4613 snprintf(p, p_max_size, "Logfile Empty");
4614 } /* if readlines */
4616 } /* if cur_upd_time >= */
4618 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4622 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4623 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4625 obj->data.tail.last_update = current_update_time;
4627 tailstring *head = NULL;
4628 tailstring *headtmp = NULL;
4629 tailstring *freetmp = NULL;
4630 fp = fopen(obj->data.tail.logfile, "rt");
4632 ERR("head logfile failed to open");
4634 obj->data.tail.readlines = 0;
4635 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
4636 addtail(&head, obj->data.tail.buffer);
4637 obj->data.tail.readlines++;
4641 if (obj->data.tail.readlines > 0) {
4643 addtail(&headtmp, head->data);
4648 strcpy(obj->data.tail.buffer, headtmp->data);
4649 headtmp = headtmp->next;
4651 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 */
4652 headtmp = headtmp->next;
4655 /* get rid of any ugly newlines at the end */
4656 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4657 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4659 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4661 strcpy(obj->data.tail.buffer, "Logfile Empty");
4662 snprintf(p, p_max_size, "Logfile Empty");
4663 } /* if readlines > 0 */
4664 } /* if fp == null */
4665 } /* cur_upd_time >= */
4667 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4670 #ifdef TCP_PORT_MONITOR
4673 /* grab a pointer to this port monitor */
4674 tcp_port_monitor_t * p_monitor =
4675 find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
4676 obj->data.tcp_port_monitor.port_range_begin,
4677 obj->data.tcp_port_monitor.port_range_end );
4679 snprintf(p, p_max_size, "monitor not found");
4683 /* now grab the text of interest */
4684 if ( peek_tcp_port_monitor( p_monitor,
4685 obj->data.tcp_port_monitor.item,
4686 obj->data.tcp_port_monitor.connection_index,
4687 p, p_max_size ) != 0 )
4689 snprintf(p, p_max_size, "monitor peek error");
4698 iconv_converting = 1;
4699 iconv_selected = obj->a;
4704 iconv_converting = 0;
4713 unsigned int a = strlen(p);
4716 if (a > 0 && iconv_converting && iconv_selected > 0 && (iconv_cd[iconv_selected - 1] != (iconv_t)(-1))) {
4718 size_t dummy1, dummy2;
4719 char *ptr = buff_in;
4722 dummy1 = dummy2 = a;
4724 strncpy(buff_in, p, P_MAX_SIZE);
4726 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
4727 while (dummy1 > 0) {
4728 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1, &outptr, &dummy2);
4730 ERR("Iconv codeset conversion failed");
4735 /* It is nessecary when we are converting from multibyte to singlebyte codepage */
4746 double current_update_time, last_update_time;
4748 static void generate_text()
4750 struct information *cur = &info;
4758 current_update_time = get_time();
4762 /* add things to the buffer */
4768 generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
4770 if (stuff_in_upper_case) {
4781 last_update_time = current_update_time;
4788 static void set_font()
4792 if (window.xftdraw != NULL) {
4793 XftDrawDestroy(window.xftdraw);
4795 window.xftdraw = XftDrawCreate(display, window.drawable,
4796 DefaultVisual(display,
4798 DefaultColormap(display,
4803 XSetFont(display, window.gc, fonts[selected_font].font->fid);
4809 static inline int get_string_width(const char *s)
4812 return *s ? calc_text_width(s, strlen(s)) : 0;
4818 static inline int get_string_width_special(char *s)
4831 if (*p == SPECIAL_CHAR) {
4832 /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
4833 for (i = 0; i < strlen(p); i++) {
4834 *(p + i) = *(p + i + 1);
4836 if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
4837 width += specials[special_index+index].width;
4844 if (strlen(final) > 1) {
4845 width += calc_text_width(final, strlen(final));
4855 static void text_size_updater(char *s);
4857 int last_font_height;
4858 static void update_text_area()
4862 /* update text size if it isn't fixed */
4867 text_width = minimum_width;
4870 last_font_height = font_height();
4871 for_each_line(text_buffer, text_size_updater);
4873 if (text_height < minimum_height)
4874 text_height = minimum_height;
4875 if (text_width > maximum_width && maximum_width > 0)
4876 text_width = maximum_width;
4879 /* get text position on workarea */
4880 switch (text_alignment) {
4887 x = workarea[2] - text_width - gap_x;
4894 y = workarea[3] - text_height - gap_y;
4898 x = workarea[2] - text_width - gap_x;
4899 y = workarea[3] - text_height - gap_y;
4903 case NONE: // Let the WM manage the window
4914 if (own_window && !fixed_pos) {
4917 text_start_x = border_margin + 1;
4918 text_start_y = border_margin + 1;
4919 window.x = x - border_margin - 1;
4920 window.y = y - border_margin - 1;
4924 /* If window size doesn't match to workarea's size, then window
4925 * probably includes panels (gnome).
4926 * Blah, doesn't work on KDE. */
4927 if (workarea[2] != window.width
4928 || workarea[3] != window.height) {
4942 static int cur_x, cur_y; /* current x and y for drawing */
4943 static int draw_mode; /* FG, BG or OUTLINE */
4944 static long current_color;
4947 static void text_size_updater(char *s)
4951 /* get string widths and skip specials */
4954 if (*p == SPECIAL_CHAR) {
4956 w += get_string_width(s);
4959 if (specials[special_index].type == BAR
4960 || specials[special_index].type == GRAPH) {
4961 w += specials[special_index].width;
4962 if (specials[special_index].height > last_font_height) {
4963 last_font_height = specials[special_index].height;
4964 last_font_height += font_ascent();
4966 } else if (specials[special_index].type == OFFSET) {
4967 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
4968 } else if (specials[special_index].type == VOFFSET) {
4969 last_font_height += specials[special_index].arg;
4970 } else if (specials[special_index].type == GOTO) {
4971 if (specials[special_index].arg >= 0)
4972 w += (int)specials[special_index].arg - cur_x;
4973 } else if (specials[special_index].type == TAB) {
4974 int start = specials[special_index].arg;
4975 int step = specials[special_index].width;
4976 if (!step || step < 0)
4978 w += step - (cur_x - text_start_x - start) % step;
4979 } else if (specials[special_index].type == FONT) {
4980 selected_font = specials[special_index].font_added;
4981 if (font_height() > last_font_height) {
4982 last_font_height = font_height();
4991 w += get_string_width(s);
4992 if (w > text_width) {
4995 if (text_width > maximum_width && maximum_width) {
4996 text_width = maximum_width;
4999 text_height += last_font_height;
5000 last_font_height = font_height();
5004 static inline void set_foreground_color(long c)
5007 XSetForeground(display, window.gc, c);
5011 static void draw_string(const char *s)
5015 int i, i2, pos, width_of_s;
5018 width_of_s = get_string_width(s);
5019 if (out_to_console) {
5021 fflush(stdout); /* output immediately, don't buffer */
5023 /* 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 */
5024 memset(tmpstring1,0,TEXT_BUFFER_SIZE);
5025 memset(tmpstring2,0,TEXT_BUFFER_SIZE);
5026 strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
5030 snprintf(space, 2, " ");
5032 max = ((text_width - width_of_s) / get_string_width(space));
5035 * This code looks for tabs in the text and coverts them to spaces.
5036 * The trick is getting the correct number of spaces,
5037 * and not going over the window's size without forcing
5038 * the window larger.
5040 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
5041 if (tmpstring1[i] == '\t') // 9 is ascii tab
5045 i2 < (8 - (1 + pos) % 8) && added <= max;
5048 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
5049 fprintf(stderr,"buffer overrun detected\n");
5051 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
5056 if (tmpstring1[i] != 9) {
5058 if ( pos > TEXT_BUFFER_SIZE-1 )
5059 fprintf(stderr,"buffer overrun detected\n");
5061 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
5067 if (text_width == maximum_width) {
5068 /* this means the text is probably pushing the limit, so we'll chop it */
5069 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
5070 tmpstring2[strlen(tmpstring2)-1] = '\0';
5080 c.pixel = current_color;
5081 XQueryColor(display, DefaultColormap(display, screen), &c);
5084 c2.color.red = c.red;
5085 c2.color.green = c.green;
5086 c2.color.blue = c.blue;
5087 c2.color.alpha = fonts[selected_font].font_alpha;
5089 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5090 cur_x, cur_y, (XftChar8 *) s,
5093 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5094 cur_x, cur_y, (XftChar8 *) s,
5100 XDrawString(display, window.drawable, window.gc,
5101 cur_x, cur_y, s, strlen(s));
5103 cur_x += width_of_s;
5105 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
5108 long redmask, greenmask, bluemask;
5110 void set_up_gradient()
5113 colour_depth = DisplayPlanes(display, screen);
5117 if (colour_depth != 24 && colour_depth != 16) {
5118 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
5124 for(i = (colour_depth / 3)-1; i>=0; i--) {
5126 greenmask |= 1 << i;
5129 if (colour_depth%3 == 1) {
5130 greenmask |= 1 << (colour_depth / 3);
5132 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
5133 greenmask = greenmask << (colour_depth / 3);
5136 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 */
5138 int red1, green1, blue1; // first colour
5139 int red2, green2, blue2; // second colour
5140 int red3 = 0, green3 = 0, blue3 = 0; // difference
5141 short redshift = (2*colour_depth / 3 + colour_depth%3);
5142 short greenshift = (colour_depth / 3);
5143 red1 = (first_colour & redmask) >> redshift;
5144 green1 = (first_colour & greenmask) >> greenshift;
5145 blue1 = first_colour & bluemask;
5146 red2 = (last_colour & redmask) >> redshift;
5147 green2 = (last_colour & greenmask) >> greenshift;
5148 blue2 = last_colour & bluemask;
5155 if (green1 > green2) {
5158 if (green1 < green2) {
5161 if (blue1 > blue2) {
5164 if (blue1 < blue2) {
5179 if (red1 > bluemask) {
5182 if (green1 > bluemask) {
5185 if (blue1 > bluemask) {
5188 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
5192 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
5193 if (colour_depth == 0) {
5196 int red1, green1, blue1; // first colour
5197 int red2, green2, blue2; // second colour
5198 long redshift = (2*colour_depth / 3 + colour_depth%3);
5199 long greenshift = (colour_depth / 3);
5200 int red3 = 0, green3 = 0, blue3 = 0; // difference
5201 red1 = (first_colour & redmask) >> redshift;
5202 green1 = (first_colour & greenmask) >> greenshift;
5203 blue1 = first_colour & bluemask;
5204 red2 = (last_colour & redmask) >> redshift;
5205 green2 = (last_colour & greenmask) >> greenshift;
5206 blue2 = last_colour & bluemask;
5207 red3 = abs(red1 - red2);
5208 green3 = abs(green1 - green2);
5209 blue3 = abs(blue1 - blue2);
5218 static void draw_line(char *s)
5222 cur_x = text_start_x;
5223 cur_y += font_ascent();
5225 short font_h = font_height();
5227 /* find specials and draw stuff */
5230 if (*p == SPECIAL_CHAR) {
5233 /* draw string before special */
5240 switch (specials[special_index].type) {
5241 case HORIZONTAL_LINE:
5244 specials[special_index].height;
5245 int mid = font_ascent() / 2;
5246 w = text_start_x + text_width -
5249 XSetLineAttributes(display,
5254 XDrawLine(display, window.drawable,
5265 specials[special_index].height;
5267 specials[special_index].arg;
5268 int mid = font_ascent() / 2;
5269 char ss[2] = { s, s };
5270 w = text_start_x + text_width -
5273 XSetLineAttributes(display,
5278 XSetDashes(display, window.gc, 0,
5280 XDrawLine(display, window.drawable,
5290 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5294 specials[special_index].height;
5296 specials[special_index].arg;
5297 int by = cur_y - (font_ascent() / 2) - 1;
5298 if (h < font_height()) {
5301 w = specials[special_index].width;
5304 text_width - cur_x - 1;
5308 XSetLineAttributes(display,
5314 XDrawRectangle(display,
5318 XFillRectangle(display,
5322 w * bar_usage / 255,
5324 if (specials[special_index].
5326 && specials[special_index].
5330 [special_index].height;
5337 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5341 specials[special_index].height;
5342 unsigned long last_colour = current_color;
5343 int by = cur_y - (font_ascent()/2) - 1;
5344 if (h < font_height()) {
5347 w = specials[special_index].width;
5349 w = text_start_x + text_width - cur_x - 1;
5352 if (draw_graph_borders) {
5353 XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
5354 XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
5356 XSetLineAttributes(display,
5363 int gradient_size = 0;
5364 float gradient_factor = 0;
5365 float gradient_update = 0;
5366 unsigned long tmpcolour = current_color;
5367 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5368 tmpcolour = specials[special_index].last_colour;
5369 gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
5370 gradient_factor = (float)gradient_size / (w - 2);
5372 for (i = w - 2; i > -1; i--) {
5373 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5374 XSetForeground(display, window.gc, tmpcolour);
5375 gradient_update += gradient_factor;
5376 while (gradient_update > 0) {
5377 tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
5381 if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
5384 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 */
5386 if (specials[special_index].
5388 && specials[special_index].
5392 [special_index].height;
5394 /* if (draw_mode == BG) {
5395 set_foreground_color(default_bg_color);
5397 else if (draw_mode == OUTLINE) {
5398 set_foreground_color(default_out_color);
5400 set_foreground_color(default_fg_color);
5402 set_foreground_color(last_colour);
5408 int old = font_ascent();
5409 cur_y -= font_ascent();
5410 selected_font = specials[special_index].font_added;
5411 if (cur_y + font_ascent() < cur_y + old) {
5414 cur_y += font_ascent();
5420 if (draw_mode == FG)
5421 set_foreground_color(specials
5427 if (draw_mode == BG)
5428 set_foreground_color(specials
5434 if (draw_mode == OUTLINE)
5435 set_foreground_color(specials
5441 w += specials[special_index].arg;
5445 cur_y += specials[special_index].arg;
5449 if (specials[special_index].arg >= 0)
5450 cur_x = (int)specials[special_index].arg;
5455 int start = specials[special_index].arg;
5456 int step = specials[special_index].width;
5457 if (!step || step < 0)
5459 w = step - (cur_x - text_start_x - start) % step;
5465 int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
5466 /*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);*/
5467 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5468 cur_x = pos_x - specials[special_index].arg;
5475 int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
5476 /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
5477 /*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);*/
5479 specials[special_index].arg)
5482 [special_index].arg;
5499 if (cur_y_add > 0) {
5501 cur_y -= font_descent();
5506 cur_y += font_descent();
5510 static void draw_text()
5513 cur_y = text_start_y;
5516 if (draw_borders && border_width > 0) {
5517 unsigned int b = (border_width + 1) / 2;
5519 if (stippled_borders) {
5521 { stippled_borders, stippled_borders };
5522 XSetLineAttributes(display, window.gc,
5523 border_width, LineOnOffDash,
5524 CapButt, JoinMiter);
5525 XSetDashes(display, window.gc, 0, ss, 2);
5527 XSetLineAttributes(display, window.gc,
5528 border_width, LineSolid,
5529 CapButt, JoinMiter);
5532 XDrawRectangle(display, window.drawable, window.gc,
5533 text_start_x - border_margin + b,
5534 text_start_y - border_margin + b,
5535 text_width + border_margin * 2 - 1 - b * 2,
5536 text_height + border_margin * 2 - 1 -
5543 for_each_line(text_buffer, draw_line);
5546 static void draw_stuff()
5550 if (draw_shades && !draw_outline) {
5553 set_foreground_color(default_bg_color);
5563 for (i = -1; i < 2; i++)
5564 for (j = -1; j < 2; j++) {
5565 if (i == 0 && j == 0)
5569 set_foreground_color(default_out_color);
5570 draw_mode = OUTLINE;
5577 set_foreground_color(default_fg_color);
5585 swap.swap_window = window.window;
5586 swap.swap_action = XdbeBackground;
5587 XdbeSwapBuffers(display, &swap, 1);
5593 static void clear_text(int exposures)
5597 return; /* The swap action is XdbeBackground, which clears */
5601 /* there is some extra space for borders and outlines */
5602 XClearArea(display, window.drawable,
5603 text_start_x - border_margin - 1,
5604 text_start_y - border_margin - 1,
5605 text_width + border_margin * 2 + 2,
5606 text_height + border_margin * 2 + 2,
5607 exposures ? True : 0);
5612 static int need_to_update;
5614 /* update_text() generates new text and clears old text area */
5615 static void update_text()
5624 static void main_loop()
5626 #ifdef SIGNAL_BLOCKING
5627 sigset_t newmask, oldmask;
5629 sigemptyset(&newmask);
5630 sigaddset(&newmask,SIGINT);
5631 sigaddset(&newmask,SIGTERM);
5632 sigaddset(&newmask,SIGUSR1);
5636 Region region = XCreateRegion();
5638 int event_base, error_base;
5639 if (!XDamageQueryExtension (display, &event_base, &error_base)) {
5640 ERR("Xdamage extension unavailable");
5642 Damage damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5643 XserverRegion region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5644 XserverRegion part = XFixesCreateRegionFromWindow(display, window.window, 0);
5645 #endif /* HAVE_XDAMAGE */
5649 while (total_run_times == 0 || info.looped < total_run_times) {
5652 #ifdef SIGNAL_BLOCKING
5653 /* block signals. we will inspect for pending signals later */
5654 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
5655 CRIT_ERR("unable to sigprocmask()");
5661 /* wait for X event or timeout */
5663 if (!XPending(display)) {
5668 update_interval - (get_time() -
5674 tv.tv_sec = (long) t;
5675 tv.tv_usec = (long) (t * 1000000) % 1000000;
5677 FD_SET(ConnectionNumber(display), &fdsr);
5680 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
5683 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
5688 ERR("can't select(): %s",
5699 if (need_to_update) {
5701 int wx = window.x, wy = window.y;
5709 /* resize window if it isn't right size */
5711 (text_width + border_margin * 2 + 1 !=
5713 || text_height + border_margin * 2 + 1 !=
5717 border_margin * 2 + 1;
5720 border_margin * 2 + 1;
5721 XResizeWindow(display,
5726 set_transparent_background(window.drawable);
5730 /* move window if it isn't in right position */
5733 || window.y != wy)) {
5734 XMoveWindow(display, window.window,
5735 window.x, window.y);
5745 r.x = text_start_x - border_margin;
5746 r.y = text_start_y - border_margin;
5747 r.width = text_width + border_margin * 2;
5748 r.height = text_height + border_margin * 2;
5749 XUnionRectWithRegion(&r, region, region);
5754 /* handle X events */
5756 while (XPending(display)) {
5758 XNextEvent(display, &ev);
5765 r.width = ev.xexpose.width;
5766 r.height = ev.xexpose.height;
5767 XUnionRectWithRegion(&r, region,
5773 case ReparentNotify:
5774 /* set background to ParentRelative for all parents */
5776 set_transparent_background(window.
5781 case ConfigureNotify:
5783 /* if window size isn't what expected, set fixed size */
5784 if (ev.xconfigure.width !=
5786 || ev.xconfigure.height !=
5788 if (window.width != 0
5789 && window.height != 0)
5792 /* clear old stuff before screwing up size and pos */
5798 if (XGetWindowAttributes(display, window.window, &attrs)) {
5813 border_margin * 2 - 1;
5816 border_margin * 2 - 1;
5817 if (text_width > maximum_width && maximum_width > 0)
5818 text_width = maximum_width;
5821 /* if position isn't what expected, set fixed pos, total_updates
5822 * avoids setting fixed_pos when window is set to weird locations
5824 /*if (total_updates >= 2 this is broken
5826 && (window.x != ev.xconfigure.x
5829 && (ev.xconfigure.x != 0
5830 || ev.xconfigure.y != 0)) {
5840 /* forward the click to the desktop window */
5841 XUngrabPointer(display, ev.xbutton.time);
5842 ev.xbutton.window = window.desktop;
5843 XSendEvent(display, ev.xbutton.window, False, ButtonPressMask, &ev);
5850 /* forward the release to the desktop window */
5851 ev.xbutton.window = window.desktop;
5852 XSendEvent(display, ev.xbutton.window, False, ButtonReleaseMask, &ev);
5860 if (ev.type == event_base + XDamageNotify) {
5861 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
5862 XFixesSetRegion(display, part, &dev->area, 1);
5863 XFixesUnionRegion(display, region2, region2, part);
5865 #endif /* HAVE_XDAMAGE */
5870 XDamageSubtract(display, damage, region2, None);
5871 XFixesSetRegion(display, region2, 0, 0);
5872 #endif /* HAVE_XDAMAGE */
5874 /* XDBE doesn't seem to provide a way to clear the back buffer without
5875 * interfering with the front buffer, other than passing XdbeBackground
5876 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
5877 * redraw the text even if it wasn't part of the exposed area. OTOH,
5878 * if we're not going to call draw_stuff at all, then no swap happens
5879 * and we can safely do nothing.
5882 if (!XEmptyRegion(region)) {
5886 r.x = text_start_x - border_margin;
5887 r.y = text_start_y - border_margin;
5888 r.width = text_width + border_margin * 2;
5889 r.height = text_height + border_margin * 2;
5890 XUnionRectWithRegion(&r, region, region);
5893 XSetRegion(display, window.gc, region);
5896 XftDrawSetClip(window.xftdraw, region);
5902 XDestroyRegion(region);
5903 region = XCreateRegion();
5907 #ifdef SIGNAL_BLOCKING
5908 /* unblock signals of interest and let handler fly */
5909 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
5910 CRIT_ERR("unable to sigprocmask()");
5913 switch(g_signal_pending) {
5916 ERR("received SIGUSR1. reloading the config file.");
5923 ERR("received SIGINT or SIGTERM to terminate. bye!");
5926 XDestroyRegion(region);
5929 return; /* return from main_loop */
5934 /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
5935 * but didn't write any code to deal with it. if you don't want to
5936 * handle a signal, don't set a handler on it in the first place. */
5937 if (g_signal_pending)
5938 ERR("ignoring signal (%d)", g_signal_pending);
5945 #if defined(X11) && defined(HAVE_XDAMAGE)
5946 XDamageDestroy(display, damage);
5947 XFixesDestroyRegion(display, region2);
5948 XFixesDestroyRegion(display, part);
5949 XDestroyRegion(region);
5951 #endif /* X11 && HAVE_XDAMAGE */
5954 static void load_config_file(const char *);
5956 /* reload the config file */
5957 void reload_config(void)
5959 //lock_all_threads();
5961 if (info.cpu_usage) {
5962 free(info.cpu_usage);
5963 info.cpu_usage = NULL;
5966 if ( (info.audacious.thread) && (destroy_audacious_thread()!=0) )
5967 ERR("error destroying audacious thread");
5969 #ifdef TCP_PORT_MONITOR
5970 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
5973 if (current_config) {
5975 load_config_file(current_config);
5979 XClearWindow(display, RootWindow(display, screen)); // clear the window first
5982 #ifdef TCP_PORT_MONITOR
5983 info.p_tcp_port_monitor_collection = NULL;
5986 if ( (!info.audacious.thread) && (create_audacious_thread() !=0) )
5987 CRIT_ERR("unable to create audacious thread!");
5989 extract_variable_text(text);
5999 //lock_all_threads();
6001 if (info.cpu_usage) {
6002 free(info.cpu_usage);
6003 info.cpu_usage = NULL;
6008 XdbeDeallocateBackBufferName(display, window.back_buffer);
6014 XDestroyWindow(display, window.window);
6015 XClearWindow(display, RootWindow(display, screen));
6021 XClearWindow(display, RootWindow(display, screen));
6026 XFreeGC(display, window.gc);
6030 /* it is really pointless to free() memory at the end of program but ak|ra
6031 * wants me to do this */
6033 free_text_objects(text_object_count, text_objects);
6034 text_object_count = 0;
6035 text_objects = NULL;
6037 if (text != original_text)
6040 free(current_config);
6041 free(current_mail_spool);
6042 #ifdef TCP_PORT_MONITOR
6043 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6044 info.p_tcp_port_monitor_collection = NULL;
6047 if ( info.audacious.thread && (destroy_audacious_thread()!=0) )
6048 ERR("error destroying audacious thread");
6052 static int string_to_bool(const char *s)
6056 if (strcasecmp(s, "yes") == 0)
6058 if (strcasecmp(s, "true") == 0)
6060 if (strcasecmp(s, "1") == 0)
6065 static enum alignment string_to_alignment(const char *s)
6067 if (strcasecmp(s, "top_left") == 0)
6069 else if (strcasecmp(s, "top_right") == 0)
6071 else if (strcasecmp(s, "bottom_left") == 0)
6073 else if (strcasecmp(s, "bottom_right") == 0)
6074 return BOTTOM_RIGHT;
6075 else if (strcasecmp(s, "tl") == 0)
6077 else if (strcasecmp(s, "tr") == 0)
6079 else if (strcasecmp(s, "bl") == 0)
6081 else if (strcasecmp(s, "br") == 0)
6082 return BOTTOM_RIGHT;
6083 else if (strcasecmp(s, "none") == 0)
6090 static void set_default_configurations(void)
6092 fork_to_background = 0;
6093 total_run_times = 0;
6094 info.cpu_avg_samples = 2;
6095 info.net_avg_samples = 2;
6100 strcpy(info.mpd.host, "localhost");
6101 info.mpd.port = 6600;
6102 info.mpd.status = NULL;
6103 info.mpd.artist = NULL;
6104 info.mpd.album = NULL;
6105 info.mpd.title = NULL;
6106 info.mpd.random = NULL;
6107 info.mpd.track = NULL;
6108 info.mpd.name = NULL;
6109 info.mpd.file = NULL;
6112 info.xmms2.artist = NULL;
6113 info.xmms2.album = NULL;
6114 info.xmms2.title = NULL;
6115 info.xmms2.genre = NULL;
6116 info.xmms2.comment = NULL;
6117 info.xmms2.decoder = NULL;
6118 info.xmms2.transport = NULL;
6119 info.xmms2.url = NULL;
6120 info.xmms2.status = NULL;
6129 default_fg_color = WhitePixel(display, screen);
6130 default_bg_color = BlackPixel(display, screen);
6131 default_out_color = BlackPixel(display, screen);
6134 draw_graph_borders = 1;
6136 set_first_font("6x10");
6144 window.type=TYPE_NORMAL;
6146 strcpy(window.wm_class_name, "conky");
6148 stippled_borders = 0;
6151 text_alignment = BOTTOM_LEFT;
6155 free(current_mail_spool);
6158 variable_substitute(MAIL_FILE, buf, 256);
6160 current_mail_spool = strdup(buf);
6164 update_interval = 10.0;
6165 stuff_in_upper_case = 0;
6167 #ifdef TCP_PORT_MONITOR
6168 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6169 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6173 static void load_config_file(const char *f)
6175 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6179 set_default_configurations();
6185 char buf[256], *p, *p2, *name, *value;
6187 if (fgets(buf, 256, fp) == NULL)
6192 /* break at comment */
6193 p2 = strchr(p, '#');
6198 while (*p && isspace((int) *p))
6201 continue; /* empty line */
6207 while (*p2 && !isspace((int) *p2))
6210 *p2 = '\0'; /* break at name's end */
6217 while (*p && isspace((int) *p))
6222 p2 = value + strlen(value);
6223 while (isspace((int) *(p2 - 1)))
6229 #define CONF2(a) if (strcasecmp(name, a) == 0)
6230 #define CONF(a) else CONF2(a)
6231 #define CONF3(a,b) \
6232 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
6236 CONF2("alignment") {
6238 int a = string_to_alignment(value);
6248 ERR("on_bottom is deprecated. use own_window_hints below");
6249 on_bottom = string_to_bool(value);
6251 SET_HINT(window.hints,HINT_BELOW);
6256 CONF("background") {
6257 fork_to_background = string_to_bool(value);
6261 CONF2("background") {
6262 fork_to_background = string_to_bool(value);
6266 CONF("border_margin") {
6268 border_margin = strtol(value, 0, 0);
6272 CONF("border_width") {
6274 border_width = strtol(value, 0, 0);
6278 CONF("default_color") {
6280 default_fg_color = get_x11_color(value);
6284 CONF3("default_shade_color", "default_shadecolor") {
6286 default_bg_color = get_x11_color(value);
6290 CONF3("default_outline_color", "default_outlinecolor") {
6292 default_out_color = get_x11_color(value);
6297 CONF("xmms_player") {
6299 ERR("xmms_player is deprecated. see ./configure --help");
6303 info.mail = parse_mail_args(IMAP, value);
6310 info.mail = parse_mail_args(POP3, value);
6318 strncpy(info.mpd.host, value, 127);
6324 info.mpd.port = strtol(value, 0, 0);
6325 if (info.mpd.port < 1
6326 || info.mpd.port > 0xffff)
6330 CONF("mpd_password") {
6332 strncpy(info.mpd.password, value, 127);
6337 CONF("cpu_avg_samples") {
6339 cpu_avg_samples = strtol(value, 0, 0);
6340 if (cpu_avg_samples < 1
6341 || cpu_avg_samples > 14)
6350 CONF("net_avg_samples") {
6352 net_avg_samples = strtol(value, 0, 0);
6353 if (net_avg_samples < 1
6354 || net_avg_samples > 14)
6370 CONF("double_buffer") {
6371 use_xdbe = string_to_bool(value);
6375 CONF("override_utf8_locale") {
6376 utf8_mode = string_to_bool(value);
6379 CONF("draw_borders") {
6380 draw_borders = string_to_bool(value);
6382 CONF("draw_graph_borders") {
6383 draw_graph_borders = string_to_bool(value);
6385 CONF("draw_shades") {
6386 draw_shades = string_to_bool(value);
6388 CONF("draw_outline") {
6389 draw_outline = string_to_bool(value);
6392 CONF("out_to_console") {
6393 out_to_console = string_to_bool(value);
6395 CONF("use_spacer") {
6396 use_spacer = string_to_bool(value);
6401 use_xft = string_to_bool(value);
6405 set_first_font(value);
6410 if (value && font_count >= 0)
6411 fonts[0].font_alpha = atof(value)
6420 if (string_to_bool(value))
6421 ERR("Xft not enabled");
6424 /* xftfont silently ignored when no Xft */
6427 /* xftalpha is silently ignored when no Xft */
6432 set_first_font(value);
6441 gap_x = atoi(value);
6447 gap_y = atoi(value);
6452 CONF("mail_spool") {
6455 variable_substitute(value, buf, 256);
6459 if (current_mail_spool)
6460 free(current_mail_spool);
6461 current_mail_spool = strdup(buf);
6467 CONF("minimum_size") {
6470 (value, "%d %d", &minimum_width,
6471 &minimum_height) != 2)
6474 &minimum_width) != 1)
6479 CONF("maximum_width") {
6481 if (sscanf(value, "%d", &maximum_width) != 1)
6487 CONF("no_buffers") {
6488 no_buffers = string_to_bool(value);
6490 CONF("pad_percents") {
6491 pad_percents = atoi(value);
6495 CONF("own_window") {
6496 own_window = string_to_bool(value);
6498 CONF("wm_class_name") {
6499 memset(window.wm_class_name,0,sizeof(window.wm_class_name));
6500 strncpy(window.wm_class_name, value, sizeof(window.wm_class_name)-1);
6502 CONF("own_window_transparent") {
6503 set_transparent = string_to_bool(value);
6505 CONF("own_window_colour") {
6507 background_colour = get_x11_color(value);
6509 ERR("Invalid colour for own_winder_colour (try omitting the '#' for hex colours");
6512 CONF("own_window_hints") {
6514 char *p_hint, *p_save;
6515 char delim[] = ", ";
6517 /* tokenize the value into individual hints */
6518 if ((p_hint=strtok_r(value, delim, &p_save)) != NULL)
6520 /*fprintf(stderr, "hint [%s] parsed\n", p_hint);*/
6521 if (strncmp(p_hint,"undecorate",10) == 0)
6522 SET_HINT(window.hints,HINT_UNDECORATED);
6523 else if (strncmp(p_hint,"below",5) == 0)
6524 SET_HINT(window.hints,HINT_BELOW);
6525 else if (strncmp(p_hint,"above",5) == 0)
6526 SET_HINT(window.hints,HINT_ABOVE);
6527 else if (strncmp(p_hint,"sticky",6) == 0)
6528 SET_HINT(window.hints,HINT_STICKY);
6529 else if (strncmp(p_hint,"skip_taskbar",12) == 0)
6530 SET_HINT(window.hints,HINT_SKIP_TASKBAR);
6531 else if (strncmp(p_hint,"skip_pager",10) == 0)
6532 SET_HINT(window.hints,HINT_SKIP_PAGER);
6536 p_hint=strtok_r(NULL, delim, &p_save);
6538 while (p_hint!=NULL);
6541 CONF("own_window_type") {
6543 if (strncmp(value,"normal",6)==0)
6544 window.type = TYPE_NORMAL;
6545 else if (strncmp(value,"desktop",7)==0)
6546 window.type = TYPE_DESKTOP;
6547 else if (strncmp(value,"override",8)==0)
6548 window.type = TYPE_OVERRIDE;
6554 CONF("stippled_borders") {
6556 stippled_borders = strtol(value, 0, 0);
6558 stippled_borders = 4;
6562 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
6565 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
6567 CONF("update_interval") {
6569 update_interval = strtod(value, 0);
6573 CONF("total_run_times") {
6575 total_run_times = strtod(value, 0);
6580 stuff_in_upper_case = string_to_bool(value);
6583 if (text != original_text)
6593 int l = strlen(text);
6594 if (fgets(buf, 256, fp) == NULL)
6597 realloc(text, l + strlen(buf)
6601 if (strlen(text) > 1024 * 8)
6605 text_lines = line + 1;
6608 #ifdef TCP_PORT_MONITOR
6609 CONF("min_port_monitors")
6612 (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) ||
6613 tcp_port_monitor_collection_args.min_port_monitors < 0 )
6615 /* an error. use default, warn and continue. */
6616 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6619 else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
6621 /* no error, just use default */
6622 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6624 /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
6626 CONF("min_port_monitor_connections")
6629 (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1)
6630 || tcp_port_monitor_args.min_port_monitor_connections < 0 )
6632 /* an error. use default, warni and continue. */
6633 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6636 else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
6638 /* no error, just use default */
6639 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6641 /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
6645 ERR("%s: %d: no such configuration: '%s'", f, line, name);
6657 /* : means that character before that takes an argument */
6658 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
6671 int main(int argc, char **argv)
6673 struct sigaction act, oact;
6676 memset(&info, 0, sizeof(info) );
6678 #ifdef TCP_PORT_MONITOR
6679 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6680 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6683 /* handle command line parameters that don't change configs */
6688 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
6689 *s) || ((s = getenv("LANG")) && *s)) {
6690 temp = (char *)malloc((strlen(s) + 1) * sizeof(char));
6692 ERR("malloc failed");
6694 for (x = 0; x < strlen(s); x++) {
6695 temp[x] = tolower(s[x]);
6697 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6703 if (!setlocale(LC_CTYPE, "")) {
6704 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6708 int c = getopt(argc,
6719 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
6720 * though free() does the NULL check itself;), then load optarg value */
6722 free(current_config);
6723 current_config = strdup(optarg);
6728 ("Usage: %s [OPTION]...\n"
6729 "Conky is a system monitor that renders text on desktop or to own transparent\n"
6730 "window. Command line options will override configurations defined in config\n"
6733 " -c FILE config file to load instead of "
6736 " -d daemonize, fork to background\n"
6739 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
6740 " -f FONT font to use\n"
6742 " -o create own window to draw\n"
6745 " -b double buffer (prevents flickering)\n"
6747 " -w WIN_ID window id to draw\n"
6748 " -x X x position\n"
6749 " -y Y y position\n"
6751 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
6752 " -u SECS update interval\n"
6753 " -i NUM number of times to update Conky\n", argv[0]);
6757 window.window = strtol(optarg, 0, 0);
6766 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
6770 /* load current_config or CONFIG_FILE */
6773 if (current_config == NULL) {
6774 /* load default config file */
6777 variable_substitute(CONFIG_FILE, buf, 256);
6780 current_config = strdup(buf);
6784 if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
6785 load_config_file(current_config);
6787 set_default_configurations();
6791 if (current_mail_spool == NULL) {
6793 variable_substitute(MAIL_FILE, buf, 256);
6796 current_mail_spool = strdup(buf);
6800 /* handle other command line arguments */
6802 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
6803 optind = optreset = 1;
6808 #if defined(__FreeBSD__)
6809 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
6810 O_RDONLY, "kvm_open")) == NULL)
6811 CRIT_ERR( "cannot read kvm");
6815 int c = getopt(argc,
6823 fork_to_background = 1;
6828 set_first_font(optarg);
6831 text_alignment = string_to_alignment(optarg);
6846 if (text != original_text)
6848 text = strdup(optarg);
6849 convert_escapes(text);
6853 update_interval = strtod(optarg, 0);
6857 total_run_times = strtod(optarg, 0);
6861 gap_x = atoi(optarg);
6865 gap_y = atoi(optarg);
6879 /* generate text and get initial size */
6880 extract_variable_text(text);
6881 if (text != original_text) {
6886 if (fork_to_background) {
6890 ERR("can't fork() to background: %s",
6897 fprintf(stderr,"\n");fflush(stderr);
6901 /* parent process */
6902 fprintf(stderr,"Conky: forked to background, pid is %d\n",pid);
6913 update_text_area(); /* to get initial size of the window */
6917 text_width + border_margin * 2 + 1,
6918 text_height + border_margin * 2 + 1,
6919 set_transparent, background_colour, info.uname_s.nodename, argv, argc);
6922 update_text_area(); /* to position text/window on screen */
6927 if (own_window && !fixed_pos) {
6928 XMoveWindow(display, window.window, window.x, window.y);
6931 set_transparent_background(window.window);
6942 /* Set signal handlers */
6943 act.sa_handler = signal_handler;
6944 sigemptyset(&act.sa_mask);
6947 act.sa_flags |= SA_RESTART;
6950 if ( sigaction(SIGINT,&act,&oact) < 0 ||
6951 sigaction(SIGUSR1,&act,&oact) < 0 ||
6952 sigaction(SIGTERM,&act,&oact) < 0 )
6954 ERR("error setting signal handler: %s", strerror(errno) );
6958 if ( (create_audacious_thread() !=0) )
6960 CRIT_ERR("unable to create audacious thread!");
6967 if ( info.audacious.thread && (destroy_audacious_thread()!=0) )
6968 ERR("error destroying audacious thread");
6971 #if defined(__FreeBSD__)
6978 void signal_handler(int sig)
6980 /* signal handler is light as a feather, as it should be.
6981 * we will poll g_signal_pending with each loop of conky
6982 * and do any signal processing there, NOT here. */
6983 g_signal_pending=sig;