all repos — slstatus @ f13104156f20c0e394297c56550ff8a948ff1c1a

my build of slstatus (tools.suckless.org/slstatus/)

battery_state() function added
Aaron Marcher info@nulltime.net
Tue, 13 Sep 2016 22:03:36 +0200
commit

f13104156f20c0e394297c56550ff8a948ff1c1a

parent

7aad78bd95cb265306f1ab81844c7bcd66054068

3 files changed, 34 insertions(+), 1 deletions(-)

jump to
M README.mdREADME.md

@@ -8,7 +8,7 @@ Looking at the LOC (lines of code) of the [Conky project](https://github.com/brndnmtthws/conky), very interesting: *28.346 lines C++, 219 lines Python and 110 lines Lua*. slstatus currently has about **800 lines of clean documented C code** and even includes additional possibilities as it can be customized and extended very easily. Configure it by customizing the config.h (C header file) which is secure and fast as no config files are parsed at runtime.

The following information is included: -- Battery percentage +- Battery percentage/state - CPU usage (in percent) - Custom shell commands - Date and time
M config.def.hconfig.def.h

@@ -8,6 +8,7 @@ #define UNKNOWN_STR "n/a"

/* 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]
M slstatus.cslstatus.c

@@ -40,6 +40,7 @@ };

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 *);

@@ -111,6 +112,37 @@ fscanf(fp, "%i", &perc);

fclose(fp); 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 *