diff --git a/crates/weft-compositor/src/input.rs b/crates/weft-compositor/src/input.rs index e5e1721..28069b0 100644 --- a/crates/weft-compositor/src/input.rs +++ b/crates/weft-compositor/src/input.rs @@ -6,7 +6,7 @@ use smithay::{ TouchEvent, }, input::{ - keyboard::FilterResult, + keyboard::{FilterResult, KeyboardTarget}, pointer::{AxisFrame, ButtonEvent, MotionEvent}, }, utils::{Logical, Point, SERIAL_COUNTER}, @@ -42,41 +42,97 @@ pub fn process_input_event(state: &mut WeftCompositorState, eve } } +/// What the compositor does with a key instead of forwarding it. +enum Shortcut { + /// Alt+F4: close the focused application window. + Close, + /// A Super key tap: show the shell. + Shell, + /// Nothing: a suppressed key. + None, +} + fn handle_keyboard(state: &mut WeftCompositorState, event: B::KeyboardKeyEvent) { + use smithay::backend::input::KeyState; + use smithay::input::keyboard::keysyms; + let serial = SERIAL_COUNTER.next_serial(); let time = event.time_msec(); let key_state = event.state(); + let Some(keyboard) = state.seat.get_keyboard() else { + return; + }; - if let Some(keyboard) = state.seat.get_keyboard() { - // 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| { - // A release is taken only when its press was, so no client - // sees half of a key. - let code = keysym.raw_code().raw(); - if key_state == smithay::backend::input::KeyState::Pressed { - if mods.alt - && keysym.modified_sym() == smithay::input::keyboard::keysyms::KEY_F4.into() - { - state.suppressed_keys.push(code); - return FilterResult::Intercept(true); - } - } else if let Some(i) = state.suppressed_keys.iter().position(|&k| k == code) { - state.suppressed_keys.remove(i); - return FilterResult::Intercept(false); + // Alt+F4 and the Super keys belong to the compositor. Alt+F4 asks the + // focused application window to close. Super's key events are never + // delivered to an application: tapped alone, without another key or a + // button, it brings the shell's panel to the front with keyboard focus + // and is then delivered to the shell, which opens or closes its + // launcher. Held, it still sets the Super modifier for other keys. + let (filtered, mods_changed) = keyboard.input_intercept::<_, _>( + state, + event.key_code(), + key_state, + |state, mods, keysym| { + // A release is taken only when its press was, so no client + // sees half of a key. + let code = keysym.raw_code().raw(); + let sym = keysym.modified_sym(); + if key_state == KeyState::Pressed { + if mods.alt && sym == keysyms::KEY_F4.into() { + state.super_tap = None; + state.suppressed_keys.push(code); + return FilterResult::Intercept(Shortcut::Close); } - FilterResult::Forward - }, - ); - if close == Some(true) { - state.close_focused_window(); + if sym == keysyms::KEY_Super_L.into() || sym == keysyms::KEY_Super_R.into() { + state.super_tap = (!mods.ctrl && !mods.alt && !mods.shift).then_some(code); + state.suppressed_keys.push(code); + return FilterResult::Intercept(Shortcut::None); + } + state.super_tap = None; + } else if let Some(i) = state.suppressed_keys.iter().position(|&k| k == code) { + state.suppressed_keys.remove(i); + if state.super_tap.take() == Some(code) { + return FilterResult::Intercept(Shortcut::Shell); + } + return FilterResult::Intercept(Shortcut::None); + } + FilterResult::Forward + }, + ); + let shortcut = match filtered { + FilterResult::Forward => { + keyboard.input_forward( + state, + event.key_code(), + key_state, + serial, + time, + mods_changed, + ); + return; } + FilterResult::Intercept(shortcut) => shortcut, + }; + // The focused client follows the modifiers even when the key that + // changed them is taken, so none stays held there. + if mods_changed && let Some(focus) = keyboard.current_focus() { + let seat = state.seat.clone(); + focus.modifiers(&seat, state, keyboard.modifier_state(), serial); + } + match shortcut { + Shortcut::Close => state.close_focused_window(), + Shortcut::Shell => { + if state.activate_panel() { + // The tap itself, for the shell; the keyboard's own state + // already saw it. + let code = event.key_code(); + keyboard.input_forward(state, code, KeyState::Pressed, serial, time, false); + let release = SERIAL_COUNTER.next_serial(); + keyboard.input_forward(state, code, KeyState::Released, release, time, false); + } + } + Shortcut::None => {} } } @@ -144,8 +200,10 @@ fn handle_pointer_button( let button_state = event.state(); // On press: activate the window under the pointer, which raises an - // application window and focuses it. + // application window and focuses it. A held Super key is then no + // longer a tap. if button_state == ButtonState::Pressed { + state.super_tap = None; let pointer_location = state.pointer_location; let window = state .space @@ -221,6 +279,7 @@ fn handle_pointer_axis( } fn handle_touch_down(state: &mut WeftCompositorState, event: B::TouchDownEvent) { + state.super_tap = None; let serial = SERIAL_COUNTER.next_serial(); let output = state.space.outputs().next().cloned(); if let Some(output) = output { diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 79b701c..9eb8ba5 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -101,6 +101,9 @@ pub struct WeftCompositorState { /// Keycodes whose press a compositor shortcut took, so their release is /// taken as well and no client sees half of a key. pub suppressed_keys: Vec, + /// The Super key held down while no other key was pressed since; its + /// release is a tap that opens the shell. + pub super_tap: Option, pub compositor_state: CompositorState, pub xdg_shell_state: XdgShellState, @@ -200,6 +203,7 @@ impl WeftCompositorState { running: true, gesture_state: GestureState::default(), suppressed_keys: Vec::new(), + super_tap: None, #[cfg(unix)] appd_ipc: None, #[cfg(target_os = "linux")] diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index 2d7b527..ff0b306 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -4,13 +4,13 @@ //! panel fills the output and starts beneath every application window; //! application windows fill the work area, the output less the strips the //! panel reserves. A session's first window, a clicked window (the panel -//! included, which then shows the shell's home over the applications) and -//! one appd asks to activate is raised to the top, marked activated and -//! given keyboard focus; apart from a panel being lowered when it -//! registers, nothing else changes the order, and a window that maps -//! without taking focus goes beneath the window in front. When the -//! focused window closes, the topmost remaining application window gets -//! focus. +//! included, which then shows the shell's home over the applications), one +//! appd asks to activate and the panel when a Super key is tapped are +//! raised to the top, marked activated and given keyboard focus; apart +//! from a panel being lowered when it registers, nothing else changes the +//! order, and a window that maps without taking focus goes beneath the +//! window in front. When the focused window closes, the topmost remaining +//! application window gets focus. use smithay::desktop::Window; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; @@ -58,6 +58,31 @@ impl WeftCompositorState { } } + /// Activates the shell's panel; returns whether one is registered. With + /// several, the first registered is the shell's. + pub fn activate_panel(&mut self) -> bool { + let panel = self + .weft_shell_state + .panels() + .filter(|p| p.is_alive()) + .find_map(|panel| { + let surface = panel + .data::() + .and_then(|data| data.surface.clone())?; + self.space + .elements() + .find(|window| window.wl_surface().is_some_and(|s| *s == surface)) + .cloned() + }); + match panel { + Some(panel) => { + self.activate_window(&panel); + true + } + None => false, + } + } + /// 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. diff --git a/docs/security.md b/docs/security.md index d9c2bb3..b65315c 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; close is not yet decided by the compositor per session; there is no user placement or resizing, since application windows fill the work area the panel leaves, one in front at a time; only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; close is not yet decided by the compositor per session; there is no user placement or resizing, since application windows fill the work area the panel leaves, one in front at a time; only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. Such a client registered as a panel would also be raised and focused by a Super tap; the compositor picks the first panel registered. ## JavaScript Engine (SpiderMonkey)