diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 40eccb55..378da877 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -626,6 +626,7 @@ impl ConfigPath { mod tests { use insta::{assert_debug_snapshot, assert_snapshot}; use pretty_assertions::assert_eq; + use smithay::input::keyboard::Keysym; use super::*; @@ -648,6 +649,445 @@ mod tests { .unwrap() } + #[test] + fn parse_release_binds_for_supported_triggers() { + // Keys and mouse buttons have release events, so release sections are supported. + do_parse( + r#" + binds { + Mod+MouseLeft { + release { close-window; } + } + Mod+Q { + press { close-window; } + release { close-window; } + } + } + "#, + ); + } + + #[test] + fn parse_release_binds_for_unsupported_triggers() { + // Scroll ticks are dispatched as press events only, so scroll binds can't have a release + // action. + assert_parse_error( + r#" + binds { + Mod+WheelScrollDown { + release { toggle-overview; } + } + } + "#, + "release sections are not supported for scroll binds", + ); + assert_parse_error( + r#" + binds { + Mod+TouchpadScrollDown { + release { toggle-overview; } + } + } + "#, + "release sections are not supported for scroll binds", + ); + } + + #[test] + fn parse_bind_repeat_on_release_only() { + // A release-only bind has no press action to repeat, so repeat=true is rejected. + assert_parse_error( + r#" + binds { + Mod+Q repeat=true { + release { close-window; } + } + } + "#, + "release-only binds cannot repeat", + ); + + do_parse( + r#" + binds { + Mod+T repeat=true { spawn "alacritty"; } + Mod+Q repeat=true { + press { close-window; } + release { close-window; } + } + Mod+W { + release { close-window; } + } + Mod+E repeat=false { + release { close-window; } + } + } + "#, + ); + } + + /// Asserts that parsing fails with a diagnostic containing `message`. + #[track_caller] + fn assert_parse_error(text: &str, message: &str) { + let err = match Config::parse_mem(text) { + Ok(_) => panic!("expected a parse error for:\n{text}"), + Err(err) => err, + }; + + // knuffel keeps the individual diagnostics in a private field, so check the Debug + // representation, which includes their messages. + let err = format!("{err:?}"); + assert!( + err.contains(message), + "expected {message:?} in parse error:\n{err}" + ); + } + + #[test] + fn parse_bind_section_errors() { + // A direct action node and a `press` section both specify the press action, so mixing them + // is reported as mixing the two syntaxes rather than as a duplicate `press` section. + assert_parse_error( + r#" + binds { + Mod+Q { + close-window; + press { center-column; } + } + } + "#, + "cannot mix direct actions with press/release sections", + ); + + // A second direct action is a duplicate action, not a mix of the two syntaxes. + assert_parse_error( + r#" + binds { + Mod+Q { + close-window; + center-column; + } + } + "#, + "only one action is allowed per keybind", + ); + + // A direct action following a `press` section is mixing the two syntaxes. + assert_parse_error( + r#" + binds { + Mod+Q { + press { close-window; } + center-column; + } + } + "#, + "cannot mix direct actions with press/release sections", + ); + + // Two `press` sections are still duplicates. + assert_parse_error( + r#" + binds { + Mod+Q { + press { close-window; } + press { center-column; } + } + } + "#, + "duplicate `press` section", + ); + + assert_parse_error( + r#" + binds { + Mod+Q { + center-column; + release { close-window; } + } + } + "#, + "cannot mix direct actions with press/release sections", + ); + + assert_parse_error( + r#" + binds { + Mod+Q { + release { close-window; } + center-column; + } + } + "#, + "cannot mix direct actions with press/release sections", + ); + } + + #[test] + fn parse_bind_bound_action() { + let config = do_parse( + r#" + binds { + Mod+A { close-window; } + Mod+B { + press { close-window; } + } + Mod+C { + release { toggle-overview; } + } + Mod+D { + press { close-window; } + release { toggle-overview; } + } + } + "#, + ); + + let action = |index: usize| &config.binds.0[index].action; + + // A direct action and a press section both bind only the press phase. + assert!(matches!(action(0), BoundAction::Press(Action::CloseWindow))); + assert!(matches!(action(1), BoundAction::Press(Action::CloseWindow))); + + // A release section binds only the release phase. + assert!(matches!( + action(2), + BoundAction::Release(Action::ToggleOverview) + )); + + // Both sections bind both phases. + assert!(matches!( + action(3), + BoundAction::Both { + press: Action::CloseWindow, + release: Action::ToggleOverview, + } + )); + } + + #[test] + fn parse_bare_modifier_keys() { + let config = do_parse( + r#" + binds { + Mod { + release { toggle-overview; } + } + Ctrl { + release { close-window; } + } + Shift { + release { close-window; } + } + Alt { + release { close-window; } + } + Super { + release { close-window; } + } + Mod5 { + release { close-window; } + } + Mod3 { + release { close-window; } + } + + Ctrl+Alt_L { + release { close-window; } + } + } + "#, + ); + + let triggers = config + .binds + .0 + .iter() + .map(|bind| bind.key.trigger) + .collect::>(); + + assert_eq!( + triggers, + vec![ + Trigger::CompositorMod, + Trigger::Modifier(ModKey::Ctrl), + Trigger::Modifier(ModKey::Shift), + Trigger::Modifier(ModKey::Alt), + Trigger::Modifier(ModKey::Super), + Trigger::Modifier(ModKey::IsoLevel3Shift), + Trigger::Modifier(ModKey::IsoLevel5Shift), + Trigger::Keysym(Keysym::Alt_L), + ] + ); + + // Ctrl+Alt_L has modifiers of Ctrl (manually specified) and Alt (implied by the keysym). + assert_eq!( + config.binds.0[7].key.modifiers, + Modifiers::CTRL | Modifiers::ALT + ); + } + + #[test] + fn parse_bind_conflicting_modifier_keys() { + // A modifier key can be spelled as the modifier itself (`Alt`) or as its keysym (`Alt_L`), + // and a modifier key is triggered by either of its keysyms, so one of these two binds + // would always shadow the other. + assert_parse_error( + r#" + binds { + Alt { + release { toggle-overview; } + } + Alt_L { + release { close-window; } + } + } + "#, + "conflicting keybind later defined here", + ); + assert_parse_error( + r#" + recent-windows { + binds { + Alt+Control_L { next-window; } + Alt+Ctrl { next-window; } + } + } + "#, + "conflicting keybind later defined here", + ); + + // The left and right keysyms are different keys, so those can be bound separately. + do_parse( + r#" + binds { + Alt_L { + release { close-window; } + } + Alt_R { + release { center-column; } + } + } + "#, + ); + + // A modifier keysym on its own is an ordinary modifier-only bind. + let config = do_parse( + r#" + binds { + Alt_L { + release { close-window; } + } + } + "#, + ); + assert!(config.binds.0[0].key.trigger.is_modifier()); + } + + #[test] + fn parse_recent_windows_bind_modifier() { + // Modifier-only binds are allowed. + do_parse( + r#" + recent-windows { + binds { + Ctrl+Alt_L { next-window; } + Ctrl+Shift { next-window; } + } + } + "#, + ); + + // Binds must have an extra modifier. + assert_parse_error( + r#" + recent-windows { + binds { + Alt_L { next-window; } + } + } + "#, + "keybind must have a modifier key", + ); + assert_parse_error( + r#" + recent-windows { + binds { + Alt+Alt_L { next-window; } + } + } + "#, + "keybind must have a modifier key", + ); + + // A trigger with no modifier at all is rejected. + assert_parse_error( + r#" + recent-windows { + binds { + Tab { next-window; } + } + } + "#, + "keybind must have a modifier key", + ); + + // Only keyboard keys are supported here for now. + assert_parse_error( + r#" + recent-windows { + binds { + Alt+MouseLeft { next-window; } + } + } + "#, + "key must be a keyboard key", + ); + } + + #[test] + fn parse_bind_redundant_modifiers_are_the_same_bind() { + // Redundant modifiers are normalized, so these should error as duplicates. + assert_parse_error( + r#" + binds { + Alt_L { close-window; } + Alt+Alt_L { center-column; } + } + "#, + "duplicate keybind later defined here", + ); + assert_parse_error( + r#" + binds { + Alt { close-window; } + Alt+Alt { center-column; } + } + "#, + "duplicate keybind later defined here", + ); + assert_parse_error( + r#" + binds { + Mod { close-window; } + Mod+Mod { center-column; } + } + "#, + "duplicate keybind later defined here", + ); + assert_parse_error( + r#" + recent-windows { + binds { + Alt+Control_L { next-window; } + Alt+Ctrl+Control_L { next-window; } + } + } + "#, + "duplicate keybind later defined here", + ); + } + #[test] fn parse_on_xdg_activate() { let parsed = do_parse( diff --git a/src/input/mod.rs b/src/input/mod.rs index a4be54b7..03d9fbd9 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -5640,82 +5640,150 @@ fn make_binds_iter<'a>( #[cfg(test)] mod tests { use super::*; + use crate::animation::Clock; + + const CLOSE_KEYSYM: Keysym = Keysym::q; + const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw()); + struct TestState { + screenshot_ui: ScreenshotUi, + disable_power_key_handling: bool, + is_inhibiting: bool, + is_locked: bool, + suppressed_keys: HashSet, + pending_release_binds: HashMap, + cooldown_timers: HashMap, + } + + fn create_test_state() -> TestState { + TestState { + screenshot_ui: ScreenshotUi::new(Clock::default(), Default::default()), + disable_power_key_handling: false, + is_inhibiting: false, + is_locked: false, + suppressed_keys: HashSet::new(), + pending_release_binds: HashMap::new(), + cooldown_timers: HashMap::new(), + } + } + + /// Returns a bind with the given trigger, modifiers, and action, and default flags. + fn bind(trigger: Trigger, modifiers: Modifiers, action: BoundAction) -> Bind { + Bind { + key: Key { trigger, modifiers }, + action, + repeat: false, + cooldown: None, + allow_when_locked: false, + allow_inhibiting: true, + hotkey_overlay_title: None, + } + } + + /// Returns a `Mod+Q` bind with the given action and default flags. + fn close_bind(action: BoundAction) -> Bind { + bind(Trigger::Keysym(CLOSE_KEYSYM), Modifiers::COMPOSITOR, action) + } + + fn mod_held() -> ModifiersState { + ModifiersState { + logo: true, + ..Default::default() + } + } + + /// Processes a press of the `Mod+Q` bind that the tests are written with. + fn press_close_key( + state: &mut TestState, + bindings: &Binds, + mods: ModifiersState, + ) -> ShouldInterceptResult { + process_close_key(state, bindings, mods, KeyState::Pressed) + } + + /// Processes a release of the `Mod+Q` bind that the tests are written with. + fn release_close_key( + state: &mut TestState, + bindings: &Binds, + mods: ModifiersState, + ) -> ShouldInterceptResult { + process_close_key(state, bindings, mods, KeyState::Released) + } + + fn process_close_key( + state: &mut TestState, + bindings: &Binds, + mods: ModifiersState, + key_state: KeyState, + ) -> ShouldInterceptResult { + let pressed = key_state == KeyState::Pressed; + + if pressed { + retain_press_binds(&mut state.pending_release_binds); + } + + should_intercept_key( + &mut state.suppressed_keys, + &mut state.pending_release_binds, + &state.cooldown_timers, + &bindings.0, + ModKey::Super, + CLOSE_KEY_CODE, + CLOSE_KEYSYM, + Some(CLOSE_KEYSYM), + pressed, + mods, + &state.screenshot_ui, + state.disable_power_key_handling, + state.is_inhibiting, + state.is_locked, + ) + } + + macro_rules! assert_matches { + ($expression:expr, $pattern:pat) => { + let value = $expression; + assert!( + matches!(value, $pattern), + "Expected {:?} to match {}", + value, + stringify!($pattern) + ); + }; + } #[test] fn comp_mod_handling() { let bindings = Binds(vec![ - Bind { - key: Key { - trigger: Trigger::Keysym(Keysym::q), - modifiers: Modifiers::COMPOSITOR, - }, - action: BoundAction::Press(Action::CloseWindow), - repeat: true, - cooldown: None, - allow_when_locked: false, - allow_inhibiting: true, - hotkey_overlay_title: None, - }, - Bind { - key: Key { - trigger: Trigger::Keysym(Keysym::h), - modifiers: Modifiers::SUPER, - }, - action: BoundAction::Press(Action::FocusColumnLeft), - repeat: true, - cooldown: None, - allow_when_locked: false, - allow_inhibiting: true, - hotkey_overlay_title: None, - }, - Bind { - key: Key { - trigger: Trigger::Keysym(Keysym::j), - modifiers: Modifiers::empty(), - }, - action: BoundAction::Press(Action::FocusWindowDown), - repeat: true, - cooldown: None, - allow_when_locked: false, - allow_inhibiting: true, - hotkey_overlay_title: None, - }, - Bind { - key: Key { - trigger: Trigger::Keysym(Keysym::k), - modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, - }, - action: BoundAction::Press(Action::FocusWindowUp), - repeat: true, - cooldown: None, - allow_when_locked: false, - allow_inhibiting: true, - hotkey_overlay_title: None, - }, - Bind { - key: Key { - trigger: Trigger::Keysym(Keysym::l), - modifiers: Modifiers::SUPER | Modifiers::ALT, - }, - action: BoundAction::Press(Action::FocusColumnRight), - repeat: true, - cooldown: None, - allow_when_locked: false, - allow_inhibiting: true, - hotkey_overlay_title: None, - }, - Bind { - key: Key { - trigger: Trigger::Keysym(Keysym::Super_L), - modifiers: Modifiers::empty(), - }, - action: BoundAction::Release(Action::ToggleOverview), - repeat: false, - cooldown: None, - allow_when_locked: false, - allow_inhibiting: true, - hotkey_overlay_title: None, - }, + bind( + Trigger::Keysym(Keysym::q), + Modifiers::COMPOSITOR, + BoundAction::Press(Action::CloseWindow), + ), + bind( + Trigger::Keysym(Keysym::h), + Modifiers::SUPER, + BoundAction::Press(Action::FocusColumnLeft), + ), + bind( + Trigger::Keysym(Keysym::j), + Modifiers::empty(), + BoundAction::Press(Action::FocusWindowDown), + ), + bind( + Trigger::Keysym(Keysym::k), + Modifiers::COMPOSITOR | Modifiers::SUPER, + BoundAction::Press(Action::FocusWindowUp), + ), + bind( + Trigger::Keysym(Keysym::l), + Modifiers::SUPER | Modifiers::ALT, + BoundAction::Press(Action::FocusColumnRight), + ), + bind( + Trigger::Keysym(Keysym::Super_L), + Modifiers::empty(), + BoundAction::Release(Action::ToggleOverview), + ), ]); assert_eq!( @@ -5850,4 +5918,248 @@ mod tests { Some(&bindings.0[5]) ); } + + #[test] + fn bind_allowed_when_locked_requires_both_actions_of_a_both_bind() { + // A press could start something that its release would never undo, so both actions must be + // allowed while locked. + let both = |press, release| close_bind(BoundAction::Both { press, release }); + assert!(!bind_allowed_when_locked( + &both(Action::Suspend, Action::CloseWindow), + true + )); + assert!(!bind_allowed_when_locked( + &both(Action::CloseWindow, Action::Suspend), + true + )); + assert!(bind_allowed_when_locked( + &both(Action::Suspend, Action::PowerOffMonitors), + true + )); + } + + #[test] + fn bind_allowed_when_locked_requires_only_the_single_action() { + assert!(bind_allowed_when_locked( + &close_bind(BoundAction::Press(Action::Suspend)), + true + )); + assert!(!bind_allowed_when_locked( + &close_bind(BoundAction::Press(Action::CloseWindow)), + true + )); + assert!(bind_allowed_when_locked( + &close_bind(BoundAction::Release(Action::Suspend)), + false + )); + assert!(!bind_allowed_when_locked( + &close_bind(BoundAction::Release(Action::CloseWindow)), + false + )); + } + + #[test] + fn bind_allowed_when_locked_always_allows_a_both_bind_release() { + // The release only happens if its press ran, and the press required both actions to be + // allowed, so the release is never blocked separately. + let both = close_bind(BoundAction::Both { + press: Action::Suspend, + release: Action::CloseWindow, + }); + assert!(bind_allowed_when_locked(&both, false)); + } + + #[test] + fn allow_when_locked_allows_the_whole_bind() { + let bind = |action| Bind { + allow_when_locked: true, + ..close_bind(action) + }; + assert!(bind_allowed_when_locked( + &bind(BoundAction::Press(Action::CloseWindow)), + true + )); + assert!(bind_allowed_when_locked( + &bind(BoundAction::Release(Action::CloseWindow)), + false + )); + + let both = bind(BoundAction::Both { + press: Action::CloseWindow, + release: Action::CenterColumn, + }); + assert!(bind_allowed_when_locked(&both, true)); + assert!(bind_allowed_when_locked(&both, false)); + } + + #[test] + fn locked_both_bind_does_not_run_its_press_when_its_release_is_not_allowed() { + // `Mod+Q { press { suspend; } release { close-window; } }`: the press is allowed while + // locked but the release isn't, so the press must not run, since nothing could undo it. + let bindings = Binds(vec![close_bind(BoundAction::Both { + press: Action::Suspend, + release: Action::CloseWindow, + })]); + + let mut state = TestState { + is_locked: true, + ..create_test_state() + }; + let result = press_close_key(&mut state, &bindings, mod_held()); + + // `handle_bind()` drops the press while locked, and nothing is recorded to undo. + assert_matches!(result, ShouldInterceptResult::InterceptAndHandle(_)); + assert!(state.pending_release_binds.is_empty()); + + let result = release_close_key(&mut state, &bindings, mod_held()); + assert_matches!(result, ShouldInterceptResult::InterceptOnly); + } + + #[test] + fn both_bind_release_recorded_while_unlocked_runs_while_locked() { + // Its press action ran while unlocked, so the release must run later even if the screen got + // locked in between. + let bindings = Binds(vec![close_bind(BoundAction::Both { + press: Action::Suspend, + release: Action::CloseWindow, + })]); + + let mut state = create_test_state(); + let result = press_close_key(&mut state, &bindings, mod_held()); + assert_matches!(result, ShouldInterceptResult::InterceptAndHandle(_)); + assert!(state.pending_release_binds.contains_key(&CLOSE_KEY_CODE)); + + state.is_locked = true; + let result = release_close_key(&mut state, &bindings, mod_held()); + assert_matches!( + result, + ShouldInterceptResult::InterceptAndHandle(Bind { + action: BoundAction::Both { + release: Action::CloseWindow, + .. + }, + .. + }) + ); + assert!(state.pending_release_binds.is_empty()); + } + + #[test] + fn release_only_bind_is_recorded_on_press_and_runs_on_release() { + let bindings = Binds(vec![close_bind(BoundAction::Release(Action::CloseWindow))]); + + let mut state = create_test_state(); + let result = press_close_key(&mut state, &bindings, mod_held()); + assert_matches!(result, ShouldInterceptResult::InterceptOnly); + assert!(state.pending_release_binds.contains_key(&CLOSE_KEY_CODE)); + + let result = release_close_key(&mut state, &bindings, mod_held()); + assert_matches!( + result, + ShouldInterceptResult::InterceptAndHandle(Bind { + action: BoundAction::Release(Action::CloseWindow), + .. + }) + ); + assert!(state.pending_release_binds.is_empty()); + } + + #[test] + fn release_only_bind_allowed_while_locked_is_recorded_and_runs_while_locked() { + let bindings = Binds(vec![Bind { + allow_when_locked: true, + ..close_bind(BoundAction::Release(Action::CloseWindow)) + }]); + + let mut state = TestState { + is_locked: true, + ..create_test_state() + }; + let result = press_close_key(&mut state, &bindings, mod_held()); + assert_matches!(result, ShouldInterceptResult::InterceptOnly); + assert!(state.pending_release_binds.contains_key(&CLOSE_KEY_CODE)); + + let result = release_close_key(&mut state, &bindings, mod_held()); + assert_matches!( + result, + ShouldInterceptResult::InterceptAndHandle(Bind { + action: BoundAction::Release(Action::CloseWindow), + .. + }) + ); + assert!(state.pending_release_binds.is_empty()); + } + + #[test] + fn release_only_bind_not_allowed_while_locked_is_not_recorded_while_locked() { + let bindings = Binds(vec![close_bind(BoundAction::Release(Action::CloseWindow))]); + + let mut state = TestState { + is_locked: true, + ..create_test_state() + }; + let result = press_close_key(&mut state, &bindings, mod_held()); + assert_matches!(result, ShouldInterceptResult::InterceptOnly); + assert!(state.pending_release_binds.is_empty()); + + let result = release_close_key(&mut state, &bindings, mod_held()); + assert_matches!(result, ShouldInterceptResult::InterceptOnly); + assert!(state.pending_release_binds.is_empty()); + } + + #[test] + fn bind_allowed_during_screenshot_requires_both_actions_of_a_both_bind() { + // A press could start something that its release would never undo, so both actions must be + // allowed while the screenshot UI is open. + let both = |press, release| close_bind(BoundAction::Both { press, release }); + assert!(!bind_allowed_during_screenshot( + &both(Action::MoveColumnLeft, Action::CloseWindow), + true + )); + assert!(!bind_allowed_during_screenshot( + &both(Action::CloseWindow, Action::MoveColumnLeft), + true + )); + assert!(bind_allowed_during_screenshot( + &both(Action::MoveColumnLeft, Action::MoveWindowUp), + true + )); + } + + #[test] + fn bind_allowed_during_screenshot_requires_only_the_single_action() { + assert!(bind_allowed_during_screenshot( + &close_bind(BoundAction::Press(Action::MoveColumnLeft)), + true + )); + assert!(!bind_allowed_during_screenshot( + &close_bind(BoundAction::Press(Action::CloseWindow)), + true + )); + } + + #[test] + fn bind_allowed_during_screenshot_always_allows_a_both_bind_release() { + // The release only happens if its press ran, so the screenshot UI opening in between must + // not block it. + let both = |press, release| close_bind(BoundAction::Both { press, release }); + assert!(bind_allowed_during_screenshot( + &both(Action::MoveColumnLeft, Action::CloseWindow), + false + )); + assert!(bind_allowed_during_screenshot( + &both(Action::CloseWindow, Action::MoveColumnLeft), + false + )); + + // A release-only bind has no press to undo, so its own action must be allowed. + assert!(bind_allowed_during_screenshot( + &close_bind(BoundAction::Release(Action::MoveColumnLeft)), + false + )); + assert!(!bind_allowed_during_screenshot( + &close_bind(BoundAction::Release(Action::CloseWindow)), + false + )); + } } diff --git a/src/tests/binds.rs b/src/tests/binds.rs index 5f76de8a..4e5afe6b 100644 --- a/src/tests/binds.rs +++ b/src/tests/binds.rs @@ -4,30 +4,87 @@ use insta::assert_snapshot; use niri_config::{Action, BoundAction, Config}; use smithay::backend::input::{InputEvent, InputTime, KeyState, Keycode}; use smithay::input::keyboard::xkb::Keymap; +use wayland_client::protocol::wl_pointer; use wayland_client::protocol::wl_surface::WlSurface; use crate::tests::client::ClientId; use crate::tests::fixture::Fixture; use crate::tests::test_input_backend::{TestInputBackend, TestKeyboardKeyEvent}; +#[derive(Clone, Copy)] enum Op { Press(Keycode), Release(Keycode), + MouseButton { + name: &'static str, + code: u32, + state: wl_pointer::ButtonState, + }, + /// A scroll tick. Scrolls are momentary events, so they have no press or release. + Scroll { + name: &'static str, + source: wl_pointer::AxisSource, + /// Positive scrolls down. + amount: f64, + }, } +/// Mouse buttons that can be pressed and released, named after the binds that trigger them. +const MOUSE_BUTTONS: &[(&str, u32)] = &[ + ("MouseLeft", 0x110), + ("MouseRight", 0x111), + ("MouseMiddle", 0x112), + ("MouseBack", 0x113), + ("MouseForward", 0x114), +]; + +/// Scrolls, named after the binds that trigger them. +const SCROLLS: &[(&str, wl_pointer::AxisSource, f64)] = &[ + ("WheelScrollDown", wl_pointer::AxisSource::Wheel, 10.), + ("WheelScrollUp", wl_pointer::AxisSource::Wheel, -10.), +]; + fn parse(keymap: &Keymap, input: &str) -> Vec { let mut ops = Vec::new(); for part in input.split_ascii_whitespace() { - let name = &part[1..]; + // Scrolls are momentary, so they are written without a prefix. + let (pressed, name) = match part.as_bytes().first() { + Some(b'+') => (Some(true), &part[1..]), + Some(b'-') => (Some(false), &part[1..]), + _ => (None, part), + }; + + if let Some((name, source, amount)) = SCROLLS.iter().copied().find(|(n, _, _)| *n == name) { + assert!( + pressed.is_none(), + "scrolls have no press or release: {part}" + ); + ops.push(Op::Scroll { + name, + source, + amount, + }); + continue; + } + + if let Some((name, code)) = MOUSE_BUTTONS.iter().copied().find(|(n, _)| *n == name) { + let state = match pressed { + Some(true) => wl_pointer::ButtonState::Pressed, + Some(false) => wl_pointer::ButtonState::Released, + None => panic!("a mouse button needs a + or - prefix: {part}"), + }; + ops.push(Op::MouseButton { name, code, state }); + continue; + } + let Some(key) = keymap.key_by_name(name) else { panic!("unknown key {name}"); }; - let c = part.bytes().next().unwrap(); - let op = match c { - b'+' => Op::Press(key), - b'-' => Op::Release(key), - _ => panic!("keys must begin with + or -, got {c}"), + let op = match pressed { + Some(true) => Op::Press(key), + Some(false) => Op::Release(key), + None => panic!("a key needs a + or - prefix: {part}"), }; ops.push(op); @@ -37,16 +94,19 @@ fn parse(keymap: &Keymap, input: &str) -> Vec { fn set_up(config: &str) -> (Fixture, ClientId, WlSurface) { let mut config = Config::parse_mem(config).unwrap(); - // knuffel doesn't understand #[cfg(test)]... + for bind in &mut config.binds.0 { - bind.action = match &bind.action { - BoundAction::Press(_) => BoundAction::Press(Action::TestAction), - BoundAction::Release(_) => BoundAction::Release(Action::TestAction), - BoundAction::Both { .. } => BoundAction::Both { - press: Action::TestAction, - release: Action::TestAction, - }, + let actions = match &mut bind.action { + BoundAction::Press(press) => [Some(press), None], + BoundAction::Release(release) => [None, Some(release)], + BoundAction::Both { press, release } => [Some(press), Some(release)], }; + for action in actions.into_iter().flatten() { + // knuffel doesn't understand #[cfg(test)]... + if !matches!(action, Action::Screenshot(_, _)) { + *action = Action::TestAction; + } + } } let mut f = Fixture::with_config(config); @@ -83,46 +143,81 @@ fn run_f(f: &mut Fixture, id: ClientId, surface: &WlSurface, input: &str) -> Str } for op in ops { - let (code, key_state) = match op { - Op::Press(code) => (code, KeyState::Pressed), - Op::Release(code) => (code, KeyState::Released), - }; + let prev = f.niri().test_action_count; - let state = f.niri_state(); - let keyboard = state.niri.seat.get_keyboard().unwrap(); - keyboard.with_xkb_state(state, |xkb| { - let xkb = xkb.xkb().lock().unwrap(); - let xkb_state = unsafe { xkb.state() }; - let keymap = xkb_state.get_keymap(); + match op { + Op::Press(code) | Op::Release(code) => { + let key_state = match op { + Op::Press(_) => KeyState::Pressed, + Op::Release(_) => KeyState::Released, + _ => unreachable!(), + }; - let c = match key_state { - KeyState::Pressed => "+", - KeyState::Released => "-", - }; + let state = f.niri_state(); + let keyboard = state.niri.seat.get_keyboard().unwrap(); + keyboard.with_xkb_state(state, |xkb| { + let xkb = xkb.xkb().lock().unwrap(); + let xkb_state = unsafe { xkb.state() }; + let keymap = xkb_state.get_keymap(); - let name = keymap.key_get_name(code).unwrap_or("None"); - let keysym = xkb_state.key_get_one_sym(code); + let c = match key_state { + KeyState::Pressed => "+", + KeyState::Released => "-", + }; - let _ = writeln!(&mut rv, "{c}{name} {:>3} {keysym:?}", code.raw()); - }); + let name = keymap.key_get_name(code).unwrap_or("None"); + let keysym = xkb_state.key_get_one_sym(code); - let prev = state.niri.test_action_count; + let _ = writeln!(&mut rv, "{c}{name} {:>3} {keysym:?}", code.raw()); + }); - state.process_input_event(InputEvent::::Keyboard { - event: TestKeyboardKeyEvent { - time: InputTime::from_micros(0), + let state = f.niri_state(); + state.process_input_event(InputEvent::::Keyboard { + event: TestKeyboardKeyEvent { + time: InputTime::from_micros(0), + code, + state: key_state, + count: 1, // niri doesn't use this + }, + }); + } + Op::MouseButton { + name, code, - state: key_state, - count: 1, // niri doesn't use this - }, - }); + state: button_state, + } => { + let c = match button_state { + wl_pointer::ButtonState::Pressed => "+", + wl_pointer::ButtonState::Released => "-", + _ => unreachable!(), + }; + let _ = writeln!(&mut rv, "{c}{name}"); + + let pointer = f.client(id).state.virtual_pointer(); + pointer.button(0, code, button_state); + pointer.frame(); + } + Op::Scroll { + name, + source, + amount, + } => { + let _ = writeln!(&mut rv, "{name}"); + + let pointer = f.client(id).state.virtual_pointer(); + pointer.axis_source(source); + pointer.axis(0, wl_pointer::Axis::VerticalScroll, amount); + pointer.frame(); + } + } + + f.roundtrip(id); let diff = f.niri().test_action_count - prev; for _ in 0..diff { let _ = writeln!(&mut rv, " niri test-action"); } - f.roundtrip(id); for event in f.client(id).state.recent_keyboard_events(surface) { let _ = writeln!(&mut rv, " surface {event}"); } @@ -512,3 +607,1239 @@ fn layouts() { " ); } + +#[test] +fn release_binds() { + let c = " + binds { + Mod { + release { toggle-overview; } + } + Mod+Q { + release { close-window; } + } + Mod+U { center-column; } + Ctrl+Alt_L { + release { switch-layout \"next\"; } + } + Ctrl+Alt+U { close-window; } + } + "; + + // Releasing Mod by itself runs its release action. + assert_snapshot!(run(c, "+LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + niri test-action + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing another key in between cancels a modifier-only release bind. + assert_snapshot!(run(c, "+LWIN +LatA -LatA -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing a key that triggers its own bind cancels the pending release bind. + assert_snapshot!(run(c, "+LWIN +LatU -LatU -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD07 30 XK_u + niri test-action + -AD07 30 XK_u + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // A key that was already held when the bind was pressed doesn't cancel it: only input in + // between does. + assert_snapshot!(run(c, "+LatA +LWIN -LatA -LWIN"), @" + +AC01 38 XK_a + surface key pressed: 30 + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -AC01 38 XK_a + surface key released: 30 + -LWIN 133 XK_Super_L + niri test-action + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // A release-only bind on a regular key. + assert_snapshot!(run(c, "+LWIN +LatQ -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + -AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // The release action runs even if the modifiers are released before the trigger key. + assert_snapshot!(run(c, "+LWIN +LatQ -LWIN -LatQ"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + -AD01 24 XK_q + niri test-action + "); + + // Another key in between cancels it, no matter the trigger. + assert_snapshot!(run(c, "+LWIN +LatQ +LatA -LatA -LWIN -LatQ"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + -AD01 24 XK_q + "); + + // Ctrl+Alt_L is a modifier-only bind: its whole key consists of modifiers, so releasing the + // combo on its own runs its release action. + assert_snapshot!(run(c, "+LCTL +LALT -LALT -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=12, latched=0, locked=0, group=0 + -LALT 64 XK_Alt_L + niri test-action + surface key released: 56 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing another key in between cancels it, so using Ctrl+Alt+A as an app shortcut does not + // additionally switch the layout. + assert_snapshot!(run(c, "+LCTL +LALT +LatA -LatA -LALT -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=12, latched=0, locked=0, group=0 + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + -LALT 64 XK_Alt_L + surface key released: 56 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing a key that triggers its own bind cancels the Ctrl+Alt_L release bind. + assert_snapshot!(run(c, "+LCTL +LALT +LatU -LatU -LALT -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=12, latched=0, locked=0, group=0 + +AD07 30 XK_u + niri test-action + -AD07 30 XK_u + -LALT 64 XK_Alt_L + surface key released: 56 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn press_and_release_binds() { + let c = " + binds { + Mod+Q { + press { close-window; } + release { center-column; } + } + } + "; + + // Both actions run, and the key never reaches the surface. + assert_snapshot!(run(c, "+LWIN +LatQ -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + niri test-action + -AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // The release action runs even if the modifiers are released first. + assert_snapshot!(run(c, "+LWIN +LatQ -LWIN -LatQ"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + -AD01 24 XK_q + niri test-action + "); + + // Extra modifiers held at release don't stop the release action either. + assert_snapshot!(run(c, "+LWIN +LatQ +LCTL +LFSH -LatQ -LFSH -LCTL -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + niri test-action + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=68, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=69, latched=0, locked=0, group=0 + -AD01 24 XK_Q + niri test-action + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=68, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Push-to-talk: other input in between doesn't cancel the release action. + assert_snapshot!(run(c, "+LWIN +LatQ +LatA -LatA -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + niri test-action + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + -AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing the key without the mod key held doesn't run either action. + assert_snapshot!(run(c, "+LatQ +LWIN -LatQ -LWIN"), @" + +AD01 24 XK_q + surface key pressed: 16 + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -AD01 24 XK_q + surface key released: 16 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn generic_modifier_press_release() { + let c = " + binds { + Super+Shift { + press { close-window; } + release { center-column; } + } + + Ctrl+Shift { + release { close-window; } + } + } + "; + + // Press Super and then two Shifts. + assert_snapshot!(run(c, "+LWIN +LFSH +RTSH -RTSH -LFSH -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + niri test-action + surface modifiers: depressed=65, latched=0, locked=0, group=0 + +RTSH 62 XK_Shift_R + niri test-action + -RTSH 62 XK_Shift_R + niri test-action + -LFSH 50 XK_Shift_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Other release order. + assert_snapshot!(run(c, "+LWIN +LFSH +RTSH -LFSH -RTSH -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + niri test-action + surface modifiers: depressed=65, latched=0, locked=0, group=0 + +RTSH 62 XK_Shift_R + niri test-action + -LFSH 50 XK_Shift_L + niri test-action + -RTSH 62 XK_Shift_R + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Same but release-only. + assert_snapshot!(run(c, "+LCTL +LFSH +RTSH -RTSH -LFSH -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=5, latched=0, locked=0, group=0 + +RTSH 62 XK_Shift_R + surface key pressed: 54 + -RTSH 62 XK_Shift_R + niri test-action + surface key released: 54 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Other release order (release-only). + assert_snapshot!(run(c, "+LCTL +LFSH +RTSH -LFSH -RTSH -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=5, latched=0, locked=0, group=0 + +RTSH 62 XK_Shift_R + surface key pressed: 54 + -LFSH 50 XK_Shift_L + surface key released: 42 + -RTSH 62 XK_Shift_R + niri test-action + surface key released: 54 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Now, Shift first. + assert_snapshot!(run(c, "+LFSH +LWIN -LWIN -LFSH"), @" + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=1, latched=0, locked=0, group=0 + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=65, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=1, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Two Supers. + assert_snapshot!(run(c, "+LFSH +LWIN +RWIN -RWIN -LWIN -LFSH"), @" + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=1, latched=0, locked=0, group=0 + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=65, latched=0, locked=0, group=0 + +RWIN 134 XK_Super_R + surface key pressed: 126 + -RWIN 134 XK_Super_R + surface key released: 126 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=1, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Unrelated modifier in the middle. + assert_snapshot!(run(c, "+LWIN +LCTL +LFSH -LCTL -LFSH -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=68, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=69, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=65, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn mod_key_press_and_release_bind() { + let c = " + binds { + Mod { + press { close-window; } + release { center-column; } + } + } + "; + + // Both actions run, and the window doesn't see the key press or release themselves, only the + // modifier state. + assert_snapshot!(run(c, "+LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Other input in between doesn't cancel the release action. + assert_snapshot!(run(c, "+LWIN +LatA -LatA -LWIN"), @" + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + -LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn mod_key_press_and_release_bind_focus_change() { + let c = " + binds { + Mod { + press { close-window; } + release { center-column; } + } + } + "; + + let (mut f, id, surface) = set_up(c); + + // Hold mod: the press action runs, and the window sees the modifier state. + run_f(&mut f, id, &surface, "+LWIN"); + + // A new window takes the keyboard focus while mod is held. + let window = f.client(id).create_window(); + let surface2 = window.surface.clone(); + window.commit(); + f.roundtrip(id); + let window = f.client(id).window(&surface2); + window.attach_new_buffer(); + window.ack_last_and_commit(); + f.roundtrip(id); + + // The new window doesn't see the key press or release, but the focus change and the + // intercepted release still update its modifier state, so it doesn't get stuck with mod held. + assert_snapshot!(run_f(&mut f, id, &surface2, "-LWIN +LatA -LatA"), @" + surface enter: [] + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=0, latched=0, locked=0, group=0 + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + "); +} + +#[test] +fn modifier_key_trigger_binds() { + let c = " + binds { + Mod+Control_L { + press { close-window; } + release { center-column; } + } + Alt+Control_L { close-window; } + } + "; + + // A modifier key used as the trigger is intercepted, so the window doesn't see the key press + // and release, only the modifier state; both actions run. + assert_snapshot!(run(c, "+LWIN +LCTL -LCTL -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + niri test-action + surface modifiers: depressed=68, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Without the mod key held the bind doesn't match. + assert_snapshot!(run(c, "+LCTL -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // With Alt held instead, the Control_L press is intercepted, and only the press action runs. + assert_snapshot!(run(c, "+LALT +LCTL -LCTL -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + niri test-action + surface modifiers: depressed=12, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface modifiers: depressed=8, latched=0, locked=0, group=0 + -LALT 64 XK_Alt_L + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn release_bind_on_modifier_key() { + let c = " + binds { + Mod+Control_L { + release { close-window; } + } + } + "; + + // The press is forwarded so that apps see the modifier, and the release action runs when the + // modifier is released on its own. + assert_snapshot!(run(c, "+LWIN +LCTL -LCTL -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=68, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + niri test-action + surface key released: 29 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing another key in between cancels the release action. + assert_snapshot!(run(c, "+LWIN +LCTL +LFSH -LCTL -LFSH -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=68, latched=0, locked=0, group=0 + +LFSH 50 XK_Shift_L + surface key pressed: 42 + surface modifiers: depressed=69, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=65, latched=0, locked=0, group=0 + -LFSH 50 XK_Shift_L + surface key released: 42 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn modifier_key_and_keysym_triggers() { + // `Ctrl` is not the mod key, so it is matched by keysym directly: both Control_L and + // Control_R trigger it. + let by_modifier = " + binds { + Ctrl { + release { close-window; } + } + } + "; + + assert_snapshot!(run(by_modifier, "+LCTL -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + niri test-action + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(by_modifier, "+RCTL -RCTL"), @" + +RCTL 105 XK_Control_R + surface key pressed: 97 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -RCTL 105 XK_Control_R + niri test-action + surface key released: 97 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // `Control_L` is a specific key instead, so it doesn't trigger on Control_R, which reaches + // the window instead. + let by_keysym = " + binds { + Control_L { + release { close-window; } + } + } + "; + + assert_snapshot!(run(by_keysym, "+LCTL -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + niri test-action + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + assert_snapshot!(run(by_keysym, "+RCTL -RCTL"), @" + +RCTL 105 XK_Control_R + surface key pressed: 97 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -RCTL 105 XK_Control_R + surface key released: 97 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn mod_key_trigger_with_own_modifier() { + // The trigger is the configured mod key, and has Mod at a modifier. + let bind_by_mod = " + binds { + Mod+Super_L { close-window; } + } + "; + + assert_snapshot!(run(bind_by_mod, "+LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + let bind_by_keysym = " + binds { + Super_L { close-window; } + } + "; + + assert_snapshot!(run(bind_by_keysym, "+LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + let bind_by_generic = " + binds { + Mod+Super { close-window; } + } + "; + + assert_snapshot!(run(bind_by_generic, "+LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + niri test-action + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn release_bind_requires_matching_modifiers_on_press() { + let c = " + binds { + Mod+Ctrl+Q { + release { close-window; } + } + } + "; + + // Pressing Q without the mod key held forwards it and doesn't arm the release action, even if + // the mod key is held by the time Q is released. + assert_snapshot!(run(c, "+LCTL +LatQ +LWIN -LatQ -LWIN -LCTL"), @" + +LCTL 37 XK_Control_L + surface key pressed: 29 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + +AD01 24 XK_q + surface key pressed: 16 + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=68, latched=0, locked=0, group=0 + -AD01 24 XK_q + surface key released: 16 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=4, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface key released: 29 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +/// `Mod` and `Mod+Q` release-only binds, used by the inhibitor tests below. +const INHIBITING_BINDS: &str = " + binds { + Mod { + release { toggle-overview; } + } + Mod+Q { + release { close-window; } + } + } +"; + +#[test] +fn inhibiting_release_binds() { + let (mut f, id, surface) = set_up(INHIBITING_BINDS); + let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface); + f.roundtrip(id); + + // While inhibiting, release binds don't run, and the whole key is forwarded to the window. + assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN +LatQ -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + surface key pressed: 16 + -AD01 24 XK_q + surface key released: 16 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn release_binds_run_after_the_inhibitor_is_gone() { + let (mut f, id, surface) = set_up(INHIBITING_BINDS); + let inhibitor = f.client(id).state.inhibit_shortcuts(&surface); + f.roundtrip(id); + inhibitor.destroy(); + f.roundtrip(id); + + assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN +LatQ -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + -AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn deactivating_the_inhibitor_does_not_arm_a_release_bind_retroactively() { + let (mut f, id, surface) = set_up(INHIBITING_BINDS); + let inhibitor = f.client(id).state.inhibit_shortcuts(&surface); + f.roundtrip(id); + + // The press while inhibiting is forwarded and arms nothing. + run_f(&mut f, id, &surface, "+LWIN +LatQ"); + + inhibitor.destroy(); + f.roundtrip(id); + + // The release is still forwarded, and still runs no action. + assert_snapshot!(run_f(&mut f, id, &surface, "-LatQ -LWIN"), @" + -AD01 24 XK_q + surface key released: 16 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn activating_the_inhibitor_keeps_an_armed_release_bind() { + let (mut f, id, surface) = set_up(INHIBITING_BINDS); + + // The press while not inhibiting is intercepted and arms the release action. + run_f(&mut f, id, &surface, "+LWIN +LatQ"); + + let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface); + f.roundtrip(id); + + // The release stays intercepted, and still runs the armed release action. + assert_snapshot!(run_f(&mut f, id, &surface, "-LatQ -LWIN"), @" + -AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn inhibiting_a_modifier_press_still_forwards_it() { + let (mut f, id, surface) = set_up(INHIBITING_BINDS); + let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface); + f.roundtrip(id); + + // A modifier press is forwarded either way; the inhibitor only decides whether it arms the + // release action, and here it doesn't. + assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + "); +} + +#[test] +fn armed_modifier_release_runs_after_the_inhibitor_activates() { + let (mut f, id, surface) = set_up(INHIBITING_BINDS); + + // The modifier press while not inhibiting arms its release action. + run_f(&mut f, id, &surface, "+LWIN"); + + let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface); + f.roundtrip(id); + + assert_snapshot!(run_f(&mut f, id, &surface, "-LWIN"), @" + -LWIN 133 XK_Super_L + niri test-action + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn egl_release_action_runs_when_press_opens_screenshot_ui() { + // Release actions must always run if their press action ran, + // including if the screenshot UI is opened before the key is released. + let c = " + binds { + Mod+Q { + press { screenshot; } + release { close-window; } + } + } + "; + + let (mut f, id, surface) = set_up(c); + f.niri_state().backend.headless().add_renderer().unwrap(); + + assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN +LatQ -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + surface leave + -AD01 24 XK_q + niri test-action + -LWIN 133 XK_Super_L + "); + assert!(f.niri().screenshot_ui.is_open()); +} + +#[test] +fn cooldown_skips_whole_bind() { + // A bind on cooldown is skipped entirely: its press runs no action, and no release action is + // recorded, so its release runs no action either. + let c = " + binds { + Mod+MouseLeft cooldown-ms=1000 { + press { close-window; } + release { center-column; } + } + } + "; + + assert_snapshot!(run(c, "+LWIN +MouseLeft -MouseLeft +MouseLeft -MouseLeft -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +MouseLeft + niri test-action + -MouseLeft + niri test-action + +MouseLeft + -MouseLeft + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn cooldown_only_skips_the_action() { + let c = " + binds { + Mod+Control_L cooldown-ms=1000 { close-window; } + } + "; + + // The cooldown only skips the action: the second press runs no action, but it is still + // intercepted and hidden from the window, only advertising the modifier state. + assert_snapshot!(run(c, "+LWIN +LCTL -LCTL +LCTL"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + niri test-action + surface modifiers: depressed=68, latched=0, locked=0, group=0 + -LCTL 37 XK_Control_L + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +LCTL 37 XK_Control_L + surface modifiers: depressed=68, latched=0, locked=0, group=0 + "); +} + +#[test] +fn cooldown_applies_to_release_action_of_release_only_bind() { + // The release action is what starts this bind, so the cooldown rate-limits it: it runs for the + // first release, and not for the second while the bind is on cooldown. + let c = " + binds { + Mod cooldown-ms=1000 { + release { toggle-overview; } + } + } + "; + + assert_snapshot!(run(c, "+LWIN -LWIN +LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + niri test-action + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn cooldown_applies_to_press_action_only() { + // The cooldown applies to the press action only, and the release action runs only if its press + // action ran, so the second press runs no action at all. + let c = " + binds { + Mod+Q cooldown-ms=1000 { + press { close-window; } + release { center-column; } + } + } + "; + + assert_snapshot!(run(c, "+LWIN +LatQ -LatQ +LatQ -LatQ -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +AD01 24 XK_q + niri test-action + -AD01 24 XK_q + niri test-action + +AD01 24 XK_q + -AD01 24 XK_q + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn release_bind_runs_when_mru_closes() { + // `Alt` is a push-to-talk bind: the default Alt+Tab opens the MRU, and releasing Alt closes it + // instead of going through the regular bind lookup, so its release action must still run. + let c = " + binds { + Alt { + press { spawn \"ptt-toggle\"; } + release { spawn \"ptt-toggle\"; } + } + } + "; + + assert_snapshot!(run(c, "+LALT +TAB -LALT"), @" + +LALT 64 XK_Alt_L + niri test-action + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -LALT 64 XK_Alt_L + niri test-action + surface enter: [] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn mru_close_forwards_key_release_without_bind() { + // No bind is recorded for Alt, so releasing it while the MRU is open closes the MRU and the + // release is forwarded to the window like any other unsuppressed key. + let c = " + binds { + Mod+Q { close-window; } + } + "; + + assert_snapshot!(run(c, "+LALT +TAB -LALT"), @" + +LALT 64 XK_Alt_L + surface key pressed: 56 + surface modifiers: depressed=8, latched=0, locked=0, group=0 + +TAB 23 XK_Tab + surface leave + -LALT 64 XK_Alt_L + surface enter: [56] + surface modifiers: depressed=0, latched=0, locked=0, group=0 + surface key released: 56 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn vt_switch_runs_release_actions_of_held_binds() { + // A VT switch can take the input away without ever delivering the releases of everything held + // down, so the held binds are treated as released rather than forgotten. + let c = " + binds { + Mod+Q { + press { close-window; } + release { center-column; } + } + Mod+MouseLeft { + press { close-window; } + release { center-column; } + } + } + "; + + let (mut f, id, surface) = set_up(c); + + // Hold a key and a mouse button, both with press and release actions. + run_f(&mut f, id, &surface, "+LWIN +LatQ +MouseLeft"); + assert_eq!(f.niri().test_action_count, 2); + + // The VT switch runs both release actions. + f.niri_state().do_action(Action::ChangeVt(1), false); + assert_eq!(f.niri().test_action_count, 4); + + // If the releases arrive anyway, they run nothing. + run_f(&mut f, id, &surface, "-LatQ -MouseLeft"); + assert_eq!(f.niri().test_action_count, 4); +} + +#[test] +fn other_input_cancels_release_only_mouse_bind() { + // A release-only mouse bind is cancelled by other input in between, just like a release-only + // keyboard bind. + let c = " + binds { + Mod+MouseLeft { + release { close-window; } + } + } + "; + + // Pressing a key in between cancels it. + assert_snapshot!(run(c, "+LWIN +MouseLeft +LatA -LatA -MouseLeft -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +MouseLeft + +AC01 38 XK_a + surface key pressed: 30 + -AC01 38 XK_a + surface key released: 30 + -MouseLeft + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Pressing another mouse button in between cancels it. + assert_snapshot!(run(c, "+LWIN +MouseLeft +MouseRight -MouseRight -MouseLeft -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +MouseLeft + +MouseRight + -MouseRight + -MouseLeft + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // With nothing else in between, the release action runs. + assert_snapshot!(run(c, "+LWIN +MouseLeft -MouseLeft -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +MouseLeft + -MouseLeft + niri test-action + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn mouse_button_press_cancels_release_only_key_bind() { + // A mouse button press is other input, so it interrupts a pending release-only keyboard bind, + // including one on a bare modifier. Only the mouse bind's press action runs. + let c = " + binds { + Mod { + release { center-column; } + } + Mod+MouseLeft { close-window; } + } + "; + + assert_snapshot!(run(c, "+LWIN +MouseLeft -MouseLeft -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + +MouseLeft + niri test-action + -MouseLeft + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn scroll_cancels_release_only_key_bind() { + // Scrolling is input, so it cancels a pending release-only bind. Here there is no scroll bind + // for the held modifiers. + let c = " + binds { + Mod { + release { center-column; } + } + } + "; + + // The scroll in between cancels the pending release bind, so the release runs no action. + assert_snapshot!(run(c, "+LWIN WheelScrollDown -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + WheelScrollDown + -LWIN 133 XK_Super_L + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); + + // Without a scroll in between, the release action runs. + assert_snapshot!(run(c, "+LWIN -LWIN"), @" + +LWIN 133 XK_Super_L + surface key pressed: 125 + surface modifiers: depressed=64, latched=0, locked=0, group=0 + -LWIN 133 XK_Super_L + niri test-action + surface key released: 125 + surface modifiers: depressed=0, latched=0, locked=0, group=0 + "); +} + +#[test] +fn redundant_modifier_binds() { + // Binds should behave identically with or without redundant modifiers. + let input = "+LALT -LALT"; + assert_eq!( + run("binds { Alt_L { release { close-window; }; }; }", input), + run("binds { Alt+Alt_L { release { close-window; }; }; }", input) + ); + + let input = "+LALT +LatQ -LatQ -LALT"; + assert_eq!( + run("binds { Alt+Q { release { close-window; }; }; }", input), + run("binds { Alt+Alt+Q { release { close-window; }; }; }", input) + ); +} diff --git a/src/tests/client.rs b/src/tests/client.rs index d6624440..03f8f7f1 100644 --- a/src/tests/client.rs +++ b/src/tests/client.rs @@ -62,6 +62,7 @@ pub struct State { pub xdg_wm_base: Option, pub layer_shell: Option, pub virtual_pointer_manager: Option, + pub virtual_pointer: Option, pub spbm: Option, pub viewporter: Option, pub ksim: Option, @@ -262,6 +263,7 @@ impl Client { xdg_wm_base: None, layer_shell: None, virtual_pointer_manager: None, + virtual_pointer: None, spbm: None, viewporter: None, ksim: None, @@ -430,6 +432,17 @@ impl State { let seat = self.seats.keys().next().unwrap(); ksim.inhibit_shortcuts(surface, seat, &self.qh, ()) } + + pub fn virtual_pointer(&mut self) -> ZwlrVirtualPointerV1 { + if let Some(pointer) = &self.virtual_pointer { + return pointer.clone(); + } + + let manager = self.virtual_pointer_manager.as_ref().unwrap(); + let pointer = manager.create_virtual_pointer(None, &self.qh, ()); + self.virtual_pointer = Some(pointer.clone()); + pointer + } } impl Window { diff --git a/src/tests/virtual_pointer.rs b/src/tests/virtual_pointer.rs index b9aa02ae..85dcbea4 100644 --- a/src/tests/virtual_pointer.rs +++ b/src/tests/virtual_pointer.rs @@ -8,8 +8,7 @@ fn axis_discrete_overflow() { let id = f.add_client(); let client = f.client(id); - let manager = client.state.virtual_pointer_manager.as_ref().unwrap(); - let pointer = manager.create_virtual_pointer(None, &client.qh, ()); + let pointer = client.state.virtual_pointer(); pointer.axis_discrete(0, wl_pointer::Axis::VerticalScroll, 0., i32::MAX); f.roundtrip(id); }