1 /* Conky, a system monitor, based on torsmo
3 * This program is licensed under BSD license, read COPYING
23 #include <X11/Xutil.h>
24 #include <sys/types.h>
27 #define CONFIG_FILE "$HOME/.conkyrc"
28 #define MAIL_FILE "$MAIL"
29 #define MAX_IF_BLOCK_DEPTH 5
39 /* default config file */
40 static char *current_config;
42 /* set to 1 if you want all text to be in uppercase */
43 static unsigned int stuff_in_upper_case;
45 /* Position on the screen */
46 static int text_alignment;
47 static int gap_x, gap_y;
50 static char *font_name;
53 static double update_interval;
55 /* Run how many times? */
56 static unsigned long total_run_times;
59 static int fork_to_background;
62 static int draw_borders;
63 static int stippled_borders;
65 static int draw_shades, draw_outline;
67 static int border_margin, border_width;
69 static long default_fg_color, default_bg_color, default_out_color;
71 static int cpu_avg_samples, net_avg_samples;
74 /* create own window or draw stuff to root? */
75 static int own_window = 0;
77 /* fixed size/pos is set if wm/user changes them */
78 static int fixed_size = 0, fixed_pos = 0;
81 static int minimum_width, minimum_height;
83 /* no buffers in used memory? */
86 /* pad percentages to decimals? */
87 static int pad_percents = 0;
89 /* Text that is shown */
90 static char original_text[] =
91 "$nodename - $sysname $kernel on $machine\n"
93 "${color grey}Uptime:$color $uptime\n"
94 "${color grey}Frequency (in MHz):$color $freq\n"
95 "${color grey}RAM Usage:$color $mem/$memmax - $memperc% ${membar 4}\n"
96 "${color grey}Swap Usage:$color $swap/$swapmax - $swapperc% ${swapbar 4}\n"
97 "${color grey}CPU Usage:$color $cpu% ${cpubar 4}\n"
98 "${color grey}Processes:$color $processes ${color grey}Running:$color $running_processes\n"
100 "${color grey}File systems:\n"
101 " / $color${fs_free /}/${fs_size /} ${fs_bar 6 /}\n"
102 "${color grey}Networking:\n"
103 " Up:$color ${upspeed eth0} k/s${color grey} - Down:$color ${downspeed eth0} k/s\n"
104 "${color grey}Temperatures:\n"
105 " CPU:$color ${i2c temp 1}°C${color grey} - MB:$color ${i2c temp 2}°C\n"
108 "${color grey}SETI@Home Statistics:\n"
109 "${color grey}Seti Unit Number:$color $seti_credit\n"
110 "${color grey}Seti Progress:$color $seti_prog% $seti_progbar\n"
113 "${color grey}MPD: $mpd_status $mpd_artist - $mpd_title from $mpd_album at $mpd_vol\n"
114 "Bitrate: $mpd_bitrate\n"
115 "Progress: $mpd_bar\n"
117 "${color grey}Name PID CPU% MEM%\n"
118 " ${color lightgrey} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}\n"
119 " ${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}\n"
120 " ${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}\n"
121 " ${color lightgrey} ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}\n"
122 "${tail /var/log/Xorg.0.log 3}" ;
124 static char *text = original_text;
126 static int total_updates;
130 static XFontStruct *font;
133 static XftFont *xftfont;
134 static int font_alpha = 65535;
138 static int blockdepth = 0;
139 static int if_jumped = 0;
140 static int blockstart[MAX_IF_BLOCK_DEPTH];
142 int check_mount (char* s)
145 FILE* mtab = fopen("/etc/mtab","r");
147 char buf1[256],buf2[128];
148 while(fgets (buf1,256,mtab))
150 sscanf(buf1,"%*s %128s",buf2);
151 if(!strcmp(s,buf2)) { ret=1; break; }
154 } else { ERR("Could not open mtab"); }
160 static inline int calc_text_width(const char *s, unsigned int l)
165 XftTextExtents8(display, xftfont, s, l, &gi);
170 return XTextWidth(font, s, l);
176 #define font_height() use_xft ? (xftfont->ascent + xftfont->descent) : \
177 (font->max_bounds.ascent + font->max_bounds.descent)
178 #define font_ascent() use_xft ? xftfont->ascent : font->max_bounds.ascent
179 #define font_descent() use_xft ? xftfont->descent : font->max_bounds.descent
183 #define font_height() (font->max_bounds.ascent + font->max_bounds.descent)
184 #define font_ascent() font->max_bounds.ascent
185 #define font_descent() font->max_bounds.descent
189 /* formatted text to render on screen, generated in generate_text(),
190 * drawn in draw_stuff() */
192 static char text_buffer[TEXT_BUFFER_SIZE*4];
194 /* special stuff in text_buffer */
196 #define SPECIAL_CHAR '\x01'
209 static struct special_t {
216 static int special_count;
217 static int special_index; /* used when drawing */
219 static struct special_t *new_special(char *buf, int t)
221 if (special_count >= 128)
222 CRIT_ERR("too much special things in text");
224 buf[0] = SPECIAL_CHAR;
226 specials[special_count].type = t;
227 return &specials[special_count++];
230 typedef struct tailstring_list {
231 char data[TEXT_BUFFER_SIZE];
232 struct tailstring_list *next;
235 void addtail(tailstring **head, char *data_in) {
237 if ((tmp = malloc(sizeof(*tmp))) == NULL) {
240 strncpy(tmp->data, data_in, TEXT_BUFFER_SIZE);
245 void freetail(tailstring *head) {
248 while (head != NULL) {
256 static void new_bar(char *buf, int w, int h, int usage)
258 struct special_t *s = new_special(buf, BAR);
259 s->arg = (usage > 255) ? 255 : ((usage < 0) ? 0 : usage);
264 static const char *scan_bar(const char *args, int *w, int *h)
266 *w = 0; /* zero width means all space that is available */
268 /* bar's argument is either height or height,width */
271 if (sscanf(args, "%d,%d %n", h, w, &n) <= 1)
272 sscanf(args, "%d %n", h, &n);
279 static inline void new_hr(char *buf, int a)
281 new_special(buf, HORIZONTAL_LINE)->height = a;
284 static inline void new_stippled_hr(char *buf, int a, int b)
286 struct special_t *s = new_special(buf, STIPPLED_HR);
291 static inline void new_fg(char *buf, long c)
293 new_special(buf, FG)->arg = c;
296 static inline void new_bg(char *buf, long c)
298 new_special(buf, BG)->arg = c;
301 static inline void new_outline(char *buf, long c)
303 new_special(buf, OUTLINE)->arg = c;
306 static inline void new_alignr(char *buf, long c) {
307 new_special(buf, ALIGNR)->arg = c;
310 static inline void new_alignc(char *buf, long c) {
311 new_special(buf, ALIGNC)->arg = c;
314 /* quite boring functions */
316 static inline void for_each_line(char *b, void (*f) (char *))
320 for (ps = b, pe = b; *pe; pe++) {
333 static void convert_escapes(char *buf)
335 char *p = buf, *s = buf;
351 /* converts from bytes to human readable format (k, M, G) */
352 static void human_readable(long long a, char *buf, int size)
354 if (a >= 1024 * 1024 * 1024)
355 snprintf(buf, size, "%.2fG", (a / 1024 / 1024) / 1024.0);
356 else if (a >= 1024 * 1024) {
357 double m = (a / 1024) / 1024.0;
359 snprintf(buf, size, "%.0fM", m);
361 snprintf(buf, size, "%.1fM", m);
362 } else if (a >= 1024)
363 snprintf(buf, size, "%Ldk", a / (long long) 1024);
365 snprintf(buf, size, "%Ld", a);
370 enum text_object_type {
427 OBJ_ml_upload_counter,
428 OBJ_ml_download_counter,
429 OBJ_ml_nshared_files,
430 OBJ_ml_shared_counter,
431 OBJ_ml_tcp_upload_rate,
432 OBJ_ml_tcp_download_rate,
433 OBJ_ml_udp_upload_rate,
434 OBJ_ml_udp_download_rate,
435 OBJ_ml_ndownloaded_files,
436 OBJ_ml_ndownloading_files,
439 OBJ_running_processes,
448 OBJ_temp1, /* i2c is used instead in these */
494 char *s; /* some string */
495 int i; /* some integer */
496 long l; /* some other integer */
497 struct net_stat *net;
499 unsigned char loadavg[3];
532 char buffer[TEXT_BUFFER_SIZE*4];
550 static unsigned int text_object_count;
551 static struct text_object *text_objects;
553 /* new_text_object() allocates a new zeroed text_object */
554 static struct text_object *new_text_object()
557 text_objects = (struct text_object *) realloc(text_objects,
561 memset(&text_objects[text_object_count - 1], 0,
562 sizeof(struct text_object));
564 return &text_objects[text_object_count - 1];
567 static void free_text_objects()
571 for (i = 0; i < text_object_count; i++) {
572 switch (text_objects[i].type) {
574 close(text_objects[i].data.i);
578 close(text_objects[i].data.i2c.fd);
582 case OBJ_if_existing:
585 free(text_objects[i].data.ifblock.s);
599 free(text_objects[i].data.s);
603 free(text_objects[i].data.execi.cmd);
604 free(text_objects[i].data.execi.buffer);
611 text_object_count = 0;
614 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
619 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
620 *a = mixer_init(buf1);
621 (void) scan_bar(arg + n, w, h);
624 (void) scan_bar(arg, w, h);
628 /* construct_text_object() creates a new text_object */
629 static void construct_text_object(const char *s, const char *arg)
631 struct text_object *obj = new_text_object();
633 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
634 #define END ; } } else
637 obj->type = OBJ_color;
638 obj->data.l = get_x11_color(s);
640 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
641 END OBJ(acpiacadapter, 0)
642 END OBJ(freq, 0) END OBJ(acpifan, 0) END OBJ(battery,
645 sscanf(arg, "%63s", bat);
648 obj->data.s = strdup(bat);
649 END OBJ(buffers, INFO_BUFFERS)
650 END OBJ(cached, INFO_BUFFERS)
651 END OBJ(cpu, INFO_CPU)
652 END OBJ(cpubar, INFO_CPU)
653 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
654 END OBJ(color, 0) obj->data.l =
655 arg ? get_x11_color(arg) : default_fg_color;
656 END OBJ(downspeed, INFO_NET) obj->data.net = get_net_stat(arg);
657 END OBJ(downspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
661 text_objects[blockstart[blockdepth-1]-1].data.ifblock.pos = text_object_count;
662 blockstart[blockdepth-1] = text_object_count;
663 obj->data.ifblock.pos = text_object_count + 2;
664 } else { ERR("$else: no matching $if_*"); }
669 text_objects[blockstart[blockdepth]-1].data.ifblock.pos = text_object_count;
670 } else { ERR("$endif: no matching $if_*"); }
673 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
676 obj->data.s = strdup(arg ? arg : "");
678 OBJ(execi, 0) unsigned int n;
681 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
683 ERR("${execi <interval> command}");
684 obj->type = OBJ_text;
685 snprintf(buf, 256, "${%s}", s);
686 obj->data.s = strdup(buf);
688 obj->data.execi.cmd = strdup(arg + n);
689 obj->data.execi.buffer =
690 (char *) calloc(1, TEXT_BUFFER_SIZE);
692 END OBJ(pre_exec, 0) obj->type = OBJ_text;
694 FILE *fp = popen(arg, "r");
698 n = fread(buf, 1, 2048, fp);
701 if (n && buf[n - 1] == '\n')
706 obj->data.s = strdup(buf);
708 obj->data.s = strdup("");
711 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
712 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
714 while (isspace(*arg))
720 obj->data.fsbar.fs = prepare_fs_stat(arg);
721 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
724 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
728 obj->data.fsbar.fs = prepare_fs_stat(arg);
729 END OBJ(fs_free, INFO_FS) if (!arg)
731 obj->data.fs = prepare_fs_stat(arg);
732 END OBJ(fs_used_perc, INFO_FS) if (!arg)
734 obj->data.fs = prepare_fs_stat(arg);
735 END OBJ(fs_free_perc, INFO_FS) if (!arg)
737 obj->data.fs = prepare_fs_stat(arg);
738 END OBJ(fs_size, INFO_FS) if (!arg)
740 obj->data.fs = prepare_fs_stat(arg);
741 END OBJ(fs_used, INFO_FS) if (!arg)
743 obj->data.fs = prepare_fs_stat(arg);
744 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
745 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
749 ERR("i2c needs arguments");
750 obj->type = OBJ_text;
751 obj->data.s = strdup("${i2c}");
755 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
756 /* if scanf couldn't read three values, read type and num and use
758 sscanf(arg, "%63s %d", buf2, &n);
760 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
761 obj->data.i2c.devtype);
762 strcpy(obj->data.i2c.type, buf2);
765 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
766 obj->data.i2c.devtype);
767 strcpy(obj->data.i2c.type, buf2);
770 END OBJ(top, INFO_TOP)
774 ERR("top needs arguments");
775 obj->type = OBJ_text;
776 obj->data.s = strdup("${top}");
779 if(sscanf(arg, "%63s %i", buf, &n) == 2) {
780 if(strcmp(buf, "name") == 0) {
781 obj->data.top.type = TOP_NAME;
783 else if(strcmp(buf, "cpu") == 0) {
784 obj->data.top.type = TOP_CPU;
786 else if(strcmp(buf, "pid") == 0) {
787 obj->data.top.type = TOP_PID;
789 else if(strcmp(buf, "mem") == 0) {
790 obj->data.top.type = TOP_MEM;
794 ERR("invalid arg for top");
797 if(n < 1 || n > 10) {
798 CRIT_ERR("invalid arg for top");
802 obj->data.top.num = n-1;
807 ERR("invalid args given for top");
811 OBJ(top_mem, INFO_TOP)
815 ERR("top_mem needs arguments");
816 obj->type = OBJ_text;
817 obj->data.s = strdup("${top_mem}");
820 if(sscanf(arg, "%63s %i", buf, &n) == 2) {
821 if(strcmp(buf, "name") == 0) {
822 obj->data.top.type = TOP_NAME;
824 else if(strcmp(buf, "cpu") == 0) {
825 obj->data.top.type = TOP_CPU;
827 else if(strcmp(buf, "pid") == 0) {
828 obj->data.top.type = TOP_PID;
830 else if(strcmp(buf, "mem") == 0) {
831 obj->data.top.type = TOP_MEM;
835 ERR("invalid arg for top");
838 if(n < 1 || n > 10) {
839 CRIT_ERR("invalid arg for top");
843 obj->data.top.num = n-1;
848 ERR("invalid args given for top");
853 obj->data.net = get_net_stat(arg);
855 OBJ(linkstatus, INFO_WIFI)
856 obj->data.net = get_net_stat(arg);
862 ERR("tail needs arguments");
863 obj->type = OBJ_text;
864 obj->data.s = strdup("${tail}");
867 if(sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
868 if(n1 < 1 || n1 > 30) {
869 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
874 fp = fopen (buf,"rt");
876 obj->data.tail.logfile = malloc(TEXT_BUFFER_SIZE);
877 strcpy(obj->data.tail.logfile, buf);
878 obj->data.tail.wantedlines = n1-1;
879 obj->data.tail.interval = update_interval*2;
884 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
888 else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
889 if(n1 < 1 || n1 > 30) {
890 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
893 else if(n2 < 1 || n2 < update_interval) {
894 CRIT_ERR("invalid arg for tail, interval must be greater than 0 and Conky's interval");
899 fp = fopen (buf,"rt");
901 obj->data.tail.logfile = malloc(TEXT_BUFFER_SIZE);
902 strcpy(obj->data.tail.logfile, buf);
903 obj->data.tail.wantedlines = n1-1;
904 obj->data.tail.interval = n2;
909 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
915 ERR("invalid args given for tail");
919 OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
921 r = sscanf(arg, "%d %d %d", &a, &b, &c);
922 if (r >= 3 && (c < 1 || c > 3))
924 if (r >= 2 && (b < 1 || b > 3))
926 if (r >= 1 && (a < 1 || a > 3))
929 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
930 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
931 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
934 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
935 {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
937 ERR("if_existing needs an argument");
938 obj->data.ifblock.s = 0;
941 obj->data.ifblock.s = strdup(arg);
942 blockstart[blockdepth] = text_object_count;
943 obj->data.ifblock.pos = text_object_count + 2;
947 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
948 {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
950 ERR("if_mounted needs an argument");
951 obj->data.ifblock.s = 0;
954 obj->data.ifblock.s = strdup(arg);
955 blockstart[blockdepth] = text_object_count;
956 obj->data.ifblock.pos = text_object_count + 2;
960 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
961 {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
964 snprintf(buf,256,"pidof %s >/dev/null",arg);
965 obj->data.ifblock.s = strdup(buf);
967 ERR("if_running needs an argument");
968 obj->data.ifblock.s = 0;
970 blockstart[blockdepth] = text_object_count;
971 obj->data.ifblock.pos = text_object_count + 2;
976 END OBJ(mails, INFO_MAIL)
977 END OBJ(mem, INFO_MEM)
978 END OBJ(memmax, INFO_MEM)
979 END OBJ(memperc, INFO_MEM)
980 END OBJ(membar, INFO_MEM)
981 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
982 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
983 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
984 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
985 END OBJ(mixerbar, INFO_MIXER)
986 scan_mixer_bar(arg, &obj->data.mixerbar.l,
987 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
988 END OBJ(mixerlbar, INFO_MIXER)
989 scan_mixer_bar(arg, &obj->data.mixerbar.l,
990 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
991 END OBJ(mixerrbar, INFO_MIXER)
992 scan_mixer_bar(arg, &obj->data.mixerbar.l,
993 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
996 OBJ(ml_upload_counter, INFO_MLDONKEY)
998 OBJ(ml_download_counter, INFO_MLDONKEY)
1000 OBJ(ml_nshared_files, INFO_MLDONKEY)
1002 OBJ(ml_shared_counter, INFO_MLDONKEY)
1004 OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
1006 OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
1008 OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
1010 OBJ(ml_udp_download_rate, INFO_MLDONKEY)
1012 OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
1014 OBJ(ml_ndownloading_files, INFO_MLDONKEY)
1017 OBJ(new_mails, INFO_MAIL)
1018 END OBJ(nodename, 0)
1019 END OBJ(processes, INFO_PROCS)
1020 END OBJ(running_processes, INFO_RUN_PROCS)
1021 END OBJ(shadecolor, 0)
1022 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
1023 END OBJ(outlinecolor, 0)
1024 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
1025 END OBJ(stippled_hr, 0) int a = stippled_borders, b = 1;
1027 if (sscanf(arg, "%d %d", &a, &b) != 2)
1028 sscanf(arg, "%d", &b);
1032 obj->data.pair.a = a;
1033 obj->data.pair.b = b;
1034 END OBJ(swap, INFO_MEM)
1035 END OBJ(swapmax, INFO_MEM)
1036 END OBJ(swapperc, INFO_MEM)
1037 END OBJ(swapbar, INFO_MEM)
1038 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1039 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
1041 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
1042 obj->data.i2c.devtype);
1043 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1045 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1046 obj->data.i2c.devtype);
1047 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1048 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1049 END OBJ(totaldown, INFO_NET) obj->data.net = get_net_stat(arg);
1050 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1054 obj->data.i = arg ? atoi(arg) : 1;
1058 obj->data.i = arg ? atoi(arg) : 1;
1059 END OBJ(upspeed, INFO_NET) obj->data.net = get_net_stat(arg);
1060 END OBJ(upspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1061 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1068 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1069 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1070 END OBJ(seti_credit, INFO_SETI) END
1073 OBJ(metar_ob_time, INFO_METAR)
1075 OBJ(metar_temp, INFO_METAR)
1077 OBJ(metar_tempf, INFO_METAR)
1079 OBJ(metar_windchill, INFO_METAR)
1081 OBJ(metar_dew_point, INFO_METAR)
1083 OBJ(metar_rh, INFO_METAR)
1086 OBJ(metar_windspeed, INFO_METAR)
1088 OBJ(metar_winddir, INFO_METAR)
1090 OBJ(metar_swinddir, INFO_METAR)
1093 OBJ(metar_cloud, INFO_METAR)
1095 OBJ(metar_u2d_time, INFO_METAR)
1100 OBJ(mpd_artist, INFO_MPD)
1101 END OBJ(mpd_title, INFO_MPD)
1102 END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol, INFO_MPD) END OBJ(mpd_bitrate, INFO_MPD) END
1103 OBJ(mpd_status, INFO_MPD) END
1104 OBJ(mpd_bar, INFO_MPD)
1105 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1110 ERR("unknown variable %s", s);
1111 obj->type = OBJ_text;
1112 snprintf(buf, 256, "${%s}", s);
1113 obj->data.s = strdup(buf);
1118 /* append_text() appends text to last text_object if it's text, if it isn't
1119 * it creates a new text_object */
1120 static void append_text(const char *s)
1122 struct text_object *obj;
1124 if (s == NULL || *s == '\0')
1127 obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1129 /* create a new text object? */
1130 if (!obj || obj->type != OBJ_text) {
1131 obj = new_text_object();
1132 obj->type = OBJ_text;
1133 obj->data.s = strdup(s);
1136 obj->data.s = (char *) realloc(obj->data.s,
1137 strlen(obj->data.s) +
1139 strcat(obj->data.s, s);
1143 static void extract_variable_text(const char *p)
1147 free_text_objects();
1162 /* variable is either $foo or ${foo} */
1166 while (*p && *p != '}')
1172 while (*p && (isalnum((int) *p)
1177 /* copy variable to buffer */
1178 len = (p - s > 255) ? 255 : (p - s);
1179 strncpy(buf, s, len);
1188 /* if variable wasn't found from environment, use some special */
1194 if (strchr(buf, ' ')) {
1195 arg = strchr(buf, ' ');
1198 while (isspace((int) *arg))
1204 /* lowercase variable name */
1211 construct_text_object(buf, arg);
1221 if(blockdepth) ERR("one or more $endif's are missing");
1224 double current_update_time, last_update_time;
1226 static void generate_text()
1229 struct information *cur = &info;
1236 current_update_time = get_time();
1242 n = TEXT_BUFFER_SIZE*4 - 2;
1245 for (i = 0; i < text_object_count; i++) {
1246 struct text_object *obj = &text_objects[i];
1248 #define OBJ(a) break; case OBJ_##a:
1250 switch (obj->type) {
1253 ERR("not implemented obj type %d",
1257 /* does anyone have decimals in acpi temperature? */
1259 snprintf(p, n, "%d",
1260 (int) get_acpi_temperature(obj->
1264 snprintf(p, 5, "%d ",
1265 (int) get_acpi_temperature(obj->
1270 snprintf(p, n, "%s", get_freq());
1273 snprintf(p, n, "%s", get_adt746x_cpu());
1276 snprintf(p, n, "%s", get_adt746x_fan());
1279 snprintf(p, n, "%s", get_acpi_fan());
1281 OBJ(acpiacadapter) {
1282 snprintf(p, n, "%s",
1283 get_acpi_ac_adapter());
1286 get_battery_stuff(p, n, obj->data.s);
1289 human_readable(cur->buffers * 1024, p, 255);
1292 human_readable(cur->cached * 1024, p, 255);
1296 snprintf(p, n, "%*d", pad_percents,
1297 (int) (cur->cpu_usage * 100.0));
1299 snprintf(p, 4, "%*d ", pad_percents,
1300 (int) (cur->cpu_usage * 100.0));
1303 new_bar(p, obj->data.pair.a,
1305 (int) (cur->cpu_usage * 255.0));
1308 new_fg(p, obj->data.l);
1312 snprintf(p, n, "%d",
1313 (int) (obj->data.net->recv_speed /
1316 snprintf(p, 6, "%d ",
1317 (int) (obj->data.net->recv_speed /
1322 snprintf(p, n, "%.1f",
1323 obj->data.net->recv_speed /
1326 snprintf(p, 8, "%.1f ",
1327 obj->data.net->recv_speed /
1333 i=obj->data.ifblock.pos-2;
1347 FILE *fp = popen(obj->data.s, "r");
1348 int n2 = fread(p, 1, n, fp);
1352 if (n2 && p[n2 - 1] == '\n')
1363 FILE *fp = popen(obj->data.s, "r");
1364 int n2 = fread(p, 1, n, fp);
1368 if (n2 && p[n2 - 1] == '\n')
1371 snprintf(p, n, "%u.%u.%u.%u",
1372 obj->data.net->addr.sa_data[2] & 255,
1373 obj->data.net->addr.sa_data[3] & 255,
1374 obj->data.net->addr.sa_data[4] & 255,
1375 obj->data.net->addr.sa_data[5] & 255);
1379 snprintf(p, n, "%d", obj->data.net->linkstatus);
1387 if (sscanf(p, "%lf", &barnum) == 0)
1389 ERR("reading execbar value failed (perhaps it's not the correct format?)");
1391 if (barnum > 100 || barnum < 0)
1393 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
1397 barnum = barnum/100.0;
1400 (int) (barnum * 255.0));
1405 if (current_update_time -
1406 obj->data.execi.last_update <
1407 obj->data.execi.interval) {
1408 snprintf(p, n, "%s",
1409 obj->data.execi.buffer);
1411 char *p2 = obj->data.execi.buffer;
1413 popen(obj->data.execi.cmd,
1416 fread(p2, 1, TEXT_BUFFER_SIZE,
1421 if (n2 && p2[n2 - 1] == '\n')
1430 snprintf(p, n, "%s",
1431 obj->data.execi.buffer);
1433 obj->data.execi.last_update =
1434 current_update_time;
1439 if (obj->data.fs != NULL) {
1440 if (obj->data.fs->size == 0)
1459 if (obj->data.fs != NULL)
1460 human_readable(obj->data.fs->avail,
1464 if (obj->data.fs != NULL) {
1465 if (obj->data.fs->size)
1466 snprintf(p, n, "%*d", pad_percents, (int) ((obj->data.fs->avail *100) / obj->data.fs->size));
1468 snprintf(p, n, "0");
1472 if (obj->data.fs != NULL)
1473 human_readable(obj->data.fs->size,
1477 if (obj->data.fs != NULL)
1478 human_readable(obj->data.fs->size -
1479 obj->data.fs->avail,
1483 if (obj->data.fs != NULL) {
1484 if (obj->data.fs->size == 0)
1502 if (obj->data.fs != NULL) {
1503 if (obj->data.fs->size)
1504 snprintf(p, 4, "%d",
1514 snprintf(p, n, "0");
1518 float *v = info.loadavg;
1520 if (obj->data.loadavg[2])
1521 snprintf(p, n, "%.2f %.2f %.2f",
1522 v[obj->data.loadavg[0] -
1524 v[obj->data.loadavg[1] -
1526 v[obj->data.loadavg[2] -
1528 else if (obj->data.loadavg[1])
1529 snprintf(p, n, "%.2f %.2f",
1530 v[obj->data.loadavg[0] -
1532 v[obj->data.loadavg[1] -
1534 else if (obj->data.loadavg[0])
1535 snprintf(p, n, "%.2f",
1536 v[obj->data.loadavg[0] -
1540 new_hr(p, obj->data.i);
1545 r = get_i2c_info(&obj->data.i2c.fd,
1547 obj->data.i2c.devtype,
1548 obj->data.i2c.type);
1550 if (r >= 100.0 || r == 0)
1551 snprintf(p, n,"%d", (int) r);
1553 snprintf(p, n, "%.1f", r);
1556 new_alignr(p, obj->data.i);
1559 new_alignc(p, obj->data.i);
1563 if((obj->data.ifblock.s)&&(stat(obj->data.ifblock.s,&tmp) == -1)) {
1564 i=obj->data.ifblock.pos-2;
1566 } else if_jumped = 0;
1569 if((obj->data.ifblock.s)&&(!check_mount(obj->data.ifblock.s))) {
1570 i=obj->data.ifblock.pos-2;
1572 } else if_jumped = 0;
1575 if((obj->data.ifblock.s)&&system(obj->data.ifblock.s)) {
1576 i=obj->data.ifblock.pos-2;
1578 } else if_jumped = 0;
1581 snprintf(p, n, "%s", cur->uname_s.release);
1584 snprintf(p, n, "%s", cur->uname_s.machine);
1589 human_readable(cur->mem * 1024, p, 6);
1592 human_readable(cur->memmax * 1024, p, 255);
1598 snprintf(p, n, "%*d", pad_percents,
1602 snprintf(p, 4, "%*d ", pad_percents,
1608 new_bar(p, obj->data.pair.a,
1610 cur->memmax ? (cur->mem * 255) /
1616 snprintf(p, n, "%d",
1617 mixer_get_avg(obj->data.l));
1620 snprintf(p, n, "%d",
1621 mixer_get_left(obj->data.l));
1624 snprintf(p, n, "%d",
1625 mixer_get_right(obj->data.l));
1628 new_bar(p, obj->data.mixerbar.w,
1629 obj->data.mixerbar.h,
1630 mixer_get_avg(obj->data.mixerbar.
1634 new_bar(p, obj->data.mixerbar.w,
1635 obj->data.mixerbar.h,
1636 mixer_get_left(obj->data.mixerbar.
1640 new_bar(p, obj->data.mixerbar.w,
1641 obj->data.mixerbar.h,
1642 mixer_get_right(obj->data.mixerbar.
1648 snprintf(p, n, "%d", cur->mail_count);
1651 snprintf(p, n, "%d", cur->new_mail_count);
1654 OBJ(ml_upload_counter) {
1655 snprintf(p, n, "%lld", mlinfo.upload_counter/1048576);
1657 OBJ(ml_download_counter) {
1658 snprintf(p, n, "%lld", mlinfo.download_counter/1048576);
1660 OBJ(ml_nshared_files) {
1661 snprintf(p, n, "%i", mlinfo.nshared_files);
1663 OBJ(ml_shared_counter) {
1664 snprintf(p, n, "%lld", mlinfo.shared_counter/1048576);
1666 OBJ(ml_tcp_upload_rate) {
1667 snprintf(p, n, "%.2f", (float)mlinfo.tcp_upload_rate/1024);
1669 OBJ(ml_tcp_download_rate) {
1670 snprintf(p, n, "%.2f",(float)mlinfo.tcp_download_rate/1024);
1672 OBJ(ml_udp_upload_rate) {
1673 snprintf(p, n, "%.2f",(float)mlinfo.udp_upload_rate/1024);
1675 OBJ(ml_udp_download_rate) {
1676 snprintf(p, n, "%.2f",(float)mlinfo.udp_download_rate/1024);
1678 OBJ(ml_ndownloaded_files) {
1679 snprintf(p, n, "%i", mlinfo.ndownloaded_files);
1681 OBJ(ml_ndownloading_files) {
1682 snprintf(p, n, "%i", mlinfo.ndownloading_files);
1687 snprintf(p, n, "%s",
1688 cur->uname_s.nodename);
1691 new_outline(p, obj->data.l);
1695 snprintf(p, n, "%d", cur->procs);
1697 snprintf(p, 5, "%d ", cur->procs);
1699 OBJ(running_processes) {
1701 snprintf(p, n, "%d", cur->run_procs);
1703 snprintf(p, 3, "%d ", cur->run_procs);
1706 snprintf(p, n, "%s", obj->data.s);
1709 new_bg(p, obj->data.l);
1712 new_stippled_hr(p, obj->data.pair.a,
1716 human_readable(cur->swap * 1024, p, 255);
1719 human_readable(cur->swapmax * 1024, p, 255);
1722 if (cur->swapmax == 0) {
1723 strncpy(p, "No swap", 255);
1726 snprintf(p, 255, "%*u",
1731 snprintf(p, 4, "%*u ",
1738 new_bar(p, obj->data.pair.a,
1740 cur->swapmax ? (cur->swap * 255) /
1741 (cur->swapmax) : 0);
1744 snprintf(p, n, "%s", cur->uname_s.sysname);
1747 time_t t = time(NULL);
1748 struct tm *tm = localtime(&t);
1749 setlocale(LC_TIME, "");
1750 strftime(p, n, obj->data.s, tm);
1753 time_t t = time(NULL);
1754 struct tm *tm = gmtime(&t);
1755 strftime(p, n, obj->data.s, tm);
1758 human_readable(obj->data.net->recv, p, 255);
1761 human_readable(obj->data.net->trans, p, 255);
1764 snprintf(p, n, "%d", total_updates);
1768 snprintf(p, n, "%d",
1769 (int) (obj->data.net->
1770 trans_speed / 1024));
1772 snprintf(p, 5, "%d ",
1773 (int) (obj->data.net->
1774 trans_speed / 1024));
1778 snprintf(p, n, "%.1f",
1779 obj->data.net->trans_speed /
1782 snprintf(p, 8, "%.1f ",
1783 obj->data.net->trans_speed /
1787 format_seconds_short(p, n,
1791 format_seconds(p, n, (int) cur->uptime);
1796 snprintf(p, n, "%.2f",
1797 cur->seti_prog * 100.0f);
1800 new_bar(p, obj->data.pair.a,
1802 (int) (cur->seti_prog * 255.0f));
1805 snprintf(p, n, "%.0f", cur->seti_credit);
1811 snprintf(p, n, "%s", cur->mpd.title);
1814 snprintf(p, n, "%s", cur->mpd.artist);
1817 snprintf(p, n, "%s", cur->mpd.album);
1820 snprintf(p, n, "%i", cur->mpd.volume);
1823 snprintf(p, n, "%i", cur->mpd.bitrate);
1826 snprintf(p, n, "%s", cur->mpd.status);
1829 new_bar(p, obj->data.pair.a,
1831 (int) (cur->mpd.progress * 255.0f));
1835 if (obj->data.top.type == TOP_NAME && obj->data.top.num >= 0 && obj->data.top.num < 10){
1836 // if we limit the buffer and add a bunch of space after, it stops the thing from
1837 // moving other shit around, which is really fucking annoying
1838 snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
1840 else if (obj->data.top.type == TOP_CPU && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1841 snprintf(p, 7, "%3.2f ", cur->cpu[obj->data.top.num]->amount);
1843 else if (obj->data.top.type == TOP_PID && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1844 snprintf(p, 8, "%i ", cur->cpu[obj->data.top.num]->pid);
1846 else if (obj->data.top.type == TOP_MEM && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1847 snprintf(p, 7, "%3.2f ", cur->cpu[obj->data.top.num]->totalmem);
1851 if (obj->data.top.type == TOP_NAME && obj->data.top.num >= 0 && obj->data.top.num < 10){
1852 // if we limit the buffer and add a bunch of space after, it stops the thing from
1853 // moving other shit around, which is really fucking annoying
1854 snprintf(p, 17, "%s ", cur->memu[obj->data.top.num]->name);
1856 else if (obj->data.top.type == TOP_CPU && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1857 snprintf(p, 7, "%3.2f ", cur->memu[obj->data.top.num]->amount);
1859 else if (obj->data.top.type == TOP_PID && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1860 snprintf(p, 8, "%i ", cur->memu[obj->data.top.num]->pid);
1862 else if (obj->data.top.type == TOP_MEM && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1863 snprintf(p, 7, "%3.2f ", cur->memu[obj->data.top.num]->totalmem);
1870 * I'm tired of everything being packed in
1877 if (current_update_time -
1878 obj->data.tail.last_update <
1879 obj->data.tail.interval) {
1880 snprintf(p, n, "%s",
1881 obj->data.tail.buffer);
1884 obj->data.tail.last_update =
1885 current_update_time;
1889 tailstring *head = NULL;
1890 tailstring *headtmp1 = NULL;
1891 tailstring *headtmp2 = NULL;
1892 tailstring *headtmp2tmp = NULL;
1893 fp = fopen (obj->data.tail.logfile , "rt");
1894 if (fp == NULL) ERR("tail logfile failed to open");
1896 obj->data.tail.readlines = 0;
1898 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL) {
1899 addtail(&head, obj->data.tail.buffer);
1900 obj->data.tail.readlines++;
1906 if (obj->data.tail.readlines > 0) {
1907 for(i=0;i<obj->data.tail.wantedlines;i++) {
1910 addtail(&headtmp2, head->data);
1916 headtmp2tmp = headtmp2;
1917 for(i=0;i<obj->data.tail.wantedlines;i++) {
1919 strncat(obj->data.tail.buffer, headtmp2->data, TEXT_BUFFER_SIZE*4/obj->data.tail.wantedlines);
1920 headtmp2 = headtmp2->next;
1924 /* this is good enough for now. */
1925 snprintf(p, n, "%s", obj->data.tail.buffer);
1927 freetail(headtmp2tmp);
1936 // Hmm, it's expensive to calculate this shit every time FIXME
1938 if ( data.ob_hour != INT_MAX && data.ob_minute != INT_MAX && metar_worked )
1939 format_seconds(p, n, data.ob_hour*3600+data.ob_minute*60);
1941 format_seconds(p, n, 0);
1944 if ( data.temp != INT_MAX && metar_worked)
1945 snprintf(p, n, "%i", data.temp);
1947 snprintf(p, n, "-");
1950 if ( data.temp != INT_MAX && metar_worked)
1951 snprintf(p, n, "%3.1f", (data.temp+40)*9.0/5-40);
1953 snprintf(p, n, "-");
1955 OBJ(metar_windchill){
1956 if ( data.temp != INT_MAX && data.winData.windSpeed != INT_MAX && metar_worked)
1957 snprintf(p, n, "%i", calculateWindChill(data.temp, data.winData.windSpeed));
1959 snprintf(p, n, "-");
1961 OBJ(metar_dew_point){
1962 if ( data.dew_pt_temp != INT_MAX && metar_worked)
1963 snprintf(p, n, "%i", data.dew_pt_temp);
1965 snprintf(p, n, "-");
1968 if ( data.temp != INT_MAX && data.dew_pt_temp != INT_MAX && metar_worked)
1969 snprintf(p, n, "%i", calculateRelativeHumidity(data.temp, data.dew_pt_temp));
1971 snprintf(p, n, "-");
1974 OBJ(metar_windspeed){
1975 if ( data.winData.windSpeed != INT_MAX && metar_worked)
1976 snprintf(p, n, "%i", knTokph(data.winData.windSpeed));
1978 snprintf(p, n, "-");
1981 if ( data.winData.windDir != INT_MAX && metar_worked)
1982 snprintf(p, n, "%s", calculateWindDirectionString(data.winData.windDir));
1984 snprintf(p, n, "-");
1986 OBJ(metar_swinddir){
1987 if ( data.winData.windDir != INT_MAX && metar_worked)
1988 snprintf(p, n, "%s", calculateShortWindDirectionString(data.winData.windDir));
1990 snprintf(p, n, "-");
1994 if (data.cldTypHgt[0].cloud_type[0] != '\0' && metar_worked){
1995 if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "SKC") == 0 )
1996 snprintf(p, n, "Clear Sky");
1997 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "CLR") == 0 )
1998 snprintf(p, n, "Clear Sky");
1999 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "FEW") == 0 )
2000 snprintf(p, n, "Few clouds");
2001 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "SCT") == 0 )
2002 snprintf(p, n, "Scattered");
2003 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "BKN") == 0 )
2004 snprintf(p, n, "Broken");
2005 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "OVC") == 0 )
2006 snprintf(p, n, "Overcast");
2008 snprintf(p, n, "Checking...");
2011 snprintf(p, n, "Checking...");
2013 OBJ(metar_u2d_time){
2014 format_seconds(p, n, (int)last_metar_update%(3600*24)+3600);
2022 unsigned int a = strlen(p);
2028 if (stuff_in_upper_case) {
2038 last_update_time = current_update_time;
2047 static int text_start_x, text_start_y; /* text start position in window */
2048 static int text_width, text_height;
2050 static inline int get_string_width(const char *s)
2052 return *s ? calc_text_width(s, strlen(s)) : 0;
2055 static void text_size_updater(char *s)
2060 /* get string widths and skip specials */
2063 if (*p == SPECIAL_CHAR) {
2065 w += get_string_width(s);
2068 if (specials[special_index].type == BAR) {
2069 w += specials[special_index].width;
2078 w += get_string_width(s);
2083 text_height += font_height();
2086 static void update_text_area()
2090 /* update text size if it isn't fixed */
2095 text_width = minimum_width;
2098 for_each_line(text_buffer, text_size_updater);
2100 if (text_height < minimum_height)
2101 text_height = minimum_height;
2104 /* get text position on workarea */
2105 switch (text_alignment) {
2112 x = workarea[2] - text_width - gap_x;
2119 y = workarea[3] - text_height - gap_y;
2123 x = workarea[2] - text_width - gap_x;
2124 y = workarea[3] - text_height - gap_y;
2132 text_start_x = border_margin + 1;
2133 text_start_y = border_margin + 1;
2134 window.x = x - border_margin - 1;
2135 window.y = y - border_margin - 1;
2139 /* If window size doesn't match to workarea's size, then window
2140 * probably includes panels (gnome).
2141 * Blah, doesn't work on KDE. */
2142 if (workarea[2] != window.width
2143 || workarea[3] != window.height) {
2157 static int cur_x, cur_y; /* current x and y for drawing */
2158 static int draw_mode; /* FG, BG or OUTLINE */
2159 static long current_color;
2161 static inline void set_foreground_color(long c)
2164 XSetForeground(display, window.gc, c);
2167 static void draw_string(const char *s)
2171 int i, i2, pos, width_of_s;
2173 width_of_s = get_string_width(s);
2178 strcpy(tmpstring1, s);
2182 snprintf(space, 2, " ");
2183 max = ((text_width-width_of_s)/get_string_width(space));
2184 //printf("width: %i, length: %i, max: %i space: %i\n", text_width, width_of_s, max, get_string_width(space));
2186 * This code looks for tabs in the text and coverts them to spaces.
2187 * The trick is getting the correct number of spaces,
2188 * and not going over the window's size without forcing
2189 * the window larger.
2191 for(i=0;i<TEXT_BUFFER_SIZE;i++)
2193 if (tmpstring1[i] == '\t') // 9 is ascii tab
2196 for(i2=0;i2<(8-(1+pos)%8) && added <= max;i2++)
2198 tmpstring2[pos+i2] = ' ';
2205 if (tmpstring1[i] != 9) {
2206 tmpstring2[pos] = tmpstring1[i];
2216 c.pixel = current_color;
2217 XQueryColor(display, DefaultColormap(display, screen), &c);
2220 c2.color.red = c.red;
2221 c2.color.green = c.green;
2222 c2.color.blue = c.blue;
2223 c2.color.alpha = font_alpha;
2224 XftDrawStringUtf8(window.xftdraw, &c2, xftfont,
2225 cur_x, cur_y, (XftChar8 *) s, strlen(s));
2229 XDrawString(display, window.drawable, window.gc,
2230 cur_x, cur_y, s, strlen(s));
2232 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
2233 cur_x += width_of_s;
2238 static void draw_line(char *s)
2242 cur_x = text_start_x;
2243 cur_y += font_ascent();
2245 /* find specials and draw stuff */
2248 if (*p == SPECIAL_CHAR) {
2251 /* draw string before special */
2258 switch (specials[special_index].type) {
2259 case HORIZONTAL_LINE:
2262 specials[special_index].height;
2263 int mid = font_ascent() / 2;
2264 w = text_start_x + text_width -
2267 XSetLineAttributes(display,
2272 XDrawLine(display, window.drawable,
2274 cur_y - mid/2, cur_x + w,
2282 specials[special_index].height;
2284 specials[special_index].arg;
2285 int mid = font_ascent() / 2;
2286 char ss[2] = { s, s };
2287 w = text_start_x + text_width -
2290 XSetLineAttributes(display,
2295 XSetDashes(display, window.gc, 0,
2297 XDrawLine(display, window.drawable,
2299 cur_y - mid/2, cur_x + w,
2307 specials[special_index].height;
2309 specials[special_index].arg;
2311 cur_y - (font_ascent() +
2313 w = specials[special_index].width;
2316 text_width - cur_x - 1;
2320 XSetLineAttributes(display,
2326 XDrawRectangle(display,
2330 XFillRectangle(display,
2334 w * bar_usage / 255,
2340 if (draw_mode == FG)
2341 set_foreground_color(specials
2347 if (draw_mode == BG)
2348 set_foreground_color(specials
2354 if (draw_mode == OUTLINE)
2355 set_foreground_color(specials
2362 int pos_x = text_start_x + text_width - cur_x - 1 - get_string_width(p);
2363 if ( pos_x > specials[special_index].arg)
2364 w = pos_x - specials[special_index].arg;
2370 int pos_x = text_start_x + text_width - cur_x - 1 - get_string_width(p)/2 - (text_width/2);
2371 if ( pos_x > specials[special_index].arg)
2372 w = pos_x - specials[special_index].arg;
2388 cur_y += font_descent();
2391 static void draw_text()
2393 cur_y = text_start_y;
2396 if (draw_borders && border_width > 0) {
2397 unsigned int b = (border_width + 1) / 2;
2399 if (stippled_borders) {
2401 { stippled_borders, stippled_borders };
2402 XSetLineAttributes(display, window.gc,
2403 border_width, LineOnOffDash,
2404 CapButt, JoinMiter);
2405 XSetDashes(display, window.gc, 0, ss, 2);
2407 XSetLineAttributes(display, window.gc,
2408 border_width, LineSolid,
2409 CapButt, JoinMiter);
2412 XDrawRectangle(display, window.drawable, window.gc,
2413 text_start_x - border_margin + b,
2414 text_start_y - border_margin + b,
2415 text_width + border_margin * 2 - 1 - b * 2,
2416 text_height + border_margin * 2 - 1 -
2422 for_each_line(text_buffer, draw_line);
2425 static void draw_stuff()
2427 if (draw_shades && !draw_outline) {
2430 set_foreground_color(default_bg_color);
2439 for (i = -1; i < 2; i++)
2440 for (j = -1; j < 2; j++) {
2441 if (i == 0 && j == 0)
2445 set_foreground_color(default_out_color);
2446 draw_mode = OUTLINE;
2453 set_foreground_color(default_fg_color);
2460 swap.swap_window = window.window;
2461 swap.swap_action = XdbeBackground;
2462 XdbeSwapBuffers(display, &swap, 1);
2465 /*#ifdef METAR wtf is this for exactly? aside from trying to cause segfaults?
2466 if (metar_station != NULL) {
2467 free(metar_station);
2468 metar_station = NULL;
2470 if (metar_server != NULL) {
2472 metar_server = NULL;
2474 if (metar_path != NULL) {
2482 static void clear_text(int exposures)
2486 return; /* The swap action is XdbeBackground, which clears */
2488 /* there is some extra space for borders and outlines */
2489 XClearArea(display, window.drawable,
2490 text_start_x - border_margin - 1,
2491 text_start_y - border_margin - 1,
2492 text_width + border_margin * 2 + 2,
2493 text_height + border_margin * 2 + 2,
2494 exposures ? True : 0);
2497 static int need_to_update;
2499 /* update_text() generates new text and clears old text area */
2500 static void update_text()
2507 static void main_loop()
2509 Region region = XCreateRegion();
2511 while (total_run_times == 0 || info.looped < total_run_times-1) {
2515 /* wait for X event or timeout */
2517 if (!XPending(display)) {
2522 update_interval - (get_time() -
2528 tv.tv_sec = (long) t;
2529 tv.tv_usec = (long) (t * 1000000) % 1000000;
2532 FD_SET(ConnectionNumber(display), &fdsr);
2534 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
2538 ERR("can't select(): %s",
2547 if (need_to_update) {
2549 int wx = window.x, wy = window.y;
2558 /* resize window if it isn't right size */
2560 (text_width + border_margin * 2 !=
2562 || text_height + border_margin * 2 !=
2566 border_margin * 2 + 1;
2569 border_margin * 2 + 1;
2570 XResizeWindow(display,
2576 /* move window if it isn't in right position */
2579 || window.y != wy)) {
2580 XMoveWindow(display, window.window,
2581 window.x, window.y);
2591 r.x = text_start_x - border_margin;
2592 r.y = text_start_y - border_margin;
2593 r.width = text_width + border_margin * 2;
2594 r.height = text_height + border_margin * 2;
2595 XUnionRectWithRegion(&r, region, region);
2600 /* handle X events */
2602 while (XPending(display)) {
2604 XNextEvent(display, &ev);
2612 r.width = ev.xexpose.width;
2613 r.height = ev.xexpose.height;
2614 XUnionRectWithRegion(&r, region,
2620 case ReparentNotify:
2621 /* set background to ParentRelative for all parents */
2623 set_transparent_background(window.
2627 case ConfigureNotify:
2629 /* if window size isn't what expected, set fixed size */
2630 if (ev.xconfigure.width !=
2632 || ev.xconfigure.height !=
2634 if (window.width != 0
2635 && window.height != 0)
2638 /* clear old stuff before screwing up size and pos */
2644 if (XGetWindowAttributes(display, window.window, &attrs)) {
2659 border_margin * 2 - 1;
2662 border_margin * 2 - 1;
2665 /* if position isn't what expected, set fixed pos, total_updates
2666 * avoids setting fixed_pos when window is set to weird locations
2668 if (total_updates >= 2
2670 && (window.x != ev.xconfigure.x
2673 && (ev.xconfigure.x != 0
2674 || ev.xconfigure.y != 0)) {
2686 /* XDBE doesn't seem to provide a way to clear the back buffer without
2687 * interfering with the front buffer, other than passing XdbeBackground
2688 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
2689 * redraw the text even if it wasn't part of the exposed area. OTOH,
2690 * if we're not going to call draw_stuff at all, then no swap happens
2691 * and we can safely do nothing.
2694 if (!XEmptyRegion(region)) {
2698 r.x = text_start_x - border_margin;
2699 r.y = text_start_y - border_margin;
2700 r.width = text_width + border_margin * 2;
2701 r.height = text_height + border_margin * 2;
2702 XUnionRectWithRegion(&r, region, region);
2705 XSetRegion(display, window.gc, region);
2708 XftDrawSetClip(window.xftdraw, region);
2711 XDestroyRegion(region);
2712 region = XCreateRegion();
2717 static void load_font()
2722 if (xftfont != NULL)
2723 XftFontClose(display, xftfont);
2726 XftFontOpenName(display, screen, font_name)) != NULL)
2729 ERR("can't load Xft font '%s'", font_name);
2731 XftFontOpenName(display, screen,
2732 "courier-12")) != NULL)
2735 ERR("can't load Xft font '%s'", "courier-12");
2737 if ((font = XLoadQueryFont(display, "fixed")) == NULL) {
2738 CRIT_ERR("can't load font '%s'", "fixed");
2746 /* load normal font */
2748 XFreeFont(display, font);
2750 if ((font = XLoadQueryFont(display, font_name)) == NULL) {
2751 ERR("can't load font '%s'", font_name);
2752 if ((font = XLoadQueryFont(display, "fixed")) == NULL) {
2753 CRIT_ERR("can't load font '%s'", "fixed");
2758 static void set_font()
2762 if (window.xftdraw != NULL)
2763 XftDrawDestroy(window.xftdraw);
2764 window.xftdraw = XftDrawCreate(display, window.drawable,
2765 DefaultVisual(display,
2767 DefaultColormap(display,
2772 XSetFont(display, window.gc, font->fid);
2776 static void load_config_file(const char *);
2778 /* signal handler that reloads config file */
2779 static void reload_handler(int a)
2781 fprintf(stderr, "Conky: received signal %d, reloading config\n",
2784 if (current_config) {
2786 load_config_file(current_config);
2789 extract_variable_text(text);
2796 static void clean_up()
2800 XdbeDeallocateBackBufferName(display, window.back_buffer);
2804 XDestroyWindow(display, window.window);
2812 XFreeGC(display, window.gc);
2814 /* it is really pointless to free() memory at the end of program but ak|ra
2815 * wants me to do this */
2817 free_text_objects();
2819 if (text != original_text)
2822 free(current_config);
2823 free(current_mail_spool);
2829 static void term_handler(int a)
2831 a = a; /* to get rid of warning */
2836 static int string_to_bool(const char *s)
2840 if (strcasecmp(s, "yes") == 0)
2842 if (strcasecmp(s, "true") == 0)
2844 if (strcasecmp(s, "1") == 0)
2849 static enum alignment string_to_alignment(const char *s)
2851 if (strcasecmp(s, "top_left") == 0)
2853 else if (strcasecmp(s, "top_right") == 0)
2855 else if (strcasecmp(s, "bottom_left") == 0)
2857 else if (strcasecmp(s, "bottom_right") == 0)
2858 return BOTTOM_RIGHT;
2859 else if (strcasecmp(s, "tl") == 0)
2861 else if (strcasecmp(s, "tr") == 0)
2863 else if (strcasecmp(s, "bl") == 0)
2865 else if (strcasecmp(s, "br") == 0)
2866 return BOTTOM_RIGHT;
2871 static void set_default_configurations(void)
2873 text_alignment = BOTTOM_LEFT;
2874 fork_to_background = 0;
2877 total_run_times = 0;
2878 info.cpu_avg_samples = 2;
2879 info.net_avg_samples = 2;
2884 strcpy(info.mpd.host, "localhost");
2885 info.mpd.port = 6600;
2886 info.mpd.status = "Checking status...";
2890 default_fg_color = WhitePixel(display, screen);
2891 default_bg_color = BlackPixel(display, screen);
2892 default_out_color = BlackPixel(display, screen);
2899 font_name = strdup("courier-12");
2901 font_name = strdup("6x10");
2906 free(current_mail_spool);
2909 variable_substitute(MAIL_FILE, buf, 256);
2911 current_mail_spool = strdup(buf);
2920 stippled_borders = 0;
2921 update_interval = 10.0;
2922 stuff_in_upper_case = 0;
2924 mlconfig.mldonkey_hostname="127.0.0.1";
2925 mlconfig.mldonkey_port=4001;
2926 mlconfig.mldonkey_login=NULL;
2927 mlconfig.mldonkey_password=NULL;
2930 metar_station = NULL;
2931 metar_server = NULL;
2933 last_metar_update = 0;
2937 static void load_config_file(const char *f)
2939 #define CONF_ERR ERR("%s: %d: config file error", f, line);
2943 set_default_configurations();
2945 fp = open_file(f, 0);
2950 char buf[256], *p, *p2, *name, *value;
2952 if (fgets(buf, 256, fp) == NULL)
2957 /* break at comment */
2958 p2 = strchr(p, '#');
2963 while (*p && isspace((int) *p))
2966 continue; /* empty line */
2972 while (*p2 && !isspace((int) *p2))
2975 *p2 = '\0'; /* break at name's end */
2982 while (*p && isspace((int) *p))
2987 p2 = value + strlen(value);
2988 while (isspace((int) *(p2 - 1)))
2994 #define CONF2(a) if (strcasecmp(name, a) == 0)
2995 #define CONF(a) else CONF2(a)
2996 #define CONF3(a,b) \
2997 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
3000 CONF2("alignment") {
3002 int a = string_to_alignment(value);
3009 CONF("background") {
3010 fork_to_background = string_to_bool(value);
3012 CONF("border_margin") {
3015 strtol(value, 0, 0);
3018 CONF("border_width") {
3021 strtol(value, 0, 0);
3024 CONF("default_color") {
3032 ("default_shade_color",
3033 "default_shadecolor")
3043 ("default_outline_color",
3044 "default_outlinecolor")
3072 if (info.mpd.port < 1 || info.mpd.port > 0xffff)
3076 CONF("cpu_avg_samples") {
3084 if (cpu_avg_samples < 1 || cpu_avg_samples > 14)
3093 CONF("net_avg_samples") {
3101 if (net_avg_samples < 1 || net_avg_samples > 14)
3117 CONF("double_buffer") {
3124 CONF("draw_borders") {
3130 CONF("draw_shades") {
3136 CONF("draw_outline") {
3142 CONF("out_to_console") {
3148 CONF("use_spacer") {
3162 /* font silently ignored when Xft */
3177 if (string_to_bool(value))
3178 ERR("Xft not enabled");
3181 /* xftfont silently ignored when no Xft */
3184 /* xftalpha is silently ignored when no Xft */
3212 CONF("mail_spool") {
3224 if (current_mail_spool)
3225 free(current_mail_spool);
3233 CONF("minimum_size") {
3235 if (sscanf(value, "%d %d", &minimum_width, &minimum_height) != 2)
3236 if (sscanf(value, "%d", &minimum_width) != 1)
3240 CONF("no_buffers") {
3243 string_to_bool(value);
3246 CONF("mldonkey_hostname") {
3248 mlconfig.mldonkey_hostname = strdup(value);
3252 CONF("mldonkey_port") {
3254 mlconfig.mldonkey_port = atoi(value);
3258 CONF("mldonkey_login") {
3260 mlconfig.mldonkey_login = strdup(value);
3264 CONF("mldonkey_password") {
3266 mlconfig.mldonkey_password = strdup(value);
3272 CONF("own_window") {
3279 CONF("pad_percents") {
3285 CONF("stippled_borders") {
3299 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
3302 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
3304 CONF("update_interval") {
3312 CONF_ERR} CONF("total_run_times") {
3344 CONF("metar_station"){
3345 metar_station = (char*)malloc(strlen(value) + 5);
3346 strcpy(metar_station, value);
3347 strcat(metar_station,".TXT");
3349 CONF("metar_server"){
3350 metar_server = (char*)malloc(strlen(value) + 1);
3351 strcpy(metar_server, value);
3354 metar_path = (char*)malloc(strlen(value) + 1);
3355 strcpy(metar_path, value);
3360 if (text != original_text)
3380 if (fgets(buf, 256, fp) == NULL)
3395 if (strlen(text) > 1024 * 8)
3403 ERR("%s: %d: no such configuration: '%s'", f, line, name);
3414 /* : means that character before that takes an argument */
3420 "vVdt:f:u:i:hc:w:x:y:a:"
3430 int main(int argc, char **argv) {
3431 /* handle command line parameters that don't change configs */
3435 if (((s = getenv("LC_ALL")) && *s) ||
3436 ((s = getenv("LC_CTYPE")) && *s) ||
3437 ((s = getenv("LANG")) && *s)) {
3438 if (strstr(s, "UTF-8"))
3441 if (!setlocale(LC_CTYPE, "")) {
3442 fprintf(stderr, "Can't set the specified locale! "
3443 "Check LANG, LC_CTYPE, LC_ALL.\n");
3449 int c = getopt(argc,
3470 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
3471 * though free() does the NULL check itself;), then load optarg value */
3473 free(current_config);
3482 ("Usage: %s [OPTION]...\n"
3483 "Conky is a system monitor that renders text on desktop or to own transparent\n"
3484 "window. Command line options will override configurations defined in config\n"
3487 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
3488 " -c FILE config file to load instead of "
3491 " -d daemonize, fork to background\n"
3492 " -f FONT font to use\n"
3495 " -o create own window to draw\n"
3498 " -b double buffer (prevents flickering)\n"
3500 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
3501 " -u SECS update interval\n"
3502 " -i NUM number of times to update Conky\n"
3503 " -w WIN_ID window id to draw\n"
3504 " -x X x position\n"
3505 " -y Y y position\n",
3525 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
3529 tmpstring1 = (char*)malloc(2*TEXT_BUFFER_SIZE);
3530 tmpstring2 = (char*)malloc(2*TEXT_BUFFER_SIZE);
3532 /* load current_config or CONFIG_FILE */
3535 if (current_config == NULL) {
3536 /* load default config file */
3552 if (current_config != NULL)
3556 set_default_configurations
3560 if (current_mail_spool == NULL) {
3575 /* handle other command line arguments */
3582 int c = getopt(argc,
3627 if (text != original_text)
3629 text = strdup(optarg);
3673 /* generate text and get initial size */
3674 extract_variable_text
3676 if (text != original_text)
3685 update_text_area(); /* to get initial size of the window */
3704 update_text_area(); /* to position text/window on screen */
3707 // why the fuck not?
3733 if (fork_to_background) {
3739 ERR("can't fork() to background: %s", strerror(errno));
3748 "Conky: forked to background, pid is %d\n",
3754 /* set SIGUSR1, SIGINT and SIGTERM handlers */
3760 sa.sa_handler = reload_handler;
3764 sa.sa_flags = SA_RESTART;
3765 if (sigaction(SIGUSR1, &sa, NULL) != 0)
3766 ERR("can't set signal handler for SIGUSR1: %s", strerror(errno));
3768 sa.sa_handler = term_handler;
3772 sa.sa_flags = SA_RESTART;
3773 if (sigaction(SIGINT, &sa, NULL) != 0)
3774 ERR("can't set signal handler for SIGINT: %s", strerror(errno));
3776 sa.sa_handler = term_handler;
3780 sa.sa_flags = SA_RESTART;
3781 if (sigaction(SIGTERM, &sa, NULL) != 0)
3782 ERR("can't set signal handler for SIGTERM: %s", strerror(errno));