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

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