PDA

View Full Version : X11 clipboard trouble



spin
08-08-2012, 02:09 PM
Hi,

I'm having some trouble with the clipboard on Linux, using the latest clanlib 2.3 SVN.
I think there are two issues in Display/X11/clipboard_x11.cpp:

-) In CL_Clipboard_X11::get_clipboard_text, shouldn't there be a check like

if (clipboard_available)
{
return clipboard_current;
}
at the beginning of the function? Both variables are set throughout the file, but never actually used. This might be especially important since selection requests from the same window are ignored (line 127).
I tested this modification, works fine. A patch containing the above mentioned lines is attached.

-) It appears to me that the get_clipboard_text function is not "waiting long enough" for the SelectionNotify event. When I execute the program normally, I can't copy text from other windows. However, when I step through the code with a debugger, copying from other windows works. Out of curiosity, I also tested with a sleep(1) before XCheckTypedWindowEvent - works as well.
So my impression is that clanlib (or my computer) is a bit too fast here. But I do not really know how to fix this properly. Waiting in a while loop for the event can be problematic if other applications do not produce the correct events. Maybe an asynchronous callback would be the proper way to fix this, but that would require bigger modifications.

Do you have any other ideas how to fix issue #2?
Does the clipboard work on your computers?

Thanks,
spin

rombust
08-10-2012, 08:58 PM
I haven't had a chance to look at your patch.

Ref issue #2

Your bug description does seem to make sense.

It might have occured during the http://clanlib.org/wiki/ClanLib_2.3.4_Release_Notes release

For example...

Linux - Window events are fired straight away, instead of queuing them. This ensures xlib and the application are synchronized
... and others.

I don't think I ever tested the clipboard after the change.

I have some ideas to fix it. I'll have a look, but it maybe in a couple of weeks time.

Thanks

rombust
08-22-2012, 09:56 AM
I had a quick (2 hour) look at the problem.

"clipboard_available" is not actually required

It is a flag to determine if the X window supports the clipboard.

The same check can be made by using "if (atom_CLIPBOARD == None ) "

CL_Clipboard_X11::set_clipboard_text() "clipboard_current = text;" should be set, even if the X11 clipboard is not available
CL_Clipboard_X11::get_clipboard_text() should return clipboard_current, not CL_String when the X11 clipboard is not available

The XCheckTypedWindowEvent issue can be fixed by replacing the code as follows:



XConvertSelection(disp, atom_CLIPBOARD, XA_STRING, None, window, CurrentTime);
XFlush(disp);

int retry_count = 500; // Waiting for half second
while(true)
{
XEvent event;
if (XCheckTypedWindowEvent(x11_window->get_display(), x11_window->get_window(), SelectionNotify, &event))
break;

retry_count--;
if (retry_count < 0)
return CL_String();

usleep(1000);
}



But when testing by copying and pasting from the Terminal to a ClanLib application, the terminal and the ClanLib application both vanished!

That is as far as I got.

Notice, that I also changed XConvertSelection, from "XConvertSelection(disp, atom_CLIPBOARD, XA_STRING, atom_CLIPBOARD, window, CurrentTime);", which I believe was incorrect.

If interested, have a look at the Wine source code: dlls/winex11.drv/clipboard.c .It is a more complete working implementation. Unfortunately, we cannot copy the entire code, due to it having a conflicting license.
But we can still follow the same method, implementing it, in our way.