Skip to content

Commit 8b0de9a

Browse files
committed
[client] Add support of UPower to detect battery status on linux
Signed-off-by: Vitalii Koshura <[email protected]>
1 parent 2e43443 commit 8b0de9a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

client/hostinfo_unix.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ int get_timezone() {
192192
return 0;
193193
}
194194

195+
std::string read_upower_status() {
196+
char upower_out[256];
197+
FILE *f = popen("upower -d", "r");
198+
if (f) {
199+
while(!std::feof(f)) {
200+
if (!fgets(upower_out, sizeof(upower_out), f)) return "";
201+
if (strstr(upower_out, "on-battery:")) {
202+
return std::string(upower_out);
203+
}
204+
}
205+
fclose(f);
206+
}
207+
return "";
208+
}
209+
195210
// Returns true if the host is currently running off battery power
196211
// If you can't figure out, return false
197212
//
@@ -223,13 +238,21 @@ bool HOST_INFO::host_is_running_on_batteries() {
223238
#elif LINUX_LIKE_SYSTEM
224239
static enum {
225240
Detect,
241+
UPower,
226242
ProcAPM,
227243
ProcACPI,
228244
SysClass,
229245
NoBattery
230246
} method = Detect;
231247
static char path[64] = "";
232248

249+
if (Detect == method) {
250+
// try UPower
251+
if (!read_upower_status().empty()) {
252+
method = UPower;
253+
}
254+
}
255+
233256
if (Detect == method) {
234257
// try APM in ProcFS
235258
FILE *fapm = fopen("/proc/apm", "r");
@@ -301,6 +324,11 @@ bool HOST_INFO::host_is_running_on_batteries() {
301324
// if we haven't found a method so far, give up
302325
method = NoBattery;
303326
// fall through
327+
case UPower:
328+
{
329+
if (strstr(read_upower_status().c_str(), "no")) return true;
330+
return false;
331+
}
304332
case ProcAPM:
305333
{
306334
// use /proc/apm

0 commit comments

Comments
 (0)