mirror of
https://github.com/glfw/glfw.git
synced 2026-09-22 16:52:57 +00:00
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:
+27
-10
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user