mirror of
https://github.com/glfw/glfw.git
synced 2024-11-25 19:42:00 +00:00
Simplified and made clipboard API more type safe.
This commit is contained in:
parent
490c472328
commit
7044ed6f06
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user