forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBe1971c4Ivan Molodetskikh2 years ago493 commits

input.rs1471 lines, 52.0 KiBHistoryRawDownload
1use std::any::Any;
2use std::collections::HashSet;
3
4use niri_config::{Action, Binds, LayoutAction, Modifiers};
5use smithay::backend::input::{
6 AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event,
7 GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _,
8 InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent,
9 PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent,
10 TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState,
11};
12use smithay::backend::libinput::LibinputInputBackend;
13use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState};
14use smithay::input::pointer::{
15 AxisFrame, ButtonEvent, CursorImageStatus, GestureHoldBeginEvent, GestureHoldEndEvent,
16 GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent,
17 GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent, RelativeMotionEvent,
18};
19use smithay::reexports::input;
20use smithay::utils::{Logical, Point, SERIAL_COUNTER};
21use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint};
22use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait};
23
24use crate::niri::State;
25use crate::screenshot_ui::ScreenshotUi;
26use crate::utils::{center, get_monotonic_time, spawn};
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum CompositorMod {
30 Super,
31 Alt,
32}
33
34#[derive(Debug, Clone, Copy, PartialEq)]
35pub struct TabletData {
36 pub aspect_ratio: f64,
37}
38
39impl State {
40 pub fn process_input_event<I: InputBackend>(&mut self, event: InputEvent<I>)
41 where
42 I::Device: 'static, // Needed for downcasting.
43 {
44 let _span = tracy_client::span!("process_input_event");
45
46 // A bit of a hack, but animation end runs some logic (i.e. workspace clean-up) and it
47 // doesn't always trigger due to damage, etc. So run it here right before it might prove
48 // important. Besides, animations affect the input, so it's best to have up-to-date values
49 // here.
50 self.niri.layout.advance_animations(get_monotonic_time());
51
52 // Power on monitors if they were off.
53 if should_activate_monitors(&event) {
54 self.niri.activate_monitors(&self.backend);
55 }
56
57 use InputEvent::*;
58 match event {
59 DeviceAdded { device } => self.on_device_added(device),
60 DeviceRemoved { device } => self.on_device_removed(device),
61 Keyboard { event } => self.on_keyboard::<I>(event),
62 PointerMotion { event } => self.on_pointer_motion::<I>(event),
63 PointerMotionAbsolute { event } => self.on_pointer_motion_absolute::<I>(event),
64 PointerButton { event } => self.on_pointer_button::<I>(event),
65 PointerAxis { event } => self.on_pointer_axis::<I>(event),
66 TabletToolAxis { event } => self.on_tablet_tool_axis::<I>(event),
67 TabletToolTip { event } => self.on_tablet_tool_tip::<I>(event),
68 TabletToolProximity { event } => self.on_tablet_tool_proximity::<I>(event),
69 TabletToolButton { event } => self.on_tablet_tool_button::<I>(event),
70 GestureSwipeBegin { event } => self.on_gesture_swipe_begin::<I>(event),
71 GestureSwipeUpdate { event } => self.on_gesture_swipe_update::<I>(event),
72 GestureSwipeEnd { event } => self.on_gesture_swipe_end::<I>(event),
73 GesturePinchBegin { event } => self.on_gesture_pinch_begin::<I>(event),
74 GesturePinchUpdate { event } => self.on_gesture_pinch_update::<I>(event),
75 GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event),
76 GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event),
77 GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event),
78 TouchDown { .. } => (),
79 TouchMotion { .. } => (),
80 TouchUp { .. } => (),
81 TouchCancel { .. } => (),
82 TouchFrame { .. } => (),
83 Special(_) => (),
84 }
85 }
86
87 pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) {
88 let _span = tracy_client::span!("process_libinput_event");
89
90 match event {
91 InputEvent::DeviceAdded { device } => {
92 // According to Mutter code, this setting is specific to touchpads.
93 let is_touchpad = device.config_tap_finger_count() > 0;
94 if is_touchpad {
95 let c = &self.niri.config.borrow().input.touchpad;
96 let _ = device.config_tap_set_enabled(c.tap);
97 let _ = device.config_dwt_set_enabled(c.dwt);
98 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
99 let _ = device.config_accel_set_speed(c.accel_speed);
100
101 if let Some(accel_profile) = c.accel_profile {
102 let _ = device.config_accel_set_profile(accel_profile.into());
103 }
104 }
105
106 if device.has_capability(input::DeviceCapability::TabletTool) {
107 match device.size() {
108 Some((w, h)) => {
109 let aspect_ratio = w / h;
110 let data = TabletData { aspect_ratio };
111 self.niri.tablets.insert(device.clone(), data);
112 }
113 None => {
114 warn!("tablet tool device has no size");
115 }
116 }
117 }
118 }
119 InputEvent::DeviceRemoved { device } => {
120 self.niri.tablets.remove(device);
121 }
122 _ => (),
123 }
124 }
125
126 fn on_device_added(&mut self, device: impl Device) {
127 if device.has_capability(DeviceCapability::TabletTool) {
128 let tablet_seat = self.niri.seat.tablet_seat();
129
130 let desc = TabletDescriptor::from(&device);
131 tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc);
132 }
133 }
134
135 fn on_device_removed(&mut self, device: impl Device) {
136 if device.has_capability(DeviceCapability::TabletTool) {
137 let tablet_seat = self.niri.seat.tablet_seat();
138
139 let desc = TabletDescriptor::from(&device);
140 tablet_seat.remove_tablet(&desc);
141
142 // If there are no tablets in seat we can remove all tools
143 if tablet_seat.count_tablets() == 0 {
144 tablet_seat.clear_tools();
145 }
146 }
147 }
148
149 /// Computes the cursor position for the tablet event.
150 ///
151 /// This function handles the tablet output mapping, as well as coordinate clamping and aspect
152 /// ratio correction.
153 fn compute_tablet_position<I: InputBackend>(
154 &self,
155 event: &(impl Event<I> + TabletToolEvent<I>),
156 ) -> Option<Point<f64, Logical>>
157 where
158 I::Device: 'static,
159 {
160 let output = self.niri.output_for_tablet()?;
161 let output_geo = self.niri.global_space.output_geometry(output).unwrap();
162
163 let mut pos = event.position_transformed(output_geo.size);
164 pos.x /= output_geo.size.w as f64;
165 pos.y /= output_geo.size.h as f64;
166
167 let device = event.device();
168 if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() {
169 if let Some(data) = self.niri.tablets.get(device) {
170 // This code does the same thing as mutter with "keep aspect ratio" enabled.
171 let output_aspect_ratio = output_geo.size.w as f64 / output_geo.size.h as f64;
172 let ratio = data.aspect_ratio / output_aspect_ratio;
173
174 if ratio > 1. {
175 pos.x *= ratio;
176 } else {
177 pos.y /= ratio;
178 }
179 }
180 };
181
182 pos.x *= output_geo.size.w as f64;
183 pos.y *= output_geo.size.h as f64;
184 pos.x = pos.x.clamp(0.0, output_geo.size.w as f64 - 1.);
185 pos.y = pos.y.clamp(0.0, output_geo.size.h as f64 - 1.);
186 Some(pos + output_geo.loc.to_f64())
187 }
188
189 fn on_keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) {
190 let comp_mod = self.backend.mod_key();
191
192 let serial = SERIAL_COUNTER.next_serial();
193 let time = Event::time_msec(&event);
194 let pressed = event.state() == KeyState::Pressed;
195
196 let Some(Some(action)) = self.niri.seat.get_keyboard().unwrap().input(
197 self,
198 event.key_code(),
199 event.state(),
200 serial,
201 time,
202 |this, mods, keysym| {
203 let bindings = &this.niri.config.borrow().binds;
204 let key_code = event.key_code();
205 let modified = keysym.modified_sym();
206 let raw = keysym.raw_latin_sym_or_raw_current_sym();
207 should_intercept_key(
208 &mut this.niri.suppressed_keys,
209 bindings,
210 comp_mod,
211 key_code,
212 modified,
213 raw,
214 pressed,
215 *mods,
216 &this.niri.screenshot_ui,
217 this.niri.config.borrow().input.disable_power_key_handling,
218 )
219 },
220 ) else {
221 return;
222 };
223
224 // Filter actions when the key is released or the session is locked.
225 if !pressed {
226 return;
227 }
228
229 if self.niri.is_locked() && !allowed_when_locked(&action) {
230 return;
231 }
232
233 match action {
234 Action::Quit => {
235 info!("quitting because quit bind was pressed");
236 self.niri.stop_signal.stop()
237 }
238 Action::ChangeVt(vt) => {
239 self.backend.change_vt(vt);
240 // Changing `VT` may not deliver the key releases, so clear the state.
241 self.niri.suppressed_keys.clear();
242 }
243 Action::Suspend => {
244 self.backend.suspend();
245 // Suspend may not deliver the key releases, so clear the state.
246 self.niri.suppressed_keys.clear();
247 }
248 Action::PowerOffMonitors => {
249 self.niri.deactivate_monitors(&self.backend);
250 }
251 Action::ToggleDebugTint => {
252 self.backend.toggle_debug_tint();
253 self.niri.queue_redraw_all();
254 }
255 Action::Spawn(command) => {
256 spawn(command);
257 }
258 Action::ScreenshotScreen => {
259 let active = self.niri.layout.active_output().cloned();
260 if let Some(active) = active {
261 self.backend.with_primary_renderer(|renderer| {
262 if let Err(err) = self.niri.screenshot(renderer, &active) {
263 warn!("error taking screenshot: {err:?}");
264 }
265 });
266 }
267 }
268 Action::ConfirmScreenshot => {
269 self.backend.with_primary_renderer(|renderer| {
270 match self.niri.screenshot_ui.capture(renderer) {
271 Ok((size, pixels)) => {
272 if let Err(err) = self.niri.save_screenshot(size, pixels) {
273 warn!("error saving screenshot: {err:?}");
274 }
275 }
276 Err(err) => {
277 warn!("error capturing screenshot: {err:?}");
278 }
279 }
280 });
281
282 self.niri.screenshot_ui.close();
283 self.niri
284 .cursor_manager
285 .set_cursor_image(CursorImageStatus::default_named());
286 self.niri.queue_redraw_all();
287 }
288 Action::CancelScreenshot => {
289 self.niri.screenshot_ui.close();
290 self.niri
291 .cursor_manager
292 .set_cursor_image(CursorImageStatus::default_named());
293 self.niri.queue_redraw_all();
294 }
295 Action::Screenshot => {
296 self.backend.with_primary_renderer(|renderer| {
297 self.niri.open_screenshot_ui(renderer);
298 });
299 }
300 Action::ScreenshotWindow => {
301 let active = self.niri.layout.active_window();
302 if let Some((window, output)) = active {
303 self.backend.with_primary_renderer(|renderer| {
304 if let Err(err) = self.niri.screenshot_window(renderer, output, window) {
305 warn!("error taking screenshot: {err:?}");
306 }
307 });
308 }
309 }
310 Action::CloseWindow => {
311 if let Some(window) = self.niri.layout.focus() {
312 window.toplevel().send_close();
313 }
314 }
315 Action::FullscreenWindow => {
316 let focus = self.niri.layout.focus().cloned();
317 if let Some(window) = focus {
318 self.niri.layout.toggle_fullscreen(&window);
319 }
320 }
321 Action::SwitchLayout(action) => {
322 self.niri.seat.get_keyboard().unwrap().with_xkb_state(
323 self,
324 |mut state| match action {
325 LayoutAction::Next => state.cycle_next_layout(),
326 LayoutAction::Prev => state.cycle_prev_layout(),
327 },
328 );
329 }
330 Action::MoveColumnLeft => {
331 self.niri.layout.move_left();
332 // FIXME: granular
333 self.niri.queue_redraw_all();
334 }
335 Action::MoveColumnRight => {
336 self.niri.layout.move_right();
337 // FIXME: granular
338 self.niri.queue_redraw_all();
339 }
340 Action::MoveColumnToFirst => {
341 self.niri.layout.move_column_to_first();
342 // FIXME: granular
343 self.niri.queue_redraw_all();
344 }
345 Action::MoveColumnToLast => {
346 self.niri.layout.move_column_to_last();
347 // FIXME: granular
348 self.niri.queue_redraw_all();
349 }
350 Action::MoveWindowDown => {
351 self.niri.layout.move_down();
352 // FIXME: granular
353 self.niri.queue_redraw_all();
354 }
355 Action::MoveWindowUp => {
356 self.niri.layout.move_up();
357 // FIXME: granular
358 self.niri.queue_redraw_all();
359 }
360 Action::MoveWindowDownOrToWorkspaceDown => {
361 self.niri.layout.move_down_or_to_workspace_down();
362 // FIXME: granular
363 self.niri.queue_redraw_all();
364 }
365 Action::MoveWindowUpOrToWorkspaceUp => {
366 self.niri.layout.move_up_or_to_workspace_up();
367 // FIXME: granular
368 self.niri.queue_redraw_all();
369 }
370 Action::FocusColumnLeft => {
371 self.niri.layout.focus_left();
372 }
373 Action::FocusColumnRight => {
374 self.niri.layout.focus_right();
375 }
376 Action::FocusColumnFirst => {
377 self.niri.layout.focus_column_first();
378 }
379 Action::FocusColumnLast => {
380 self.niri.layout.focus_column_last();
381 }
382 Action::FocusWindowDown => {
383 self.niri.layout.focus_down();
384 }
385 Action::FocusWindowUp => {
386 self.niri.layout.focus_up();
387 }
388 Action::FocusWindowOrWorkspaceDown => {
389 self.niri.layout.focus_window_or_workspace_down();
390 // FIXME: granular
391 self.niri.queue_redraw_all();
392 }
393 Action::FocusWindowOrWorkspaceUp => {
394 self.niri.layout.focus_window_or_workspace_up();
395 // FIXME: granular
396 self.niri.queue_redraw_all();
397 }
398 Action::MoveWindowToWorkspaceDown => {
399 self.niri.layout.move_to_workspace_down();
400 // FIXME: granular
401 self.niri.queue_redraw_all();
402 }
403 Action::MoveWindowToWorkspaceUp => {
404 self.niri.layout.move_to_workspace_up();
405 // FIXME: granular
406 self.niri.queue_redraw_all();
407 }
408 Action::MoveWindowToWorkspace(idx) => {
409 let idx = idx.saturating_sub(1) as usize;
410 self.niri.layout.move_to_workspace(idx);
411 // FIXME: granular
412 self.niri.queue_redraw_all();
413 }
414 Action::FocusWorkspaceDown => {
415 self.niri.layout.switch_workspace_down();
416 // FIXME: granular
417 self.niri.queue_redraw_all();
418 }
419 Action::FocusWorkspaceUp => {
420 self.niri.layout.switch_workspace_up();
421 // FIXME: granular
422 self.niri.queue_redraw_all();
423 }
424 Action::FocusWorkspace(idx) => {
425 let idx = idx.saturating_sub(1) as usize;
426 self.niri.layout.switch_workspace(idx);
427 // FIXME: granular
428 self.niri.queue_redraw_all();
429 }
430 Action::MoveWorkspaceDown => {
431 self.niri.layout.move_workspace_down();
432 // FIXME: granular
433 self.niri.queue_redraw_all();
434 }
435 Action::MoveWorkspaceUp => {
436 self.niri.layout.move_workspace_up();
437 // FIXME: granular
438 self.niri.queue_redraw_all();
439 }
440 Action::ConsumeWindowIntoColumn => {
441 self.niri.layout.consume_into_column();
442 // FIXME: granular
443 self.niri.queue_redraw_all();
444 }
445 Action::ExpelWindowFromColumn => {
446 self.niri.layout.expel_from_column();
447 // FIXME: granular
448 self.niri.queue_redraw_all();
449 }
450 Action::SwitchPresetColumnWidth => {
451 self.niri.layout.toggle_width();
452 }
453 Action::CenterColumn => {
454 self.niri.layout.center_column();
455 // FIXME: granular
456 self.niri.queue_redraw_all();
457 }
458 Action::MaximizeColumn => {
459 self.niri.layout.toggle_full_width();
460 }
461 Action::FocusMonitorLeft => {
462 if let Some(output) = self.niri.output_left() {
463 self.niri.layout.focus_output(&output);
464 self.move_cursor_to_output(&output);
465 }
466 }
467 Action::FocusMonitorRight => {
468 if let Some(output) = self.niri.output_right() {
469 self.niri.layout.focus_output(&output);
470 self.move_cursor_to_output(&output);
471 }
472 }
473 Action::FocusMonitorDown => {
474 if let Some(output) = self.niri.output_down() {
475 self.niri.layout.focus_output(&output);
476 self.move_cursor_to_output(&output);
477 }
478 }
479 Action::FocusMonitorUp => {
480 if let Some(output) = self.niri.output_up() {
481 self.niri.layout.focus_output(&output);
482 self.move_cursor_to_output(&output);
483 }
484 }
485 Action::MoveWindowToMonitorLeft => {
486 if let Some(output) = self.niri.output_left() {
487 self.niri.layout.move_to_output(&output);
488 self.move_cursor_to_output(&output);
489 }
490 }
491 Action::MoveWindowToMonitorRight => {
492 if let Some(output) = self.niri.output_right() {
493 self.niri.layout.move_to_output(&output);
494 self.move_cursor_to_output(&output);
495 }
496 }
497 Action::MoveWindowToMonitorDown => {
498 if let Some(output) = self.niri.output_down() {
499 self.niri.layout.move_to_output(&output);
500 self.move_cursor_to_output(&output);
501 }
502 }
503 Action::MoveWindowToMonitorUp => {
504 if let Some(output) = self.niri.output_up() {
505 self.niri.layout.move_to_output(&output);
506 self.move_cursor_to_output(&output);
507 }
508 }
509 Action::SetColumnWidth(change) => {
510 self.niri.layout.set_column_width(change);
511 }
512 Action::SetWindowHeight(change) => {
513 self.niri.layout.set_window_height(change);
514 }
515 }
516 }
517
518 fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) {
519 // We need an output to be able to move the pointer.
520 if self.niri.global_space.outputs().next().is_none() {
521 return;
522 }
523
524 let serial = SERIAL_COUNTER.next_serial();
525
526 let pointer = self.niri.seat.get_pointer().unwrap();
527
528 let pos = pointer.current_location();
529
530 // We have an output, so we can compute the new location and focus.
531 let mut new_pos = pos + event.delta();
532
533 // We received an event for the regular pointer, so show it now.
534 self.niri.tablet_cursor_location = None;
535
536 // Check if we have an active pointer constraint.
537 let mut pointer_confined = None;
538 if let Some(focus) = self.niri.pointer_focus.as_ref() {
539 let focus_surface_loc = focus.surface.1;
540 let pos_within_surface = pos.to_i32_round() - focus_surface_loc;
541
542 let mut pointer_locked = false;
543 with_pointer_constraint(&focus.surface.0, &pointer, |constraint| {
544 let Some(constraint) = constraint else { return };
545 if !constraint.is_active() {
546 return;
547 }
548
549 // Constraint does not apply if not within region.
550 if let Some(region) = constraint.region() {
551 if !region.contains(pos_within_surface) {
552 return;
553 }
554 }
555
556 match &*constraint {
557 PointerConstraint::Locked(_locked) => {
558 pointer_locked = true;
559 }
560 PointerConstraint::Confined(confine) => {
561 pointer_confined = Some((focus.surface.clone(), confine.region().cloned()));
562 }
563 }
564 });
565
566 // If the pointer is locked, only send relative motion.
567 if pointer_locked {
568 pointer.relative_motion(
569 self,
570 Some(focus.surface.clone()),
571 &RelativeMotionEvent {
572 delta: event.delta(),
573 delta_unaccel: event.delta_unaccel(),
574 utime: event.time(),
575 },
576 );
577
578 pointer.frame(self);
579
580 // I guess a redraw to hide the tablet cursor could be nice? Doesn't matter too
581 // much here I think.
582 return;
583 }
584 }
585
586 if self
587 .niri
588 .global_space
589 .output_under(new_pos)
590 .next()
591 .is_none()
592 {
593 // We ended up outside the outputs and need to clip the movement.
594 if let Some(output) = self.niri.global_space.output_under(pos).next() {
595 // The pointer was previously on some output. Clip the movement against its
596 // boundaries.
597 let geom = self.niri.global_space.output_geometry(output).unwrap();
598 new_pos.x = new_pos
599 .x
600 .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64);
601 new_pos.y = new_pos
602 .y
603 .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64);
604 } else {
605 // The pointer was not on any output in the first place. Find one for it.
606 // Let's do the simple thing and just put it on the first output.
607 let output = self.niri.global_space.outputs().next().unwrap();
608 let geom = self.niri.global_space.output_geometry(output).unwrap();
609 new_pos = center(geom).to_f64();
610 }
611 }
612
613 if let Some(output) = self.niri.screenshot_ui.selection_output() {
614 let geom = self.niri.global_space.output_geometry(output).unwrap();
615 let mut point = new_pos;
616 point.x = point
617 .x
618 .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64);
619 point.y = point
620 .y
621 .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64);
622 let point = (point - geom.loc.to_f64())
623 .to_physical(output.current_scale().fractional_scale())
624 .to_i32_round();
625 self.niri.screenshot_ui.pointer_motion(point);
626 }
627
628 let under = self.niri.surface_under_and_global_space(new_pos);
629
630 // Handle confined pointer.
631 if let Some((focus_surface, region)) = pointer_confined {
632 let mut prevent = false;
633
634 // Prevent the pointer from leaving the focused surface.
635 if Some(&focus_surface.0) != under.as_ref().map(|x| &x.surface.0) {
636 prevent = true;
637 }
638
639 // Prevent the pointer from leaving the confine region, if any.
640 if let Some(region) = region {
641 let new_pos_within_surface = new_pos.to_i32_round() - focus_surface.1;
642 if !region.contains(new_pos_within_surface) {
643 prevent = true;
644 }
645 }
646
647 if prevent {
648 pointer.relative_motion(
649 self,
650 Some(focus_surface),
651 &RelativeMotionEvent {
652 delta: event.delta(),
653 delta_unaccel: event.delta_unaccel(),
654 utime: event.time(),
655 },
656 );
657
658 pointer.frame(self);
659
660 return;
661 }
662 }
663
664 // Activate a new confinement if necessary.
665 self.niri.maybe_activate_pointer_constraint(new_pos, &under);
666
667 self.niri.pointer_focus = under.clone();
668 let under = under.map(|u| u.surface);
669
670 pointer.motion(
671 self,
672 under.clone(),
673 &MotionEvent {
674 location: new_pos,
675 serial,
676 time: event.time_msec(),
677 },
678 );
679
680 pointer.relative_motion(
681 self,
682 under,
683 &RelativeMotionEvent {
684 delta: event.delta(),
685 delta_unaccel: event.delta_unaccel(),
686 utime: event.time(),
687 },
688 );
689
690 pointer.frame(self);
691
692 // Redraw to update the cursor position.
693 // FIXME: redraw only outputs overlapping the cursor.
694 self.niri.queue_redraw_all();
695 }
696
697 fn on_pointer_motion_absolute<I: InputBackend>(
698 &mut self,
699 event: I::PointerMotionAbsoluteEvent,
700 ) {
701 let Some(output) = self.niri.global_space.outputs().next() else {
702 return;
703 };
704
705 let output_geo = self.niri.global_space.output_geometry(output).unwrap();
706
707 let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64();
708
709 let serial = SERIAL_COUNTER.next_serial();
710
711 let pointer = self.niri.seat.get_pointer().unwrap();
712
713 if let Some(output) = self.niri.screenshot_ui.selection_output() {
714 let geom = self.niri.global_space.output_geometry(output).unwrap();
715 let mut point = pos;
716 point.x = point
717 .x
718 .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64);
719 point.y = point
720 .y
721 .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64);
722 let point = (point - geom.loc.to_f64())
723 .to_physical(output.current_scale().fractional_scale())
724 .to_i32_round();
725 self.niri.screenshot_ui.pointer_motion(point);
726 }
727
728 let under = self.niri.surface_under_and_global_space(pos);
729 self.niri.maybe_activate_pointer_constraint(pos, &under);
730 self.niri.pointer_focus = under.clone();
731 let under = under.map(|u| u.surface);
732
733 pointer.motion(
734 self,
735 under,
736 &MotionEvent {
737 location: pos,
738 serial,
739 time: event.time_msec(),
740 },
741 );
742
743 pointer.frame(self);
744
745 // We moved the regular pointer, so show it now.
746 self.niri.tablet_cursor_location = None;
747
748 // Redraw to update the cursor position.
749 // FIXME: redraw only outputs overlapping the cursor.
750 self.niri.queue_redraw_all();
751 }
752
753 fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
754 let pointer = self.niri.seat.get_pointer().unwrap();
755
756 let serial = SERIAL_COUNTER.next_serial();
757
758 let button = event.button_code();
759
760 let button_state = event.state();
761
762 if ButtonState::Pressed == button_state && !pointer.is_grabbed() {
763 if let Some(window) = self.niri.window_under_cursor() {
764 let window = window.clone();
765 self.niri.layout.activate_window(&window);
766 } else if let Some(output) = self.niri.output_under_cursor() {
767 self.niri.layout.activate_output(&output);
768 }
769 };
770
771 self.update_pointer_focus();
772
773 if let Some(button) = event.button() {
774 let pos = pointer.current_location();
775 if let Some((output, _)) = self.niri.output_under(pos) {
776 let output = output.clone();
777 let geom = self.niri.global_space.output_geometry(&output).unwrap();
778 let mut point = pos;
779 // Re-clamp as pointer can be within 0.5 from the limit which will round up
780 // to a wrong value.
781 point.x = point
782 .x
783 .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64);
784 point.y = point
785 .y
786 .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64);
787 let point = (point - geom.loc.to_f64())
788 .to_physical(output.current_scale().fractional_scale())
789 .to_i32_round();
790 if self
791 .niri
792 .screenshot_ui
793 .pointer_button(output, point, button, button_state)
794 {
795 self.niri.queue_redraw_all();
796 }
797 }
798 }
799
800 pointer.button(
801 self,
802 &ButtonEvent {
803 button,
804 state: button_state,
805 serial,
806 time: event.time_msec(),
807 },
808 );
809 pointer.frame(self);
810 }
811
812 fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) {
813 let source = event.source();
814
815 let horizontal_amount = event
816 .amount(Axis::Horizontal)
817 .unwrap_or_else(|| event.amount_v120(Axis::Horizontal).unwrap_or(0.0) * 3.0 / 120.);
818 let vertical_amount = event
819 .amount(Axis::Vertical)
820 .unwrap_or_else(|| event.amount_v120(Axis::Vertical).unwrap_or(0.0) * 3.0 / 120.);
821 let horizontal_amount_discrete = event.amount_v120(Axis::Horizontal);
822 let vertical_amount_discrete = event.amount_v120(Axis::Vertical);
823
824 let mut frame = AxisFrame::new(event.time_msec()).source(source);
825 if horizontal_amount != 0.0 {
826 frame = frame
827 .relative_direction(Axis::Horizontal, event.relative_direction(Axis::Horizontal));
828 frame = frame.value(Axis::Horizontal, horizontal_amount);
829 if let Some(discrete) = horizontal_amount_discrete {
830 frame = frame.v120(Axis::Horizontal, discrete as i32);
831 }
832 }
833 if vertical_amount != 0.0 {
834 frame =
835 frame.relative_direction(Axis::Vertical, event.relative_direction(Axis::Vertical));
836 frame = frame.value(Axis::Vertical, vertical_amount);
837 if let Some(discrete) = vertical_amount_discrete {
838 frame = frame.v120(Axis::Vertical, discrete as i32);
839 }
840 }
841
842 if source == AxisSource::Finger {
843 if event.amount(Axis::Horizontal) == Some(0.0) {
844 frame = frame.stop(Axis::Horizontal);
845 }
846 if event.amount(Axis::Vertical) == Some(0.0) {
847 frame = frame.stop(Axis::Vertical);
848 }
849 }
850
851 self.update_pointer_focus();
852
853 let pointer = &self.niri.seat.get_pointer().unwrap();
854 pointer.axis(self, frame);
855 pointer.frame(self);
856 }
857
858 fn on_tablet_tool_axis<I: InputBackend>(&mut self, event: I::TabletToolAxisEvent)
859 where
860 I::Device: 'static, // Needed for downcasting.
861 {
862 let Some(pos) = self.compute_tablet_position(&event) else {
863 return;
864 };
865
866 let under = self.niri.surface_under_and_global_space(pos);
867 let under = under.map(|u| u.surface);
868
869 let tablet_seat = self.niri.seat.tablet_seat();
870 let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device()));
871 let tool = tablet_seat.get_tool(&event.tool());
872 if let (Some(tablet), Some(tool)) = (tablet, tool) {
873 if event.pressure_has_changed() {
874 tool.pressure(event.pressure());
875 }
876 if event.distance_has_changed() {
877 tool.distance(event.distance());
878 }
879 if event.tilt_has_changed() {
880 tool.tilt(event.tilt());
881 }
882 if event.slider_has_changed() {
883 tool.slider_position(event.slider_position());
884 }
885 if event.rotation_has_changed() {
886 tool.rotation(event.rotation());
887 }
888 if event.wheel_has_changed() {
889 tool.wheel(event.wheel_delta(), event.wheel_delta_discrete());
890 }
891
892 tool.motion(
893 pos,
894 under,
895 &tablet,
896 SERIAL_COUNTER.next_serial(),
897 event.time_msec(),
898 );
899
900 self.niri.tablet_cursor_location = Some(pos);
901 }
902
903 // Redraw to update the cursor position.
904 // FIXME: redraw only outputs overlapping the cursor.
905 self.niri.queue_redraw_all();
906 }
907
908 fn on_tablet_tool_tip<I: InputBackend>(&mut self, event: I::TabletToolTipEvent) {
909 let tool = self.niri.seat.tablet_seat().get_tool(&event.tool());
910
911 if let Some(tool) = tool {
912 match event.tip_state() {
913 TabletToolTipState::Down => {
914 let serial = SERIAL_COUNTER.next_serial();
915 tool.tip_down(serial, event.time_msec());
916
917 if let Some(pos) = self.niri.tablet_cursor_location {
918 if let Some(window) = self.niri.window_under(pos) {
919 let window = window.clone();
920 self.niri.layout.activate_window(&window);
921
922 // FIXME: granular.
923 self.niri.queue_redraw_all();
924 } else if let Some((output, _)) = self.niri.output_under(pos) {
925 let output = output.clone();
926 self.niri.layout.activate_output(&output);
927
928 // FIXME: granular.
929 self.niri.queue_redraw_all();
930 }
931 }
932 }
933 TabletToolTipState::Up => {
934 tool.tip_up(event.time_msec());
935 }
936 }
937 }
938 }
939
940 fn on_tablet_tool_proximity<I: InputBackend>(&mut self, event: I::TabletToolProximityEvent)
941 where
942 I::Device: 'static, // Needed for downcasting.
943 {
944 let Some(pos) = self.compute_tablet_position(&event) else {
945 return;
946 };
947
948 let under = self.niri.surface_under_and_global_space(pos);
949 let under = under.map(|u| u.surface);
950
951 let tablet_seat = self.niri.seat.tablet_seat();
952 let tool = tablet_seat.add_tool::<Self>(&self.niri.display_handle, &event.tool());
953 let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device()));
954 if let Some(tablet) = tablet {
955 match event.state() {
956 ProximityState::In => {
957 if let Some(under) = under {
958 tool.proximity_in(
959 pos,
960 under,
961 &tablet,
962 SERIAL_COUNTER.next_serial(),
963 event.time_msec(),
964 );
965 }
966 self.niri.tablet_cursor_location = Some(pos);
967 }
968 ProximityState::Out => {
969 tool.proximity_out(event.time_msec());
970
971 // Move the mouse pointer here to avoid discontinuity.
972 //
973 // Plus, Wayland SDL2 currently warps the pointer into some weird
974 // location on proximity out, so this shuold help it a little.
975 if let Some(pos) = self.niri.tablet_cursor_location {
976 self.move_cursor(pos);
977 }
978
979 self.niri.tablet_cursor_location = None;
980 }
981 }
982
983 // FIXME: granular.
984 self.niri.queue_redraw_all();
985 }
986 }
987
988 fn on_tablet_tool_button<I: InputBackend>(&mut self, event: I::TabletToolButtonEvent) {
989 let tool = self.niri.seat.tablet_seat().get_tool(&event.tool());
990
991 if let Some(tool) = tool {
992 tool.button(
993 event.button(),
994 event.button_state(),
995 SERIAL_COUNTER.next_serial(),
996 event.time_msec(),
997 );
998 }
999 }
1000
1001 fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) {
1002 if event.fingers() == 3 {
1003 if let Some(output) = self.niri.output_under_cursor() {
1004 self.niri.layout.workspace_switch_gesture_begin(&output);
1005
1006 // FIXME: granular. This one is awkward because this can cancel a gesture on
1007 // multiple other outputs in theory.
1008 self.niri.queue_redraw_all();
1009 }
1010
1011 // We handled this event.
1012 return;
1013 }
1014
1015 let serial = SERIAL_COUNTER.next_serial();
1016 let pointer = self.niri.seat.get_pointer().unwrap();
1017
1018 if self.update_pointer_focus() {
1019 pointer.frame(self);
1020 }
1021
1022 pointer.gesture_swipe_begin(
1023 self,
1024 &GestureSwipeBeginEvent {
1025 serial,
1026 time: event.time_msec(),
1027 fingers: event.fingers(),
1028 },
1029 );
1030 }
1031
1032 fn on_gesture_swipe_update<I: InputBackend>(&mut self, event: I::GestureSwipeUpdateEvent) {
1033 let res = self
1034 .niri
1035 .layout
1036 .workspace_switch_gesture_update(event.delta_y());
1037 if let Some(output) = res {
1038 if let Some(output) = output {
1039 self.niri.queue_redraw(output);
1040 }
1041
1042 // We handled this event.
1043 return;
1044 }
1045
1046 let pointer = self.niri.seat.get_pointer().unwrap();
1047
1048 if self.update_pointer_focus() {
1049 pointer.frame(self);
1050 }
1051
1052 pointer.gesture_swipe_update(
1053 self,
1054 &GestureSwipeUpdateEvent {
1055 time: event.time_msec(),
1056 delta: event.delta(),
1057 },
1058 );
1059 }
1060
1061 fn on_gesture_swipe_end<I: InputBackend>(&mut self, event: I::GestureSwipeEndEvent) {
1062 let res = self
1063 .niri
1064 .layout
1065 .workspace_switch_gesture_end(event.cancelled());
1066 if let Some(output) = res {
1067 self.niri.queue_redraw(output);
1068
1069 // We handled this event.
1070 return;
1071 }
1072
1073 let serial = SERIAL_COUNTER.next_serial();
1074 let pointer = self.niri.seat.get_pointer().unwrap();
1075
1076 if self.update_pointer_focus() {
1077 pointer.frame(self);
1078 }
1079
1080 pointer.gesture_swipe_end(
1081 self,
1082 &GestureSwipeEndEvent {
1083 serial,
1084 time: event.time_msec(),
1085 cancelled: event.cancelled(),
1086 },
1087 );
1088 }
1089
1090 fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) {
1091 let serial = SERIAL_COUNTER.next_serial();
1092 let pointer = self.niri.seat.get_pointer().unwrap();
1093
1094 if self.update_pointer_focus() {
1095 pointer.frame(self);
1096 }
1097
1098 pointer.gesture_pinch_begin(
1099 self,
1100 &GesturePinchBeginEvent {
1101 serial,
1102 time: event.time_msec(),
1103 fingers: event.fingers(),
1104 },
1105 );
1106 }
1107
1108 fn on_gesture_pinch_update<I: InputBackend>(&mut self, event: I::GesturePinchUpdateEvent) {
1109 let pointer = self.niri.seat.get_pointer().unwrap();
1110
1111 if self.update_pointer_focus() {
1112 pointer.frame(self);
1113 }
1114
1115 pointer.gesture_pinch_update(
1116 self,
1117 &GesturePinchUpdateEvent {
1118 time: event.time_msec(),
1119 delta: event.delta(),
1120 scale: event.scale(),
1121 rotation: event.rotation(),
1122 },
1123 );
1124 }
1125
1126 fn on_gesture_pinch_end<I: InputBackend>(&mut self, event: I::GesturePinchEndEvent) {
1127 let serial = SERIAL_COUNTER.next_serial();
1128 let pointer = self.niri.seat.get_pointer().unwrap();
1129
1130 if self.update_pointer_focus() {
1131 pointer.frame(self);
1132 }
1133
1134 pointer.gesture_pinch_end(
1135 self,
1136 &GesturePinchEndEvent {
1137 serial,
1138 time: event.time_msec(),
1139 cancelled: event.cancelled(),
1140 },
1141 );
1142 }
1143
1144 fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) {
1145 let serial = SERIAL_COUNTER.next_serial();
1146 let pointer = self.niri.seat.get_pointer().unwrap();
1147
1148 if self.update_pointer_focus() {
1149 pointer.frame(self);
1150 }
1151
1152 pointer.gesture_hold_begin(
1153 self,
1154 &GestureHoldBeginEvent {
1155 serial,
1156 time: event.time_msec(),
1157 fingers: event.fingers(),
1158 },
1159 );
1160 }
1161
1162 fn on_gesture_hold_end<I: InputBackend>(&mut self, event: I::GestureHoldEndEvent) {
1163 let serial = SERIAL_COUNTER.next_serial();
1164 let pointer = self.niri.seat.get_pointer().unwrap();
1165
1166 if self.update_pointer_focus() {
1167 pointer.frame(self);
1168 }
1169
1170 pointer.gesture_hold_end(
1171 self,
1172 &GestureHoldEndEvent {
1173 serial,
1174 time: event.time_msec(),
1175 cancelled: event.cancelled(),
1176 },
1177 );
1178 }
1179}
1180
1181/// Check whether the key should be intercepted and mark intercepted
1182/// pressed keys as `suppressed`, thus preventing `releases` corresponding
1183/// to them from being delivered.
1184#[allow(clippy::too_many_arguments)]
1185fn should_intercept_key(
1186 suppressed_keys: &mut HashSet<u32>,
1187 bindings: &Binds,
1188 comp_mod: CompositorMod,
1189 key_code: u32,
1190 modified: Keysym,
1191 raw: Option<Keysym>,
1192 pressed: bool,
1193 mods: ModifiersState,
1194 screenshot_ui: &ScreenshotUi,
1195 disable_power_key_handling: bool,
1196) -> FilterResult<Option<Action>> {
1197 // Actions are only triggered on presses, release of the key
1198 // shouldn't try to intercept anything unless we have marked
1199 // the key to suppress.
1200 if !pressed && !suppressed_keys.contains(&key_code) {
1201 return FilterResult::Forward;
1202 }
1203
1204 let mut final_action = action(
1205 bindings,
1206 comp_mod,
1207 modified,
1208 raw,
1209 mods,
1210 disable_power_key_handling,
1211 );
1212
1213 // Allow only a subset of compositor actions while the screenshot UI is open, since the user
1214 // cannot see the screen.
1215 if screenshot_ui.is_open() {
1216 let mut use_screenshot_ui_action = true;
1217
1218 if let Some(action) = &final_action {
1219 if allowed_during_screenshot(action) {
1220 use_screenshot_ui_action = false;
1221 }
1222 }
1223
1224 if use_screenshot_ui_action {
1225 final_action = screenshot_ui.action(raw, mods);
1226 }
1227 }
1228
1229 match (final_action, pressed) {
1230 (Some(action), true) => {
1231 suppressed_keys.insert(key_code);
1232 FilterResult::Intercept(Some(action))
1233 }
1234 (_, false) => {
1235 suppressed_keys.remove(&key_code);
1236 FilterResult::Intercept(None)
1237 }
1238 (None, true) => FilterResult::Forward,
1239 }
1240}
1241
1242fn action(
1243 bindings: &Binds,
1244 comp_mod: CompositorMod,
1245 modified: Keysym,
1246 raw: Option<Keysym>,
1247 mods: ModifiersState,
1248 disable_power_key_handling: bool,
1249) -> Option<Action> {
1250 use keysyms::*;
1251
1252 // Handle hardcoded binds.
1253 #[allow(non_upper_case_globals)] // wat
1254 match modified.raw() {
1255 modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => {
1256 let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32;
1257 return Some(Action::ChangeVt(vt));
1258 }
1259 KEY_XF86PowerOff if !disable_power_key_handling => return Some(Action::Suspend),
1260 _ => (),
1261 }
1262
1263 // Handle configured binds.
1264 let mut modifiers = Modifiers::empty();
1265 if mods.ctrl {
1266 modifiers |= Modifiers::CTRL;
1267 }
1268 if mods.shift {
1269 modifiers |= Modifiers::SHIFT;
1270 }
1271 if mods.alt {
1272 modifiers |= Modifiers::ALT;
1273 }
1274 if mods.logo {
1275 modifiers |= Modifiers::SUPER;
1276 }
1277
1278 let (mod_down, mut comp_mod) = match comp_mod {
1279 CompositorMod::Super => (mods.logo, Modifiers::SUPER),
1280 CompositorMod::Alt => (mods.alt, Modifiers::ALT),
1281 };
1282 if mod_down {
1283 modifiers |= Modifiers::COMPOSITOR;
1284 } else {
1285 comp_mod = Modifiers::empty();
1286 }
1287
1288 let Some(raw) = raw else {
1289 return None;
1290 };
1291
1292 for bind in &bindings.0 {
1293 if bind.key.keysym != raw {
1294 continue;
1295 }
1296
1297 if bind.key.modifiers | comp_mod == modifiers {
1298 return bind.actions.first().cloned();
1299 }
1300 }
1301
1302 None
1303}
1304
1305fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool {
1306 match event {
1307 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
1308 InputEvent::PointerMotion { .. }
1309 | InputEvent::PointerMotionAbsolute { .. }
1310 | InputEvent::PointerButton { .. }
1311 | InputEvent::PointerAxis { .. }
1312 | InputEvent::GestureSwipeBegin { .. }
1313 | InputEvent::GesturePinchBegin { .. }
1314 | InputEvent::GestureHoldBegin { .. }
1315 | InputEvent::TouchDown { .. }
1316 | InputEvent::TouchMotion { .. }
1317 | InputEvent::TabletToolAxis { .. }
1318 | InputEvent::TabletToolProximity { .. }
1319 | InputEvent::TabletToolTip { .. }
1320 | InputEvent::TabletToolButton { .. } => true,
1321 // Ignore events like device additions and removals, key releases, gesture ends.
1322 _ => false,
1323 }
1324}
1325
1326fn allowed_when_locked(action: &Action) -> bool {
1327 matches!(
1328 action,
1329 Action::Quit
1330 | Action::ChangeVt(_)
1331 | Action::Suspend
1332 | Action::PowerOffMonitors
1333 | Action::SwitchLayout(_)
1334 )
1335}
1336
1337fn allowed_during_screenshot(action: &Action) -> bool {
1338 matches!(
1339 action,
1340 Action::Quit | Action::ChangeVt(_) | Action::Suspend | Action::PowerOffMonitors
1341 )
1342}
1343
1344#[cfg(test)]
1345mod tests {
1346 use niri_config::{Action, Bind, Binds, Key, Modifiers};
1347
1348 use super::*;
1349
1350 #[test]
1351 fn bindings_suppress_keys() {
1352 let close_keysym = Keysym::q;
1353 let bindings = Binds(vec![Bind {
1354 key: Key {
1355 keysym: close_keysym,
1356 modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL,
1357 },
1358 actions: vec![Action::CloseWindow],
1359 }]);
1360
1361 let comp_mod = CompositorMod::Super;
1362 let mut suppressed_keys = HashSet::new();
1363
1364 let screenshot_ui = ScreenshotUi::new();
1365 let disable_power_key_handling = false;
1366
1367 // The key_code we pick is arbitrary, the only thing
1368 // that matters is that they are different between cases.
1369
1370 let close_key_code = close_keysym.into();
1371 let close_key_event = |suppr: &mut HashSet<u32>, mods: ModifiersState, pressed| {
1372 should_intercept_key(
1373 suppr,
1374 &bindings,
1375 comp_mod,
1376 close_key_code,
1377 close_keysym,
1378 Some(close_keysym),
1379 pressed,
1380 mods,
1381 &screenshot_ui,
1382 disable_power_key_handling,
1383 )
1384 };
1385
1386 // Key event with the code which can't trigger any action.
1387 let none_key_event = |suppr: &mut HashSet<u32>, mods: ModifiersState, pressed| {
1388 should_intercept_key(
1389 suppr,
1390 &bindings,
1391 comp_mod,
1392 Keysym::l.into(),
1393 Keysym::l,
1394 Some(Keysym::l),
1395 pressed,
1396 mods,
1397 &screenshot_ui,
1398 disable_power_key_handling,
1399 )
1400 };
1401
1402 let mut mods = ModifiersState {
1403 logo: true,
1404 ctrl: true,
1405 ..Default::default()
1406 };
1407
1408 // Action press/release.
1409
1410 let filter = close_key_event(&mut suppressed_keys, mods, true);
1411 assert!(matches!(
1412 filter,
1413 FilterResult::Intercept(Some(Action::CloseWindow))
1414 ));
1415 assert!(suppressed_keys.contains(&close_key_code));
1416
1417 let filter = close_key_event(&mut suppressed_keys, mods, false);
1418 assert!(matches!(filter, FilterResult::Intercept(None)));
1419 assert!(suppressed_keys.is_empty());
1420
1421 // Remove mod to make it for a binding.
1422
1423 mods.shift = true;
1424 let filter = close_key_event(&mut suppressed_keys, mods, true);
1425 assert!(matches!(filter, FilterResult::Forward));
1426
1427 mods.shift = false;
1428 let filter = close_key_event(&mut suppressed_keys, mods, false);
1429 assert!(matches!(filter, FilterResult::Forward));
1430
1431 // Just none press/release.
1432
1433 let filter = none_key_event(&mut suppressed_keys, mods, true);
1434 assert!(matches!(filter, FilterResult::Forward));
1435
1436 let filter = none_key_event(&mut suppressed_keys, mods, false);
1437 assert!(matches!(filter, FilterResult::Forward));
1438
1439 // Press action, press arbitrary, release action, release arbitrary.
1440
1441 let filter = close_key_event(&mut suppressed_keys, mods, true);
1442 assert!(matches!(
1443 filter,
1444 FilterResult::Intercept(Some(Action::CloseWindow))
1445 ));
1446
1447 let filter = none_key_event(&mut suppressed_keys, mods, true);
1448 assert!(matches!(filter, FilterResult::Forward));
1449
1450 let filter = close_key_event(&mut suppressed_keys, mods, false);
1451 assert!(matches!(filter, FilterResult::Intercept(None)));
1452
1453 let filter = none_key_event(&mut suppressed_keys, mods, false);
1454 assert!(matches!(filter, FilterResult::Forward));
1455
1456 // Trigger and remove all mods.
1457
1458 let filter = close_key_event(&mut suppressed_keys, mods, true);
1459 assert!(matches!(
1460 filter,
1461 FilterResult::Intercept(Some(Action::CloseWindow))
1462 ));
1463
1464 mods = Default::default();
1465 let filter = close_key_event(&mut suppressed_keys, mods, false);
1466 assert!(matches!(filter, FilterResult::Intercept(None)));
1467
1468 // Ensure that no keys are being suppressed.
1469 assert!(suppressed_keys.is_empty());
1470 }
1471}