wxTheClipboard doesnt work under WSL2 (wslg) (Issue #23544)

259 views
Skip to first unread message

elfmz

unread,
May 13, 2023, 8:41:50 AM5/13/23
to wx-...@googlegroups.com, Subscribed

Under Windows 11/WSL2 wxClipboard (wxTheClipboard instance) doesn't copy-paste across applications - data copied/pasted only within application itself. However when trying to create wxTextCtrl and copy/paste text by pressing ctrl+c/v inside of it then clipboard works properly.

Patch or snippet allowing to reproduce the problem:
Here is minimalistic example: https://pastecode.io/s/ksm4we9u
It creates window with wxTextCtrl and wxButton.
Button click handler gets text from clipboard pritns it to console and then sets new text string "1234567890" into clipboard.
So when i try to copy/paste between other applications and wxTextCtrl using Ctrl+C/Ctrl+V hotkeys - everything works as expected - text is being get/set across this and other application correctly. However when trying to click the button - then text is not transferred to other application, i.e. clipboard starting to behave like to be 'independently private' for my process.

Platform and version information

This only happens only under Windows 11/WSL2, no problems under vanilla Ubuntu-s.
However other applications like gedit under WSL seems to work correctly.

Reproduced with wxWidgets GTK3 v3.0.5 (from repository) and v3.2 (built manually).


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544@github.com>

VZ

unread,
May 13, 2023, 8:54:40 AM5/13/23
to wx-...@googlegroups.com, Subscribed

Sorry, I don't have WSL2 and am unlikely to have it, so I can't debug this. I'd recommend starting by setting environment variable WXTRACE="clipboard" and checking what is the difference (if any) with the output under normal Linux systems.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1546645917@github.com>

elfmz

unread,
May 13, 2023, 12:10:13 PM5/13/23
to wx-...@googlegroups.com, Subscribed

Here're logs, what was done: ran app and copied in browser word 'Stable' then clicked button twice. After that pressed in browser Ctrl+V and as a result in Ubuntu got 12345678 but in Windows there is still 'Stable ' pasted in browser, while test application gets 12345678 on second button press.
ubuntu.log
wsl.log


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1546701604@github.com>

VZ

unread,
May 15, 2023, 11:36:27 AM5/15/23
to wx-...@googlegroups.com, Subscribed

Looking at the diff between the logs it seems that WSL provides text/plain;charset=utf-8 format instead of UTF8_STRING. It should be relatively simple to add support for this format too, but I have no idea why it doesn't use the de facto standard here.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1548095205@github.com>

KeyWorksRW

unread,
Dec 18, 2023, 1:37:46 AM12/18/23
to wx-...@googlegroups.com, Subscribed

It's worth nothing that even a very simple call to gtk fails to place the text in the Windows clipboard:

    GtkClipboard* clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_clipboard_set_text(clipboard, "Hello World!", -1);

wxClipBoard calls gtk_selection_add_target() which places compound data into the WSL clipboard, but not the Windows clipboard. The only exception I can find is when the gtk clipboard is bypassed completely, which means a native window has do do the pasting to the clipboard. I suspect that WSL has an underlying special case for this -- they already added a special case for Ctrl+v/Ctrl+p for command line shells.

The only other way I've found to do this is the following:

#include <cstdlib>
#include <gtk/gtk.h>
#include <string>

void pasteToWindowsClipboard(const std::string& text)
{
    std::string result = std::getenv("WSL_DISTRO_NAME");
    if (result.size())
    {
        std::string command = "echo " + text + " | clip.exe";
        std::system(command.c_str());
    }
    else
    {
        // Use normal wxTheClipboard code here
    }
}

That only copies text, not compound data but it does end up in the Windows clipboard.

Bottom line, I don't think there is any way to copy to the Windows clipboard using any gtk API. It's also worth noting that you also can't paste from the Windows clipboard either -- attempting to paste using gtk API will use the WSL clipboard instead of the Windows clipboard.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1859639216@github.com>

VZ

unread,
Dec 18, 2023, 8:02:54 AM12/18/23
to wx-...@googlegroups.com, Subscribed

But how does clip.exe work? There must be some way of accessing Windows clipboard from WSL...

I naively thought that X server used under WSL had built-in support for this, but I could be wrong, of course.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1860443216@github.com>

Randalphwa

unread,
Dec 18, 2023, 12:25:54 PM12/18/23
to wx-...@googlegroups.com, Subscribed

clip.exe is a Windows program, so the pipe launches that program, the text is piped into it, and then it copies that text into the clipboard normally. Presumably one could do the opposite by creating a Linux clip program, and a Windows program could launch the Linux clip program and pipe text into it which would then get pasted into the WSL clipboard normally. As far as an API, though, I was unable to find anything on the internet that allowed calling a Windows API from within a Linux program running on WSL.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1861125040@github.com>

unxed

unread,
Mar 12, 2024, 3:15:47 PM3/12/24
to wx-...@googlegroups.com, Subscribed

I checked the "text" program from wxWidgets under WSLg + Ubuntu 23.10.
Copying between "text" and "Telegram (linux)" does not work in both directions.
Copying between "text" and "far2l GUI" does not work both ways.
Copying between "text" and "far2l GUI with GDK_BACKEND=x11" does not work in both directions.
Copying between "Telegram (linux)" and "far2l GUI with GDK_BACKEND=x11" works in both directions.

From elfmz/far2l#1658 (comment)


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1992366971@github.com>

unxed

unread,
Mar 13, 2024, 4:34:10 PM3/13/24
to wx-...@googlegroups.com, Subscribed

Please check with #24400


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1995707871@github.com>

unxed

unread,
Mar 14, 2024, 9:34:25 AM3/14/24
to wx-...@googlegroups.com, Subscribed

    std::string command = "echo " + text + " | clip.exe";

More advanced solution for wslg:
elfmz/far2l#1658 (comment)


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/1997474372@github.com>

unxed

unread,
Mar 16, 2024, 6:58:17 PM3/16/24
to wx-...@googlegroups.com, Subscribed

I see messages in the far2l chat that copying from far2l to other applications worked after elfmz/far2l@1e7b2a7

But it still doesn’t work in the opposite direction.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/2002170895@github.com>

unxed

unread,
Mar 16, 2024, 6:58:30 PM3/16/24
to wx-...@googlegroups.com, Subscribed

See also: microsoft/wslg#1216


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/2002170966@github.com>

unxed

unread,
Mar 16, 2024, 7:01:25 PM3/16/24
to wx-...@googlegroups.com, Subscribed

Looking at the diff between the logs it seems that WSL provides text/plain;charset=utf-8 format instead of UTF8_STRING. It should be relatively simple to add support for this format too, but I have no idea why it doesn't use the de facto standard here.

Even after we added support for providing text/plain;charset=utf-8 in far2l,
copying from far2l to other apps still don't work. Probably we have two or more bugs here.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/2002171964@github.com>

unxed

unread,
Mar 16, 2024, 7:20:32 PM3/16/24
to wx-...@googlegroups.com, Subscribed

It's worth nothing that even a very simple call to gtk fails to place the text in the Windows clipboard

gtk-based gedit still can do copy/paste in Ubuntu 23.10 on wslg, though.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/23544/2002179375@github.com>

Reply all
Reply to author
Forward
0 new messages