public inbox for cygwin-cvs@sourceware.org help / color / mirror / Atom feed
From: Takashi Yano <tyan0@sourceware.org> To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: clipboard: Add workaround for setting clipboard failure. Date: Tue, 12 Jul 2022 11:00:28 +0000 (GMT) [thread overview] Message-ID: <20220712110028.4BF623858C50@sourceware.org> (raw) https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=df0890575ce1aa9dfa643a42899a1904c88799c5 commit df0890575ce1aa9dfa643a42899a1904c88799c5 Author: Takashi Yano <takashi.yano@nifty.ne.jp> Date: Sat Jul 2 12:08:51 2022 +0900 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. Diff: --- winsup/cygwin/fhandler_clipboard.cc | 47 +++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/winsup/cygwin/fhandler_clipboard.cc b/winsup/cygwin/fhandler_clipboard.cc index 05f54ffb3..a220cfc1b 100644 --- a/winsup/cygwin/fhandler_clipboard.cc +++ b/winsup/cygwin/fhandler_clipboard.cc @@ -19,6 +19,27 @@ details. */ #include "child_info.h" #include <sys/clipboard.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 @@ -29,9 +50,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 (); } /* @@ -53,7 +74,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; @@ -61,7 +82,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); @@ -81,7 +102,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); */ @@ -99,7 +120,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; @@ -107,14 +128,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); */ @@ -168,7 +189,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; @@ -191,7 +212,7 @@ fhandler_dev_clipboard::fstat (struct stat *buf) buf->st_size = clipbuf->cb_size; GlobalUnlock (hglb); } - CloseClipboard (); + close_clipboard (); } return 0; @@ -207,7 +228,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len) LPVOID cb_data; int rach; - if (!OpenClipboard (NULL)) + if (!open_clipboard ()) { len = 0; return; @@ -218,7 +239,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len) || !(hglb = GetClipboardData (format)) || !(cb_data = GlobalLock (hglb))) { - CloseClipboard (); + close_clipboard (); len = 0; return; } @@ -305,7 +326,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len) } } GlobalUnlock (hglb); - CloseClipboard (); + close_clipboard (); len = ret; }
reply other threads:[~2022-07-12 11:00 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220712110028.4BF623858C50@sourceware.org \ --to=tyan0@sourceware.org \ --cc=cygwin-cvs@sourceware.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).