Atlas - SDL_waylandutil.c
Home / ext / SDL / src / video / wayland Lines: 1 | Size: 4955 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#ifdef SDL_VIDEO_DRIVER_WAYLAND 24 25#include "SDL_waylandevents_c.h" 26#include "SDL_waylandutil.h" 27#include "xdg-activation-v1-client-protocol.h" 28 29#define WAYLAND_HANDLE_PREFIX "wayland:" 30 31typedef struct Wayland_ActivationParams 32{ 33 char **token; 34 bool done; 35} Wayland_ActivationParams; 36 37static void handle_xdg_activation_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token_v1, const char *token) 38{ 39 Wayland_ActivationParams *activation_params = (Wayland_ActivationParams *)data; 40 *activation_params->token = SDL_strdup(token); 41 activation_params->done = true; 42 43 xdg_activation_token_v1_destroy(xdg_activation_token_v1); 44} 45 46static const struct xdg_activation_token_v1_listener xdg_activation_listener = { 47 handle_xdg_activation_done 48}; 49 50bool Wayland_GetActivationTokenForExport(SDL_VideoDevice *_this, char **token, char **window_id) 51{ 52 if (!_this || !token) { 53 return false; 54 } 55 56 SDL_VideoData *viddata = _this->internal; 57 58 SDL_WaylandSeat *seat = viddata->last_implicit_grab_seat; 59 SDL_WindowData *focus = NULL; 60 61 if (seat) { 62 focus = seat->keyboard.focus; 63 if (!focus) { 64 focus = seat->pointer.focus; 65 } 66 } 67 68 const char *xdg_activation_token = SDL_getenv("XDG_ACTIVATION_TOKEN"); 69 if (xdg_activation_token) { 70 *token = SDL_strdup(xdg_activation_token); 71 if (!*token) { 72 return false; 73 } 74 75 // Unset the envvar after claiming the token. 76 SDL_unsetenv_unsafe("XDG_ACTIVATION_TOKEN"); 77 } else if (viddata->activation_manager) { 78 struct wl_surface *requesting_surface = focus ? focus->surface : NULL; 79 Wayland_ActivationParams params = { 80 .token = token, 81 .done = false 82 }; 83 84 struct wl_event_queue *activation_token_queue = Wayland_DisplayCreateQueue(viddata->display, "SDL Activation Token Generation Queue"); 85 86 struct wl_proxy *activation_manager_wrapper = WAYLAND_wl_proxy_create_wrapper(viddata->activation_manager); 87 WAYLAND_wl_proxy_set_queue(activation_manager_wrapper, activation_token_queue); 88 struct xdg_activation_token_v1 *activation_token = xdg_activation_v1_get_activation_token((struct xdg_activation_v1 *)activation_manager_wrapper); 89 xdg_activation_token_v1_add_listener(activation_token, &xdg_activation_listener, ¶ms); 90 91 if (requesting_surface) { 92 // This specifies the surface from which the activation request is originating, not the activation target surface. 93 xdg_activation_token_v1_set_surface(activation_token, requesting_surface); 94 } 95 if (seat && seat->wl_seat) { 96 xdg_activation_token_v1_set_serial(activation_token, seat->last_implicit_grab_serial, seat->wl_seat); 97 } 98 if (focus && focus->app_id) { 99 // Set the app ID for external use. 100 xdg_activation_token_v1_set_app_id(activation_token, focus->app_id); 101 } 102 xdg_activation_token_v1_commit(activation_token); 103 104 while (!params.done) { 105 WAYLAND_wl_display_dispatch_queue(viddata->display, activation_token_queue); 106 } 107 WAYLAND_wl_proxy_wrapper_destroy(activation_manager_wrapper); 108 WAYLAND_wl_event_queue_destroy(activation_token_queue); 109 110 if (!*token) { 111 return false; 112 } 113 } 114 115 if (focus && window_id) { 116 const char *id = SDL_GetStringProperty(focus->sdlwindow->props, SDL_PROP_WINDOW_WAYLAND_XDG_TOPLEVEL_EXPORT_HANDLE_STRING, NULL); 117 if (id) { 118 const size_t len = SDL_strlen(id) + sizeof(WAYLAND_HANDLE_PREFIX) + 1; 119 *window_id = SDL_malloc(len); 120 if (!*window_id) { 121 SDL_free(*token); 122 *token = NULL; 123 return false; 124 } 125 126 SDL_strlcpy(*window_id, WAYLAND_HANDLE_PREFIX, len); 127 SDL_strlcat(*window_id, id, len); 128 } 129 } 130 131 return true; 132} 133 134#endif // SDL_VIDEO_DRIVER_WAYLAND 135[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.