Implement bind cooldown-ms
Browse files at this commitb06e51da60505bba42ef310d510f38440b897f9bParent: 6c08ba33 changed files +101 -9
- niri-config/src/lib.rs +50 -3
- src/input.rs +48 -5
- src/niri.rs +3 -1
niri-config/src/lib.rsmodified+50 -3
| @@ -5,6 +5,7 @@ use std::collections::HashSet; | |||
| 5 | use std::ffi::OsStr; | 5 | use std::ffi::OsStr; |
| 6 | use std::path::{Path, PathBuf}; | 6 | use std::path::{Path, PathBuf}; |
| 7 | use std::str::FromStr; | 7 | use std::str::FromStr; |
| 8 | +use std::time::Duration; | ||
| 8 | 9 | ||
| 9 | use bitflags::bitflags; | 10 | use bitflags::bitflags; |
| 10 | use knuffel::errors::DecodeError; | 11 | use knuffel::errors::DecodeError; |
| @@ -720,6 +721,7 @@ pub struct Binds(pub Vec<Bind>); | |||
| 720 | pub struct Bind { | 721 | pub struct Bind { |
| 721 | pub key: Key, | 722 | pub key: Key, |
| 722 | pub action: Action, | 723 | pub action: Action, |
| 724 | + pub cooldown: Option<Duration>, | ||
| 723 | } | 725 | } |
| 724 | 726 | ||
| 725 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] | 727 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
| @@ -1405,13 +1407,45 @@ where | |||
| 1405 | node: &knuffel::ast::SpannedNode<S>, | 1407 | node: &knuffel::ast::SpannedNode<S>, |
| 1406 | ctx: &mut knuffel::decode::Context<S>, | 1408 | ctx: &mut knuffel::decode::Context<S>, |
| 1407 | ) -> Result<Self, DecodeError<S>> { | 1409 | ) -> Result<Self, DecodeError<S>> { |
| 1408 | - expect_only_children(node, ctx); | 1410 | + if let Some(type_name) = &node.type_name { |
| 1411 | + ctx.emit_error(DecodeError::unexpected( | ||
| 1412 | + type_name, | ||
| 1413 | + "type name", | ||
| 1414 | + "no type name expected for this node", | ||
| 1415 | + )); | ||
| 1416 | + } | ||
| 1417 | + | ||
| 1418 | + for val in node.arguments.iter() { | ||
| 1419 | + ctx.emit_error(DecodeError::unexpected( | ||
| 1420 | + &val.literal, | ||
| 1421 | + "argument", | ||
| 1422 | + "no arguments expected for this node", | ||
| 1423 | + )) | ||
| 1424 | + } | ||
| 1409 | 1425 | ||
| 1410 | let key = node | 1426 | let key = node |
| 1411 | .node_name | 1427 | .node_name |
| 1412 | .parse::<Key>() | 1428 | .parse::<Key>() |
| 1413 | .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?; | 1429 | .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?; |
| 1414 | 1430 | ||
| 1431 | + let mut cooldown = None; | ||
| 1432 | + for (name, val) in &node.properties { | ||
| 1433 | + match &***name { | ||
| 1434 | + "cooldown-ms" => { | ||
| 1435 | + cooldown = Some(Duration::from_millis( | ||
| 1436 | + knuffel::traits::DecodeScalar::decode(val, ctx)?, | ||
| 1437 | + )); | ||
| 1438 | + } | ||
| 1439 | + name_str => { | ||
| 1440 | + ctx.emit_error(DecodeError::unexpected( | ||
| 1441 | + name, | ||
| 1442 | + "property", | ||
| 1443 | + format!("unexpected property `{}`", name_str.escape_default()), | ||
| 1444 | + )); | ||
| 1445 | + } | ||
| 1446 | + } | ||
| 1447 | + } | ||
| 1448 | + | ||
| 1415 | let mut children = node.children(); | 1449 | let mut children = node.children(); |
| 1416 | 1450 | ||
| 1417 | // If the action is invalid but the key is fine, we still want to return something. | 1451 | // If the action is invalid but the key is fine, we still want to return something. |
| @@ -1420,6 +1454,7 @@ where | |||
| 1420 | let dummy = Self { | 1454 | let dummy = Self { |
| 1421 | key, | 1455 | key, |
| 1422 | action: Action::Spawn(vec![]), | 1456 | action: Action::Spawn(vec![]), |
| 1457 | + cooldown: None, | ||
| 1423 | }; | 1458 | }; |
| 1424 | 1459 | ||
| 1425 | if let Some(child) = children.next() { | 1460 | if let Some(child) = children.next() { |
| @@ -1431,7 +1466,11 @@ where | |||
| 1431 | )); | 1466 | )); |
| 1432 | } | 1467 | } |
| 1433 | match Action::decode_node(child, ctx) { | 1468 | match Action::decode_node(child, ctx) { |
| 1434 | - Ok(action) => Ok(Self { key, action }), | 1469 | + Ok(action) => Ok(Self { |
| 1470 | + key, | ||
| 1471 | + action, | ||
| 1472 | + cooldown, | ||
| 1473 | + }), | ||
| 1435 | Err(e) => { | 1474 | Err(e) => { |
| 1436 | ctx.emit_error(e); | 1475 | ctx.emit_error(e); |
| 1437 | Ok(dummy) | 1476 | Ok(dummy) |
| @@ -1732,7 +1771,7 @@ mod tests { | |||
| 1732 | Mod+Comma { consume-window-into-column; } | 1771 | Mod+Comma { consume-window-into-column; } |
| 1733 | Mod+1 { focus-workspace 1; } | 1772 | Mod+1 { focus-workspace 1; } |
| 1734 | Mod+Shift+E { quit skip-confirmation=true; } | 1773 | Mod+Shift+E { quit skip-confirmation=true; } |
| 1735 | - Mod+WheelDown { focus-workspace-down; } | 1774 | + Mod+WheelDown cooldown-ms=150 { focus-workspace-down; } |
| 1736 | } | 1775 | } |
| 1737 | 1776 | ||
| 1738 | debug { | 1777 | debug { |
| @@ -1920,6 +1959,7 @@ mod tests { | |||
| 1920 | modifiers: Modifiers::COMPOSITOR, | 1959 | modifiers: Modifiers::COMPOSITOR, |
| 1921 | }, | 1960 | }, |
| 1922 | action: Action::Spawn(vec!["alacritty".to_owned()]), | 1961 | action: Action::Spawn(vec!["alacritty".to_owned()]), |
| 1962 | + cooldown: None, | ||
| 1923 | }, | 1963 | }, |
| 1924 | Bind { | 1964 | Bind { |
| 1925 | key: Key { | 1965 | key: Key { |
| @@ -1927,6 +1967,7 @@ mod tests { | |||
| 1927 | modifiers: Modifiers::COMPOSITOR, | 1967 | modifiers: Modifiers::COMPOSITOR, |
| 1928 | }, | 1968 | }, |
| 1929 | action: Action::CloseWindow, | 1969 | action: Action::CloseWindow, |
| 1970 | + cooldown: None, | ||
| 1930 | }, | 1971 | }, |
| 1931 | Bind { | 1972 | Bind { |
| 1932 | key: Key { | 1973 | key: Key { |
| @@ -1934,6 +1975,7 @@ mod tests { | |||
| 1934 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, | 1975 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, |
| 1935 | }, | 1976 | }, |
| 1936 | action: Action::FocusMonitorLeft, | 1977 | action: Action::FocusMonitorLeft, |
| 1978 | + cooldown: None, | ||
| 1937 | }, | 1979 | }, |
| 1938 | Bind { | 1980 | Bind { |
| 1939 | key: Key { | 1981 | key: Key { |
| @@ -1941,6 +1983,7 @@ mod tests { | |||
| 1941 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT | Modifiers::CTRL, | 1983 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT | Modifiers::CTRL, |
| 1942 | }, | 1984 | }, |
| 1943 | action: Action::MoveWindowToMonitorRight, | 1985 | action: Action::MoveWindowToMonitorRight, |
| 1986 | + cooldown: None, | ||
| 1944 | }, | 1987 | }, |
| 1945 | Bind { | 1988 | Bind { |
| 1946 | key: Key { | 1989 | key: Key { |
| @@ -1948,6 +1991,7 @@ mod tests { | |||
| 1948 | modifiers: Modifiers::COMPOSITOR, | 1991 | modifiers: Modifiers::COMPOSITOR, |
| 1949 | }, | 1992 | }, |
| 1950 | action: Action::ConsumeWindowIntoColumn, | 1993 | action: Action::ConsumeWindowIntoColumn, |
| 1994 | + cooldown: None, | ||
| 1951 | }, | 1995 | }, |
| 1952 | Bind { | 1996 | Bind { |
| 1953 | key: Key { | 1997 | key: Key { |
| @@ -1955,6 +1999,7 @@ mod tests { | |||
| 1955 | modifiers: Modifiers::COMPOSITOR, | 1999 | modifiers: Modifiers::COMPOSITOR, |
| 1956 | }, | 2000 | }, |
| 1957 | action: Action::FocusWorkspace(1), | 2001 | action: Action::FocusWorkspace(1), |
| 2002 | + cooldown: None, | ||
| 1958 | }, | 2003 | }, |
| 1959 | Bind { | 2004 | Bind { |
| 1960 | key: Key { | 2005 | key: Key { |
| @@ -1962,6 +2007,7 @@ mod tests { | |||
| 1962 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, | 2007 | modifiers: Modifiers::COMPOSITOR | Modifiers::SHIFT, |
| 1963 | }, | 2008 | }, |
| 1964 | action: Action::Quit(true), | 2009 | action: Action::Quit(true), |
| 2010 | + cooldown: None, | ||
| 1965 | }, | 2011 | }, |
| 1966 | Bind { | 2012 | Bind { |
| 1967 | key: Key { | 2013 | key: Key { |
| @@ -1969,6 +2015,7 @@ mod tests { | |||
| 1969 | modifiers: Modifiers::COMPOSITOR, | 2015 | modifiers: Modifiers::COMPOSITOR, |
| 1970 | }, | 2016 | }, |
| 1971 | action: Action::FocusWorkspaceDown, | 2017 | action: Action::FocusWorkspaceDown, |
| 2018 | + cooldown: Some(Duration::from_millis(150)), | ||
| 1972 | }, | 2019 | }, |
| 1973 | ]), | 2020 | ]), |
| 1974 | debug: DebugConfig { | 2021 | debug: DebugConfig { |
src/input.rsmodified+48 -5
| @@ -1,7 +1,9 @@ | |||
| 1 | use std::any::Any; | 1 | use std::any::Any; |
| 2 | +use std::collections::hash_map::Entry; | ||
| 2 | use std::collections::HashSet; | 3 | use std::collections::HashSet; |
| 3 | use std::time::Duration; | 4 | use std::time::Duration; |
| 4 | 5 | ||
| 6 | +use calloop::timer::{TimeoutAction, Timer}; | ||
| 5 | use input::event::gesture::GestureEventCoordinates as _; | 7 | use input::event::gesture::GestureEventCoordinates as _; |
| 6 | use niri_config::{Action, Bind, Binds, Key, Modifiers, Trigger}; | 8 | use niri_config::{Action, Bind, Binds, Key, Modifiers, Trigger}; |
| 7 | use niri_ipc::LayoutSwitchTarget; | 9 | use niri_ipc::LayoutSwitchTarget; |
| @@ -289,7 +291,40 @@ impl State { | |||
| 289 | return; | 291 | return; |
| 290 | } | 292 | } |
| 291 | 293 | ||
| 292 | - self.do_action(bind.action); | 294 | + self.handle_bind(bind); |
| 295 | + } | ||
| 296 | + | ||
| 297 | + pub fn handle_bind(&mut self, bind: Bind) { | ||
| 298 | + let Some(cooldown) = bind.cooldown else { | ||
| 299 | + self.do_action(bind.action); | ||
| 300 | + return; | ||
| 301 | + }; | ||
| 302 | + | ||
| 303 | + // Check this first so that it doesn't trigger the cooldown. | ||
| 304 | + if self.niri.is_locked() && !allowed_when_locked(&bind.action) { | ||
| 305 | + return; | ||
| 306 | + } | ||
| 307 | + | ||
| 308 | + match self.niri.bind_cooldown_timers.entry(bind.key) { | ||
| 309 | + // The bind is on cooldown. | ||
| 310 | + Entry::Occupied(_) => (), | ||
| 311 | + Entry::Vacant(entry) => { | ||
| 312 | + let timer = Timer::from_duration(cooldown); | ||
| 313 | + let token = self | ||
| 314 | + .niri | ||
| 315 | + .event_loop | ||
| 316 | + .insert_source(timer, move |_, _, state| { | ||
| 317 | + if state.niri.bind_cooldown_timers.remove(&bind.key).is_none() { | ||
| 318 | + error!("bind cooldown timer entry disappeared"); | ||
| 319 | + } | ||
| 320 | + TimeoutAction::Drop | ||
| 321 | + }) | ||
| 322 | + .unwrap(); | ||
| 323 | + entry.insert(token); | ||
| 324 | + | ||
| 325 | + self.do_action(bind.action); | ||
| 326 | + } | ||
| 327 | + } | ||
| 293 | } | 328 | } |
| 294 | 329 | ||
| 295 | pub fn do_action(&mut self, action: Action) { | 330 | pub fn do_action(&mut self, action: Action) { |
| @@ -1100,12 +1135,12 @@ impl State { | |||
| 1100 | let ticks = self.niri.horizontal_wheel_tracker.accumulate(v120); | 1135 | let ticks = self.niri.horizontal_wheel_tracker.accumulate(v120); |
| 1101 | if let Some(right) = bind_right { | 1136 | if let Some(right) = bind_right { |
| 1102 | for _ in 0..ticks { | 1137 | for _ in 0..ticks { |
| 1103 | - self.do_action(right.action.clone()); | 1138 | + self.handle_bind(right.clone()); |
| 1104 | } | 1139 | } |
| 1105 | } | 1140 | } |
| 1106 | if let Some(left) = bind_left { | 1141 | if let Some(left) = bind_left { |
| 1107 | for _ in ticks..0 { | 1142 | for _ in ticks..0 { |
| 1108 | - self.do_action(left.action.clone()); | 1143 | + self.handle_bind(left.clone()); |
| 1109 | } | 1144 | } |
| 1110 | } | 1145 | } |
| 1111 | return; | 1146 | return; |
| @@ -1125,12 +1160,12 @@ impl State { | |||
| 1125 | let ticks = self.niri.vertical_wheel_tracker.accumulate(v120); | 1160 | let ticks = self.niri.vertical_wheel_tracker.accumulate(v120); |
| 1126 | if let Some(down) = bind_down { | 1161 | if let Some(down) = bind_down { |
| 1127 | for _ in 0..ticks { | 1162 | for _ in 0..ticks { |
| 1128 | - self.do_action(down.action.clone()); | 1163 | + self.handle_bind(down.clone()); |
| 1129 | } | 1164 | } |
| 1130 | } | 1165 | } |
| 1131 | if let Some(up) = bind_up { | 1166 | if let Some(up) = bind_up { |
| 1132 | for _ in ticks..0 { | 1167 | for _ in ticks..0 { |
| 1133 | - self.do_action(up.action.clone()); | 1168 | + self.handle_bind(up.clone()); |
| 1134 | } | 1169 | } |
| 1135 | } | 1170 | } |
| 1136 | return; | 1171 | return; |
| @@ -1725,6 +1760,7 @@ fn should_intercept_key( | |||
| 1725 | modifiers: Modifiers::empty(), | 1760 | modifiers: Modifiers::empty(), |
| 1726 | }, | 1761 | }, |
| 1727 | action, | 1762 | action, |
| 1763 | + cooldown: None, | ||
| 1728 | }); | 1764 | }); |
| 1729 | } | 1765 | } |
| 1730 | } | 1766 | } |
| @@ -1772,6 +1808,7 @@ fn find_bind( | |||
| 1772 | modifiers: Modifiers::empty(), | 1808 | modifiers: Modifiers::empty(), |
| 1773 | }, | 1809 | }, |
| 1774 | action, | 1810 | action, |
| 1811 | + cooldown: None, | ||
| 1775 | }); | 1812 | }); |
| 1776 | } | 1813 | } |
| 1777 | 1814 | ||
| @@ -1991,6 +2028,7 @@ mod tests { | |||
| 1991 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, | 2028 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, |
| 1992 | }, | 2029 | }, |
| 1993 | action: Action::CloseWindow, | 2030 | action: Action::CloseWindow, |
| 2031 | + cooldown: None, | ||
| 1994 | }]); | 2032 | }]); |
| 1995 | 2033 | ||
| 1996 | let comp_mod = CompositorMod::Super; | 2034 | let comp_mod = CompositorMod::Super; |
| @@ -2122,6 +2160,7 @@ mod tests { | |||
| 2122 | modifiers: Modifiers::COMPOSITOR, | 2160 | modifiers: Modifiers::COMPOSITOR, |
| 2123 | }, | 2161 | }, |
| 2124 | action: Action::CloseWindow, | 2162 | action: Action::CloseWindow, |
| 2163 | + cooldown: None, | ||
| 2125 | }, | 2164 | }, |
| 2126 | Bind { | 2165 | Bind { |
| 2127 | key: Key { | 2166 | key: Key { |
| @@ -2129,6 +2168,7 @@ mod tests { | |||
| 2129 | modifiers: Modifiers::SUPER, | 2168 | modifiers: Modifiers::SUPER, |
| 2130 | }, | 2169 | }, |
| 2131 | action: Action::FocusColumnLeft, | 2170 | action: Action::FocusColumnLeft, |
| 2171 | + cooldown: None, | ||
| 2132 | }, | 2172 | }, |
| 2133 | Bind { | 2173 | Bind { |
| 2134 | key: Key { | 2174 | key: Key { |
| @@ -2136,6 +2176,7 @@ mod tests { | |||
| 2136 | modifiers: Modifiers::empty(), | 2176 | modifiers: Modifiers::empty(), |
| 2137 | }, | 2177 | }, |
| 2138 | action: Action::FocusWindowDown, | 2178 | action: Action::FocusWindowDown, |
| 2179 | + cooldown: None, | ||
| 2139 | }, | 2180 | }, |
| 2140 | Bind { | 2181 | Bind { |
| 2141 | key: Key { | 2182 | key: Key { |
| @@ -2143,6 +2184,7 @@ mod tests { | |||
| 2143 | modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, | 2184 | modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, |
| 2144 | }, | 2185 | }, |
| 2145 | action: Action::FocusWindowUp, | 2186 | action: Action::FocusWindowUp, |
| 2187 | + cooldown: None, | ||
| 2146 | }, | 2188 | }, |
| 2147 | Bind { | 2189 | Bind { |
| 2148 | key: Key { | 2190 | key: Key { |
| @@ -2150,6 +2192,7 @@ mod tests { | |||
| 2150 | modifiers: Modifiers::SUPER | Modifiers::ALT, | 2192 | modifiers: Modifiers::SUPER | Modifiers::ALT, |
| 2151 | }, | 2193 | }, |
| 2152 | action: Action::FocusColumnRight, | 2194 | action: Action::FocusColumnRight, |
| 2195 | + cooldown: None, | ||
| 2153 | }, | 2196 | }, |
| 2154 | ]); | 2197 | ]); |
| 2155 | 2198 | ||
src/niri.rsmodified+3 -1
| @@ -11,7 +11,7 @@ use std::{env, mem, thread}; | |||
| 11 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; | 11 | use _server_decoration::server::org_kde_kwin_server_decoration_manager::Mode as KdeDecorationsMode; |
| 12 | use anyhow::{ensure, Context}; | 12 | use anyhow::{ensure, Context}; |
| 13 | use calloop::futures::Scheduler; | 13 | use calloop::futures::Scheduler; |
| 14 | -use niri_config::{Config, TrackLayout}; | 14 | +use niri_config::{Config, Key, TrackLayout}; |
| 15 | use smithay::backend::allocator::Fourcc; | 15 | use smithay::backend::allocator::Fourcc; |
| 16 | use smithay::backend::renderer::element::memory::MemoryRenderBufferRenderElement; | 16 | use smithay::backend::renderer::element::memory::MemoryRenderBufferRenderElement; |
| 17 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; | 17 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| @@ -194,6 +194,7 @@ pub struct Niri { | |||
| 194 | pub seat: Seat<State>, | 194 | pub seat: Seat<State>, |
| 195 | /// Scancodes of the keys to suppress. | 195 | /// Scancodes of the keys to suppress. |
| 196 | pub suppressed_keys: HashSet<u32>, | 196 | pub suppressed_keys: HashSet<u32>, |
| 197 | + pub bind_cooldown_timers: HashMap<Key, RegistrationToken>, | ||
| 197 | pub keyboard_focus: KeyboardFocus, | 198 | pub keyboard_focus: KeyboardFocus, |
| 198 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, | 199 | pub idle_inhibiting_surfaces: HashSet<WlSurface>, |
| 199 | pub is_fdo_idle_inhibited: Arc<AtomicBool>, | 200 | pub is_fdo_idle_inhibited: Arc<AtomicBool>, |
| @@ -1251,6 +1252,7 @@ impl Niri { | |||
| 1251 | popups: PopupManager::default(), | 1252 | popups: PopupManager::default(), |
| 1252 | popup_grab: None, | 1253 | popup_grab: None, |
| 1253 | suppressed_keys: HashSet::new(), | 1254 | suppressed_keys: HashSet::new(), |
| 1255 | + bind_cooldown_timers: HashMap::new(), | ||
| 1254 | presentation_state, | 1256 | presentation_state, |
| 1255 | security_context_state, | 1257 | security_context_state, |
| 1256 | gamma_control_manager_state, | 1258 | gamma_control_manager_state, |