From e266f484e231bbad5399323cde97199e2a3d2da3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 31 Jan 2025 22:11:03 -0500 Subject: Windows: Improve clipboard reliability --- src/win.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/win.c b/src/win.c index dd736ff..129c780 100644 --- a/src/win.c +++ b/src/win.c @@ -1410,8 +1410,21 @@ puglGetClipboard(PuglView* const view, { PuglInternals* const impl = view->impl; - if (typeIndex > 0U || !IsClipboardFormatAvailable(CF_UNICODETEXT) || - !OpenClipboard(impl->hwnd)) { + if (typeIndex > 0U || !IsClipboardFormatAvailable(CF_UNICODETEXT)) { + return NULL; + } + + // Try to open the clipboard several times since others may have locked it + BOOL opened = FALSE; + static const unsigned max_tries = 16U; + for (unsigned i = 0U; !opened && i < max_tries; ++i) { + opened = OpenClipboard(impl->hwnd); + if (!opened) { + Sleep(0); + } + } + + if (!opened) { return NULL; } -- cgit v1.2.1