clipboard: SDL_GetPrimarySelectionText() now follows the SDL_GetStringRule.

Reference Issue #10229.
This commit is contained in:
Ryan C. Gordon 2024-07-15 14:07:33 -04:00
parent 158fc459f1
commit d40b89dff6
5 changed files with 12 additions and 16 deletions

View file

@ -101,23 +101,22 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasClipboardText(void);
extern SDL_DECLSPEC int SDLCALL SDL_SetPrimarySelectionText(const char *text); extern SDL_DECLSPEC int SDLCALL SDL_SetPrimarySelectionText(const char *text);
/** /**
* Get UTF-8 text from the primary selection, which must be freed with * Get UTF-8 text from the primary selection.
* SDL_free().
* *
* 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 primary selection's content. * 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 * \returns the primary selection text on success or an empty string on
* failure; call SDL_GetError() for more information. Caller must * failure; call SDL_GetError() for more information.
* call SDL_free() 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_HasPrimarySelectionText * \sa SDL_HasPrimarySelectionText
* \sa SDL_SetPrimarySelectionText * \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 * Query whether the primary selection exists and contains a non-empty text

View file

@ -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(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_Locale*,SDL_GetPreferredLocales,(void),(),return)
SDL_DYNAPI_PROC(SDL_DisplayID,SDL_GetPrimaryDisplay,(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(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_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),) 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),)

View file

@ -2264,13 +2264,12 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
case SDLK_V: case SDLK_V:
if (withAlt) { if (withAlt) {
/* Alt-V paste awesome text from the primary selection! */ /* Alt-V paste awesome text from the primary selection! */
char *text = SDL_GetPrimarySelectionText(); const char *text = SDL_GetPrimarySelectionText();
if (*text) { if (*text) {
SDL_Log("Primary selection: %s\n", text); SDL_Log("Primary selection: %s\n", text);
} else { } else {
SDL_Log("Primary selection is empty\n"); SDL_Log("Primary selection is empty\n");
} }
SDL_free(text);
} else if (withControl) { } else if (withControl) {
if (withShift) { if (withShift) {

View file

@ -349,7 +349,7 @@ int SDL_SetPrimarySelectionText(const char *text)
return -1; return -1;
} }
} else { } 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); _this->primary_selection_text = SDL_strdup(text);
} }
@ -357,7 +357,7 @@ int SDL_SetPrimarySelectionText(const char *text)
return 0; return 0;
} }
char *SDL_GetPrimarySelectionText(void) const char *SDL_GetPrimarySelectionText(void)
{ {
SDL_VideoDevice *_this = SDL_GetVideoDevice(); SDL_VideoDevice *_this = SDL_GetVideoDevice();
@ -367,13 +367,13 @@ char *SDL_GetPrimarySelectionText(void)
} }
if (_this->GetPrimarySelectionText) { if (_this->GetPrimarySelectionText) {
return _this->GetPrimarySelectionText(_this); return SDL_FreeLater(_this->GetPrimarySelectionText(_this)); // returned pointer follows the SDL_GetStringRule
} else { } else {
const char *text = _this->primary_selection_text; const char *text = _this->primary_selection_text;
if (!text) { if (!text) {
text = ""; text = "";
} }
return SDL_strdup(text); return text;
} }
} }

View file

@ -466,7 +466,7 @@ static int clipboard_testPrimarySelectionTextFunctions(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);
@ -483,7 +483,6 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg)
charResult && SDL_strcmp(charResult, "") == 0, charResult && SDL_strcmp(charResult, "") == 0,
"Verify SDL_GetPrimarySelectionText returned \"\", got %s", "Verify SDL_GetPrimarySelectionText returned \"\", got %s",
charResult); charResult);
SDL_free(charResult);
boolResult = SDL_HasPrimarySelectionText(); boolResult = SDL_HasPrimarySelectionText();
SDLTest_AssertCheck( SDLTest_AssertCheck(
boolResult == SDL_FALSE, boolResult == SDL_FALSE,
@ -515,7 +514,6 @@ static int clipboard_testPrimarySelectionTextFunctions(void *arg)
charResult && SDL_strcmp(textRef, charResult) == 0, charResult && SDL_strcmp(textRef, charResult) == 0,
"Verify SDL_GetPrimarySelectionText returned correct string, expected '%s', got '%s'", "Verify SDL_GetPrimarySelectionText 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",