summaryrefslogtreecommitdiffstats
path: root/linux/Battery.c
diff options
context:
space:
mode:
authorJan Palus <atler@pld-linux.org>2020-10-23 00:59:26 +0200
committercgzones <cgzones@googlemail.com>2020-10-26 19:03:09 +0100
commit167adc0a2b4a940cae6c9eb71f3185b5d2d3b4fa (patch)
treed10792bef6706635f3d54950d643516a927ac8ca /linux/Battery.c
parent94e32cf1e80d145c3028435e547194016dd8e93d (diff)
Parse POWER_SUPPLY_CAPACITY
If POWER_SUPPLY_{CHARGE,ENERGY}_NOW is missing then try to use POWER_SUPPLY_CAPACITY to determine current charge level.
Diffstat (limited to 'linux/Battery.c')
-rw-r--r--linux/Battery.c12
1 files changed, 11 insertions, 1 deletions
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;