1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-26 20:28:49 -05:00

Wayland: Clean up clipboard reading

This commit is contained in:
Camilla Löwy 2022-03-24 19:04:15 +01:00
parent 4651165272
commit c132135332

View File

@ -1704,10 +1704,6 @@ void _glfwSetClipboardStringWayland(const char* string)
const char* _glfwGetClipboardStringWayland(void) const char* _glfwGetClipboardStringWayland(void)
{ {
int fds[2];
int ret;
size_t len = 0;
if (!_glfw.wl.selectionOffer) if (!_glfw.wl.selectionOffer)
{ {
_glfwInputError(GLFW_FORMAT_UNAVAILABLE, _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
@ -1718,8 +1714,9 @@ const char* _glfwGetClipboardStringWayland(void)
if (_glfw.wl.selectionSource) if (_glfw.wl.selectionSource)
return _glfw.wl.clipboardString; return _glfw.wl.clipboardString;
ret = pipe2(fds, O_CLOEXEC); int fds[2];
if (ret < 0)
if (pipe2(fds, O_CLOEXEC) == -1)
{ {
// TODO: also report errno maybe? // TODO: also report errno maybe?
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
@ -1732,11 +1729,14 @@ const char* _glfwGetClipboardStringWayland(void)
flushDisplay(); flushDisplay();
close(fds[1]); close(fds[1]);
size_t length = 0;
for (;;) for (;;)
{ {
// Grow the clipboard if we need to paste something bigger, there is no // Grow the clipboard if we need to paste something bigger, there is no
// shrink operation yet. // 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) if (requiredSize > _glfw.wl.clipboardSize)
{ {
char* string = _glfw_realloc(_glfw.wl.clipboardString, requiredSize); char* string = _glfw_realloc(_glfw.wl.clipboardString, requiredSize);
@ -1753,23 +1753,27 @@ const char* _glfwGetClipboardStringWayland(void)
} }
// Then read from the fd to the clipboard, handling all known errors. // Then read from the fd to the clipboard, handling all known errors.
ret = read(fds[0], _glfw.wl.clipboardString + len, 4096); const ssize_t result = read(fds[0], _glfw.wl.clipboardString + length, readSize);
if (ret == 0) if (result == 0)
break; break;
if (ret == -1 && errno == EINTR) else if (result == -1)
continue;
if (ret == -1)
{ {
if (errno == EINTR)
continue;
// TODO: also report errno maybe. // TODO: also report errno maybe.
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"Wayland: Failed to read from clipboard fd"); "Wayland: Failed to read from clipboard fd");
close(fds[0]); close(fds[0]);
return NULL; return NULL;
} }
len += ret;
length += result;
} }
close(fds[0]); close(fds[0]);
_glfw.wl.clipboardString[len] = '\0';
_glfw.wl.clipboardString[length] = '\0';
return _glfw.wl.clipboardString; return _glfw.wl.clipboardString;
} }