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 {
426 OBJ_ml_upload_counter,
427 OBJ_ml_download_counter,
428 OBJ_ml_nshared_files,
429 OBJ_ml_shared_counter,
430 OBJ_ml_tcp_upload_rate,
431 OBJ_ml_tcp_download_rate,
432 OBJ_ml_udp_upload_rate,
433 OBJ_ml_udp_download_rate,
434 OBJ_ml_ndownloaded_files,
435 OBJ_ml_ndownloading_files,
438 OBJ_running_processes,
447 OBJ_temp1, /* i2c is used instead in these */
493 char *s; /* some string */
494 int i; /* some integer */
495 long l; /* some other integer */
496 struct net_stat *net;
498 unsigned char loadavg[3];
531 char buffer[TEXT_BUFFER_SIZE*4];
549 static unsigned int text_object_count;
550 static struct text_object *text_objects;
552 /* new_text_object() allocates a new zeroed text_object */
553 static struct text_object *new_text_object()
556 text_objects = (struct text_object *) realloc(text_objects,
560 memset(&text_objects[text_object_count - 1], 0,
561 sizeof(struct text_object));
563 return &text_objects[text_object_count - 1];
566 static void free_text_objects()
570 for (i = 0; i < text_object_count; i++) {
571 switch (text_objects[i].type) {
573 close(text_objects[i].data.i);
577 close(text_objects[i].data.i2c.fd);
581 case OBJ_if_existing:
584 free(text_objects[i].data.ifblock.s);
598 free(text_objects[i].data.s);
602 free(text_objects[i].data.execi.cmd);
603 free(text_objects[i].data.execi.buffer);
610 text_object_count = 0;
613 void scan_mixer_bar(const char *arg, int *a, int *w, int *h)
618 if (arg && sscanf(arg, "%63s %n", buf1, &n) >= 1) {
619 *a = mixer_init(buf1);
620 (void) scan_bar(arg + n, w, h);
623 (void) scan_bar(arg, w, h);
627 /* construct_text_object() creates a new text_object */
628 static void construct_text_object(const char *s, const char *arg)
630 struct text_object *obj = new_text_object();
632 #define OBJ(a, n) if (strcmp(s, #a) == 0) { obj->type = OBJ_##a; need_mask |= (1 << n); {
633 #define END ; } } else
636 obj->type = OBJ_color;
637 obj->data.l = get_x11_color(s);
639 OBJ(acpitemp, 0) obj->data.i = open_acpi_temperature(arg);
640 END OBJ(acpiacadapter, 0)
641 END OBJ(freq, 0) END OBJ(acpifan, 0) END OBJ(battery,
644 sscanf(arg, "%63s", bat);
647 obj->data.s = strdup(bat);
648 END OBJ(buffers, INFO_BUFFERS)
649 END OBJ(cached, INFO_BUFFERS)
650 END OBJ(cpu, INFO_CPU)
651 END OBJ(cpubar, INFO_CPU)
652 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
653 END OBJ(color, 0) obj->data.l =
654 arg ? get_x11_color(arg) : default_fg_color;
655 END OBJ(downspeed, INFO_NET) obj->data.net = get_net_stat(arg);
656 END OBJ(downspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
660 text_objects[blockstart[blockdepth-1]-1].data.ifblock.pos = text_object_count;
661 blockstart[blockdepth-1] = text_object_count;
662 obj->data.ifblock.pos = text_object_count + 2;
663 } else { ERR("$else: no matching $if_*"); }
668 text_objects[blockstart[blockdepth]-1].data.ifblock.pos = text_object_count;
669 } else { ERR("$endif: no matching $if_*"); }
672 OBJ(exec, 0) obj->data.s = strdup(arg ? arg : "");
675 obj->data.s = strdup(arg ? arg : "");
677 OBJ(execi, 0) unsigned int n;
680 || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
682 ERR("${execi <interval> command}");
683 obj->type = OBJ_text;
684 snprintf(buf, 256, "${%s}", s);
685 obj->data.s = strdup(buf);
687 obj->data.execi.cmd = strdup(arg + n);
688 obj->data.execi.buffer =
689 (char *) calloc(1, TEXT_BUFFER_SIZE);
691 END OBJ(pre_exec, 0) obj->type = OBJ_text;
693 FILE *fp = popen(arg, "r");
697 n = fread(buf, 1, 2048, fp);
700 if (n && buf[n - 1] == '\n')
705 obj->data.s = strdup(buf);
707 obj->data.s = strdup("");
710 OBJ(fs_bar, INFO_FS) obj->data.fsbar.h = 4;
711 arg = scan_bar(arg, &obj->data.fsbar.w, &obj->data.fsbar.h);
713 while (isspace(*arg))
719 obj->data.fsbar.fs = prepare_fs_stat(arg);
720 END OBJ(fs_bar_free, INFO_FS) obj->data.fsbar.h = 4;
723 if (sscanf(arg, "%d %n", &obj->data.fsbar.h, &n) >= 1)
727 obj->data.fsbar.fs = prepare_fs_stat(arg);
728 END OBJ(fs_free, INFO_FS) if (!arg)
730 obj->data.fs = prepare_fs_stat(arg);
731 END OBJ(fs_used_perc, INFO_FS) if (!arg)
733 obj->data.fs = prepare_fs_stat(arg);
734 END OBJ(fs_free_perc, INFO_FS) if (!arg)
736 obj->data.fs = prepare_fs_stat(arg);
737 END OBJ(fs_size, INFO_FS) if (!arg)
739 obj->data.fs = prepare_fs_stat(arg);
740 END OBJ(fs_used, INFO_FS) if (!arg)
742 obj->data.fs = prepare_fs_stat(arg);
743 END OBJ(hr, 0) obj->data.i = arg ? atoi(arg) : 1;
744 END OBJ(i2c, INFO_I2C) char buf1[64], buf2[64];
748 ERR("i2c needs arguments");
749 obj->type = OBJ_text;
750 obj->data.s = strdup("${i2c}");
754 if (sscanf(arg, "%63s %63s %d", buf1, buf2, &n) != 3) {
755 /* if scanf couldn't read three values, read type and num and use
757 sscanf(arg, "%63s %d", buf2, &n);
759 open_i2c_sensor(0, buf2, n, &obj->data.i2c.arg,
760 obj->data.i2c.devtype);
761 strcpy(obj->data.i2c.type, buf2);
764 open_i2c_sensor(buf1, buf2, n, &obj->data.i2c.arg,
765 obj->data.i2c.devtype);
766 strcpy(obj->data.i2c.type, buf2);
769 END OBJ(top, INFO_TOP)
773 ERR("top needs arguments");
774 obj->type = OBJ_text;
775 obj->data.s = strdup("${top}");
778 if(sscanf(arg, "%63s %i", buf, &n) == 2) {
779 if(strcmp(buf, "name") == 0) {
780 obj->data.top.type = TOP_NAME;
782 else if(strcmp(buf, "cpu") == 0) {
783 obj->data.top.type = TOP_CPU;
785 else if(strcmp(buf, "pid") == 0) {
786 obj->data.top.type = TOP_PID;
788 else if(strcmp(buf, "mem") == 0) {
789 obj->data.top.type = TOP_MEM;
793 ERR("invalid arg for top");
796 if(n < 1 || n > 10) {
797 CRIT_ERR("invalid arg for top");
801 obj->data.top.num = n-1;
805 ERR("invalid args given for top");
810 obj->data.net = get_net_stat(arg);
812 OBJ(linkstatus, INFO_WIFI)
813 obj->data.net = get_net_stat(arg);
819 ERR("tail needs arguments");
820 obj->type = OBJ_text;
821 obj->data.s = strdup("${tail}");
824 if(sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 2) {
825 if(n1 < 1 || n1 > 30) {
826 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
831 fp = fopen (buf,"rt");
833 obj->data.tail.logfile = malloc(TEXT_BUFFER_SIZE);
834 strcpy(obj->data.tail.logfile, buf);
835 obj->data.tail.wantedlines = n1-1;
836 obj->data.tail.interval = update_interval*2;
841 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
845 else if (sscanf(arg, "%63s %i %i", buf, &n1, &n2) == 3) {
846 if(n1 < 1 || n1 > 30) {
847 CRIT_ERR("invalid arg for tail, number of lines must be between 1 and 30");
850 else if(n2 < 1 || n2 < update_interval) {
851 CRIT_ERR("invalid arg for tail, interval must be greater than 0 and Conky's interval");
856 fp = fopen (buf,"rt");
858 obj->data.tail.logfile = malloc(TEXT_BUFFER_SIZE);
859 strcpy(obj->data.tail.logfile, buf);
860 obj->data.tail.wantedlines = n1-1;
861 obj->data.tail.interval = n2;
866 CRIT_ERR("tail logfile does not exist, or you do not have correct permissions");
872 ERR("invalid args given for tail");
876 OBJ(loadavg, INFO_LOADAVG) int a = 1, b = 2, c = 3, r = 3;
878 r = sscanf(arg, "%d %d %d", &a, &b, &c);
879 if (r >= 3 && (c < 1 || c > 3))
881 if (r >= 2 && (b < 1 || b > 3))
883 if (r >= 1 && (a < 1 || a > 3))
886 obj->data.loadavg[0] = (r >= 1) ? (unsigned char) a : 0;
887 obj->data.loadavg[1] = (r >= 2) ? (unsigned char) b : 0;
888 obj->data.loadavg[2] = (r >= 3) ? (unsigned char) c : 0;
891 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
892 {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
894 ERR("if_existing needs an argument");
895 obj->data.ifblock.s = 0;
898 obj->data.ifblock.s = strdup(arg);
899 blockstart[blockdepth] = text_object_count;
900 obj->data.ifblock.pos = text_object_count + 2;
904 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
905 {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
907 ERR("if_mounted needs an argument");
908 obj->data.ifblock.s = 0;
911 obj->data.ifblock.s = strdup(arg);
912 blockstart[blockdepth] = text_object_count;
913 obj->data.ifblock.pos = text_object_count + 2;
917 if(blockdepth>=MAX_IF_BLOCK_DEPTH)
918 {CRIT_ERR("MAX_IF_BLOCK_DEPTH exceeded");}
921 snprintf(buf,256,"pidof %s >/dev/null",arg);
922 obj->data.ifblock.s = strdup(buf);
924 ERR("if_running needs an argument");
925 obj->data.ifblock.s = 0;
927 blockstart[blockdepth] = text_object_count;
928 obj->data.ifblock.pos = text_object_count + 2;
933 END OBJ(mails, INFO_MAIL)
934 END OBJ(mem, INFO_MEM)
935 END OBJ(memmax, INFO_MEM)
936 END OBJ(memperc, INFO_MEM)
937 END OBJ(membar, INFO_MEM)
938 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
939 END OBJ(mixer, INFO_MIXER) obj->data.l = mixer_init(arg);
940 END OBJ(mixerl, INFO_MIXER) obj->data.l = mixer_init(arg);
941 END OBJ(mixerr, INFO_MIXER) obj->data.l = mixer_init(arg);
942 END OBJ(mixerbar, INFO_MIXER)
943 scan_mixer_bar(arg, &obj->data.mixerbar.l,
944 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
945 END OBJ(mixerlbar, INFO_MIXER)
946 scan_mixer_bar(arg, &obj->data.mixerbar.l,
947 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
948 END OBJ(mixerrbar, INFO_MIXER)
949 scan_mixer_bar(arg, &obj->data.mixerbar.l,
950 &obj->data.mixerbar.w, &obj->data.mixerbar.h);
953 OBJ(ml_upload_counter, INFO_MLDONKEY)
955 OBJ(ml_download_counter, INFO_MLDONKEY)
957 OBJ(ml_nshared_files, INFO_MLDONKEY)
959 OBJ(ml_shared_counter, INFO_MLDONKEY)
961 OBJ(ml_tcp_upload_rate, INFO_MLDONKEY)
963 OBJ(ml_tcp_download_rate, INFO_MLDONKEY)
965 OBJ(ml_udp_upload_rate, INFO_MLDONKEY)
967 OBJ(ml_udp_download_rate, INFO_MLDONKEY)
969 OBJ(ml_ndownloaded_files, INFO_MLDONKEY)
971 OBJ(ml_ndownloading_files, INFO_MLDONKEY)
974 OBJ(new_mails, INFO_MAIL)
976 END OBJ(processes, INFO_PROCS)
977 END OBJ(running_processes, INFO_RUN_PROCS)
978 END OBJ(shadecolor, 0)
979 obj->data.l = arg ? get_x11_color(arg) : default_bg_color;
980 END OBJ(outlinecolor, 0)
981 obj->data.l = arg ? get_x11_color(arg) : default_out_color;
982 END OBJ(stippled_hr, 0) int a = stippled_borders, b = 1;
984 if (sscanf(arg, "%d %d", &a, &b) != 2)
985 sscanf(arg, "%d", &b);
989 obj->data.pair.a = a;
990 obj->data.pair.b = b;
991 END OBJ(swap, INFO_MEM)
992 END OBJ(swapmax, INFO_MEM)
993 END OBJ(swapperc, INFO_MEM)
994 END OBJ(swapbar, INFO_MEM)
995 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
996 END OBJ(sysname, 0) END OBJ(temp1, INFO_I2C) obj->type = OBJ_i2c;
998 open_i2c_sensor(0, "temp", 1, &obj->data.i2c.arg,
999 obj->data.i2c.devtype);
1000 END OBJ(temp2, INFO_I2C) obj->type = OBJ_i2c;
1002 open_i2c_sensor(0, "temp", 2, &obj->data.i2c.arg,
1003 obj->data.i2c.devtype);
1004 END OBJ(time, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1005 END OBJ(utime, 0) obj->data.s = strdup(arg ? arg : "%F %T");
1006 END OBJ(totaldown, INFO_NET) obj->data.net = get_net_stat(arg);
1007 END OBJ(totalup, INFO_NET) obj->data.net = get_net_stat(arg);
1011 obj->data.i = arg ? atoi(arg) : 1;
1015 obj->data.i = arg ? atoi(arg) : 1;
1016 END OBJ(upspeed, INFO_NET) obj->data.net = get_net_stat(arg);
1017 END OBJ(upspeedf, INFO_NET) obj->data.net = get_net_stat(arg);
1018 END OBJ(uptime_short, INFO_UPTIME) END OBJ(uptime, INFO_UPTIME) END
1025 OBJ(seti_prog, INFO_SETI) END OBJ(seti_progbar, INFO_SETI)
1026 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1027 END OBJ(seti_credit, INFO_SETI) END
1030 OBJ(metar_ob_time, INFO_METAR)
1032 OBJ(metar_temp, INFO_METAR)
1034 OBJ(metar_tempf, INFO_METAR)
1036 OBJ(metar_windchill, INFO_METAR)
1038 OBJ(metar_dew_point, INFO_METAR)
1040 OBJ(metar_rh, INFO_METAR)
1043 OBJ(metar_windspeed, INFO_METAR)
1045 OBJ(metar_winddir, INFO_METAR)
1047 OBJ(metar_swinddir, INFO_METAR)
1050 OBJ(metar_cloud, INFO_METAR)
1052 OBJ(metar_u2d_time, INFO_METAR)
1057 OBJ(mpd_artist, INFO_MPD)
1058 END OBJ(mpd_title, INFO_MPD)
1059 END OBJ(mpd_album, INFO_MPD) END OBJ(mpd_vol, INFO_MPD) END OBJ(mpd_bitrate, INFO_MPD) END
1060 OBJ(mpd_status, INFO_MPD) END
1061 OBJ(mpd_bar, INFO_MPD)
1062 (void) scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
1067 ERR("unknown variable %s", s);
1068 obj->type = OBJ_text;
1069 snprintf(buf, 256, "${%s}", s);
1070 obj->data.s = strdup(buf);
1075 /* append_text() appends text to last text_object if it's text, if it isn't
1076 * it creates a new text_object */
1077 static void append_text(const char *s)
1079 struct text_object *obj;
1081 if (s == NULL || *s == '\0')
1084 obj = text_object_count ? &text_objects[text_object_count - 1] : 0;
1086 /* create a new text object? */
1087 if (!obj || obj->type != OBJ_text) {
1088 obj = new_text_object();
1089 obj->type = OBJ_text;
1090 obj->data.s = strdup(s);
1093 obj->data.s = (char *) realloc(obj->data.s,
1094 strlen(obj->data.s) +
1096 strcat(obj->data.s, s);
1100 static void extract_variable_text(const char *p)
1104 free_text_objects();
1119 /* variable is either $foo or ${foo} */
1123 while (*p && *p != '}')
1129 while (*p && (isalnum((int) *p)
1134 /* copy variable to buffer */
1135 len = (p - s > 255) ? 255 : (p - s);
1136 strncpy(buf, s, len);
1145 /* if variable wasn't found from environment, use some special */
1151 if (strchr(buf, ' ')) {
1152 arg = strchr(buf, ' ');
1155 while (isspace((int) *arg))
1161 /* lowercase variable name */
1168 construct_text_object(buf, arg);
1178 if(blockdepth) ERR("one or more $endif's are missing");
1181 double current_update_time, last_update_time;
1183 static void generate_text()
1186 struct information *cur = &info;
1193 current_update_time = get_time();
1199 n = TEXT_BUFFER_SIZE*4 - 2;
1202 for (i = 0; i < text_object_count; i++) {
1203 struct text_object *obj = &text_objects[i];
1205 #define OBJ(a) break; case OBJ_##a:
1207 switch (obj->type) {
1210 ERR("not implemented obj type %d",
1214 /* does anyone have decimals in acpi temperature? */
1216 snprintf(p, n, "%d",
1217 (int) get_acpi_temperature(obj->
1221 snprintf(p, 5, "%d ",
1222 (int) get_acpi_temperature(obj->
1227 snprintf(p, n, "%s", get_freq());
1230 snprintf(p, n, "%s", get_adt746x_cpu());
1233 snprintf(p, n, "%s", get_adt746x_fan());
1236 snprintf(p, n, "%s", get_acpi_fan());
1238 OBJ(acpiacadapter) {
1239 snprintf(p, n, "%s",
1240 get_acpi_ac_adapter());
1243 get_battery_stuff(p, n, obj->data.s);
1246 human_readable(cur->buffers * 1024, p, 255);
1249 human_readable(cur->cached * 1024, p, 255);
1253 snprintf(p, n, "%*d", pad_percents,
1254 (int) (cur->cpu_usage * 100.0));
1256 snprintf(p, 4, "%*d ", pad_percents,
1257 (int) (cur->cpu_usage * 100.0));
1260 new_bar(p, obj->data.pair.a,
1262 (int) (cur->cpu_usage * 255.0));
1265 new_fg(p, obj->data.l);
1269 snprintf(p, n, "%d",
1270 (int) (obj->data.net->recv_speed /
1273 snprintf(p, 6, "%d ",
1274 (int) (obj->data.net->recv_speed /
1279 snprintf(p, n, "%.1f",
1280 obj->data.net->recv_speed /
1283 snprintf(p, 8, "%.1f ",
1284 obj->data.net->recv_speed /
1290 i=obj->data.ifblock.pos-2;
1304 FILE *fp = popen(obj->data.s, "r");
1305 int n2 = fread(p, 1, n, fp);
1309 if (n2 && p[n2 - 1] == '\n')
1320 FILE *fp = popen(obj->data.s, "r");
1321 int n2 = fread(p, 1, n, fp);
1325 if (n2 && p[n2 - 1] == '\n')
1328 snprintf(p, n, "%u.%u.%u.%u",
1329 obj->data.net->addr.sa_data[2] & 255,
1330 obj->data.net->addr.sa_data[3] & 255,
1331 obj->data.net->addr.sa_data[4] & 255,
1332 obj->data.net->addr.sa_data[5] & 255);
1336 snprintf(p, n, "%d", obj->data.net->linkstatus);
1344 if (sscanf(p, "%lf", &barnum) == 0)
1346 ERR("reading execbar value failed (perhaps it's not the correct format?)");
1348 if (barnum > 100 || barnum < 0)
1350 ERR("your execbar value is not between 0 and 100, therefore it will be ignored");
1354 barnum = barnum/100.0;
1357 (int) (barnum * 255.0));
1362 if (current_update_time -
1363 obj->data.execi.last_update <
1364 obj->data.execi.interval) {
1365 snprintf(p, n, "%s",
1366 obj->data.execi.buffer);
1368 char *p2 = obj->data.execi.buffer;
1370 popen(obj->data.execi.cmd,
1373 fread(p2, 1, TEXT_BUFFER_SIZE,
1378 if (n2 && p2[n2 - 1] == '\n')
1387 snprintf(p, n, "%s",
1388 obj->data.execi.buffer);
1390 obj->data.execi.last_update =
1391 current_update_time;
1396 if (obj->data.fs != NULL) {
1397 if (obj->data.fs->size == 0)
1416 if (obj->data.fs != NULL)
1417 human_readable(obj->data.fs->avail,
1421 if (obj->data.fs != NULL) {
1422 if (obj->data.fs->size)
1423 snprintf(p, n, "%*d", pad_percents, (int) ((obj->data.fs->avail *100) / obj->data.fs->size));
1425 snprintf(p, n, "0");
1429 if (obj->data.fs != NULL)
1430 human_readable(obj->data.fs->size,
1434 if (obj->data.fs != NULL)
1435 human_readable(obj->data.fs->size -
1436 obj->data.fs->avail,
1440 if (obj->data.fs != NULL) {
1441 if (obj->data.fs->size == 0)
1459 if (obj->data.fs != NULL) {
1460 if (obj->data.fs->size)
1461 snprintf(p, 4, "%d",
1471 snprintf(p, n, "0");
1475 float *v = info.loadavg;
1477 if (obj->data.loadavg[2])
1478 snprintf(p, n, "%.2f %.2f %.2f",
1479 v[obj->data.loadavg[0] -
1481 v[obj->data.loadavg[1] -
1483 v[obj->data.loadavg[2] -
1485 else if (obj->data.loadavg[1])
1486 snprintf(p, n, "%.2f %.2f",
1487 v[obj->data.loadavg[0] -
1489 v[obj->data.loadavg[1] -
1491 else if (obj->data.loadavg[0])
1492 snprintf(p, n, "%.2f",
1493 v[obj->data.loadavg[0] -
1497 new_hr(p, obj->data.i);
1502 r = get_i2c_info(&obj->data.i2c.fd,
1504 obj->data.i2c.devtype,
1505 obj->data.i2c.type);
1507 if (r >= 100.0 || r == 0)
1508 snprintf(p, n,"%d", (int) r);
1510 snprintf(p, n, "%.1f", r);
1513 new_alignr(p, obj->data.i);
1516 new_alignc(p, obj->data.i);
1520 if((obj->data.ifblock.s)&&(stat(obj->data.ifblock.s,&tmp) == -1)) {
1521 i=obj->data.ifblock.pos-2;
1523 } else if_jumped = 0;
1526 if((obj->data.ifblock.s)&&(!check_mount(obj->data.ifblock.s))) {
1527 i=obj->data.ifblock.pos-2;
1529 } else if_jumped = 0;
1532 if((obj->data.ifblock.s)&&system(obj->data.ifblock.s)) {
1533 i=obj->data.ifblock.pos-2;
1535 } else if_jumped = 0;
1538 snprintf(p, n, "%s", cur->uname_s.release);
1541 snprintf(p, n, "%s", cur->uname_s.machine);
1546 human_readable(cur->mem * 1024, p, 6);
1549 human_readable(cur->memmax * 1024, p, 255);
1555 snprintf(p, n, "%*d", pad_percents,
1559 snprintf(p, 4, "%*d ", pad_percents,
1565 new_bar(p, obj->data.pair.a,
1567 cur->memmax ? (cur->mem * 255) /
1573 snprintf(p, n, "%d",
1574 mixer_get_avg(obj->data.l));
1577 snprintf(p, n, "%d",
1578 mixer_get_left(obj->data.l));
1581 snprintf(p, n, "%d",
1582 mixer_get_right(obj->data.l));
1585 new_bar(p, obj->data.mixerbar.w,
1586 obj->data.mixerbar.h,
1587 mixer_get_avg(obj->data.mixerbar.
1591 new_bar(p, obj->data.mixerbar.w,
1592 obj->data.mixerbar.h,
1593 mixer_get_left(obj->data.mixerbar.
1597 new_bar(p, obj->data.mixerbar.w,
1598 obj->data.mixerbar.h,
1599 mixer_get_right(obj->data.mixerbar.
1605 snprintf(p, n, "%d", cur->mail_count);
1608 snprintf(p, n, "%d", cur->new_mail_count);
1611 OBJ(ml_upload_counter) {
1612 snprintf(p, n, "%lld", mlinfo.upload_counter/1048576);
1614 OBJ(ml_download_counter) {
1615 snprintf(p, n, "%lld", mlinfo.download_counter/1048576);
1617 OBJ(ml_nshared_files) {
1618 snprintf(p, n, "%i", mlinfo.nshared_files);
1620 OBJ(ml_shared_counter) {
1621 snprintf(p, n, "%lld", mlinfo.shared_counter/1048576);
1623 OBJ(ml_tcp_upload_rate) {
1624 snprintf(p, n, "%.2f", (float)mlinfo.tcp_upload_rate/1024);
1626 OBJ(ml_tcp_download_rate) {
1627 snprintf(p, n, "%.2f",(float)mlinfo.tcp_download_rate/1024);
1629 OBJ(ml_udp_upload_rate) {
1630 snprintf(p, n, "%.2f",(float)mlinfo.udp_upload_rate/1024);
1632 OBJ(ml_udp_download_rate) {
1633 snprintf(p, n, "%.2f",(float)mlinfo.udp_download_rate/1024);
1635 OBJ(ml_ndownloaded_files) {
1636 snprintf(p, n, "%i", mlinfo.ndownloaded_files);
1638 OBJ(ml_ndownloading_files) {
1639 snprintf(p, n, "%i", mlinfo.ndownloading_files);
1644 snprintf(p, n, "%s",
1645 cur->uname_s.nodename);
1648 new_outline(p, obj->data.l);
1652 snprintf(p, n, "%d", cur->procs);
1654 snprintf(p, 5, "%d ", cur->procs);
1656 OBJ(running_processes) {
1658 snprintf(p, n, "%d", cur->run_procs);
1660 snprintf(p, 3, "%d ", cur->run_procs);
1663 snprintf(p, n, "%s", obj->data.s);
1666 new_bg(p, obj->data.l);
1669 new_stippled_hr(p, obj->data.pair.a,
1673 human_readable(cur->swap * 1024, p, 255);
1676 human_readable(cur->swapmax * 1024, p, 255);
1679 if (cur->swapmax == 0) {
1680 strncpy(p, "No swap", 255);
1683 snprintf(p, 255, "%*u",
1688 snprintf(p, 4, "%*u ",
1695 new_bar(p, obj->data.pair.a,
1697 cur->swapmax ? (cur->swap * 255) /
1698 (cur->swapmax) : 0);
1701 snprintf(p, n, "%s", cur->uname_s.sysname);
1704 time_t t = time(NULL);
1705 struct tm *tm = localtime(&t);
1706 setlocale(LC_TIME, "");
1707 strftime(p, n, obj->data.s, tm);
1710 time_t t = time(NULL);
1711 struct tm *tm = gmtime(&t);
1712 strftime(p, n, obj->data.s, tm);
1715 human_readable(obj->data.net->recv, p, 255);
1718 human_readable(obj->data.net->trans, p, 255);
1721 snprintf(p, n, "%d", total_updates);
1725 snprintf(p, n, "%d",
1726 (int) (obj->data.net->
1727 trans_speed / 1024));
1729 snprintf(p, 5, "%d ",
1730 (int) (obj->data.net->
1731 trans_speed / 1024));
1735 snprintf(p, n, "%.1f",
1736 obj->data.net->trans_speed /
1739 snprintf(p, 8, "%.1f ",
1740 obj->data.net->trans_speed /
1744 format_seconds_short(p, n,
1748 format_seconds(p, n, (int) cur->uptime);
1753 snprintf(p, n, "%.2f",
1754 cur->seti_prog * 100.0f);
1757 new_bar(p, obj->data.pair.a,
1759 (int) (cur->seti_prog * 255.0f));
1762 snprintf(p, n, "%.0f", cur->seti_credit);
1768 snprintf(p, n, "%s", cur->mpd.title);
1771 snprintf(p, n, "%s", cur->mpd.artist);
1774 snprintf(p, n, "%s", cur->mpd.album);
1777 snprintf(p, n, "%i", cur->mpd.volume);
1780 snprintf(p, n, "%i", cur->mpd.bitrate);
1783 snprintf(p, n, "%s", cur->mpd.status);
1786 new_bar(p, obj->data.pair.a,
1788 (int) (cur->mpd.progress * 255.0f));
1792 if (obj->data.top.type == TOP_NAME && obj->data.top.num >= 0 && obj->data.top.num < 10){
1793 // if we limit the buffer and add a bunch of space after, it stops the thing from
1794 // moving other shit around, which is really fucking annoying
1795 snprintf(p, 17, "%s ", cur->tops[obj->data.top.num]->name);
1797 else if (obj->data.top.type == TOP_CPU && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1798 snprintf(p, 7, "%3.2f ", cur->tops[obj->data.top.num]->amount);
1800 else if (obj->data.top.type == TOP_PID && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1801 snprintf(p, 8, "%i ", cur->tops[obj->data.top.num]->pid);
1803 else if (obj->data.top.type == TOP_MEM && obj->data.top.num >= 0 && obj->data.top.num < 10) {
1804 snprintf(p, 7, "%3.2f ", cur->tops[obj->data.top.num]->totalmem);
1811 * I'm tired of everything being packed in
1818 if (current_update_time -
1819 obj->data.tail.last_update <
1820 obj->data.tail.interval) {
1821 snprintf(p, n, "%s",
1822 obj->data.tail.buffer);
1825 obj->data.tail.last_update =
1826 current_update_time;
1830 tailstring *head = NULL;
1831 tailstring *headtmp1 = NULL;
1832 tailstring *headtmp2 = NULL;
1833 tailstring *headtmp2tmp = NULL;
1834 fp = fopen (obj->data.tail.logfile , "rt");
1835 if (fp == NULL) ERR("tail logfile failed to open");
1837 obj->data.tail.readlines = 0;
1839 while (fgets(obj->data.tail.buffer, TEXT_BUFFER_SIZE*4, fp) != NULL) {
1840 addtail(&head, obj->data.tail.buffer);
1841 obj->data.tail.readlines++;
1847 if (obj->data.tail.readlines > 0) {
1848 for(i=0;i<obj->data.tail.wantedlines;i++) {
1851 addtail(&headtmp2, head->data);
1857 headtmp2tmp = headtmp2;
1858 for(i=0;i<obj->data.tail.wantedlines;i++) {
1860 strncat(obj->data.tail.buffer, headtmp2->data, TEXT_BUFFER_SIZE*4/obj->data.tail.wantedlines);
1861 headtmp2 = headtmp2->next;
1865 /* this is good enough for now. */
1866 snprintf(p, n, "%s", obj->data.tail.buffer);
1868 freetail(headtmp2tmp);
1877 // Hmm, it's expensive to calculate this shit every time FIXME
1879 if ( data.ob_hour != INT_MAX && data.ob_minute != INT_MAX && metar_worked )
1880 format_seconds(p, n, data.ob_hour*3600+data.ob_minute*60);
1882 format_seconds(p, n, 0);
1885 if ( data.temp != INT_MAX && metar_worked)
1886 snprintf(p, n, "%i", data.temp);
1888 snprintf(p, n, "-");
1891 if ( data.temp != INT_MAX && metar_worked)
1892 snprintf(p, n, "%3.1f", (data.temp+40)*9.0/5-40);
1894 snprintf(p, n, "-");
1896 OBJ(metar_windchill){
1897 if ( data.temp != INT_MAX && data.winData.windSpeed != INT_MAX && metar_worked)
1898 snprintf(p, n, "%i", calculateWindChill(data.temp, data.winData.windSpeed));
1900 snprintf(p, n, "-");
1902 OBJ(metar_dew_point){
1903 if ( data.dew_pt_temp != INT_MAX && metar_worked)
1904 snprintf(p, n, "%i", data.dew_pt_temp);
1906 snprintf(p, n, "-");
1909 if ( data.temp != INT_MAX && data.dew_pt_temp != INT_MAX && metar_worked)
1910 snprintf(p, n, "%i", calculateRelativeHumidity(data.temp, data.dew_pt_temp));
1912 snprintf(p, n, "-");
1915 OBJ(metar_windspeed){
1916 if ( data.winData.windSpeed != INT_MAX && metar_worked)
1917 snprintf(p, n, "%i", knTokph(data.winData.windSpeed));
1919 snprintf(p, n, "-");
1922 if ( data.winData.windDir != INT_MAX && metar_worked)
1923 snprintf(p, n, "%s", calculateWindDirectionString(data.winData.windDir));
1925 snprintf(p, n, "-");
1927 OBJ(metar_swinddir){
1928 if ( data.winData.windDir != INT_MAX && metar_worked)
1929 snprintf(p, n, "%s", calculateShortWindDirectionString(data.winData.windDir));
1931 snprintf(p, n, "-");
1935 if (data.cldTypHgt[0].cloud_type[0] != '\0' && metar_worked){
1936 if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "SKC") == 0 )
1937 snprintf(p, n, "Clear Sky");
1938 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "CLR") == 0 )
1939 snprintf(p, n, "Clear Sky");
1940 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "FEW") == 0 )
1941 snprintf(p, n, "Few clouds");
1942 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "SCT") == 0 )
1943 snprintf(p, n, "Scattered");
1944 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "BKN") == 0 )
1945 snprintf(p, n, "Broken");
1946 else if ( strcmp(&data.cldTypHgt[0].cloud_type[0], "OVC") == 0 )
1947 snprintf(p, n, "Overcast");
1949 snprintf(p, n, "Checking...");
1952 snprintf(p, n, "Checking...");
1954 OBJ(metar_u2d_time){
1955 format_seconds(p, n, (int)last_metar_update%(3600*24)+3600);
1963 unsigned int a = strlen(p);
1969 if (stuff_in_upper_case) {
1979 last_update_time = current_update_time;
1988 static int text_start_x, text_start_y; /* text start position in window */
1989 static int text_width, text_height;
1991 static inline int get_string_width(const char *s)
1993 return *s ? calc_text_width(s, strlen(s)) : 0;
1996 static void text_size_updater(char *s)
2001 /* get string widths and skip specials */
2004 if (*p == SPECIAL_CHAR) {
2006 w += get_string_width(s);
2009 if (specials[special_index].type == BAR) {
2010 w += specials[special_index].width;
2019 w += get_string_width(s);
2024 text_height += font_height();
2027 static void update_text_area()
2031 /* update text size if it isn't fixed */
2036 text_width = minimum_width;
2039 for_each_line(text_buffer, text_size_updater);
2041 if (text_height < minimum_height)
2042 text_height = minimum_height;
2045 /* get text position on workarea */
2046 switch (text_alignment) {
2053 x = workarea[2] - text_width - gap_x;
2060 y = workarea[3] - text_height - gap_y;
2064 x = workarea[2] - text_width - gap_x;
2065 y = workarea[3] - text_height - gap_y;
2073 text_start_x = border_margin + 1;
2074 text_start_y = border_margin + 1;
2075 window.x = x - border_margin - 1;
2076 window.y = y - border_margin - 1;
2080 /* If window size doesn't match to workarea's size, then window
2081 * probably includes panels (gnome).
2082 * Blah, doesn't work on KDE. */
2083 if (workarea[2] != window.width
2084 || workarea[3] != window.height) {
2098 static int cur_x, cur_y; /* current x and y for drawing */
2099 static int draw_mode; /* FG, BG or OUTLINE */
2100 static long current_color;
2102 static inline void set_foreground_color(long c)
2105 XSetForeground(display, window.gc, c);
2108 static void draw_string(const char *s)
2112 int i, i2, pos, width_of_s;
2114 width_of_s = get_string_width(s);
2119 strcpy(tmpstring1, s);
2123 snprintf(space, 2, " ");
2124 max = ((text_width-width_of_s)/get_string_width(space));
2125 //printf("width: %i, length: %i, max: %i space: %i\n", text_width, width_of_s, max, get_string_width(space));
2127 * This code looks for tabs in the text and coverts them to spaces.
2128 * The trick is getting the correct number of spaces,
2129 * and not going over the window's size without forcing
2130 * the window larger.
2132 for(i=0;i<TEXT_BUFFER_SIZE;i++)
2134 if (tmpstring1[i] == '\t') // 9 is ascii tab
2137 for(i2=0;i2<(8-(1+pos)%8) && added <= max;i2++)
2139 //printf("pos thinger %i added %i max %i\n",(8-(1+pos)%8), added, max);
2140 //printf("adding tab to: %s\n", s);
2141 tmpstring2[pos+i2] = ' ';
2145 //printf("not adding tab to: %s\n", s);
2149 if (tmpstring1[i] != 9) {
2150 tmpstring2[pos] = tmpstring1[i];
2154 tmpstring2[pos] = ' ';
2164 c.pixel = current_color;
2165 XQueryColor(display, DefaultColormap(display, screen), &c);
2168 c2.color.red = c.red;
2169 c2.color.green = c.green;
2170 c2.color.blue = c.blue;
2171 c2.color.alpha = font_alpha;
2172 //printf("at: %i, %i\nstring: %s", cur_x, cur_y, s);
2173 XftDrawString8(window.xftdraw, &c2, xftfont,
2174 cur_x, cur_y, (XftChar8 *) s, strlen(s));
2178 XDrawString(display, window.drawable, window.gc,
2179 cur_x, cur_y, s, strlen(s));
2181 memcpy(tmpstring1, s, TEXT_BUFFER_SIZE);
2182 cur_x += width_of_s;
2187 static void draw_line(char *s)
2191 cur_x = text_start_x;
2192 cur_y += font_ascent();
2194 /* find specials and draw stuff */
2197 if (*p == SPECIAL_CHAR) {
2200 /* draw string before special */
2207 switch (specials[special_index].type) {
2208 case HORIZONTAL_LINE:
2211 specials[special_index].height;
2212 int mid = font_ascent() / 2;
2213 w = text_start_x + text_width -
2216 XSetLineAttributes(display,
2221 XDrawLine(display, window.drawable,
2223 cur_y - mid, cur_x + w,
2231 specials[special_index].height;
2233 specials[special_index].arg;
2234 int mid = font_ascent() / 2;
2235 char ss[2] = { s, s };
2236 w = text_start_x + text_width -
2239 XSetLineAttributes(display,
2244 XSetDashes(display, window.gc, 0,
2246 XDrawLine(display, window.drawable,
2248 cur_y - mid, cur_x + w,
2256 specials[special_index].height;
2258 specials[special_index].arg;
2260 cur_y - (font_ascent() +
2262 w = specials[special_index].width;
2265 text_width - cur_x - 1;
2269 XSetLineAttributes(display,
2275 XDrawRectangle(display,
2279 XFillRectangle(display,
2283 w * bar_usage / 255,
2289 if (draw_mode == FG)
2290 set_foreground_color(specials
2296 if (draw_mode == BG)
2297 set_foreground_color(specials
2303 if (draw_mode == OUTLINE)
2304 set_foreground_color(specials
2311 int pos_x = text_start_x + text_width - cur_x - 1 - get_string_width(p);
2312 if ( pos_x > specials[special_index].arg)
2313 w = pos_x - specials[special_index].arg;
2319 int pos_x = text_start_x + text_width - cur_x - 1 - get_string_width(p)/2 - (text_width/2);
2320 if ( pos_x > specials[special_index].arg)
2321 w = pos_x - specials[special_index].arg;
2337 cur_y += font_descent();
2340 static void draw_text()
2342 cur_y = text_start_y;
2345 if (draw_borders && border_width > 0) {
2346 unsigned int b = (border_width + 1) / 2;
2348 if (stippled_borders) {
2350 { stippled_borders, stippled_borders };
2351 XSetLineAttributes(display, window.gc,
2352 border_width, LineOnOffDash,
2353 CapButt, JoinMiter);
2354 XSetDashes(display, window.gc, 0, ss, 2);
2356 XSetLineAttributes(display, window.gc,
2357 border_width, LineSolid,
2358 CapButt, JoinMiter);
2361 XDrawRectangle(display, window.drawable, window.gc,
2362 text_start_x - border_margin + b,
2363 text_start_y - border_margin + b,
2364 text_width + border_margin * 2 - 1 - b * 2,
2365 text_height + border_margin * 2 - 1 -
2371 for_each_line(text_buffer, draw_line);
2374 static void draw_stuff()
2376 if (draw_shades && !draw_outline) {
2379 set_foreground_color(default_bg_color);
2388 for (i = -1; i < 2; i++)
2389 for (j = -1; j < 2; j++) {
2390 if (i == 0 && j == 0)
2394 set_foreground_color(default_out_color);
2395 draw_mode = OUTLINE;
2402 set_foreground_color(default_fg_color);
2409 swap.swap_window = window.window;
2410 swap.swap_action = XdbeBackground;
2411 XdbeSwapBuffers(display, &swap, 1);
2414 /*#ifdef METAR wtf is this for exactly? aside from trying to cause segfaults?
2415 if (metar_station != NULL) {
2416 free(metar_station);
2417 metar_station = NULL;
2419 if (metar_server != NULL) {
2421 metar_server = NULL;
2423 if (metar_path != NULL) {
2431 static void clear_text(int exposures)
2435 return; /* The swap action is XdbeBackground, which clears */
2437 /* there is some extra space for borders and outlines */
2438 XClearArea(display, window.drawable,
2439 text_start_x - border_margin - 1,
2440 text_start_y - border_margin - 1,
2441 text_width + border_margin * 2 + 2,
2442 text_height + border_margin * 2 + 2,
2443 exposures ? True : 0);
2446 static int need_to_update;
2448 /* update_text() generates new text and clears old text area */
2449 static void update_text()
2456 static void main_loop()
2458 Region region = XCreateRegion();
2460 while (total_run_times == 0 || info.looped < total_run_times) {
2464 /* wait for X event or timeout */
2466 if (!XPending(display)) {
2471 update_interval - (get_time() -
2477 tv.tv_sec = (long) t;
2478 tv.tv_usec = (long) (t * 1000000) % 1000000;
2481 FD_SET(ConnectionNumber(display), &fdsr);
2483 s = select(ConnectionNumber(display) + 1, &fdsr, 0,
2487 ERR("can't select(): %s",
2496 if (need_to_update) {
2498 int wx = window.x, wy = window.y;
2507 /* resize window if it isn't right size */
2509 (text_width + border_margin * 2 !=
2511 || text_height + border_margin * 2 !=
2515 border_margin * 2 + 1;
2518 border_margin * 2 + 1;
2519 XResizeWindow(display,
2525 /* move window if it isn't in right position */
2528 || window.y != wy)) {
2529 XMoveWindow(display, window.window,
2530 window.x, window.y);
2540 r.x = text_start_x - border_margin;
2541 r.y = text_start_y - border_margin;
2542 r.width = text_width + border_margin * 2;
2543 r.height = text_height + border_margin * 2;
2544 XUnionRectWithRegion(&r, region, region);
2549 /* handle X events */
2551 while (XPending(display)) {
2553 XNextEvent(display, &ev);
2561 r.width = ev.xexpose.width;
2562 r.height = ev.xexpose.height;
2563 XUnionRectWithRegion(&r, region,
2569 case ReparentNotify:
2570 /* set background to ParentRelative for all parents */
2572 set_transparent_background(window.
2576 case ConfigureNotify:
2578 /* if window size isn't what expected, set fixed size */
2579 if (ev.xconfigure.width !=
2581 || ev.xconfigure.height !=
2583 if (window.width != 0
2584 && window.height != 0)
2587 /* clear old stuff before screwing up size and pos */
2593 if (XGetWindowAttributes(display, window.window, &attrs)) {
2608 border_margin * 2 - 1;
2611 border_margin * 2 - 1;
2614 /* if position isn't what expected, set fixed pos, total_updates
2615 * avoids setting fixed_pos when window is set to weird locations
2617 if (total_updates >= 2
2619 && (window.x != ev.xconfigure.x
2622 && (ev.xconfigure.x != 0
2623 || ev.xconfigure.y != 0)) {
2635 /* XDBE doesn't seem to provide a way to clear the back buffer without
2636 * interfering with the front buffer, other than passing XdbeBackground
2637 * to XdbeSwapBuffers. That means that if we're using XDBE, we need to
2638 * redraw the text even if it wasn't part of the exposed area. OTOH,
2639 * if we're not going to call draw_stuff at all, then no swap happens
2640 * and we can safely do nothing.
2643 if (!XEmptyRegion(region)) {
2647 r.x = text_start_x - border_margin;
2648 r.y = text_start_y - border_margin;
2649 r.width = text_width + border_margin * 2;
2650 r.height = text_height + border_margin * 2;
2651 XUnionRectWithRegion(&r, region, region);
2654 XSetRegion(display, window.gc, region);
2657 XftDrawSetClip(window.xftdraw, region);
2660 XDestroyRegion(region);
2661 region = XCreateRegion();
2666 static void load_font()
2671 if (xftfont != NULL)
2672 XftFontClose(display, xftfont);
2675 XftFontOpenName(display, screen, font_name)) != NULL)
2678 ERR("can't load Xft font '%s'", font_name);
2680 XftFontOpenName(display, screen,
2681 "courier-12")) != NULL)
2684 ERR("can't load Xft font '%s'", "courier-12");
2686 if ((font = XLoadQueryFont(display, "fixed")) == NULL) {
2687 CRIT_ERR("can't load font '%s'", "fixed");
2695 /* load normal font */
2697 XFreeFont(display, font);
2699 if ((font = XLoadQueryFont(display, font_name)) == NULL) {
2700 ERR("can't load font '%s'", font_name);
2701 if ((font = XLoadQueryFont(display, "fixed")) == NULL) {
2702 CRIT_ERR("can't load font '%s'", "fixed");
2707 static void set_font()
2711 if (window.xftdraw != NULL)
2712 XftDrawDestroy(window.xftdraw);
2713 window.xftdraw = XftDrawCreate(display, window.drawable,
2714 DefaultVisual(display,
2716 DefaultColormap(display,
2721 XSetFont(display, window.gc, font->fid);
2725 static void load_config_file(const char *);
2727 /* signal handler that reloads config file */
2728 static void reload_handler(int a)
2730 fprintf(stderr, "Conky: received signal %d, reloading config\n",
2733 if (current_config) {
2735 load_config_file(current_config);
2738 extract_variable_text(text);
2745 static void clean_up()
2749 XdbeDeallocateBackBufferName(display, window.back_buffer);
2753 XDestroyWindow(display, window.window);
2761 XFreeGC(display, window.gc);
2763 /* it is really pointless to free() memory at the end of program but ak|ra
2764 * wants me to do this */
2766 free_text_objects();
2768 if (text != original_text)
2771 free(current_config);
2772 free(current_mail_spool);
2778 static void term_handler(int a)
2780 a = a; /* to get rid of warning */
2785 static int string_to_bool(const char *s)
2789 if (strcasecmp(s, "yes") == 0)
2791 if (strcasecmp(s, "true") == 0)
2793 if (strcasecmp(s, "1") == 0)
2798 static enum alignment string_to_alignment(const char *s)
2800 if (strcasecmp(s, "top_left") == 0)
2802 else if (strcasecmp(s, "top_right") == 0)
2804 else if (strcasecmp(s, "bottom_left") == 0)
2806 else if (strcasecmp(s, "bottom_right") == 0)
2807 return BOTTOM_RIGHT;
2808 else if (strcasecmp(s, "tl") == 0)
2810 else if (strcasecmp(s, "tr") == 0)
2812 else if (strcasecmp(s, "bl") == 0)
2814 else if (strcasecmp(s, "br") == 0)
2815 return BOTTOM_RIGHT;
2820 static void set_default_configurations(void)
2822 text_alignment = BOTTOM_LEFT;
2823 fork_to_background = 0;
2826 total_run_times = 0;
2827 info.cpu_avg_samples = 2;
2828 info.net_avg_samples = 2;
2831 info.mpd.host = "localhost";
2832 info.mpd.port = 6600;
2833 info.mpd.status = "Checking status...";
2838 default_fg_color = WhitePixel(display, screen);
2839 default_bg_color = BlackPixel(display, screen);
2840 default_out_color = BlackPixel(display, screen);
2847 font_name = strdup("courier-12");
2849 font_name = strdup("6x10");
2854 free(current_mail_spool);
2857 variable_substitute(MAIL_FILE, buf, 256);
2859 current_mail_spool = strdup(buf);
2868 stippled_borders = 0;
2869 update_interval = 10.0;
2870 stuff_in_upper_case = 0;
2872 mlconfig.mldonkey_hostname="127.0.0.1";
2873 mlconfig.mldonkey_port=4001;
2874 mlconfig.mldonkey_login=NULL;
2875 mlconfig.mldonkey_password=NULL;
2878 metar_station = NULL;
2879 metar_server = NULL;
2881 last_metar_update = 0;
2885 static void load_config_file(const char *f)
2887 #define CONF_ERR ERR("%s: %d: config file error", f, line);
2891 set_default_configurations();
2893 fp = open_file(f, 0);
2898 char buf[256], *p, *p2, *name, *value;
2900 if (fgets(buf, 256, fp) == NULL)
2905 /* break at comment */
2906 p2 = strchr(p, '#');
2911 while (*p && isspace((int) *p))
2914 continue; /* empty line */
2920 while (*p2 && !isspace((int) *p2))
2923 *p2 = '\0'; /* break at name's end */
2930 while (*p && isspace((int) *p))
2935 p2 = value + strlen(value);
2936 while (isspace((int) *(p2 - 1)))
2942 #define CONF2(a) if (strcasecmp(name, a) == 0)
2943 #define CONF(a) else CONF2(a)
2944 #define CONF3(a,b) \
2945 else if (strcasecmp(name, a) == 0 || strcasecmp(name, a) == 0)
2948 CONF2("alignment") {
2950 int a = string_to_alignment(value);
2957 CONF("background") {
2958 fork_to_background = string_to_bool(value);
2960 CONF("border_margin") {
2963 strtol(value, 0, 0);
2966 CONF("border_width") {
2969 strtol(value, 0, 0);
2972 CONF("default_color") {
2980 ("default_shade_color",
2981 "default_shadecolor")
2991 ("default_outline_color",
2992 "default_outlinecolor")
3021 if (info.mpd.port < 1 || info.mpd.port > 0xffff)
3025 CONF("cpu_avg_samples") {
3033 if (cpu_avg_samples < 1 || cpu_avg_samples > 14)
3042 CONF("net_avg_samples") {
3050 if (net_avg_samples < 1 || net_avg_samples > 14)
3066 CONF("double_buffer") {
3073 CONF("draw_borders") {
3079 CONF("draw_shades") {
3085 CONF("draw_outline") {
3091 CONF("out_to_console") {
3097 CONF("top_sort_cpu") {
3103 CONF("use_spacer") {
3117 /* font silently ignored when Xft */
3132 if (string_to_bool(value))
3133 ERR("Xft not enabled");
3136 /* xftfont silently ignored when no Xft */
3139 /* xftalpha is silently ignored when no Xft */
3167 CONF("mail_spool") {
3179 if (current_mail_spool)
3180 free(current_mail_spool);
3188 CONF("minimum_size") {
3190 if (sscanf(value, "%d %d", &minimum_width, &minimum_height) != 2)
3191 if (sscanf(value, "%d", &minimum_width) != 1)
3195 CONF("no_buffers") {
3198 string_to_bool(value);
3201 CONF("mldonkey_hostname") {
3203 mlconfig.mldonkey_hostname = strdup(value);
3207 CONF("mldonkey_port") {
3209 mlconfig.mldonkey_port = atoi(value);
3213 CONF("mldonkey_login") {
3215 mlconfig.mldonkey_login = strdup(value);
3219 CONF("mldonkey_password") {
3221 mlconfig.mldonkey_password = strdup(value);
3227 CONF("own_window") {
3234 CONF("pad_percents") {
3240 CONF("stippled_borders") {
3254 ERR("temp1 configuration is obsolete, use ${i2c <i2c device here> temp 1}");
3257 ERR("temp2 configuration is obsolete, use ${i2c <i2c device here> temp 2}");
3259 CONF("update_interval") {
3267 CONF_ERR} CONF("total_run_times") {
3299 CONF("metar_station"){
3300 metar_station = (char*)malloc(strlen(value) + 5);
3301 strcpy(metar_station, value);
3302 strcat(metar_station,".TXT");
3304 CONF("metar_server"){
3305 metar_server = (char*)malloc(strlen(value) + 1);
3306 strcpy(metar_server, value);
3309 metar_path = (char*)malloc(strlen(value) + 1);
3310 strcpy(metar_path, value);
3315 if (text != original_text)
3335 if (fgets(buf, 256, fp) == NULL)
3350 if (strlen(text) > 1024 * 8)
3358 ERR("%s: %d: no such configuration: '%s'", f, line, name);
3369 /* : means that character before that takes an argument */
3375 "vVdt:f:u:i:hc:w:x:y:a:"
3385 int main(int argc, char **argv) {
3386 /* handle command line parameters that don't change configs */
3390 int c = getopt(argc,
3411 /* if current_config is set to a strdup of CONFIG_FILE, free it (even
3412 * though free() does the NULL check itself;), then load optarg value */
3414 free(current_config);
3423 ("Usage: %s [OPTION]...\n"
3424 "Conky is a system monitor that renders text on desktop or to own transparent\n"
3425 "window. Command line options will override configurations defined in config\n"
3428 " -a ALIGNMENT text alignment on screen, {top,bottom}_{left,right}\n"
3429 " -c FILE config file to load instead of "
3432 " -d daemonize, fork to background\n"
3433 " -f FONT font to use\n"
3436 " -o create own window to draw\n"
3439 " -b double buffer (prevents flickering)\n"
3441 " -t TEXT text to render, remember single quotes, like -t '$uptime'\n"
3442 " -u SECS update interval\n"
3443 " -i NUM number of times to update Conky\n"
3444 " -w WIN_ID window id to draw\n"
3445 " -x X x position\n"
3446 " -y Y y position\n",
3466 /* initalize X BEFORE we load config. (we need to so that 'screen' is set) */
3470 tmpstring1 = (char*)malloc(2*TEXT_BUFFER_SIZE);
3471 tmpstring2 = (char*)malloc(2*TEXT_BUFFER_SIZE);
3473 /* load current_config or CONFIG_FILE */
3476 if (current_config == NULL) {
3477 /* load default config file */
3493 if (current_config != NULL)
3497 set_default_configurations
3501 if (current_mail_spool == NULL) {
3516 /* handle other command line arguments */
3523 int c = getopt(argc,
3568 if (text != original_text)
3570 text = strdup(optarg);
3614 /* generate text and get initial size */
3615 extract_variable_text
3617 if (text != original_text)
3626 update_text_area(); /* to get initial size of the window */
3645 update_text_area(); /* to position text/window on screen */
3648 // why the fuck not?
3674 if (fork_to_background) {
3680 ERR("can't fork() to background: %s", strerror(errno));
3689 "Conky: forked to background, pid is %d\n",
3695 /* set SIGUSR1, SIGINT and SIGTERM handlers */
3701 sa.sa_handler = reload_handler;
3705 sa.sa_flags = SA_RESTART;
3706 if (sigaction(SIGUSR1, &sa, NULL) != 0)
3707 ERR("can't set signal handler for SIGUSR1: %s", strerror(errno));
3709 sa.sa_handler = term_handler;
3713 sa.sa_flags = SA_RESTART;
3714 if (sigaction(SIGINT, &sa, NULL) != 0)
3715 ERR("can't set signal handler for SIGINT: %s", strerror(errno));
3717 sa.sa_handler = term_handler;
3721 sa.sa_flags = SA_RESTART;
3722 if (sigaction(SIGTERM, &sa, NULL) != 0)
3723 ERR("can't set signal handler for SIGTERM: %s", strerror(errno));