Bug 489103 - Wayland: X11 apps can't access primary selection
Summary: Wayland: X11 apps can't access primary selection
Status: REPORTED
Alias: None
Product: kwin
Classification: Plasma
Component: core (other bugs)
Version First Reported In: 6.1.0
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-06-24 12:57 UTC by Konstantin Kharlamov
Modified: 2024-07-31 05:42 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Konstantin Kharlamov 2024-06-24 12:57:54 UTC
SUMMARY

I'm in the process of migrating to KDE Wayland, and one thing I heavily rely upon is primary selection. I use it as part of my work in order to be able to quickly select part of the text in logs and then execute a Emacs function that parses and finds various locations based on it.

However, on KDE Wayland primary selection in Emacs has inconsistent content. Further research showed that it isn't specific in any way to Emacs, but rather that in general there's some miscommunication about primary selection between X11 and Wayland apps.

Below it is shown by using `xlip` as the simplest X11 utility for accessing "selection".

STEPS TO REPRODUCE
1. On KDE Wayland, open Konsole, and select any text
2. Execute `xclip -selection clipboard -o`

OBSERVED RESULT

It prints anything but the text you just selected

EXPECTED RESULT

It should print the text you selected

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: 6.1.0
KDE Plasma Version: 6.1.0
KDE Frameworks Version: 6.3.0
Qt Version: 6.7.1
Comment 1 Zamundaaa 2024-06-24 13:56:47 UTC
It's expected that xclip doesn't work, since KWin intentionally doesn't update the X11 clipboard unless a X11 window is focused. You can use wl-paste --primary as a Wayland native alternative that acts as a clipboard manager to get privileged access.
Comment 2 Konstantin Kharlamov 2024-06-24 15:02:47 UTC
(In reply to Zamundaaa from comment #1)
> It's expected that xclip doesn't work, since KWin intentionally doesn't
> update the X11 clipboard unless a X11 window is focused. You can use
> wl-paste --primary as a Wayland native alternative that acts as a clipboard
> manager to get privileged access.

Should I create a separate report for Emacs then? Emacs definitely has a focused window when I'm trying to make it access primary clipboard.
Comment 3 Zamundaaa 2024-06-24 15:24:25 UTC
Is the focused window using X11 or Wayland? If it's Wayland native, it does need to be reported to emacs. If not, it should work
Comment 4 Konstantin Kharlamov 2024-06-24 15:31:59 UTC
(In reply to Zamundaaa from comment #3)
> Is the focused window using X11 or Wayland? If it's Wayland native, it does
> need to be reported to emacs. If not, it should work

Just to clarify: by saying "focused window" you are talking about Emacs window, right?

So, my non-minimal actual problem (that involves a focused X11 window, i.e. Emacs) is: select text in Wayland Konsole, and then focus Emacs and make it fetch the primary selection. That doesn't work. So I should create a separate report for that, right?
Comment 5 Zamundaaa 2024-06-24 15:42:03 UTC
I meant the Emacs window, yes.

FWIW I just tested this and it spits out the clipboard content... which is afaict expected, as you tell it to use the clipboard?
"xclip -o" prints the selection.
It does however only work if the selection is from a X11 window, it prints
> xclip: Error: There is no owner for the PRIMARY selection
when it's from a Wayland window. This might just be xclip being weird (as middle click paste works fine in Discord) but it might be a bug in KWin too
Comment 6 Konstantin Kharlamov 2024-06-24 15:53:19 UTC
(In reply to Zamundaaa from comment #5)
> I meant the Emacs window, yes.
> 
> FWIW I just tested this and it spits out the clipboard content... which is
> afaict expected, as you tell it to use the clipboard?
> "xclip -o" prints the selection.
> It does however only work if the selection is from a X11 window, it prints
> > xclip: Error: There is no owner for the PRIMARY selection
> when it's from a Wayland window. This might just be xclip being weird (as
> middle click paste works fine in Discord) but it might be a bug in KWin too

I'm sorry for confusion.

Okay, so, should I maybe create a separate report (and have this one closed) that's using X11 Emacs specifically to fetch primary selection? I mean, from the discussion above it seems like an actual application with a window apparently might suit better.

Btw, I just found one peculiar thing with Emacs: `middle-click` paste into Emacs works no problem. What does not, however, is accessing primary selection programmatically by evaluating `(gui-get-selection 'PRIMARY)`.
Comment 7 Konstantin Kharlamov 2024-06-25 07:10:09 UTC
I'd appreciate any code pointers I might have a look at it in my spare time
Comment 8 David Edmundson 2024-06-27 09:54:25 UTC
Can you confirm if emacs is a wayland window or not. 
An easy test is to run xprop and hover over the emacs window. If it changes to a + icon, it's in xwayland.
Comment 9 Konstantin Kharlamov 2024-06-27 10:59:16 UTC
Emacs is an X11 window. There's an experimental Wayland support aka "pure GTK3", but AFAIK it has problems on X11 (which is why it was never made default), so I'd prefer to stick to X11 build because I might have to switch between Wayland and X11.
Comment 10 Konstantin Kharlamov 2024-07-01 11:28:17 UTC
FTR, I just tested same way on Gnome Wayland and X11 Emacs does see selection from Wayland Konsole, so it is something on the kwin's side.
Comment 11 Bug Janitor Service 2024-07-16 03:46:31 UTC Comment hidden (spam)
Comment 12 Bug Janitor Service 2024-07-31 03:46:17 UTC Comment hidden (spam)
Comment 13 Konstantin Kharlamov 2024-07-31 05:42:46 UTC
Oh, somehow I missed the previous notification. Yeah, all information has been provided, it's just that the status wasn't changed.

So per notification, changing to REPORTED