diff --git a/include/SDL3/SDL_clipboard.h b/include/SDL3/SDL_clipboard.h index 71d0e5c5b9..1014481d7f 100644 --- a/include/SDL3/SDL_clipboard.h +++ b/include/SDL3/SDL_clipboard.h @@ -101,23 +101,22 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasClipboardText(void); extern SDL_DECLSPEC int SDLCALL SDL_SetPrimarySelectionText(const char *text); /** - * Get UTF-8 text from the primary selection, which must be freed with - * SDL_free(). + * Get UTF-8 text from the primary selection. * * This functions returns empty string if there was not enough memory left for * a copy of the primary selection's content. * + * The returned string follows the SDL_GetStringRule. + * * \returns the primary selection 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). + * failure; call SDL_GetError() for more information. * * \since This function is available since SDL 3.0.0. * * \sa SDL_HasPrimarySelectionText * \sa SDL_SetPrimarySelectionText */ -extern SDL_DECLSPEC char * SDLCALL SDL_GetPrimarySelectionText(void); +extern SDL_DECLSPEC const char * SDLCALL SDL_GetPrimarySelectionText(void); /** * Query whether the primary selection exists and contains a non-empty text diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 748cc50e89..135289933b 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -416,7 +416,7 @@ SDL_DYNAPI_PROC(SDL_PowerState,SDL_GetPowerInfo,(int *a, int *b),(a,b),return) SDL_DYNAPI_PROC(char*,SDL_GetPrefPath,(const char *a, const char *b),(a,b),return) SDL_DYNAPI_PROC(SDL_Locale*,SDL_GetPreferredLocales,(void),(),return) SDL_DYNAPI_PROC(SDL_DisplayID,SDL_GetPrimaryDisplay,(void),(),return) -SDL_DYNAPI_PROC(char*,SDL_GetPrimarySelectionText,(void),(),return) +SDL_DYNAPI_PROC(const char*,SDL_GetPrimarySelectionText,(void),(),return) SDL_DYNAPI_PROC(SDL_PropertyType,SDL_GetPropertyType,(SDL_PropertiesID a, const char *b),(a,b),return) SDL_DYNAPI_PROC(void,SDL_GetRGB,(Uint32 a, const SDL_PixelFormatDetails *b, const SDL_Palette *c, Uint8 *d, Uint8 *e, Uint8 *f),(a,b,c,d,e,f),) SDL_DYNAPI_PROC(void,SDL_GetRGBA,(Uint32 a, const SDL_PixelFormatDetails *b, const SDL_Palette *c, Uint8 *d, Uint8 *e, Uint8 *f, Uint8 *g),(a,b,c,d,e,f,g),) diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c index 442cb7ef93..dca7e57c2b 100644 --- a/src/test/SDL_test_common.c +++ b/src/test/SDL_test_common.c @@ -2264,13 +2264,12 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event case SDLK_V: if (withAlt) { /* Alt-V paste awesome text from the primary selection! */ - char *text = SDL_GetPrimarySelectionText(); + const char *text = SDL_GetPrimarySelectionText(); if (*text) { SDL_Log("Primary selection: %s\n", text); } else { SDL_Log("Primary selection is empty\n"); } - SDL_free(text); } else if (withControl) { if (withShift) { diff --git a/src/video/SDL_clipboard.c b/src/video/SDL_clipboard.c index ab1c57fc08..77ddc5941f 100644 --- a/src/video/SDL_clipboard.c +++ b/src/video/SDL_clipboard.c @@ -349,7 +349,7 @@ int SDL_SetPrimarySelectionText(const char *text) return -1; } } else { - SDL_free(_this->primary_selection_text); + SDL_FreeLater(_this->primary_selection_text); // this pointer might be given to the app by SDL_GetPrimarySelectionText. _this->primary_selection_text = SDL_strdup(text); } @@ -357,7 +357,7 @@ int SDL_SetPrimarySelectionText(const char *text) return 0; } -char *SDL_GetPrimarySelectionText(void) +const char *SDL_GetPrimarySelectionText(void) { SDL_VideoDevice *_this = SDL_GetVideoDevice(); @@ -367,13 +367,13 @@ char *SDL_GetPrimarySelectionText(void) } if (_this->GetPrimarySelectionText) { - return _this->GetPrimarySelectionText(_this); + return SDL_FreeLater(_this->GetPrimarySelectionText(_this)); // returned pointer follows the SDL_GetStringRule } else { const char *text = _this->primary_selection_text; if (!text) { text = ""; } - return SDL_strdup(text); + return text; } } diff --git a/test/testautomation_clipboard.c b/test/testautomation_clipboard.c index 6b80e44b30..5568029178 100644 --- a/test/testautomation_clipboard.c +++ b/test/testautomation_clipboard.c @@ -466,7 +466,7 @@ static int clipboard_testPrimarySelectionTextFunctions(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); @@ -483,7 +483,6 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg) charResult && SDL_strcmp(charResult, "") == 0, "Verify SDL_GetPrimarySelectionText returned \"\", got %s", charResult); - SDL_free(charResult); boolResult = SDL_HasPrimarySelectionText(); SDLTest_AssertCheck( boolResult == SDL_FALSE, @@ -515,7 +514,6 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg) charResult && SDL_strcmp(textRef, charResult) == 0, "Verify SDL_GetPrimarySelectionText 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",