This MR adds clipboard support to the winewayland driver by using the wlr-data-control-unstable-v1 protocol. Note that this protocol was recently merged into wayland-protocols as ext-data-control-v1, but it's not yet widely supported, so I preferred to use the wlr version for now.
This MR has many similarities to https://gitlab.winehq.org/wine/wine/-/merge_requests/7236, but due to the unconstrained nature of wlr-data-control-unstable-v1 the implementation is simpler and more robust. It can neatly fit into the existing Wine clipboard design (single proxy window in the desktop process) without requiring any event forwarding etc.
Since wlr-data-control-unstable-v1/ext-data-control-v1 is considered privileged, it may not be available in all situations, e.g., in sandboxes, and there are also a couple of compositors that may not implement it at all. It is therefore useful to have a simple fallback using the core wl_data_device interface. This fallback is not part of this MR, but I have pushed a WIP implementation based on this MR at: https://gitlab.winehq.org/afrantzis/wine/-/commits/wayland-copy-paste-data-c... (note: only the win32 -> wayland direction implemented at the moment). The new fallback approach is different from https://gitlab.winehq.org/wine/wine/-/merge_requests/7236 in that when using wl_data_device it creates per-process clipboard windows instead of forwarding messages from the desktop clipboard window. This allows it to work within the design of this MR with minimal changes and maximum code reuse.
-- v3: winewayland: Normalize received MIME type strings. winewayland: Support copying data from native clipboard to win32 apps. winewayland: Support exporting various clipboard formats. winewayland: Generalize support for exporting clipboard formats. winewayland: Support copying text from win32 clipboard to native apps. winewayland: Implement zwlr_data_control_device_v1 initialization.
From: Alexandros Frantzis alexandros.frantzis@collabora.com
--- dlls/winewayland.drv/Makefile.in | 2 + dlls/winewayland.drv/wayland.c | 8 + dlls/winewayland.drv/wayland_data_device.c | 59 ++++ dlls/winewayland.drv/waylanddrv.h | 15 + dlls/winewayland.drv/waylanddrv_main.c | 1 + .../wlr-data-control-unstable-v1.xml | 278 ++++++++++++++++++ 6 files changed, 363 insertions(+) create mode 100644 dlls/winewayland.drv/wayland_data_device.c create mode 100644 dlls/winewayland.drv/wlr-data-control-unstable-v1.xml
diff --git a/dlls/winewayland.drv/Makefile.in b/dlls/winewayland.drv/Makefile.in index 9129a3f2839..142db22ba9e 100644 --- a/dlls/winewayland.drv/Makefile.in +++ b/dlls/winewayland.drv/Makefile.in @@ -14,6 +14,7 @@ SOURCES = \ viewporter.xml \ vulkan.c \ wayland.c \ + wayland_data_device.c \ wayland_keyboard.c \ wayland_output.c \ wayland_pointer.c \ @@ -22,5 +23,6 @@ SOURCES = \ waylanddrv_main.c \ window.c \ window_surface.c \ + wlr-data-control-unstable-v1.xml \ xdg-output-unstable-v1.xml \ xdg-shell.xml diff --git a/dlls/winewayland.drv/wayland.c b/dlls/winewayland.drv/wayland.c index f4170f6dcc4..2fb0edf8023 100644 --- a/dlls/winewayland.drv/wayland.c +++ b/dlls/winewayland.drv/wayland.c @@ -145,6 +145,9 @@ static void registry_handle_global(void *data, struct wl_registry *registry, wl_seat_add_listener(seat->wl_seat, &seat_listener, NULL); pthread_mutex_unlock(&seat->mutex); if (process_wayland.zwp_text_input_manager_v3) wayland_text_input_init(); + /* Recreate the data device for the new seat. */ + if (process_wayland.data_device.zwlr_data_control_device_v1) + wayland_data_device_init(); } else if (strcmp(interface, "wp_viewporter") == 0) { @@ -172,6 +175,11 @@ static void registry_handle_global(void *data, struct wl_registry *registry, wl_registry_bind(registry, id, &zwp_text_input_manager_v3_interface, 1); if (process_wayland.seat.wl_seat) wayland_text_input_init(); } + else if (strcmp(interface, "zwlr_data_control_manager_v1") == 0) + { + process_wayland.zwlr_data_control_manager_v1 = + wl_registry_bind(registry, id, &zwlr_data_control_manager_v1_interface, 1); + } }
static void registry_handle_global_remove(void *data, struct wl_registry *registry, diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c new file mode 100644 index 00000000000..6e53ed4046b --- /dev/null +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -0,0 +1,59 @@ +/* + * Wayland clipboard + * + * Copyright 2025 Alexandros Frantzis for Collabora Ltd + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#if 0 +#pragma makedep unix +#endif + +#include "config.h" + +#include "waylanddrv.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(clipboard); + +void wayland_data_device_init(void) +{ + struct wayland_data_device *data_device = &process_wayland.data_device; + + TRACE("\n"); + + if (data_device->zwlr_data_control_device_v1) + zwlr_data_control_device_v1_destroy(data_device->zwlr_data_control_device_v1); + data_device->zwlr_data_control_device_v1 = + zwlr_data_control_manager_v1_get_data_device( + process_wayland.zwlr_data_control_manager_v1, + process_wayland.seat.wl_seat); +} + +LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) +{ + switch (msg) + { + case WM_NCCREATE: + pthread_mutex_lock(&process_wayland.seat.mutex); + if (process_wayland.seat.wl_seat && process_wayland.zwlr_data_control_manager_v1) + wayland_data_device_init(); + pthread_mutex_unlock(&process_wayland.seat.mutex); + return TRUE; + } + + return NtUserMessageCall(hwnd, msg, wparam, lparam, NULL, NtUserDefWindowProc, FALSE); +} diff --git a/dlls/winewayland.drv/waylanddrv.h b/dlls/winewayland.drv/waylanddrv.h index 5c5ce5bf130..12ac686fbd9 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -35,6 +35,7 @@ #include "viewporter-client-protocol.h" #include "xdg-output-unstable-v1-client-protocol.h" #include "xdg-shell-client-protocol.h" +#include "wlr-data-control-unstable-v1-client-protocol.h"
#include "windef.h" #include "winbase.h" @@ -129,6 +130,11 @@ struct wayland_seat pthread_mutex_t mutex; };
+struct wayland_data_device +{ + struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1; +}; + struct wayland { BOOL initialized; @@ -144,10 +150,12 @@ struct wayland struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1; struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1; struct zwp_text_input_manager_v3 *zwp_text_input_manager_v3; + struct zwlr_data_control_manager_v1 *zwlr_data_control_manager_v1; struct wayland_seat seat; struct wayland_keyboard keyboard; struct wayland_pointer pointer; struct wayland_text_input text_input; + struct wayland_data_device data_device; struct wl_list output_list; /* Protects the output_list and the wayland_output.current states. */ pthread_mutex_t output_mutex; @@ -360,6 +368,12 @@ void wayland_pointer_clear_constraint(void); void wayland_text_input_init(void); void wayland_text_input_deinit(void);
+/********************************************************************** + * Wayland data device + */ + +void wayland_data_device_init(void); + /********************************************************************** * OpenGL */ @@ -391,6 +405,7 @@ RGNDATA *get_region_data(HRGN region); * USER driver functions */
+LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); BOOL WAYLAND_ClipCursor(const RECT *clip, BOOL reset); LRESULT WAYLAND_DesktopWindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); void WAYLAND_DestroyWindow(HWND hwnd); diff --git a/dlls/winewayland.drv/waylanddrv_main.c b/dlls/winewayland.drv/waylanddrv_main.c index 633b2f4a043..ca7ec47b674 100644 --- a/dlls/winewayland.drv/waylanddrv_main.c +++ b/dlls/winewayland.drv/waylanddrv_main.c @@ -35,6 +35,7 @@ char *process_name = NULL;
static const struct user_driver_funcs waylanddrv_funcs = { + .pClipboardWindowProc = WAYLAND_ClipboardWindowProc, .pClipCursor = WAYLAND_ClipCursor, .pDesktopWindowProc = WAYLAND_DesktopWindowProc, .pDestroyWindow = WAYLAND_DestroyWindow, diff --git a/dlls/winewayland.drv/wlr-data-control-unstable-v1.xml b/dlls/winewayland.drv/wlr-data-control-unstable-v1.xml new file mode 100644 index 00000000000..75e8671b0de --- /dev/null +++ b/dlls/winewayland.drv/wlr-data-control-unstable-v1.xml @@ -0,0 +1,278 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="wlr_data_control_unstable_v1"> + <copyright> + Copyright © 2018 Simon Ser + Copyright © 2019 Ivan Molodetskikh + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + </copyright> + + <description summary="control data devices"> + This protocol allows a privileged client to control data devices. In + particular, the client will be able to manage the current selection and take + the role of a clipboard manager. + + 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. + </description> + + <interface name="zwlr_data_control_manager_v1" version="2"> + <description summary="manager to control data devices"> + This interface is a manager that allows creating per-seat data device + controls. + </description> + + <request name="create_data_source"> + <description summary="create a new data source"> + Create a new data source. + </description> + <arg name="id" type="new_id" interface="zwlr_data_control_source_v1" + summary="data source to create"/> + </request> + + <request name="get_data_device"> + <description summary="get a data device for a seat"> + Create a data device that can be used to manage a seat's selection. + </description> + <arg name="id" type="new_id" interface="zwlr_data_control_device_v1"/> + <arg name="seat" type="object" interface="wl_seat"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy the manager"> + All objects created by the manager will still remain valid, until their + appropriate destroy request has been called. + </description> + </request> + </interface> + + <interface name="zwlr_data_control_device_v1" version="2"> + <description summary="manage a data device for a seat"> + This interface allows a client to manage a seat's selection. + + When the seat is destroyed, this object becomes inert. + </description> + + <request name="set_selection"> + <description summary="copy data to the selection"> + This request asks the compositor to set the selection to the data from + the source on behalf of the client. + + The given source may not be used in any further set_selection or + set_primary_selection requests. Attempting to use a previously used + source is a protocol error. + + To unset the selection, set the source to NULL. + </description> + <arg name="source" type="object" interface="zwlr_data_control_source_v1" + allow-null="true"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy this data device"> + Destroys the data device object. + </description> + </request> + + <event name="data_offer"> + <description summary="introduce a new wlr_data_control_offer"> + The data_offer event introduces a new wlr_data_control_offer object, + which will subsequently be used in either the + wlr_data_control_device.selection event (for the regular clipboard + selections) or the wlr_data_control_device.primary_selection event (for + the primary clipboard selections). Immediately following the + wlr_data_control_device.data_offer event, the new data_offer object + will send out wlr_data_control_offer.offer events to describe the MIME + types it offers. + </description> + <arg name="id" type="new_id" interface="zwlr_data_control_offer_v1"/> + </event> + + <event name="selection"> + <description summary="advertise new selection"> + The selection event is sent out to notify the client of a new + wlr_data_control_offer for the selection for this device. The + wlr_data_control_device.data_offer and the wlr_data_control_offer.offer + events are sent out immediately before this event to introduce the data + offer object. The selection event is sent to a client when a new + selection is set. The wlr_data_control_offer is valid until a new + wlr_data_control_offer or NULL is received. The client must destroy the + previous selection wlr_data_control_offer, if any, upon receiving this + event. + + The first selection event is sent upon binding the + wlr_data_control_device object. + </description> + <arg name="id" type="object" interface="zwlr_data_control_offer_v1" + allow-null="true"/> + </event> + + <event name="finished"> + <description summary="this data control is no longer valid"> + This data control object is no longer valid and should be destroyed by + the client. + </description> + </event> + + <!-- Version 2 additions --> + + <event name="primary_selection" since="2"> + <description summary="advertise new primary selection"> + The primary_selection event is sent out to notify the client of a new + wlr_data_control_offer for the primary selection for this device. The + wlr_data_control_device.data_offer and the wlr_data_control_offer.offer + events are sent out immediately before this event to introduce the data + offer object. The primary_selection event is sent to a client when a + new primary selection is set. The wlr_data_control_offer is valid until + a new wlr_data_control_offer or NULL is received. The client must + destroy the previous primary selection wlr_data_control_offer, if any, + upon receiving this event. + + If the compositor supports primary selection, the first + primary_selection event is sent upon binding the + wlr_data_control_device object. + </description> + <arg name="id" type="object" interface="zwlr_data_control_offer_v1" + allow-null="true"/> + </event> + + <request name="set_primary_selection" since="2"> + <description summary="copy data to the primary selection"> + This request asks the compositor to set the primary selection to the + data from the source on behalf of the client. + + The given source may not be used in any further set_selection or + set_primary_selection requests. Attempting to use a previously used + source is a protocol error. + + To unset the primary selection, set the source to NULL. + + The compositor will ignore this request if it does not support primary + selection. + </description> + <arg name="source" type="object" interface="zwlr_data_control_source_v1" + allow-null="true"/> + </request> + + <enum name="error" since="2"> + <entry name="used_source" value="1" + summary="source given to set_selection or set_primary_selection was already used before"/> + </enum> + </interface> + + <interface name="zwlr_data_control_source_v1" version="1"> + <description summary="offer to transfer data"> + The wlr_data_control_source object is the source side of a + wlr_data_control_offer. It is created by the source client in a data + transfer and provides a way to describe the offered data and a way to + respond to requests to transfer the data. + </description> + + <enum name="error"> + <entry name="invalid_offer" value="1" + summary="offer sent after wlr_data_control_device.set_selection"/> + </enum> + + <request name="offer"> + <description summary="add an offered MIME type"> + This request adds a MIME type to the set of MIME types advertised to + targets. Can be called several times to offer multiple types. + + Calling this after wlr_data_control_device.set_selection is a protocol + error. + </description> + <arg name="mime_type" type="string" + summary="MIME type offered by the data source"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy this source"> + Destroys the data source object. + </description> + </request> + + <event name="send"> + <description summary="send the data"> + Request for data from the client. Send the data as the specified MIME + type over the passed file descriptor, then close it. + </description> + <arg name="mime_type" type="string" summary="MIME type for the data"/> + <arg name="fd" type="fd" summary="file descriptor for the data"/> + </event> + + <event name="cancelled"> + <description summary="selection was cancelled"> + This data source is no longer valid. The data source has been replaced + by another data source. + + The client should clean up and destroy this data source. + </description> + </event> + </interface> + + <interface name="zwlr_data_control_offer_v1" version="1"> + <description summary="offer to transfer data"> + A wlr_data_control_offer represents a piece of data offered for transfer + by another client (the source client). The offer describes the different + MIME types that the data can be converted to and provides the mechanism + for transferring the data directly from the source client. + </description> + + <request name="receive"> + <description summary="request that the data is transferred"> + To transfer the offered data, the client issues this request and + indicates the MIME type it wants to receive. The transfer happens + through the passed file descriptor (typically created with the pipe + system call). The source client writes the data in the MIME type + representation requested and then closes the file descriptor. + + The receiving client reads from the read end of the pipe until EOF and + then closes its end, at which point the transfer is complete. + + This request may happen multiple times for different MIME types. + </description> + <arg name="mime_type" type="string" + summary="MIME type desired by receiver"/> + <arg name="fd" type="fd" summary="file descriptor for data transfer"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy this offer"> + Destroys the data offer object. + </description> + </request> + + <event name="offer"> + <description summary="advertise offered MIME type"> + Sent immediately after creating the wlr_data_control_offer object. + One event per offered MIME type. + </description> + <arg name="mime_type" type="string" summary="offered MIME type"/> + </event> + </interface> +</protocol>
From: Alexandros Frantzis alexandros.frantzis@collabora.com
--- dlls/winewayland.drv/wayland.c | 1 + dlls/winewayland.drv/wayland_data_device.c | 173 +++++++++++++++++++++ dlls/winewayland.drv/waylanddrv.h | 2 + 3 files changed, 176 insertions(+)
diff --git a/dlls/winewayland.drv/wayland.c b/dlls/winewayland.drv/wayland.c index 2fb0edf8023..ee162d8fe77 100644 --- a/dlls/winewayland.drv/wayland.c +++ b/dlls/winewayland.drv/wayland.c @@ -38,6 +38,7 @@ struct wayland process_wayland = .keyboard.mutex = PTHREAD_MUTEX_INITIALIZER, .pointer.mutex = PTHREAD_MUTEX_INITIALIZER, .text_input.mutex = PTHREAD_MUTEX_INITIALIZER, + .data_device.mutex = PTHREAD_MUTEX_INITIALIZER, .output_list = {&process_wayland.output_list, &process_wayland.output_list}, .output_mutex = PTHREAD_MUTEX_INITIALIZER }; diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index 6e53ed4046b..b51f5b25ed4 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -24,23 +24,191 @@
#include "config.h"
+#include <errno.h> +#include <stdlib.h> +#include <unistd.h> + #include "waylanddrv.h" #include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
+static HWND clipboard_hwnd; + +static void write_all(int fd, const void *buf, size_t count) +{ + size_t nwritten = 0; + ssize_t ret; + + while (nwritten < count) + { + ret = write(fd, (const char*)buf + nwritten, count - nwritten); + if (ret == -1 && errno != EINTR) break; + else if (ret > 0) nwritten += ret; + } + + if (nwritten < count) + { + WARN("Failed to write all clipboard data, had %zu bytes, wrote %zu bytes\n", + count, nwritten); + } +} + +static void *export_unicode_text(void *data, size_t size, size_t *ret_size) +{ + DWORD byte_count; + char *bytes; + + /* Wayland apps expect strings to not be zero-terminated, so avoid + * zero-terminating the resulting converted string. */ + if (size >= sizeof(WCHAR) && ((WCHAR *)data)[size / sizeof(WCHAR) - 1] == 0) + size -= sizeof(WCHAR); + + RtlUnicodeToUTF8N(NULL, 0, &byte_count, data, size); + if (!(bytes = malloc(byte_count))) return NULL; + RtlUnicodeToUTF8N(bytes, byte_count, &byte_count, data, size); + + *ret_size = byte_count; + return bytes; +} + +/********************************************************************** + * zwlr_data_control_source_v1 handling + */ + +static void wayland_data_source_export(int32_t fd) +{ + struct get_clipboard_params params = { .data_only = TRUE, .size = 1024 }; + void *exported = NULL; + size_t exported_size; + + TRACE("\n"); + + if (!NtUserOpenClipboard(clipboard_hwnd, 0)) + { + TRACE("failed to open clipboard for export\n"); + return; + } + + for (;;) + { + if (!(params.data = malloc(params.size))) break; + if (NtUserGetClipboardData(CF_UNICODETEXT, ¶ms)) + { + exported = export_unicode_text(params.data, params.size, &exported_size); + break; + } + if (!params.data_size) break; + free(params.data); + params.size = params.data_size; + params.data_size = 0; + } + + NtUserCloseClipboard(); + if (exported) write_all(fd, exported, exported_size); + + free(exported); + free(params.data); +} + +static void data_control_source_send(void *data, + struct zwlr_data_control_source_v1 *source, + const char *mime_type, int32_t fd) +{ + if (!strcmp(mime_type, "text/plain;charset=utf-8")) + wayland_data_source_export(fd); + close(fd); +} + +static void data_control_source_cancelled(void *data, + struct zwlr_data_control_source_v1 *source) +{ + struct wayland_data_device *data_device = data; + + pthread_mutex_lock(&data_device->mutex); + zwlr_data_control_source_v1_destroy(source); + if (source == data_device->zwlr_data_control_source_v1) + data_device->zwlr_data_control_source_v1 = NULL; + pthread_mutex_unlock(&data_device->mutex); +} + +static const struct zwlr_data_control_source_v1_listener data_control_source_listener = +{ + data_control_source_send, + data_control_source_cancelled, +}; + void wayland_data_device_init(void) { struct wayland_data_device *data_device = &process_wayland.data_device;
TRACE("\n");
+ pthread_mutex_lock(&data_device->mutex); if (data_device->zwlr_data_control_device_v1) zwlr_data_control_device_v1_destroy(data_device->zwlr_data_control_device_v1); data_device->zwlr_data_control_device_v1 = zwlr_data_control_manager_v1_get_data_device( process_wayland.zwlr_data_control_manager_v1, process_wayland.seat.wl_seat); + pthread_mutex_unlock(&data_device->mutex); +} + +static void clipboard_update(void) +{ + struct wayland_data_device *data_device = &process_wayland.data_device; + struct zwlr_data_control_source_v1 *source; + UINT *formats, formats_size = 256, i; + + if (!process_wayland.zwlr_data_control_manager_v1) return; + + TRACE("\n"); + + source = zwlr_data_control_manager_v1_create_data_source( + process_wayland.zwlr_data_control_manager_v1); + if (!source) + { + ERR("failed to create data source\n"); + return; + } + + for (;;) + { + if (!(formats = malloc(formats_size * sizeof(*formats)))) break; + if (NtUserGetUpdatedClipboardFormats(formats, formats_size, &formats_size)) break; + free(formats); + formats = NULL; + if (RtlGetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER) break; + } + + if (!formats && formats_size) + { + ERR("failed to get clipboard formats\n"); + zwlr_data_control_source_v1_destroy(source); + return; + } + + for (i = 0; i < formats_size; ++i) + { + if (formats[i] == CF_UNICODETEXT) + zwlr_data_control_source_v1_offer(source, "text/plain;charset=utf-8"); + } + + free(formats); + + zwlr_data_control_source_v1_add_listener(source, &data_control_source_listener, data_device); + + pthread_mutex_lock(&data_device->mutex); + if (data_device->zwlr_data_control_device_v1) + zwlr_data_control_device_v1_set_selection(data_device->zwlr_data_control_device_v1, source); + /* Destroy any previous source only after setting the new source, to + * avoid spurious 'selection(nil)' events. */ + if (data_device->zwlr_data_control_source_v1) + zwlr_data_control_source_v1_destroy(data_device->zwlr_data_control_source_v1); + data_device->zwlr_data_control_source_v1 = source; + pthread_mutex_unlock(&data_device->mutex); + + wl_display_flush(process_wayland.wl_display); }
LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) @@ -48,11 +216,16 @@ LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l switch (msg) { case WM_NCCREATE: + clipboard_hwnd = hwnd; + NtUserAddClipboardFormatListener(hwnd); pthread_mutex_lock(&process_wayland.seat.mutex); if (process_wayland.seat.wl_seat && process_wayland.zwlr_data_control_manager_v1) wayland_data_device_init(); pthread_mutex_unlock(&process_wayland.seat.mutex); return TRUE; + case WM_CLIPBOARDUPDATE: + clipboard_update(); + break; }
return NtUserMessageCall(hwnd, msg, wparam, lparam, NULL, NtUserDefWindowProc, FALSE); diff --git a/dlls/winewayland.drv/waylanddrv.h b/dlls/winewayland.drv/waylanddrv.h index 12ac686fbd9..c8f0ede23ba 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -133,6 +133,8 @@ struct wayland_seat struct wayland_data_device { struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1; + struct zwlr_data_control_source_v1 *zwlr_data_control_source_v1; + pthread_mutex_t mutex; };
struct wayland
From: Alexandros Frantzis alexandros.frantzis@collabora.com
Introduce the infrastructure to export various clipboard formats in a table-driven manner, similar to what's used by winex11. --- dlls/winewayland.drv/wayland_data_device.c | 62 +++++++++++++++++++--- 1 file changed, 54 insertions(+), 8 deletions(-)
diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index b51f5b25ed4..77b9f4f7af0 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -33,6 +33,13 @@
WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
+struct data_device_format +{ + const char *mime_type; + UINT clipboard_format; + void *(*export)(void *data, size_t size, size_t *ret_size); +}; + static HWND clipboard_hwnd;
static void write_all(int fd, const void *buf, size_t count) @@ -72,17 +79,49 @@ static void *export_unicode_text(void *data, size_t size, size_t *ret_size) return bytes; }
+/* Order is important. When selecting a mime-type for a clipboard format we + * will choose the first entry that matches the specified clipboard format. */ +static struct data_device_format supported_formats[] = +{ + {"text/plain;charset=utf-8", CF_UNICODETEXT, export_unicode_text}, + {NULL, 0, NULL}, +}; + +static struct data_device_format *data_device_format_for_clipboard_format(UINT clipboard_format) +{ + struct data_device_format *format; + + for (format = supported_formats; format->mime_type; ++format) + { + if (format->clipboard_format == clipboard_format) return format; + } + + return NULL; +} + +static struct data_device_format *data_device_format_for_mime_type(const char *mime) +{ + struct data_device_format *format; + + for (format = supported_formats; format->mime_type; ++format) + { + if (!strcmp(mime, format->mime_type)) return format; + } + + return NULL; +} + /********************************************************************** * zwlr_data_control_source_v1 handling */
-static void wayland_data_source_export(int32_t fd) +static void wayland_data_source_export(struct data_device_format *format, int fd) { struct get_clipboard_params params = { .data_only = TRUE, .size = 1024 }; void *exported = NULL; size_t exported_size;
- TRACE("\n"); + TRACE("format=%u => mime=%s\n", format->clipboard_format, format->mime_type);
if (!NtUserOpenClipboard(clipboard_hwnd, 0)) { @@ -93,9 +132,9 @@ static void wayland_data_source_export(int32_t fd) for (;;) { if (!(params.data = malloc(params.size))) break; - if (NtUserGetClipboardData(CF_UNICODETEXT, ¶ms)) + if (NtUserGetClipboardData(format->clipboard_format, ¶ms)) { - exported = export_unicode_text(params.data, params.size, &exported_size); + exported = format->export(params.data, params.size, &exported_size); break; } if (!params.data_size) break; @@ -115,8 +154,10 @@ static void data_control_source_send(void *data, struct zwlr_data_control_source_v1 *source, const char *mime_type, int32_t fd) { - if (!strcmp(mime_type, "text/plain;charset=utf-8")) - wayland_data_source_export(fd); + struct data_device_format *format = + data_device_format_for_mime_type(mime_type); + + if (format) wayland_data_source_export(format, fd); close(fd); }
@@ -190,8 +231,13 @@ static void clipboard_update(void)
for (i = 0; i < formats_size; ++i) { - if (formats[i] == CF_UNICODETEXT) - zwlr_data_control_source_v1_offer(source, "text/plain;charset=utf-8"); + struct data_device_format *format = + data_device_format_for_clipboard_format(formats[i]); + if (format) + { + TRACE("offering mime=%s for format=%u\n", format->mime_type, formats[i]); + zwlr_data_control_source_v1_offer(source, format->mime_type); + } }
free(formats);
From: Alexandros Frantzis alexandros.frantzis@collabora.com
Add support for some formats commonly used in applications and which do not require special exporting: RTF, TIFF, PNG, JPEG, GIF, SVG, RIFF, WAV. --- dlls/winewayland.drv/wayland_data_device.c | 39 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index 77b9f4f7af0..1659e9bd6df 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -37,10 +37,16 @@ struct data_device_format { const char *mime_type; UINT clipboard_format; + const WCHAR *register_name; void *(*export)(void *data, size_t size, size_t *ret_size); };
static HWND clipboard_hwnd; +static const WCHAR rich_text_formatW[] = {'R','i','c','h',' ','T','e','x','t',' ','F','o','r','m','a','t',0}; +static const WCHAR pngW[] = {'P','N','G',0}; +static const WCHAR jfifW[] = {'J','F','I','F',0}; +static const WCHAR gifW[] = {'G','I','F',0}; +static const WCHAR image_svg_xmlW[] = {'i','m','a','g','e','/','s','v','g','+','x','m','l',0};
static void write_all(int fd, const void *buf, size_t count) { @@ -79,11 +85,26 @@ static void *export_unicode_text(void *data, size_t size, size_t *ret_size) return bytes; }
+static void *export_data(void *data, size_t size, size_t *ret_size) +{ + *ret_size = size; + return data; +} + /* Order is important. When selecting a mime-type for a clipboard format we * will choose the first entry that matches the specified clipboard format. */ static struct data_device_format supported_formats[] = { - {"text/plain;charset=utf-8", CF_UNICODETEXT, export_unicode_text}, + {"text/plain;charset=utf-8", CF_UNICODETEXT, NULL, export_unicode_text}, + {"text/rtf", 0, rich_text_formatW, export_data}, + {"image/tiff", CF_TIFF, NULL, export_data}, + {"image/png", 0, pngW, export_data}, + {"image/jpeg", 0, jfifW, export_data}, + {"image/gif", 0, gifW, export_data}, + {"image/svg+xml", 0, image_svg_xmlW, export_data}, + {"application/x-riff", CF_RIFF, NULL, export_data}, + {"audio/wav", CF_WAVE, NULL, export_data}, + {"audio/x-wav", CF_WAVE, NULL, export_data}, {NULL, 0, NULL}, };
@@ -111,6 +132,13 @@ static struct data_device_format *data_device_format_for_mime_type(const char *m return NULL; }
+static ATOM register_clipboard_format(const WCHAR *name) +{ + ATOM atom; + if (NtAddAtom(name, lstrlenW(name) * sizeof(WCHAR), &atom)) return 0; + return atom; +} + /********************************************************************** * zwlr_data_control_source_v1 handling */ @@ -146,7 +174,7 @@ static void wayland_data_source_export(struct data_device_format *format, int fd NtUserCloseClipboard(); if (exported) write_all(fd, exported, exported_size);
- free(exported); + if (exported != params.data) free(exported); free(params.data); }
@@ -182,6 +210,7 @@ static const struct zwlr_data_control_source_v1_listener data_control_source_lis void wayland_data_device_init(void) { struct wayland_data_device *data_device = &process_wayland.data_device; + struct data_device_format *format = supported_formats;
TRACE("\n");
@@ -193,6 +222,12 @@ void wayland_data_device_init(void) process_wayland.zwlr_data_control_manager_v1, process_wayland.seat.wl_seat); pthread_mutex_unlock(&data_device->mutex); + + for (; format->mime_type; ++format) + { + if (format->clipboard_format == 0) + format->clipboard_format = register_clipboard_format(format->register_name); + } }
static void clipboard_update(void)
From: Alexandros Frantzis alexandros.frantzis@collabora.com
--- dlls/winewayland.drv/wayland_data_device.c | 379 ++++++++++++++++++++- dlls/winewayland.drv/waylanddrv.h | 1 + 2 files changed, 367 insertions(+), 13 deletions(-)
diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index 1659e9bd6df..4018386b97c 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -25,6 +25,7 @@ #include "config.h"
#include <errno.h> +#include <fcntl.h> #include <stdlib.h> #include <unistd.h>
@@ -33,12 +34,23 @@
WINE_DEFAULT_DEBUG_CHANNEL(clipboard);
+/* A special MIME type we mark our data offers with, so we can detect that + * they are coming from us. */ +#define WINEWAYLAND_TAG_MIME_TYPE "application/x.winewayland.tag" + struct data_device_format { const char *mime_type; UINT clipboard_format; const WCHAR *register_name; void *(*export)(void *data, size_t size, size_t *ret_size); + void *(*import)(void *data, size_t size, size_t *ret_size); +}; + +struct wayland_data_offer +{ + struct zwlr_data_control_offer_v1 *zwlr_data_control_offer_v1; + struct wl_array types; };
static HWND clipboard_hwnd; @@ -67,6 +79,59 @@ static void write_all(int fd, const void *buf, size_t count) } }
+static void *read_all(int fd, size_t *size_out) +{ + size_t buffer_size = 4096; + int total = 0; + unsigned char *buffer; + int nread; + + if (!(buffer = malloc(buffer_size))) + { + ERR("failed to allocate read buffer\n"); + goto out; + } + + do + { + nread = read(fd, buffer + total, buffer_size - total); + if (nread == -1 && errno != EINTR) + { + TRACE("failed to read from fd (errno: %d)\n", errno); + total = 0; + goto out; + } + else if (nread > 0) + { + total += nread; + if (total == buffer_size) + { + unsigned char *new_buffer; + buffer_size *= 2; + new_buffer = realloc(buffer, buffer_size); + if (!new_buffer) + { + ERR("failed to reallocate read buffer\n"); + total = 0; + goto out; + } + buffer = new_buffer; + } + } + } while (nread > 0); + + TRACE("read %d bytes\n", total); + +out: + if (total == 0 && buffer != NULL) + { + free(buffer); + buffer = NULL; + } + *size_out = total; + return buffer; +} + static void *export_unicode_text(void *data, size_t size, size_t *ret_size) { DWORD byte_count; @@ -91,30 +156,66 @@ static void *export_data(void *data, size_t size, size_t *ret_size) return data; }
+static void *import_unicode_text(void *data, size_t size, size_t *ret_size) +{ + DWORD wsize; + WCHAR *ret; + + RtlUTF8ToUnicodeN(NULL, 0, &wsize, data, size); + if (!(ret = malloc(wsize + sizeof(WCHAR)))) return NULL; + RtlUTF8ToUnicodeN(ret, wsize, &wsize, data, size); + ret[wsize / sizeof(WCHAR)] = 0; + + *ret_size = wsize + sizeof(WCHAR); + + return ret; +} + +static void *import_data(void *data, size_t size, size_t *ret_size) +{ + *ret_size = size; + return data; +} + /* Order is important. When selecting a mime-type for a clipboard format we * will choose the first entry that matches the specified clipboard format. */ static struct data_device_format supported_formats[] = { - {"text/plain;charset=utf-8", CF_UNICODETEXT, NULL, export_unicode_text}, - {"text/rtf", 0, rich_text_formatW, export_data}, - {"image/tiff", CF_TIFF, NULL, export_data}, - {"image/png", 0, pngW, export_data}, - {"image/jpeg", 0, jfifW, export_data}, - {"image/gif", 0, gifW, export_data}, - {"image/svg+xml", 0, image_svg_xmlW, export_data}, - {"application/x-riff", CF_RIFF, NULL, export_data}, - {"audio/wav", CF_WAVE, NULL, export_data}, - {"audio/x-wav", CF_WAVE, NULL, export_data}, + {"text/plain;charset=utf-8", CF_UNICODETEXT, NULL, export_unicode_text, import_unicode_text}, + {"text/rtf", 0, rich_text_formatW, export_data, import_data}, + {"image/tiff", CF_TIFF, NULL, export_data, import_data}, + {"image/png", 0, pngW, export_data, import_data}, + {"image/jpeg", 0, jfifW, export_data, import_data}, + {"image/gif", 0, gifW, export_data, import_data}, + {"image/svg+xml", 0, image_svg_xmlW, export_data, import_data}, + {"application/x-riff", CF_RIFF, NULL, export_data, import_data}, + {"audio/wav", CF_WAVE, NULL, export_data, import_data}, + {"audio/x-wav", CF_WAVE, NULL, export_data, import_data}, {NULL, 0, NULL}, };
-static struct data_device_format *data_device_format_for_clipboard_format(UINT clipboard_format) +static BOOL string_array_contains(struct wl_array *array, const char *str) +{ + char **p; + + wl_array_for_each(p, array) + if (!strcmp(*p, str)) return TRUE; + + return FALSE; +} + +static struct data_device_format *data_device_format_for_clipboard_format(UINT clipboard_format, + struct wl_array *types) { struct data_device_format *format;
for (format = supported_formats; format->mime_type; ++format) { - if (format->clipboard_format == clipboard_format) return format; + if (format->clipboard_format == clipboard_format && + (!types || string_array_contains(types, format->mime_type))) + { + return format; + } }
return NULL; @@ -207,6 +308,202 @@ static const struct zwlr_data_control_source_v1_listener data_control_source_lis data_control_source_cancelled, };
+/********************************************************************** + * zwlr_data_control_offer_v1 handling + */ + +static void data_control_offer_offer(void *data, + struct zwlr_data_control_offer_v1 *zwlr_data_control_offer_v1, + const char *type) +{ + struct wayland_data_offer *data_offer = data; + const char *type_copy; + const char **p; + + if ((type_copy = strdup(type)) && + (p = wl_array_add(&data_offer->types, sizeof *p))) + { + *p = type_copy; + } +} + +static const struct zwlr_data_control_offer_v1_listener data_control_offer_listener = +{ + data_control_offer_offer, +}; + +static void wayland_data_offer_create(struct zwlr_data_control_offer_v1 *zwlr_data_control_offer_v1) +{ + struct wayland_data_offer *data_offer; + + if (!(data_offer = calloc(1, sizeof(*data_offer)))) + { + ERR("Failed to allocate memory for data offer\n"); + return; + } + + data_offer->zwlr_data_control_offer_v1 = zwlr_data_control_offer_v1; + wl_array_init(&data_offer->types); + zwlr_data_control_offer_v1_add_listener(data_offer->zwlr_data_control_offer_v1, + &data_control_offer_listener, data_offer); +} + +static void wayland_data_offer_destroy(struct wayland_data_offer *data_offer) +{ + char **p; + + zwlr_data_control_offer_v1_destroy(data_offer->zwlr_data_control_offer_v1); + wl_array_for_each(p, &data_offer->types) + free(*p); + wl_array_release(&data_offer->types); + free(data_offer); +} + +static int wayland_data_offer_get_import_fd(struct wayland_data_offer *data_offer, + const char *mime_type) +{ + int data_pipe[2]; + +#if HAVE_PIPE2 + if (pipe2(data_pipe, O_CLOEXEC) == -1) +#endif + { + if (pipe(data_pipe) == -1) + { + ERR("failed to create clipboard data pipe\n"); + return -1; + } + fcntl(data_pipe[0], F_SETFD, FD_CLOEXEC); + fcntl(data_pipe[1], F_SETFD, FD_CLOEXEC); + } + + zwlr_data_control_offer_v1_receive(data_offer->zwlr_data_control_offer_v1, + mime_type, data_pipe[1]); + close(data_pipe[1]); + + /* Flush to ensure our receive request reaches the server. */ + wl_display_flush(process_wayland.wl_display); + + return data_pipe[0]; +} + +static void *import_format(int fd, struct data_device_format *format, size_t *ret_size) +{ + size_t size; + void *data, *ret; + + if (!(data = read_all(fd, &size))) return NULL; + ret = format->import(data, size, ret_size); + if (ret != data) free(data); + return ret; +} + +/********************************************************************** + * zwlr_data_control_device_v1 handling + */ + +static void wayland_data_device_destroy_clipboard_data_offer(struct wayland_data_device *data_device) +{ + if (data_device->clipboard_zwlr_data_control_offer_v1) + { + struct wayland_data_offer *data_offer = + zwlr_data_control_offer_v1_get_user_data(data_device->clipboard_zwlr_data_control_offer_v1); + if (data_offer) wayland_data_offer_destroy(data_offer); + data_device->clipboard_zwlr_data_control_offer_v1 = NULL; + } +} + +static void data_control_device_data_offer( + void *data, + struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1, + struct zwlr_data_control_offer_v1 *zwlr_data_control_offer_v1) +{ + wayland_data_offer_create(zwlr_data_control_offer_v1); +} + +static void clipboard_update(void); + +static void data_control_device_selection( + void *data, + struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1, + struct zwlr_data_control_offer_v1 *zwlr_data_control_offer_v1) +{ + struct wayland_data_device *data_device = data; + struct wayland_data_offer *data_offer = NULL; + char **p; + + if (!zwlr_data_control_offer_v1 || + !(data_offer = zwlr_data_control_offer_v1_get_user_data(zwlr_data_control_offer_v1))) + { + TRACE("empty offer, clearing clipboard\n"); + if (NtUserOpenClipboard(clipboard_hwnd, 0)) + { + NtUserEmptyClipboard(); + NtUserCloseClipboard(); + } + goto done; + } + + TRACE("updating clipboard from wayland offer\n"); + + /* If this offer contains the special winewayland tag mime-type, it was sent + * by a winewayland process to notify external wayland clients about a Wine + * clipboard update. */ + wl_array_for_each(p, &data_offer->types) + { + if (!strcmp(*p, WINEWAYLAND_TAG_MIME_TYPE)) + { + TRACE("offer sent by winewayland, ignoring\n"); + wayland_data_offer_destroy(data_offer); + data_offer = NULL; + goto done; + } + } + + if (!NtUserOpenClipboard(clipboard_hwnd, 0)) + { + TRACE("failed to open clipboard for selection\n"); + wayland_data_offer_destroy(data_offer); + data_offer = NULL; + goto done; + } + + NtUserEmptyClipboard(); + + /* For each mime type, mark that we have available clipboard data. */ + wl_array_for_each(p, &data_offer->types) + { + struct data_device_format *format = data_device_format_for_mime_type(*p); + if (format) + { + struct set_clipboard_params params = {0}; + TRACE("available clipboard format for %s => %u\n", + *p, format->clipboard_format); + NtUserSetClipboardData(format->clipboard_format, 0, ¶ms); + } + } + + NtUserCloseClipboard(); + +done: + pthread_mutex_lock(&data_device->mutex); + wayland_data_device_destroy_clipboard_data_offer(data_device); + if (data_offer) data_device->clipboard_zwlr_data_control_offer_v1 = zwlr_data_control_offer_v1; + pthread_mutex_unlock(&data_device->mutex); +} + +static void data_control_device_finished( + void *data, struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1) +{ +} + +static const struct zwlr_data_control_device_v1_listener data_control_device_listener = +{ + data_control_device_data_offer, + data_control_device_selection, + data_control_device_finished, +}; + void wayland_data_device_init(void) { struct wayland_data_device *data_device = &process_wayland.data_device; @@ -221,6 +518,12 @@ void wayland_data_device_init(void) zwlr_data_control_manager_v1_get_data_device( process_wayland.zwlr_data_control_manager_v1, process_wayland.seat.wl_seat); + if (data_device->zwlr_data_control_device_v1) + { + zwlr_data_control_device_v1_add_listener( + data_device->zwlr_data_control_device_v1, &data_control_device_listener, + data_device); + } pthread_mutex_unlock(&data_device->mutex);
for (; format->mime_type; ++format) @@ -267,7 +570,7 @@ static void clipboard_update(void) for (i = 0; i < formats_size; ++i) { struct data_device_format *format = - data_device_format_for_clipboard_format(formats[i]); + data_device_format_for_clipboard_format(formats[i], NULL); if (format) { TRACE("offering mime=%s for format=%u\n", format->mime_type, formats[i]); @@ -277,6 +580,7 @@ static void clipboard_update(void)
free(formats);
+ zwlr_data_control_source_v1_offer(source, WINEWAYLAND_TAG_MIME_TYPE); zwlr_data_control_source_v1_add_listener(source, &data_control_source_listener, data_device);
pthread_mutex_lock(&data_device->mutex); @@ -292,6 +596,48 @@ static void clipboard_update(void) wl_display_flush(process_wayland.wl_display); }
+static void render_format(UINT clipboard_format) +{ + struct wayland_data_device *data_device = &process_wayland.data_device; + struct wayland_data_offer *data_offer; + struct data_device_format *format; + int import_fd = -1; + + TRACE("clipboard_format=%u\n", clipboard_format); + + pthread_mutex_lock(&data_device->mutex); + if (data_device->clipboard_zwlr_data_control_offer_v1 && + (data_offer = zwlr_data_control_offer_v1_get_user_data(data_device->clipboard_zwlr_data_control_offer_v1)) && + (format = data_device_format_for_clipboard_format(clipboard_format, + &data_offer->types))) + { + import_fd = wayland_data_offer_get_import_fd(data_offer, format->mime_type); + } + pthread_mutex_unlock(&data_device->mutex); + + if (import_fd >= 0) + { + struct set_clipboard_params params = {0}; + if ((params.data = import_format(import_fd, format, ¶ms.size))) + { + NtUserSetClipboardData(format->clipboard_format, 0, ¶ms); + free(params.data); + } + close(import_fd); + } +} + +static void destroy_clipboard(void) +{ + struct wayland_data_device *data_device = &process_wayland.data_device; + + TRACE("\n"); + + pthread_mutex_lock(&data_device->mutex); + wayland_data_device_destroy_clipboard_data_offer(data_device); + pthread_mutex_unlock(&data_device->mutex); +} + LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch (msg) @@ -305,8 +651,15 @@ LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l pthread_mutex_unlock(&process_wayland.seat.mutex); return TRUE; case WM_CLIPBOARDUPDATE: + if (NtUserGetClipboardOwner() == clipboard_hwnd) break; clipboard_update(); break; + case WM_RENDERFORMAT: + render_format(wparam); + break; + case WM_DESTROYCLIPBOARD: + destroy_clipboard(); + break; }
return NtUserMessageCall(hwnd, msg, wparam, lparam, NULL, NtUserDefWindowProc, FALSE); diff --git a/dlls/winewayland.drv/waylanddrv.h b/dlls/winewayland.drv/waylanddrv.h index c8f0ede23ba..c389167c3b7 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -134,6 +134,7 @@ struct wayland_data_device { struct zwlr_data_control_device_v1 *zwlr_data_control_device_v1; struct zwlr_data_control_source_v1 *zwlr_data_control_source_v1; + struct zwlr_data_control_offer_v1 *clipboard_zwlr_data_control_offer_v1; pthread_mutex_t mutex; };
From: Alexandros Frantzis alexandros.frantzis@collabora.com
--- dlls/winewayland.drv/wayland_data_device.c | 45 +++++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-)
diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index 4018386b97c..04872df4fcb 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -60,6 +60,35 @@ static const WCHAR jfifW[] = {'J','F','I','F',0}; static const WCHAR gifW[] = {'G','I','F',0}; static const WCHAR image_svg_xmlW[] = {'i','m','a','g','e','/','s','v','g','+','x','m','l',0};
+/* Normalize the MIME type string by skipping inconsequential characters, + * such as spaces and double quotes, and convert to lower case. */ +static const char *normalize_mime_type(const char *mime_type) +{ + char *new_mime_type; + const char *cur_read; + char *cur_write; + size_t new_mime_len = 0; + + for (cur_read = mime_type; *cur_read != '\0'; ++cur_read) + { + if (*cur_read != ' ' && *cur_read != '"') + new_mime_len++; + } + + new_mime_type = malloc(new_mime_len + 1); + if (!new_mime_type) return NULL; + + for (cur_read = mime_type, cur_write = new_mime_type; *cur_read != '\0'; ++cur_read) + { + if (*cur_read != ' ' && *cur_read != '"') + *cur_write++ = tolower(*cur_read); + } + + *cur_write = '\0'; + + return new_mime_type; +} + static void write_all(int fd, const void *buf, size_t count) { size_t nwritten = 0; @@ -283,10 +312,14 @@ static void data_control_source_send(void *data, struct zwlr_data_control_source_v1 *source, const char *mime_type, int32_t fd) { - struct data_device_format *format = - data_device_format_for_mime_type(mime_type); + struct data_device_format *format; + const char *normalized;
- if (format) wayland_data_source_export(format, fd); + if ((normalized = normalize_mime_type(mime_type)) && + (format = data_device_format_for_mime_type(normalized))) + { + wayland_data_source_export(format, fd); + } close(fd); }
@@ -317,13 +350,13 @@ static void data_control_offer_offer(void *data, const char *type) { struct wayland_data_offer *data_offer = data; - const char *type_copy; + const char *normalized; const char **p;
- if ((type_copy = strdup(type)) && + if ((normalized = normalize_mime_type(type)) && (p = wl_array_add(&data_offer->types, sizeof *p))) { - *p = type_copy; + *p = normalized; } }
On Thu Feb 27 14:56:41 2025 +0000, Alexandros Frantzis wrote:
changed this line in [version 3 of the diff](/wine/wine/-/merge_requests/7336/diffs?diff_id=160836&start_sha=bd0ab7e566ddbcff81ffad81f3eac60149f2dc17#86ae8b2d3d29d93b74b2e476e24778ab0070dca1_93_92)
Removed the timeouts. Although, as mentioned before, avoiding blocking reads is standard practice in Wayland clients, it rarely is a problem in practice and we are also better off in this MR (compared to !7236), since only the clipboard thread will be directly affected.
Other than that I don't really have anything meaningful to say about it. It could probably use a bit more traces for debugging.
I have added some more traces, but let me know if you would like some more in particular (note that I typically use WAYLAND_DEBUG=1 along with WINEDEBUG, to get some information about the protocol traffic).
I believe there's some discussion on !7236 (comment 95147) about whether this protocol is the right one to use. This one looks simpler, though I don't really have an opinion. It's working fine with labwc as far as I can tell.
Unfortunately we have touched upon a controversial topic in Wayland protocol land (see my reply in the other thread), with fair arguments on both sides.
Yet another alternative here (because we don't have enough already :)), is to go with the "fallback" (wl_data_device) approach first (effectively doing the inverse of the fallback branch mentioned in the description), and give some time to the community to reach a consensus on acceptable uses for the wlr/ext-data-control.
Seems okay to me. I don't have an opinion about using this protocol over another, it seems to me that this one is a better fit for what Wine really does: give unrestricted access to the clipboard to Windows applications. We are not a sandbox and we shouldn't implement restrictions that don't exist on Windows ourselves.
This merge request was approved by Rémi Bernon.