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 --- slstatus.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'slstatus.c') 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