cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 1 | //! Lazy Xwayland. monolith owns an X display socket from startup and only |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 2 | //! starts Xwayland when a client connects, handing it the socket so the |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 3 | //! connection that woke it is served. Xwayland exits once its last client |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 4 | //! leaves and the socket is watched again. |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 5 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 6 | use crate::state::State; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 7 | use smithay::desktop::Window; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 8 | use smithay::reexports::calloop::generic::Generic; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 9 | use smithay::reexports::calloop::{Interest, Mode, PostAction, RegistrationToken}; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 10 | use smithay::reexports::rustix::io::{FdFlags, fcntl_getfd, fcntl_setfd}; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 11 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 12 | use smithay::utils::{Logical, Rectangle, Size}; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 13 | use smithay::wayland::xwayland_shell::{XWaylandShellHandler, XWaylandShellState}; |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 14 | use smithay::xwayland::xwm::{Reorder, ResizeEdge, WmWindowProperty, WmWindowType, XwmId}; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 15 | use smithay::xwayland::{X11Surface, X11Wm, XWayland, XWaylandEvent, XwmHandler}; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 16 | use std::os::fd::AsFd; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 17 | use std::os::linux::net::SocketAddrExt; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 18 | use std::os::unix::net::{SocketAddr, UnixListener}; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 19 | use std::process::Stdio; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 20 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 21 | pub struct Xwayland { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 22 | pub display: u32, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 23 | listeners: Vec<UnixListener>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 24 | watch: Vec<(usize, RegistrationToken)>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 25 | running: bool, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 26 | pub wm: Option<X11Wm>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 27 | /// Override-redirect windows such as menus and tooltips, bottom to top. |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 28 | pub unmanaged: Vec<X11Surface>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 29 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 30 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 31 | impl Drop for Xwayland { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 32 | fn drop(&mut self) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 33 | let _ = std::fs::remove_file(socket_path(self.display)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 34 | let _ = std::fs::remove_file(lock_path(self.display)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 35 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 36 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 37 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 38 | fn socket_path(n: u32) -> String { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 39 | format!("/tmp/.X11-unix/X{n}") |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 40 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 41 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 42 | fn lock_path(n: u32) -> String { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 43 | format!("/tmp/.X{n}-lock") |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 44 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 45 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 46 | /// Claims the first free X display, taking over stale locks left by dead |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 47 | /// servers. |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 48 | fn claim() -> Option<(u32, Vec<UnixListener>)> { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 49 | let _ = std::fs::create_dir_all("/tmp/.X11-unix"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 50 | for n in 0..64 { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 51 | let lock = lock_path(n); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 52 | let created = std::fs::OpenOptions::new() |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 53 | .write(true) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 54 | .create_new(true) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 55 | .open(&lock); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 56 | let file = match created { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 57 | Ok(f) => f, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 58 | Err(_) => { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 59 | let owner = std::fs::read_to_string(&lock) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 60 | .ok() |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 61 | .and_then(|s| s.trim().parse::<u32>().ok()); |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 62 | let stale = owner.is_some_and(|pid| !holds_display(pid)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 63 | if !stale || std::fs::remove_file(&lock).is_err() { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 64 | continue; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 65 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 66 | match std::fs::OpenOptions::new() |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 67 | .write(true) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 68 | .create_new(true) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 69 | .open(&lock) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 70 | { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 71 | Ok(f) => f, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 72 | Err(_) => continue, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 73 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 74 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 75 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 76 | use std::io::Write; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 77 | let _ = writeln!(&file, "{:>10}", std::process::id()); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 78 | let path = socket_path(n); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 79 | let _ = std::fs::remove_file(&path); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 80 | let Ok(fs) = UnixListener::bind(&path) else { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 81 | let _ = std::fs::remove_file(&lock); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 82 | continue; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 83 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 84 | let mut listeners = vec![fs]; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 85 | if let Ok(addr) = SocketAddr::from_abstract_name(path.as_bytes()) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 86 | && let Ok(abs) = UnixListener::bind_addr(&addr) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 87 | { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 88 | listeners.push(abs); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 89 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 90 | return Some((n, listeners)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 91 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 92 | None |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 93 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 94 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 95 | pub fn init(state: &mut State) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 96 | let Some((number, listeners)) = claim() else { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 97 | tracing::warn!("no free X display, X11 clients will not run"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 98 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 99 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 100 | let mut watch = Vec::new(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 101 | for (i, l) in listeners.iter().enumerate() { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 102 | let Ok(l) = l.try_clone() else { continue }; |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 103 | let token = state.core.handle.insert_source( |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 104 | Generic::new(l, Interest::READ, Mode::Level), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 105 | move |_, _, state| { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 106 | start(state, i); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 107 | Ok(PostAction::Disable) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 108 | }, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 109 | ); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 110 | if let Ok(t) = token { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 111 | watch.push((i, t)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 112 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 113 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 114 | tracing::info!("X11 clients will start Xwayland on :{number}"); |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 115 | state.services.xwayland = Some(Xwayland { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 116 | display: number, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 117 | listeners, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 118 | watch, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 119 | running: false, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 120 | wm: None, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 121 | unmanaged: Vec::new(), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 122 | }); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 123 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 124 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 125 | /// Waits for the next X client to knock. |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 126 | fn rearm(state: &mut State) { |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 127 | let Some(x) = state.services.xwayland.as_mut() else { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 128 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 129 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 130 | x.running = false; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 131 | for (_, t) in &x.watch { |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 132 | let _ = state.core.handle.enable(t); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 133 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 134 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 135 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 136 | fn set_inherit(listeners: &[UnixListener], inherit: bool) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 137 | for l in listeners { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 138 | let flags = fcntl_getfd(l.as_fd()).unwrap_or(FdFlags::CLOEXEC); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 139 | let flags = if inherit { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 140 | flags - FdFlags::CLOEXEC |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 141 | } else { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 142 | flags | FdFlags::CLOEXEC |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 143 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 144 | let _ = fcntl_setfd(l.as_fd(), flags); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 145 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 146 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 147 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 148 | fn start(state: &mut State, fired: usize) { |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 149 | let Some(x) = state.services.xwayland.as_mut() else { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 150 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 151 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 152 | if x.running { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 153 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 154 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 155 | x.running = true; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 156 | for (i, t) in &x.watch { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 157 | if *i != fired { |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 158 | state.core.handle.disable(t).ok(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 159 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 160 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 161 | let mut args = Vec::new(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 162 | for l in &x.listeners { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 163 | args.push("-listenfd".to_string()); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 164 | args.push(std::os::fd::AsRawFd::as_raw_fd(l).to_string()); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 165 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 166 | // smithay only passes on the sockets it creates itself, so ours are |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 167 | // made inheritable just for this one spawn. |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 168 | set_inherit(&x.listeners, true); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 169 | let spawned = XWayland::spawn( |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 170 | &state.core.display, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 171 | None, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 172 | std::iter::empty::<(String, String)>(), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 173 | args, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 174 | false, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 175 | Stdio::null(), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 176 | Stdio::null(), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 177 | |_| {}, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 178 | ); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 179 | set_inherit(&x.listeners, false); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 180 | let (xwayland, client) = match spawned { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 181 | Ok(s) => s, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 182 | Err(e) => { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 183 | tracing::error!("cannot start Xwayland, X11 clients will not run: {e}"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 184 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 185 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 186 | }; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 187 | tracing::info!("starting Xwayland"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 188 | let inserted = state |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 189 | .core |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 190 | .handle |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 191 | .insert_source(xwayland, move |event, _, state| match event { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 192 | XWaylandEvent::Ready { x11_socket, .. } => { |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 193 | let dh = state.core.display.clone(); |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 194 | match X11Wm::start_wm(state.core.handle.clone(), &dh, x11_socket, client.clone()) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 195 | Ok(wm) => { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 196 | tracing::info!("Xwayland ready"); |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 197 | if let Some(x) = state.services.xwayland.as_mut() { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 198 | x.wm = Some(wm); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 199 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 200 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 201 | Err(e) => tracing::error!("cannot manage Xwayland windows: {e}"), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 202 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 203 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 204 | XWaylandEvent::Error => { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 205 | tracing::warn!("Xwayland failed to start"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 206 | rearm(state); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 207 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 208 | }); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 209 | if let Err(e) = inserted { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 210 | tracing::error!("cannot watch Xwayland: {e}"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 211 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 212 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 213 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 214 | impl XWaylandShellHandler for State { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 215 | fn xwayland_shell_state(&mut self) -> &mut XWaylandShellState { |
a772647a949f | atagen | 2026-10-17 02:10 UTC | 216 | &mut self.protocols.xwayland_shell_state |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 217 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 218 | |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 219 | fn surface_associated(&mut self, _xwm: XwmId, surface: WlSurface, window: X11Surface) { |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 220 | if let Some(id) = self.x11_window_id(&window) { |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 221 | self.wm.index(id); |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 222 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 223 | self.on_buffer(&surface); |
7e58494265db | atagen | 2026-10-17 02:56 UTC | 224 | self.dirty.redraw = true; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 225 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 226 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 227 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 228 | impl XwmHandler for State { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 229 | fn xwm_state(&mut self, _xwm: XwmId) -> &mut X11Wm { |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 230 | self.services |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 231 | .xwayland |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 232 | .as_mut() |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 233 | .and_then(|x| x.wm.as_mut()) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 234 | .expect("X11 events only arrive while the WM runs") |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 235 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 236 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 237 | fn new_window(&mut self, _xwm: XwmId, _window: X11Surface) {} |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 238 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 239 | fn new_override_redirect_window(&mut self, _xwm: XwmId, _window: X11Surface) {} |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 240 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 241 | fn map_window_request(&mut self, _xwm: XwmId, window: X11Surface) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 242 | if let Err(e) = window.set_mapped(true) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 243 | tracing::warn!("cannot map X11 window: {e}"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 244 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 245 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 246 | let float = floats(&window).then(|| window.geometry()); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 247 | self.map_window(Window::new_x11_window(window), float); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 248 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 249 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 250 | fn mapped_override_redirect_window(&mut self, _xwm: XwmId, window: X11Surface) { |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 251 | if let Some(x) = self.services.xwayland.as_mut() { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 252 | x.unmanaged.retain(|w| w != &window); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 253 | x.unmanaged.push(window); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 254 | } |
7e58494265db | atagen | 2026-10-17 02:56 UTC | 255 | self.dirty.redraw = true; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 256 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 257 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 258 | fn unmapped_window(&mut self, _xwm: XwmId, window: X11Surface) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 259 | self.forget_x11(&window); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 260 | if !window.is_override_redirect() { |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 261 | logged("unmap", window.set_mapped(false)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 262 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 263 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 264 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 265 | fn destroyed_window(&mut self, _xwm: XwmId, window: X11Surface) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 266 | self.forget_x11(&window); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 267 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 268 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 269 | fn configure_request( |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 270 | &mut self, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 271 | _xwm: XwmId, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 272 | window: X11Surface, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 273 | x: Option<i32>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 274 | y: Option<i32>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 275 | w: Option<u32>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 276 | h: Option<u32>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 277 | _reorder: Option<Reorder>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 278 | ) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 279 | if self.x11_window_id(&window).is_some() { |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 280 | logged("configure", window.configure(None)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 281 | return; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 282 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 283 | let mut geo = window.geometry(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 284 | geo.loc.x = x.unwrap_or(geo.loc.x); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 285 | geo.loc.y = y.unwrap_or(geo.loc.y); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 286 | geo.size.w = w.map_or(geo.size.w, |w| w as i32); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 287 | geo.size.h = h.map_or(geo.size.h, |h| h as i32); |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 288 | logged("configure", window.configure(geo)); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 289 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 290 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 291 | fn configure_notify( |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 292 | &mut self, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 293 | _xwm: XwmId, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 294 | window: X11Surface, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 295 | _geometry: Rectangle<i32, Logical>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 296 | _above: Option<smithay::xwayland::xwm::X11Window>, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 297 | ) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 298 | if window.is_override_redirect() { |
7e58494265db | atagen | 2026-10-17 02:56 UTC | 299 | self.dirty.redraw = true; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 300 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 301 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 302 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 303 | fn resize_request( |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 304 | &mut self, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 305 | _xwm: XwmId, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 306 | _window: X11Surface, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 307 | _button: u32, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 308 | _edge: ResizeEdge, |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 309 | ) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 310 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 311 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 312 | fn move_request(&mut self, _xwm: XwmId, _window: X11Surface, _button: u32) {} |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 313 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 314 | fn fullscreen_request(&mut self, _xwm: XwmId, window: X11Surface) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 315 | self.x11_fullscreen(&window, true); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 316 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 317 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 318 | fn unfullscreen_request(&mut self, _xwm: XwmId, window: X11Surface) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 319 | self.x11_fullscreen(&window, false); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 320 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 321 | |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 322 | fn property_notify(&mut self, _xwm: XwmId, window: X11Surface, property: WmWindowProperty) { |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 323 | if matches!(property, WmWindowProperty::Title | WmWindowProperty::Class) |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 324 | && let Some(id) = self.x11_window_id(&window) |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 325 | { |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 326 | self.refresh_identity(id); |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 327 | } |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 328 | } |
272bdf93aead | atagen | 2026-10-17 03:12 UTC | 329 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 330 | fn disconnected(&mut self, _xwm: XwmId) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 331 | tracing::info!("Xwayland exited"); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 332 | let gone: Vec<_> = self |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 333 | .wm |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 334 | .iter() |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 335 | .filter(|(_, w)| w.window.x11_surface().is_some()) |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 336 | .map(|(id, _)| id) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 337 | .collect(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 338 | for id in gone { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 339 | self.remove_window(id); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 340 | } |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 341 | if let Some(x) = self.services.xwayland.as_mut() { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 342 | x.wm = None; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 343 | x.unmanaged.clear(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 344 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 345 | rearm(self); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 346 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 347 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 348 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 349 | /// Dialogs, splash screens and fixed-size windows float at their own size. |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 350 | fn floats(window: &X11Surface) -> bool { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 351 | let kind = matches!( |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 352 | window.window_type(), |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 353 | Some( |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 354 | WmWindowType::Dialog |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 355 | | WmWindowType::Splash |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 356 | | WmWindowType::Utility |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 357 | | WmWindowType::Toolbar |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 358 | | WmWindowType::Menu |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 359 | ) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 360 | ); |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 361 | kind || fixed_size(window).is_some() || window.is_transient_for().is_some() |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 362 | } |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 363 | |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 364 | /// The size of a window that asks for exactly one. |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 365 | pub fn fixed_size(window: &X11Surface) -> Option<Size<i32, Logical>> { |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 366 | match (window.min_size(), window.max_size()) { |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 367 | (Some(min), Some(max)) if min == max && min.w > 0 && min.h > 0 => Some(min), |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 368 | _ => None, |
754ca2132943 | atagen | 2026-10-19 07:29 UTC | 369 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 370 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 371 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 372 | impl State { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 373 | pub fn x11_window_id(&self, window: &X11Surface) -> Option<monolith_layout::WindowId> { |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 374 | self.wm |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 375 | .iter() |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 376 | .find(|(_, w)| w.window.x11_surface() == Some(window)) |
60956e3999de | atagen | 2026-10-17 02:50 UTC | 377 | .map(|(id, _)| id) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 378 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 379 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 380 | fn forget_x11(&mut self, window: &X11Surface) { |
c7e08bac3e01 | atagen | 2026-10-17 02:45 UTC | 381 | if let Some(x) = self.services.xwayland.as_mut() { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 382 | x.unmanaged.retain(|w| w != window); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 383 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 384 | if let Some(id) = self.x11_window_id(window) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 385 | self.remove_window(id); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 386 | } |
7e58494265db | atagen | 2026-10-17 02:56 UTC | 387 | self.dirty.redraw = true; |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 388 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 389 | |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 390 | fn x11_fullscreen(&mut self, window: &X11Surface, on: bool) { |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 391 | if let Some(id) = self.x11_window_id(window) |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 392 | && self |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 393 | .wm |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 394 | .layout |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 395 | .window(id) |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 396 | .is_some_and(|w| w.fullscreen != on) |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 397 | { |
a76842e12aca | atagen | 2026-10-17 02:49 UTC | 398 | self.wm.layout.toggle_fullscreen(id); |
f50ebc6999a6 | atagen | 2026-10-19 07:51 UTC | 399 | self.wm.layout.focus_if_covering(id); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 400 | self.apply_layout(); |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 401 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 402 | } |
cce3ef7e0162 | atagen | 2026-10-16 22:31 UTC | 403 | } |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 404 | |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 405 | /// X11 requests fail when the window is already gone, which is routine, but |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 406 | /// a failure on a live window means it never got the geometry we chose. |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 407 | pub fn logged<E: std::fmt::Display>(what: &str, result: Result<(), E>) { |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 408 | if let Err(e) = result { |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 409 | tracing::debug!("X11 {what} failed: {e}"); |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 410 | } |
0e6ab4933125 | atagen | 2026-10-17 05:08 UTC | 411 | } |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 412 | |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 413 | /// Whether `pid` is alive and could own an X display lock. A SIGKILLed |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 414 | /// monolith leaves its lock behind, and its pid may since belong to anything. |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 415 | fn holds_display(pid: u32) -> bool { |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 416 | std::fs::read_to_string(format!("/proc/{pid}/comm")) |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 417 | .is_ok_and(|comm| comm.starts_with('X') || comm.trim() == "monolith") |
28e12a7e36cd | atagen | 2026-10-17 05:11 UTC | 418 | } |