From 75b18533135e3171157bedc731be5f0ee98937f3 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 18:54:15 +0200 Subject: Fixes issue #15. Forgot to close() a socket in wifi_essid()... --- slstatus.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/slstatus.c b/slstatus.c index e38b963..841fb47 100644 --- a/slstatus.c +++ b/slstatus.c @@ -568,6 +568,8 @@ wifi_essid(const char *wificard) return smprintf(UNKNOWN_STR); } + close(sockfd); + if (strcmp((char *)wreq.u.essid.pointer, "") == 0) return smprintf(UNKNOWN_STR); else -- cgit v1.2.3 From a1c962f8a385dc6da5ea84fe0363a3f71f342d06 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 18:57:56 +0200 Subject: simplify status_string clearing --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 841fb47..6e86092 100644 --- a/slstatus.c +++ b/slstatus.c @@ -587,7 +587,7 @@ main(void) dpy = XOpenDisplay(NULL); for (;;) { - memset(status_string, 0, sizeof(status_string)); + status_string[0] = '\0'; for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; if (argument.args == NULL) -- cgit v1.2.3 From 67d3f9c5405b633df7bae93c330bbe313e0c9e94 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 19:09:01 +0200 Subject: signal handling to exit gracefully + small coding style fixes --- slstatus.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index 6e86092..cb5f1f2 100644 --- a/slstatus.c +++ b/slstatus.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -62,9 +63,11 @@ static char *username(void); static char *vol_perc(const char *); static char *wifi_perc(const char *); static char *wifi_essid(const char *); +static void sighandler(const int); static unsigned short int delay; static Display *dpy; +static int done = 0; #include "config.h" @@ -470,7 +473,7 @@ uid(void) } -static char * +static char * vol_perc(const char *snd_card) { /* FIX THIS SHIT! */ long int vol, max, min; @@ -516,7 +519,7 @@ wifi_perc(const char *wificard) fp = fopen(concat, "r"); - if(fp == NULL) { + if (fp == NULL) { warn("Error opening wifi operstate file"); return smprintf(UNKNOWN_STR); } @@ -558,7 +561,7 @@ wifi_essid(const char *wificard) memset(&wreq, 0, sizeof(struct iwreq)); wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; sprintf(wreq.ifr_name, wificard); - if(sockfd == -1) { + if (sockfd == -1) { warn("Cannot open socket for interface: %s", wificard); return smprintf(UNKNOWN_STR); } @@ -576,6 +579,14 @@ wifi_essid(const char *wificard) return smprintf("%s", (char *)wreq.u.essid.pointer); } +static void +sighandler(int signo) +{ + if (signo == SIGTERM || signo == SIGINT) { + done = 1; + } +} + int main(void) { @@ -583,10 +594,16 @@ main(void) char status_string[4096]; char *res, *element; struct arg argument; + struct sigaction act; + + memset(&act, 0, sizeof(act)); + act.sa_handler = sighandler; + sigaction(SIGINT, &act, 0); + sigaction(SIGTERM, &act, 0); dpy = XOpenDisplay(NULL); - for (;;) { + while (!done) { status_string[0] = '\0'; for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { argument = args[i]; -- cgit v1.2.3 From ba2947ae456c7afa1a6b060d79a37f08d1f6b824 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 19:21:54 +0200 Subject: sighandle forgot const --- slstatus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index cb5f1f2..7babe1f 100644 --- a/slstatus.c +++ b/slstatus.c @@ -580,7 +580,7 @@ wifi_essid(const char *wificard) } static void -sighandler(int signo) +sighandler(const int signo) { if (signo == SIGTERM || signo == SIGINT) { done = 1; -- cgit v1.2.3 From 98e7324010626c5ba05d199190d9416359cf801e Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 19:34:25 +0200 Subject: set old WM_NAME content before exiting --- slstatus.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/slstatus.c b/slstatus.c index 7babe1f..991334a 100644 --- a/slstatus.c +++ b/slstatus.c @@ -592,7 +592,7 @@ main(void) { size_t i; char status_string[4096]; - char *res, *element; + char *res, *element, *status_old; struct arg argument; struct sigaction act; @@ -603,6 +603,8 @@ main(void) dpy = XOpenDisplay(NULL); + XFetchName(dpy, DefaultRootWindow(dpy), &status_old); + while (!done) { status_string[0] = '\0'; for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) { @@ -630,6 +632,9 @@ main(void) delay = 0; } + XStoreName(dpy, DefaultRootWindow(dpy), status_old); + XSync(dpy, False); + XCloseDisplay(dpy); return 0; -- cgit v1.2.3 From 60df4f0f05e261d2787b9512cea262248ebaf166 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 19:35:54 +0200 Subject: loop's not needed anymore --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 9f24bdf..534eee0 100644 --- a/README.md +++ b/README.md @@ -39,11 +39,7 @@ Before you continue, please be sure that a C compiler (preferrably gcc), GNU mak Write the following code to your ~/.xinitrc (or any other initialization script): - while true; do - slstatus - done & - -The loop is needed that the program runs after suspend to ram. + slstatus & ## Contributing -- cgit v1.2.3 From 2d1bbf0d35f096803a96afbc8ae89a057e646d9f Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 20:54:45 +0200 Subject: simplified battery_perc() a lot and removed useless options from config.def.h --- config.def.h | 5 ----- slstatus.c | 30 ++++++++++++++++-------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/config.def.h b/config.def.h index b105869..8cf8e56 100644 --- a/config.def.h +++ b/config.def.h @@ -3,11 +3,6 @@ /* alsa sound */ #define ALSA_CHANNEL "Master" -/* battery */ -#define BATTERY_PATH "/sys/class/power_supply/" -#define BATTERY_NOW "energy_now" -#define BATTERY_FULL "energy_full_design" - /* how often to update the statusbar (min value == 1) */ #define UPDATE_INTERVAL 1 diff --git a/slstatus.c b/slstatus.c index 991334a..bf3c826 100644 --- a/slstatus.c +++ b/slstatus.c @@ -98,34 +98,36 @@ smprintf(const char *fmt, ...) static char * battery_perc(const char *battery) { - int now, full, perc; + int now, full; FILE *fp; - ccat(4, BATTERY_PATH, battery, "/", BATTERY_NOW); - + ccat(3, "/sys/class/power_supply/", battery, "/energy_now"); fp = fopen(concat, "r"); if (fp == NULL) { - warn("Error opening battery file: %s", concat); - return smprintf(UNKNOWN_STR); + ccat(4, "/sys/class/power_supply/", battery, "/charge_now"); + fp = fopen(concat, "r"); + if (fp == NULL) { + warn("Error opening battery file: %s", concat); + return smprintf(UNKNOWN_STR); + } } - fscanf(fp, "%i", &now); fclose(fp); - ccat(4, BATTERY_PATH, battery, "/", BATTERY_FULL); - + ccat(3, "/sys/class/power_supply/", battery, "/energy_full"); fp = fopen(concat, "r"); if (fp == NULL) { - warn("Error opening battery file: %s", concat); - return smprintf(UNKNOWN_STR); + ccat(4, "/sys/class/power_supply/", battery, "/charge_full"); + fp = fopen(concat, "r"); + if (fp == NULL) { + warn("Error opening battery file: %s", concat); + return smprintf(UNKNOWN_STR); + } } - fscanf(fp, "%i", &full); fclose(fp); - perc = now / (full / 100); - - return smprintf("%d%%", perc); + return smprintf("%d%%", now / (full / 100)); } static char * -- cgit v1.2.3 From 74b6e340f2be27f82b4dcfc95fab47cbc0a5843b Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 21:05:49 +0200 Subject: lol, battery_perc() is even simpler, fuck this shit :D --- slstatus.c | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/slstatus.c b/slstatus.c index bf3c826..f61e987 100644 --- a/slstatus.c +++ b/slstatus.c @@ -98,36 +98,19 @@ smprintf(const char *fmt, ...) static char * battery_perc(const char *battery) { - int now, full; + int perc; FILE *fp; - ccat(3, "/sys/class/power_supply/", battery, "/energy_now"); - fp = fopen(concat, "r"); - if (fp == NULL) { - ccat(4, "/sys/class/power_supply/", battery, "/charge_now"); - fp = fopen(concat, "r"); - if (fp == NULL) { - warn("Error opening battery file: %s", concat); - return smprintf(UNKNOWN_STR); - } - } - fscanf(fp, "%i", &now); - fclose(fp); - - ccat(3, "/sys/class/power_supply/", battery, "/energy_full"); + ccat(3, "/sys/class/power_supply/", battery, "/capacity"); fp = fopen(concat, "r"); if (fp == NULL) { - ccat(4, "/sys/class/power_supply/", battery, "/charge_full"); - fp = fopen(concat, "r"); - if (fp == NULL) { - warn("Error opening battery file: %s", concat); - return smprintf(UNKNOWN_STR); - } + warn("Error opening battery file: %s", concat); + return smprintf(UNKNOWN_STR); } - fscanf(fp, "%i", &full); + fscanf(fp, "%i", &perc); fclose(fp); - return smprintf("%d%%", now / (full / 100)); + return smprintf("%d%%", perc); } static char * -- cgit v1.2.3 From 87c1377b08a9001a16943942e9d67458d8d19c33 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 21:08:44 +0200 Subject: simplified vol_perc() (and with that config.def.h is super clean) --- config.def.h | 3 --- slstatus.c | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/config.def.h b/config.def.h index 8cf8e56..a445aa7 100644 --- a/config.def.h +++ b/config.def.h @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -/* alsa sound */ -#define ALSA_CHANNEL "Master" - /* how often to update the statusbar (min value == 1) */ #define UPDATE_INTERVAL 1 diff --git a/slstatus.c b/slstatus.c index f61e987..1b45f64 100644 --- a/slstatus.c +++ b/slstatus.c @@ -459,7 +459,7 @@ uid(void) static char * -vol_perc(const char *snd_card) +vol_perc(const char *soundcard) { /* FIX THIS SHIT! */ long int vol, max, min; snd_mixer_t *handle; @@ -471,7 +471,7 @@ vol_perc(const char *snd_card) snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); snd_mixer_selem_id_malloc(&s_elem); - snd_mixer_selem_id_set_name(s_elem, ALSA_CHANNEL); + snd_mixer_selem_id_set_name(s_elem, "Master"); elem = snd_mixer_find_selem(handle, s_elem); if (elem == NULL) { -- cgit v1.2.3 From 7aad78bd95cb265306f1ab81844c7bcd66054068 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 21:11:11 +0200 Subject: fixed vol_perc() typo and added % sign again --- slstatus.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slstatus.c b/slstatus.c index 1b45f64..fa91cce 100644 --- a/slstatus.c +++ b/slstatus.c @@ -460,14 +460,14 @@ uid(void) static char * vol_perc(const char *soundcard) -{ /* FIX THIS SHIT! */ +{ long int vol, max, min; snd_mixer_t *handle; snd_mixer_elem_t *elem; snd_mixer_selem_id_t *s_elem; snd_mixer_open(&handle, 0); - snd_mixer_attach(handle, snd_card); + snd_mixer_attach(handle, soundcard); snd_mixer_selem_register(handle, NULL, NULL); snd_mixer_load(handle); snd_mixer_selem_id_malloc(&s_elem); @@ -477,7 +477,7 @@ vol_perc(const char *soundcard) if (elem == NULL) { snd_mixer_selem_id_free(s_elem); snd_mixer_close(handle); - warn("error: ALSA"); + warn("Failed to get volume percentage for: %s.", soundcard); return smprintf(UNKNOWN_STR); } @@ -488,7 +488,7 @@ vol_perc(const char *soundcard) snd_mixer_selem_id_free(s_elem); snd_mixer_close(handle); - return smprintf("%d", ((uint_fast16_t)(vol * 100) / max)); + return smprintf("%d%%", ((uint_fast16_t)(vol * 100) / max)); } static char * -- cgit v1.2.3 From f13104156f20c0e394297c56550ff8a948ff1c1a Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Tue, 13 Sep 2016 22:03:36 +0200 Subject: battery_state() function added --- README.md | 2 +- config.def.h | 1 + slstatus.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 534eee0..686922c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Looking at the LOC (lines of code) of the [Conky project](https://github.com/brn The following information is included: -- Battery percentage +- Battery percentage/state - CPU usage (in percent) - Custom shell commands - Date and time diff --git a/config.def.h b/config.def.h index a445aa7..4f56eb0 100644 --- a/config.def.h +++ b/config.def.h @@ -8,6 +8,7 @@ /* statusbar - battery_perc (battery percentage) [argument: battery name] +- battery_state (battery charging state) [argument: battery name] - cpu_perc (cpu usage in percent) [argument: NULL] - datetime (date and time) [argument: format] - disk_free (disk usage in percent) [argument: mountpoint] diff --git a/slstatus.c b/slstatus.c index fa91cce..1d46674 100644 --- a/slstatus.c +++ b/slstatus.c @@ -40,6 +40,7 @@ struct arg { static char *smprintf(const char *, ...); static char *battery_perc(const char *); +static char *battery_state(const char *); static char *cpu_perc(void); static char *datetime(const char *); static char *disk_free(const char *); @@ -113,6 +114,37 @@ battery_perc(const char *battery) return smprintf("%d%%", perc); } +static char * +battery_state(const char *battery) +{ + char *state = malloc(sizeof(char)*12); + FILE *fp; + + if (!state) { + warn("Failed to get battery state."); + return smprintf(UNKNOWN_STR); + } + + + ccat(3, "/sys/class/power_supply/", battery, "/status"); + fp = fopen(concat, "r"); + if (fp == NULL) { + warn("Error opening battery file: %s", concat); + return smprintf(UNKNOWN_STR); + } + fscanf(fp, "%s", state); + fclose(fp); + + if (strcmp(state, "Charging") == 0) + return smprintf("+"); + else if (strcmp(state, "Discharging") == 0) + return smprintf("-"); + else if (strcmp(state, "Full") == 0) + return smprintf("="); + else + return smprintf("?"); +} + static char * cpu_perc(void) { -- cgit v1.2.3