diff options
author | Takashi Yano <takashi.yano@nifty.ne.jp> | 2022-07-02 12:08:51 +0900 |
---|---|---|
committer | Takashi Yano <takashi.yano@nifty.ne.jp> | 2022-07-12 19:59:38 +0900 |
commit | 9193f6efdeb090a677fe94b2279ede0d011246b9 (patch) | |
tree | db40fd0111058fc0b13db59f1acbe9a08f0308d5 | |
parent | 1db7cf5ce68e938d5f66ef5d70474f725b85d0e2 (diff) | |
download | newlib-9193f6efdeb090a677fe94b2279ede0d011246b9.zip newlib-9193f6efdeb090a677fe94b2279ede0d011246b9.tar.gz newlib-9193f6efdeb090a677fe94b2279ede0d011246b9.tar.bz2 |
Cygwin: clipboard: Add workaround for setting clipboard failure.
- OpenClipboard() just after CloseClipboard() sometimes fails. Due
to this, /dev/clipboard sometimes fails to set CF_UNICODETEXT
data. This patch add a workaround for this issue.
-rw-r--r-- | winsup/cygwin/fhandler_clipboard.cc | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/winsup/cygwin/fhandler_clipboard.cc b/winsup/cygwin/fhandler_clipboard.cc index 4886968..fe3545b 100644 --- a/winsup/cygwin/fhandler_clipboard.cc +++ b/winsup/cygwin/fhandler_clipboard.cc @@ -20,6 +20,27 @@ details. */ #include <sys/clipboard.h> #include <unistd.h> +/* Opening clipboard immediately after CloseClipboard() + sometimes fails. Therefore use retry-loop. */ +static inline bool +open_clipboard () +{ + const int max_retry = 10; + for (int i = 0; i < max_retry; i++) + { + if (OpenClipboard (NULL)) + return true; + Sleep (1); + } + return false; +} + +static inline bool +close_clipboard () +{ + return CloseClipboard (); +} + /* * Robert Collins: * FIXME: should we use GetClipboardSequenceNumber to tell if the clipboard has @@ -30,9 +51,9 @@ fhandler_dev_clipboard::fhandler_dev_clipboard () : fhandler_base (), pos (0), membuffer (NULL), msize (0) { /* FIXME: check for errors and loop until we can open the clipboard */ - OpenClipboard (NULL); + open_clipboard (); cygnativeformat = RegisterClipboardFormatW (CYGWIN_NATIVE); - CloseClipboard (); + close_clipboard (); } /* @@ -54,7 +75,7 @@ fhandler_dev_clipboard::set_clipboard (const void *buf, size_t len) { HGLOBAL hmem; /* Native CYGWIN format */ - if (OpenClipboard (NULL)) + if (open_clipboard ()) { cygcb_t *clipbuf; @@ -62,7 +83,7 @@ fhandler_dev_clipboard::set_clipboard (const void *buf, size_t len) if (!hmem) { __seterrno (); - CloseClipboard (); + close_clipboard (); return -1; } clipbuf = (cygcb_t *) GlobalLock (hmem); @@ -74,7 +95,7 @@ fhandler_dev_clipboard::set_clipboard (const void *buf, size_t len) GlobalUnlock (hmem); EmptyClipboard (); HANDLE ret = SetClipboardData (cygnativeformat, hmem); - CloseClipboard (); + close_clipboard (); /* According to MSDN, hmem must not be free'd after transferring the data to the clipboard via SetClipboardData. */ /* GlobalFree (hmem); */ @@ -92,7 +113,7 @@ fhandler_dev_clipboard::set_clipboard (const void *buf, size_t len) set_errno (EILSEQ); return -1; } - if (OpenClipboard (NULL)) + if (open_clipboard ()) { PWCHAR clipbuf; @@ -100,14 +121,14 @@ fhandler_dev_clipboard::set_clipboard (const void *buf, size_t len) if (!hmem) { __seterrno (); - CloseClipboard (); + close_clipboard (); return -1; } clipbuf = (PWCHAR) GlobalLock (hmem); sys_mbstowcs (clipbuf, len + 1, (const char *) buf); GlobalUnlock (hmem); HANDLE ret = SetClipboardData (CF_UNICODETEXT, hmem); - CloseClipboard (); + close_clipboard (); /* According to MSDN, hmem must not be free'd after transferring the data to the clipboard via SetClipboardData. */ /* GlobalFree (hmem); */ @@ -161,7 +182,7 @@ fhandler_dev_clipboard::fstat (struct stat *buf) buf->st_ctim.tv_nsec = 0L; buf->st_birthtim = buf->st_atim = buf->st_mtim = buf->st_ctim; - if (OpenClipboard (NULL)) + if (open_clipboard ()) { UINT formatlist[1] = { cygnativeformat }; int format; @@ -176,7 +197,7 @@ fhandler_dev_clipboard::fstat (struct stat *buf) buf->st_size = clipbuf->cb_size; GlobalUnlock (hglb); } - CloseClipboard (); + close_clipboard (); } return 0; @@ -192,7 +213,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len) LPVOID cb_data; int rach; - if (!OpenClipboard (NULL)) + if (!open_clipboard ()) { len = 0; return; @@ -203,7 +224,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len) || !(hglb = GetClipboardData (format)) || !(cb_data = GlobalLock (hglb))) { - CloseClipboard (); + close_clipboard (); len = 0; return; } @@ -290,7 +311,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len) } } GlobalUnlock (hglb); - CloseClipboard (); + close_clipboard (); len = ret; } |