diff --git a/src/wl_init.c b/src/wl_init.c index fb874ba6..3ee46825 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -1241,15 +1241,6 @@ int _glfwPlatformInit(void) wl_data_device_manager_get_data_device(_glfw.wl.dataDeviceManager, _glfw.wl.seat); wl_data_device_add_listener(_glfw.wl.dataDevice, &dataDeviceListener, NULL); - - _glfw.wl.clipboardSize = 4096; - _glfw.wl.clipboardString = calloc(_glfw.wl.clipboardSize, 1); - if (!_glfw.wl.clipboardString) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Wayland: Failed to allocate clipboard memory"); - return GLFW_FALSE; - } } return GLFW_TRUE; @@ -1346,7 +1337,6 @@ void _glfwPlatformTerminate(void) close(_glfw.wl.cursorTimerfd); free(_glfw.wl.clipboardString); - free(_glfw.wl.clipboardSendString); } const char* _glfwPlatformGetVersionString(void) diff --git a/src/wl_platform.h b/src/wl_platform.h index 31aa0fc7..4b97813b 100644 --- a/src/wl_platform.h +++ b/src/wl_platform.h @@ -267,7 +267,6 @@ typedef struct _GLFWlibraryWayland int keyboardLastScancode; char* clipboardString; size_t clipboardSize; - char* clipboardSendString; int timerfd; short int keycodes[256]; short int scancodes[GLFW_KEY_LAST + 1]; diff --git a/src/wl_window.c b/src/wl_window.c index 98f2891c..1ecaccbd 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -1725,7 +1725,7 @@ static void dataSourceHandleSend(void* userData, const char* mimeType, int fd) { - char* string = _glfw.wl.clipboardSendString; + char* string = _glfw.wl.clipboardString; size_t len = strlen(string); int ret; @@ -1801,16 +1801,23 @@ void _glfwPlatformSetClipboardString(const char* string) _glfw.wl.selectionSource = NULL; } - char* copy = _glfw_strdup(string); - if (!copy) + const size_t requiredSize = strlen(string) + 1; + if (requiredSize > _glfw.wl.clipboardSize) { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Wayland: Failed to allocate clipboard string"); - return; + free(_glfw.wl.clipboardString); + _glfw.wl.clipboardString = calloc(requiredSize, 1); + if (!_glfw.wl.clipboardString) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to allocate clipboard string"); + return; + } + + _glfw.wl.clipboardSize = requiredSize; } - free(_glfw.wl.clipboardSendString); - _glfw.wl.clipboardSendString = copy; + // The argument may be a substring of the clipboard string + memmove(_glfw.wl.clipboardString, string, requiredSize); _glfw.wl.selectionSource = wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager); @@ -1818,8 +1825,6 @@ void _glfwPlatformSetClipboardString(const char* string) { _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Failed to create clipboard data source"); - free(_glfw.wl.clipboardSendString); - _glfw.wl.clipboardSendString = NULL; return; } wl_data_source_add_listener(_glfw.wl.selectionSource, @@ -1831,22 +1836,6 @@ void _glfwPlatformSetClipboardString(const char* string) _glfw.wl.serial); } -static GLFWbool growClipboardString(void) -{ - char* clipboard = _glfw.wl.clipboardString; - - clipboard = realloc(clipboard, _glfw.wl.clipboardSize * 2); - if (!clipboard) - { - _glfwInputError(GLFW_OUT_OF_MEMORY, - "Wayland: Failed to grow clipboard string"); - return GLFW_FALSE; - } - _glfw.wl.clipboardString = clipboard; - _glfw.wl.clipboardSize = _glfw.wl.clipboardSize * 2; - return GLFW_TRUE; -} - const char* _glfwPlatformGetClipboardString(void) { int fds[2]; @@ -1861,7 +1850,7 @@ const char* _glfwPlatformGetClipboardString(void) } if (_glfw.wl.selectionSource) - return _glfw.wl.clipboardSendString; + return _glfw.wl.clipboardString; ret = pipe2(fds, O_CLOEXEC); if (ret < 0) @@ -1881,13 +1870,20 @@ const char* _glfwPlatformGetClipboardString(void) { // Grow the clipboard if we need to paste something bigger, there is no // shrink operation yet. - if (len + 4096 > _glfw.wl.clipboardSize) + const size_t requiredSize = len + 4096 + 1; + if (requiredSize > _glfw.wl.clipboardSize) { - if (!growClipboardString()) + char* string = realloc(_glfw.wl.clipboardString, requiredSize); + if (!string) { + _glfwInputError(GLFW_OUT_OF_MEMORY, + "Wayland: Failed to grow clipboard string"); close(fds[0]); return NULL; } + + _glfw.wl.clipboardString = string; + _glfw.wl.clipboardSize = requiredSize; } // Then read from the fd to the clipboard, handling all known errors. @@ -1907,11 +1903,6 @@ const char* _glfwPlatformGetClipboardString(void) len += ret; } close(fds[0]); - if (len + 1 > _glfw.wl.clipboardSize) - { - if (!growClipboardString()) - return NULL; - } _glfw.wl.clipboardString[len] = '\0'; return _glfw.wl.clipboardString; }