niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::str::FromStr; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use bitflags::bitflags; |
| 5 | use knuffel::errors::DecodeError; |
| 6 | use miette::miette; |
| 7 | use niri_ipc::{ |
| 8 | ColumnDisplay, LayoutSwitchTarget, PositionChange, SizeChange, WorkspaceReferenceArg, |
| 9 | }; |
| 10 | use smithay::input::keyboard::keysyms::KEY_NoSymbol; |
| 11 | use smithay::input::keyboard::xkb::{keysym_from_name, KEYSYM_CASE_INSENSITIVE, KEYSYM_NO_FLAGS}; |
| 12 | use smithay::input::keyboard::Keysym; |
| 13 | use tracing::warn; |
| 14 | |
| 15 | use crate::input::ModKey; |
| 16 | use crate::recent_windows::{MruDirection, MruFilter, MruScope}; |
| 17 | use crate::utils::{expect_only_children, MergeWith}; |
| 18 | |
| 19 | #[derive(Debug, Default, PartialEq)] |
| 20 | pub struct Binds(pub Vec<Bind>); |
| 21 | |
| 22 | #[derive(Debug, Clone, PartialEq)] |
| 23 | pub struct Bind { |
| 24 | pub key: Key, |
| 25 | pub action: BoundAction, |
| 26 | pub repeat: bool, |
| 27 | pub cooldown: Option<Duration>, |
| 28 | pub allow_when_locked: bool, |
| 29 | pub allow_inhibiting: bool, |
| 30 | pub hotkey_overlay_title: Option<Option<String>>, |
| 31 | } |
| 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 | pub fn into_action(self, pressed: bool) -> Option<Action> { |
| 72 | match (self.action, pressed) { |
| 73 | (BoundAction::Press(action), true) | (BoundAction::Release(action), false) => { |
| 74 | Some(action) |
| 75 | } |
| 76 | (BoundAction::Both { press, .. }, true) => Some(press), |
| 77 | (BoundAction::Both { release, .. }, false) => Some(release), |
| 78 | _ => None, |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
| 84 | pub struct Key { |
| 85 | pub trigger: Trigger, |
| 86 | pub modifiers: Modifiers, |
| 87 | } |
| 88 | |
| 89 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] |
| 90 | pub enum Trigger { |
| 91 | Keysym(Keysym), |
| 92 | CompositorMod, |
| 93 | Modifier(ModKey), |
| 94 | MouseLeft, |
| 95 | MouseRight, |
| 96 | MouseMiddle, |
| 97 | MouseBack, |
| 98 | MouseForward, |
| 99 | WheelScrollDown, |
| 100 | WheelScrollUp, |
| 101 | WheelScrollLeft, |
| 102 | WheelScrollRight, |
| 103 | TouchpadScrollDown, |
| 104 | TouchpadScrollUp, |
| 105 | TouchpadScrollLeft, |
| 106 | TouchpadScrollRight, |
| 107 | TabletStylusButton1, |
| 108 | TabletStylusButton2, |
| 109 | TabletStylusButton3, |
| 110 | } |
| 111 | |
| 112 | /// Returns whether `keysym` is a modifier key like `Shift_L`. |
| 113 | /// |
| 114 | /// This function matches what the niri config considers modifiers. |
| 115 | /// So, it does not correspond exactly to `keysym.is_modifier_key()`. |
| 116 | /// For example, unlike XKB, `Caps_Lock` and `ISO_Next_Group` aren't considered modifiers. |
| 117 | pub fn is_keysym_modifier(keysym: Keysym) -> bool { |
| 118 | ModKey::from_keysym(keysym).is_some() |
| 119 | } |
| 120 | |
| 121 | impl Trigger { |
| 122 | pub fn is_modifier(self) -> bool { |
| 123 | match self { |
| 124 | Self::CompositorMod | Self::Modifier(_) => true, |
| 125 | Self::Keysym(keysym) => is_keysym_modifier(keysym), |
| 126 | _ => false, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub fn is_generic_modifier(self) -> bool { |
| 131 | matches!(self, Self::CompositorMod | Self::Modifier(_)) |
| 132 | } |
| 133 | |
| 134 | /// The modifier flags that this trigger sets, if it is a modifier key. |
| 135 | pub fn implied_modifier(self) -> Modifiers { |
| 136 | match self { |
| 137 | Self::CompositorMod => Modifiers::COMPOSITOR, |
| 138 | Self::Modifier(modifier) => modifier.to_modifiers(), |
| 139 | Self::Keysym(keysym) => ModKey::from_keysym(keysym) |
| 140 | .map_or(Modifiers::empty(), |modifier| modifier.to_modifiers()), |
| 141 | _ => Modifiers::empty(), |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | pub fn is_keyboard(self) -> bool { |
| 146 | matches!( |
| 147 | self, |
| 148 | Trigger::Keysym(_) | Trigger::CompositorMod | Trigger::Modifier(_) |
| 149 | ) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl Key { |
| 154 | /// Whether the two keys are triggered by the same key press. |
| 155 | /// |
| 156 | /// Two different keybinds can be triggered by the same key press. For example, a modifier key |
| 157 | /// can be spelled as the modifier itself (`Alt`) or as its keysym (`Alt_L`). |
| 158 | pub fn conflicts_with(&self, other: &Self) -> bool { |
| 159 | // The trigger and the modifiers match exactly. |
| 160 | // |
| 161 | // All non-modifier triggers fall into this category. |
| 162 | if *self == *other { |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | // Modifiers include the trigger's implied modifier, so differing modifiers can never be |
| 167 | // triggered by the same key press. |
| 168 | if self.modifiers != other.modifiers { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Generic-modifier-only binds are press-order-independent, so only their modifier set |
| 173 | // (which already includes the trigger implied modifier) has to match. |
| 174 | (self.trigger.is_generic_modifier() && other.trigger.is_modifier()) |
| 175 | || (self.trigger.is_modifier() && other.trigger.is_generic_modifier()) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bitflags! { |
| 180 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 181 | pub struct Modifiers : u8 { |
| 182 | const CTRL = 1; |
| 183 | const SHIFT = 1 << 1; |
| 184 | const ALT = 1 << 2; |
| 185 | const SUPER = 1 << 3; |
| 186 | const ISO_LEVEL3_SHIFT = 1 << 4; |
| 187 | const ISO_LEVEL5_SHIFT = 1 << 5; |
| 188 | const COMPOSITOR = 1 << 6; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| 193 | pub struct SwitchBinds { |
| 194 | #[knuffel(child)] |
| 195 | pub lid_open: Option<SwitchAction>, |
| 196 | #[knuffel(child)] |
| 197 | pub lid_close: Option<SwitchAction>, |
| 198 | #[knuffel(child)] |
| 199 | pub tablet_mode_on: Option<SwitchAction>, |
| 200 | #[knuffel(child)] |
| 201 | pub tablet_mode_off: Option<SwitchAction>, |
| 202 | } |
| 203 | |
| 204 | impl MergeWith<SwitchBinds> for SwitchBinds { |
| 205 | fn merge_with(&mut self, part: &SwitchBinds) { |
| 206 | merge_clone_opt!( |
| 207 | (self, part), |
| 208 | lid_open, |
| 209 | lid_close, |
| 210 | tablet_mode_on, |
| 211 | tablet_mode_off, |
| 212 | ); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | #[derive(knuffel::Decode, Debug, Clone, PartialEq)] |
| 217 | pub struct SwitchAction { |
| 218 | #[knuffel(child, unwrap(arguments))] |
| 219 | pub spawn: Vec<String>, |
| 220 | } |
| 221 | |
| 222 | // Remember to add new actions to the CLI enum too. |
| 223 | #[derive(knuffel::Decode, Debug, Clone, PartialEq)] |
| 224 | pub enum Action { |
| 225 | Quit(#[knuffel(property(name = "skip-confirmation"), default)] bool), |
| 226 | #[knuffel(skip)] |
| 227 | ChangeVt(i32), |
| 228 | Suspend, |
| 229 | PowerOffMonitors, |
| 230 | PowerOnMonitors, |
| 231 | ToggleDebugTint, |
| 232 | DebugToggleOpaqueRegions, |
| 233 | DebugToggleDamage, |
| 234 | ToggleTouchpad, |
| 235 | Spawn(#[knuffel(arguments)] Vec<String>), |
| 236 | SpawnSh(#[knuffel(argument)] String), |
| 237 | DoScreenTransition(#[knuffel(property(name = "delay-ms"))] Option<u16>), |
| 238 | #[knuffel(skip)] |
| 239 | ConfirmScreenshot { |
| 240 | write_to_disk: bool, |
| 241 | }, |
| 242 | #[knuffel(skip)] |
| 243 | CancelScreenshot, |
| 244 | #[knuffel(skip)] |
| 245 | ScreenshotTogglePointer, |
| 246 | Screenshot( |
| 247 | #[knuffel(property(name = "show-pointer"), default = true)] bool, |
| 248 | // Path; not settable from knuffel |
| 249 | Option<String>, |
| 250 | ), |
| 251 | ScreenshotScreen( |
| 252 | #[knuffel(property(name = "write-to-disk"), default = true)] bool, |
| 253 | #[knuffel(property(name = "show-pointer"), default = true)] bool, |
| 254 | // Path; not settable from knuffel |
| 255 | Option<String>, |
| 256 | ), |
| 257 | ScreenshotWindow( |
| 258 | #[knuffel(property(name = "write-to-disk"), default = true)] bool, |
| 259 | #[knuffel(property(name = "show-pointer"), default = false)] bool, |
| 260 | // Path; not settable from knuffel |
| 261 | Option<String>, |
| 262 | ), |
| 263 | #[knuffel(skip)] |
| 264 | ScreenshotWindowById { |
| 265 | id: u64, |
| 266 | write_to_disk: bool, |
| 267 | show_pointer: bool, |
| 268 | path: Option<String>, |
| 269 | }, |
| 270 | ToggleKeyboardShortcutsInhibit, |
| 271 | CloseWindow, |
| 272 | #[knuffel(skip)] |
| 273 | CloseWindowById(u64), |
| 274 | FullscreenWindow, |
| 275 | #[knuffel(skip)] |
| 276 | FullscreenWindowById(u64), |
| 277 | ToggleWindowedFullscreen, |
| 278 | #[knuffel(skip)] |
| 279 | ToggleWindowedFullscreenById(u64), |
| 280 | #[knuffel(skip)] |
| 281 | FocusWindow(u64), |
| 282 | FocusWindowInColumn(#[knuffel(argument)] u8), |
| 283 | FocusWindowPrevious, |
| 284 | FocusColumnLeft, |
| 285 | #[knuffel(skip)] |
| 286 | FocusColumnLeftUnderMouse, |
| 287 | FocusColumnRight, |
| 288 | #[knuffel(skip)] |
| 289 | FocusColumnRightUnderMouse, |
| 290 | FocusColumnFirst, |
| 291 | FocusColumnLast, |
| 292 | FocusColumnRightOrFirst, |
| 293 | FocusColumnLeftOrLast, |
| 294 | FocusColumn(#[knuffel(argument)] usize), |
| 295 | FocusWindowOrMonitorUp, |
| 296 | FocusWindowOrMonitorDown, |
| 297 | FocusColumnOrMonitorLeft, |
| 298 | FocusColumnOrMonitorRight, |
| 299 | FocusWindowDown, |
| 300 | FocusWindowUp, |
| 301 | FocusWindowDownOrColumnLeft, |
| 302 | FocusWindowDownOrColumnRight, |
| 303 | FocusWindowUpOrColumnLeft, |
| 304 | FocusWindowUpOrColumnRight, |
| 305 | FocusWindowOrWorkspaceDown, |
| 306 | FocusWindowOrWorkspaceUp, |
| 307 | FocusWindowTop, |
| 308 | FocusWindowBottom, |
| 309 | FocusWindowDownOrTop, |
| 310 | FocusWindowUpOrBottom, |
| 311 | MoveColumnLeft, |
| 312 | MoveColumnRight, |
| 313 | MoveColumnToFirst, |
| 314 | MoveColumnToLast, |
| 315 | MoveColumnLeftOrToMonitorLeft, |
| 316 | MoveColumnRightOrToMonitorRight, |
| 317 | MoveColumnToIndex(#[knuffel(argument)] usize), |
| 318 | MoveWindowDown, |
| 319 | MoveWindowUp, |
| 320 | MoveWindowDownOrToWorkspaceDown, |
| 321 | MoveWindowUpOrToWorkspaceUp, |
| 322 | ConsumeOrExpelWindowLeft, |
| 323 | #[knuffel(skip)] |
| 324 | ConsumeOrExpelWindowLeftById(u64), |
| 325 | ConsumeOrExpelWindowRight, |
| 326 | #[knuffel(skip)] |
| 327 | ConsumeOrExpelWindowRightById(u64), |
| 328 | ConsumeWindowIntoColumn, |
| 329 | ExpelWindowFromColumn, |
| 330 | SwapWindowLeft, |
| 331 | SwapWindowRight, |
| 332 | ToggleColumnTabbedDisplay, |
| 333 | SetColumnDisplay(#[knuffel(argument, str)] ColumnDisplay), |
| 334 | CenterColumn, |
| 335 | CenterWindow, |
| 336 | #[knuffel(skip)] |
| 337 | CenterWindowById(u64), |
| 338 | CenterVisibleColumns, |
| 339 | FocusWorkspaceDown, |
| 340 | #[knuffel(skip)] |
| 341 | FocusWorkspaceDownUnderMouse, |
| 342 | FocusWorkspaceUp, |
| 343 | #[knuffel(skip)] |
| 344 | FocusWorkspaceUpUnderMouse, |
| 345 | FocusWorkspace(#[knuffel(argument)] WorkspaceReference), |
| 346 | FocusWorkspacePrevious, |
| 347 | MoveWindowToWorkspaceDown(#[knuffel(property(name = "focus"), default = true)] bool), |
| 348 | MoveWindowToWorkspaceUp(#[knuffel(property(name = "focus"), default = true)] bool), |
| 349 | MoveWindowToWorkspace( |
| 350 | #[knuffel(argument)] WorkspaceReference, |
| 351 | #[knuffel(property(name = "focus"), default = true)] bool, |
| 352 | ), |
| 353 | #[knuffel(skip)] |
| 354 | MoveWindowToWorkspaceById { |
| 355 | window_id: u64, |
| 356 | reference: WorkspaceReference, |
| 357 | focus: bool, |
| 358 | }, |
| 359 | MoveColumnToWorkspaceDown(#[knuffel(property(name = "focus"), default = true)] bool), |
| 360 | MoveColumnToWorkspaceUp(#[knuffel(property(name = "focus"), default = true)] bool), |
| 361 | MoveColumnToWorkspace( |
| 362 | #[knuffel(argument)] WorkspaceReference, |
| 363 | #[knuffel(property(name = "focus"), default = true)] bool, |
| 364 | ), |
| 365 | MoveWorkspaceDown, |
| 366 | MoveWorkspaceUp, |
| 367 | MoveWorkspaceToIndex(#[knuffel(argument)] usize), |
| 368 | #[knuffel(skip)] |
| 369 | MoveWorkspaceToIndexByRef { |
| 370 | new_idx: usize, |
| 371 | reference: WorkspaceReference, |
| 372 | }, |
| 373 | #[knuffel(skip)] |
| 374 | MoveWorkspaceToMonitorByRef { |
| 375 | output_name: String, |
| 376 | reference: WorkspaceReference, |
| 377 | }, |
| 378 | MoveWorkspaceToMonitor(#[knuffel(argument)] String), |
| 379 | SetWorkspaceName(#[knuffel(argument)] String), |
| 380 | #[knuffel(skip)] |
| 381 | SetWorkspaceNameByRef { |
| 382 | name: String, |
| 383 | reference: WorkspaceReference, |
| 384 | }, |
| 385 | UnsetWorkspaceName, |
| 386 | #[knuffel(skip)] |
| 387 | UnsetWorkSpaceNameByRef(#[knuffel(argument)] WorkspaceReference), |
| 388 | FocusMonitorLeft, |
| 389 | FocusMonitorRight, |
| 390 | FocusMonitorDown, |
| 391 | FocusMonitorUp, |
| 392 | FocusMonitorPrevious, |
| 393 | FocusMonitorNext, |
| 394 | FocusMonitor(#[knuffel(argument)] String), |
| 395 | MoveWindowToMonitorLeft, |
| 396 | MoveWindowToMonitorRight, |
| 397 | MoveWindowToMonitorDown, |
| 398 | MoveWindowToMonitorUp, |
| 399 | MoveWindowToMonitorPrevious, |
| 400 | MoveWindowToMonitorNext, |
| 401 | MoveWindowToMonitor(#[knuffel(argument)] String), |
| 402 | #[knuffel(skip)] |
| 403 | MoveWindowToMonitorById { |
| 404 | id: u64, |
| 405 | output: String, |
| 406 | }, |
| 407 | MoveColumnToMonitorLeft, |
| 408 | MoveColumnToMonitorRight, |
| 409 | MoveColumnToMonitorDown, |
| 410 | MoveColumnToMonitorUp, |
| 411 | MoveColumnToMonitorPrevious, |
| 412 | MoveColumnToMonitorNext, |
| 413 | MoveColumnToMonitor(#[knuffel(argument)] String), |
| 414 | SetWindowWidth(#[knuffel(argument, str)] SizeChange), |
| 415 | #[knuffel(skip)] |
| 416 | SetWindowWidthById { |
| 417 | id: u64, |
| 418 | change: SizeChange, |
| 419 | }, |
| 420 | SetWindowHeight(#[knuffel(argument, str)] SizeChange), |
| 421 | #[knuffel(skip)] |
| 422 | SetWindowHeightById { |
| 423 | id: u64, |
| 424 | change: SizeChange, |
| 425 | }, |
| 426 | ResetWindowHeight, |
| 427 | #[knuffel(skip)] |
| 428 | ResetWindowHeightById(u64), |
| 429 | SwitchPresetColumnWidth, |
| 430 | SwitchPresetColumnWidthBack, |
| 431 | SwitchPresetWindowWidth, |
| 432 | SwitchPresetWindowWidthBack, |
| 433 | #[knuffel(skip)] |
| 434 | SwitchPresetWindowWidthById(u64), |
| 435 | #[knuffel(skip)] |
| 436 | SwitchPresetWindowWidthBackById(u64), |
| 437 | SwitchPresetWindowHeight, |
| 438 | SwitchPresetWindowHeightBack, |
| 439 | #[knuffel(skip)] |
| 440 | SwitchPresetWindowHeightById(u64), |
| 441 | #[knuffel(skip)] |
| 442 | SwitchPresetWindowHeightBackById(u64), |
| 443 | MaximizeColumn, |
| 444 | MaximizeWindowToEdges, |
| 445 | #[knuffel(skip)] |
| 446 | MaximizeWindowToEdgesById(u64), |
| 447 | SetColumnWidth(#[knuffel(argument, str)] SizeChange), |
| 448 | ExpandColumnToAvailableWidth, |
| 449 | SwitchLayout(#[knuffel(argument, str)] LayoutSwitchTarget), |
| 450 | ShowHotkeyOverlay, |
| 451 | MoveWorkspaceToMonitorLeft, |
| 452 | MoveWorkspaceToMonitorRight, |
| 453 | MoveWorkspaceToMonitorDown, |
| 454 | MoveWorkspaceToMonitorUp, |
| 455 | MoveWorkspaceToMonitorPrevious, |
| 456 | MoveWorkspaceToMonitorNext, |
| 457 | ToggleWindowFloating, |
| 458 | #[knuffel(skip)] |
| 459 | ToggleWindowFloatingById(u64), |
| 460 | MoveWindowToFloating, |
| 461 | #[knuffel(skip)] |
| 462 | MoveWindowToFloatingById(u64), |
| 463 | MoveWindowToTiling, |
| 464 | #[knuffel(skip)] |
| 465 | MoveWindowToTilingById(u64), |
| 466 | FocusFloating, |
| 467 | FocusTiling, |
| 468 | SwitchFocusBetweenFloatingAndTiling, |
| 469 | #[knuffel(skip)] |
| 470 | MoveFloatingWindowById { |
| 471 | id: Option<u64>, |
| 472 | x: PositionChange, |
| 473 | y: PositionChange, |
| 474 | }, |
| 475 | ToggleWindowRuleOpacity, |
| 476 | #[knuffel(skip)] |
| 477 | ToggleWindowRuleOpacityById(u64), |
| 478 | SetDynamicCastWindow, |
| 479 | #[knuffel(skip)] |
| 480 | SetDynamicCastWindowById(u64), |
| 481 | SetDynamicCastMonitor(#[knuffel(argument)] Option<String>), |
| 482 | ClearDynamicCastTarget, |
| 483 | #[knuffel(skip)] |
| 484 | StopCast(u64), |
| 485 | ToggleOverview, |
| 486 | OpenOverview, |
| 487 | CloseOverview, |
| 488 | #[knuffel(skip)] |
| 489 | ToggleWindowUrgent(u64), |
| 490 | #[knuffel(skip)] |
| 491 | SetWindowUrgent(u64), |
| 492 | #[knuffel(skip)] |
| 493 | UnsetWindowUrgent(u64), |
| 494 | #[knuffel(skip)] |
| 495 | LoadConfigFile(#[knuffel(argument)] Option<String>), |
| 496 | #[knuffel(skip)] |
| 497 | MruAdvance { |
| 498 | direction: MruDirection, |
| 499 | scope: Option<MruScope>, |
| 500 | filter: Option<MruFilter>, |
| 501 | }, |
| 502 | #[knuffel(skip)] |
| 503 | MruConfirm, |
| 504 | #[knuffel(skip)] |
| 505 | MruCancel, |
| 506 | #[knuffel(skip)] |
| 507 | MruCloseCurrentWindow, |
| 508 | #[knuffel(skip)] |
| 509 | MruFirst, |
| 510 | #[knuffel(skip)] |
| 511 | MruLast, |
| 512 | #[knuffel(skip)] |
| 513 | MruSetScope(MruScope), |
| 514 | #[knuffel(skip)] |
| 515 | MruCycleScope, |
| 516 | |
| 517 | // Doesn't look like knuffel allows gating on #[cfg(test)]... |
| 518 | #[knuffel(skip)] |
| 519 | TestAction, |
| 520 | } |
| 521 | |
| 522 | impl From<niri_ipc::Action> for Action { |
| 523 | fn from(value: niri_ipc::Action) -> Self { |
| 524 | match value { |
| 525 | niri_ipc::Action::Quit { skip_confirmation } => Self::Quit(skip_confirmation), |
| 526 | niri_ipc::Action::PowerOffMonitors {} => Self::PowerOffMonitors, |
| 527 | niri_ipc::Action::PowerOnMonitors {} => Self::PowerOnMonitors, |
| 528 | niri_ipc::Action::Spawn { command } => Self::Spawn(command), |
| 529 | niri_ipc::Action::SpawnSh { command } => Self::SpawnSh(command), |
| 530 | niri_ipc::Action::DoScreenTransition { delay_ms } => Self::DoScreenTransition(delay_ms), |
| 531 | niri_ipc::Action::Screenshot { show_pointer, path } => { |
| 532 | Self::Screenshot(show_pointer, path) |
| 533 | } |
| 534 | niri_ipc::Action::ScreenshotScreen { |
| 535 | write_to_disk, |
| 536 | show_pointer, |
| 537 | path, |
| 538 | } => Self::ScreenshotScreen(write_to_disk, show_pointer, path), |
| 539 | niri_ipc::Action::ScreenshotWindow { |
| 540 | id: None, |
| 541 | write_to_disk, |
| 542 | show_pointer, |
| 543 | path, |
| 544 | } => Self::ScreenshotWindow(write_to_disk, show_pointer, path), |
| 545 | niri_ipc::Action::ScreenshotWindow { |
| 546 | id: Some(id), |
| 547 | write_to_disk, |
| 548 | show_pointer, |
| 549 | path, |
| 550 | } => Self::ScreenshotWindowById { |
| 551 | id, |
| 552 | write_to_disk, |
| 553 | show_pointer, |
| 554 | path, |
| 555 | }, |
| 556 | niri_ipc::Action::ToggleKeyboardShortcutsInhibit {} => { |
| 557 | Self::ToggleKeyboardShortcutsInhibit |
| 558 | } |
| 559 | niri_ipc::Action::CloseWindow { id: None } => Self::CloseWindow, |
| 560 | niri_ipc::Action::CloseWindow { id: Some(id) } => Self::CloseWindowById(id), |
| 561 | niri_ipc::Action::FullscreenWindow { id: None } => Self::FullscreenWindow, |
| 562 | niri_ipc::Action::FullscreenWindow { id: Some(id) } => Self::FullscreenWindowById(id), |
| 563 | niri_ipc::Action::ToggleWindowedFullscreen { id: None } => { |
| 564 | Self::ToggleWindowedFullscreen |
| 565 | } |
| 566 | niri_ipc::Action::ToggleWindowedFullscreen { id: Some(id) } => { |
| 567 | Self::ToggleWindowedFullscreenById(id) |
| 568 | } |
| 569 | niri_ipc::Action::FocusWindow { id } => Self::FocusWindow(id), |
| 570 | niri_ipc::Action::FocusWindowInColumn { index } => Self::FocusWindowInColumn(index), |
| 571 | niri_ipc::Action::FocusWindowPrevious {} => Self::FocusWindowPrevious, |
| 572 | niri_ipc::Action::FocusColumnLeft {} => Self::FocusColumnLeft, |
| 573 | niri_ipc::Action::FocusColumnRight {} => Self::FocusColumnRight, |
| 574 | niri_ipc::Action::FocusColumnFirst {} => Self::FocusColumnFirst, |
| 575 | niri_ipc::Action::FocusColumnLast {} => Self::FocusColumnLast, |
| 576 | niri_ipc::Action::FocusColumnRightOrFirst {} => Self::FocusColumnRightOrFirst, |
| 577 | niri_ipc::Action::FocusColumnLeftOrLast {} => Self::FocusColumnLeftOrLast, |
| 578 | niri_ipc::Action::FocusColumn { index } => Self::FocusColumn(index), |
| 579 | niri_ipc::Action::FocusWindowOrMonitorUp {} => Self::FocusWindowOrMonitorUp, |
| 580 | niri_ipc::Action::FocusWindowOrMonitorDown {} => Self::FocusWindowOrMonitorDown, |
| 581 | niri_ipc::Action::FocusColumnOrMonitorLeft {} => Self::FocusColumnOrMonitorLeft, |
| 582 | niri_ipc::Action::FocusColumnOrMonitorRight {} => Self::FocusColumnOrMonitorRight, |
| 583 | niri_ipc::Action::FocusWindowDown {} => Self::FocusWindowDown, |
| 584 | niri_ipc::Action::FocusWindowUp {} => Self::FocusWindowUp, |
| 585 | niri_ipc::Action::FocusWindowDownOrColumnLeft {} => Self::FocusWindowDownOrColumnLeft, |
| 586 | niri_ipc::Action::FocusWindowDownOrColumnRight {} => Self::FocusWindowDownOrColumnRight, |
| 587 | niri_ipc::Action::FocusWindowUpOrColumnLeft {} => Self::FocusWindowUpOrColumnLeft, |
| 588 | niri_ipc::Action::FocusWindowUpOrColumnRight {} => Self::FocusWindowUpOrColumnRight, |
| 589 | niri_ipc::Action::FocusWindowOrWorkspaceDown {} => Self::FocusWindowOrWorkspaceDown, |
| 590 | niri_ipc::Action::FocusWindowOrWorkspaceUp {} => Self::FocusWindowOrWorkspaceUp, |
| 591 | niri_ipc::Action::FocusWindowTop {} => Self::FocusWindowTop, |
| 592 | niri_ipc::Action::FocusWindowBottom {} => Self::FocusWindowBottom, |
| 593 | niri_ipc::Action::FocusWindowDownOrTop {} => Self::FocusWindowDownOrTop, |
| 594 | niri_ipc::Action::FocusWindowUpOrBottom {} => Self::FocusWindowUpOrBottom, |
| 595 | niri_ipc::Action::MoveColumnLeft {} => Self::MoveColumnLeft, |
| 596 | niri_ipc::Action::MoveColumnRight {} => Self::MoveColumnRight, |
| 597 | niri_ipc::Action::MoveColumnToFirst {} => Self::MoveColumnToFirst, |
| 598 | niri_ipc::Action::MoveColumnToLast {} => Self::MoveColumnToLast, |
| 599 | niri_ipc::Action::MoveColumnToIndex { index } => Self::MoveColumnToIndex(index), |
| 600 | niri_ipc::Action::MoveColumnLeftOrToMonitorLeft {} => { |
| 601 | Self::MoveColumnLeftOrToMonitorLeft |
| 602 | } |
| 603 | niri_ipc::Action::MoveColumnRightOrToMonitorRight {} => { |
| 604 | Self::MoveColumnRightOrToMonitorRight |
| 605 | } |
| 606 | niri_ipc::Action::MoveWindowDown {} => Self::MoveWindowDown, |
| 607 | niri_ipc::Action::MoveWindowUp {} => Self::MoveWindowUp, |
| 608 | niri_ipc::Action::MoveWindowDownOrToWorkspaceDown {} => { |
| 609 | Self::MoveWindowDownOrToWorkspaceDown |
| 610 | } |
| 611 | niri_ipc::Action::MoveWindowUpOrToWorkspaceUp {} => Self::MoveWindowUpOrToWorkspaceUp, |
| 612 | niri_ipc::Action::ConsumeOrExpelWindowLeft { id: None } => { |
| 613 | Self::ConsumeOrExpelWindowLeft |
| 614 | } |
| 615 | niri_ipc::Action::ConsumeOrExpelWindowLeft { id: Some(id) } => { |
| 616 | Self::ConsumeOrExpelWindowLeftById(id) |
| 617 | } |
| 618 | niri_ipc::Action::ConsumeOrExpelWindowRight { id: None } => { |
| 619 | Self::ConsumeOrExpelWindowRight |
| 620 | } |
| 621 | niri_ipc::Action::ConsumeOrExpelWindowRight { id: Some(id) } => { |
| 622 | Self::ConsumeOrExpelWindowRightById(id) |
| 623 | } |
| 624 | niri_ipc::Action::ConsumeWindowIntoColumn {} => Self::ConsumeWindowIntoColumn, |
| 625 | niri_ipc::Action::ExpelWindowFromColumn {} => Self::ExpelWindowFromColumn, |
| 626 | niri_ipc::Action::SwapWindowRight {} => Self::SwapWindowRight, |
| 627 | niri_ipc::Action::SwapWindowLeft {} => Self::SwapWindowLeft, |
| 628 | niri_ipc::Action::ToggleColumnTabbedDisplay {} => Self::ToggleColumnTabbedDisplay, |
| 629 | niri_ipc::Action::SetColumnDisplay { display } => Self::SetColumnDisplay(display), |
| 630 | niri_ipc::Action::CenterColumn {} => Self::CenterColumn, |
| 631 | niri_ipc::Action::CenterWindow { id: None } => Self::CenterWindow, |
| 632 | niri_ipc::Action::CenterWindow { id: Some(id) } => Self::CenterWindowById(id), |
| 633 | niri_ipc::Action::CenterVisibleColumns {} => Self::CenterVisibleColumns, |
| 634 | niri_ipc::Action::FocusWorkspaceDown {} => Self::FocusWorkspaceDown, |
| 635 | niri_ipc::Action::FocusWorkspaceUp {} => Self::FocusWorkspaceUp, |
| 636 | niri_ipc::Action::FocusWorkspace { reference } => { |
| 637 | Self::FocusWorkspace(WorkspaceReference::from(reference)) |
| 638 | } |
| 639 | niri_ipc::Action::FocusWorkspacePrevious {} => Self::FocusWorkspacePrevious, |
| 640 | niri_ipc::Action::MoveWindowToWorkspaceDown { focus } => { |
| 641 | Self::MoveWindowToWorkspaceDown(focus) |
| 642 | } |
| 643 | niri_ipc::Action::MoveWindowToWorkspaceUp { focus } => { |
| 644 | Self::MoveWindowToWorkspaceUp(focus) |
| 645 | } |
| 646 | niri_ipc::Action::MoveWindowToWorkspace { |
| 647 | window_id: None, |
| 648 | reference, |
| 649 | focus, |
| 650 | } => Self::MoveWindowToWorkspace(WorkspaceReference::from(reference), focus), |
| 651 | niri_ipc::Action::MoveWindowToWorkspace { |
| 652 | window_id: Some(window_id), |
| 653 | reference, |
| 654 | focus, |
| 655 | } => Self::MoveWindowToWorkspaceById { |
| 656 | window_id, |
| 657 | reference: WorkspaceReference::from(reference), |
| 658 | focus, |
| 659 | }, |
| 660 | niri_ipc::Action::MoveColumnToWorkspaceDown { focus } => { |
| 661 | Self::MoveColumnToWorkspaceDown(focus) |
| 662 | } |
| 663 | niri_ipc::Action::MoveColumnToWorkspaceUp { focus } => { |
| 664 | Self::MoveColumnToWorkspaceUp(focus) |
| 665 | } |
| 666 | niri_ipc::Action::MoveColumnToWorkspace { reference, focus } => { |
| 667 | Self::MoveColumnToWorkspace(WorkspaceReference::from(reference), focus) |
| 668 | } |
| 669 | niri_ipc::Action::MoveWorkspaceDown {} => Self::MoveWorkspaceDown, |
| 670 | niri_ipc::Action::MoveWorkspaceUp {} => Self::MoveWorkspaceUp, |
| 671 | niri_ipc::Action::SetWorkspaceName { |
| 672 | name, |
| 673 | workspace: None, |
| 674 | } => Self::SetWorkspaceName(name), |
| 675 | niri_ipc::Action::SetWorkspaceName { |
| 676 | name, |
| 677 | workspace: Some(reference), |
| 678 | } => Self::SetWorkspaceNameByRef { |
| 679 | name, |
| 680 | reference: WorkspaceReference::from(reference), |
| 681 | }, |
| 682 | niri_ipc::Action::UnsetWorkspaceName { reference: None } => Self::UnsetWorkspaceName, |
| 683 | niri_ipc::Action::UnsetWorkspaceName { |
| 684 | reference: Some(reference), |
| 685 | } => Self::UnsetWorkSpaceNameByRef(WorkspaceReference::from(reference)), |
| 686 | niri_ipc::Action::FocusMonitorLeft {} => Self::FocusMonitorLeft, |
| 687 | niri_ipc::Action::FocusMonitorRight {} => Self::FocusMonitorRight, |
| 688 | niri_ipc::Action::FocusMonitorDown {} => Self::FocusMonitorDown, |
| 689 | niri_ipc::Action::FocusMonitorUp {} => Self::FocusMonitorUp, |
| 690 | niri_ipc::Action::FocusMonitorPrevious {} => Self::FocusMonitorPrevious, |
| 691 | niri_ipc::Action::FocusMonitorNext {} => Self::FocusMonitorNext, |
| 692 | niri_ipc::Action::FocusMonitor { output } => Self::FocusMonitor(output), |
| 693 | niri_ipc::Action::MoveWindowToMonitorLeft {} => Self::MoveWindowToMonitorLeft, |
| 694 | niri_ipc::Action::MoveWindowToMonitorRight {} => Self::MoveWindowToMonitorRight, |
| 695 | niri_ipc::Action::MoveWindowToMonitorDown {} => Self::MoveWindowToMonitorDown, |
| 696 | niri_ipc::Action::MoveWindowToMonitorUp {} => Self::MoveWindowToMonitorUp, |
| 697 | niri_ipc::Action::MoveWindowToMonitorPrevious {} => Self::MoveWindowToMonitorPrevious, |
| 698 | niri_ipc::Action::MoveWindowToMonitorNext {} => Self::MoveWindowToMonitorNext, |
| 699 | niri_ipc::Action::MoveWindowToMonitor { id: None, output } => { |
| 700 | Self::MoveWindowToMonitor(output) |
| 701 | } |
| 702 | niri_ipc::Action::MoveWindowToMonitor { |
| 703 | id: Some(id), |
| 704 | output, |
| 705 | } => Self::MoveWindowToMonitorById { id, output }, |
| 706 | niri_ipc::Action::MoveColumnToMonitorLeft {} => Self::MoveColumnToMonitorLeft, |
| 707 | niri_ipc::Action::MoveColumnToMonitorRight {} => Self::MoveColumnToMonitorRight, |
| 708 | niri_ipc::Action::MoveColumnToMonitorDown {} => Self::MoveColumnToMonitorDown, |
| 709 | niri_ipc::Action::MoveColumnToMonitorUp {} => Self::MoveColumnToMonitorUp, |
| 710 | niri_ipc::Action::MoveColumnToMonitorPrevious {} => Self::MoveColumnToMonitorPrevious, |
| 711 | niri_ipc::Action::MoveColumnToMonitorNext {} => Self::MoveColumnToMonitorNext, |
| 712 | niri_ipc::Action::MoveColumnToMonitor { output } => Self::MoveColumnToMonitor(output), |
| 713 | niri_ipc::Action::SetWindowWidth { id: None, change } => Self::SetWindowWidth(change), |
| 714 | niri_ipc::Action::SetWindowWidth { |
| 715 | id: Some(id), |
| 716 | change, |
| 717 | } => Self::SetWindowWidthById { id, change }, |
| 718 | niri_ipc::Action::SetWindowHeight { id: None, change } => Self::SetWindowHeight(change), |
| 719 | niri_ipc::Action::SetWindowHeight { |
| 720 | id: Some(id), |
| 721 | change, |
| 722 | } => Self::SetWindowHeightById { id, change }, |
| 723 | niri_ipc::Action::ResetWindowHeight { id: None } => Self::ResetWindowHeight, |
| 724 | niri_ipc::Action::ResetWindowHeight { id: Some(id) } => Self::ResetWindowHeightById(id), |
| 725 | niri_ipc::Action::SwitchPresetColumnWidth {} => Self::SwitchPresetColumnWidth, |
| 726 | niri_ipc::Action::SwitchPresetColumnWidthBack {} => Self::SwitchPresetColumnWidthBack, |
| 727 | niri_ipc::Action::SwitchPresetWindowWidth { id: None } => Self::SwitchPresetWindowWidth, |
| 728 | niri_ipc::Action::SwitchPresetWindowWidthBack { id: None } => { |
| 729 | Self::SwitchPresetWindowWidthBack |
| 730 | } |
| 731 | niri_ipc::Action::SwitchPresetWindowWidth { id: Some(id) } => { |
| 732 | Self::SwitchPresetWindowWidthById(id) |
| 733 | } |
| 734 | niri_ipc::Action::SwitchPresetWindowWidthBack { id: Some(id) } => { |
| 735 | Self::SwitchPresetWindowWidthBackById(id) |
| 736 | } |
| 737 | niri_ipc::Action::SwitchPresetWindowHeight { id: None } => { |
| 738 | Self::SwitchPresetWindowHeight |
| 739 | } |
| 740 | niri_ipc::Action::SwitchPresetWindowHeightBack { id: None } => { |
| 741 | Self::SwitchPresetWindowHeightBack |
| 742 | } |
| 743 | niri_ipc::Action::SwitchPresetWindowHeight { id: Some(id) } => { |
| 744 | Self::SwitchPresetWindowHeightById(id) |
| 745 | } |
| 746 | niri_ipc::Action::SwitchPresetWindowHeightBack { id: Some(id) } => { |
| 747 | Self::SwitchPresetWindowHeightBackById(id) |
| 748 | } |
| 749 | niri_ipc::Action::MaximizeColumn {} => Self::MaximizeColumn, |
| 750 | niri_ipc::Action::MaximizeWindowToEdges { id: None } => Self::MaximizeWindowToEdges, |
| 751 | niri_ipc::Action::MaximizeWindowToEdges { id: Some(id) } => { |
| 752 | Self::MaximizeWindowToEdgesById(id) |
| 753 | } |
| 754 | niri_ipc::Action::SetColumnWidth { change } => Self::SetColumnWidth(change), |
| 755 | niri_ipc::Action::ExpandColumnToAvailableWidth {} => Self::ExpandColumnToAvailableWidth, |
| 756 | niri_ipc::Action::SwitchLayout { layout } => Self::SwitchLayout(layout), |
| 757 | niri_ipc::Action::ShowHotkeyOverlay {} => Self::ShowHotkeyOverlay, |
| 758 | niri_ipc::Action::MoveWorkspaceToMonitorLeft {} => Self::MoveWorkspaceToMonitorLeft, |
| 759 | niri_ipc::Action::MoveWorkspaceToMonitorRight {} => Self::MoveWorkspaceToMonitorRight, |
| 760 | niri_ipc::Action::MoveWorkspaceToMonitorDown {} => Self::MoveWorkspaceToMonitorDown, |
| 761 | niri_ipc::Action::MoveWorkspaceToMonitorUp {} => Self::MoveWorkspaceToMonitorUp, |
| 762 | niri_ipc::Action::MoveWorkspaceToMonitorPrevious {} => { |
| 763 | Self::MoveWorkspaceToMonitorPrevious |
| 764 | } |
| 765 | niri_ipc::Action::MoveWorkspaceToIndex { |
| 766 | index, |
| 767 | reference: Some(reference), |
| 768 | } => Self::MoveWorkspaceToIndexByRef { |
| 769 | new_idx: index, |
| 770 | reference: WorkspaceReference::from(reference), |
| 771 | }, |
| 772 | niri_ipc::Action::MoveWorkspaceToIndex { |
| 773 | index, |
| 774 | reference: None, |
| 775 | } => Self::MoveWorkspaceToIndex(index), |
| 776 | niri_ipc::Action::MoveWorkspaceToMonitor { |
| 777 | output, |
| 778 | reference: Some(reference), |
| 779 | } => Self::MoveWorkspaceToMonitorByRef { |
| 780 | output_name: output, |
| 781 | reference: WorkspaceReference::from(reference), |
| 782 | }, |
| 783 | niri_ipc::Action::MoveWorkspaceToMonitor { |
| 784 | output, |
| 785 | reference: None, |
| 786 | } => Self::MoveWorkspaceToMonitor(output), |
| 787 | niri_ipc::Action::MoveWorkspaceToMonitorNext {} => Self::MoveWorkspaceToMonitorNext, |
| 788 | niri_ipc::Action::ToggleDebugTint {} => Self::ToggleDebugTint, |
| 789 | niri_ipc::Action::DebugToggleOpaqueRegions {} => Self::DebugToggleOpaqueRegions, |
| 790 | niri_ipc::Action::DebugToggleDamage {} => Self::DebugToggleDamage, |
| 791 | niri_ipc::Action::ToggleTouchpad {} => Self::ToggleTouchpad, |
| 792 | niri_ipc::Action::ToggleWindowFloating { id: None } => Self::ToggleWindowFloating, |
| 793 | niri_ipc::Action::ToggleWindowFloating { id: Some(id) } => { |
| 794 | Self::ToggleWindowFloatingById(id) |
| 795 | } |
| 796 | niri_ipc::Action::MoveWindowToFloating { id: None } => Self::MoveWindowToFloating, |
| 797 | niri_ipc::Action::MoveWindowToFloating { id: Some(id) } => { |
| 798 | Self::MoveWindowToFloatingById(id) |
| 799 | } |
| 800 | niri_ipc::Action::MoveWindowToTiling { id: None } => Self::MoveWindowToTiling, |
| 801 | niri_ipc::Action::MoveWindowToTiling { id: Some(id) } => { |
| 802 | Self::MoveWindowToTilingById(id) |
| 803 | } |
| 804 | niri_ipc::Action::FocusFloating {} => Self::FocusFloating, |
| 805 | niri_ipc::Action::FocusTiling {} => Self::FocusTiling, |
| 806 | niri_ipc::Action::SwitchFocusBetweenFloatingAndTiling {} => { |
| 807 | Self::SwitchFocusBetweenFloatingAndTiling |
| 808 | } |
| 809 | niri_ipc::Action::MoveFloatingWindow { id, x, y } => { |
| 810 | Self::MoveFloatingWindowById { id, x, y } |
| 811 | } |
| 812 | niri_ipc::Action::ToggleWindowRuleOpacity { id: None } => Self::ToggleWindowRuleOpacity, |
| 813 | niri_ipc::Action::ToggleWindowRuleOpacity { id: Some(id) } => { |
| 814 | Self::ToggleWindowRuleOpacityById(id) |
| 815 | } |
| 816 | niri_ipc::Action::SetDynamicCastWindow { id: None } => Self::SetDynamicCastWindow, |
| 817 | niri_ipc::Action::SetDynamicCastWindow { id: Some(id) } => { |
| 818 | Self::SetDynamicCastWindowById(id) |
| 819 | } |
| 820 | niri_ipc::Action::SetDynamicCastMonitor { output } => { |
| 821 | Self::SetDynamicCastMonitor(output) |
| 822 | } |
| 823 | niri_ipc::Action::ClearDynamicCastTarget {} => Self::ClearDynamicCastTarget, |
| 824 | niri_ipc::Action::StopCast { session_id } => Self::StopCast(session_id), |
| 825 | niri_ipc::Action::ToggleOverview {} => Self::ToggleOverview, |
| 826 | niri_ipc::Action::OpenOverview {} => Self::OpenOverview, |
| 827 | niri_ipc::Action::CloseOverview {} => Self::CloseOverview, |
| 828 | niri_ipc::Action::ToggleWindowUrgent { id } => Self::ToggleWindowUrgent(id), |
| 829 | niri_ipc::Action::SetWindowUrgent { id } => Self::SetWindowUrgent(id), |
| 830 | niri_ipc::Action::UnsetWindowUrgent { id } => Self::UnsetWindowUrgent(id), |
| 831 | niri_ipc::Action::LoadConfigFile { path } => Self::LoadConfigFile(path), |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | #[derive(Debug, PartialEq, Eq, Clone)] |
| 837 | pub enum WorkspaceReference { |
| 838 | Id(u64), |
| 839 | Index(u8), |
| 840 | Name(String), |
| 841 | } |
| 842 | |
| 843 | impl From<WorkspaceReferenceArg> for WorkspaceReference { |
| 844 | fn from(reference: WorkspaceReferenceArg) -> WorkspaceReference { |
| 845 | match reference { |
| 846 | WorkspaceReferenceArg::Id(id) => Self::Id(id), |
| 847 | WorkspaceReferenceArg::Index(i) => Self::Index(i), |
| 848 | WorkspaceReferenceArg::Name(n) => Self::Name(n), |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceReference { |
| 854 | fn type_check( |
| 855 | type_name: &Option<knuffel::span::Spanned<knuffel::ast::TypeName, S>>, |
| 856 | ctx: &mut knuffel::decode::Context<S>, |
| 857 | ) { |
| 858 | if let Some(type_name) = &type_name { |
| 859 | ctx.emit_error(DecodeError::unexpected( |
| 860 | type_name, |
| 861 | "type name", |
| 862 | "no type name expected for this node", |
| 863 | )); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | fn raw_decode( |
| 868 | val: &knuffel::span::Spanned<knuffel::ast::Literal, S>, |
| 869 | ctx: &mut knuffel::decode::Context<S>, |
| 870 | ) -> Result<WorkspaceReference, DecodeError<S>> { |
| 871 | match &**val { |
| 872 | knuffel::ast::Literal::String(ref s) => Ok(WorkspaceReference::Name(s.clone().into())), |
| 873 | knuffel::ast::Literal::Int(ref value) => match value.try_into() { |
| 874 | Ok(v) => Ok(WorkspaceReference::Index(v)), |
| 875 | Err(e) => { |
| 876 | ctx.emit_error(DecodeError::conversion(val, e)); |
| 877 | Ok(WorkspaceReference::Index(0)) |
| 878 | } |
| 879 | }, |
| 880 | _ => { |
| 881 | ctx.emit_error(DecodeError::unsupported( |
| 882 | val, |
| 883 | "Unsupported value, only numbers and strings are recognized", |
| 884 | )); |
| 885 | Ok(WorkspaceReference::Index(0)) |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | impl<S> knuffel::Decode<S> for Binds |
| 892 | where |
| 893 | S: knuffel::traits::ErrorSpan, |
| 894 | { |
| 895 | fn decode_node( |
| 896 | node: &knuffel::ast::SpannedNode<S>, |
| 897 | ctx: &mut knuffel::decode::Context<S>, |
| 898 | ) -> Result<Self, DecodeError<S>> { |
| 899 | expect_only_children(node, ctx); |
| 900 | |
| 901 | let mut nodes: Vec<&knuffel::ast::SpannedNode<S>> = Vec::new(); |
| 902 | let mut binds: Vec<Bind> = Vec::new(); |
| 903 | |
| 904 | for child in node.children() { |
| 905 | match Bind::decode_node(child, ctx) { |
| 906 | Err(e) => { |
| 907 | ctx.emit_error(e); |
| 908 | } |
| 909 | Ok(bind) => { |
| 910 | let conflict = binds |
| 911 | .iter() |
| 912 | .position(|prev| prev.key.conflicts_with(&bind.key)); |
| 913 | if let Some(idx) = conflict { |
| 914 | // Even though it's technically incorrect, we use |
| 915 | // `DecodeError::Missing` here because it labels the bind with |
| 916 | // "node starts here", which is the least bad option |
| 917 | ctx.emit_error(DecodeError::missing( |
| 918 | nodes[idx], |
| 919 | "keybind first defined here", |
| 920 | )); |
| 921 | |
| 922 | ctx.emit_error(DecodeError::unexpected( |
| 923 | &child.node_name, |
| 924 | "keybind", |
| 925 | if binds[idx].key == bind.key { |
| 926 | "duplicate keybind later defined here" |
| 927 | } else { |
| 928 | "conflicting keybind later defined here" |
| 929 | }, |
| 930 | )); |
| 931 | } else { |
| 932 | nodes.push(child); |
| 933 | binds.push(bind); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | Ok(Self(binds)) |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | impl<S> knuffel::Decode<S> for Bind |
| 944 | where |
| 945 | S: knuffel::traits::ErrorSpan, |
| 946 | { |
| 947 | fn decode_node( |
| 948 | node: &knuffel::ast::SpannedNode<S>, |
| 949 | ctx: &mut knuffel::decode::Context<S>, |
| 950 | ) -> Result<Self, DecodeError<S>> { |
| 951 | if let Some(type_name) = &node.type_name { |
| 952 | ctx.emit_error(DecodeError::unexpected( |
| 953 | type_name, |
| 954 | "type name", |
| 955 | "no type name expected for this node", |
| 956 | )); |
| 957 | } |
| 958 | |
| 959 | for val in node.arguments.iter() { |
| 960 | ctx.emit_error(DecodeError::unexpected( |
| 961 | &val.literal, |
| 962 | "argument", |
| 963 | "no arguments expected for this node", |
| 964 | )); |
| 965 | } |
| 966 | |
| 967 | let key = node |
| 968 | .node_name |
| 969 | .parse::<Key>() |
| 970 | .map_err(|e| DecodeError::conversion(&node.node_name, e.wrap_err("invalid keybind")))?; |
| 971 | |
| 972 | let mut repeat = None; |
| 973 | let mut repeat_node = None; |
| 974 | let mut cooldown = None; |
| 975 | let mut allow_when_locked = false; |
| 976 | let mut allow_when_locked_node = None; |
| 977 | let mut allow_inhibiting = true; |
| 978 | let mut hotkey_overlay_title = None; |
| 979 | |
| 980 | for (name, val) in &node.properties { |
| 981 | match &***name { |
| 982 | "repeat" => { |
| 983 | repeat = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); |
| 984 | repeat_node = Some(name); |
| 985 | } |
| 986 | "cooldown-ms" => { |
| 987 | cooldown = Some(Duration::from_millis( |
| 988 | knuffel::traits::DecodeScalar::decode(val, ctx)?, |
| 989 | )); |
| 990 | } |
| 991 | "allow-when-locked" => { |
| 992 | allow_when_locked = knuffel::traits::DecodeScalar::decode(val, ctx)?; |
| 993 | allow_when_locked_node = Some(name); |
| 994 | } |
| 995 | "allow-inhibiting" => { |
| 996 | allow_inhibiting = knuffel::traits::DecodeScalar::decode(val, ctx)?; |
| 997 | } |
| 998 | "hotkey-overlay-title" => { |
| 999 | hotkey_overlay_title = Some(knuffel::traits::DecodeScalar::decode(val, ctx)?); |
| 1000 | } |
| 1001 | name_str => { |
| 1002 | ctx.emit_error(DecodeError::unexpected( |
| 1003 | name, |
| 1004 | "property", |
| 1005 | format!("unexpected property `{}`", name_str.escape_default()), |
| 1006 | )); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | // If the action is invalid but the key is fine, we still want to return something. |
| 1012 | // That way, the parent can handle the existence of duplicate keybinds, |
| 1013 | // even if their contents are not valid. |
| 1014 | let dummy = Self { |
| 1015 | key, |
| 1016 | action: BoundAction::Press(Action::Spawn(vec![])), |
| 1017 | repeat: true, |
| 1018 | cooldown: None, |
| 1019 | allow_when_locked: false, |
| 1020 | allow_inhibiting: true, |
| 1021 | hotkey_overlay_title: None, |
| 1022 | }; |
| 1023 | |
| 1024 | // direct_action is an action node that is not wrapped in a press or release section |
| 1025 | let mut direct_action: Option<Action> = None; |
| 1026 | let mut press_action: Option<Action> = None; |
| 1027 | let mut release_action: Option<Action> = None; |
| 1028 | |
| 1029 | let decode_action = |ctx: &mut knuffel::decode::Context<S>, |
| 1030 | child: &knuffel::ast::SpannedNode<S>| { |
| 1031 | match Action::decode_node(child, ctx) { |
| 1032 | Ok(action) => Some(action), |
| 1033 | Err(e) => { |
| 1034 | ctx.emit_error(e); |
| 1035 | None |
| 1036 | } |
| 1037 | } |
| 1038 | }; |
| 1039 | |
| 1040 | for child in node.children() { |
| 1041 | let child_name = &*child.node_name; |
| 1042 | let child_name: &str = child_name.as_ref(); |
| 1043 | |
| 1044 | if child_name == "press" || child_name == "release" { |
| 1045 | let is_press = child_name == "press"; |
| 1046 | let slot = if is_press { |
| 1047 | &mut press_action |
| 1048 | } else { |
| 1049 | &mut release_action |
| 1050 | }; |
| 1051 | |
| 1052 | if slot.is_some() { |
| 1053 | ctx.emit_error(DecodeError::unexpected( |
| 1054 | &child.node_name, |
| 1055 | "section", |
| 1056 | format!("duplicate `{child_name}` section"), |
| 1057 | )); |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | if direct_action.is_some() { |
| 1062 | ctx.emit_error(DecodeError::unexpected( |
| 1063 | &child.node_name, |
| 1064 | "section", |
| 1065 | "cannot mix direct actions with press/release sections", |
| 1066 | )); |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | // The action is the section's single child node. |
| 1071 | let mut children = child.children(); |
| 1072 | let Some(action_child) = children.next() else { |
| 1073 | ctx.emit_error(DecodeError::missing( |
| 1074 | child, |
| 1075 | format!("expected an action for this {child_name} section"), |
| 1076 | )); |
| 1077 | continue; |
| 1078 | }; |
| 1079 | |
| 1080 | for unwanted_child in children { |
| 1081 | ctx.emit_error(DecodeError::unexpected( |
| 1082 | unwanted_child, |
| 1083 | "node", |
| 1084 | "only one action is allowed per keybind", |
| 1085 | )); |
| 1086 | } |
| 1087 | |
| 1088 | *slot = decode_action(ctx, action_child); |
| 1089 | } else { |
| 1090 | if direct_action.is_some() { |
| 1091 | ctx.emit_error(DecodeError::unexpected( |
| 1092 | &child.node_name, |
| 1093 | "node", |
| 1094 | "only one action is allowed per keybind", |
| 1095 | )); |
| 1096 | continue; |
| 1097 | } |
| 1098 | |
| 1099 | if press_action.is_some() || release_action.is_some() { |
| 1100 | ctx.emit_error(DecodeError::unexpected( |
| 1101 | &child.node_name, |
| 1102 | "node", |
| 1103 | "cannot mix direct actions with press/release sections", |
| 1104 | )); |
| 1105 | continue; |
| 1106 | } |
| 1107 | |
| 1108 | direct_action = decode_action(ctx, child); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | let press_action = press_action.or(direct_action); |
| 1113 | |
| 1114 | if release_action.is_some() |
| 1115 | && matches!( |
| 1116 | key.trigger, |
| 1117 | Trigger::WheelScrollDown |
| 1118 | | Trigger::WheelScrollUp |
| 1119 | | Trigger::WheelScrollLeft |
| 1120 | | Trigger::WheelScrollRight |
| 1121 | | Trigger::TouchpadScrollDown |
| 1122 | | Trigger::TouchpadScrollUp |
| 1123 | | Trigger::TouchpadScrollLeft |
| 1124 | | Trigger::TouchpadScrollRight |
| 1125 | ) |
| 1126 | { |
| 1127 | ctx.emit_error(DecodeError::unexpected( |
| 1128 | node, |
| 1129 | "bind", |
| 1130 | "release sections are not supported for scroll binds", |
| 1131 | )); |
| 1132 | release_action = None; |
| 1133 | } |
| 1134 | |
| 1135 | if let Some(node) = repeat_node { |
| 1136 | if repeat == Some(true) && press_action.is_none() && release_action.is_some() { |
| 1137 | ctx.emit_error(DecodeError::unexpected( |
| 1138 | node, |
| 1139 | "property", |
| 1140 | "release-only binds cannot repeat", |
| 1141 | )); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if let Some(node) = allow_when_locked_node { |
| 1146 | for action in press_action.iter().chain(release_action.iter()) { |
| 1147 | if !matches!(action, Action::Spawn(_) | Action::SpawnSh(_)) { |
| 1148 | ctx.emit_error(DecodeError::unexpected( |
| 1149 | node, |
| 1150 | "property", |
| 1151 | "allow-when-locked can only be set on spawn binds", |
| 1152 | )); |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | // The toggle-inhibit action must always be uninhibitable. |
| 1158 | // Otherwise, it would be impossible to trigger it. |
| 1159 | if matches!(press_action, Some(Action::ToggleKeyboardShortcutsInhibit)) |
| 1160 | || matches!(release_action, Some(Action::ToggleKeyboardShortcutsInhibit)) |
| 1161 | { |
| 1162 | allow_inhibiting = false; |
| 1163 | } |
| 1164 | |
| 1165 | let repeat = match repeat { |
| 1166 | Some(value) => value, |
| 1167 | None => release_action.is_none(), |
| 1168 | }; |
| 1169 | |
| 1170 | let action = match (press_action, release_action) { |
| 1171 | (Some(press), Some(release)) => BoundAction::Both { press, release }, |
| 1172 | (Some(press), None) => BoundAction::Press(press), |
| 1173 | (None, Some(release)) => BoundAction::Release(release), |
| 1174 | (None, None) => { |
| 1175 | // If a press or release section was present, an error about its missing or |
| 1176 | // invalid action was already emitted above. |
| 1177 | let has_section = node.children().any(|child| { |
| 1178 | let name = child.node_name.as_ref(); |
| 1179 | name == "press" || name == "release" |
| 1180 | }); |
| 1181 | if !has_section { |
| 1182 | ctx.emit_error(DecodeError::missing( |
| 1183 | node, |
| 1184 | "expected an action for this keybind", |
| 1185 | )); |
| 1186 | } |
| 1187 | return Ok(dummy); |
| 1188 | } |
| 1189 | }; |
| 1190 | |
| 1191 | Ok(Self { |
| 1192 | key, |
| 1193 | action, |
| 1194 | repeat, |
| 1195 | cooldown, |
| 1196 | allow_when_locked, |
| 1197 | allow_inhibiting, |
| 1198 | hotkey_overlay_title, |
| 1199 | }) |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | impl FromStr for Key { |
| 1204 | type Err = miette::Error; |
| 1205 | |
| 1206 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 1207 | if s.contains('\0') { |
| 1208 | return Err(miette!("invalid key with a null byte: {s:?}")); |
| 1209 | } |
| 1210 | |
| 1211 | let mut written_modifiers: Vec<(Modifiers, &str)> = Vec::new(); |
| 1212 | |
| 1213 | let mut split = s.split('+'); |
| 1214 | let key = split.next_back().unwrap(); |
| 1215 | |
| 1216 | for part in split { |
| 1217 | let part = part.trim(); |
| 1218 | let modifier = if part.eq_ignore_ascii_case("mod") { |
| 1219 | Modifiers::COMPOSITOR |
| 1220 | } else if part.eq_ignore_ascii_case("ctrl") || part.eq_ignore_ascii_case("control") { |
| 1221 | Modifiers::CTRL |
| 1222 | } else if part.eq_ignore_ascii_case("shift") { |
| 1223 | Modifiers::SHIFT |
| 1224 | } else if part.eq_ignore_ascii_case("alt") { |
| 1225 | Modifiers::ALT |
| 1226 | } else if part.eq_ignore_ascii_case("super") || part.eq_ignore_ascii_case("win") { |
| 1227 | Modifiers::SUPER |
| 1228 | } else if part.eq_ignore_ascii_case("iso_level3_shift") |
| 1229 | || part.eq_ignore_ascii_case("mod5") |
| 1230 | { |
| 1231 | Modifiers::ISO_LEVEL3_SHIFT |
| 1232 | } else if part.eq_ignore_ascii_case("iso_level5_shift") |
| 1233 | || part.eq_ignore_ascii_case("mod3") |
| 1234 | { |
| 1235 | Modifiers::ISO_LEVEL5_SHIFT |
| 1236 | } else { |
| 1237 | return Err(miette!("invalid modifier: {part}")); |
| 1238 | }; |
| 1239 | |
| 1240 | written_modifiers.push((modifier, part)); |
| 1241 | } |
| 1242 | |
| 1243 | let trigger = if key.eq_ignore_ascii_case("MouseLeft") { |
| 1244 | Trigger::MouseLeft |
| 1245 | } else if key.eq_ignore_ascii_case("MouseRight") { |
| 1246 | Trigger::MouseRight |
| 1247 | } else if key.eq_ignore_ascii_case("MouseMiddle") { |
| 1248 | Trigger::MouseMiddle |
| 1249 | } else if key.eq_ignore_ascii_case("MouseBack") { |
| 1250 | Trigger::MouseBack |
| 1251 | } else if key.eq_ignore_ascii_case("MouseForward") { |
| 1252 | Trigger::MouseForward |
| 1253 | } else if key.eq_ignore_ascii_case("WheelScrollDown") { |
| 1254 | Trigger::WheelScrollDown |
| 1255 | } else if key.eq_ignore_ascii_case("WheelScrollUp") { |
| 1256 | Trigger::WheelScrollUp |
| 1257 | } else if key.eq_ignore_ascii_case("WheelScrollLeft") { |
| 1258 | Trigger::WheelScrollLeft |
| 1259 | } else if key.eq_ignore_ascii_case("WheelScrollRight") { |
| 1260 | Trigger::WheelScrollRight |
| 1261 | } else if key.eq_ignore_ascii_case("TouchpadScrollDown") { |
| 1262 | Trigger::TouchpadScrollDown |
| 1263 | } else if key.eq_ignore_ascii_case("TouchpadScrollUp") { |
| 1264 | Trigger::TouchpadScrollUp |
| 1265 | } else if key.eq_ignore_ascii_case("TouchpadScrollLeft") { |
| 1266 | Trigger::TouchpadScrollLeft |
| 1267 | } else if key.eq_ignore_ascii_case("TouchpadScrollRight") { |
| 1268 | Trigger::TouchpadScrollRight |
| 1269 | } else if key.eq_ignore_ascii_case("TabletStylusButton1") { |
| 1270 | Trigger::TabletStylusButton1 |
| 1271 | } else if key.eq_ignore_ascii_case("TabletStylusButton2") { |
| 1272 | Trigger::TabletStylusButton2 |
| 1273 | } else if key.eq_ignore_ascii_case("TabletStylusButton3") { |
| 1274 | Trigger::TabletStylusButton3 |
| 1275 | } else if key.eq_ignore_ascii_case("Mod") { |
| 1276 | Trigger::CompositorMod |
| 1277 | } else if key.eq_ignore_ascii_case("Ctrl") || key.eq_ignore_ascii_case("Control") { |
| 1278 | Trigger::Modifier(ModKey::Ctrl) |
| 1279 | } else if key.eq_ignore_ascii_case("Shift") { |
| 1280 | Trigger::Modifier(ModKey::Shift) |
| 1281 | } else if key.eq_ignore_ascii_case("Alt") { |
| 1282 | Trigger::Modifier(ModKey::Alt) |
| 1283 | } else if key.eq_ignore_ascii_case("Super") || key.eq_ignore_ascii_case("Win") { |
| 1284 | Trigger::Modifier(ModKey::Super) |
| 1285 | } else if key.eq_ignore_ascii_case("ISO_Level3_Shift") || key.eq_ignore_ascii_case("Mod5") { |
| 1286 | Trigger::Modifier(ModKey::IsoLevel3Shift) |
| 1287 | } else if key.eq_ignore_ascii_case("ISO_Level5_Shift") || key.eq_ignore_ascii_case("Mod3") { |
| 1288 | Trigger::Modifier(ModKey::IsoLevel5Shift) |
| 1289 | } else { |
| 1290 | let mut keysym = keysym_from_name(key, KEYSYM_CASE_INSENSITIVE); |
| 1291 | // The keyboard event handling code can receive either |
| 1292 | // XF86ScreenSaver or XF86Screensaver, because there is no |
| 1293 | // case mapping defined between these keysyms. If we just |
| 1294 | // use the case-insensitive version of keysym_from_name it |
| 1295 | // is not possible to bind the uppercase version, because the |
| 1296 | // case-insensitive match prefers the lowercase version when |
| 1297 | // there is a choice. |
| 1298 | // |
| 1299 | // Therefore, when we match this key with the initial |
| 1300 | // case-insensitive match we try a further case-sensitive match |
| 1301 | // (so that either key can be bound). If that fails, we change |
| 1302 | // to the uppercase version because: |
| 1303 | // |
| 1304 | // - A comment in xkb_keysym_from_name (in libxkbcommon) tells us that the uppercase |
| 1305 | // version is the "best" of the two. [0] |
| 1306 | // - The xkbcommon crate only has a constant for ScreenSaver. [1] |
| 1307 | // |
| 1308 | // [0]: https://github.com/xkbcommon/libxkbcommon/blob/45a118d5325b051343b4b174f60c1434196fa7d4/src/keysym.c#L276 |
| 1309 | // [1]: https://docs.rs/xkbcommon/latest/xkbcommon/xkb/keysyms/index.html#:~:text=KEY%5FXF86ScreenSaver |
| 1310 | // |
| 1311 | // See https://github.com/niri-wm/niri/issues/1969 |
| 1312 | if keysym == Keysym::XF86_Screensaver { |
| 1313 | keysym = keysym_from_name(key, KEYSYM_NO_FLAGS); |
| 1314 | if keysym.raw() == KEY_NoSymbol { |
| 1315 | keysym = Keysym::XF86_ScreenSaver; |
| 1316 | } |
| 1317 | } |
| 1318 | if keysym.raw() == KEY_NoSymbol { |
| 1319 | return Err(miette!("invalid key: {key}")); |
| 1320 | } |
| 1321 | Trigger::Keysym(keysym) |
| 1322 | }; |
| 1323 | |
| 1324 | let mut modifiers = trigger.implied_modifier(); |
| 1325 | for (modifier, spelling) in written_modifiers { |
| 1326 | if modifiers.contains(modifier) { |
| 1327 | warn!("redundant modifier `{spelling}` in keybind `{s}`"); |
| 1328 | } |
| 1329 | |
| 1330 | modifiers |= modifier; |
| 1331 | } |
| 1332 | |
| 1333 | Ok(Key { trigger, modifiers }) |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | #[cfg(test)] |
| 1338 | mod tests { |
| 1339 | use super::*; |
| 1340 | |
| 1341 | #[test] |
| 1342 | fn parse_xf86_screensaver() { |
| 1343 | assert_eq!( |
| 1344 | "XF86ScreenSaver".parse::<Key>().unwrap(), |
| 1345 | Key { |
| 1346 | trigger: Trigger::Keysym(Keysym::XF86_ScreenSaver), |
| 1347 | modifiers: Modifiers::empty(), |
| 1348 | }, |
| 1349 | ); |
| 1350 | assert_eq!( |
| 1351 | "XF86Screensaver".parse::<Key>().unwrap(), |
| 1352 | Key { |
| 1353 | trigger: Trigger::Keysym(Keysym::XF86_Screensaver), |
| 1354 | modifiers: Modifiers::empty(), |
| 1355 | } |
| 1356 | ); |
| 1357 | assert_eq!( |
| 1358 | "xf86screensaver".parse::<Key>().unwrap(), |
| 1359 | Key { |
| 1360 | trigger: Trigger::Keysym(Keysym::XF86_ScreenSaver), |
| 1361 | modifiers: Modifiers::empty(), |
| 1362 | } |
| 1363 | ); |
| 1364 | } |
| 1365 | |
| 1366 | #[test] |
| 1367 | fn parse_iso_level_shifts() { |
| 1368 | assert_eq!( |
| 1369 | "ISO_Level3_Shift+A".parse::<Key>().unwrap(), |
| 1370 | Key { |
| 1371 | trigger: Trigger::Keysym(Keysym::a), |
| 1372 | modifiers: Modifiers::ISO_LEVEL3_SHIFT |
| 1373 | }, |
| 1374 | ); |
| 1375 | assert_eq!( |
| 1376 | "Mod5+A".parse::<Key>().unwrap(), |
| 1377 | Key { |
| 1378 | trigger: Trigger::Keysym(Keysym::a), |
| 1379 | modifiers: Modifiers::ISO_LEVEL3_SHIFT |
| 1380 | }, |
| 1381 | ); |
| 1382 | |
| 1383 | assert_eq!( |
| 1384 | "ISO_Level5_Shift+A".parse::<Key>().unwrap(), |
| 1385 | Key { |
| 1386 | trigger: Trigger::Keysym(Keysym::a), |
| 1387 | modifiers: Modifiers::ISO_LEVEL5_SHIFT |
| 1388 | }, |
| 1389 | ); |
| 1390 | assert_eq!( |
| 1391 | "Mod3+A".parse::<Key>().unwrap(), |
| 1392 | Key { |
| 1393 | trigger: Trigger::Keysym(Keysym::a), |
| 1394 | modifiers: Modifiers::ISO_LEVEL5_SHIFT |
| 1395 | }, |
| 1396 | ); |
| 1397 | } |
| 1398 | |
| 1399 | #[test] |
| 1400 | fn parse_bare_modifier_spellings() { |
| 1401 | // Each modifier can be spelled with its canonical name or an alias. A bare modifier key |
| 1402 | // parses to `Trigger::Modifier`. |
| 1403 | for (text, modifier) in [ |
| 1404 | ("Ctrl", ModKey::Ctrl), |
| 1405 | ("Control", ModKey::Ctrl), |
| 1406 | ("Shift", ModKey::Shift), |
| 1407 | ("Alt", ModKey::Alt), |
| 1408 | ("Super", ModKey::Super), |
| 1409 | ("Win", ModKey::Super), |
| 1410 | ("Mod5", ModKey::IsoLevel3Shift), |
| 1411 | ("ISO_Level3_Shift", ModKey::IsoLevel3Shift), |
| 1412 | ("Mod3", ModKey::IsoLevel5Shift), |
| 1413 | ("ISO_Level5_Shift", ModKey::IsoLevel5Shift), |
| 1414 | ] { |
| 1415 | assert_eq!( |
| 1416 | text.parse::<Key>().unwrap(), |
| 1417 | Key { |
| 1418 | trigger: Trigger::Modifier(modifier), |
| 1419 | modifiers: modifier.to_modifiers(), |
| 1420 | }, |
| 1421 | "parsing `{text}`", |
| 1422 | ); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | #[test] |
| 1427 | fn parse_mod_as_trigger() { |
| 1428 | // The mod key keeps its own trigger, which is resolved to a real modifier at runtime. |
| 1429 | assert_eq!( |
| 1430 | "Mod".parse::<Key>().unwrap(), |
| 1431 | Key { |
| 1432 | trigger: Trigger::CompositorMod, |
| 1433 | modifiers: Modifiers::COMPOSITOR, |
| 1434 | }, |
| 1435 | ); |
| 1436 | |
| 1437 | // It can also be used together with other held modifiers. |
| 1438 | assert_eq!( |
| 1439 | "Ctrl+Mod".parse::<Key>().unwrap(), |
| 1440 | Key { |
| 1441 | trigger: Trigger::CompositorMod, |
| 1442 | modifiers: Modifiers::CTRL | Modifiers::COMPOSITOR, |
| 1443 | }, |
| 1444 | ); |
| 1445 | |
| 1446 | // `Mod` also works as a held modifier of another trigger. |
| 1447 | assert_eq!( |
| 1448 | "Mod+Control_L".parse::<Key>().unwrap(), |
| 1449 | Key { |
| 1450 | trigger: Trigger::Keysym(Keysym::Control_L), |
| 1451 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, |
| 1452 | }, |
| 1453 | ); |
| 1454 | } |
| 1455 | |
| 1456 | #[test] |
| 1457 | fn parse_multi_modifier_triggers() { |
| 1458 | // A modifier key as the trigger with another held modifier, either spelled as the modifier |
| 1459 | // itself or as its keysym. |
| 1460 | assert_eq!( |
| 1461 | "Ctrl+Alt".parse::<Key>().unwrap(), |
| 1462 | Key { |
| 1463 | trigger: Trigger::Modifier(ModKey::Alt), |
| 1464 | modifiers: Modifiers::CTRL | Modifiers::ALT, |
| 1465 | }, |
| 1466 | ); |
| 1467 | assert_eq!( |
| 1468 | "Ctrl+Alt_L".parse::<Key>().unwrap(), |
| 1469 | Key { |
| 1470 | trigger: Trigger::Keysym(Keysym::Alt_L), |
| 1471 | modifiers: Modifiers::CTRL | Modifiers::ALT, |
| 1472 | }, |
| 1473 | ); |
| 1474 | } |
| 1475 | |
| 1476 | #[test] |
| 1477 | fn parse_normalizes_redundant_modifiers() { |
| 1478 | let key = |text: &str| text.parse::<Key>().unwrap(); |
| 1479 | |
| 1480 | // Modifier triggers can be written with or without the redundant modifier. |
| 1481 | assert_eq!(key("Alt+Alt_L"), key("Alt_L")); |
| 1482 | assert_eq!(key("Alt+Alt_R"), key("Alt_R")); |
| 1483 | assert_eq!(key("Alt+Alt"), key("Alt")); |
| 1484 | assert_eq!(key("Alt+Alt+Alt_L"), key("Alt_L")); |
| 1485 | assert_eq!(key("Ctrl+Shift+Control_L"), key("Shift+Control_L")); |
| 1486 | assert_eq!(key("Mod+Mod"), key("Mod")); |
| 1487 | |
| 1488 | // Duplicate modifiers are fine with non-modifier triggers. |
| 1489 | assert_eq!(key("Alt+Alt+Q"), key("Alt+Q")); |
| 1490 | assert_eq!(key("Mod+Mod+Q"), key("Mod+Q")); |
| 1491 | |
| 1492 | // Still works if spelled differently. |
| 1493 | assert_eq!(key("Ctrl+Control+Q"), key("Ctrl+Q")); |
| 1494 | assert_eq!(key("Super+Win+Q"), key("Super+Q")); |
| 1495 | } |
| 1496 | |
| 1497 | #[test] |
| 1498 | fn parse_modifier_trigger_includes_implied_modifier() { |
| 1499 | // The modifier that the trigger implies is part of the key. |
| 1500 | assert_eq!( |
| 1501 | "Alt_L".parse::<Key>().unwrap(), |
| 1502 | Key { |
| 1503 | trigger: Trigger::Keysym(Keysym::Alt_L), |
| 1504 | modifiers: Modifiers::ALT, |
| 1505 | }, |
| 1506 | ); |
| 1507 | assert_eq!( |
| 1508 | "Alt+Alt".parse::<Key>().unwrap(), |
| 1509 | Key { |
| 1510 | trigger: Trigger::Modifier(ModKey::Alt), |
| 1511 | modifiers: Modifiers::ALT, |
| 1512 | }, |
| 1513 | ); |
| 1514 | // `Mod` is only resolved at runtime, so no trigger implies it. |
| 1515 | assert_eq!( |
| 1516 | "Alt+Mod".parse::<Key>().unwrap(), |
| 1517 | Key { |
| 1518 | trigger: Trigger::CompositorMod, |
| 1519 | modifiers: Modifiers::ALT | Modifiers::COMPOSITOR, |
| 1520 | }, |
| 1521 | ); |
| 1522 | } |
| 1523 | |
| 1524 | #[test] |
| 1525 | fn key_conflicts() { |
| 1526 | let key = |text: &str| text.parse::<Key>().unwrap(); |
| 1527 | |
| 1528 | // Alt_L and Alt_R conflict with Alt, and vice versa. |
| 1529 | let alt = key("Alt"); |
| 1530 | let alt_l = key("Alt_L"); |
| 1531 | let alt_r = key("Alt_R"); |
| 1532 | assert!(alt.conflicts_with(&alt_l)); |
| 1533 | assert!(alt.conflicts_with(&alt_r)); |
| 1534 | assert!(alt_l.conflicts_with(&alt)); |
| 1535 | assert!(alt_r.conflicts_with(&alt)); |
| 1536 | |
| 1537 | // The left and right keysyms are different keys. |
| 1538 | assert!(!alt_l.conflicts_with(&alt_r)); |
| 1539 | |
| 1540 | // Different modifiers mean different binds, even for the same trigger. |
| 1541 | assert!(!alt.conflicts_with(&key("Ctrl+Alt_L"))); |
| 1542 | |
| 1543 | // Non-overlapping triggers don't conflict, even with the same modifiers. |
| 1544 | assert!(!key("Mod+Q").conflicts_with(&key("Mod+L"))); |
| 1545 | assert!(!key("Q").conflicts_with(&key("Ctrl"))); |
| 1546 | |
| 1547 | // Identical keys are duplicates. |
| 1548 | assert!(key("Q").conflicts_with(&key("Q"))); |
| 1549 | assert!(alt.conflicts_with(&alt)); |
| 1550 | |
| 1551 | // The compositor mod key is only resolved at runtime, so `Mod` isn't compared against the |
| 1552 | // mod key's own spellings. |
| 1553 | let compositor_mod = key("Mod"); |
| 1554 | assert!(!compositor_mod.conflicts_with(&key("Super_L"))); |
| 1555 | assert!(!compositor_mod.conflicts_with(&key("Super"))); |
| 1556 | |
| 1557 | // Generic-modifier-only keys are order-independent. |
| 1558 | assert!(key("Ctrl+Alt").conflicts_with(&key("Alt+Ctrl"))); |
| 1559 | assert!(key("Mod+Shift+Alt").conflicts_with(&key("Shift+Alt+Mod"))); |
| 1560 | assert!(key("Ctrl+Alt").conflicts_with(&key("Alt+Control_L"))); |
| 1561 | assert!(key("Ctrl+Alt_L").conflicts_with(&key("Alt+Ctrl"))); |
| 1562 | |
| 1563 | assert!(!key("Ctrl+Alt").conflicts_with(&key("Ctrl+Alt+A"))); |
| 1564 | } |
| 1565 | |
| 1566 | #[test] |
| 1567 | fn null_byte() { |
| 1568 | // Shouldn't panic. |
| 1569 | let _ = "W\0".parse::<Key>(); |
| 1570 | } |
| 1571 | } |