mirror of
https://github.com/glfw/glfw.git
synced 2024-11-22 21:14:35 +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_VERSION_UNAVAILABLE 0x00070007
|
||||||
#define GLFW_PLATFORM_ERROR 0x00070008
|
#define GLFW_PLATFORM_ERROR 0x00070008
|
||||||
#define GLFW_WINDOW_NOT_ACTIVE 0x00070009
|
#define GLFW_WINDOW_NOT_ACTIVE 0x00070009
|
||||||
#define GLFW_CLIPBOARD_FORMAT_UNAVAILABLE 0x00070010
|
#define GLFW_FORMAT_UNAVAILABLE 0x0007000A
|
||||||
|
|
||||||
/* Gamma ramps */
|
/* Gamma ramps */
|
||||||
#define GLFW_GAMMA_RAMP_SIZE 256
|
#define GLFW_GAMMA_RAMP_SIZE 256
|
||||||
|
|
||||||
/* Clipboard formats */
|
|
||||||
#define GLFW_CLIPBOARD_FORMAT_NONE 0
|
|
||||||
#define GLFW_CLIPBOARD_FORMAT_STRING 1
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* Typedefs
|
* Typedefs
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
@ -580,8 +576,8 @@ GLFWAPI int glfwGetJoystickPos(int joy, float* pos, int numaxes);
|
|||||||
GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons);
|
GLFWAPI int glfwGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons);
|
||||||
|
|
||||||
/* Clipboard */
|
/* Clipboard */
|
||||||
GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format);
|
GLFWAPI void glfwSetClipboardString(const char* data);
|
||||||
GLFWAPI size_t glfwGetClipboardData(void* data, size_t size, int format);
|
GLFWAPI size_t glfwGetClipboardString(char* data, size_t size);
|
||||||
|
|
||||||
/* Time */
|
/* Time */
|
||||||
GLFWAPI double glfwGetTime(void);
|
GLFWAPI double glfwGetTime(void);
|
||||||
|
@ -41,7 +41,7 @@
|
|||||||
// Set the clipboard contents
|
// Set the clipboard contents
|
||||||
//========================================================================
|
//========================================================================
|
||||||
|
|
||||||
GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format)
|
GLFWAPI void glfwSetClipboardString(const char* string)
|
||||||
{
|
{
|
||||||
if (!_glfwInitialized)
|
if (!_glfwInitialized)
|
||||||
{
|
{
|
||||||
@ -49,10 +49,7 @@ GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == GLFW_CLIPBOARD_FORMAT_NONE)
|
_glfwPlatformSetClipboardString(string);
|
||||||
return;
|
|
||||||
|
|
||||||
_glfwPlatformSetClipboardData(data, size, format);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -60,7 +57,7 @@ GLFWAPI void glfwSetClipboardData(void* data, size_t size, int format)
|
|||||||
// Return the current clipboard contents
|
// 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)
|
if (!_glfwInitialized)
|
||||||
{
|
{
|
||||||
@ -68,12 +65,9 @@ GLFWAPI size_t glfwGetClipboardData(void* data, size_t size, int format)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == GLFW_CLIPBOARD_FORMAT_NONE)
|
if (!string || !size)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!data || !size)
|
return _glfwPlatformGetClipboardString(string, size);
|
||||||
return 0;
|
|
||||||
|
|
||||||
return _glfwPlatformGetClipboardData(data, size, format);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -289,8 +289,8 @@ void _glfwPlatformGetGammaRamp(GLFWgammaramp* ramp);
|
|||||||
void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp);
|
void _glfwPlatformSetGammaRamp(const GLFWgammaramp* ramp);
|
||||||
|
|
||||||
// Clipboard
|
// Clipboard
|
||||||
void _glfwPlatformSetClipboardData(void *data, size_t size, int format);
|
void _glfwPlatformSetClipboardString(const char* string);
|
||||||
size_t _glfwPlatformGetClipboardData(void *data, size_t size, int format);
|
size_t _glfwPlatformGetClipboardString(char *data, size_t size);
|
||||||
|
|
||||||
// Joystick
|
// Joystick
|
||||||
int _glfwPlatformGetJoystickParam(int joy, int param);
|
int _glfwPlatformGetJoystickParam(int joy, int param);
|
||||||
|
@ -93,35 +93,25 @@ Atom _glfwSelectionRequest(XSelectionRequestEvent* request)
|
|||||||
// Set the clipboard contents
|
// Set the clipboard contents
|
||||||
//========================================================================
|
//========================================================================
|
||||||
|
|
||||||
void _glfwPlatformSetClipboardData(void* data, size_t size, int format)
|
void _glfwPlatformSetClipboardString(const char* string)
|
||||||
{
|
{
|
||||||
switch (format)
|
size_t size = strlen(string) + 1;
|
||||||
{
|
|
||||||
case GLFW_CLIPBOARD_FORMAT_STRING:
|
|
||||||
{
|
|
||||||
// Allocate memory to keep track of the clipboard
|
|
||||||
char* cb = malloc(size + 1);
|
|
||||||
|
|
||||||
// Copy the clipboard data
|
// Allocate memory to keep track of the clipboard
|
||||||
memcpy(cb, data, size);
|
char* cb = malloc(size);
|
||||||
|
|
||||||
// Set the string length
|
// Copy the clipboard data
|
||||||
_glfwLibrary.X11.selection.stringLength = size;
|
memcpy(cb, string, size);
|
||||||
|
|
||||||
// Check if existing clipboard memory needs to be freed
|
// Set the string length
|
||||||
if (_glfwLibrary.X11.selection.string)
|
_glfwLibrary.X11.selection.stringLength = size;
|
||||||
free(_glfwLibrary.X11.selection.string);
|
|
||||||
|
|
||||||
// Now set the clipboard (awaiting the event SelectionRequest)
|
// Check if existing clipboard memory needs to be freed
|
||||||
_glfwLibrary.X11.selection.string = cb;
|
if (_glfwLibrary.X11.selection.string)
|
||||||
break;
|
free(_glfwLibrary.X11.selection.string);
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
// Now set the clipboard (awaiting the event SelectionRequest)
|
||||||
_glfwSetError(GLFW_CLIPBOARD_FORMAT_UNAVAILABLE,
|
_glfwLibrary.X11.selection.string = cb;
|
||||||
"X11/GLX: Unavailable clipboard format");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the selection owner to our active window
|
// Set the selection owner to our active window
|
||||||
XSetSelectionOwner(_glfwLibrary.X11.display, XA_PRIMARY,
|
XSetSelectionOwner(_glfwLibrary.X11.display, XA_PRIMARY,
|
||||||
@ -137,7 +127,7 @@ void _glfwPlatformSetClipboardData(void* data, size_t size, int format)
|
|||||||
// Return the current clipboard contents
|
// 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;
|
size_t len, rembytes, dummy;
|
||||||
unsigned char* d;
|
unsigned char* d;
|
||||||
@ -176,8 +166,8 @@ size_t _glfwPlatformGetClipboardData(void* data, size_t size, int format)
|
|||||||
// Unsuccessful conversion, bail with no clipboard data
|
// Unsuccessful conversion, bail with no clipboard data
|
||||||
if (_glfwLibrary.X11.selection.converted)
|
if (_glfwLibrary.X11.selection.converted)
|
||||||
{
|
{
|
||||||
_glfwSetError(GLFW_CLIPBOARD_FORMAT_UNAVAILABLE,
|
_glfwSetError(GLFW_FORMAT_UNAVAILABLE,
|
||||||
"X11/GLX: Unavailable clipboard format");
|
"X11/GLX: Failed to convert selection to string");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,8 +212,7 @@ size_t _glfwPlatformGetClipboardData(void* data, size_t size, int format)
|
|||||||
memcpy(data, d, s);
|
memcpy(data, d, s);
|
||||||
|
|
||||||
// Null-terminate strings.
|
// Null-terminate strings.
|
||||||
if (format == GLFW_CLIPBOARD_FORMAT_STRING)
|
((char*) data)[s] = '\0';
|
||||||
((char*) data)[s] = '\0';
|
|
||||||
|
|
||||||
// Free the data allocated using X11.
|
// Free the data allocated using X11.
|
||||||
XFree(d);
|
XFree(d);
|
||||||
|
@ -64,7 +64,7 @@ static void key_callback(GLFWwindow window, int key, int action)
|
|||||||
|
|
||||||
printf("Paste test.\n");
|
printf("Paste test.\n");
|
||||||
|
|
||||||
size = glfwGetClipboardData(buffer, sizeof(buffer), GLFW_CLIPBOARD_FORMAT_STRING);
|
size = glfwGetClipboardString(buffer, sizeof(buffer));
|
||||||
if (size >= sizeof(buffer))
|
if (size >= sizeof(buffer))
|
||||||
printf("Buffer wasn't big enough to hold clipboard data.\n");
|
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:
|
case GLFW_KEY_C:
|
||||||
if (control_is_down(window))
|
if (control_is_down(window))
|
||||||
{
|
{
|
||||||
glfwSetClipboardData("Hello GLFW World!", sizeof("Hello GLFW World!"),
|
const char* string = "Hello GLFW World!";
|
||||||
GLFW_CLIPBOARD_FORMAT_STRING);
|
glfwSetClipboardString(string);
|
||||||
printf("Setting clipboard to: %s\n", "Hello GLFW World!");
|
printf("Setting clipboard to: %s\n", string);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user