From 915e43885c50958bdd2dd74e93e59f7dd1c77813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 24 Mar 2022 19:04:15 +0100 Subject: [PATCH] Wayland: Clean up clipboard reading (cherry picked from commit c13213533241d7bbf53da89641a8fe6c71d53c0e) --- src/wl_window.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/wl_window.c b/src/wl_window.c index 1ecaccbd..24434fa5 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -1838,10 +1838,6 @@ void _glfwPlatformSetClipboardString(const char* string) const char* _glfwPlatformGetClipboardString(void) { - int fds[2]; - int ret; - size_t len = 0; - if (!_glfw.wl.selectionOffer) { _glfwInputError(GLFW_FORMAT_UNAVAILABLE, @@ -1852,8 +1848,9 @@ const char* _glfwPlatformGetClipboardString(void) if (_glfw.wl.selectionSource) return _glfw.wl.clipboardString; - ret = pipe2(fds, O_CLOEXEC); - if (ret < 0) + int fds[2]; + + if (pipe2(fds, O_CLOEXEC) == -1) { // TODO: also report errno maybe? _glfwInputError(GLFW_PLATFORM_ERROR, @@ -1866,11 +1863,14 @@ const char* _glfwPlatformGetClipboardString(void) flushDisplay(); close(fds[1]); + size_t length = 0; + for (;;) { // Grow the clipboard if we need to paste something bigger, there is no // shrink operation yet. - const size_t requiredSize = len + 4096 + 1; + const size_t readSize = 4096; + const size_t requiredSize = length + readSize + 1; if (requiredSize > _glfw.wl.clipboardSize) { char* string = realloc(_glfw.wl.clipboardString, requiredSize); @@ -1887,23 +1887,27 @@ const char* _glfwPlatformGetClipboardString(void) } // Then read from the fd to the clipboard, handling all known errors. - ret = read(fds[0], _glfw.wl.clipboardString + len, 4096); - if (ret == 0) + const ssize_t result = read(fds[0], _glfw.wl.clipboardString + length, readSize); + if (result == 0) break; - if (ret == -1 && errno == EINTR) - continue; - if (ret == -1) + else if (result == -1) { + if (errno == EINTR) + continue; + // TODO: also report errno maybe. _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Failed to read from clipboard fd"); close(fds[0]); return NULL; } - len += ret; + + length += result; } + close(fds[0]); - _glfw.wl.clipboardString[len] = '\0'; + + _glfw.wl.clipboardString[length] = '\0'; return _glfw.wl.clipboardString; }