forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBbbbd35eIvan Molodetskikh1 year ago1693 commits

mod.rs3624 lines, 136.3 KiBHistoryRawDownload
1use std::any::Any;
2use std::cmp::min;
3use std::collections::hash_map::Entry;
4use std::collections::HashSet;
5use std::time::Duration;
6
7use calloop::timer::{TimeoutAction, Timer};
8use input::event::gesture::GestureEventCoordinates as _;
9use niri_config::{Action, Bind, Binds, Key, Modifiers, SwitchBinds, Trigger};
10use niri_ipc::LayoutSwitchTarget;
11use smithay::backend::input::{
12 AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event,
13 GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _,
14 InputBackend, InputEvent, KeyState, KeyboardKeyEvent, Keycode, MouseButton, PointerAxisEvent,
15 PointerButtonEvent, PointerMotionEvent, ProximityState, Switch, SwitchState, SwitchToggleEvent,
16 TabletToolButtonEvent, TabletToolEvent, TabletToolProximityEvent, TabletToolTipEvent,
17 TabletToolTipState, TouchEvent,
18};
19use smithay::backend::libinput::LibinputInputBackend;
20use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState};
21use smithay::input::pointer::{
22 AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, Focus, GestureHoldBeginEvent,
23 GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent,
24 GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent,
25 GrabStartData as PointerGrabStartData, MotionEvent, RelativeMotionEvent,
26};
27use smithay::input::touch::{
28 DownEvent, GrabStartData as TouchGrabStartData, MotionEvent as TouchMotionEvent, UpEvent,
29};
30use smithay::input::SeatHandler;
31use smithay::utils::{Logical, Point, Rectangle, Transform, SERIAL_COUNTER};
32use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint};
33use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait};
34use touch_move_grab::TouchMoveGrab;
35
36use self::move_grab::MoveGrab;
37use self::resize_grab::ResizeGrab;
38use self::spatial_movement_grab::SpatialMovementGrab;
39use crate::layout::scrolling::ScrollDirection;
40use crate::niri::State;
41use crate::ui::screenshot_ui::ScreenshotUi;
42use crate::utils::spawning::spawn;
43use crate::utils::{center, get_monotonic_time, ResizeEdge};
44
45pub mod move_grab;
46pub mod resize_grab;
47pub mod scroll_tracker;
48pub mod spatial_movement_grab;
49pub mod swipe_tracker;
50pub mod touch_move_grab;
51pub mod touch_resize_grab;
52
53pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400);
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum CompositorMod {
57 Super,
58 Alt,
59}
60
61#[derive(Debug, Clone, Copy, PartialEq)]
62pub struct TabletData {
63 pub aspect_ratio: f64,
64}
65
66pub enum PointerOrTouchStartData<D: SeatHandler> {
67 Pointer(PointerGrabStartData<D>),
68 Touch(TouchGrabStartData<D>),
69}
70
71impl<D: SeatHandler> PointerOrTouchStartData<D> {
72 pub fn location(&self) -> Point<f64, Logical> {
73 match self {
74 PointerOrTouchStartData::Pointer(x) => x.location,
75 PointerOrTouchStartData::Touch(x) => x.location,
76 }
77 }
78}
79
80impl State {
81 pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>)
82 where
83 I::Device: 'static, // Needed for downcasting.
84 {
85 let _span = tracy_client::span!("process_input_event");
86
87 // Make sure some logic like workspace clean-up has a chance to run before doing actions.
88 self.niri.advance_animations();
89
90 if self.niri.monitors_active {
91 // Notify the idle-notifier of activity.
92 if should_notify_activity(&event) {
93 let _span = tracy_client::span!("IdleNotifierState::notify_activity");
94 self.niri
95 .idle_notifier_state
96 .notify_activity(&self.niri.seat);
97 }
98 } else {
99 // Power on monitors if they were off.
100 if should_activate_monitors(&event) {
101 self.niri.activate_monitors(&mut self.backend);
102
103 // Notify the idle-notifier of activity only if we're also powering on the
104 // monitors.
105 self.niri
106 .idle_notifier_state
107 .notify_activity(&self.niri.seat);
108 }
109 }
110
111 if should_reset_pointer_inactivity_timer(&event) {
112 self.niri.reset_pointer_inactivity_timer();
113 }
114
115 let hide_hotkey_overlay =
116 self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event);
117
118 let hide_exit_confirm_dialog = self
119 .niri
120 .exit_confirm_dialog
121 .as_ref()
122 .is_some_and(|d| d.is_open())
123 && should_hide_exit_confirm_dialog(&event);
124
125 use InputEvent::*;
126 match event {
127 DeviceAdded { device } => self.on_device_added(device),
128 DeviceRemoved { device } => self.on_device_removed(device),
129 Keyboard { event } => self.on_keyboard::<I>(event),
130 PointerMotion { event } => self.on_pointer_motion::<I>(event),
131 PointerMotionAbsolute { event } => self.on_pointer_motion_absolute::<I>(event),
132 PointerButton { event } => self.on_pointer_button::<I>(event),
133 PointerAxis { event } => self.on_pointer_axis::<I>(event),
134 TabletToolAxis { event } => self.on_tablet_tool_axis::<I>(event),
135 TabletToolTip { event } => self.on_tablet_tool_tip::<I>(event),
136 TabletToolProximity { event } => self.on_tablet_tool_proximity::<I>(event),
137 TabletToolButton { event } => self.on_tablet_tool_button::<I>(event),
138 GestureSwipeBegin { event } => self.on_gesture_swipe_begin::<I>(event),
139 GestureSwipeUpdate { event } => self.on_gesture_swipe_update::<I>(event),
140 GestureSwipeEnd { event } => self.on_gesture_swipe_end::<I>(event),
141 GesturePinchBegin { event } => self.on_gesture_pinch_begin::<I>(event),
142 GesturePinchUpdate { event } => self.on_gesture_pinch_update::<I>(event),
143 GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event),
144 GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event),
145 GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event),
146 TouchDown { event } => self.on_touch_down::<I>(event),
147 TouchMotion { event } => self.on_touch_motion::<I>(event),
148 TouchUp { event } => self.on_touch_up::<I>(event),
149 TouchCancel { event } => self.on_touch_cancel::<I>(event),
150 TouchFrame { event } => self.on_touch_frame::<I>(event),
151 SwitchToggle { event } => self.on_switch_toggle::<I>(event),
152 Special(_) => (),
153 }
154
155 // Do this last so that screenshot still gets it.
156 // FIXME: do this in a less cursed fashion somehow.
157 if hide_hotkey_overlay && self.niri.hotkey_overlay.hide() {
158 self.niri.queue_redraw_all();
159 }
160
161 if let Some(dialog) = &mut self.niri.exit_confirm_dialog {
162 if hide_exit_confirm_dialog && dialog.hide() {
163 self.niri.queue_redraw_all();
164 }
165 }
166 }
167
168 pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) {
169 let _span = tracy_client::span!("process_libinput_event");
170
171 match event {
172 InputEvent::DeviceAdded { device } => {
173 self.niri.devices.insert(device.clone());
174
175 if device.has_capability(input::DeviceCapability::TabletTool) {
176 match device.size() {
177 Some((w, h)) => {
178 let aspect_ratio = w / h;
179 let data = TabletData { aspect_ratio };
180 self.niri.tablets.insert(device.clone(), data);
181 }
182 None => {
183 warn!("tablet tool device has no size");
184 }
185 }
186 }
187
188 if device.has_capability(input::DeviceCapability::Keyboard) {
189 if let Some(led_state) = self
190 .niri
191 .seat
192 .get_keyboard()
193 .map(|keyboard| keyboard.led_state())
194 {
195 device.led_update(led_state.into());
196 }
197 }
198
199 if device.has_capability(input::DeviceCapability::Touch) {
200 self.niri.touch.insert(device.clone());
201 }
202
203 apply_libinput_settings(&self.niri.config.borrow().input, device);
204 }
205 InputEvent::DeviceRemoved { device } => {
206 self.niri.touch.remove(device);
207 self.niri.tablets.remove(device);
208 self.niri.devices.remove(device);
209 }
210 _ => (),
211 }
212 }
213
214 fn on_device_added(&mut self, device: impl Device) {
215 if device.has_capability(DeviceCapability::TabletTool) {
216 let tablet_seat = self.niri.seat.tablet_seat();
217
218 let desc = TabletDescriptor::from(&device);
219 tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc);
220 }
221 if device.has_capability(DeviceCapability::Touch) && self.niri.seat.get_touch().is_none() {
222 self.niri.seat.add_touch();
223 }
224 }
225
226 fn on_device_removed(&mut self, device: impl Device) {
227 if device.has_capability(DeviceCapability::TabletTool) {
228 let tablet_seat = self.niri.seat.tablet_seat();
229
230 let desc = TabletDescriptor::from(&device);
231 tablet_seat.remove_tablet(&desc);
232
233 // If there are no tablets in seat we can remove all tools
234 if tablet_seat.count_tablets() == 0 {
235 tablet_seat.clear_tools();
236 }
237 }
238 if device.has_capability(DeviceCapability::Touch) && self.niri.touch.is_empty() {
239 self.niri.seat.remove_touch();
240 }
241 }
242
243 /// Computes the rectangle that covers all outputs in global space.
244 fn global_bounding_rectangle(&self) -> Option<Rectangle<i32, Logical>> {
245 self.niri.global_space.outputs().fold(
246 None,
247 |acc: Option<Rectangle<i32, Logical>>, output| {
248 self.niri
249 .global_space
250 .output_geometry(output)
251 .map(|geo| acc.map(|acc| acc.merge(geo)).unwrap_or(geo))
252 },
253 )
254 }
255
256 /// Computes the cursor position for the tablet event.
257 ///
258 /// This function handles the tablet output mapping, as well as coordinate clamping and aspect
259 /// ratio correction.
260 fn compute_tablet_position<I: InputBackend>(
261 &self,
262 event: &(impl Event<I> + TabletToolEvent<I>),
263 ) -> Option<Point<f64, Logical>>
264 where
265 I::Device: 'static,
266 {
267 let (target_geo, keep_ratio, px, transform) =
268 if let Some(output) = self.niri.output_for_tablet() {
269 (
270 self.niri.global_space.output_geometry(output).unwrap(),
271 true,
272 1. / output.current_scale().fractional_scale(),
273 output.current_transform(),
274 )
275 } else {
276 let geo = self.global_bounding_rectangle()?;
277
278 // FIXME: this 1 px size should ideally somehow be computed for the rightmost output
279 // corresponding to the position on the right when clamping.
280 let output = self.niri.global_space.outputs().next().unwrap();
281 let scale = output.current_scale().fractional_scale();
282
283 // Do not keep ratio for the unified mode as this is what OpenTabletDriver expects.
284 (geo, false, 1. / scale, Transform::Normal)
285 };
286
287 let mut pos = {
288 let size = transform.invert().transform_size(target_geo.size);
289 transform.transform_point_in(event.position_transformed(size), &size.to_f64())
290 };
291
292 if keep_ratio {
293 pos.x /= target_geo.size.w as f64;
294 pos.y /= target_geo.size.h as f64;
295
296 let device = event.device();
297 if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() {
298 if let Some(data) = self.niri.tablets.get(device) {
299 // This code does the same thing as mutter with "keep aspect ratio" enabled.
300 let size = transform.invert().transform_size(target_geo.size);
301 let output_aspect_ratio = size.w as f64 / size.h as f64;
302 let ratio = data.aspect_ratio / output_aspect_ratio;
303
304 if ratio > 1. {
305 pos.x *= ratio;
306 } else {
307 pos.y /= ratio;
308 }
309 }
310 };
311
312 pos.x *= target_geo.size.w as f64;
313 pos.y *= target_geo.size.h as f64;
314 }
315
316 pos.x = pos.x.clamp(0.0, target_geo.size.w as f64 - px);
317 pos.y = pos.y.clamp(0.0, target_geo.size.h as f64 - px);
318 Some(pos + target_geo.loc.to_f64())
319 }
320
321 fn on_keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) {
322 let comp_mod = self.backend.mod_key();
323
324 let serial = SERIAL_COUNTER.next_serial();
325 let time = Event::time_msec(&event);
326 let pressed = event.state() == KeyState::Pressed;
327
328 // Stop bind key repeat on any release. This won't work 100% correctly in cases like:
329 // 1. Press Mod
330 // 2. Press Left (repeat starts)
331 // 3. Press PgDown (new repeat starts)
332 // 4. Release Left (PgDown repeat stops)
333 // But it's good enough for now.
334 // FIXME: handle this properly.
335 if !pressed {
336 if let Some(token) = self.niri.bind_repeat_timer.take() {
337 self.niri.event_loop.remove(token);
338 }
339 }
340
341 if pressed {
342 self.hide_cursor_if_needed();
343 }
344
345 let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input(
346 self,
347 event.key_code(),
348 event.state(),
349 serial,
350 time,
351 |this, mods, keysym| {
352 let bindings = &this.niri.config.borrow().binds;
353 let key_code = event.key_code();
354 let modified = keysym.modified_sym();
355 let raw = keysym.raw_latin_sym_or_raw_current_sym();
356
357 if let Some(dialog) = &this.niri.exit_confirm_dialog {
358 if dialog.is_open() && pressed && raw == Some(Keysym::Return) {
359 info!("quitting after confirming exit dialog");
360 this.niri.stop_signal.stop();
361 }
362 }
363
364 should_intercept_key(
365 &mut this.niri.suppressed_keys,
366 bindings,
367 comp_mod,
368 key_code,
369 modified,
370 raw,
371 pressed,
372 *mods,
373 &this.niri.screenshot_ui,
374 this.niri.config.borrow().input.disable_power_key_handling,
375 )
376 },
377 ) else {
378 return;
379 };
380
381 if !pressed {
382 return;
383 }
384
385 self.handle_bind(bind.clone());
386
387 self.start_key_repeat(bind);
388 }
389
390 fn start_key_repeat(&mut self, bind: Bind) {
391 if !bind.repeat {
392 return;
393 }
394
395 // Stop the previous key repeat if any.
396 if let Some(token) = self.niri.bind_repeat_timer.take() {
397 self.niri.event_loop.remove(token);
398 }
399
400 let config = self.niri.config.borrow();
401 let config = &config.input.keyboard;
402
403 let repeat_rate = config.repeat_rate;
404 if repeat_rate == 0 {
405 return;
406 }
407 let repeat_duration = Duration::from_secs_f64(1. / f64::from(repeat_rate));
408
409 let repeat_timer =
410 Timer::from_duration(Duration::from_millis(u64::from(config.repeat_delay)));
411
412 let token = self
413 .niri
414 .event_loop
415 .insert_source(repeat_timer, move |_, _, state| {
416 state.handle_bind(bind.clone());
417 TimeoutAction::ToDuration(repeat_duration)
418 })
419 .unwrap();
420
421 self.niri.bind_repeat_timer = Some(token);
422 }
423
424 fn hide_cursor_if_needed(&mut self) {
425 if !self.niri.config.borrow().cursor.hide_when_typing {
426 return;
427 }
428
429 // niri keeps this set only while actively using a tablet, which means the cursor position
430 // is likely to change almost immediately, causing pointer_hidden to just flicker back and
431 // forth.
432 if self.niri.tablet_cursor_location.is_some() {
433 return;
434 }
435
436 self.niri.pointer_hidden = true;
437 self.niri.queue_redraw_all();
438 }
439
440 pub fn handle_bind(&mut self, bind: Bind) {
441 let Some(cooldown) = bind.cooldown else {
442 self.do_action(bind.action, bind.allow_when_locked);
443 return;
444 };
445
446 // Check this first so that it doesn't trigger the cooldown.
447 if self.niri.is_locked() && !(bind.allow_when_locked || allowed_when_locked(&bind.action)) {
448 return;
449 }
450
451 match self.niri.bind_cooldown_timers.entry(bind.key) {
452 // The bind is on cooldown.
453 Entry::Occupied(_) => (),
454 Entry::Vacant(entry) => {
455 let timer = Timer::from_duration(cooldown);
456 let token = self
457 .niri
458 .event_loop
459 .insert_source(timer, move |_, _, state| {
460 if state.niri.bind_cooldown_timers.remove(&bind.key).is_none() {
461 error!("bind cooldown timer entry disappeared");
462 }
463 TimeoutAction::Drop
464 })
465 .unwrap();
466 entry.insert(token);
467
468 self.do_action(bind.action, bind.allow_when_locked);
469 }
470 }
471 }
472
473 pub fn do_action(&mut self, action: Action, allow_when_locked: bool) {
474 if self.niri.is_locked() && !(allow_when_locked || allowed_when_locked(&action)) {
475 return;
476 }
477
478 if let Some(touch) = self.niri.seat.get_touch() {
479 touch.cancel(self);
480 }
481
482 match action {
483 Action::Quit(skip_confirmation) => {
484 if !skip_confirmation {
485 if let Some(dialog) = &mut self.niri.exit_confirm_dialog {
486 if dialog.show() {
487 self.niri.queue_redraw_all();
488 }
489 return;
490 }
491 }
492
493 info!("quitting as requested");
494 self.niri.stop_signal.stop()
495 }
496 Action::ChangeVt(vt) => {
497 self.backend.change_vt(vt);
498 // Changing VT may not deliver the key releases, so clear the state.
499 self.niri.suppressed_keys.clear();
500 }
501 Action::Suspend => {
502 self.backend.suspend();
503 // Suspend may not deliver the key releases, so clear the state.
504 self.niri.suppressed_keys.clear();
505 }
506 Action::PowerOffMonitors => {
507 self.niri.deactivate_monitors(&mut self.backend);
508 }
509 Action::PowerOnMonitors => {
510 self.niri.activate_monitors(&mut self.backend);
511 }
512 Action::ToggleDebugTint => {
513 self.backend.toggle_debug_tint();
514 self.niri.queue_redraw_all();
515 }
516 Action::DebugToggleOpaqueRegions => {
517 self.niri.debug_draw_opaque_regions = !self.niri.debug_draw_opaque_regions;
518 self.niri.queue_redraw_all();
519 }
520 Action::DebugToggleDamage => {
521 self.niri.debug_toggle_damage();
522 }
523 Action::Spawn(command) => {
524 let (token, _) = self.niri.activation_state.create_external_token(None);
525 spawn(command, Some(token.clone()));
526 }
527 Action::DoScreenTransition(delay_ms) => {
528 self.backend.with_primary_renderer(|renderer| {
529 self.niri.do_screen_transition(renderer, delay_ms);
530 });
531 }
532 Action::ScreenshotScreen => {
533 let active = self.niri.layout.active_output().cloned();
534 if let Some(active) = active {
535 self.backend.with_primary_renderer(|renderer| {
536 if let Err(err) = self.niri.screenshot(renderer, &active) {
537 warn!("error taking screenshot: {err:?}");
538 }
539 });
540 }
541 }
542 Action::ConfirmScreenshot => {
543 if !self.niri.screenshot_ui.is_open() {
544 return;
545 }
546
547 self.backend.with_primary_renderer(|renderer| {
548 match self.niri.screenshot_ui.capture(renderer) {
549 Ok((size, pixels)) => {
550 if let Err(err) = self.niri.save_screenshot(size, pixels) {
551 warn!("error saving screenshot: {err:?}");
552 }
553 }
554 Err(err) => {
555 warn!("error capturing screenshot: {err:?}");
556 }
557 }
558 });
559
560 self.niri.screenshot_ui.close();
561 self.niri
562 .cursor_manager
563 .set_cursor_image(CursorImageStatus::default_named());
564 self.niri.queue_redraw_all();
565 }
566 Action::CancelScreenshot => {
567 if !self.niri.screenshot_ui.is_open() {
568 return;
569 }
570
571 self.niri.screenshot_ui.close();
572 self.niri
573 .cursor_manager
574 .set_cursor_image(CursorImageStatus::default_named());
575 self.niri.queue_redraw_all();
576 }
577 Action::ScreenshotTogglePointer => {
578 self.niri.screenshot_ui.toggle_pointer();
579 self.niri.queue_redraw_all();
580 }
581 Action::Screenshot => {
582 self.open_screenshot_ui();
583 }
584 Action::ScreenshotWindow => {
585 let focus = self.niri.layout.focus_with_output();
586 if let Some((mapped, output)) = focus {
587 self.backend.with_primary_renderer(|renderer| {
588 if let Err(err) = self.niri.screenshot_window(renderer, output, mapped) {
589 warn!("error taking screenshot: {err:?}");
590 }
591 });
592 }
593 }
594 Action::ScreenshotWindowById(id) => {
595 let mut windows = self.niri.layout.windows();
596 let window = windows.find(|(_, m)| m.id().get() == id);
597 if let Some((Some(monitor), mapped)) = window {
598 let output = monitor.output();
599 self.backend.with_primary_renderer(|renderer| {
600 if let Err(err) = self.niri.screenshot_window(renderer, output, mapped) {
601 warn!("error taking screenshot: {err:?}");
602 }
603 });
604 }
605 }
606 Action::CloseWindow => {
607 if let Some(mapped) = self.niri.layout.focus() {
608 mapped.toplevel().send_close();
609 }
610 }
611 Action::CloseWindowById(id) => {
612 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
613 if let Some((_, mapped)) = window {
614 mapped.toplevel().send_close();
615 }
616 }
617 Action::FullscreenWindow => {
618 let focus = self.niri.layout.focus().map(|m| m.window.clone());
619 if let Some(window) = focus {
620 self.niri.layout.toggle_fullscreen(&window);
621 // FIXME: granular
622 self.niri.queue_redraw_all();
623 }
624 }
625 Action::FullscreenWindowById(id) => {
626 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
627 let window = window.map(|(_, m)| m.window.clone());
628 if let Some(window) = window {
629 self.niri.layout.toggle_fullscreen(&window);
630 // FIXME: granular
631 self.niri.queue_redraw_all();
632 }
633 }
634 Action::FocusWindow(id) => {
635 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
636 let window = window.map(|(_, m)| m.window.clone());
637 if let Some(window) = window {
638 self.focus_window(&window);
639 }
640 }
641 Action::FocusWindowPrevious => {
642 if let Some(window) = self.niri.previously_focused_window.clone() {
643 self.focus_window(&window);
644 }
645 }
646 Action::SwitchLayout(action) => {
647 let keyboard = &self.niri.seat.get_keyboard().unwrap();
648 keyboard.with_xkb_state(self, |mut state| match action {
649 LayoutSwitchTarget::Next => state.cycle_next_layout(),
650 LayoutSwitchTarget::Prev => state.cycle_prev_layout(),
651 });
652 }
653 Action::MoveColumnLeft => {
654 self.niri.layout.move_left();
655 self.maybe_warp_cursor_to_focus();
656 // FIXME: granular
657 self.niri.queue_redraw_all();
658 }
659 Action::MoveColumnRight => {
660 self.niri.layout.move_right();
661 self.maybe_warp_cursor_to_focus();
662 // FIXME: granular
663 self.niri.queue_redraw_all();
664 }
665 Action::MoveColumnToFirst => {
666 self.niri.layout.move_column_to_first();
667 self.maybe_warp_cursor_to_focus();
668 // FIXME: granular
669 self.niri.queue_redraw_all();
670 }
671 Action::MoveColumnToLast => {
672 self.niri.layout.move_column_to_last();
673 self.maybe_warp_cursor_to_focus();
674 // FIXME: granular
675 self.niri.queue_redraw_all();
676 }
677 Action::MoveColumnLeftOrToMonitorLeft => {
678 if let Some(output) = self.niri.output_left() {
679 if self.niri.layout.move_column_left_or_to_output(&output)
680 && !self.maybe_warp_cursor_to_focus_centered()
681 {
682 self.move_cursor_to_output(&output);
683 } else {
684 self.maybe_warp_cursor_to_focus();
685 }
686 } else {
687 self.niri.layout.move_left();
688 self.maybe_warp_cursor_to_focus();
689 }
690
691 // FIXME: granular
692 self.niri.queue_redraw_all();
693 }
694 Action::MoveColumnRightOrToMonitorRight => {
695 if let Some(output) = self.niri.output_right() {
696 if self.niri.layout.move_column_right_or_to_output(&output)
697 && !self.maybe_warp_cursor_to_focus_centered()
698 {
699 self.move_cursor_to_output(&output);
700 } else {
701 self.maybe_warp_cursor_to_focus();
702 }
703 } else {
704 self.niri.layout.move_right();
705 self.maybe_warp_cursor_to_focus();
706 }
707
708 // FIXME: granular
709 self.niri.queue_redraw_all();
710 }
711 Action::MoveWindowDown => {
712 self.niri.layout.move_down();
713 self.maybe_warp_cursor_to_focus();
714 // FIXME: granular
715 self.niri.queue_redraw_all();
716 }
717 Action::MoveWindowUp => {
718 self.niri.layout.move_up();
719 self.maybe_warp_cursor_to_focus();
720 // FIXME: granular
721 self.niri.queue_redraw_all();
722 }
723 Action::MoveWindowDownOrToWorkspaceDown => {
724 self.niri.layout.move_down_or_to_workspace_down();
725 self.maybe_warp_cursor_to_focus();
726 // FIXME: granular
727 self.niri.queue_redraw_all();
728 }
729 Action::MoveWindowUpOrToWorkspaceUp => {
730 self.niri.layout.move_up_or_to_workspace_up();
731 self.maybe_warp_cursor_to_focus();
732 // FIXME: granular
733 self.niri.queue_redraw_all();
734 }
735 Action::ConsumeOrExpelWindowLeft => {
736 self.niri.layout.consume_or_expel_window_left(None);
737 self.maybe_warp_cursor_to_focus();
738 // FIXME: granular
739 self.niri.queue_redraw_all();
740 }
741 Action::ConsumeOrExpelWindowLeftById(id) => {
742 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
743 let window = window.map(|(_, m)| m.window.clone());
744 if let Some(window) = window {
745 self.niri.layout.consume_or_expel_window_left(Some(&window));
746 self.maybe_warp_cursor_to_focus();
747 // FIXME: granular
748 self.niri.queue_redraw_all();
749 }
750 }
751 Action::ConsumeOrExpelWindowRight => {
752 self.niri.layout.consume_or_expel_window_right(None);
753 self.maybe_warp_cursor_to_focus();
754 // FIXME: granular
755 self.niri.queue_redraw_all();
756 }
757 Action::ConsumeOrExpelWindowRightById(id) => {
758 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
759 let window = window.map(|(_, m)| m.window.clone());
760 if let Some(window) = window {
761 self.niri
762 .layout
763 .consume_or_expel_window_right(Some(&window));
764 self.maybe_warp_cursor_to_focus();
765 // FIXME: granular
766 self.niri.queue_redraw_all();
767 }
768 }
769 Action::FocusColumnLeft => {
770 self.niri.layout.focus_left();
771 self.maybe_warp_cursor_to_focus();
772 self.niri.layer_shell_on_demand_focus = None;
773 // FIXME: granular
774 self.niri.queue_redraw_all();
775 }
776 Action::FocusColumnRight => {
777 self.niri.layout.focus_right();
778 self.maybe_warp_cursor_to_focus();
779 self.niri.layer_shell_on_demand_focus = None;
780 // FIXME: granular
781 self.niri.queue_redraw_all();
782 }
783 Action::FocusColumnFirst => {
784 self.niri.layout.focus_column_first();
785 self.maybe_warp_cursor_to_focus();
786 self.niri.layer_shell_on_demand_focus = None;
787 // FIXME: granular
788 self.niri.queue_redraw_all();
789 }
790 Action::FocusColumnLast => {
791 self.niri.layout.focus_column_last();
792 self.maybe_warp_cursor_to_focus();
793 self.niri.layer_shell_on_demand_focus = None;
794 // FIXME: granular
795 self.niri.queue_redraw_all();
796 }
797 Action::FocusColumnRightOrFirst => {
798 self.niri.layout.focus_column_right_or_first();
799 self.maybe_warp_cursor_to_focus();
800 self.niri.layer_shell_on_demand_focus = None;
801 // FIXME: granular
802 self.niri.queue_redraw_all();
803 }
804 Action::FocusColumnLeftOrLast => {
805 self.niri.layout.focus_column_left_or_last();
806 self.maybe_warp_cursor_to_focus();
807 self.niri.layer_shell_on_demand_focus = None;
808 // FIXME: granular
809 self.niri.queue_redraw_all();
810 }
811 Action::FocusWindowOrMonitorUp => {
812 if let Some(output) = self.niri.output_up() {
813 if self.niri.layout.focus_window_up_or_output(&output)
814 && !self.maybe_warp_cursor_to_focus_centered()
815 {
816 self.move_cursor_to_output(&output);
817 } else {
818 self.maybe_warp_cursor_to_focus();
819 }
820 } else {
821 self.niri.layout.focus_up();
822 self.maybe_warp_cursor_to_focus();
823 }
824 self.niri.layer_shell_on_demand_focus = None;
825
826 // FIXME: granular
827 self.niri.queue_redraw_all();
828 }
829 Action::FocusWindowOrMonitorDown => {
830 if let Some(output) = self.niri.output_down() {
831 if self.niri.layout.focus_window_down_or_output(&output)
832 && !self.maybe_warp_cursor_to_focus_centered()
833 {
834 self.move_cursor_to_output(&output);
835 } else {
836 self.maybe_warp_cursor_to_focus();
837 }
838 } else {
839 self.niri.layout.focus_down();
840 self.maybe_warp_cursor_to_focus();
841 }
842 self.niri.layer_shell_on_demand_focus = None;
843
844 // FIXME: granular
845 self.niri.queue_redraw_all();
846 }
847 Action::FocusColumnOrMonitorLeft => {
848 if let Some(output) = self.niri.output_left() {
849 if self.niri.layout.focus_column_left_or_output(&output)
850 && !self.maybe_warp_cursor_to_focus_centered()
851 {
852 self.move_cursor_to_output(&output);
853 } else {
854 self.maybe_warp_cursor_to_focus();
855 }
856 } else {
857 self.niri.layout.focus_left();
858 self.maybe_warp_cursor_to_focus();
859 }
860 self.niri.layer_shell_on_demand_focus = None;
861
862 // FIXME: granular
863 self.niri.queue_redraw_all();
864 }
865 Action::FocusColumnOrMonitorRight => {
866 if let Some(output) = self.niri.output_right() {
867 if self.niri.layout.focus_column_right_or_output(&output)
868 && !self.maybe_warp_cursor_to_focus_centered()
869 {
870 self.move_cursor_to_output(&output);
871 } else {
872 self.maybe_warp_cursor_to_focus();
873 }
874 } else {
875 self.niri.layout.focus_right();
876 self.maybe_warp_cursor_to_focus();
877 }
878 self.niri.layer_shell_on_demand_focus = None;
879
880 // FIXME: granular
881 self.niri.queue_redraw_all();
882 }
883 Action::FocusWindowDown => {
884 self.niri.layout.focus_down();
885 self.maybe_warp_cursor_to_focus();
886 self.niri.layer_shell_on_demand_focus = None;
887 // FIXME: granular
888 self.niri.queue_redraw_all();
889 }
890 Action::FocusWindowUp => {
891 self.niri.layout.focus_up();
892 self.maybe_warp_cursor_to_focus();
893 self.niri.layer_shell_on_demand_focus = None;
894 // FIXME: granular
895 self.niri.queue_redraw_all();
896 }
897 Action::FocusWindowDownOrColumnLeft => {
898 self.niri.layout.focus_down_or_left();
899 self.maybe_warp_cursor_to_focus();
900 self.niri.layer_shell_on_demand_focus = None;
901 // FIXME: granular
902 self.niri.queue_redraw_all();
903 }
904 Action::FocusWindowDownOrColumnRight => {
905 self.niri.layout.focus_down_or_right();
906 self.maybe_warp_cursor_to_focus();
907 self.niri.layer_shell_on_demand_focus = None;
908 // FIXME: granular
909 self.niri.queue_redraw_all();
910 }
911 Action::FocusWindowUpOrColumnLeft => {
912 self.niri.layout.focus_up_or_left();
913 self.maybe_warp_cursor_to_focus();
914 self.niri.layer_shell_on_demand_focus = None;
915 // FIXME: granular
916 self.niri.queue_redraw_all();
917 }
918 Action::FocusWindowUpOrColumnRight => {
919 self.niri.layout.focus_up_or_right();
920 self.maybe_warp_cursor_to_focus();
921 self.niri.layer_shell_on_demand_focus = None;
922 // FIXME: granular
923 self.niri.queue_redraw_all();
924 }
925 Action::FocusWindowOrWorkspaceDown => {
926 self.niri.layout.focus_window_or_workspace_down();
927 self.maybe_warp_cursor_to_focus();
928 self.niri.layer_shell_on_demand_focus = None;
929 // FIXME: granular
930 self.niri.queue_redraw_all();
931 }
932 Action::FocusWindowOrWorkspaceUp => {
933 self.niri.layout.focus_window_or_workspace_up();
934 self.maybe_warp_cursor_to_focus();
935 self.niri.layer_shell_on_demand_focus = None;
936 // FIXME: granular
937 self.niri.queue_redraw_all();
938 }
939 Action::MoveWindowToWorkspaceDown => {
940 self.niri.layout.move_to_workspace_down();
941 self.maybe_warp_cursor_to_focus();
942 // FIXME: granular
943 self.niri.queue_redraw_all();
944 }
945 Action::MoveWindowToWorkspaceUp => {
946 self.niri.layout.move_to_workspace_up();
947 self.maybe_warp_cursor_to_focus();
948 // FIXME: granular
949 self.niri.queue_redraw_all();
950 }
951 Action::MoveWindowToWorkspace(reference) => {
952 if let Some((mut output, index)) =
953 self.niri.find_output_and_workspace_index(reference)
954 {
955 // The source output is always the active output, so if the target output is
956 // also the active output, we don't need to use move_to_output().
957 if let Some(active) = self.niri.layout.active_output() {
958 if output.as_ref() == Some(active) {
959 output = None;
960 }
961 }
962
963 if let Some(output) = output {
964 self.niri.layout.move_to_output(None, &output, Some(index));
965
966 if !self.maybe_warp_cursor_to_focus_centered() {
967 self.move_cursor_to_output(&output);
968 }
969 } else {
970 self.niri.layout.move_to_workspace(None, index);
971 self.maybe_warp_cursor_to_focus();
972 }
973
974 // FIXME: granular
975 self.niri.queue_redraw_all();
976 }
977 }
978 Action::MoveWindowToWorkspaceById {
979 window_id: id,
980 reference,
981 } => {
982 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
983 let window = window.map(|(_, m)| m.window.clone());
984 if let Some(window) = window {
985 if let Some((output, index)) =
986 self.niri.find_output_and_workspace_index(reference)
987 {
988 let target_was_active = self
989 .niri
990 .layout
991 .active_output()
992 .is_some_and(|active| output.as_ref() == Some(active));
993
994 if let Some(output) = output {
995 self.niri
996 .layout
997 .move_to_output(Some(&window), &output, Some(index));
998
999 // If the active output changed (window was moved and focused).
1000 #[allow(clippy::collapsible_if)]
1001 if !target_was_active
1002 && self.niri.layout.active_output() == Some(&output)
1003 {
1004 if !self.maybe_warp_cursor_to_focus_centered() {
1005 self.move_cursor_to_output(&output);
1006 }
1007 }
1008 } else {
1009 self.niri.layout.move_to_workspace(Some(&window), index);
1010
1011 // If we focused the target window.
1012 let new_focus = self.niri.layout.focus();
1013 if new_focus.is_some_and(|win| win.window == window) {
1014 self.maybe_warp_cursor_to_focus();
1015 }
1016 }
1017
1018 // FIXME: granular
1019 self.niri.queue_redraw_all();
1020 }
1021 }
1022 }
1023 Action::MoveColumnToWorkspaceDown => {
1024 self.niri.layout.move_column_to_workspace_down();
1025 self.maybe_warp_cursor_to_focus();
1026 // FIXME: granular
1027 self.niri.queue_redraw_all();
1028 }
1029 Action::MoveColumnToWorkspaceUp => {
1030 self.niri.layout.move_column_to_workspace_up();
1031 self.maybe_warp_cursor_to_focus();
1032 // FIXME: granular
1033 self.niri.queue_redraw_all();
1034 }
1035 Action::MoveColumnToWorkspace(reference) => {
1036 if let Some((mut output, index)) =
1037 self.niri.find_output_and_workspace_index(reference)
1038 {
1039 if let Some(active) = self.niri.layout.active_output() {
1040 if output.as_ref() == Some(active) {
1041 output = None;
1042 }
1043 }
1044
1045 if let Some(output) = output {
1046 self.niri
1047 .layout
1048 .move_column_to_workspace_on_output(&output, index);
1049 if !self.maybe_warp_cursor_to_focus_centered() {
1050 self.move_cursor_to_output(&output);
1051 }
1052 } else {
1053 self.niri.layout.move_column_to_workspace(index);
1054 self.maybe_warp_cursor_to_focus();
1055 }
1056
1057 // FIXME: granular
1058 self.niri.queue_redraw_all();
1059 }
1060 }
1061 Action::FocusWorkspaceDown => {
1062 self.niri.layout.switch_workspace_down();
1063 self.maybe_warp_cursor_to_focus();
1064 self.niri.layer_shell_on_demand_focus = None;
1065 // FIXME: granular
1066 self.niri.queue_redraw_all();
1067 }
1068 Action::FocusWorkspaceUp => {
1069 self.niri.layout.switch_workspace_up();
1070 self.maybe_warp_cursor_to_focus();
1071 self.niri.layer_shell_on_demand_focus = None;
1072 // FIXME: granular
1073 self.niri.queue_redraw_all();
1074 }
1075 Action::FocusWorkspace(reference) => {
1076 if let Some((mut output, index)) =
1077 self.niri.find_output_and_workspace_index(reference)
1078 {
1079 if let Some(active) = self.niri.layout.active_output() {
1080 if output.as_ref() == Some(active) {
1081 output = None;
1082 }
1083 }
1084
1085 if let Some(output) = output {
1086 self.niri.layout.focus_output(&output);
1087 self.niri.layout.switch_workspace(index);
1088 if !self.maybe_warp_cursor_to_focus_centered() {
1089 self.move_cursor_to_output(&output);
1090 }
1091 } else {
1092 let config = &self.niri.config;
1093 if config.borrow().input.workspace_auto_back_and_forth {
1094 self.niri.layout.switch_workspace_auto_back_and_forth(index);
1095 } else {
1096 self.niri.layout.switch_workspace(index);
1097 }
1098 self.maybe_warp_cursor_to_focus();
1099 }
1100 self.niri.layer_shell_on_demand_focus = None;
1101
1102 // FIXME: granular
1103 self.niri.queue_redraw_all();
1104 }
1105 }
1106 Action::FocusWorkspacePrevious => {
1107 self.niri.layout.switch_workspace_previous();
1108 self.maybe_warp_cursor_to_focus();
1109 self.niri.layer_shell_on_demand_focus = None;
1110 // FIXME: granular
1111 self.niri.queue_redraw_all();
1112 }
1113 Action::MoveWorkspaceDown => {
1114 self.niri.layout.move_workspace_down();
1115 // FIXME: granular
1116 self.niri.queue_redraw_all();
1117 }
1118 Action::MoveWorkspaceUp => {
1119 self.niri.layout.move_workspace_up();
1120 // FIXME: granular
1121 self.niri.queue_redraw_all();
1122 }
1123 Action::SetWorkspaceName(name) => {
1124 self.niri.layout.set_workspace_name(name, None);
1125 }
1126 Action::SetWorkspaceNameByRef { name, reference } => {
1127 self.niri.layout.set_workspace_name(name, Some(reference));
1128 }
1129 Action::UnsetWorkspaceName => {
1130 self.niri.layout.unset_workspace_name(None);
1131 }
1132 Action::UnsetWorkSpaceNameByRef(reference) => {
1133 self.niri.layout.unset_workspace_name(Some(reference));
1134 }
1135 Action::ConsumeWindowIntoColumn => {
1136 self.niri.layout.consume_into_column();
1137 // This does not cause immediate focus or window size change, so warping mouse to
1138 // focus won't do anything here.
1139 // FIXME: granular
1140 self.niri.queue_redraw_all();
1141 }
1142 Action::ExpelWindowFromColumn => {
1143 self.niri.layout.expel_from_column();
1144 self.maybe_warp_cursor_to_focus();
1145 // FIXME: granular
1146 self.niri.queue_redraw_all();
1147 }
1148 Action::SwapWindowRight => {
1149 self.niri
1150 .layout
1151 .swap_window_in_direction(ScrollDirection::Right);
1152 self.maybe_warp_cursor_to_focus();
1153 // FIXME: granular
1154 self.niri.queue_redraw_all();
1155 }
1156 Action::SwapWindowLeft => {
1157 self.niri
1158 .layout
1159 .swap_window_in_direction(ScrollDirection::Left);
1160 self.maybe_warp_cursor_to_focus();
1161 // FIXME: granular
1162 self.niri.queue_redraw_all();
1163 }
1164 Action::SwitchPresetColumnWidth => {
1165 self.niri.layout.toggle_width();
1166 }
1167 Action::SwitchPresetWindowWidth => {
1168 self.niri.layout.toggle_window_width(None);
1169 }
1170 Action::SwitchPresetWindowWidthById(id) => {
1171 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1172 let window = window.map(|(_, m)| m.window.clone());
1173 if let Some(window) = window {
1174 self.niri.layout.toggle_window_width(Some(&window));
1175 }
1176 }
1177 Action::SwitchPresetWindowHeight => {
1178 self.niri.layout.toggle_window_height(None);
1179 }
1180 Action::SwitchPresetWindowHeightById(id) => {
1181 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1182 let window = window.map(|(_, m)| m.window.clone());
1183 if let Some(window) = window {
1184 self.niri.layout.toggle_window_height(Some(&window));
1185 }
1186 }
1187 Action::CenterColumn => {
1188 self.niri.layout.center_column();
1189 // FIXME: granular
1190 self.niri.queue_redraw_all();
1191 }
1192 Action::CenterWindow => {
1193 self.niri.layout.center_window(None);
1194 // FIXME: granular
1195 self.niri.queue_redraw_all();
1196 }
1197 Action::CenterWindowById(id) => {
1198 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1199 let window = window.map(|(_, m)| m.window.clone());
1200 if let Some(window) = window {
1201 self.niri.layout.center_window(Some(&window));
1202 // FIXME: granular
1203 self.niri.queue_redraw_all();
1204 }
1205 }
1206 Action::MaximizeColumn => {
1207 self.niri.layout.toggle_full_width();
1208 }
1209 Action::FocusMonitorLeft => {
1210 if let Some(output) = self.niri.output_left() {
1211 self.niri.layout.focus_output(&output);
1212 if !self.maybe_warp_cursor_to_focus_centered() {
1213 self.move_cursor_to_output(&output);
1214 }
1215 self.niri.layer_shell_on_demand_focus = None;
1216 }
1217 }
1218 Action::FocusMonitorRight => {
1219 if let Some(output) = self.niri.output_right() {
1220 self.niri.layout.focus_output(&output);
1221 if !self.maybe_warp_cursor_to_focus_centered() {
1222 self.move_cursor_to_output(&output);
1223 }
1224 self.niri.layer_shell_on_demand_focus = None;
1225 }
1226 }
1227 Action::FocusMonitorDown => {
1228 if let Some(output) = self.niri.output_down() {
1229 self.niri.layout.focus_output(&output);
1230 if !self.maybe_warp_cursor_to_focus_centered() {
1231 self.move_cursor_to_output(&output);
1232 }
1233 self.niri.layer_shell_on_demand_focus = None;
1234 }
1235 }
1236 Action::FocusMonitorUp => {
1237 if let Some(output) = self.niri.output_up() {
1238 self.niri.layout.focus_output(&output);
1239 if !self.maybe_warp_cursor_to_focus_centered() {
1240 self.move_cursor_to_output(&output);
1241 }
1242 self.niri.layer_shell_on_demand_focus = None;
1243 }
1244 }
1245 Action::FocusMonitorPrevious => {
1246 if let Some(output) = self.niri.output_previous() {
1247 self.niri.layout.focus_output(&output);
1248 if !self.maybe_warp_cursor_to_focus_centered() {
1249 self.move_cursor_to_output(&output);
1250 }
1251 self.niri.layer_shell_on_demand_focus = None;
1252 }
1253 }
1254 Action::FocusMonitorNext => {
1255 if let Some(output) = self.niri.output_next() {
1256 self.niri.layout.focus_output(&output);
1257 if !self.maybe_warp_cursor_to_focus_centered() {
1258 self.move_cursor_to_output(&output);
1259 }
1260 self.niri.layer_shell_on_demand_focus = None;
1261 }
1262 }
1263 Action::MoveWindowToMonitorLeft => {
1264 if let Some(output) = self.niri.output_left() {
1265 self.niri.layout.move_to_output(None, &output, None);
1266 self.niri.layout.focus_output(&output);
1267 if !self.maybe_warp_cursor_to_focus_centered() {
1268 self.move_cursor_to_output(&output);
1269 }
1270 }
1271 }
1272 Action::MoveWindowToMonitorRight => {
1273 if let Some(output) = self.niri.output_right() {
1274 self.niri.layout.move_to_output(None, &output, None);
1275 self.niri.layout.focus_output(&output);
1276 if !self.maybe_warp_cursor_to_focus_centered() {
1277 self.move_cursor_to_output(&output);
1278 }
1279 }
1280 }
1281 Action::MoveWindowToMonitorDown => {
1282 if let Some(output) = self.niri.output_down() {
1283 self.niri.layout.move_to_output(None, &output, None);
1284 self.niri.layout.focus_output(&output);
1285 if !self.maybe_warp_cursor_to_focus_centered() {
1286 self.move_cursor_to_output(&output);
1287 }
1288 }
1289 }
1290 Action::MoveWindowToMonitorUp => {
1291 if let Some(output) = self.niri.output_up() {
1292 self.niri.layout.move_to_output(None, &output, None);
1293 self.niri.layout.focus_output(&output);
1294 if !self.maybe_warp_cursor_to_focus_centered() {
1295 self.move_cursor_to_output(&output);
1296 }
1297 }
1298 }
1299 Action::MoveWindowToMonitorPrevious => {
1300 if let Some(output) = self.niri.output_previous() {
1301 self.niri.layout.move_to_output(None, &output, None);
1302 self.niri.layout.focus_output(&output);
1303 if !self.maybe_warp_cursor_to_focus_centered() {
1304 self.move_cursor_to_output(&output);
1305 }
1306 }
1307 }
1308 Action::MoveWindowToMonitorNext => {
1309 if let Some(output) = self.niri.output_next() {
1310 self.niri.layout.move_to_output(None, &output, None);
1311 self.niri.layout.focus_output(&output);
1312 if !self.maybe_warp_cursor_to_focus_centered() {
1313 self.move_cursor_to_output(&output);
1314 }
1315 }
1316 }
1317 Action::MoveColumnToMonitorLeft => {
1318 if let Some(output) = self.niri.output_left() {
1319 self.niri.layout.move_column_to_output(&output);
1320 self.niri.layout.focus_output(&output);
1321 if !self.maybe_warp_cursor_to_focus_centered() {
1322 self.move_cursor_to_output(&output);
1323 }
1324 }
1325 }
1326 Action::MoveColumnToMonitorRight => {
1327 if let Some(output) = self.niri.output_right() {
1328 self.niri.layout.move_column_to_output(&output);
1329 self.niri.layout.focus_output(&output);
1330 if !self.maybe_warp_cursor_to_focus_centered() {
1331 self.move_cursor_to_output(&output);
1332 }
1333 }
1334 }
1335 Action::MoveColumnToMonitorDown => {
1336 if let Some(output) = self.niri.output_down() {
1337 self.niri.layout.move_column_to_output(&output);
1338 self.niri.layout.focus_output(&output);
1339 if !self.maybe_warp_cursor_to_focus_centered() {
1340 self.move_cursor_to_output(&output);
1341 }
1342 }
1343 }
1344 Action::MoveColumnToMonitorUp => {
1345 if let Some(output) = self.niri.output_up() {
1346 self.niri.layout.move_column_to_output(&output);
1347 self.niri.layout.focus_output(&output);
1348 if !self.maybe_warp_cursor_to_focus_centered() {
1349 self.move_cursor_to_output(&output);
1350 }
1351 }
1352 }
1353 Action::MoveColumnToMonitorPrevious => {
1354 if let Some(output) = self.niri.output_previous() {
1355 self.niri.layout.move_column_to_output(&output);
1356 self.niri.layout.focus_output(&output);
1357 if !self.maybe_warp_cursor_to_focus_centered() {
1358 self.move_cursor_to_output(&output);
1359 }
1360 }
1361 }
1362 Action::MoveColumnToMonitorNext => {
1363 if let Some(output) = self.niri.output_next() {
1364 self.niri.layout.move_column_to_output(&output);
1365 self.niri.layout.focus_output(&output);
1366 if !self.maybe_warp_cursor_to_focus_centered() {
1367 self.move_cursor_to_output(&output);
1368 }
1369 }
1370 }
1371 Action::SetColumnWidth(change) => {
1372 self.niri.layout.set_column_width(change);
1373 }
1374 Action::SetWindowWidth(change) => {
1375 self.niri.layout.set_window_width(None, change);
1376 }
1377 Action::SetWindowWidthById { id, change } => {
1378 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1379 let window = window.map(|(_, m)| m.window.clone());
1380 if let Some(window) = window {
1381 self.niri.layout.set_window_width(Some(&window), change);
1382 }
1383 }
1384 Action::SetWindowHeight(change) => {
1385 self.niri.layout.set_window_height(None, change);
1386 }
1387 Action::SetWindowHeightById { id, change } => {
1388 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1389 let window = window.map(|(_, m)| m.window.clone());
1390 if let Some(window) = window {
1391 self.niri.layout.set_window_height(Some(&window), change);
1392 }
1393 }
1394 Action::ResetWindowHeight => {
1395 self.niri.layout.reset_window_height(None);
1396 }
1397 Action::ResetWindowHeightById(id) => {
1398 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1399 let window = window.map(|(_, m)| m.window.clone());
1400 if let Some(window) = window {
1401 self.niri.layout.reset_window_height(Some(&window));
1402 }
1403 }
1404 Action::ShowHotkeyOverlay => {
1405 if self.niri.hotkey_overlay.show() {
1406 self.niri.queue_redraw_all();
1407 }
1408 }
1409 Action::MoveWorkspaceToMonitorLeft => {
1410 if let Some(output) = self.niri.output_left() {
1411 self.niri.layout.move_workspace_to_output(&output);
1412 if !self.maybe_warp_cursor_to_focus_centered() {
1413 self.move_cursor_to_output(&output);
1414 }
1415 }
1416 }
1417 Action::MoveWorkspaceToMonitorRight => {
1418 if let Some(output) = self.niri.output_right() {
1419 self.niri.layout.move_workspace_to_output(&output);
1420 if !self.maybe_warp_cursor_to_focus_centered() {
1421 self.move_cursor_to_output(&output);
1422 }
1423 }
1424 }
1425 Action::MoveWorkspaceToMonitorDown => {
1426 if let Some(output) = self.niri.output_down() {
1427 self.niri.layout.move_workspace_to_output(&output);
1428 if !self.maybe_warp_cursor_to_focus_centered() {
1429 self.move_cursor_to_output(&output);
1430 }
1431 }
1432 }
1433 Action::MoveWorkspaceToMonitorUp => {
1434 if let Some(output) = self.niri.output_up() {
1435 self.niri.layout.move_workspace_to_output(&output);
1436 if !self.maybe_warp_cursor_to_focus_centered() {
1437 self.move_cursor_to_output(&output);
1438 }
1439 }
1440 }
1441 Action::MoveWorkspaceToMonitorPrevious => {
1442 if let Some(output) = self.niri.output_previous() {
1443 self.niri.layout.move_workspace_to_output(&output);
1444 if !self.maybe_warp_cursor_to_focus_centered() {
1445 self.move_cursor_to_output(&output);
1446 }
1447 }
1448 }
1449 Action::MoveWorkspaceToMonitorNext => {
1450 if let Some(output) = self.niri.output_next() {
1451 self.niri.layout.move_workspace_to_output(&output);
1452 if !self.maybe_warp_cursor_to_focus_centered() {
1453 self.move_cursor_to_output(&output);
1454 }
1455 }
1456 }
1457 Action::ToggleWindowFloating => {
1458 self.niri.layout.toggle_window_floating(None);
1459 // FIXME: granular
1460 self.niri.queue_redraw_all();
1461 }
1462 Action::ToggleWindowFloatingById(id) => {
1463 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1464 let window = window.map(|(_, m)| m.window.clone());
1465 if let Some(window) = window {
1466 self.niri.layout.toggle_window_floating(Some(&window));
1467 // FIXME: granular
1468 self.niri.queue_redraw_all();
1469 }
1470 }
1471 Action::MoveWindowToFloating => {
1472 self.niri.layout.set_window_floating(None, true);
1473 // FIXME: granular
1474 self.niri.queue_redraw_all();
1475 }
1476 Action::MoveWindowToFloatingById(id) => {
1477 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1478 let window = window.map(|(_, m)| m.window.clone());
1479 if let Some(window) = window {
1480 self.niri.layout.set_window_floating(Some(&window), true);
1481 // FIXME: granular
1482 self.niri.queue_redraw_all();
1483 }
1484 }
1485 Action::MoveWindowToTiling => {
1486 self.niri.layout.set_window_floating(None, false);
1487 // FIXME: granular
1488 self.niri.queue_redraw_all();
1489 }
1490 Action::MoveWindowToTilingById(id) => {
1491 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1492 let window = window.map(|(_, m)| m.window.clone());
1493 if let Some(window) = window {
1494 self.niri.layout.set_window_floating(Some(&window), false);
1495 // FIXME: granular
1496 self.niri.queue_redraw_all();
1497 }
1498 }
1499 Action::FocusFloating => {
1500 self.niri.layout.focus_floating();
1501 self.maybe_warp_cursor_to_focus();
1502 // FIXME: granular
1503 self.niri.queue_redraw_all();
1504 }
1505 Action::FocusTiling => {
1506 self.niri.layout.focus_tiling();
1507 self.maybe_warp_cursor_to_focus();
1508 // FIXME: granular
1509 self.niri.queue_redraw_all();
1510 }
1511 Action::SwitchFocusBetweenFloatingAndTiling => {
1512 self.niri.layout.switch_focus_floating_tiling();
1513 self.maybe_warp_cursor_to_focus();
1514 // FIXME: granular
1515 self.niri.queue_redraw_all();
1516 }
1517 Action::MoveFloatingWindowById { id, x, y } => {
1518 let window = if let Some(id) = id {
1519 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1520 let window = window.map(|(_, m)| m.window.clone());
1521 if window.is_none() {
1522 return;
1523 }
1524 window
1525 } else {
1526 None
1527 };
1528
1529 self.niri
1530 .layout
1531 .move_floating_window(window.as_ref(), x, y, true);
1532 // FIXME: granular
1533 self.niri.queue_redraw_all();
1534 }
1535 }
1536 }
1537
1538 fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) {
1539 // We need an output to be able to move the pointer.
1540 if self.niri.global_space.outputs().next().is_none() {
1541 return;
1542 }
1543
1544 let serial = SERIAL_COUNTER.next_serial();
1545
1546 let pointer = self.niri.seat.get_pointer().unwrap();
1547
1548 let pos = pointer.current_location();
1549
1550 // We have an output, so we can compute the new location and focus.
1551 let mut new_pos = pos + event.delta();
1552
1553 // We received an event for the regular pointer, so show it now.
1554 self.niri.pointer_hidden = false;
1555 self.niri.tablet_cursor_location = None;
1556
1557 // Check if we have an active pointer constraint.
1558 //
1559 // FIXME: ideally this should use the pointer focus with up-to-date global location.
1560 let mut pointer_confined = None;
1561 if let Some(under) = &self.niri.pointer_contents.surface {
1562 // No need to check if the pointer focus surface matches, because here we're checking
1563 // for an already-active constraint, and the constraint is deactivated when the focused
1564 // surface changes.
1565 let pos_within_surface = pos - under.1;
1566
1567 let mut pointer_locked = false;
1568 with_pointer_constraint(&under.0, &pointer, |constraint| {
1569 let Some(constraint) = constraint else { return };
1570 if !constraint.is_active() {
1571 return;
1572 }
1573
1574 // Constraint does not apply if not within region.
1575 if let Some(region) = constraint.region() {
1576 if !region.contains(pos_within_surface.to_i32_round()) {
1577 return;
1578 }
1579 }
1580
1581 match &*constraint {
1582 PointerConstraint::Locked(_locked) => {
1583 pointer_locked = true;
1584 }
1585 PointerConstraint::Confined(confine) => {
1586 pointer_confined = Some((under.clone(), confine.region().cloned()));
1587 }
1588 }
1589 });
1590
1591 // If the pointer is locked, only send relative motion.
1592 if pointer_locked {
1593 pointer.relative_motion(
1594 self,
1595 Some(under.clone()),
1596 &RelativeMotionEvent {
1597 delta: event.delta(),
1598 delta_unaccel: event.delta_unaccel(),
1599 utime: event.time(),
1600 },
1601 );
1602
1603 pointer.frame(self);
1604
1605 // I guess a redraw to hide the tablet cursor could be nice? Doesn't matter too
1606 // much here I think.
1607 return;
1608 }
1609 }
1610
1611 if self
1612 .niri
1613 .global_space
1614 .output_under(new_pos)
1615 .next()
1616 .is_none()
1617 {
1618 // We ended up outside the outputs and need to clip the movement.
1619 if let Some(output) = self.niri.global_space.output_under(pos).next() {
1620 // The pointer was previously on some output. Clip the movement against its
1621 // boundaries.
1622 let geom = self.niri.global_space.output_geometry(output).unwrap();
1623 new_pos.x = new_pos
1624 .x
1625 .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64);
1626 new_pos.y = new_pos
1627 .y
1628 .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64);
1629 } else {
1630 // The pointer was not on any output in the first place. Find one for it.
1631 // Let's do the simple thing and just put it on the first output.
1632 let output = self.niri.global_space.outputs().next().unwrap();
1633 let geom = self.niri.global_space.output_geometry(output).unwrap();
1634 new_pos = center(geom).to_f64();
1635 }
1636 }
1637
1638 if let Some(output) = self.niri.screenshot_ui.selection_output() {
1639 let geom = self.niri.global_space.output_geometry(output).unwrap();
1640 let mut point = (new_pos - geom.loc.to_f64())
1641 .to_physical(output.current_scale().fractional_scale())
1642 .to_i32_round::<i32>();
1643
1644 let size = output.current_mode().unwrap().size;
1645 let transform = output.current_transform();
1646 let size = transform.transform_size(size);
1647 point.x = point.x.clamp(0, size.w - 1);
1648 point.y = point.y.clamp(0, size.h - 1);
1649
1650 self.niri.screenshot_ui.pointer_motion(point);
1651 }
1652
1653 let under = self.niri.contents_under(new_pos);
1654
1655 // Handle confined pointer.
1656 if let Some((focus_surface, region)) = pointer_confined {
1657 let mut prevent = false;
1658
1659 // Prevent the pointer from leaving the focused surface.
1660 if Some(&focus_surface.0) != under.surface.as_ref().map(|(s, _)| s) {
1661 prevent = true;
1662 }
1663
1664 // Prevent the pointer from leaving the confine region, if any.
1665 if let Some(region) = region {
1666 let new_pos_within_surface = new_pos - focus_surface.1;
1667 if !region.contains(new_pos_within_surface.to_i32_round()) {
1668 prevent = true;
1669 }
1670 }
1671
1672 if prevent {
1673 pointer.relative_motion(
1674 self,
1675 Some(focus_surface),
1676 &RelativeMotionEvent {
1677 delta: event.delta(),
1678 delta_unaccel: event.delta_unaccel(),
1679 utime: event.time(),
1680 },
1681 );
1682
1683 pointer.frame(self);
1684
1685 return;
1686 }
1687 }
1688
1689 self.niri.handle_focus_follows_mouse(&under);
1690
1691 self.niri.pointer_contents.clone_from(&under);
1692
1693 pointer.motion(
1694 self,
1695 under.surface.clone(),
1696 &MotionEvent {
1697 location: new_pos,
1698 serial,
1699 time: event.time_msec(),
1700 },
1701 );
1702
1703 pointer.relative_motion(
1704 self,
1705 under.surface,
1706 &RelativeMotionEvent {
1707 delta: event.delta(),
1708 delta_unaccel: event.delta_unaccel(),
1709 utime: event.time(),
1710 },
1711 );
1712
1713 pointer.frame(self);
1714
1715 // Activate a new confinement if necessary.
1716 self.niri.maybe_activate_pointer_constraint();
1717
1718 // Redraw to update the cursor position.
1719 // FIXME: redraw only outputs overlapping the cursor.
1720 self.niri.queue_redraw_all();
1721 }
1722
1723 fn on_pointer_motion_absolute<I: InputBackend>(
1724 &mut self,
1725 event: I::PointerMotionAbsoluteEvent,
1726 ) {
1727 let Some(output_geo) = self.global_bounding_rectangle() else {
1728 return;
1729 };
1730
1731 let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64();
1732
1733 let serial = SERIAL_COUNTER.next_serial();
1734
1735 let pointer = self.niri.seat.get_pointer().unwrap();
1736
1737 if let Some(output) = self.niri.screenshot_ui.selection_output() {
1738 let geom = self.niri.global_space.output_geometry(output).unwrap();
1739 let mut point = (pos - geom.loc.to_f64())
1740 .to_physical(output.current_scale().fractional_scale())
1741 .to_i32_round::<i32>();
1742
1743 let size = output.current_mode().unwrap().size;
1744 let transform = output.current_transform();
1745 let size = transform.transform_size(size);
1746 point.x = point.x.clamp(0, size.w - 1);
1747 point.y = point.y.clamp(0, size.h - 1);
1748
1749 self.niri.screenshot_ui.pointer_motion(point);
1750 }
1751
1752 let under = self.niri.contents_under(pos);
1753
1754 self.niri.handle_focus_follows_mouse(&under);
1755
1756 self.niri.pointer_contents.clone_from(&under);
1757
1758 pointer.motion(
1759 self,
1760 under.surface,
1761 &MotionEvent {
1762 location: pos,
1763 serial,
1764 time: event.time_msec(),
1765 },
1766 );
1767
1768 pointer.frame(self);
1769
1770 self.niri.maybe_activate_pointer_constraint();
1771
1772 // We moved the pointer, show it.
1773 self.niri.pointer_hidden = false;
1774
1775 // We moved the regular pointer, so show it now.
1776 self.niri.tablet_cursor_location = None;
1777
1778 // Redraw to update the cursor position.
1779 // FIXME: redraw only outputs overlapping the cursor.
1780 self.niri.queue_redraw_all();
1781 }
1782
1783 fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
1784 let pointer = self.niri.seat.get_pointer().unwrap();
1785
1786 let serial = SERIAL_COUNTER.next_serial();
1787
1788 let button = event.button();
1789
1790 let button_code = event.button_code();
1791
1792 let button_state = event.state();
1793
1794 // Ignore release events for mouse clicks that triggered a bind.
1795 if self.niri.suppressed_buttons.remove(&button_code) {
1796 return;
1797 }
1798
1799 if ButtonState::Pressed == button_state {
1800 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
1801 let modifiers = modifiers_from_state(mods);
1802
1803 if self.niri.mods_with_mouse_binds.contains(&modifiers) {
1804 let comp_mod = self.backend.mod_key();
1805
1806 if let Some(bind) = match button {
1807 Some(MouseButton::Left) => Some(Trigger::MouseLeft),
1808 Some(MouseButton::Right) => Some(Trigger::MouseRight),
1809 Some(MouseButton::Middle) => Some(Trigger::MouseMiddle),
1810 Some(MouseButton::Back) => Some(Trigger::MouseBack),
1811 Some(MouseButton::Forward) => Some(Trigger::MouseForward),
1812 _ => None,
1813 }
1814 .and_then(|trigger| {
1815 let config = self.niri.config.borrow();
1816 let bindings = &config.binds;
1817 find_configured_bind(bindings, comp_mod, trigger, mods)
1818 }) {
1819 self.niri.suppressed_buttons.insert(button_code);
1820 self.handle_bind(bind.clone());
1821 return;
1822 };
1823 }
1824
1825 // We received an event for the regular pointer, so show it now.
1826 self.niri.pointer_hidden = false;
1827 self.niri.tablet_cursor_location = None;
1828
1829 if let Some(mapped) = self.niri.window_under_cursor() {
1830 let window = mapped.window.clone();
1831
1832 // Check if we need to start an interactive move.
1833 if button == Some(MouseButton::Left) && !pointer.is_grabbed() {
1834 let mod_down = match self.backend.mod_key() {
1835 CompositorMod::Super => mods.logo,
1836 CompositorMod::Alt => mods.alt,
1837 };
1838 if mod_down {
1839 let location = pointer.current_location();
1840 let (output, pos_within_output) = self.niri.output_under(location).unwrap();
1841 let output = output.clone();
1842
1843 self.niri.layout.activate_window(&window);
1844
1845 if self.niri.layout.interactive_move_begin(
1846 window.clone(),
1847 &output,
1848 pos_within_output,
1849 ) {
1850 let start_data = PointerGrabStartData {
1851 focus: None,
1852 button: button_code,
1853 location,
1854 };
1855 let grab = MoveGrab::new(start_data, window.clone());
1856 pointer.set_grab(self, grab, serial, Focus::Clear);
1857 self.niri
1858 .cursor_manager
1859 .set_cursor_image(CursorImageStatus::Named(CursorIcon::Move));
1860 }
1861 }
1862 }
1863 // Check if we need to start an interactive resize.
1864 else if button == Some(MouseButton::Right) && !pointer.is_grabbed() {
1865 let mod_down = match self.backend.mod_key() {
1866 CompositorMod::Super => mods.logo,
1867 CompositorMod::Alt => mods.alt,
1868 };
1869 if mod_down {
1870 let location = pointer.current_location();
1871 let (output, pos_within_output) = self.niri.output_under(location).unwrap();
1872 let edges = self
1873 .niri
1874 .layout
1875 .resize_edges_under(output, pos_within_output)
1876 .unwrap();
1877
1878 if !edges.is_empty() {
1879 // See if we got a double resize-click gesture.
1880 // FIXME: deduplicate with resize_request in xdg-shell somehow.
1881 let time = get_monotonic_time();
1882 let last_cell = mapped.last_interactive_resize_start();
1883 let mut last = last_cell.get();
1884 last_cell.set(Some((time, edges)));
1885
1886 // Floating windows don't have either of the double-resize-click
1887 // gestures, so just allow it to resize.
1888 if mapped.is_floating() {
1889 last = None;
1890 last_cell.set(None);
1891 }
1892
1893 if let Some((last_time, last_edges)) = last {
1894 if time.saturating_sub(last_time) <= DOUBLE_CLICK_TIME {
1895 // Allow quick resize after a triple click.
1896 last_cell.set(None);
1897
1898 let intersection = edges.intersection(last_edges);
1899 if intersection.intersects(ResizeEdge::LEFT_RIGHT) {
1900 // FIXME: don't activate once we can pass specific windows
1901 // to actions.
1902 self.niri.layout.activate_window(&window);
1903 self.niri.layout.toggle_full_width();
1904 }
1905 if intersection.intersects(ResizeEdge::TOP_BOTTOM) {
1906 self.niri.layout.activate_window(&window);
1907 self.niri.layout.reset_window_height(Some(&window));
1908 }
1909 // FIXME: granular.
1910 self.niri.queue_redraw_all();
1911 return;
1912 }
1913 }
1914
1915 self.niri.layout.activate_window(&window);
1916
1917 if self
1918 .niri
1919 .layout
1920 .interactive_resize_begin(window.clone(), edges)
1921 {
1922 let start_data = PointerGrabStartData {
1923 focus: None,
1924 button: button_code,
1925 location,
1926 };
1927 let grab = ResizeGrab::new(start_data, window.clone());
1928 pointer.set_grab(self, grab, serial, Focus::Clear);
1929 self.niri.cursor_manager.set_cursor_image(
1930 CursorImageStatus::Named(edges.cursor_icon()),
1931 );
1932 }
1933 }
1934 }
1935 }
1936
1937 self.niri.layout.activate_window(&window);
1938
1939 // FIXME: granular.
1940 self.niri.queue_redraw_all();
1941 } else if let Some(output) = self.niri.output_under_cursor() {
1942 self.niri.layout.activate_output(&output);
1943
1944 // FIXME: granular.
1945 self.niri.queue_redraw_all();
1946 }
1947
1948 if button == Some(MouseButton::Middle) && !pointer.is_grabbed() {
1949 let mod_down = match self.backend.mod_key() {
1950 CompositorMod::Super => mods.logo,
1951 CompositorMod::Alt => mods.alt,
1952 };
1953 if mod_down {
1954 if let Some(output) = self.niri.output_under_cursor() {
1955 let location = pointer.current_location();
1956 let start_data = PointerGrabStartData {
1957 focus: None,
1958 button: button_code,
1959 location,
1960 };
1961 let grab = SpatialMovementGrab::new(start_data, output);
1962 pointer.set_grab(self, grab, serial, Focus::Clear);
1963 self.niri
1964 .cursor_manager
1965 .set_cursor_image(CursorImageStatus::Named(CursorIcon::AllScroll));
1966 }
1967 }
1968 }
1969 };
1970
1971 self.update_pointer_contents();
1972
1973 if ButtonState::Pressed == button_state {
1974 let layer_under = self.niri.pointer_contents.layer.clone();
1975 self.niri.focus_layer_surface_if_on_demand(layer_under);
1976 }
1977
1978 if let Some(button) = button {
1979 let pos = pointer.current_location();
1980 if let Some((output, _)) = self.niri.output_under(pos) {
1981 let output = output.clone();
1982 let geom = self.niri.global_space.output_geometry(&output).unwrap();
1983 let mut point = (pos - geom.loc.to_f64())
1984 .to_physical(output.current_scale().fractional_scale())
1985 .to_i32_round();
1986
1987 let size = output.current_mode().unwrap().size;
1988 let transform = output.current_transform();
1989 let size = transform.transform_size(size);
1990 point.x = min(size.w - 1, point.x);
1991 point.y = min(size.h - 1, point.y);
1992
1993 if self
1994 .niri
1995 .screenshot_ui
1996 .pointer_button(output, point, button, button_state)
1997 {
1998 self.niri.queue_redraw_all();
1999 }
2000 }
2001 }
2002
2003 pointer.button(
2004 self,
2005 &ButtonEvent {
2006 button: button_code,
2007 state: button_state,
2008 serial,
2009 time: event.time_msec(),
2010 },
2011 );
2012 pointer.frame(self);
2013 }
2014
2015 fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) {
2016 let source = event.source();
2017
2018 // We received an event for the regular pointer, so show it now. This is also needed for
2019 // update_pointer_contents() below to return the real contents, necessary for the pointer
2020 // axis event to reach the window.
2021 self.niri.pointer_hidden = false;
2022 self.niri.tablet_cursor_location = None;
2023
2024 let horizontal_amount_v120 = event.amount_v120(Axis::Horizontal);
2025 let vertical_amount_v120 = event.amount_v120(Axis::Vertical);
2026
2027 // Handle wheel scroll bindings.
2028 if source == AxisSource::Wheel {
2029 // If we have a scroll bind with current modifiers, then accumulate and don't pass to
2030 // Wayland. If there's no bind, reset the accumulator.
2031 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
2032 let modifiers = modifiers_from_state(mods);
2033 if self.niri.mods_with_wheel_binds.contains(&modifiers) {
2034 let comp_mod = self.backend.mod_key();
2035
2036 let horizontal = horizontal_amount_v120.unwrap_or(0.);
2037 let ticks = self.niri.horizontal_wheel_tracker.accumulate(horizontal);
2038 if ticks != 0 {
2039 let config = self.niri.config.borrow();
2040 let bindings = &config.binds;
2041 let bind_left =
2042 find_configured_bind(bindings, comp_mod, Trigger::WheelScrollLeft, mods);
2043 let bind_right =
2044 find_configured_bind(bindings, comp_mod, Trigger::WheelScrollRight, mods);
2045 drop(config);
2046
2047 if let Some(right) = bind_right {
2048 for _ in 0..ticks {
2049 self.handle_bind(right.clone());
2050 }
2051 }
2052 if let Some(left) = bind_left {
2053 for _ in ticks..0 {
2054 self.handle_bind(left.clone());
2055 }
2056 }
2057 }
2058
2059 let vertical = vertical_amount_v120.unwrap_or(0.);
2060 let ticks = self.niri.vertical_wheel_tracker.accumulate(vertical);
2061 if ticks != 0 {
2062 let config = self.niri.config.borrow();
2063 let bindings = &config.binds;
2064 let bind_up =
2065 find_configured_bind(bindings, comp_mod, Trigger::WheelScrollUp, mods);
2066 let bind_down =
2067 find_configured_bind(bindings, comp_mod, Trigger::WheelScrollDown, mods);
2068 drop(config);
2069
2070 if let Some(down) = bind_down {
2071 for _ in 0..ticks {
2072 self.handle_bind(down.clone());
2073 }
2074 }
2075 if let Some(up) = bind_up {
2076 for _ in ticks..0 {
2077 self.handle_bind(up.clone());
2078 }
2079 }
2080 }
2081
2082 return;
2083 } else {
2084 self.niri.horizontal_wheel_tracker.reset();
2085 self.niri.vertical_wheel_tracker.reset();
2086 }
2087 }
2088
2089 let horizontal_amount = event.amount(Axis::Horizontal);
2090 let vertical_amount = event.amount(Axis::Vertical);
2091
2092 // Handle touchpad scroll bindings.
2093 if source == AxisSource::Finger {
2094 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
2095 let modifiers = modifiers_from_state(mods);
2096 if self.niri.mods_with_finger_scroll_binds.contains(&modifiers) {
2097 let comp_mod = self.backend.mod_key();
2098
2099 let horizontal = horizontal_amount.unwrap_or(0.);
2100 let ticks = self
2101 .niri
2102 .horizontal_finger_scroll_tracker
2103 .accumulate(horizontal);
2104 if ticks != 0 {
2105 let config = self.niri.config.borrow();
2106 let bindings = &config.binds;
2107 let bind_left =
2108 find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollLeft, mods);
2109 let bind_right = find_configured_bind(
2110 bindings,
2111 comp_mod,
2112 Trigger::TouchpadScrollRight,
2113 mods,
2114 );
2115 drop(config);
2116
2117 if let Some(right) = bind_right {
2118 for _ in 0..ticks {
2119 self.handle_bind(right.clone());
2120 }
2121 }
2122 if let Some(left) = bind_left {
2123 for _ in ticks..0 {
2124 self.handle_bind(left.clone());
2125 }
2126 }
2127 }
2128
2129 let vertical = vertical_amount.unwrap_or(0.);
2130 let ticks = self
2131 .niri
2132 .vertical_finger_scroll_tracker
2133 .accumulate(vertical);
2134 if ticks != 0 {
2135 let config = self.niri.config.borrow();
2136 let bindings = &config.binds;
2137 let bind_up =
2138 find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollUp, mods);
2139 let bind_down =
2140 find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollDown, mods);
2141 drop(config);
2142
2143 if let Some(down) = bind_down {
2144 for _ in 0..ticks {
2145 self.handle_bind(down.clone());
2146 }
2147 }
2148 if let Some(up) = bind_up {
2149 for _ in ticks..0 {
2150 self.handle_bind(up.clone());
2151 }
2152 }
2153 }
2154
2155 return;
2156 } else {
2157 self.niri.horizontal_finger_scroll_tracker.reset();
2158 self.niri.vertical_finger_scroll_tracker.reset();
2159 }
2160 }
2161
2162 let scroll_factor = match source {
2163 AxisSource::Wheel => self.niri.config.borrow().input.mouse.scroll_factor,
2164 AxisSource::Finger => self.niri.config.borrow().input.touchpad.scroll_factor,
2165 _ => None,
2166 };
2167 let scroll_factor = scroll_factor.map(|x| x.0).unwrap_or(1.);
2168
2169 let horizontal_amount = horizontal_amount.unwrap_or_else(|| {
2170 // Winit backend, discrete scrolling.
2171 horizontal_amount_v120.unwrap_or(0.0) / 120. * 15.
2172 }) * scroll_factor;
2173 let vertical_amount = vertical_amount.unwrap_or_else(|| {
2174 // Winit backend, discrete scrolling.
2175 vertical_amount_v120.unwrap_or(0.0) / 120. * 15.
2176 }) * scroll_factor;
2177
2178 let horizontal_amount_v120 = horizontal_amount_v120.map(|x| x * scroll_factor);
2179 let vertical_amount_v120 = vertical_amount_v120.map(|x| x * scroll_factor);
2180
2181 let mut frame = AxisFrame::new(event.time_msec()).source(source);
2182 if horizontal_amount != 0.0 {
2183 frame = frame
2184 .relative_direction(Axis::Horizontal, event.relative_direction(Axis::Horizontal));
2185 frame = frame.value(Axis::Horizontal, horizontal_amount);
2186 if let Some(v120) = horizontal_amount_v120 {
2187 frame = frame.v120(Axis::Horizontal, v120 as i32);
2188 }
2189 }
2190 if vertical_amount != 0.0 {
2191 frame =
2192 frame.relative_direction(Axis::Vertical, event.relative_direction(Axis::Vertical));
2193 frame = frame.value(Axis::Vertical, vertical_amount);
2194 if let Some(v120) = vertical_amount_v120 {
2195 frame = frame.v120(Axis::Vertical, v120 as i32);
2196 }
2197 }
2198
2199 if source == AxisSource::Finger {
2200 if event.amount(Axis::Horizontal) == Some(0.0) {
2201 frame = frame.stop(Axis::Horizontal);
2202 }
2203 if event.amount(Axis::Vertical) == Some(0.0) {
2204 frame = frame.stop(Axis::Vertical);
2205 }
2206 }
2207
2208 self.update_pointer_contents();
2209
2210 let pointer = &self.niri.seat.get_pointer().unwrap();
2211 pointer.axis(self, frame);
2212 pointer.frame(self);
2213 }
2214
2215 fn on_tablet_tool_axis<I: InputBackend>(&mut self, event: I::TabletToolAxisEvent)
2216 where
2217 I::Device: 'static, // Needed for downcasting.
2218 {
2219 let Some(pos) = self.compute_tablet_position(&event) else {
2220 return;
2221 };
2222
2223 let under = self.niri.contents_under(pos);
2224
2225 let tablet_seat = self.niri.seat.tablet_seat();
2226 let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device()));
2227 let tool = tablet_seat.get_tool(&event.tool());
2228 if let (Some(tablet), Some(tool)) = (tablet, tool) {
2229 if event.pressure_has_changed() {
2230 tool.pressure(event.pressure());
2231 }
2232 if event.distance_has_changed() {
2233 tool.distance(event.distance());
2234 }
2235 if event.tilt_has_changed() {
2236 tool.tilt(event.tilt());
2237 }
2238 if event.slider_has_changed() {
2239 tool.slider_position(event.slider_position());
2240 }
2241 if event.rotation_has_changed() {
2242 tool.rotation(event.rotation());
2243 }
2244 if event.wheel_has_changed() {
2245 tool.wheel(event.wheel_delta(), event.wheel_delta_discrete());
2246 }
2247
2248 tool.motion(
2249 pos,
2250 under.surface,
2251 &tablet,
2252 SERIAL_COUNTER.next_serial(),
2253 event.time_msec(),
2254 );
2255
2256 self.niri.pointer_hidden = false;
2257 self.niri.tablet_cursor_location = Some(pos);
2258 }
2259
2260 // Redraw to update the cursor position.
2261 // FIXME: redraw only outputs overlapping the cursor.
2262 self.niri.queue_redraw_all();
2263 }
2264
2265 fn on_tablet_tool_tip<I: InputBackend>(&mut self, event: I::TabletToolTipEvent) {
2266 let tool = self.niri.seat.tablet_seat().get_tool(&event.tool());
2267
2268 if let Some(tool) = tool {
2269 match event.tip_state() {
2270 TabletToolTipState::Down => {
2271 let serial = SERIAL_COUNTER.next_serial();
2272 tool.tip_down(serial, event.time_msec());
2273
2274 if let Some(pos) = self.niri.tablet_cursor_location {
2275 let under = self.niri.contents_under(pos);
2276 if let Some(window) = under.window {
2277 self.niri.layout.activate_window(&window);
2278
2279 // FIXME: granular.
2280 self.niri.queue_redraw_all();
2281 } else if let Some(output) = under.output {
2282 self.niri.layout.activate_output(&output);
2283
2284 // FIXME: granular.
2285 self.niri.queue_redraw_all();
2286 }
2287 self.niri.focus_layer_surface_if_on_demand(under.layer);
2288 }
2289 }
2290 TabletToolTipState::Up => {
2291 tool.tip_up(event.time_msec());
2292 }
2293 }
2294 }
2295 }
2296
2297 fn on_tablet_tool_proximity<I: InputBackend>(&mut self, event: I::TabletToolProximityEvent)
2298 where
2299 I::Device: 'static, // Needed for downcasting.
2300 {
2301 let Some(pos) = self.compute_tablet_position(&event) else {
2302 return;
2303 };
2304
2305 let under = self.niri.contents_under(pos);
2306
2307 let tablet_seat = self.niri.seat.tablet_seat();
2308 let display_handle = self.niri.display_handle.clone();
2309 let tool = tablet_seat.add_tool::<Self>(self, &display_handle, &event.tool());
2310 let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device()));
2311 if let Some(tablet) = tablet {
2312 match event.state() {
2313 ProximityState::In => {
2314 if let Some(under) = under.surface {
2315 tool.proximity_in(
2316 pos,
2317 under,
2318 &tablet,
2319 SERIAL_COUNTER.next_serial(),
2320 event.time_msec(),
2321 );
2322 }
2323 self.niri.pointer_hidden = false;
2324 self.niri.tablet_cursor_location = Some(pos);
2325 }
2326 ProximityState::Out => {
2327 tool.proximity_out(event.time_msec());
2328
2329 // Move the mouse pointer here to avoid discontinuity.
2330 //
2331 // Plus, Wayland SDL2 currently warps the pointer into some weird
2332 // location on proximity out, so this should help it a little.
2333 if let Some(pos) = self.niri.tablet_cursor_location {
2334 self.move_cursor(pos);
2335 }
2336
2337 self.niri.pointer_hidden = false;
2338 self.niri.tablet_cursor_location = None;
2339 }
2340 }
2341
2342 // FIXME: granular.
2343 self.niri.queue_redraw_all();
2344 }
2345 }
2346
2347 fn on_tablet_tool_button<I: InputBackend>(&mut self, event: I::TabletToolButtonEvent) {
2348 let tool = self.niri.seat.tablet_seat().get_tool(&event.tool());
2349
2350 if let Some(tool) = tool {
2351 tool.button(
2352 event.button(),
2353 event.button_state(),
2354 SERIAL_COUNTER.next_serial(),
2355 event.time_msec(),
2356 );
2357 }
2358 }
2359
2360 fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) {
2361 if event.fingers() == 3 {
2362 self.niri.gesture_swipe_3f_cumulative = Some((0., 0.));
2363
2364 // We handled this event.
2365 return;
2366 }
2367
2368 let serial = SERIAL_COUNTER.next_serial();
2369 let pointer = self.niri.seat.get_pointer().unwrap();
2370
2371 if self.update_pointer_contents() {
2372 pointer.frame(self);
2373 }
2374
2375 pointer.gesture_swipe_begin(
2376 self,
2377 &GestureSwipeBeginEvent {
2378 serial,
2379 time: event.time_msec(),
2380 fingers: event.fingers(),
2381 },
2382 );
2383 }
2384
2385 fn on_gesture_swipe_update<I: InputBackend + 'static>(
2386 &mut self,
2387 event: I::GestureSwipeUpdateEvent,
2388 ) where
2389 I::Device: 'static,
2390 {
2391 let mut delta_x = event.delta_x();
2392 let mut delta_y = event.delta_y();
2393
2394 if let Some(libinput_event) =
2395 (&event as &dyn Any).downcast_ref::<input::event::gesture::GestureSwipeUpdateEvent>()
2396 {
2397 delta_x = libinput_event.dx_unaccelerated();
2398 delta_y = libinput_event.dy_unaccelerated();
2399 }
2400
2401 let device = event.device();
2402 if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() {
2403 if device.config_scroll_natural_scroll_enabled() {
2404 delta_x = -delta_x;
2405 delta_y = -delta_y;
2406 }
2407 }
2408
2409 if let Some((cx, cy)) = &mut self.niri.gesture_swipe_3f_cumulative {
2410 *cx += delta_x;
2411 *cy += delta_y;
2412
2413 // Check if the gesture moved far enough to decide. Threshold copied from GNOME Shell.
2414 let (cx, cy) = (*cx, *cy);
2415 if cx * cx + cy * cy >= 16. * 16. {
2416 self.niri.gesture_swipe_3f_cumulative = None;
2417
2418 if let Some(output) = self.niri.output_under_cursor() {
2419 if cx.abs() > cy.abs() {
2420 self.niri.layout.view_offset_gesture_begin(&output, true);
2421 } else {
2422 self.niri
2423 .layout
2424 .workspace_switch_gesture_begin(&output, true);
2425 }
2426 }
2427 }
2428 }
2429
2430 let timestamp = Duration::from_micros(event.time());
2431
2432 let mut handled = false;
2433 let res = self
2434 .niri
2435 .layout
2436 .workspace_switch_gesture_update(delta_y, timestamp, true);
2437 if let Some(output) = res {
2438 if let Some(output) = output {
2439 self.niri.queue_redraw(&output);
2440 }
2441 handled = true;
2442 }
2443
2444 let res = self
2445 .niri
2446 .layout
2447 .view_offset_gesture_update(delta_x, timestamp, true);
2448 if let Some(output) = res {
2449 if let Some(output) = output {
2450 self.niri.queue_redraw(&output);
2451 }
2452 handled = true;
2453 }
2454
2455 if handled {
2456 // We handled this event.
2457 return;
2458 }
2459
2460 let pointer = self.niri.seat.get_pointer().unwrap();
2461
2462 if self.update_pointer_contents() {
2463 pointer.frame(self);
2464 }
2465
2466 pointer.gesture_swipe_update(
2467 self,
2468 &GestureSwipeUpdateEvent {
2469 time: event.time_msec(),
2470 delta: event.delta(),
2471 },
2472 );
2473 }
2474
2475 fn on_gesture_swipe_end<I: InputBackend>(&mut self, event: I::GestureSwipeEndEvent) {
2476 self.niri.gesture_swipe_3f_cumulative = None;
2477
2478 let mut handled = false;
2479 let res = self
2480 .niri
2481 .layout
2482 .workspace_switch_gesture_end(event.cancelled(), Some(true));
2483 if let Some(output) = res {
2484 self.niri.queue_redraw(&output);
2485 handled = true;
2486 }
2487
2488 let res = self
2489 .niri
2490 .layout
2491 .view_offset_gesture_end(event.cancelled(), Some(true));
2492 if let Some(output) = res {
2493 self.niri.queue_redraw(&output);
2494 handled = true;
2495 }
2496
2497 if handled {
2498 // We handled this event.
2499 return;
2500 }
2501
2502 let serial = SERIAL_COUNTER.next_serial();
2503 let pointer = self.niri.seat.get_pointer().unwrap();
2504
2505 if self.update_pointer_contents() {
2506 pointer.frame(self);
2507 }
2508
2509 pointer.gesture_swipe_end(
2510 self,
2511 &GestureSwipeEndEvent {
2512 serial,
2513 time: event.time_msec(),
2514 cancelled: event.cancelled(),
2515 },
2516 );
2517 }
2518
2519 fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) {
2520 let serial = SERIAL_COUNTER.next_serial();
2521 let pointer = self.niri.seat.get_pointer().unwrap();
2522
2523 if self.update_pointer_contents() {
2524 pointer.frame(self);
2525 }
2526
2527 pointer.gesture_pinch_begin(
2528 self,
2529 &GesturePinchBeginEvent {
2530 serial,
2531 time: event.time_msec(),
2532 fingers: event.fingers(),
2533 },
2534 );
2535 }
2536
2537 fn on_gesture_pinch_update<I: InputBackend>(&mut self, event: I::GesturePinchUpdateEvent) {
2538 let pointer = self.niri.seat.get_pointer().unwrap();
2539
2540 if self.update_pointer_contents() {
2541 pointer.frame(self);
2542 }
2543
2544 pointer.gesture_pinch_update(
2545 self,
2546 &GesturePinchUpdateEvent {
2547 time: event.time_msec(),
2548 delta: event.delta(),
2549 scale: event.scale(),
2550 rotation: event.rotation(),
2551 },
2552 );
2553 }
2554
2555 fn on_gesture_pinch_end<I: InputBackend>(&mut self, event: I::GesturePinchEndEvent) {
2556 let serial = SERIAL_COUNTER.next_serial();
2557 let pointer = self.niri.seat.get_pointer().unwrap();
2558
2559 if self.update_pointer_contents() {
2560 pointer.frame(self);
2561 }
2562
2563 pointer.gesture_pinch_end(
2564 self,
2565 &GesturePinchEndEvent {
2566 serial,
2567 time: event.time_msec(),
2568 cancelled: event.cancelled(),
2569 },
2570 );
2571 }
2572
2573 fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) {
2574 let serial = SERIAL_COUNTER.next_serial();
2575 let pointer = self.niri.seat.get_pointer().unwrap();
2576
2577 if self.update_pointer_contents() {
2578 pointer.frame(self);
2579 }
2580
2581 pointer.gesture_hold_begin(
2582 self,
2583 &GestureHoldBeginEvent {
2584 serial,
2585 time: event.time_msec(),
2586 fingers: event.fingers(),
2587 },
2588 );
2589 }
2590
2591 fn on_gesture_hold_end<I: InputBackend>(&mut self, event: I::GestureHoldEndEvent) {
2592 let serial = SERIAL_COUNTER.next_serial();
2593 let pointer = self.niri.seat.get_pointer().unwrap();
2594
2595 if self.update_pointer_contents() {
2596 pointer.frame(self);
2597 }
2598
2599 pointer.gesture_hold_end(
2600 self,
2601 &GestureHoldEndEvent {
2602 serial,
2603 time: event.time_msec(),
2604 cancelled: event.cancelled(),
2605 },
2606 );
2607 }
2608
2609 /// Computes the cursor position for the touch event.
2610 ///
2611 /// This function handles the touch output mapping, as well as coordinate transform
2612 fn compute_touch_location<I: InputBackend, E: AbsolutePositionEvent<I>>(
2613 &self,
2614 evt: &E,
2615 ) -> Option<Point<f64, Logical>> {
2616 let output = self.niri.output_for_touch()?;
2617 let output_geo = self.niri.global_space.output_geometry(output).unwrap();
2618 let transform = output.current_transform();
2619 let size = transform.invert().transform_size(output_geo.size);
2620 Some(
2621 transform.transform_point_in(evt.position_transformed(size), &size.to_f64())
2622 + output_geo.loc.to_f64(),
2623 )
2624 }
2625
2626 fn on_touch_down<I: InputBackend>(&mut self, evt: I::TouchDownEvent) {
2627 let Some(handle) = self.niri.seat.get_touch() else {
2628 return;
2629 };
2630 let Some(touch_location) = self.compute_touch_location(&evt) else {
2631 return;
2632 };
2633
2634 let serial = SERIAL_COUNTER.next_serial();
2635
2636 let under = self.niri.contents_under(touch_location);
2637
2638 if !handle.is_grabbed() {
2639 if let Some(window) = under.window {
2640 self.niri.layout.activate_window(&window);
2641
2642 // Check if we need to start an interactive move.
2643 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
2644 let mod_down = match self.backend.mod_key() {
2645 CompositorMod::Super => mods.logo,
2646 CompositorMod::Alt => mods.alt,
2647 };
2648 if mod_down {
2649 let (output, pos_within_output) =
2650 self.niri.output_under(touch_location).unwrap();
2651 let output = output.clone();
2652
2653 if self.niri.layout.interactive_move_begin(
2654 window.clone(),
2655 &output,
2656 pos_within_output,
2657 ) {
2658 let start_data = TouchGrabStartData {
2659 focus: None,
2660 slot: evt.slot(),
2661 location: touch_location,
2662 };
2663 let grab = TouchMoveGrab::new(start_data, window.clone());
2664 handle.set_grab(self, grab, serial);
2665 }
2666 }
2667
2668 // FIXME: granular.
2669 self.niri.queue_redraw_all();
2670 } else if let Some(output) = under.output {
2671 self.niri.layout.activate_output(&output);
2672
2673 // FIXME: granular.
2674 self.niri.queue_redraw_all();
2675 }
2676 self.niri.focus_layer_surface_if_on_demand(under.layer);
2677 };
2678
2679 handle.down(
2680 self,
2681 under.surface,
2682 &DownEvent {
2683 slot: evt.slot(),
2684 location: touch_location,
2685 serial,
2686 time: evt.time_msec(),
2687 },
2688 );
2689
2690 // We're using touch, hide the pointer.
2691 self.niri.pointer_hidden = true;
2692 }
2693 fn on_touch_up<I: InputBackend>(&mut self, evt: I::TouchUpEvent) {
2694 let Some(handle) = self.niri.seat.get_touch() else {
2695 return;
2696 };
2697 let serial = SERIAL_COUNTER.next_serial();
2698 handle.up(
2699 self,
2700 &UpEvent {
2701 slot: evt.slot(),
2702 serial,
2703 time: evt.time_msec(),
2704 },
2705 )
2706 }
2707 fn on_touch_motion<I: InputBackend>(&mut self, evt: I::TouchMotionEvent) {
2708 let Some(handle) = self.niri.seat.get_touch() else {
2709 return;
2710 };
2711 let Some(touch_location) = self.compute_touch_location(&evt) else {
2712 return;
2713 };
2714 let under = self.niri.contents_under(touch_location);
2715 handle.motion(
2716 self,
2717 under.surface,
2718 &TouchMotionEvent {
2719 slot: evt.slot(),
2720 location: touch_location,
2721 time: evt.time_msec(),
2722 },
2723 );
2724 }
2725 fn on_touch_frame<I: InputBackend>(&mut self, _evt: I::TouchFrameEvent) {
2726 let Some(handle) = self.niri.seat.get_touch() else {
2727 return;
2728 };
2729 handle.frame(self);
2730 }
2731 fn on_touch_cancel<I: InputBackend>(&mut self, _evt: I::TouchCancelEvent) {
2732 let Some(handle) = self.niri.seat.get_touch() else {
2733 return;
2734 };
2735 handle.cancel(self);
2736 }
2737
2738 fn on_switch_toggle<I: InputBackend>(&mut self, evt: I::SwitchToggleEvent) {
2739 let Some(switch) = evt.switch() else {
2740 return;
2741 };
2742
2743 if switch == Switch::Lid {
2744 let is_closed = evt.state() == SwitchState::On;
2745 debug!("lid switch {}", if is_closed { "closed" } else { "opened" });
2746 self.niri.is_lid_closed = is_closed;
2747 self.backend.on_output_config_changed(&mut self.niri);
2748 }
2749
2750 let action = {
2751 let bindings = &self.niri.config.borrow().switch_events;
2752 find_configured_switch_action(bindings, switch, evt.state())
2753 };
2754
2755 if let Some(action) = action {
2756 self.do_action(action, true);
2757 }
2758 }
2759}
2760
2761/// Check whether the key should be intercepted and mark intercepted
2762/// pressed keys as `suppressed`, thus preventing `releases` corresponding
2763/// to them from being delivered.
2764#[allow(clippy::too_many_arguments)]
2765fn should_intercept_key(
2766 suppressed_keys: &mut HashSet<Keycode>,
2767 bindings: &Binds,
2768 comp_mod: CompositorMod,
2769 key_code: Keycode,
2770 modified: Keysym,
2771 raw: Option<Keysym>,
2772 pressed: bool,
2773 mods: ModifiersState,
2774 screenshot_ui: &ScreenshotUi,
2775 disable_power_key_handling: bool,
2776) -> FilterResult<Option<Bind>> {
2777 // Actions are only triggered on presses, release of the key
2778 // shouldn't try to intercept anything unless we have marked
2779 // the key to suppress.
2780 if !pressed && !suppressed_keys.contains(&key_code) {
2781 return FilterResult::Forward;
2782 }
2783
2784 let mut final_bind = find_bind(
2785 bindings,
2786 comp_mod,
2787 modified,
2788 raw,
2789 mods,
2790 disable_power_key_handling,
2791 );
2792
2793 // Allow only a subset of compositor actions while the screenshot UI is open, since the user
2794 // cannot see the screen.
2795 if screenshot_ui.is_open() {
2796 let mut use_screenshot_ui_action = true;
2797
2798 if let Some(bind) = &final_bind {
2799 if allowed_during_screenshot(&bind.action) {
2800 use_screenshot_ui_action = false;
2801 }
2802 }
2803
2804 if use_screenshot_ui_action {
2805 if let Some(raw) = raw {
2806 final_bind = screenshot_ui.action(raw, mods).map(|action| Bind {
2807 key: Key {
2808 trigger: Trigger::Keysym(raw),
2809 // Not entirely correct but it doesn't matter in how we currently use it.
2810 modifiers: Modifiers::empty(),
2811 },
2812 action,
2813 repeat: true,
2814 cooldown: None,
2815 allow_when_locked: false,
2816 });
2817 }
2818 }
2819 }
2820
2821 match (final_bind, pressed) {
2822 (Some(bind), true) => {
2823 suppressed_keys.insert(key_code);
2824 FilterResult::Intercept(Some(bind))
2825 }
2826 (_, false) => {
2827 suppressed_keys.remove(&key_code);
2828 FilterResult::Intercept(None)
2829 }
2830 (None, true) => FilterResult::Forward,
2831 }
2832}
2833
2834fn find_bind(
2835 bindings: &Binds,
2836 comp_mod: CompositorMod,
2837 modified: Keysym,
2838 raw: Option<Keysym>,
2839 mods: ModifiersState,
2840 disable_power_key_handling: bool,
2841) -> Option<Bind> {
2842 use keysyms::*;
2843
2844 // Handle hardcoded binds.
2845 #[allow(non_upper_case_globals)] // wat
2846 let hardcoded_action = match modified.raw() {
2847 modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => {
2848 let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32;
2849 Some(Action::ChangeVt(vt))
2850 }
2851 KEY_XF86PowerOff if !disable_power_key_handling => Some(Action::Suspend),
2852 _ => None,
2853 };
2854
2855 if let Some(action) = hardcoded_action {
2856 return Some(Bind {
2857 key: Key {
2858 // Not entirely correct but it doesn't matter in how we currently use it.
2859 trigger: Trigger::Keysym(modified),
2860 modifiers: Modifiers::empty(),
2861 },
2862 action,
2863 repeat: true,
2864 cooldown: None,
2865 allow_when_locked: false,
2866 });
2867 }
2868
2869 let trigger = Trigger::Keysym(raw?);
2870 find_configured_bind(bindings, comp_mod, trigger, mods)
2871}
2872
2873fn find_configured_bind(
2874 bindings: &Binds,
2875 comp_mod: CompositorMod,
2876 trigger: Trigger,
2877 mods: ModifiersState,
2878) -> Option<Bind> {
2879 // Handle configured binds.
2880 let mut modifiers = modifiers_from_state(mods);
2881
2882 let (mod_down, comp_mod) = match comp_mod {
2883 CompositorMod::Super => (mods.logo, Modifiers::SUPER),
2884 CompositorMod::Alt => (mods.alt, Modifiers::ALT),
2885 };
2886 if mod_down {
2887 modifiers |= Modifiers::COMPOSITOR;
2888 }
2889
2890 for bind in &bindings.0 {
2891 if bind.key.trigger != trigger {
2892 continue;
2893 }
2894
2895 let mut bind_modifiers = bind.key.modifiers;
2896 if bind_modifiers.contains(Modifiers::COMPOSITOR) {
2897 bind_modifiers |= comp_mod;
2898 } else if bind_modifiers.contains(comp_mod) {
2899 bind_modifiers |= Modifiers::COMPOSITOR;
2900 }
2901
2902 if bind_modifiers == modifiers {
2903 return Some(bind.clone());
2904 }
2905 }
2906
2907 None
2908}
2909
2910fn find_configured_switch_action(
2911 bindings: &SwitchBinds,
2912 switch: Switch,
2913 state: SwitchState,
2914) -> Option<Action> {
2915 let switch_action = match (switch, state) {
2916 (Switch::Lid, SwitchState::Off) => &bindings.lid_open,
2917 (Switch::Lid, SwitchState::On) => &bindings.lid_close,
2918 (Switch::TabletMode, SwitchState::Off) => &bindings.tablet_mode_off,
2919 (Switch::TabletMode, SwitchState::On) => &bindings.tablet_mode_on,
2920 _ => unreachable!(),
2921 };
2922 switch_action
2923 .as_ref()
2924 .map(|switch_action| Action::Spawn(switch_action.spawn.clone()))
2925}
2926
2927fn modifiers_from_state(mods: ModifiersState) -> Modifiers {
2928 let mut modifiers = Modifiers::empty();
2929 if mods.ctrl {
2930 modifiers |= Modifiers::CTRL;
2931 }
2932 if mods.shift {
2933 modifiers |= Modifiers::SHIFT;
2934 }
2935 if mods.alt {
2936 modifiers |= Modifiers::ALT;
2937 }
2938 if mods.logo {
2939 modifiers |= Modifiers::SUPER;
2940 }
2941 if mods.iso_level3_shift {
2942 modifiers |= Modifiers::ISO_LEVEL3_SHIFT;
2943 }
2944 if mods.iso_level5_shift {
2945 modifiers |= Modifiers::ISO_LEVEL5_SHIFT;
2946 }
2947 modifiers
2948}
2949
2950fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool {
2951 match event {
2952 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
2953 InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true,
2954 InputEvent::PointerMotion { .. }
2955 | InputEvent::PointerMotionAbsolute { .. }
2956 | InputEvent::PointerAxis { .. }
2957 | InputEvent::GestureSwipeBegin { .. }
2958 | InputEvent::GesturePinchBegin { .. }
2959 | InputEvent::GestureHoldBegin { .. }
2960 | InputEvent::TouchDown { .. }
2961 | InputEvent::TouchMotion { .. }
2962 | InputEvent::TabletToolAxis { .. }
2963 | InputEvent::TabletToolProximity { .. }
2964 | InputEvent::TabletToolTip { .. }
2965 | InputEvent::TabletToolButton { .. } => true,
2966 // Ignore events like device additions and removals, key releases, gesture ends.
2967 _ => false,
2968 }
2969}
2970
2971fn should_hide_hotkey_overlay<I: InputBackend>(event: &InputEvent<I>) -> bool {
2972 match event {
2973 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
2974 InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true,
2975 InputEvent::PointerAxis { .. }
2976 | InputEvent::GestureSwipeBegin { .. }
2977 | InputEvent::GesturePinchBegin { .. }
2978 | InputEvent::TouchDown { .. }
2979 | InputEvent::TouchMotion { .. }
2980 | InputEvent::TabletToolTip { .. }
2981 | InputEvent::TabletToolButton { .. } => true,
2982 _ => false,
2983 }
2984}
2985
2986fn should_hide_exit_confirm_dialog<I: InputBackend>(event: &InputEvent<I>) -> bool {
2987 match event {
2988 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
2989 InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true,
2990 InputEvent::PointerAxis { .. }
2991 | InputEvent::GestureSwipeBegin { .. }
2992 | InputEvent::GesturePinchBegin { .. }
2993 | InputEvent::TouchDown { .. }
2994 | InputEvent::TouchMotion { .. }
2995 | InputEvent::TabletToolTip { .. }
2996 | InputEvent::TabletToolButton { .. } => true,
2997 _ => false,
2998 }
2999}
3000
3001fn should_notify_activity<I: InputBackend>(event: &InputEvent<I>) -> bool {
3002 !matches!(
3003 event,
3004 InputEvent::DeviceAdded { .. } | InputEvent::DeviceRemoved { .. }
3005 )
3006}
3007
3008fn should_reset_pointer_inactivity_timer<I: InputBackend>(event: &InputEvent<I>) -> bool {
3009 matches!(
3010 event,
3011 InputEvent::PointerAxis { .. }
3012 | InputEvent::PointerButton { .. }
3013 | InputEvent::PointerMotion { .. }
3014 | InputEvent::PointerMotionAbsolute { .. }
3015 | InputEvent::TabletToolAxis { .. }
3016 | InputEvent::TabletToolButton { .. }
3017 | InputEvent::TabletToolProximity { .. }
3018 | InputEvent::TabletToolTip { .. }
3019 )
3020}
3021
3022fn allowed_when_locked(action: &Action) -> bool {
3023 matches!(
3024 action,
3025 Action::Quit(_)
3026 | Action::ChangeVt(_)
3027 | Action::Suspend
3028 | Action::PowerOffMonitors
3029 | Action::PowerOnMonitors
3030 | Action::SwitchLayout(_)
3031 )
3032}
3033
3034fn allowed_during_screenshot(action: &Action) -> bool {
3035 matches!(
3036 action,
3037 Action::Quit(_)
3038 | Action::ChangeVt(_)
3039 | Action::Suspend
3040 | Action::PowerOffMonitors
3041 | Action::PowerOnMonitors
3042 )
3043}
3044
3045pub fn apply_libinput_settings(config: &niri_config::Input, device: &mut input::Device) {
3046 // According to Mutter code, this setting is specific to touchpads.
3047 let is_touchpad = device.config_tap_finger_count() > 0;
3048 if is_touchpad {
3049 let c = &config.touchpad;
3050 let _ = device.config_send_events_set_mode(if c.off {
3051 input::SendEventsMode::DISABLED
3052 } else if c.disabled_on_external_mouse {
3053 input::SendEventsMode::DISABLED_ON_EXTERNAL_MOUSE
3054 } else {
3055 input::SendEventsMode::ENABLED
3056 });
3057 let _ = device.config_tap_set_enabled(c.tap);
3058 let _ = device.config_dwt_set_enabled(c.dwt);
3059 let _ = device.config_dwtp_set_enabled(c.dwtp);
3060 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
3061 let _ = device.config_accel_set_speed(c.accel_speed);
3062 let _ = device.config_left_handed_set(c.left_handed);
3063 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
3064
3065 if let Some(accel_profile) = c.accel_profile {
3066 let _ = device.config_accel_set_profile(accel_profile.into());
3067 } else if let Some(default) = device.config_accel_default_profile() {
3068 let _ = device.config_accel_set_profile(default);
3069 }
3070
3071 if let Some(method) = c.scroll_method {
3072 let _ = device.config_scroll_set_method(method.into());
3073
3074 if method == niri_config::ScrollMethod::OnButtonDown {
3075 if let Some(button) = c.scroll_button {
3076 let _ = device.config_scroll_set_button(button);
3077 }
3078 }
3079 } else if let Some(default) = device.config_scroll_default_method() {
3080 let _ = device.config_scroll_set_method(default);
3081
3082 if default == input::ScrollMethod::OnButtonDown {
3083 if let Some(button) = c.scroll_button {
3084 let _ = device.config_scroll_set_button(button);
3085 }
3086 }
3087 }
3088
3089 if let Some(tap_button_map) = c.tap_button_map {
3090 let _ = device.config_tap_set_button_map(tap_button_map.into());
3091 } else if let Some(default) = device.config_tap_default_button_map() {
3092 let _ = device.config_tap_set_button_map(default);
3093 }
3094
3095 if let Some(method) = c.click_method {
3096 let _ = device.config_click_set_method(method.into());
3097 } else if let Some(default) = device.config_click_default_method() {
3098 let _ = device.config_click_set_method(default);
3099 }
3100 }
3101
3102 // This is how Mutter tells apart mice.
3103 let mut is_trackball = false;
3104 let mut is_trackpoint = false;
3105 if let Some(udev_device) = unsafe { device.udev_device() } {
3106 if udev_device.property_value("ID_INPUT_TRACKBALL").is_some() {
3107 is_trackball = true;
3108 }
3109 if udev_device
3110 .property_value("ID_INPUT_POINTINGSTICK")
3111 .is_some()
3112 {
3113 is_trackpoint = true;
3114 }
3115 }
3116
3117 let is_mouse = device.has_capability(input::DeviceCapability::Pointer)
3118 && !is_touchpad
3119 && !is_trackball
3120 && !is_trackpoint;
3121 if is_mouse {
3122 let c = &config.mouse;
3123 let _ = device.config_send_events_set_mode(if c.off {
3124 input::SendEventsMode::DISABLED
3125 } else {
3126 input::SendEventsMode::ENABLED
3127 });
3128 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
3129 let _ = device.config_accel_set_speed(c.accel_speed);
3130 let _ = device.config_left_handed_set(c.left_handed);
3131 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
3132
3133 if let Some(accel_profile) = c.accel_profile {
3134 let _ = device.config_accel_set_profile(accel_profile.into());
3135 } else if let Some(default) = device.config_accel_default_profile() {
3136 let _ = device.config_accel_set_profile(default);
3137 }
3138
3139 if let Some(method) = c.scroll_method {
3140 let _ = device.config_scroll_set_method(method.into());
3141
3142 if method == niri_config::ScrollMethod::OnButtonDown {
3143 if let Some(button) = c.scroll_button {
3144 let _ = device.config_scroll_set_button(button);
3145 }
3146 }
3147 } else if let Some(default) = device.config_scroll_default_method() {
3148 let _ = device.config_scroll_set_method(default);
3149
3150 if default == input::ScrollMethod::OnButtonDown {
3151 if let Some(button) = c.scroll_button {
3152 let _ = device.config_scroll_set_button(button);
3153 }
3154 }
3155 }
3156 }
3157
3158 if is_trackball {
3159 let c = &config.trackball;
3160 let _ = device.config_send_events_set_mode(if c.off {
3161 input::SendEventsMode::DISABLED
3162 } else {
3163 input::SendEventsMode::ENABLED
3164 });
3165 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
3166 let _ = device.config_accel_set_speed(c.accel_speed);
3167 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
3168 let _ = device.config_left_handed_set(c.left_handed);
3169
3170 if let Some(accel_profile) = c.accel_profile {
3171 let _ = device.config_accel_set_profile(accel_profile.into());
3172 } else if let Some(default) = device.config_accel_default_profile() {
3173 let _ = device.config_accel_set_profile(default);
3174 }
3175
3176 if let Some(method) = c.scroll_method {
3177 let _ = device.config_scroll_set_method(method.into());
3178
3179 if method == niri_config::ScrollMethod::OnButtonDown {
3180 if let Some(button) = c.scroll_button {
3181 let _ = device.config_scroll_set_button(button);
3182 }
3183 }
3184 } else if let Some(default) = device.config_scroll_default_method() {
3185 let _ = device.config_scroll_set_method(default);
3186
3187 if default == input::ScrollMethod::OnButtonDown {
3188 if let Some(button) = c.scroll_button {
3189 let _ = device.config_scroll_set_button(button);
3190 }
3191 }
3192 }
3193 }
3194
3195 if is_trackpoint {
3196 let c = &config.trackpoint;
3197 let _ = device.config_send_events_set_mode(if c.off {
3198 input::SendEventsMode::DISABLED
3199 } else {
3200 input::SendEventsMode::ENABLED
3201 });
3202 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
3203 let _ = device.config_accel_set_speed(c.accel_speed);
3204 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
3205
3206 if let Some(accel_profile) = c.accel_profile {
3207 let _ = device.config_accel_set_profile(accel_profile.into());
3208 } else if let Some(default) = device.config_accel_default_profile() {
3209 let _ = device.config_accel_set_profile(default);
3210 }
3211
3212 if let Some(method) = c.scroll_method {
3213 let _ = device.config_scroll_set_method(method.into());
3214
3215 if method == niri_config::ScrollMethod::OnButtonDown {
3216 if let Some(button) = c.scroll_button {
3217 let _ = device.config_scroll_set_button(button);
3218 }
3219 }
3220 } else if let Some(default) = device.config_scroll_default_method() {
3221 let _ = device.config_scroll_set_method(default);
3222
3223 if default == input::ScrollMethod::OnButtonDown {
3224 if let Some(button) = c.scroll_button {
3225 let _ = device.config_scroll_set_button(button);
3226 }
3227 }
3228 }
3229 }
3230
3231 let is_tablet = device.has_capability(input::DeviceCapability::TabletTool);
3232 if is_tablet {
3233 let c = &config.tablet;
3234 let _ = device.config_send_events_set_mode(if c.off {
3235 input::SendEventsMode::DISABLED
3236 } else {
3237 input::SendEventsMode::ENABLED
3238 });
3239 let _ = device.config_left_handed_set(c.left_handed);
3240 }
3241}
3242
3243pub fn mods_with_binds(
3244 comp_mod: CompositorMod,
3245 binds: &Binds,
3246 triggers: &[Trigger],
3247) -> HashSet<Modifiers> {
3248 let comp_mod = match comp_mod {
3249 CompositorMod::Super => Modifiers::SUPER,
3250 CompositorMod::Alt => Modifiers::ALT,
3251 };
3252
3253 let mut rv = HashSet::new();
3254 for bind in &binds.0 {
3255 if !triggers.iter().any(|trigger| bind.key.trigger == *trigger) {
3256 continue;
3257 }
3258
3259 let mut mods = bind.key.modifiers;
3260 if mods.contains(Modifiers::COMPOSITOR) {
3261 mods.remove(Modifiers::COMPOSITOR);
3262 mods.insert(comp_mod);
3263 }
3264
3265 rv.insert(mods);
3266 }
3267
3268 rv
3269}
3270
3271pub fn mods_with_mouse_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> {
3272 mods_with_binds(
3273 comp_mod,
3274 binds,
3275 &[
3276 Trigger::MouseLeft,
3277 Trigger::MouseRight,
3278 Trigger::MouseMiddle,
3279 Trigger::MouseBack,
3280 Trigger::MouseForward,
3281 ],
3282 )
3283}
3284
3285pub fn mods_with_wheel_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> {
3286 mods_with_binds(
3287 comp_mod,
3288 binds,
3289 &[
3290 Trigger::WheelScrollUp,
3291 Trigger::WheelScrollDown,
3292 Trigger::WheelScrollLeft,
3293 Trigger::WheelScrollRight,
3294 ],
3295 )
3296}
3297
3298pub fn mods_with_finger_scroll_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> {
3299 mods_with_binds(
3300 comp_mod,
3301 binds,
3302 &[
3303 Trigger::TouchpadScrollUp,
3304 Trigger::TouchpadScrollDown,
3305 Trigger::TouchpadScrollLeft,
3306 Trigger::TouchpadScrollRight,
3307 ],
3308 )
3309}
3310
3311#[cfg(test)]
3312mod tests {
3313 use super::*;
3314 use crate::animation::Clock;
3315
3316 #[test]
3317 fn bindings_suppress_keys() {
3318 let close_keysym = Keysym::q;
3319 let bindings = Binds(vec![Bind {
3320 key: Key {
3321 trigger: Trigger::Keysym(close_keysym),
3322 modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL,
3323 },
3324 action: Action::CloseWindow,
3325 repeat: true,
3326 cooldown: None,
3327 allow_when_locked: false,
3328 }]);
3329
3330 let comp_mod = CompositorMod::Super;
3331 let mut suppressed_keys = HashSet::new();
3332
3333 let screenshot_ui = ScreenshotUi::new(Clock::default(), Default::default());
3334 let disable_power_key_handling = false;
3335
3336 // The key_code we pick is arbitrary, the only thing
3337 // that matters is that they are different between cases.
3338
3339 let close_key_code = Keycode::from(close_keysym.raw() + 8u32);
3340 let close_key_event = |suppr: &mut HashSet<Keycode>, mods: ModifiersState, pressed| {
3341 should_intercept_key(
3342 suppr,
3343 &bindings,
3344 comp_mod,
3345 close_key_code,
3346 close_keysym,
3347 Some(close_keysym),
3348 pressed,
3349 mods,
3350 &screenshot_ui,
3351 disable_power_key_handling,
3352 )
3353 };
3354
3355 // Key event with the code which can't trigger any action.
3356 let none_key_event = |suppr: &mut HashSet<Keycode>, mods: ModifiersState, pressed| {
3357 should_intercept_key(
3358 suppr,
3359 &bindings,
3360 comp_mod,
3361 Keycode::from(Keysym::l.raw() + 8),
3362 Keysym::l,
3363 Some(Keysym::l),
3364 pressed,
3365 mods,
3366 &screenshot_ui,
3367 disable_power_key_handling,
3368 )
3369 };
3370
3371 let mut mods = ModifiersState {
3372 logo: true,
3373 ctrl: true,
3374 ..Default::default()
3375 };
3376
3377 // Action press/release.
3378
3379 let filter = close_key_event(&mut suppressed_keys, mods, true);
3380 assert!(matches!(
3381 filter,
3382 FilterResult::Intercept(Some(Bind {
3383 action: Action::CloseWindow,
3384 ..
3385 }))
3386 ));
3387 assert!(suppressed_keys.contains(&close_key_code));
3388
3389 let filter = close_key_event(&mut suppressed_keys, mods, false);
3390 assert!(matches!(filter, FilterResult::Intercept(None)));
3391 assert!(suppressed_keys.is_empty());
3392
3393 // Remove mod to make it for a binding.
3394
3395 mods.shift = true;
3396 let filter = close_key_event(&mut suppressed_keys, mods, true);
3397 assert!(matches!(filter, FilterResult::Forward));
3398
3399 mods.shift = false;
3400 let filter = close_key_event(&mut suppressed_keys, mods, false);
3401 assert!(matches!(filter, FilterResult::Forward));
3402
3403 // Just none press/release.
3404
3405 let filter = none_key_event(&mut suppressed_keys, mods, true);
3406 assert!(matches!(filter, FilterResult::Forward));
3407
3408 let filter = none_key_event(&mut suppressed_keys, mods, false);
3409 assert!(matches!(filter, FilterResult::Forward));
3410
3411 // Press action, press arbitrary, release action, release arbitrary.
3412
3413 let filter = close_key_event(&mut suppressed_keys, mods, true);
3414 assert!(matches!(
3415 filter,
3416 FilterResult::Intercept(Some(Bind {
3417 action: Action::CloseWindow,
3418 ..
3419 }))
3420 ));
3421
3422 let filter = none_key_event(&mut suppressed_keys, mods, true);
3423 assert!(matches!(filter, FilterResult::Forward));
3424
3425 let filter = close_key_event(&mut suppressed_keys, mods, false);
3426 assert!(matches!(filter, FilterResult::Intercept(None)));
3427
3428 let filter = none_key_event(&mut suppressed_keys, mods, false);
3429 assert!(matches!(filter, FilterResult::Forward));
3430
3431 // Trigger and remove all mods.
3432
3433 let filter = close_key_event(&mut suppressed_keys, mods, true);
3434 assert!(matches!(
3435 filter,
3436 FilterResult::Intercept(Some(Bind {
3437 action: Action::CloseWindow,
3438 ..
3439 }))
3440 ));
3441
3442 mods = Default::default();
3443 let filter = close_key_event(&mut suppressed_keys, mods, false);
3444 assert!(matches!(filter, FilterResult::Intercept(None)));
3445
3446 // Ensure that no keys are being suppressed.
3447 assert!(suppressed_keys.is_empty());
3448 }
3449
3450 #[test]
3451 fn comp_mod_handling() {
3452 let bindings = Binds(vec![
3453 Bind {
3454 key: Key {
3455 trigger: Trigger::Keysym(Keysym::q),
3456 modifiers: Modifiers::COMPOSITOR,
3457 },
3458 action: Action::CloseWindow,
3459 repeat: true,
3460 cooldown: None,
3461 allow_when_locked: false,
3462 },
3463 Bind {
3464 key: Key {
3465 trigger: Trigger::Keysym(Keysym::h),
3466 modifiers: Modifiers::SUPER,
3467 },
3468 action: Action::FocusColumnLeft,
3469 repeat: true,
3470 cooldown: None,
3471 allow_when_locked: false,
3472 },
3473 Bind {
3474 key: Key {
3475 trigger: Trigger::Keysym(Keysym::j),
3476 modifiers: Modifiers::empty(),
3477 },
3478 action: Action::FocusWindowDown,
3479 repeat: true,
3480 cooldown: None,
3481 allow_when_locked: false,
3482 },
3483 Bind {
3484 key: Key {
3485 trigger: Trigger::Keysym(Keysym::k),
3486 modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER,
3487 },
3488 action: Action::FocusWindowUp,
3489 repeat: true,
3490 cooldown: None,
3491 allow_when_locked: false,
3492 },
3493 Bind {
3494 key: Key {
3495 trigger: Trigger::Keysym(Keysym::l),
3496 modifiers: Modifiers::SUPER | Modifiers::ALT,
3497 },
3498 action: Action::FocusColumnRight,
3499 repeat: true,
3500 cooldown: None,
3501 allow_when_locked: false,
3502 },
3503 ]);
3504
3505 assert_eq!(
3506 find_configured_bind(
3507 &bindings,
3508 CompositorMod::Super,
3509 Trigger::Keysym(Keysym::q),
3510 ModifiersState {
3511 logo: true,
3512 ..Default::default()
3513 }
3514 )
3515 .as_ref(),
3516 Some(&bindings.0[0])
3517 );
3518 assert_eq!(
3519 find_configured_bind(
3520 &bindings,
3521 CompositorMod::Super,
3522 Trigger::Keysym(Keysym::q),
3523 ModifiersState::default(),
3524 ),
3525 None,
3526 );
3527
3528 assert_eq!(
3529 find_configured_bind(
3530 &bindings,
3531 CompositorMod::Super,
3532 Trigger::Keysym(Keysym::h),
3533 ModifiersState {
3534 logo: true,
3535 ..Default::default()
3536 }
3537 )
3538 .as_ref(),
3539 Some(&bindings.0[1])
3540 );
3541 assert_eq!(
3542 find_configured_bind(
3543 &bindings,
3544 CompositorMod::Super,
3545 Trigger::Keysym(Keysym::h),
3546 ModifiersState::default(),
3547 ),
3548 None,
3549 );
3550
3551 assert_eq!(
3552 find_configured_bind(
3553 &bindings,
3554 CompositorMod::Super,
3555 Trigger::Keysym(Keysym::j),
3556 ModifiersState {
3557 logo: true,
3558 ..Default::default()
3559 }
3560 ),
3561 None,
3562 );
3563 assert_eq!(
3564 find_configured_bind(
3565 &bindings,
3566 CompositorMod::Super,
3567 Trigger::Keysym(Keysym::j),
3568 ModifiersState::default(),
3569 )
3570 .as_ref(),
3571 Some(&bindings.0[2])
3572 );
3573
3574 assert_eq!(
3575 find_configured_bind(
3576 &bindings,
3577 CompositorMod::Super,
3578 Trigger::Keysym(Keysym::k),
3579 ModifiersState {
3580 logo: true,
3581 ..Default::default()
3582 }
3583 )
3584 .as_ref(),
3585 Some(&bindings.0[3])
3586 );
3587 assert_eq!(
3588 find_configured_bind(
3589 &bindings,
3590 CompositorMod::Super,
3591 Trigger::Keysym(Keysym::k),
3592 ModifiersState::default(),
3593 ),
3594 None,
3595 );
3596
3597 assert_eq!(
3598 find_configured_bind(
3599 &bindings,
3600 CompositorMod::Super,
3601 Trigger::Keysym(Keysym::l),
3602 ModifiersState {
3603 logo: true,
3604 alt: true,
3605 ..Default::default()
3606 }
3607 )
3608 .as_ref(),
3609 Some(&bindings.0[4])
3610 );
3611 assert_eq!(
3612 find_configured_bind(
3613 &bindings,
3614 CompositorMod::Super,
3615 Trigger::Keysym(Keysym::l),
3616 ModifiersState {
3617 logo: true,
3618 ..Default::default()
3619 },
3620 ),
3621 None,
3622 );
3623 }
3624}