Skip to content

Commit fc7432a

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 fc7432a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

client/hostinfo_unix.cpp

+27
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,6 +238,7 @@ 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,
@@ -296,6 +312,12 @@ bool HOST_INFO::host_is_running_on_batteries() {
296312
}
297313
}
298314
}
315+
if (Detect == method) {
316+
// try UPower
317+
if (!read_upower_status().empty()) {
318+
method = UPower;
319+
}
320+
}
299321
switch (method) {
300322
case Detect:
301323
// if we haven't found a method so far, give up
@@ -356,6 +378,11 @@ bool HOST_INFO::host_is_running_on_batteries() {
356378
// online is 1 if on AC power, 0 if on battery
357379
return (0 == online);
358380
}
381+
case UPower:
382+
{
383+
if (strstr(read_upower_status().c_str(), "no")) return true;
384+
return false;
385+
}
359386
case NoBattery:
360387
default:
361388
// we have no way to determine if we're on batteries,

0 commit comments

Comments
 (0)