diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index 32c3899a..ceb3df76 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -581,7 +581,7 @@ GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbutto /* Clipboard */ GLFWAPI void glfwSetClipboardString(GLFWwindow window, const char* string); -GLFWAPI size_t glfwGetClipboardString(GLFWwindow window, char* string, size_t size); +GLFWAPI const char* glfwGetClipboardString(GLFWwindow window); /* Time */ GLFWAPI double glfwGetTime(void); diff --git a/src/clipboard.c b/src/clipboard.c index b66d5656..0bdea5b4 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -59,19 +59,16 @@ GLFWAPI void glfwSetClipboardString(GLFWwindow handle, const char* string) // Return the current clipboard contents //======================================================================== -GLFWAPI size_t glfwGetClipboardString(GLFWwindow handle, char* string, size_t size) +GLFWAPI const char* glfwGetClipboardString(GLFWwindow handle) { _GLFWwindow* window = (_GLFWwindow*) handle; if (!_glfwInitialized) { _glfwSetError(GLFW_NOT_INITIALIZED, NULL); - return 0; + return NULL; } - if (!string || !size) - return 0; - - return _glfwPlatformGetClipboardString(window, string, size); + return _glfwPlatformGetClipboardString(window); } diff --git a/src/cocoa_clipboard.m b/src/cocoa_clipboard.m index e52b839e..07bf8856 100644 --- a/src/cocoa_clipboard.m +++ b/src/cocoa_clipboard.m @@ -56,7 +56,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) // Return the current clipboard contents //======================================================================== -size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) +const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) { const char* source; size_t targetSize; @@ -65,7 +65,7 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t if (![[pasteboard types] containsObject:NSStringPboardType]) { _glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL); - return 0; + return NULL; } NSString* object = [pasteboard stringForType:NSStringPboardType]; @@ -73,15 +73,12 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t { _glfwSetError(GLFW_PLATFORM_ERROR, "Cocoa/NSGL: Failed to retrieve object from pasteboard"); - return 0; + return NULL; } - source = [object UTF8String]; - targetSize = strlen(source) + 1; - if (targetSize > size) - targetSize = size; + free(_glfwLibrary.NS.clipboardString); + _glfwLibrary.NS.clipboardString = strdup([object UTF8String]); - strlcpy(string, source, targetSize); - return 0; + return _glfwLibrary.NS.clipboardString; } diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index ea12273e..77aa3481 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -95,6 +95,8 @@ typedef struct _GLFWlibraryNS CGEventSourceRef eventSource; id delegate; id autoreleasePool; + + char* clipboardString; } _GLFWlibraryNS; diff --git a/src/internal.h b/src/internal.h index 407270ea..4e7bce1a 100644 --- a/src/internal.h +++ b/src/internal.h @@ -290,7 +290,7 @@ void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp); // Clipboard void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string); -size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char *data, size_t size); +const char* _glfwPlatformGetClipboardString(_GLFWwindow* window); // Joystick int _glfwPlatformGetJoystickParam(int joy, int param); diff --git a/src/win32_clipboard.c b/src/win32_clipboard.c index 9b14f158..2b8742ce 100644 --- a/src/win32_clipboard.c +++ b/src/win32_clipboard.c @@ -94,23 +94,21 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) // Return the current clipboard contents //======================================================================== -size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) +const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) { HANDLE stringHandle; - char* utf8String; - size_t utf8Size; if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) { _glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL); - return 0; + return NULL; } if (!OpenClipboard(window->Win32.handle)) { _glfwSetError(GLFW_PLATFORM_ERROR, "Win32/WGL: Failed to open clipboard"); - return 0; + return NULL; } stringHandle = GetClipboardData(CF_UNICODETEXT); @@ -120,30 +118,23 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t _glfwSetError(GLFW_PLATFORM_ERROR, "Win32/WGL: Failed to retrieve clipboard data"); - return 0; + return NULL; } - utf8String = _glfwCreateUTF8FromWideString(GlobalLock(stringHandle)); + free(_glfwLibrary.Win32.clipboardString); + _glfwLibrary.Win32.clipboardString = + _glfwCreateUTF8FromWideString(GlobalLock(stringHandle)); + GlobalUnlock(stringHandle); CloseClipboard(); - if (!utf8String) + if (!_glfwLibrary.Win32.clipboardString) { _glfwSetError(GLFW_PLATFORM_ERROR, "Win32/WGL: Failed to convert wide string to UTF-8"); - return 0; + return NULL; } - utf8Size = strlen(utf8String) + 1; - if (utf8Size > size) - { - memcpy(string, utf8String, size); - string[size - 1] = '\0'; - } - else - memcpy(string, utf8String, utf8Size); - - free(utf8String); - return utf8Size; + return _glfwLibrary.Win32.clipboardString; } diff --git a/src/win32_platform.h b/src/win32_platform.h index b2ba9182..7c369767 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -177,6 +177,7 @@ typedef struct _GLFWlibraryWin32 ATOM classAtom; // Window class atom HHOOK keyboardHook; // Keyboard hook handle DWORD foregroundLockTimeout; + char* clipboardString; // Default monitor struct { diff --git a/src/x11_clipboard.c b/src/x11_clipboard.c index 55259e4f..a833ed1f 100644 --- a/src/x11_clipboard.c +++ b/src/x11_clipboard.c @@ -152,10 +152,9 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string) // Return the current clipboard contents //======================================================================== -size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) +const char* _glfwPlatformGetClipboardString(_GLFWwindow* window) { int i; - size_t sourceSize, targetSize; _glfwLibrary.X11.selection.status = _GLFW_CONVERSION_INACTIVE; @@ -184,18 +183,9 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t { _glfwSetError(GLFW_FORMAT_UNAVAILABLE, "X11/GLX: Failed to convert selection to string"); - return 0; + return NULL; } - sourceSize = strlen(_glfwLibrary.X11.selection.string) + 1; - - targetSize = sourceSize; - if (targetSize > size) - targetSize = size; - - memcpy(string, _glfwLibrary.X11.selection.string, targetSize); - string[targetSize - 1] = '\0'; - - return sourceSize; + return _glfwLibrary.X11.selection.string; } diff --git a/tests/clipboard.c b/tests/clipboard.c index da776e61..ebeb98dd 100644 --- a/tests/clipboard.c +++ b/tests/clipboard.c @@ -59,16 +59,15 @@ static void key_callback(GLFWwindow window, int key, int action) case GLFW_KEY_V: if (control_is_down(window)) { - char buffer[4096]; - size_t size; + const char* string; printf("Paste test.\n"); - size = glfwGetClipboardString(window, buffer, sizeof(buffer)); - if (size >= sizeof(buffer)) - printf("Buffer wasn't big enough to hold clipboard data.\n"); + string = glfwGetClipboardString(window); + if (!string) + printf("Failed to retrieve clipboard string\n"); - printf("[%lu]: %s\n", (unsigned long) size, buffer); + printf("%s\n", string); } break;