From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin@cygwin.com
Subject: /dev/clipboard sometimes fails to set CF_UNICODETEXT data.
Date: Sat, 2 Jul 2022 12:20:56 +0900 [thread overview]
Message-ID: <20220702122056.39a6c5e49c637d028694412e@nifty.ne.jp> (raw)
[-- Attachment #1: Type: text/plain, Size: 1168 bytes --]
Hello,
In one of my PCs,
dd if=/dev/urandom count=10 | xxd > /dev/clipboard
sometimes fails to set CF_UNICODETEXT data.
As a result, pasting clipboard to notepad does not work.
Even in the case, cygnativeformat data is set correctly.
So, "cat /dev/clipboard" works.
This problem depends on machine very much.
My one PC with Xeon E3-1281 v3 CPU has a high probability of failure,
however, another machine with Core i7-6700K CPU does not.
I looked into this problem, and found OpenClipboard() for
CF_UNICODETEXT fails. It seems that OpenClipboard() just
after CloseClipboard() has high probability of failure.
You can see the following test case immediately stops with error.
Even with Core i7-6700K CPU machine above, the test case fails.
#include <windows.h>
#include <stdio.h>
int main()
{
for (;;) {
if (!OpenClipboard(0)) {
printf("Open error.\n");
break;
}
if (!EmptyClipboard()) {
printf("Empty error.\n");
break;
}
if (!CloseClipboard()) {
printf("Cloes error.\n");
break;
}
}
return 0;
}
I also found the patch attached solves the issue.
I would appreciate any suggestion.
--
Takashi Yano <takashi.yano@nifty.ne.jp>
[-- Attachment #2: 0001-Cygwin-clipboard-Add-workaround-for-setting-clipboar.patch --]
[-- Type: application/octet-stream, Size: 2564 bytes --]
From c3338ec1a4fff96022c99671c73ee9841871f6e8 Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Sat, 2 Jul 2022 12:08:51 +0900
Subject: [PATCH] Cygwin: clipboard: Add workaround for setting clipboard
failure.
- OpenClipboard() just after CloseClipboard() has a high probabolity
of failure. Due to this, /dev/clipboard sometimes fails to set
CF_UNICODETEXT data. This patch add a workaround for this issue.
---
winsup/cygwin/fhandler_clipboard.cc | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/winsup/cygwin/fhandler_clipboard.cc b/winsup/cygwin/fhandler_clipboard.cc
index 05f54ffb3..b0cdf485e 100644
--- a/winsup/cygwin/fhandler_clipboard.cc
+++ b/winsup/cygwin/fhandler_clipboard.cc
@@ -19,6 +19,18 @@ details. */
#include "child_info.h"
#include <sys/clipboard.h>
+/* Opening clipboard immediately after CloseClipboard() has
+ high probability of failure. 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;
+ return false;
+}
+
/*
* Robert Collins:
* FIXME: should we use GetClipboardSequenceNumber to tell if the clipboard has
@@ -29,7 +41,7 @@ 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 ();
}
@@ -53,7 +65,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;
@@ -99,7 +111,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;
@@ -168,7 +180,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;
@@ -207,7 +219,7 @@ fhandler_dev_clipboard::read (void *ptr, size_t& len)
LPVOID cb_data;
int rach;
- if (!OpenClipboard (NULL))
+ if (!open_clipboard ())
{
len = 0;
return;
--
2.36.1
next reply other threads:[~2022-07-02 3:21 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-02 3:20 Takashi Yano [this message]
2022-07-05 7:42 ` Mark Geisert
2022-07-05 8:40 ` Takashi Yano
2022-07-05 11:42 ` Corinna Vinschen
2022-07-05 14:46 ` Andrey Repin
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=20220702122056.39a6c5e49c637d028694412e@nifty.ne.jp \
--to=takashi.yano@nifty.ne.jp \
--cc=cygwin@cygwin.com \
/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: link
Be 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).