Skip to content

Commit

Permalink
clipboard: SDL_GetClipboardText() now follows the SDL_GetStringRule.
Browse files Browse the repository at this point in the history
Reference Issue #10229.
  • Loading branch information
icculus committed Jul 16, 2024
1 parent 3bc81a8 commit 158fc45
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
10 changes: 5 additions & 5 deletions include/SDL3/SDL_clipboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/test/SDL_test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions src/video/SDL_clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 3 additions & 7 deletions test/testautomation_clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions test/testcontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 158fc45

Please sign in to comment.