From e337dea201518b621b1235bf56cce97c09ecd595 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Thu, 24 Oct 2024 21:38:24 +0300 Subject: [PATCH] Add with_grab() and downcasting to KeyboardHandle Same deal, lets the compositor inspect the grab should it need it. --- src/input/keyboard/mod.rs | 15 ++++++++++++++- src/wayland/input_method/mod.rs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/input/keyboard/mod.rs b/src/input/keyboard/mod.rs index 919278ac9785..2809a2d8dabc 100644 --- a/src/input/keyboard/mod.rs +++ b/src/input/keyboard/mod.rs @@ -2,6 +2,7 @@ use crate::backend::input::KeyState; use crate::utils::{IsAlive, Serial, SERIAL_COUNTER}; +use downcast_rs::{impl_downcast, Downcast}; use std::collections::HashSet; #[cfg(feature = "wayland_frontend")] use std::sync::RwLock; @@ -590,7 +591,7 @@ impl Clone for GrabStartData { /// When your grab ends (either as you requested it or if it was forcefully cancelled by the server), /// the struct implementing this trait will be dropped. As such you should put clean-up logic in the destructor, /// rather than trying to guess when the grab will end. -pub trait KeyboardGrab { +pub trait KeyboardGrab: Downcast { /// An input was reported. /// /// `modifiers` are only passed when their state actually changes. The modifier must be @@ -623,6 +624,8 @@ pub trait KeyboardGrab { fn unset(&mut self, data: &mut D); } +impl_downcast!(KeyboardGrab where D: SeatHandler); + /// An handle to a keyboard handler /// /// It can be cloned and all clones manipulate the same internal state. @@ -918,6 +921,16 @@ impl KeyboardHandle { } } + /// Calls `f` with the active grab, if any. + pub fn with_grab(&self, f: impl FnOnce(Serial, &dyn KeyboardGrab) -> T) -> Option { + let guard = self.arc.internal.lock().unwrap(); + if let GrabStatus::Active(s, g) = &guard.grab { + Some(f(*s, &**g)) + } else { + None + } + } + /// Handle a keystroke /// /// All keystrokes from the input backend should be fed _in order_ to this method of the diff --git a/src/wayland/input_method/mod.rs b/src/wayland/input_method/mod.rs index dcfddc973673..533ce3558cbd 100644 --- a/src/wayland/input_method/mod.rs +++ b/src/wayland/input_method/mod.rs @@ -81,7 +81,7 @@ use crate::{ }; pub use input_method_handle::{InputMethodHandle, InputMethodUserData}; -pub use input_method_keyboard_grab::InputMethodKeyboardUserData; +pub use input_method_keyboard_grab::{InputMethodKeyboardGrab, InputMethodKeyboardUserData}; pub use input_method_popup_surface::InputMethodPopupSurfaceUserData; use super::text_input::TextInputHandle;