dbus: Use xdg-desktop-portal Inhibit when running under Flatpak or Snap In order to inhibit the screen saver, SDL currently uses `org.freedesktop.ScreenSaver.Inhibit()` and, as a fallback, a protocol specific method for X11 or Wayland. Accessing `org.freedesktop.ScreenSaver` is usually not allowed when inside a sandbox like Flatpak, unless the permission has been explicitly granted to the application. Another issue is that the Wayland protocol "Idle inhibit" is relatively new and not yet widely adopted. For example Mutter still doesn't support it. For those reasons, when running under Flatpak or Snap, we should try to inhibit the screen saver by using xdg-desktop-portal instead. This should give us an higher chance of success. Fixes: https://github.com/libsdl-org/SDL/issues/6075 Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
diff --git a/src/core/linux/SDL_dbus.c b/src/core/linux/SDL_dbus.c
index 58d3fd3..3ab362d 100644
--- a/src/core/linux/SDL_dbus.c
+++ b/src/core/linux/SDL_dbus.c
@@ -22,6 +22,7 @@
#include "SDL_hints.h"
#include "SDL_dbus.h"
#include "SDL_atomic.h"
+#include "SDL_sandbox.h"
#include "../../stdlib/SDL_vacopy.h"
#if SDL_USE_LIBDBUS
@@ -29,6 +30,7 @@
#include "SDL_loadso.h"
static const char *dbus_library = "libdbus-1.so.3";
static void *dbus_handle = NULL;
+static char *inhibit_handle = NULL;
static unsigned int screensaver_cookie = 0;
static SDL_DBusContext dbus;
@@ -192,6 +194,8 @@ SDL_DBus_Quit(void)
#endif
SDL_zero(dbus);
UnloadDBUSLibrary();
+ SDL_free(inhibit_handle);
+ inhibit_handle = NULL;
}
SDL_DBusContext *
@@ -363,20 +367,108 @@ SDL_DBus_QueryProperty(const char *node, const char *path, const char *interface
void
SDL_DBus_ScreensaverTickle(void)
{
- if (screensaver_cookie == 0) { /* no need to tickle if we're inhibiting. */
+ if (screensaver_cookie == 0 && inhibit_handle == NULL) { /* no need to tickle if we're inhibiting. */
/* org.gnome.ScreenSaver is the legacy interface, but it'll either do nothing or just be a second harmless tickle on newer systems, so we leave it for now. */
SDL_DBus_CallVoidMethod("org.gnome.ScreenSaver", "/org/gnome/ScreenSaver", "org.gnome.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
SDL_DBus_CallVoidMethod("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver", "SimulateUserActivity", DBUS_TYPE_INVALID);
}
}
+static SDL_bool
+SDL_DBus_AppendDictWithKeyValue(DBusMessageIter *iterInit, const char *key, const char *value)
+{
+ DBusMessageIter iterDict, iterEntry, iterValue;
+
+ if (!dbus.message_iter_open_container(iterInit, DBUS_TYPE_ARRAY, "{sv}", &iterDict))
+ goto failed;
+
+ if (!dbus.message_iter_open_container(&iterDict, DBUS_TYPE_DICT_ENTRY, NULL, &iterEntry))
+ goto failed;
+
+ if (!dbus.message_iter_append_basic(&iterEntry, DBUS_TYPE_STRING, &key))
+ goto failed;
+
+ if (!dbus.message_iter_open_container(&iterEntry, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &iterValue))
+ goto failed;
+
+ if (!dbus.message_iter_append_basic(&iterValue, DBUS_TYPE_STRING, &value))
+ goto failed;
+
+ if (!dbus.message_iter_close_container(&iterEntry, &iterValue)
+ || !dbus.message_iter_close_container(&iterDict, &iterEntry)
+ || !dbus.message_iter_close_container(iterInit, &iterDict)) {
+ goto failed;
+ }
+
+ return SDL_TRUE;
+
+failed:
+ /* message_iter_abandon_container_if_open() and message_iter_abandon_container() might be
+ * missing if libdbus is too old. Instead, we just return without cleaning up any eventual
+ * open container */
+ return SDL_FALSE;
+}
+
SDL_bool
SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
{
- if ( (inhibit && (screensaver_cookie != 0)) || (!inhibit && (screensaver_cookie == 0)) ) {
+ const char *default_inhibit_reason = "Playing a game";
+
+ if ( (inhibit && (screensaver_cookie != 0 || inhibit_handle != NULL))
+ || (!inhibit && (screensaver_cookie == 0 && inhibit_handle == NULL)) ) {
return SDL_TRUE;
+ }
+
+ if (SDL_DetectSandbox() != SDL_SANDBOX_NONE) {
+ const char *bus_name = "org.freedesktop.portal.Desktop";
+ const char *path = "/org/freedesktop/portal/desktop";
+ const char *interface = "org.freedesktop.portal.Inhibit";
+ const char *window = ""; /* As a future improvement we could gather the X11 XID or Wayland surface identifier */
+ static const unsigned int INHIBIT_IDLE = 8; /* Taken from the portal API reference */
+ DBusMessageIter iterInit;
+
+ if (inhibit) {
+ DBusMessage *msg;
+ SDL_bool retval = SDL_FALSE;
+ const char *key = "reason";
+ const char *reply = NULL;
+ const char *reason = SDL_GetHint(SDL_HINT_SCREENSAVER_INHIBIT_ACTIVITY_NAME);
+ if (!reason || !reason[0]) {
+ reason = default_inhibit_reason;
+ }
+
+ msg = dbus.message_new_method_call(bus_name, path, interface, "Inhibit");
+ if (!msg) {
+ return SDL_FALSE;
+ }
+
+ if (!dbus.message_append_args(msg, DBUS_TYPE_STRING, &window, DBUS_TYPE_UINT32, &INHIBIT_IDLE, DBUS_TYPE_INVALID)) {
+ dbus.message_unref(msg);
+ return SDL_FALSE;
+ }
+
+ dbus.message_iter_init_append(msg, &iterInit);
+ if (!SDL_DBus_AppendDictWithKeyValue(&iterInit, key, reason)) {
+ dbus.message_unref(msg);
+ return SDL_FALSE;
+ }
+
+ if (SDL_DBus_CallWithBasicReply(dbus.session_conn, msg, DBUS_TYPE_OBJECT_PATH, &reply)) {
+ inhibit_handle = SDL_strdup(reply);
+ retval = SDL_TRUE;
+ }
+
+ dbus.message_unref(msg);
+ return retval;
+ } else {
+ if (!SDL_DBus_CallVoidMethod(bus_name, inhibit_handle, "org.freedesktop.portal.Request", "Close", DBUS_TYPE_INVALID)) {
+ return SDL_FALSE;
+ }
+ SDL_free(inhibit_handle);
+ inhibit_handle = NULL;
+ }
} else {
- const char *node = "org.freedesktop.ScreenSaver";
+ const char *bus_name = "org.freedesktop.ScreenSaver";
const char *path = "/org/freedesktop/ScreenSaver";
const char *interface = "org.freedesktop.ScreenSaver";
@@ -387,17 +479,17 @@ SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
app = "My SDL application";
}
if (!reason || !reason[0]) {
- reason = "Playing a game";
+ reason = default_inhibit_reason;
}
- if (!SDL_DBus_CallMethod(node, path, interface, "Inhibit",
+ if (!SDL_DBus_CallMethod(bus_name, path, interface, "Inhibit",
DBUS_TYPE_STRING, &app, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID,
DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
return SDL_FALSE;
}
return (screensaver_cookie != 0) ? SDL_TRUE : SDL_FALSE;
} else {
- if (!SDL_DBus_CallVoidMethod(node, path, interface, "UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
+ if (!SDL_DBus_CallVoidMethod(bus_name, path, interface, "UnInhibit", DBUS_TYPE_UINT32, &screensaver_cookie, DBUS_TYPE_INVALID)) {
return SDL_FALSE;
}
screensaver_cookie = 0;