SDL_GetClipboardData() follows the SDL_GetStringRule

This commit is contained in:
Sam Lantinga 2024-07-19 11:08:03 -07:00
parent 61a7a0e579
commit c4eac60000
5 changed files with 22 additions and 31 deletions

View File

@ -219,15 +219,16 @@ extern SDL_DECLSPEC int SDLCALL SDL_ClearClipboardData(void);
* \param mime_type the mime type to read from the clipboard. * \param mime_type the mime type to read from the clipboard.
* \param size a pointer filled in with the length of the returned data. * \param size a pointer filled in with the length of the returned data.
* \returns the retrieved data buffer or NULL on failure; call SDL_GetError() * \returns the retrieved data buffer or NULL on failure; call SDL_GetError()
* for more information. Caller must call SDL_free() on the returned * for more information.
* pointer when done with it. *
* This returns temporary memory which will be automatically freed later, and can be claimed with SDL_ClaimTemporaryMemory().
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_HasClipboardData * \sa SDL_HasClipboardData
* \sa SDL_SetClipboardData * \sa SDL_SetClipboardData
*/ */
extern SDL_DECLSPEC void * SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size); extern SDL_DECLSPEC const void * SDLCALL SDL_GetClipboardData(const char *mime_type, size_t *size);
/** /**
* Query whether there is data in the clipboard for the provided mime type. * Query whether there is data in the clipboard for the provided mime type.

View File

@ -234,7 +234,7 @@ SDL_DYNAPI_PROC(SDL_CameraPosition,SDL_GetCameraPosition,(SDL_CameraID a),(a),re
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),return) SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),return)
SDL_DYNAPI_PROC(const SDL_CameraSpec* const*,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return) SDL_DYNAPI_PROC(const SDL_CameraSpec* const*,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)
SDL_DYNAPI_PROC(const SDL_CameraID*,SDL_GetCameras,(int *a),(a),return) SDL_DYNAPI_PROC(const 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(const void*,SDL_GetClipboardData,(const char *a, size_t *b),(a,b),return)
SDL_DYNAPI_PROC(const 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)

View File

@ -1996,7 +1996,7 @@ static void SDLTest_PasteScreenShot(void)
for (i = 0; i < SDL_arraysize(image_formats); ++i) { for (i = 0; i < SDL_arraysize(image_formats); ++i) {
size_t size; size_t size;
void *data = SDL_GetClipboardData(image_formats[i], &size); const void *data = SDL_GetClipboardData(image_formats[i], &size);
if (data) { if (data) {
char filename[16]; char filename[16];
SDL_IOStream *file; SDL_IOStream *file;
@ -2008,7 +2008,6 @@ static void SDLTest_PasteScreenShot(void)
SDL_WriteIO(file, data, size); SDL_WriteIO(file, data, size);
SDL_CloseIO(file); SDL_CloseIO(file);
} }
SDL_free(data);
return; return;
} }
} }

View File

@ -162,7 +162,7 @@ void *SDL_GetInternalClipboardData(SDL_VideoDevice *_this, const char *mime_type
return data; return data;
} }
void *SDL_GetClipboardData(const char *mime_type, size_t *size) const void *SDL_GetClipboardData(const char *mime_type, size_t *size)
{ {
SDL_VideoDevice *_this = SDL_GetVideoDevice(); SDL_VideoDevice *_this = SDL_GetVideoDevice();
@ -184,16 +184,16 @@ void *SDL_GetClipboardData(const char *mime_type, size_t *size)
*size = 0; *size = 0;
if (_this->GetClipboardData) { if (_this->GetClipboardData) {
return _this->GetClipboardData(_this, mime_type, size); return SDL_FreeLater(_this->GetClipboardData(_this, mime_type, size));
} else if (_this->GetClipboardText && SDL_IsTextMimeType(mime_type)) { } else if (_this->GetClipboardText && SDL_IsTextMimeType(mime_type)) {
void *data = _this->GetClipboardText(_this); char *text = _this->GetClipboardText(_this);
if (data && *(char *)data == '\0') { if (text && *text == '\0') {
SDL_free(data); SDL_free(text);
data = NULL; text = NULL;
} }
return data; return SDL_FreeLater(text);
} else { } else {
return SDL_GetInternalClipboardData(_this, mime_type, size); return SDL_FreeLater(SDL_GetInternalClipboardData(_this, mime_type, size));
} }
} }
@ -297,16 +297,15 @@ const char *SDL_GetClipboardText(void)
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) {
void *clipdata = SDL_GetClipboardData(text_mime_types[i], &length); const void *clipdata = SDL_GetClipboardData(text_mime_types[i], &length);
if (clipdata) { if (clipdata) {
text = (const char *) clipdata; text = (const char *)clipdata;
SDL_FreeLater(clipdata); // returned string follows the SDL_GetStringRule.
break; break;
} }
} }
if (!text) { if (!text) {
text = ""; text = SDL_CreateTemporaryString("");
} }
return text; return text;
} }
@ -349,7 +348,7 @@ int SDL_SetPrimarySelectionText(const char *text)
return -1; return -1;
} }
} else { } else {
SDL_FreeLater(_this->primary_selection_text); // SDL_GetPrimarySelectionText() returns this pointer. SDL_free(_this->primary_selection_text);
_this->primary_selection_text = SDL_strdup(text); _this->primary_selection_text = SDL_strdup(text);
} }
@ -367,13 +366,13 @@ const char *SDL_GetPrimarySelectionText(void)
} }
if (_this->GetPrimarySelectionText) { if (_this->GetPrimarySelectionText) {
return SDL_FreeLater(_this->GetPrimarySelectionText(_this)); // returned pointer follows the SDL_GetStringRule return SDL_FreeLater(_this->GetPrimarySelectionText(_this));
} else { } else {
const char *text = _this->primary_selection_text; const char *text = _this->primary_selection_text;
if (!text) { if (!text) {
text = ""; text = "";
} }
return text; return SDL_CreateTemporaryString(text);
} }
} }

View File

@ -86,9 +86,9 @@ static int clipboard_testClipboardDataFunctions(void *arg)
int last_clipboard_update_count; int last_clipboard_update_count;
int last_clipboard_callback_count; int last_clipboard_callback_count;
int last_clipboard_cleanup_count; int last_clipboard_cleanup_count;
void *data; const void *data;
size_t size; size_t size;
char *text; const char *text;
const char *expected_text; const char *expected_text;
TestClipboardData test_data1 = { TestClipboardData test_data1 = {
@ -186,7 +186,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text), size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d", "Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size); (int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
expected_text = "CUSTOM"; expected_text = "CUSTOM";
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]); boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]);
@ -206,7 +205,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text), size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d", "Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size); (int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_DATA]); boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_DATA]);
SDLTest_AssertCheck( SDLTest_AssertCheck(
@ -220,7 +218,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == test_data1.data_size, size == test_data1.data_size,
"Verify test data size, expected %d, got %d", "Verify test data size, expected %d, got %d",
(int)test_data1.data_size, (int)size); (int)test_data1.data_size, (int)size);
SDL_free(data);
boolResult = SDL_HasClipboardData("test/invalid"); boolResult = SDL_HasClipboardData("test/invalid");
SDLTest_AssertCheck( SDLTest_AssertCheck(
@ -235,7 +232,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == 0, size == 0,
"Verify invalid data size, expected 0, got %d", "Verify invalid data size, expected 0, got %d",
(int)size); (int)size);
SDL_free(data);
#if 0 /* There's no guarantee how or when the callback is called */ #if 0 /* There's no guarantee how or when the callback is called */
SDLTest_AssertCheck( SDLTest_AssertCheck(
@ -287,7 +283,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text), size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d", "Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size); (int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
expected_text = "CUSTOM"; expected_text = "CUSTOM";
boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]); boolResult = SDL_HasClipboardData(test_mime_types[TEST_MIME_TYPE_CUSTOM_TEXT]);
@ -307,7 +302,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == SDL_strlen(expected_text), size == SDL_strlen(expected_text),
"Verify test text size, expected %d, got %d", "Verify test text size, expected %d, got %d",
(int)SDL_strlen(expected_text), (int)size); (int)SDL_strlen(expected_text), (int)size);
SDL_free(text);
data = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_DATA], &size); data = SDL_GetClipboardData(test_mime_types[TEST_MIME_TYPE_DATA], &size);
SDLTest_AssertCheck( SDLTest_AssertCheck(
@ -317,7 +311,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == test_data2.data_size, size == test_data2.data_size,
"Verify test data size, expected %d, got %d", "Verify test data size, expected %d, got %d",
(int)test_data2.data_size, (int)size); (int)test_data2.data_size, (int)size);
SDL_free(data);
data = SDL_GetClipboardData("test/invalid", &size); data = SDL_GetClipboardData("test/invalid", &size);
SDLTest_AssertCheck( SDLTest_AssertCheck(
@ -328,7 +321,6 @@ static int clipboard_testClipboardDataFunctions(void *arg)
size == 0, size == 0,
"Verify invalid data size, expected 0, got %d", "Verify invalid data size, expected 0, got %d",
(int)size); (int)size);
SDL_free(data);
#if 0 /* There's no guarantee how or when the callback is called */ #if 0 /* There's no guarantee how or when the callback is called */
SDLTest_AssertCheck( SDLTest_AssertCheck(