Skip to content

Commit

Permalink
[client] Add support of UPower to detect battery status on linux
Browse files Browse the repository at this point in the history
Signed-off-by: Vitalii Koshura <lestat.de.lionkur@gmail.com>
  • Loading branch information
AenBleidd committed Jan 20, 2020
1 parent 2e43443 commit 8b0de9a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions client/hostinfo_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ int get_timezone() {
return 0;
}

std::string read_upower_status() {
char upower_out[256];
FILE *f = popen("upower -d", "r");
if (f) {
while(!std::feof(f)) {
if (!fgets(upower_out, sizeof(upower_out), f)) return "";
if (strstr(upower_out, "on-battery:")) {
return std::string(upower_out);
}
}
fclose(f);
}
return "";
}

// Returns true if the host is currently running off battery power
// If you can't figure out, return false
//
Expand Down Expand Up @@ -223,13 +238,21 @@ bool HOST_INFO::host_is_running_on_batteries() {
#elif LINUX_LIKE_SYSTEM
static enum {
Detect,
UPower,
ProcAPM,
ProcACPI,
SysClass,
NoBattery
} method = Detect;
static char path[64] = "";

if (Detect == method) {
// try UPower
if (!read_upower_status().empty()) {
method = UPower;
}
}

if (Detect == method) {
// try APM in ProcFS
FILE *fapm = fopen("/proc/apm", "r");
Expand Down Expand Up @@ -301,6 +324,11 @@ bool HOST_INFO::host_is_running_on_batteries() {
// if we haven't found a method so far, give up
method = NoBattery;
// fall through
case UPower:
{
if (strstr(read_upower_status().c_str(), "no")) return true;
return false;
}
case ProcAPM:
{
// use /proc/apm
Expand Down

0 comments on commit 8b0de9a

Please sign in to comment.