wayland: Add support for the session management protocol

Add session management protocol support for saving/restoring toplevel window state across runs.
This commit is contained in:
Frank Praznik
2026-03-18 13:21:37 -04:00
parent 464b90b475
commit d8971fa00a
7 changed files with 555 additions and 3 deletions

View File

@@ -70,6 +70,7 @@
#include "pointer-warp-v1-client-protocol.h"
#include "pointer-gestures-unstable-v1-client-protocol.h"
#include "single-pixel-buffer-v1-client-protocol.h"
#include "xdg-session-management-v1-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
@@ -1326,6 +1327,89 @@ static void Wayland_InitColorManager(SDL_VideoData *d)
}
}
static void handle_xdg_session_created(void *data, struct xdg_session_v1 *xdg_session_v1, const char *id)
{
SDL_SetStringProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_SESSION_ID_STRING, id);
}
static void handle_xdg_session_restored(void *data, struct xdg_session_v1 *xdg_session_v1)
{
// NOP
}
static void handle_xdg_session_replaced(void *data, struct xdg_session_v1 *xdg_session_v1)
{
SDL_VideoDevice *viddev = SDL_GetVideoDevice();
SDL_VideoData *viddata = data;
// Clean up all session objects, as they have become inert, and should be destroyed.
SDL_SetStringProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_SESSION_ID_STRING, NULL);
for (SDL_Window *w = viddev->windows; w; w = w->next) {
SDL_WindowData *d = w->internal;
if (d->xdg_toplevel_session) {
xdg_toplevel_session_v1_destroy(d->xdg_toplevel_session);
d->xdg_toplevel_session = NULL;
SDL_free(d->session_id);
d->session_id = NULL;
}
}
if (viddata->xdg_session) {
xdg_session_v1_destroy(viddata->xdg_session);
viddata->xdg_session = NULL;
}
}
static const struct xdg_session_v1_listener xdg_session_listener = {
.created = handle_xdg_session_created,
.restored = handle_xdg_session_restored,
.replaced = handle_xdg_session_replaced
};
void Wayland_CreateSession(SDL_VideoData *viddata)
{
if (!viddata->xdg_session_manager) {
// Set the ID string to null if session management is not available.
SDL_SetStringProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_SESSION_ID_STRING, NULL);
return;
}
// Register a new session, if one does not yet exist.
if (!viddata->xdg_session) {
const char *session_id = SDL_GetStringProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_SESSION_ID_STRING, NULL);
if (session_id) {
if (*session_id == '\0') {
// Create a new session if the ID string is empty.
session_id = NULL;
}
const enum xdg_session_manager_v1_reason reason = session_id ? XDG_SESSION_MANAGER_V1_REASON_SESSION_RESTORE : XDG_SESSION_MANAGER_V1_REASON_LAUNCH;
viddata->xdg_session = xdg_session_manager_v1_get_session(viddata->xdg_session_manager, reason, session_id);
xdg_session_v1_add_listener(viddata->xdg_session, &xdg_session_listener, viddata);
}
}
}
static void Wayland_SessionDestroy(SDL_VideoData *viddata)
{
// If the session string was cleared, remove the session.
if (viddata->xdg_session) {
const char *session_id = SDL_GetStringProperty(SDL_GetGlobalProperties(), SDL_PROP_GLOBAL_VIDEO_WAYLAND_SESSION_ID_STRING, NULL);
if (!session_id || *session_id == '\0') {
xdg_session_v1_remove(viddata->xdg_session);
WAYLAND_wl_display_roundtrip(viddata->display);
} else {
xdg_session_v1_destroy(viddata->xdg_session);
}
viddata->xdg_session = NULL;
}
}
static void handle_xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg, uint32_t serial)
{
xdg_wm_base_pong(xdg, serial);
@@ -1425,6 +1509,8 @@ static void handle_registry_global(void *data, struct wl_registry *registry, uin
Wayland_DisplayInitPointerGestureManager(d);
} else if (SDL_strcmp(interface, wp_single_pixel_buffer_manager_v1_interface.name) == 0) {
d->single_pixel_buffer_manager = wl_registry_bind(d->registry, id, &wp_single_pixel_buffer_manager_v1_interface, 1);
} else if (SDL_strcmp(interface, xdg_session_manager_v1_interface.name) == 0) {
d->xdg_session_manager = wl_registry_bind(d->registry, id, &xdg_session_manager_v1_interface, 1);
}
#ifdef SDL_WL_FIXES_VERSION
else if (SDL_strcmp(interface, wl_fixes_interface.name) == 0) {
@@ -1685,6 +1771,8 @@ static void Wayland_VideoCleanup(SDL_VideoDevice *_this)
SDL_VideoData *data = _this->internal;
SDL_WaylandSeat *seat, *tmp;
Wayland_SessionDestroy(data);
for (int i = _this->num_displays - 1; i >= 0; --i) {
SDL_VideoDisplay *display = _this->displays[i];
Wayland_free_display(display, false);
@@ -1842,6 +1930,11 @@ static void Wayland_VideoCleanup(SDL_VideoDevice *_this)
data->single_pixel_buffer_manager = NULL;
}
if (data->xdg_session_manager) {
xdg_session_manager_v1_destroy(data->xdg_session_manager);
data->xdg_session_manager = NULL;
}
if (data->subcompositor) {
wl_subcompositor_destroy(data->subcompositor);
data->subcompositor = NULL;

View File

@@ -87,7 +87,9 @@ struct SDL_VideoData
struct wl_fixes *wl_fixes;
struct zwp_pointer_gestures_v1 *zwp_pointer_gestures;
struct wp_single_pixel_buffer_manager_v1 *single_pixel_buffer_manager;
struct xdg_session_manager_v1 *xdg_session_manager;
struct xdg_session_v1 *xdg_session;
struct xkb_context *xkb_context;
struct wl_list seat_list;
@@ -162,9 +164,10 @@ extern bool SDL_WAYLAND_own_surface(struct wl_surface *surface);
extern bool SDL_WAYLAND_own_output(struct wl_output *output);
extern SDL_WindowData *Wayland_GetWindowDataForOwnedSurface(struct wl_surface *surface);
void Wayland_AddWindowDataToExternalList(SDL_WindowData *data);
void Wayland_RemoveWindowDataFromExternalList(SDL_WindowData *data);
struct wl_event_queue *Wayland_DisplayCreateQueue(struct wl_display *display, const char *name);
extern void Wayland_AddWindowDataToExternalList(SDL_WindowData *data);
extern void Wayland_RemoveWindowDataFromExternalList(SDL_WindowData *data);
extern struct wl_event_queue *Wayland_DisplayCreateQueue(struct wl_display *display, const char *name);
extern void Wayland_CreateSession(SDL_VideoData *data);
extern bool Wayland_LoadLibdecor(SDL_VideoData *data, bool ignore_xdg);

View File

@@ -49,6 +49,7 @@
#include "frog-color-management-v1-client-protocol.h"
#include "xdg-toplevel-icon-v1-client-protocol.h"
#include "color-management-v1-client-protocol.h"
#include "xdg-session-management-v1-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
@@ -2050,6 +2051,60 @@ bool Wayland_SetWindowModal(SDL_VideoDevice *_this, SDL_Window *window, bool mod
return true;
}
static void Wayland_RegisterToplevelForSession(SDL_WindowData *data)
{
SDL_VideoDevice *viddev = SDL_GetVideoDevice();
SDL_VideoData *viddata = viddev->internal;
struct xdg_toplevel *toplevel = GetToplevelForWindow(data);
if (!toplevel) {
return;
}
if (viddata->xdg_session_manager) {
const char *id = SDL_GetStringProperty(data->sdlwindow->props, SDL_PROP_WINDOW_WAYLAND_WINDOW_ID_STRING, NULL);
if (id && *id != '\0') {
Wayland_CreateSession(viddata);
if (viddata->xdg_session) {
CHECK_PARAM (true) {
// Windows must not have a duplicate ID string, or a protocol error will result.
for (SDL_Window *w = viddev->windows; w; w = w->next) {
SDL_WindowData *d = w->internal;
if (d->session_id && SDL_strcmp(d->session_id, id) == 0) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Duplicate window ID string %s found; window will not be added to session", id);
return;
}
}
}
data->xdg_toplevel_session = xdg_session_v1_restore_toplevel(viddata->xdg_session, toplevel, id);
data->session_id = SDL_strdup(id);
}
}
}
}
static void Wayland_DestroyToplevelSession(SDL_WindowData *data)
{
SDL_VideoData *viddata = data->waylandData;
if (data->xdg_toplevel_session) {
const char *id = SDL_GetStringProperty(data->sdlwindow->props, SDL_PROP_WINDOW_WAYLAND_WINDOW_ID_STRING, NULL);
// If the ID string was cleared, remove the window from the session.
if (!id || *id == '\0') {
xdg_session_v1_remove_toplevel(viddata->xdg_session, data->session_id);
}
xdg_toplevel_session_v1_destroy(data->xdg_toplevel_session);
data->xdg_toplevel_session = NULL;
SDL_free(data->session_id);
data->session_id = NULL;
}
}
static void show_hide_sync_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
{
// Get the window from the ID as it may have been destroyed
@@ -2256,6 +2311,7 @@ void Wayland_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
}
// Restore state that was set prior to this call
Wayland_RegisterToplevelForSession(data);
Wayland_SetWindowParent(_this, window, window->parent);
if (window->flags & SDL_WINDOW_MODAL) {
@@ -2476,6 +2532,9 @@ void Wayland_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
wl_surface_attach(wind->surface, NULL, 0, 0);
wl_surface_commit(wind->surface);
// Need to destroy the session object after unmapping the window, or the state may not be saved.
Wayland_DestroyToplevelSession(wind);
SDL_zero(wind->shell_surface);
wind->show_hide_sync_required = true;
struct wl_callback *cb = wl_display_sync(_this->internal->display);
@@ -3108,6 +3167,12 @@ bool Wayland_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_Proper
SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, data->surface);
SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_VIEWPORT_POINTER, data->viewport);
SDL_SetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_EGL_WINDOW_POINTER, data->egl_window);
if (data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL || data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
const char *window_id = SDL_GetStringProperty(create_props, SDL_PROP_WINDOW_CREATE_WAYLAND_WINDOW_ID_STRING, NULL);
if (window_id && *window_id != '\0') {
SDL_SetStringProperty(props, SDL_PROP_WINDOW_WAYLAND_WINDOW_ID_STRING, window_id);
}
}
data->hit_test_result = SDL_HITTEST_NORMAL;

View File

@@ -118,6 +118,7 @@ struct SDL_WindowData
struct xdg_toplevel_icon_v1 *xdg_toplevel_icon_v1;
struct frog_color_managed_surface *frog_color_managed_surface;
struct wp_color_management_surface_feedback_v1 *wp_color_management_surface_feedback;
struct xdg_toplevel_session_v1 *xdg_toplevel_session;
struct Wayland_ColorInfoState *color_info_state;
@@ -127,6 +128,7 @@ struct SDL_WindowData
int num_outputs;
char *app_id;
char *session_id;
double scale_factor;
struct wl_buffer **icon_buffers;