2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
26 #include <X11/Xutil.h>
27 #include <sys/types.h>
30 #define CONFIG_FILE "$HOME/.conkyrc"
31 #define MAIL_FILE "$MAIL"
32 #define MAX_IF_BLOCK_DEPTH 5
46 char name[TEXT_BUFFER_SIZE];
56 static int selected_font = 0;
57 static int font_count = -1;
58 struct font_list *fonts = NULL;
62 #define font_height() use_xft ? (fonts[selected_font].xftfont->ascent + fonts[selected_font].xftfont->descent) : \
63 (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
64 #define font_ascent() use_xft ? fonts[selected_font].xftfont->ascent : fonts[selected_font].font->max_bounds.ascent
65 #define font_descent() use_xft ? fonts[selected_font].xftfont->descent : fonts[selected_font].font->max_bounds.descent
69 #define font_height() (fonts[selected_font].font->max_bounds.ascent + fonts[selected_font].font->max_bounds.descent)
70 #define font_ascent() fonts[selected_font].font->max_bounds.ascent
71 #define font_descent() fonts[selected_font].font->max_bounds.descent
75 #define MAX_FONTS 64 // hmm, no particular reason, just makes sense.
78 static void set_font();
81 int addfont(const char *data_in)
83 if (font_count > MAX_FONTS) {
84 CRIT_ERR("you don't need that many fonts, sorry.");
87 if (font_count == 0) {
91 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
95 fonts = realloc(fonts, (sizeof(struct font_list) * (font_count+1)));
97 CRIT_ERR("realloc in addfont");
99 if (strlen(data_in) < TEXT_BUFFER_SIZE) { // must account for null terminator
100 strncpy(fonts[font_count].name, data_in, TEXT_BUFFER_SIZE);
102 fonts[font_count].font_alpha = 0xffff;
105 CRIT_ERR("Oops...looks like something overflowed in addfont().");
110 void set_first_font(const char *data_in)
112 if (font_count < 0) {
113 if ((fonts = (struct font_list*)malloc(sizeof(struct font_list))) == NULL) {
118 if (strlen(data_in) > 1) {
119 strncpy(fonts[0].name, data_in, TEXT_BUFFER_SIZE-1);
121 fonts[0].font_alpha = 0xffff;
129 for (i=0;i<=font_count;i++) {
132 XftFontClose(display, fonts[i].xftfont);
136 XFreeFont(display, fonts[i].font);
143 set_first_font("6x10");
147 static void load_fonts()
150 for (i=0;i<=font_count;i++) {
154 /*if (fonts[i].xftfont != NULL && selected_font == 0) {
155 XftFontClose(display, fonts[i].xftfont);
157 if ((fonts[i].xftfont =
158 XftFontOpenName(display, screen, fonts[i].name)) != NULL)
161 ERR("can't load Xft font '%s'", fonts[i].name);
162 if ((fonts[i].xftfont =
163 XftFontOpenName(display, screen,
164 "courier-12")) != NULL)
167 ERR("can't load Xft font '%s'", "courier-12");
169 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
170 CRIT_ERR("can't load font '%s'", "fixed");
177 /* load normal font */
178 /* if (fonts[i].font != NULL)
179 XFreeFont(display, fonts[i].font);*/
181 if ((fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) {
182 ERR("can't load font '%s'", fonts[i].name);
183 if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) {
184 CRIT_ERR("can't load font '%s'", "fixed");
190 /* default config file */
191 static char *current_config;
193 /* set to 1 if you want all text to be in uppercase */
194 static unsigned int stuff_in_upper_case;
196 /* Position on the screen */
197 static int text_alignment;
198 static int gap_x, gap_y;
200 /* Update interval */
201 static double update_interval;
203 /* Run how many times? */
204 static unsigned long total_run_times;
207 static int fork_to_background;
210 static int draw_borders;
211 static int stippled_borders;
213 static int draw_shades, draw_outline;
215 static int border_margin, border_width;
217 static long default_fg_color, default_bg_color, default_out_color;
219 static int cpu_avg_samples, net_avg_samples;
221 /* create own window or draw stuff to root? */
222 static int own_window = 0;
225 /* fixed size/pos is set if wm/user changes them */
226 static int fixed_size = 0, fixed_pos = 0;
229 static int minimum_width, minimum_height;
231 /* no buffers in used memory? */
234 /* pad percentages to decimals? */
235 static int pad_percents = 0;
241 /* Text that is shown */
242 static char original_text[] =
243 "$nodename - $sysname $kernel on $machine\n"
245 "${color grey}Uptime:$color $uptime\n"
246 "${color grey}Frequency (in MHz):$color $freq\n"
247 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
248 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
249 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
250 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
252 "${color grey}File systems:\n"
253 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
254 "${color grey}Networking:\n"
255 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
256 "${color grey}Temperatures:\n"
257 " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
260 "${color grey}SETI@Home Statistics:\n"
261 "${color grey}Seti Unit Number:$color $seti_credit\n"
262 "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
265 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
266 "Bitrate: $mpd_bitrate\n" "Progress: $mpd_bar\n"
268 "${color grey}Name PID CPU% MEM%\n"
269 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
270 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
271 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
272 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
273 "${tail /var/log/Xorg.0.log 3}";
275 static char *text = original_text;
277 static int total_updates;
280 static int blockdepth = 0;
281 static int if_jumped = 0;
282 static int blockstart[MAX_IF_BLOCK_DEPTH];
284 int check_mount(char *s)
287 FILE *mtab = fopen("/etc/mtab", "r");
289 char buf1[256], buf2[128];
290 while (fgets(buf1, 256, mtab)) {
291 sscanf(buf1, "%*s %128s", buf2);
292 if (!strcmp(s, buf2)) {
299 ERR("Could not open mtab");
306 static inline int calc_text_width(const char *s, unsigned int l)
312 XftTextExtentsUtf8(display, fonts[selected_font].xftfont, s, l, &gi);
314 XftTextExtents8(display, fonts[selected_font].xftfont, s, l, &gi);
320 return XTextWidth(fonts[selected_font].font, s, l);
324 /* formatted text to render on screen, generated in generate_text(),
325 * drawn in draw_stuff() */
327 static char text_buffer[TEXT_BUFFER_SIZE * 4];
329 /* special stuff in text_buffer */
331 #define SPECIAL_CHAR '\x01'
347 static struct special_t {
357 unsigned long first_colour; // for graph gradient
358 unsigned long last_colour;
361 static int special_count;
362 static int special_index; /* used when drawing */
364 #define MAX_GRAPH_DEPTH 256 /* why 256? who knows. */
366 static struct special_t *new_special(char *buf, int t)
368 if (special_count >= 128)
369 CRIT_ERR("too many special things in text");
371 buf[0] = SPECIAL_CHAR;
373 if (t == GRAPH && specials[special_count].graph == NULL) {
374 if (specials[special_count].width > 0
375 && specials[special_count].width < MAX_GRAPH_DEPTH)
376 specials[special_count].graph_width = specials[special_count].width - 3; // subtract 3 for the box
378 specials[special_count].graph_width =
380 specials[special_count].graph =
381 calloc(specials[special_count].graph_width,
383 specials[special_count].graph_scale = 100;
385 specials[special_count].type = t;
386 return &specials[special_count++];
389 typedef struct tailstring_list {
390 char data[TEXT_BUFFER_SIZE];
391 struct tailstring_list *next;
394 void addtail(tailstring ** head, char *data_in)
397 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
400 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
405 void freetail(tailstring * head)
409 while (head != NULL) {
418 static void new_bar(char *buf, int w, int h, int usage)
420 struct special_t *s = new_special(buf, BAR);
421 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
426 static const char *scan_bar(const char *args, int *w, int *h)
428 *w = 0; /* zero width means all space that is available */
430 /* bar's argument is either height or height,width */
433 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
434 sscanf(args, "%d %n", h, &n);
441 static char *scan_font(const char *args)
443 if (args && sizeof(args) < 127) {
447 ERR("font scan failed, lets hope it doesn't mess stuff up");
452 static void new_font(char *buf, char * args) {
453 struct special_t *s = new_special(buf, FONT);
454 if (!s->font_added || strcmp(args, fonts[s->font_added].name)) {
455 int tmp = selected_font;
456 selected_font = s->font_added = addfont(args);
463 inline void graph_append(struct special_t *graph, double f)
467 graph->graph_scale = 0;
469 graph->graph[graph->graph_width - 1] = f; /* add new data */
470 for (i = 0; i < graph->graph_width - 1; i++) { /* shift all the data by 1 */
471 graph->graph[i] = graph->graph[i + 1];
472 if (graph->scaled && graph->graph[i] > graph->graph_scale) {
473 graph->graph_scale = graph->graph[i]; /* check if we need to update the scale */
478 static void new_graph(char *buf, int w, int h, unsigned int first_colour, unsigned int second_colour, double i, int scaled)
480 struct special_t *s = new_special(buf, GRAPH);
483 s->first_colour = first_colour;
484 s->last_colour = second_colour;
487 s->graph_width = s->width - 3; // subtract 3 for rectangle around
492 s->graph_scale = 100;
497 static const char *scan_graph(const char *args, int *w, int *h, unsigned int *first_colour, unsigned int *last_colour)
499 *w = 0; /* zero width means all space that is available */
503 /* graph's argument is either height or height,width */
505 if (sscanf(args, "%*s %d,%d %x %x", h, w, first_colour, last_colour) < 4) {
506 if (sscanf(args, "%d,%d %x %x", h, w, first_colour, last_colour) < 4) {
509 if (sscanf(args, "%*s %x %x", first_colour, last_colour) < 2) {
512 if (sscanf(args, "%x %x", first_colour, last_colour) < 2) {
515 if (sscanf(args, "%d,%d", h, w) < 2) {
518 sscanf(args, "%*s %d,%d", h, w);
530 static inline void new_hr(char *buf, int a)
532 new_special(buf, HORIZONTAL_LINE)->height = a;
535 static inline void new_stippled_hr(char *buf, int a, int b)
537 struct special_t *s = new_special(buf, STIPPLED_HR);
542 static inline void new_fg(char *buf, long c)
544 new_special(buf, FG)->arg = c;
547 static inline void new_bg(char *buf, long c)
549 new_special(buf, BG)->arg = c;
552 static inline void new_outline(char *buf, long c)
554 new_special(buf, OUTLINE)->arg = c;
557 static inline void new_offset(char *buf, long c)
559 new_special(buf, OFFSET)->arg = c;
562 static inline void new_alignr(char *buf, long c)
564 new_special(buf, ALIGNR)->arg = c;
567 static inline void new_alignc(char *buf, long c)
569 new_special(buf, ALIGNC)->arg = c;
572 /* quite boring functions */
574 static inline void for_each_line(char *b, void (*f) (char *))
578 for (ps = b, pe = b; *pe; pe++) {
591 static void convert_escapes(char *buf)
593 char *p = buf, *s = buf;
609 /* converts from bytes to human readable format (k, M, G) */
610 static void human_readable(long long a, char *buf, int size)
612 if (a >= 1024 * 1024 * 1024)
613 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
614 else if (a >= 1024 * 1024) {
615 double m = (a / 1024) / 1024.0;
617 snprintf(buf, size, "%.0fM", m);
619 snprintf(buf, size, "%.1fM", m);
620 } else if (a >= 1024)
621 snprintf(buf, size, "%Ldk", a / (long long) 1024);
623 snprintf(buf, size, "%Ld", a);
628 enum text_object_type {
691 OBJ_ml_upload_counter,
692 OBJ_ml_download_counter,
693 OBJ_ml_nshared_files,
694 OBJ_ml_shared_counter,
695 OBJ_ml_tcp_upload_rate,
696 OBJ_ml_tcp_download_rate,
697 OBJ_ml_udp_upload_rate,
698 OBJ_ml_udp_download_rate,
699 OBJ_ml_ndownloaded_files,
700 OBJ_ml_ndownloading_files,
703 OBJ_running_processes,
712 OBJ_temp1, /* i2c is used instead in these */
764 char *s; /* some string */
765 int i; /* some integer */
766 long l; /* some other integer */
767 struct net_stat *net;
769 unsigned char loadavg[3];
802 char buffer[TEXT_BUFFER_SIZE*4];
818 static unsigned int text_object_count;
819 static struct text_object *text_objects;
821 /* new_text_object() allocates a new zeroed text_object */
822 static struct text_object *new_text_object()
825 text_objects = (struct text_object *) realloc(text_objects,
829 memset(&text_objects[text_object_count - 1], 0,
830 sizeof(struct text_object));
832 return &text_objects[text_object_count - 1];
835 static void free_text_objects()
839 for (i = 0; i < text_object_count; i++) {
840 switch (text_objects[i].type) {
842 close(text_objects[i].data.i);
846 close(text_objects[i].data.i2c.fd);
850 case OBJ_if_existing:
853 free(text_objects[i].data.ifblock.s);
868 free(text_objects[i].data.s);
872 free(text_objects[i].data.execi.cmd);
873 free(text_objects[i].data.execi.buffer);
880 text_object_count = 0;
883 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
888 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
889 *a = mixer_init(buf1);
890 (void) scan_bar(arg + n, w, h);
893 (void) scan_bar(arg, w, h);
897 /* construct_text_object() creates a new text_object */
898 static void construct_text_object(const char *s, const char *arg)
900 struct text_object *obj = new_text_object();
902 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
903 #define END ; } } else
906 obj->type = OBJ_color;
907 obj->data.l = get_x11_color(s);
909 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
910 END OBJ(acpiacadapter, 0)
911 END OBJ(freq, 0) END OBJ(acpifan, 0) END OBJ(battery,
914 sscanf(arg, "%63s", bat);
917 obj->data.s = strdup(bat);
918 END OBJ(buffers, INFO_BUFFERS)
919 END OBJ(cached, INFO_BUFFERS)
920 END OBJ(cpu, INFO_CPU)
921 END OBJ(cpubar, INFO_CPU)
922 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
923 END OBJ(cpugraph, INFO_CPU)
924 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
925 END OBJ(color, 0) obj->data.l =
926 arg ? get_x11_color(arg) : default_fg_color;
929 obj->data.s = scan_font(arg);
931 OBJ(downspeed, INFO_NET) obj->data.net = get_net_stat(arg);
932 END OBJ(downspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
933 END OBJ(downspeedgraph, INFO_NET)
934 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
936 sscanf(arg, "%63s %*i,%*i %*i", buf);
937 obj->data.net = get_net_stat(buf);
938 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
939 if (sscanf(arg, "%*s %d,%d", &obj->b, &obj->a) <= 1) {
948 text_objects[blockstart[blockdepth - 1] -
949 1].data.ifblock.pos = text_object_count;
950 blockstart[blockdepth - 1] = text_object_count;
951 obj->data.ifblock.pos = text_object_count + 2;
953 ERR("$else: no matching $if_*");
958 text_objects[blockstart[blockdepth] - 1].data.ifblock.pos =
961 ERR("$endif: no matching $if_*");
965 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
966 END OBJ(execbar, 0) obj->data.s = strdup(arg ? arg : "");
967 END OBJ(execgraph, 0) obj->data.s = strdup(arg ? arg : "");
968 END OBJ(execi, 0) unsigned int n;
971 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
973 ERR("${execi <interval> command}");
974 obj->type = OBJ_text;
975 snprintf(buf, 256, "${%s}", s);
976 obj->data.s = strdup(buf);
978 obj->data.execi.cmd = strdup(arg + n);
979 obj->data.execi.buffer =
980 (char *) calloc(1, TEXT_BUFFER_SIZE);
982 END OBJ(pre_exec, 0) obj->type = OBJ_text;
984 FILE *fp = popen(arg, "r");
988 n = fread(buf, 1, 2048, fp);
991 if (n && buf[n - 1] == '\n')
996 obj->data.s = strdup(buf);
998 obj->data.s = strdup("");
1001 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
1002 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
1004 while (isspace(*arg))
1010 obj->data.fsbar.fs = prepare_fs_stat(arg);
1011 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
1014 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
1018 obj->data.fsbar.fs = prepare_fs_stat(arg);
1019 END OBJ(fs_free, INFO_FS) if (!arg)
1021 obj->data.fs = prepare_fs_stat(arg);
1022 END OBJ(fs_used_perc, INFO_FS) if (!arg)
1024 obj->data.fs = prepare_fs_stat(arg);
1025 END OBJ(fs_free_perc, INFO_FS) if (!arg)
1027 obj->data.fs = prepare_fs_stat(arg);
1028 END OBJ(fs_size, INFO_FS) if (!arg)
1030 obj->data.fs = prepare_fs_stat(arg);
1031 END OBJ(fs_used, INFO_FS) if (!arg)
1033 obj->data.fs = prepare_fs_stat(arg);
1034 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
1035 END OBJ(offset, 0) obj->data.i = arg ? atoi(arg) : 1;
1036 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
1040 ERR("i2c needs arguments");
1041 obj->type = OBJ_text;
1042 obj->data.s = strdup("${i2c}");
1046 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
1047 /* if scanf couldn't read three values, read type and num and use
1049 sscanf(arg, "%63s %d", buf2, &n);
1051 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
1052 obj->data.i2c.devtype);
1053 strcpy(obj->data.i2c.type, buf2);
1056 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
1057 obj->data.i2c.devtype);
1058 strcpy(obj->data.i2c.type, buf2);
1061 END OBJ(top, INFO_TOP)
1065 ERR("top needs arguments");
1066 obj->type = OBJ_text;
1067 obj->data.s = strdup("${top}");
1070 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1071 if (strcmp(buf, "name") == 0) {
1072 obj->data.top.type = TOP_NAME;
1073 } else if (strcmp(buf, "cpu") == 0) {
1074 obj->data.top.type = TOP_CPU;
1075 } else if (strcmp(buf, "pid") == 0) {
1076 obj->data.top.type = TOP_PID;
1077 } else if (strcmp(buf, "mem") == 0) {
1078 obj->data.top.type = TOP_MEM;
1080 ERR("invalid arg for top");
1083 if (n < 1 || n > 10) {
1084 CRIT_ERR("invalid arg for top");
1087 obj->data.top.num = n - 1;
1091 ERR("invalid args given for top");
1094 END OBJ(top_mem, INFO_TOP)
1098 ERR("top_mem needs arguments");
1099 obj->type = OBJ_text;
1100 obj->data.s = strdup("${top_mem}");
1103 if (sscanf(arg, "%63s %i", buf, &n) == 2) {
1104 if (strcmp(buf, "name") == 0) {
1105 obj->data.top.type = TOP_NAME;
1106 } else if (strcmp(buf, "cpu") == 0) {
1107 obj->data.top.type = TOP_CPU;
1108 } else if (strcmp(buf, "pid") == 0) {
1109 obj->data.top.type = TOP_PID;
1110 } else if (strcmp(buf, "mem") == 0) {
1111 obj->data.top.type = TOP_MEM;
1113 ERR("invalid arg for top");
1116 if (n < 1 || n > 10) {
1117 CRIT_ERR("invalid arg for top");
1120 obj->data.top.num = n - 1;
1124 ERR("invalid args given for top");
1127 END OBJ(addr, INFO_NET) obj->data.net = get_net_stat(arg);
1128 END OBJ(linkstatus, INFO_WIFI) obj->data.net = get_net_stat(arg);
1133 ERR("tail needs arguments");
1134 obj->type = OBJ_text;
1135 obj->data.s = strdup("${tail}");
1138 if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
1139 if (n1 < 1 || n1 > 30) {
1141 ("invalid arg for tail, number of lines must be between 1 and 30");
1145 fp = fopen(buf, "rt");
1147 obj->data.tail.logfile =
1148 malloc(TEXT_BUFFER_SIZE);
1149 strcpy(obj->data.tail.logfile, buf);
1150 obj->data.tail.wantedlines = n1 - 1;
1151 obj->data.tail.interval =
1152 update_interval * 2;
1157 ("tail logfile does not exist, or you do not have correct permissions");
1160 } else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
1161 if (n1 < 1 || n1 > 30) {
1163 ("invalid arg for tail, number of lines must be between 1 and 30");
1165 } else if (n2 < 1 || n2 < update_interval) {
1167 ("invalid arg for tail, interval must be greater than 0 and Conky's interval");
1171 fp = fopen(buf, "rt");
1173 obj->data.tail.logfile =
1174 malloc(TEXT_BUFFER_SIZE);
1175 strcpy(obj->data.tail.logfile, buf);
1176 obj->data.tail.wantedlines = n1 - 1;
1177 obj->data.tail.interval = n2;
1182 ("tail logfile does not exist, or you do not have correct permissions");
1188 ERR("invalid args given for tail");
1191 END OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
1193 r = sscanf(arg, "%d %d %d", &a, &b, &c);
1194 if (r >= 3 && (c < 1 || c > 3))
1196 if (r >= 2 && (b < 1 || b > 3))
1198 if (r >= 1 && (a < 1 || a > 3))
1201 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
1202 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
1203 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
1204 END OBJ(if_existing, 0)
1205 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1206 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1209 ERR("if_existing needs an argument");
1210 obj->data.ifblock.s = 0;
1212 obj->data.ifblock.s = strdup(arg);
1213 blockstart[blockdepth] = text_object_count;
1214 obj->data.ifblock.pos = text_object_count + 2;
1216 END OBJ(if_mounted, 0)
1217 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1218 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1221 ERR("if_mounted needs an argument");
1222 obj->data.ifblock.s = 0;
1224 obj->data.ifblock.s = strdup(arg);
1225 blockstart[blockdepth] = text_object_count;
1226 obj->data.ifblock.pos = text_object_count + 2;
1228 END OBJ(if_running, 0)
1229 if (blockdepth >= MAX_IF_BLOCK_DEPTH) {
1230 CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");
1234 snprintf(buf, 256, "pidof %s >/dev/null", arg);
1235 obj->data.ifblock.s = strdup(buf);
1237 ERR("if_running needs an argument");
1238 obj->data.ifblock.s = 0;
1240 blockstart[blockdepth] = text_object_count;
1241 obj->data.ifblock.pos = text_object_count + 2;
1245 END OBJ(mails, INFO_MAIL)
1246 END OBJ(mem, INFO_MEM)
1247 END OBJ(memmax, INFO_MEM)
1248 END OBJ(memperc, INFO_MEM)
1249 END OBJ(membar, INFO_MEM)
1250 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1251 END OBJ(membar, INFO_MEM)
1252 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1253 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
1254 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
1255 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
1256 END OBJ(mixerbar, INFO_MIXER)
1257 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1258 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1259 END OBJ(mixerlbar, INFO_MIXER)
1260 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1261 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1262 END OBJ(mixerrbar, INFO_MIXER)
1263 scan_mixer_bar(arg, &obj->data.mixerbar.l,
1264 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
1267 OBJ(ml_upload_counter, INFO_MLDONKEY)
1268 END OBJ(ml_download_counter, INFO_MLDONKEY)
1269 END OBJ(ml_nshared_files, INFO_MLDONKEY)
1270 END OBJ(ml_shared_counter, INFO_MLDONKEY)
1271 END OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1272 END OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1273 END OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1274 END OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1275 END OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1276 END OBJ(ml_ndownloading_files, INFO_MLDONKEY) END
1278 OBJ(new_mails, INFO_MAIL)
1279 END OBJ(nodename, 0)
1280 END OBJ(processes, INFO_PROCS)
1281 END OBJ(running_processes, INFO_RUN_PROCS)
1282 END OBJ(shadecolor, 0)
1283 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1284 END OBJ(outlinecolor, 0)
1285 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1286 END OBJ(stippled_hr, 0) int a = stippled_borders, b = 1;
1288 if (sscanf(arg, "%d %d", &a, &b) != 2)
1289 sscanf(arg, "%d", &b);
1293 obj->data.pair.a = a;
1294 obj->data.pair.b = b;
1295 END OBJ(swap, INFO_MEM)
1296 END OBJ(swapmax, INFO_MEM)
1297 END OBJ(swapperc, INFO_MEM)
1298 END OBJ(swapbar, INFO_MEM)
1299 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1300 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1302 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1303 obj->data.i2c.devtype);
1304 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1306 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1307 obj->data.i2c.devtype);
1308 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1309 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1310 END OBJ(totaldown, INFO_NET) obj->data.net = get_net_stat(arg);
1311 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1313 END OBJ(alignr, 0) obj->data.i = arg ? atoi(arg) : 1;
1314 END OBJ(alignc, 0) obj->data.i = arg ? atoi(arg) : 1;
1315 END OBJ(upspeed, INFO_NET) obj->data.net = get_net_stat(arg);
1316 END OBJ(upspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1317 END OBJ(upspeedgraph, INFO_NET)
1318 (void) scan_graph(arg, &obj->a, &obj->b, &obj->c, &obj->d);
1320 sscanf(arg, "%63s %*i,%*i %*i", buf);
1321 obj->data.net = get_net_stat(buf);
1322 if (sscanf(arg, "%*s %d,%d %*d", &obj->b, &obj->a) <= 1) {
1323 if (sscanf(arg, "%*s %d,%d", &obj->a, &obj->a) <= 1) {
1328 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1329 OBJ(adt746xcpu, 0) END OBJ(adt746xfan, 0) END
1331 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1332 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1333 END OBJ(seti_credit, INFO_SETI) END
1336 OBJ(metar_ob_time, INFO_METAR)
1338 OBJ(metar_temp, INFO_METAR)
1340 OBJ(metar_tempf, INFO_METAR)
1342 OBJ(metar_windchill, INFO_METAR)
1344 OBJ(metar_dew_point, INFO_METAR)
1346 OBJ(metar_rh, INFO_METAR)
1348 OBJ(metar_windspeed, INFO_METAR)
1350 OBJ(metar_winddir, INFO_METAR)
1352 OBJ(metar_swinddir, INFO_METAR)
1353 END OBJ(metar_cloud, INFO_METAR)
1354 END OBJ(metar_u2d_time, INFO_METAR) END
1357 OBJ(mpd_artist, INFO_MPD)
1358 END OBJ(mpd_title, INFO_MPD)
1359 END OBJ(mpd_elapsed, INFO_MPD)
1360 END OBJ(mpd_length, INFO_MPD)
1361 END OBJ(mpd_percent, INFO_MPD)
1362 END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol,
1363 INFO_MPD) END OBJ(mpd_bitrate,
1365 END OBJ(mpd_status, INFO_MPD) END OBJ(mpd_bar, INFO_MPD)
1366 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1371 ERR("unknown variable %s", s);
1372 obj->type = OBJ_text;
1373 snprintf(buf, 256, "${%s}", s);
1374 obj->data.s = strdup(buf);
1379 /* append_text() appends text to last text_object if it's text, if it isn't
1380 * it creates a new text_object */
1381 static void append_text(const char *s)
1383 struct text_object *obj;
1385 if (s == NULL || *s == '\0')
1388 obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1390 /* create a new text object? */
1391 if (!obj || obj->type != OBJ_text) {
1392 obj = new_text_object();
1393 obj->type = OBJ_text;
1394 obj->data.s = strdup(s);
1397 obj->data.s = (char *) realloc(obj->data.s,
1398 strlen(obj->data.s) +
1400 strcat(obj->data.s, s);
1404 static void extract_variable_text(const char *p)
1408 free_text_objects();
1423 /* variable is either $foo or ${foo} */
1427 while (*p && *p != '}')
1433 while (*p && (isalnum((int) *p)
1438 /* copy variable to buffer */
1439 len = (p - s > 255) ? 255 : (p - s);
1440 strncpy(buf, s, len);
1449 /* if variable wasn't found from environment, use some special */
1455 if (strchr(buf, ' ')) {
1456 arg = strchr(buf, ' ');
1459 while (isspace((int) *arg))
1465 /* lowercase variable name */
1472 construct_text_object(buf, arg);
1483 ERR("one or more $endif's are missing");
1487 double current_update_time, last_update_time;
1489 static void generate_text()
1492 struct information *cur = &info;
1499 current_update_time = get_time();
1505 n = TEXT_BUFFER_SIZE * 4 - 2;
1508 for (i = 0; i < text_object_count; i++) {
1509 struct text_object *obj = &text_objects[i];
1511 #define OBJ(a) break; case OBJ_##a:
1513 switch (obj->type) {
1516 ERR("not implemented obj type %d",
1520 /* does anyone have decimals in acpi temperature? */
1522 snprintf(p, n, "%d", (int)
1523 get_acpi_temperature(obj->
1527 snprintf(p, 5, "%d ", (int)
1528 get_acpi_temperature(obj->
1533 snprintf(p, n, "%s", get_freq());
1536 snprintf(p, n, "%s", get_adt746x_cpu());
1539 snprintf(p, n, "%s", get_adt746x_fan());
1542 snprintf(p, n, "%s", get_acpi_fan());
1544 OBJ(acpiacadapter) {
1545 snprintf(p, n, "%s",
1546 get_acpi_ac_adapter());
1549 get_battery_stuff(p, n, obj->data.s);
1552 human_readable(cur->buffers * 1024, p,
1556 human_readable(cur->cached * 1024, p, 255);
1560 snprintf(p, n, "%*d", pad_percents,
1561 (int) (cur->cpu_usage *
1564 snprintf(p, 4, "%*d ",
1566 (int) (cur->cpu_usage *
1570 new_bar(p, obj->data.pair.a,
1572 (int) (cur->cpu_usage * 255.0));
1575 new_graph(p, obj->a,
1576 obj->b, obj->c, obj->d,
1577 (unsigned int) (cur->cpu_usage *
1581 new_fg(p, obj->data.l);
1584 new_font(p, obj->data.s);
1588 snprintf(p, n, "%d",
1589 (int) (obj->data.net->
1593 snprintf(p, 6, "%d ",
1594 (int) (obj->data.net->
1600 snprintf(p, n, "%.1f",
1602 recv_speed / 1024.0);
1604 snprintf(p, 8, "%.1f ",
1606 recv_speed / 1024.0);
1608 OBJ(downspeedgraph) {
1609 if (obj->data.net->recv_speed == 0) // this is just to make the ugliness at start go away
1610 obj->data.net->recv_speed = 0.01;
1611 new_graph(p, obj->a, obj->b, obj->c, obj->d,
1612 (obj->data.net->recv_speed /
1619 i = obj->data.ifblock.pos - 2;
1629 snprintf(p, n, "%u.%u.%u.%u",
1630 obj->data.net->addr.
1632 obj->data.net->addr.
1634 obj->data.net->addr.
1636 obj->data.net->addr.
1641 snprintf(p, n, "%d",
1642 obj->data.net->linkstatus);
1647 FILE *fp = popen(obj->data.s, "r");
1648 int n2 = fread(p, 1, n, fp);
1652 if (n2 && p[n2 - 1] == '\n')
1663 FILE *fp = popen(obj->data.s, "r");
1664 int n2 = fread(p, 1, n, fp);
1668 if (n2 && p[n2 - 1] == '\n')
1677 if (sscanf(p, "%lf", &barnum) == 0) {
1678 ERR("reading execbar value failed (perhaps it's not the correct format?)");
1680 if (barnum > 100 || barnum < 0) {
1681 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
1683 barnum = barnum / 100.0;
1685 4, (int) (barnum * 255.0));
1691 FILE *fp = popen(obj->data.s, "r");
1692 int n2 = fread(p, 1, n, fp);
1696 if (n2 && p[n2 - 1] == '\n')
1705 if (sscanf(p, "%lf", &barnum) == 0) {
1706 ERR("reading execgraph value failed (perhaps it's not the correct format?)");
1708 if (barnum > 100 || barnum < 0) {
1709 ERR("your execgraph value is not between 0 and 100, therefore it will be ignored");
1712 25, obj->c, obj->d, (int) (barnum), 0);
1717 if (current_update_time -
1718 obj->data.execi.last_update <
1719 obj->data.execi.interval) {
1720 snprintf(p, n, "%s",
1721 obj->data.execi.buffer);
1723 char *p2 = obj->data.execi.buffer;
1725 popen(obj->data.execi.cmd,
1728 fread(p2, 1, TEXT_BUFFER_SIZE,
1733 if (n2 && p2[n2 - 1] == '\n')
1742 snprintf(p, n, "%s",
1743 obj->data.execi.buffer);
1745 obj->data.execi.last_update =
1746 current_update_time;
1751 if (obj->data.fs != NULL) {
1752 if (obj->data.fs->size == 0)
1771 if (obj->data.fs != NULL)
1772 human_readable(obj->data.fs->avail,
1776 if (obj->data.fs != NULL) {
1777 if (obj->data.fs->size)
1778 snprintf(p, n, "%*d",
1787 snprintf(p, n, "0");
1791 if (obj->data.fs != NULL)
1792 human_readable(obj->data.fs->size,
1796 if (obj->data.fs != NULL)
1797 human_readable(obj->data.fs->size -
1798 obj->data.fs->avail,
1802 if (obj->data.fs != NULL) {
1803 if (obj->data.fs->size == 0)
1821 if (obj->data.fs != NULL) {
1822 if (obj->data.fs->size)
1823 snprintf(p, 4, "%d",
1833 snprintf(p, n, "0");
1837 float *v = info.loadavg;
1839 if (obj->data.loadavg[2])
1840 snprintf(p, n, "%.2f %.2f %.2f",
1841 v[obj->data.loadavg[0] -
1843 v[obj->data.loadavg[1] -
1845 v[obj->data.loadavg[2] -
1847 else if (obj->data.loadavg[1])
1848 snprintf(p, n, "%.2f %.2f",
1849 v[obj->data.loadavg[0] -
1851 v[obj->data.loadavg[1] -
1853 else if (obj->data.loadavg[0])
1854 snprintf(p, n, "%.2f",
1855 v[obj->data.loadavg[0] -
1859 new_hr(p, obj->data.i);
1862 new_offset(p, obj->data.i);
1867 r = get_i2c_info(&obj->data.i2c.fd,
1869 obj->data.i2c.devtype,
1870 obj->data.i2c.type);
1872 if (r >= 100.0 || r == 0)
1873 snprintf(p, n, "%d", (int) r);
1875 snprintf(p, n, "%.1f", r);
1878 new_alignr(p, obj->data.i);
1881 new_alignc(p, obj->data.i);
1885 if ((obj->data.ifblock.s)
1886 && (stat(obj->data.ifblock.s, &tmp) ==
1888 i = obj->data.ifblock.pos - 2;
1894 if ((obj->data.ifblock.s)
1895 && (!check_mount(obj->data.ifblock.s))) {
1896 i = obj->data.ifblock.pos - 2;
1902 if ((obj->data.ifblock.s)
1903 && system(obj->data.ifblock.s)) {
1904 i = obj->data.ifblock.pos - 2;
1910 snprintf(p, n, "%s", cur->uname_s.release);
1913 snprintf(p, n, "%s", cur->uname_s.machine);
1918 human_readable(cur->mem * 1024, p, 6);
1921 human_readable(cur->memmax * 1024, p, 255);
1926 snprintf(p, n, "%*d",
1931 snprintf(p, 4, "%*d ",
1938 new_bar(p, obj->data.pair.a,
1940 cur->memmax ? (cur->mem * 255) /
1945 new_graph(p, obj->a,
1946 obj->b, obj->c, obj->d,
1947 cur->memmax ? (cur->mem) /
1948 (cur->memmax) : 0, 0);
1952 snprintf(p, n, "%d",
1953 mixer_get_avg(obj->data.l));
1956 snprintf(p, n, "%d",
1957 mixer_get_left(obj->data.l));
1960 snprintf(p, n, "%d",
1961 mixer_get_right(obj->data.l));
1964 new_bar(p, obj->data.mixerbar.w,
1965 obj->data.mixerbar.h,
1966 mixer_get_avg(obj->data.mixerbar.
1970 new_bar(p, obj->data.mixerbar.w,
1971 obj->data.mixerbar.h,
1972 mixer_get_left(obj->data.mixerbar.
1976 new_bar(p, obj->data.mixerbar.w,
1977 obj->data.mixerbar.h,
1978 mixer_get_right(obj->data.mixerbar.
1984 snprintf(p, n, "%d", cur->mail_count);
1987 snprintf(p, n, "%d", cur->new_mail_count);
1990 OBJ(ml_upload_counter) {
1991 snprintf(p, n, "%lld",
1992 mlinfo.upload_counter / 1048576);
1994 OBJ(ml_download_counter) {
1995 snprintf(p, n, "%lld",
1996 mlinfo.download_counter /
1999 OBJ(ml_nshared_files) {
2000 snprintf(p, n, "%i", mlinfo.nshared_files);
2002 OBJ(ml_shared_counter) {
2003 snprintf(p, n, "%lld",
2004 mlinfo.shared_counter / 1048576);
2006 OBJ(ml_tcp_upload_rate) {
2007 snprintf(p, n, "%.2f",
2008 (float) mlinfo.tcp_upload_rate /
2011 OBJ(ml_tcp_download_rate) {
2012 snprintf(p, n, "%.2f",
2013 (float) mlinfo.tcp_download_rate /
2016 OBJ(ml_udp_upload_rate) {
2017 snprintf(p, n, "%.2f",
2018 (float) mlinfo.udp_upload_rate /
2021 OBJ(ml_udp_download_rate) {
2022 snprintf(p, n, "%.2f",
2023 (float) mlinfo.udp_download_rate /
2026 OBJ(ml_ndownloaded_files) {
2027 snprintf(p, n, "%i",
2028 mlinfo.ndownloaded_files);
2030 OBJ(ml_ndownloading_files) {
2031 snprintf(p, n, "%i",
2032 mlinfo.ndownloading_files);
2037 snprintf(p, n, "%s",
2038 cur->uname_s.nodename);
2041 new_outline(p, obj->data.l);
2045 snprintf(p, n, "%d", cur->procs);
2047 snprintf(p, 5, "%d ",
2050 OBJ(running_processes) {
2052 snprintf(p, n, "%d",
2055 snprintf(p, 3, "%d ",
2059 snprintf(p, n, "%s", obj->data.s);
2062 new_bg(p, obj->data.l);
2065 new_stippled_hr(p, obj->data.pair.a,
2069 human_readable(cur->swap * 1024, p, 255);
2072 human_readable(cur->swapmax * 1024, p,
2076 if (cur->swapmax == 0) {
2077 strncpy(p, "No swap", 255);
2080 snprintf(p, 255, "%*u",
2086 snprintf(p, 4, "%*u ",
2094 new_bar(p, obj->data.pair.a,
2096 cur->swapmax ? (cur->swap * 255) /
2097 (cur->swapmax) : 0);
2100 snprintf(p, n, "%s", cur->uname_s.sysname);
2103 time_t t = time(NULL);
2104 struct tm *tm = localtime(&t);
2105 setlocale(LC_TIME, "");
2106 strftime(p, n, obj->data.s, tm);
2109 time_t t = time(NULL);
2110 struct tm *tm = gmtime(&t);
2111 strftime(p, n, obj->data.s, tm);
2114 human_readable(obj->data.net->recv, p,
2118 human_readable(obj->data.net->trans, p,
2122 snprintf(p, n, "%d", total_updates);
2126 snprintf(p, n, "%d",
2127 (int) (obj->data.net->
2131 snprintf(p, 5, "%d ",
2132 (int) (obj->data.net->
2138 snprintf(p, n, "%.1f",
2140 trans_speed / 1024.0);
2142 snprintf(p, 8, "%.1f ",
2144 trans_speed / 1024.0);
2147 if (obj->data.net->trans_speed == 0) // this is just to make the ugliness at start go away
2148 obj->data.net->trans_speed = 0.01;
2149 new_graph(p, obj->a, obj->b, obj->c, obj->d,
2150 (obj->data.net->trans_speed /
2154 format_seconds_short(p, n,
2158 format_seconds(p, n, (int) cur->uptime);
2163 snprintf(p, n, "%.2f",
2164 cur->seti_prog * 100.0f);
2167 new_bar(p, obj->data.pair.a,
2169 (int) (cur->seti_prog * 255.0f));
2172 snprintf(p, n, "%.0f", cur->seti_credit);
2178 snprintf(p, n, "%s", cur->mpd.title);
2181 snprintf(p, n, "%s", cur->mpd.artist);
2184 snprintf(p, n, "%s", cur->mpd.album);
2187 snprintf(p, n, "%i", cur->mpd.volume);
2190 snprintf(p, n, "%i", cur->mpd.bitrate);
2193 snprintf(p, n, "%s", cur->mpd.status);
2196 int days = 0, hours = 0, minutes =
2198 int tmp = cur->mpd.elapsed;
2199 while (tmp >= 86400) {
2203 while (tmp >= 3600) {
2213 snprintf(p, n, "%i days %i:%i:%2i",
2214 days, hours, minutes,
2217 snprintf(p, n, "%i:%i:%02i", hours,
2220 snprintf(p, n, "%i:%02i", minutes,
2224 int days = 0, hours = 0, minutes =
2226 int tmp = cur->mpd.length;
2227 while (tmp >= 86400) {
2231 while (tmp >= 3600) {
2242 "%i days %i:%i:%02i",
2243 days, hours, minutes,
2246 snprintf(p, n, "%i:%i:%02i", hours,
2249 snprintf(p, n, "%i:%02i", minutes,
2253 snprintf(p, n, "%2.0f",
2254 cur->mpd.progress * 100);
2257 new_bar(p, obj->data.pair.a,
2259 (int) (cur->mpd.progress *
2264 if (obj->data.top.type == TOP_NAME
2265 && obj->data.top.num >= 0
2266 && obj->data.top.num < 10) {
2267 // if we limit the buffer and add a bunch of space after, it stops the thing from
2268 // moving other shit around, which is really fucking annoying
2271 cur->cpu[obj->data.top.
2273 } else if (obj->data.top.type == TOP_CPU
2274 && obj->data.top.num >= 0
2275 && obj->data.top.num < 10) {
2276 snprintf(p, 7, "%3.2f ",
2277 cur->cpu[obj->data.top.
2279 } else if (obj->data.top.type == TOP_PID
2280 && obj->data.top.num >= 0
2281 && obj->data.top.num < 10) {
2282 snprintf(p, 8, "%i ",
2283 cur->cpu[obj->data.top.
2285 } else if (obj->data.top.type == TOP_MEM
2286 && obj->data.top.num >= 0
2287 && obj->data.top.num < 10) {
2288 snprintf(p, 7, "%3.2f ",
2289 cur->cpu[obj->data.top.
2294 if (obj->data.top.type == TOP_NAME
2295 && obj->data.top.num >= 0
2296 && obj->data.top.num < 10) {
2297 // if we limit the buffer and add a bunch of space after, it stops the thing from
2298 // moving other shit around, which is really fucking annoying
2301 cur->memu[obj->data.top.
2303 } else if (obj->data.top.type == TOP_CPU
2304 && obj->data.top.num >= 0
2305 && obj->data.top.num < 10) {
2306 snprintf(p, 7, "%3.2f ",
2307 cur->memu[obj->data.top.
2309 } else if (obj->data.top.type == TOP_PID
2310 && obj->data.top.num >= 0
2311 && obj->data.top.num < 10) {
2312 snprintf(p, 8, "%i ",
2313 cur->memu[obj->data.top.
2315 } else if (obj->data.top.type == TOP_MEM
2316 && obj->data.top.num >= 0
2317 && obj->data.top.num < 10) {
2318 snprintf(p, 7, "%3.2f ",
2319 cur->memu[obj->data.top.
2327 * I'm tired of everything being packed in
2334 if (current_update_time -obj->data.tail.last_update < obj->data.tail.interval) {
2335 snprintf(p, n, "%s", obj->data.tail.buffer);
2337 obj->data.tail.last_update = current_update_time;
2340 tailstring *head = NULL;
2341 tailstring *headtmp = NULL;
2342 fp = fopen(obj->data.tail.logfile, "rt");
2344 ERR("tail logfile failed to open");
2346 obj->data.tail.readlines = 0;
2348 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL) {
2349 addtail(&head, obj->data.tail.buffer);
2350 obj->data.tail.readlines++;
2355 if (obj->data.tail.readlines > 0) {
2356 for (i = 0;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
2357 addtail(&headtmp, head->data);
2360 strcpy(obj->data.tail.buffer, headtmp->data);
2361 headtmp = headtmp->next;
2362 for (i = 1;i < obj->data.tail.wantedlines + 1 && i < obj->data.tail.readlines; i++) {
2364 strncat(obj->data.tail.buffer, headtmp->data, TEXT_BUFFER_SIZE * 4 / obj->data.tail.wantedlines);
2365 headtmp = headtmp->next;
2369 /* get rid of any ugly newlines at the end */
2370 if (obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] == '\n') {
2371 obj->data.tail.buffer[strlen(obj->data.tail.buffer)-1] = '\0';
2373 snprintf(p, n, "%s", obj->data.tail.buffer);
2378 strcpy(obj->data.tail.buffer, "Logfile Empty");
2379 snprintf(p, n, "Logfile Empty");
2388 // Hmm, it's expensive to calculate this shit every time FIXME
2389 OBJ(metar_ob_time) {
2390 if (data.ob_hour != INT_MAX
2391 && data.ob_minute != INT_MAX
2393 format_seconds(p, n,
2399 format_seconds(p, n, 0);
2402 if (data.temp != INT_MAX && metar_worked)
2403 snprintf(p, n, "%i", data.temp);
2405 snprintf(p, n, "-");
2408 if (data.temp != INT_MAX && metar_worked)
2409 snprintf(p, n, "%3.1f",
2411 40) * 9.0 / 5 - 40);
2413 snprintf(p, n, "-");
2415 OBJ(metar_windchill) {
2416 if (data.temp != INT_MAX
2417 && data.winData.windSpeed != INT_MAX
2419 snprintf(p, n, "%i",
2420 calculateWindChill(data.
2426 snprintf(p, n, "-");
2428 OBJ(metar_dew_point) {
2429 if (data.dew_pt_temp != INT_MAX
2431 snprintf(p, n, "%i",
2434 snprintf(p, n, "-");
2437 if (data.temp != INT_MAX
2438 && data.dew_pt_temp != INT_MAX
2440 snprintf(p, n, "%i",
2441 calculateRelativeHumidity
2445 snprintf(p, n, "-");
2448 OBJ(metar_windspeed) {
2449 if (data.winData.windSpeed != INT_MAX
2451 snprintf(p, n, "%i",
2452 knTokph(data.winData.
2455 snprintf(p, n, "-");
2457 OBJ(metar_winddir) {
2458 if (data.winData.windDir != INT_MAX
2460 snprintf(p, n, "%s",
2461 calculateWindDirectionString
2462 (data.winData.windDir));
2464 snprintf(p, n, "-");
2466 OBJ(metar_swinddir) {
2467 if (data.winData.windDir != INT_MAX
2469 snprintf(p, n, "%s",
2470 calculateShortWindDirectionString
2471 (data.winData.windDir));
2473 snprintf(p, n, "-");
2477 if (data.cldTypHgt[0].cloud_type[0] != '\0'
2480 (&data.cldTypHgt[0].
2481 cloud_type[0], "SKC") == 0)
2485 (&data.cldTypHgt[0].
2491 (&data.cldTypHgt[0].
2497 (&data.cldTypHgt[0].
2501 "Scattered clouds");
2503 (&data.cldTypHgt[0].
2509 (&data.cldTypHgt[0].
2512 snprintf(p, n, "Overcast");
2517 snprintf(p, n, "Checking...");
2519 OBJ(metar_u2d_time) {
2520 format_seconds(p, n,
2521 (int) last_metar_update %
2522 (3600 * 24) + 3600);
2530 unsigned int a = strlen(p);
2536 if (stuff_in_upper_case) {
2546 last_update_time = current_update_time;
2552 static void set_font()
2556 if (window.xftdraw != NULL)
2557 XftDrawDestroy(window.xftdraw);
2558 window.xftdraw = XftDrawCreate(display, window.drawable,
2559 DefaultVisual(display,
2561 DefaultColormap(display,
2567 XSetFont(display, window.gc, fonts[selected_font].font->fid);
2576 static int text_start_x, text_start_y; /* text start position in window */
2577 static int text_width, text_height;
2579 static inline int get_string_width(const char *s)
2581 return *s ? calc_text_width(s, strlen(s)) : 0;
2586 static void text_size_updater(char *s)
2590 int h = font_height();
2591 /* get string widths and skip specials */
2594 if (*p == SPECIAL_CHAR) {
2596 w += get_string_width(s);
2599 if (specials[special_index].type == BAR
2600 || specials[special_index].type == GRAPH) {
2601 w += specials[special_index].width;
2602 if (specials[special_index].height > h) {
2603 h = specials[special_index].height;
2608 else if (specials[special_index].type == OFFSET) {
2609 w += specials[special_index].arg + get_string_width("a"); /* filthy, but works */
2612 else if (specials[special_index].type == FONT) {
2613 fontchange = specials[special_index].font_added;
2614 selected_font = specials[special_index].font_added;
2624 w += get_string_width(s);
2634 static void update_text_area()
2638 /* update text size if it isn't fixed */
2643 text_width = minimum_width;
2646 for_each_line(text_buffer, text_size_updater);
2648 if (text_height < minimum_height)
2649 text_height = minimum_height;
2652 /* get text position on workarea */
2653 switch (text_alignment) {
2660 x = workarea[2] - text_width - gap_x;
2667 y = workarea[3] - text_height - gap_y;
2671 x = workarea[2] - text_width - gap_x;
2672 y = workarea[3] - text_height - gap_y;
2680 text_start_x = border_margin + 1;
2681 text_start_y = border_margin + 1;
2682 window.x = x - border_margin - 1;
2683 window.y = y - border_margin - 1;
2687 /* If window size doesn't match to workarea's size, then window
2688 * probably includes panels (gnome).
2689 * Blah, doesn't work on KDE. */
2690 if (workarea[2] != window.width
2691 || workarea[3] != window.height) {
2705 static int cur_x, cur_y; /* current x and y for drawing */
2706 static int draw_mode; /* FG, BG or OUTLINE */
2707 static long current_color;
2709 static inline void set_foreground_color(long c)
2712 XSetForeground(display, window.gc, c);
2715 static void draw_string(const char *s)
2719 int i, i2, pos, width_of_s;
2721 width_of_s = get_string_width(s);
2722 if (out_to_console) {
2725 strcpy(tmpstring1, s);
2729 snprintf(space, 2, " ");
2730 max = ((text_width - width_of_s) / get_string_width(space));
2732 * This code looks for tabs in the text and coverts them to spaces.
2733 * The trick is getting the correct number of spaces,
2734 * and not going over the window's size without forcing
2735 * the window larger.
2737 for (i = 0; i < TEXT_BUFFER_SIZE; i++) {
2738 if (tmpstring1[i] == '\t') // 9 is ascii tab
2742 i2 < (8 - (1 + pos) % 8) && added <= max;
2744 tmpstring2[pos + i2] = ' ';
2749 if (tmpstring1[i] != 9) {
2750 tmpstring2[pos] = tmpstring1[i];
2760 c.pixel = current_color;
2761 XQueryColor(display, DefaultColormap(display, screen), &c);
2764 c2.color.red = c.red;
2765 c2.color.green = c.green;
2766 c2.color.blue = c.blue;
2767 c2.color.alpha = fonts[selected_font].font_alpha;
2769 XftDrawStringUtf8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2770 cur_x, cur_y, (XftChar8 *) s,
2773 XftDrawString8(window.xftdraw, &c2, fonts[selected_font].xftfont,
2774 cur_x, cur_y, (XftChar8 *) s,
2780 XDrawString(display, window.drawable, window.gc,
2781 cur_x, cur_y, s, strlen(s));
2783 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
2784 cur_x += width_of_s;
2787 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 */
2789 int red1, green1, blue1; // first colour
2790 int red2, green2, blue2; // second colour
2791 int red3 = 0, green3 = 0, blue3 = 0; // difference
2792 red1 = (first_colour & 0xff0000) >> 16;
2793 green1 = (first_colour & 0xff00) >> 8;
2794 blue1 = first_colour & 0xff;
2795 red2 = (last_colour & 0xff0000) >> 16;
2796 green2 = (last_colour & 0xff00) >> 8;
2797 blue2 = last_colour & 0xff;
2804 if (green1 > green2) {
2807 if (green1 < green2) {
2810 if (blue1 > blue2) {
2813 if (blue1 < blue2) {
2831 if (green1 > 0xff) {
2837 tmp_color = (red1 << 16) | (green1 << 8) | blue1;
2841 inline unsigned long gradient_max(unsigned long first_colour, unsigned long last_colour) { /* this function returns the max diff for a gradient */
2842 int red1, green1, blue1; // first colour
2843 int red2, green2, blue2; // second colour
2844 int red3 = 0, green3 = 0, blue3 = 0; // difference
2845 red1 = (first_colour & 0xff0000) >> 16;
2846 green1 = (first_colour & 0xff00) >> 8;
2847 blue1 = first_colour & 0xff;
2848 red2 = (last_colour & 0xff0000) >> 16;
2849 green2 = (last_colour & 0xff00) >> 8;
2850 blue2 = last_colour & 0xff;
2851 red3 = abs(red1 - red2);
2852 green3 = abs(green1 - green2);
2853 blue3 = abs(blue1 - blue2);
2863 static void draw_line(char *s)
2867 cur_x = text_start_x;
2868 cur_y += font_ascent();
2870 short font_h = font_height();
2872 /* find specials and draw stuff */
2875 if (*p == SPECIAL_CHAR) {
2878 /* draw string before special */
2885 switch (specials[special_index].type) {
2886 case HORIZONTAL_LINE:
2889 specials[special_index].height;
2890 int mid = font_ascent() / 2;
2891 w = text_start_x + text_width -
2894 XSetLineAttributes(display,
2899 XDrawLine(display, window.drawable,
2910 specials[special_index].height;
2912 specials[special_index].arg;
2913 int mid = font_ascent() / 2;
2914 char ss[2] = { s, s };
2915 w = text_start_x + text_width -
2918 XSetLineAttributes(display,
2923 XSetDashes(display, window.gc, 0,
2925 XDrawLine(display, window.drawable,
2936 specials[special_index].height;
2938 specials[special_index].arg;
2940 cur_y - (font_ascent() +
2942 w = specials[special_index].width;
2945 text_width - cur_x - 1;
2949 XSetLineAttributes(display,
2955 XDrawRectangle(display,
2959 XFillRectangle(display,
2963 w * bar_usage / 255,
2965 if (specials[special_index].
2967 && specials[special_index].
2971 [special_index].height;
2979 specials[special_index].height;
2983 by = cur_y - (font_ascent() +
2988 by = cur_y - (font_ascent()/2);
2990 w = specials[special_index].width;
2992 w = text_start_x + text_width - cur_x - 1;
2995 XSetLineAttributes(display,
3000 XDrawRectangle(display,
3004 XSetLineAttributes(display,
3011 int gradient_size = 0;
3012 float gradient_factor = 0;
3013 float gradient_update = 0;
3014 unsigned int tmpcolour = current_color;
3015 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3016 tmpcolour = specials[special_index].first_colour;
3017 gradient_size = gradient_max(specials[special_index].first_colour, specials[special_index].last_colour);
3018 gradient_factor = (float)gradient_size / (w - 3);
3020 for (i = 0; i < w - 3; i++) {
3021 if (specials[special_index].first_colour != specials[special_index].last_colour) {
3022 XSetForeground(display, window.gc, tmpcolour);
3023 gradient_update += gradient_factor;
3024 while (gradient_update > 0) {
3025 tmpcolour = do_gradient(tmpcolour, specials[special_index].last_colour);
3033 graph_width)) > j) {
3036 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 */
3038 if (specials[special_index].
3040 && specials[special_index].
3044 [special_index].height;
3047 if (draw_mode == BG) {
3048 set_foreground_color(default_bg_color);
3050 else if (draw_mode == OUTLINE) {
3051 set_foreground_color(default_out_color);
3053 set_foreground_color(default_fg_color);
3059 cur_y -= font_ascent();
3060 selected_font = specials[special_index].font_added;
3061 cur_y += font_ascent();
3072 if (draw_mode == FG)
3073 set_foreground_color(specials
3079 if (draw_mode == BG)
3080 set_foreground_color(specials
3086 if (draw_mode == OUTLINE)
3087 set_foreground_color(specials
3094 w = text_start_x + specials[special_index].arg;
3101 text_start_x + text_width -
3103 get_string_width(p);
3105 specials[special_index].arg)
3108 [special_index].arg;
3114 int pos_x = (text_start_x*2) + text_width - cur_x - ((float)get_string_width(p) / 2) - ((float)text_width / 2);
3116 specials[special_index].arg)
3119 [special_index].arg;
3132 if (cur_y_add > 0) {
3134 cur_y -= font_descent();
3139 cur_y += font_descent();
3145 static void draw_text()
3147 cur_y = text_start_y;
3150 if (draw_borders && border_width > 0) {
3151 unsigned int b = (border_width + 1) / 2;
3153 if (stippled_borders) {
3155 { stippled_borders, stippled_borders };
3156 XSetLineAttributes(display, window.gc,
3157 border_width, LineOnOffDash,
3158 CapButt, JoinMiter);
3159 XSetDashes(display, window.gc, 0, ss, 2);
3161 XSetLineAttributes(display, window.gc,
3162 border_width, LineSolid,
3163 CapButt, JoinMiter);
3166 XDrawRectangle(display, window.drawable, window.gc,
3167 text_start_x - border_margin + b,
3168 text_start_y - border_margin + b,
3169 text_width + border_margin * 2 - 1 - b * 2,
3170 text_height + border_margin * 2 - 1 -
3176 for_each_line(text_buffer, draw_line);
3179 static void draw_stuff()
3181 if (draw_shades && !draw_outline) {
3184 set_foreground_color(default_bg_color);
3193 for (i = -1; i < 2; i++)
3194 for (j = -1; j < 2; j++) {
3195 if (i == 0 && j == 0)
3199 set_foreground_color(default_out_color);
3200 draw_mode = OUTLINE;
3207 set_foreground_color(default_fg_color);
3214 swap.swap_window = window.window;
3215 swap.swap_action = XdbeBackground;
3216 XdbeSwapBuffers(display, &swap, 1);
3219 /*#ifdef METAR wtf is this for exactly? aside from trying to cause segfaults?
3220 if (metar_station != NULL) {
3221 free(metar_station);
3222 metar_station = NULL;
3224 if (metar_server != NULL) {
3226 metar_server = NULL;
3228 if (metar_path != NULL) {
3236 static void clear_text(int exposures)
3240 return; /* The swap action is XdbeBackground, which clears */
3242 /* there is some extra space for borders and outlines */
3243 XClearArea(display, window.drawable,
3244 text_start_x - border_margin - 1,
3245 text_start_y - border_margin - 1,
3246 text_width + border_margin * 2 + 2,
3247 text_height + border_margin * 2 + 2,
3248 exposures ? True : 0);
3251 static int need_to_update;
3253 /* update_text() generates new text and clears old text area */
3254 static void update_text()
3261 static void main_loop()
3263 Region region = XCreateRegion();
3265 while (total_run_times == 0 || info.looped < total_run_times - 1) {
3269 /* wait for X event or timeout */
3271 if (!XPending(display)) {
3276 update_interval - (get_time() -
3282 tv.tv_sec = (long) t;
3283 tv.tv_usec = (long) (t * 1000000) % 1000000;
3286 FD_SET(ConnectionNumber(display), &fdsr);
3288 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
3292 ERR("can't select(): %s",
3301 if (need_to_update) {
3303 int wx = window.x, wy = window.y;
3312 /* resize window if it isn't right size */
3314 (text_width + border_margin * 2 !=
3316 || text_height + border_margin * 2 !=
3320 border_margin * 2 + 1;
3323 border_margin * 2 + 1;
3324 XResizeWindow(display,
3330 /* move window if it isn't in right position */
3333 || window.y != wy)) {
3334 XMoveWindow(display, window.window,
3335 window.x, window.y);
3345 r.x = text_start_x - border_margin;
3346 r.y = text_start_y - border_margin;
3347 r.width = text_width + border_margin * 2;
3348 r.height = text_height + border_margin * 2;
3349 XUnionRectWithRegion(&r, region, region);
3354 /* handle X events */
3356 while (XPending(display)) {
3358 XNextEvent(display, &ev);
3366 r.width = ev.xexpose.width;
3367 r.height = ev.xexpose.height;
3368 XUnionRectWithRegion(&r, region,
3374 case ReparentNotify:
3375 /* set background to ParentRelative for all parents */
3377 set_transparent_background(window.
3381 case ConfigureNotify:
3383 /* if window size isn't what expected, set fixed size */
3384 if (ev.xconfigure.width !=
3386 || ev.xconfigure.height !=
3388 if (window.width != 0
3389 && window.height != 0)
3392 /* clear old stuff before screwing up size and pos */
3398 if (XGetWindowAttributes(display, window.window, &attrs)) {
3413 border_margin * 2 - 1;
3416 border_margin * 2 - 1;
3419 /* if position isn't what expected, set fixed pos, total_updates
3420 * avoids setting fixed_pos when window is set to weird locations
3422 if (total_updates >= 2
3424 && (window.x != ev.xconfigure.x
3427 && (ev.xconfigure.x != 0
3428 || ev.xconfigure.y != 0)) {
3440 /* XDBE doesn't seem to provide a way to clear the back buffer without
3441 * interfering with the front buffer, other than passing XdbeBackground
3442 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
3443 * redraw the text even if it wasn't part of the exposed area. OTOH,
3444 * if we're not going to call draw_stuff at all, then no swap happens
3445 * and we can safely do nothing.
3448 if (!XEmptyRegion(region)) {
3452 r.x = text_start_x - border_margin;
3453 r.y = text_start_y - border_margin;
3454 r.width = text_width + border_margin * 2;
3455 r.height = text_height + border_margin * 2;
3456 XUnionRectWithRegion(&r, region, region);
3459 XSetRegion(display, window.gc, region);
3462 XftDrawSetClip(window.xftdraw, region);
3465 XDestroyRegion(region);
3466 region = XCreateRegion();
3471 static void load_config_file(const char *);
3473 /* signal handler that reloads config file */
3474 static void reload_handler(int a)
3476 ERR("Conky: received signal %d, reloading config\n", a);
3478 if (current_config) {
3480 load_config_file(current_config);
3483 extract_variable_text(text);
3490 static void clean_up()
3494 XdbeDeallocateBackBufferName(display, window.back_buffer);
3498 XDestroyWindow(display, window.window);
3506 XFreeGC(display, window.gc);
3508 /* it is really pointless to free() memory at the end of program but ak|ra
3509 * wants me to do this */
3511 free_text_objects();
3513 if (text != original_text)
3516 free(current_config);
3517 free(current_mail_spool);
3523 static void term_handler(int a)
3525 a = a; /* to get rid of warning */
3530 static int string_to_bool(const char *s)
3534 if (strcasecmp(s, "yes") == 0)
3536 if (strcasecmp(s, "true") == 0)
3538 if (strcasecmp(s, "1") == 0)
3543 static enum alignment string_to_alignment(const char *s)
3545 if (strcasecmp(s, "top_left") == 0)
3547 else if (strcasecmp(s, "top_right") == 0)
3549 else if (strcasecmp(s, "bottom_left") == 0)
3551 else if (strcasecmp(s, "bottom_right") == 0)
3552 return BOTTOM_RIGHT;
3553 else if (strcasecmp(s, "tl") == 0)
3555 else if (strcasecmp(s, "tr") == 0)
3557 else if (strcasecmp(s, "bl") == 0)
3559 else if (strcasecmp(s, "br") == 0)
3560 return BOTTOM_RIGHT;
3565 static void set_default_configurations(void)
3567 text_alignment = BOTTOM_LEFT;
3568 fork_to_background = 0;
3571 total_run_times = 0;
3572 info.cpu_avg_samples = 2;
3573 info.net_avg_samples = 2;
3578 strcpy(info.mpd.host, "localhost");
3579 info.mpd.port = 6600;
3580 info.mpd.status = "Checking status...";
3584 default_fg_color = WhitePixel(display, screen);
3585 default_bg_color = BlackPixel(display, screen);
3586 default_out_color = BlackPixel(display, screen);
3592 set_first_font("courier-12");
3595 set_first_font("6x10");
3599 free(current_mail_spool);
3602 variable_substitute(MAIL_FILE, buf, 256);
3604 current_mail_spool = strdup(buf);
3613 stippled_borders = 0;
3614 update_interval = 10.0;
3615 stuff_in_upper_case = 0;
3617 mlconfig.mldonkey_hostname = "127.0.0.1";
3618 mlconfig.mldonkey_port = 4001;
3619 mlconfig.mldonkey_login = NULL;
3620 mlconfig.mldonkey_password = NULL;
3623 metar_station = NULL;
3624 metar_server = NULL;
3626 last_metar_update = 0;
3630 static void load_config_file(const char *f)
3632 #define CONF_ERR ERR("%s: %d: config file error", f, line)
3636 set_default_configurations();
3638 fp = open_file(f, 0);
3643 char buf[256], *p, *p2, *name, *value;
3645 if (fgets(buf, 256, fp) == NULL)
3650 /* break at comment */
3651 p2 = strchr(p, '#');
3656 while (*p && isspace((int) *p))
3659 continue; /* empty line */
3665 while (*p2 && !isspace((int) *p2))
3668 *p2 = '\0'; /* break at name's end */
3675 while (*p && isspace((int) *p))
3680 p2 = value + strlen(value);
3681 while (isspace((int) *(p2 - 1)))
3687 #define CONF2(a) if (strcasecmp(name, a) == 0)
3688 #define CONF(a) else CONF2(a)
3689 #define CONF3(a,b) \
3690 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
3693 CONF2("alignment") {
3695 int a = string_to_alignment(value);
3703 CONF("background") {
3704 fork_to_background = string_to_bool(value);
3706 CONF("border_margin") {
3708 border_margin = strtol(value, 0, 0);
3712 CONF("border_width") {
3714 border_width = strtol(value, 0, 0);
3718 CONF("default_color") {
3720 default_fg_color = get_x11_color(value);
3724 CONF3("default_shade_color", "default_shadecolor") {
3726 default_bg_color = get_x11_color(value);
3730 CONF3("default_outline_color", "default_outlinecolor") {
3732 default_out_color = get_x11_color(value);
3739 strcpy(info.mpd.host, value);
3745 info.mpd.port = strtol(value, 0, 0);
3746 if (info.mpd.port < 1
3747 || info.mpd.port > 0xffff)
3752 CONF("cpu_avg_samples") {
3754 cpu_avg_samples = strtol(value, 0, 0);
3755 if (cpu_avg_samples < 1
3756 || cpu_avg_samples > 14)
3765 CONF("net_avg_samples") {
3767 net_avg_samples = strtol(value, 0, 0);
3768 if (net_avg_samples < 1
3769 || net_avg_samples > 14)
3784 CONF("override_utf8_locale") {
3785 utf8_mode = string_to_bool(value);
3788 CONF("double_buffer") {
3789 use_xdbe = string_to_bool(value);
3792 CONF("draw_borders") {
3793 draw_borders = string_to_bool(value);
3795 CONF("draw_shades") {
3796 draw_shades = string_to_bool(value);
3798 CONF("draw_outline") {
3799 draw_outline = string_to_bool(value);
3801 CONF("out_to_console") {
3802 out_to_console = string_to_bool(value);
3804 CONF("use_spacer") {
3805 use_spacer = string_to_bool(value);
3809 use_xft = string_to_bool(value);
3814 set_first_font(value);
3820 if (value && font_count >= 0)
3821 fonts[0].font_alpha = atof(value)
3829 if (string_to_bool(value))
3830 ERR("Xft not enabled");
3833 /* xftfont silently ignored when no Xft */
3836 /* xftalpha is silently ignored when no Xft */
3841 set_first_font(value);
3847 gap_x = atoi(value);
3853 gap_y = atoi(value);
3857 CONF("mail_spool") {
3860 variable_substitute(value, buf, 256);
3864 if (current_mail_spool)
3865 free(current_mail_spool);
3866 current_mail_spool = strdup(buf);
3871 CONF("minimum_size") {
3874 (value, "%d %d", &minimum_width,
3875 &minimum_height) != 2)
3878 &minimum_width) != 1)
3883 CONF("no_buffers") {
3884 no_buffers = string_to_bool(value);
3887 CONF("mldonkey_hostname") {
3889 if (mlconfig.mldonkey_hostname != NULL) {
3890 free(mlconfig.mldonkey_hostname);
3892 mlconfig.mldonkey_hostname = strdup(value);
3897 CONF("mldonkey_port") {
3899 mlconfig.mldonkey_port = atoi(value);
3903 CONF("mldonkey_login") {
3905 if (mlconfig.mldonkey_login != NULL) {
3906 free(mlconfig.mldonkey_login);
3908 mlconfig.mldonkey_login = strdup(value);
3913 CONF("mldonkey_password") {
3915 if (mlconfig.mldonkey_password != NULL) {
3916 free(mlconfig.mldonkey_password);
3918 mlconfig.mldonkey_password = strdup(value);
3925 CONF("own_window") {
3926 own_window = string_to_bool(value);
3929 CONF("pad_percents") {
3930 pad_percents = atoi(value);
3932 CONF("stippled_borders") {
3934 stippled_borders = strtol(value, 0, 0);
3936 stippled_borders = 4;
3939 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
3942 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
3944 CONF("update_interval") {
3946 update_interval = strtod(value, 0);
3950 CONF("total_run_times") {
3952 total_run_times = strtod(value, 0);
3957 stuff_in_upper_case = string_to_bool(value);
3962 malloc(strlen(value)
3964 strcpy(seti_dir, value);
3968 CONF("metar_station") {
3969 metar_station = (char *)
3970 malloc(strlen(value)
3972 strcpy(metar_station, value);
3973 strcat(metar_station, ".TXT");
3975 CONF("metar_server") {
3976 metar_server = (char *)
3977 malloc(strlen(value)
3979 strcpy(metar_server, value);
3981 CONF("metar_path") {
3982 metar_path = (char *)
3983 malloc(strlen(value)
3985 strcpy(metar_path, value);
3990 if (text != original_text)
4000 int l = strlen(text);
4001 if (fgets(buf, 256, fp) == NULL)
4004 realloc(text, l + strlen(buf)
4008 if (strlen(text) > 1024 * 8)
4015 ERR("%s: %d: no such configuration: '%s'", f, line, name);
4025 /* : means that character before that takes an argument */
4029 *getopt_string = "vVdt:f:u:i:hc:w:x:y:a:"
4039 int main(int argc, char **argv)
4041 /* handle command line parameters that don't change configs */
4046 if (((s = getenv("LC_ALL")) && *s) || ((s = getenv("LC_CTYPE")) &&
4047 *s) || ((s = getenv("LANG")) && *s)) {
4049 for(x = 0; x < strlen(s) ; x++) {
4050 temp[x] = tolower(s[x]);
4052 if (strstr(temp, "utf-8") || strstr(temp, "utf8")) {
4056 if (!setlocale(LC_CTYPE, "")) {
4057 ERR("Can't set the specified locale!\nCheck LANG, LC_CTYPE, LC_ALL.");
4061 int c = getopt(argc,
4071 ("Conky " VERSION " compiled " __DATE__ "\n");
4075 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
4076 * though free() does the NULL check itself;), then load optarg value */
4078 free(current_config);
4079 current_config = strdup(optarg);
4084 ("Usage: %s [OPTION]...\n"
4085 "Conky is a system monitor that renders text on desktop or to own transparent\n"
4086 "window. Command line options will override configurations defined in config\n"
4089 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
4090 " -c FILE config file to load instead of "
4093 " -d daemonize, fork to background\n"
4094 " -f FONT font to use\n"
4097 " -o create own window to draw\n"
4100 " -b double buffer (prevents flickering)\n"
4102 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
4103 " -u SECS update interval\n"
4104 " -i NUM number of times to update Conky\n"
4105 " -w WIN_ID window id to draw\n"
4106 " -x X x position\n"
4107 " -y Y y position\n", argv[0]);
4111 window.window = strtol(optarg, 0, 0);
4118 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
4121 tmpstring1 = (char *)
4122 malloc(TEXT_BUFFER_SIZE);
4123 tmpstring2 = (char *)
4124 malloc(TEXT_BUFFER_SIZE);
4126 /* load current_config or CONFIG_FILE */
4129 if (current_config == NULL) {
4130 /* load default config file */
4133 variable_substitute(CONFIG_FILE, buf, 256);
4136 current_config = strdup(buf);
4140 if (current_config != NULL)
4141 load_config_file(current_config);
4143 set_default_configurations();
4146 if (current_mail_spool == NULL) {
4148 variable_substitute(MAIL_FILE, buf, 256);
4151 current_mail_spool = strdup(buf);
4155 /* handle other command line arguments */
4160 int c = getopt(argc,
4168 text_alignment = string_to_alignment(optarg);
4172 fork_to_background = 1;
4176 set_first_font(optarg);
4191 if (text != original_text)
4193 text = strdup(optarg);
4194 convert_escapes(text);
4198 update_interval = strtod(optarg, 0);
4202 total_run_times = strtod(optarg, 0);
4206 gap_x = atoi(optarg);
4210 gap_y = atoi(optarg);
4221 /* generate text and get initial size */
4222 extract_variable_text(text);
4223 if (text != original_text) {
4231 update_text_area(); /* to get initial size of the window */
4236 + border_margin * 2 + 1, text_height + border_margin * 2 + 1);
4238 update_text_area(); /* to position text/window on screen */
4241 // why the fuck not?
4247 XMoveWindow(display, window.window, window.x, window.y);
4257 if (fork_to_background) {
4261 ERR("can't fork() to background: %s",
4271 "Conky: forked to background, pid is %d\n",
4277 /* set SIGUSR1, SIGINT and SIGTERM handlers */
4282 sa.sa_handler = reload_handler;
4283 sigemptyset(&sa.sa_mask);
4284 sa.sa_flags = SA_RESTART;
4285 if (sigaction(SIGUSR1, &sa, NULL) != 0)
4286 ERR("can't set signal handler for SIGUSR1: %s",
4289 sa.sa_handler = term_handler;
4290 sigemptyset(&sa.sa_mask);
4291 sa.sa_flags = SA_RESTART;
4292 if (sigaction(SIGINT, &sa, NULL) != 0)
4293 ERR("can't set signal handler for SIGINT: %s",
4296 sa.sa_handler = term_handler;
4297 sigemptyset(&sa.sa_mask);
4298 sa.sa_flags = SA_RESTART;
4299 if (sigaction(SIGTERM, &sa, NULL) != 0)
4300 ERR("can't set signal handler for SIGTERM: %s",