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; char *line = NULL;
bool full = false; bool full = false;
bool now = false; bool now = false;
int fullSize = 0;
double capacityLevel = NAN;
while ((line = strsep(&buf, "\n")) != NULL) { while ((line = strsep(&buf, "\n")) != NULL) {
#define match(str,prefix) \ #define match(str,prefix) \
(String_startsWith(str,prefix) ? (str) + strlen(prefix) : NULL) (String_startsWith(str,prefix) ? (str) + strlen(prefix) : NULL)
@ -237,6 +239,10 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
if (!ps) { if (!ps) {
continue; continue;
} }
const char* capacity = match(ps, "CAPACITY=");
if (capacity) {
capacityLevel = atoi(capacity) / 100.0;
}
const char* energy = match(ps, "ENERGY_"); const char* energy = match(ps, "ENERGY_");
if (!energy) { if (!energy) {
energy = match(ps, "CHARGE_"); energy = match(ps, "CHARGE_");
@ -246,7 +252,8 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
} }
const char* value = (!full) ? match(energy, "FULL=") : NULL; const char* value = (!full) ? match(energy, "FULL=") : NULL;
if (value) { if (value) {
totalFull += atoi(value); fullSize = atoi(value);
totalFull += fullSize;
full = true; full = true;
if (now) break; if (now) break;
continue; continue;
@ -260,6 +267,9 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) {
} }
} }
#undef match #undef match
if (!now && full && !isnan(capacityLevel)) {
totalRemain += (capacityLevel * fullSize);
}
} else if (entryName[0] == 'A') { } else if (entryName[0] == 'A') {
if (*isOnAC != AC_ERROR) { if (*isOnAC != AC_ERROR) {
continue; continue;