Skip to content

Linux: native Wayland support (global shortcuts portal, wl-clipboard) - #292

Open
kareem-jalal wants to merge 2 commits into
theJayTea:mainfrom
kareem-jalal:wayland-native
Open

kareem-jalal wants to merge 2 commits into
theJayTea:mainfrom
kareem-jalal:wayland-native

Conversation

@kareem-jalal

Copy link
Copy Markdown

Writing Tools on a Wayland session can't see or touch native Wayland apps today. pynput only gets keyboard events from XWayland windows, so the hotkey never fires while a Wayland app has focus, and the synthetic Ctrl+C/Ctrl+V never reach the app either. Issue #93 covers this. Right now the workaround is running your other apps under XWayland or turning off Wayland for Flatpaks.

This PR adds native Wayland support on Linux. X11, Windows and macOS keep their current code paths.

How it works

When the app starts in a Wayland session, it uses a new Windows_and_Linux/linux_wayland/ package in place of pynput and pyperclip:

  • Global hotkey: registered through the XDG GlobalShortcuts portal (KDE Plasma 5.27+, GNOME 48+, Hyprland). The desktop owns the shortcut, asks the user to confirm it the first time, and lists it in its own keyboard settings. Per-button hotkeys go through the portal too.
  • Desktops without the portal: main.py --trigger [button] forwards to the running instance over a Unix socket. Users bind a key to it in their desktop settings.
  • Reading the selection: wl-clipboard plus ydotool (any compositor) or wtype (wlroots). The app puts a random marker on the clipboard, marked sensitive so clipboard managers don't store it, then sends Ctrl+C and waits for the clipboard to change. Clearing the clipboard instead doesn't work on KDE, because Klipper refills an empty clipboard from its history. The app restores the previous clipboard afterwards, images included, unless you copied something new in the meantime.
  • Ordering: Wayland can't send keys to a window that has lost focus, so the app copies first and shows the popup after. It also waits for the hotkey's modifiers to be released before sending Ctrl+C, so Ctrl+Alt+Space can't turn into Ctrl+Alt+C in your app.

WRITING_TOOLS_FORCE_X11=1 switches back to the old path.

Changes to existing files

They're small on purpose: 7 lines deleted across the existing files. The rest is new code.

  • WritingToolApp.py: picks the Wayland path once at startup. pynput/pyperclip are imported only on the old path.
  • ui/CustomPopupWindow.py: doesn't import pynput on Wayland, and validates hotkeys in the portal format.
  • main.py: --trigger.
  • requirements.txt: jeepney (pure-Python D-Bus), Linux only.
  • README (run-from-source page): a Wayland section with packages, shortcut setup and options.

I kept the existing code style and didn't reformat anything, so the diff stays reviewable. That was the sticking point on #229.

Testing

  • 49 unit tests in Windows_and_Linux/tests/: python3 -m unittest discover -s tests -t .
  • Used on a KDE Plasma 6.7 desktop (Wayland, Nobara 44).
  • An end-to-end run in a nested KWin: select text in a Qt window, trigger Proofread, and check that the corrected text replaces the selection.
  • Ctrl+C response measured in the same setup for Qt, Chrome, Zen (Firefox), VS Code and Kate: 65 to 70 ms median for each. With text selected, the popup appears about 175 ms after the key press.

Not tested yet: GNOME, Hyprland and Sway. They implement the same portal, but I haven't run it there. I'd appreciate it if someone on those could try it.

Notes

  • ydotool needs its ydotoold service running and write access to /dev/uinput (usually the input group). The README says so.
  • Thanks to feat: Linux local build script, install workflow, and Wayland native support #272 for the wl-clipboard and ydotool approach I started from. This PR is narrower: it focuses on Wayland support, and puts the hotkey on the portal, since pynput can't see keys while a Wayland app has focus.

Fixes #93. Related: #16, #229, #272.

pynput and pyperclip only work against an X server. On a Wayland session
the global hotkey never fires while a native Wayland window has focus,
and synthetic Ctrl+C/Ctrl+V never reach it, so the app does nothing.

When the session is Wayland, Writing Tools now:

- registers its hotkeys through the XDG GlobalShortcuts portal (KDE
  Plasma 5.27+, GNOME 48+, Hyprland). The desktop owns the binding and
  lists it in its own shortcut settings.
- offers `main.py --trigger [button]` for desktops without the portal,
  so users can bind a key to it in their desktop settings.
- captures the selection with wl-clipboard and ydotool or wtype. A
  sensitive sentinel on the clipboard tells a real copy apart from
  clipboard managers refilling an emptied clipboard. The previous
  clipboard, including images, is restored afterwards unless something
  new was copied in the meantime.
- waits for the hotkey's modifiers to be released before sending Ctrl+C,
  and shows the popup only after the copy, since Wayland can't send keys
  to a window that has lost focus.

X11 and Windows keep the existing pynput/pyperclip code paths.
WRITING_TOOLS_FORCE_X11=1 forces the old path on Wayland.
The popup waits for the selection to be copied, so every step before
Ctrl+C shows up as delay. Measured from the key press in a nested KWin:
380 ms to the popup with text selected, 1.85 s with nothing selected.

- Prepare the clipboard while waiting for the hotkey to be released,
  instead of after.
- Remember which input devices are keyboards. Finding them opened every
  input device on each press (~50 ms with 17 devices); now that happens
  once at startup and again only when a device is added or removed.
- Poll the clipboard every 15 ms instead of 40 ms.
- When nothing gets copied, press Ctrl+C again after 0.25 s and give up
  after 0.8 s instead of 1.5 s. A Qt text field, Chrome, Zen (Firefox),
  VS Code and Kate all filled the clipboard within 140 ms of Ctrl+C.

Now 175 ms with text selected and 0.96 s without.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Linux] Application does not get triggered when hot-key is pressed

1 participant