From 167adc0a2b4a940cae6c9eb71f3185b5d2d3b4fa Mon Sep 17 00:00:00 2001 From: Jan Palus Date: Fri, 23 Oct 2020 00:59:26 +0200 Subject: [PATCH] Parse POWER_SUPPLY_CAPACITY If POWER_SUPPLY_{CHARGE,ENERGY}_NOW is missing then try to use POWER_SUPPLY_CAPACITY to determine current charge level. --- linux/Battery.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/linux/Battery.c b/linux/Battery.c index 4eab24dd..0822ee9a 100644 --- a/linux/Battery.c +++ b/linux/Battery.c @@ -230,6 +230,8 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { char *line = NULL; bool full = false; bool now = false; + int fullSize = 0; + double capacityLevel = NAN; while ((line = strsep(&buf, "\n")) != NULL) { #define match(str,prefix) \ (String_startsWith(str,prefix) ? (str) + strlen(prefix) : NULL) @@ -237,6 +239,10 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { if (!ps) { continue; } + const char* capacity = match(ps, "CAPACITY="); + if (capacity) { + capacityLevel = atoi(capacity) / 100.0; + } const char* energy = match(ps, "ENERGY_"); if (!energy) { energy = match(ps, "CHARGE_"); @@ -246,7 +252,8 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { } const char* value = (!full) ? match(energy, "FULL=") : NULL; if (value) { - totalFull += atoi(value); + fullSize = atoi(value); + totalFull += fullSize; full = true; if (now) break; continue; @@ -260,6 +267,9 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { } } #undef match + if (!now && full && !isnan(capacityLevel)) { + totalRemain += (capacityLevel * fullSize); + } } else if (entryName[0] == 'A') { if (*isOnAC != AC_ERROR) { continue;