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) (cherry picked from commit 7e8be3f2804b8dfca1ede2a4e463f8a0b2b352d5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
diff --git a/src/video/windows/SDL_windowsclipboard.c b/src/video/windows/SDL_windowsclipboard.c
index bd43fc5..892af0d 100644
--- a/src/video/windows/SDL_windowsclipboard.c
+++ b/src/video/windows/SDL_windowsclipboard.c
@@ -102,23 +102,31 @@ int WIN_SetClipboardText(_THIS, const char *text)
char *WIN_GetClipboardText(_THIS)
{
- char *text;
-
- text = NULL;
- if (IsClipboardFormatAvailable(TEXT_FORMAT) &&
- OpenClipboard(GetWindowHandle(_this))) {
- HANDLE hMem;
- LPTSTR tstr;
-
- hMem = GetClipboardData(TEXT_FORMAT);
- if (hMem) {
- tstr = (LPTSTR)GlobalLock(hMem);
- text = WIN_StringToUTF8(tstr);
- GlobalUnlock(hMem);
- } else {
- WIN_SetError("Couldn't get clipboard data");
+ char *text = NULL;
+
+ if (IsClipboardFormatAvailable(TEXT_FORMAT)) {
+ /* Retry to open the clipboard in case another application has it open */
+ const int MAX_ATTEMPTS = 3;
+ int attempt;
+
+ for (attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
+ if (OpenClipboard(GetWindowHandle(_this))) {
+ HANDLE hMem;
+ LPTSTR tstr;
+
+ hMem = GetClipboardData(TEXT_FORMAT);
+ 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) {
text = SDL_strdup("");