Commit c27dc5147850a8026aec7e7753eb3b2538797fd8

Ryan C. Gordon 2017-07-03T16:38:37

power: Linux /sys/class testing should skip "device" scopes. (the PS4 game controllers report their batteries through this interface, which is cool, but not helpful for powering the rest of the system. :) )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
diff --git a/src/power/linux/SDL_syspower.c b/src/power/linux/SDL_syspower.c
index a53fca4..4c1d9d3 100644
--- a/src/power/linux/SDL_syspower.c
+++ b/src/power/linux/SDL_syspower.c
@@ -460,6 +460,16 @@ SDL_GetPowerInfo_Linux_sys_class_power_supply(SDL_PowerState *state, int *second
             continue;  /* we don't care about UPS and such. */
         }
 
+        /* if the scope is "device," it might be something like a PS4
+           controller reporting its own battery, and not something that powers
+           the system. Most system batteries don't list a scope at all; we
+           assume it's a system battery if not specified. */
+        if (read_power_file(base, name, "scope", str, sizeof (str))) {
+            if (SDL_strcmp(str, "device\n") != 0) {
+                continue;  /* skip external devices with their own batteries. */
+            }
+        }
+
         /* some drivers don't offer this, so if it's not explicitly reported assume it's present. */
         if (read_power_file(base, name, "present", str, sizeof (str)) && (SDL_strcmp(str, "0\n") == 0)) {
             st = SDL_POWERSTATE_NO_BATTERY;