From 0d705ca2757005fdb1d2f485d07c78dfb1dfade6 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 19 Dec 2025 17:41:52 -0800
Subject: [PATCH] Use the aggregate upower device for power status when
available
---
src/power/linux/SDL_syspower.c | 37 +++++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 10 deletions(-)
diff --git a/src/power/linux/SDL_syspower.c b/src/power/linux/SDL_syspower.c
index 428be4aa5b870..d59952c0a3acc 100644
--- a/src/power/linux/SDL_syspower.c
+++ b/src/power/linux/SDL_syspower.c
@@ -621,24 +621,41 @@ bool SDL_GetPowerInfo_Linux_org_freedesktop_upower(SDL_PowerState *state, int *s
#ifdef SDL_USE_LIBDBUS
SDL_DBusContext *dbus = SDL_DBus_GetContext();
char **paths = NULL;
+ char *path = NULL;
int i, numpaths = 0;
- if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn, UPOWER_DBUS_NODE, UPOWER_DBUS_PATH, UPOWER_DBUS_INTERFACE, "EnumerateDevices",
- DBUS_TYPE_INVALID,
- DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &numpaths, DBUS_TYPE_INVALID)) {
+ if (!dbus) {
return false; // try a different approach than UPower.
}
- result = true; // Clearly we can use this interface.
- *state = SDL_POWERSTATE_NO_BATTERY; // assume we're just plugged in.
- *seconds = -1;
- *percent = -1;
+ if (SDL_DBus_CallMethodOnConnection(dbus->system_conn, UPOWER_DBUS_NODE, UPOWER_DBUS_PATH, UPOWER_DBUS_INTERFACE, "GetDisplayDevice", DBUS_TYPE_INVALID, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ result = true; // Clearly we can use this interface.
+ *state = SDL_POWERSTATE_NO_BATTERY; // assume we're just plugged in.
+ *seconds = -1;
+ *percent = -1;
+
+ // Make a copy of the path before making more dbus calls
+ path = SDL_strdup(path);
+ if (!path) {
+ // Out of memory, try to do something else
+ return false;
+ }
+ check_upower_device(dbus->system_conn, path, state, seconds, percent);
+ SDL_free(path);
+
+ } else if (SDL_DBus_CallMethodOnConnection(dbus->system_conn, UPOWER_DBUS_NODE, UPOWER_DBUS_PATH, UPOWER_DBUS_INTERFACE, "EnumerateDevices", DBUS_TYPE_INVALID, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &numpaths, DBUS_TYPE_INVALID)) {
+ result = true; // Clearly we can use this interface.
+ *state = SDL_POWERSTATE_NO_BATTERY; // assume we're just plugged in.
+ *seconds = -1;
+ *percent = -1;
+
+ for (i = 0; i < numpaths; i++) {
+ check_upower_device(dbus->system_conn, paths[i], state, seconds, percent);
+ }
- for (i = 0; i < numpaths; i++) {
- check_upower_device(dbus->system_conn, paths[i], state, seconds, percent);
+ dbus->free_string_array(paths);
}
- dbus->free_string_array(paths);
#endif // SDL_USE_LIBDBUS
return result;