Skip to content

Commit 7e15603

Browse files
committed
FIX: better handling of fast read/write operations to the clipboard on Windows
1 parent 9671f84 commit 7e15603

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/os/win32/dev-clipboard.c

+24-7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
REBUNI *cp;
7777
REBUNI *bin;
7878
REBINT len;
79+
REBCNT ok;
7980

8081
req->actual = 0;
8182

@@ -85,7 +86,14 @@
8586
return DR_ERROR;
8687
}
8788

88-
if (!OpenClipboard(NULL)) {
89+
MSG msg;
90+
for (int i = 1; i < 4; i++) {
91+
ok = OpenClipboard(NULL);
92+
if (ok) break;
93+
Sleep(i);
94+
PeekMessage(&msg, NULL, 0, 0, 0);
95+
}
96+
if (!ok) {
8997
req->error = 20;
9098
return DR_ERROR;
9199
}
@@ -130,8 +138,9 @@
130138
***********************************************************************/
131139
{
132140
HANDLE data;
141+
MSG msg;
133142
REBYTE *bin;
134-
REBCNT err;
143+
REBCNT ok;
135144
REBINT len = req->length; // in bytes
136145

137146
req->actual = 0;
@@ -151,20 +160,28 @@
151160

152161
COPY_MEM(bin, req->data, len);
153162
bin[len] = 0;
154-
GlobalUnlock(data);
155163

156-
if (!OpenClipboard(NULL)) {
164+
165+
for (int i = 1; i < 4; i++) {
166+
ok = OpenClipboard(NULL);
167+
if (ok) break;
168+
Sleep(i);
169+
PeekMessage(&msg, NULL, 0, 0, 0);
170+
}
171+
if (!ok) {
172+
GlobalUnlock(data);
157173
req->error = 20;
158174
return DR_ERROR;
159175
}
160176

161177
EmptyClipboard();
162178

163-
err = !SetClipboardData(GET_FLAG(req->flags, RRF_WIDE) ? CF_UNICODETEXT : CF_TEXT, data);
164-
179+
ok = SetClipboardData(GET_FLAG(req->flags, RRF_WIDE) ? CF_UNICODETEXT : CF_TEXT, data);
180+
181+
GlobalUnlock(data);
165182
CloseClipboard();
166183

167-
if (err) {
184+
if (!ok) {
168185
req->error = 50;
169186
return DR_ERROR;
170187
}

0 commit comments

Comments
 (0)