Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client] Add support of UPower to detect battery status on linux #3425

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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