2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
27 #include <X11/Xutil.h>
29 #include <sys/types.h>
32 #define CONFIG_FILE "$HOME/.conkyrc"
33 #define MAIL_FILE "$MAIL"
34 #define MAX_IF_BLOCK_DEPTH 5
51 char name[TEXT_BUFFER_SIZE];
61 static int selected_font = 0;
62 static int font_count = -1;
63 struct font_list *fonts = NULL;
67 #define font_height() use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
68 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
69 #define font_ascent() use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent
70 #define font_descent() use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent
74 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
75 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
76 #define font_descent() fonts[selected_font].font->max_bounds.descent
80 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
83 static void set_font();
86 int addfont(const char *data_in)
88 if (font_count > MAX_FONTS) {
89 CRIT_ERR("you don't need that many fonts, sorry.");
92 if (font_count == 0) {
96 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
100 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
102 CRIT_ERR("realloc in addfont");
104 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
105 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
107 fonts[font_count].font_alpha = 0xffff;
110 CRIT_ERR("Oops...looks like something overflowed in addfont().");
115 void set_first_font(const char *data_in)
117 if (font_count < 0) {
118 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
123 if (strlen(data_in) > 1) {
124 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
126 fonts[0].font_alpha = 0xffff;
134 for (i=0;i<=font_count;i++) {
137 XftFontClose(display, fonts[i].xftfont);
141 XFreeFont(display, fonts[i].font);
148 set_first_font("6x10");
152 static void load_fonts()
155 for (i=0;i<=font_count;i++) {
159 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
160 XftFontClose(display, fonts[i].xftfont);
162 if ((fonts[i].xftfont =
163 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
166 ERR("can't load Xft font '%s'", fonts[i].name);
167 if ((fonts[i].xftfont =
168 XftFontOpenName(display, screen,
169 "courier-12")) != NULL)
172 ERR("can't load Xft font '%s'", "courier-12");
174 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
175 CRIT_ERR("can't load font '%s'", "fixed");
182 /* load normal font */
183 /* if (fonts[i].font != NULL)
184 XFreeFont(display, fonts[i].font);*/
186 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
187 ERR("can't load font '%s'", fonts[i].name);
188 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
189 CRIT_ERR("can't load font '%s'", "fixed");
197 /* default config file */
198 static char *current_config;
200 /* set to 1 if you want all text to be in uppercase */
201 static unsigned int stuff_in_upper_case;
203 /* Update interval */
204 static double update_interval;
206 /* Run how many times? */
207 static unsigned long total_run_times;
210 static int fork_to_background;
212 static int cpu_avg_samples, net_avg_samples;
216 /* Always on bottom */
217 static int on_bottom;
219 /* Position on the screen */
220 static int text_alignment;
221 static int gap_x, gap_y;
224 static int draw_borders;
225 static int stippled_borders;
227 static int draw_shades, draw_outline;
229 static int border_margin, border_width;
231 static long default_fg_color, default_bg_color, default_out_color;
233 /* create own window or draw stuff to root? */
234 static int own_window = 0;
237 /* fixed size/pos is set if wm/user changes them */
238 static int fixed_size = 0, fixed_pos = 0;
241 static int minimum_width, minimum_height;
242 static int maximum_width;
249 /* no buffers in used memory? */
252 /* pad percentages to decimals? */
253 static int pad_percents = 0;
255 /* Text that is shown */
256 static char original_text[] =
257 "$nodename - $sysname $kernel on $machine\n"
259 "${color grey}Uptime:$color $uptime\n"
260 "${color grey}Frequency (in MHz):$color $freq\n"
261 "${color grey}Frequency (in Ghz):$color $freq_g\n"
262 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
263 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
264 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
265 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
267 "${color grey}File systems:\n"
268 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
269 "${color grey}Networking:\n"
270 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
271 "${color grey}Temperatures:\n"
272 " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
275 "${color grey}SETI@Home Statistics:\n"
276 "${color grey}Seti Unit Number:$color $seti_credit\n"
277 "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
280 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
281 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
283 "${color grey}Name PID CPU% MEM%\n"
284 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
285 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
286 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
287 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
288 "${tail /var/log/Xorg.0.log 3}";
290 static char *text = original_text;
292 static int total_updates;
295 static int blockdepth = 0;
296 static int if_jumped = 0;
297 static int blockstart[MAX_IF_BLOCK_DEPTH];
299 int check_mount(char *s)
302 FILE *mtab = fopen("/etc/mtab", "r");
304 char buf1[256], buf2[128];
305 while (fgets(buf1, 256, mtab)) {
306 sscanf(buf1, "%*s %128s", buf2);
307 if (!strcmp(s, buf2)) {
314 ERR("Could not open mtab");
321 static inline int calc_text_width(const char *s, unsigned int l)
327 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
329 XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
335 return XTextWidth(fonts[selected_font].font, s, l);
340 /* formatted text to render on screen, generated in generate_text(),
341 * drawn in draw_stuff() */
343 static char text_buffer[TEXT_BUFFER_SIZE * 4];
345 /* special stuff in text_buffer */
347 #define SPECIAL_CHAR '\x01'
364 static struct special_t {
374 unsigned long first_colour; // for graph gradient
375 unsigned long last_colour;
378 static int special_count;
380 static int special_index; /* used when drawing */
383 #define MAX_GRAPH_DEPTH 256 /* why 256? who knows. */
385 static struct special_t *new_special(char *buf, int t)
387 if (special_count >= 128)
388 CRIT_ERR("too many special things in text");
390 buf[0] = SPECIAL_CHAR;
392 if (t == GRAPH && specials[special_count].graph == NULL) {
393 if (specials[special_count].width > 0
394 && specials[special_count].width < MAX_GRAPH_DEPTH)
395 specials[special_count].graph_width = specials[special_count].width - 3; // subtract 3 for the box
397 specials[special_count].graph_width =
399 specials[special_count].graph =
400 calloc(specials[special_count].graph_width,
402 specials[special_count].graph_scale = 100;
404 specials[special_count].type = t;
405 return &specials[special_count++];
408 typedef struct tailstring_list {
409 char data[TEXT_BUFFER_SIZE];
410 struct tailstring_list *next;
411 struct tailstring_list *first;
414 void addtail(tailstring ** head, char *data_in)
417 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
423 tmp->first = (*head)->first;
425 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
430 void freetail(tailstring * head)
433 while (head != NULL) {
440 void freelasttail(tailstring * head)
442 tailstring * tmp = head;
444 if (tmp->next == head->first) {
451 while(head != NULL && tmp != NULL) {
457 static void new_bar(char *buf, int w, int h, int usage)
459 struct special_t *s = new_special(buf, BAR);
460 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
465 static const char *scan_bar(const char *args, int *w, int *h)
467 *w = 0; /* zero width means all space that is available */
469 /* bar's argument is either height or height,width */
472 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
473 sscanf(args, "%d %n", h, &n);
480 static char *scan_font(const char *args)
482 if (args && sizeof(args) < 127) {
486 ERR("font scan failed, lets hope it doesn't mess stuff up");
492 static void new_font(char *buf, char * args) {
493 struct special_t *s = new_special(buf, FONT);
494 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
495 int tmp = selected_font;
496 selected_font = s->font_added = addfont(args);
504 inline void graph_append(struct special_t *graph, double f)
508 graph->graph_scale = 0;
510 graph->graph[graph->graph_width - 1] = f; /* add new data */
511 for (i = 0; i < graph->graph_width - 1; i++) { /* shift all the data by 1 */
512 graph->graph[i] = graph->graph[i + 1];
513 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
514 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
519 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scaled, int append)
521 struct special_t *s = new_special(buf, GRAPH);
524 s->first_colour = first_colour;
525 s->last_colour = second_colour;
528 s->graph_width = s->width - 3; // subtract 3 for rectangle around
533 s->graph_scale = 100;
540 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour)
542 *w = 0; /* zero width means all space that is available */
546 /* graph's argument is either height or height,width */
548 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
549 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
552 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 3) {
555 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
558 if (sscanf(args, "%d,%d", h, w) < 2) {
561 sscanf(args, "%*s %d,%d", h, w);
573 static inline void new_hr(char *buf, int a)
575 new_special(buf, HORIZONTAL_LINE)->height = a;
578 static inline void new_stippled_hr(char *buf, int a, int b)
580 struct special_t *s = new_special(buf, STIPPLED_HR);
585 static inline void new_fg(char *buf, long c)
587 new_special(buf, FG)->arg = c;
590 static inline void new_bg(char *buf, long c)
592 new_special(buf, BG)->arg = c;
595 static inline void new_outline(char *buf, long c)
597 new_special(buf, OUTLINE)->arg = c;
600 static inline void new_offset(char *buf, long c)
602 new_special(buf, OFFSET)->arg = c;
605 static inline void new_voffset(char *buf, long c)
607 new_special(buf, VOFFSET)->arg = c;
610 static inline void new_alignr(char *buf, long c)
612 new_special(buf, ALIGNR)->arg = c;
615 static inline void new_alignc(char *buf, long c)
617 new_special(buf, ALIGNC)->arg = c;
620 /* quite boring functions */
622 static inline void for_each_line(char *b, void (*f) (char *))
626 for (ps = b, pe = b; *pe; pe++) {
639 static void convert_escapes(char *buf)
641 char *p = buf, *s = buf;
657 /* converts from bytes to human readable format (k, M, G, T) */
658 static void human_readable(long long a, char *buf, int size)
660 //Strange conditional due to possible overflows
661 if(a / 1024 / 1024 / 1024.0 > 1024.0){
662 snprintf(buf, size, "%.2fT", (a / 1024 / 1024) / 1024 / 1024.0);
664 else if (a >= 1024 * 1024 * 1024) {
665 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
667 else if (a >= 1024 * 1024) {
668 double m = (a / 1024) / 1024.0;
670 snprintf(buf, size, "%.0fM", m);
672 snprintf(buf, size, "%.1fM", m);
673 } else if (a >= 1024)
674 snprintf(buf, size, "%Ldk", a / (long long) 1024);
676 snprintf(buf, size, "%Ld", a);
681 enum text_object_type {
749 OBJ_ml_upload_counter,
750 OBJ_ml_download_counter,
751 OBJ_ml_nshared_files,
752 OBJ_ml_shared_counter,
753 OBJ_ml_tcp_upload_rate,
754 OBJ_ml_tcp_download_rate,
755 OBJ_ml_udp_upload_rate,
756 OBJ_ml_udp_download_rate,
757 OBJ_ml_ndownloaded_files,
758 OBJ_ml_ndownloading_files,
761 OBJ_running_processes,
770 OBJ_temp1, /* i2c is used instead in these */
809 char *s; /* some string */
810 int i; /* some integer */
811 long l; /* some other integer */
812 struct net_stat *net;
814 unsigned char loadavg[3];
864 static unsigned int text_object_count;
865 static struct text_object *text_objects;
867 /* new_text_object() allocates a new zeroed text_object */
868 static struct text_object *new_text_object()
871 text_objects = (struct text_object *) realloc(text_objects,
875 memset(&text_objects[text_object_count - 1], 0,
876 sizeof(struct text_object));
878 return &text_objects[text_object_count - 1];
881 static void free_text_objects()
884 for (i = 0; i < text_object_count; i++) {
885 switch (text_objects[i].type) {
887 close(text_objects[i].data.i);
890 close(text_objects[i].data.i);
893 close(text_objects[i].data.i2c.fd);
897 case OBJ_if_existing:
900 free(text_objects[i].data.ifblock.s);
904 free(text_objects[i].data.s);
907 free(text_objects[i].data.s);
910 free(text_objects[i].data.s);
912 /* case OBJ_execibar:
913 free(text_objects[i].data.s);
916 free(text_objects[i].data.s);
927 free(text_objects[i].data.s);
931 free(text_objects[i].data.execi.cmd);
932 free(text_objects[i].data.execi.buffer);
939 text_object_count = 0;
942 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
947 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
948 *a = mixer_init(buf1);
949 (void) scan_bar(arg + n, w, h);
952 (void) scan_bar(arg, w, h);
956 /* construct_text_object() creates a new text_object */
957 static void construct_text_object(const char *s, const char *arg)
959 struct text_object *obj = new_text_object();
961 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
962 #define END ; } } else
966 obj->type = OBJ_color;
967 obj->data.l = get_x11_color(s);
970 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
971 END OBJ(acpitempf, 0) obj->data.i = open_acpi_temperature(arg);
972 END OBJ(acpiacadapter, 0)
979 sscanf(arg, "%63s", bat);
982 obj->data.s = strdup(bat);
983 END OBJ(buffers, INFO_BUFFERS)
984 END OBJ(cached, INFO_BUFFERS)
985 END OBJ(cpu, INFO_CPU)
986 END OBJ(cpubar, INFO_CPU)
987 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
988 END OBJ(cpugraph, INFO_CPU)
989 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
992 obj->data.l = arg ? get_x11_color(arg) : default_fg_color;
996 obj->data.s = scan_font(arg);
998 OBJ(downspeed, INFO_NET) obj->data.net = get_net_stat(arg);
999 END OBJ(downspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1000 END OBJ(downspeedgraph, INFO_NET)
1001 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1003 sscanf(arg, "%63s %*i,%*i %*i", buf);
1004 obj->data.net = get_net_stat(buf);
1005 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1006 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
1015 text_objects[blockstart[blockdepth - 1] -
1016 1].data.ifblock.pos = text_object_count;
1017 blockstart[blockdepth - 1] = text_object_count;
1018 obj->data.ifblock.pos = text_object_count + 2;
1020 ERR("$else: no matching $if_*");
1025 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
1028 ERR("$endif: no matching $if_*");
1032 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
1033 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
1034 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
1035 END OBJ(execibar, 0) unsigned int n;
1036 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1038 ERR("${execibar <interval> command}");
1039 obj->type = OBJ_text;
1040 snprintf(buf, 256, "${%s}", s);
1041 obj->data.s = strdup(buf);
1043 obj->data.s = strdup(arg + n);
1045 END OBJ(execigraph, 0) unsigned int n;
1046 if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1048 ERR("${execigraph <interval> command}");
1049 obj->type = OBJ_text;
1050 snprintf(buf, 256, "${%s}", s);
1051 obj->data.s = strdup(buf);
1053 obj->data.s = strdup(arg + n);
1055 END OBJ(execi, 0) unsigned int n;
1058 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
1060 ERR("${execi <interval> command}");
1061 obj->type = OBJ_text;
1062 snprintf(buf, 256, "${%s}", s);
1063 obj->data.s = strdup(buf);
1065 obj->data.execi.cmd = strdup(arg + n);
1066 obj->data.execi.buffer =
1067 (char *) calloc(1, TEXT_BUFFER_SIZE);
1069 END OBJ(pre_exec, 0) obj->type = OBJ_text;
1071 FILE *fp = popen(arg, "r");
1075 n = fread(buf, 1, 2048, fp);
1078 if (n && buf[n - 1] == '\n')
1083 obj->data.s = strdup(buf);
1085 obj->data.s = strdup("");
1088 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1089 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1091 while (isspace(*arg))
1097 obj->data.fsbar.fs = prepare_fs_stat(arg);
1098 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1101 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1105 obj->data.fsbar.fs = prepare_fs_stat(arg);
1106 END OBJ(fs_free, INFO_FS) if (!arg)
1108 obj->data.fs = prepare_fs_stat(arg);
1109 END OBJ(fs_used_perc, INFO_FS) if (!arg)
1111 obj->data.fs = prepare_fs_stat(arg);
1112 END OBJ(fs_free_perc, INFO_FS) if (!arg)
1114 obj->data.fs = prepare_fs_stat(arg);
1115 END OBJ(fs_size, INFO_FS) if (!arg)
1117 obj->data.fs = prepare_fs_stat(arg);
1118 END OBJ(fs_used, INFO_FS) if (!arg)
1120 obj->data.fs = prepare_fs_stat(arg);
1121 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
1122 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
1123 END OBJ(voffset, 0) obj->data.i = arg ? atoi(arg) : 1;
1124 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
1128 ERR("i2c needs arguments");
1129 obj->type = OBJ_text;
1130 obj->data.s = strdup("${i2c}");
1134 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1135 /* if scanf couldn't read three values, read type and num and use
1137 sscanf(arg, "%63s %d", buf2, &n);
1139 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1140 obj->data.i2c.devtype);
1141 strcpy(obj->data.i2c.type, buf2);
1144 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1145 obj->data.i2c.devtype);
1146 strcpy(obj->data.i2c.type, buf2);
1149 END OBJ(top, INFO_TOP)
1153 ERR("top needs arguments");
1154 obj->type = OBJ_text;
1155 obj->data.s = strdup("${top}");
1158 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1159 if (strcmp(buf, "name") == 0) {
1160 obj->data.top.type = TOP_NAME;
1161 } else if (strcmp(buf, "cpu") == 0) {
1162 obj->data.top.type = TOP_CPU;
1163 } else if (strcmp(buf, "pid") == 0) {
1164 obj->data.top.type = TOP_PID;
1165 } else if (strcmp(buf, "mem") == 0) {
1166 obj->data.top.type = TOP_MEM;
1168 ERR("invalid arg for top");
1171 if (n < 1 || n > 10) {
1172 CRIT_ERR("invalid arg for top");
1175 obj->data.top.num = n - 1;
1179 ERR("invalid args given for top");
1182 END OBJ(top_mem, INFO_TOP)
1186 ERR("top_mem needs arguments");
1187 obj->type = OBJ_text;
1188 obj->data.s = strdup("${top_mem}");
1191 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1192 if (strcmp(buf, "name") == 0) {
1193 obj->data.top.type = TOP_NAME;
1194 } else if (strcmp(buf, "cpu") == 0) {
1195 obj->data.top.type = TOP_CPU;
1196 } else if (strcmp(buf, "pid") == 0) {
1197 obj->data.top.type = TOP_PID;
1198 } else if (strcmp(buf, "mem") == 0) {
1199 obj->data.top.type = TOP_MEM;
1201 ERR("invalid arg for top");
1204 if (n < 1 || n > 10) {
1205 CRIT_ERR("invalid arg for top");
1208 obj->data.top.num = n - 1;
1212 ERR("invalid args given for top");
1215 END OBJ(addr, INFO_NET) obj->data.net = get_net_stat(arg);
1216 END OBJ(linkstatus, INFO_WIFI) obj->data.net = get_net_stat(arg);
1221 ERR("tail needs arguments");
1222 obj->type = OBJ_text;
1223 obj->data.s = strdup("${tail}");
1226 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1227 if (n1 < 1 || n1 > 30) {
1228 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
1232 fp = fopen(buf, "rt");
1234 obj->data.tail.logfile =
1235 malloc(TEXT_BUFFER_SIZE);
1236 strcpy(obj->data.tail.logfile, buf);
1237 obj->data.tail.wantedlines = n1 - 1;
1238 obj->data.tail.interval =
1239 update_interval * 2;
1243 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1246 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1247 if (n1 < 1 || n1 > 30) {
1249 ("invalid arg for tail, number of lines must be between 1 and 30");
1251 } else if (n2 < 1 || n2 < update_interval) {
1253 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1257 fp = fopen(buf, "rt");
1259 obj->data.tail.logfile =
1260 malloc(TEXT_BUFFER_SIZE);
1261 strcpy(obj->data.tail.logfile, buf);
1262 obj->data.tail.wantedlines = n1 - 1;
1263 obj->data.tail.interval = n2;
1267 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
1273 ERR("invalid args given for tail");
1276 obj->data.tail.buffer = malloc(TEXT_BUFFER_SIZE * 6); /* asumming all else worked */
1277 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
1279 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1280 if (r >= 3 && (c < 1 || c > 3))
1282 if (r >= 2 && (b < 1 || b > 3))
1284 if (r >= 1 && (a < 1 || a > 3))
1287 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1288 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1289 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1290 END OBJ(if_existing, 0)
1291 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1292 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1295 ERR("if_existing needs an argument");
1296 obj->data.ifblock.s = 0;
1298 obj->data.ifblock.s = strdup(arg);
1299 blockstart[blockdepth] = text_object_count;
1300 obj->data.ifblock.pos = text_object_count + 2;
1302 END OBJ(if_mounted, 0)
1303 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1304 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1307 ERR("if_mounted needs an argument");
1308 obj->data.ifblock.s = 0;
1310 obj->data.ifblock.s = strdup(arg);
1311 blockstart[blockdepth] = text_object_count;
1312 obj->data.ifblock.pos = text_object_count + 2;
1314 END OBJ(if_running, 0)
1315 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1316 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1320 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1321 obj->data.ifblock.s = strdup(buf);
1323 ERR("if_running needs an argument");
1324 obj->data.ifblock.s = 0;
1326 blockstart[blockdepth] = text_object_count;
1327 obj->data.ifblock.pos = text_object_count + 2;
1331 END OBJ(mails, INFO_MAIL)
1332 END OBJ(mem, INFO_MEM)
1333 END OBJ(memmax, INFO_MEM)
1334 END OBJ(memperc, INFO_MEM)
1335 END OBJ(membar, INFO_MEM)
1336 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1337 END OBJ(memgraph, INFO_MEM)
1338 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1339 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
1340 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
1341 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
1342 END OBJ(mixerbar, INFO_MIXER)
1343 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1344 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1345 END OBJ(mixerlbar, INFO_MIXER)
1346 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1347 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1348 END OBJ(mixerrbar, INFO_MIXER)
1349 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1350 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1353 OBJ(ml_upload_counter, INFO_MLDONKEY)
1354 END OBJ(ml_download_counter, INFO_MLDONKEY)
1355 END OBJ(ml_nshared_files, INFO_MLDONKEY)
1356 END OBJ(ml_shared_counter, INFO_MLDONKEY)
1357 END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1358 END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1359 END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1360 END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1361 END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1362 END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
1364 OBJ(new_mails, INFO_MAIL)
1365 END OBJ(nodename, 0)
1366 END OBJ(processes, INFO_PROCS)
1367 END OBJ(running_processes, INFO_RUN_PROCS)
1368 END OBJ(shadecolor, 0)
1370 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1372 END OBJ(outlinecolor, 0)
1374 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1376 END OBJ(stippled_hr, 0)
1378 int a = stippled_borders, b = 1;
1380 if (sscanf(arg, "%d %d", &a, &b) != 2)
1381 sscanf(arg, "%d", &b);
1385 obj->data.pair.a = a;
1386 obj->data.pair.b = b;
1388 END OBJ(swap, INFO_MEM)
1389 END OBJ(swapmax, INFO_MEM)
1390 END OBJ(swapperc, INFO_MEM)
1391 END OBJ(swapbar, INFO_MEM)
1392 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1393 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1395 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1396 obj->data.i2c.devtype);
1397 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1399 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1400 obj->data.i2c.devtype);
1401 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1402 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1403 END OBJ(totaldown, INFO_NET) obj->data.net = get_net_stat(arg);
1404 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1406 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 0;
1407 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 0;
1408 END OBJ(upspeed, INFO_NET) obj->data.net = get_net_stat(arg);
1409 END OBJ(upspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1410 END OBJ(upspeedgraph, INFO_NET)
1411 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1413 sscanf(arg, "%63s %*i,%*i %*i", buf);
1414 obj->data.net = get_net_stat(buf);
1415 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1416 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
1421 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1422 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
1424 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1425 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1426 END OBJ(seti_credit, INFO_SETI) END
1429 OBJ(mpd_artist, INFO_MPD)
1430 END OBJ(mpd_title, INFO_MPD)
1431 END OBJ(mpd_elapsed, INFO_MPD)
1432 END OBJ(mpd_length, INFO_MPD)
1433 END OBJ(mpd_percent, INFO_MPD)
1434 END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol,
1435 INFO_MPD) END OBJ(mpd_bitrate,
1437 END OBJ(mpd_status, INFO_MPD) END OBJ(mpd_bar, INFO_MPD)
1438 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1443 ERR("unknown variable %s", s);
1444 obj->type = OBJ_text;
1445 snprintf(buf, 256, "${%s}", s);
1446 obj->data.s = strdup(buf);
1451 /* append_text() appends text to last text_object if it's text, if it isn't
1452 * it creates a new text_object */
1453 static void append_text(const char *s)
1455 struct text_object *obj;
1457 if (s == NULL || *s == '\0')
1460 obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1462 /* create a new text object? */
1463 if (!obj || obj->type != OBJ_text) {
1464 obj = new_text_object();
1465 obj->type = OBJ_text;
1466 obj->data.s = strdup(s);
1469 obj->data.s = (char *) realloc(obj->data.s,
1470 strlen(obj->data.s) +
1472 strcat(obj->data.s, s);
1476 static void extract_variable_text(const char *p)
1480 free_text_objects();
1495 /* variable is either $foo or ${foo} */
1499 while (*p && *p != '}')
1505 while (*p && (isalnum((int) *p)
1510 /* copy variable to buffer */
1511 len = (p - s > 255) ? 255 : (p - s);
1512 strncpy(buf, s, len);
1521 /* if variable wasn't found from environment, use some special */
1527 if (strchr(buf, ' ')) {
1528 arg = strchr(buf, ' ');
1531 while (isspace((int) *arg))
1537 /* lowercase variable name */
1544 construct_text_object(buf, arg);
1555 ERR("one or more $endif's are missing");
1559 double current_update_time, last_update_time;
1561 static void generate_text()
1564 struct information *cur = &info;
1571 current_update_time = get_time();
1577 n = TEXT_BUFFER_SIZE * 4 - 2;
1580 for (i = 0; i < text_object_count; i++) {
1581 struct text_object *obj = &text_objects[i];
1583 #define OBJ(a) break; case OBJ_##a:
1585 switch (obj->type) {
1588 ERR("not implemented obj type %d",
1592 /* does anyone have decimals in acpi temperature? */
1594 snprintf(p, n, "%d", (int)
1595 get_acpi_temperature(obj->
1599 snprintf(p, 5, "%d ", (int)
1600 get_acpi_temperature(obj->
1605 /* does anyone have decimals in acpi temperature? */
1607 snprintf(p, n, "%d", (int)
1608 ((get_acpi_temperature(obj->
1610 i)+ 40) * 9.0 / 5 - 40));
1612 snprintf(p, 5, "%d ", (int)
1613 ((get_acpi_temperature(obj->
1615 i)+ 40) * 9.0 / 5 - 40));
1618 snprintf(p, n, "%.0fMhz", get_freq());
1621 float ghz = (float)(get_freq()/1000);
1622 //printf("%f\n", ghz);
1623 snprintf(p, n, "%'.2fGhz", ghz);
1626 snprintf(p, n, "%s", get_adt746x_cpu());
1629 snprintf(p, n, "%s", get_adt746x_fan());
1632 snprintf(p, n, "%s", get_acpi_fan());
1634 OBJ(acpiacadapter) {
1635 snprintf(p, n, "%s",
1636 get_acpi_ac_adapter());
1639 get_battery_stuff(p, n, obj->data.s);
1642 human_readable(cur->buffers * 1024, p,
1646 human_readable(cur->cached * 1024, p, 255);
1650 snprintf(p, n, "%*d", pad_percents,
1651 (int) (cur->cpu_usage *
1654 snprintf(p, 4, "%*d ",
1656 (int) (cur->cpu_usage *
1660 new_bar(p, obj->data.pair.a,
1662 (int) (cur->cpu_usage * 255.0));
1665 new_graph(p, obj->a,
1666 obj->b, obj->c, obj->d,
1667 (unsigned int) (cur->cpu_usage *
1671 new_fg(p, obj->data.l);
1675 new_font(p, obj->data.s);
1680 snprintf(p, n, "%d",
1681 (int) (obj->data.net->
1685 snprintf(p, 6, "%d ",
1686 (int) (obj->data.net->
1692 snprintf(p, n, "%.1f",
1694 recv_speed / 1024.0);
1696 snprintf(p, 8, "%.1f ",
1698 recv_speed / 1024.0);
1700 OBJ(downspeedgraph) {
1701 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
1702 obj->data.net->recv_speed = 0.01;
1703 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1704 (obj->data.net->recv_speed /
1711 i = obj->data.ifblock.pos - 2;
1721 snprintf(p, n, "%u.%u.%u.%u",
1722 obj->data.net->addr.
1724 obj->data.net->addr.
1726 obj->data.net->addr.
1728 obj->data.net->addr.
1733 snprintf(p, n, "%d",
1734 obj->data.net->linkstatus);
1739 FILE *fp = popen(obj->data.s, "r");
1740 int n2 = fread(p, 1, n, fp);
1744 if (n2 && p[n2 - 1] == '\n')
1755 FILE *fp = popen(obj->data.s, "r");
1756 int n2 = fread(p, 1, n, fp);
1760 if (n2 && p[n2 - 1] == '\n')
1769 if (sscanf(p, "%lf", &barnum) == 0) {
1770 ERR("reading execbar value failed (perhaps it's not the correct format?)");
1772 if (barnum > 100 || barnum < 0) {
1773 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
1775 barnum = barnum / 100.0;
1776 new_bar(p, 0, 4, (int) (barnum * 255.0));
1782 FILE *fp = popen(obj->data.s, "r");
1783 int n2 = fread(p, 1, n, fp);
1787 if (n2 && p[n2 - 1] == '\n')
1796 if (sscanf(p, "%lf", &barnum) == 0) {
1797 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
1799 if (barnum > 100 || barnum < 0) {
1800 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
1803 25, obj->c, obj->d, (int) (barnum), 0, 1);
1808 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
1809 new_bar(p, 0, 4, (int) obj->data.execi.data);
1812 FILE *fp = popen(obj->data.s, "r");
1813 int n2 = fread(p, 1, n, fp);
1816 if (n2 && p[n2 - 1] == '\n')
1825 if (sscanf(p, "%lf", &barnum) == 0) {
1826 ERR("reading execibar value failed (perhaps it's not the correct format?)");
1828 if (barnum > 100 || barnum < 0) {
1829 ERR("your execibar value is not between 0 and 100, therefore it will be ignored");
1831 obj->data.execi.data = 255 * barnum / 100.0;
1832 new_bar(p, 0, 4, (int) obj->data.execi.data);
1834 obj->data.execi.last_update =
1835 current_update_time;
1839 if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
1840 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->data.execi.data), 0, 0);
1843 FILE *fp = popen(obj->data.s, "r");
1844 int n2 = fread(p, 1, n, fp);
1847 if (n2 && p[n2 - 1] == '\n')
1856 if (sscanf(p, "%lf", &barnum) == 0) {
1857 ERR("reading execigraph value failed (perhaps it's not the correct format?)");
1859 if (barnum > 100 || barnum < 0) {
1860 ERR("your execigraph value is not between 0 and 100, therefore it will be ignored");
1862 obj->data.execi.data = barnum;
1863 new_graph(p, 0, 25, obj->c, obj->d, (int) (obj->data.execi.data), 0, 1);
1865 obj->data.execi.last_update = current_update_time;
1871 if (current_update_time -
1872 obj->data.execi.last_update <
1873 obj->data.execi.interval) {
1874 snprintf(p, n, "%s",
1875 obj->data.execi.buffer);
1877 char *p2 = obj->data.execi.buffer;
1879 popen(obj->data.execi.cmd,
1882 fread(p2, 1, TEXT_BUFFER_SIZE,
1887 if (n2 && p2[n2 - 1] == '\n')
1896 snprintf(p, n, "%s",
1897 obj->data.execi.buffer);
1899 obj->data.execi.last_update =
1900 current_update_time;
1905 if (obj->data.fs != NULL) {
1906 if (obj->data.fs->size == 0)
1925 if (obj->data.fs != NULL)
1926 human_readable(obj->data.fs->avail,
1930 if (obj->data.fs != NULL) {
1931 if (obj->data.fs->size)
1932 snprintf(p, n, "%*d",
1941 snprintf(p, n, "0");
1945 if (obj->data.fs != NULL)
1946 human_readable(obj->data.fs->size,
1950 if (obj->data.fs != NULL)
1951 human_readable(obj->data.fs->size -
1952 obj->data.fs->avail,
1956 if (obj->data.fs != NULL) {
1957 if (obj->data.fs->size == 0)
1975 if (obj->data.fs != NULL) {
1976 if (obj->data.fs->size)
1977 snprintf(p, 4, "%d",
1987 snprintf(p, n, "0");
1991 float *v = info.loadavg;
1993 if (obj->data.loadavg[2])
1994 snprintf(p, n, "%.2f %.2f %.2f",
1995 v[obj->data.loadavg[0] -
1997 v[obj->data.loadavg[1] -
1999 v[obj->data.loadavg[2] -
2001 else if (obj->data.loadavg[1])
2002 snprintf(p, n, "%.2f %.2f",
2003 v[obj->data.loadavg[0] -
2005 v[obj->data.loadavg[1] -
2007 else if (obj->data.loadavg[0])
2008 snprintf(p, n, "%.2f",
2009 v[obj->data.loadavg[0] -
2013 new_hr(p, obj->data.i);
2016 new_offset(p, obj->data.i);
2019 new_voffset(p, obj->data.i);
2024 r = get_i2c_info(&obj->data.i2c.fd,
2026 obj->data.i2c.devtype,
2027 obj->data.i2c.type);
2029 if (r >= 100.0 || r == 0)
2030 snprintf(p, n, "%d", (int) r);
2032 snprintf(p, n, "%.1f", r);
2035 new_alignr(p, obj->data.i);
2038 new_alignc(p, obj->data.i);
2042 if ((obj->data.ifblock.s)
2043 && (stat(obj->data.ifblock.s, &tmp) ==
2045 i = obj->data.ifblock.pos - 2;
2051 if ((obj->data.ifblock.s)
2052 && (!check_mount(obj->data.ifblock.s))) {
2053 i = obj->data.ifblock.pos - 2;
2059 if ((obj->data.ifblock.s)
2060 && system(obj->data.ifblock.s)) {
2061 i = obj->data.ifblock.pos - 2;
2067 snprintf(p, n, "%s", cur->uname_s.release);
2070 snprintf(p, n, "%s", cur->uname_s.machine);
2075 human_readable(cur->mem * 1024, p, 6);
2078 human_readable(cur->memmax * 1024, p, 255);
2083 snprintf(p, n, "%*d",
2088 snprintf(p, 4, "%*d ",
2095 new_bar(p, obj->data.pair.a,
2097 cur->memmax ? (cur->mem * 255) /
2102 new_graph(p, obj->a,
2103 obj->b, obj->c, obj->d,
2104 cur->memmax ? (cur->mem * 100.0) /
2105 (cur->memmax) : 0.0, 0, 1);
2109 snprintf(p, n, "%d",
2110 mixer_get_avg(obj->data.l));
2113 snprintf(p, n, "%d",
2114 mixer_get_left(obj->data.l));
2117 snprintf(p, n, "%d",
2118 mixer_get_right(obj->data.l));
2121 new_bar(p, obj->data.mixerbar.w,
2122 obj->data.mixerbar.h,
2123 mixer_get_avg(obj->data.mixerbar.
2127 new_bar(p, obj->data.mixerbar.w,
2128 obj->data.mixerbar.h,
2129 mixer_get_left(obj->data.mixerbar.
2133 new_bar(p, obj->data.mixerbar.w,
2134 obj->data.mixerbar.h,
2135 mixer_get_right(obj->data.mixerbar.
2141 snprintf(p, n, "%d", cur->mail_count);
2144 snprintf(p, n, "%d", cur->new_mail_count);
2147 OBJ(ml_upload_counter) {
2148 snprintf(p, n, "%lld",
2149 mlinfo.upload_counter / 1048576);
2151 OBJ(ml_download_counter) {
2152 snprintf(p, n, "%lld",
2153 mlinfo.download_counter /
2156 OBJ(ml_nshared_files) {
2157 snprintf(p, n, "%i", mlinfo.nshared_files);
2159 OBJ(ml_shared_counter) {
2160 snprintf(p, n, "%lld",
2161 mlinfo.shared_counter / 1048576);
2163 OBJ(ml_tcp_upload_rate) {
2164 snprintf(p, n, "%.2f",
2165 (float) mlinfo.tcp_upload_rate /
2168 OBJ(ml_tcp_download_rate) {
2169 snprintf(p, n, "%.2f",
2170 (float) mlinfo.tcp_download_rate /
2173 OBJ(ml_udp_upload_rate) {
2174 snprintf(p, n, "%.2f",
2175 (float) mlinfo.udp_upload_rate /
2178 OBJ(ml_udp_download_rate) {
2179 snprintf(p, n, "%.2f",
2180 (float) mlinfo.udp_download_rate /
2183 OBJ(ml_ndownloaded_files) {
2184 snprintf(p, n, "%i",
2185 mlinfo.ndownloaded_files);
2187 OBJ(ml_ndownloading_files) {
2188 snprintf(p, n, "%i",
2189 mlinfo.ndownloading_files);
2194 snprintf(p, n, "%s",
2195 cur->uname_s.nodename);
2198 new_outline(p, obj->data.l);
2202 snprintf(p, n, "%d", cur->procs);
2204 snprintf(p, 5, "%d ",
2207 OBJ(running_processes) {
2209 snprintf(p, n, "%d",
2212 snprintf(p, 3, "%d ",
2216 snprintf(p, n, "%s", obj->data.s);
2219 new_bg(p, obj->data.l);
2222 new_stippled_hr(p, obj->data.pair.a,
2226 human_readable(cur->swap * 1024, p, 255);
2229 human_readable(cur->swapmax * 1024, p,
2233 if (cur->swapmax == 0) {
2234 strncpy(p, "No swap", 255);
2237 snprintf(p, 255, "%*u",
2243 snprintf(p, 4, "%*u ",
2251 new_bar(p, obj->data.pair.a,
2253 cur->swapmax ? (cur->swap * 255) /
2254 (cur->swapmax) : 0);
2257 snprintf(p, n, "%s", cur->uname_s.sysname);
2260 time_t t = time(NULL);
2261 struct tm *tm = localtime(&t);
2262 setlocale(LC_TIME, "");
2263 strftime(p, n, obj->data.s, tm);
2266 time_t t = time(NULL);
2267 struct tm *tm = gmtime(&t);
2268 strftime(p, n, obj->data.s, tm);
2271 human_readable(obj->data.net->recv, p,
2275 human_readable(obj->data.net->trans, p,
2279 snprintf(p, n, "%d", total_updates);
2283 snprintf(p, n, "%d",
2284 (int) (obj->data.net->
2288 snprintf(p, 5, "%d ",
2289 (int) (obj->data.net->
2295 snprintf(p, n, "%.1f",
2297 trans_speed / 1024.0);
2299 snprintf(p, 8, "%.1f ",
2301 trans_speed / 1024.0);
2304 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
2305 obj->data.net->trans_speed = 0.01;
2306 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2307 (obj->data.net->trans_speed /
2311 format_seconds_short(p, n,
2315 format_seconds(p, n, (int) cur->uptime);
2320 snprintf(p, n, "%.2f",
2321 cur->seti_prog * 100.0f);
2324 new_bar(p, obj->data.pair.a,
2326 (int) (cur->seti_prog * 255.0f));
2329 snprintf(p, n, "%.0f", cur->seti_credit);
2335 snprintf(p, n, "%s", cur->mpd.title);
2338 snprintf(p, n, "%s", cur->mpd.artist);
2341 snprintf(p, n, "%s", cur->mpd.album);
2344 snprintf(p, n, "%i", cur->mpd.volume);
2347 snprintf(p, n, "%i", cur->mpd.bitrate);
2350 snprintf(p, n, "%s", cur->mpd.status);
2353 int days = 0, hours = 0, minutes =
2355 int tmp = cur->mpd.elapsed;
2356 while (tmp >= 86400) {
2360 while (tmp >= 3600) {
2370 snprintf(p, n, "%i days %i:%i:%2i",
2371 days, hours, minutes,
2374 snprintf(p, n, "%i:%i:%02i", hours,
2377 snprintf(p, n, "%i:%02i", minutes,
2381 int days = 0, hours = 0, minutes =
2383 int tmp = cur->mpd.length;
2384 while (tmp >= 86400) {
2388 while (tmp >= 3600) {
2399 "%i days %i:%i:%02i",
2400 days, hours, minutes,
2403 snprintf(p, n, "%i:%i:%02i", hours,
2406 snprintf(p, n, "%i:%02i", minutes,
2410 snprintf(p, n, "%2.0f",
2411 cur->mpd.progress * 100);
2414 new_bar(p, obj->data.pair.a,
2416 (int) (cur->mpd.progress *
2421 if (obj->data.top.type == TOP_NAME
2422 && obj->data.top.num >= 0
2423 && obj->data.top.num < 10) {
2424 // if we limit the buffer and add a bunch of space after, it stops the thing from
2425 // moving other shit around, which is really fucking annoying
2426 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
2427 } else if (obj->data.top.type == TOP_CPU
2428 && obj->data.top.num >= 0
2429 && obj->data.top.num < 10) {
2430 snprintf(p, 7, "%3.2f ",
2431 cur->cpu[obj->data.top.
2433 } else if (obj->data.top.type == TOP_PID
2434 && obj->data.top.num >= 0
2435 && obj->data.top.num < 10) {
2436 snprintf(p, 8, "%i ",
2437 cur->cpu[obj->data.top.
2439 } else if (obj->data.top.type == TOP_MEM
2440 && obj->data.top.num >= 0
2441 && obj->data.top.num < 10) {
2442 snprintf(p, 7, "%3.2f ",
2443 cur->cpu[obj->data.top.
2448 if (obj->data.top.type == TOP_NAME
2449 && obj->data.top.num >= 0
2450 && obj->data.top.num < 10) {
2451 // if we limit the buffer and add a bunch of space after, it stops the thing from
2452 // moving other shit around, which is really fucking annoying
2455 cur->memu[obj->data.top.
2457 } else if (obj->data.top.type == TOP_CPU
2458 && obj->data.top.num >= 0
2459 && obj->data.top.num < 10) {
2460 snprintf(p, 7, "%3.2f ",
2461 cur->memu[obj->data.top.
2463 } else if (obj->data.top.type == TOP_PID
2464 && obj->data.top.num >= 0
2465 && obj->data.top.num < 10) {
2466 snprintf(p, 8, "%i ",
2467 cur->memu[obj->data.top.
2469 } else if (obj->data.top.type == TOP_MEM
2470 && obj->data.top.num >= 0
2471 && obj->data.top.num < 10) {
2472 snprintf(p, 7, "%3.2f ",
2473 cur->memu[obj->data.top.
2481 * I'm tired of everything being packed in
2488 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
2489 snprintf(p, n, "%s", obj->data.tail.buffer);
2491 obj->data.tail.last_update = current_update_time;
2495 tailstring *head = NULL;
2496 tailstring *headtmp = NULL;
2497 fp = fopen(obj->data.tail.logfile, "rt");
2499 ERR("tail logfile failed to open");
2502 obj->data.tail.readlines = 0;
2504 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL) {
2511 addtail(&head, obj->data.tail.buffer);
2512 obj->data.tail.readlines++;
2517 if (obj->data.tail.readlines > 0) {
2518 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
2519 addtail(&headtmp, head->data);
2522 strcpy(obj->data.tail.buffer, headtmp->data);
2523 headtmp = headtmp->next;
2524 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
2526 strncat(obj->data.tail.buffer, headtmp->data, (TEXT_BUFFER_SIZE * 6 / obj->data.tail.wantedlines) - strlen(obj->data.tail.buffer)); /* without strlen() at the end this becomes a possible */
2527 headtmp = headtmp->next;
2531 /* get rid of any ugly newlines at the end */
2532 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
2533 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
2535 snprintf(p, n, "%s", obj->data.tail.buffer);
2540 strcpy(obj->data.tail.buffer, "Logfile Empty");
2541 snprintf(p, n, "Logfile Empty");
2554 unsigned int a = strlen(p);
2560 if (stuff_in_upper_case) {
2570 last_update_time = current_update_time;
2576 static void set_font()
2580 if (window.xftdraw != NULL)
2581 XftDrawDestroy(window.xftdraw);
2582 window.xftdraw = XftDrawCreate(display, window.drawable,
2583 DefaultVisual(display,
2585 DefaultColormap(display,
2590 XSetFont(display, window.gc, fonts[selected_font].font->fid);
2599 static int text_start_x, text_start_y; /* text start position in window */
2600 static int text_width, text_height;
2604 static inline int get_string_width(const char *s)
2607 return *s ? calc_text_width(s, strlen(s)) : 0;
2616 static void text_size_updater(char *s)
2620 int h = font_height();
2621 /* get string widths and skip specials */
2624 if (*p == SPECIAL_CHAR) {
2626 w += get_string_width(s);
2629 if (specials[special_index].type == BAR
2630 || specials[special_index].type == GRAPH) {
2631 w += specials[special_index].width;
2632 if (specials[special_index].height > h) {
2633 h = specials[special_index].height;
2638 else if (specials[special_index].type == OFFSET) {
2639 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
2641 else if (specials[special_index].type == VOFFSET) {
2642 h += specials[special_index].arg;
2644 else if (specials[special_index].type == FONT) {
2645 fontchange = specials[special_index].font_added;
2646 selected_font = specials[special_index].font_added;
2656 w += get_string_width(s);
2659 if (text_width > maximum_width && maximum_width)
2660 text_width = maximum_width;
2671 static void update_text_area()
2675 /* update text size if it isn't fixed */
2680 text_width = minimum_width;
2683 for_each_line(text_buffer, text_size_updater);
2685 if (text_height < minimum_height)
2686 text_height = minimum_height;
2687 if (text_width > maximum_width && maximum_width > 0)
2688 text_width = maximum_width;
2691 /* get text position on workarea */
2692 switch (text_alignment) {
2699 x = workarea[2] - text_width - gap_x;
2706 y = workarea[3] - text_height - gap_y;
2710 x = workarea[2] - text_width - gap_x;
2711 y = workarea[3] - text_height - gap_y;
2715 case NONE: // Let the WM manage the window
2726 if (own_window && !fixed_pos) {
2729 text_start_x = border_margin + 1;
2730 text_start_y = border_margin + 1;
2731 window.x = x - border_margin - 1;
2732 window.y = y - border_margin - 1;
2736 /* If window size doesn't match to workarea's size, then window
2737 * probably includes panels (gnome).
2738 * Blah, doesn't work on KDE. */
2739 if (workarea[2] != window.width
2740 || workarea[3] != window.height) {
2754 static int cur_x, cur_y; /* current x and y for drawing */
2755 static int draw_mode; /* FG, BG or OUTLINE */
2756 static long current_color;
2758 static inline void set_foreground_color(long c)
2761 XSetForeground(display, window.gc, c);
2765 static void draw_string(const char *s)
2769 int i, i2, pos, width_of_s;
2772 width_of_s = get_string_width(s);
2773 if (out_to_console) {
2776 /* 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 */
2777 strcpy(tmpstring1, s);
2781 snprintf(space, 2, " ");
2783 max = ((text_width - width_of_s) / get_string_width(space));
2786 * This code looks for tabs in the text and coverts them to spaces.
2787 * The trick is getting the correct number of spaces,
2788 * and not going over the window's size without forcing
2789 * the window larger.
2791 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
2792 if (tmpstring1[i] == '\t') // 9 is ascii tab
2796 i2 < (8 - (1 + pos) % 8) && added <= max;
2798 tmpstring2[pos + i2] = ' ';
2803 if (tmpstring1[i] != 9) {
2804 tmpstring2[pos] = tmpstring1[i];
2809 if (text_width == maximum_width) {
2810 /* this means the text is probably pushing the limit, so we'll chop it */
2811 while (cur_x + get_string_width(tmpstring2) - text_start_x > maximum_width && strlen(tmpstring2) > 0) {
2812 tmpstring2[strlen(tmpstring2)-1] = '\0';
2821 c.pixel = current_color;
2822 XQueryColor(display, DefaultColormap(display, screen), &c);
2825 c2.color.red = c.red;
2826 c2.color.green = c.green;
2827 c2.color.blue = c.blue;
2828 c2.color.alpha = fonts[selected_font].font_alpha;
2830 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2831 cur_x, cur_y, (XftChar8 *) s,
2834 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2835 cur_x, cur_y, (XftChar8 *) s,
2841 XDrawString(display, window.drawable, window.gc,
2842 cur_x, cur_y, s, strlen(s));
2844 cur_x += width_of_s;
2846 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
2849 long redmask, greenmask, bluemask;
2850 short colour_depth = 0;
2852 void set_up_gradient()
2855 colour_depth = DisplayPlanes(display, screen);
2859 if (colour_depth != 24 && colour_depth != 16) {
2860 ERR("using non-standard colour depth, gradients may look like a lolly-pop");
2866 for(i = (colour_depth / 3)-1; i>=0; i--) {
2868 greenmask |= 1 << i;
2871 if (colour_depth%3 == 1) {
2872 greenmask |= 1 << (colour_depth / 3);
2874 redmask = redmask << (2*colour_depth / 3 + colour_depth%3);
2875 greenmask = greenmask << (colour_depth / 3);
2878 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 */
2880 int red1, green1, blue1; // first colour
2881 int red2, green2, blue2; // second colour
2882 int red3 = 0, green3 = 0, blue3 = 0; // difference
2883 short redshift = (2*colour_depth / 3 + colour_depth%3);
2884 short greenshift = (colour_depth / 3);
2885 red1 = (first_colour & redmask) >> redshift;
2886 green1 = (first_colour & greenmask) >> greenshift;
2887 blue1 = first_colour & bluemask;
2888 red2 = (last_colour & redmask) >> redshift;
2889 green2 = (last_colour & greenmask) >> greenshift;
2890 blue2 = last_colour & bluemask;
2897 if (green1 > green2) {
2900 if (green1 < green2) {
2903 if (blue1 > blue2) {
2906 if (blue1 < blue2) {
2921 if (red1 > bluemask) {
2924 if (green1 > bluemask) {
2927 if (blue1 > bluemask) {
2930 tmp_color = (red1 << redshift) | (green1 << greenshift) | blue1;
2934 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
2935 if (colour_depth == 0) {
2938 int red1, green1, blue1; // first colour
2939 int red2, green2, blue2; // second colour
2940 long redshift = (2*colour_depth / 3 + colour_depth%3);
2941 long greenshift = (colour_depth / 3);
2942 int red3 = 0, green3 = 0, blue3 = 0; // difference
2943 red1 = (first_colour & redmask) >> redshift;
2944 green1 = (first_colour & greenmask) >> greenshift;
2945 blue1 = first_colour & bluemask;
2946 red2 = (last_colour & redmask) >> redshift;
2947 green2 = (last_colour & greenmask) >> greenshift;
2948 blue2 = last_colour & bluemask;
2949 red3 = abs(red1 - red2);
2950 green3 = abs(green1 - green2);
2951 blue3 = abs(blue1 - blue2);
2960 static void draw_line(char *s)
2964 cur_x = text_start_x;
2965 cur_y += font_ascent();
2967 short font_h = font_height();
2969 /* find specials and draw stuff */
2972 if (*p == SPECIAL_CHAR) {
2975 /* draw string before special */
2982 switch (specials[special_index].type) {
2983 case HORIZONTAL_LINE:
2986 specials[special_index].height;
2987 int mid = font_ascent() / 2;
2988 w = text_start_x + text_width -
2991 XSetLineAttributes(display,
2996 XDrawLine(display, window.drawable,
3007 specials[special_index].height;
3009 specials[special_index].arg;
3010 int mid = font_ascent() / 2;
3011 char ss[2] = { s, s };
3012 w = text_start_x + text_width -
3015 XSetLineAttributes(display,
3020 XSetDashes(display, window.gc, 0,
3022 XDrawLine(display, window.drawable,
3032 if (cur_x > maximum_width - text_start_x && maximum_width > 0) {
3036 specials[special_index].height;
3038 specials[special_index].arg;
3040 cur_y - (font_ascent() +
3042 w = specials[special_index].width;
3045 text_width - cur_x - 1;
3049 XSetLineAttributes(display,
3055 XDrawRectangle(display,
3059 XFillRectangle(display,
3063 w * bar_usage / 255,
3065 if (specials[special_index].
3067 && specials[special_index].
3071 [special_index].height;
3078 if (cur_x > maximum_width - text_start_x && maximum_width > 0) {
3082 specials[special_index].height;
3086 by = cur_y - (font_ascent() +
3091 by = cur_y - (font_ascent()/2);
3093 w = specials[special_index].width;
3095 w = text_start_x + text_width - cur_x - 1;
3098 XSetLineAttributes(display,
3103 XDrawRectangle(display,
3107 XSetLineAttributes(display,
3114 int gradient_size = 0;
3115 float gradient_factor = 0;
3116 float gradient_update = 0;
3117 unsigned long tmpcolour = current_color;
3118 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3119 tmpcolour = specials[special_index].first_colour;
3120 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3121 gradient_factor = (float)gradient_size / (w - 3);
3123 for (i = 0; i < w - 3; i++) {
3124 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3125 XSetForeground(display, window.gc, tmpcolour);
3126 gradient_update += gradient_factor;
3127 while (gradient_update > 0) {
3128 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3132 if (i / ((float) (w - 3) / (specials[special_index].graph_width)) > j) {
3135 XDrawLine(display, window.drawable, window.gc, cur_x + i + 2, by + h, cur_x + i + 2, by + h - specials[special_index].graph[j] * (h - 1) / specials[special_index].graph_scale); /* this is mugfugly, but it works */
3137 if (specials[special_index].
3139 && specials[special_index].
3143 [special_index].height;
3146 if (draw_mode == BG) {
3147 set_foreground_color(default_bg_color);
3149 else if (draw_mode == OUTLINE) {
3150 set_foreground_color(default_out_color);
3152 set_foreground_color(default_fg_color);
3158 cur_y -= font_ascent();
3159 selected_font = specials[special_index].font_added;
3160 cur_y += font_ascent();
3170 if (draw_mode == FG)
3171 set_foreground_color(specials
3177 if (draw_mode == BG)
3178 set_foreground_color(specials
3184 if (draw_mode == OUTLINE)
3185 set_foreground_color(specials
3192 w += specials[special_index].arg;
3197 cur_y += specials[special_index].arg;
3203 int pos_x = text_width + gap_x - get_string_width(p) /*- border_margin*2 - 1*/;
3204 /*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(p), gap_x, specials[special_index].arg, border_margin, border_width);*/
3205 if (pos_x > specials[special_index].arg && pos_x > cur_x) {
3206 cur_x = pos_x - specials[special_index].arg;
3213 int pos_x = (text_width)/2 - get_string_width(p)/2 - (cur_x - text_start_x);
3214 /*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(p), gap_x, specials[special_index].arg);*/
3216 specials[special_index].arg)
3219 [special_index].arg;
3236 if (cur_y_add > 0) {
3238 cur_y -= font_descent();
3243 cur_y += font_descent();
3250 static void draw_text()
3253 cur_y = text_start_y;
3256 if (draw_borders && border_width > 0) {
3257 unsigned int b = (border_width + 1) / 2;
3259 if (stippled_borders) {
3261 { stippled_borders, stippled_borders };
3262 XSetLineAttributes(display, window.gc,
3263 border_width, LineOnOffDash,
3264 CapButt, JoinMiter);
3265 XSetDashes(display, window.gc, 0, ss, 2);
3267 XSetLineAttributes(display, window.gc,
3268 border_width, LineSolid,
3269 CapButt, JoinMiter);
3272 XDrawRectangle(display, window.drawable, window.gc,
3273 text_start_x - border_margin + b,
3274 text_start_y - border_margin + b,
3275 text_width + border_margin * 2 - 1 - b * 2,
3276 text_height + border_margin * 2 - 1 -
3283 for_each_line(text_buffer, draw_line);
3286 static void draw_stuff()
3289 if (draw_shades && !draw_outline) {
3292 set_foreground_color(default_bg_color);
3301 for (i = -1; i < 2; i++)
3302 for (j = -1; j < 2; j++) {
3303 if (i == 0 && j == 0)
3307 set_foreground_color(default_out_color);
3308 draw_mode = OUTLINE;
3315 set_foreground_color(default_fg_color);
3323 swap.swap_window = window.window;
3324 swap.swap_action = XdbeBackground;
3325 XdbeSwapBuffers(display, &swap, 1);
3331 static void clear_text(int exposures)
3335 return; /* The swap action is XdbeBackground, which clears */
3337 /* there is some extra space for borders and outlines */
3338 XClearArea(display, window.drawable,
3339 text_start_x - border_margin - 1,
3340 text_start_y - border_margin - 1,
3341 text_width + border_margin * 2 + 2,
3342 text_height + border_margin * 2 + 2,
3343 exposures ? True : 0);
3347 static int need_to_update;
3349 /* update_text() generates new text and clears old text area */
3350 static void update_text()
3359 static void main_loop()
3362 Region region = XCreateRegion();
3366 while (total_run_times == 0 || info.looped < total_run_times - 1) {
3371 /* wait for X event or timeout */
3373 if (!XPending(display)) {
3378 update_interval - (get_time() -
3384 tv.tv_sec = (long) t;
3385 tv.tv_usec = (long) (t * 1000000) % 1000000;
3387 FD_SET(ConnectionNumber(display), &fdsr);
3390 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
3393 usleep(update_interval*1000000); /* FIXME just sleep for the interval time if no X11 */
3398 ERR("can't select(): %s",
3409 if (need_to_update) {
3411 int wx = window.x, wy = window.y;
3420 /* resize window if it isn't right size */
3422 (text_width + border_margin * 2 !=
3424 || text_height + border_margin * 2 !=
3428 border_margin * 2 + 1;
3431 border_margin * 2 + 1;
3432 XResizeWindow(display,
3438 /* move window if it isn't in right position */
3441 || window.y != wy)) {
3442 XMoveWindow(display, window.window,
3443 window.x, window.y);
3453 r.x = text_start_x - border_margin;
3454 r.y = text_start_y - border_margin;
3455 r.width = text_width + border_margin * 2;
3456 r.height = text_height + border_margin * 2;
3457 XUnionRectWithRegion(&r, region, region);
3462 /* handle X events */
3464 while (XPending(display)) {
3466 XNextEvent(display, &ev);
3474 r.width = ev.xexpose.width;
3475 r.height = ev.xexpose.height;
3476 XUnionRectWithRegion(&r, region,
3482 case ReparentNotify:
3483 /* set background to ParentRelative for all parents */
3485 set_transparent_background(window.
3489 case ConfigureNotify:
3491 /* if window size isn't what expected, set fixed size */
3492 if (ev.xconfigure.width !=
3494 || ev.xconfigure.height !=
3496 if (window.width != 0
3497 && window.height != 0)
3500 /* clear old stuff before screwing up size and pos */
3506 if (XGetWindowAttributes(display, window.window, &attrs)) {
3521 border_margin * 2 - 1;
3524 border_margin * 2 - 1;
3525 if (text_width > maximum_width && maximum_width > 0)
3526 text_width = maximum_width;
3529 /* if position isn't what expected, set fixed pos, total_updates
3530 * avoids setting fixed_pos when window is set to weird locations
3532 if (total_updates >= 2
3534 && (window.x != ev.xconfigure.x
3537 && (ev.xconfigure.x != 0
3538 || ev.xconfigure.y != 0)) {
3550 /* XDBE doesn't seem to provide a way to clear the back buffer without
3551 * interfering with the front buffer, other than passing XdbeBackground
3552 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
3553 * redraw the text even if it wasn't part of the exposed area. OTOH,
3554 * if we're not going to call draw_stuff at all, then no swap happens
3555 * and we can safely do nothing.
3558 if (!XEmptyRegion(region)) {
3562 r.x = text_start_x - border_margin;
3563 r.y = text_start_y - border_margin;
3564 r.width = text_width + border_margin * 2;
3565 r.height = text_height + border_margin * 2;
3566 XUnionRectWithRegion(&r, region, region);
3569 XSetRegion(display, window.gc, region);
3572 XftDrawSetClip(window.xftdraw, region);
3577 XDestroyRegion(region);
3578 region = XCreateRegion();
3585 static void load_config_file(const char *);
3587 /* signal handler that reloads config file */
3588 static void reload_handler(int a)
3590 ERR("Conky: received signal %d, reloading config\n", a);
3592 if (current_config) {
3594 load_config_file(current_config);
3599 extract_variable_text(text);
3606 static void clean_up()
3611 XdbeDeallocateBackBufferName(display, window.back_buffer);
3615 XDestroyWindow(display, window.window);
3619 XClearWindow(display, RootWindow(display, screen));
3624 XFreeGC(display, window.gc);
3628 /* it is really pointless to free() memory at the end of program but ak|ra
3629 * wants me to do this */
3631 free_text_objects();
3633 if (text != original_text)
3636 free(current_config);
3637 free(current_mail_spool);
3643 static void term_handler(int a)
3645 a = a; /* to get rid of warning */
3650 static int string_to_bool(const char *s)
3654 if (strcasecmp(s, "yes") == 0)
3656 if (strcasecmp(s, "true") == 0)
3658 if (strcasecmp(s, "1") == 0)
3663 static enum alignment string_to_alignment(const char *s)
3665 if (strcasecmp(s, "top_left") == 0)
3667 else if (strcasecmp(s, "top_right") == 0)
3669 else if (strcasecmp(s, "bottom_left") == 0)
3671 else if (strcasecmp(s, "bottom_right") == 0)
3672 return BOTTOM_RIGHT;
3673 else if (strcasecmp(s, "tl") == 0)
3675 else if (strcasecmp(s, "tr") == 0)
3677 else if (strcasecmp(s, "bl") == 0)
3679 else if (strcasecmp(s, "br") == 0)
3680 return BOTTOM_RIGHT;
3681 else if (strcasecmp(s, "none") == 0)
3688 static void set_default_configurations(void)
3690 fork_to_background = 0;
3691 total_run_times = 0;
3692 info.cpu_avg_samples = 2;
3693 info.net_avg_samples = 2;
3698 strcpy(info.mpd.host, "localhost");
3699 info.mpd.port = 6600;
3700 info.mpd.status = "Checking status...";
3709 default_fg_color = WhitePixel(display, screen);
3710 default_bg_color = BlackPixel(display, screen);
3711 default_out_color = BlackPixel(display, screen);
3715 set_first_font("6x10");
3724 stippled_borders = 0;
3727 text_alignment = BOTTOM_LEFT;
3731 free(current_mail_spool);
3734 variable_substitute(MAIL_FILE, buf, 256);
3736 current_mail_spool = strdup(buf);
3740 update_interval = 10.0;
3741 stuff_in_upper_case = 0;
3743 mlconfig.mldonkey_hostname = "127.0.0.1";
3744 mlconfig.mldonkey_port = 4001;
3745 mlconfig.mldonkey_login = NULL;
3746 mlconfig.mldonkey_password = NULL;
3750 static void load_config_file(const char *f)
3752 #define CONF_ERR ERR("%s: %d: config file error", f, line)
3756 set_default_configurations();
3758 fp = open_file(f, 0);
3763 char buf[256], *p, *p2, *name, *value;
3765 if (fgets(buf, 256, fp) == NULL)
3770 /* break at comment */
3771 p2 = strchr(p, '#');
3776 while (*p && isspace((int) *p))
3779 continue; /* empty line */
3785 while (*p2 && !isspace((int) *p2))
3788 *p2 = '\0'; /* break at name's end */
3795 while (*p && isspace((int) *p))
3800 p2 = value + strlen(value);
3801 while (isspace((int) *(p2 - 1)))
3807 #define CONF2(a) if (strcasecmp(name, a) == 0)
3808 #define CONF(a) else CONF2(a)
3809 #define CONF3(a,b) \
3810 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
3814 CONF2("alignment") {
3816 int a = string_to_alignment(value);
3826 on_bottom = string_to_bool(value);
3830 CONF("background") {
3831 fork_to_background = string_to_bool(value);
3835 CONF2("background") {
3836 fork_to_background = string_to_bool(value);
3840 CONF("border_margin") {
3842 border_margin = strtol(value, 0, 0);
3846 CONF("border_width") {
3848 border_width = strtol(value, 0, 0);
3852 CONF("default_color") {
3854 default_fg_color = get_x11_color(value);
3858 CONF3("default_shade_color", "default_shadecolor") {
3860 default_bg_color = get_x11_color(value);
3864 CONF3("default_outline_color", "default_outlinecolor") {
3866 default_out_color = get_x11_color(value);
3874 strcpy(info.mpd.host, value);
3880 info.mpd.port = strtol(value, 0, 0);
3881 if (info.mpd.port < 1
3882 || info.mpd.port > 0xffff)
3887 CONF("cpu_avg_samples") {
3889 cpu_avg_samples = strtol(value, 0, 0);
3890 if (cpu_avg_samples < 1
3891 || cpu_avg_samples > 14)
3900 CONF("net_avg_samples") {
3902 net_avg_samples = strtol(value, 0, 0);
3903 if (net_avg_samples < 1
3904 || net_avg_samples > 14)
3920 CONF("double_buffer") {
3922 use_xdbe = string_to_bool(value);
3927 CONF("override_utf8_locale") {
3928 utf8_mode = string_to_bool(value);
3931 CONF("draw_borders") {
3932 draw_borders = string_to_bool(value);
3934 CONF("draw_shades") {
3935 draw_shades = string_to_bool(value);
3937 CONF("draw_outline") {
3938 draw_outline = string_to_bool(value);
3941 CONF("out_to_console") {
3942 out_to_console = string_to_bool(value);
3944 CONF("use_spacer") {
3945 use_spacer = string_to_bool(value);
3950 use_xft = string_to_bool(value);
3955 set_first_font(value);
3961 if (value && font_count >= 0)
3962 fonts[0].font_alpha = atof(value)
3970 if (string_to_bool(value))
3971 ERR("Xft not enabled");
3974 /* xftfont silently ignored when no Xft */
3977 /* xftalpha is silently ignored when no Xft */
3982 set_first_font(value);
3988 gap_x = atoi(value);
3994 gap_y = atoi(value);
3999 CONF("mail_spool") {
4002 variable_substitute(value, buf, 256);
4006 if (current_mail_spool)
4007 free(current_mail_spool);
4008 current_mail_spool = strdup(buf);
4014 CONF("minimum_size") {
4017 (value, "%d %d", &minimum_width,
4018 &minimum_height) != 2)
4021 &minimum_width) != 1)
4026 CONF("maximum_width") {
4028 if (sscanf(value, "%d", &maximum_width) != 1)
4034 CONF("no_buffers") {
4035 no_buffers = string_to_bool(value);
4038 CONF("mldonkey_hostname") {
4040 if (mlconfig.mldonkey_hostname != NULL) {
4041 free(mlconfig.mldonkey_hostname);
4043 mlconfig.mldonkey_hostname = strdup(value);
4048 CONF("mldonkey_port") {
4050 mlconfig.mldonkey_port = atoi(value);
4054 CONF("mldonkey_login") {
4056 if (mlconfig.mldonkey_login != NULL) {
4057 free(mlconfig.mldonkey_login);
4059 mlconfig.mldonkey_login = strdup(value);
4064 CONF("mldonkey_password") {
4066 if (mlconfig.mldonkey_password != NULL) {
4067 free(mlconfig.mldonkey_password);
4069 mlconfig.mldonkey_password = strdup(value);
4075 CONF("pad_percents") {
4076 pad_percents = atoi(value);
4080 CONF("own_window") {
4081 own_window = string_to_bool(value);
4087 CONF("stippled_borders") {
4089 stippled_borders = strtol(value, 0, 0);
4091 stippled_borders = 4;
4095 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
4098 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
4100 CONF("update_interval") {
4102 update_interval = strtod(value, 0);
4106 CONF("total_run_times") {
4108 total_run_times = strtod(value, 0);
4113 stuff_in_upper_case = string_to_bool(value);
4118 malloc(strlen(value)
4120 strcpy(seti_dir, value);
4124 if (text != original_text)
4134 int l = strlen(text);
4135 if (fgets(buf, 256, fp) == NULL)
4138 realloc(text, l + strlen(buf)
4142 if (strlen(text) > 1024 * 8)
4149 ERR("%s: %d: no such configuration: '%s'", f, line, name);
4159 /* : means that character before that takes an argument */
4160 static const char *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
4173 int main(int argc, char **argv)
4175 /* handle command line parameters that don't change configs */
4181 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
4182 *s) || ((s = getenv("LANG")) && *s)) {
4184 for(x = 0; x < strlen(s) ; x++) {
4185 temp[x] = tolower(s[x]);
4187 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
4191 if (!setlocale(LC_CTYPE, "")) {
4192 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
4197 int c = getopt(argc,
4207 ("Conky " VERSION " compiled " __DATE__ "\n");
4211 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
4212 * though free() does the NULL check itself;), then load optarg value */
4214 free(current_config);
4215 current_config = strdup(optarg);
4220 ("Usage: %s [OPTION]...\n"
4221 "Conky is a system monitor that renders text on desktop or to own transparent\n"
4222 "window. Command line options will override configurations defined in config\n"
4225 " -c FILE config file to load instead of "
4228 " -d daemonize, fork to background\n"
4231 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
4232 " -f FONT font to use\n"
4234 " -o create own window to draw\n"
4237 " -b double buffer (prevents flickering)\n"
4239 " -w WIN_ID window id to draw\n"
4240 " -x X x position\n"
4241 " -y Y y position\n"
4243 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
4244 " -u SECS update interval\n"
4245 " -i NUM number of times to update Conky\n", argv[0]);
4249 window.window = strtol(optarg, 0, 0);
4258 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
4262 tmpstring1 = (char *)
4263 malloc(TEXT_BUFFER_SIZE);
4264 tmpstring2 = (char *)
4265 malloc(TEXT_BUFFER_SIZE);
4267 /* load current_config or CONFIG_FILE */
4270 if (current_config == NULL) {
4271 /* load default config file */
4274 variable_substitute(CONFIG_FILE, buf, 256);
4277 current_config = strdup(buf);
4281 if (current_config != NULL)
4282 load_config_file(current_config);
4284 set_default_configurations();
4287 if (current_mail_spool == NULL) {
4289 variable_substitute(MAIL_FILE, buf, 256);
4292 current_mail_spool = strdup(buf);
4296 /* handle other command line arguments */
4298 #if defined(__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__)
4299 optind = optreset = 1;
4305 int c = getopt(argc,
4313 fork_to_background = 1;
4318 set_first_font(optarg);
4321 text_alignment = string_to_alignment(optarg);
4339 if (text != original_text)
4341 text = strdup(optarg);
4342 convert_escapes(text);
4346 update_interval = strtod(optarg, 0);
4350 total_run_times = strtod(optarg, 0);
4354 gap_x = atoi(optarg);
4358 gap_y = atoi(optarg);
4372 /* generate text and get initial size */
4373 extract_variable_text(text);
4374 if (text != original_text) {
4383 update_text_area(); /* to get initial size of the window */
4385 #if defined OWN_WINDOW
4388 text_width + border_margin * 2 + 1,
4389 text_height + border_margin * 2 + 1,
4390 on_bottom, fixed_pos);
4394 text_width + border_margin * 2 + 1,
4395 text_height + border_margin * 2 + 1,
4400 update_text_area(); /* to position text/window on screen */
4404 // why the fuck not?
4410 if (own_window && !fixed_pos)
4411 XMoveWindow(display, window.window, window.x, window.y);
4421 if (fork_to_background) {
4425 ERR("can't fork() to background: %s",
4435 "Conky: forked to background, pid is %d\n",
4441 /* set SIGUSR1, SIGINT and SIGTERM handlers */
4446 sa.sa_handler = reload_handler;
4447 sigemptyset(&sa.sa_mask);
4448 sa.sa_flags = SA_RESTART;
4449 if (sigaction(SIGUSR1, &sa, NULL) != 0)
4450 ERR("can't set signal handler for SIGUSR1: %s",
4453 sa.sa_handler = term_handler;
4454 sigemptyset(&sa.sa_mask);
4455 sa.sa_flags = SA_RESTART;
4456 if (sigaction(SIGINT, &sa, NULL) != 0)
4457 ERR("can't set signal handler for SIGINT: %s",
4460 sa.sa_handler = term_handler;
4461 sigemptyset(&sa.sa_mask);
4462 sa.sa_flags = SA_RESTART;
4463 if (sigaction(SIGTERM, &sa, NULL) != 0)
4464 ERR("can't set signal handler for SIGTERM: %s",