Bug 5735

Summary: Serious flaw in Cygwin X clipboard integration prevents paste from X to Windows apps
Product: xorg Reporter: Colin Harrison <colin.harrison>
Component: Server/DDX/XwinAssignee: Xorg Project Team <xorg-team>
Status: RESOLVED MOVED QA Contact:
Severity: normal    
Priority: high CC: alanh, jon.turney
Version: git   
Hardware: x86 (IA32)   
OS: Windows (All)   
Whiteboard: 2011BRB_Reviewed
i915 platform: i915 features:
Attachments:
Description Flags
Experimental patch to fix the problem
none
Revised patch
none
Adjusted patch to clear Windows clipboard on loss of focus none

Description Colin Harrison 2006-01-26 21:40:10 UTC
Stahlman Brett reported this following an investigation of Vim, see
http://cygwin.com/ml/cygwin-xfree/2006-01/msg00030.html
Comment 1 Colin Harrison 2006-01-26 21:48:04 UTC
Created attachment 4479 [details] [review]
Experimental patch to fix the problem

Here's an experimental patch. It appears to fix the issue Stahlman Brett was
having with unix gvim running on Windows.

I have used the patch in Xming and, so far, it looks sound and fixes the bug.
(Xming shares the same code as Cygwin/X)

Colin Harrison
Comment 2 Colin Harrison 2006-01-27 18:13:25 UTC
Sorry about the "endian" bug in Brett's name
Should be "Brett Stahlman" credited for reporting and fixing this bug.

Thanks
Harrison Colin
Comment 3 MMJ 2006-07-21 06:33:03 UTC
I am finding this bug very annoying, as I use gvim with cygwin.
Why isn't the patch checked in. There isn't any comment on the patch also.

-MMJ
Comment 4 Alan Hourihane 2006-08-18 01:22:39 UTC
Can someone explain why the patch does this....

 
-#ifdef HAVE_XWIN_CONFIG_H
-#include <xwin-config.h>
-#endif
-#include <sys/types.h>
-#include <sys/time.h>

???
Comment 5 Colin Harrison 2006-08-18 04:11:45 UTC
Created attachment 6592 [details] [review]
Revised patch
Comment 6 Colin Harrison 2006-08-18 04:13:57 UTC
Revised patch removes that and 3 unneccesary ErrorF traps.
Comment 7 Alan Hourihane 2006-08-18 06:43:37 UTC
Applied. Thanks.
Comment 8 Jon Turney 2009-05-20 05:14:10 UTC
Reopening.  This patch solves the problem it addresses, but creates a new one.

Clipboard monitoring applications (such as clipbrd.exe) notice the delayed reset of the clipboard done in WM_USER_PASTE_COMPLETE message handler, and request the clipboard contents to be rendered again, leading to an infinite loop.

A bit more analysis is needed of why working with the selection in gvim doesn't always cause the windows clipboard contents to get updated.
Comment 9 Jeremy Huddleston Sequoia 2011-10-09 19:37:42 UTC
Has this been addressed?
Comment 10 Colin Harrison 2011-10-10 04:07:57 UTC
I still use the patch for this problem in Xming..but only under a special 'quirk' option -clipboard2. I have had no user reports since providing this and cannot reproduce the problem anyway. So the bug has not gone away..it's just got old!

The code is better without the patch and should be reverted.
Comment 11 Jon Turney 2011-10-13 05:11:57 UTC
(In reply to comment #10)
> I still use the patch for this problem in Xming..but only under a special
> 'quirk' option -clipboard2. I have had no user reports since providing this and
> cannot reproduce the problem anyway. So the bug has not gone away..it's just
> got old!
> 
> The code is better without the patch and should be reverted.

I agree this patch is not the correct fix.  I've been reverting this patch in the Cygwin X server releases for a couple of years, so I've generated a patch to revert this and sent it for your review.

However, I can reproduce the original bug quite easily using gitk, copying the SHA-1 hash from that works the first time, but the windows clipboard does not get updated when a changed SHA-1 hash is copied.
Comment 12 Jon Turney 2012-02-13 13:28:14 UTC
Created attachment 56991 [details] [review]
Adjusted patch to clear Windows clipboard on loss of focus

Digging into why this is happening with gitk:

XWin only monitors for selection owner changing.  An application which already owns the selection can change it's idea of what's in the selection without us noticing.

For example, gitk (and probably all applications using Tk) only claim the selection when it moves between controls, so one can adjust the selection within a control without any notice.

It's pretty straightforward to adjust the previous fix so we only clear the Windows clipboard when an X Window loses focus, which is actually suggested in the email thread linked.

But this makes the assumption that whenever X gains focus, the selection will be adjusted. So is almost, but not quite right: If the selection isn't altered while we have the focus, then we've destroyed what was on the clipboard previously.

I did hope that we could use the selection TIMESTAMP to determine if it has been adjusted while an X window has focus, but that doesn't seem to be workable: Tk always returns 0 for the TIMESTAMP.

Another approach would be to act like xclipboard, and claim the selection every time we lose ownership, so that X applications have to claim it everytime they want to change it, so we get a selection ownership change notification everytime that happens.  This should be workable for CLIPBOARD, but is no good for PRIMARY, as taking ownership of PRIMARY causes the highlighting of the PRIMARY selection to disappear.
Comment 13 Jon Turney 2012-06-28 07:47:43 UTC
(In reply to comment #12) 
> XWin only monitors for selection owner changing.  An application which already
> owns the selection can change it's idea of what's in the selection without us
> noticing.

Note that this is only an issue with PRIMARY.  The ICCCM requires that ownership of CLIPBOARD is reasserted when it's contents change.
 
> It's pretty straightforward to adjust the previous fix so we only clear the
> Windows clipboard when an X Window loses focus, which is actually suggested in
> the email thread linked.
> 
> But this makes the assumption that whenever X gains focus, the selection will
> be adjusted. So is almost, but not quite right: If the selection isn't altered
> while we have the focus, then we've destroyed what was on the clipboard
> previously.

So, perhaps when X server loses focus, if ownership of PRIMARY hasn't changed since the last time we lost focus, we could request the contents of PRIMARY, and if it has changed, clear the Windows clipboard?  (This might interact badly with with changes to the CLIPBOARD selection as we wouldn't know when the contents of PRIMARY changed, so would always be assuming that PRIMARY changed last)
Comment 14 GitLab Migration User 2018-12-13 18:31:29 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/xorg/xserver/issues/197.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.