From 7044ed6f06a042eba8e694ba9f14812a35040fce Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Mon, 9 Apr 2012 15:54:36 +0200 Subject: [PATCH] Simplified and made clipboard API more type safe. --- include/GL/glfw3.h | 10 +++------- src/clipboard.c | 16 +++++----------- src/internal.h | 4 ++-- src/x11_clipboard.c | 45 +++++++++++++++++---------------------------- tests/clipboard.c | 8 ++++---- 5 files changed, 31 insertions(+), 52 deletions(-) diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index e2f25901..d7be4fd2 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -461,15 +461,11 @@ extern "C" { #define GLFW_VERSION_UNAVAILABLE 0x00070007 #define GLFW_PLATFORM_ERROR 0x00070008 #define GLFW_WINDOW_NOT_ACTIVE 0x00070009 -#define GLFW_CLIPBOARD_FORMAT_UNAVAILABLE 0x00070010 +#define GLFW_FORMAT_UNAVAILABLE 0x0007000A /* Gamma ramps */ #define GLFW_GAMMA_RAMP_SIZE 256 -/* Clipboard formats */ -#define GLFW_CLIPBOARD_FORMAT_NONE 0 -#define GLFW_CLIPBOARD_FORMAT_STRING 1 - /************************************************************************* * Typedefs *************************************************************************/ @@ -580,8 +576,8 @@ GLFWAPI int glfwGetJoystickPos(int joy, float* pos, int numaxes); GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons); /* Clipboard */ -GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format); -GLFWAPI size_t glfwGetClipboardData(void* data, size_t size, int format); +GLFWAPI void glfwSetClipboardString(const char* data); +GLFWAPI size_t glfwGetClipboardString(char* data, size_t size); /* Time */ GLFWAPI double glfwGetTime(void); diff --git a/src/clipboard.c b/src/clipboard.c index 0f1889c3..a816ebb0 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -41,7 +41,7 @@ // Set the clipboard contents //======================================================================== -GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format) +GLFWAPI void glfwSetClipboardString(const char* string) { if (!_glfwInitialized) { @@ -49,10 +49,7 @@ GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format) return; } - if (format == GLFW_CLIPBOARD_FORMAT_NONE) - return; - - _glfwPlatformSetClipboardData(data, size, format); + _glfwPlatformSetClipboardString(string); } @@ -60,7 +57,7 @@ GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format) // Return the current clipboard contents //======================================================================== -GLFWAPI size_t glfwGetClipboardData(void* data, size_t size, int format) +GLFWAPI size_t glfwGetClipboardString(char* string, size_t size) { if (!_glfwInitialized) { @@ -68,12 +65,9 @@ GLFWAPI size_t glfwGetClipboardData(void* data, size_t size, int format) return 0; } - if (format == GLFW_CLIPBOARD_FORMAT_NONE) + if (!string || !size) return 0; - if (!data || !size) - return 0; - - return _glfwPlatformGetClipboardData(data, size, format); + return _glfwPlatformGetClipboardString(string, size); } diff --git a/src/internal.h b/src/internal.h index c16fc03f..2e0b9c38 100644 --- a/src/internal.h +++ b/src/internal.h @@ -289,8 +289,8 @@ void _glfwPlatformGetGammaRamp(GLFWgammaramp* ramp); void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp); // Clipboard -void _glfwPlatformSetClipboardData(void *data, size_t size, int format); -size_t _glfwPlatformGetClipboardData(void *data, size_t size, int format); +void _glfwPlatformSetClipboardString(const char* string); +size_t _glfwPlatformGetClipboardString(char *data, size_t size); // Joystick int _glfwPlatformGetJoystickParam(int joy, int param); diff --git a/src/x11_clipboard.c b/src/x11_clipboard.c index e260e7cb..b733b486 100644 --- a/src/x11_clipboard.c +++ b/src/x11_clipboard.c @@ -93,35 +93,25 @@ Atom _glfwSelectionRequest(XSelectionRequestEvent* request) // Set the clipboard contents //======================================================================== -void _glfwPlatformSetClipboardData(void* data, size_t size, int format) +void _glfwPlatformSetClipboardString(const char* string) { - switch (format) - { - case GLFW_CLIPBOARD_FORMAT_STRING: - { - // Allocate memory to keep track of the clipboard - char* cb = malloc(size + 1); + size_t size = strlen(string) + 1; - // Copy the clipboard data - memcpy(cb, data, size); + // Allocate memory to keep track of the clipboard + char* cb = malloc(size); - // Set the string length - _glfwLibrary.X11.selection.stringLength = size; + // Copy the clipboard data + memcpy(cb, string, size); - // Check if existing clipboard memory needs to be freed - if (_glfwLibrary.X11.selection.string) - free(_glfwLibrary.X11.selection.string); + // Set the string length + _glfwLibrary.X11.selection.stringLength = size; - // Now set the clipboard (awaiting the event SelectionRequest) - _glfwLibrary.X11.selection.string = cb; - break; - } + // Check if existing clipboard memory needs to be freed + if (_glfwLibrary.X11.selection.string) + free(_glfwLibrary.X11.selection.string); - default: - _glfwSetError(GLFW_CLIPBOARD_FORMAT_UNAVAILABLE, - "X11/GLX: Unavailable clipboard format"); - return; - } + // Now set the clipboard (awaiting the event SelectionRequest) + _glfwLibrary.X11.selection.string = cb; // Set the selection owner to our active window XSetSelectionOwner(_glfwLibrary.X11.display, XA_PRIMARY, @@ -137,7 +127,7 @@ void _glfwPlatformSetClipboardData(void* data, size_t size, int format) // Return the current clipboard contents //======================================================================== -size_t _glfwPlatformGetClipboardData(void* data, size_t size, int format) +size_t _glfwPlatformGetClipboardString(char* data, size_t size) { size_t len, rembytes, dummy; unsigned char* d; @@ -176,8 +166,8 @@ size_t _glfwPlatformGetClipboardData(void* data, size_t size, int format) // Unsuccessful conversion, bail with no clipboard data if (_glfwLibrary.X11.selection.converted) { - _glfwSetError(GLFW_CLIPBOARD_FORMAT_UNAVAILABLE, - "X11/GLX: Unavailable clipboard format"); + _glfwSetError(GLFW_FORMAT_UNAVAILABLE, + "X11/GLX: Failed to convert selection to string"); return 0; } @@ -222,8 +212,7 @@ size_t _glfwPlatformGetClipboardData(void* data, size_t size, int format) memcpy(data, d, s); // Null-terminate strings. - if (format == GLFW_CLIPBOARD_FORMAT_STRING) - ((char*) data)[s] = '\0'; + ((char*) data)[s] = '\0'; // Free the data allocated using X11. XFree(d); diff --git a/tests/clipboard.c b/tests/clipboard.c index 1960d6ec..ac83448b 100644 --- a/tests/clipboard.c +++ b/tests/clipboard.c @@ -64,7 +64,7 @@ static void key_callback(GLFWwindow window, int key, int action) printf("Paste test.\n"); - size = glfwGetClipboardData(buffer, sizeof(buffer), GLFW_CLIPBOARD_FORMAT_STRING); + size = glfwGetClipboardString(buffer, sizeof(buffer)); if (size >= sizeof(buffer)) printf("Buffer wasn't big enough to hold clipboard data.\n"); @@ -75,9 +75,9 @@ static void key_callback(GLFWwindow window, int key, int action) case GLFW_KEY_C: if (control_is_down(window)) { - glfwSetClipboardData("Hello GLFW World!", sizeof("Hello GLFW World!"), - GLFW_CLIPBOARD_FORMAT_STRING); - printf("Setting clipboard to: %s\n", "Hello GLFW World!"); + const char* string = "Hello GLFW World!"; + glfwSetClipboardString(string); + printf("Setting clipboard to: %s\n", string); } break; }