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.
This commit is contained in:
alexei-zebra 2026-10-09 01:48:53 +03:00
parent 6a5e485bca
commit d9794afa67
No known key found for this signature in database
GPG key ID: D19D450AC3D9A80E
3 changed files with 42 additions and 23 deletions

View file

@ -212,6 +212,7 @@ void on_display_error() {
} }
void WaylandInput::pollEvents(bool waitForRefresh) { void WaylandInput::pollEvents(bool waitForRefresh) {
updateClipboard();
beginFrame(); beginFrame();
dispatch(waitForRefresh); dispatch(waitForRefresh);
if (window) { if (window) {

View file

@ -6,6 +6,7 @@
#include "window/Window.hpp" #include "window/Window.hpp"
#include <fcntl.h>
#include <poll.h> #include <poll.h>
#include <unistd.h> #include <unistd.h>
@ -20,39 +21,49 @@ public:
void pollEvents(bool waitForRefresh) override; void pollEvents(bool waitForRefresh) override;
const char* getClipboardText() const override { const char* getClipboardText() const override {
return clipboard.c_str();
}
void requestClipboardText() {
if (state.dataDevice == nullptr || selection == nullptr) { if (state.dataDevice == nullptr || selection == nullptr) {
return clipboard.c_str(); return;
} }
const char* mime = pickMime(); const char* mime = pickMime();
if (mime == nullptr) { if (mime == nullptr) {
return clipboard.c_str(); clipboard.clear();
return;
} }
int fds[2]; int fds[2];
if (pipe(fds) == -1) { 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]); wl_data_offer_receive(selection, mime, fds[1]);
close(fds[1]); close(fds[1]);
wl_display_flush(state.display); if (state.display) {
wl_display_flush(state.display);
clipboard.clear(); }
char buffer[4096]; }
auto deadline = std::chrono::steady_clock::now() +
std::chrono::milliseconds(500); void updateClipboard() {
while (std::chrono::steady_clock::now() < deadline) { if (clipboardFd < 0) {
pollfd descriptor {fds[0], POLLIN, 0}; return;
if (poll(&descriptor, 1, 50) <= 0) { }
wl_display_dispatch_pending(state.display); char buffer[4096];
continue; const ssize_t length = read(clipboardFd, buffer, sizeof(buffer));
} if (length > 0) {
ssize_t length = read(fds[0], buffer, sizeof(buffer)); clipboard.append(buffer, length);
if (length <= 0) { return;
break; }
} if (length == 0) {
clipboard.append(buffer, length); close(clipboardFd);
clipboardFd = -1;
} }
close(fds[0]);
return clipboard.c_str();
} }
void setClipboardText(const char* text) override { void setClipboardText(const char* text) override {
@ -67,6 +78,11 @@ public:
wl_data_source_offer(source, "text/plain"); wl_data_source_offer(source, "text/plain");
wl_data_source_offer(source, "UTF8_STRING"); wl_data_source_offer(source, "UTF8_STRING");
sourceText = text ? text : ""; sourceText = text ? text : "";
clipboard = sourceText;
if (clipboardFd >= 0) {
close(clipboardFd);
clipboardFd = -1;
}
wl_data_device_set_selection(state.dataDevice, source, state.inputSerial); wl_data_device_set_selection(state.dataDevice, source, state.inputSerial);
wl_display_flush(state.display); wl_display_flush(state.display);
} }
@ -177,7 +193,8 @@ public:
wl_data_offer* selection = nullptr; wl_data_offer* selection = nullptr;
std::vector<std::string> selectionMimes; std::vector<std::string> selectionMimes;
std::vector<std::string> pendingMimes; std::vector<std::string> pendingMimes;
mutable std::string clipboard; std::string clipboard;
int clipboardFd = -1;
std::string sourceText; std::string sourceText;
private: private:
static uint32_t shapeId(CursorShape shape) { static uint32_t shapeId(CursorShape shape) {

View file

@ -268,6 +268,7 @@ void data_device_selection(void*, wl_data_device*, wl_data_offer* offer) {
input->selection = offer; input->selection = offer;
input->selectionMimes = std::move(input->pendingMimes); input->selectionMimes = std::move(input->pendingMimes);
input->pendingMimes.clear(); input->pendingMimes.clear();
input->requestClipboardText();
} }
void registry_global( void registry_global(