Fixed bug 4920 - Wider support for seconds of battery life left on Linux platforms using sys interface

Murad

On my system, SDL_GetPowerInfo() returns -1 seconds of battery life left. I have quickly investigated that in my case SDL uses sys interface to get battery data. It tries to read "time_to_empty_now" file which is not always present. However, it is still possible to calculate remaining lifetime using "energy_now" and "power_now" files. This is what my simple patch (included as attachment) tries to accomplish.
Best wishes.
Sam Lantinga 2019-12-30 13:21:47 -08:00
parent 8b0660b25a
commit 58a11d30de
1 changed files with 10 additions and 3 deletions

View File

@ -451,6 +451,8 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
SDL_PowerState st; SDL_PowerState st;
int secs; int secs;
int pct; int pct;
int energy;
int power;
if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) { if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
continue; /* skip these, of course. */ continue; /* skip these, of course. */
@ -492,11 +494,16 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
} }
if (!read_power_file(base, name, "time_to_empty_now", str, sizeof (str))) { if (read_power_file(base, name, "time_to_empty_now", str, sizeof (str))) {
secs = -1;
} else {
secs = SDL_atoi(str); secs = SDL_atoi(str);
secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */ secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */
} else if (st == SDL_POWERSTATE_ON_BATTERY) {
/* energy is Watt*hours and power is Watts */
energy = (read_power_file(base, name, "energy_now", str, sizeof (str))) ? SDL_atoi(str) : -1;
power = (read_power_file(base, name, "power_now", str, sizeof (str))) ? SDL_atoi(str) : -1;
secs = (energy >= 0 && power > 0) ? (3600LL * energy) / power : -1;
} else {
secs = -1;
} }
/* /*