forge
Explore
Log in

Don't you dare go Hollow

input: support press and release bind actions

Browse files at this commit

5331880570ba5ad1d21997995fd018eb7b32cbdcJustin SpeddingParent: fc3d54d
There's a small behavior change: pressing a bind that is on a cooldown
and has repeat=true won't start the key repeat (it used to). The reason
is that for Press+Release keys it would make it annoying to track when
the release bind should fire (only after the first repeated key rather
than currently when it's recorded on press).
niri-config/src/binds.rsmodified+213 -45
@@ -22,7 +22,7 @@ pub struct Binds(pub Vec<Bind>);
22 #[derive(Debug, Clone, PartialEq)]22 #[derive(Debug, Clone, PartialEq)]
23 pub struct Bind {23 pub struct Bind {
24 pub key: Key,24 pub key: Key,
25- pub action: Action,25+ pub action: BoundAction,
26 pub repeat: bool,26 pub repeat: bool,
27 pub cooldown: Option<Duration>,27 pub cooldown: Option<Duration>,
28 pub allow_when_locked: bool,28 pub allow_when_locked: bool,
@@ -30,6 +30,45 @@ pub struct Bind {
30 pub hotkey_overlay_title: Option<Option<String>>,30 pub hotkey_overlay_title: Option<Option<String>>,
31 }31 }
32 32
33+#[derive(Debug, Clone, PartialEq)]
34+pub enum BoundAction {
35+ Press(Action),
36+ Release(Action),
37+ Both { press: Action, release: Action },
38+}
39+
40+impl Bind {
41+ pub fn has_press(&self) -> bool {
42+ matches!(
43+ self.action,
44+ BoundAction::Press(_) | BoundAction::Both { .. }
45+ )
46+ }
47+
48+ pub fn has_release(&self) -> bool {
49+ matches!(
50+ self.action,
51+ BoundAction::Release(_) | BoundAction::Both { .. }
52+ )
53+ }
54+
55+ pub fn press_action(&self) -> Option<&Action> {
56+ match &self.action {
57+ BoundAction::Press(action) => Some(action),
58+ BoundAction::Both { press, .. } => Some(press),
59+ BoundAction::Release(_) => None,
60+ }
61+ }
62+
63+ pub fn release_action(&self) -> Option<&Action> {
64+ match &self.action {
65+ BoundAction::Release(action) => Some(action),
66+ BoundAction::Both { release, .. } => Some(release),
67+ BoundAction::Press(_) => None,
68+ }
69+ }
70+}
71+
33 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]72 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
34 pub struct Key {73 pub struct Key {
35 pub trigger: Trigger,74 pub trigger: Trigger,
@@ -921,16 +960,19 @@ where
921 .parse::<Key>()960 .parse::<Key>()
922 .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?;961 .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?;
923 962
924- let mut repeat = true;963+ let mut repeat = None;
964+ let mut repeat_node = None;
925 let mut cooldown = None;965 let mut cooldown = None;
926 let mut allow_when_locked = false;966 let mut allow_when_locked = false;
927 let mut allow_when_locked_node = None;967 let mut allow_when_locked_node = None;
928 let mut allow_inhibiting = true;968 let mut allow_inhibiting = true;
929 let mut hotkey_overlay_title = None;969 let mut hotkey_overlay_title = None;
970+
930 for (name, val) in &node.properties {971 for (name, val) in &node.properties {
931 match &***name {972 match &***name {
932 "repeat" => {973 "repeat" => {
933- repeat = knuffel::traits::DecodeScalar::decode(val, ctx)?;974+ repeat = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?);
975+ repeat_node = Some(name);
934 }976 }
935 "cooldown-ms" => {977 "cooldown-ms" => {
936 cooldown = Some(Duration::from_millis(978 cooldown = Some(Duration::from_millis(
@@ -957,14 +999,12 @@ where
957 }999 }
958 }1000 }
959 1001
960- let mut children = node.children();
961-
962 // If the action is invalid but the key is fine, we still want to return something.1002 // If the action is invalid but the key is fine, we still want to return something.
963 // That way, the parent can handle the existence of duplicate keybinds,1003 // That way, the parent can handle the existence of duplicate keybinds,
964 // even if their contents are not valid.1004 // even if their contents are not valid.
965 let dummy = Self {1005 let dummy = Self {
966 key,1006 key,
967- action: Action::Spawn(vec![]),1007+ action: BoundAction::Press(Action::Spawn(vec![])),
968 repeat: true,1008 repeat: true,
969 cooldown: None,1009 cooldown: None,
970 allow_when_locked: false,1010 allow_when_locked: false,
@@ -972,54 +1012,182 @@ where
972 hotkey_overlay_title: None,1012 hotkey_overlay_title: None,
973 };1013 };
974 1014
975- if let Some(child) = children.next() {1015+ // direct_action is an action node that is not wrapped in a press or release section
976- for unwanted_child in children {1016+ let mut direct_action: Option<Action> = None;
977- ctx.emit_error(DecodeError::unexpected(1017+ let mut press_action: Option<Action> = None;
978- unwanted_child,1018+ let mut release_action: Option<Action> = None;
979- "node",1019+
980- "only one action is allowed per keybind",1020+ let decode_action = |ctx: &mut knuffel::decode::Context<S>,
981- ));1021+ child: &knuffel::ast::SpannedNode<S>| {
982- }
983 match Action::decode_node(child, ctx) {1022 match Action::decode_node(child, ctx) {
984- Ok(action) => {1023+ Ok(action) => Some(action),
985- if !matches!(action, Action::Spawn(_) | Action::SpawnSh(_)) {1024+ Err(e) => {
986- if let Some(node) = allow_when_locked_node {1025+ ctx.emit_error(e);
987- ctx.emit_error(DecodeError::unexpected(1026+ None
988- node,1027+ }
989- "property",1028+ }
990- "allow-when-locked can only be set on spawn binds",1029+ };
991- ));
992- }
993- }
994 1030
995- // The toggle-inhibit action must always be uninhibitable.1031+ for child in node.children() {
996- // Otherwise, it would be impossible to trigger it.1032+ let child_name = &*child.node_name;
997- if matches!(action, Action::ToggleKeyboardShortcutsInhibit) {1033+ let child_name: &str = child_name.as_ref();
998- allow_inhibiting = false;1034+
999- }1035+ if child_name == "press" || child_name == "release" {
1036+ let is_press = child_name == "press";
1037+ let slot = if is_press {
1038+ &mut press_action
1039+ } else {
1040+ &mut release_action
1041+ };
1042+
1043+ if slot.is_some() {
1044+ ctx.emit_error(DecodeError::unexpected(
1045+ &child.node_name,
1046+ "section",
1047+ format!("duplicate `{child_name}` section"),
1048+ ));
1049+ continue;
1050+ }
1000 1051
1001- Ok(Self {1052+ if direct_action.is_some() {
1002- key,1053+ ctx.emit_error(DecodeError::unexpected(
1003- action,1054+ &child.node_name,
1004- repeat,1055+ "section",
1005- cooldown,1056+ "cannot mix direct actions with press/release sections",
1006- allow_when_locked,1057+ ));
1007- allow_inhibiting,1058+ continue;
1008- hotkey_overlay_title,
1009- })
1010 }1059 }
1011- Err(e) => {1060+
1012- ctx.emit_error(e);1061+ // The action is the section's single child node.
1013- Ok(dummy)1062+ let mut children = child.children();
1063+ let Some(action_child) = children.next() else {
1064+ ctx.emit_error(DecodeError::missing(
1065+ child,
1066+ format!("expected an action for this {child_name} section"),
1067+ ));
1068+ continue;
1069+ };
1070+
1071+ for unwanted_child in children {
1072+ ctx.emit_error(DecodeError::unexpected(
1073+ unwanted_child,
1074+ "node",
1075+ "only one action is allowed per keybind",
1076+ ));
1077+ }
1078+
1079+ *slot = decode_action(ctx, action_child);
1080+ } else {
1081+ if direct_action.is_some() {
1082+ ctx.emit_error(DecodeError::unexpected(
1083+ &child.node_name,
1084+ "node",
1085+ "only one action is allowed per keybind",
1086+ ));
1087+ continue;
1088+ }
1089+
1090+ if press_action.is_some() || release_action.is_some() {
1091+ ctx.emit_error(DecodeError::unexpected(
1092+ &child.node_name,
1093+ "node",
1094+ "cannot mix direct actions with press/release sections",
1095+ ));
1096+ continue;
1014 }1097 }
1098+
1099+ direct_action = decode_action(ctx, child);
1015 }1100 }
1016- } else {1101+ }
1017- ctx.emit_error(DecodeError::missing(1102+
1103+ let press_action = press_action.or(direct_action);
1104+
1105+ if release_action.is_some()
1106+ && matches!(
1107+ key.trigger,
1108+ Trigger::WheelScrollDown
1109+ | Trigger::WheelScrollUp
1110+ | Trigger::WheelScrollLeft
1111+ | Trigger::WheelScrollRight
1112+ | Trigger::TouchpadScrollDown
1113+ | Trigger::TouchpadScrollUp
1114+ | Trigger::TouchpadScrollLeft
1115+ | Trigger::TouchpadScrollRight
1116+ )
1117+ {
1118+ ctx.emit_error(DecodeError::unexpected(
1018 node,1119 node,
1019- "expected an action for this keybind",1120+ "bind",
1121+ "release sections are not supported for scroll binds",
1020 ));1122 ));
1021- Ok(dummy)1123+ release_action = None;
1124+ }
1125+
1126+ if let Some(node) = repeat_node {
1127+ if repeat == Some(true) && press_action.is_none() && release_action.is_some() {
1128+ ctx.emit_error(DecodeError::unexpected(
1129+ node,
1130+ "property",
1131+ "release-only binds cannot repeat",
1132+ ));
1133+ }
1134+ }
1135+
1136+ if let Some(node) = allow_when_locked_node {
1137+ for action in press_action.iter().chain(release_action.iter()) {
1138+ if !matches!(action, Action::Spawn(_) | Action::SpawnSh(_)) {
1139+ ctx.emit_error(DecodeError::unexpected(
1140+ node,
1141+ "property",
1142+ "allow-when-locked can only be set on spawn binds",
1143+ ));
1144+ }
1145+ }
1146+ }
1147+
1148+ // The toggle-inhibit action must always be uninhibitable.
1149+ // Otherwise, it would be impossible to trigger it.
1150+ if matches!(press_action, Some(Action::ToggleKeyboardShortcutsInhibit))
1151+ || matches!(release_action, Some(Action::ToggleKeyboardShortcutsInhibit))
1152+ {
1153+ allow_inhibiting = false;
1022 }1154 }
1155+
1156+ let repeat = match repeat {
1157+ Some(value) => value,
1158+ None => release_action.is_none(),
1159+ };
1160+
1161+ let action = match (press_action, release_action) {
1162+ (Some(press), Some(release)) => BoundAction::Both { press, release },
1163+ (Some(press), None) => BoundAction::Press(press),
1164+ (None, Some(release)) => BoundAction::Release(release),
1165+ (None, None) => {
1166+ // If a press or release section was present, an error about its missing or
1167+ // invalid action was already emitted above.
1168+ let has_section = node.children().any(|child| {
1169+ let name = child.node_name.as_ref();
1170+ name == "press" || name == "release"
1171+ });
1172+ if !has_section {
1173+ ctx.emit_error(DecodeError::missing(
1174+ node,
1175+ "expected an action for this keybind",
1176+ ));
1177+ }
1178+ return Ok(dummy);
1179+ }
1180+ };
1181+
1182+ Ok(Self {
1183+ key,
1184+ action,
1185+ repeat,
1186+ cooldown,
1187+ allow_when_locked,
1188+ allow_inhibiting,
1189+ hotkey_overlay_title,
1190+ })
1023 }1191 }
1024 }1192 }
1025 1193
niri-config/src/lib.rsmodified+124 -50
@@ -950,6 +950,12 @@ mod tests {
950 Mod+Shift+E allow-inhibiting=false { quit skip-confirmation=true; }950 Mod+Shift+E allow-inhibiting=false { quit skip-confirmation=true; }
951 Mod+WheelScrollDown cooldown-ms=150 { focus-workspace-down; }951 Mod+WheelScrollDown cooldown-ms=150 { focus-workspace-down; }
952 Super+Alt+S allow-when-locked=true { spawn-sh "pkill orca || exec orca"; }952 Super+Alt+S allow-when-locked=true { spawn-sh "pkill orca || exec orca"; }
953+ Mod {
954+ release { toggle-overview; }
955+ }
956+ Shift+Mod {
957+ release { toggle-window-floating; }
958+ }
953 }959 }
954 960
955 switch-events {961 switch-events {
@@ -2006,7 +2012,9 @@ mod tests {
2006 COMPOSITOR,2012 COMPOSITOR,
2007 ),2013 ),
2008 },2014 },
2009- action: ToggleKeyboardShortcutsInhibit,2015+ action: Press(
2016+ ToggleKeyboardShortcutsInhibit,
2017+ ),
2010 repeat: true,2018 repeat: true,
2011 cooldown: None,2019 cooldown: None,
2012 allow_when_locked: false,2020 allow_when_locked: false,
@@ -2026,7 +2034,9 @@ mod tests {
2026 SHIFT | COMPOSITOR,2034 SHIFT | COMPOSITOR,
2027 ),2035 ),
2028 },2036 },
2029- action: ToggleKeyboardShortcutsInhibit,2037+ action: Press(
2038+ ToggleKeyboardShortcutsInhibit,
2039+ ),
2030 repeat: true,2040 repeat: true,
2031 cooldown: None,2041 cooldown: None,
2032 allow_when_locked: false,2042 allow_when_locked: false,
@@ -2042,10 +2052,12 @@ mod tests {
2042 COMPOSITOR,2052 COMPOSITOR,
2043 ),2053 ),
2044 },2054 },
2045- action: Spawn(2055+ action: Press(
2046- [2056+ Spawn(
2047- "alacritty",2057+ [
2048- ],2058+ "alacritty",
2059+ ],
2060+ ),
2049 ),2061 ),
2050 repeat: true,2062 repeat: true,
2051 cooldown: None,2063 cooldown: None,
@@ -2062,7 +2074,9 @@ mod tests {
2062 COMPOSITOR,2074 COMPOSITOR,
2063 ),2075 ),
2064 },2076 },
2065- action: CloseWindow,2077+ action: Press(
2078+ CloseWindow,
2079+ ),
2066 repeat: true,2080 repeat: true,
2067 cooldown: None,2081 cooldown: None,
2068 allow_when_locked: false,2082 allow_when_locked: false,
@@ -2080,7 +2094,9 @@ mod tests {
2080 SHIFT | COMPOSITOR,2094 SHIFT | COMPOSITOR,
2081 ),2095 ),
2082 },2096 },
2083- action: FocusMonitorLeft,2097+ action: Press(
2098+ FocusMonitorLeft,
2099+ ),
2084 repeat: true,2100 repeat: true,
2085 cooldown: None,2101 cooldown: None,
2086 allow_when_locked: false,2102 allow_when_locked: false,
@@ -2096,8 +2112,10 @@ mod tests {
2096 SHIFT | COMPOSITOR,2112 SHIFT | COMPOSITOR,
2097 ),2113 ),
2098 },2114 },
2099- action: FocusMonitor(2115+ action: Press(
2100- "eDP-1",2116+ FocusMonitor(
2117+ "eDP-1",
2118+ ),
2101 ),2119 ),
2102 repeat: true,2120 repeat: true,
2103 cooldown: None,2121 cooldown: None,
@@ -2114,7 +2132,9 @@ mod tests {
2114 CTRL | SHIFT | COMPOSITOR,2132 CTRL | SHIFT | COMPOSITOR,
2115 ),2133 ),
2116 },2134 },
2117- action: MoveWindowToMonitorRight,2135+ action: Press(
2136+ MoveWindowToMonitorRight,
2137+ ),
2118 repeat: true,2138 repeat: true,
2119 cooldown: None,2139 cooldown: None,
2120 allow_when_locked: false,2140 allow_when_locked: false,
@@ -2130,8 +2150,10 @@ mod tests {
2130 CTRL | ALT | COMPOSITOR,2150 CTRL | ALT | COMPOSITOR,
2131 ),2151 ),
2132 },2152 },
2133- action: MoveWindowToMonitor(2153+ action: Press(
2134- "eDP-1",2154+ MoveWindowToMonitor(
2155+ "eDP-1",
2156+ ),
2135 ),2157 ),
2136 repeat: true,2158 repeat: true,
2137 cooldown: None,2159 cooldown: None,
@@ -2148,8 +2170,10 @@ mod tests {
2148 CTRL | ALT | COMPOSITOR,2170 CTRL | ALT | COMPOSITOR,
2149 ),2171 ),
2150 },2172 },
2151- action: MoveColumnToMonitor(2173+ action: Press(
2152- "DP-1",2174+ MoveColumnToMonitor(
2175+ "DP-1",
2176+ ),
2153 ),2177 ),
2154 repeat: true,2178 repeat: true,
2155 cooldown: None,2179 cooldown: None,
@@ -2166,7 +2190,9 @@ mod tests {
2166 COMPOSITOR,2190 COMPOSITOR,
2167 ),2191 ),
2168 },2192 },
2169- action: ConsumeWindowIntoColumn,2193+ action: Press(
2194+ ConsumeWindowIntoColumn,
2195+ ),
2170 repeat: true,2196 repeat: true,
2171 cooldown: None,2197 cooldown: None,
2172 allow_when_locked: false,2198 allow_when_locked: false,
@@ -2182,9 +2208,11 @@ mod tests {
2182 COMPOSITOR,2208 COMPOSITOR,
2183 ),2209 ),
2184 },2210 },
2185- action: FocusWorkspace(2211+ action: Press(
2186- Index(2212+ FocusWorkspace(
2187- 1,2213+ Index(
2214+ 1,
2215+ ),
2188 ),2216 ),
2189 ),2217 ),
2190 repeat: true,2218 repeat: true,
@@ -2202,9 +2230,11 @@ mod tests {
2202 SHIFT | COMPOSITOR,2230 SHIFT | COMPOSITOR,
2203 ),2231 ),
2204 },2232 },
2205- action: FocusWorkspace(2233+ action: Press(
2206- Name(2234+ FocusWorkspace(
2207- "workspace-1",2235+ Name(
2236+ "workspace-1",
2237+ ),
2208 ),2238 ),
2209 ),2239 ),
2210 repeat: true,2240 repeat: true,
@@ -2222,8 +2252,10 @@ mod tests {
2222 SHIFT | COMPOSITOR,2252 SHIFT | COMPOSITOR,
2223 ),2253 ),
2224 },2254 },
2225- action: Quit(2255+ action: Press(
2226- true,2256+ Quit(
2257+ true,
2258+ ),
2227 ),2259 ),
2228 repeat: true,2260 repeat: true,
2229 cooldown: None,2261 cooldown: None,
@@ -2238,7 +2270,9 @@ mod tests {
2238 COMPOSITOR,2270 COMPOSITOR,
2239 ),2271 ),
2240 },2272 },
2241- action: FocusWorkspaceDown,2273+ action: Press(
2274+ FocusWorkspaceDown,
2275+ ),
2242 repeat: true,2276 repeat: true,
2243 cooldown: Some(2277 cooldown: Some(
2244 150ms,2278 150ms,
@@ -2256,8 +2290,10 @@ mod tests {
2256 ALT | SUPER,2290 ALT | SUPER,
2257 ),2291 ),
2258 },2292 },
2259- action: SpawnSh(2293+ action: Press(
2260- "pkill orca || exec orca",2294+ SpawnSh(
2295+ "pkill orca || exec orca",
2296+ ),
2261 ),2297 ),
2262 repeat: true,2298 repeat: true,
2263 cooldown: None,2299 cooldown: None,
@@ -2265,6 +2301,38 @@ mod tests {
2265 allow_inhibiting: true,2301 allow_inhibiting: true,
2266 hotkey_overlay_title: None,2302 hotkey_overlay_title: None,
2267 },2303 },
2304+ Bind {
2305+ key: Key {
2306+ trigger: CompositorMod,
2307+ modifiers: Modifiers(
2308+ COMPOSITOR,
2309+ ),
2310+ },
2311+ action: Release(
2312+ ToggleOverview,
2313+ ),
2314+ repeat: false,
2315+ cooldown: None,
2316+ allow_when_locked: false,
2317+ allow_inhibiting: true,
2318+ hotkey_overlay_title: None,
2319+ },
2320+ Bind {
2321+ key: Key {
2322+ trigger: CompositorMod,
2323+ modifiers: Modifiers(
2324+ SHIFT | COMPOSITOR,
2325+ ),
2326+ },
2327+ action: Release(
2328+ ToggleWindowFloating,
2329+ ),
2330+ repeat: false,
2331+ cooldown: None,
2332+ allow_when_locked: false,
2333+ allow_inhibiting: true,
2334+ hotkey_overlay_title: None,
2335+ },
2268 ],2336 ],
2269 ),2337 ),
2270 switch_events: SwitchBinds {2338 switch_events: SwitchBinds {
@@ -2378,13 +2446,15 @@ mod tests {
2378 ALT,2446 ALT,
2379 ),2447 ),
2380 },2448 },
2381- action: MruAdvance {2449+ action: Press(
2382- direction: Forward,2450+ MruAdvance {
2383- scope: None,2451+ direction: Forward,
2384- filter: Some(2452+ scope: None,
2385- All,2453+ filter: Some(
2386- ),2454+ All,
2387- },2455+ ),
2456+ },
2457+ ),
2388 repeat: true,2458 repeat: true,
2389 cooldown: None,2459 cooldown: None,
2390 allow_when_locked: false,2460 allow_when_locked: false,
@@ -2400,13 +2470,15 @@ mod tests {
2400 ALT,2470 ALT,
2401 ),2471 ),
2402 },2472 },
2403- action: MruAdvance {2473+ action: Press(
2404- direction: Forward,2474+ MruAdvance {
2405- scope: None,2475+ direction: Forward,
2406- filter: Some(2476+ scope: None,
2407- AppId,2477+ filter: Some(
2408- ),2478+ AppId,
2409- },2479+ ),
2480+ },
2481+ ),
2410 repeat: true,2482 repeat: true,
2411 cooldown: None,2483 cooldown: None,
2412 allow_when_locked: false,2484 allow_when_locked: false,
@@ -2422,15 +2494,17 @@ mod tests {
2422 SUPER,2494 SUPER,
2423 ),2495 ),
2424 },2496 },
2425- action: MruAdvance {2497+ action: Press(
2426- direction: Forward,2498+ MruAdvance {
2427- scope: Some(2499+ direction: Forward,
2428- Output,2500+ scope: Some(
2429- ),2501+ Output,
2430- filter: Some(2502+ ),
2431- All,2503+ filter: Some(
2432- ),2504+ All,
2433- },2505+ ),
2506+ },
2507+ ),
2434 repeat: true,2508 repeat: true,
2435 cooldown: None,2509 cooldown: None,
2436 allow_when_locked: false,2510 allow_when_locked: false,
niri-config/src/recent_windows.rsmodified+2 -2
@@ -2,7 +2,7 @@ use knuffel::errors::DecodeError;
2 use smithay::input::keyboard::Keysym;2 use smithay::input::keyboard::Keysym;
3 3
4 use crate::utils::{expect_only_children, MergeWith};4 use crate::utils::{expect_only_children, MergeWith};
5-use crate::{Action, Bind, Color, FloatOrInt, Key, Modifiers, Trigger};5+use crate::{Action, Bind, BoundAction, Color, FloatOrInt, Key, Modifiers, Trigger};
6 6
7 #[derive(Debug, PartialEq)]7 #[derive(Debug, PartialEq)]
8 pub struct RecentWindows {8 pub struct RecentWindows {
@@ -146,7 +146,7 @@ impl From<MruBind> for Bind {
146 fn from(x: MruBind) -> Self {146 fn from(x: MruBind) -> Self {
147 Self {147 Self {
148 key: x.key,148 key: x.key,
149- action: Action::from(x.action),149+ action: BoundAction::Press(Action::from(x.action)),
150 repeat: true,150 repeat: true,
151 cooldown: None,151 cooldown: None,
152 allow_when_locked: false,152 allow_when_locked: false,
src/backend/tty.rsmodified+8 -0
@@ -618,6 +618,14 @@ impl Tty {
618 SessionEvent::PauseSession => {618 SessionEvent::PauseSession => {
619 debug!("pausing session");619 debug!("pausing session");
620 620
621+ // We won't receive key or button releases for whatever is held down while the
622+ // session is paused, so forget about all of it.
623+ //
624+ // Slightly cursed to do from an idle... But we don't have State here conveniently,
625+ // and it should be fine to delay this until the loop callback.
626+ niri.event_loop
627+ .insert_idle(|state| state.release_held_inputs());
628+
621 self.libinput.suspend();629 self.libinput.suspend();
622 630
623 for device in self.devices.values_mut() {631 for device in self.devices.values_mut() {
src/input/mod.rsmodified+427 -151
@@ -1,12 +1,14 @@
1 use std::any::Any;1 use std::any::Any;
2 use std::collections::hash_map::Entry;2 use std::collections::hash_map::Entry;
3-use std::collections::HashSet;3+use std::collections::{HashMap, HashSet};
4 use std::time::Duration;4 use std::time::Duration;
5 5
6 use calloop::timer::{TimeoutAction, Timer};6 use calloop::timer::{TimeoutAction, Timer};
7+use calloop::RegistrationToken;
7 use input::event::gesture::GestureEventCoordinates as _;8 use input::event::gesture::GestureEventCoordinates as _;
8 use niri_config::{9 use niri_config::{
9- Action, Bind, Binds, Config, Key, ModKey, Modifiers, MruDirection, SwitchBinds, Trigger,10+ is_keysym_modifier, Action, Bind, Binds, BoundAction, Config, Key, ModKey, Modifiers,
11+ MruDirection, SwitchBinds, Trigger,
10 };12 };
11 use niri_ipc::LayoutSwitchTarget;13 use niri_ipc::LayoutSwitchTarget;
12 use smithay::backend::input::{14 use smithay::backend::input::{
@@ -50,7 +52,7 @@ use self::spatial_movement_grab::SpatialMovementGrab;
50 use crate::dbus::freedesktop_a11y::KbMonBlock;52 use crate::dbus::freedesktop_a11y::KbMonBlock;
51 use crate::layout::scrolling::ScrollDirection;53 use crate::layout::scrolling::ScrollDirection;
52 use crate::layout::{ActivateWindow, LayoutElement as _};54 use crate::layout::{ActivateWindow, LayoutElement as _};
53-use crate::niri::{CastTarget, PointerVisibility, State};55+use crate::niri::{CastTarget, Niri, PointerVisibility, State};
54 use crate::ui::mru::{WindowMru, WindowMruUi};56 use crate::ui::mru::{WindowMru, WindowMruUi};
55 use crate::ui::screenshot_ui::ScreenshotUi;57 use crate::ui::screenshot_ui::ScreenshotUi;
56 use crate::utils::spawning::{spawn, spawn_sh};58 use crate::utils::spawning::{spawn, spawn_sh};
@@ -137,6 +139,8 @@ impl<D: SeatHandler + TabletSeatHandler> AnyStartData<D> {
137 enum ShouldInterceptResult {139 enum ShouldInterceptResult {
138 /// Forward the event to the focused client.140 /// Forward the event to the focused client.
139 Forward,141 Forward,
142+ /// Forward the event to the focused client, and handle the given bind.
143+ ForwardAndHandle(Bind),
140 /// Do not forward the event, and handle the given bind.144 /// Do not forward the event, and handle the given bind.
141 InterceptAndHandle(Bind),145 InterceptAndHandle(Bind),
142 /// Do not forward the event, and do nothing else.146 /// Do not forward the event, and do nothing else.
@@ -492,6 +496,13 @@ impl State {
492 // The accessibility modifier first press must not change XKB state, so we return496 // The accessibility modifier first press must not change XKB state, so we return
493 // early here.497 // early here.
494 if block == KbMonBlock::ModifierFirstPress {498 if block == KbMonBlock::ModifierFirstPress {
499+ // A pending release action recorded for the key must still run.
500+ if !pressed {
501+ if let Some(bind) = self.niri.pending_release_binds.remove(&event.key_code()) {
502+ self.niri.suppressed_keys.remove(&event.key_code());
503+ self.handle_bind(bind, false);
504+ }
505+ }
495 return;506 return;
496 }507 }
497 block508 block
@@ -533,8 +544,13 @@ impl State {
533 // modifiers without accidentally leaking them to wl_keyboard.enter. So for now544 // modifiers without accidentally leaking them to wl_keyboard.enter. So for now
534 // let's forward modifier releases to the clients here to deal with the most545 // let's forward modifier releases to the clients here to deal with the most
535 // common case.546 // common case.
536- if !pressed547+ if !pressed {
537- && matches!(548+ // The accessibility handler consumes the release.
549+ // So, make sure to run any pending release actions.
550+ let bind = this.niri.pending_release_binds.remove(&key_code);
551+ this.niri.suppressed_keys.remove(&key_code);
552+
553+ let forward = matches!(
538 modified,554 modified,
539 Keysym::Shift_L555 Keysym::Shift_L
540 | Keysym::Shift_R556 | Keysym::Shift_R
@@ -544,12 +560,11 @@ impl State {
544 | Keysym::Super_R560 | Keysym::Super_R
545 | Keysym::Alt_L561 | Keysym::Alt_L
546 | Keysym::Alt_R562 | Keysym::Alt_R
547- )563+ );
548- {564+ return bind_intercept_result(bind, forward);
549- return ShouldInterceptResult::Forward;
550- } else {
551- return ShouldInterceptResult::InterceptOnly;
552 }565 }
566+
567+ return ShouldInterceptResult::InterceptOnly;
553 }568 }
554 569
555 if this.niri.exit_confirm_dialog.is_open() && pressed {570 if this.niri.exit_confirm_dialog.is_open() && pressed {
@@ -568,11 +583,13 @@ impl State {
568 if this.niri.window_mru_ui.is_open() && !pressed && modifiers.is_empty() {583 if this.niri.window_mru_ui.is_open() && !pressed && modifiers.is_empty() {
569 this.do_action(Action::MruConfirm, false);584 this.do_action(Action::MruConfirm, false);
570 585
571- if this.niri.suppressed_keys.remove(&key_code) {586+ // A pending release action recorded for the key must still run if it
572- return ShouldInterceptResult::InterceptOnly;587+ // closed the MRU instead of going through the regular bind lookup.
573- } else {588+ return released_key_intercept_result(
574- return ShouldInterceptResult::Forward;589+ &mut this.niri.suppressed_keys,
575- }590+ &mut this.niri.pending_release_binds,
591+ key_code,
592+ );
576 }593 }
577 594
578 if pressed && raw == Some(Keysym::Escape) {595 if pressed && raw == Some(Keysym::Escape) {
@@ -592,6 +609,11 @@ impl State {
592 this.niri.screenshot_ui.set_space_down(pressed);609 this.niri.screenshot_ui.set_space_down(pressed);
593 }610 }
594 611
612+ if pressed {
613+ this.niri.cancel_interrupted_release_binds();
614+ }
615+
616+ let is_locked = this.niri.is_locked();
595 let res = {617 let res = {
596 let config = this.niri.config.borrow();618 let config = this.niri.config.borrow();
597 let bindings =619 let bindings =
@@ -599,6 +621,8 @@ impl State {
599 621
600 should_intercept_key(622 should_intercept_key(
601 &mut this.niri.suppressed_keys,623 &mut this.niri.suppressed_keys,
624+ &mut this.niri.pending_release_binds,
625+ &this.niri.bind_cooldown_timers,
602 bindings,626 bindings,
603 mod_key,627 mod_key,
604 key_code,628 key_code,
@@ -609,6 +633,7 @@ impl State {
609 &this.niri.screenshot_ui,633 &this.niri.screenshot_ui,
610 this.niri.config.borrow().input.disable_power_key_handling,634 this.niri.config.borrow().input.disable_power_key_handling,
611 is_inhibiting_shortcuts,635 is_inhibiting_shortcuts,
636+ is_locked,
612 )637 )
613 };638 };
614 639
@@ -633,8 +658,9 @@ impl State {
633 return;658 return;
634 };659 };
635 660
636- match result {661+ // Forward the event to the client, unless the key was intercepted.
637- ShouldInterceptResult::Forward => {662+ match &result {
663+ ShouldInterceptResult::Forward | ShouldInterceptResult::ForwardAndHandle(_) => {
638 keyboard.input_forward(664 keyboard.input_forward(
639 self,665 self,
640 event.key_code(),666 event.key_code(),
@@ -644,13 +670,23 @@ impl State {
644 mods_changed,670 mods_changed,
645 );671 );
646 }672 }
647- ShouldInterceptResult::InterceptAndHandle(bind) => {673+ ShouldInterceptResult::InterceptAndHandle(_) | ShouldInterceptResult::InterceptOnly => {
674+ if mods_changed {
675+ keyboard.advertise_modifier_state(self);
676+ }
677+ }
678+ }
679+
680+ // Handle the bind, if the result came with one.
681+ match result {
682+ ShouldInterceptResult::ForwardAndHandle(bind)
683+ | ShouldInterceptResult::InterceptAndHandle(bind) => {
684+ self.handle_bind(bind.clone(), pressed);
648 if pressed {685 if pressed {
649- self.handle_bind(bind.clone());
650 self.start_key_repeat(bind);686 self.start_key_repeat(bind);
651 }687 }
652 }688 }
653- ShouldInterceptResult::InterceptOnly => {}689+ ShouldInterceptResult::Forward | ShouldInterceptResult::InterceptOnly => {}
654 }690 }
655 }691 }
656 692
@@ -680,7 +716,7 @@ impl State {
680 .niri716 .niri
681 .event_loop717 .event_loop
682 .insert_source(repeat_timer, move |_, _, state| {718 .insert_source(repeat_timer, move |_, _, state| {
683- state.handle_bind(bind.clone());719+ state.handle_bind(bind.clone(), true);
684 TimeoutAction::ToDuration(repeat_duration)720 TimeoutAction::ToDuration(repeat_duration)
685 })721 })
686 .unwrap();722 .unwrap();
@@ -710,37 +746,75 @@ impl State {
710 self.niri.queue_redraw_all();746 self.niri.queue_redraw_all();
711 }747 }
712 748
713- pub fn handle_bind(&mut self, bind: Bind) {749+ pub fn handle_bind(&mut self, bind: Bind, pressed: bool) {
714- let Some(cooldown) = bind.cooldown else {750+ let action = if pressed {
715- self.do_action(bind.action, bind.allow_when_locked);751+ bind.press_action()
752+ } else {
753+ bind.release_action()
754+ };
755+ let Some(action) = action.cloned() else {
716 return;756 return;
717 };757 };
718 758
759+ let allowed_when_locked = bind_allowed_when_locked(&bind, pressed);
760+ let allowed_during_screenshot = bind_allowed_during_screenshot(&bind, pressed);
761+
719 // Check this first so that it doesn't trigger the cooldown.762 // Check this first so that it doesn't trigger the cooldown.
720- if self.niri.is_locked() && !(bind.allow_when_locked || allowed_when_locked(&bind.action)) {763+ if self.niri.is_locked() && !allowed_when_locked {
721 return;764 return;
722 }765 }
723 766
724- match self.niri.bind_cooldown_timers.entry(bind.key) {767+ // The bind lookup already substitutes the screenshot UI's own
725- // The bind is on cooldown.768+ // actions for presses, so only releases need to be filtered here.
726- Entry::Occupied(_) => (),769+ if !pressed && self.niri.screenshot_ui.is_open() && !allowed_during_screenshot {
727- Entry::Vacant(entry) => {770+ return;
728- let timer = Timer::from_duration(cooldown);771+ }
729- let token = self
730- .niri
731- .event_loop
732- .insert_source(timer, move |_, _, state| {
733- if state.niri.bind_cooldown_timers.remove(&bind.key).is_none() {
734- error!("bind cooldown timer entry disappeared");
735- }
736- TimeoutAction::Drop
737- })
738- .unwrap();
739- entry.insert(token);
740 772
741- self.do_action(bind.action, bind.allow_when_locked);773+ // Check to see if we are under a cooldown.
774+ // We apply the cooldown to the press action for press-only and press/release.
775+ // We apply the cooldown to the release action for release-only.
776+ if pressed == bind.has_press() {
777+ if let Some(cooldown) = bind.cooldown {
778+ match self.niri.bind_cooldown_timers.entry(bind.key) {
779+ // The bind is on cooldown, so don't run its action.
780+ Entry::Occupied(_) => return,
781+ Entry::Vacant(entry) => {
782+ let timer = Timer::from_duration(cooldown);
783+ let token = self
784+ .niri
785+ .event_loop
786+ .insert_source(timer, move |_, _, state| {
787+ if state.niri.bind_cooldown_timers.remove(&bind.key).is_none() {
788+ error!("bind cooldown timer entry disappeared");
789+ }
790+ TimeoutAction::Drop
791+ })
792+ .unwrap();
793+ entry.insert(token);
794+ }
795+ }
742 }796 }
743 }797 }
798+
799+ self.do_action(action, allowed_when_locked);
800+ }
801+
802+ /// Forgets all held inputs, running the release actions of anything that was still held.
803+ pub fn release_held_inputs(&mut self) {
804+ let mut binds = Vec::new();
805+ binds.extend(
806+ self.niri
807+ .pending_release_binds
808+ .drain()
809+ .map(|(_, bind)| bind),
810+ );
811+
812+ // Forget the rest of the input state: the releases will never arrive.
813+ self.niri.clear_input_state();
814+
815+ for bind in binds {
816+ self.handle_bind(bind, false);
817+ }
744 }818 }
745 819
746 pub fn do_action(&mut self, action: Action, allow_when_locked: bool) {820 pub fn do_action(&mut self, action: Action, allow_when_locked: bool) {
@@ -763,26 +837,18 @@ impl State {
763 self.niri.stop_signal.stop()837 self.niri.stop_signal.stop()
764 }838 }
765 Action::ChangeVt(vt) => {839 Action::ChangeVt(vt) => {
766- self.backend.change_vt(vt);840+ // Changing VT may not deliver the key releases, so treat the held binds as
767-841+ // released while the session is still usable.
768- // Changing VT may not deliver the key releases, so clear the state.842+ self.release_held_inputs();
769- self.niri.suppressed_keys.clear();
770 843
771- // Stop ongoing key repeat if any.844+ self.backend.change_vt(vt);
772- if let Some(token) = self.niri.bind_repeat_timer.take() {
773- self.niri.event_loop.remove(token);
774- }
775 }845 }
776 Action::Suspend => {846 Action::Suspend => {
777- self.backend.suspend();847+ // Suspend may not deliver the key releases, so treat the held binds as
848+ // released before suspending.
849+ self.release_held_inputs();
778 850
779- // Suspend may not deliver the key releases, so clear the state.851+ self.backend.suspend();
780- self.niri.suppressed_keys.clear();
781-
782- // Stop ongoing key repeat if any.
783- if let Some(token) = self.niri.bind_repeat_timer.take() {
784- self.niri.event_loop.remove(token);
785- }
786 }852 }
787 Action::PowerOffMonitors => {853 Action::PowerOffMonitors => {
788 self.niri.deactivate_monitors(&mut self.backend);854 self.niri.deactivate_monitors(&mut self.backend);
@@ -2869,16 +2935,17 @@ impl State {
2869 2935
2870 let mod_key = self.backend.mod_key(&self.niri.config.borrow());2936 let mod_key = self.backend.mod_key(&self.niri.config.borrow());
2871 2937
2872- // Ignore release events for mouse clicks that triggered a bind.
2873- if self.niri.suppressed_buttons.remove(&button_code) {
2874- return;
2875- }
2876-
2877 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();2938 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
2878 let modifiers = modifiers_from_state(mods);2939 let modifiers = modifiers_from_state(mods);
2879 let mod_down = modifiers.contains(mod_key.to_modifiers());2940 let mod_down = modifiers.contains(mod_key.to_modifiers());
2880 2941
2942+ if self.niri.suppressed_buttons.remove(&button_code) {
2943+ return;
2944+ }
2945+
2881 if ButtonState::Pressed == button_state {2946 if ButtonState::Pressed == button_state {
2947+ self.niri.cancel_interrupted_release_binds();
2948+
2882 let mut is_mru_open = false;2949 let mut is_mru_open = false;
2883 if let Some(mru_output) = self.niri.window_mru_ui.output() {2950 if let Some(mru_output) = self.niri.window_mru_ui.output() {
2884 is_mru_open = true;2951 is_mru_open = true;
@@ -2917,12 +2984,14 @@ impl State {
2917 find_configured_bind(bindings, mod_key, trigger, mods)2984 find_configured_bind(bindings, mod_key, trigger, mods)
2918 })2985 })
2919 .filter(|bind| {2986 .filter(|bind| {
2920- !self.niri.screenshot_ui.is_open() || allowed_during_screenshot(&bind.action)2987+ !self.niri.screenshot_ui.is_open() || bind_allowed_during_screenshot(bind, true)
2921 }) {2988 }) {
2922 self.niri.suppressed_buttons.insert(button_code);2989 self.niri.suppressed_buttons.insert(button_code);
2923- self.handle_bind(bind.clone());2990+ if bind.has_press() {
2991+ self.handle_bind(bind.clone(), true);
2992+ }
2924 return;2993 return;
2925- };2994+ }
2926 }2995 }
2927 2996
2928 // We received an event for the regular pointer, so show it now.2997 // We received an event for the regular pointer, so show it now.
@@ -3187,6 +3256,8 @@ impl State {
3187 }3256 }
3188 3257
3189 fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) {3258 fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) {
3259+ self.niri.cancel_interrupted_release_binds();
3260+
3190 let pointer = &self.niri.seat.get_pointer().unwrap();3261 let pointer = &self.niri.seat.get_pointer().unwrap();
3191 3262
3192 let source = event.source();3263 let source = event.source();
@@ -3249,7 +3320,7 @@ impl State {
3249 trigger: Trigger::WheelScrollLeft,3320 trigger: Trigger::WheelScrollLeft,
3250 modifiers: Modifiers::empty(),3321 modifiers: Modifiers::empty(),
3251 },3322 },
3252- action: Action::FocusColumnLeftUnderMouse,3323+ action: BoundAction::Press(Action::FocusColumnLeftUnderMouse),
3253 repeat: true,3324 repeat: true,
3254 cooldown: None,3325 cooldown: None,
3255 allow_when_locked: false,3326 allow_when_locked: false,
@@ -3261,7 +3332,7 @@ impl State {
3261 trigger: Trigger::WheelScrollRight,3332 trigger: Trigger::WheelScrollRight,
3262 modifiers: Modifiers::empty(),3333 modifiers: Modifiers::empty(),
3263 },3334 },
3264- action: Action::FocusColumnRightUnderMouse,3335+ action: BoundAction::Press(Action::FocusColumnRightUnderMouse),
3265 repeat: true,3336 repeat: true,
3266 cooldown: None,3337 cooldown: None,
3267 allow_when_locked: false,3338 allow_when_locked: false,
@@ -3281,7 +3352,7 @@ impl State {
3281 )3352 )
3282 .filter(|bind| {3353 .filter(|bind| {
3283 !self.niri.screenshot_ui.is_open()3354 !self.niri.screenshot_ui.is_open()
3284- || allowed_during_screenshot(&bind.action)3355+ || bind_allowed_during_screenshot(bind, true)
3285 });3356 });
3286 let bind_right = find_configured_bind(3357 let bind_right = find_configured_bind(
3287 bindings,3358 bindings,
@@ -3291,19 +3362,19 @@ impl State {
3291 )3362 )
3292 .filter(|bind| {3363 .filter(|bind| {
3293 !self.niri.screenshot_ui.is_open()3364 !self.niri.screenshot_ui.is_open()
3294- || allowed_during_screenshot(&bind.action)3365+ || bind_allowed_during_screenshot(bind, true)
3295 });3366 });
3296 (bind_left, bind_right)3367 (bind_left, bind_right)
3297 };3368 };
3298 3369
3299 if let Some(right) = bind_right {3370 if let Some(right) = bind_right {
3300 for _ in 0..ticks {3371 for _ in 0..ticks {
3301- self.handle_bind(right.clone());3372+ self.handle_bind(right.clone(), true);
3302 }3373 }
3303 }3374 }
3304 if let Some(left) = bind_left {3375 if let Some(left) = bind_left {
3305 for _ in ticks..0 {3376 for _ in ticks..0 {
3306- self.handle_bind(left.clone());3377+ self.handle_bind(left.clone(), true);
3307 }3378 }
3308 }3379 }
3309 }3380 }
@@ -3329,7 +3400,7 @@ impl State {
3329 trigger: Trigger::WheelScrollUp,3400 trigger: Trigger::WheelScrollUp,
3330 modifiers: Modifiers::empty(),3401 modifiers: Modifiers::empty(),
3331 },3402 },
3332- action: Action::FocusWorkspaceUpUnderMouse,3403+ action: BoundAction::Press(Action::FocusWorkspaceUpUnderMouse),
3333 repeat: true,3404 repeat: true,
3334 cooldown: Some(Duration::from_millis(50)),3405 cooldown: Some(Duration::from_millis(50)),
3335 allow_when_locked: false,3406 allow_when_locked: false,
@@ -3341,7 +3412,7 @@ impl State {
3341 trigger: Trigger::WheelScrollDown,3412 trigger: Trigger::WheelScrollDown,
3342 modifiers: Modifiers::empty(),3413 modifiers: Modifiers::empty(),
3343 },3414 },
3344- action: Action::FocusWorkspaceDownUnderMouse,3415+ action: BoundAction::Press(Action::FocusWorkspaceDownUnderMouse),
3345 repeat: true,3416 repeat: true,
3346 cooldown: Some(Duration::from_millis(50)),3417 cooldown: Some(Duration::from_millis(50)),
3347 allow_when_locked: false,3418 allow_when_locked: false,
@@ -3355,7 +3426,7 @@ impl State {
3355 trigger: Trigger::WheelScrollUp,3426 trigger: Trigger::WheelScrollUp,
3356 modifiers: Modifiers::empty(),3427 modifiers: Modifiers::empty(),
3357 },3428 },
3358- action: Action::FocusColumnLeftUnderMouse,3429+ action: BoundAction::Press(Action::FocusColumnLeftUnderMouse),
3359 repeat: true,3430 repeat: true,
3360 cooldown: Some(Duration::from_millis(50)),3431 cooldown: Some(Duration::from_millis(50)),
3361 allow_when_locked: false,3432 allow_when_locked: false,
@@ -3367,7 +3438,7 @@ impl State {
3367 trigger: Trigger::WheelScrollDown,3438 trigger: Trigger::WheelScrollDown,
3368 modifiers: Modifiers::empty(),3439 modifiers: Modifiers::empty(),
3369 },3440 },
3370- action: Action::FocusColumnRightUnderMouse,3441+ action: BoundAction::Press(Action::FocusColumnRightUnderMouse),
3371 repeat: true,3442 repeat: true,
3372 cooldown: Some(Duration::from_millis(50)),3443 cooldown: Some(Duration::from_millis(50)),
3373 allow_when_locked: false,3444 allow_when_locked: false,
@@ -3387,25 +3458,25 @@ impl State {
3387 )3458 )
3388 .filter(|bind| {3459 .filter(|bind| {
3389 !self.niri.screenshot_ui.is_open()3460 !self.niri.screenshot_ui.is_open()
3390- || allowed_during_screenshot(&bind.action)3461+ || bind_allowed_during_screenshot(bind, true)
3391 });3462 });
3392 let bind_down =3463 let bind_down =
3393 find_configured_bind(bindings, mod_key, Trigger::WheelScrollDown, mods)3464 find_configured_bind(bindings, mod_key, Trigger::WheelScrollDown, mods)
3394 .filter(|bind| {3465 .filter(|bind| {
3395 !self.niri.screenshot_ui.is_open()3466 !self.niri.screenshot_ui.is_open()
3396- || allowed_during_screenshot(&bind.action)3467+ || bind_allowed_during_screenshot(bind, true)
3397 });3468 });
3398 (bind_up, bind_down)3469 (bind_up, bind_down)
3399 };3470 };
3400 3471
3401 if let Some(down) = bind_down {3472 if let Some(down) = bind_down {
3402 for _ in 0..ticks {3473 for _ in 0..ticks {
3403- self.handle_bind(down.clone());3474+ self.handle_bind(down.clone(), true);
3404 }3475 }
3405 }3476 }
3406 if let Some(up) = bind_up {3477 if let Some(up) = bind_up {
3407 for _ in ticks..0 {3478 for _ in ticks..0 {
3408- self.handle_bind(up.clone());3479+ self.handle_bind(up.clone(), true);
3409 }3480 }
3410 }3481 }
3411 }3482 }
@@ -3539,24 +3610,24 @@ impl State {
3539 )3610 )
3540 .filter(|bind| {3611 .filter(|bind| {
3541 !self.niri.screenshot_ui.is_open()3612 !self.niri.screenshot_ui.is_open()
3542- || allowed_during_screenshot(&bind.action)3613+ || bind_allowed_during_screenshot(bind, true)
3543 });3614 });
3544 let bind_right =3615 let bind_right =
3545 find_configured_bind(bindings, mod_key, Trigger::TouchpadScrollRight, mods)3616 find_configured_bind(bindings, mod_key, Trigger::TouchpadScrollRight, mods)
3546 .filter(|bind| {3617 .filter(|bind| {
3547 !self.niri.screenshot_ui.is_open()3618 !self.niri.screenshot_ui.is_open()
3548- || allowed_during_screenshot(&bind.action)3619+ || bind_allowed_during_screenshot(bind, true)
3549 });3620 });
3550 drop(config);3621 drop(config);
3551 3622
3552 if let Some(right) = bind_right {3623 if let Some(right) = bind_right {
3553 for _ in 0..ticks {3624 for _ in 0..ticks {
3554- self.handle_bind(right.clone());3625+ self.handle_bind(right.clone(), true);
3555 }3626 }
3556 }3627 }
3557 if let Some(left) = bind_left {3628 if let Some(left) = bind_left {
3558 for _ in ticks..0 {3629 for _ in ticks..0 {
3559- self.handle_bind(left.clone());3630+ self.handle_bind(left.clone(), true);
3560 }3631 }
3561 }3632 }
3562 }3633 }
@@ -3577,24 +3648,24 @@ impl State {
3577 )3648 )
3578 .filter(|bind| {3649 .filter(|bind| {
3579 !self.niri.screenshot_ui.is_open()3650 !self.niri.screenshot_ui.is_open()
3580- || allowed_during_screenshot(&bind.action)3651+ || bind_allowed_during_screenshot(bind, true)
3581 });3652 });
3582 let bind_down =3653 let bind_down =
3583 find_configured_bind(bindings, mod_key, Trigger::TouchpadScrollDown, mods)3654 find_configured_bind(bindings, mod_key, Trigger::TouchpadScrollDown, mods)
3584 .filter(|bind| {3655 .filter(|bind| {
3585 !self.niri.screenshot_ui.is_open()3656 !self.niri.screenshot_ui.is_open()
3586- || allowed_during_screenshot(&bind.action)3657+ || bind_allowed_during_screenshot(bind, true)
3587 });3658 });
3588 drop(config);3659 drop(config);
3589 3660
3590 if let Some(down) = bind_down {3661 if let Some(down) = bind_down {
3591 for _ in 0..ticks {3662 for _ in 0..ticks {
3592- self.handle_bind(down.clone());3663+ self.handle_bind(down.clone(), true);
3593 }3664 }
3594 }3665 }
3595 if let Some(up) = bind_up {3666 if let Some(up) = bind_up {
3596 for _ in ticks..0 {3667 for _ in ticks..0 {
3597- self.handle_bind(up.clone());3668+ self.handle_bind(up.clone(), true);
3598 }3669 }
3599 }3670 }
3600 }3671 }
@@ -3771,6 +3842,8 @@ impl State {
3771 3842
3772 match tip_state {3843 match tip_state {
3773 TabletToolTipState::Down => {3844 TabletToolTipState::Down => {
3845+ self.niri.cancel_interrupted_release_binds();
3846+
3774 if let Some(pos) = self.niri.tablet_cursor_location {3847 if let Some(pos) = self.niri.tablet_cursor_location {
3775 let under = self.niri.contents_under(pos);3848 let under = self.niri.contents_under(pos);
3776 3849
@@ -3986,10 +4059,9 @@ impl State {
3986 4059
3987 if let Some(tool) = tool {4060 if let Some(tool) = tool {
3988 let button = event.button();4061 let button = event.button();
4062+ let button_state = event.button_state();
3989 4063
3990- if self.niri.suppressed_buttons.remove(&button) {4064+ let mod_key = self.backend.mod_key(&self.niri.config.borrow());
3991- return;
3992- }
3993 4065
3994 let trigger = match button {4066 let trigger = match button {
3995 BTN_STYLUS => Some(Trigger::TabletStylusButton1),4067 BTN_STYLUS => Some(Trigger::TabletStylusButton1),
@@ -3998,9 +4070,17 @@ impl State {
3998 _ => None,4070 _ => None,
3999 };4071 };
4000 4072
4073+ if self.niri.suppressed_buttons.remove(&button) {
4074+ return;
4075+ }
4076+
4077+ if button_state == ButtonState::Pressed {
4078+ self.niri.cancel_interrupted_release_binds();
4079+ }
4080+
4081+ // Handle press binds.
4001 if let Some(trigger) = trigger {4082 if let Some(trigger) = trigger {
4002- if event.button_state() == ButtonState::Pressed {4083+ if button_state == ButtonState::Pressed {
4003- let mod_key = self.backend.mod_key(&self.niri.config.borrow());
4004 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();4084 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
4005 let modifiers = modifiers_from_state(mods);4085 let modifiers = modifiers_from_state(mods);
4006 4086
@@ -4012,11 +4092,13 @@ impl State {
4012 }4092 }
4013 .filter(|bind| {4093 .filter(|bind| {
4014 !self.niri.screenshot_ui.is_open()4094 !self.niri.screenshot_ui.is_open()
4015- || allowed_during_screenshot(&bind.action)4095+ || bind_allowed_during_screenshot(bind, true)
4016 });4096 });
4017 if let Some(bind) = bind {4097 if let Some(bind) = bind {
4018 self.niri.suppressed_buttons.insert(button);4098 self.niri.suppressed_buttons.insert(button);
4019- self.handle_bind(bind.clone());4099+ if bind.has_press() {
4100+ self.handle_bind(bind.clone(), true);
4101+ }
4020 return;4102 return;
4021 }4103 }
4022 }4104 }
@@ -4030,7 +4112,7 @@ impl State {
4030 &tablet::tool::ButtonEvent {4112 &tablet::tool::ButtonEvent {
4031 serial: SERIAL_COUNTER.next_serial(),4113 serial: SERIAL_COUNTER.next_serial(),
4032 button,4114 button,
4033- state: event.button_state(),4115+ state: button_state,
4034 time,4116 time,
4035 },4117 },
4036 );4118 );
@@ -4040,6 +4122,8 @@ impl State {
4040 }4122 }
4041 4123
4042 fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) {4124 fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) {
4125+ self.niri.cancel_interrupted_release_binds();
4126+
4043 if self.niri.window_mru_ui.is_open() || self.niri.screenshot_ui.is_open() {4127 if self.niri.window_mru_ui.is_open() || self.niri.screenshot_ui.is_open() {
4044 // Don't start swipe gestures while in the compositor UI.4128 // Don't start swipe gestures while in the compositor UI.
4045 return;4129 return;
@@ -4243,6 +4327,8 @@ impl State {
4243 }4327 }
4244 4328
4245 fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) {4329 fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) {
4330+ self.niri.cancel_interrupted_release_binds();
4331+
4246 let serial = SERIAL_COUNTER.next_serial();4332 let serial = SERIAL_COUNTER.next_serial();
4247 let pointer = self.niri.seat.get_pointer().unwrap();4333 let pointer = self.niri.seat.get_pointer().unwrap();
4248 4334
@@ -4314,6 +4400,8 @@ impl State {
4314 }4400 }
4315 4401
4316 fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) {4402 fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) {
4403+ self.niri.cancel_interrupted_release_binds();
4404+
4317 let serial = SERIAL_COUNTER.next_serial();4405 let serial = SERIAL_COUNTER.next_serial();
4318 let pointer = self.niri.seat.get_pointer().unwrap();4406 let pointer = self.niri.seat.get_pointer().unwrap();
4319 4407
@@ -4385,6 +4473,8 @@ impl State {
4385 };4473 };
4386 let slot = evt.slot();4474 let slot = evt.slot();
4387 4475
4476+ self.niri.cancel_interrupted_release_binds();
4477+
4388 let serial = SERIAL_COUNTER.next_serial();4478 let serial = SERIAL_COUNTER.next_serial();
4389 4479
4390 let under = self.niri.contents_under(pos);4480 let under = self.niri.contents_under(pos);
@@ -4617,12 +4707,59 @@ impl State {
4617 }4707 }
4618 }4708 }
4619 4709
4620-/// Check whether the key should be intercepted and mark intercepted4710+impl Niri {
4621-/// pressed keys as `suppressed`, thus preventing `releases` corresponding4711+ fn cancel_interrupted_release_binds(&mut self) {
4622-/// to them from being delivered.4712+ retain_press_binds(&mut self.pending_release_binds);
4713+ }
4714+}
4715+
4716+fn retain_press_binds<K>(binds: &mut HashMap<K, Bind>) {
4717+ binds.retain(|_, bind| bind.has_press());
4718+}
4719+
4720+fn released_key_intercept_result(
4721+ suppressed_keys: &mut HashSet<Keycode>,
4722+ pending_release_binds: &mut HashMap<Keycode, Bind>,
4723+ key_code: Keycode,
4724+) -> ShouldInterceptResult {
4725+ let bind = pending_release_binds.remove(&key_code);
4726+ let suppressed = suppressed_keys.remove(&key_code);
4727+
4728+ bind_intercept_result(bind, !suppressed)
4729+}
4730+
4731+fn bind_intercept_result(bind: Option<Bind>, forward: bool) -> ShouldInterceptResult {
4732+ match (bind, forward) {
4733+ (Some(bind), true) => ShouldInterceptResult::ForwardAndHandle(bind),
4734+ (Some(bind), false) => ShouldInterceptResult::InterceptAndHandle(bind),
4735+ (None, true) => ShouldInterceptResult::Forward,
4736+ (None, false) => ShouldInterceptResult::InterceptOnly,
4737+ }
4738+}
4739+
4740+fn is_bind_on_cooldown(bind: &Bind, cooldown_timers: &HashMap<Key, RegistrationToken>) -> bool {
4741+ bind.cooldown.is_some() && cooldown_timers.contains_key(&bind.key)
4742+}
4743+
4744+/// Checks whether the key should be intercepted, and tracks the presses of intercepted keys.
4745+///
4746+/// An intercepted key press is recorded in `suppressed_keys`, which prevents its release from
4747+/// being forwarded to clients. If the matching bind has a release action, the bind is also
4748+/// recorded in `pending_release_binds`, so that its release action triggers when the key is
4749+/// released, regardless of the modifiers held at that point.
4750+///
4751+/// A release action only triggers if the bind's modifiers matched when the key was pressed.
4752+///
4753+/// A release-only bind is cancelled by other input before this runs; see
4754+/// `cancel_interrupted_release_binds()`. A bind with a press action always runs its release action.
4755+///
4756+/// A bind whose press action is rate-limited by its cooldown is skipped entirely:
4757+/// No action runs, and no release action is recorded.
4623 #[allow(clippy::too_many_arguments)]4758 #[allow(clippy::too_many_arguments)]
4624 fn should_intercept_key<'a>(4759 fn should_intercept_key<'a>(
4625 suppressed_keys: &mut HashSet<Keycode>,4760 suppressed_keys: &mut HashSet<Keycode>,
4761+ pending_release_binds: &mut HashMap<Keycode, Bind>,
4762+ cooldown_timers: &HashMap<Key, RegistrationToken>,
4626 bindings: impl IntoIterator<Item = &'a Bind>,4763 bindings: impl IntoIterator<Item = &'a Bind>,
4627 mod_key: ModKey,4764 mod_key: ModKey,
4628 key_code: Keycode,4765 key_code: Keycode,
@@ -4633,12 +4770,12 @@ fn should_intercept_key<'a>(
4633 screenshot_ui: &ScreenshotUi,4770 screenshot_ui: &ScreenshotUi,
4634 disable_power_key_handling: bool,4771 disable_power_key_handling: bool,
4635 is_inhibiting_shortcuts: bool,4772 is_inhibiting_shortcuts: bool,
4773+ is_locked: bool,
4636 ) -> ShouldInterceptResult {4774 ) -> ShouldInterceptResult {
4637- // Actions are only triggered on presses, release of the key4775+ // A release never runs a press action, so its result depends only on the bind recorded when
4638- // shouldn't try to intercept anything unless we have marked4776+ // the key was pressed, and on whether that press was suppressed.
4639- // the key to suppress.4777+ if !pressed {
4640- if !pressed && !suppressed_keys.contains(&key_code) {4778+ return released_key_intercept_result(suppressed_keys, pending_release_binds, key_code);
4641- return ShouldInterceptResult::Forward;
4642 }4779 }
4643 4780
4644 let mut final_bind = find_bind(4781 let mut final_bind = find_bind(
@@ -4656,7 +4793,7 @@ fn should_intercept_key<'a>(
4656 let mut use_screenshot_ui_action = true;4793 let mut use_screenshot_ui_action = true;
4657 4794
4658 if let Some(bind) = &final_bind {4795 if let Some(bind) = &final_bind {
4659- if allowed_during_screenshot(&bind.action) {4796+ if bind_allowed_during_screenshot(bind, true) {
4660 use_screenshot_ui_action = false;4797 use_screenshot_ui_action = false;
4661 }4798 }
4662 }4799 }
@@ -4669,7 +4806,7 @@ fn should_intercept_key<'a>(
4669 // Not entirely correct but it doesn't matter in how we currently use it.4806 // Not entirely correct but it doesn't matter in how we currently use it.
4670 modifiers: Modifiers::empty(),4807 modifiers: Modifiers::empty(),
4671 },4808 },
4672- action,4809+ action: BoundAction::Press(action),
4673 repeat: true,4810 repeat: true,
4674 cooldown: None,4811 cooldown: None,
4675 allow_when_locked: false,4812 allow_when_locked: false,
@@ -4683,25 +4820,46 @@ fn should_intercept_key<'a>(
4683 }4820 }
4684 }4821 }
4685 4822
4686- match (final_bind, pressed) {4823+ match final_bind {
4687- (Some(bind), true) => {4824+ Some(bind) => {
4688 if is_inhibiting_shortcuts && bind.allow_inhibiting {4825 if is_inhibiting_shortcuts && bind.allow_inhibiting {
4689 ShouldInterceptResult::Forward4826 ShouldInterceptResult::Forward
4827+ } else if bind.has_press() && is_bind_on_cooldown(&bind, cooldown_timers) {
4828+ // If on cooldown, do not run the press action, record a release action,
4829+ // or forward the press.
4830+ suppressed_keys.insert(key_code);
4831+ ShouldInterceptResult::InterceptOnly
4832+ } else if is_keysym_modifier(modified) {
4833+ if should_record_release_bind(&bind, is_locked, cooldown_timers) {
4834+ pending_release_binds.insert(key_code, bind.clone());
4835+ }
4836+ if bind.has_press() {
4837+ suppressed_keys.insert(key_code);
4838+ // A bind with a press action takes the key press. Applications will still learn
4839+ // about the modifier state via `advertise_modifier_state()`.
4840+ ShouldInterceptResult::InterceptAndHandle(bind)
4841+ } else {
4842+ // This is to ensure that, if you have a modifier-triggered release bind like
4843+ // Ctrl+Alt, We still forward the Ctrl and Alt presses to the application
4844+ // to use for its own shortcuts.
4845+ ShouldInterceptResult::Forward
4846+ }
4690 } else {4847 } else {
4848+ if should_record_release_bind(&bind, is_locked, cooldown_timers) {
4849+ pending_release_binds.insert(key_code, bind.clone());
4850+ }
4691 suppressed_keys.insert(key_code);4851 suppressed_keys.insert(key_code);
4692- ShouldInterceptResult::InterceptAndHandle(bind)4852+ if bind.has_press() {
4853+ ShouldInterceptResult::InterceptAndHandle(bind)
4854+ } else {
4855+ // Release-only bind: intercept the key press so that it doesn't reach the
4856+ // client, and remember to trigger the release action when the key is
4857+ // released.
4858+ ShouldInterceptResult::InterceptOnly
4859+ }
4693 }4860 }
4694 }4861 }
4695- (_, false) => {4862+ None => ShouldInterceptResult::Forward,
4696- // By this point, we know that the key was suppressed on press. Even if we're inhibiting
4697- // shortcuts, we should still suppress the release.
4698- // But we don't need to check for shortcuts inhibition here, because
4699- // if it was inhibited on press (forwarded to the client), it wouldn't be suppressed,
4700- // so the release would already have been forwarded at the start of this function.
4701- suppressed_keys.remove(&key_code);
4702- ShouldInterceptResult::InterceptOnly
4703- }
4704- (None, true) => ShouldInterceptResult::Forward,
4705 }4863 }
4706 }4864 }
4707 4865
@@ -4733,7 +4891,7 @@ fn find_bind<'a>(
4733 trigger: Trigger::Keysym(modified),4891 trigger: Trigger::Keysym(modified),
4734 modifiers: Modifiers::empty(),4892 modifiers: Modifiers::empty(),
4735 },4893 },
4736- action,4894+ action: BoundAction::Press(action),
4737 // Repeating VT switches and power off doesn't make sense (and it actually happens on4895 // Repeating VT switches and power off doesn't make sense (and it actually happens on
4738 // FreeBSD where it doesn't get released and ends up broken).4896 // FreeBSD where it doesn't get released and ends up broken).
4739 repeat: false,4897 repeat: false,
@@ -4749,42 +4907,89 @@ fn find_bind<'a>(
4749 });4907 });
4750 }4908 }
4751 4909
4752- let trigger = Trigger::Keysym(raw?);4910+ let raw = raw?;
4753- find_configured_bind(bindings, mod_key, trigger, mods)4911+
4912+ let modifiers = modifiers_from_state(mods);
4913+
4914+ // A modifier key can trigger binds bound as the compositor mod key (`Mod`), by its own keysym
4915+ // (like `Alt_L`), or as the modifier itself (like `Alt`). Return the first bind that matches.
4916+ bindings
4917+ .into_iter()
4918+ .find(|bind| {
4919+ // Modifier-only binds can be written in several ways:
4920+ // - `Mod { quit; }` - Trigger::CompositorMod
4921+ // - `Super { quit; }` - Trigger::Modifier(_)
4922+ // - `Super_L { quit; }` - Trigger::Keysym(_)
4923+ //
4924+ // These only matter for keyboard keys and not for mouse or others. So, check them here,
4925+ // and then pass the bind's trigger itself to the simpler matching function.
4926+ let bind_trigger = bind.key.trigger;
4927+ trigger_matches_keysym(bind_trigger, raw, mod_key)
4928+ && bind_matches_trigger(bind, bind_trigger, mod_key, modifiers)
4929+ })
4930+ .cloned()
4931+}
4932+
4933+fn trigger_matches_keysym(trigger: Trigger, raw: Keysym, mod_key: ModKey) -> bool {
4934+ match trigger {
4935+ Trigger::CompositorMod => mod_key.matches_keysym(raw),
4936+ Trigger::Keysym(keysym) => keysym == raw,
4937+ Trigger::Modifier(modifier) => modifier.matches_keysym(raw),
4938+ _ => false,
4939+ }
4754 }4940 }
4755 4941
4942+/// Finds the configured bind for a trigger and held modifiers.
4943+///
4944+/// Returns the first bind with this exact trigger whose modifiers match, so overlapping binds
4945+/// like `Mod+Q` and `Super+Q` are resolved by definition order.
4756 fn find_configured_bind<'a>(4946 fn find_configured_bind<'a>(
4757 bindings: impl IntoIterator<Item = &'a Bind>,4947 bindings: impl IntoIterator<Item = &'a Bind>,
4758 mod_key: ModKey,4948 mod_key: ModKey,
4759 trigger: Trigger,4949 trigger: Trigger,
4760 mods: ModifiersState,4950 mods: ModifiersState,
4761 ) -> Option<Bind> {4951 ) -> Option<Bind> {
4762- // Handle configured binds.4952+ let modifiers = modifiers_from_state(mods);
4763- let mut modifiers = modifiers_from_state(mods);4953+
4954+ bindings
4955+ .into_iter()
4956+ .find(|bind| bind_matches_trigger(bind, trigger, mod_key, modifiers))
4957+ .cloned()
4958+}
4764 4959
4765- let mod_down = modifiers_from_state(mods).contains(mod_key.to_modifiers());4960+fn bind_matches_trigger(bind: &Bind, trigger: Trigger, mod_key: ModKey, mods: Modifiers) -> bool {
4766- if mod_down {4961+ if bind.key.trigger != trigger {
4767- modifiers |= Modifiers::COMPOSITOR;4962+ return false;
4768 }4963 }
4769 4964
4770- for bind in bindings {4965+ let mut held_modifiers = mods;
4771- if bind.key.trigger != trigger {4966+ let mut required_modifiers = bind.key.modifiers;
4772- continue;
4773- }
4774 4967
4775- let mut bind_modifiers = bind.key.modifiers;4968+ // Check if the trigger is the mod key itself, either as `Mod`, as its own keysym (like
4776- if bind_modifiers.contains(Modifiers::COMPOSITOR) {4969+ // `Super_L`), or as the modifier itself (like `Super`). If so, normalize the modifiers not
4777- bind_modifiers |= mod_key.to_modifiers();4970+ // to need it.
4778- } else if bind_modifiers.contains(mod_key.to_modifiers()) {4971+ let trigger_is_mod_key = match trigger {
4779- bind_modifiers |= Modifiers::COMPOSITOR;4972+ Trigger::CompositorMod => true,
4780- }4973+ Trigger::Modifier(modifier) => modifier == mod_key,
4974+ Trigger::Keysym(keysym) => mod_key.matches_keysym(keysym),
4975+ _ => false,
4976+ };
4781 4977
4782- if bind_modifiers == modifiers {4978+ if trigger_is_mod_key {
4783- return Some(bind.clone());4979+ held_modifiers.remove(mod_key.to_modifiers());
4980+ required_modifiers.remove(Modifiers::COMPOSITOR | mod_key.to_modifiers());
4981+ } else {
4982+ if held_modifiers.contains(mod_key.to_modifiers()) {
4983+ held_modifiers |= Modifiers::COMPOSITOR;
4984+ }
4985+ if required_modifiers.contains(Modifiers::COMPOSITOR) {
4986+ required_modifiers |= mod_key.to_modifiers();
4987+ } else if required_modifiers.contains(mod_key.to_modifiers()) {
4988+ required_modifiers |= Modifiers::COMPOSITOR;
4784 }4989 }
4785 }4990 }
4786 4991
4787- None4992+ required_modifiers == held_modifiers
4788 }4993 }
4789 4994
4790 fn find_configured_switch_action(4995 fn find_configured_switch_action(
@@ -4912,6 +5117,36 @@ fn allowed_when_locked(action: &Action) -> bool {
4912 )5117 )
4913 }5118 }
4914 5119
5120+fn bind_allowed_when_locked(bind: &Bind, pressed: bool) -> bool {
5121+ if bind.allow_when_locked {
5122+ return true;
5123+ }
5124+
5125+ match &bind.action {
5126+ BoundAction::Press(action) | BoundAction::Release(action) => allowed_when_locked(action),
5127+ BoundAction::Both { press, release } => {
5128+ if pressed {
5129+ allowed_when_locked(press) && allowed_when_locked(release)
5130+ } else {
5131+ // The press ran, so the release must run too.
5132+ true
5133+ }
5134+ }
5135+ }
5136+}
5137+
5138+fn should_record_release_bind(
5139+ bind: &Bind,
5140+ is_locked: bool,
5141+ cooldown_timers: &HashMap<Key, RegistrationToken>,
5142+) -> bool {
5143+ if bind.has_press() && is_bind_on_cooldown(bind, cooldown_timers) {
5144+ return false;
5145+ }
5146+
5147+ bind.has_release() && (!is_locked || bind_allowed_when_locked(bind, true))
5148+}
5149+
4915 fn allowed_during_screenshot(action: &Action) -> bool {5150 fn allowed_during_screenshot(action: &Action) -> bool {
4916 matches!(5151 matches!(
4917 action,5152 action,
@@ -4953,6 +5188,22 @@ fn allowed_during_screenshot(action: &Action) -> bool {
4953 )5188 )
4954 }5189 }
4955 5190
5191+fn bind_allowed_during_screenshot(bind: &Bind, pressed: bool) -> bool {
5192+ match &bind.action {
5193+ BoundAction::Press(action) | BoundAction::Release(action) => {
5194+ allowed_during_screenshot(action)
5195+ }
5196+ BoundAction::Both { press, release } => {
5197+ if pressed {
5198+ allowed_during_screenshot(press) && allowed_during_screenshot(release)
5199+ } else {
5200+ // The press ran, so the release must run too.
5201+ true
5202+ }
5203+ }
5204+ }
5205+}
5206+
4956 fn hardcoded_overview_bind(raw: Keysym, mods: ModifiersState) -> Option<Bind> {5207 fn hardcoded_overview_bind(raw: Keysym, mods: ModifiersState) -> Option<Bind> {
4957 let mods = modifiers_from_state(mods);5208 let mods = modifiers_from_state(mods);
4958 if !mods.is_empty() {5209 if !mods.is_empty() {
@@ -4979,7 +5230,7 @@ fn hardcoded_overview_bind(raw: Keysym, mods: ModifiersState) -> Option<Bind> {
4979 trigger: Trigger::Keysym(raw),5230 trigger: Trigger::Keysym(raw),
4980 modifiers: Modifiers::empty(),5231 modifiers: Modifiers::empty(),
4981 },5232 },
4982- action,5233+ action: BoundAction::Press(action),
4983 repeat,5234 repeat,
4984 cooldown: None,5235 cooldown: None,
4985 allow_when_locked: false,5236 allow_when_locked: false,
@@ -5417,7 +5668,7 @@ mod tests {
5417 trigger: Trigger::Keysym(Keysym::q),5668 trigger: Trigger::Keysym(Keysym::q),
5418 modifiers: Modifiers::COMPOSITOR,5669 modifiers: Modifiers::COMPOSITOR,
5419 },5670 },
5420- action: Action::CloseWindow,5671+ action: BoundAction::Press(Action::CloseWindow),
5421 repeat: true,5672 repeat: true,
5422 cooldown: None,5673 cooldown: None,
5423 allow_when_locked: false,5674 allow_when_locked: false,
@@ -5429,7 +5680,7 @@ mod tests {
5429 trigger: Trigger::Keysym(Keysym::h),5680 trigger: Trigger::Keysym(Keysym::h),
5430 modifiers: Modifiers::SUPER,5681 modifiers: Modifiers::SUPER,
5431 },5682 },
5432- action: Action::FocusColumnLeft,5683+ action: BoundAction::Press(Action::FocusColumnLeft),
5433 repeat: true,5684 repeat: true,
5434 cooldown: None,5685 cooldown: None,
5435 allow_when_locked: false,5686 allow_when_locked: false,
@@ -5441,7 +5692,7 @@ mod tests {
5441 trigger: Trigger::Keysym(Keysym::j),5692 trigger: Trigger::Keysym(Keysym::j),
5442 modifiers: Modifiers::empty(),5693 modifiers: Modifiers::empty(),
5443 },5694 },
5444- action: Action::FocusWindowDown,5695+ action: BoundAction::Press(Action::FocusWindowDown),
5445 repeat: true,5696 repeat: true,
5446 cooldown: None,5697 cooldown: None,
5447 allow_when_locked: false,5698 allow_when_locked: false,
@@ -5453,7 +5704,7 @@ mod tests {
5453 trigger: Trigger::Keysym(Keysym::k),5704 trigger: Trigger::Keysym(Keysym::k),
5454 modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER,5705 modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER,
5455 },5706 },
5456- action: Action::FocusWindowUp,5707+ action: BoundAction::Press(Action::FocusWindowUp),
5457 repeat: true,5708 repeat: true,
5458 cooldown: None,5709 cooldown: None,
5459 allow_when_locked: false,5710 allow_when_locked: false,
@@ -5465,13 +5716,25 @@ mod tests {
5465 trigger: Trigger::Keysym(Keysym::l),5716 trigger: Trigger::Keysym(Keysym::l),
5466 modifiers: Modifiers::SUPER | Modifiers::ALT,5717 modifiers: Modifiers::SUPER | Modifiers::ALT,
5467 },5718 },
5468- action: Action::FocusColumnRight,5719+ action: BoundAction::Press(Action::FocusColumnRight),
5469 repeat: true,5720 repeat: true,
5470 cooldown: None,5721 cooldown: None,
5471 allow_when_locked: false,5722 allow_when_locked: false,
5472 allow_inhibiting: true,5723 allow_inhibiting: true,
5473 hotkey_overlay_title: None,5724 hotkey_overlay_title: None,
5474 },5725 },
5726+ Bind {
5727+ key: Key {
5728+ trigger: Trigger::Keysym(Keysym::Super_L),
5729+ modifiers: Modifiers::empty(),
5730+ },
5731+ action: BoundAction::Release(Action::ToggleOverview),
5732+ repeat: false,
5733+ cooldown: None,
5734+ allow_when_locked: false,
5735+ allow_inhibiting: true,
5736+ hotkey_overlay_title: None,
5737+ },
5475 ]);5738 ]);
5476 5739
5477 assert_eq!(5740 assert_eq!(
@@ -5482,7 +5745,7 @@ mod tests {
5482 ModifiersState {5745 ModifiersState {
5483 logo: true,5746 logo: true,
5484 ..Default::default()5747 ..Default::default()
5485- }5748+ },
5486 )5749 )
5487 .as_ref(),5750 .as_ref(),
5488 Some(&bindings.0[0])5751 Some(&bindings.0[0])
@@ -5505,7 +5768,7 @@ mod tests {
5505 ModifiersState {5768 ModifiersState {
5506 logo: true,5769 logo: true,
5507 ..Default::default()5770 ..Default::default()
5508- }5771+ },
5509 )5772 )
5510 .as_ref(),5773 .as_ref(),
5511 Some(&bindings.0[1])5774 Some(&bindings.0[1])
@@ -5528,7 +5791,7 @@ mod tests {
5528 ModifiersState {5791 ModifiersState {
5529 logo: true,5792 logo: true,
5530 ..Default::default()5793 ..Default::default()
5531- }5794+ },
5532 ),5795 ),
5533 None,5796 None,
5534 );5797 );
@@ -5551,7 +5814,7 @@ mod tests {
5551 ModifiersState {5814 ModifiersState {
5552 logo: true,5815 logo: true,
5553 ..Default::default()5816 ..Default::default()
5554- }5817+ },
5555 )5818 )
5556 .as_ref(),5819 .as_ref(),
5557 Some(&bindings.0[3])5820 Some(&bindings.0[3])
@@ -5575,7 +5838,7 @@ mod tests {
5575 logo: true,5838 logo: true,
5576 alt: true,5839 alt: true,
5577 ..Default::default()5840 ..Default::default()
5578- }5841+ },
5579 )5842 )
5580 .as_ref(),5843 .as_ref(),
5581 Some(&bindings.0[4])5844 Some(&bindings.0[4])
@@ -5592,5 +5855,18 @@ mod tests {
5592 ),5855 ),
5593 None,5856 None,
5594 );5857 );
5858+
5859+ // A release-only bind is found the same as any other: what to do with it depends on the
5860+ // event, not on the lookup.
5861+ assert_eq!(
5862+ find_configured_bind(
5863+ &bindings.0,
5864+ ModKey::Super,
5865+ Trigger::Keysym(Keysym::Super_L),
5866+ ModifiersState::default(),
5867+ )
5868+ .as_ref(),
5869+ Some(&bindings.0[5])
5870+ );
5595 }5871 }
5596 }5872 }
src/niri.rsmodified+15 -1
@@ -17,7 +17,7 @@ use niri_config::debug::PreviewRender;
17 use niri_config::output::MaxBpc;17 use niri_config::output::MaxBpc;
18 use niri_config::window_rule::VrrRule;18 use niri_config::window_rule::VrrRule;
19 use niri_config::{19 use niri_config::{
20- Config, FloatOrInt, Key, Modifiers, OutputName, TrackLayout, WarpMouseToFocusMode,20+ Bind, Config, FloatOrInt, Key, Modifiers, OutputName, TrackLayout, WarpMouseToFocusMode,
21 WorkspaceReference, Xkb,21 WorkspaceReference, Xkb,
22 };22 };
23 use smithay::backend::allocator::Fourcc;23 use smithay::backend::allocator::Fourcc;
@@ -359,6 +359,8 @@ pub struct Niri {
359 pub suppressed_keys: HashSet<Keycode>,359 pub suppressed_keys: HashSet<Keycode>,
360 /// Button codes of the mouse buttons to suppress.360 /// Button codes of the mouse buttons to suppress.
361 pub suppressed_buttons: HashSet<u32>,361 pub suppressed_buttons: HashSet<u32>,
362+ /// Binds whose release action is pending, keyed by the key code.
363+ pub pending_release_binds: HashMap<Keycode, Bind>,
362 pub bind_cooldown_timers: HashMap<Key, RegistrationToken>,364 pub bind_cooldown_timers: HashMap<Key, RegistrationToken>,
363 pub bind_repeat_timer: Option<RegistrationToken>,365 pub bind_repeat_timer: Option<RegistrationToken>,
364 pub keyboard_focus: KeyboardFocus,366 pub keyboard_focus: KeyboardFocus,
@@ -2837,6 +2839,7 @@ impl Niri {
2837 popup_grab: None,2839 popup_grab: None,
2838 suppressed_keys: HashSet::new(),2840 suppressed_keys: HashSet::new(),
2839 suppressed_buttons: HashSet::new(),2841 suppressed_buttons: HashSet::new(),
2842+ pending_release_binds: HashMap::new(),
2840 bind_cooldown_timers: HashMap::new(),2843 bind_cooldown_timers: HashMap::new(),
2841 bind_repeat_timer: Option::default(),2844 bind_repeat_timer: Option::default(),
2842 presentation_state,2845 presentation_state,
@@ -6590,6 +6593,17 @@ impl Niri {
6590 Ok(())6593 Ok(())
6591 }6594 }
6592 6595
6596+ pub fn clear_input_state(&mut self) {
6597+ self.suppressed_keys.clear();
6598+ self.suppressed_buttons.clear();
6599+ self.pending_release_binds.clear();
6600+
6601+ // Stop ongoing key repeat if any.
6602+ if let Some(token) = self.bind_repeat_timer.take() {
6603+ self.event_loop.remove(token);
6604+ }
6605+ }
6606+
6593 pub fn is_locked(&self) -> bool {6607 pub fn is_locked(&self) -> bool {
6594 match self.lock_state {6608 match self.lock_state {
6595 LockState::Unlocked | LockState::WaitingForSurfaces { .. } => false,6609 LockState::Unlocked | LockState::WaitingForSurfaces { .. } => false,
src/tests/binds.rsmodified+9 -2
@@ -1,7 +1,7 @@
1 use std::fmt::Write as _;1 use std::fmt::Write as _;
2 2
3 use insta::assert_snapshot;3 use insta::assert_snapshot;
4-use niri_config::{Action, 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_surface::WlSurface;7 use wayland_client::protocol::wl_surface::WlSurface;
@@ -39,7 +39,14 @@ fn set_up(config: &str) -> (Fixture, ClientId, WlSurface) {
39 let mut config = Config::parse_mem(config).unwrap();39 let mut config = Config::parse_mem(config).unwrap();
40 // knuffel doesn't understand #[cfg(test)]...40 // knuffel doesn't understand #[cfg(test)]...
41 for bind in &mut config.binds.0 {41 for bind in &mut config.binds.0 {
42- bind.action = Action::TestAction;42+ bind.action = match &bind.action {
43+ BoundAction::Press(_) => BoundAction::Press(Action::TestAction),
44+ BoundAction::Release(_) => BoundAction::Release(Action::TestAction),
45+ BoundAction::Both { .. } => BoundAction::Both {
46+ press: Action::TestAction,
47+ release: Action::TestAction,
48+ },
49+ };
43 }50 }
44 51
45 let mut f = Fixture::with_config(config);52 let mut f = Fixture::with_config(config);
src/ui/hotkey_overlay.rsmodified+193 -27
@@ -152,13 +152,23 @@ impl HotkeyOverlay {
152 }152 }
153 }153 }
154 154
155+/// Returns the actions that a bind triggers.
156+fn bind_actions(bind: &Bind) -> impl Iterator<Item = &Action> {
157+ bind.press_action().into_iter().chain(bind.release_action())
158+}
159+
160+/// Whether the bind triggers the action, on press or on release.
161+fn bind_triggers(bind: &Bind, action: &Action) -> bool {
162+ bind.press_action() == Some(action) || bind.release_action() == Some(action)
163+}
164+
155 fn format_bind(binds: &[Bind], action: &Action) -> Option<(Option<Key>, String)> {165 fn format_bind(binds: &[Bind], action: &Action) -> Option<(Option<Key>, String)> {
156 let mut bind_with_non_null = None;166 let mut bind_with_non_null = None;
157 let mut bind_with_custom_title = None;167 let mut bind_with_custom_title = None;
158 let mut found_null_title = false;168 let mut found_null_title = false;
159 169
160 for bind in binds {170 for bind in binds {
161- if bind.action != *action {171+ if !bind_triggers(bind, action) {
162 continue;172 continue;
163 }173 }
164 174
@@ -202,9 +212,15 @@ fn collect_actions(config: &Config) -> Vec<&Action> {
202 212
203 // Prefer Quit(false) if found, otherwise try Quit(true), and if there's neither, fall back to213 // Prefer Quit(false) if found, otherwise try Quit(true), and if there's neither, fall back to
204 // Quit(false).214 // Quit(false).
205- if binds.iter().any(|bind| bind.action == Action::Quit(false)) {215+ if binds
216+ .iter()
217+ .any(|bind| bind_triggers(bind, &Action::Quit(false)))
218+ {
206 actions.push(&Action::Quit(false));219 actions.push(&Action::Quit(false));
207- } else if binds.iter().any(|bind| bind.action == Action::Quit(true)) {220+ } else if binds
221+ .iter()
222+ .any(|bind| bind_triggers(bind, &Action::Quit(true)))
223+ {
208 actions.push(&Action::Quit(true));224 actions.push(&Action::Quit(true));
209 } else {225 } else {
210 actions.push(&Action::Quit(false));226 actions.push(&Action::Quit(false));
@@ -221,14 +237,16 @@ fn collect_actions(config: &Config) -> Vec<&Action> {
221 ]);237 ]);
222 238
223 // Prefer move-column-to-workspace-down, but fall back to move-window-to-workspace-down.239 // Prefer move-column-to-workspace-down, but fall back to move-window-to-workspace-down.
224- if let Some(bind) = binds240+ if let Some(action) = binds
225 .iter()241 .iter()
226- .find(|bind| matches!(bind.action, Action::MoveColumnToWorkspaceDown(_)))242+ .flat_map(bind_actions)
243+ .find(|action| matches!(action, Action::MoveColumnToWorkspaceDown(_)))
227 {244 {
228- actions.push(&bind.action);245+ actions.push(action);
229 } else if binds246 } else if binds
230 .iter()247 .iter()
231- .any(|bind| matches!(bind.action, Action::MoveWindowToWorkspaceDown(_)))248+ .flat_map(bind_actions)
249+ .any(|action| matches!(action, Action::MoveWindowToWorkspaceDown(_)))
232 {250 {
233 actions.push(&Action::MoveWindowToWorkspaceDown(true));251 actions.push(&Action::MoveWindowToWorkspaceDown(true));
234 } else {252 } else {
@@ -236,14 +254,16 @@ fn collect_actions(config: &Config) -> Vec<&Action> {
236 }254 }
237 255
238 // Same for -up.256 // Same for -up.
239- if let Some(bind) = binds257+ if let Some(action) = binds
240 .iter()258 .iter()
241- .find(|bind| matches!(bind.action, Action::MoveColumnToWorkspaceUp(_)))259+ .flat_map(bind_actions)
260+ .find(|action| matches!(action, Action::MoveColumnToWorkspaceUp(_)))
242 {261 {
243- actions.push(&bind.action);262+ actions.push(action);
244 } else if binds263 } else if binds
245 .iter()264 .iter()
246- .any(|bind| matches!(bind.action, Action::MoveWindowToWorkspaceUp(_)))265+ .flat_map(bind_actions)
266+ .any(|action| matches!(action, Action::MoveWindowToWorkspaceUp(_)))
247 {267 {
248 actions.push(&Action::MoveWindowToWorkspaceUp(true));268 actions.push(&Action::MoveWindowToWorkspaceUp(true));
249 } else {269 } else {
@@ -261,34 +281,42 @@ fn collect_actions(config: &Config) -> Vec<&Action> {
261 ]);281 ]);
262 282
263 // Screenshot is not as important, can omit if not bound.283 // Screenshot is not as important, can omit if not bound.
264- if let Some(bind) = binds284+ if let Some(action) = binds
265 .iter()285 .iter()
266- .find(|bind| matches!(bind.action, Action::Screenshot(_, _)))286+ .flat_map(bind_actions)
287+ .find(|action| matches!(action, Action::Screenshot(_, _)))
267 {288 {
268- actions.push(&bind.action);289+ actions.push(action);
269 }290 }
270 291
271 // Add actions with a custom hotkey-overlay-title.292 // Add actions with a custom hotkey-overlay-title.
272 for bind in binds {293 for bind in binds {
273 if matches!(bind.hotkey_overlay_title, Some(Some(_))) {294 if matches!(bind.hotkey_overlay_title, Some(Some(_))) {
274 // Avoid duplicate actions.295 // Avoid duplicate actions.
275- if !actions.contains(&&bind.action) {296+ if let Some(action) = bind_actions(bind).next() {
276- actions.push(&bind.action);297+ if !actions.contains(&action) {
298+ actions.push(action);
299+ }
277 }300 }
278 }301 }
279 }302 }
280 303
281 // Add the spawn actions.304 // Add the spawn actions.
282- for bind in binds.iter().filter(|bind| {305+ for action in binds.iter().filter_map(|bind| {
283- matches!(bind.action, Action::Spawn(_) | Action::SpawnSh(_))306+ // Includes the trigger's implied modifier.
284- // Only show binds with Mod or Super to filter out stuff like volume up/down.307+ let modifiers = bind.key.modifiers;
285- && (bind.key.modifiers.contains(Modifiers::COMPOSITOR)308+
286- || bind.key.modifiers.contains(Modifiers::SUPER))309+ // Only show binds with Mod or Super to filter out stuff like volume up/down.
287- // Also filter out wheel and touchpad scroll binds.310+ // Also filter out wheel and touchpad scroll binds.
288- && matches!(bind.key.trigger, Trigger::Keysym(_))311+ if bind.key.trigger.is_keyboard()
312+ && modifiers.intersects(Modifiers::COMPOSITOR | Modifiers::SUPER)
313+ {
314+ bind_actions(bind)
315+ .find(|action| matches!(action, Action::Spawn(_) | Action::SpawnSh(_)))
316+ } else {
317+ None
318+ }
289 }) {319 }) {
290- let action = &bind.action;
291-
292 // We only show one bind for each action, so we need to deduplicate the Spawn actions.320 // We only show one bind for each action, so we need to deduplicate the Spawn actions.
293 if !actions.contains(&action) {321 if !actions.contains(&action) {
294 actions.push(action);322 actions.push(action);
@@ -297,7 +325,7 @@ fn collect_actions(config: &Config) -> Vec<&Action> {
297 325
298 if config.hotkey_overlay.hide_not_bound {326 if config.hotkey_overlay.hide_not_bound {
299 // Only keep actions that have been bound327 // Only keep actions that have been bound
300- actions.retain(|&action| binds.iter().any(|bind| bind.action == *action))328+ actions.retain(|&action| binds.iter().any(|bind| bind_triggers(bind, action)))
301 }329 }
302 330
303 actions331 actions
@@ -497,7 +525,13 @@ fn key_name(screen_reader: bool, mod_key: ModKey, key: &Key) -> String {
497 let mut name = String::new();525 let mut name = String::new();
498 526
499 // Do not show modifiers implied by the trigger.527 // Do not show modifiers implied by the trigger.
500- let modifiers = key.modifiers & !key.trigger.implied_modifier();528+ let mut implied = key.trigger.implied_modifier();
529+ // Spellings of the mod key also imply the compositor modifier (e.g. Mod+Super_L).
530+ let mod_modifiers = Modifiers::COMPOSITOR | mod_key.to_modifiers();
531+ if implied.intersects(mod_modifiers) {
532+ implied |= mod_modifiers;
533+ }
534+ let modifiers = key.modifiers & !implied;
501 535
502 let has_comp_mod = modifiers.contains(Modifiers::COMPOSITOR);536 let has_comp_mod = modifiers.contains(Modifiers::COMPOSITOR);
503 537
@@ -713,6 +747,33 @@ mod tests {
713 @" Super + Control_R : Close Focused Window"747 @" Super + Control_R : Close Focused Window"
714 );748 );
715 749
750+ // A release bind is shown too.
751+ assert_snapshot!(
752+ check(
753+ r#"binds {
754+ Mod {
755+ release { toggle-overview; }
756+ }
757+ }"#,
758+ Action::ToggleOverview,
759+ ),
760+ @" Super : Open the Overview"
761+ );
762+
763+ // Same for the release action of a bind with both actions.
764+ assert_snapshot!(
765+ check(
766+ r#"binds {
767+ Mod+Shift+Q {
768+ press { close-window; }
769+ release { toggle-overview; }
770+ }
771+ }"#,
772+ Action::ToggleOverview,
773+ ),
774+ @" Super + Shift + Q : Open the Overview"
775+ );
776+
716 // Bound with a default title.777 // Bound with a default title.
717 assert_snapshot!(778 assert_snapshot!(
718 check(779 check(
@@ -794,5 +855,110 @@ mod tests {
794 ),855 ),
795 @" Super + P : Hello"856 @" Super + P : Hello"
796 );857 );
858+
859+ // Mod overlapping with trigger.
860+ assert_snapshot!(
861+ check(
862+ r#"binds {
863+ Mod+Super_L { screenshot; }
864+ }"#,
865+ Action::Screenshot(true, None),
866+ ),
867+ @" Super_L : Take a Screenshot"
868+ );
869+ assert_snapshot!(
870+ check(
871+ r#"binds {
872+ Mod+Super { screenshot; }
873+ }"#,
874+ Action::Screenshot(true, None),
875+ ),
876+ @" Super : Take a Screenshot"
877+ );
878+ assert_snapshot!(
879+ check(
880+ r#"binds {
881+ Super+Mod { screenshot; }
882+ }"#,
883+ Action::Screenshot(true, None),
884+ ),
885+ @" Super : Take a Screenshot"
886+ );
887+ }
888+
889+ #[test]
890+ fn test_collect_actions() {
891+ // Release binds count as bound, both for the actions that are always listed and for the
892+ // ones that are only listed when bound.
893+ let config = Config::parse_mem(
894+ r#"binds {
895+ Alt {
896+ release { screenshot; }
897+ }
898+ Mod {
899+ release { spawn "foot"; }
900+ }
901+ }"#,
902+ )
903+ .unwrap();
904+
905+ let actions = collect_actions(&config);
906+ assert!(actions.contains(&&Action::Screenshot(true, None)));
907+ assert!(actions.contains(&&Action::Spawn(vec![String::from("foot")])));
908+
909+ // Release binds keep actions in the list with hide-not-bound.
910+ let config = Config::parse_mem(
911+ r#"binds {
912+ Mod {
913+ release { toggle-overview; }
914+ }
915+ }
916+ hotkey-overlay { hide-not-bound; }"#,
917+ )
918+ .unwrap();
919+
920+ assert!(collect_actions(&config).contains(&&Action::ToggleOverview));
921+
922+ // A release bind with a custom title adds its action to the list.
923+ let config = Config::parse_mem(
924+ r#"binds {
925+ Mod+Q hotkey-overlay-title="Custom" {
926+ release { center-column; }
927+ }
928+ }"#,
929+ )
930+ .unwrap();
931+
932+ assert!(collect_actions(&config).contains(&&Action::CenterColumn));
933+
934+ // Various filtering behavior.
935+ let config = Config::parse_mem(
936+ r#"binds {
937+ XF86AudioMute { spawn "Mute"; }
938+ Super_L { spawn "Super_L"; }
939+ Mod { spawn "Mod"; }
940+ Shift_L { spawn "Shift_L"; }
941+ Alt { spawn "Alt"; }
942+ Mod+Shift_L { spawn "Mod+Shift_L"; }
943+ Super+Ctrl { spawn "Super+Ctrl"; }
944+ Mod+Alt { spawn "Mod+Alt"; }
945+ }"#,
946+ )
947+ .unwrap();
948+
949+ let mut snapshot = String::new();
950+ for action in collect_actions(&config) {
951+ if let Action::Spawn(args) = action {
952+ snapshot.push_str(&args[0]);
953+ snapshot.push('\n');
954+ }
955+ }
956+ assert_snapshot!(snapshot, @"
957+ Super_L
958+ Mod
959+ Mod+Shift_L
960+ Super+Ctrl
961+ Mod+Alt
962+ ");
797 }963 }
798 }964 }
src/ui/mru.rsmodified+9 -5
@@ -7,8 +7,8 @@ use std::time::Duration;
7 7
8 use anyhow::ensure;8 use anyhow::ensure;
9 use niri_config::{9 use niri_config::{
10- Action, Bind, Color, Config, CornerRadius, GradientInterpolation, Key, Modifiers, MruDirection,10+ Action, Bind, BoundAction, Color, Config, CornerRadius, GradientInterpolation, Key, Modifiers,
11- MruFilter, MruScope, Trigger,11+ MruDirection, MruFilter, MruScope, Trigger,
12 };12 };
13 use pango::FontDescription;13 use pango::FontDescription;
14 use pangocairo::cairo::{self, ImageSurface};14 use pangocairo::cairo::{self, ImageSurface};
@@ -1834,7 +1834,7 @@ fn make_preset_opened_binds() -> Vec<Bind> {
1834 // The modifier is filled dynamically.1834 // The modifier is filled dynamically.
1835 modifiers: Modifiers::empty(),1835 modifiers: Modifiers::empty(),
1836 },1836 },
1837- action,1837+ action: BoundAction::Press(action),
1838 repeat: true,1838 repeat: true,
1839 cooldown: None,1839 cooldown: None,
1840 allow_when_locked: false,1840 allow_when_locked: false,
@@ -1882,7 +1882,11 @@ fn make_dynamic_opened_binds(config: &Config) -> Vec<Bind> {
1882 let mut binds: HashMap<Trigger, Vec<Bind>> = HashMap::new();1882 let mut binds: HashMap<Trigger, Vec<Bind>> = HashMap::new();
1883 1883
1884 for bind in &config.binds.0 {1884 for bind in &config.binds.0 {
1885- let action = match &bind.action {1885+ let Some(press_action) = bind.press_action() else {
1886+ continue;
1887+ };
1888+
1889+ let action = match press_action {
1886 Action::FocusColumnRight1890 Action::FocusColumnRight
1887 | Action::FocusColumnRightOrFirst1891 | Action::FocusColumnRightOrFirst
1888 | Action::FocusColumnOrMonitorRight1892 | Action::FocusColumnOrMonitorRight
@@ -1907,7 +1911,7 @@ fn make_dynamic_opened_binds(config: &Config) -> Vec<Bind> {
1907 };1911 };
1908 1912
1909 binds.entry(bind.key.trigger).or_default().push(Bind {1913 binds.entry(bind.key.trigger).or_default().push(Bind {
1910- action,1914+ action: BoundAction::Press(action),
1911 ..bind.clone()1915 ..bind.clone()
1912 });1916 });
1913 }1917 }