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); 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 * This functions returns empty string if there was not enough memory left for
* a copy of the clipboard's content. * 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 * \returns the clipboard text on success or an empty string on failure; call
* SDL_GetError() for more information. Caller must call SDL_free() * SDL_GetError() for more information.
* on the returned pointer when done with it (even if there was an
* error).
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_HasClipboardText * \sa SDL_HasClipboardText
* \sa SDL_SetClipboardText * \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. * 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_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(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(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 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_GetCurrentAudioDriver,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_GetCurrentCameraDriver,(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(); SDLTest_PasteScreenShot();
} else { } else {
/* Ctrl-V paste awesome text! */ /* Ctrl-V paste awesome text! */
char *text = SDL_GetClipboardText(); const char *text = SDL_GetClipboardText();
if (*text) { if (*text) {
SDL_Log("Clipboard: %s\n", text); SDL_Log("Clipboard: %s\n", text);
} else { } else {
SDL_Log("Clipboard is empty\n"); SDL_Log("Clipboard is empty\n");
} }
SDL_free(text);
} }
} }
break; break;

View file

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

View file

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

View file

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