Linux HUD: say why it can't show; the service finds the session's Wayland display (#756) (#758)

* Linux HUD: name why it can't show; the service adopts the session's Wayland display (#756)

* fix log field name

* clippy
This commit is contained in:
Dax
2026-11-01 00:52:01 -04:00
committed by GitHub
parent e7aa4c792f
commit db58f64ffc
6 changed files with 156 additions and 28 deletions
+6
View File
@@ -250,6 +250,12 @@ substitute for source inspection or a second roadmap.
process (host thread, window on demand through one seam, Quit), the hotkey
capture-transcribe-paste loop with Paste Last,
and paste.
- `linux/service` also adopts the session's Wayland display at `hex service`
start when the unit has none (Hyprland imports nothing into systemd; #756):
the user manager's `WAYLAND_DISPLAY`, else the newest answering
`$XDG_RUNTIME_DIR/wayland-*` socket the user owns. `linux/rune_hud`'s
`limitation()` names the HUD's failure: no display, no layer-shell, or
Rune's overlay error.
- `linux/clipboard`: the clipboard a Linux paste restores: every format of the
CLIPBOARD selection read within a size cap and deadline, then served again
from Hex (ext-data-control, else wlr-data-control on Wayland, its own selection owner with INCR on
+12 -1
View File
@@ -82,7 +82,18 @@ explicitly clear stale Wayland/X11 values inherited by the user manager; missing
locale and data-directory variables are left to the user manager. A saved
`XAUTHORITY` whose file is gone (Xwayland writes a new cookie at each login, so
a reboot strands the old path) is replaced at startup by the user manager's
current value, or dropped so Xlib uses `~/.Xauthority`. No
current value, or dropped so Xlib uses `~/.Xauthority`. A unit started with no
display at all (the desktop never imported its environment into systemd) adopts
the user manager's `WAYLAND_DISPLAY`, else the newest Wayland socket of yours in
`XDG_RUNTIME_DIR` that answers. Hyprland imports nothing by default; add this to
its config so the service, its paste helpers, and the HUD get your session:
```ini
exec-once = dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP
```
When the HUD can't show, Hex's log says why: no display, no layer-shell (and
no XWayland), or the overlay failing to open, with its error. No
permissions are granted. The unit is `PartOf=graphical-session.target`;
desktops managing a custom session target must bind its lifetime to their
login/logout lifecycle. Systemd restarts a crashed Hex (`Restart=on-failure`)
+8
View File
@@ -96,7 +96,15 @@ pub(crate) fn run(shutdown: &'static AtomicBool) -> Result<()> {
)),
command => command,
};
// Before the log, which starts a thread: the service may need the
// session's Wayland display (#756).
let adopted = matches!(command, Command::Service { embedded: false })
.then(crate::linux::service::adopt_wayland_display)
.flatten();
let log_dir = crate::app_paths::init_process_logging(shutdown)?;
if let Some(wayland_display) = adopted {
tracing::info!(%wayland_display, "adopted the session's Wayland display");
}
let event_path = log_dir.join("live.ndjson");
match command {
+69 -27
View File
@@ -125,39 +125,81 @@ pub(crate) fn install<S: 'static>(app: &mut rune::App<S>) {
let _ = HUD.set(sender);
}
/// Why the HUD can't show: Rune tried and could not, or the Wayland
/// compositor offers no layer-shell (GNOME's Mutter) and there is no
/// XWayland for Rune to show it on instead. Logged once, the first time it
/// is known.
/// Why the HUD can't show, logged once per reason: Hex reaches no display at
/// all (a service started without the session's environment, #756), the
/// Wayland compositor offers no layer-shell (GNOME's Mutter) and there is no
/// XWayland for Rune to show it on instead, or Rune could not open the overlay.
pub(crate) fn limitation() -> Option<String> {
static NO_OVERLAY: OnceLock<bool> = OnceLock::new();
static LOGGED: std::sync::Once = std::sync::Once::new();
let rune_refused = HUD
static DISPLAY: OnceLock<Option<Limitation>> = OnceLock::new();
static LOGGED: Mutex<Option<String>> = Mutex::new(None);
let refused = HUD
.get()
.is_some_and(|hud| hud.availability() == Availability::Unavailable);
let unavailable = rune_refused
|| *NO_OVERLAY.get_or_init(|| {
let no_layer_shell = crate::linux::session::LinuxSession::detect().is_wayland()
&& wayland_reachable()
&& !crate::linux::paste::wayland_offers(LAYER_SHELL);
if no_layer_shell && xwayland() {
tracing::info!("HUD shows on XWayland: compositor has no layer-shell");
}
no_layer_shell && !xwayland()
});
if !unavailable {
return None;
.filter(|hud| hud.availability() == Availability::Unavailable)
.map(|hud| Limitation::Refused(hud.failure()));
let limitation = match DISPLAY.get_or_init(display_limitation) {
Some(Limitation::NoDisplay) => Limitation::NoDisplay,
display => refused.or_else(|| display.clone())?,
};
let (log, user) = limitation.describe();
let mut logged = LOGGED.lock().unwrap_or_else(|error| error.into_inner());
if logged.as_deref() != Some(log.as_str()) {
tracing::warn!("HUD unavailable: {log}; dictation still works");
*logged = Some(log);
}
LOGGED.call_once(|| {
tracing::warn!(
"HUD unavailable: compositor has no layer-shell and XWayland is not running; dictation still works"
)
});
Some(LIMITATION.into())
Some(user)
}
#[derive(Clone, Debug, PartialEq)]
enum Limitation {
/// Neither `WAYLAND_DISPLAY` reaches a compositor nor is `DISPLAY` set.
NoDisplay,
/// The compositor has no layer-shell, and there is no XWayland.
NoLayerShell,
/// Rune could not open the overlay, with its reason when it gives one.
Refused(Option<String>),
}
impl Limitation {
/// The log line and the text Settings shows.
fn describe(&self) -> (String, String) {
match self {
Self::NoDisplay => (
"Hex has no Wayland or X11 display (no WAYLAND_DISPLAY or DISPLAY in its environment)".into(),
"Recording HUD unavailable: Hex can't reach your display. If Hex runs as a systemd service, import the session's environment (on Hyprland: exec-once = dbus-update-activation-environment --systemd WAYLAND_DISPLAY XDG_CURRENT_DESKTOP). Dictation still works.".into(),
),
Self::NoLayerShell => (
"compositor has no layer-shell and XWayland is not running".into(),
"Recording HUD unavailable: this compositor does not support Wayland layer-shell and XWayland is not running. Dictation still works.".into(),
),
Self::Refused(failure) => {
let reason = failure.as_deref().unwrap_or("no reason given");
(
format!("Rune could not open the overlay ({reason})"),
format!("Recording HUD unavailable: the overlay could not open ({reason}). Dictation still works."),
)
}
}
}
}
/// What the display alone says about the HUD, checked once.
fn display_limitation() -> Option<Limitation> {
let wayland = crate::linux::session::LinuxSession::detect().is_wayland();
let reachable = wayland && wayland_reachable();
if !reachable && !xwayland() {
return Some(Limitation::NoDisplay);
}
if reachable && !crate::linux::paste::wayland_offers(LAYER_SHELL) {
if xwayland() {
tracing::info!("HUD shows on XWayland: compositor has no layer-shell");
return None;
}
return Some(Limitation::NoLayerShell);
}
None
}
const LAYER_SHELL: &str = "zwlr_layer_shell_v1";
const LIMITATION: &str = "Recording HUD unavailable: this compositor does not support Wayland layer-shell and XWayland is not running. Dictation still works.";
/// Whether a Wayland connection opens at all, so a missing display is never
/// read as a compositor without layer-shell.
+55
View File
@@ -120,6 +120,61 @@ pub(crate) fn refresh_x_authority() {
}
}
/// A service the user manager started before the desktop imported its
/// environment has no display: Hyprland, for one, imports nothing into
/// systemd unless its config runs `dbus-update-activation-environment`
/// (#756). Adopts the user manager's current `WAYLAND_DISPLAY`, else the
/// newest Wayland socket of this user's in `XDG_RUNTIME_DIR` that accepts a
/// connection. Returns the display it adopted. Called on the main thread
/// before Hex starts any other thread.
pub(crate) fn adopt_wayland_display() -> Option<String> {
if graphical_session() {
return None;
}
let uid = unsafe { libc::getuid() };
let runtime = std::env::var_os("XDG_RUNTIME_DIR")
.filter(|dir| !dir.is_empty())
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(format!("/run/user/{uid}")));
let display = manager_environment("WAYLAND_DISPLAY")
.filter(|display| wayland_socket(&runtime.join(display), uid))
.or_else(|| newest_wayland_socket(&runtime, uid))?;
unsafe {
std::env::set_var("XDG_RUNTIME_DIR", &runtime);
std::env::set_var("WAYLAND_DISPLAY", &display);
}
Some(display)
}
/// The newest `wayland-*` socket in `runtime` that `uid` owns and that
/// accepts a connection.
fn newest_wayland_socket(runtime: &Path, uid: u32) -> Option<String> {
let mut sockets = fs::read_dir(runtime)
.ok()?
.filter_map(|entry| entry.ok())
.filter_map(|entry| {
let name = entry.file_name().into_string().ok()?;
let metadata = entry.metadata().ok()?;
(name.starts_with("wayland-")
&& !name.ends_with(".lock")
&& metadata.file_type().is_socket()
&& metadata.uid() == uid)
.then(|| (metadata.modified().ok(), name))
})
.collect::<Vec<_>>();
sockets.sort_by_key(|(modified, _)| std::cmp::Reverse(*modified));
sockets
.into_iter()
.map(|(_, name)| name)
.find(|name| wayland_socket(&runtime.join(name), uid))
}
fn wayland_socket(path: &Path, uid: u32) -> bool {
fs::symlink_metadata(path)
.is_ok_and(|metadata| metadata.file_type().is_socket() && metadata.uid() == uid)
&& UnixStream::connect(path).is_ok()
}
/// A variable in the systemd user manager's environment, which the desktop
/// session imports at each login.
fn manager_environment(key: &str) -> Option<String> {
+6
View File
@@ -183,6 +183,12 @@ impl HudSender {
pub(crate) fn availability(&self) -> rune::Availability {
self.overlay.availability()
}
/// Why Rune could not open the overlay, when it says.
#[cfg(target_os = "linux")]
pub(crate) fn failure(&self) -> Option<String> {
self.overlay.failure()
}
}
/// The overlay's state: the model and what it last drew.