File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -192,6 +192,21 @@ int get_timezone() {
192
192
return 0 ;
193
193
}
194
194
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
+
195
210
// Returns true if the host is currently running off battery power
196
211
// If you can't figure out, return false
197
212
//
@@ -223,6 +238,7 @@ bool HOST_INFO::host_is_running_on_batteries() {
223
238
#elif LINUX_LIKE_SYSTEM
224
239
static enum {
225
240
Detect,
241
+ UPower,
226
242
ProcAPM,
227
243
ProcACPI,
228
244
SysClass,
@@ -296,6 +312,12 @@ bool HOST_INFO::host_is_running_on_batteries() {
296
312
}
297
313
}
298
314
}
315
+ if (Detect == method) {
316
+ // try UPower
317
+ if (!read_upower_status ().empty ()) {
318
+ method = UPower;
319
+ }
320
+ }
299
321
switch (method) {
300
322
case Detect:
301
323
// if we haven't found a method so far, give up
@@ -356,6 +378,11 @@ bool HOST_INFO::host_is_running_on_batteries() {
356
378
// online is 1 if on AC power, 0 if on battery
357
379
return (0 == online);
358
380
}
381
+ case UPower:
382
+ {
383
+ if (strstr (read_upower_status ().c_str (), " no" )) return true ;
384
+ return false ;
385
+ }
359
386
case NoBattery:
360
387
default :
361
388
// we have no way to determine if we're on batteries,
You can’t perform that action at this time.
0 commit comments