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 /* Position on the screen */
286 static int text_alignment;
287 static int gap_x, gap_y;
290 static int draw_borders;
291 static int draw_graph_borders;
292 static int stippled_borders;
294 static int draw_shades, draw_outline;
296 static int border_margin, border_width;
298 static long default_fg_color, default_bg_color, default_out_color;
300 /* create own window or draw stuff to root? */
301 static int set_transparent = 0;
305 static int own_window = 0;
306 static int background_colour = 0;
307 /* fixed size/pos is set if wm/user changes them */
308 static int fixed_size = 0, fixed_pos = 0;
311 static int minimum_width, minimum_height;
312 static int maximum_width;
316 /* maximum size of config TEXT buffer, i.e. below TEXT line. */
317 static unsigned int max_user_text = MAX_USER_TEXT_DEFAULT;
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 */
1148 timed_thread *p_timed_thread;
1154 #ifdef TCP_PORT_MONITOR
1156 in_port_t port_range_begin; /* starting port to monitor */
1157 in_port_t port_range_end; /* ending port to monitor */
1158 int item; /* enum value from libtcp-portmon.h, e.g. COUNT, REMOTEIP, etc. */
1159 int connection_index; /* 0 to n-1 connections. */
1172 struct text_object_list {
1173 unsigned int text_object_count;
1174 struct text_object *text_objects;
1177 static unsigned int text_object_count;
1178 static struct text_object *text_objects;
1179 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
1181 #define MAXDATASIZE 1000
1185 struct mail_s* parse_mail_args(char type, const char *arg) {
1186 struct mail_s *mail;
1187 mail = malloc(sizeof(struct mail_s));
1188 memset(mail, 0, sizeof(struct mail_s));
1190 if (sscanf(arg, "%128s %128s %128s", mail->host, mail->user, mail->pass) != 3) {
1192 ERR("Scanning IMAP args failed");
1193 } else if (type == IMAP) {
1194 ERR("Scanning POP3 args failed");
1197 // see if password needs prompting
1198 if (mail->pass[0] == '*' && mail->pass[1] == '\0') {
1199 int fp = fileno(stdin);
1200 struct termios term;
1201 tcgetattr(fp, &term);
1202 term.c_lflag &= ~ECHO;
1203 tcsetattr(fp, TCSANOW, &term);
1204 printf("Enter mailbox password (%s@%s): ", mail->user, mail->host);
1205 scanf("%128s", mail->pass);
1207 term.c_lflag |= ECHO;
1208 tcsetattr(fp, TCSANOW, &term);
1210 // now we check for optional args
1211 tmp = strstr(arg, "-i ");
1214 sscanf(tmp, "%f", &mail->interval);
1216 mail->interval = 300; // 5 minutes
1218 tmp = strstr(arg, "-p ");
1221 sscanf(tmp, "%lu", &mail->port);
1224 mail->port = 110; // default pop3 port
1225 } else if (type == IMAP) {
1226 mail->port = 143; // default imap port
1230 tmp = strstr(arg, "-f ");
1233 sscanf(tmp, "%s", mail->folder);
1235 strncpy(mail->folder, "INBOX", 128); // default imap inbox
1238 tmp = strstr(arg, "-e ");
1242 if (tmp[0] == '\'') {
1243 len = strstr(tmp+1, "'") - tmp - 1;
1248 strncpy(mail->command, tmp+1, len);
1250 mail->command[0] = '\0';
1252 mail->p_timed_thread = NULL;
1256 void *imap_thread(struct mail_s* mail)
1258 int sockfd, numbytes;
1259 char recvbuf[MAXDATASIZE];
1260 char sendbuf[MAXDATASIZE];
1263 unsigned int old_unseen = UINT_MAX;
1264 unsigned int old_messages = UINT_MAX;
1266 struct sockaddr_in their_addr; // connector's address information
1267 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1268 herror("gethostbyname");
1273 ERR("Trying IMAP connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1274 sleep((int)mail->interval);
1276 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1282 their_addr.sin_family = AF_INET; // host byte order
1283 their_addr.sin_port = htons(mail->port); // short, network byte order
1284 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1285 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1288 (sockfd, (struct sockaddr *) &their_addr,
1289 sizeof(struct sockaddr)) == -1) {
1294 struct timeval timeout;
1297 timeout.tv_sec = 60; // 60 second timeout i guess
1298 timeout.tv_usec = 0;
1300 FD_SET(sockfd, &fdset);
1301 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1304 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1311 ERR("IMAP connection failed: timeout");
1315 recvbuf[numbytes] = '\0';
1316 if (strstr(recvbuf, "* OK") != recvbuf) {
1317 ERR("IMAP connection failed, probably not an IMAP server");
1321 strncpy(sendbuf, "a1 login ", MAXDATASIZE);
1322 strncat(sendbuf, mail->user,
1323 MAXDATASIZE - strlen(sendbuf) - 1);
1324 strncat(sendbuf, " ", MAXDATASIZE - strlen(sendbuf) - 1);
1325 strncat(sendbuf, mail->pass,
1326 MAXDATASIZE - strlen(sendbuf) - 1);
1327 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1328 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1333 timeout.tv_sec = 60; // 60 second timeout i guess
1334 timeout.tv_usec = 0;
1336 FD_SET(sockfd, &fdset);
1337 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1340 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1347 recvbuf[numbytes] = '\0';
1348 if (strstr(recvbuf, "a1 OK") == NULL) {
1349 ERR("IMAP server login failed: %s", recvbuf);
1353 strncpy(sendbuf, "a2 STATUS ", MAXDATASIZE);
1354 strncat(sendbuf, mail->folder,
1355 MAXDATASIZE - strlen(sendbuf) - 1);
1356 strncat(sendbuf, " (MESSAGES UNSEEN)\n",
1357 MAXDATASIZE - strlen(sendbuf) - 1);
1358 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1363 timeout.tv_sec = 60; // 60 second timeout i guess
1364 timeout.tv_usec = 0;
1366 FD_SET(sockfd, &fdset);
1367 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1370 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1377 recvbuf[numbytes] = '\0';
1378 if (strstr(recvbuf, "a2 OK") == NULL) {
1379 ERR("IMAP status failed: %s", recvbuf);
1383 // now we get the data
1384 reply = strstr(recvbuf, " (MESSAGES ");
1386 *strchr(reply, ')') = '\0';
1387 if (reply == NULL) {
1388 ERR("Error parsing IMAP response: %s", recvbuf);
1392 timed_thread_lock (mail->p_timed_thread);
1393 sscanf(reply, "MESSAGES %lu UNSEEN %lu",
1396 timed_thread_unlock (mail->p_timed_thread);
1398 strncpy(sendbuf, "a3 logout\n", MAXDATASIZE);
1399 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1404 timeout.tv_sec = 60; // 60 second timeout i guess
1405 timeout.tv_usec = 0;
1407 FD_SET(sockfd, &fdset);
1408 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1411 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1418 recvbuf[numbytes] = '\0';
1419 if (strstr(recvbuf, "a3 OK") == NULL) {
1420 ERR("IMAP logout failed: %s", recvbuf);
1425 if (strlen(mail->command) > 1 && (mail->unseen > old_unseen || (mail->messages > old_messages && mail->unseen > 0))) { // new mail goodie
1426 if (system(mail->command) == -1) {
1431 old_unseen = mail->unseen;
1432 old_messages = mail->messages;
1433 if (timed_thread_test (mail->p_timed_thread))
1434 timed_thread_exit (mail->p_timed_thread);
1439 void *pop3_thread(struct mail_s *mail)
1441 int sockfd, numbytes;
1442 char recvbuf[MAXDATASIZE];
1443 char sendbuf[MAXDATASIZE];
1446 unsigned int old_unseen = UINT_MAX;
1448 struct sockaddr_in their_addr; // connector's address information
1449 if ((he = gethostbyname(mail->host)) == NULL) { // get the host info
1450 herror("gethostbyname");
1455 ERR("Trying POP3 connection again for %s@%s (try %i/5)", mail->user, mail->host, fail + 1);
1456 sleep((int)mail->interval);
1458 if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
1464 their_addr.sin_family = AF_INET; // host byte order
1465 their_addr.sin_port = htons(mail->port); // short, network byte order
1466 their_addr.sin_addr = *((struct in_addr *) he->h_addr);
1467 memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
1470 (sockfd, (struct sockaddr *) &their_addr,
1471 sizeof(struct sockaddr)) == -1) {
1476 struct timeval timeout;
1479 timeout.tv_sec = 60; // 60 second timeout i guess
1480 timeout.tv_usec = 0;
1482 FD_SET(sockfd, &fdset);
1483 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1486 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1493 ERR("POP3 connection failed: timeout\n");
1497 recvbuf[numbytes] = '\0';
1498 if (strstr(recvbuf, "+OK ") != recvbuf) {
1499 ERR("POP3 connection failed, probably not a POP3 server");
1503 strncpy(sendbuf, "USER ", MAXDATASIZE);
1504 strncat(sendbuf, mail->user,
1505 MAXDATASIZE - strlen(sendbuf) - 1);
1506 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1507 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1508 perror("send USER");
1512 timeout.tv_sec = 60; // 60 second timeout i guess
1513 timeout.tv_usec = 0;
1515 FD_SET(sockfd, &fdset);
1516 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1519 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1521 perror("recv USER");
1526 recvbuf[numbytes] = '\0';
1527 if (strstr(recvbuf, "+OK ") == NULL) {
1528 ERR("POP3 server login failed: %s", recvbuf);
1532 strncpy(sendbuf, "PASS ", MAXDATASIZE);
1533 strncat(sendbuf, mail->pass,
1534 MAXDATASIZE - strlen(sendbuf) - 1);
1535 strncat(sendbuf, "\n", MAXDATASIZE - strlen(sendbuf) - 1);
1536 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1537 perror("send PASS");
1541 timeout.tv_sec = 60; // 60 second timeout i guess
1542 timeout.tv_usec = 0;
1544 FD_SET(sockfd, &fdset);
1545 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1548 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1550 perror("recv PASS");
1555 recvbuf[numbytes] = '\0';
1556 if (strstr(recvbuf, "+OK ") == NULL) {
1557 ERR("POP3 server login failed: %s", recvbuf);
1561 strncpy(sendbuf, "STAT\n", MAXDATASIZE);
1562 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1563 perror("send STAT");
1567 timeout.tv_sec = 60; // 60 second timeout i guess
1568 timeout.tv_usec = 0;
1570 FD_SET(sockfd, &fdset);
1571 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1574 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1576 perror("recv STAT");
1581 recvbuf[numbytes] = '\0';
1582 if (strstr(recvbuf, "+OK ") == NULL) {
1583 ERR("POP3 status failed: %s", recvbuf);
1587 // now we get the data
1588 reply = recvbuf + 4;
1589 if (reply == NULL) {
1590 ERR("Error parsing POP3 response: %s", recvbuf);
1594 timed_thread_lock (mail->p_timed_thread);
1595 sscanf(reply, "%lu %lu", &mail->unseen,
1597 timed_thread_unlock (mail->p_timed_thread);
1599 strncpy(sendbuf, "QUIT\n", MAXDATASIZE);
1600 if (send(sockfd, sendbuf, strlen(sendbuf), 0) == -1) {
1601 perror("send QUIT");
1605 timeout.tv_sec = 60; // 60 second timeout i guess
1606 timeout.tv_usec = 0;
1608 FD_SET(sockfd, &fdset);
1609 res = select(sockfd + 1, &fdset, NULL, NULL, &timeout);
1612 recv(sockfd, recvbuf, MAXDATASIZE - 1,
1614 perror("recv QUIT");
1619 recvbuf[numbytes] = '\0';
1620 if (strstr(recvbuf, "+OK") == NULL) {
1621 ERR("POP3 logout failed: %s", recvbuf);
1626 if (strlen(mail->command) > 1 && mail->unseen > old_unseen) { // new mail goodie
1627 if (system(mail->command) == -1) {
1632 old_unseen = mail->unseen;
1633 if (timed_thread_test (mail->p_timed_thread))
1634 timed_thread_exit (mail->p_timed_thread);
1640 void *threaded_exec(struct text_object *obj) {
1642 char *p2 = obj->data.texeci.buffer;
1643 FILE *fp = popen(obj->data.texeci.cmd,"r");
1644 timed_thread_lock (obj->data.texeci.p_timed_thread);
1645 int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
1648 if (n2 && p2[n2 - 1] == '\n') {
1652 if (*p2 == '\001') {
1657 timed_thread_unlock (obj->data.texeci.p_timed_thread);
1658 if (timed_thread_test (obj->data.texeci.p_timed_thread))
1659 timed_thread_exit (obj->data.texeci.p_timed_thread);
1664 static struct text_object *new_text_object_internal()
1666 struct text_object *obj = malloc(sizeof(struct text_object));
1667 memset(obj, 0, sizeof(struct text_object));
1671 static void free_text_objects(unsigned int count, struct text_object *objs)
1674 for (i = 0; i < count; i++) {
1675 switch (objs[i].type) {
1677 close(objs[i].data.i);
1680 close(objs[i].data.i);
1683 close(objs[i].data.i2c.fd);
1686 free(objs[i].data.s);
1689 free(objs[i].data.s);
1692 free(objs[i].data.tztime.tz);
1693 free(objs[i].data.tztime.fmt);
1698 case OBJ_imap_unseen:
1699 if (!objs[i].global_mode) {
1700 free(objs[i].data.mail);
1703 case OBJ_imap_messages:
1704 if (!objs[i].global_mode) {
1705 free(objs[i].data.mail);
1711 case OBJ_pop3_unseen:
1712 if (!objs[i].global_mode) {
1713 free(objs[i].data.mail);
1717 if (!objs[i].global_mode) {
1718 free(objs[i].data.mail);
1721 case OBJ_if_existing:
1722 case OBJ_if_mounted:
1723 case OBJ_if_running:
1724 free(objs[i].data.ifblock.s);
1727 free(objs[i].data.tail.logfile);
1728 free(objs[i].data.tail.buffer);
1730 case OBJ_text: case OBJ_font:
1731 free(objs[i].data.s);
1734 free(objs[i].data.s);
1737 free(objs[i].data.s);
1740 free(objs[i].data.s);
1743 free(objs[i].data.s);
1745 /* case OBJ_execibar:
1746 free(objs[i].data.s);
1748 case OBJ_execigraph:
1749 free(objs[i].data.s);
1752 case OBJ_iconv_start:
1758 if (info.mpd.title) {
1759 free(info.mpd.title);
1763 case OBJ_mpd_artist:
1764 if (info.mpd.artist) {
1765 free(info.mpd.artist);
1766 info.mpd.artist = 0;
1770 if (info.mpd.album) {
1771 free(info.mpd.album);
1775 case OBJ_mpd_random:
1776 if (info.mpd.random) {
1777 free(info.mpd.random);
1778 info.mpd.random = 0;
1781 case OBJ_mpd_repeat:
1782 if (info.mpd.repeat) {
1783 free(info.mpd.repeat);
1784 info.mpd.repeat = 0;
1788 if (info.mpd.track) {
1789 free(info.mpd.track);
1794 if (info.mpd.name) {
1795 free(info.mpd.name);
1800 if (info.mpd.file) {
1801 free(info.mpd.file);
1805 case OBJ_mpd_status:
1806 if (info.mpd.status) {
1807 free(info.mpd.status);
1808 info.mpd.status = 0;
1812 if (info.mpd.artist) {
1813 free(info.mpd.artist);
1814 info.mpd.artist = 0;
1816 if (info.mpd.title) {
1817 free(info.mpd.title);
1820 if (info.mpd.file) {
1821 free(info.mpd.file);
1828 case OBJ_xmms2_artist:
1829 if (info.xmms2.artist) {
1830 free(info.xmms2.artist);
1831 info.xmms2.artist = 0;
1834 case OBJ_xmms2_album:
1835 if (info.xmms2.album) {
1836 free(info.xmms2.album);
1837 info.xmms2.album = 0;
1840 case OBJ_xmms2_title:
1841 if (info.xmms2.title) {
1842 free(info.xmms2.title);
1843 info.xmms2.title = 0;
1846 case OBJ_xmms2_genre:
1847 if (info.xmms2.genre) {
1848 free(info.xmms2.genre);
1849 info.xmms2.genre = 0;
1852 case OBJ_xmms2_comment:
1853 if (info.xmms2.comment) {
1854 free(info.xmms2.comment);
1855 info.xmms2.comment = 0;
1858 case OBJ_xmms2_decoder:
1859 if (info.xmms2.decoder) {
1860 free(info.xmms2.decoder);
1864 case OBJ_xmms2_transport:
1865 if (info.xmms2.transport) {
1866 free(info.xmms2.transport);
1871 if (info.xmms2.url) {
1872 free(info.xmms2.url);
1876 case OBJ_xmms2_date:
1877 if (info.xmms2.date) {
1878 free(info.xmms2.date);
1879 info.xmms2.date = 0;
1882 case OBJ_xmms2_status:
1883 if (info.xmms2.status) {
1884 free(info.xmms2.status);
1885 info.xmms2.status = 0;
1888 case OBJ_xmms2_smart:
1889 if (info.xmms2.artist) {
1890 free(info.xmms2.artist);
1891 info.xmms2.artist = 0;
1893 if (info.xmms2.title) {
1894 free(info.xmms2.title);
1895 info.xmms2.title = 0;
1897 if (info.xmms2.url) {
1898 free(info.xmms2.url);
1904 case OBJ_bmpx_title:
1905 case OBJ_bmpx_artist:
1906 case OBJ_bmpx_album:
1907 case OBJ_bmpx_track:
1909 case OBJ_bmpx_bitrate:
1913 free(objs[i].data.s);
1917 free(objs[i].data.execi.cmd);
1918 free(objs[i].data.execi.buffer);
1921 free(objs[i].data.texeci.cmd);
1922 free(objs[i].data.texeci.buffer);
1925 if (info.first_process) {
1926 free_all_processes();
1927 info.first_process = NULL;
1931 if (info.first_process) {
1932 free_all_processes();
1933 info.first_process = NULL;
1938 free(objs[i].data.hddtemp.dev);
1939 free(objs[i].data.hddtemp.addr);
1945 //text_objects = NULL;
1946 //text_object_count = 0;
1949 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
1954 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
1955 *a = mixer_init(buf1);
1956 (void) scan_bar(arg + n, w, h);
1959 (void) scan_bar(arg, w, h);
1964 /* construct_text_object() creates a new text_object */
1965 static struct text_object *construct_text_object(const char *s, const char *arg, unsigned int object_count, struct text_object *text_objects, long line)
1967 //struct text_object *obj = new_text_object();
1968 struct text_object *obj = new_text_object_internal();
1971 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
1972 #define END ; } } else
1976 obj->type = OBJ_color;
1977 obj->data.l = get_x11_color(s);
1980 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
1981 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
1982 END OBJ(acpiacadapter, 0)
1983 #if defined(__linux__)
1990 || (unsigned int)atoi(&arg[0])>info.cpu_count)
1992 obj->data.cpu_index=1;
1993 // ERR("freq: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
1997 obj->data.cpu_index=atoi(&arg[0]);
2006 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2008 obj->data.cpu_index=1;
2009 // ERR("freq_g: Invalid CPU number or you don't have that many CPUs! Displaying the clock for CPU 1.");
2013 obj->data.cpu_index=atoi(&arg[0]);
2016 END OBJ(voltage_mv, 0)
2022 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2024 obj->data.cpu_index=1;
2025 // ERR("voltage_mv: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2029 obj->data.cpu_index=atoi(&arg[0]);
2032 END OBJ(voltage_v, 0)
2038 || (unsigned int)atoi(&arg[0])>info.cpu_count)
2040 obj->data.cpu_index=1;
2041 // ERR("voltage_v: Invalid CPU number or you don't have that many CPUs! Displaying voltage for CPU 1.");
2045 obj->data.cpu_index=atoi(&arg[0]);
2051 #endif /* __linux__ */
2052 END OBJ(freq_dyn, 0);
2053 END OBJ(freq_dyn_g, 0);
2054 END OBJ(acpifan, 0);
2055 END OBJ(battery, 0);
2058 sscanf(arg, "%63s", bat);
2060 strcpy(bat, "BAT0");
2061 obj->data.s = strdup(bat);
2062 #if defined(__linux__)
2063 END OBJ(i8k_version, INFO_I8K)
2064 END OBJ(i8k_bios, INFO_I8K)
2065 END OBJ(i8k_serial, INFO_I8K)
2066 END OBJ(i8k_cpu_temp, INFO_I8K)
2067 END OBJ(i8k_cpu_tempf, INFO_I8K)
2068 END OBJ(i8k_left_fan_status, INFO_I8K)
2069 END OBJ(i8k_right_fan_status, INFO_I8K)
2070 END OBJ(i8k_left_fan_rpm, INFO_I8K)
2071 END OBJ(i8k_right_fan_rpm, INFO_I8K)
2072 END OBJ(i8k_ac_status, INFO_I8K)
2073 END OBJ(i8k_buttons_status, INFO_I8K)
2075 END OBJ(ibm_temps, 0)
2077 CRIT_ERR("ibm_temps: needs an argument");
2079 if (!isdigit(arg[0])
2081 || atoi(&arg[0]) >=8)
2084 ERR("Invalid temperature sensor! Sensor number must be 0 to 7. Using 0 (CPU temp sensor).");
2086 obj->data.sensor = atoi(&arg[0]);
2087 END OBJ(ibm_volume, 0)
2088 END OBJ(ibm_brightness, 0)
2089 END OBJ(pb_battery, 0)
2090 if (arg && strcmp(arg, "status") == 0) {
2091 obj->data.i = PB_BATT_STATUS;
2092 } else if (arg && strcmp(arg, "percent") == 0) {
2093 obj->data.i = PB_BATT_PERCENT;
2094 } else if (arg && strcmp(arg, "time") == 0) {
2095 obj->data.i = PB_BATT_TIME;
2097 ERR("pb_battery: needs one argument: status, percent or time");
2102 #endif /* __linux__ */
2103 END OBJ(buffers, INFO_BUFFERS)
2104 END OBJ(cached, INFO_BUFFERS)
2105 END OBJ(cpu, INFO_CPU)
2107 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2108 obj->data.cpu_index = atoi(&arg[3]);
2110 } else {obj->data.cpu_index = 0; }
2112 obj->data.cpu_index = 0;
2114 END OBJ(cpubar, INFO_CPU)
2116 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2117 obj->data.cpu_index = atoi(&arg[3]);
2120 else {obj->data.cpu_index = 0;}
2121 (void) scan_bar(arg, &obj->a, &obj->b);
2123 (void) scan_bar(arg, &obj->a, &obj->b);
2124 obj->data.cpu_index = 0;
2126 END OBJ(cpugraph, INFO_CPU)
2128 if (strncmp(arg, "cpu", 3) == 0 && isdigit(arg[3])) {
2129 obj->data.cpu_index = atoi(&arg[3]);
2132 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2134 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2135 obj->data.cpu_index = 0;
2137 END OBJ(diskio, INFO_DISKIO)
2138 END OBJ(diskiograph, INFO_DISKIO) (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2141 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
2145 obj->data.s = scan_font(arg);
2147 OBJ(downspeed, INFO_NET)
2149 obj->data.net = get_net_stat(arg);
2152 CRIT_ERR("downspeed needs argument");
2154 END OBJ(downspeedf, INFO_NET)
2156 obj->data.net = get_net_stat(arg);
2159 CRIT_ERR("downspeedf needs argument");
2161 END OBJ(downspeedgraph, INFO_NET)
2162 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2164 sscanf(arg, "%63s %*i,%*i %*i", buf);
2165 obj->data.net = get_net_stat(buf);
2166 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2167 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
2174 (text_objects[blockstart[blockdepth - 1]]).data.ifblock.pos = object_count;
2175 blockstart[blockdepth - 1] = object_count;
2176 obj->data.ifblock.pos = object_count + 2;
2178 ERR("$else: no matching $if_*");
2183 text_objects[blockstart[blockdepth]].data.ifblock.pos = object_count;
2185 ERR("$endif: no matching $if_*");
2188 OBJ(image, 0) obj->data.s = strdup(arg ? arg : "");
2191 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
2192 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
2193 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
2194 END OBJ(execibar, 0) unsigned int n;
2195 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2197 ERR("${execibar <interval> command}");
2198 obj->type = OBJ_text;
2199 snprintf(buf, 256, "${%s}", s);
2200 obj->data.s = strdup(buf);
2202 obj->data.execi.cmd = strdup(arg + n);
2204 END OBJ(execigraph, 0) unsigned int n;
2205 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2207 ERR("${execigraph <interval> command}");
2208 obj->type = OBJ_text;
2209 snprintf(buf, 256, "${%s}", s);
2210 obj->data.s = strdup(buf);
2212 obj->data.execi.cmd = strdup(arg + n);
2214 END OBJ(execi, 0) unsigned int n;
2215 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
2217 ERR("${execi <interval> command}");
2218 obj->type = OBJ_text;
2219 snprintf(buf, 256, "${%s}", s);
2220 obj->data.s = strdup(buf);
2222 obj->data.execi.cmd = strdup(arg + n);
2223 obj->data.execi.buffer =
2224 (char *) calloc(1, TEXT_BUFFER_SIZE);
2226 END OBJ(texeci, 0) unsigned int n;
2227 if (!arg || sscanf(arg, "%f %n", &obj->data.texeci.interval, &n) <= 0) {
2229 ERR("${texeci <interval> command}");
2230 obj->type = OBJ_text;
2231 snprintf(buf, 256, "${%s}", s);
2232 obj->data.s = strdup(buf);
2234 obj->data.texeci.cmd = strdup(arg + n);
2235 obj->data.texeci.buffer =
2236 (char *) calloc(1, TEXT_BUFFER_SIZE);
2238 obj->data.texeci.p_timed_thread = NULL;
2239 END OBJ(pre_exec, 0) obj->type = OBJ_text;
2241 FILE *fp = popen(arg, "r");
2245 n = fread(buf, 1, 2048, fp);
2248 if (n && buf[n - 1] == '\n')
2253 obj->data.s = strdup(buf);
2255 obj->data.s = strdup("");
2258 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
2259 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
2261 while (isspace(*arg))
2267 obj->data.fsbar.fs = prepare_fs_stat(arg);
2268 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
2271 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
2275 obj->data.fsbar.fs = prepare_fs_stat(arg);
2276 END OBJ(fs_free, INFO_FS) if (!arg)
2278 obj->data.fs = prepare_fs_stat(arg);
2279 END OBJ(fs_used_perc, INFO_FS) if (!arg)
2281 obj->data.fs = prepare_fs_stat(arg);
2282 END OBJ(fs_free_perc, INFO_FS) if (!arg)
2284 obj->data.fs = prepare_fs_stat(arg);
2285 END OBJ(fs_size, INFO_FS) if (!arg)
2287 obj->data.fs = prepare_fs_stat(arg);
2288 END OBJ(fs_used, INFO_FS) if (!arg)
2290 obj->data.fs = prepare_fs_stat(arg);
2291 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
2292 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
2293 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
2297 ERR("goto needs arguments");
2298 obj->type = OBJ_text;
2299 obj->data.s = strdup("${goto}");
2303 obj->data.i = atoi(arg);
2308 if (sscanf(arg, "%d %d", &a, &b) != 2)
2309 sscanf(arg, "%d", &b);
2313 obj->data.pair.a = a;
2314 obj->data.pair.b = b;
2316 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
2320 ERR("i2c needs arguments");
2321 obj->type = OBJ_text;
2322 //obj->data.s = strdup("${i2c}");
2326 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
2327 /* if scanf couldn't read three values, read type and num and use
2329 sscanf(arg, "%63s %d", buf2, &n);
2331 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
2332 obj->data.i2c.devtype);
2333 strncpy(obj->data.i2c.type, buf2, 63);
2336 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
2337 obj->data.i2c.devtype);
2338 strncpy(obj->data.i2c.type, buf2, 63);
2341 END OBJ(top, INFO_TOP)
2345 ERR("top needs arguments");
2346 obj->type = OBJ_text;
2347 //obj->data.s = strdup("${top}");
2350 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2351 if (strcmp(buf, "name") == 0) {
2352 obj->data.top.type = TOP_NAME;
2353 } else if (strcmp(buf, "cpu") == 0) {
2354 obj->data.top.type = TOP_CPU;
2355 } else if (strcmp(buf, "pid") == 0) {
2356 obj->data.top.type = TOP_PID;
2357 } else if (strcmp(buf, "mem") == 0) {
2358 obj->data.top.type = TOP_MEM;
2360 ERR("invalid arg for top");
2363 if (n < 1 || n > 10) {
2364 CRIT_ERR("invalid arg for top");
2367 obj->data.top.num = n - 1;
2371 ERR("invalid args given for top");
2374 END OBJ(top_mem, INFO_TOP)
2378 ERR("top_mem needs arguments");
2379 obj->type = OBJ_text;
2380 obj->data.s = strdup("${top_mem}");
2383 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
2384 if (strcmp(buf, "name") == 0) {
2385 obj->data.top.type = TOP_NAME;
2386 } else if (strcmp(buf, "cpu") == 0) {
2387 obj->data.top.type = TOP_CPU;
2388 } else if (strcmp(buf, "pid") == 0) {
2389 obj->data.top.type = TOP_PID;
2390 } else if (strcmp(buf, "mem") == 0) {
2391 obj->data.top.type = TOP_MEM;
2393 ERR("invalid arg for top");
2396 if (n < 1 || n > 10) {
2397 CRIT_ERR("invalid arg for top");
2400 obj->data.top.num = n - 1;
2404 ERR("invalid args given for top");
2407 END OBJ(addr, INFO_NET)
2409 obj->data.net = get_net_stat(arg);
2412 CRIT_ERR("addr needs argument");
2414 END OBJ(linkstatus, INFO_WIFI)
2416 obj->data.net = get_net_stat(arg);
2419 CRIT_ERR("linkstatus needs argument");
2425 ERR("tail needs arguments");
2426 obj->type = OBJ_text;
2427 obj->data.s = strdup("${tail}");
2430 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2431 if (n1 < 1 || n1 > 30) {
2432 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
2436 fp = fopen(buf, "r");
2438 obj->data.tail.logfile =
2439 malloc(TEXT_BUFFER_SIZE);
2440 strcpy(obj->data.tail.logfile, buf);
2441 obj->data.tail.wantedlines = n1 - 1;
2442 obj->data.tail.interval =
2443 update_interval * 2;
2447 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2450 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2451 if (n1 < 1 || n1 > 30) {
2453 ("invalid arg for tail, number of lines must be between 1 and 30");
2455 } else if (n2 < 1 || n2 < update_interval) {
2457 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
2461 fp = fopen(buf, "r");
2463 obj->data.tail.logfile =
2464 malloc(TEXT_BUFFER_SIZE);
2465 strcpy(obj->data.tail.logfile, buf);
2466 obj->data.tail.wantedlines = n1 - 1;
2467 obj->data.tail.interval = n2;
2471 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
2477 ERR("invalid args given for tail");
2480 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2485 ERR("head needs arguments");
2486 obj->type = OBJ_text;
2487 obj->data.s = strdup("${head}");
2490 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
2491 if (n1 < 1 || n1 > 30) {
2492 CRIT_ERR("invalid arg for head, number of lines must be between 1 and 30");
2496 fp = fopen(buf, "r");
2498 obj->data.tail.logfile =
2499 malloc(TEXT_BUFFER_SIZE);
2500 strcpy(obj->data.tail.logfile, buf);
2501 obj->data.tail.wantedlines = n1 - 1;
2502 obj->data.tail.interval =
2503 update_interval * 2;
2507 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2510 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
2511 if (n1 < 1 || n1 > 30) {
2513 ("invalid arg for head, number of lines must be between 1 and 30");
2515 } else if (n2 < 1 || n2 < update_interval) {
2517 ("invalid arg for head, interval must be greater than 0 and Conky's interval");
2521 fp = fopen(buf, "r");
2523 obj->data.tail.logfile =
2524 malloc(TEXT_BUFFER_SIZE);
2525 strcpy(obj->data.tail.logfile, buf);
2526 obj->data.tail.wantedlines = n1 - 1;
2527 obj->data.tail.interval = n2;
2531 CRIT_ERR("head logfile does not exist, or you do not have correct permissions");
2537 ERR("invalid args given for head");
2540 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 20); /* asumming all else worked */
2541 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
2543 r = sscanf(arg, "%d %d %d", &a, &b, &c);
2544 if (r >= 3 && (c < 1 || c > 3))
2546 if (r >= 2 && (b < 1 || b > 3))
2548 if (r >= 1 && (a < 1 || a > 3))
2551 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
2552 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
2553 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
2554 END OBJ(if_existing, 0)
2555 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2556 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2559 ERR("if_existing needs an argument");
2560 obj->data.ifblock.s = 0;
2562 obj->data.ifblock.s = strdup(arg);
2563 blockstart[blockdepth] = object_count;
2564 obj->data.ifblock.pos = object_count + 2;
2566 END OBJ(if_mounted, 0)
2567 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2568 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2571 ERR("if_mounted needs an argument");
2572 obj->data.ifblock.s = 0;
2574 obj->data.ifblock.s = strdup(arg);
2575 blockstart[blockdepth] = object_count;
2576 obj->data.ifblock.pos = object_count + 2;
2578 END OBJ(if_running, 0)
2579 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
2580 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
2584 snprintf(buf, 256, "pidof %s >/dev/null", arg);
2585 obj->data.ifblock.s = strdup(buf);
2587 ERR("if_running needs an argument");
2588 obj->data.ifblock.s = 0;
2590 blockstart[blockdepth] = object_count;
2591 obj->data.ifblock.pos = object_count + 2;
2595 END OBJ(mails, INFO_MAIL)
2596 END OBJ(mem, INFO_MEM)
2597 END OBJ(memmax, INFO_MEM)
2598 END OBJ(memperc, INFO_MEM)
2599 END OBJ(membar, INFO_MEM)
2600 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2601 END OBJ(memgraph, INFO_MEM)
2602 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2603 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
2604 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
2605 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
2606 END OBJ(mixerbar, INFO_MIXER)
2607 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2608 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2609 END OBJ(mixerlbar, INFO_MIXER)
2610 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2611 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2612 END OBJ(mixerrbar, INFO_MIXER)
2613 scan_mixer_bar(arg, &obj->data.mixerbar.l,
2614 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
2616 OBJ(new_mails, INFO_MAIL)
2617 END OBJ(nodename, 0)
2618 END OBJ(processes, INFO_PROCS)
2619 END OBJ(running_processes, INFO_RUN_PROCS)
2620 END OBJ(shadecolor, 0)
2622 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
2624 END OBJ(outlinecolor, 0)
2626 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
2628 END OBJ(stippled_hr, 0)
2630 int a = stippled_borders, b = 1;
2632 if (sscanf(arg, "%d %d", &a, &b) != 2)
2633 sscanf(arg, "%d", &b);
2637 obj->data.pair.a = a;
2638 obj->data.pair.b = b;
2640 END OBJ(swap, INFO_MEM)
2641 END OBJ(swapmax, INFO_MEM)
2642 END OBJ(swapperc, INFO_MEM)
2643 END OBJ(swapbar, INFO_MEM)
2644 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2645 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
2647 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
2648 obj->data.i2c.devtype);
2649 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
2651 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
2652 obj->data.i2c.devtype);
2653 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2654 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
2656 char buf1[256], buf2[256], *fmt, *tz;
2659 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
2668 obj->data.tztime.fmt = strdup(fmt ? fmt : "%F %T");
2669 obj->data.tztime.tz = tz ? strdup(tz) : NULL;
2671 END OBJ(iconv_start, 0)
2672 if (iconv_converting) {
2673 CRIT_ERR("You must stop your last iconv conversion before starting another");
2676 char iconv_from[CODEPAGE_LENGTH];
2677 char iconv_to[CODEPAGE_LENGTH];
2678 if (sscanf(arg, "%s %s", iconv_from, iconv_to) != 2) {
2679 CRIT_ERR("Invalid arguments for iconv_start");
2682 new_iconv = iconv_open(iconv_to, iconv_from);
2683 if (new_iconv == (iconv_t)(-1)) {
2684 ERR("Can't convert from %s to %s.", iconv_from, iconv_to);
2686 obj->a = register_iconv(&new_iconv);
2687 iconv_converting = 1;
2691 CRIT_ERR("Iconv requires arguments");
2693 END OBJ(iconv_stop, 0)
2694 iconv_converting = 0;
2697 END OBJ(totaldown, INFO_NET)
2699 obj->data.net = get_net_stat(arg);
2702 CRIT_ERR("totaldown needs argument");
2704 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
2706 obj->data.net = get_net_stat(arg);
2709 CRIT_ERR("totalup needs argument");
2712 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
2713 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
2714 END OBJ(upspeed, INFO_NET)
2716 obj->data.net = get_net_stat(arg);
2719 CRIT_ERR("upspeed needs argument");
2721 END OBJ(upspeedf, INFO_NET)
2723 obj->data.net = get_net_stat(arg);
2726 CRIT_ERR("upspeedf needs argument");
2729 END OBJ(upspeedgraph, INFO_NET)
2730 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d, &obj->e);
2732 sscanf(arg, "%63s %*i,%*i %*i", buf);
2733 obj->data.net = get_net_stat(buf);
2734 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
2735 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
2740 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
2741 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
2742 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
2743 OBJ(apm_adapter, 0) END
2744 OBJ(apm_battery_life, 0) END
2745 OBJ(apm_battery_time, 0) END
2746 #endif /* __FreeBSD__ */
2750 obj->data.mail = parse_mail_args(IMAP, arg);
2751 obj->global_mode = 0;
2753 obj->global_mode = 1;
2756 OBJ(imap_messages, 0)
2759 obj->data.mail = parse_mail_args(IMAP, arg);
2760 obj->global_mode = 0;
2762 obj->global_mode = 1;
2768 obj->data.mail = parse_mail_args(POP3, arg);
2769 obj->global_mode = 0;
2771 obj->global_mode = 1;
2777 obj->data.mail = parse_mail_args(POP3, arg);
2778 obj->global_mode = 0;
2780 obj->global_mode = 1;
2784 OBJ(mpd_artist, INFO_MPD)
2785 END OBJ(mpd_title, INFO_MPD)
2786 END OBJ(mpd_random, INFO_MPD)
2787 END OBJ(mpd_repeat, INFO_MPD)
2788 END OBJ(mpd_elapsed, INFO_MPD)
2789 END OBJ(mpd_length, INFO_MPD)
2790 END OBJ(mpd_track, INFO_MPD)
2791 END OBJ(mpd_name, INFO_MPD)
2792 END OBJ(mpd_file, INFO_MPD)
2793 END OBJ(mpd_percent, INFO_MPD)
2794 END OBJ(mpd_album, INFO_MPD)
2795 END OBJ(mpd_vol, INFO_MPD)
2796 END OBJ(mpd_bitrate, INFO_MPD)
2797 END OBJ(mpd_status, INFO_MPD)
2798 END OBJ(mpd_bar, INFO_MPD)
2799 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2800 END OBJ(mpd_smart, INFO_MPD) END
2803 OBJ(xmms2_artist, INFO_XMMS2)
2804 END OBJ(xmms2_album, INFO_XMMS2)
2805 END OBJ(xmms2_title, INFO_XMMS2)
2806 END OBJ(xmms2_genre, INFO_XMMS2)
2807 END OBJ(xmms2_comment, INFO_XMMS2)
2808 END OBJ(xmms2_decoder, INFO_XMMS2)
2809 END OBJ(xmms2_transport, INFO_XMMS2)
2810 END OBJ(xmms2_url, INFO_XMMS2)
2811 END OBJ(xmms2_tracknr, INFO_XMMS2)
2812 END OBJ(xmms2_bitrate, INFO_XMMS2)
2813 END OBJ(xmms2_date, INFO_XMMS2)
2814 END OBJ(xmms2_id, INFO_XMMS2)
2815 END OBJ(xmms2_duration, INFO_XMMS2)
2816 END OBJ(xmms2_elapsed, INFO_XMMS2)
2817 END OBJ(xmms2_size, INFO_XMMS2)
2818 END OBJ(xmms2_status, INFO_XMMS2)
2819 END OBJ(xmms2_percent, INFO_XMMS2)
2820 END OBJ(xmms2_bar, INFO_XMMS2)
2821 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
2822 END OBJ(xmms2_smart, INFO_XMMS2)
2826 OBJ(audacious_status, INFO_AUDACIOUS) END
2827 OBJ(audacious_title, INFO_AUDACIOUS) END
2828 OBJ(audacious_length, INFO_AUDACIOUS) END
2829 OBJ(audacious_length_seconds, INFO_AUDACIOUS) END
2830 OBJ(audacious_position, INFO_AUDACIOUS) END
2831 OBJ(audacious_position_seconds, INFO_AUDACIOUS) END
2832 OBJ(audacious_bitrate, INFO_AUDACIOUS) END
2833 OBJ(audacious_frequency, INFO_AUDACIOUS) END
2834 OBJ(audacious_channels, INFO_AUDACIOUS) END
2835 OBJ(audacious_filename, INFO_AUDACIOUS) END
2836 OBJ(audacious_playlist_length, INFO_AUDACIOUS) END
2837 OBJ(audacious_playlist_position, INFO_AUDACIOUS) END
2838 OBJ(audacious_bar, INFO_AUDACIOUS)
2839 (void) scan_bar(arg, &obj->a, &obj->b);
2843 OBJ(bmpx_title, INFO_BMPX)
2844 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2846 OBJ(bmpx_artist, INFO_BMPX)
2847 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2849 OBJ(bmpx_album, INFO_BMPX)
2850 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2852 OBJ(bmpx_track, INFO_BMPX)
2853 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2855 OBJ(bmpx_uri, INFO_BMPX)
2856 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2858 OBJ(bmpx_bitrate, INFO_BMPX)
2859 memset(&(info.bmpx), 0, sizeof(struct bmpx_s));
2864 if (!arg || scan_hddtemp(arg, &obj->data.hddtemp.dev,
2865 &obj->data.hddtemp.addr, &obj->data.hddtemp.port)) {
2866 ERR("hddtemp needs arguments");
2867 obj->type = OBJ_text;
2868 obj->data.s = strdup("${hddtemp}");
2873 #ifdef TCP_PORT_MONITOR
2874 OBJ(tcp_portmon, INFO_TCP_PORT_MONITOR)
2875 int argc, port_begin, port_end, item, connection_index;
2877 memset(itembuf,0,sizeof(itembuf));
2879 /* massive argument checking */
2881 CRIT_ERR("tcp_portmon: needs arguments");
2883 argc=sscanf(arg, "%d %d %31s %d", &port_begin, &port_end, itembuf, &connection_index);
2884 if ( (argc != 3) && (argc != 4) )
2886 CRIT_ERR("tcp_portmon: requires 3 or 4 arguments");
2888 if ( (port_begin<1) || (port_begin>65535) || (port_end<1) || (port_end>65535) )
2890 CRIT_ERR("tcp_portmon: port values must be from 1 to 65535");
2892 if ( port_begin > port_end )
2894 CRIT_ERR("tcp_portmon: starting port must be <= ending port");
2896 if ( strncmp(itembuf,"count",31) == 0 )
2898 else if ( strncmp(itembuf,"rip",31) == 0 )
2900 else if ( strncmp(itembuf,"rhost",31) == 0 )
2902 else if ( strncmp(itembuf,"rport",31) == 0 )
2904 else if ( strncmp(itembuf,"rservice",31) == 0 )
2906 else if ( strncmp(itembuf,"lip",31) == 0 )
2908 else if ( strncmp(itembuf,"lhost",31) == 0 )
2910 else if ( strncmp(itembuf,"lport",31) == 0 )
2912 else if ( strncmp(itembuf,"lservice",31) == 0 )
2916 CRIT_ERR("tcp_portmon: invalid item specified");
2918 if ( (argc==3) && (item!=COUNT) )
2920 CRIT_ERR("tcp_portmon: 3 argument form valid only for \"count\" item");
2922 if ( (argc==4) && (connection_index<0) )
2924 CRIT_ERR("tcp_portmon: connection index must be non-negative");
2926 /* ok, args looks good. save the text object data */
2927 obj->data.tcp_port_monitor.port_range_begin = (in_port_t)port_begin;
2928 obj->data.tcp_port_monitor.port_range_end = (in_port_t)port_end;
2929 obj->data.tcp_port_monitor.item = item;
2930 obj->data.tcp_port_monitor.connection_index = connection_index;
2932 /* if the port monitor collection hasn't been created, we must create it */
2933 if ( !info.p_tcp_port_monitor_collection )
2935 info.p_tcp_port_monitor_collection =
2936 create_tcp_port_monitor_collection( &tcp_port_monitor_collection_args );
2937 if ( !info.p_tcp_port_monitor_collection )
2939 CRIT_ERR("tcp_portmon: unable to create port monitor collection");
2943 /* if a port monitor for this port does not exist, create one and add it to the collection */
2944 if ( find_tcp_port_monitor( info.p_tcp_port_monitor_collection, port_begin, port_end ) == NULL )
2946 tcp_port_monitor_t * p_monitor =
2947 create_tcp_port_monitor( port_begin, port_end, &tcp_port_monitor_args );
2950 CRIT_ERR("tcp_portmon: unable to create port monitor");
2952 /* add the newly created monitor to the collection */
2953 if ( insert_tcp_port_monitor_into_collection( info.p_tcp_port_monitor_collection,
2956 CRIT_ERR("tcp_portmon: unable to add port monitor to collection");
2963 ERR("unknown variable %s", s);
2964 obj->type = OBJ_text;
2965 snprintf(buf, 256, "${%s}", s);
2966 obj->data.s = strdup(buf);
2973 static struct text_object *create_plain_text(const char *s)
2975 struct text_object *obj;
2977 if (s == NULL || *s == '\0') {
2981 obj = new_text_object_internal();
2983 obj->type = OBJ_text;
2984 obj->data.s = strdup(s);
2988 static struct text_object_list *extract_variable_text_internal(const char *p)
2990 struct text_object_list *retval;
2991 struct text_object *obj;
2994 retval = malloc(sizeof(struct text_object_list));
2995 memset(retval, 0, sizeof(struct text_object_list));
2996 retval->text_object_count = 0;
2998 long line = text_lines;
3006 obj = create_plain_text(s);
3008 // allocate memory for the object
3009 retval->text_objects = realloc(retval->text_objects,
3010 sizeof(struct text_object) * (retval->text_object_count+1));
3011 // assign the new object to the end of the list.
3012 memcpy(&retval->text_objects[retval->text_object_count++],
3013 obj, sizeof(struct text_object));
3025 /* variable is either $foo or ${foo} */
3029 while (*p && *p != '}')
3035 while (*p && (isalnum((int) *p)
3040 /* copy variable to buffer */
3041 len = (p - s > 255) ? 255 : (p - s);
3042 strncpy(buf, s, len);
3051 /* if variable wasn't found from environment, use some special */
3057 if (strchr(buf, ' ')) {
3058 arg = strchr(buf, ' ');
3061 while (isspace((int) *arg))
3067 /* lowercase variable name */
3074 // create new object
3075 obj = construct_text_object(buf, arg, retval->text_object_count, retval->text_objects, line);
3077 // allocate memory for the object
3078 retval->text_objects = realloc(retval->text_objects,
3079 sizeof(struct text_object) * (retval->text_object_count+1));
3080 // assign the new object to the end of the list.
3081 memcpy(&retval->text_objects[retval->text_object_count++],
3082 obj, sizeof(struct text_object));
3088 obj = create_plain_text("$");
3090 // allocate memory for the object
3091 retval->text_objects = realloc(retval->text_objects,
3092 sizeof(struct text_object) * (retval->text_object_count+1));
3093 // assign the new object to the end of the list.
3094 memcpy(&retval->text_objects[retval->text_object_count++],
3095 obj, sizeof(struct text_object));
3102 obj = create_plain_text(s);
3104 // allocate memory for the object
3105 retval->text_objects = realloc(retval->text_objects,
3106 sizeof(struct text_object) * (retval->text_object_count+1));
3107 // assign the new object to the end of the list.
3108 memcpy(&retval->text_objects[retval->text_object_count++],
3109 obj, sizeof(struct text_object));
3114 ERR("one or more $endif's are missing");
3120 static void extract_variable_text(const char *p)
3122 struct text_object_list *list;
3124 free_text_objects(text_object_count, text_objects);
3125 text_object_count = 0;
3126 text_objects = NULL;
3129 list = extract_variable_text_internal(p);
3130 text_objects = list->text_objects;
3131 text_object_count = list->text_object_count;
3138 void parse_conky_vars(char * text, char * p, struct information *cur) {
3139 struct text_object_list *object_list = extract_variable_text_internal(text);
3140 generate_text_internal(p, P_MAX_SIZE, object_list->text_objects, object_list->text_object_count, cur);
3144 static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur)
3149 char buff_in[P_MAX_SIZE] = {0};
3150 iconv_converting = 0;
3153 for (i = 0; i < object_count; i++) {
3154 struct text_object *obj = &objs[i];
3156 #define OBJ(a) break; case OBJ_##a:
3158 switch (obj->type) {
3161 ERR("not implemented obj type %d",
3165 /* does anyone have decimals in acpi temperature? */
3167 snprintf(p, p_max_size, "%d", (int)
3168 get_acpi_temperature(obj->
3172 snprintf(p, 5, "%d ", (int)
3173 get_acpi_temperature(obj->
3178 /* does anyone have decimals in acpi temperature? */
3180 snprintf(p, p_max_size, "%d", (int)
3181 ((get_acpi_temperature(obj->
3183 i)+ 40) * 9.0 / 5 - 40));
3185 snprintf(p, 5, "%d ", (int)
3186 ((get_acpi_temperature(obj->
3188 i)+ 40) * 9.0 / 5 - 40));
3192 obj->a = get_freq(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3197 obj->a = get_freq(p, p_max_size, "%'.2f", 1000, obj->data.cpu_index);
3200 #if defined(__linux__)
3203 obj->a = get_voltage(p, p_max_size, "%.0f", 1, obj->data.cpu_index);
3208 obj->a = get_voltage(p, p_max_size, "%'.3f", 1000, obj->data.cpu_index);
3211 #endif /* __linux__ */
3215 get_freq_dynamic(p, 6, "%.0f ", 1 );
3217 get_freq_dynamic(p, p_max_size, "%.0f", 1 );
3222 get_freq_dynamic(p, 6, "%'.2f ", 1000);
3224 get_freq_dynamic(p, p_max_size, "%'.2f", 1000);
3228 get_adt746x_cpu(p, p_max_size);
3231 get_adt746x_fan(p, p_max_size);
3234 get_acpi_fan(p, p_max_size);
3236 OBJ(acpiacadapter) {
3237 get_acpi_ac_adapter(p, p_max_size);
3240 get_battery_stuff(p, p_max_size, obj->data.s);
3243 human_readable(cur->buffers * 1024, p, 255);
3246 human_readable(cur->cached * 1024, p, 255);
3249 if (obj->data.cpu_index > info.cpu_count) {
3250 printf("obj->data.cpu_index %i info.cpu_count %i", obj->data.cpu_index, info.cpu_count);
3251 CRIT_ERR("attempting to use more CPUs then you have!");
3254 snprintf(p, p_max_size, "%*d", pad_percents,
3255 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3258 snprintf(p, 4, "%*d ",
3260 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3266 (int) round_to_int(cur->cpu_usage[obj->data.cpu_index] * 255.0));
3269 new_graph(p, obj->a,
3270 obj->b, obj->c, obj->d,
3271 (unsigned int) round_to_int(cur->cpu_usage[obj->data.cpu_index] *
3275 new_fg(p, obj->data.l);
3277 #if defined(__linux__)
3279 snprintf(p, p_max_size, "%s", i8k.version);
3282 snprintf(p, p_max_size, "%s", i8k.bios);
3285 snprintf(p, p_max_size, "%s", i8k.serial);
3288 snprintf(p, p_max_size, "%s", i8k.cpu_temp);
3290 OBJ(i8k_cpu_tempf) {
3292 sscanf(i8k.cpu_temp, "%d", &cpu_temp);
3293 snprintf(p, p_max_size, "%.1f", cpu_temp*(9.0/5.0)+32.0);
3295 OBJ(i8k_left_fan_status) {
3296 int left_fan_status;
3297 sscanf(i8k.left_fan_status, "%d", &left_fan_status);
3298 if(left_fan_status == 0) {
3299 snprintf(p, p_max_size,"off");
3300 } if(left_fan_status == 1) {
3301 snprintf(p, p_max_size, "low");
3302 } if(left_fan_status == 2) {
3303 snprintf(p, p_max_size, "high");
3307 OBJ(i8k_right_fan_status) {
3308 int right_fan_status;
3309 sscanf(i8k.right_fan_status, "%d", &right_fan_status);
3310 if(right_fan_status == 0) {
3311 snprintf(p, p_max_size,"off");
3312 } if(right_fan_status == 1) {
3313 snprintf(p, p_max_size, "low");
3314 } if(right_fan_status == 2) {
3315 snprintf(p, p_max_size, "high");
3318 OBJ(i8k_left_fan_rpm) {
3319 snprintf(p, p_max_size, "%s", i8k.left_fan_rpm);
3321 OBJ(i8k_right_fan_rpm) {
3322 snprintf(p, p_max_size, "%s", i8k.right_fan_rpm);
3324 OBJ(i8k_ac_status) {
3326 sscanf(i8k.ac_status, "%d", &ac_status);
3327 if(ac_status == -1) {
3328 snprintf(p, p_max_size,"disabled (read i8k docs)");
3329 } if(ac_status == 0) {
3330 snprintf(p, p_max_size, "off");
3331 } if(ac_status == 1) {
3332 snprintf(p, p_max_size, "on");
3335 OBJ(i8k_buttons_status) {
3336 snprintf(p, p_max_size, "%s", i8k.buttons_status);
3340 get_ibm_acpi_fan(p, p_max_size);
3343 get_ibm_acpi_temps();
3344 snprintf(p, p_max_size, "%d",
3345 ibm_acpi.temps[obj->data.sensor]);
3348 get_ibm_acpi_volume(p, p_max_size);
3350 OBJ(ibm_brightness) {
3351 get_ibm_acpi_brightness(p, p_max_size);
3354 get_powerbook_batt_info(p, p_max_size, obj->data.i);
3356 #endif /* __linux__ */
3360 new_font(p, obj->data.s);
3365 if (diskio_value > 1024*1024) {
3366 snprintf(p, p_max_size, "%.1fGiB",
3367 (double)diskio_value/1024/1024);
3368 } else if (diskio_value > 1024) {
3369 snprintf(p, p_max_size, "%.1fMiB",
3370 (double)diskio_value/1024);
3371 } else if (diskio_value > 0) {
3372 snprintf(p, p_max_size, "%dKiB", diskio_value);
3374 snprintf(p, p_max_size, "%dB", diskio_value);
3377 if (diskio_value > 1024*1024) {
3378 snprintf(p, 6, "%.1fGiB ",
3379 (double)diskio_value/1024/1024);
3380 } else if (diskio_value > 1024) {
3381 snprintf(p, 6, "%.1fMiB ",
3382 (double)diskio_value/1024);
3383 } else if (diskio_value > 0) {
3384 snprintf(p, 6, "%dKiB ", diskio_value);
3386 snprintf(p, 6, "%dB ", diskio_value);
3391 new_graph(p, obj->a,
3392 obj->b, obj->c, obj->d,
3393 diskio_value, obj->e, 1);
3398 snprintf(p, p_max_size, "%d",
3399 (int) (obj->data.net->
3403 snprintf(p, 6, "%d ",
3404 (int) (obj->data.net->
3410 snprintf(p, p_max_size, "%.1f",
3412 recv_speed / 1024.0);
3414 snprintf(p, 8, "%.1f ",
3416 recv_speed / 1024.0);
3418 OBJ(downspeedgraph) {
3419 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
3420 obj->data.net->recv_speed = 0.01;
3421 new_graph(p, obj->a, obj->b, obj->c, obj->d,
3422 (obj->data.net->recv_speed /
3423 1024.0), obj->e, 1);
3427 i = obj->data.ifblock.pos - 1;
3437 snprintf(p, p_max_size, "%u.%u.%u.%u",
3438 obj->data.net->addr.
3440 obj->data.net->addr.
3442 obj->data.net->addr.
3444 obj->data.net->addr.
3450 snprintf(p, p_max_size, "%d",
3451 obj->data.net->linkstatus);
3454 snprintf(p, 6, "%d ",
3455 obj->data.net->linkstatus);
3458 #if defined(IMLIB2) && defined(X11)
3463 Imlib_Image image, buffer;
3464 image = imlib_load_image(obj->data.s);
3465 imlib_context_set_image(image);
3468 w = imlib_image_get_width();
3469 h = imlib_image_get_height();
3470 buffer = imlib_create_image(w, h);
3471 imlib_context_set_display(display);
3472 imlib_context_set_drawable(window.drawable);
3473 imlib_context_set_colormap(DefaultColormap(display, screen));
3474 imlib_context_set_visual(DefaultVisual(display, screen));
3475 imlib_context_set_image(buffer);
3476 imlib_blend_image_onto_image(image, 0, 0, 0, w, h, text_start_x, text_start_y, w, h);
3477 imlib_render_image_on_drawable(text_start_x, text_start_y);
3479 imlib_context_set_image(image);
3486 FILE *fp = popen(obj->data.s, "r");
3487 int length = fread(p, 1, p_max_size, fp);
3490 /*output[length] = '\0';
3491 if (length > 0 && output[length - 1] == '\n') {
3492 output[length - 1] = '\0';
3495 if (length > 0 && p[length - 1] == '\n') {
3496 p[length - 1] = '\0';
3499 //parse_conky_vars(output, p, cur);
3503 FILE *fp = popen(obj->data.s, "r");
3504 int n2 = fread(p, 1, p_max_size, fp);
3508 if (n2 && p[n2 - 1] == '\n')
3517 if (sscanf(p, "%lf", &barnum) == 0) {
3518 ERR("reading execbar value failed (perhaps it's not the correct format?)");
3520 if (barnum > 100 || barnum < 0) {
3521 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
3523 barnum = barnum / 100.0;
3524 new_bar(p, 0, 4, (int) (barnum * 255.0));
3530 FILE *fp = popen(obj->data.s, "r");
3531 int n2 = fread(p, 1, p_max_size, fp);
3535 if (n2 && p[n2 - 1] == '\n')
3544 if (sscanf(p, "%lf", &barnum) == 0) {
3545 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
3547 if (barnum > 100 || barnum < 0) {
3548 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
3550 new_graph(p, 0, 25, obj->c, obj->d, (int) (barnum), obj->e, 1);
3555 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3556 new_bar(p, 0, 4, (int) obj->f);
3559 FILE *fp = popen(obj->data.execi.cmd, "r");
3560 int n2 = fread(p, 1, p_max_size, fp);
3563 if (n2 && p[n2 - 1] == '\n')
3572 if (sscanf(p, "%f", &barnum) == 0) {
3573 ERR("reading execibar value failed (perhaps it's not the correct format?)");
3575 if (barnum > 100 || barnum < 0) {
3576 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
3578 obj->f = 255 * barnum / 100.0;
3579 new_bar(p, 0, 4, (int) obj->f);
3581 obj->data.execi.last_update =
3582 current_update_time;
3586 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
3587 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 0);
3590 FILE *fp = popen(obj->data.execi.cmd, "r");
3591 int n2 = fread(p, 1, p_max_size, fp);
3594 if (n2 && p[n2 - 1] == '\n')
3603 if (sscanf(p, "%f", &barnum) == 0) {
3604 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
3606 if (barnum > 100 || barnum < 0) {
3607 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
3610 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->f), 100, 1);
3612 obj->data.execi.last_update = current_update_time;
3618 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval || obj->data.execi.interval == 0) {
3619 snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
3621 char *output = obj->data.execi.buffer;
3622 FILE *fp = popen(obj->data.execi.cmd, "r");
3623 //int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3624 int length = fread(output, 1, TEXT_BUFFER_SIZE, fp);
3627 output[length] = '\0';
3628 if (length > 0 && output[length - 1] == '\n') {
3629 output[length - 1] = '\0';
3631 obj->data.execi.last_update = current_update_time;
3632 snprintf(p, p_max_size, "%s", output);
3634 //parse_conky_vars(output, p, cur);
3637 if (!obj->data.texeci.p_timed_thread)
3639 obj->data.texeci.p_timed_thread=
3640 timed_thread_create ((void*)threaded_exec, (void*) obj,
3641 obj->data.texeci.interval * 1000000);
3642 if (!obj->data.texeci.p_timed_thread)
3643 ERR("Error starting texeci thread");
3644 timed_thread_register (obj->data.texeci.p_timed_thread,
3645 &obj->data.texeci.p_timed_thread);
3647 timed_thread_lock (obj->data.texeci.p_timed_thread);
3648 snprintf(p, p_max_size, "%s", obj->data.texeci.buffer);
3649 timed_thread_unlock (obj->data.texeci.p_timed_thread);
3651 #endif /* HAVE_POPEN */
3653 if (obj->global_mode && info.mail) { // this means we use info
3654 if (!info.mail->p_timed_thread)
3656 info.mail->p_timed_thread =
3657 timed_thread_create ((void*)imap_thread,
3659 info.mail->interval * 1000000);
3660 if (!info.mail->p_timed_thread)
3661 ERR("Error starting imap thread");
3662 timed_thread_register (info.mail->p_timed_thread,
3663 &info.mail->p_timed_thread);
3665 timed_thread_lock (info.mail->p_timed_thread);
3666 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3667 timed_thread_unlock (info.mail->p_timed_thread);
3668 } else if (obj->data.mail) { // this means we use obj
3669 if (!obj->data.mail->p_timed_thread)
3671 obj->data.mail->p_timed_thread =
3672 timed_thread_create ((void*)imap_thread,
3673 (void*)obj->data.mail,
3674 obj->data.mail->interval * 1000000);
3675 if (!obj->data.mail->p_timed_thread)
3676 ERR("Error starting imap thread");
3677 timed_thread_register (obj->data.mail->p_timed_thread,
3678 &obj->data.mail->p_timed_thread);
3680 timed_thread_lock (obj->data.mail->p_timed_thread);
3681 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3682 timed_thread_unlock (obj->data.mail->p_timed_thread);
3683 } else if (!obj->a) { // something is wrong, warn once then stop
3684 ERR("Theres a problem with your imap_unseen settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3688 OBJ(imap_messages) {
3689 if (obj->global_mode && info.mail) { // this means we use info
3690 if (!info.mail->p_timed_thread)
3692 info.mail->p_timed_thread =
3693 timed_thread_create ((void*)imap_thread,
3695 info.mail->interval * 1000000);
3696 if (!info.mail->p_timed_thread)
3697 ERR("Error starting imap thread");
3698 timed_thread_register (info.mail->p_timed_thread,
3699 &info.mail->p_timed_thread);
3701 timed_thread_lock (info.mail->p_timed_thread);
3702 snprintf(p, p_max_size, "%lu", info.mail->messages);
3703 timed_thread_unlock (info.mail->p_timed_thread);
3704 } else if (obj->data.mail) { // this means we use obj
3705 if (!obj->data.mail->p_timed_thread)
3707 obj->data.mail->p_timed_thread =
3708 timed_thread_create ((void*)imap_thread,
3709 (void*)obj->data.mail,
3710 obj->data.mail->interval * 1000000);
3711 if (!obj->data.mail->p_timed_thread)
3712 ERR("Error starting imap thread");
3713 timed_thread_register (obj->data.mail->p_timed_thread,
3714 &obj->data.mail->p_timed_thread);
3716 timed_thread_lock (obj->data.mail->p_timed_thread);
3717 snprintf(p, p_max_size, "%lu", obj->data.mail->messages);
3718 timed_thread_lock (obj->data.mail->p_timed_thread);
3719 } else if (!obj->a) { // something is wrong, warn once then stop
3720 ERR("Theres a problem with your imap_messages settings. Check that the global IMAP settings are defined properly (line %li).", obj->line);
3725 if (obj->global_mode && info.mail) { // this means we use info
3726 if (!info.mail->p_timed_thread)
3728 info.mail->p_timed_thread =
3729 timed_thread_create ((void*)pop3_thread,
3731 info.mail->interval * 1000000);
3732 if (!info.mail->p_timed_thread)
3733 ERR("Error starting pop3 thread");
3734 timed_thread_register (info.mail->p_timed_thread,
3735 &info.mail->p_timed_thread);
3737 timed_thread_lock (info.mail->p_timed_thread);
3738 snprintf(p, p_max_size, "%lu", info.mail->unseen);
3739 timed_thread_unlock (info.mail->p_timed_thread);
3740 } else if (obj->data.mail) { // this means we use obj
3741 if (!obj->data.mail->p_timed_thread)
3743 obj->data.mail->p_timed_thread =
3744 timed_thread_create ((void*)pop3_thread,
3746 obj->data.mail->interval * 1000000);
3747 if (!obj->data.mail->p_timed_thread)
3748 ERR("Error starting pop3 thread");
3749 timed_thread_register (obj->data.mail->p_timed_thread,
3750 &obj->data.mail->p_timed_thread);
3752 timed_thread_lock (obj->data.mail->p_timed_thread);
3753 snprintf(p, p_max_size, "%lu", obj->data.mail->unseen);
3754 timed_thread_unlock (obj->data.mail->p_timed_thread);
3755 } else if (!obj->a) { // something is wrong, warn once then stop
3756 ERR("Theres a problem with your pop3_unseen settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3761 if (obj->global_mode && info.mail) { // this means we use info
3762 if (!info.mail->p_timed_thread)
3764 info.mail->p_timed_thread =
3765 timed_thread_create ((void*)pop3_thread,
3767 info.mail->interval * 1000000);
3768 if (!info.mail->p_timed_thread)
3769 ERR("Error starting pop3 thread");
3770 timed_thread_register (info.mail->p_timed_thread,
3771 &info.mail->p_timed_thread);
3773 timed_thread_lock (info.mail->p_timed_thread);
3774 snprintf(p, p_max_size, "%.1f", info.mail->used/1024.0/1024.0);
3775 timed_thread_unlock (info.mail->p_timed_thread);
3776 } else if (obj->data.mail) { // this means we use obj
3777 if (!obj->data.mail->p_timed_thread)
3779 obj->data.mail->p_timed_thread =
3780 timed_thread_create ((void*)pop3_thread,
3781 (void*)obj->data.mail,
3782 obj->data.mail->interval * 1000000);
3783 if (!obj->data.mail->p_timed_thread)
3784 ERR("Error starting pop3 thread");
3785 timed_thread_register (obj->data.mail->p_timed_thread,
3786 &obj->data.mail->p_timed_thread);
3788 timed_thread_lock (obj->data.mail->p_timed_thread);
3789 snprintf(p, p_max_size, "%.1f", obj->data.mail->used/1024.0/1024.0);
3790 timed_thread_unlock (obj->data.mail->p_timed_thread);
3791 } else if (!obj->a) { // something is wrong, warn once then stop
3792 ERR("Theres a problem with your pop3_used settings. Check that the global POP3 settings are defined properly (line %li).", obj->line);
3797 if (obj->data.fs != NULL) {
3798 if (obj->data.fs->size == 0)
3817 if (obj->data.fs != NULL)
3818 human_readable(obj->data.fs->avail,
3822 if (obj->data.fs != NULL) {
3823 if (obj->data.fs->size)
3824 snprintf(p, p_max_size, "%*d",
3833 snprintf(p, p_max_size, "0");
3837 if (obj->data.fs != NULL)
3838 human_readable(obj->data.fs->size,
3842 if (obj->data.fs != NULL)
3843 human_readable(obj->data.fs->size -
3844 (obj->data.fs->free ? obj->data.fs->free :obj->data.fs->avail),
3848 if (obj->data.fs != NULL) {
3849 if (obj->data.fs->size == 0)
3867 if (obj->data.fs != NULL) {
3868 if (obj->data.fs->size)
3869 snprintf(p, 4, "%d",
3879 snprintf(p, p_max_size, "0");
3883 float *v = info.loadavg;
3885 if (obj->data.loadavg[2])
3886 snprintf(p, p_max_size, "%.2f %.2f %.2f",
3887 v[obj->data.loadavg[0] -
3889 v[obj->data.loadavg[1] -
3891 v[obj->data.loadavg[2] -
3893 else if (obj->data.loadavg[1])
3894 snprintf(p, p_max_size, "%.2f %.2f",
3895 v[obj->data.loadavg[0] -
3897 v[obj->data.loadavg[1] -
3899 else if (obj->data.loadavg[0])
3900 snprintf(p, p_max_size, "%.2f",
3901 v[obj->data.loadavg[0] -
3905 new_goto(p, obj->data.i);
3908 new_tab(p, obj->data.pair.a, obj->data.pair.b);
3911 new_hr(p, obj->data.i);
3918 temp = get_hddtemp_info(obj->data.hddtemp.dev,
3919 obj->data.hddtemp.addr, obj->data.hddtemp.port, &unit);
3921 snprintf(p, p_max_size, "N/A");
3922 } else if (unit == '*') {
3923 snprintf(p, p_max_size, "%s", temp);
3925 snprintf(p, p_max_size, "%s°%c", temp, unit);
3930 new_offset(p, obj->data.i);
3933 new_voffset(p, obj->data.i);
3938 r = get_i2c_info(&obj->data.i2c.fd,
3940 obj->data.i2c.devtype,
3941 obj->data.i2c.type);
3943 if (r >= 100.0 || r == 0)
3944 snprintf(p, p_max_size, "%d", (int) r);
3946 snprintf(p, p_max_size, "%.1f", r);
3949 new_alignr(p, obj->data.i);
3952 new_alignc(p, obj->data.i);
3956 if ((obj->data.ifblock.s)
3957 && (stat(obj->data.ifblock.s, &tmp) ==
3959 i = obj->data.ifblock.pos;
3966 if ((obj->data.ifblock.s)
3967 && (!check_mount(obj->data.ifblock.s))) {
3968 i = obj->data.ifblock.pos;
3975 if ((obj->data.ifblock.s)
3976 && system(obj->data.ifblock.s)) {
3977 i = obj->data.ifblock.pos;
3984 snprintf(p, p_max_size, "%s", cur->uname_s.release);
3987 snprintf(p, p_max_size, "%s", cur->uname_s.machine);
3992 human_readable(cur->mem * 1024, p, 255);
3995 human_readable(cur->memmax * 1024, p, 255);
4000 snprintf(p, p_max_size, "%*Lu",
4005 snprintf(p, 4, "%*Lu ",
4012 new_bar(p, obj->data.pair.a,
4014 cur->memmax ? (cur->mem * 255) /
4019 new_graph(p, obj->a,
4020 obj->b, obj->c, obj->d,
4021 cur->memmax ? (cur->mem * 100.0) /
4022 (cur->memmax) : 0.0, 100, 1);
4026 snprintf(p, p_max_size, "%d",
4027 mixer_get_avg(obj->data.l));
4030 snprintf(p, p_max_size, "%d",
4031 mixer_get_left(obj->data.l));
4034 snprintf(p, p_max_size, "%d",
4035 mixer_get_right(obj->data.l));
4038 new_bar(p, obj->data.mixerbar.w,
4039 obj->data.mixerbar.h,
4040 mixer_get_avg(obj->data.mixerbar.
4044 new_bar(p, obj->data.mixerbar.w,
4045 obj->data.mixerbar.h,
4046 mixer_get_left(obj->data.mixerbar.
4050 new_bar(p, obj->data.mixerbar.w,
4051 obj->data.mixerbar.h,
4052 mixer_get_right(obj->data.mixerbar.
4058 snprintf(p, p_max_size, "%d", cur->mail_count);
4061 snprintf(p, p_max_size, "%d", cur->new_mail_count);
4065 snprintf(p, p_max_size, "%s",
4066 cur->uname_s.nodename);
4069 new_outline(p, obj->data.l);
4073 snprintf(p, p_max_size, "%hu", cur->procs);
4075 snprintf(p, 5, "%hu ",
4078 OBJ(running_processes) {
4080 snprintf(p, p_max_size, "%hu",
4083 snprintf(p, 3, "%hu ",
4087 snprintf(p, p_max_size, "%s", obj->data.s);
4090 new_bg(p, obj->data.l);
4093 new_stippled_hr(p, obj->data.pair.a,
4097 human_readable(cur->swap * 1024, p, 255);
4100 human_readable(cur->swapmax * 1024, p,
4104 if (cur->swapmax == 0) {
4105 strncpy(p, "No swap", 255);
4108 snprintf(p, 255, "%*Lu",
4114 snprintf(p, 4, "%*Lu ",
4122 new_bar(p, obj->data.pair.a,
4124 cur->swapmax ? (cur->swap * 255) /
4125 (cur->swapmax) : 0);
4128 snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
4131 time_t t = time(NULL);
4132 struct tm *tm = localtime(&t);
4133 setlocale(LC_TIME, "");
4134 strftime(p, p_max_size, obj->data.s, tm);
4137 time_t t = time(NULL);
4138 struct tm *tm = gmtime(&t);
4139 strftime(p, p_max_size, obj->data.s, tm);
4143 if (obj->data.tztime.tz) {
4144 oldTZ = getenv("TZ");
4145 setenv("TZ", obj->data.tztime.tz, 1);
4148 time_t t = time(NULL);
4149 struct tm *tm = localtime(&t);
4150 setlocale(LC_TIME, "");
4151 strftime(p, p_max_size, obj->data.tztime.fmt, tm);
4153 setenv("TZ", oldTZ, 1);
4158 // Needless to free oldTZ since getenv gives ptr to static data
4161 human_readable(obj->data.net->recv, p,
4165 human_readable(obj->data.net->trans, p,
4169 snprintf(p, p_max_size, "%d", total_updates);
4173 snprintf(p, p_max_size, "%d",
4174 (int) (obj->data.net->
4178 snprintf(p, 5, "%d ",
4179 (int) (obj->data.net->
4185 snprintf(p, p_max_size, "%.1f",
4187 trans_speed / 1024.0);
4189 snprintf(p, 8, "%.1f ",
4191 trans_speed / 1024.0);
4194 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
4195 obj->data.net->trans_speed = 0.01;
4196 new_graph(p, obj->a, obj->b, obj->c, obj->d,
4197 (obj->data.net->trans_speed /
4198 1024.0), obj->e, 1);
4201 format_seconds_short(p, p_max_size,
4205 format_seconds(p, p_max_size, (int) cur->uptime);
4208 #if defined(__FreeBSD__) && (defined(i386) || defined(__i386__))
4210 snprintf(p, p_max_size, "%s", get_apm_adapter());
4212 OBJ(apm_battery_life) {
4214 msg = get_apm_battery_life();
4215 snprintf(p, p_max_size, "%s", msg);
4218 OBJ(apm_battery_time) {
4220 msg = get_apm_battery_time();
4221 snprintf(p, p_max_size, "%s", msg);
4224 #endif /* __FreeBSD__ */
4228 snprintf(p, p_max_size, "%s", cur->mpd.title);
4231 snprintf(p, p_max_size, "%s", cur->mpd.artist);
4234 snprintf(p, p_max_size, "%s", cur->mpd.album);
4237 snprintf(p, p_max_size, "%s", cur->mpd.random);
4240 snprintf(p, p_max_size, "%s", cur->mpd.repeat);
4243 snprintf(p, p_max_size, "%s", cur->mpd.track);
4246 snprintf(p, p_max_size, "%s", cur->mpd.name);
4249 snprintf(p, p_max_size, "%s", cur->mpd.file);
4252 snprintf(p, p_max_size, "%i", cur->mpd.volume);
4255 snprintf(p, p_max_size, "%i", cur->mpd.bitrate);
4258 snprintf(p, p_max_size, "%s", cur->mpd.status);
4261 int days = 0, hours = 0, minutes =
4263 int tmp = cur->mpd.elapsed;
4264 while (tmp >= 86400) {
4268 while (tmp >= 3600) {
4278 snprintf(p, p_max_size, "%i days %i:%02i:%02i",
4279 days, hours, minutes,
4282 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4285 snprintf(p, p_max_size, "%i:%02i", minutes,
4289 int days = 0, hours = 0, minutes =
4291 int tmp = cur->mpd.length;
4292 while (tmp >= 86400) {
4296 while (tmp >= 3600) {
4306 snprintf(p, p_max_size,
4307 "%i days %i:%02i:%02i",
4308 days, hours, minutes,
4311 snprintf(p, p_max_size, "%i:%02i:%02i", hours,
4314 snprintf(p, p_max_size, "%i:%02i", minutes,
4318 snprintf(p, p_max_size, "%2.0f",
4319 cur->mpd.progress * 100);
4322 new_bar(p, obj->data.pair.a,
4324 (int) (cur->mpd.progress *
4328 if (strlen(cur->mpd.title) < 2 && strlen(cur->mpd.title) < 2) {
4329 snprintf(p, p_max_size, "%s", cur->mpd.file);
4331 snprintf(p, p_max_size, "%s - %s", cur->mpd.artist, cur->mpd.title);
4337 snprintf(p, p_max_size, "%s", cur->xmms2.artist);
4340 snprintf(p, p_max_size, "%s", cur->xmms2.album);
4343 snprintf(p, p_max_size, "%s", cur->xmms2.title);
4346 snprintf(p, p_max_size, "%s", cur->xmms2.genre);
4348 OBJ(xmms2_comment) {
4349 snprintf(p, p_max_size, "%s", cur->xmms2.comment);
4351 OBJ(xmms2_decoder) {
4352 snprintf(p, p_max_size, "%s", cur->xmms2.decoder);
4354 OBJ(xmms2_transport) {
4355 snprintf(p, p_max_size, "%s", cur->xmms2.transport);
4358 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4361 snprintf(p, p_max_size, "%s", cur->xmms2.status);
4364 snprintf(p, p_max_size, "%s", cur->xmms2.date);
4366 OBJ(xmms2_tracknr) {
4367 if (cur->xmms2.tracknr != -1)
4368 snprintf(p, p_max_size, "%i", cur->xmms2.tracknr);
4370 OBJ(xmms2_bitrate) {
4371 snprintf(p, p_max_size, "%i", cur->xmms2.bitrate);
4374 snprintf(p, p_max_size, "%u", cur->xmms2.id);
4377 snprintf(p, p_max_size, "%2.1f", cur->xmms2.size);
4379 OBJ(xmms2_elapsed) {
4380 int tmp = cur->xmms2.elapsed;
4381 snprintf(p, p_max_size, "%02d:%02d",
4382 tmp / 60000, (tmp / 1000) % 60);
4384 OBJ(xmms2_duration) {
4385 int tmp = cur->xmms2.duration;
4386 snprintf(p, p_max_size, "%02d:%02d",
4387 tmp / 60000, (tmp / 1000) % 60);
4389 OBJ(xmms2_percent) {
4390 snprintf(p, p_max_size, "%2.0f",
4391 cur->xmms2.progress * 100);
4394 new_bar(p, obj->data.pair.a,
4396 (int) (cur->xmms2.progress *
4400 if (strlen(cur->xmms2.title) < 2 && strlen(cur->xmms2.title) < 2) {
4401 snprintf(p, p_max_size, "%s", cur->xmms2.url);
4403 snprintf(p, p_max_size, "%s - %s", cur->xmms2.artist, cur->xmms2.title);
4408 OBJ(audacious_status) {
4409 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_STATUS]);
4411 OBJ(audacious_title) {
4412 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_TITLE]);
4414 OBJ(audacious_length) {
4415 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH]);
4417 OBJ(audacious_length_seconds) {
4418 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4420 OBJ(audacious_position) {
4421 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION]);
4423 OBJ(audacious_position_seconds) {
4424 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_POSITION_SECONDS]);
4426 OBJ(audacious_bitrate) {
4427 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_BITRATE]);
4429 OBJ(audacious_frequency) {
4430 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FREQUENCY]);
4432 OBJ(audacious_channels) {
4433 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_CHANNELS]);
4435 OBJ(audacious_filename) {
4436 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_FILENAME]);
4438 OBJ(audacious_playlist_length) {
4439 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_LENGTH]);
4441 OBJ(audacious_playlist_position) {
4442 snprintf(p, p_max_size, "%s", cur->audacious.items[AUDACIOUS_PLAYLIST_POSITION]);
4444 OBJ(audacious_bar) {
4446 progress= atof(cur->audacious.items[AUDACIOUS_POSITION_SECONDS]) /
4447 atof(cur->audacious.items[AUDACIOUS_LENGTH_SECONDS]);
4448 new_bar(p,obj->a,obj->b,(int)(progress*255.0f));
4453 snprintf(p, p_max_size, "%s", cur->bmpx.title);
4456 snprintf(p, p_max_size, "%s", cur->bmpx.artist);
4459 snprintf(p, p_max_size, "%s", cur->bmpx.album);
4462 snprintf(p, p_max_size, "%s", cur->bmpx.uri);
4465 snprintf(p, p_max_size, "%i", cur->bmpx.track);
4468 snprintf(p, p_max_size, "%i", cur->bmpx.bitrate);
4472 if (obj->data.top.type == TOP_NAME
4473 && obj->data.top.num >= 0
4474 && obj->data.top.num < 10) {
4475 // if we limit the buffer and add a bunch of space after, it stops the thing from
4476 // moving other shit around, which is really fucking annoying
4477 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
4478 } else if (obj->data.top.type == TOP_CPU
4479 && obj->data.top.num >= 0
4480 && obj->data.top.num < 10) {
4481 snprintf(p, 7, "%3.2f ",
4482 cur->cpu[obj->data.top.
4484 } else if (obj->data.top.type == TOP_PID
4485 && obj->data.top.num >= 0
4486 && obj->data.top.num < 10) {
4487 snprintf(p, 8, "%i ",
4488 cur->cpu[obj->data.top.
4490 } else if (obj->data.top.type == TOP_MEM
4491 && obj->data.top.num >= 0
4492 && obj->data.top.num < 10) {
4493 snprintf(p, 7, "%3.2f ",
4494 cur->cpu[obj->data.top.
4499 if (obj->data.top.type == TOP_NAME
4500 && obj->data.top.num >= 0
4501 && obj->data.top.num < 10) {
4502 // if we limit the buffer and add a bunch of space after, it stops the thing from
4503 // moving other shit around, which is really fucking annoying
4506 cur->memu[obj->data.top.
4508 } else if (obj->data.top.type == TOP_CPU
4509 && obj->data.top.num >= 0
4510 && obj->data.top.num < 10) {
4511 snprintf(p, 7, "%3.2f ",
4512 cur->memu[obj->data.top.
4514 } else if (obj->data.top.type == TOP_PID
4515 && obj->data.top.num >= 0
4516 && obj->data.top.num < 10) {
4517 snprintf(p, 8, "%i ",
4518 cur->memu[obj->data.top.
4520 } else if (obj->data.top.type == TOP_MEM
4521 && obj->data.top.num >= 0
4522 && obj->data.top.num < 10) {
4523 snprintf(p, 7, "%3.2f ",
4524 cur->memu[obj->data.top.
4531 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4532 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4534 obj->data.tail.last_update = current_update_time;
4538 tailstring *head = NULL;
4539 tailstring *headtmp = NULL;
4540 tailstring *freetmp = NULL;
4541 fp = fopen(obj->data.tail.logfile, "rt");
4543 ERR("tail logfile failed to open");
4546 obj->data.tail.readlines = 0;
4548 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL) {
4555 addtail(&head, obj->data.tail.buffer);
4556 obj->data.tail.readlines++;
4562 if (obj->data.tail.readlines > 0) {
4563 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4564 addtail(&headtmp, head->data);
4569 strcpy(obj->data.tail.buffer, headtmp->data);
4570 headtmp = headtmp->next;
4571 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
4573 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 */
4574 headtmp = headtmp->next;
4578 /* get rid of any ugly newlines at the end */
4579 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4580 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4582 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4586 strcpy(obj->data.tail.buffer, "Logfile Empty");
4587 snprintf(p, p_max_size, "Logfile Empty");
4588 } /* if readlines */
4590 } /* if cur_upd_time >= */
4592 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4596 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
4597 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4599 obj->data.tail.last_update = current_update_time;
4601 tailstring *head = NULL;
4602 tailstring *headtmp = NULL;
4603 tailstring *freetmp = NULL;
4604 fp = fopen(obj->data.tail.logfile, "rt");
4606 ERR("head logfile failed to open");
4608 obj->data.tail.readlines = 0;
4609 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*20, fp) != NULL && obj->data.tail.readlines <= obj->data.tail.wantedlines) {
4610 addtail(&head, obj->data.tail.buffer);
4611 obj->data.tail.readlines++;
4615 if (obj->data.tail.readlines > 0) {
4617 addtail(&headtmp, head->data);
4622 strcpy(obj->data.tail.buffer, headtmp->data);
4623 headtmp = headtmp->next;
4625 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 */
4626 headtmp = headtmp->next;
4629 /* get rid of any ugly newlines at the end */
4630 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
4631 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
4633 snprintf(p, p_max_size, "%s", obj->data.tail.buffer);
4635 strcpy(obj->data.tail.buffer, "Logfile Empty");
4636 snprintf(p, p_max_size, "Logfile Empty");
4637 } /* if readlines > 0 */
4638 } /* if fp == null */
4639 } /* cur_upd_time >= */
4641 //parse_conky_vars(obj->data.tail.buffer, p, cur);
4644 #ifdef TCP_PORT_MONITOR
4647 /* grab a pointer to this port monitor */
4648 tcp_port_monitor_t * p_monitor =
4649 find_tcp_port_monitor( info.p_tcp_port_monitor_collection,
4650 obj->data.tcp_port_monitor.port_range_begin,
4651 obj->data.tcp_port_monitor.port_range_end );
4653 snprintf(p, p_max_size, "monitor not found");
4657 /* now grab the text of interest */
4658 if ( peek_tcp_port_monitor( p_monitor,
4659 obj->data.tcp_port_monitor.item,
4660 obj->data.tcp_port_monitor.connection_index,
4661 p, p_max_size ) != 0 )
4663 snprintf(p, p_max_size, "monitor peek error");
4672 iconv_converting = 1;
4673 iconv_selected = obj->a;
4678 iconv_converting = 0;
4687 unsigned int a = strlen(p);
4690 if (a > 0 && iconv_converting && iconv_selected > 0 && (iconv_cd[iconv_selected - 1] != (iconv_t)(-1))) {
4692 size_t dummy1, dummy2;
4693 char *ptr = buff_in;
4696 dummy1 = dummy2 = a;
4698 strncpy(buff_in, p, P_MAX_SIZE);
4700 iconv(*iconv_cd[iconv_selected - 1], NULL, NULL, NULL, NULL);
4701 while (dummy1 > 0) {
4702 bytes = iconv(*iconv_cd[iconv_selected - 1], &ptr, &dummy1, &outptr, &dummy2);
4704 ERR("Iconv codeset conversion failed");
4709 /* It is nessecary when we are converting from multibyte to singlebyte codepage */
4720 double current_update_time, last_update_time;
4722 static void generate_text()
4724 struct information *cur = &info;
4732 current_update_time = get_time();
4736 /* add things to the buffer */
4742 generate_text_internal(p, P_MAX_SIZE, text_objects, text_object_count, cur);
4744 if (stuff_in_upper_case) {
4755 last_update_time = current_update_time;
4762 static void set_font()
4766 if (window.xftdraw != NULL) {
4767 XftDrawDestroy(window.xftdraw);
4769 window.xftdraw = XftDrawCreate(display, window.drawable,
4770 DefaultVisual(display,
4772 DefaultColormap(display,
4777 XSetFont(display, window.gc, fonts[selected_font].font->fid);
4783 static inline int get_string_width(const char *s)
4786 return *s ? calc_text_width(s, strlen(s)) : 0;
4792 static inline int get_string_width_special(char *s)
4805 if (*p == SPECIAL_CHAR) {
4806 /* shift everything over by 1 so that the special char doesn't mess up the size calculation */
4807 for (i = 0; i < strlen(p); i++) {
4808 *(p + i) = *(p + i + 1);
4810 if (specials[special_index+index].type == GRAPH || specials[special_index+index].type == BAR) {
4811 width += specials[special_index+index].width;
4818 if (strlen(final) > 1) {
4819 width += calc_text_width(final, strlen(final));
4829 static void text_size_updater(char *s);
4831 int last_font_height;
4832 static void update_text_area()
4836 /* update text size if it isn't fixed */
4841 text_width = minimum_width;
4844 last_font_height = font_height();
4845 for_each_line(text_buffer, text_size_updater);
4847 if (text_height < minimum_height)
4848 text_height = minimum_height;
4849 if (text_width > maximum_width && maximum_width > 0)
4850 text_width = maximum_width;
4853 /* get text position on workarea */
4854 switch (text_alignment) {
4861 x = workarea[2] - text_width - gap_x;
4868 y = workarea[3] - text_height - gap_y;
4872 x = workarea[2] - text_width - gap_x;
4873 y = workarea[3] - text_height - gap_y;
4877 case NONE: // Let the WM manage the window
4888 if (own_window && !fixed_pos) {
4891 text_start_x = border_margin + 1;
4892 text_start_y = border_margin + 1;
4893 window.x = x - border_margin - 1;
4894 window.y = y - border_margin - 1;
4898 /* If window size doesn't match to workarea's size, then window
4899 * probably includes panels (gnome).
4900 * Blah, doesn't work on KDE. */
4901 if (workarea[2] != window.width
4902 || workarea[3] != window.height) {
4916 static int cur_x, cur_y; /* current x and y for drawing */
4917 static int draw_mode; /* FG, BG or OUTLINE */
4918 static long current_color;
4921 static void text_size_updater(char *s)
4925 /* get string widths and skip specials */
4928 if (*p == SPECIAL_CHAR) {
4930 w += get_string_width(s);
4933 if (specials[special_index].type == BAR
4934 || specials[special_index].type == GRAPH) {
4935 w += specials[special_index].width;
4936 if (specials[special_index].height > last_font_height) {
4937 last_font_height = specials[special_index].height;
4938 last_font_height += font_ascent();
4940 } else if (specials[special_index].type == OFFSET) {
4941 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
4942 } else if (specials[special_index].type == VOFFSET) {
4943 last_font_height += specials[special_index].arg;
4944 } else if (specials[special_index].type == GOTO) {
4945 if (specials[special_index].arg >= 0)
4946 w += (int)specials[special_index].arg - cur_x;
4947 } else if (specials[special_index].type == TAB) {
4948 int start = specials[special_index].arg;
4949 int step = specials[special_index].width;
4950 if (!step || step < 0)
4952 w += step - (cur_x - text_start_x - start) % step;
4953 } else if (specials[special_index].type == FONT) {
4954 selected_font = specials[special_index].font_added;
4955 if (font_height() > last_font_height) {
4956 last_font_height = font_height();
4965 w += get_string_width(s);
4966 if (w > text_width) {
4969 if (text_width > maximum_width && maximum_width) {
4970 text_width = maximum_width;
4973 text_height += last_font_height;
4974 last_font_height = font_height();
4978 static inline void set_foreground_color(long c)
4981 XSetForeground(display, window.gc, c);
4985 static void draw_string(const char *s)
4989 int i, i2, pos, width_of_s;
4992 width_of_s = get_string_width(s);
4993 if (out_to_console) {
4995 fflush(stdout); /* output immediately, don't buffer */
4997 /* 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 */
4998 memset(tmpstring1,0,TEXT_BUFFER_SIZE);
4999 memset(tmpstring2,0,TEXT_BUFFER_SIZE);
5000 strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
5004 snprintf(space, 2, " ");
5006 max = ((text_width - width_of_s) / get_string_width(space));
5009 * This code looks for tabs in the text and coverts them to spaces.
5010 * The trick is getting the correct number of spaces,
5011 * and not going over the window's size without forcing
5012 * the window larger.
5014 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
5015 if (tmpstring1[i] == '\t') // 9 is ascii tab
5019 i2 < (8 - (1 + pos) % 8) && added <= max;
5022 if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
5023 fprintf(stderr,"buffer overrun detected\n");
5025 tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
5030 if (tmpstring1[i] != 9) {
5032 if ( pos > TEXT_BUFFER_SIZE-1 )
5033 fprintf(stderr,"buffer overrun detected\n");
5035 tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
5041 if (text_width == maximum_width) {
5042 /* this means the text is probably pushing the limit, so we'll chop it */
5043 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
5044 tmpstring2[strlen(tmpstring2)-1] = '\0';
5054 c.pixel = current_color;
5055 XQueryColor(display, DefaultColormap(display, screen), &c);
5058 c2.color.red = c.red;
5059 c2.color.green = c.green;
5060 c2.color.blue = c.blue;
5061 c2.color.alpha = fonts[selected_font].font_alpha;
5063 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5064 cur_x, cur_y, (XftChar8 *) s,
5067 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
5068 cur_x, cur_y, (XftChar8 *) s,
5074 XDrawString(display, window.drawable, window.gc,
5075 cur_x, cur_y, s, strlen(s));
5077 cur_x += width_of_s;
5079 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
5082 long redmask, greenmask, bluemask;
5084 void set_up_gradient()
5087 colour_depth = DisplayPlanes(display, screen);
5091 if (colour_depth != 24 && colour_depth != 16) {
5092 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
5098 for(i = (colour_depth / 3)-1; i>=0; i--) {
5100 greenmask |= 1 << i;
5103 if (colour_depth%3 == 1) {
5104 greenmask |= 1 << (colour_depth / 3);
5106 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
5107 greenmask = greenmask << (colour_depth / 3);
5110 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 */
5112 int red1, green1, blue1; // first colour
5113 int red2, green2, blue2; // second colour
5114 int red3 = 0, green3 = 0, blue3 = 0; // difference
5115 short redshift = (2*colour_depth / 3 + colour_depth%3);
5116 short greenshift = (colour_depth / 3);
5117 red1 = (first_colour & redmask) >> redshift;
5118 green1 = (first_colour & greenmask) >> greenshift;
5119 blue1 = first_colour & bluemask;
5120 red2 = (last_colour & redmask) >> redshift;
5121 green2 = (last_colour & greenmask) >> greenshift;
5122 blue2 = last_colour & bluemask;
5129 if (green1 > green2) {
5132 if (green1 < green2) {
5135 if (blue1 > blue2) {
5138 if (blue1 < blue2) {
5153 if (red1 > bluemask) {
5156 if (green1 > bluemask) {
5159 if (blue1 > bluemask) {
5162 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
5166 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
5167 if (colour_depth == 0) {
5170 int red1, green1, blue1; // first colour
5171 int red2, green2, blue2; // second colour
5172 long redshift = (2*colour_depth / 3 + colour_depth%3);
5173 long greenshift = (colour_depth / 3);
5174 int red3 = 0, green3 = 0, blue3 = 0; // difference
5175 red1 = (first_colour & redmask) >> redshift;
5176 green1 = (first_colour & greenmask) >> greenshift;
5177 blue1 = first_colour & bluemask;
5178 red2 = (last_colour & redmask) >> redshift;
5179 green2 = (last_colour & greenmask) >> greenshift;
5180 blue2 = last_colour & bluemask;
5181 red3 = abs(red1 - red2);
5182 green3 = abs(green1 - green2);
5183 blue3 = abs(blue1 - blue2);
5192 static void draw_line(char *s)
5196 cur_x = text_start_x;
5197 cur_y += font_ascent();
5199 short font_h = font_height();
5201 /* find specials and draw stuff */
5204 if (*p == SPECIAL_CHAR) {
5207 /* draw string before special */
5214 switch (specials[special_index].type) {
5215 case HORIZONTAL_LINE:
5218 specials[special_index].height;
5219 int mid = font_ascent() / 2;
5220 w = text_start_x + text_width -
5223 XSetLineAttributes(display,
5228 XDrawLine(display, window.drawable,
5239 specials[special_index].height;
5241 specials[special_index].arg;
5242 int mid = font_ascent() / 2;
5243 char ss[2] = { s, s };
5244 w = text_start_x + text_width -
5247 XSetLineAttributes(display,
5252 XSetDashes(display, window.gc, 0,
5254 XDrawLine(display, window.drawable,
5264 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5268 specials[special_index].height;
5270 specials[special_index].arg;
5271 int by = cur_y - (font_ascent() / 2) - 1;
5272 if (h < font_height()) {
5275 w = specials[special_index].width;
5278 text_width - cur_x - 1;
5282 XSetLineAttributes(display,
5288 XDrawRectangle(display,
5292 XFillRectangle(display,
5296 w * bar_usage / 255,
5298 if (specials[special_index].
5300 && specials[special_index].
5304 [special_index].height;
5311 if (cur_x - text_start_x > maximum_width && maximum_width > 0) {
5315 specials[special_index].height;
5316 unsigned long last_colour = current_color;
5317 int by = cur_y - (font_ascent()/2) - 1;
5318 if (h < font_height()) {
5321 w = specials[special_index].width;
5323 w = text_start_x + text_width - cur_x - 1;
5326 if (draw_graph_borders) {
5327 XSetLineAttributes(display, window.gc, 1, LineSolid, CapButt, JoinMiter);
5328 XDrawRectangle(display,window.drawable, window.gc, cur_x, by, w, h);
5330 XSetLineAttributes(display,
5337 int gradient_size = 0;
5338 float gradient_factor = 0;
5339 float gradient_update = 0;
5340 unsigned long tmpcolour = current_color;
5341 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5342 tmpcolour = specials[special_index].last_colour;
5343 gradient_size = gradient_max(specials[special_index].last_colour, specials[special_index].first_colour);
5344 gradient_factor = (float)gradient_size / (w - 2);
5346 for (i = w - 2; i > -1; i--) {
5347 if (specials[special_index].last_colour != 0 || specials[special_index].first_colour != 0) {
5348 XSetForeground(display, window.gc, tmpcolour);
5349 gradient_update += gradient_factor;
5350 while (gradient_update > 0) {
5351 tmpcolour = do_gradient(tmpcolour, specials[special_index].first_colour);
5355 if ((w - i) / ((float) (w - 2) / (specials[special_index].graph_width)) > j && j < MAX_GRAPH_DEPTH - 3) {
5358 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 */
5360 if (specials[special_index].
5362 && specials[special_index].
5366 [special_index].height;
5368 /* if (draw_mode == BG) {
5369 set_foreground_color(default_bg_color);
5371 else if (draw_mode == OUTLINE) {
5372 set_foreground_color(default_out_color);
5374 set_foreground_color(default_fg_color);
5376 set_foreground_color(last_colour);
5382 int old = font_ascent();
5383 cur_y -= font_ascent();
5384 selected_font = specials[special_index].font_added;
5385 if (cur_y + font_ascent() < cur_y + old) {
5388 cur_y += font_ascent();
5394 if (draw_mode == FG)
5395 set_foreground_color(specials
5401 if (draw_mode == BG)
5402 set_foreground_color(specials
5408 if (draw_mode == OUTLINE)
5409 set_foreground_color(specials
5415 w += specials[special_index].arg;
5419 cur_y += specials[special_index].arg;
5423 if (specials[special_index].arg >= 0)
5424 cur_x = (int)specials[special_index].arg;
5429 int start = specials[special_index].arg;
5430 int step = specials[special_index].width;
5431 if (!step || step < 0)
5433 w = step - (cur_x - text_start_x - start) % step;
5439 int pos_x = text_start_x + text_width - get_string_width_special(s) /*+ border_margin*/;
5440 /*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);*/
5441 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
5442 cur_x = pos_x - specials[special_index].arg;
5449 int pos_x = (text_width)/2 - get_string_width_special(s)/2 - (cur_x - text_start_x);
5450 /*int pos_x = text_start_x + text_width/2 - get_string_width_special(s)/2;*/
5451 /*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);*/
5453 specials[special_index].arg)
5456 [special_index].arg;
5473 if (cur_y_add > 0) {
5475 cur_y -= font_descent();
5480 cur_y += font_descent();
5484 static void draw_text()
5487 cur_y = text_start_y;
5490 if (draw_borders && border_width > 0) {
5491 unsigned int b = (border_width + 1) / 2;
5493 if (stippled_borders) {
5495 { stippled_borders, stippled_borders };
5496 XSetLineAttributes(display, window.gc,
5497 border_width, LineOnOffDash,
5498 CapButt, JoinMiter);
5499 XSetDashes(display, window.gc, 0, ss, 2);
5501 XSetLineAttributes(display, window.gc,
5502 border_width, LineSolid,
5503 CapButt, JoinMiter);
5506 XDrawRectangle(display, window.drawable, window.gc,
5507 text_start_x - border_margin + b,
5508 text_start_y - border_margin + b,
5509 text_width + border_margin * 2 - 1 - b * 2,
5510 text_height + border_margin * 2 - 1 -
5517 for_each_line(text_buffer, draw_line);
5520 static void draw_stuff()
5524 if (draw_shades && !draw_outline) {
5527 set_foreground_color(default_bg_color);
5537 for (i = -1; i < 2; i++)
5538 for (j = -1; j < 2; j++) {
5539 if (i == 0 && j == 0)
5543 set_foreground_color(default_out_color);
5544 draw_mode = OUTLINE;
5551 set_foreground_color(default_fg_color);
5559 swap.swap_window = window.window;
5560 swap.swap_action = XdbeBackground;
5561 XdbeSwapBuffers(display, &swap, 1);
5567 static void clear_text(int exposures)
5571 return; /* The swap action is XdbeBackground, which clears */
5575 /* there is some extra space for borders and outlines */
5576 XClearArea(display, window.window,
5577 text_start_x - border_margin - 1,
5578 text_start_y - border_margin - 1,
5579 text_width + border_margin * 2 + 2,
5580 text_height + border_margin * 2 + 2,
5581 exposures ? True : 0);
5586 static int need_to_update;
5588 /* update_text() generates new text and clears old text area */
5589 static void update_text()
5598 static void main_loop()
5600 #ifdef SIGNAL_BLOCKING
5601 sigset_t newmask, oldmask;
5603 sigemptyset(&newmask);
5604 sigaddset(&newmask,SIGINT);
5605 sigaddset(&newmask,SIGTERM);
5606 sigaddset(&newmask,SIGUSR1);
5610 Region region = XCreateRegion();
5612 int event_base, error_base;
5613 if (!XDamageQueryExtension (display, &event_base, &error_base)) {
5614 ERR("Xdamage extension unavailable");
5616 Damage damage = XDamageCreate(display, window.window, XDamageReportNonEmpty);
5617 XserverRegion region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
5618 XserverRegion part = XFixesCreateRegionFromWindow(display, window.window, 0);
5619 #endif /* HAVE_XDAMAGE */
5623 while (total_run_times == 0 || info.looped < total_run_times) {
5626 #ifdef SIGNAL_BLOCKING
5627 /* block signals. we will inspect for pending signals later */
5628 if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
5629 CRIT_ERR("unable to sigprocmask()");
5635 /* wait for X event or timeout */
5637 if (!XPending(display)) {
5642 update_interval - (get_time() -
5648 tv.tv_sec = (long) t;
5649 tv.tv_usec = (long) (t * 1000000) % 1000000;
5651 FD_SET(ConnectionNumber(display), &fdsr);
5654 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
5657 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
5662 ERR("can't select(): %s",
5673 if (need_to_update) {
5675 int wx = window.x, wy = window.y;
5683 /* resize window if it isn't right size */
5685 (text_width + border_margin * 2 + 1 !=
5687 || text_height + border_margin * 2 + 1 !=
5691 border_margin * 2 + 1;
5694 border_margin * 2 + 1;
5695 XResizeWindow(display,
5700 set_transparent_background(window.window);
5704 /* move window if it isn't in right position */
5707 || window.y != wy)) {
5708 XMoveWindow(display, window.window,
5709 window.x, window.y);
5719 r.x = text_start_x - border_margin;
5720 r.y = text_start_y - border_margin;
5721 r.width = text_width + border_margin * 2;
5722 r.height = text_height + border_margin * 2;
5723 XUnionRectWithRegion(&r, region, region);
5728 /* handle X events */
5730 while (XPending(display)) {
5732 XNextEvent(display, &ev);
5739 r.width = ev.xexpose.width;
5740 r.height = ev.xexpose.height;
5741 XUnionRectWithRegion(&r, region,
5747 case ReparentNotify:
5748 /* set background to ParentRelative for all parents */
5750 set_transparent_background(window.
5755 case ConfigureNotify:
5757 /* if window size isn't what expected, set fixed size */
5758 if (ev.xconfigure.width !=
5760 || ev.xconfigure.height !=
5762 if (window.width != 0
5763 && window.height != 0)
5766 /* clear old stuff before screwing up size and pos */
5772 if (XGetWindowAttributes(display, window.window, &attrs)) {
5787 border_margin * 2 - 1;
5790 border_margin * 2 - 1;
5791 if (text_width > maximum_width && maximum_width > 0)
5792 text_width = maximum_width;
5795 /* if position isn't what expected, set fixed pos, total_updates
5796 * avoids setting fixed_pos when window is set to weird locations
5798 /*if (total_updates >= 2 this is broken
5800 && (window.x != ev.xconfigure.x
5803 && (ev.xconfigure.x != 0
5804 || ev.xconfigure.y != 0)) {
5814 /* forward the click to the desktop window */
5815 XUngrabPointer(display, ev.xbutton.time);
5816 ev.xbutton.window = window.desktop;
5817 XSendEvent(display, ev.xbutton.window, False, ButtonPressMask, &ev);
5824 /* forward the release to the desktop window */
5825 ev.xbutton.window = window.desktop;
5826 XSendEvent(display, ev.xbutton.window, False, ButtonReleaseMask, &ev);
5834 if (ev.type == event_base + XDamageNotify) {
5835 XDamageNotifyEvent *dev = (XDamageNotifyEvent *) &ev;
5836 XFixesSetRegion(display, part, &dev->area, 1);
5837 XFixesUnionRegion(display, region2, region2, part);
5839 #endif /* HAVE_XDAMAGE */
5844 XDamageSubtract(display, damage, region2, None);
5845 XFixesSetRegion(display, region2, 0, 0);
5846 #endif /* HAVE_XDAMAGE */
5848 /* XDBE doesn't seem to provide a way to clear the back buffer without
5849 * interfering with the front buffer, other than passing XdbeBackground
5850 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
5851 * redraw the text even if it wasn't part of the exposed area. OTOH,
5852 * if we're not going to call draw_stuff at all, then no swap happens
5853 * and we can safely do nothing.
5856 if (!XEmptyRegion(region)) {
5860 r.x = text_start_x - border_margin;
5861 r.y = text_start_y - border_margin;
5862 r.width = text_width + border_margin * 2;
5863 r.height = text_height + border_margin * 2;
5864 XUnionRectWithRegion(&r, region, region);
5867 XSetRegion(display, window.gc, region);
5870 XftDrawSetClip(window.xftdraw, region);
5876 XDestroyRegion(region);
5877 region = XCreateRegion();
5881 #ifdef SIGNAL_BLOCKING
5882 /* unblock signals of interest and let handler fly */
5883 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
5884 CRIT_ERR("unable to sigprocmask()");
5887 switch(g_signal_pending) {
5890 ERR("received SIGUSR1. reloading the config file.");
5897 ERR("received SIGINT or SIGTERM to terminate. bye!");
5900 XDestroyRegion(region);
5903 return; /* return from main_loop */
5908 /* Reaching here means someone set a signal( SIGXXXX, signal_handler )
5909 * but didn't write any code to deal with it. if you don't want to
5910 * handle a signal, don't set a handler on it in the first place. */
5911 if (g_signal_pending)
5912 ERR("ignoring signal (%d)", g_signal_pending);
5919 #if defined(X11) && defined(HAVE_XDAMAGE)
5920 XDamageDestroy(display, damage);
5921 XFixesDestroyRegion(display, region2);
5922 XFixesDestroyRegion(display, part);
5923 XDestroyRegion(region);
5925 #endif /* X11 && HAVE_XDAMAGE */
5928 static void load_config_file(const char *);
5930 /* reload the config file */
5931 void reload_config(void)
5933 timed_thread_destroy_registered_threads ();
5935 if (info.cpu_usage) {
5936 free(info.cpu_usage);
5937 info.cpu_usage = NULL;
5940 #ifdef TCP_PORT_MONITOR
5941 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
5944 if (current_config) {
5946 load_config_file(current_config);
5950 XClearWindow(display, RootWindow(display, screen)); // clear the window first
5953 #ifdef TCP_PORT_MONITOR
5954 info.p_tcp_port_monitor_collection = NULL;
5957 if (create_audacious_thread() !=0)
5958 CRIT_ERR("unable to create audacious thread!");
5959 timed_thread_register (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
5961 extract_variable_text(text);
5970 timed_thread_destroy_registered_threads ();
5972 if (info.cpu_usage) {
5973 free(info.cpu_usage);
5974 info.cpu_usage = NULL;
5979 XdbeDeallocateBackBufferName(display, window.back_buffer);
5985 XDestroyWindow(display, window.window);
5986 XClearWindow(display, RootWindow(display, screen));
5992 XClearWindow(display, RootWindow(display, screen));
5997 XFreeGC(display, window.gc);
6001 /* it is really pointless to free() memory at the end of program but ak|ra
6002 * wants me to do this */
6004 free_text_objects(text_object_count, text_objects);
6005 text_object_count = 0;
6006 text_objects = NULL;
6008 if (text != original_text)
6011 free(current_config);
6012 free(current_mail_spool);
6013 #ifdef TCP_PORT_MONITOR
6014 destroy_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
6015 info.p_tcp_port_monitor_collection = NULL;
6019 static int string_to_bool(const char *s)
6023 if (strcasecmp(s, "yes") == 0)
6025 if (strcasecmp(s, "true") == 0)
6027 if (strcasecmp(s, "1") == 0)
6032 static enum alignment string_to_alignment(const char *s)
6034 if (strcasecmp(s, "top_left") == 0)
6036 else if (strcasecmp(s, "top_right") == 0)
6038 else if (strcasecmp(s, "bottom_left") == 0)
6040 else if (strcasecmp(s, "bottom_right") == 0)
6041 return BOTTOM_RIGHT;
6042 else if (strcasecmp(s, "tl") == 0)
6044 else if (strcasecmp(s, "tr") == 0)
6046 else if (strcasecmp(s, "bl") == 0)
6048 else if (strcasecmp(s, "br") == 0)
6049 return BOTTOM_RIGHT;
6050 else if (strcasecmp(s, "none") == 0)
6057 static void set_default_configurations(void)
6059 fork_to_background = 0;
6060 total_run_times = 0;
6061 info.cpu_avg_samples = 2;
6062 info.net_avg_samples = 2;
6067 strcpy(info.mpd.host, "localhost");
6068 info.mpd.port = 6600;
6069 info.mpd.status = NULL;
6070 info.mpd.artist = NULL;
6071 info.mpd.album = NULL;
6072 info.mpd.title = NULL;
6073 info.mpd.random = NULL;
6074 info.mpd.track = NULL;
6075 info.mpd.name = NULL;
6076 info.mpd.file = NULL;
6079 info.xmms2.artist = NULL;
6080 info.xmms2.album = NULL;
6081 info.xmms2.title = NULL;
6082 info.xmms2.genre = NULL;
6083 info.xmms2.comment = NULL;
6084 info.xmms2.decoder = NULL;
6085 info.xmms2.transport = NULL;
6086 info.xmms2.url = NULL;
6087 info.xmms2.status = NULL;
6096 default_fg_color = WhitePixel(display, screen);
6097 default_bg_color = BlackPixel(display, screen);
6098 default_out_color = BlackPixel(display, screen);
6101 draw_graph_borders = 1;
6103 set_first_font("6x10");
6111 window.type=TYPE_NORMAL;
6113 strcpy(window.wm_class_name, "conky");
6115 stippled_borders = 0;
6118 text_alignment = BOTTOM_LEFT;
6121 free(current_mail_spool);
6124 variable_substitute(MAIL_FILE, buf, 256);
6126 current_mail_spool = strdup(buf);
6130 update_interval = 10.0;
6131 stuff_in_upper_case = 0;
6133 #ifdef TCP_PORT_MONITOR
6134 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6135 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6139 static void load_config_file(const char *f)
6141 #define CONF_ERR ERR("%s: %d: config file error", f, line)
6145 set_default_configurations();
6151 char buf[256], *p, *p2, *name, *value;
6153 if (fgets(buf, 256, fp) == NULL)
6158 /* break at comment */
6159 p2 = strchr(p, '#');
6164 while (*p && isspace((int) *p))
6167 continue; /* empty line */
6173 while (*p2 && !isspace((int) *p2))
6176 *p2 = '\0'; /* break at name's end */
6183 while (*p && isspace((int) *p))
6188 p2 = value + strlen(value);
6189 while (isspace((int) *(p2 - 1)))
6195 #define CONF2(a) if (strcasecmp(name, a) == 0)
6196 #define CONF(a) else CONF2(a)
6197 #define CONF3(a,b) \
6198 else if (strcasecmp(name, a) == 0 || strcasecmp(name, b) == 0)
6202 CONF2("alignment") {
6204 int a = string_to_alignment(value);
6212 CONF("background") {
6213 fork_to_background = string_to_bool(value);
6217 CONF2("background") {
6218 fork_to_background = string_to_bool(value);
6222 CONF("border_margin") {
6224 border_margin = strtol(value, 0, 0);
6228 CONF("border_width") {
6230 border_width = strtol(value, 0, 0);
6234 CONF("default_color") {
6236 default_fg_color = get_x11_color(value);
6240 CONF3("default_shade_color", "default_shadecolor") {
6242 default_bg_color = get_x11_color(value);
6246 CONF3("default_outline_color", "default_outlinecolor") {
6248 default_out_color = get_x11_color(value);
6255 info.mail = parse_mail_args(IMAP, value);
6262 info.mail = parse_mail_args(POP3, value);
6270 strncpy(info.mpd.host, value, 127);
6276 info.mpd.port = strtol(value, 0, 0);
6277 if (info.mpd.port < 1
6278 || info.mpd.port > 0xffff)
6282 CONF("mpd_password") {
6284 strncpy(info.mpd.password, value, 127);
6289 CONF("cpu_avg_samples") {
6291 cpu_avg_samples = strtol(value, 0, 0);
6292 if (cpu_avg_samples < 1
6293 || cpu_avg_samples > 14)
6302 CONF("net_avg_samples") {
6304 net_avg_samples = strtol(value, 0, 0);
6305 if (net_avg_samples < 1
6306 || net_avg_samples > 14)
6322 CONF("double_buffer") {
6323 use_xdbe = string_to_bool(value);
6327 CONF("override_utf8_locale") {
6328 utf8_mode = string_to_bool(value);
6331 CONF("draw_borders") {
6332 draw_borders = string_to_bool(value);
6334 CONF("draw_graph_borders") {
6335 draw_graph_borders = string_to_bool(value);
6337 CONF("draw_shades") {
6338 draw_shades = string_to_bool(value);
6340 CONF("draw_outline") {
6341 draw_outline = string_to_bool(value);
6344 CONF("out_to_console") {
6345 out_to_console = string_to_bool(value);
6347 CONF("use_spacer") {
6348 use_spacer = string_to_bool(value);
6353 use_xft = string_to_bool(value);
6357 set_first_font(value);
6362 if (value && font_count >= 0)
6363 fonts[0].font_alpha = atof(value)
6372 if (string_to_bool(value))
6373 ERR("Xft not enabled");
6376 /* xftfont silently ignored when no Xft */
6379 /* xftalpha is silently ignored when no Xft */
6384 set_first_font(value);
6393 gap_x = atoi(value);
6399 gap_y = atoi(value);
6404 CONF("mail_spool") {
6407 variable_substitute(value, buf, 256);
6411 if (current_mail_spool)
6412 free(current_mail_spool);
6413 current_mail_spool = strdup(buf);
6419 CONF("minimum_size") {
6422 (value, "%d %d", &minimum_width,
6423 &minimum_height) != 2)
6426 &minimum_width) != 1)
6431 CONF("maximum_width") {
6433 if (sscanf(value, "%d", &maximum_width) != 1)
6439 CONF("no_buffers") {
6440 no_buffers = string_to_bool(value);
6442 CONF("pad_percents") {
6443 pad_percents = atoi(value);
6447 CONF("own_window") {
6448 own_window = string_to_bool(value);
6450 CONF("wm_class_name") {
6451 memset(window.wm_class_name,0,sizeof(window.wm_class_name));
6452 strncpy(window.wm_class_name, value, sizeof(window.wm_class_name)-1);
6454 CONF("own_window_transparent") {
6455 set_transparent = string_to_bool(value);
6457 CONF("own_window_colour") {
6459 background_colour = get_x11_color(value);
6461 ERR("Invalid colour for own_winder_colour (try omitting the '#' for hex colours");
6464 CONF("own_window_hints") {
6466 char *p_hint, *p_save;
6467 char delim[] = ", ";
6469 /* tokenize the value into individual hints */
6470 if ((p_hint=strtok_r(value, delim, &p_save)) != NULL)
6472 /*fprintf(stderr, "hint [%s] parsed\n", p_hint);*/
6473 if (strncmp(p_hint,"undecorate",10) == 0)
6474 SET_HINT(window.hints,HINT_UNDECORATED);
6475 else if (strncmp(p_hint,"below",5) == 0)
6476 SET_HINT(window.hints,HINT_BELOW);
6477 else if (strncmp(p_hint,"above",5) == 0)
6478 SET_HINT(window.hints,HINT_ABOVE);
6479 else if (strncmp(p_hint,"sticky",6) == 0)
6480 SET_HINT(window.hints,HINT_STICKY);
6481 else if (strncmp(p_hint,"skip_taskbar",12) == 0)
6482 SET_HINT(window.hints,HINT_SKIP_TASKBAR);
6483 else if (strncmp(p_hint,"skip_pager",10) == 0)
6484 SET_HINT(window.hints,HINT_SKIP_PAGER);
6488 p_hint=strtok_r(NULL, delim, &p_save);
6490 while (p_hint!=NULL);
6493 CONF("own_window_type") {
6495 if (strncmp(value,"normal",6)==0)
6496 window.type = TYPE_NORMAL;
6497 else if (strncmp(value,"desktop",7)==0)
6498 window.type = TYPE_DESKTOP;
6499 else if (strncmp(value,"override",8)==0)
6500 window.type = TYPE_OVERRIDE;
6506 CONF("stippled_borders") {
6508 stippled_borders = strtol(value, 0, 0);
6510 stippled_borders = 4;
6514 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
6517 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
6519 CONF("update_interval") {
6521 update_interval = strtod(value, 0);
6525 CONF("total_run_times") {
6527 total_run_times = strtod(value, 0);
6532 stuff_in_upper_case = string_to_bool(value);
6534 CONF("max_user_text") {
6536 max_user_text = atoi(value);
6541 if (text != original_text)
6551 int l = strlen(text);
6552 if (fgets(buf, 256, fp) == NULL)
6555 realloc(text, l + strlen(buf)
6559 if (strlen(text) > max_user_text)
6563 text_lines = line + 1;
6566 #ifdef TCP_PORT_MONITOR
6567 CONF("min_port_monitors")
6570 (sscanf(value, "%d", &tcp_port_monitor_collection_args.min_port_monitors) != 1) ||
6571 tcp_port_monitor_collection_args.min_port_monitors < 0 )
6573 /* an error. use default, warn and continue. */
6574 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6577 else if ( tcp_port_monitor_collection_args.min_port_monitors == 0 )
6579 /* no error, just use default */
6580 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6582 /* else tcp_port_monitor_collection_args.min_port_monitors > 0 as per config */
6584 CONF("min_port_monitor_connections")
6587 (sscanf(value, "%d", &tcp_port_monitor_args.min_port_monitor_connections) != 1)
6588 || tcp_port_monitor_args.min_port_monitor_connections < 0 )
6590 /* an error. use default, warni and continue. */
6591 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6594 else if ( tcp_port_monitor_args.min_port_monitor_connections == 0 )
6596 /* no error, just use default */
6597 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6599 /* else tcp_port_monitor_args.min_port_monitor_connections > 0 as per config */
6603 ERR("%s: %d: no such configuration: '%s'", f, line, name);
6615 /* : means that character before that takes an argument */
6616 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
6629 int main(int argc, char **argv)
6631 struct sigaction act, oact;
6634 memset(&info, 0, sizeof(info) );
6636 #ifdef TCP_PORT_MONITOR
6637 tcp_port_monitor_collection_args.min_port_monitors = MIN_PORT_MONITORS_DEFAULT;
6638 tcp_port_monitor_args.min_port_monitor_connections = MIN_PORT_MONITOR_CONNECTIONS_DEFAULT;
6641 /* handle command line parameters that don't change configs */
6646 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
6647 *s) || ((s = getenv("LANG")) && *s)) {
6648 temp = (char *)malloc((strlen(s) + 1) * sizeof(char));
6650 ERR("malloc failed");
6652 for (x = 0; x < strlen(s); x++) {
6653 temp[x] = tolower(s[x]);
6655 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
6661 if (!setlocale(LC_CTYPE, "")) {
6662 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
6666 int c = getopt(argc,
6677 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
6678 * though free() does the NULL check itself;), then load optarg value */
6680 free(current_config);
6681 current_config = strdup(optarg);
6686 ("Usage: %s [OPTION]...\n"
6687 "Conky is a system monitor that renders text on desktop or to own transparent\n"
6688 "window. Command line options will override configurations defined in config\n"
6691 " -c FILE config file to load instead of "
6694 " -d daemonize, fork to background\n"
6697 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
6698 " -f FONT font to use\n"
6700 " -o create own window to draw\n"
6703 " -b double buffer (prevents flickering)\n"
6705 " -w WIN_ID window id to draw\n"
6706 " -x X x position\n"
6707 " -y Y y position\n"
6709 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
6710 " -u SECS update interval\n"
6711 " -i NUM number of times to update Conky\n", argv[0]);
6715 window.window = strtol(optarg, 0, 0);
6724 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
6728 /* load current_config or CONFIG_FILE */
6731 if (current_config == NULL) {
6732 /* load default config file */
6735 variable_substitute(CONFIG_FILE, buf, 256);
6738 current_config = strdup(buf);
6742 if (current_config != NULL && fopen((const char *)current_config, (const char *)"r"))
6743 load_config_file(current_config);
6745 set_default_configurations();
6749 if (current_mail_spool == NULL) {
6751 variable_substitute(MAIL_FILE, buf, 256);
6754 current_mail_spool = strdup(buf);
6758 /* handle other command line arguments */
6760 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
6761 optind = optreset = 1;
6766 #if defined(__FreeBSD__)
6767 if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null",
6768 O_RDONLY, "kvm_open")) == NULL)
6769 CRIT_ERR( "cannot read kvm");
6773 int c = getopt(argc,
6781 fork_to_background = 1;
6786 set_first_font(optarg);
6789 text_alignment = string_to_alignment(optarg);
6804 if (text != original_text)
6806 text = strdup(optarg);
6807 convert_escapes(text);
6811 update_interval = strtod(optarg, 0);
6815 total_run_times = strtod(optarg, 0);
6819 gap_x = atoi(optarg);
6823 gap_y = atoi(optarg);
6837 /* generate text and get initial size */
6838 extract_variable_text(text);
6839 if (text != original_text) {
6844 if (fork_to_background) {
6848 ERR("can't fork() to background: %s",
6855 fprintf(stderr,"\n");fflush(stderr);
6859 /* parent process */
6860 fprintf(stderr,"Conky: forked to background, pid is %d\n",pid);
6871 update_text_area(); /* to get initial size of the window */
6875 text_width + border_margin * 2 + 1,
6876 text_height + border_margin * 2 + 1,
6877 set_transparent, background_colour, info.uname_s.nodename, argv, argc);
6880 update_text_area(); /* to position text/window on screen */
6885 if (own_window && !fixed_pos) {
6886 XMoveWindow(display, window.window, window.x, window.y);
6889 set_transparent_background(window.window);
6900 /* Set signal handlers */
6901 act.sa_handler = signal_handler;
6902 sigemptyset(&act.sa_mask);
6905 act.sa_flags |= SA_RESTART;
6908 if ( sigaction(SIGINT,&act,&oact) < 0 ||
6909 sigaction(SIGUSR1,&act,&oact) < 0 ||
6910 sigaction(SIGTERM,&act,&oact) < 0 )
6912 ERR("error setting signal handler: %s", strerror(errno) );
6916 if (create_audacious_thread() !=0)
6917 CRIT_ERR("unable to create audacious thread!");
6918 timed_thread_register (info.audacious.p_timed_thread, &info.audacious.p_timed_thread);
6923 #if defined(__FreeBSD__)
6930 void signal_handler(int sig)
6932 /* signal handler is light as a feather, as it should be.
6933 * we will poll g_signal_pending with each loop of conky
6934 * and do any signal processing there, NOT here. */
6935 g_signal_pending=sig;