Repository navigation
Linux: native Wayland support (global shortcuts portal, wl-clipboard) - #292
Open
kareem-jalal wants to merge 2 commits into
Open
kareem-jalal wants to merge 2 commits into
kareem-jalal wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:main.py --trigger [button]forwards to the running instance over a Unix socket. Users bind a key to it in their desktop settings.wl-clipboardplusydotool(any compositor) orwtype(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.WRITING_TOOLS_FORCE_X11=1switches 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.I kept the existing code style and didn't reformat anything, so the diff stays reviewable. That was the sticking point on #229.
Testing
Windows_and_Linux/tests/:python3 -m unittest discover -s tests -t .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
ydotoolneeds itsydotooldservice running and write access to/dev/uinput(usually theinputgroup). The README says so.Fixes #93. Related: #16, #229, #272.