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 <[email protected]>
  • Loading branch information
AenBleidd committed Jan 28, 2020
1 parent 2e43443 commit fc7432a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 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,6 +238,7 @@ bool HOST_INFO::host_is_running_on_batteries() {
#elif LINUX_LIKE_SYSTEM
static enum {
Detect,
UPower,
ProcAPM,
ProcACPI,
SysClass,
Expand Down Expand Up @@ -296,6 +312,12 @@ bool HOST_INFO::host_is_running_on_batteries() {
}
}
}
if (Detect == method) {
// try UPower
if (!read_upower_status().empty()) {
method = UPower;
}
}
switch (method) {
case Detect:
// if we haven't found a method so far, give up
Expand Down Expand Up @@ -356,6 +378,11 @@ bool HOST_INFO::host_is_running_on_batteries() {
// online is 1 if on AC power, 0 if on battery
return (0 == online);
}
case UPower:
{
if (strstr(read_upower_status().c_str(), "no")) return true;
return false;
}
case NoBattery:
default:
// we have no way to determine if we're on batteries,
Expand Down

0 comments on commit fc7432a

Please sign in to comment.