Don't fail to get battery status if the upower refresh call fails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
diff --git a/src/power/linux/SDL_syspower.c b/src/power/linux/SDL_syspower.c
index 1f238c5..f4e1ecc 100644
--- a/src/power/linux/SDL_syspower.c
+++ b/src/power/linux/SDL_syspower.c
@@ -561,22 +561,30 @@ check_upower_device(DBusConnection *conn, const char *path, SDL_PowerState *stat
return;
} else if (!ui32) {
return; /* we don't care about random devices with batteries, like wireless controllers, etc */
- } else if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "IsPresent", DBUS_TYPE_BOOLEAN, &ui32)) {
+ }
+
+ if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "IsPresent", DBUS_TYPE_BOOLEAN, &ui32)) {
return;
- } else if (!ui32) {
+ }
+ if (!ui32) {
st = SDL_POWERSTATE_NO_BATTERY;
- } else if (!SDL_DBus_CallMethodOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "Refresh", DBUS_TYPE_INVALID, DBUS_TYPE_INVALID)) {
- return;
- } else if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "State", DBUS_TYPE_UINT32, &ui32)) {
- st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
- } else if (ui32 == 1) { /* 1 == charging */
- st = SDL_POWERSTATE_CHARGING;
- } else if ((ui32 == 2) || (ui32 == 3)) { /* 2 == discharging, 3 == empty. */
- st = SDL_POWERSTATE_ON_BATTERY;
- } else if (ui32 == 4) { /* 4 == full */
- st = SDL_POWERSTATE_CHARGED;
} else {
- st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
+ /* Get updated information on the battery status
+ * This can occasionally fail, and we'll just return slightly stale data in that case
+ */
+ SDL_DBus_CallMethodOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "Refresh", DBUS_TYPE_INVALID, DBUS_TYPE_INVALID);
+
+ if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "State", DBUS_TYPE_UINT32, &ui32)) {
+ st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
+ } else if (ui32 == 1) { /* 1 == charging */
+ st = SDL_POWERSTATE_CHARGING;
+ } else if ((ui32 == 2) || (ui32 == 3)) { /* 2 == discharging, 3 == empty. */
+ st = SDL_POWERSTATE_ON_BATTERY;
+ } else if (ui32 == 4) { /* 4 == full */
+ st = SDL_POWERSTATE_CHARGED;
+ } else {
+ st = SDL_POWERSTATE_UNKNOWN; /* uh oh */
+ }
}
if (!SDL_DBus_QueryPropertyOnConnection(conn, UPOWER_DBUS_NODE, path, UPOWER_DEVICE_DBUS_INTERFACE, "Percentage", DBUS_TYPE_DOUBLE, &d)) {