From 158fc459f181a99d3d3c993d74e5cade0bab90e7 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 15 Jul 2024 14:00:52 -0400 Subject: [PATCH] clipboard: SDL_GetClipboardText() now follows the SDL_GetStringRule. Reference Issue #10229. --- include/SDL3/SDL_clipboard.h | 10 +++++----- src/dynapi/SDL_dynapi_procs.h | 2 +- src/test/SDL_test_common.c | 3 +-- src/video/SDL_clipboard.c | 14 ++++++++------ test/testautomation_clipboard.c | 10 +++------- test/testcontroller.c | 7 +++---- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/include/SDL3/SDL_clipboard.h b/include/SDL3/SDL_clipboard.h index 073c309d0..71d0e5c5b 100644 --- a/include/SDL3/SDL_clipboard.h +++ b/include/SDL3/SDL_clipboard.h @@ -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. diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 0322906df..748cc50e8 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -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) diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c index 03ab26078..442cb7ef9 100644 --- a/src/test/SDL_test_common.c +++ b/src/test/SDL_test_common.c @@ -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; diff --git a/src/video/SDL_clipboard.c b/src/video/SDL_clipboard.c index 4c177227e..ab1c57fc0 100644 --- a/src/video/SDL_clipboard.c +++ b/src/video/SDL_clipboard.c @@ -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; } diff --git a/test/testautomation_clipboard.c b/test/testautomation_clipboard.c index 1b67434f1..6b80e44b3 100644 --- a/test/testautomation_clipboard.c +++ b/test/testautomation_clipboard.c @@ -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", diff --git a/test/testcontroller.c b/test/testcontroller.c index 7ab21e941..7f791e09f 100644 --- a/test/testcontroller.c +++ b/test/testcontroller.c @@ -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(); }