From 828984be4bdf34dc8bb670c3c9014d0d5c09dd60 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:22:59 +0000 Subject: [PATCH] fix(compositor): close the focused window on Alt+F4 and let the shells exit without crashing There was no compositor-owned way to close an application window. Alt+F4 is now taken by the compositor: it sends xdg_toplevel close to the focused application window, never the panel, and the key does not reach the client. Closing exposed a crash in both Servo hosts: their shell-protocol client, created on winit's Wayland display, was dropped after that display was torn down, so wl_proxy_destroy touched freed memory and the process ended with SIGSEGV. The hosts now release Servo and the shell client in winit's exiting handler, while the display is alive. check_activation.py closes the focused application with Alt+F4 and requires its session to stop with the app shell exiting with status 0, the window to disappear, and Alt+F4 with the panel focused to leave the panel in place. --- crates/weft-app-shell/src/embedder.rs | 8 ++++++++ crates/weft-compositor/src/input.rs | 17 +++++++++++++++-- crates/weft-compositor/src/windows.rs | 25 +++++++++++++++++++++++++ crates/weft-servo-shell/src/embedder.rs | 8 ++++++++ docs/architecture.md | 2 +- tests/frame/check_activation.py | 25 +++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 3 deletions(-) diff --git a/crates/weft-app-shell/src/embedder.rs b/crates/weft-app-shell/src/embedder.rs index 5f7529f..1cffa8d 100644 --- a/crates/weft-app-shell/src/embedder.rs +++ b/crates/weft-app-shell/src/embedder.rs @@ -431,6 +431,14 @@ impl App { } impl ApplicationHandler for App { + /// Releases Servo and the shell protocol objects while winit's Wayland + /// display is still connected; dropped later, the shell client's + /// proxies would be destroyed on a display that is gone. + fn exiting(&mut self, _event_loop: &ActiveEventLoop) { + self.shut_down(); + self.shell_client = None; + } + fn resumed(&mut self, event_loop: &ActiveEventLoop) { if self.window.is_some() { return; diff --git a/crates/weft-compositor/src/input.rs b/crates/weft-compositor/src/input.rs index ef7d72d..bf64e6d 100644 --- a/crates/weft-compositor/src/input.rs +++ b/crates/weft-compositor/src/input.rs @@ -48,14 +48,27 @@ fn handle_keyboard(state: &mut WeftCompositorState, event: B::K let key_state = event.state(); if let Some(keyboard) = state.seat.get_keyboard() { - keyboard.input::<(), _>( + // Alt+F4 belongs to the compositor: it asks the focused application + // window to close, and the key never reaches a client. + let close = keyboard.input::( state, event.key_code(), key_state, serial, time, - |_state, _mods, _keysym| FilterResult::Forward, + |_state, mods, keysym| { + if mods.alt + && keysym.modified_sym() == smithay::input::keyboard::keysyms::KEY_F4.into() + { + FilterResult::Intercept(key_state == smithay::backend::input::KeyState::Pressed) + } else { + FilterResult::Forward + } + }, ); + if close == Some(true) { + state.close_focused_window(); + } } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index 4e5f319..7bfd476 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -58,6 +58,31 @@ impl WeftCompositorState { } } + /// Asks the focused application window to close. Its client decides + /// how to end; for an app shell that ends the session. The panel is not + /// closed this way. + pub fn close_focused_window(&mut self) { + let Some(focus) = self + .seat + .get_keyboard() + .and_then(|keyboard| keyboard.current_focus()) + else { + return; + }; + if self.is_panel_surface(&focus) { + return; + } + let window = self + .space + .elements() + .find(|window| window.wl_surface().is_some_and(|s| *s == focus)) + .cloned(); + if let Some(toplevel) = window.as_ref().and_then(|w| w.toplevel()) { + tracing::info!(session = ?window.as_ref().and_then(window_session), "closing the focused window"); + toplevel.send_close(); + } + } + /// Activates the topmost window of `session_id`; returns whether the /// session has one. pub fn activate_session(&mut self, session_id: u64) -> bool { diff --git a/crates/weft-servo-shell/src/embedder.rs b/crates/weft-servo-shell/src/embedder.rs index db30713..1c22d03 100644 --- a/crates/weft-servo-shell/src/embedder.rs +++ b/crates/weft-servo-shell/src/embedder.rs @@ -346,6 +346,14 @@ impl App { } impl ApplicationHandler for App { + /// Releases Servo and the shell protocol objects while winit's Wayland + /// display is still connected; dropped later, the shell client's + /// proxies would be destroyed on a display that is gone. + fn exiting(&mut self, _event_loop: &ActiveEventLoop) { + self.shut_down(); + self.shell_client = None; + } + fn resumed(&mut self, event_loop: &ActiveEventLoop) { if self.window.is_some() { return; diff --git a/docs/architecture.md b/docs/architecture.md index 2d995d3..d850dbe 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated or opens its first window; layout changes keep the stacking order, and a window that maps without taking focus goes beneath the window in front; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated or opens its first window; layout changes keep the stacking order, and a window that maps without taking focus goes beneath the window in front; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. Alt+F4 is the compositor's: it asks the focused application window to close (never the panel), and the app shell then exits and its session ends. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index 3d69843..f1ba160 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -15,6 +15,8 @@ colour and a lighter shade on every key press. In the presented pixels: - clicking the taskbar strip brings the panel, the shell's home, over the applications, and activating an application brings it back; - when the focused application ends, the remaining one gets keyboard focus; +- Alt+F4 asks the focused application window to close; its app shell exits + cleanly and the session ends; Alt+F4 does not close the shell's panel; - ACTIVATE_APP for a session that is not running is refused. The applications are unsigned test packages, recorded as development content @@ -209,6 +211,29 @@ def run(args, desktop, store, home): key("a") shown(red) + # Alt+F4 closes the focused application window, which ends its session; + # with the panel focused, it closes nothing. + key("alt+F4") + try: + appd.wait_for(lambda m: m.get("type") == "APP_STATE" and m.get("state") == "stopped" + and m.get("session_id") == sessions[red], 20) + except TimeoutError: + raise AssertionError("Alt+F4 did not end the focused application's session") + # The app shell ended by closing, with exit status 0, not by a crash. + clean = (f'stopping session session_id={sessions[red]} ' + 'reason="app shell exited (Ok(ExitStatus(unix_wait_status(0))))"') + if clean not in desktop.log_text("appd"): + raise AssertionError("the closed app shell did not exit cleanly; see appd.log") + deadline = time.monotonic() + 20 + while any(area(desktop.capture(), c)[0] is not None for cs in APPS.values() for c in cs): + if time.monotonic() > deadline: + raise AssertionError("the closed application is still shown") + time.sleep(0.3) + key("alt+F4") + time.sleep(2) + if area(desktop.capture(), GREEN)[0] != panel: + raise AssertionError("Alt+F4 closed or moved the shell's panel") + appd.send({"type": "ACTIVATE_APP", "session_id": 999}) reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10) if reply.get("code") != 404: