From fc3d54d9dcb62ccf781c793cc21faa5341c062aa Mon Sep 17 00:00:00 2001 From: Justin Spedding Date: Wed, 23 Sep 2026 19:45:58 -0400 Subject: [PATCH] input: use an enum for the keyboard intercept result --- src/input/mod.rs | 75 +++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/src/input/mod.rs b/src/input/mod.rs index 10c1d02c..8129ec69 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -19,9 +19,7 @@ use smithay::backend::input::{ }; use smithay::backend::libinput::LibinputInputBackend; use smithay::input::dnd::DnDGrab; -use smithay::input::keyboard::{ - keysyms, FilterResult, KeyboardSource, Keysym, Layout, ModifiersState, -}; +use smithay::input::keyboard::{keysyms, KeyboardSource, Keysym, Layout, ModifiersState}; use smithay::input::pointer::{ AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, Focus, GestureHoldBeginEvent, GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, @@ -134,6 +132,17 @@ impl AnyStartData { } } +/// What to do with a key event that the keyboard filter looked at. +#[derive(Debug)] +enum ShouldInterceptResult { + /// Forward the event to the focused client. + Forward, + /// Do not forward the event, and handle the given bind. + InterceptAndHandle(Bind), + /// Do not forward the event, and do nothing else. + InterceptOnly, +} + impl State { pub fn process_input_event(&mut self, event: InputEvent) where @@ -490,13 +499,15 @@ impl State { #[cfg(not(feature = "dbus"))] let _ = consumed_by_a11y; - let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input_from_source( + // We can't use smithay's `input` here because we want to forward _and_ handle + // events for modifier keys. This lets us do things like bind Control_L to an + // action without confusing applications about its state. + let keyboard = self.niri.seat.get_keyboard().unwrap(); + let Some((result, mods_changed)) = keyboard.input_intercept( source, self, event.key_code(), event.state(), - serial, - time, |this, mods, keysym| { let key_code = event.key_code(); let modified = keysym.modified_sym(); @@ -535,9 +546,9 @@ impl State { | Keysym::Alt_R ) { - return FilterResult::Forward; + return ShouldInterceptResult::Forward; } else { - return FilterResult::Intercept(None); + return ShouldInterceptResult::InterceptOnly; } } @@ -549,7 +560,7 @@ impl State { // Don't send this press to any clients. this.niri.suppressed_keys.insert(key_code); - return FilterResult::Intercept(None); + return ShouldInterceptResult::InterceptOnly; } // Check if all modifiers were released while the MRU UI was open. If so, close the @@ -558,9 +569,9 @@ impl State { this.do_action(Action::MruConfirm, false); if this.niri.suppressed_keys.remove(&key_code) { - return FilterResult::Intercept(None); + return ShouldInterceptResult::InterceptOnly; } else { - return FilterResult::Forward; + return ShouldInterceptResult::Forward; } } @@ -573,7 +584,7 @@ impl State { { pointer.unset_grab(this, serial, time); this.niri.suppressed_keys.insert(key_code); - return FilterResult::Intercept(None); + return ShouldInterceptResult::InterceptOnly; } } @@ -601,13 +612,13 @@ impl State { ) }; - if matches!(res, FilterResult::Forward) { + if matches!(res, ShouldInterceptResult::Forward) { // If we didn't find any bind, try other hardcoded keys. if this.niri.keyboard_focus.is_overview() && pressed { if let Some(bind) = raw.and_then(|raw| hardcoded_overview_bind(raw, *mods)) { this.niri.suppressed_keys.insert(key_code); - return FilterResult::Intercept(Some(bind)); + return ShouldInterceptResult::InterceptAndHandle(bind); } } @@ -622,13 +633,25 @@ impl State { return; }; - if !pressed { - return; + match result { + ShouldInterceptResult::Forward => { + keyboard.input_forward( + self, + event.key_code(), + event.state(), + serial, + time, + mods_changed, + ); + } + ShouldInterceptResult::InterceptAndHandle(bind) => { + if pressed { + self.handle_bind(bind.clone()); + self.start_key_repeat(bind); + } + } + ShouldInterceptResult::InterceptOnly => {} } - - self.handle_bind(bind.clone()); - - self.start_key_repeat(bind); } fn start_key_repeat(&mut self, bind: Bind) { @@ -4610,12 +4633,12 @@ fn should_intercept_key<'a>( screenshot_ui: &ScreenshotUi, disable_power_key_handling: bool, is_inhibiting_shortcuts: bool, -) -> FilterResult> { +) -> ShouldInterceptResult { // Actions are only triggered on presses, release of the key // shouldn't try to intercept anything unless we have marked // the key to suppress. if !pressed && !suppressed_keys.contains(&key_code) { - return FilterResult::Forward; + return ShouldInterceptResult::Forward; } let mut final_bind = find_bind( @@ -4663,10 +4686,10 @@ fn should_intercept_key<'a>( match (final_bind, pressed) { (Some(bind), true) => { if is_inhibiting_shortcuts && bind.allow_inhibiting { - FilterResult::Forward + ShouldInterceptResult::Forward } else { suppressed_keys.insert(key_code); - FilterResult::Intercept(Some(bind)) + ShouldInterceptResult::InterceptAndHandle(bind) } } (_, false) => { @@ -4676,9 +4699,9 @@ fn should_intercept_key<'a>( // if it was inhibited on press (forwarded to the client), it wouldn't be suppressed, // so the release would already have been forwarded at the start of this function. suppressed_keys.remove(&key_code); - FilterResult::Intercept(None) + ShouldInterceptResult::InterceptOnly } - (None, true) => FilterResult::Forward, + (None, true) => ShouldInterceptResult::Forward, } }