clipboard: SDL_GetClipboardText() now follows the SDL_GetStringRule.

Reference Issue #10229.
This commit is contained in:
Ryan C. Gordon 2024-07-15 14:00:52 -04:00
parent 3bc81a81f5
commit 158fc459f1
6 changed files with 21 additions and 25 deletions

View File

@ -57,22 +57,22 @@ extern "C" {
extern SDL_DECLSPEC int SDLCALL SDL_SetClipboardText(const char *text);
/**
* Get UTF-8 text from the clipboard, which must be freed with SDL_free().
* Get UTF-8 text from the clipboard.
*
* This functions returns empty string if there was not enough memory left for
* a copy of the clipboard's content.
*
* The returned string follows the SDL_GetStringRule.
*
* \returns the clipboard text on success or an empty string on failure; call
* SDL_GetError() for more information. Caller must call SDL_free()
* on the returned pointer when done with it (even if there was an
* error).
* SDL_GetError() for more information.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_HasClipboardText
* \sa SDL_SetClipboardText
*/
extern SDL_DECLSPEC char * SDLCALL SDL_GetClipboardText(void);
extern SDL_DECLSPEC const char * SDLCALL SDL_GetClipboardText(void);
/**
* Query whether the clipboard exists and contains a non-empty text string.

View File

@ -233,7 +233,7 @@ SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),ret
SDL_DYNAPI_PROC(SDL_CameraSpec*,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_CameraID*,SDL_GetCameras,(int *a),(a),return)
SDL_DYNAPI_PROC(void*,SDL_GetClipboardData,(const char *a, size_t *b),(a,b),return)
SDL_DYNAPI_PROC(char*,SDL_GetClipboardText,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_GetClipboardText,(void),(),return)
SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetClosestFullscreenDisplayMode,(SDL_DisplayID a, int b, int c, float d, SDL_bool e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(const char*,SDL_GetCurrentAudioDriver,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_GetCurrentCameraDriver,(void),(),return)

View File

@ -2278,13 +2278,12 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
SDLTest_PasteScreenShot();
} else {
/* Ctrl-V paste awesome text! */
char *text = SDL_GetClipboardText();
const char *text = SDL_GetClipboardText();
if (*text) {
SDL_Log("Clipboard: %s\n", text);
} else {
SDL_Log("Clipboard is empty\n");
}
SDL_free(text);
}
}
break;

View File

@ -282,29 +282,31 @@ int SDL_SetClipboardText(const char *text)
return SDL_ClearClipboardData();
}
char *SDL_GetClipboardText(void)
const char *SDL_GetClipboardText(void)
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
size_t i, num_mime_types;
const char **text_mime_types;
size_t length;
char *text = NULL;
const char *text = NULL;
if (!_this) {
SDL_SetError("Video subsystem must be initialized to get clipboard text");
return SDL_strdup("");
return "";
}
text_mime_types = SDL_GetTextMimeTypes(_this, &num_mime_types);
for (i = 0; i < num_mime_types; ++i) {
text = (char *)SDL_GetClipboardData(text_mime_types[i], &length);
if (text) {
void *clipdata = SDL_GetClipboardData(text_mime_types[i], &length);
if (clipdata) {
text = (const char *) clipdata;
SDL_FreeLater(clipdata); // returned string follows the SDL_GetStringRule.
break;
}
}
if (!text) {
text = SDL_strdup("");
text = "";
}
return text;
}

View File

@ -163,12 +163,11 @@ static int clipboard_testClipboardDataFunctions(void *arg)
clipboard_cleanup_count - last_clipboard_cleanup_count);
expected_text = "TEST";
text = SDL_GetClipboardText();
text = (char *) SDL_GetClipboardText();
SDLTest_AssertCheck(
text && SDL_strcmp(text, expected_text) == 0,
"Verify clipboard text, expected \"%s\", got \"%s\"",
expected_text, text);
SDL_free(text);
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
SDLTest_AssertCheck(
@ -265,12 +264,11 @@ static int clipboard_testClipboardDataFunctions(void *arg)
clipboard_cleanup_count - last_clipboard_cleanup_count);
expected_text = "TEST";
text = SDL_GetClipboardText();
text = (char *) SDL_GetClipboardText();
SDLTest_AssertCheck(
text && SDL_strcmp(text, expected_text) == 0,
"Verify clipboard text, expected \"%s\", got \"%s\"",
expected_text, text);
SDL_free(text);
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_TEXT]);
SDLTest_AssertCheck(
@ -386,7 +384,7 @@ static int clipboard_testClipboardTextFunctions(void *arg)
char *text = SDL_strdup(textRef);
SDL_bool boolResult;
int intResult;
char *charResult;
const char *charResult;
int last_clipboard_update_count;
SDL_AddEventWatch(ClipboardEventWatch, NULL);
@ -403,7 +401,6 @@ static int clipboard_testClipboardTextFunctions(void *arg)
charResult && SDL_strcmp(charResult, "") == 0,
"Verify SDL_GetClipboardText returned \"\", got %s",
charResult);
SDL_free(charResult);
boolResult = SDL_HasClipboardText();
SDLTest_AssertCheck(
boolResult == SDL_FALSE,
@ -436,7 +433,6 @@ static int clipboard_testClipboardTextFunctions(void *arg)
charResult && SDL_strcmp(textRef, charResult) == 0,
"Verify SDL_GetClipboardText returned correct string, expected '%s', got '%s'",
textRef, charResult);
SDL_free(charResult);
SDLTest_AssertCheck(
clipboard_update_count == last_clipboard_update_count + 1,
"Verify clipboard update count incremented by 1, got %d",

View File

@ -680,14 +680,13 @@ static void CopyMapping(void)
static void PasteMapping(void)
{
if (controller) {
char *mapping = SDL_GetClipboardText();
const char *mapping = SDL_GetClipboardText();
if (MappingHasBindings(mapping)) {
StopBinding();
SetAndFreeGamepadMapping(mapping);
SDL_SetGamepadMapping(controller->id, mapping);
RefreshControllerName();
} else {
/* Not a valid mapping, ignore it */
SDL_free(mapping);
}
}
}
@ -743,7 +742,7 @@ static void CopyControllerName(void)
static void PasteControllerName(void)
{
SDL_free(controller_name);
controller_name = SDL_GetClipboardText();
controller_name = SDL_strdup(SDL_GetClipboardText());
CommitControllerName();
}