From c4cd8377bb193fb7d7c0475a8800104f18efd289 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 19 Nov 2026 21:45:48 +0100 Subject: [PATCH] Hint All slides as a transient floating window on Linux --- Cargo.lock | 4 +- Cargo.toml | 2 +- crates/pdf-presenter/Cargo.toml | 2 + crates/pdf-presenter/src/native.rs | 117 +++++++++++++++++++--- crates/pdf-presenter/tests/native.rs | 84 ++++++++++++++++ docs/changelog.md | 10 ++ docs/development/native-implementation.md | 24 +++++ docs/user-guide.md | 21 ++++ 8 files changed, 249 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7e92a0..87a0380 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2212,7 +2212,7 @@ dependencies = [ [[package]] name = "pdf-presenter" -version = "0.1.32" +version = "0.1.33" dependencies = [ "bytemuck", "chrono", @@ -2233,6 +2233,8 @@ dependencies = [ "sys-locale", "tempfile", "toml 0.8.23", + "wayland-client", + "wayland-protocols", "winit", "winresource", "x11-dl", diff --git a/Cargo.toml b/Cargo.toml index c994503..46d345f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["crates/pdf-presenter"] resolver = "3" [workspace.package] -version = "0.1.32" +version = "0.1.33" edition = "2024" license = "MIT" repository = "https://proxy.dandaedre.com/gitea/idle-org/pdf-presenter" diff --git a/crates/pdf-presenter/Cargo.toml b/crates/pdf-presenter/Cargo.toml index df79006..5204902 100644 --- a/crates/pdf-presenter/Cargo.toml +++ b/crates/pdf-presenter/Cargo.toml @@ -33,6 +33,8 @@ libc = "0.2" [target.'cfg(target_os = "linux")'.dependencies] x11-dl = "=2.21.0" +wayland-client = { version = "=0.31.15", features = ["system", "dlopen"] } +wayland-protocols = { version = "=0.32.13", features = ["client"] } [build-dependencies] winresource = "=0.1.17" diff --git a/crates/pdf-presenter/src/native.rs b/crates/pdf-presenter/src/native.rs index 23e48fe..e2e31b7 100644 --- a/crates/pdf-presenter/src/native.rs +++ b/crates/pdf-presenter/src/native.rs @@ -46,6 +46,8 @@ struct NativeWindow { transition: Option<(Instant, bool)>, frames: usize, display_action: Option, + #[cfg(target_os = "linux")] + browser_parent: Option, } impl NativeWindow { fn create(el: &ActiveEventLoop, role: Role, monitor: String) -> Result { @@ -54,19 +56,23 @@ impl NativeWindow { Role::Browser => "PDF Presenter — All slides", Role::Presentation => "PDF Presenter — Presentation", }; - let window = Rc::new( - el.create_window( - crate::artwork::window_attributes() - .with_title(title) - .with_window_level(if role == Role::Browser { - winit::window::WindowLevel::AlwaysOnTop - } else { - winit::window::WindowLevel::Normal - }) - .with_visible(false) - .with_inner_size(PhysicalSize::new(1100, 750)), - )?, - ); + let attributes = crate::artwork::window_attributes() + .with_title(title) + .with_window_level(if role == Role::Browser { + winit::window::WindowLevel::AlwaysOnTop + } else { + winit::window::WindowLevel::Normal + }) + .with_visible(false) + .with_inner_size(PhysicalSize::new(1100, 750)); + #[cfg(target_os = "linux")] + let attributes = if role == Role::Browser { + use winit::platform::x11::{WindowAttributesExtX11, WindowType}; + attributes.with_x11_window_type(vec![WindowType::Dialog]) + } else { + attributes + }; + let window = Rc::new(el.create_window(attributes)?); let surface = softbuffer::Surface::new(&softbuffer::Context::new(window.clone())?, window.clone())?; let ctx = egui::Context::default(); @@ -92,6 +98,8 @@ impl NativeWindow { transition: None, frames: 0, display_action: None, + #[cfg(target_os = "linux")] + browser_parent: None, }) } fn place(&mut self, handle: &MonitorHandle, id: &str, fullscreen: bool, x11: bool) { @@ -243,6 +251,66 @@ impl NativeWindow { Ok(()) } } +/// Associate a separate, resizable browser with its presenter before first mapping. +/// No modal/child-window style: the operator can still use both windows. +#[cfg(target_os = "linux")] +fn linux_browser_parent(browser: &Window, parent: &Window) -> Result<(), Failure> { + use winit::raw_window_handle::{ + HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle, + }; + match browser.display_handle()?.as_raw() { + RawDisplayHandle::Wayland(display) => { + use wayland_client::{ + Connection, Proxy, + backend::{Backend, ObjectId}, + }; + use wayland_protocols::xdg::shell::client::xdg_toplevel::XdgToplevel; + use winit::platform::wayland::WindowExtWayland; + let child = browser + .xdg_toplevel() + .ok_or("Missing browser Wayland toplevel")?; + let owner = parent + .xdg_toplevel() + .ok_or("Missing presenter Wayland toplevel")?; + // SAFETY: both live Winit windows belong to this event loop/display. These + // temporary wrappers borrow its handles; they never destroy proxies, dispatch + // events, disconnect the display, or outlive either Window. Winit flushes the + // queued request with its next surface commit; there is no roundtrip here. + unsafe { + let connection = Connection::from_backend(Backend::from_foreign_display( + display.display.as_ptr().cast(), + )); + let child = XdgToplevel::from_id( + &connection, + ObjectId::from_ptr(XdgToplevel::interface(), child.as_ptr().cast())?, + )?; + let owner = XdgToplevel::from_id( + &connection, + ObjectId::from_ptr(XdgToplevel::interface(), owner.as_ptr().cast())?, + )?; + child.set_parent(Some(&owner)); + } + } + RawDisplayHandle::Xlib(display) => { + let (RawWindowHandle::Xlib(child), RawWindowHandle::Xlib(owner)) = ( + browser.window_handle()?.as_raw(), + parent.window_handle()?.as_raw(), + ) else { + return Err("Mismatched browser/presenter window backends".into()); + }; + let display = display.display.ok_or("Missing X11 display")?; + let xlib = x11_dl::xlib::Xlib::open()?; + // SAFETY: borrowed Xlib connection and live window IDs from this event loop. + // This sets a WM hint only; it neither reparents nor destroys either window. + unsafe { + (xlib.XSetTransientForHint)(display.as_ptr().cast(), child.window, owner.window); + } + } + _ => return Err("Unsupported Linux window backend".into()), + } + Ok(()) +} + fn monitors(el: &ActiveEventLoop) -> (Vec<(Monitor, MonitorHandle)>, usize) { let primary = el.primary_monitor(); let mut names = HashMap::new(); @@ -463,6 +531,29 @@ impl App { .push(NativeWindow::create(el, role, id.clone())?); self.windows.len() - 1 }; + #[cfg(target_os = "linux")] + if role == Role::Browser { + // Prefer the presenter on the chosen monitor, never an audience window. + let parent = self + .windows + .iter() + .filter(|w| w.role == Role::Presenter) + .min_by_key(|w| w.monitor != id) + .map(|w| w.window.clone()); + if let Some(parent) = parent { + let browser = &mut self.windows[index]; + if browser.browser_parent != Some(parent.id()) { + if let Err(error) = linux_browser_parent(&browser.window, &parent) + && let Some(s) = &mut self.studio + { + s.status = format!( + "Could not associate All slides with the presenter: {error}" + ); + } + browser.browser_parent = Some(parent.id()); + } + } + } self.windows[index].place(handle, &id, full, self.x11); } if let Some(s) = &mut self.studio { diff --git a/crates/pdf-presenter/tests/native.rs b/crates/pdf-presenter/tests/native.rs index f31e015..d8106c8 100644 --- a/crates/pdf-presenter/tests/native.rs +++ b/crates/pdf-presenter/tests/native.rs @@ -478,3 +478,87 @@ fn orphaned_output_holds_until_closed_then_exits_on_close_or_escape() { ); } } + +#[cfg(target_os = "linux")] +#[test] +fn wayland_browser_parents_to_presenter_before_first_buffer() { + if std::env::var_os("PDF_PRESENTER_GUI_TESTS").is_none() + || std::env::var_os("WAYLAND_DISPLAY").is_none() + { + return; + } + let trace = tempfile::NamedTempFile::new().unwrap(); + let mut child = Command::new(env!("CARGO_BIN_EXE_pdf-presenter")) + .arg("--smoke-test") + .env("WAYLAND_DEBUG", "client") + .stdout(Stdio::null()) + .stderr(trace.reopen().unwrap()) + .spawn() + .unwrap(); + let deadline = Instant::now() + Duration::from_secs(20); + loop { + if let Some(status) = child.try_wait().unwrap() { + assert!( + status.success(), + "{}", + std::fs::read_to_string(trace.path()).unwrap() + ); + break; + } + if Instant::now() >= deadline { + let _ = child.kill(); + let _ = child.wait(); + panic!("Wayland browser smoke timed out"); + } + std::thread::sleep(Duration::from_millis(10)); + } + let log = std::fs::read_to_string(trace.path()).unwrap(); + let top = |title: &str| { + log.lines() + .find(|line| line.contains(&format!(".set_title(\"{title}\")"))) + .unwrap() + .split(" -> ") + .nth(1) + .unwrap() + .split('.') + .next() + .unwrap() + }; + let presenter = top("PDF Presenter — Presenter"); + let browser = top("PDF Presenter — All slides"); + assert_ne!(browser, presenter); + let parent_at = log + .find(&format!("{browser}.set_parent({presenter})")) + .unwrap(); + let xdg = log + .lines() + .find(|l| l.contains(&format!(".get_toplevel(new id {browser})"))) + .unwrap() + .split(" -> ") + .nth(1) + .unwrap() + .split('.') + .next() + .unwrap(); + let surface = log + .lines() + .find(|l| l.contains(&format!(".get_xdg_surface(new id {xdg},"))) + .unwrap() + .split(", ") + .last() + .unwrap() + .trim_end_matches(')'); + let buffer_at = log.find(&format!("{surface}.attach(wl_buffer")).unwrap(); + assert!( + parent_at < buffer_at, + "The parent hint must precede mapping" + ); + assert!( + log.contains(&format!("{browser}.set_max_size(0, 0)")), + "Browser must remain resizable" + ); + assert!( + !log.contains(&format!("{presenter}.set_parent(")), + "Only the browser is parented" + ); +} diff --git a/docs/changelog.md b/docs/changelog.md index 32497d6..7cd323a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,16 @@ ## Unreleased +### 0.1.33 + +- Associate All slides with a presenter window on Linux: send Wayland's toplevel + parent hint and X11's transient/dialog hints so tiling desktops can float it. + Keep the browser separate, resizable and non-modal; refresh its parent after + presenter recreation. Compositor policies still control placement. Linux tests, + Clippy/rustdoc, Python checks and X11 native smoke pass; coverage is 86.38%. + Verified the Wayland parent request before first buffer and actual X11 dialog/ + transient properties. Niri and Windows/macOS runtime testing were not available. + ### 0.1.32 - Show a warning beneath the display setup notice whenever either fullscreen diff --git a/docs/development/native-implementation.md b/docs/development/native-implementation.md index 975e88b..88e2e73 100644 --- a/docs/development/native-implementation.md +++ b/docs/development/native-implementation.md @@ -266,3 +266,27 @@ remains part of the parent menu tree. Commands still explicitly close the menu; Escape dismisses it. Pointer regression coverage exercises both choice menus, Preferences controls and the three dismissal paths. Physical desktop acceptance remains separate from these egui input tests. + +## Linux browser window hints — 0.1.33 + +All slides is a non-modal, resizable auxiliary toplevel. Before its first rendered +buffer it sends `xdg_toplevel.set_parent` on Wayland, or `WM_TRANSIENT_FOR` plus +`_NET_WM_WINDOW_TYPE_DIALOG` on X11. A presenter on the browser's selected monitor +is preferred, otherwise the first presenter; an audience window is never the parent. +The association is refreshed if presenter windows are recreated or reassigned. +Winit 0.30.13 exposes the Wayland toplevel but not a portable transient-parent API; +short-lived typed Wayland wrappers borrow Winit's live handles without event dispatch, +roundtrips, proxy destruction or display ownership. Existing transitive Wayland crates +are direct Linux-only dependencies for this operation. Parent hints do not guarantee +compositor placement. Niri-specific runtime acceptance remains a separate check. + +Validation for 0.1.33: a native Wayland smoke run on the available KWin session +passed. The protocol regression verifies that All slides sends `set_parent` to +the presenter's toplevel before attaching its first buffer, retains unlimited +maximum size, and does not parent the presenter. X11 smoke passed; external +`xprop` inspection of the owned test windows confirmed the browser's dialog type +and transient owner pointing to the same process's presenter. Full Linux tests, +formatting, warnings-denied Clippy/rustdoc and 13 Python tests pass; whole-app +X11 coverage is 86.38%. Niri is unavailable here, so this verifies hint delivery, +not Niri placement or physical multi-monitor acceptance. Windows/macOS were not +built or runtime-tested for this Linux-only change. diff --git a/docs/user-guide.md b/docs/user-guide.md index 5cacf13..d313026 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -237,3 +237,24 @@ This placeholder is not a real slide: it cannot be selected or shown to the audience, does not appear in All slides, and does not change the slide count. Advancing at the end keeps the last real slide selected. Hiding the Next preview also hides the placeholder. + +## All slides on tiling Linux desktops + +All slides remains a separate, resizable window. On Linux it identifies a presenter +window as its parent (Wayland) or transient owner (X11), and requests the X11 dialog +window type. These hints let compositors treat it as a floating auxiliary window. +[Niri automatically floats parented windows](https://github.com/niri-wm/niri/wiki/Floating-Windows), +unless a window rule overrides that behavior. No desktop configuration is changed. +The compositor can still override placement, including the selected browser monitor. + +If your Niri rules still tile it, you can add this optional rule to your own config: + +```kdl +window-rule { + match app-id="^pdf-presenter$" title="^PDF Presenter — All slides$" + open-floating true +} +``` + +See [Niri window rules](https://github.com/niri-wm/niri/wiki/Configuration%3A-Window-Rules). +The rule targets only All slides, not presenter or audience windows.