Handle modifiers even when consumed by a11y
Browse files at this commitf46338c18b77d9b4d1e0a30e8a29c84defc82a2dParent: 07c166b2 changed files +57 -15
- src/dbus/freedesktop_a11y.rs +29 -7
- src/input/mod.rs +28 -8
src/dbus/freedesktop_a11y.rsmodified+29 -7
| @@ -44,6 +44,17 @@ pub struct KeyboardMonitor { | ||
| 44 | 44 | iface: Arc<OnceLock<InterfaceRef<Self>>>, |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | +/// Keyboard monitor key block reason. | |
| 48 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 49 | +pub enum KbMonBlock { | |
| 50 | + /// Not blocked. | |
| 51 | + Pass, | |
| 52 | + /// Blocked, and this is the first press/release of the a11y modifier. | |
| 53 | + ModifierFirstPress, | |
| 54 | + /// Blocked, and this is not the a11y modifier. | |
| 55 | + Block, | |
| 56 | +} | |
| 57 | + | |
| 47 | 58 | /// Interface for monitoring of keyboard input by assistive technologies. |
| 48 | 59 | /// |
| 49 | 60 | /// This interface is used by assistive technologies to monitor keyboard input of the compositor. |
| @@ -211,7 +222,7 @@ impl KeyboardMonitor { | ||
| 211 | 222 | mods: u32, |
| 212 | 223 | keysym: Keysym, |
| 213 | 224 | unichar: u32, |
| 214 | - ) -> bool { | |
| 225 | + ) -> KbMonBlock { | |
| 215 | 226 | let _span = tracy_client::span!("KeyboardMonitor::process_key"); |
| 216 | 227 | |
| 217 | 228 | let mut ctxt = self.iface.get().unwrap().signal_emitter().clone(); |
| @@ -250,7 +261,7 @@ impl KeyboardMonitor { | ||
| 250 | 261 | // second press that got handled normally. |
| 251 | 262 | if !data.suppressed_keys.contains(&keysym) { |
| 252 | 263 | trace!("handling release for second press of grabbed modifier: {keysym:?}"); |
| 253 | - return false; | |
| 264 | + return KbMonBlock::Pass; | |
| 254 | 265 | } |
| 255 | 266 | } else { |
| 256 | 267 | let last_press_entry = data |
| @@ -263,7 +274,7 @@ impl KeyboardMonitor { | ||
| 263 | 274 | // Modifier pressed twice; handle it as normal. |
| 264 | 275 | if time <= last_press.saturating_add(repeat_delay) { |
| 265 | 276 | trace!("handling second press of grabbed modifier: {keysym:?}"); |
| 266 | - return false; | |
| 277 | + return KbMonBlock::Pass; | |
| 267 | 278 | } |
| 268 | 279 | } |
| 269 | 280 | } |
| @@ -293,7 +304,13 @@ impl KeyboardMonitor { | ||
| 293 | 304 | } |
| 294 | 305 | } |
| 295 | 306 | |
| 296 | - block | |
| 307 | + if !block { | |
| 308 | + KbMonBlock::Pass | |
| 309 | + } else if data.grabbed_mods.contains(&keysym) { | |
| 310 | + KbMonBlock::ModifierFirstPress | |
| 311 | + } else { | |
| 312 | + KbMonBlock::Block | |
| 313 | + } | |
| 297 | 314 | } |
| 298 | 315 | } |
| 299 | 316 | |
| @@ -430,9 +447,14 @@ impl Start for KeyboardMonitor { | ||
| 430 | 447 | } |
| 431 | 448 | |
| 432 | 449 | impl State { |
| 433 | - pub fn a11y_process_key(&mut self, time: Duration, keycode: Keycode, state: KeyState) -> bool { | |
| 450 | + pub fn a11y_process_key( | |
| 451 | + &mut self, | |
| 452 | + time: Duration, | |
| 453 | + keycode: Keycode, | |
| 454 | + state: KeyState, | |
| 455 | + ) -> KbMonBlock { | |
| 434 | 456 | if self.niri.a11y_keyboard_monitor.is_none() { |
| 435 | - return false; | |
| 457 | + return KbMonBlock::Pass; | |
| 436 | 458 | } |
| 437 | 459 | |
| 438 | 460 | let keyboard = self.niri.seat.get_keyboard().unwrap(); |
| @@ -454,7 +476,7 @@ impl State { | ||
| 454 | 476 | let released = state == KeyState::Released; |
| 455 | 477 | |
| 456 | 478 | let Some(monitor) = &self.niri.a11y_keyboard_monitor else { |
| 457 | - return false; | |
| 479 | + return KbMonBlock::Pass; | |
| 458 | 480 | }; |
| 459 | 481 | monitor.process_key(repeat_delay, time, keycode, released, mods, keysym, unichar) |
| 460 | 482 | } |
src/input/mod.rsmodified+28 -8
| @@ -41,6 +41,8 @@ use touch_overview_grab::TouchOverviewGrab; | ||
| 41 | 41 | use self::move_grab::MoveGrab; |
| 42 | 42 | use self::resize_grab::ResizeGrab; |
| 43 | 43 | use self::spatial_movement_grab::SpatialMovementGrab; |
| 44 | +#[cfg(feature = "dbus")] | |
| 45 | +use crate::dbus::freedesktop_a11y::KbMonBlock; | |
| 44 | 46 | use crate::layout::scrolling::ScrollDirection; |
| 45 | 47 | use crate::layout::{ActivateWindow, LayoutElement as _}; |
| 46 | 48 | use crate::niri::{CastTarget, PointerVisibility, State}; |
| @@ -388,15 +390,26 @@ impl State { | ||
| 388 | 390 | |
| 389 | 391 | // Accessibility modifier grabs should override XKB state changes (e.g. Caps Lock), so we |
| 390 | 392 | // need to process them before keyboard.input() below. |
| 393 | + // | |
| 394 | + // Other accessibility-grabbed keys should still update our XKB state, but not cause any | |
| 395 | + // other changes. | |
| 391 | 396 | #[cfg(feature = "dbus")] |
| 392 | - if self.a11y_process_key( | |
| 393 | - Duration::from_millis(u64::from(time)), | |
| 394 | - event.key_code(), | |
| 395 | - event.state(), | |
| 396 | - ) { | |
| 397 | - *consumed_by_a11y = true; | |
| 398 | - return; | |
| 399 | - } | |
| 397 | + let block = { | |
| 398 | + let block = self.a11y_process_key( | |
| 399 | + Duration::from_millis(u64::from(time)), | |
| 400 | + event.key_code(), | |
| 401 | + event.state(), | |
| 402 | + ); | |
| 403 | + if block != KbMonBlock::Pass { | |
| 404 | + *consumed_by_a11y = true; | |
| 405 | + } | |
| 406 | + // The accessibility modifier first press must not change XKB state, so we return | |
| 407 | + // early here. | |
| 408 | + if block == KbMonBlock::ModifierFirstPress { | |
| 409 | + return; | |
| 410 | + } | |
| 411 | + block | |
| 412 | + }; | |
| 400 | 413 | |
| 401 | 414 | let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( |
| 402 | 415 | self, |
| @@ -405,6 +418,13 @@ impl State { | ||
| 405 | 418 | serial, |
| 406 | 419 | time, |
| 407 | 420 | |this, mods, keysym| { |
| 421 | + // After updating XKB state from accessibility-grabbed keys, return right away and | |
| 422 | + // don't handle them. | |
| 423 | + #[cfg(feature = "dbus")] | |
| 424 | + if block != KbMonBlock::Pass { | |
| 425 | + return FilterResult::Intercept(None); | |
| 426 | + } | |
| 427 | + | |
| 408 | 428 | let key_code = event.key_code(); |
| 409 | 429 | let modified = keysym.modified_sym(); |
| 410 | 430 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); |