From 65c28ed31d02eb2ab5a0773aa58b5f7a80240bbe Mon Sep 17 00:00:00 2001 From: Riteo Date: Mon, 2 Dec 2024 20:08:01 +0100 Subject: [PATCH] Wayland: Add support for xdg-foreign-unstable-v2 The v1 version is deprecated and bound to be removed in the future from all compositors. This patch adds a v1/v2 designator to everything related to the protocol and prefers the v2 protocol if both are available. Additionally, renames the event handler to follow the Wayland interface name, for consistency with the rest of the codebase. --- platform/linuxbsd/wayland/SCsub | 17 +- platform/linuxbsd/wayland/wayland_thread.cpp | 60 ++++-- platform/linuxbsd/wayland/wayland_thread.h | 33 ++- .../xdg-foreign/xdg-foreign-unstable-v2.xml | 200 ++++++++++++++++++ 4 files changed, 287 insertions(+), 23 deletions(-) create mode 100644 thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v2.xml diff --git a/platform/linuxbsd/wayland/SCsub b/platform/linuxbsd/wayland/SCsub index f3a554a58db8..c5d8c00c6d77 100644 --- a/platform/linuxbsd/wayland/SCsub +++ b/platform/linuxbsd/wayland/SCsub @@ -163,15 +163,25 @@ env.WAYLAND_API_CODE( ) env.WAYLAND_API_HEADER( - target="protocol/xdg_foreign.gen.h", + target="protocol/xdg_foreign_v1.gen.h", source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v1.xml", ) env.WAYLAND_API_CODE( - target="protocol/xdg_foreign.gen.c", + target="protocol/xdg_foreign_v1.gen.c", source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v1.xml", ) +env.WAYLAND_API_HEADER( + target="protocol/xdg_foreign_v2.gen.h", + source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v2.xml", +) + +env.WAYLAND_API_CODE( + target="protocol/xdg_foreign_v2.gen.c", + source="#thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v2.xml", +) + env.WAYLAND_API_HEADER( target="protocol/xdg_system_bell.gen.h", source="#thirdparty/wayland-protocols/staging/xdg-system-bell/xdg-system-bell-v1.xml", @@ -188,7 +198,8 @@ source_files = [ "protocol/fractional_scale.gen.c", "protocol/xdg_shell.gen.c", "protocol/xdg_system_bell.gen.c", - "protocol/xdg_foreign.gen.c", + "protocol/xdg_foreign_v1.gen.c", + "protocol/xdg_foreign_v2.gen.c", "protocol/xdg_decoration.gen.c", "protocol/xdg_activation.gen.c", "protocol/relative_pointer.gen.c", diff --git a/platform/linuxbsd/wayland/wayland_thread.cpp b/platform/linuxbsd/wayland/wayland_thread.cpp index 8e3dc7558184..baba4d6f781b 100644 --- a/platform/linuxbsd/wayland/wayland_thread.cpp +++ b/platform/linuxbsd/wayland/wayland_thread.cpp @@ -378,9 +378,16 @@ void WaylandThread::_wl_registry_on_global(void *data, struct wl_registry *wl_re return; } + // NOTE: Deprecated. if (strcmp(interface, zxdg_exporter_v1_interface.name) == 0) { - registry->xdg_exporter = (struct zxdg_exporter_v1 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v1_interface, 1); - registry->xdg_exporter_name = name; + registry->xdg_exporter_v1 = (struct zxdg_exporter_v1 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v1_interface, 1); + registry->xdg_exporter_v1_name = name; + return; + } + + if (strcmp(interface, zxdg_exporter_v2_interface.name) == 0) { + registry->xdg_exporter_v2 = (struct zxdg_exporter_v2 *)wl_registry_bind(wl_registry, name, &zxdg_exporter_v2_interface, 1); + registry->xdg_exporter_v2_name = name; return; } @@ -600,13 +607,25 @@ void WaylandThread::_wl_registry_on_global_remove(void *data, struct wl_registry return; } - if (name == registry->xdg_exporter_name) { - if (registry->xdg_exporter) { - zxdg_exporter_v1_destroy(registry->xdg_exporter); - registry->xdg_exporter = nullptr; + // NOTE: Deprecated. + if (name == registry->xdg_exporter_v1_name) { + if (registry->xdg_exporter_v1) { + zxdg_exporter_v1_destroy(registry->xdg_exporter_v1); + registry->xdg_exporter_v1 = nullptr; + } + + registry->xdg_exporter_v1_name = 0; + + return; + } + + if (name == registry->xdg_exporter_v2_name) { + if (registry->xdg_exporter_v2) { + zxdg_exporter_v2_destroy(registry->xdg_exporter_v2); + registry->xdg_exporter_v2 = nullptr; } - registry->xdg_exporter_name = 0; + registry->xdg_exporter_v2_name = 0; return; } @@ -1187,7 +1206,15 @@ void WaylandThread::_xdg_toplevel_on_wm_capabilities(void *data, struct xdg_topl } } -void WaylandThread::_xdg_exported_on_exported(void *data, zxdg_exported_v1 *exported, const char *handle) { +// NOTE: Deprecated. +void WaylandThread::_xdg_exported_v1_on_handle(void *data, zxdg_exported_v1 *exported, const char *handle) { + WindowState *ws = (WindowState *)data; + ERR_FAIL_NULL(ws); + + ws->exported_handle = vformat("wayland:%s", String::utf8(handle)); +} + +void WaylandThread::_xdg_exported_v2_on_handle(void *data, zxdg_exported_v2 *exported, const char *handle) { WindowState *ws = (WindowState *)data; ERR_FAIL_NULL(ws); @@ -3285,9 +3312,12 @@ void WaylandThread::window_create(DisplayServer::WindowID p_window_id, int p_wid ws.frame_callback = wl_surface_frame(ws.wl_surface); wl_callback_add_listener(ws.frame_callback, &frame_wl_callback_listener, &ws); - if (registry.xdg_exporter) { - ws.xdg_exported = zxdg_exporter_v1_export(registry.xdg_exporter, ws.wl_surface); - zxdg_exported_v1_add_listener(ws.xdg_exported, &xdg_exported_listener, &ws); + if (registry.xdg_exporter_v2) { + ws.xdg_exported_v2 = zxdg_exporter_v2_export_toplevel(registry.xdg_exporter_v2, ws.wl_surface); + zxdg_exported_v2_add_listener(ws.xdg_exported_v2, &xdg_exported_v2_listener, &ws); + } else if (registry.xdg_exporter_v1) { + ws.xdg_exported_v1 = zxdg_exporter_v1_export(registry.xdg_exporter_v1, ws.wl_surface); + zxdg_exported_v1_add_listener(ws.xdg_exported_v1, &xdg_exported_v1_listener, &ws); } wl_surface_commit(ws.wl_surface); @@ -4411,10 +4441,14 @@ void WaylandThread::destroy() { xdg_wm_base_destroy(registry.xdg_wm_base); } - if (registry.xdg_exporter) { - zxdg_exporter_v1_destroy(registry.xdg_exporter); + // NOTE: Deprecated. + if (registry.xdg_exporter_v1) { + zxdg_exporter_v1_destroy(registry.xdg_exporter_v1); } + if (registry.xdg_exporter_v2) { + zxdg_exporter_v2_destroy(registry.xdg_exporter_v2); + } if (registry.wl_shm) { wl_shm_destroy(registry.wl_shm); } diff --git a/platform/linuxbsd/wayland/wayland_thread.h b/platform/linuxbsd/wayland/wayland_thread.h index c07694b52ae8..8f1605b82027 100644 --- a/platform/linuxbsd/wayland/wayland_thread.h +++ b/platform/linuxbsd/wayland/wayland_thread.h @@ -67,10 +67,13 @@ #include "wayland/protocol/wayland.gen.h" #include "wayland/protocol/xdg_activation.gen.h" #include "wayland/protocol/xdg_decoration.gen.h" -#include "wayland/protocol/xdg_foreign.gen.h" +#include "wayland/protocol/xdg_foreign_v2.gen.h" #include "wayland/protocol/xdg_shell.gen.h" #include "wayland/protocol/xdg_system_bell.gen.h" +// NOTE: Deprecated. +#include "wayland/protocol/xdg_foreign_v1.gen.h" + #ifdef LIBDECOR_ENABLED #ifdef SOWRAP_ENABLED #include "dynwrappers/libdecor-so_wrap.h" @@ -149,8 +152,12 @@ class WaylandThread { struct xdg_wm_base *xdg_wm_base = nullptr; uint32_t xdg_wm_base_name = 0; - struct zxdg_exporter_v1 *xdg_exporter = nullptr; - uint32_t xdg_exporter_name = 0; + // NOTE: Deprecated. + struct zxdg_exporter_v1 *xdg_exporter_v1 = nullptr; + uint32_t xdg_exporter_v1_name = 0; + + uint32_t xdg_exporter_v2_name = 0; + struct zxdg_exporter_v2 *xdg_exporter_v2 = nullptr; // wayland-protocols globals. @@ -224,7 +231,11 @@ class WaylandThread { struct wp_viewport *wp_viewport = nullptr; struct wp_fractional_scale_v1 *wp_fractional_scale = nullptr; - struct zxdg_exported_v1 *xdg_exported = nullptr; + + // NOTE: Deprecated. + struct zxdg_exported_v1 *xdg_exported_v1 = nullptr; + + struct zxdg_exported_v2 *xdg_exported_v2 = nullptr; String exported_handle; @@ -652,7 +663,10 @@ class WaylandThread { static void _xdg_toplevel_decoration_on_configure(void *data, struct zxdg_toplevel_decoration_v1 *xdg_toplevel_decoration, uint32_t mode); - static void _xdg_exported_on_exported(void *data, zxdg_exported_v1 *exported, const char *handle); + // NOTE: Deprecated. + static void _xdg_exported_v1_on_handle(void *data, zxdg_exported_v1 *exported, const char *handle); + + static void _xdg_exported_v2_on_handle(void *data, zxdg_exported_v2 *exported, const char *handle); static void _xdg_activation_token_on_done(void *data, struct xdg_activation_token_v1 *xdg_activation_token, const char *token); @@ -820,8 +834,13 @@ class WaylandThread { .done = _wp_text_input_on_done, }; - static constexpr struct zxdg_exported_v1_listener xdg_exported_listener = { - .handle = _xdg_exported_on_exported + // NOTE: Deprecated. + static constexpr struct zxdg_exported_v1_listener xdg_exported_v1_listener = { + .handle = _xdg_exported_v1_on_handle, + }; + + static constexpr struct zxdg_exported_v2_listener xdg_exported_v2_listener = { + .handle = _xdg_exported_v2_on_handle, }; static constexpr struct zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_listener = { diff --git a/thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v2.xml b/thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v2.xml new file mode 100644 index 000000000000..cc3271dca4d6 --- /dev/null +++ b/thirdparty/wayland-protocols/unstable/xdg-foreign/xdg-foreign-unstable-v2.xml @@ -0,0 +1,200 @@ + + + + + Copyright © 2015-2016 Red Hat Inc. + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + This protocol specifies a way for making it possible to reference a surface + of a different client. With such a reference, a client can, by using the + interfaces provided by this protocol, manipulate the relationship between + its own surfaces and the surface of some other client. For example, stack + some of its own surface above the other clients surface. + + In order for a client A to get a reference of a surface of client B, client + B must first export its surface using xdg_exporter.export_toplevel. Upon + doing this, client B will receive a handle (a unique string) that it may + share with client A in some way (for example D-Bus). After client A has + received the handle from client B, it may use xdg_importer.import_toplevel + to create a reference to the surface client B just exported. See the + corresponding requests for details. + + A possible use case for this is out-of-process dialogs. For example when a + sandboxed client without file system access needs the user to select a file + on the file system, given sandbox environment support, it can export its + surface, passing the exported surface handle to an unsandboxed process that + can show a file browser dialog and stack it above the sandboxed client's + surface. + + Warning! The protocol described in this file is experimental and backward + incompatible changes may be made. Backward compatible changes may be added + together with the corresponding interface version bump. Backward + incompatible changes are done by bumping the version number in the protocol + and interface names and resetting the interface version. Once the protocol + is to be declared stable, the 'z' prefix and the version number in the + protocol and interface names are removed and the interface version number is + reset. + + + + + A global interface used for exporting surfaces that can later be imported + using xdg_importer. + + + + + Notify the compositor that the xdg_exporter object will no longer be + used. + + + + + + These errors can be emitted in response to invalid xdg_exporter + requests. + + + + + + + The export_toplevel request exports the passed surface so that it can later be + imported via xdg_importer. When called, a new xdg_exported object will + be created and xdg_exported.handle will be sent immediately. See the + corresponding interface and event for details. + + A surface may be exported multiple times, and each exported handle may + be used to create an xdg_imported multiple times. Only xdg_toplevel + equivalent surfaces may be exported, otherwise an invalid_surface + protocol error is sent. + + + + + + + + + A global interface used for importing surfaces exported by xdg_exporter. + With this interface, a client can create a reference to a surface of + another client. + + + + + Notify the compositor that the xdg_importer object will no longer be + used. + + + + + + The import_toplevel request imports a surface from any client given a handle + retrieved by exporting said surface using xdg_exporter.export_toplevel. + When called, a new xdg_imported object will be created. This new object + represents the imported surface, and the importing client can + manipulate its relationship using it. See xdg_imported for details. + + + + + + + + + An xdg_exported object represents an exported reference to a surface. The + exported surface may be referenced as long as the xdg_exported object not + destroyed. Destroying the xdg_exported invalidates any relationship the + importer may have established using xdg_imported. + + + + + Revoke the previously exported surface. This invalidates any + relationship the importer may have set up using the xdg_imported created + given the handle sent via xdg_exported.handle. + + + + + + The handle event contains the unique handle of this exported surface + reference. It may be shared with any client, which then can use it to + import the surface by calling xdg_importer.import_toplevel. A handle + may be used to import the surface multiple times. + + + + + + + + An xdg_imported object represents an imported reference to surface exported + by some client. A client can use this interface to manipulate + relationships between its own surfaces and the imported surface. + + + + + These errors can be emitted in response to invalid xdg_imported + requests. + + + + + + + Notify the compositor that it will no longer use the xdg_imported + object. Any relationship that may have been set up will at this point + be invalidated. + + + + + + Set the imported surface as the parent of some surface of the client. + The passed surface must be an xdg_toplevel equivalent, otherwise an + invalid_surface protocol error is sent. Calling this function sets up + a surface to surface relation with the same stacking and positioning + semantics as xdg_toplevel.set_parent. + + + + + + + The imported surface handle has been destroyed and any relationship set + up has been invalidated. This may happen for various reasons, for + example if the exported surface or the exported surface handle has been + destroyed, if the handle used for importing was invalid. + + + + +