1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-10-08 07:07:25 -04:00

Simplified clipboard API.

This commit is contained in:
Camilla Berglund 2012-04-12 00:51:58 +02:00
parent 721e0a7fd0
commit f868712f02
9 changed files with 33 additions and 56 deletions

View File

@ -581,7 +581,7 @@ GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbutto
/* Clipboard */ /* Clipboard */
GLFWAPI void glfwSetClipboardString(GLFWwindow window, const char* string); 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 */ /* Time */
GLFWAPI double glfwGetTime(void); GLFWAPI double glfwGetTime(void);

View File

@ -59,19 +59,16 @@ GLFWAPI void glfwSetClipboardString(GLFWwindow handle, const char* string)
// Return the current clipboard contents // 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; _GLFWwindow* window = (_GLFWwindow*) handle;
if (!_glfwInitialized) if (!_glfwInitialized)
{ {
_glfwSetError(GLFW_NOT_INITIALIZED, NULL); _glfwSetError(GLFW_NOT_INITIALIZED, NULL);
return 0; return NULL;
} }
if (!string || !size) return _glfwPlatformGetClipboardString(window);
return 0;
return _glfwPlatformGetClipboardString(window, string, size);
} }

View File

@ -56,7 +56,7 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
// Return the current clipboard contents // Return the current clipboard contents
//======================================================================== //========================================================================
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{ {
const char* source; const char* source;
size_t targetSize; size_t targetSize;
@ -65,7 +65,7 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
if (![[pasteboard types] containsObject:NSStringPboardType]) if (![[pasteboard types] containsObject:NSStringPboardType])
{ {
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL); _glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL);
return 0; return NULL;
} }
NSString* object = [pasteboard stringForType:NSStringPboardType]; NSString* object = [pasteboard stringForType:NSStringPboardType];
@ -73,15 +73,12 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
{ {
_glfwSetError(GLFW_PLATFORM_ERROR, _glfwSetError(GLFW_PLATFORM_ERROR,
"Cocoa/NSGL: Failed to retrieve object from pasteboard"); "Cocoa/NSGL: Failed to retrieve object from pasteboard");
return 0; return NULL;
} }
source = [object UTF8String]; free(_glfwLibrary.NS.clipboardString);
targetSize = strlen(source) + 1; _glfwLibrary.NS.clipboardString = strdup([object UTF8String]);
if (targetSize > size)
targetSize = size;
strlcpy(string, source, targetSize); return _glfwLibrary.NS.clipboardString;
return 0;
} }

View File

@ -95,6 +95,8 @@ typedef struct _GLFWlibraryNS
CGEventSourceRef eventSource; CGEventSourceRef eventSource;
id delegate; id delegate;
id autoreleasePool; id autoreleasePool;
char* clipboardString;
} _GLFWlibraryNS; } _GLFWlibraryNS;

View File

@ -290,7 +290,7 @@ void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp);
// Clipboard // Clipboard
void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string); void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string);
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char *data, size_t size); const char* _glfwPlatformGetClipboardString(_GLFWwindow* window);
// Joystick // Joystick
int _glfwPlatformGetJoystickParam(int joy, int param); int _glfwPlatformGetJoystickParam(int joy, int param);

View File

@ -94,23 +94,21 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
// Return the current clipboard contents // Return the current clipboard contents
//======================================================================== //========================================================================
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{ {
HANDLE stringHandle; HANDLE stringHandle;
char* utf8String;
size_t utf8Size;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
{ {
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL); _glfwSetError(GLFW_FORMAT_UNAVAILABLE, NULL);
return 0; return NULL;
} }
if (!OpenClipboard(window->Win32.handle)) if (!OpenClipboard(window->Win32.handle))
{ {
_glfwSetError(GLFW_PLATFORM_ERROR, _glfwSetError(GLFW_PLATFORM_ERROR,
"Win32/WGL: Failed to open clipboard"); "Win32/WGL: Failed to open clipboard");
return 0; return NULL;
} }
stringHandle = GetClipboardData(CF_UNICODETEXT); stringHandle = GetClipboardData(CF_UNICODETEXT);
@ -120,30 +118,23 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
_glfwSetError(GLFW_PLATFORM_ERROR, _glfwSetError(GLFW_PLATFORM_ERROR,
"Win32/WGL: Failed to retrieve clipboard data"); "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); GlobalUnlock(stringHandle);
CloseClipboard(); CloseClipboard();
if (!utf8String) if (!_glfwLibrary.Win32.clipboardString)
{ {
_glfwSetError(GLFW_PLATFORM_ERROR, _glfwSetError(GLFW_PLATFORM_ERROR,
"Win32/WGL: Failed to convert wide string to UTF-8"); "Win32/WGL: Failed to convert wide string to UTF-8");
return 0; return NULL;
} }
utf8Size = strlen(utf8String) + 1; return _glfwLibrary.Win32.clipboardString;
if (utf8Size > size)
{
memcpy(string, utf8String, size);
string[size - 1] = '\0';
}
else
memcpy(string, utf8String, utf8Size);
free(utf8String);
return utf8Size;
} }

View File

@ -177,6 +177,7 @@ typedef struct _GLFWlibraryWin32
ATOM classAtom; // Window class atom ATOM classAtom; // Window class atom
HHOOK keyboardHook; // Keyboard hook handle HHOOK keyboardHook; // Keyboard hook handle
DWORD foregroundLockTimeout; DWORD foregroundLockTimeout;
char* clipboardString;
// Default monitor // Default monitor
struct { struct {

View File

@ -152,10 +152,9 @@ void _glfwPlatformSetClipboardString(_GLFWwindow* window, const char* string)
// Return the current clipboard contents // Return the current clipboard contents
//======================================================================== //========================================================================
size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t size) const char* _glfwPlatformGetClipboardString(_GLFWwindow* window)
{ {
int i; int i;
size_t sourceSize, targetSize;
_glfwLibrary.X11.selection.status = _GLFW_CONVERSION_INACTIVE; _glfwLibrary.X11.selection.status = _GLFW_CONVERSION_INACTIVE;
@ -184,18 +183,9 @@ size_t _glfwPlatformGetClipboardString(_GLFWwindow* window, char* string, size_t
{ {
_glfwSetError(GLFW_FORMAT_UNAVAILABLE, _glfwSetError(GLFW_FORMAT_UNAVAILABLE,
"X11/GLX: Failed to convert selection to string"); "X11/GLX: Failed to convert selection to string");
return 0; return NULL;
} }
sourceSize = strlen(_glfwLibrary.X11.selection.string) + 1; return _glfwLibrary.X11.selection.string;
targetSize = sourceSize;
if (targetSize > size)
targetSize = size;
memcpy(string, _glfwLibrary.X11.selection.string, targetSize);
string[targetSize - 1] = '\0';
return sourceSize;
} }

View File

@ -59,16 +59,15 @@ static void key_callback(GLFWwindow window, int key, int action)
case GLFW_KEY_V: case GLFW_KEY_V:
if (control_is_down(window)) if (control_is_down(window))
{ {
char buffer[4096]; const char* string;
size_t size;
printf("Paste test.\n"); printf("Paste test.\n");
size = glfwGetClipboardString(window, buffer, sizeof(buffer)); string = glfwGetClipboardString(window);
if (size >= sizeof(buffer)) if (!string)
printf("Buffer wasn't big enough to hold clipboard data.\n"); printf("Failed to retrieve clipboard string\n");
printf("[%lu]: %s\n", (unsigned long) size, buffer); printf("%s\n", string);
} }
break; break;