Parse POWER_SUPPLY_CAPACITY

If POWER_SUPPLY_{CHARGE,ENERGY}_NOW is missing then try to use
POWER_SUPPLY_CAPACITY to determine current charge level.
This commit is contained in:
Jan Palus 2020-10-23 00:59:26 +02:00 committed by cgzones
parent 94e32cf1e8
commit 167adc0a2b
1 changed files with 11 additions and 1 deletions

View File

@ -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;