input: use an enum for the keyboard intercept result
Browse files at this commitfc3d54d9dcb62ccf781c793cc21faa5341c062aaParent: 1b16c761 changed file +49 -26
- src/input/mod.rs +49 -26
src/input/mod.rsmodified+49 -26
| @@ -19,9 +19,7 @@ use smithay::backend::input::{ | |||
| 19 | }; | 19 | }; |
| 20 | use smithay::backend::libinput::LibinputInputBackend; | 20 | use smithay::backend::libinput::LibinputInputBackend; |
| 21 | use smithay::input::dnd::DnDGrab; | 21 | use smithay::input::dnd::DnDGrab; |
| 22 | -use smithay::input::keyboard::{ | 22 | +use smithay::input::keyboard::{keysyms, KeyboardSource, Keysym, Layout, ModifiersState}; |
| 23 | - keysyms, FilterResult, KeyboardSource, Keysym, Layout, ModifiersState, | ||
| 24 | -}; | ||
| 25 | use smithay::input::pointer::{ | 23 | use smithay::input::pointer::{ |
| 26 | AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, Focus, GestureHoldBeginEvent, | 24 | AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, Focus, GestureHoldBeginEvent, |
| 27 | GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, | 25 | GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, |
| @@ -134,6 +132,17 @@ impl<D: SeatHandler + TabletSeatHandler> AnyStartData<D> { | |||
| 134 | } | 132 | } |
| 135 | } | 133 | } |
| 136 | 134 | ||
| 135 | +/// What to do with a key event that the keyboard filter looked at. | ||
| 136 | +#[derive(Debug)] | ||
| 137 | +enum ShouldInterceptResult { | ||
| 138 | + /// Forward the event to the focused client. | ||
| 139 | + Forward, | ||
| 140 | + /// Do not forward the event, and handle the given bind. | ||
| 141 | + InterceptAndHandle(Bind), | ||
| 142 | + /// Do not forward the event, and do nothing else. | ||
| 143 | + InterceptOnly, | ||
| 144 | +} | ||
| 145 | + | ||
| 137 | impl State { | 146 | impl State { |
| 138 | pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>) | 147 | pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>) |
| 139 | where | 148 | where |
| @@ -490,13 +499,15 @@ impl State { | |||
| 490 | #[cfg(not(feature = "dbus"))] | 499 | #[cfg(not(feature = "dbus"))] |
| 491 | let _ = consumed_by_a11y; | 500 | let _ = consumed_by_a11y; |
| 492 | 501 | ||
| 493 | - let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input_from_source( | 502 | + // We can't use smithay's `input` here because we want to forward _and_ handle |
| 503 | + // events for modifier keys. This lets us do things like bind Control_L to an | ||
| 504 | + // action without confusing applications about its state. | ||
| 505 | + let keyboard = self.niri.seat.get_keyboard().unwrap(); | ||
| 506 | + let Some((result, mods_changed)) = keyboard.input_intercept( | ||
| 494 | source, | 507 | source, |
| 495 | self, | 508 | self, |
| 496 | event.key_code(), | 509 | event.key_code(), |
| 497 | event.state(), | 510 | event.state(), |
| 498 | - serial, | ||
| 499 | - time, | ||
| 500 | |this, mods, keysym| { | 511 | |this, mods, keysym| { |
| 501 | let key_code = event.key_code(); | 512 | let key_code = event.key_code(); |
| 502 | let modified = keysym.modified_sym(); | 513 | let modified = keysym.modified_sym(); |
| @@ -535,9 +546,9 @@ impl State { | |||
| 535 | | Keysym::Alt_R | 546 | | Keysym::Alt_R |
| 536 | ) | 547 | ) |
| 537 | { | 548 | { |
| 538 | - return FilterResult::Forward; | 549 | + return ShouldInterceptResult::Forward; |
| 539 | } else { | 550 | } else { |
| 540 | - return FilterResult::Intercept(None); | 551 | + return ShouldInterceptResult::InterceptOnly; |
| 541 | } | 552 | } |
| 542 | } | 553 | } |
| 543 | 554 | ||
| @@ -549,7 +560,7 @@ impl State { | |||
| 549 | 560 | ||
| 550 | // Don't send this press to any clients. | 561 | // Don't send this press to any clients. |
| 551 | this.niri.suppressed_keys.insert(key_code); | 562 | this.niri.suppressed_keys.insert(key_code); |
| 552 | - return FilterResult::Intercept(None); | 563 | + return ShouldInterceptResult::InterceptOnly; |
| 553 | } | 564 | } |
| 554 | 565 | ||
| 555 | // Check if all modifiers were released while the MRU UI was open. If so, close the | 566 | // Check if all modifiers were released while the MRU UI was open. If so, close the |
| @@ -558,9 +569,9 @@ impl State { | |||
| 558 | this.do_action(Action::MruConfirm, false); | 569 | this.do_action(Action::MruConfirm, false); |
| 559 | 570 | ||
| 560 | if this.niri.suppressed_keys.remove(&key_code) { | 571 | if this.niri.suppressed_keys.remove(&key_code) { |
| 561 | - return FilterResult::Intercept(None); | 572 | + return ShouldInterceptResult::InterceptOnly; |
| 562 | } else { | 573 | } else { |
| 563 | - return FilterResult::Forward; | 574 | + return ShouldInterceptResult::Forward; |
| 564 | } | 575 | } |
| 565 | } | 576 | } |
| 566 | 577 | ||
| @@ -573,7 +584,7 @@ impl State { | |||
| 573 | { | 584 | { |
| 574 | pointer.unset_grab(this, serial, time); | 585 | pointer.unset_grab(this, serial, time); |
| 575 | this.niri.suppressed_keys.insert(key_code); | 586 | this.niri.suppressed_keys.insert(key_code); |
| 576 | - return FilterResult::Intercept(None); | 587 | + return ShouldInterceptResult::InterceptOnly; |
| 577 | } | 588 | } |
| 578 | } | 589 | } |
| 579 | 590 | ||
| @@ -601,13 +612,13 @@ impl State { | |||
| 601 | ) | 612 | ) |
| 602 | }; | 613 | }; |
| 603 | 614 | ||
| 604 | - if matches!(res, FilterResult::Forward) { | 615 | + if matches!(res, ShouldInterceptResult::Forward) { |
| 605 | // If we didn't find any bind, try other hardcoded keys. | 616 | // If we didn't find any bind, try other hardcoded keys. |
| 606 | if this.niri.keyboard_focus.is_overview() && pressed { | 617 | if this.niri.keyboard_focus.is_overview() && pressed { |
| 607 | if let Some(bind) = raw.and_then(|raw| hardcoded_overview_bind(raw, *mods)) | 618 | if let Some(bind) = raw.and_then(|raw| hardcoded_overview_bind(raw, *mods)) |
| 608 | { | 619 | { |
| 609 | this.niri.suppressed_keys.insert(key_code); | 620 | this.niri.suppressed_keys.insert(key_code); |
| 610 | - return FilterResult::Intercept(Some(bind)); | 621 | + return ShouldInterceptResult::InterceptAndHandle(bind); |
| 611 | } | 622 | } |
| 612 | } | 623 | } |
| 613 | 624 | ||
| @@ -622,13 +633,25 @@ impl State { | |||
| 622 | return; | 633 | return; |
| 623 | }; | 634 | }; |
| 624 | 635 | ||
| 625 | - if !pressed { | 636 | + match result { |
| 626 | - return; | 637 | + ShouldInterceptResult::Forward => { |
| 638 | + keyboard.input_forward( | ||
| 639 | + self, | ||
| 640 | + event.key_code(), | ||
| 641 | + event.state(), | ||
| 642 | + serial, | ||
| 643 | + time, | ||
| 644 | + mods_changed, | ||
| 645 | + ); | ||
| 646 | + } | ||
| 647 | + ShouldInterceptResult::InterceptAndHandle(bind) => { | ||
| 648 | + if pressed { | ||
| 649 | + self.handle_bind(bind.clone()); | ||
| 650 | + self.start_key_repeat(bind); | ||
| 651 | + } | ||
| 652 | + } | ||
| 653 | + ShouldInterceptResult::InterceptOnly => {} | ||
| 627 | } | 654 | } |
| 628 | - | ||
| 629 | - self.handle_bind(bind.clone()); | ||
| 630 | - | ||
| 631 | - self.start_key_repeat(bind); | ||
| 632 | } | 655 | } |
| 633 | 656 | ||
| 634 | fn start_key_repeat(&mut self, bind: Bind) { | 657 | fn start_key_repeat(&mut self, bind: Bind) { |
| @@ -4610,12 +4633,12 @@ fn should_intercept_key<'a>( | |||
| 4610 | screenshot_ui: &ScreenshotUi, | 4633 | screenshot_ui: &ScreenshotUi, |
| 4611 | disable_power_key_handling: bool, | 4634 | disable_power_key_handling: bool, |
| 4612 | is_inhibiting_shortcuts: bool, | 4635 | is_inhibiting_shortcuts: bool, |
| 4613 | -) -> FilterResult<Option<Bind>> { | 4636 | +) -> ShouldInterceptResult { |
| 4614 | // Actions are only triggered on presses, release of the key | 4637 | // Actions are only triggered on presses, release of the key |
| 4615 | // shouldn't try to intercept anything unless we have marked | 4638 | // shouldn't try to intercept anything unless we have marked |
| 4616 | // the key to suppress. | 4639 | // the key to suppress. |
| 4617 | if !pressed && !suppressed_keys.contains(&key_code) { | 4640 | if !pressed && !suppressed_keys.contains(&key_code) { |
| 4618 | - return FilterResult::Forward; | 4641 | + return ShouldInterceptResult::Forward; |
| 4619 | } | 4642 | } |
| 4620 | 4643 | ||
| 4621 | let mut final_bind = find_bind( | 4644 | let mut final_bind = find_bind( |
| @@ -4663,10 +4686,10 @@ fn should_intercept_key<'a>( | |||
| 4663 | match (final_bind, pressed) { | 4686 | match (final_bind, pressed) { |
| 4664 | (Some(bind), true) => { | 4687 | (Some(bind), true) => { |
| 4665 | if is_inhibiting_shortcuts && bind.allow_inhibiting { | 4688 | if is_inhibiting_shortcuts && bind.allow_inhibiting { |
| 4666 | - FilterResult::Forward | 4689 | + ShouldInterceptResult::Forward |
| 4667 | } else { | 4690 | } else { |
| 4668 | suppressed_keys.insert(key_code); | 4691 | suppressed_keys.insert(key_code); |
| 4669 | - FilterResult::Intercept(Some(bind)) | 4692 | + ShouldInterceptResult::InterceptAndHandle(bind) |
| 4670 | } | 4693 | } |
| 4671 | } | 4694 | } |
| 4672 | (_, false) => { | 4695 | (_, false) => { |
| @@ -4676,9 +4699,9 @@ fn should_intercept_key<'a>( | |||
| 4676 | // if it was inhibited on press (forwarded to the client), it wouldn't be suppressed, | 4699 | // if it was inhibited on press (forwarded to the client), it wouldn't be suppressed, |
| 4677 | // so the release would already have been forwarded at the start of this function. | 4700 | // so the release would already have been forwarded at the start of this function. |
| 4678 | suppressed_keys.remove(&key_code); | 4701 | suppressed_keys.remove(&key_code); |
| 4679 | - FilterResult::Intercept(None) | 4702 | + ShouldInterceptResult::InterceptOnly |
| 4680 | } | 4703 | } |
| 4681 | - (None, true) => FilterResult::Forward, | 4704 | + (None, true) => ShouldInterceptResult::Forward, |
| 4682 | } | 4705 | } |
| 4683 | } | 4706 | } |
| 4684 | 4707 | ||