File tree 1 file changed +28
-0
lines changed
1 file changed +28
-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,13 +238,21 @@ 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,
229
245
NoBattery
230
246
} method = Detect;
231
247
static char path[64 ] = " " ;
232
248
249
+ if (Detect == method) {
250
+ // try UPower
251
+ if (!read_upower_status ().empty ()) {
252
+ method = UPower;
253
+ }
254
+ }
255
+
233
256
if (Detect == method) {
234
257
// try APM in ProcFS
235
258
FILE *fapm = fopen (" /proc/apm" , " r" );
@@ -301,6 +324,11 @@ bool HOST_INFO::host_is_running_on_batteries() {
301
324
// if we haven't found a method so far, give up
302
325
method = NoBattery;
303
326
// fall through
327
+ case UPower:
328
+ {
329
+ if (strstr (read_upower_status ().c_str (), " no" )) return true ;
330
+ return false ;
331
+ }
304
332
case ProcAPM:
305
333
{
306
334
// use /proc/apm
You can’t perform that action at this time.
0 commit comments