Retry to open the clipboard in case another application has it open

This fixes 'testautomation --filter clipboard_testClipboardTextFunctions' on Windows

(cherry picked from commit c24496727cdd40d5c0ffdf7b6a61085ec3a2766d)
This commit is contained in:
Sam Lantinga 2023-07-03 16:06:59 -07:00
parent a6ba8a1585
commit 7e8be3f280

View file

@ -102,23 +102,31 @@ int WIN_SetClipboardText(_THIS, const char *text)
char *WIN_GetClipboardText(_THIS) char *WIN_GetClipboardText(_THIS)
{ {
char *text; char *text = NULL;
text = NULL; if (IsClipboardFormatAvailable(TEXT_FORMAT)) {
if (IsClipboardFormatAvailable(TEXT_FORMAT) && /* Retry to open the clipboard in case another application has it open */
OpenClipboard(GetWindowHandle(_this))) { const int MAX_ATTEMPTS = 3;
HANDLE hMem; int attempt;
LPTSTR tstr;
hMem = GetClipboardData(TEXT_FORMAT); for (attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
if (hMem) { if (OpenClipboard(GetWindowHandle(_this))) {
tstr = (LPTSTR)GlobalLock(hMem); HANDLE hMem;
text = WIN_StringToUTF8(tstr); LPTSTR tstr;
GlobalUnlock(hMem);
} else { hMem = GetClipboardData(TEXT_FORMAT);
WIN_SetError("Couldn't get clipboard data"); if (hMem) {
tstr = (LPTSTR)GlobalLock(hMem);
text = WIN_StringToUTF8(tstr);
GlobalUnlock(hMem);
} else {
WIN_SetError("Couldn't get clipboard data");
}
CloseClipboard();
break;
}
SDL_Delay(10);
} }
CloseClipboard();
} }
if (text == NULL) { if (text == NULL) {
text = SDL_strdup(""); text = SDL_strdup("");