Skip to content

Unified Wayland support via XDG GlobalShortcuts portal#1

Closed
0-don wants to merge 2 commits intodevfrom
unified-wayland
Closed

Unified Wayland support via XDG GlobalShortcuts portal#1
0-don wants to merge 2 commits intodevfrom
unified-wayland

Conversation

@0-don
Copy link
Copy Markdown
Owner

@0-don 0-don commented Mar 27, 2026

Summary

Adds transparent Wayland support to the global-hotkey crate without breaking the existing public API. The same register() / unregister() / GlobalHotKeyEvent::receiver() calls work on both X11 and Wayland with zero consumer code changes.

This is an alternative approach to tauri-apps/global-hotkey#162 which introduces a separate Wayland API surface (wl_register_all(), WlNewHotKeyAction, etc.). Instead, this fork keeps the API unified by handling Wayland differences internally.

Related upstream issues:

How it works

  • Runtime detection: checks WAYLAND_DISPLAY env var at GlobalHotKeyManager::new() time
  • Wayland backend: uses ashpd crate to communicate with XDG GlobalShortcuts portal via D-Bus
  • ID mapping: uses HotKey::id() (u32) as portal action IDs, ensuring event round-trip compatibility
  • Batch registration: all shortcuts bound in a single BindShortcuts call per register/unregister operation
  • Tokio contained: explicit current_thread runtime on a dedicated OS thread (no nested runtime issues with Tauri)
  • App ID: resolved from GLOBAL_HOTKEY_APP_ID or FLATPAK_ID env vars, with a default fallback

File changes

  • src/platform_impl/mod.rs: path changed from x11/ to linux/
  • src/platform_impl/linux/mod.rs: new runtime dispatch layer with is_wayland() detection
  • src/platform_impl/linux/x11.rs: existing X11 backend (moved from x11/mod.rs, trimmed)
  • src/platform_impl/linux/wayland.rs: new Wayland backend (~400 lines)
  • Cargo.toml: added ashpd, tokio, futures (Linux only)
  • src/lib.rs: updated docs from "X11 Only" to "X11 and Wayland"

Tested on

  • X11: existing tests pass, no regression
  • Wayland: builds and links correctly (portal integration requires running compositor)

0-don added 2 commits March 27, 2026 20:59
Restructure Linux backend to support both X11 and Wayland transparently.
The existing public API (register/unregister/GlobalHotKeyEvent) works
identically on both display servers with runtime detection via
WAYLAND_DISPLAY env var.

Wayland backend uses ashpd crate to communicate with the XDG
GlobalShortcuts portal. HotKey IDs are used as portal action IDs,
ensuring event round-trip compatibility. App ID can be set via
GLOBAL_HOTKEY_APP_ID env var.
Applications that already run a tokio runtime (like Tauri apps) would
panic with 'Cannot start a runtime from within a runtime' because
#[tokio::main] tries to create a nested runtime. Instead, explicitly
build a new current_thread runtime on the dedicated event processor
thread.
@0-don 0-don closed this Mar 27, 2026
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.

1 participant