diff --git a/client/hostinfo_unix.cpp b/client/hostinfo_unix.cpp index f0dc257f120..76a6313ca93 100644 --- a/client/hostinfo_unix.cpp +++ b/client/hostinfo_unix.cpp @@ -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 // @@ -223,6 +238,7 @@ bool HOST_INFO::host_is_running_on_batteries() { #elif LINUX_LIKE_SYSTEM static enum { Detect, + UPower, ProcAPM, ProcACPI, SysClass, @@ -230,6 +246,13 @@ bool HOST_INFO::host_is_running_on_batteries() { } 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"); @@ -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