From b6645bb25518b95cdce682e30136bb682043ab78 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 3 Jul 2023 16:06:59 -0700
Subject: [PATCH] Retry to open the clipboard in case another application has
it open
This fixes 'testautomation --filter clipboard_testClipboardTextFunctions' on Windows
---
src/video/windows/SDL_windowsclipboard.c | 40 ++++++++++++++----------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/src/video/windows/SDL_windowsclipboard.c b/src/video/windows/SDL_windowsclipboard.c
index 78aa4621f79f..7af109c44fa9 100644
--- a/src/video/windows/SDL_windowsclipboard.c
+++ b/src/video/windows/SDL_windowsclipboard.c
@@ -102,23 +102,31 @@ int WIN_SetClipboardText(SDL_VideoDevice *_this, const char *text)
char *WIN_GetClipboardText(SDL_VideoDevice *_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("");