Implement key repeat for compositor binds
Browse files at this commitd3aebdbec4ae2c1ac4199cdd4e95a8d218362b25Parent: a56e4ff3 changed files +73 -2
- niri-config/src/lib.rs +16 -0
- src/input/mod.rs +55 -2
- src/niri.rs +2 -0
niri-config/src/lib.rsmodified+16 -0
| @@ -886,6 +886,7 @@ pub struct Binds(pub Vec<Bind>); | |||
| 886 | pub struct Bind { | 886 | pub struct Bind { |
| 887 | pub key: Key, | 887 | pub key: Key, |
| 888 | pub action: Action, | 888 | pub action: Action, |
| 889 | + pub repeat: bool, | ||
| 889 | pub cooldown: Option<Duration>, | 890 | pub cooldown: Option<Duration>, |
| 890 | pub allow_when_locked: bool, | 891 | pub allow_when_locked: bool, |
| 891 | } | 892 | } |
| @@ -2217,11 +2218,15 @@ where | |||
| 2217 | .parse::<Key>() | 2218 | .parse::<Key>() |
| 2218 | .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?; | 2219 | .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?; |
| 2219 | 2220 | ||
| 2221 | + let mut repeat = true; | ||
| 2220 | let mut cooldown = None; | 2222 | let mut cooldown = None; |
| 2221 | let mut allow_when_locked = false; | 2223 | let mut allow_when_locked = false; |
| 2222 | let mut allow_when_locked_node = None; | 2224 | let mut allow_when_locked_node = None; |
| 2223 | for (name, val) in &node.properties { | 2225 | for (name, val) in &node.properties { |
| 2224 | match &***name { | 2226 | match &***name { |
| 2227 | + "repeat" => { | ||
| 2228 | + repeat = knuffel::traits::DecodeScalar::decode(val, ctx)?; | ||
| 2229 | + } | ||
| 2225 | "cooldown-ms" => { | 2230 | "cooldown-ms" => { |
| 2226 | cooldown = Some(Duration::from_millis( | 2231 | cooldown = Some(Duration::from_millis( |
| 2227 | knuffel::traits::DecodeScalar::decode(val, ctx)?, | 2232 | knuffel::traits::DecodeScalar::decode(val, ctx)?, |
| @@ -2249,6 +2254,7 @@ where | |||
| 2249 | let dummy = Self { | 2254 | let dummy = Self { |
| 2250 | key, | 2255 | key, |
| 2251 | action: Action::Spawn(vec![]), | 2256 | action: Action::Spawn(vec![]), |
| 2257 | + repeat: true, | ||
| 2252 | cooldown: None, | 2258 | cooldown: None, |
| 2253 | allow_when_locked: false, | 2259 | allow_when_locked: false, |
| 2254 | }; | 2260 | }; |
| @@ -2276,6 +2282,7 @@ where | |||
| 2276 | Ok(Self { | 2282 | Ok(Self { |
| 2277 | key, | 2283 | key, |
| 2278 | action, | 2284 | action, |
| 2285 | + repeat, | ||
| 2279 | cooldown, | 2286 | cooldown, |
| 2280 | allow_when_locked, | 2287 | allow_when_locked, |
| 2281 | }) | 2288 | }) |
| @@ -2844,6 +2851,7 @@ mod tests { | |||
| 2844 | modifiers: Modifiers::COMPOSITOR, | 2851 | modifiers: Modifiers::COMPOSITOR, |
| 2845 | }, | 2852 | }, |
| 2846 | action: Action::Spawn(vec!["alacritty".to_owned()]), | 2853 | action: Action::Spawn(vec!["alacritty".to_owned()]), |
| 2854 | + repeat: true, | ||
| 2847 | cooldown: None, | 2855 | cooldown: None, |
| 2848 | allow_when_locked: true, | 2856 | allow_when_locked: true, |
| 2849 | }, | 2857 | }, |
| @@ -2853,6 +2861,7 @@ mod tests { | |||
| 2853 | modifiers: Modifiers::COMPOSITOR, | 2861 | modifiers: Modifiers::COMPOSITOR, |
| 2854 | }, | 2862 | }, |
| 2855 | action: Action::CloseWindow, | 2863 | action: Action::CloseWindow, |
| 2864 | + repeat: true, | ||
| 2856 | cooldown: None, | 2865 | cooldown: None, |
| 2857 | allow_when_locked: false, | 2866 | allow_when_locked: false, |
| 2858 | }, | 2867 | }, |
| @@ -2862,6 +2871,7 @@ mod tests { | |||
| 2862 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, | 2871 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, |
| 2863 | }, | 2872 | }, |
| 2864 | action: Action::FocusMonitorLeft, | 2873 | action: Action::FocusMonitorLeft, |
| 2874 | + repeat: true, | ||
| 2865 | cooldown: None, | 2875 | cooldown: None, |
| 2866 | allow_when_locked: false, | 2876 | allow_when_locked: false, |
| 2867 | }, | 2877 | }, |
| @@ -2871,6 +2881,7 @@ mod tests { | |||
| 2871 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT | Modifiers::CTRL, | 2881 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT | Modifiers::CTRL, |
| 2872 | }, | 2882 | }, |
| 2873 | action: Action::MoveWindowToMonitorRight, | 2883 | action: Action::MoveWindowToMonitorRight, |
| 2884 | + repeat: true, | ||
| 2874 | cooldown: None, | 2885 | cooldown: None, |
| 2875 | allow_when_locked: false, | 2886 | allow_when_locked: false, |
| 2876 | }, | 2887 | }, |
| @@ -2880,6 +2891,7 @@ mod tests { | |||
| 2880 | modifiers: Modifiers::COMPOSITOR, | 2891 | modifiers: Modifiers::COMPOSITOR, |
| 2881 | }, | 2892 | }, |
| 2882 | action: Action::ConsumeWindowIntoColumn, | 2893 | action: Action::ConsumeWindowIntoColumn, |
| 2894 | + repeat: true, | ||
| 2883 | cooldown: None, | 2895 | cooldown: None, |
| 2884 | allow_when_locked: false, | 2896 | allow_when_locked: false, |
| 2885 | }, | 2897 | }, |
| @@ -2889,6 +2901,7 @@ mod tests { | |||
| 2889 | modifiers: Modifiers::COMPOSITOR, | 2901 | modifiers: Modifiers::COMPOSITOR, |
| 2890 | }, | 2902 | }, |
| 2891 | action: Action::FocusWorkspace(WorkspaceReference::Index(1)), | 2903 | action: Action::FocusWorkspace(WorkspaceReference::Index(1)), |
| 2904 | + repeat: true, | ||
| 2892 | cooldown: None, | 2905 | cooldown: None, |
| 2893 | allow_when_locked: false, | 2906 | allow_when_locked: false, |
| 2894 | }, | 2907 | }, |
| @@ -2900,6 +2913,7 @@ mod tests { | |||
| 2900 | action: Action::FocusWorkspace(WorkspaceReference::Name( | 2913 | action: Action::FocusWorkspace(WorkspaceReference::Name( |
| 2901 | "workspace-1".to_string(), | 2914 | "workspace-1".to_string(), |
| 2902 | )), | 2915 | )), |
| 2916 | + repeat: true, | ||
| 2903 | cooldown: None, | 2917 | cooldown: None, |
| 2904 | allow_when_locked: false, | 2918 | allow_when_locked: false, |
| 2905 | }, | 2919 | }, |
| @@ -2909,6 +2923,7 @@ mod tests { | |||
| 2909 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, | 2923 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, |
| 2910 | }, | 2924 | }, |
| 2911 | action: Action::Quit(true), | 2925 | action: Action::Quit(true), |
| 2926 | + repeat: true, | ||
| 2912 | cooldown: None, | 2927 | cooldown: None, |
| 2913 | allow_when_locked: false, | 2928 | allow_when_locked: false, |
| 2914 | }, | 2929 | }, |
| @@ -2918,6 +2933,7 @@ mod tests { | |||
| 2918 | modifiers: Modifiers::COMPOSITOR, | 2933 | modifiers: Modifiers::COMPOSITOR, |
| 2919 | }, | 2934 | }, |
| 2920 | action: Action::FocusWorkspaceDown, | 2935 | action: Action::FocusWorkspaceDown, |
| 2936 | + repeat: true, | ||
| 2921 | cooldown: Some(Duration::from_millis(150)), | 2937 | cooldown: Some(Duration::from_millis(150)), |
| 2922 | allow_when_locked: false, | 2938 | allow_when_locked: false, |
| 2923 | }, | 2939 | }, |
src/input/mod.rsmodified+55 -2
| @@ -294,6 +294,19 @@ impl State { | |||
| 294 | let time = Event::time_msec(&event); | 294 | let time = Event::time_msec(&event); |
| 295 | let pressed = event.state() == KeyState::Pressed; | 295 | let pressed = event.state() == KeyState::Pressed; |
| 296 | 296 | ||
| 297 | + // Stop bind key repeat on any release. This won't work 100% correctly in cases like: | ||
| 298 | + // 1. Press Mod | ||
| 299 | + // 2. Press Left (repeat starts) | ||
| 300 | + // 3. Press PgDown (new repeat starts) | ||
| 301 | + // 4. Release Left (PgDown repeat stops) | ||
| 302 | + // But it's good enough for now. | ||
| 303 | + // FIXME: handle this properly. | ||
| 304 | + if !pressed { | ||
| 305 | + if let Some(token) = self.niri.bind_repeat_timer.take() { | ||
| 306 | + self.niri.event_loop.remove(token); | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + | ||
| 297 | let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( | 310 | let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( |
| 298 | self, | 311 | self, |
| 299 | event.key_code(), | 312 | event.key_code(), |
| @@ -330,12 +343,44 @@ impl State { | |||
| 330 | return; | 343 | return; |
| 331 | }; | 344 | }; |
| 332 | 345 | ||
| 333 | - // Filter actions when the key is released or the session is locked. | ||
| 334 | if !pressed { | 346 | if !pressed { |
| 335 | return; | 347 | return; |
| 336 | } | 348 | } |
| 337 | 349 | ||
| 338 | - self.handle_bind(bind); | 350 | + self.handle_bind(bind.clone()); |
| 351 | + | ||
| 352 | + // Start the key repeat timer if necessary. | ||
| 353 | + if !bind.repeat { | ||
| 354 | + return; | ||
| 355 | + } | ||
| 356 | + | ||
| 357 | + // Stop the previous key repeat if any. | ||
| 358 | + if let Some(token) = self.niri.bind_repeat_timer.take() { | ||
| 359 | + self.niri.event_loop.remove(token); | ||
| 360 | + } | ||
| 361 | + | ||
| 362 | + let config = self.niri.config.borrow(); | ||
| 363 | + let config = &config.input.keyboard; | ||
| 364 | + | ||
| 365 | + let repeat_rate = config.repeat_rate; | ||
| 366 | + if repeat_rate == 0 { | ||
| 367 | + return; | ||
| 368 | + } | ||
| 369 | + let repeat_duration = Duration::from_secs_f64(1. / f64::from(repeat_rate)); | ||
| 370 | + | ||
| 371 | + let repeat_timer = | ||
| 372 | + Timer::from_duration(Duration::from_millis(u64::from(config.repeat_delay))); | ||
| 373 | + | ||
| 374 | + let token = self | ||
| 375 | + .niri | ||
| 376 | + .event_loop | ||
| 377 | + .insert_source(repeat_timer, move |_, _, state| { | ||
| 378 | + state.handle_bind(bind.clone()); | ||
| 379 | + TimeoutAction::ToDuration(repeat_duration) | ||
| 380 | + }) | ||
| 381 | + .unwrap(); | ||
| 382 | + | ||
| 383 | + self.niri.bind_repeat_timer = Some(token); | ||
| 339 | } | 384 | } |
| 340 | 385 | ||
| 341 | pub fn handle_bind(&mut self, bind: Bind) { | 386 | pub fn handle_bind(&mut self, bind: Bind) { |
| @@ -2132,6 +2177,7 @@ fn should_intercept_key( | |||
| 2132 | modifiers: Modifiers::empty(), | 2177 | modifiers: Modifiers::empty(), |
| 2133 | }, | 2178 | }, |
| 2134 | action, | 2179 | action, |
| 2180 | + repeat: true, | ||
| 2135 | cooldown: None, | 2181 | cooldown: None, |
| 2136 | allow_when_locked: false, | 2182 | allow_when_locked: false, |
| 2137 | }); | 2183 | }); |
| @@ -2181,6 +2227,7 @@ fn find_bind( | |||
| 2181 | modifiers: Modifiers::empty(), | 2227 | modifiers: Modifiers::empty(), |
| 2182 | }, | 2228 | }, |
| 2183 | action, | 2229 | action, |
| 2230 | + repeat: true, | ||
| 2184 | cooldown: None, | 2231 | cooldown: None, |
| 2185 | allow_when_locked: false, | 2232 | allow_when_locked: false, |
| 2186 | }); | 2233 | }); |
| @@ -2512,6 +2559,7 @@ mod tests { | |||
| 2512 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, | 2559 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, |
| 2513 | }, | 2560 | }, |
| 2514 | action: Action::CloseWindow, | 2561 | action: Action::CloseWindow, |
| 2562 | + repeat: true, | ||
| 2515 | cooldown: None, | 2563 | cooldown: None, |
| 2516 | allow_when_locked: false, | 2564 | allow_when_locked: false, |
| 2517 | }]); | 2565 | }]); |
| @@ -2645,6 +2693,7 @@ mod tests { | |||
| 2645 | modifiers: Modifiers::COMPOSITOR, | 2693 | modifiers: Modifiers::COMPOSITOR, |
| 2646 | }, | 2694 | }, |
| 2647 | action: Action::CloseWindow, | 2695 | action: Action::CloseWindow, |
| 2696 | + repeat: true, | ||
| 2648 | cooldown: None, | 2697 | cooldown: None, |
| 2649 | allow_when_locked: false, | 2698 | allow_when_locked: false, |
| 2650 | }, | 2699 | }, |
| @@ -2654,6 +2703,7 @@ mod tests { | |||
| 2654 | modifiers: Modifiers::SUPER, | 2703 | modifiers: Modifiers::SUPER, |
| 2655 | }, | 2704 | }, |
| 2656 | action: Action::FocusColumnLeft, | 2705 | action: Action::FocusColumnLeft, |
| 2706 | + repeat: true, | ||
| 2657 | cooldown: None, | 2707 | cooldown: None, |
| 2658 | allow_when_locked: false, | 2708 | allow_when_locked: false, |
| 2659 | }, | 2709 | }, |
| @@ -2663,6 +2713,7 @@ mod tests { | |||
| 2663 | modifiers: Modifiers::empty(), | 2713 | modifiers: Modifiers::empty(), |
| 2664 | }, | 2714 | }, |
| 2665 | action: Action::FocusWindowDown, | 2715 | action: Action::FocusWindowDown, |
| 2716 | + repeat: true, | ||
| 2666 | cooldown: None, | 2717 | cooldown: None, |
| 2667 | allow_when_locked: false, | 2718 | allow_when_locked: false, |
| 2668 | }, | 2719 | }, |
| @@ -2672,6 +2723,7 @@ mod tests { | |||
| 2672 | modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, | 2723 | modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, |
| 2673 | }, | 2724 | }, |
| 2674 | action: Action::FocusWindowUp, | 2725 | action: Action::FocusWindowUp, |
| 2726 | + repeat: true, | ||
| 2675 | cooldown: None, | 2727 | cooldown: None, |
| 2676 | allow_when_locked: false, | 2728 | allow_when_locked: false, |
| 2677 | }, | 2729 | }, |
| @@ -2681,6 +2733,7 @@ mod tests { | |||
| 2681 | modifiers: Modifiers::SUPER | Modifiers::ALT, | 2733 | modifiers: Modifiers::SUPER | Modifiers::ALT, |
| 2682 | }, | 2734 | }, |
| 2683 | action: Action::FocusColumnRight, | 2735 | action: Action::FocusColumnRight, |
| 2736 | + repeat: true, | ||
| 2684 | cooldown: None, | 2737 | cooldown: None, |
| 2685 | allow_when_locked: false, | 2738 | allow_when_locked: false, |
| 2686 | }, | 2739 | }, |
src/niri.rsmodified+2 -0
| @@ -234,6 +234,7 @@ pub struct Niri { | |||
| 234 | /// Scancodes of the keys to suppress. | 234 | /// Scancodes of the keys to suppress. |
| 235 | pub suppressed_keys: HashSet<u32>, | 235 | pub suppressed_keys: HashSet<u32>, |
| 236 | pub bind_cooldown_timers: HashMap<Key, RegistrationToken>, | 236 | pub bind_cooldown_timers: HashMap<Key, RegistrationToken>, |
| 237 | + pub bind_repeat_timer: Option<RegistrationToken>, | ||
| 237 | pub keyboard_focus: KeyboardFocus, | 238 | pub keyboard_focus: KeyboardFocus, |
| 238 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, | 239 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, |
| 239 | pub is_fdo_idle_inhibited: Arc<AtomicBool>, | 240 | pub is_fdo_idle_inhibited: Arc<AtomicBool>, |
| @@ -1657,6 +1658,7 @@ impl Niri { | |||
| 1657 | popup_grab: None, | 1658 | popup_grab: None, |
| 1658 | suppressed_keys: HashSet::new(), | 1659 | suppressed_keys: HashSet::new(), |
| 1659 | bind_cooldown_timers: HashMap::new(), | 1660 | bind_cooldown_timers: HashMap::new(), |
| 1661 | + bind_repeat_timer: Option::default(), | ||
| 1660 | presentation_state, | 1662 | presentation_state, |
| 1661 | security_context_state, | 1663 | security_context_state, |
| 1662 | gamma_control_manager_state, | 1664 | gamma_control_manager_state, |