public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* /dev/clipboard sometimes fails to set CF_UNICODETEXT data.
@ 2022-07-02  3:20 Takashi Yano
  2022-07-05  7:42 ` Mark Geisert
  0 siblings, 1 reply; 5+ messages in thread
From: Takashi Yano @ 2022-07-02  3:20 UTC (permalink / raw)
  To: cygwin

[-- 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: /dev/clipboard sometimes fails to set CF_UNICODETEXT data.
  2022-07-02  3:20 /dev/clipboard sometimes fails to set CF_UNICODETEXT data Takashi Yano
@ 2022-07-05  7:42 ` Mark Geisert
  2022-07-05  8:40   ` Takashi Yano
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Geisert @ 2022-07-05  7:42 UTC (permalink / raw)
  To: cygwin

Hi Takashi,

Takashi Yano wrote:
> 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.

Would be neet if this patch wasn't needed, but, oh well.  My only comment on the 
patch would be to possibly call the new wrapper function MyOpenClipboard, or 
CygOpenClipboard, to help keep the name paired with CloseClipboard.

I suppose it might happen in the future that a faster processor will require the 
loop count to be made larger.  Once again, oh well.
My 2c (adjusted for inflation),

..mark

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: /dev/clipboard sometimes fails to set CF_UNICODETEXT data.
  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
  0 siblings, 2 replies; 5+ messages in thread
From: Takashi Yano @ 2022-07-05  8:40 UTC (permalink / raw)
  To: cygwin

On Tue, 5 Jul 2022 00:42:50 -0700
Mark Geisert wrote:
> Hi Takashi,
> 
> Takashi Yano wrote:
> > 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.

Additional information:
In the machine with Xeon E3-1281 v3 CPU, the test case fails
in a several times of the loop, while it fails after 1000 to
3000 times of the loop in the machine with Core i7-6700K CPU.

The probability of failure depends much on machine.

> > #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.
> 
> Would be neet if this patch wasn't needed, but, oh well.  My only comment on the 
> patch would be to possibly call the new wrapper function MyOpenClipboard, or 
> CygOpenClipboard, to help keep the name paired with CloseClipboard.

Thanks for the comment. IIUC, the camel case name should be
used only for Win32 APIs related. However, this is the wrapper
for Win32 API. So, it might be acceptable. In other places,
some wrapper functions are named as camel case.

Corinna, WDYT?

> I suppose it might happen in the future that a faster processor will require the 
> loop count to be made larger.  Once again, oh well.

Maybe. In my most cases, OpenClipboard() successes after one
or two times of retrying. So, I think max_retry = 10 is enough
for the time being.

> My 2c (adjusted for inflation),

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: /dev/clipboard sometimes fails to set CF_UNICODETEXT data.
  2022-07-05  8:40   ` Takashi Yano
@ 2022-07-05 11:42     ` Corinna Vinschen
  2022-07-05 14:46     ` Andrey Repin
  1 sibling, 0 replies; 5+ messages in thread
From: Corinna Vinschen @ 2022-07-05 11:42 UTC (permalink / raw)
  To: cygwin

On Jul  5 17:40, Takashi Yano wrote:
> On Tue, 5 Jul 2022 00:42:50 -0700
> Mark Geisert wrote:
> > Takashi Yano wrote:
> > > I also found the patch attached solves the issue.
> > > 
> > > I would appreciate any suggestion.
> > 
> > Would be neet if this patch wasn't needed, but, oh well.  My only comment on the 
> > patch would be to possibly call the new wrapper function MyOpenClipboard, or 
> > CygOpenClipboard, to help keep the name paired with CloseClipboard.
> 
> Thanks for the comment. IIUC, the camel case name should be
> used only for Win32 APIs related. However, this is the wrapper
> for Win32 API. So, it might be acceptable. In other places,
> some wrapper functions are named as camel case.
> 
> Corinna, WDYT?

What about just adding a static inline wrapper called close_clipboard
for symmetry?


Corinna

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: /dev/clipboard sometimes fails to set CF_UNICODETEXT data.
  2022-07-05  8:40   ` Takashi Yano
  2022-07-05 11:42     ` Corinna Vinschen
@ 2022-07-05 14:46     ` Andrey Repin
  1 sibling, 0 replies; 5+ messages in thread
From: Andrey Repin @ 2022-07-05 14:46 UTC (permalink / raw)
  To: Takashi Yano, cygwin

Greetings, Takashi Yano!

> On Tue, 5 Jul 2022 00:42:50 -0700
> Mark Geisert wrote:
>> Hi Takashi,
>> 
>> Takashi Yano wrote:
>> > 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.

> Additional information:
> In the machine with Xeon E3-1281 v3 CPU, the test case fails
> in a several times of the loop, while it fails after 1000 to
> 3000 times of the loop in the machine with Core i7-6700K CPU.

> The probability of failure depends much on machine.

MFG… this might as well explain the issues OpenOffice having with clipboard
content. I'll forward your findings.

>> > #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.
>> 
>> Would be neet if this patch wasn't needed, but, oh well.  My only comment on the 
>> patch would be to possibly call the new wrapper function MyOpenClipboard, or 
>> CygOpenClipboard, to help keep the name paired with CloseClipboard.

> Thanks for the comment. IIUC, the camel case name should be
> used only for Win32 APIs related. However, this is the wrapper
> for Win32 API. So, it might be acceptable. In other places,
> some wrapper functions are named as camel case.

> Corinna, WDYT?

>> I suppose it might happen in the future that a faster processor will require the 
>> loop count to be made larger.  Once again, oh well.

> Maybe. In my most cases, OpenClipboard() successes after one
> or two times of retrying. So, I think max_retry = 10 is enough
> for the time being.

>> My 2c (adjusted for inflation),

> -- 
> Takashi Yano <takashi.yano@nifty.ne.jp>



-- 
With best regards,
Andrey Repin
Tuesday, July 5, 2022 17:44:56

Sorry for my terrible english...

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-07-05 14:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-02  3:20 /dev/clipboard sometimes fails to set CF_UNICODETEXT data Takashi Yano
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

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).