Win32: Improve clipboard contention issue

This is primarily a workaround for a GLFW application reading and/or
writing to the clipboard in rapid succession and catching up with the
Windows Clipboard History, which also has to contend for the lock.
This commit is contained in:
Camilla Löwy
2020-01-17 03:25:54 +01:00
parent 2c3eb75748
commit 29885c6942
3 changed files with 38 additions and 10 deletions
+27 -10
View File
@@ -2293,7 +2293,7 @@ void _glfwSetCursorWin32(_GLFWwindow* window, _GLFWcursor* cursor)
void _glfwSetClipboardStringWin32(const char* string)
{
int characterCount;
int characterCount, tries = 0;
HANDLE object;
WCHAR* buffer;
@@ -2321,12 +2321,20 @@ void _glfwSetClipboardStringWin32(const char* string)
MultiByteToWideChar(CP_UTF8, 0, string, -1, buffer, characterCount);
GlobalUnlock(object);
if (!OpenClipboard(_glfw.win32.helperWindowHandle))
// NOTE: Retry clipboard opening a few times as some other application may have it
// open and also the Windows Clipboard History reads it after each update
while (!OpenClipboard(_glfw.win32.helperWindowHandle))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"Win32: Failed to open clipboard");
GlobalFree(object);
return;
Sleep(1);
tries++;
if (tries == 3)
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"Win32: Failed to open clipboard");
GlobalFree(object);
return;
}
}
EmptyClipboard();
@@ -2338,12 +2346,21 @@ const char* _glfwGetClipboardStringWin32(void)
{
HANDLE object;
WCHAR* buffer;
int tries = 0;
if (!OpenClipboard(_glfw.win32.helperWindowHandle))
// NOTE: Retry clipboard opening a few times as some other application may have it
// open and also the Windows Clipboard History reads it after each update
while (!OpenClipboard(_glfw.win32.helperWindowHandle))
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"Win32: Failed to open clipboard");
return NULL;
Sleep(1);
tries++;
if (tries == 3)
{
_glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
"Win32: Failed to open clipboard");
return NULL;
}
}
object = GetClipboardData(CF_UNICODETEXT);