forge
Explore
Log in

Don't you dare go Hollow

input: add press and release bind tests

Browse files at this commit

9828c09d32ce5c32e9f8071ce8d0b82bf9ee3907Justin SpeddingParent: 3c6e215
niri-config/src/lib.rsmodified+440 -0
@@ -626,6 +626,7 @@ impl ConfigPath {
626 mod tests {626 mod tests {
627 use insta::{assert_debug_snapshot, assert_snapshot};627 use insta::{assert_debug_snapshot, assert_snapshot};
628 use pretty_assertions::assert_eq;628 use pretty_assertions::assert_eq;
629+ use smithay::input::keyboard::Keysym;
629 630
630 use super::*;631 use super::*;
631 632
@@ -648,6 +649,445 @@ mod tests {
648 .unwrap()649 .unwrap()
649 }650 }
650 651
652+ #[test]
653+ fn parse_release_binds_for_supported_triggers() {
654+ // Keys and mouse buttons have release events, so release sections are supported.
655+ do_parse(
656+ r#"
657+ binds {
658+ Mod+MouseLeft {
659+ release { close-window; }
660+ }
661+ Mod+Q {
662+ press { close-window; }
663+ release { close-window; }
664+ }
665+ }
666+ "#,
667+ );
668+ }
669+
670+ #[test]
671+ fn parse_release_binds_for_unsupported_triggers() {
672+ // Scroll ticks are dispatched as press events only, so scroll binds can't have a release
673+ // action.
674+ assert_parse_error(
675+ r#"
676+ binds {
677+ Mod+WheelScrollDown {
678+ release { toggle-overview; }
679+ }
680+ }
681+ "#,
682+ "release sections are not supported for scroll binds",
683+ );
684+ assert_parse_error(
685+ r#"
686+ binds {
687+ Mod+TouchpadScrollDown {
688+ release { toggle-overview; }
689+ }
690+ }
691+ "#,
692+ "release sections are not supported for scroll binds",
693+ );
694+ }
695+
696+ #[test]
697+ fn parse_bind_repeat_on_release_only() {
698+ // A release-only bind has no press action to repeat, so repeat=true is rejected.
699+ assert_parse_error(
700+ r#"
701+ binds {
702+ Mod+Q repeat=true {
703+ release { close-window; }
704+ }
705+ }
706+ "#,
707+ "release-only binds cannot repeat",
708+ );
709+
710+ do_parse(
711+ r#"
712+ binds {
713+ Mod+T repeat=true { spawn "alacritty"; }
714+ Mod+Q repeat=true {
715+ press { close-window; }
716+ release { close-window; }
717+ }
718+ Mod+W {
719+ release { close-window; }
720+ }
721+ Mod+E repeat=false {
722+ release { close-window; }
723+ }
724+ }
725+ "#,
726+ );
727+ }
728+
729+ /// Asserts that parsing fails with a diagnostic containing `message`.
730+ #[track_caller]
731+ fn assert_parse_error(text: &str, message: &str) {
732+ let err = match Config::parse_mem(text) {
733+ Ok(_) => panic!("expected a parse error for:\n{text}"),
734+ Err(err) => err,
735+ };
736+
737+ // knuffel keeps the individual diagnostics in a private field, so check the Debug
738+ // representation, which includes their messages.
739+ let err = format!("{err:?}");
740+ assert!(
741+ err.contains(message),
742+ "expected {message:?} in parse error:\n{err}"
743+ );
744+ }
745+
746+ #[test]
747+ fn parse_bind_section_errors() {
748+ // A direct action node and a `press` section both specify the press action, so mixing them
749+ // is reported as mixing the two syntaxes rather than as a duplicate `press` section.
750+ assert_parse_error(
751+ r#"
752+ binds {
753+ Mod+Q {
754+ close-window;
755+ press { center-column; }
756+ }
757+ }
758+ "#,
759+ "cannot mix direct actions with press/release sections",
760+ );
761+
762+ // A second direct action is a duplicate action, not a mix of the two syntaxes.
763+ assert_parse_error(
764+ r#"
765+ binds {
766+ Mod+Q {
767+ close-window;
768+ center-column;
769+ }
770+ }
771+ "#,
772+ "only one action is allowed per keybind",
773+ );
774+
775+ // A direct action following a `press` section is mixing the two syntaxes.
776+ assert_parse_error(
777+ r#"
778+ binds {
779+ Mod+Q {
780+ press { close-window; }
781+ center-column;
782+ }
783+ }
784+ "#,
785+ "cannot mix direct actions with press/release sections",
786+ );
787+
788+ // Two `press` sections are still duplicates.
789+ assert_parse_error(
790+ r#"
791+ binds {
792+ Mod+Q {
793+ press { close-window; }
794+ press { center-column; }
795+ }
796+ }
797+ "#,
798+ "duplicate `press` section",
799+ );
800+
801+ assert_parse_error(
802+ r#"
803+ binds {
804+ Mod+Q {
805+ center-column;
806+ release { close-window; }
807+ }
808+ }
809+ "#,
810+ "cannot mix direct actions with press/release sections",
811+ );
812+
813+ assert_parse_error(
814+ r#"
815+ binds {
816+ Mod+Q {
817+ release { close-window; }
818+ center-column;
819+ }
820+ }
821+ "#,
822+ "cannot mix direct actions with press/release sections",
823+ );
824+ }
825+
826+ #[test]
827+ fn parse_bind_bound_action() {
828+ let config = do_parse(
829+ r#"
830+ binds {
831+ Mod+A { close-window; }
832+ Mod+B {
833+ press { close-window; }
834+ }
835+ Mod+C {
836+ release { toggle-overview; }
837+ }
838+ Mod+D {
839+ press { close-window; }
840+ release { toggle-overview; }
841+ }
842+ }
843+ "#,
844+ );
845+
846+ let action = |index: usize| &config.binds.0[index].action;
847+
848+ // A direct action and a press section both bind only the press phase.
849+ assert!(matches!(action(0), BoundAction::Press(Action::CloseWindow)));
850+ assert!(matches!(action(1), BoundAction::Press(Action::CloseWindow)));
851+
852+ // A release section binds only the release phase.
853+ assert!(matches!(
854+ action(2),
855+ BoundAction::Release(Action::ToggleOverview)
856+ ));
857+
858+ // Both sections bind both phases.
859+ assert!(matches!(
860+ action(3),
861+ BoundAction::Both {
862+ press: Action::CloseWindow,
863+ release: Action::ToggleOverview,
864+ }
865+ ));
866+ }
867+
868+ #[test]
869+ fn parse_bare_modifier_keys() {
870+ let config = do_parse(
871+ r#"
872+ binds {
873+ Mod {
874+ release { toggle-overview; }
875+ }
876+ Ctrl {
877+ release { close-window; }
878+ }
879+ Shift {
880+ release { close-window; }
881+ }
882+ Alt {
883+ release { close-window; }
884+ }
885+ Super {
886+ release { close-window; }
887+ }
888+ Mod5 {
889+ release { close-window; }
890+ }
891+ Mod3 {
892+ release { close-window; }
893+ }
894+
895+ Ctrl+Alt_L {
896+ release { close-window; }
897+ }
898+ }
899+ "#,
900+ );
901+
902+ let triggers = config
903+ .binds
904+ .0
905+ .iter()
906+ .map(|bind| bind.key.trigger)
907+ .collect::<Vec<_>>();
908+
909+ assert_eq!(
910+ triggers,
911+ vec![
912+ Trigger::CompositorMod,
913+ Trigger::Modifier(ModKey::Ctrl),
914+ Trigger::Modifier(ModKey::Shift),
915+ Trigger::Modifier(ModKey::Alt),
916+ Trigger::Modifier(ModKey::Super),
917+ Trigger::Modifier(ModKey::IsoLevel3Shift),
918+ Trigger::Modifier(ModKey::IsoLevel5Shift),
919+ Trigger::Keysym(Keysym::Alt_L),
920+ ]
921+ );
922+
923+ // Ctrl+Alt_L has modifiers of Ctrl (manually specified) and Alt (implied by the keysym).
924+ assert_eq!(
925+ config.binds.0[7].key.modifiers,
926+ Modifiers::CTRL | Modifiers::ALT
927+ );
928+ }
929+
930+ #[test]
931+ fn parse_bind_conflicting_modifier_keys() {
932+ // A modifier key can be spelled as the modifier itself (`Alt`) or as its keysym (`Alt_L`),
933+ // and a modifier key is triggered by either of its keysyms, so one of these two binds
934+ // would always shadow the other.
935+ assert_parse_error(
936+ r#"
937+ binds {
938+ Alt {
939+ release { toggle-overview; }
940+ }
941+ Alt_L {
942+ release { close-window; }
943+ }
944+ }
945+ "#,
946+ "conflicting keybind later defined here",
947+ );
948+ assert_parse_error(
949+ r#"
950+ recent-windows {
951+ binds {
952+ Alt+Control_L { next-window; }
953+ Alt+Ctrl { next-window; }
954+ }
955+ }
956+ "#,
957+ "conflicting keybind later defined here",
958+ );
959+
960+ // The left and right keysyms are different keys, so those can be bound separately.
961+ do_parse(
962+ r#"
963+ binds {
964+ Alt_L {
965+ release { close-window; }
966+ }
967+ Alt_R {
968+ release { center-column; }
969+ }
970+ }
971+ "#,
972+ );
973+
974+ // A modifier keysym on its own is an ordinary modifier-only bind.
975+ let config = do_parse(
976+ r#"
977+ binds {
978+ Alt_L {
979+ release { close-window; }
980+ }
981+ }
982+ "#,
983+ );
984+ assert!(config.binds.0[0].key.trigger.is_modifier());
985+ }
986+
987+ #[test]
988+ fn parse_recent_windows_bind_modifier() {
989+ // Modifier-only binds are allowed.
990+ do_parse(
991+ r#"
992+ recent-windows {
993+ binds {
994+ Ctrl+Alt_L { next-window; }
995+ Ctrl+Shift { next-window; }
996+ }
997+ }
998+ "#,
999+ );
1000+
1001+ // Binds must have an extra modifier.
1002+ assert_parse_error(
1003+ r#"
1004+ recent-windows {
1005+ binds {
1006+ Alt_L { next-window; }
1007+ }
1008+ }
1009+ "#,
1010+ "keybind must have a modifier key",
1011+ );
1012+ assert_parse_error(
1013+ r#"
1014+ recent-windows {
1015+ binds {
1016+ Alt+Alt_L { next-window; }
1017+ }
1018+ }
1019+ "#,
1020+ "keybind must have a modifier key",
1021+ );
1022+
1023+ // A trigger with no modifier at all is rejected.
1024+ assert_parse_error(
1025+ r#"
1026+ recent-windows {
1027+ binds {
1028+ Tab { next-window; }
1029+ }
1030+ }
1031+ "#,
1032+ "keybind must have a modifier key",
1033+ );
1034+
1035+ // Only keyboard keys are supported here for now.
1036+ assert_parse_error(
1037+ r#"
1038+ recent-windows {
1039+ binds {
1040+ Alt+MouseLeft { next-window; }
1041+ }
1042+ }
1043+ "#,
1044+ "key must be a keyboard key",
1045+ );
1046+ }
1047+
1048+ #[test]
1049+ fn parse_bind_redundant_modifiers_are_the_same_bind() {
1050+ // Redundant modifiers are normalized, so these should error as duplicates.
1051+ assert_parse_error(
1052+ r#"
1053+ binds {
1054+ Alt_L { close-window; }
1055+ Alt+Alt_L { center-column; }
1056+ }
1057+ "#,
1058+ "duplicate keybind later defined here",
1059+ );
1060+ assert_parse_error(
1061+ r#"
1062+ binds {
1063+ Alt { close-window; }
1064+ Alt+Alt { center-column; }
1065+ }
1066+ "#,
1067+ "duplicate keybind later defined here",
1068+ );
1069+ assert_parse_error(
1070+ r#"
1071+ binds {
1072+ Mod { close-window; }
1073+ Mod+Mod { center-column; }
1074+ }
1075+ "#,
1076+ "duplicate keybind later defined here",
1077+ );
1078+ assert_parse_error(
1079+ r#"
1080+ recent-windows {
1081+ binds {
1082+ Alt+Control_L { next-window; }
1083+ Alt+Ctrl+Control_L { next-window; }
1084+ }
1085+ }
1086+ "#,
1087+ "duplicate keybind later defined here",
1088+ );
1089+ }
1090+
651 #[test]1091 #[test]
652 fn parse_on_xdg_activate() {1092 fn parse_on_xdg_activate() {
653 let parsed = do_parse(1093 let parsed = do_parse(
src/input/mod.rsmodified+384 -72
@@ -5640,82 +5640,150 @@ fn make_binds_iter<'a>(
5640 #[cfg(test)]5640 #[cfg(test)]
5641 mod tests {5641 mod tests {
5642 use super::*;5642 use super::*;
5643+ use crate::animation::Clock;
5644+
5645+ const CLOSE_KEYSYM: Keysym = Keysym::q;
5646+ const CLOSE_KEY_CODE: Keycode = Keycode::new(CLOSE_KEYSYM.raw());
5647+ struct TestState {
5648+ screenshot_ui: ScreenshotUi,
5649+ disable_power_key_handling: bool,
5650+ is_inhibiting: bool,
5651+ is_locked: bool,
5652+ suppressed_keys: HashSet<Keycode>,
5653+ pending_release_binds: HashMap<Keycode, Bind>,
5654+ cooldown_timers: HashMap<Key, RegistrationToken>,
5655+ }
5656+
5657+ fn create_test_state() -> TestState {
5658+ TestState {
5659+ screenshot_ui: ScreenshotUi::new(Clock::default(), Default::default()),
5660+ disable_power_key_handling: false,
5661+ is_inhibiting: false,
5662+ is_locked: false,
5663+ suppressed_keys: HashSet::new(),
5664+ pending_release_binds: HashMap::new(),
5665+ cooldown_timers: HashMap::new(),
5666+ }
5667+ }
5668+
5669+ /// Returns a bind with the given trigger, modifiers, and action, and default flags.
5670+ fn bind(trigger: Trigger, modifiers: Modifiers, action: BoundAction) -> Bind {
5671+ Bind {
5672+ key: Key { trigger, modifiers },
5673+ action,
5674+ repeat: false,
5675+ cooldown: None,
5676+ allow_when_locked: false,
5677+ allow_inhibiting: true,
5678+ hotkey_overlay_title: None,
5679+ }
5680+ }
5681+
5682+ /// Returns a `Mod+Q` bind with the given action and default flags.
5683+ fn close_bind(action: BoundAction) -> Bind {
5684+ bind(Trigger::Keysym(CLOSE_KEYSYM), Modifiers::COMPOSITOR, action)
5685+ }
5686+
5687+ fn mod_held() -> ModifiersState {
5688+ ModifiersState {
5689+ logo: true,
5690+ ..Default::default()
5691+ }
5692+ }
5693+
5694+ /// Processes a press of the `Mod+Q` bind that the tests are written with.
5695+ fn press_close_key(
5696+ state: &mut TestState,
5697+ bindings: &Binds,
5698+ mods: ModifiersState,
5699+ ) -> ShouldInterceptResult {
5700+ process_close_key(state, bindings, mods, KeyState::Pressed)
5701+ }
5702+
5703+ /// Processes a release of the `Mod+Q` bind that the tests are written with.
5704+ fn release_close_key(
5705+ state: &mut TestState,
5706+ bindings: &Binds,
5707+ mods: ModifiersState,
5708+ ) -> ShouldInterceptResult {
5709+ process_close_key(state, bindings, mods, KeyState::Released)
5710+ }
5711+
5712+ fn process_close_key(
5713+ state: &mut TestState,
5714+ bindings: &Binds,
5715+ mods: ModifiersState,
5716+ key_state: KeyState,
5717+ ) -> ShouldInterceptResult {
5718+ let pressed = key_state == KeyState::Pressed;
5719+
5720+ if pressed {
5721+ retain_press_binds(&mut state.pending_release_binds);
5722+ }
5723+
5724+ should_intercept_key(
5725+ &mut state.suppressed_keys,
5726+ &mut state.pending_release_binds,
5727+ &state.cooldown_timers,
5728+ &bindings.0,
5729+ ModKey::Super,
5730+ CLOSE_KEY_CODE,
5731+ CLOSE_KEYSYM,
5732+ Some(CLOSE_KEYSYM),
5733+ pressed,
5734+ mods,
5735+ &state.screenshot_ui,
5736+ state.disable_power_key_handling,
5737+ state.is_inhibiting,
5738+ state.is_locked,
5739+ )
5740+ }
5741+
5742+ macro_rules! assert_matches {
5743+ ($expression:expr, $pattern:pat) => {
5744+ let value = $expression;
5745+ assert!(
5746+ matches!(value, $pattern),
5747+ "Expected {:?} to match {}",
5748+ value,
5749+ stringify!($pattern)
5750+ );
5751+ };
5752+ }
5643 5753
5644 #[test]5754 #[test]
5645 fn comp_mod_handling() {5755 fn comp_mod_handling() {
5646 let bindings = Binds(vec![5756 let bindings = Binds(vec![
5647- Bind {5757+ bind(
5648- key: Key {5758+ Trigger::Keysym(Keysym::q),
5649- trigger: Trigger::Keysym(Keysym::q),5759+ Modifiers::COMPOSITOR,
5650- modifiers: Modifiers::COMPOSITOR,5760+ BoundAction::Press(Action::CloseWindow),
5651- },5761+ ),
5652- action: BoundAction::Press(Action::CloseWindow),5762+ bind(
5653- repeat: true,5763+ Trigger::Keysym(Keysym::h),
5654- cooldown: None,5764+ Modifiers::SUPER,
5655- allow_when_locked: false,5765+ BoundAction::Press(Action::FocusColumnLeft),
5656- allow_inhibiting: true,5766+ ),
5657- hotkey_overlay_title: None,5767+ bind(
5658- },5768+ Trigger::Keysym(Keysym::j),
5659- Bind {5769+ Modifiers::empty(),
5660- key: Key {5770+ BoundAction::Press(Action::FocusWindowDown),
5661- trigger: Trigger::Keysym(Keysym::h),5771+ ),
5662- modifiers: Modifiers::SUPER,5772+ bind(
5663- },5773+ Trigger::Keysym(Keysym::k),
5664- action: BoundAction::Press(Action::FocusColumnLeft),5774+ Modifiers::COMPOSITOR | Modifiers::SUPER,
5665- repeat: true,5775+ BoundAction::Press(Action::FocusWindowUp),
5666- cooldown: None,5776+ ),
5667- allow_when_locked: false,5777+ bind(
5668- allow_inhibiting: true,5778+ Trigger::Keysym(Keysym::l),
5669- hotkey_overlay_title: None,5779+ Modifiers::SUPER | Modifiers::ALT,
5670- },5780+ BoundAction::Press(Action::FocusColumnRight),
5671- Bind {5781+ ),
5672- key: Key {5782+ bind(
5673- trigger: Trigger::Keysym(Keysym::j),5783+ Trigger::Keysym(Keysym::Super_L),
5674- modifiers: Modifiers::empty(),5784+ Modifiers::empty(),
5675- },5785+ BoundAction::Release(Action::ToggleOverview),
5676- action: BoundAction::Press(Action::FocusWindowDown),5786+ ),
5677- repeat: true,
5678- cooldown: None,
5679- allow_when_locked: false,
5680- allow_inhibiting: true,
5681- hotkey_overlay_title: None,
5682- },
5683- Bind {
5684- key: Key {
5685- trigger: Trigger::Keysym(Keysym::k),
5686- modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER,
5687- },
5688- action: BoundAction::Press(Action::FocusWindowUp),
5689- repeat: true,
5690- cooldown: None,
5691- allow_when_locked: false,
5692- allow_inhibiting: true,
5693- hotkey_overlay_title: None,
5694- },
5695- Bind {
5696- key: Key {
5697- trigger: Trigger::Keysym(Keysym::l),
5698- modifiers: Modifiers::SUPER | Modifiers::ALT,
5699- },
5700- action: BoundAction::Press(Action::FocusColumnRight),
5701- repeat: true,
5702- cooldown: None,
5703- allow_when_locked: false,
5704- allow_inhibiting: true,
5705- hotkey_overlay_title: None,
5706- },
5707- Bind {
5708- key: Key {
5709- trigger: Trigger::Keysym(Keysym::Super_L),
5710- modifiers: Modifiers::empty(),
5711- },
5712- action: BoundAction::Release(Action::ToggleOverview),
5713- repeat: false,
5714- cooldown: None,
5715- allow_when_locked: false,
5716- allow_inhibiting: true,
5717- hotkey_overlay_title: None,
5718- },
5719 ]);5787 ]);
5720 5788
5721 assert_eq!(5789 assert_eq!(
@@ -5850,4 +5918,248 @@ mod tests {
5850 Some(&bindings.0[5])5918 Some(&bindings.0[5])
5851 );5919 );
5852 }5920 }
5921+
5922+ #[test]
5923+ fn bind_allowed_when_locked_requires_both_actions_of_a_both_bind() {
5924+ // A press could start something that its release would never undo, so both actions must be
5925+ // allowed while locked.
5926+ let both = |press, release| close_bind(BoundAction::Both { press, release });
5927+ assert!(!bind_allowed_when_locked(
5928+ &both(Action::Suspend, Action::CloseWindow),
5929+ true
5930+ ));
5931+ assert!(!bind_allowed_when_locked(
5932+ &both(Action::CloseWindow, Action::Suspend),
5933+ true
5934+ ));
5935+ assert!(bind_allowed_when_locked(
5936+ &both(Action::Suspend, Action::PowerOffMonitors),
5937+ true
5938+ ));
5939+ }
5940+
5941+ #[test]
5942+ fn bind_allowed_when_locked_requires_only_the_single_action() {
5943+ assert!(bind_allowed_when_locked(
5944+ &close_bind(BoundAction::Press(Action::Suspend)),
5945+ true
5946+ ));
5947+ assert!(!bind_allowed_when_locked(
5948+ &close_bind(BoundAction::Press(Action::CloseWindow)),
5949+ true
5950+ ));
5951+ assert!(bind_allowed_when_locked(
5952+ &close_bind(BoundAction::Release(Action::Suspend)),
5953+ false
5954+ ));
5955+ assert!(!bind_allowed_when_locked(
5956+ &close_bind(BoundAction::Release(Action::CloseWindow)),
5957+ false
5958+ ));
5959+ }
5960+
5961+ #[test]
5962+ fn bind_allowed_when_locked_always_allows_a_both_bind_release() {
5963+ // The release only happens if its press ran, and the press required both actions to be
5964+ // allowed, so the release is never blocked separately.
5965+ let both = close_bind(BoundAction::Both {
5966+ press: Action::Suspend,
5967+ release: Action::CloseWindow,
5968+ });
5969+ assert!(bind_allowed_when_locked(&both, false));
5970+ }
5971+
5972+ #[test]
5973+ fn allow_when_locked_allows_the_whole_bind() {
5974+ let bind = |action| Bind {
5975+ allow_when_locked: true,
5976+ ..close_bind(action)
5977+ };
5978+ assert!(bind_allowed_when_locked(
5979+ &bind(BoundAction::Press(Action::CloseWindow)),
5980+ true
5981+ ));
5982+ assert!(bind_allowed_when_locked(
5983+ &bind(BoundAction::Release(Action::CloseWindow)),
5984+ false
5985+ ));
5986+
5987+ let both = bind(BoundAction::Both {
5988+ press: Action::CloseWindow,
5989+ release: Action::CenterColumn,
5990+ });
5991+ assert!(bind_allowed_when_locked(&both, true));
5992+ assert!(bind_allowed_when_locked(&both, false));
5993+ }
5994+
5995+ #[test]
5996+ fn locked_both_bind_does_not_run_its_press_when_its_release_is_not_allowed() {
5997+ // `Mod+Q { press { suspend; } release { close-window; } }`: the press is allowed while
5998+ // locked but the release isn't, so the press must not run, since nothing could undo it.
5999+ let bindings = Binds(vec![close_bind(BoundAction::Both {
6000+ press: Action::Suspend,
6001+ release: Action::CloseWindow,
6002+ })]);
6003+
6004+ let mut state = TestState {
6005+ is_locked: true,
6006+ ..create_test_state()
6007+ };
6008+ let result = press_close_key(&mut state, &bindings, mod_held());
6009+
6010+ // `handle_bind()` drops the press while locked, and nothing is recorded to undo.
6011+ assert_matches!(result, ShouldInterceptResult::InterceptAndHandle(_));
6012+ assert!(state.pending_release_binds.is_empty());
6013+
6014+ let result = release_close_key(&mut state, &bindings, mod_held());
6015+ assert_matches!(result, ShouldInterceptResult::InterceptOnly);
6016+ }
6017+
6018+ #[test]
6019+ fn both_bind_release_recorded_while_unlocked_runs_while_locked() {
6020+ // Its press action ran while unlocked, so the release must run later even if the screen got
6021+ // locked in between.
6022+ let bindings = Binds(vec![close_bind(BoundAction::Both {
6023+ press: Action::Suspend,
6024+ release: Action::CloseWindow,
6025+ })]);
6026+
6027+ let mut state = create_test_state();
6028+ let result = press_close_key(&mut state, &bindings, mod_held());
6029+ assert_matches!(result, ShouldInterceptResult::InterceptAndHandle(_));
6030+ assert!(state.pending_release_binds.contains_key(&CLOSE_KEY_CODE));
6031+
6032+ state.is_locked = true;
6033+ let result = release_close_key(&mut state, &bindings, mod_held());
6034+ assert_matches!(
6035+ result,
6036+ ShouldInterceptResult::InterceptAndHandle(Bind {
6037+ action: BoundAction::Both {
6038+ release: Action::CloseWindow,
6039+ ..
6040+ },
6041+ ..
6042+ })
6043+ );
6044+ assert!(state.pending_release_binds.is_empty());
6045+ }
6046+
6047+ #[test]
6048+ fn release_only_bind_is_recorded_on_press_and_runs_on_release() {
6049+ let bindings = Binds(vec![close_bind(BoundAction::Release(Action::CloseWindow))]);
6050+
6051+ let mut state = create_test_state();
6052+ let result = press_close_key(&mut state, &bindings, mod_held());
6053+ assert_matches!(result, ShouldInterceptResult::InterceptOnly);
6054+ assert!(state.pending_release_binds.contains_key(&CLOSE_KEY_CODE));
6055+
6056+ let result = release_close_key(&mut state, &bindings, mod_held());
6057+ assert_matches!(
6058+ result,
6059+ ShouldInterceptResult::InterceptAndHandle(Bind {
6060+ action: BoundAction::Release(Action::CloseWindow),
6061+ ..
6062+ })
6063+ );
6064+ assert!(state.pending_release_binds.is_empty());
6065+ }
6066+
6067+ #[test]
6068+ fn release_only_bind_allowed_while_locked_is_recorded_and_runs_while_locked() {
6069+ let bindings = Binds(vec![Bind {
6070+ allow_when_locked: true,
6071+ ..close_bind(BoundAction::Release(Action::CloseWindow))
6072+ }]);
6073+
6074+ let mut state = TestState {
6075+ is_locked: true,
6076+ ..create_test_state()
6077+ };
6078+ let result = press_close_key(&mut state, &bindings, mod_held());
6079+ assert_matches!(result, ShouldInterceptResult::InterceptOnly);
6080+ assert!(state.pending_release_binds.contains_key(&CLOSE_KEY_CODE));
6081+
6082+ let result = release_close_key(&mut state, &bindings, mod_held());
6083+ assert_matches!(
6084+ result,
6085+ ShouldInterceptResult::InterceptAndHandle(Bind {
6086+ action: BoundAction::Release(Action::CloseWindow),
6087+ ..
6088+ })
6089+ );
6090+ assert!(state.pending_release_binds.is_empty());
6091+ }
6092+
6093+ #[test]
6094+ fn release_only_bind_not_allowed_while_locked_is_not_recorded_while_locked() {
6095+ let bindings = Binds(vec![close_bind(BoundAction::Release(Action::CloseWindow))]);
6096+
6097+ let mut state = TestState {
6098+ is_locked: true,
6099+ ..create_test_state()
6100+ };
6101+ let result = press_close_key(&mut state, &bindings, mod_held());
6102+ assert_matches!(result, ShouldInterceptResult::InterceptOnly);
6103+ assert!(state.pending_release_binds.is_empty());
6104+
6105+ let result = release_close_key(&mut state, &bindings, mod_held());
6106+ assert_matches!(result, ShouldInterceptResult::InterceptOnly);
6107+ assert!(state.pending_release_binds.is_empty());
6108+ }
6109+
6110+ #[test]
6111+ fn bind_allowed_during_screenshot_requires_both_actions_of_a_both_bind() {
6112+ // A press could start something that its release would never undo, so both actions must be
6113+ // allowed while the screenshot UI is open.
6114+ let both = |press, release| close_bind(BoundAction::Both { press, release });
6115+ assert!(!bind_allowed_during_screenshot(
6116+ &both(Action::MoveColumnLeft, Action::CloseWindow),
6117+ true
6118+ ));
6119+ assert!(!bind_allowed_during_screenshot(
6120+ &both(Action::CloseWindow, Action::MoveColumnLeft),
6121+ true
6122+ ));
6123+ assert!(bind_allowed_during_screenshot(
6124+ &both(Action::MoveColumnLeft, Action::MoveWindowUp),
6125+ true
6126+ ));
6127+ }
6128+
6129+ #[test]
6130+ fn bind_allowed_during_screenshot_requires_only_the_single_action() {
6131+ assert!(bind_allowed_during_screenshot(
6132+ &close_bind(BoundAction::Press(Action::MoveColumnLeft)),
6133+ true
6134+ ));
6135+ assert!(!bind_allowed_during_screenshot(
6136+ &close_bind(BoundAction::Press(Action::CloseWindow)),
6137+ true
6138+ ));
6139+ }
6140+
6141+ #[test]
6142+ fn bind_allowed_during_screenshot_always_allows_a_both_bind_release() {
6143+ // The release only happens if its press ran, so the screenshot UI opening in between must
6144+ // not block it.
6145+ let both = |press, release| close_bind(BoundAction::Both { press, release });
6146+ assert!(bind_allowed_during_screenshot(
6147+ &both(Action::MoveColumnLeft, Action::CloseWindow),
6148+ false
6149+ ));
6150+ assert!(bind_allowed_during_screenshot(
6151+ &both(Action::CloseWindow, Action::MoveColumnLeft),
6152+ false
6153+ ));
6154+
6155+ // A release-only bind has no press to undo, so its own action must be allowed.
6156+ assert!(bind_allowed_during_screenshot(
6157+ &close_bind(BoundAction::Release(Action::MoveColumnLeft)),
6158+ false
6159+ ));
6160+ assert!(!bind_allowed_during_screenshot(
6161+ &close_bind(BoundAction::Release(Action::CloseWindow)),
6162+ false
6163+ ));
6164+ }
5853 }6165 }
src/tests/binds.rsmodified+1372 -41
@@ -4,30 +4,87 @@ use insta::assert_snapshot;
4 use niri_config::{Action, BoundAction, Config};4 use niri_config::{Action, BoundAction, Config};
5 use smithay::backend::input::{InputEvent, InputTime, KeyState, Keycode};5 use smithay::backend::input::{InputEvent, InputTime, KeyState, Keycode};
6 use smithay::input::keyboard::xkb::Keymap;6 use smithay::input::keyboard::xkb::Keymap;
7+use wayland_client::protocol::wl_pointer;
7 use wayland_client::protocol::wl_surface::WlSurface;8 use wayland_client::protocol::wl_surface::WlSurface;
8 9
9 use crate::tests::client::ClientId;10 use crate::tests::client::ClientId;
10 use crate::tests::fixture::Fixture;11 use crate::tests::fixture::Fixture;
11 use crate::tests::test_input_backend::{TestInputBackend, TestKeyboardKeyEvent};12 use crate::tests::test_input_backend::{TestInputBackend, TestKeyboardKeyEvent};
12 13
14+#[derive(Clone, Copy)]
13 enum Op {15 enum Op {
14 Press(Keycode),16 Press(Keycode),
15 Release(Keycode),17 Release(Keycode),
18+ MouseButton {
19+ name: &'static str,
20+ code: u32,
21+ state: wl_pointer::ButtonState,
22+ },
23+ /// A scroll tick. Scrolls are momentary events, so they have no press or release.
24+ Scroll {
25+ name: &'static str,
26+ source: wl_pointer::AxisSource,
27+ /// Positive scrolls down.
28+ amount: f64,
29+ },
16 }30 }
17 31
32+/// Mouse buttons that can be pressed and released, named after the binds that trigger them.
33+const MOUSE_BUTTONS: &[(&str, u32)] = &[
34+ ("MouseLeft", 0x110),
35+ ("MouseRight", 0x111),
36+ ("MouseMiddle", 0x112),
37+ ("MouseBack", 0x113),
38+ ("MouseForward", 0x114),
39+];
40+
41+/// Scrolls, named after the binds that trigger them.
42+const SCROLLS: &[(&str, wl_pointer::AxisSource, f64)] = &[
43+ ("WheelScrollDown", wl_pointer::AxisSource::Wheel, 10.),
44+ ("WheelScrollUp", wl_pointer::AxisSource::Wheel, -10.),
45+];
46+
18 fn parse(keymap: &Keymap, input: &str) -> Vec<Op> {47 fn parse(keymap: &Keymap, input: &str) -> Vec<Op> {
19 let mut ops = Vec::new();48 let mut ops = Vec::new();
20 for part in input.split_ascii_whitespace() {49 for part in input.split_ascii_whitespace() {
21- let name = &part[1..];50+ // Scrolls are momentary, so they are written without a prefix.
51+ let (pressed, name) = match part.as_bytes().first() {
52+ Some(b'+') => (Some(true), &part[1..]),
53+ Some(b'-') => (Some(false), &part[1..]),
54+ _ => (None, part),
55+ };
56+
57+ if let Some((name, source, amount)) = SCROLLS.iter().copied().find(|(n, _, _)| *n == name) {
58+ assert!(
59+ pressed.is_none(),
60+ "scrolls have no press or release: {part}"
61+ );
62+ ops.push(Op::Scroll {
63+ name,
64+ source,
65+ amount,
66+ });
67+ continue;
68+ }
69+
70+ if let Some((name, code)) = MOUSE_BUTTONS.iter().copied().find(|(n, _)| *n == name) {
71+ let state = match pressed {
72+ Some(true) => wl_pointer::ButtonState::Pressed,
73+ Some(false) => wl_pointer::ButtonState::Released,
74+ None => panic!("a mouse button needs a + or - prefix: {part}"),
75+ };
76+ ops.push(Op::MouseButton { name, code, state });
77+ continue;
78+ }
79+
22 let Some(key) = keymap.key_by_name(name) else {80 let Some(key) = keymap.key_by_name(name) else {
23 panic!("unknown key {name}");81 panic!("unknown key {name}");
24 };82 };
25 83
26- let c = part.bytes().next().unwrap();84+ let op = match pressed {
27- let op = match c {85+ Some(true) => Op::Press(key),
28- b'+' => Op::Press(key),86+ Some(false) => Op::Release(key),
29- b'-' => Op::Release(key),87+ None => panic!("a key needs a + or - prefix: {part}"),
30- _ => panic!("keys must begin with + or -, got {c}"),
31 };88 };
32 89
33 ops.push(op);90 ops.push(op);
@@ -37,16 +94,19 @@ fn parse(keymap: &Keymap, input: &str) -> Vec<Op> {
37 94
38 fn set_up(config: &str) -> (Fixture, ClientId, WlSurface) {95 fn set_up(config: &str) -> (Fixture, ClientId, WlSurface) {
39 let mut config = Config::parse_mem(config).unwrap();96 let mut config = Config::parse_mem(config).unwrap();
40- // knuffel doesn't understand #[cfg(test)]...97+
41 for bind in &mut config.binds.0 {98 for bind in &mut config.binds.0 {
42- bind.action = match &bind.action {99+ let actions = match &mut bind.action {
43- BoundAction::Press(_) => BoundAction::Press(Action::TestAction),100+ BoundAction::Press(press) => [Some(press), None],
44- BoundAction::Release(_) => BoundAction::Release(Action::TestAction),101+ BoundAction::Release(release) => [None, Some(release)],
45- BoundAction::Both { .. } => BoundAction::Both {102+ BoundAction::Both { press, release } => [Some(press), Some(release)],
46- press: Action::TestAction,
47- release: Action::TestAction,
48- },
49 };103 };
104+ for action in actions.into_iter().flatten() {
105+ // knuffel doesn't understand #[cfg(test)]...
106+ if !matches!(action, Action::Screenshot(_, _)) {
107+ *action = Action::TestAction;
108+ }
109+ }
50 }110 }
51 111
52 let mut f = Fixture::with_config(config);112 let mut f = Fixture::with_config(config);
@@ -83,46 +143,81 @@ fn run_f(f: &mut Fixture, id: ClientId, surface: &WlSurface, input: &str) -> Str
83 }143 }
84 144
85 for op in ops {145 for op in ops {
86- let (code, key_state) = match op {146+ let prev = f.niri().test_action_count;
87- Op::Press(code) => (code, KeyState::Pressed),
88- Op::Release(code) => (code, KeyState::Released),
89- };
90 147
91- let state = f.niri_state();148+ match op {
92- let keyboard = state.niri.seat.get_keyboard().unwrap();149+ Op::Press(code) | Op::Release(code) => {
93- keyboard.with_xkb_state(state, |xkb| {150+ let key_state = match op {
94- let xkb = xkb.xkb().lock().unwrap();151+ Op::Press(_) => KeyState::Pressed,
95- let xkb_state = unsafe { xkb.state() };152+ Op::Release(_) => KeyState::Released,
96- let keymap = xkb_state.get_keymap();153+ _ => unreachable!(),
154+ };
97 155
98- let c = match key_state {156+ let state = f.niri_state();
99- KeyState::Pressed => "+",157+ let keyboard = state.niri.seat.get_keyboard().unwrap();
100- KeyState::Released => "-",158+ keyboard.with_xkb_state(state, |xkb| {
101- };159+ let xkb = xkb.xkb().lock().unwrap();
160+ let xkb_state = unsafe { xkb.state() };
161+ let keymap = xkb_state.get_keymap();
102 162
103- let name = keymap.key_get_name(code).unwrap_or("None");163+ let c = match key_state {
104- let keysym = xkb_state.key_get_one_sym(code);164+ KeyState::Pressed => "+",
165+ KeyState::Released => "-",
166+ };
105 167
106- let _ = writeln!(&mut rv, "{c}{name} {:>3} {keysym:?}", code.raw());168+ let name = keymap.key_get_name(code).unwrap_or("None");
107- });169+ let keysym = xkb_state.key_get_one_sym(code);
108 170
109- let prev = state.niri.test_action_count;171+ let _ = writeln!(&mut rv, "{c}{name} {:>3} {keysym:?}", code.raw());
172+ });
110 173
111- state.process_input_event(InputEvent::<TestInputBackend>::Keyboard {174+ let state = f.niri_state();
112- event: TestKeyboardKeyEvent {175+ state.process_input_event(InputEvent::<TestInputBackend>::Keyboard {
113- time: InputTime::from_micros(0),176+ event: TestKeyboardKeyEvent {
177+ time: InputTime::from_micros(0),
178+ code,
179+ state: key_state,
180+ count: 1, // niri doesn't use this
181+ },
182+ });
183+ }
184+ Op::MouseButton {
185+ name,
114 code,186 code,
115- state: key_state,187+ state: button_state,
116- count: 1, // niri doesn't use this188+ } => {
117- },189+ let c = match button_state {
118- });190+ wl_pointer::ButtonState::Pressed => "+",
191+ wl_pointer::ButtonState::Released => "-",
192+ _ => unreachable!(),
193+ };
194+ let _ = writeln!(&mut rv, "{c}{name}");
195+
196+ let pointer = f.client(id).state.virtual_pointer();
197+ pointer.button(0, code, button_state);
198+ pointer.frame();
199+ }
200+ Op::Scroll {
201+ name,
202+ source,
203+ amount,
204+ } => {
205+ let _ = writeln!(&mut rv, "{name}");
206+
207+ let pointer = f.client(id).state.virtual_pointer();
208+ pointer.axis_source(source);
209+ pointer.axis(0, wl_pointer::Axis::VerticalScroll, amount);
210+ pointer.frame();
211+ }
212+ }
213+
214+ f.roundtrip(id);
119 215
120 let diff = f.niri().test_action_count - prev;216 let diff = f.niri().test_action_count - prev;
121 for _ in 0..diff {217 for _ in 0..diff {
122 let _ = writeln!(&mut rv, " niri test-action");218 let _ = writeln!(&mut rv, " niri test-action");
123 }219 }
124 220
125- f.roundtrip(id);
126 for event in f.client(id).state.recent_keyboard_events(surface) {221 for event in f.client(id).state.recent_keyboard_events(surface) {
127 let _ = writeln!(&mut rv, " surface {event}");222 let _ = writeln!(&mut rv, " surface {event}");
128 }223 }
@@ -512,3 +607,1239 @@ fn layouts() {
512 "607 "
513 );608 );
514 }609 }
610+
611+#[test]
612+fn release_binds() {
613+ let c = "
614+ binds {
615+ Mod {
616+ release { toggle-overview; }
617+ }
618+ Mod+Q {
619+ release { close-window; }
620+ }
621+ Mod+U { center-column; }
622+ Ctrl+Alt_L {
623+ release { switch-layout \"next\"; }
624+ }
625+ Ctrl+Alt+U { close-window; }
626+ }
627+ ";
628+
629+ // Releasing Mod by itself runs its release action.
630+ assert_snapshot!(run(c, "+LWIN -LWIN"), @"
631+ +LWIN 133 XK_Super_L
632+ surface key pressed: 125
633+ surface modifiers: depressed=64, latched=0, locked=0, group=0
634+ -LWIN 133 XK_Super_L
635+ niri test-action
636+ surface key released: 125
637+ surface modifiers: depressed=0, latched=0, locked=0, group=0
638+ ");
639+
640+ // Pressing another key in between cancels a modifier-only release bind.
641+ assert_snapshot!(run(c, "+LWIN +LatA -LatA -LWIN"), @"
642+ +LWIN 133 XK_Super_L
643+ surface key pressed: 125
644+ surface modifiers: depressed=64, latched=0, locked=0, group=0
645+ +AC01 38 XK_a
646+ surface key pressed: 30
647+ -AC01 38 XK_a
648+ surface key released: 30
649+ -LWIN 133 XK_Super_L
650+ surface key released: 125
651+ surface modifiers: depressed=0, latched=0, locked=0, group=0
652+ ");
653+
654+ // Pressing a key that triggers its own bind cancels the pending release bind.
655+ assert_snapshot!(run(c, "+LWIN +LatU -LatU -LWIN"), @"
656+ +LWIN 133 XK_Super_L
657+ surface key pressed: 125
658+ surface modifiers: depressed=64, latched=0, locked=0, group=0
659+ +AD07 30 XK_u
660+ niri test-action
661+ -AD07 30 XK_u
662+ -LWIN 133 XK_Super_L
663+ surface key released: 125
664+ surface modifiers: depressed=0, latched=0, locked=0, group=0
665+ ");
666+
667+ // A key that was already held when the bind was pressed doesn't cancel it: only input in
668+ // between does.
669+ assert_snapshot!(run(c, "+LatA +LWIN -LatA -LWIN"), @"
670+ +AC01 38 XK_a
671+ surface key pressed: 30
672+ +LWIN 133 XK_Super_L
673+ surface key pressed: 125
674+ surface modifiers: depressed=64, latched=0, locked=0, group=0
675+ -AC01 38 XK_a
676+ surface key released: 30
677+ -LWIN 133 XK_Super_L
678+ niri test-action
679+ surface key released: 125
680+ surface modifiers: depressed=0, latched=0, locked=0, group=0
681+ ");
682+
683+ // A release-only bind on a regular key.
684+ assert_snapshot!(run(c, "+LWIN +LatQ -LatQ -LWIN"), @"
685+ +LWIN 133 XK_Super_L
686+ surface key pressed: 125
687+ surface modifiers: depressed=64, latched=0, locked=0, group=0
688+ +AD01 24 XK_q
689+ -AD01 24 XK_q
690+ niri test-action
691+ -LWIN 133 XK_Super_L
692+ surface key released: 125
693+ surface modifiers: depressed=0, latched=0, locked=0, group=0
694+ ");
695+
696+ // The release action runs even if the modifiers are released before the trigger key.
697+ assert_snapshot!(run(c, "+LWIN +LatQ -LWIN -LatQ"), @"
698+ +LWIN 133 XK_Super_L
699+ surface key pressed: 125
700+ surface modifiers: depressed=64, latched=0, locked=0, group=0
701+ +AD01 24 XK_q
702+ -LWIN 133 XK_Super_L
703+ surface key released: 125
704+ surface modifiers: depressed=0, latched=0, locked=0, group=0
705+ -AD01 24 XK_q
706+ niri test-action
707+ ");
708+
709+ // Another key in between cancels it, no matter the trigger.
710+ assert_snapshot!(run(c, "+LWIN +LatQ +LatA -LatA -LWIN -LatQ"), @"
711+ +LWIN 133 XK_Super_L
712+ surface key pressed: 125
713+ surface modifiers: depressed=64, latched=0, locked=0, group=0
714+ +AD01 24 XK_q
715+ +AC01 38 XK_a
716+ surface key pressed: 30
717+ -AC01 38 XK_a
718+ surface key released: 30
719+ -LWIN 133 XK_Super_L
720+ surface key released: 125
721+ surface modifiers: depressed=0, latched=0, locked=0, group=0
722+ -AD01 24 XK_q
723+ ");
724+
725+ // Ctrl+Alt_L is a modifier-only bind: its whole key consists of modifiers, so releasing the
726+ // combo on its own runs its release action.
727+ assert_snapshot!(run(c, "+LCTL +LALT -LALT -LCTL"), @"
728+ +LCTL 37 XK_Control_L
729+ surface key pressed: 29
730+ surface modifiers: depressed=4, latched=0, locked=0, group=0
731+ +LALT 64 XK_Alt_L
732+ surface key pressed: 56
733+ surface modifiers: depressed=12, latched=0, locked=0, group=0
734+ -LALT 64 XK_Alt_L
735+ niri test-action
736+ surface key released: 56
737+ surface modifiers: depressed=4, latched=0, locked=0, group=0
738+ -LCTL 37 XK_Control_L
739+ surface key released: 29
740+ surface modifiers: depressed=0, latched=0, locked=0, group=0
741+ ");
742+
743+ // Pressing another key in between cancels it, so using Ctrl+Alt+A as an app shortcut does not
744+ // additionally switch the layout.
745+ assert_snapshot!(run(c, "+LCTL +LALT +LatA -LatA -LALT -LCTL"), @"
746+ +LCTL 37 XK_Control_L
747+ surface key pressed: 29
748+ surface modifiers: depressed=4, latched=0, locked=0, group=0
749+ +LALT 64 XK_Alt_L
750+ surface key pressed: 56
751+ surface modifiers: depressed=12, latched=0, locked=0, group=0
752+ +AC01 38 XK_a
753+ surface key pressed: 30
754+ -AC01 38 XK_a
755+ surface key released: 30
756+ -LALT 64 XK_Alt_L
757+ surface key released: 56
758+ surface modifiers: depressed=4, latched=0, locked=0, group=0
759+ -LCTL 37 XK_Control_L
760+ surface key released: 29
761+ surface modifiers: depressed=0, latched=0, locked=0, group=0
762+ ");
763+
764+ // Pressing a key that triggers its own bind cancels the Ctrl+Alt_L release bind.
765+ assert_snapshot!(run(c, "+LCTL +LALT +LatU -LatU -LALT -LCTL"), @"
766+ +LCTL 37 XK_Control_L
767+ surface key pressed: 29
768+ surface modifiers: depressed=4, latched=0, locked=0, group=0
769+ +LALT 64 XK_Alt_L
770+ surface key pressed: 56
771+ surface modifiers: depressed=12, latched=0, locked=0, group=0
772+ +AD07 30 XK_u
773+ niri test-action
774+ -AD07 30 XK_u
775+ -LALT 64 XK_Alt_L
776+ surface key released: 56
777+ surface modifiers: depressed=4, latched=0, locked=0, group=0
778+ -LCTL 37 XK_Control_L
779+ surface key released: 29
780+ surface modifiers: depressed=0, latched=0, locked=0, group=0
781+ ");
782+}
783+
784+#[test]
785+fn press_and_release_binds() {
786+ let c = "
787+ binds {
788+ Mod+Q {
789+ press { close-window; }
790+ release { center-column; }
791+ }
792+ }
793+ ";
794+
795+ // Both actions run, and the key never reaches the surface.
796+ assert_snapshot!(run(c, "+LWIN +LatQ -LatQ -LWIN"), @"
797+ +LWIN 133 XK_Super_L
798+ surface key pressed: 125
799+ surface modifiers: depressed=64, latched=0, locked=0, group=0
800+ +AD01 24 XK_q
801+ niri test-action
802+ -AD01 24 XK_q
803+ niri test-action
804+ -LWIN 133 XK_Super_L
805+ surface key released: 125
806+ surface modifiers: depressed=0, latched=0, locked=0, group=0
807+ ");
808+
809+ // The release action runs even if the modifiers are released first.
810+ assert_snapshot!(run(c, "+LWIN +LatQ -LWIN -LatQ"), @"
811+ +LWIN 133 XK_Super_L
812+ surface key pressed: 125
813+ surface modifiers: depressed=64, latched=0, locked=0, group=0
814+ +AD01 24 XK_q
815+ niri test-action
816+ -LWIN 133 XK_Super_L
817+ surface key released: 125
818+ surface modifiers: depressed=0, latched=0, locked=0, group=0
819+ -AD01 24 XK_q
820+ niri test-action
821+ ");
822+
823+ // Extra modifiers held at release don't stop the release action either.
824+ assert_snapshot!(run(c, "+LWIN +LatQ +LCTL +LFSH -LatQ -LFSH -LCTL -LWIN"), @"
825+ +LWIN 133 XK_Super_L
826+ surface key pressed: 125
827+ surface modifiers: depressed=64, latched=0, locked=0, group=0
828+ +AD01 24 XK_q
829+ niri test-action
830+ +LCTL 37 XK_Control_L
831+ surface key pressed: 29
832+ surface modifiers: depressed=68, latched=0, locked=0, group=0
833+ +LFSH 50 XK_Shift_L
834+ surface key pressed: 42
835+ surface modifiers: depressed=69, latched=0, locked=0, group=0
836+ -AD01 24 XK_Q
837+ niri test-action
838+ -LFSH 50 XK_Shift_L
839+ surface key released: 42
840+ surface modifiers: depressed=68, latched=0, locked=0, group=0
841+ -LCTL 37 XK_Control_L
842+ surface key released: 29
843+ surface modifiers: depressed=64, latched=0, locked=0, group=0
844+ -LWIN 133 XK_Super_L
845+ surface key released: 125
846+ surface modifiers: depressed=0, latched=0, locked=0, group=0
847+ ");
848+
849+ // Push-to-talk: other input in between doesn't cancel the release action.
850+ assert_snapshot!(run(c, "+LWIN +LatQ +LatA -LatA -LatQ -LWIN"), @"
851+ +LWIN 133 XK_Super_L
852+ surface key pressed: 125
853+ surface modifiers: depressed=64, latched=0, locked=0, group=0
854+ +AD01 24 XK_q
855+ niri test-action
856+ +AC01 38 XK_a
857+ surface key pressed: 30
858+ -AC01 38 XK_a
859+ surface key released: 30
860+ -AD01 24 XK_q
861+ niri test-action
862+ -LWIN 133 XK_Super_L
863+ surface key released: 125
864+ surface modifiers: depressed=0, latched=0, locked=0, group=0
865+ ");
866+
867+ // Pressing the key without the mod key held doesn't run either action.
868+ assert_snapshot!(run(c, "+LatQ +LWIN -LatQ -LWIN"), @"
869+ +AD01 24 XK_q
870+ surface key pressed: 16
871+ +LWIN 133 XK_Super_L
872+ surface key pressed: 125
873+ surface modifiers: depressed=64, latched=0, locked=0, group=0
874+ -AD01 24 XK_q
875+ surface key released: 16
876+ -LWIN 133 XK_Super_L
877+ surface key released: 125
878+ surface modifiers: depressed=0, latched=0, locked=0, group=0
879+ ");
880+}
881+
882+#[test]
883+fn generic_modifier_press_release() {
884+ let c = "
885+ binds {
886+ Super+Shift {
887+ press { close-window; }
888+ release { center-column; }
889+ }
890+
891+ Ctrl+Shift {
892+ release { close-window; }
893+ }
894+ }
895+ ";
896+
897+ // Press Super and then two Shifts.
898+ assert_snapshot!(run(c, "+LWIN +LFSH +RTSH -RTSH -LFSH -LWIN"), @"
899+ +LWIN 133 XK_Super_L
900+ surface key pressed: 125
901+ surface modifiers: depressed=64, latched=0, locked=0, group=0
902+ +LFSH 50 XK_Shift_L
903+ niri test-action
904+ surface modifiers: depressed=65, latched=0, locked=0, group=0
905+ +RTSH 62 XK_Shift_R
906+ niri test-action
907+ -RTSH 62 XK_Shift_R
908+ niri test-action
909+ -LFSH 50 XK_Shift_L
910+ niri test-action
911+ surface modifiers: depressed=64, latched=0, locked=0, group=0
912+ -LWIN 133 XK_Super_L
913+ surface key released: 125
914+ surface modifiers: depressed=0, latched=0, locked=0, group=0
915+ ");
916+
917+ // Other release order.
918+ assert_snapshot!(run(c, "+LWIN +LFSH +RTSH -LFSH -RTSH -LWIN"), @"
919+ +LWIN 133 XK_Super_L
920+ surface key pressed: 125
921+ surface modifiers: depressed=64, latched=0, locked=0, group=0
922+ +LFSH 50 XK_Shift_L
923+ niri test-action
924+ surface modifiers: depressed=65, latched=0, locked=0, group=0
925+ +RTSH 62 XK_Shift_R
926+ niri test-action
927+ -LFSH 50 XK_Shift_L
928+ niri test-action
929+ -RTSH 62 XK_Shift_R
930+ niri test-action
931+ surface modifiers: depressed=64, latched=0, locked=0, group=0
932+ -LWIN 133 XK_Super_L
933+ surface key released: 125
934+ surface modifiers: depressed=0, latched=0, locked=0, group=0
935+ ");
936+
937+ // Same but release-only.
938+ assert_snapshot!(run(c, "+LCTL +LFSH +RTSH -RTSH -LFSH -LCTL"), @"
939+ +LCTL 37 XK_Control_L
940+ surface key pressed: 29
941+ surface modifiers: depressed=4, latched=0, locked=0, group=0
942+ +LFSH 50 XK_Shift_L
943+ surface key pressed: 42
944+ surface modifiers: depressed=5, latched=0, locked=0, group=0
945+ +RTSH 62 XK_Shift_R
946+ surface key pressed: 54
947+ -RTSH 62 XK_Shift_R
948+ niri test-action
949+ surface key released: 54
950+ -LFSH 50 XK_Shift_L
951+ surface key released: 42
952+ surface modifiers: depressed=4, latched=0, locked=0, group=0
953+ -LCTL 37 XK_Control_L
954+ surface key released: 29
955+ surface modifiers: depressed=0, latched=0, locked=0, group=0
956+ ");
957+
958+ // Other release order (release-only).
959+ assert_snapshot!(run(c, "+LCTL +LFSH +RTSH -LFSH -RTSH -LCTL"), @"
960+ +LCTL 37 XK_Control_L
961+ surface key pressed: 29
962+ surface modifiers: depressed=4, latched=0, locked=0, group=0
963+ +LFSH 50 XK_Shift_L
964+ surface key pressed: 42
965+ surface modifiers: depressed=5, latched=0, locked=0, group=0
966+ +RTSH 62 XK_Shift_R
967+ surface key pressed: 54
968+ -LFSH 50 XK_Shift_L
969+ surface key released: 42
970+ -RTSH 62 XK_Shift_R
971+ niri test-action
972+ surface key released: 54
973+ surface modifiers: depressed=4, latched=0, locked=0, group=0
974+ -LCTL 37 XK_Control_L
975+ surface key released: 29
976+ surface modifiers: depressed=0, latched=0, locked=0, group=0
977+ ");
978+
979+ // Now, Shift first.
980+ assert_snapshot!(run(c, "+LFSH +LWIN -LWIN -LFSH"), @"
981+ +LFSH 50 XK_Shift_L
982+ surface key pressed: 42
983+ surface modifiers: depressed=1, latched=0, locked=0, group=0
984+ +LWIN 133 XK_Super_L
985+ surface key pressed: 125
986+ surface modifiers: depressed=65, latched=0, locked=0, group=0
987+ -LWIN 133 XK_Super_L
988+ surface key released: 125
989+ surface modifiers: depressed=1, latched=0, locked=0, group=0
990+ -LFSH 50 XK_Shift_L
991+ surface key released: 42
992+ surface modifiers: depressed=0, latched=0, locked=0, group=0
993+ ");
994+
995+ // Two Supers.
996+ assert_snapshot!(run(c, "+LFSH +LWIN +RWIN -RWIN -LWIN -LFSH"), @"
997+ +LFSH 50 XK_Shift_L
998+ surface key pressed: 42
999+ surface modifiers: depressed=1, latched=0, locked=0, group=0
1000+ +LWIN 133 XK_Super_L
1001+ surface key pressed: 125
1002+ surface modifiers: depressed=65, latched=0, locked=0, group=0
1003+ +RWIN 134 XK_Super_R
1004+ surface key pressed: 126
1005+ -RWIN 134 XK_Super_R
1006+ surface key released: 126
1007+ -LWIN 133 XK_Super_L
1008+ surface key released: 125
1009+ surface modifiers: depressed=1, latched=0, locked=0, group=0
1010+ -LFSH 50 XK_Shift_L
1011+ surface key released: 42
1012+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1013+ ");
1014+
1015+ // Unrelated modifier in the middle.
1016+ assert_snapshot!(run(c, "+LWIN +LCTL +LFSH -LCTL -LFSH -LWIN"), @"
1017+ +LWIN 133 XK_Super_L
1018+ surface key pressed: 125
1019+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1020+ +LCTL 37 XK_Control_L
1021+ surface key pressed: 29
1022+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1023+ +LFSH 50 XK_Shift_L
1024+ surface key pressed: 42
1025+ surface modifiers: depressed=69, latched=0, locked=0, group=0
1026+ -LCTL 37 XK_Control_L
1027+ surface key released: 29
1028+ surface modifiers: depressed=65, latched=0, locked=0, group=0
1029+ -LFSH 50 XK_Shift_L
1030+ surface key released: 42
1031+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1032+ -LWIN 133 XK_Super_L
1033+ surface key released: 125
1034+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1035+ ");
1036+}
1037+
1038+#[test]
1039+fn mod_key_press_and_release_bind() {
1040+ let c = "
1041+ binds {
1042+ Mod {
1043+ press { close-window; }
1044+ release { center-column; }
1045+ }
1046+ }
1047+ ";
1048+
1049+ // Both actions run, and the window doesn't see the key press or release themselves, only the
1050+ // modifier state.
1051+ assert_snapshot!(run(c, "+LWIN -LWIN"), @"
1052+ +LWIN 133 XK_Super_L
1053+ niri test-action
1054+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1055+ -LWIN 133 XK_Super_L
1056+ niri test-action
1057+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1058+ ");
1059+
1060+ // Other input in between doesn't cancel the release action.
1061+ assert_snapshot!(run(c, "+LWIN +LatA -LatA -LWIN"), @"
1062+ +LWIN 133 XK_Super_L
1063+ niri test-action
1064+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1065+ +AC01 38 XK_a
1066+ surface key pressed: 30
1067+ -AC01 38 XK_a
1068+ surface key released: 30
1069+ -LWIN 133 XK_Super_L
1070+ niri test-action
1071+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1072+ ");
1073+}
1074+
1075+#[test]
1076+fn mod_key_press_and_release_bind_focus_change() {
1077+ let c = "
1078+ binds {
1079+ Mod {
1080+ press { close-window; }
1081+ release { center-column; }
1082+ }
1083+ }
1084+ ";
1085+
1086+ let (mut f, id, surface) = set_up(c);
1087+
1088+ // Hold mod: the press action runs, and the window sees the modifier state.
1089+ run_f(&mut f, id, &surface, "+LWIN");
1090+
1091+ // A new window takes the keyboard focus while mod is held.
1092+ let window = f.client(id).create_window();
1093+ let surface2 = window.surface.clone();
1094+ window.commit();
1095+ f.roundtrip(id);
1096+ let window = f.client(id).window(&surface2);
1097+ window.attach_new_buffer();
1098+ window.ack_last_and_commit();
1099+ f.roundtrip(id);
1100+
1101+ // The new window doesn't see the key press or release, but the focus change and the
1102+ // intercepted release still update its modifier state, so it doesn't get stuck with mod held.
1103+ assert_snapshot!(run_f(&mut f, id, &surface2, "-LWIN +LatA -LatA"), @"
1104+ surface enter: []
1105+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1106+ -LWIN 133 XK_Super_L
1107+ niri test-action
1108+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1109+ +AC01 38 XK_a
1110+ surface key pressed: 30
1111+ -AC01 38 XK_a
1112+ surface key released: 30
1113+ ");
1114+}
1115+
1116+#[test]
1117+fn modifier_key_trigger_binds() {
1118+ let c = "
1119+ binds {
1120+ Mod+Control_L {
1121+ press { close-window; }
1122+ release { center-column; }
1123+ }
1124+ Alt+Control_L { close-window; }
1125+ }
1126+ ";
1127+
1128+ // A modifier key used as the trigger is intercepted, so the window doesn't see the key press
1129+ // and release, only the modifier state; both actions run.
1130+ assert_snapshot!(run(c, "+LWIN +LCTL -LCTL -LWIN"), @"
1131+ +LWIN 133 XK_Super_L
1132+ surface key pressed: 125
1133+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1134+ +LCTL 37 XK_Control_L
1135+ niri test-action
1136+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1137+ -LCTL 37 XK_Control_L
1138+ niri test-action
1139+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1140+ -LWIN 133 XK_Super_L
1141+ surface key released: 125
1142+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1143+ ");
1144+
1145+ // Without the mod key held the bind doesn't match.
1146+ assert_snapshot!(run(c, "+LCTL -LCTL"), @"
1147+ +LCTL 37 XK_Control_L
1148+ surface key pressed: 29
1149+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1150+ -LCTL 37 XK_Control_L
1151+ surface key released: 29
1152+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1153+ ");
1154+
1155+ // With Alt held instead, the Control_L press is intercepted, and only the press action runs.
1156+ assert_snapshot!(run(c, "+LALT +LCTL -LCTL -LALT"), @"
1157+ +LALT 64 XK_Alt_L
1158+ surface key pressed: 56
1159+ surface modifiers: depressed=8, latched=0, locked=0, group=0
1160+ +LCTL 37 XK_Control_L
1161+ niri test-action
1162+ surface modifiers: depressed=12, latched=0, locked=0, group=0
1163+ -LCTL 37 XK_Control_L
1164+ surface modifiers: depressed=8, latched=0, locked=0, group=0
1165+ -LALT 64 XK_Alt_L
1166+ surface key released: 56
1167+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1168+ ");
1169+}
1170+
1171+#[test]
1172+fn release_bind_on_modifier_key() {
1173+ let c = "
1174+ binds {
1175+ Mod+Control_L {
1176+ release { close-window; }
1177+ }
1178+ }
1179+ ";
1180+
1181+ // The press is forwarded so that apps see the modifier, and the release action runs when the
1182+ // modifier is released on its own.
1183+ assert_snapshot!(run(c, "+LWIN +LCTL -LCTL -LWIN"), @"
1184+ +LWIN 133 XK_Super_L
1185+ surface key pressed: 125
1186+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1187+ +LCTL 37 XK_Control_L
1188+ surface key pressed: 29
1189+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1190+ -LCTL 37 XK_Control_L
1191+ niri test-action
1192+ surface key released: 29
1193+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1194+ -LWIN 133 XK_Super_L
1195+ surface key released: 125
1196+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1197+ ");
1198+
1199+ // Pressing another key in between cancels the release action.
1200+ assert_snapshot!(run(c, "+LWIN +LCTL +LFSH -LCTL -LFSH -LWIN"), @"
1201+ +LWIN 133 XK_Super_L
1202+ surface key pressed: 125
1203+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1204+ +LCTL 37 XK_Control_L
1205+ surface key pressed: 29
1206+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1207+ +LFSH 50 XK_Shift_L
1208+ surface key pressed: 42
1209+ surface modifiers: depressed=69, latched=0, locked=0, group=0
1210+ -LCTL 37 XK_Control_L
1211+ surface key released: 29
1212+ surface modifiers: depressed=65, latched=0, locked=0, group=0
1213+ -LFSH 50 XK_Shift_L
1214+ surface key released: 42
1215+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1216+ -LWIN 133 XK_Super_L
1217+ surface key released: 125
1218+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1219+ ");
1220+}
1221+
1222+#[test]
1223+fn modifier_key_and_keysym_triggers() {
1224+ // `Ctrl` is not the mod key, so it is matched by keysym directly: both Control_L and
1225+ // Control_R trigger it.
1226+ let by_modifier = "
1227+ binds {
1228+ Ctrl {
1229+ release { close-window; }
1230+ }
1231+ }
1232+ ";
1233+
1234+ assert_snapshot!(run(by_modifier, "+LCTL -LCTL"), @"
1235+ +LCTL 37 XK_Control_L
1236+ surface key pressed: 29
1237+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1238+ -LCTL 37 XK_Control_L
1239+ niri test-action
1240+ surface key released: 29
1241+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1242+ ");
1243+
1244+ assert_snapshot!(run(by_modifier, "+RCTL -RCTL"), @"
1245+ +RCTL 105 XK_Control_R
1246+ surface key pressed: 97
1247+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1248+ -RCTL 105 XK_Control_R
1249+ niri test-action
1250+ surface key released: 97
1251+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1252+ ");
1253+
1254+ // `Control_L` is a specific key instead, so it doesn't trigger on Control_R, which reaches
1255+ // the window instead.
1256+ let by_keysym = "
1257+ binds {
1258+ Control_L {
1259+ release { close-window; }
1260+ }
1261+ }
1262+ ";
1263+
1264+ assert_snapshot!(run(by_keysym, "+LCTL -LCTL"), @"
1265+ +LCTL 37 XK_Control_L
1266+ surface key pressed: 29
1267+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1268+ -LCTL 37 XK_Control_L
1269+ niri test-action
1270+ surface key released: 29
1271+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1272+ ");
1273+
1274+ assert_snapshot!(run(by_keysym, "+RCTL -RCTL"), @"
1275+ +RCTL 105 XK_Control_R
1276+ surface key pressed: 97
1277+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1278+ -RCTL 105 XK_Control_R
1279+ surface key released: 97
1280+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1281+ ");
1282+}
1283+
1284+#[test]
1285+fn mod_key_trigger_with_own_modifier() {
1286+ // The trigger is the configured mod key, and has Mod at a modifier.
1287+ let bind_by_mod = "
1288+ binds {
1289+ Mod+Super_L { close-window; }
1290+ }
1291+ ";
1292+
1293+ assert_snapshot!(run(bind_by_mod, "+LWIN -LWIN"), @"
1294+ +LWIN 133 XK_Super_L
1295+ niri test-action
1296+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1297+ -LWIN 133 XK_Super_L
1298+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1299+ ");
1300+
1301+ let bind_by_keysym = "
1302+ binds {
1303+ Super_L { close-window; }
1304+ }
1305+ ";
1306+
1307+ assert_snapshot!(run(bind_by_keysym, "+LWIN -LWIN"), @"
1308+ +LWIN 133 XK_Super_L
1309+ niri test-action
1310+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1311+ -LWIN 133 XK_Super_L
1312+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1313+ ");
1314+
1315+ let bind_by_generic = "
1316+ binds {
1317+ Mod+Super { close-window; }
1318+ }
1319+ ";
1320+
1321+ assert_snapshot!(run(bind_by_generic, "+LWIN -LWIN"), @"
1322+ +LWIN 133 XK_Super_L
1323+ niri test-action
1324+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1325+ -LWIN 133 XK_Super_L
1326+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1327+ ");
1328+}
1329+
1330+#[test]
1331+fn release_bind_requires_matching_modifiers_on_press() {
1332+ let c = "
1333+ binds {
1334+ Mod+Ctrl+Q {
1335+ release { close-window; }
1336+ }
1337+ }
1338+ ";
1339+
1340+ // Pressing Q without the mod key held forwards it and doesn't arm the release action, even if
1341+ // the mod key is held by the time Q is released.
1342+ assert_snapshot!(run(c, "+LCTL +LatQ +LWIN -LatQ -LWIN -LCTL"), @"
1343+ +LCTL 37 XK_Control_L
1344+ surface key pressed: 29
1345+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1346+ +AD01 24 XK_q
1347+ surface key pressed: 16
1348+ +LWIN 133 XK_Super_L
1349+ surface key pressed: 125
1350+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1351+ -AD01 24 XK_q
1352+ surface key released: 16
1353+ -LWIN 133 XK_Super_L
1354+ surface key released: 125
1355+ surface modifiers: depressed=4, latched=0, locked=0, group=0
1356+ -LCTL 37 XK_Control_L
1357+ surface key released: 29
1358+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1359+ ");
1360+}
1361+
1362+/// `Mod` and `Mod+Q` release-only binds, used by the inhibitor tests below.
1363+const INHIBITING_BINDS: &str = "
1364+ binds {
1365+ Mod {
1366+ release { toggle-overview; }
1367+ }
1368+ Mod+Q {
1369+ release { close-window; }
1370+ }
1371+ }
1372+";
1373+
1374+#[test]
1375+fn inhibiting_release_binds() {
1376+ let (mut f, id, surface) = set_up(INHIBITING_BINDS);
1377+ let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface);
1378+ f.roundtrip(id);
1379+
1380+ // While inhibiting, release binds don't run, and the whole key is forwarded to the window.
1381+ assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN +LatQ -LatQ -LWIN"), @"
1382+ +LWIN 133 XK_Super_L
1383+ surface key pressed: 125
1384+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1385+ +AD01 24 XK_q
1386+ surface key pressed: 16
1387+ -AD01 24 XK_q
1388+ surface key released: 16
1389+ -LWIN 133 XK_Super_L
1390+ surface key released: 125
1391+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1392+ ");
1393+}
1394+
1395+#[test]
1396+fn release_binds_run_after_the_inhibitor_is_gone() {
1397+ let (mut f, id, surface) = set_up(INHIBITING_BINDS);
1398+ let inhibitor = f.client(id).state.inhibit_shortcuts(&surface);
1399+ f.roundtrip(id);
1400+ inhibitor.destroy();
1401+ f.roundtrip(id);
1402+
1403+ assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN +LatQ -LatQ -LWIN"), @"
1404+ +LWIN 133 XK_Super_L
1405+ surface key pressed: 125
1406+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1407+ +AD01 24 XK_q
1408+ -AD01 24 XK_q
1409+ niri test-action
1410+ -LWIN 133 XK_Super_L
1411+ surface key released: 125
1412+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1413+ ");
1414+}
1415+
1416+#[test]
1417+fn deactivating_the_inhibitor_does_not_arm_a_release_bind_retroactively() {
1418+ let (mut f, id, surface) = set_up(INHIBITING_BINDS);
1419+ let inhibitor = f.client(id).state.inhibit_shortcuts(&surface);
1420+ f.roundtrip(id);
1421+
1422+ // The press while inhibiting is forwarded and arms nothing.
1423+ run_f(&mut f, id, &surface, "+LWIN +LatQ");
1424+
1425+ inhibitor.destroy();
1426+ f.roundtrip(id);
1427+
1428+ // The release is still forwarded, and still runs no action.
1429+ assert_snapshot!(run_f(&mut f, id, &surface, "-LatQ -LWIN"), @"
1430+ -AD01 24 XK_q
1431+ surface key released: 16
1432+ -LWIN 133 XK_Super_L
1433+ surface key released: 125
1434+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1435+ ");
1436+}
1437+
1438+#[test]
1439+fn activating_the_inhibitor_keeps_an_armed_release_bind() {
1440+ let (mut f, id, surface) = set_up(INHIBITING_BINDS);
1441+
1442+ // The press while not inhibiting is intercepted and arms the release action.
1443+ run_f(&mut f, id, &surface, "+LWIN +LatQ");
1444+
1445+ let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface);
1446+ f.roundtrip(id);
1447+
1448+ // The release stays intercepted, and still runs the armed release action.
1449+ assert_snapshot!(run_f(&mut f, id, &surface, "-LatQ -LWIN"), @"
1450+ -AD01 24 XK_q
1451+ niri test-action
1452+ -LWIN 133 XK_Super_L
1453+ surface key released: 125
1454+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1455+ ");
1456+}
1457+
1458+#[test]
1459+fn inhibiting_a_modifier_press_still_forwards_it() {
1460+ let (mut f, id, surface) = set_up(INHIBITING_BINDS);
1461+ let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface);
1462+ f.roundtrip(id);
1463+
1464+ // A modifier press is forwarded either way; the inhibitor only decides whether it arms the
1465+ // release action, and here it doesn't.
1466+ assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN"), @"
1467+ +LWIN 133 XK_Super_L
1468+ surface key pressed: 125
1469+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1470+ ");
1471+}
1472+
1473+#[test]
1474+fn armed_modifier_release_runs_after_the_inhibitor_activates() {
1475+ let (mut f, id, surface) = set_up(INHIBITING_BINDS);
1476+
1477+ // The modifier press while not inhibiting arms its release action.
1478+ run_f(&mut f, id, &surface, "+LWIN");
1479+
1480+ let _inhibitor = f.client(id).state.inhibit_shortcuts(&surface);
1481+ f.roundtrip(id);
1482+
1483+ assert_snapshot!(run_f(&mut f, id, &surface, "-LWIN"), @"
1484+ -LWIN 133 XK_Super_L
1485+ niri test-action
1486+ surface key released: 125
1487+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1488+ ");
1489+}
1490+
1491+#[test]
1492+fn egl_release_action_runs_when_press_opens_screenshot_ui() {
1493+ // Release actions must always run if their press action ran,
1494+ // including if the screenshot UI is opened before the key is released.
1495+ let c = "
1496+ binds {
1497+ Mod+Q {
1498+ press { screenshot; }
1499+ release { close-window; }
1500+ }
1501+ }
1502+ ";
1503+
1504+ let (mut f, id, surface) = set_up(c);
1505+ f.niri_state().backend.headless().add_renderer().unwrap();
1506+
1507+ assert_snapshot!(run_f(&mut f, id, &surface, "+LWIN +LatQ -LatQ -LWIN"), @"
1508+ +LWIN 133 XK_Super_L
1509+ surface key pressed: 125
1510+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1511+ +AD01 24 XK_q
1512+ surface leave
1513+ -AD01 24 XK_q
1514+ niri test-action
1515+ -LWIN 133 XK_Super_L
1516+ ");
1517+ assert!(f.niri().screenshot_ui.is_open());
1518+}
1519+
1520+#[test]
1521+fn cooldown_skips_whole_bind() {
1522+ // A bind on cooldown is skipped entirely: its press runs no action, and no release action is
1523+ // recorded, so its release runs no action either.
1524+ let c = "
1525+ binds {
1526+ Mod+MouseLeft cooldown-ms=1000 {
1527+ press { close-window; }
1528+ release { center-column; }
1529+ }
1530+ }
1531+ ";
1532+
1533+ assert_snapshot!(run(c, "+LWIN +MouseLeft -MouseLeft +MouseLeft -MouseLeft -LWIN"), @"
1534+ +LWIN 133 XK_Super_L
1535+ surface key pressed: 125
1536+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1537+ +MouseLeft
1538+ niri test-action
1539+ -MouseLeft
1540+ niri test-action
1541+ +MouseLeft
1542+ -MouseLeft
1543+ -LWIN 133 XK_Super_L
1544+ surface key released: 125
1545+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1546+ ");
1547+}
1548+
1549+#[test]
1550+fn cooldown_only_skips_the_action() {
1551+ let c = "
1552+ binds {
1553+ Mod+Control_L cooldown-ms=1000 { close-window; }
1554+ }
1555+ ";
1556+
1557+ // The cooldown only skips the action: the second press runs no action, but it is still
1558+ // intercepted and hidden from the window, only advertising the modifier state.
1559+ assert_snapshot!(run(c, "+LWIN +LCTL -LCTL +LCTL"), @"
1560+ +LWIN 133 XK_Super_L
1561+ surface key pressed: 125
1562+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1563+ +LCTL 37 XK_Control_L
1564+ niri test-action
1565+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1566+ -LCTL 37 XK_Control_L
1567+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1568+ +LCTL 37 XK_Control_L
1569+ surface modifiers: depressed=68, latched=0, locked=0, group=0
1570+ ");
1571+}
1572+
1573+#[test]
1574+fn cooldown_applies_to_release_action_of_release_only_bind() {
1575+ // The release action is what starts this bind, so the cooldown rate-limits it: it runs for the
1576+ // first release, and not for the second while the bind is on cooldown.
1577+ let c = "
1578+ binds {
1579+ Mod cooldown-ms=1000 {
1580+ release { toggle-overview; }
1581+ }
1582+ }
1583+ ";
1584+
1585+ assert_snapshot!(run(c, "+LWIN -LWIN +LWIN -LWIN"), @"
1586+ +LWIN 133 XK_Super_L
1587+ surface key pressed: 125
1588+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1589+ -LWIN 133 XK_Super_L
1590+ niri test-action
1591+ surface key released: 125
1592+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1593+ +LWIN 133 XK_Super_L
1594+ surface key pressed: 125
1595+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1596+ -LWIN 133 XK_Super_L
1597+ surface key released: 125
1598+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1599+ ");
1600+}
1601+
1602+#[test]
1603+fn cooldown_applies_to_press_action_only() {
1604+ // The cooldown applies to the press action only, and the release action runs only if its press
1605+ // action ran, so the second press runs no action at all.
1606+ let c = "
1607+ binds {
1608+ Mod+Q cooldown-ms=1000 {
1609+ press { close-window; }
1610+ release { center-column; }
1611+ }
1612+ }
1613+ ";
1614+
1615+ assert_snapshot!(run(c, "+LWIN +LatQ -LatQ +LatQ -LatQ -LWIN"), @"
1616+ +LWIN 133 XK_Super_L
1617+ surface key pressed: 125
1618+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1619+ +AD01 24 XK_q
1620+ niri test-action
1621+ -AD01 24 XK_q
1622+ niri test-action
1623+ +AD01 24 XK_q
1624+ -AD01 24 XK_q
1625+ -LWIN 133 XK_Super_L
1626+ surface key released: 125
1627+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1628+ ");
1629+}
1630+
1631+#[test]
1632+fn release_bind_runs_when_mru_closes() {
1633+ // `Alt` is a push-to-talk bind: the default Alt+Tab opens the MRU, and releasing Alt closes it
1634+ // instead of going through the regular bind lookup, so its release action must still run.
1635+ let c = "
1636+ binds {
1637+ Alt {
1638+ press { spawn \"ptt-toggle\"; }
1639+ release { spawn \"ptt-toggle\"; }
1640+ }
1641+ }
1642+ ";
1643+
1644+ assert_snapshot!(run(c, "+LALT +TAB -LALT"), @"
1645+ +LALT 64 XK_Alt_L
1646+ niri test-action
1647+ surface modifiers: depressed=8, latched=0, locked=0, group=0
1648+ +TAB 23 XK_Tab
1649+ surface leave
1650+ -LALT 64 XK_Alt_L
1651+ niri test-action
1652+ surface enter: []
1653+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1654+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1655+ ");
1656+}
1657+
1658+#[test]
1659+fn mru_close_forwards_key_release_without_bind() {
1660+ // No bind is recorded for Alt, so releasing it while the MRU is open closes the MRU and the
1661+ // release is forwarded to the window like any other unsuppressed key.
1662+ let c = "
1663+ binds {
1664+ Mod+Q { close-window; }
1665+ }
1666+ ";
1667+
1668+ assert_snapshot!(run(c, "+LALT +TAB -LALT"), @"
1669+ +LALT 64 XK_Alt_L
1670+ surface key pressed: 56
1671+ surface modifiers: depressed=8, latched=0, locked=0, group=0
1672+ +TAB 23 XK_Tab
1673+ surface leave
1674+ -LALT 64 XK_Alt_L
1675+ surface enter: [56]
1676+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1677+ surface key released: 56
1678+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1679+ ");
1680+}
1681+
1682+#[test]
1683+fn vt_switch_runs_release_actions_of_held_binds() {
1684+ // A VT switch can take the input away without ever delivering the releases of everything held
1685+ // down, so the held binds are treated as released rather than forgotten.
1686+ let c = "
1687+ binds {
1688+ Mod+Q {
1689+ press { close-window; }
1690+ release { center-column; }
1691+ }
1692+ Mod+MouseLeft {
1693+ press { close-window; }
1694+ release { center-column; }
1695+ }
1696+ }
1697+ ";
1698+
1699+ let (mut f, id, surface) = set_up(c);
1700+
1701+ // Hold a key and a mouse button, both with press and release actions.
1702+ run_f(&mut f, id, &surface, "+LWIN +LatQ +MouseLeft");
1703+ assert_eq!(f.niri().test_action_count, 2);
1704+
1705+ // The VT switch runs both release actions.
1706+ f.niri_state().do_action(Action::ChangeVt(1), false);
1707+ assert_eq!(f.niri().test_action_count, 4);
1708+
1709+ // If the releases arrive anyway, they run nothing.
1710+ run_f(&mut f, id, &surface, "-LatQ -MouseLeft");
1711+ assert_eq!(f.niri().test_action_count, 4);
1712+}
1713+
1714+#[test]
1715+fn other_input_cancels_release_only_mouse_bind() {
1716+ // A release-only mouse bind is cancelled by other input in between, just like a release-only
1717+ // keyboard bind.
1718+ let c = "
1719+ binds {
1720+ Mod+MouseLeft {
1721+ release { close-window; }
1722+ }
1723+ }
1724+ ";
1725+
1726+ // Pressing a key in between cancels it.
1727+ assert_snapshot!(run(c, "+LWIN +MouseLeft +LatA -LatA -MouseLeft -LWIN"), @"
1728+ +LWIN 133 XK_Super_L
1729+ surface key pressed: 125
1730+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1731+ +MouseLeft
1732+ +AC01 38 XK_a
1733+ surface key pressed: 30
1734+ -AC01 38 XK_a
1735+ surface key released: 30
1736+ -MouseLeft
1737+ -LWIN 133 XK_Super_L
1738+ surface key released: 125
1739+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1740+ ");
1741+
1742+ // Pressing another mouse button in between cancels it.
1743+ assert_snapshot!(run(c, "+LWIN +MouseLeft +MouseRight -MouseRight -MouseLeft -LWIN"), @"
1744+ +LWIN 133 XK_Super_L
1745+ surface key pressed: 125
1746+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1747+ +MouseLeft
1748+ +MouseRight
1749+ -MouseRight
1750+ -MouseLeft
1751+ -LWIN 133 XK_Super_L
1752+ surface key released: 125
1753+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1754+ ");
1755+
1756+ // With nothing else in between, the release action runs.
1757+ assert_snapshot!(run(c, "+LWIN +MouseLeft -MouseLeft -LWIN"), @"
1758+ +LWIN 133 XK_Super_L
1759+ surface key pressed: 125
1760+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1761+ +MouseLeft
1762+ -MouseLeft
1763+ niri test-action
1764+ -LWIN 133 XK_Super_L
1765+ surface key released: 125
1766+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1767+ ");
1768+}
1769+
1770+#[test]
1771+fn mouse_button_press_cancels_release_only_key_bind() {
1772+ // A mouse button press is other input, so it interrupts a pending release-only keyboard bind,
1773+ // including one on a bare modifier. Only the mouse bind's press action runs.
1774+ let c = "
1775+ binds {
1776+ Mod {
1777+ release { center-column; }
1778+ }
1779+ Mod+MouseLeft { close-window; }
1780+ }
1781+ ";
1782+
1783+ assert_snapshot!(run(c, "+LWIN +MouseLeft -MouseLeft -LWIN"), @"
1784+ +LWIN 133 XK_Super_L
1785+ surface key pressed: 125
1786+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1787+ +MouseLeft
1788+ niri test-action
1789+ -MouseLeft
1790+ -LWIN 133 XK_Super_L
1791+ surface key released: 125
1792+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1793+ ");
1794+}
1795+
1796+#[test]
1797+fn scroll_cancels_release_only_key_bind() {
1798+ // Scrolling is input, so it cancels a pending release-only bind. Here there is no scroll bind
1799+ // for the held modifiers.
1800+ let c = "
1801+ binds {
1802+ Mod {
1803+ release { center-column; }
1804+ }
1805+ }
1806+ ";
1807+
1808+ // The scroll in between cancels the pending release bind, so the release runs no action.
1809+ assert_snapshot!(run(c, "+LWIN WheelScrollDown -LWIN"), @"
1810+ +LWIN 133 XK_Super_L
1811+ surface key pressed: 125
1812+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1813+ WheelScrollDown
1814+ -LWIN 133 XK_Super_L
1815+ surface key released: 125
1816+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1817+ ");
1818+
1819+ // Without a scroll in between, the release action runs.
1820+ assert_snapshot!(run(c, "+LWIN -LWIN"), @"
1821+ +LWIN 133 XK_Super_L
1822+ surface key pressed: 125
1823+ surface modifiers: depressed=64, latched=0, locked=0, group=0
1824+ -LWIN 133 XK_Super_L
1825+ niri test-action
1826+ surface key released: 125
1827+ surface modifiers: depressed=0, latched=0, locked=0, group=0
1828+ ");
1829+}
1830+
1831+#[test]
1832+fn redundant_modifier_binds() {
1833+ // Binds should behave identically with or without redundant modifiers.
1834+ let input = "+LALT -LALT";
1835+ assert_eq!(
1836+ run("binds { Alt_L { release { close-window; }; }; }", input),
1837+ run("binds { Alt+Alt_L { release { close-window; }; }; }", input)
1838+ );
1839+
1840+ let input = "+LALT +LatQ -LatQ -LALT";
1841+ assert_eq!(
1842+ run("binds { Alt+Q { release { close-window; }; }; }", input),
1843+ run("binds { Alt+Alt+Q { release { close-window; }; }; }", input)
1844+ );
1845+}
src/tests/client.rsmodified+13 -0
@@ -62,6 +62,7 @@ pub struct State {
62 pub xdg_wm_base: Option<XdgWmBase>,62 pub xdg_wm_base: Option<XdgWmBase>,
63 pub layer_shell: Option<ZwlrLayerShellV1>,63 pub layer_shell: Option<ZwlrLayerShellV1>,
64 pub virtual_pointer_manager: Option<ZwlrVirtualPointerManagerV1>,64 pub virtual_pointer_manager: Option<ZwlrVirtualPointerManagerV1>,
65+ pub virtual_pointer: Option<ZwlrVirtualPointerV1>,
65 pub spbm: Option<WpSinglePixelBufferManagerV1>,66 pub spbm: Option<WpSinglePixelBufferManagerV1>,
66 pub viewporter: Option<WpViewporter>,67 pub viewporter: Option<WpViewporter>,
67 pub ksim: Option<ZwpKeyboardShortcutsInhibitManagerV1>,68 pub ksim: Option<ZwpKeyboardShortcutsInhibitManagerV1>,
@@ -262,6 +263,7 @@ impl Client {
262 xdg_wm_base: None,263 xdg_wm_base: None,
263 layer_shell: None,264 layer_shell: None,
264 virtual_pointer_manager: None,265 virtual_pointer_manager: None,
266+ virtual_pointer: None,
265 spbm: None,267 spbm: None,
266 viewporter: None,268 viewporter: None,
267 ksim: None,269 ksim: None,
@@ -430,6 +432,17 @@ impl State {
430 let seat = self.seats.keys().next().unwrap();432 let seat = self.seats.keys().next().unwrap();
431 ksim.inhibit_shortcuts(surface, seat, &self.qh, ())433 ksim.inhibit_shortcuts(surface, seat, &self.qh, ())
432 }434 }
435+
436+ pub fn virtual_pointer(&mut self) -> ZwlrVirtualPointerV1 {
437+ if let Some(pointer) = &self.virtual_pointer {
438+ return pointer.clone();
439+ }
440+
441+ let manager = self.virtual_pointer_manager.as_ref().unwrap();
442+ let pointer = manager.create_virtual_pointer(None, &self.qh, ());
443+ self.virtual_pointer = Some(pointer.clone());
444+ pointer
445+ }
433 }446 }
434 447
435 impl Window {448 impl Window {
src/tests/virtual_pointer.rsmodified+1 -2
@@ -8,8 +8,7 @@ fn axis_discrete_overflow() {
8 let id = f.add_client();8 let id = f.add_client();
9 9
10 let client = f.client(id);10 let client = f.client(id);
11- let manager = client.state.virtual_pointer_manager.as_ref().unwrap();11+ let pointer = client.state.virtual_pointer();
12- let pointer = manager.create_virtual_pointer(None, &client.qh, ());
13 pointer.axis_discrete(0, wl_pointer::Axis::VerticalScroll, 0., i32::MAX);12 pointer.axis_discrete(0, wl_pointer::Axis::VerticalScroll, 0., i32::MAX);
14 f.roundtrip(id);13 f.roundtrip(id);
15 }14 }