X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/b176d0a2d0e3048f22b35083ea7d6571aaa69260..27b9139dfc02b76801a5a33c6eef199c822d187a:/slstatus.c diff --git a/slstatus.c b/slstatus.c index d6f5e2d..ee6b160 100644 --- a/slstatus.c +++ b/slstatus.c @@ -15,30 +15,8 @@ #include #include -/* global variables */ -static Display *dpy; - -/* statusbar configuration type and struct */ -typedef char *(*op_fun) (const char *); -struct arg { - op_fun func; - const char *format; - const char *args; -}; - -/* functions */ -void setstatus(const char *); -char *smprintf(const char *, ...); -char *get_battery(const char *); -char *get_cpu_temperature(const char *); -char *get_cpu_usage(const char *); -char *get_datetime(const char *); -char *get_diskusage(const char *); -char *get_ram_usage(const char *); -char *get_volume(const char *); -char *get_wifi_signal(const char *); - -/* include config header */ +/* local headers */ +#include "slstatus.h" #include "config.h" /* set statusbar */ @@ -67,7 +45,7 @@ smprintf(const char *fmt, ...) /* battery percentage */ char * -get_battery(const char *battery) +battery_perc(const char *battery) { int now, full, perc; char batterynowfile[64] = ""; @@ -117,32 +95,9 @@ get_battery(const char *battery) return smprintf("%d%%", perc); } -/* cpu temperature */ -char * -get_cpu_temperature(const char *file) -{ - int temperature; - FILE *fp; - - /* open temperature file */ - if (!(fp = fopen(file, "r"))) { - fprintf(stderr, "Could not open temperature file.\n"); - return smprintf("n/a"); - } - - /* extract temperature */ - fscanf(fp, "%d", &temperature); - - /* close temperature file */ - fclose(fp); - - /* return temperature in degrees */ - return smprintf("%d°C", temperature / 1000); -} - /* cpu percentage */ char * -get_cpu_usage(const char *null) +cpu_perc(const char *null) { int perc; long double a[4], b[4]; @@ -184,7 +139,7 @@ get_cpu_usage(const char *null) /* date and time */ char * -get_datetime(const char *timeformat) +datetime(const char *timeformat) { time_t tm; size_t bufsize = 64; @@ -201,12 +156,14 @@ get_datetime(const char *timeformat) setlocale(LC_TIME, "C"); /* return time */ - return smprintf("%s", buf); + char *ret = smprintf("%s", buf); + free(buf); + return ret; } /* disk usage percentage */ char * -get_diskusage(const char *mountpoint) +disk_perc(const char *mountpoint) { int perc = 0; struct statvfs fs; @@ -226,7 +183,7 @@ get_diskusage(const char *mountpoint) /* ram percentage */ char * -get_ram_usage(const char *null) +ram_perc(const char *null) { int perc; long total, free, buffers, cached; @@ -254,9 +211,33 @@ get_ram_usage(const char *null) return smprintf("%d%%", perc); } +/* temperature */ +char * +temp(const char *file) +{ + int temperature; + FILE *fp; + + /* open temperature file */ + if (!(fp = fopen(file, "r"))) { + fprintf(stderr, "Could not open temperature file.\n"); + return smprintf("n/a"); + } + + /* extract temperature */ + fscanf(fp, "%d", &temperature); + + /* close temperature file */ + fclose(fp); + + /* return temperature in degrees */ + return smprintf("%d°C", temperature / 1000); +} + + /* alsa volume percentage */ char * -get_volume(const char *soundcard) +vol_perc(const char *soundcard) { int mute = 0; long vol = 0, max = 0, min = 0; @@ -294,7 +275,7 @@ get_volume(const char *soundcard) /* wifi percentage */ char * -get_wifi_signal(const char *wificard) +wifi_perc(const char *wificard) { int bufsize = 255; int strength; @@ -368,7 +349,7 @@ main() /* return status every interval */ for (;;) { /* clear the string */ - strcpy(status_string, ""); + memset(status_string, 0, sizeof(status_string)); /* generate status_string */ for (size_t i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { @@ -376,6 +357,8 @@ main() char *res = argument.func(argument.args); char *element = smprintf(argument.format, res); strcat(status_string, element); + free(res); + free(element); } /* return the statusbar */