all repos — slstatus @ 259e967cbf855d1ed3886c7afafb1bfe6ab11645

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

add battery_power function
Aaron Marcher info@nulltime.net
Mon, 12 Jun 2017 23:55:27 +0200
commit

259e967cbf855d1ed3886c7afafb1bfe6ab11645

parent

870d68d44e33f9fb7a5402becbc0b5c363066bcc

2 files changed, 21 insertions(+), 0 deletions(-)

jump to
M config.def.hconfig.def.h

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

/* statusbar - battery_perc (battery percentage) [argument: battery name] +- battery_power (battery power usage) [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]
M slstatus.cslstatus.c

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

static char *smprintf(const char *fmt, ...); static char *battery_perc(const char *bat); +static char *battery_power(const char *bat); static char *battery_state(const char *bat); static char *cpu_perc(void); static char *datetime(const char *fmt);

@@ -116,6 +117,25 @@ fscanf(fp, "%i", &perc);

fclose(fp); return smprintf("%d%%", perc); +} + +static char * +battery_power(const char *bat) +{ + char path[PATH_MAX]; + FILE *fp; + int watts; + + snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/power_now"); + fp = fopen(path, "r"); + if (fp == NULL) { + warn("Failed to open file %s", path); + return smprintf("%s", UNKNOWN_STR); + } + fscanf(fp, "%i", &watts); + fclose(fp); + + return smprintf("%d", (watts + 500000) / 1000000); } static char *