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.
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 | 52 ++++ dlls/winewayland.drv/waylanddrv.h | 15 + dlls/winewayland.drv/waylanddrv_main.c | 1 + .../wlr-data-control-unstable-v1.xml | 278 ++++++++++++++++++ 6 files changed, 356 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 9ad1ad6889d..627604da0b9 100644 --- a/dlls/winewayland.drv/Makefile.in +++ b/dlls/winewayland.drv/Makefile.in @@ -13,6 +13,7 @@ SOURCES = \ viewporter.xml \ vulkan.c \ wayland.c \ + wayland_data_device.c \ wayland_keyboard.c \ wayland_output.c \ wayland_pointer.c \ @@ -20,5 +21,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 9432dc934c9..7fddb77338a 100644 --- a/dlls/winewayland.drv/wayland.c +++ b/dlls/winewayland.drv/wayland.c @@ -143,6 +143,9 @@ static void registry_handle_global(void *data, struct wl_registry *registry, seat->global_id = id; wl_seat_add_listener(seat->wl_seat, &seat_listener, NULL); pthread_mutex_unlock(&seat->mutex); + /* 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) { @@ -164,6 +167,11 @@ static void registry_handle_global(void *data, struct wl_registry *registry, process_wayland.zwp_relative_pointer_manager_v1 = wl_registry_bind(registry, id, &zwp_relative_pointer_manager_v1_interface, 1); } + 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..17ba8cddb03 --- /dev/null +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -0,0 +1,52 @@ +/* + * 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" + +void wayland_data_device_init(void) +{ + struct wayland_data_device *data_device = &process_wayland.data_device; + + 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: + if (process_wayland.seat.wl_seat && process_wayland.zwlr_data_control_manager_v1) + wayland_data_device_init(); + 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 72a37cb3ffb..22f4778eb56 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -34,6 +34,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" @@ -118,6 +119,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; @@ -132,9 +138,11 @@ struct wayland struct wl_subcompositor *wl_subcompositor; struct zwp_pointer_constraints_v1 *zwp_pointer_constraints_v1; struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1; + 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_data_device data_device; struct wl_list output_list; /* Protects the output_list and the wayland_output.current states. */ pthread_mutex_t output_mutex; @@ -340,6 +348,12 @@ void wayland_pointer_init(struct wl_pointer *wl_pointer); void wayland_pointer_deinit(void); void wayland_pointer_clear_constraint(void);
+/********************************************************************** + * Wayland data device + */ + +void wayland_data_device_init(void); + /********************************************************************** * OpenGL */ @@ -371,6 +385,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 47c1299dd01..d9f13194e9c 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 | 217 +++++++++++++++++++++ dlls/winewayland.drv/waylanddrv.h | 2 + 3 files changed, 220 insertions(+)
diff --git a/dlls/winewayland.drv/wayland.c b/dlls/winewayland.drv/wayland.c index 7fddb77338a..5801fab346e 100644 --- a/dlls/winewayland.drv/wayland.c +++ b/dlls/winewayland.drv/wayland.c @@ -37,6 +37,7 @@ struct wayland process_wayland = .seat.mutex = PTHREAD_MUTEX_INITIALIZER, .keyboard.mutex = PTHREAD_MUTEX_INITIALIZER, .pointer.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 17ba8cddb03..556ef8e8deb 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -24,18 +24,230 @@
#include "config.h"
+#include <errno.h> +#include <fcntl.h> +#include <poll.h> +#include <stdlib.h> +#include <unistd.h> + #include "waylanddrv.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(clipboard); + +static HWND clipboard_hwnd; + +static int poll_until(int fd, int events, ULONG end_time) +{ + struct pollfd pfd = { .fd = fd, .events = events }; + ULONG now = NtGetTickCount(); + int ret = 0; + + while ((now = NtGetTickCount()) < end_time && + (ret = poll(&pfd, 1, end_time - now)) == -1 && + errno == EINTR) + { + continue; + } + + if (ret <= 0 || !(pfd.revents & events)) + { + TRACE("Failed polling ret=%d errno=%d revents=0x%x\n", + ret, ret == -1 ? errno : 0, pfd.revents); + return ret <= 0 ? ret : -1; + } + + return ret; +} + +static void write_all(int fd, const void *buf, size_t count) +{ + static const ULONG timeout = 500; + size_t nwritten = 0; + int flags; + ULONG end_time; + ssize_t ret; + + if (((flags = fcntl(fd, F_GETFL, 0)) < 0) || + (!(flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)) + { + TRACE("Failed to make data source send fd non-blocking, " + "will not be able to properly enforce write timeout\n"); + } + + end_time = NtGetTickCount() + timeout; + while (nwritten < count) + { + if (poll_until(fd, POLLOUT, end_time) <= 0) break; + ret = write(fd, (const char*)buf + nwritten, count - nwritten); + if (ret == -1 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) + { + 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; + + 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;
+ 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; + + TRACE("\n"); + + if (!process_wayland.zwlr_data_control_manager_v1) return; + + 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) @@ -43,9 +255,14 @@ LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l switch (msg) { case WM_NCCREATE: + clipboard_hwnd = hwnd; + NtUserAddClipboardFormatListener(hwnd); if (process_wayland.seat.wl_seat && process_wayland.zwlr_data_control_manager_v1) wayland_data_device_init(); 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 22f4778eb56..8066e2f29c6 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -122,6 +122,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 | 56 +++++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-)
diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index 556ef8e8deb..ab15f08f114 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -35,6 +35,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 int poll_until(int fd, int events, ULONG end_time) @@ -115,11 +122,43 @@ 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; @@ -134,9 +173,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; @@ -156,8 +195,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); }
@@ -229,8 +270,9 @@ 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) 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 ab15f08f114..5a29ac02e89 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -39,10 +39,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 int poll_until(int fd, int events, ULONG end_time) { @@ -122,11 +128,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}, };
@@ -154,6 +175,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 */ @@ -187,7 +215,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); }
@@ -223,6 +251,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;
pthread_mutex_lock(&data_device->mutex); if (data_device->zwlr_data_control_device_v1) @@ -232,6 +261,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 | 378 ++++++++++++++++++++- dlls/winewayland.drv/waylanddrv.h | 1 + 2 files changed, 365 insertions(+), 14 deletions(-)
diff --git a/dlls/winewayland.drv/wayland_data_device.c b/dlls/winewayland.drv/wayland_data_device.c index 5a29ac02e89..cd7c25204cb 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -35,12 +35,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; @@ -52,7 +63,7 @@ static const WCHAR image_svg_xmlW[] = {'i','m','a','g','e','/','s','v','g','+','
static int poll_until(int fd, int events, ULONG end_time) { - struct pollfd pfd = { .fd = fd, .events = events }; + struct pollfd pfd = { .fd = fd, .events = events & ~POLLHUP }; ULONG now = NtGetTickCount(); int ret = 0;
@@ -110,6 +121,64 @@ static void write_all(int fd, const void *buf, size_t count) } }
+static void *read_all(int fd, size_t *size_out) +{ + static const ULONG receive_timeout = 500; + size_t buffer_size = 4096; + int total = 0; + unsigned char *buffer; + int nread; + ULONG end_time; + + if (!(buffer = malloc(buffer_size))) + { + ERR("failed to allocate read buffer\n"); + goto out; + } + + end_time = NtGetTickCount() + receive_timeout; + do + { + if (poll_until(fd, POLLIN | POLLHUP, end_time) <= 0) goto out; + + 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; @@ -134,30 +203,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; @@ -248,6 +353,199 @@ 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))) + { + if (NtUserOpenClipboard(clipboard_hwnd, 0)) + { + NtUserEmptyClipboard(); + NtUserCloseClipboard(); + } + goto done; + } + + /* 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; @@ -260,6 +558,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) @@ -306,12 +610,13 @@ 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) zwlr_data_control_source_v1_offer(source, format->mime_type); }
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); @@ -327,6 +632,44 @@ 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; + + 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; + + 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) @@ -338,8 +681,15 @@ LRESULT WAYLAND_ClipboardWindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l wayland_data_device_init(); 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 8066e2f29c6..62c74a2cabf 100644 --- a/dlls/winewayland.drv/waylanddrv.h +++ b/dlls/winewayland.drv/waylanddrv.h @@ -123,6 +123,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 cd7c25204cb..e060ad103ae 100644 --- a/dlls/winewayland.drv/wayland_data_device.c +++ b/dlls/winewayland.drv/wayland_data_device.c @@ -61,6 +61,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 int poll_until(int fd, int events, ULONG end_time) { struct pollfd pfd = { .fd = fd, .events = events & ~POLLHUP }; @@ -328,10 +357,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); }
@@ -362,13 +395,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; } }