From d9794afa674c1672fe05641be094867c6f74cf76 Mon Sep 17 00:00:00 2001 From: alexei-zebra Date: Fri, 9 Oct 2026 01:48:53 +0300 Subject: [PATCH] read the clipboard asynchronously getClipboardText() used to open a pipe, ask the selection owner for the text and poll it with a 500 ms deadline, so every paste blocked the UI thread (TextBox calls it directly) and a slow owner meant a visible hang. The selection is now fetched in the background: when the selection changes, the backend requests the text into a non-blocking pipe and getClipboardText() only returns the buffer, which the event loop fills in as the data arrives. Pasting no longer blocks, and a source that never answers simply leaves the previous buffer in place. Also clears the advertised MIME list when a new selection offer arrives, so a request is never made with MIME types of a previous owner. --- src/window/wayland/WaylandInit.cpp | 1 + src/window/wayland/WaylandInput.hpp | 63 ++++++++++++++++--------- src/window/wayland/WaylandListeners.cpp | 1 + 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/window/wayland/WaylandInit.cpp b/src/window/wayland/WaylandInit.cpp index f41996fc9..fdd52b942 100644 --- a/src/window/wayland/WaylandInit.cpp +++ b/src/window/wayland/WaylandInit.cpp @@ -212,6 +212,7 @@ void on_display_error() { } void WaylandInput::pollEvents(bool waitForRefresh) { + updateClipboard(); beginFrame(); dispatch(waitForRefresh); if (window) { diff --git a/src/window/wayland/WaylandInput.hpp b/src/window/wayland/WaylandInput.hpp index 048bfac8e..b04bde572 100644 --- a/src/window/wayland/WaylandInput.hpp +++ b/src/window/wayland/WaylandInput.hpp @@ -6,6 +6,7 @@ #include "window/Window.hpp" +#include #include #include @@ -20,39 +21,49 @@ public: void pollEvents(bool waitForRefresh) override; const char* getClipboardText() const override { + return clipboard.c_str(); + } + + void requestClipboardText() { if (state.dataDevice == nullptr || selection == nullptr) { - return clipboard.c_str(); + return; } const char* mime = pickMime(); if (mime == nullptr) { - return clipboard.c_str(); + clipboard.clear(); + return; } int fds[2]; if (pipe(fds) == -1) { - return clipboard.c_str(); + return; } + if (clipboardFd >= 0) { + close(clipboardFd); + } + clipboard.clear(); + clipboardFd = fds[0]; + fcntl(clipboardFd, F_SETFL, O_NONBLOCK); wl_data_offer_receive(selection, mime, fds[1]); close(fds[1]); - wl_display_flush(state.display); - - clipboard.clear(); - char buffer[4096]; - auto deadline = std::chrono::steady_clock::now() + - std::chrono::milliseconds(500); - while (std::chrono::steady_clock::now() < deadline) { - pollfd descriptor {fds[0], POLLIN, 0}; - if (poll(&descriptor, 1, 50) <= 0) { - wl_display_dispatch_pending(state.display); - continue; - } - ssize_t length = read(fds[0], buffer, sizeof(buffer)); - if (length <= 0) { - break; - } - clipboard.append(buffer, length); + if (state.display) { + wl_display_flush(state.display); + } + } + + void updateClipboard() { + if (clipboardFd < 0) { + return; + } + char buffer[4096]; + const ssize_t length = read(clipboardFd, buffer, sizeof(buffer)); + if (length > 0) { + clipboard.append(buffer, length); + return; + } + if (length == 0) { + close(clipboardFd); + clipboardFd = -1; } - close(fds[0]); - return clipboard.c_str(); } void setClipboardText(const char* text) override { @@ -67,6 +78,11 @@ public: wl_data_source_offer(source, "text/plain"); wl_data_source_offer(source, "UTF8_STRING"); sourceText = text ? text : ""; + clipboard = sourceText; + if (clipboardFd >= 0) { + close(clipboardFd); + clipboardFd = -1; + } wl_data_device_set_selection(state.dataDevice, source, state.inputSerial); wl_display_flush(state.display); } @@ -177,7 +193,8 @@ public: wl_data_offer* selection = nullptr; std::vector selectionMimes; std::vector pendingMimes; - mutable std::string clipboard; + std::string clipboard; + int clipboardFd = -1; std::string sourceText; private: static uint32_t shapeId(CursorShape shape) { diff --git a/src/window/wayland/WaylandListeners.cpp b/src/window/wayland/WaylandListeners.cpp index 395d30b7f..8c9742920 100644 --- a/src/window/wayland/WaylandListeners.cpp +++ b/src/window/wayland/WaylandListeners.cpp @@ -268,6 +268,7 @@ void data_device_selection(void*, wl_data_device*, wl_data_offer* offer) { input->selection = offer; input->selectionMimes = std::move(input->pendingMimes); input->pendingMimes.clear(); + input->requestClipboardText(); } void registry_global(