forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBa6baef7Anton Kesy10 months ago2623 commits

mod.rs5434 lines, 214.9 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::{
10 Action, Bind, Binds, Config, Key, ModKey, Modifiers, MruDirection, SwitchBinds, Trigger,
11};
12use niri_ipc::LayoutSwitchTarget;
13use smithay::backend::input::{
14 AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event,
15 GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _,
16 InputEvent, KeyState, KeyboardKeyEvent, Keycode, MouseButton, PointerAxisEvent,
17 PointerButtonEvent, PointerMotionEvent, ProximityState, Switch, SwitchState, SwitchToggleEvent,
18 TabletToolButtonEvent, TabletToolEvent, TabletToolProximityEvent, TabletToolTipEvent,
19 TabletToolTipState, TouchEvent,
20};
21use smithay::backend::libinput::LibinputInputBackend;
22use smithay::input::dnd::DnDGrab;
23use smithay::input::keyboard::{keysyms, FilterResult, Keysym, Layout, ModifiersState};
24use smithay::input::pointer::{
25 AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, Focus, GestureHoldBeginEvent,
26 GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent,
27 GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent,
28 GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, RelativeMotionEvent,
29};
30use smithay::input::touch::{
31 DownEvent, GrabStartData as TouchGrabStartData, MotionEvent as TouchMotionEvent, UpEvent,
32};
33use smithay::input::SeatHandler;
34use smithay::output::Output;
35use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource;
36use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
37use smithay::utils::{Logical, Point, Rectangle, Transform, SERIAL_COUNTER};
38use smithay::wayland::keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitor;
39use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint};
40use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait};
41use touch_overview_grab::TouchOverviewGrab;
42
43use self::move_grab::MoveGrab;
44use self::resize_grab::ResizeGrab;
45use self::spatial_movement_grab::SpatialMovementGrab;
46#[cfg(feature = "dbus")]
47use crate::dbus::freedesktop_a11y::KbMonBlock;
48use crate::layout::scrolling::ScrollDirection;
49use crate::layout::{ActivateWindow, LayoutElement as _};
50use crate::niri::{CastTarget, PointerVisibility, State};
51use crate::ui::mru::{WindowMru, WindowMruUi};
52use crate::ui::screenshot_ui::ScreenshotUi;
53use crate::utils::spawning::{spawn, spawn_sh};
54use crate::utils::{center, get_monotonic_time, ResizeEdge};
55
56pub mod backend_ext;
57pub mod move_grab;
58pub mod pick_color_grab;
59pub mod pick_window_grab;
60pub mod resize_grab;
61pub mod scroll_swipe_gesture;
62pub mod scroll_tracker;
63pub mod spatial_movement_grab;
64pub mod swipe_tracker;
65pub mod touch_overview_grab;
66pub mod touch_resize_grab;
67
68use backend_ext::{NiriInputBackend as InputBackend, NiriInputDevice as _};
69
70pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400);
71
72#[derive(Debug, Clone, Copy, PartialEq)]
73pub struct TabletData {
74 pub aspect_ratio: f64,
75}
76
77pub enum PointerOrTouchStartData<D: SeatHandler> {
78 Pointer(PointerGrabStartData<D>),
79 Touch(TouchGrabStartData<D>),
80}
81
82impl<D: SeatHandler> PointerOrTouchStartData<D> {
83 pub fn location(&self) -> Point<f64, Logical> {
84 match self {
85 PointerOrTouchStartData::Pointer(x) => x.location,
86 PointerOrTouchStartData::Touch(x) => x.location,
87 }
88 }
89
90 pub fn unwrap_pointer(&self) -> &PointerGrabStartData<D> {
91 match self {
92 PointerOrTouchStartData::Pointer(x) => x,
93 PointerOrTouchStartData::Touch(_) => panic!("start_data is not Pointer"),
94 }
95 }
96
97 pub fn unwrap_touch(&self) -> &TouchGrabStartData<D> {
98 match self {
99 PointerOrTouchStartData::Pointer(_) => panic!("start_data is not Touch"),
100 PointerOrTouchStartData::Touch(x) => x,
101 }
102 }
103
104 pub fn is_pointer(&self) -> bool {
105 matches!(self, Self::Pointer(_))
106 }
107
108 pub fn is_touch(&self) -> bool {
109 matches!(self, Self::Touch(_))
110 }
111}
112
113impl State {
114 pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>)
115 where
116 I::Device: 'static, // Needed for downcasting.
117 {
118 let _span = tracy_client::span!("process_input_event");
119
120 // Make sure some logic like workspace clean-up has a chance to run before doing actions.
121 self.niri.advance_animations();
122
123 if self.niri.monitors_active {
124 // Notify the idle-notifier of activity.
125 if should_notify_activity(&event) {
126 self.niri.notify_activity();
127 }
128 } else {
129 // Power on monitors if they were off.
130 if should_activate_monitors(&event) {
131 self.niri.activate_monitors(&mut self.backend);
132
133 // Notify the idle-notifier of activity only if we're also powering on the
134 // monitors.
135 self.niri.notify_activity();
136 }
137 }
138
139 if should_reset_pointer_inactivity_timer(&event) {
140 self.niri.reset_pointer_inactivity_timer();
141 }
142
143 let hide_hotkey_overlay =
144 self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event);
145
146 let hide_exit_confirm_dialog =
147 self.niri.exit_confirm_dialog.is_open() && should_hide_exit_confirm_dialog(&event);
148
149 let mut consumed_by_a11y = false;
150 use InputEvent::*;
151 match event {
152 DeviceAdded { device } => self.on_device_added(device),
153 DeviceRemoved { device } => self.on_device_removed(device),
154 Keyboard { event } => self.on_keyboard::<I>(event, &mut consumed_by_a11y),
155 PointerMotion { event } => self.on_pointer_motion::<I>(event),
156 PointerMotionAbsolute { event } => self.on_pointer_motion_absolute::<I>(event),
157 PointerButton { event } => self.on_pointer_button::<I>(event),
158 PointerAxis { event } => self.on_pointer_axis::<I>(event),
159 TabletToolAxis { event } => self.on_tablet_tool_axis::<I>(event),
160 TabletToolTip { event } => self.on_tablet_tool_tip::<I>(event),
161 TabletToolProximity { event } => self.on_tablet_tool_proximity::<I>(event),
162 TabletToolButton { event } => self.on_tablet_tool_button::<I>(event),
163 GestureSwipeBegin { event } => self.on_gesture_swipe_begin::<I>(event),
164 GestureSwipeUpdate { event } => self.on_gesture_swipe_update::<I>(event),
165 GestureSwipeEnd { event } => self.on_gesture_swipe_end::<I>(event),
166 GesturePinchBegin { event } => self.on_gesture_pinch_begin::<I>(event),
167 GesturePinchUpdate { event } => self.on_gesture_pinch_update::<I>(event),
168 GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event),
169 GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event),
170 GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event),
171 TouchDown { event } => self.on_touch_down::<I>(event),
172 TouchMotion { event } => self.on_touch_motion::<I>(event),
173 TouchUp { event } => self.on_touch_up::<I>(event),
174 TouchCancel { event } => self.on_touch_cancel::<I>(event),
175 TouchFrame { event } => self.on_touch_frame::<I>(event),
176 SwitchToggle { event } => self.on_switch_toggle::<I>(event),
177 Special(_) => (),
178 }
179
180 // Don't hide overlays if consumed by a11y, so that you can use the screen reader
181 // navigation keys.
182 if consumed_by_a11y {
183 return;
184 }
185
186 // Do this last so that screenshot still gets it.
187 if hide_hotkey_overlay && self.niri.hotkey_overlay.hide() {
188 self.niri.queue_redraw_all();
189 }
190
191 if hide_exit_confirm_dialog && self.niri.exit_confirm_dialog.hide() {
192 self.niri.queue_redraw_all();
193 }
194 }
195
196 pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) {
197 let _span = tracy_client::span!("process_libinput_event");
198
199 match event {
200 InputEvent::DeviceAdded { device } => {
201 self.niri.devices.insert(device.clone());
202
203 if device.has_capability(input::DeviceCapability::TabletTool) {
204 match device.size() {
205 Some((w, h)) => {
206 let aspect_ratio = w / h;
207 let data = TabletData { aspect_ratio };
208 self.niri.tablets.insert(device.clone(), data);
209 }
210 None => {
211 warn!("tablet tool device has no size");
212 }
213 }
214 }
215
216 if device.has_capability(input::DeviceCapability::Keyboard) {
217 if let Some(led_state) = self
218 .niri
219 .seat
220 .get_keyboard()
221 .map(|keyboard| keyboard.led_state())
222 {
223 device.led_update(led_state.into());
224 }
225 }
226
227 if device.has_capability(input::DeviceCapability::Touch) {
228 self.niri.touch.insert(device.clone());
229 }
230
231 apply_libinput_settings(&self.niri.config.borrow().input, device);
232 }
233 InputEvent::DeviceRemoved { device } => {
234 self.niri.touch.remove(device);
235 self.niri.tablets.remove(device);
236 self.niri.devices.remove(device);
237 }
238 _ => (),
239 }
240 }
241
242 fn on_device_added(&mut self, device: impl Device) {
243 if device.has_capability(DeviceCapability::TabletTool) {
244 let tablet_seat = self.niri.seat.tablet_seat();
245
246 let desc = TabletDescriptor::from(&device);
247 tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc);
248 }
249 if device.has_capability(DeviceCapability::Touch) && self.niri.seat.get_touch().is_none() {
250 self.niri.seat.add_touch();
251 }
252 }
253
254 fn on_device_removed(&mut self, device: impl Device) {
255 if device.has_capability(DeviceCapability::TabletTool) {
256 let tablet_seat = self.niri.seat.tablet_seat();
257
258 let desc = TabletDescriptor::from(&device);
259 tablet_seat.remove_tablet(&desc);
260
261 // If there are no tablets in seat we can remove all tools
262 if tablet_seat.count_tablets() == 0 {
263 tablet_seat.clear_tools();
264 }
265 }
266 if device.has_capability(DeviceCapability::Touch) && self.niri.touch.is_empty() {
267 self.niri.seat.remove_touch();
268 }
269 }
270
271 /// Computes the rectangle that covers all outputs in global space.
272 fn global_bounding_rectangle(&self) -> Option<Rectangle<i32, Logical>> {
273 self.niri.global_space.outputs().fold(
274 None,
275 |acc: Option<Rectangle<i32, Logical>>, output| {
276 self.niri
277 .global_space
278 .output_geometry(output)
279 .map(|geo| acc.map(|acc| acc.merge(geo)).unwrap_or(geo))
280 },
281 )
282 }
283
284 /// Computes the cursor position for the tablet event.
285 ///
286 /// This function handles the tablet output mapping, as well as coordinate clamping and aspect
287 /// ratio correction.
288 fn compute_tablet_position<I: InputBackend>(
289 &self,
290 event: &(impl Event<I> + TabletToolEvent<I>),
291 ) -> Option<Point<f64, Logical>>
292 where
293 I::Device: 'static,
294 {
295 let device_output = event.device().output(self);
296 let device_output = device_output.as_ref();
297 let (target_geo, keep_ratio, px, transform) =
298 if let Some(output) = device_output.or_else(|| self.niri.output_for_tablet()) {
299 (
300 self.niri.global_space.output_geometry(output).unwrap(),
301 true,
302 1. / output.current_scale().fractional_scale(),
303 output.current_transform(),
304 )
305 } else {
306 let geo = self.global_bounding_rectangle()?;
307
308 // FIXME: this 1 px size should ideally somehow be computed for the rightmost output
309 // corresponding to the position on the right when clamping.
310 let output = self.niri.global_space.outputs().next().unwrap();
311 let scale = output.current_scale().fractional_scale();
312
313 // Do not keep ratio for the unified mode as this is what OpenTabletDriver expects.
314 (geo, false, 1. / scale, Transform::Normal)
315 };
316
317 let mut pos = {
318 let size = transform.invert().transform_size(target_geo.size);
319 transform.transform_point_in(event.position_transformed(size), &size.to_f64())
320 };
321
322 if keep_ratio {
323 pos.x /= target_geo.size.w as f64;
324 pos.y /= target_geo.size.h as f64;
325
326 let device = event.device();
327 if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() {
328 if let Some(data) = self.niri.tablets.get(device) {
329 // This code does the same thing as mutter with "keep aspect ratio" enabled.
330 let size = transform.invert().transform_size(target_geo.size);
331 let output_aspect_ratio = size.w as f64 / size.h as f64;
332 let ratio = data.aspect_ratio / output_aspect_ratio;
333
334 if ratio > 1. {
335 pos.x *= ratio;
336 } else {
337 pos.y /= ratio;
338 }
339 }
340 };
341
342 pos.x *= target_geo.size.w as f64;
343 pos.y *= target_geo.size.h as f64;
344 }
345
346 pos.x = pos.x.clamp(0.0, target_geo.size.w as f64 - px);
347 pos.y = pos.y.clamp(0.0, target_geo.size.h as f64 - px);
348 Some(pos + target_geo.loc.to_f64())
349 }
350
351 fn is_inhibiting_shortcuts(&self) -> bool {
352 self.niri
353 .keyboard_focus
354 .surface()
355 .and_then(|surface| {
356 self.niri
357 .keyboard_shortcuts_inhibiting_surfaces
358 .get(surface)
359 })
360 .is_some_and(KeyboardShortcutsInhibitor::is_active)
361 }
362
363 fn on_keyboard<I: InputBackend>(
364 &mut self,
365 event: I::KeyboardKeyEvent,
366 consumed_by_a11y: &mut bool,
367 ) {
368 let mod_key = self.backend.mod_key(&self.niri.config.borrow());
369
370 let serial = SERIAL_COUNTER.next_serial();
371 let time = Event::time_msec(&event);
372 let pressed = event.state() == KeyState::Pressed;
373
374 // Stop bind key repeat on any release. This won't work 100% correctly in cases like:
375 // 1. Press Mod
376 // 2. Press Left (repeat starts)
377 // 3. Press PgDown (new repeat starts)
378 // 4. Release Left (PgDown repeat stops)
379 // But it's good enough for now.
380 // FIXME: handle this properly.
381 if !pressed {
382 if let Some(token) = self.niri.bind_repeat_timer.take() {
383 self.niri.event_loop.remove(token);
384 }
385 }
386
387 if pressed {
388 self.hide_cursor_if_needed();
389 }
390
391 let is_inhibiting_shortcuts = self.is_inhibiting_shortcuts();
392
393 // Accessibility modifier grabs should override XKB state changes (e.g. Caps Lock), so we
394 // need to process them before keyboard.input() below.
395 //
396 // Other accessibility-grabbed keys should still update our XKB state, but not cause any
397 // other changes.
398 #[cfg(feature = "dbus")]
399 let block = {
400 let block = self.a11y_process_key(
401 Duration::from_millis(u64::from(time)),
402 event.key_code(),
403 event.state(),
404 );
405 if block != KbMonBlock::Pass {
406 *consumed_by_a11y = true;
407 }
408 // The accessibility modifier first press must not change XKB state, so we return
409 // early here.
410 if block == KbMonBlock::ModifierFirstPress {
411 return;
412 }
413 block
414 };
415 #[cfg(not(feature = "dbus"))]
416 let _ = consumed_by_a11y;
417
418 let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input(
419 self,
420 event.key_code(),
421 event.state(),
422 serial,
423 time,
424 |this, mods, keysym| {
425 let key_code = event.key_code();
426 let modified = keysym.modified_sym();
427 let raw = keysym.raw_latin_sym_or_raw_current_sym();
428 let modifiers = modifiers_from_state(*mods);
429
430 // After updating XKB state from accessibility-grabbed keys, return right away and
431 // don't handle them.
432 #[cfg(feature = "dbus")]
433 if block != KbMonBlock::Pass {
434 // HACK: there's a slight problem with this code. Here we filter out keys
435 // consumed by accessibility from getting sent to the Wayland client. However,
436 // the Wayland client can still receive these keys from the wl_keyboard
437 // enter/modifiers events. In particular, this can easily happen when opening
438 // the Orca actions menu with Orca + Shift + A: in most cases, when this menu
439 // opens, Shift is still held down, so the menu receives it in
440 // wl_keyboard.enter/modifiers. Then the menu won't react to Enter presses
441 // until the user taps Shift again to "release" it (since the initial Shift
442 // release will be intercepted here).
443 //
444 // I don't think there's any good way of dealing with this apart from keeping a
445 // separate xkb state for accessibility, so that we can track the pressed
446 // modifiers without accidentally leaking them to wl_keyboard.enter. So for now
447 // let's forward modifier releases to the clients here to deal with the most
448 // common case.
449 if !pressed
450 && matches!(
451 modified,
452 Keysym::Shift_L
453 | Keysym::Shift_R
454 | Keysym::Control_L
455 | Keysym::Control_R
456 | Keysym::Super_L
457 | Keysym::Super_R
458 | Keysym::Alt_L
459 | Keysym::Alt_R
460 )
461 {
462 return FilterResult::Forward;
463 } else {
464 return FilterResult::Intercept(None);
465 }
466 }
467
468 if this.niri.exit_confirm_dialog.is_open() && pressed {
469 if raw == Some(Keysym::Return) {
470 info!("quitting after confirming exit dialog");
471 this.niri.stop_signal.stop();
472 }
473
474 // Don't send this press to any clients.
475 this.niri.suppressed_keys.insert(key_code);
476 return FilterResult::Intercept(None);
477 }
478
479 // Check if all modifiers were released while the MRU UI was open. If so, close the
480 // UI (which will also transfer the focus to the current MRU UI selection).
481 if this.niri.window_mru_ui.is_open() && !pressed && modifiers.is_empty() {
482 this.do_action(Action::MruConfirm, false);
483
484 if this.niri.suppressed_keys.remove(&key_code) {
485 return FilterResult::Intercept(None);
486 } else {
487 return FilterResult::Forward;
488 }
489 }
490
491 if pressed
492 && raw == Some(Keysym::Escape)
493 && (this.niri.pick_window.is_some() || this.niri.pick_color.is_some())
494 {
495 // We window picking state so the pick window grab must be active.
496 // Unsetting it cancels window picking.
497 this.niri
498 .seat
499 .get_pointer()
500 .unwrap()
501 .unset_grab(this, serial, time);
502 this.niri.suppressed_keys.insert(key_code);
503 return FilterResult::Intercept(None);
504 }
505
506 if let Some(Keysym::space) = raw {
507 this.niri.screenshot_ui.set_space_down(pressed);
508 }
509
510 let res = {
511 let config = this.niri.config.borrow();
512 let bindings =
513 make_binds_iter(&config, &mut this.niri.window_mru_ui, modifiers);
514
515 should_intercept_key(
516 &mut this.niri.suppressed_keys,
517 bindings,
518 mod_key,
519 key_code,
520 modified,
521 raw,
522 pressed,
523 *mods,
524 &this.niri.screenshot_ui,
525 this.niri.config.borrow().input.disable_power_key_handling,
526 is_inhibiting_shortcuts,
527 )
528 };
529
530 if matches!(res, FilterResult::Forward) {
531 // If we didn't find any bind, try other hardcoded keys.
532 if this.niri.keyboard_focus.is_overview() && pressed {
533 if let Some(bind) = raw.and_then(|raw| hardcoded_overview_bind(raw, *mods))
534 {
535 this.niri.suppressed_keys.insert(key_code);
536 return FilterResult::Intercept(Some(bind));
537 }
538 }
539
540 // Interaction with the active window, immediately update the active window's
541 // focus timestamp without waiting for a possible pending MRU lock-in delay.
542 this.niri.mru_apply_keyboard_commit();
543 }
544
545 res
546 },
547 ) else {
548 return;
549 };
550
551 if !pressed {
552 return;
553 }
554
555 self.handle_bind(bind.clone());
556
557 self.start_key_repeat(bind);
558 }
559
560 fn start_key_repeat(&mut self, bind: Bind) {
561 if !bind.repeat {
562 return;
563 }
564
565 // Stop the previous key repeat if any.
566 if let Some(token) = self.niri.bind_repeat_timer.take() {
567 self.niri.event_loop.remove(token);
568 }
569
570 let config = self.niri.config.borrow();
571 let config = &config.input.keyboard;
572
573 let repeat_rate = config.repeat_rate;
574 if repeat_rate == 0 {
575 return;
576 }
577 let repeat_duration = Duration::from_secs_f64(1. / f64::from(repeat_rate));
578
579 let repeat_timer =
580 Timer::from_duration(Duration::from_millis(u64::from(config.repeat_delay)));
581
582 let token = self
583 .niri
584 .event_loop
585 .insert_source(repeat_timer, move |_, _, state| {
586 state.handle_bind(bind.clone());
587 TimeoutAction::ToDuration(repeat_duration)
588 })
589 .unwrap();
590
591 self.niri.bind_repeat_timer = Some(token);
592 }
593
594 fn hide_cursor_if_needed(&mut self) {
595 // If the pointer is already invisible, don't reset it back to Hidden causing one frame
596 // of hover.
597 if !self.niri.pointer_visibility.is_visible() {
598 return;
599 }
600
601 if !self.niri.config.borrow().cursor.hide_when_typing {
602 return;
603 }
604
605 // niri keeps this set only while actively using a tablet, which means the cursor position
606 // is likely to change almost immediately, causing pointer_visibility to just flicker back
607 // and forth.
608 if self.niri.tablet_cursor_location.is_some() {
609 return;
610 }
611
612 self.niri.pointer_visibility = PointerVisibility::Hidden;
613 self.niri.queue_redraw_all();
614 }
615
616 pub fn handle_bind(&mut self, bind: Bind) {
617 let Some(cooldown) = bind.cooldown else {
618 self.do_action(bind.action, bind.allow_when_locked);
619 return;
620 };
621
622 // Check this first so that it doesn't trigger the cooldown.
623 if self.niri.is_locked() && !(bind.allow_when_locked || allowed_when_locked(&bind.action)) {
624 return;
625 }
626
627 match self.niri.bind_cooldown_timers.entry(bind.key) {
628 // The bind is on cooldown.
629 Entry::Occupied(_) => (),
630 Entry::Vacant(entry) => {
631 let timer = Timer::from_duration(cooldown);
632 let token = self
633 .niri
634 .event_loop
635 .insert_source(timer, move |_, _, state| {
636 if state.niri.bind_cooldown_timers.remove(&bind.key).is_none() {
637 error!("bind cooldown timer entry disappeared");
638 }
639 TimeoutAction::Drop
640 })
641 .unwrap();
642 entry.insert(token);
643
644 self.do_action(bind.action, bind.allow_when_locked);
645 }
646 }
647 }
648
649 pub fn do_action(&mut self, action: Action, allow_when_locked: bool) {
650 if self.niri.is_locked() && !(allow_when_locked || allowed_when_locked(&action)) {
651 return;
652 }
653
654 if let Some(touch) = self.niri.seat.get_touch() {
655 touch.cancel(self);
656 }
657
658 match action {
659 Action::Quit(skip_confirmation) => {
660 if !skip_confirmation && self.niri.exit_confirm_dialog.show() {
661 self.niri.queue_redraw_all();
662 return;
663 }
664
665 info!("quitting as requested");
666 self.niri.stop_signal.stop()
667 }
668 Action::ChangeVt(vt) => {
669 self.backend.change_vt(vt);
670 // Changing VT may not deliver the key releases, so clear the state.
671 self.niri.suppressed_keys.clear();
672 }
673 Action::Suspend => {
674 self.backend.suspend();
675 // Suspend may not deliver the key releases, so clear the state.
676 self.niri.suppressed_keys.clear();
677 }
678 Action::PowerOffMonitors => {
679 self.niri.deactivate_monitors(&mut self.backend);
680 }
681 Action::PowerOnMonitors => {
682 self.niri.activate_monitors(&mut self.backend);
683 }
684 Action::ToggleDebugTint => {
685 self.backend.toggle_debug_tint();
686 self.niri.queue_redraw_all();
687 }
688 Action::DebugToggleOpaqueRegions => {
689 self.niri.debug_draw_opaque_regions = !self.niri.debug_draw_opaque_regions;
690 self.niri.queue_redraw_all();
691 }
692 Action::DebugToggleDamage => {
693 self.niri.debug_toggle_damage();
694 }
695 Action::Spawn(command) => {
696 let (token, _) = self.niri.activation_state.create_external_token(None);
697 spawn(command, Some(token.clone()));
698 }
699 Action::SpawnSh(command) => {
700 let (token, _) = self.niri.activation_state.create_external_token(None);
701 spawn_sh(command, Some(token.clone()));
702 }
703 Action::DoScreenTransition(delay_ms) => {
704 self.backend.with_primary_renderer(|renderer| {
705 self.niri.do_screen_transition(renderer, delay_ms);
706 });
707 }
708 Action::ScreenshotScreen(write_to_disk, show_pointer, path) => {
709 let active = self.niri.layout.active_output().cloned();
710 if let Some(active) = active {
711 self.backend.with_primary_renderer(|renderer| {
712 if let Err(err) = self.niri.screenshot(
713 renderer,
714 &active,
715 write_to_disk,
716 show_pointer,
717 path,
718 ) {
719 warn!("error taking screenshot: {err:?}");
720 }
721 });
722 }
723 }
724 Action::ConfirmScreenshot { write_to_disk } => {
725 self.confirm_screenshot(write_to_disk);
726 }
727 Action::CancelScreenshot => {
728 if !self.niri.screenshot_ui.is_open() {
729 return;
730 }
731
732 self.niri.screenshot_ui.close();
733 self.niri
734 .cursor_manager
735 .set_cursor_image(CursorImageStatus::default_named());
736 self.niri.queue_redraw_all();
737 }
738 Action::ScreenshotTogglePointer => {
739 self.niri.screenshot_ui.toggle_pointer();
740 self.niri.queue_redraw_all();
741 }
742 Action::Screenshot(show_cursor, path) => {
743 self.open_screenshot_ui(show_cursor, path);
744 self.niri.cancel_mru();
745 }
746 Action::ScreenshotWindow(write_to_disk, show_pointer, path) => {
747 let focus = self.niri.layout.focus_with_output();
748 if let Some((mapped, output)) = focus {
749 self.backend.with_primary_renderer(|renderer| {
750 if let Err(err) = self.niri.screenshot_window(
751 renderer,
752 output,
753 mapped,
754 write_to_disk,
755 show_pointer,
756 path,
757 ) {
758 warn!("error taking screenshot: {err:?}");
759 }
760 });
761 }
762 }
763 Action::ScreenshotWindowById {
764 id,
765 write_to_disk,
766 show_pointer,
767 path,
768 } => {
769 let mut windows = self.niri.layout.windows();
770 let window = windows.find(|(_, m)| m.id().get() == id);
771 if let Some((Some(monitor), mapped)) = window {
772 let output = monitor.output();
773 self.backend.with_primary_renderer(|renderer| {
774 if let Err(err) = self.niri.screenshot_window(
775 renderer,
776 output,
777 mapped,
778 write_to_disk,
779 show_pointer,
780 path,
781 ) {
782 warn!("error taking screenshot: {err:?}");
783 }
784 });
785 }
786 }
787 Action::ToggleKeyboardShortcutsInhibit => {
788 if let Some(inhibitor) = self.niri.keyboard_focus.surface().and_then(|surface| {
789 self.niri
790 .keyboard_shortcuts_inhibiting_surfaces
791 .get(surface)
792 }) {
793 if inhibitor.is_active() {
794 inhibitor.inactivate();
795 } else {
796 inhibitor.activate();
797 }
798 }
799 }
800 Action::CloseWindow => {
801 if let Some(mapped) = self.niri.layout.focus() {
802 mapped.toplevel().send_close();
803 }
804 }
805 Action::CloseWindowById(id) => {
806 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
807 if let Some((_, mapped)) = window {
808 mapped.toplevel().send_close();
809 }
810 }
811 Action::FullscreenWindow => {
812 let focus = self.niri.layout.focus().map(|m| m.window.clone());
813 if let Some(window) = focus {
814 self.niri.layout.toggle_fullscreen(&window);
815 // FIXME: granular
816 self.niri.queue_redraw_all();
817 }
818 }
819 Action::FullscreenWindowById(id) => {
820 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
821 let window = window.map(|(_, m)| m.window.clone());
822 if let Some(window) = window {
823 self.niri.layout.toggle_fullscreen(&window);
824 // FIXME: granular
825 self.niri.queue_redraw_all();
826 }
827 }
828 Action::ToggleWindowedFullscreen => {
829 let focus = self.niri.layout.focus().map(|m| m.window.clone());
830 if let Some(window) = focus {
831 self.niri.layout.toggle_windowed_fullscreen(&window);
832 // FIXME: granular
833 self.niri.queue_redraw_all();
834 }
835 }
836 Action::ToggleWindowedFullscreenById(id) => {
837 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
838 let window = window.map(|(_, m)| m.window.clone());
839 if let Some(window) = window {
840 self.niri.layout.toggle_windowed_fullscreen(&window);
841 // FIXME: granular
842 self.niri.queue_redraw_all();
843 }
844 }
845 Action::FocusWindow(id) => {
846 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
847 let window = window.map(|(_, m)| m.window.clone());
848 if let Some(window) = window {
849 self.focus_window(&window);
850 }
851 }
852 Action::FocusWindowInColumn(index) => {
853 self.niri.layout.focus_window_in_column(index);
854 self.maybe_warp_cursor_to_focus();
855 self.niri.layer_shell_on_demand_focus = None;
856 // FIXME: granular
857 self.niri.queue_redraw_all();
858 }
859 Action::FocusWindowPrevious => {
860 let current = self.niri.layout.focus().map(|win| win.id());
861 if let Some(window) = self
862 .niri
863 .layout
864 .windows()
865 .map(|(_, win)| win)
866 .filter(|win| Some(win.id()) != current)
867 .max_by_key(|win| win.get_focus_timestamp())
868 .map(|win| win.window.clone())
869 {
870 // Commit current focus so repeated focus-window-previous works as expected.
871 self.niri.mru_apply_keyboard_commit();
872
873 self.focus_window(&window);
874 }
875 }
876 Action::SwitchLayout(action) => {
877 let keyboard = &self.niri.seat.get_keyboard().unwrap();
878 keyboard.with_xkb_state(self, |mut state| match action {
879 LayoutSwitchTarget::Next => state.cycle_next_layout(),
880 LayoutSwitchTarget::Prev => state.cycle_prev_layout(),
881 LayoutSwitchTarget::Index(layout) => {
882 let num_layouts = state.xkb().lock().unwrap().layouts().count();
883 if usize::from(layout) >= num_layouts {
884 warn!("requested layout doesn't exist")
885 } else {
886 state.set_layout(Layout(layout.into()))
887 }
888 }
889 });
890 }
891 Action::MoveColumnLeft => {
892 if self.niri.screenshot_ui.is_open() {
893 self.niri.screenshot_ui.move_left();
894 } else {
895 self.niri.layout.move_left();
896 self.maybe_warp_cursor_to_focus();
897 }
898
899 // FIXME: granular
900 self.niri.queue_redraw_all();
901 }
902 Action::MoveColumnRight => {
903 if self.niri.screenshot_ui.is_open() {
904 self.niri.screenshot_ui.move_right();
905 } else {
906 self.niri.layout.move_right();
907 self.maybe_warp_cursor_to_focus();
908 }
909
910 // FIXME: granular
911 self.niri.queue_redraw_all();
912 }
913 Action::MoveColumnToFirst => {
914 self.niri.layout.move_column_to_first();
915 self.maybe_warp_cursor_to_focus();
916 // FIXME: granular
917 self.niri.queue_redraw_all();
918 }
919 Action::MoveColumnToLast => {
920 self.niri.layout.move_column_to_last();
921 self.maybe_warp_cursor_to_focus();
922 // FIXME: granular
923 self.niri.queue_redraw_all();
924 }
925 Action::MoveColumnLeftOrToMonitorLeft => {
926 if self.niri.screenshot_ui.is_open() {
927 self.niri.screenshot_ui.move_left();
928 } else if let Some(output) = self.niri.output_left() {
929 if self.niri.layout.move_column_left_or_to_output(&output)
930 && !self.maybe_warp_cursor_to_focus_centered()
931 {
932 self.move_cursor_to_output(&output);
933 } else {
934 self.maybe_warp_cursor_to_focus();
935 }
936 } else {
937 self.niri.layout.move_left();
938 self.maybe_warp_cursor_to_focus();
939 }
940
941 // FIXME: granular
942 self.niri.queue_redraw_all();
943 }
944 Action::MoveColumnRightOrToMonitorRight => {
945 if self.niri.screenshot_ui.is_open() {
946 self.niri.screenshot_ui.move_right();
947 } else if let Some(output) = self.niri.output_right() {
948 if self.niri.layout.move_column_right_or_to_output(&output)
949 && !self.maybe_warp_cursor_to_focus_centered()
950 {
951 self.move_cursor_to_output(&output);
952 } else {
953 self.maybe_warp_cursor_to_focus();
954 }
955 } else {
956 self.niri.layout.move_right();
957 self.maybe_warp_cursor_to_focus();
958 }
959
960 // FIXME: granular
961 self.niri.queue_redraw_all();
962 }
963 Action::MoveWindowDown => {
964 if self.niri.screenshot_ui.is_open() {
965 self.niri.screenshot_ui.move_down();
966 } else {
967 self.niri.layout.move_down();
968 self.maybe_warp_cursor_to_focus();
969 }
970
971 // FIXME: granular
972 self.niri.queue_redraw_all();
973 }
974 Action::MoveWindowUp => {
975 if self.niri.screenshot_ui.is_open() {
976 self.niri.screenshot_ui.move_up();
977 } else {
978 self.niri.layout.move_up();
979 self.maybe_warp_cursor_to_focus();
980 }
981
982 // FIXME: granular
983 self.niri.queue_redraw_all();
984 }
985 Action::MoveWindowDownOrToWorkspaceDown => {
986 if self.niri.screenshot_ui.is_open() {
987 self.niri.screenshot_ui.move_down();
988 } else {
989 self.niri.layout.move_down_or_to_workspace_down();
990 self.maybe_warp_cursor_to_focus();
991 }
992 // FIXME: granular
993 self.niri.queue_redraw_all();
994 }
995 Action::MoveWindowUpOrToWorkspaceUp => {
996 if self.niri.screenshot_ui.is_open() {
997 self.niri.screenshot_ui.move_up();
998 } else {
999 self.niri.layout.move_up_or_to_workspace_up();
1000 self.maybe_warp_cursor_to_focus();
1001 }
1002 // FIXME: granular
1003 self.niri.queue_redraw_all();
1004 }
1005 Action::ConsumeOrExpelWindowLeft => {
1006 self.niri.layout.consume_or_expel_window_left(None);
1007 self.maybe_warp_cursor_to_focus();
1008 // FIXME: granular
1009 self.niri.queue_redraw_all();
1010 }
1011 Action::ConsumeOrExpelWindowLeftById(id) => {
1012 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1013 let window = window.map(|(_, m)| m.window.clone());
1014 if let Some(window) = window {
1015 self.niri.layout.consume_or_expel_window_left(Some(&window));
1016 self.maybe_warp_cursor_to_focus();
1017 // FIXME: granular
1018 self.niri.queue_redraw_all();
1019 }
1020 }
1021 Action::ConsumeOrExpelWindowRight => {
1022 self.niri.layout.consume_or_expel_window_right(None);
1023 self.maybe_warp_cursor_to_focus();
1024 // FIXME: granular
1025 self.niri.queue_redraw_all();
1026 }
1027 Action::ConsumeOrExpelWindowRightById(id) => {
1028 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1029 let window = window.map(|(_, m)| m.window.clone());
1030 if let Some(window) = window {
1031 self.niri
1032 .layout
1033 .consume_or_expel_window_right(Some(&window));
1034 self.maybe_warp_cursor_to_focus();
1035 // FIXME: granular
1036 self.niri.queue_redraw_all();
1037 }
1038 }
1039 Action::FocusColumnLeft => {
1040 self.niri.layout.focus_left();
1041 self.maybe_warp_cursor_to_focus();
1042 self.niri.layer_shell_on_demand_focus = None;
1043 // FIXME: granular
1044 self.niri.queue_redraw_all();
1045 }
1046 Action::FocusColumnLeftUnderMouse => {
1047 if let Some((output, ws)) = self.niri.workspace_under_cursor(true) {
1048 let ws_id = ws.id();
1049 let ws = {
1050 let mut workspaces = self.niri.layout.workspaces_mut();
1051 workspaces.find(|ws| ws.id() == ws_id).unwrap()
1052 };
1053 ws.focus_left();
1054 self.maybe_warp_cursor_to_focus();
1055 self.niri.layer_shell_on_demand_focus = None;
1056 self.niri.queue_redraw(&output);
1057 }
1058 }
1059 Action::FocusColumnRight => {
1060 self.niri.layout.focus_right();
1061 self.maybe_warp_cursor_to_focus();
1062 self.niri.layer_shell_on_demand_focus = None;
1063 // FIXME: granular
1064 self.niri.queue_redraw_all();
1065 }
1066 Action::FocusColumnRightUnderMouse => {
1067 if let Some((output, ws)) = self.niri.workspace_under_cursor(true) {
1068 let ws_id = ws.id();
1069 let ws = {
1070 let mut workspaces = self.niri.layout.workspaces_mut();
1071 workspaces.find(|ws| ws.id() == ws_id).unwrap()
1072 };
1073 ws.focus_right();
1074 self.maybe_warp_cursor_to_focus();
1075 self.niri.layer_shell_on_demand_focus = None;
1076 self.niri.queue_redraw(&output);
1077 }
1078 }
1079 Action::FocusColumnFirst => {
1080 self.niri.layout.focus_column_first();
1081 self.maybe_warp_cursor_to_focus();
1082 self.niri.layer_shell_on_demand_focus = None;
1083 // FIXME: granular
1084 self.niri.queue_redraw_all();
1085 }
1086 Action::FocusColumnLast => {
1087 self.niri.layout.focus_column_last();
1088 self.maybe_warp_cursor_to_focus();
1089 self.niri.layer_shell_on_demand_focus = None;
1090 // FIXME: granular
1091 self.niri.queue_redraw_all();
1092 }
1093 Action::FocusColumnRightOrFirst => {
1094 self.niri.layout.focus_column_right_or_first();
1095 self.maybe_warp_cursor_to_focus();
1096 self.niri.layer_shell_on_demand_focus = None;
1097 // FIXME: granular
1098 self.niri.queue_redraw_all();
1099 }
1100 Action::FocusColumnLeftOrLast => {
1101 self.niri.layout.focus_column_left_or_last();
1102 self.maybe_warp_cursor_to_focus();
1103 self.niri.layer_shell_on_demand_focus = None;
1104 // FIXME: granular
1105 self.niri.queue_redraw_all();
1106 }
1107 Action::FocusColumn(index) => {
1108 self.niri.layout.focus_column(index);
1109 self.maybe_warp_cursor_to_focus();
1110 self.niri.layer_shell_on_demand_focus = None;
1111 // FIXME: granular
1112 self.niri.queue_redraw_all();
1113 }
1114 Action::FocusWindowOrMonitorUp => {
1115 if let Some(output) = self.niri.output_up() {
1116 if self.niri.layout.focus_window_up_or_output(&output)
1117 && !self.maybe_warp_cursor_to_focus_centered()
1118 {
1119 self.move_cursor_to_output(&output);
1120 } else {
1121 self.maybe_warp_cursor_to_focus();
1122 }
1123 } else {
1124 self.niri.layout.focus_up();
1125 self.maybe_warp_cursor_to_focus();
1126 }
1127 self.niri.layer_shell_on_demand_focus = None;
1128
1129 // FIXME: granular
1130 self.niri.queue_redraw_all();
1131 }
1132 Action::FocusWindowOrMonitorDown => {
1133 if let Some(output) = self.niri.output_down() {
1134 if self.niri.layout.focus_window_down_or_output(&output)
1135 && !self.maybe_warp_cursor_to_focus_centered()
1136 {
1137 self.move_cursor_to_output(&output);
1138 } else {
1139 self.maybe_warp_cursor_to_focus();
1140 }
1141 } else {
1142 self.niri.layout.focus_down();
1143 self.maybe_warp_cursor_to_focus();
1144 }
1145 self.niri.layer_shell_on_demand_focus = None;
1146
1147 // FIXME: granular
1148 self.niri.queue_redraw_all();
1149 }
1150 Action::FocusColumnOrMonitorLeft => {
1151 if let Some(output) = self.niri.output_left() {
1152 if self.niri.layout.focus_column_left_or_output(&output)
1153 && !self.maybe_warp_cursor_to_focus_centered()
1154 {
1155 self.move_cursor_to_output(&output);
1156 } else {
1157 self.maybe_warp_cursor_to_focus();
1158 }
1159 } else {
1160 self.niri.layout.focus_left();
1161 self.maybe_warp_cursor_to_focus();
1162 }
1163 self.niri.layer_shell_on_demand_focus = None;
1164
1165 // FIXME: granular
1166 self.niri.queue_redraw_all();
1167 }
1168 Action::FocusColumnOrMonitorRight => {
1169 if let Some(output) = self.niri.output_right() {
1170 if self.niri.layout.focus_column_right_or_output(&output)
1171 && !self.maybe_warp_cursor_to_focus_centered()
1172 {
1173 self.move_cursor_to_output(&output);
1174 } else {
1175 self.maybe_warp_cursor_to_focus();
1176 }
1177 } else {
1178 self.niri.layout.focus_right();
1179 self.maybe_warp_cursor_to_focus();
1180 }
1181 self.niri.layer_shell_on_demand_focus = None;
1182
1183 // FIXME: granular
1184 self.niri.queue_redraw_all();
1185 }
1186 Action::FocusWindowDown => {
1187 self.niri.layout.focus_down();
1188 self.maybe_warp_cursor_to_focus();
1189 self.niri.layer_shell_on_demand_focus = None;
1190 // FIXME: granular
1191 self.niri.queue_redraw_all();
1192 }
1193 Action::FocusWindowUp => {
1194 self.niri.layout.focus_up();
1195 self.maybe_warp_cursor_to_focus();
1196 self.niri.layer_shell_on_demand_focus = None;
1197 // FIXME: granular
1198 self.niri.queue_redraw_all();
1199 }
1200 Action::FocusWindowDownOrColumnLeft => {
1201 self.niri.layout.focus_down_or_left();
1202 self.maybe_warp_cursor_to_focus();
1203 self.niri.layer_shell_on_demand_focus = None;
1204 // FIXME: granular
1205 self.niri.queue_redraw_all();
1206 }
1207 Action::FocusWindowDownOrColumnRight => {
1208 self.niri.layout.focus_down_or_right();
1209 self.maybe_warp_cursor_to_focus();
1210 self.niri.layer_shell_on_demand_focus = None;
1211 // FIXME: granular
1212 self.niri.queue_redraw_all();
1213 }
1214 Action::FocusWindowUpOrColumnLeft => {
1215 self.niri.layout.focus_up_or_left();
1216 self.maybe_warp_cursor_to_focus();
1217 self.niri.layer_shell_on_demand_focus = None;
1218 // FIXME: granular
1219 self.niri.queue_redraw_all();
1220 }
1221 Action::FocusWindowUpOrColumnRight => {
1222 self.niri.layout.focus_up_or_right();
1223 self.maybe_warp_cursor_to_focus();
1224 self.niri.layer_shell_on_demand_focus = None;
1225 // FIXME: granular
1226 self.niri.queue_redraw_all();
1227 }
1228 Action::FocusWindowOrWorkspaceDown => {
1229 self.niri.layout.focus_window_or_workspace_down();
1230 self.maybe_warp_cursor_to_focus();
1231 self.niri.layer_shell_on_demand_focus = None;
1232 // FIXME: granular
1233 self.niri.queue_redraw_all();
1234 }
1235 Action::FocusWindowOrWorkspaceUp => {
1236 self.niri.layout.focus_window_or_workspace_up();
1237 self.maybe_warp_cursor_to_focus();
1238 self.niri.layer_shell_on_demand_focus = None;
1239 // FIXME: granular
1240 self.niri.queue_redraw_all();
1241 }
1242 Action::FocusWindowTop => {
1243 self.niri.layout.focus_window_top();
1244 self.maybe_warp_cursor_to_focus();
1245 self.niri.layer_shell_on_demand_focus = None;
1246 // FIXME: granular
1247 self.niri.queue_redraw_all();
1248 }
1249 Action::FocusWindowBottom => {
1250 self.niri.layout.focus_window_bottom();
1251 self.maybe_warp_cursor_to_focus();
1252 self.niri.layer_shell_on_demand_focus = None;
1253 // FIXME: granular
1254 self.niri.queue_redraw_all();
1255 }
1256 Action::FocusWindowDownOrTop => {
1257 self.niri.layout.focus_window_down_or_top();
1258 self.maybe_warp_cursor_to_focus();
1259 self.niri.layer_shell_on_demand_focus = None;
1260 // FIXME: granular
1261 self.niri.queue_redraw_all();
1262 }
1263 Action::FocusWindowUpOrBottom => {
1264 self.niri.layout.focus_window_up_or_bottom();
1265 self.maybe_warp_cursor_to_focus();
1266 self.niri.layer_shell_on_demand_focus = None;
1267 // FIXME: granular
1268 self.niri.queue_redraw_all();
1269 }
1270 Action::MoveWindowToWorkspaceDown(focus) => {
1271 self.niri.layout.move_to_workspace_down(focus);
1272 self.maybe_warp_cursor_to_focus();
1273 // FIXME: granular
1274 self.niri.queue_redraw_all();
1275 }
1276 Action::MoveWindowToWorkspaceUp(focus) => {
1277 self.niri.layout.move_to_workspace_up(focus);
1278 self.maybe_warp_cursor_to_focus();
1279 // FIXME: granular
1280 self.niri.queue_redraw_all();
1281 }
1282 Action::MoveWindowToWorkspace(reference, focus) => {
1283 if let Some((mut output, index)) =
1284 self.niri.find_output_and_workspace_index(reference)
1285 {
1286 // The source output is always the active output, so if the target output is
1287 // also the active output, we don't need to use move_to_output().
1288 if let Some(active) = self.niri.layout.active_output() {
1289 if output.as_ref() == Some(active) {
1290 output = None;
1291 }
1292 }
1293
1294 let activate = if focus {
1295 ActivateWindow::Smart
1296 } else {
1297 ActivateWindow::No
1298 };
1299
1300 if let Some(output) = output {
1301 self.niri
1302 .layout
1303 .move_to_output(None, &output, Some(index), activate);
1304
1305 if focus {
1306 if !self.maybe_warp_cursor_to_focus_centered() {
1307 self.move_cursor_to_output(&output);
1308 }
1309 } else {
1310 self.maybe_warp_cursor_to_focus();
1311 }
1312 } else {
1313 self.niri.layout.move_to_workspace(None, index, activate);
1314 self.maybe_warp_cursor_to_focus();
1315 }
1316
1317 // FIXME: granular
1318 self.niri.queue_redraw_all();
1319 }
1320 }
1321 Action::MoveWindowToWorkspaceById {
1322 window_id: id,
1323 reference,
1324 focus,
1325 } => {
1326 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1327 let window = window.map(|(_, m)| m.window.clone());
1328 if let Some(window) = window {
1329 if let Some((output, index)) =
1330 self.niri.find_output_and_workspace_index(reference)
1331 {
1332 let target_was_active = self
1333 .niri
1334 .layout
1335 .active_output()
1336 .is_some_and(|active| output.as_ref() == Some(active));
1337
1338 let activate = if focus {
1339 ActivateWindow::Smart
1340 } else {
1341 ActivateWindow::No
1342 };
1343
1344 if let Some(output) = output {
1345 self.niri.layout.move_to_output(
1346 Some(&window),
1347 &output,
1348 Some(index),
1349 activate,
1350 );
1351
1352 // If the active output changed (window was moved and focused).
1353 #[allow(clippy::collapsible_if)]
1354 if !target_was_active
1355 && self.niri.layout.active_output() == Some(&output)
1356 {
1357 if !self.maybe_warp_cursor_to_focus_centered() {
1358 self.move_cursor_to_output(&output);
1359 }
1360 }
1361 } else {
1362 self.niri
1363 .layout
1364 .move_to_workspace(Some(&window), index, activate);
1365
1366 // If we focused the target window.
1367 let new_focus = self.niri.layout.focus();
1368 if new_focus.is_some_and(|win| win.window == window) {
1369 self.maybe_warp_cursor_to_focus();
1370 }
1371 }
1372
1373 // FIXME: granular
1374 self.niri.queue_redraw_all();
1375 }
1376 }
1377 }
1378 Action::MoveColumnToWorkspaceDown(focus) => {
1379 self.niri.layout.move_column_to_workspace_down(focus);
1380 self.maybe_warp_cursor_to_focus();
1381 // FIXME: granular
1382 self.niri.queue_redraw_all();
1383 }
1384 Action::MoveColumnToWorkspaceUp(focus) => {
1385 self.niri.layout.move_column_to_workspace_up(focus);
1386 self.maybe_warp_cursor_to_focus();
1387 // FIXME: granular
1388 self.niri.queue_redraw_all();
1389 }
1390 Action::MoveColumnToWorkspace(reference, focus) => {
1391 if let Some((mut output, index)) =
1392 self.niri.find_output_and_workspace_index(reference)
1393 {
1394 if let Some(active) = self.niri.layout.active_output() {
1395 if output.as_ref() == Some(active) {
1396 output = None;
1397 }
1398 }
1399
1400 if let Some(output) = output {
1401 self.niri
1402 .layout
1403 .move_column_to_output(&output, Some(index), focus);
1404 if focus && !self.maybe_warp_cursor_to_focus_centered() {
1405 self.move_cursor_to_output(&output);
1406 }
1407 } else {
1408 self.niri.layout.move_column_to_workspace(index, focus);
1409 if focus {
1410 self.maybe_warp_cursor_to_focus();
1411 }
1412 }
1413
1414 // FIXME: granular
1415 self.niri.queue_redraw_all();
1416 }
1417 }
1418 Action::MoveColumnToIndex(idx) => {
1419 self.niri.layout.move_column_to_index(idx);
1420 self.maybe_warp_cursor_to_focus();
1421 // FIXME: granular
1422 self.niri.queue_redraw_all();
1423 }
1424 Action::FocusWorkspaceDown => {
1425 self.niri.layout.switch_workspace_down();
1426 self.maybe_warp_cursor_to_focus();
1427 self.niri.layer_shell_on_demand_focus = None;
1428 // FIXME: granular
1429 self.niri.queue_redraw_all();
1430 }
1431 Action::FocusWorkspaceDownUnderMouse => {
1432 if let Some(output) = self.niri.output_under_cursor() {
1433 if let Some(mon) = self.niri.layout.monitor_for_output_mut(&output) {
1434 mon.switch_workspace_down();
1435 self.maybe_warp_cursor_to_focus();
1436 self.niri.layer_shell_on_demand_focus = None;
1437 self.niri.queue_redraw(&output);
1438 }
1439 }
1440 }
1441 Action::FocusWorkspaceUp => {
1442 self.niri.layout.switch_workspace_up();
1443 self.maybe_warp_cursor_to_focus();
1444 self.niri.layer_shell_on_demand_focus = None;
1445 // FIXME: granular
1446 self.niri.queue_redraw_all();
1447 }
1448 Action::FocusWorkspaceUpUnderMouse => {
1449 if let Some(output) = self.niri.output_under_cursor() {
1450 if let Some(mon) = self.niri.layout.monitor_for_output_mut(&output) {
1451 mon.switch_workspace_up();
1452 self.maybe_warp_cursor_to_focus();
1453 self.niri.layer_shell_on_demand_focus = None;
1454 self.niri.queue_redraw(&output);
1455 }
1456 }
1457 }
1458 Action::FocusWorkspace(reference) => {
1459 if let Some((mut output, index)) =
1460 self.niri.find_output_and_workspace_index(reference)
1461 {
1462 if let Some(active) = self.niri.layout.active_output() {
1463 if output.as_ref() == Some(active) {
1464 output = None;
1465 }
1466 }
1467
1468 if let Some(output) = output {
1469 self.niri.layout.focus_output(&output);
1470 self.niri.layout.switch_workspace(index);
1471 if !self.maybe_warp_cursor_to_focus_centered() {
1472 self.move_cursor_to_output(&output);
1473 }
1474 } else {
1475 let config = &self.niri.config;
1476 if config.borrow().input.workspace_auto_back_and_forth {
1477 self.niri.layout.switch_workspace_auto_back_and_forth(index);
1478 } else {
1479 self.niri.layout.switch_workspace(index);
1480 }
1481 self.maybe_warp_cursor_to_focus();
1482 }
1483 self.niri.layer_shell_on_demand_focus = None;
1484
1485 // FIXME: granular
1486 self.niri.queue_redraw_all();
1487 }
1488 }
1489 Action::FocusWorkspacePrevious => {
1490 self.niri.layout.switch_workspace_previous();
1491 self.maybe_warp_cursor_to_focus();
1492 self.niri.layer_shell_on_demand_focus = None;
1493 // FIXME: granular
1494 self.niri.queue_redraw_all();
1495 }
1496 Action::MoveWorkspaceDown => {
1497 self.niri.layout.move_workspace_down();
1498 // FIXME: granular
1499 self.niri.queue_redraw_all();
1500 }
1501 Action::MoveWorkspaceUp => {
1502 self.niri.layout.move_workspace_up();
1503 // FIXME: granular
1504 self.niri.queue_redraw_all();
1505 }
1506 Action::MoveWorkspaceToIndex(new_idx) => {
1507 let new_idx = new_idx.saturating_sub(1);
1508 self.niri.layout.move_workspace_to_idx(None, new_idx);
1509 // FIXME: granular
1510 self.niri.queue_redraw_all();
1511 }
1512 Action::MoveWorkspaceToIndexByRef { new_idx, reference } => {
1513 if let Some(res) = self.niri.find_output_and_workspace_index(reference) {
1514 let new_idx = new_idx.saturating_sub(1);
1515 self.niri.layout.move_workspace_to_idx(Some(res), new_idx);
1516 // FIXME: granular
1517 self.niri.queue_redraw_all();
1518 }
1519 }
1520 Action::SetWorkspaceName(name) => {
1521 self.niri.layout.set_workspace_name(name, None);
1522 }
1523 Action::SetWorkspaceNameByRef { name, reference } => {
1524 self.niri.layout.set_workspace_name(name, Some(reference));
1525 }
1526 Action::UnsetWorkspaceName => {
1527 self.niri.layout.unset_workspace_name(None);
1528 }
1529 Action::UnsetWorkSpaceNameByRef(reference) => {
1530 self.niri.layout.unset_workspace_name(Some(reference));
1531 }
1532 Action::ConsumeWindowIntoColumn => {
1533 self.niri.layout.consume_into_column();
1534 // This does not cause immediate focus or window size change, so warping mouse to
1535 // focus won't do anything here.
1536 // FIXME: granular
1537 self.niri.queue_redraw_all();
1538 }
1539 Action::ExpelWindowFromColumn => {
1540 self.niri.layout.expel_from_column();
1541 self.maybe_warp_cursor_to_focus();
1542 // FIXME: granular
1543 self.niri.queue_redraw_all();
1544 }
1545 Action::SwapWindowRight => {
1546 self.niri
1547 .layout
1548 .swap_window_in_direction(ScrollDirection::Right);
1549 self.maybe_warp_cursor_to_focus();
1550 // FIXME: granular
1551 self.niri.queue_redraw_all();
1552 }
1553 Action::SwapWindowLeft => {
1554 self.niri
1555 .layout
1556 .swap_window_in_direction(ScrollDirection::Left);
1557 self.maybe_warp_cursor_to_focus();
1558 // FIXME: granular
1559 self.niri.queue_redraw_all();
1560 }
1561 Action::ToggleColumnTabbedDisplay => {
1562 self.niri.layout.toggle_column_tabbed_display();
1563 self.maybe_warp_cursor_to_focus();
1564 // FIXME: granular
1565 self.niri.queue_redraw_all();
1566 }
1567 Action::SetColumnDisplay(display) => {
1568 self.niri.layout.set_column_display(display);
1569 self.maybe_warp_cursor_to_focus();
1570 // FIXME: granular
1571 self.niri.queue_redraw_all();
1572 }
1573 Action::SwitchPresetColumnWidth => {
1574 self.niri.layout.toggle_width(true);
1575 }
1576 Action::SwitchPresetColumnWidthBack => {
1577 self.niri.layout.toggle_width(false);
1578 }
1579 Action::SwitchPresetWindowWidth => {
1580 self.niri.layout.toggle_window_width(None, true);
1581 }
1582 Action::SwitchPresetWindowWidthBack => {
1583 self.niri.layout.toggle_window_width(None, false);
1584 }
1585 Action::SwitchPresetWindowWidthById(id) => {
1586 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1587 let window = window.map(|(_, m)| m.window.clone());
1588 if let Some(window) = window {
1589 self.niri.layout.toggle_window_width(Some(&window), true);
1590 }
1591 }
1592 Action::SwitchPresetWindowWidthBackById(id) => {
1593 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1594 let window = window.map(|(_, m)| m.window.clone());
1595 if let Some(window) = window {
1596 self.niri.layout.toggle_window_width(Some(&window), false);
1597 }
1598 }
1599 Action::SwitchPresetWindowHeight => {
1600 self.niri.layout.toggle_window_height(None, true);
1601 }
1602 Action::SwitchPresetWindowHeightBack => {
1603 self.niri.layout.toggle_window_height(None, false);
1604 }
1605 Action::SwitchPresetWindowHeightById(id) => {
1606 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1607 let window = window.map(|(_, m)| m.window.clone());
1608 if let Some(window) = window {
1609 self.niri.layout.toggle_window_height(Some(&window), true);
1610 }
1611 }
1612 Action::SwitchPresetWindowHeightBackById(id) => {
1613 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1614 let window = window.map(|(_, m)| m.window.clone());
1615 if let Some(window) = window {
1616 self.niri.layout.toggle_window_height(Some(&window), false);
1617 }
1618 }
1619 Action::CenterColumn => {
1620 self.niri.layout.center_column();
1621 // FIXME: granular
1622 self.niri.queue_redraw_all();
1623 }
1624 Action::CenterWindow => {
1625 self.niri.layout.center_window(None);
1626 // FIXME: granular
1627 self.niri.queue_redraw_all();
1628 }
1629 Action::CenterWindowById(id) => {
1630 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1631 let window = window.map(|(_, m)| m.window.clone());
1632 if let Some(window) = window {
1633 self.niri.layout.center_window(Some(&window));
1634 // FIXME: granular
1635 self.niri.queue_redraw_all();
1636 }
1637 }
1638 Action::CenterVisibleColumns => {
1639 self.niri.layout.center_visible_columns();
1640 // FIXME: granular
1641 self.niri.queue_redraw_all();
1642 }
1643 Action::MaximizeColumn => {
1644 self.niri.layout.toggle_full_width();
1645 }
1646 Action::MaximizeWindowToEdges => {
1647 let focus = self.niri.layout.focus().map(|m| m.window.clone());
1648 if let Some(window) = focus {
1649 self.niri.layout.toggle_maximized(&window);
1650 // FIXME: granular
1651 self.niri.queue_redraw_all();
1652 }
1653 }
1654 Action::MaximizeWindowToEdgesById(id) => {
1655 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1656 let window = window.map(|(_, m)| m.window.clone());
1657 if let Some(window) = window {
1658 self.niri.layout.toggle_maximized(&window);
1659 // FIXME: granular
1660 self.niri.queue_redraw_all();
1661 }
1662 }
1663 Action::FocusMonitorLeft => {
1664 if let Some(output) = self.niri.output_left() {
1665 self.niri.layout.focus_output(&output);
1666 if !self.maybe_warp_cursor_to_focus_centered() {
1667 self.move_cursor_to_output(&output);
1668 }
1669 self.niri.layer_shell_on_demand_focus = None;
1670 }
1671 }
1672 Action::FocusMonitorRight => {
1673 if let Some(output) = self.niri.output_right() {
1674 self.niri.layout.focus_output(&output);
1675 if !self.maybe_warp_cursor_to_focus_centered() {
1676 self.move_cursor_to_output(&output);
1677 }
1678 self.niri.layer_shell_on_demand_focus = None;
1679 }
1680 }
1681 Action::FocusMonitorDown => {
1682 if let Some(output) = self.niri.output_down() {
1683 self.niri.layout.focus_output(&output);
1684 if !self.maybe_warp_cursor_to_focus_centered() {
1685 self.move_cursor_to_output(&output);
1686 }
1687 self.niri.layer_shell_on_demand_focus = None;
1688 }
1689 }
1690 Action::FocusMonitorUp => {
1691 if let Some(output) = self.niri.output_up() {
1692 self.niri.layout.focus_output(&output);
1693 if !self.maybe_warp_cursor_to_focus_centered() {
1694 self.move_cursor_to_output(&output);
1695 }
1696 self.niri.layer_shell_on_demand_focus = None;
1697 }
1698 }
1699 Action::FocusMonitorPrevious => {
1700 if let Some(output) = self.niri.output_previous() {
1701 self.niri.layout.focus_output(&output);
1702 if !self.maybe_warp_cursor_to_focus_centered() {
1703 self.move_cursor_to_output(&output);
1704 }
1705 self.niri.layer_shell_on_demand_focus = None;
1706 }
1707 }
1708 Action::FocusMonitorNext => {
1709 if let Some(output) = self.niri.output_next() {
1710 self.niri.layout.focus_output(&output);
1711 if !self.maybe_warp_cursor_to_focus_centered() {
1712 self.move_cursor_to_output(&output);
1713 }
1714 self.niri.layer_shell_on_demand_focus = None;
1715 }
1716 }
1717 Action::FocusMonitor(output) => {
1718 if let Some(output) = self.niri.output_by_name_match(&output).cloned() {
1719 self.niri.layout.focus_output(&output);
1720 if !self.maybe_warp_cursor_to_focus_centered() {
1721 self.move_cursor_to_output(&output);
1722 }
1723 self.niri.layer_shell_on_demand_focus = None;
1724 }
1725 }
1726 Action::MoveWindowToMonitorLeft => {
1727 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1728 if let Some(target_output) = self.niri.output_left_of(current_output) {
1729 self.move_cursor_to_output(&target_output);
1730 self.niri.screenshot_ui.move_to_output(target_output);
1731 }
1732 } else if let Some(output) = self.niri.output_left() {
1733 self.niri
1734 .layout
1735 .move_to_output(None, &output, None, ActivateWindow::Smart);
1736 self.niri.layout.focus_output(&output);
1737 if !self.maybe_warp_cursor_to_focus_centered() {
1738 self.move_cursor_to_output(&output);
1739 }
1740 }
1741 }
1742 Action::MoveWindowToMonitorRight => {
1743 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1744 if let Some(target_output) = self.niri.output_right_of(current_output) {
1745 self.move_cursor_to_output(&target_output);
1746 self.niri.screenshot_ui.move_to_output(target_output);
1747 }
1748 } else if let Some(output) = self.niri.output_right() {
1749 self.niri
1750 .layout
1751 .move_to_output(None, &output, None, ActivateWindow::Smart);
1752 self.niri.layout.focus_output(&output);
1753 if !self.maybe_warp_cursor_to_focus_centered() {
1754 self.move_cursor_to_output(&output);
1755 }
1756 }
1757 }
1758 Action::MoveWindowToMonitorDown => {
1759 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1760 if let Some(target_output) = self.niri.output_down_of(current_output) {
1761 self.move_cursor_to_output(&target_output);
1762 self.niri.screenshot_ui.move_to_output(target_output);
1763 }
1764 } else if let Some(output) = self.niri.output_down() {
1765 self.niri
1766 .layout
1767 .move_to_output(None, &output, None, ActivateWindow::Smart);
1768 self.niri.layout.focus_output(&output);
1769 if !self.maybe_warp_cursor_to_focus_centered() {
1770 self.move_cursor_to_output(&output);
1771 }
1772 }
1773 }
1774 Action::MoveWindowToMonitorUp => {
1775 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1776 if let Some(target_output) = self.niri.output_up_of(current_output) {
1777 self.move_cursor_to_output(&target_output);
1778 self.niri.screenshot_ui.move_to_output(target_output);
1779 }
1780 } else if let Some(output) = self.niri.output_up() {
1781 self.niri
1782 .layout
1783 .move_to_output(None, &output, None, ActivateWindow::Smart);
1784 self.niri.layout.focus_output(&output);
1785 if !self.maybe_warp_cursor_to_focus_centered() {
1786 self.move_cursor_to_output(&output);
1787 }
1788 }
1789 }
1790 Action::MoveWindowToMonitorPrevious => {
1791 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1792 if let Some(target_output) = self.niri.output_previous_of(current_output) {
1793 self.move_cursor_to_output(&target_output);
1794 self.niri.screenshot_ui.move_to_output(target_output);
1795 }
1796 } else if let Some(output) = self.niri.output_previous() {
1797 self.niri
1798 .layout
1799 .move_to_output(None, &output, None, ActivateWindow::Smart);
1800 self.niri.layout.focus_output(&output);
1801 if !self.maybe_warp_cursor_to_focus_centered() {
1802 self.move_cursor_to_output(&output);
1803 }
1804 }
1805 }
1806 Action::MoveWindowToMonitorNext => {
1807 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1808 if let Some(target_output) = self.niri.output_next_of(current_output) {
1809 self.move_cursor_to_output(&target_output);
1810 self.niri.screenshot_ui.move_to_output(target_output);
1811 }
1812 } else if let Some(output) = self.niri.output_next() {
1813 self.niri
1814 .layout
1815 .move_to_output(None, &output, None, ActivateWindow::Smart);
1816 self.niri.layout.focus_output(&output);
1817 if !self.maybe_warp_cursor_to_focus_centered() {
1818 self.move_cursor_to_output(&output);
1819 }
1820 }
1821 }
1822 Action::MoveWindowToMonitor(output) => {
1823 if let Some(output) = self.niri.output_by_name_match(&output).cloned() {
1824 if self.niri.screenshot_ui.is_open() {
1825 self.move_cursor_to_output(&output);
1826 self.niri.screenshot_ui.move_to_output(output);
1827 } else {
1828 self.niri
1829 .layout
1830 .move_to_output(None, &output, None, ActivateWindow::Smart);
1831 self.niri.layout.focus_output(&output);
1832 if !self.maybe_warp_cursor_to_focus_centered() {
1833 self.move_cursor_to_output(&output);
1834 }
1835 }
1836 }
1837 }
1838 Action::MoveWindowToMonitorById { id, output } => {
1839 if let Some(output) = self.niri.output_by_name_match(&output).cloned() {
1840 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1841 let window = window.map(|(_, m)| m.window.clone());
1842
1843 if let Some(window) = window {
1844 let target_was_active = self
1845 .niri
1846 .layout
1847 .active_output()
1848 .is_some_and(|active| output == *active);
1849
1850 self.niri.layout.move_to_output(
1851 Some(&window),
1852 &output,
1853 None,
1854 ActivateWindow::Smart,
1855 );
1856
1857 // If the active output changed (window was moved and focused).
1858 #[allow(clippy::collapsible_if)]
1859 if !target_was_active && self.niri.layout.active_output() == Some(&output) {
1860 if !self.maybe_warp_cursor_to_focus_centered() {
1861 self.move_cursor_to_output(&output);
1862 }
1863 }
1864 }
1865 }
1866 }
1867 Action::MoveColumnToMonitorLeft => {
1868 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1869 if let Some(target_output) = self.niri.output_left_of(current_output) {
1870 self.move_cursor_to_output(&target_output);
1871 self.niri.screenshot_ui.move_to_output(target_output);
1872 }
1873 } else if let Some(output) = self.niri.output_left() {
1874 self.niri.layout.move_column_to_output(&output, None, true);
1875 self.niri.layout.focus_output(&output);
1876 if !self.maybe_warp_cursor_to_focus_centered() {
1877 self.move_cursor_to_output(&output);
1878 }
1879 }
1880 }
1881 Action::MoveColumnToMonitorRight => {
1882 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1883 if let Some(target_output) = self.niri.output_right_of(current_output) {
1884 self.move_cursor_to_output(&target_output);
1885 self.niri.screenshot_ui.move_to_output(target_output);
1886 }
1887 } else if let Some(output) = self.niri.output_right() {
1888 self.niri.layout.move_column_to_output(&output, None, true);
1889 self.niri.layout.focus_output(&output);
1890 if !self.maybe_warp_cursor_to_focus_centered() {
1891 self.move_cursor_to_output(&output);
1892 }
1893 }
1894 }
1895 Action::MoveColumnToMonitorDown => {
1896 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1897 if let Some(target_output) = self.niri.output_down_of(current_output) {
1898 self.move_cursor_to_output(&target_output);
1899 self.niri.screenshot_ui.move_to_output(target_output);
1900 }
1901 } else if let Some(output) = self.niri.output_down() {
1902 self.niri.layout.move_column_to_output(&output, None, true);
1903 self.niri.layout.focus_output(&output);
1904 if !self.maybe_warp_cursor_to_focus_centered() {
1905 self.move_cursor_to_output(&output);
1906 }
1907 }
1908 }
1909 Action::MoveColumnToMonitorUp => {
1910 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1911 if let Some(target_output) = self.niri.output_up_of(current_output) {
1912 self.move_cursor_to_output(&target_output);
1913 self.niri.screenshot_ui.move_to_output(target_output);
1914 }
1915 } else if let Some(output) = self.niri.output_up() {
1916 self.niri.layout.move_column_to_output(&output, None, true);
1917 self.niri.layout.focus_output(&output);
1918 if !self.maybe_warp_cursor_to_focus_centered() {
1919 self.move_cursor_to_output(&output);
1920 }
1921 }
1922 }
1923 Action::MoveColumnToMonitorPrevious => {
1924 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1925 if let Some(target_output) = self.niri.output_previous_of(current_output) {
1926 self.move_cursor_to_output(&target_output);
1927 self.niri.screenshot_ui.move_to_output(target_output);
1928 }
1929 } else if let Some(output) = self.niri.output_previous() {
1930 self.niri.layout.move_column_to_output(&output, None, true);
1931 self.niri.layout.focus_output(&output);
1932 if !self.maybe_warp_cursor_to_focus_centered() {
1933 self.move_cursor_to_output(&output);
1934 }
1935 }
1936 }
1937 Action::MoveColumnToMonitorNext => {
1938 if let Some(current_output) = self.niri.screenshot_ui.selection_output() {
1939 if let Some(target_output) = self.niri.output_next_of(current_output) {
1940 self.move_cursor_to_output(&target_output);
1941 self.niri.screenshot_ui.move_to_output(target_output);
1942 }
1943 } else if let Some(output) = self.niri.output_next() {
1944 self.niri.layout.move_column_to_output(&output, None, true);
1945 self.niri.layout.focus_output(&output);
1946 if !self.maybe_warp_cursor_to_focus_centered() {
1947 self.move_cursor_to_output(&output);
1948 }
1949 }
1950 }
1951 Action::MoveColumnToMonitor(output) => {
1952 if let Some(output) = self.niri.output_by_name_match(&output).cloned() {
1953 if self.niri.screenshot_ui.is_open() {
1954 self.move_cursor_to_output(&output);
1955 self.niri.screenshot_ui.move_to_output(output);
1956 } else {
1957 self.niri.layout.move_column_to_output(&output, None, true);
1958 self.niri.layout.focus_output(&output);
1959 if !self.maybe_warp_cursor_to_focus_centered() {
1960 self.move_cursor_to_output(&output);
1961 }
1962 }
1963 }
1964 }
1965 Action::SetColumnWidth(change) => {
1966 if self.niri.screenshot_ui.is_open() {
1967 self.niri.screenshot_ui.set_width(change);
1968
1969 // FIXME: granular
1970 self.niri.queue_redraw_all();
1971 } else {
1972 self.niri.layout.set_column_width(change);
1973 }
1974 }
1975 Action::SetWindowWidth(change) => {
1976 if self.niri.screenshot_ui.is_open() {
1977 self.niri.screenshot_ui.set_width(change);
1978
1979 // FIXME: granular
1980 self.niri.queue_redraw_all();
1981 } else {
1982 self.niri.layout.set_window_width(None, change);
1983 }
1984 }
1985 Action::SetWindowWidthById { id, change } => {
1986 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
1987 let window = window.map(|(_, m)| m.window.clone());
1988 if let Some(window) = window {
1989 self.niri.layout.set_window_width(Some(&window), change);
1990 }
1991 }
1992 Action::SetWindowHeight(change) => {
1993 if self.niri.screenshot_ui.is_open() {
1994 self.niri.screenshot_ui.set_height(change);
1995
1996 // FIXME: granular
1997 self.niri.queue_redraw_all();
1998 } else {
1999 self.niri.layout.set_window_height(None, change);
2000 }
2001 }
2002 Action::SetWindowHeightById { id, change } => {
2003 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
2004 let window = window.map(|(_, m)| m.window.clone());
2005 if let Some(window) = window {
2006 self.niri.layout.set_window_height(Some(&window), change);
2007 }
2008 }
2009 Action::ResetWindowHeight => {
2010 self.niri.layout.reset_window_height(None);
2011 }
2012 Action::ResetWindowHeightById(id) => {
2013 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
2014 let window = window.map(|(_, m)| m.window.clone());
2015 if let Some(window) = window {
2016 self.niri.layout.reset_window_height(Some(&window));
2017 }
2018 }
2019 Action::ExpandColumnToAvailableWidth => {
2020 self.niri.layout.expand_column_to_available_width();
2021 }
2022 Action::ShowHotkeyOverlay => {
2023 if self.niri.hotkey_overlay.show() {
2024 self.niri.queue_redraw_all();
2025
2026 #[cfg(feature = "dbus")]
2027 self.niri.a11y_announce_hotkey_overlay();
2028 }
2029 }
2030 Action::MoveWorkspaceToMonitorLeft => {
2031 if let Some(output) = self.niri.output_left() {
2032 self.niri.layout.move_workspace_to_output(&output);
2033 if !self.maybe_warp_cursor_to_focus_centered() {
2034 self.move_cursor_to_output(&output);
2035 }
2036 }
2037 }
2038 Action::MoveWorkspaceToMonitorRight => {
2039 if let Some(output) = self.niri.output_right() {
2040 self.niri.layout.move_workspace_to_output(&output);
2041 if !self.maybe_warp_cursor_to_focus_centered() {
2042 self.move_cursor_to_output(&output);
2043 }
2044 }
2045 }
2046 Action::MoveWorkspaceToMonitorDown => {
2047 if let Some(output) = self.niri.output_down() {
2048 self.niri.layout.move_workspace_to_output(&output);
2049 if !self.maybe_warp_cursor_to_focus_centered() {
2050 self.move_cursor_to_output(&output);
2051 }
2052 }
2053 }
2054 Action::MoveWorkspaceToMonitorUp => {
2055 if let Some(output) = self.niri.output_up() {
2056 self.niri.layout.move_workspace_to_output(&output);
2057 if !self.maybe_warp_cursor_to_focus_centered() {
2058 self.move_cursor_to_output(&output);
2059 }
2060 }
2061 }
2062 Action::MoveWorkspaceToMonitorPrevious => {
2063 if let Some(output) = self.niri.output_previous() {
2064 self.niri.layout.move_workspace_to_output(&output);
2065 if !self.maybe_warp_cursor_to_focus_centered() {
2066 self.move_cursor_to_output(&output);
2067 }
2068 }
2069 }
2070 Action::MoveWorkspaceToMonitorNext => {
2071 if let Some(output) = self.niri.output_next() {
2072 self.niri.layout.move_workspace_to_output(&output);
2073 if !self.maybe_warp_cursor_to_focus_centered() {
2074 self.move_cursor_to_output(&output);
2075 }
2076 }
2077 }
2078 Action::MoveWorkspaceToMonitor(new_output) => {
2079 if let Some(new_output) = self.niri.output_by_name_match(&new_output).cloned() {
2080 if self.niri.layout.move_workspace_to_output(&new_output)
2081 && !self.maybe_warp_cursor_to_focus_centered()
2082 {
2083 self.move_cursor_to_output(&new_output);
2084 }
2085 }
2086 }
2087 Action::MoveWorkspaceToMonitorByRef {
2088 output_name,
2089 reference,
2090 } => {
2091 if let Some((output, old_idx)) =
2092 self.niri.find_output_and_workspace_index(reference)
2093 {
2094 if let Some(new_output) = self.niri.output_by_name_match(&output_name).cloned()
2095 {
2096 if self.niri.layout.move_workspace_to_output_by_id(
2097 old_idx,
2098 output,
2099 &new_output,
2100 ) {
2101 // Cursor warp already calls `queue_redraw_all`
2102 if !self.maybe_warp_cursor_to_focus_centered() {
2103 self.move_cursor_to_output(&new_output);
2104 }
2105 }
2106 }
2107 }
2108 }
2109 Action::ToggleWindowFloating => {
2110 self.niri.layout.toggle_window_floating(None);
2111 // FIXME: granular
2112 self.niri.queue_redraw_all();
2113 }
2114 Action::ToggleWindowFloatingById(id) => {
2115 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
2116 let window = window.map(|(_, m)| m.window.clone());
2117 if let Some(window) = window {
2118 self.niri.layout.toggle_window_floating(Some(&window));
2119 // FIXME: granular
2120 self.niri.queue_redraw_all();
2121 }
2122 }
2123 Action::MoveWindowToFloating => {
2124 self.niri.layout.set_window_floating(None, true);
2125 // FIXME: granular
2126 self.niri.queue_redraw_all();
2127 }
2128 Action::MoveWindowToFloatingById(id) => {
2129 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
2130 let window = window.map(|(_, m)| m.window.clone());
2131 if let Some(window) = window {
2132 self.niri.layout.set_window_floating(Some(&window), true);
2133 // FIXME: granular
2134 self.niri.queue_redraw_all();
2135 }
2136 }
2137 Action::MoveWindowToTiling => {
2138 self.niri.layout.set_window_floating(None, false);
2139 // FIXME: granular
2140 self.niri.queue_redraw_all();
2141 }
2142 Action::MoveWindowToTilingById(id) => {
2143 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
2144 let window = window.map(|(_, m)| m.window.clone());
2145 if let Some(window) = window {
2146 self.niri.layout.set_window_floating(Some(&window), false);
2147 // FIXME: granular
2148 self.niri.queue_redraw_all();
2149 }
2150 }
2151 Action::FocusFloating => {
2152 self.niri.layout.focus_floating();
2153 self.maybe_warp_cursor_to_focus();
2154 // FIXME: granular
2155 self.niri.queue_redraw_all();
2156 }
2157 Action::FocusTiling => {
2158 self.niri.layout.focus_tiling();
2159 self.maybe_warp_cursor_to_focus();
2160 // FIXME: granular
2161 self.niri.queue_redraw_all();
2162 }
2163 Action::SwitchFocusBetweenFloatingAndTiling => {
2164 self.niri.layout.switch_focus_floating_tiling();
2165 self.maybe_warp_cursor_to_focus();
2166 // FIXME: granular
2167 self.niri.queue_redraw_all();
2168 }
2169 Action::MoveFloatingWindowById { id, x, y } => {
2170 let window = if let Some(id) = id {
2171 let window = self.niri.layout.windows().find(|(_, m)| m.id().get() == id);
2172 let window = window.map(|(_, m)| m.window.clone());
2173 if window.is_none() {
2174 return;
2175 }
2176 window
2177 } else {
2178 None
2179 };
2180
2181 self.niri
2182 .layout
2183 .move_floating_window(window.as_ref(), x, y, true);
2184 // FIXME: granular
2185 self.niri.queue_redraw_all();
2186 }
2187 Action::ToggleWindowRuleOpacity => {
2188 let active_window = self
2189 .niri
2190 .layout
2191 .active_workspace_mut()
2192 .and_then(|ws| ws.active_window_mut());
2193 if let Some(window) = active_window {
2194 if window.rules().opacity.is_some_and(|o| o != 1.) {
2195 window.toggle_ignore_opacity_window_rule();
2196 // FIXME: granular
2197 self.niri.queue_redraw_all();
2198 }
2199 }
2200 }
2201 Action::ToggleWindowRuleOpacityById(id) => {
2202 let window = self
2203 .niri
2204 .layout
2205 .workspaces_mut()
2206 .find_map(|ws| ws.windows_mut().find(|w| w.id().get() == id));
2207 if let Some(window) = window {
2208 if window.rules().opacity.is_some_and(|o| o != 1.) {
2209 window.toggle_ignore_opacity_window_rule();
2210 // FIXME: granular
2211 self.niri.queue_redraw_all();
2212 }
2213 }
2214 }
2215 Action::SetDynamicCastWindow => {
2216 let id = self
2217 .niri
2218 .layout
2219 .active_workspace()
2220 .and_then(|ws| ws.active_window())
2221 .map(|mapped| mapped.id().get());
2222 if let Some(id) = id {
2223 self.set_dynamic_cast_target(CastTarget::Window { id });
2224 }
2225 }
2226 Action::SetDynamicCastWindowById(id) => {
2227 let layout = &self.niri.layout;
2228 if layout.windows().any(|(_, mapped)| mapped.id().get() == id) {
2229 self.set_dynamic_cast_target(CastTarget::Window { id });
2230 }
2231 }
2232 Action::SetDynamicCastMonitor(output) => {
2233 let output = match output {
2234 None => self.niri.layout.active_output(),
2235 Some(name) => self.niri.output_by_name_match(&name),
2236 };
2237 if let Some(output) = output {
2238 let output = output.downgrade();
2239 self.set_dynamic_cast_target(CastTarget::Output(output));
2240 }
2241 }
2242 Action::ClearDynamicCastTarget => {
2243 self.set_dynamic_cast_target(CastTarget::Nothing);
2244 }
2245 Action::ToggleOverview => {
2246 self.niri.layout.toggle_overview();
2247 self.niri.queue_redraw_all();
2248 }
2249 Action::OpenOverview => {
2250 if self.niri.layout.open_overview() {
2251 self.niri.queue_redraw_all();
2252 }
2253 }
2254 Action::CloseOverview => {
2255 if self.niri.layout.close_overview() {
2256 self.niri.queue_redraw_all();
2257 }
2258 }
2259 Action::ToggleWindowUrgent(id) => {
2260 let window = self
2261 .niri
2262 .layout
2263 .workspaces_mut()
2264 .find_map(|ws| ws.windows_mut().find(|w| w.id().get() == id));
2265 if let Some(window) = window {
2266 let urgent = window.is_urgent();
2267 window.set_urgent(!urgent);
2268 }
2269 self.niri.queue_redraw_all();
2270 }
2271 Action::SetWindowUrgent(id) => {
2272 let window = self
2273 .niri
2274 .layout
2275 .workspaces_mut()
2276 .find_map(|ws| ws.windows_mut().find(|w| w.id().get() == id));
2277 if let Some(window) = window {
2278 window.set_urgent(true);
2279 }
2280 self.niri.queue_redraw_all();
2281 }
2282 Action::UnsetWindowUrgent(id) => {
2283 let window = self
2284 .niri
2285 .layout
2286 .workspaces_mut()
2287 .find_map(|ws| ws.windows_mut().find(|w| w.id().get() == id));
2288 if let Some(window) = window {
2289 window.set_urgent(false);
2290 }
2291 self.niri.queue_redraw_all();
2292 }
2293 Action::LoadConfigFile => {
2294 if let Some(watcher) = &self.niri.config_file_watcher {
2295 watcher.load_config();
2296 }
2297 }
2298 Action::MruConfirm => {
2299 self.confirm_mru();
2300 }
2301 Action::MruCancel => {
2302 self.niri.cancel_mru();
2303 }
2304 Action::MruAdvance {
2305 direction,
2306 scope,
2307 filter,
2308 } => {
2309 if self.niri.window_mru_ui.is_open() {
2310 self.niri.window_mru_ui.advance(direction, filter);
2311 self.niri.queue_redraw_mru_output();
2312 } else if self.niri.config.borrow().recent_windows.on {
2313 self.niri.mru_apply_keyboard_commit();
2314
2315 let config = self.niri.config.borrow();
2316 let scope = scope.unwrap_or(self.niri.window_mru_ui.scope());
2317
2318 let mut wmru = WindowMru::new(&self.niri);
2319 if !wmru.is_empty() {
2320 wmru.set_scope(scope);
2321 if let Some(filter) = filter {
2322 wmru.set_filter(filter);
2323 }
2324
2325 if let Some(output) = self.niri.layout.active_output() {
2326 self.niri.window_mru_ui.open(
2327 self.niri.clock.clone(),
2328 wmru,
2329 output.clone(),
2330 );
2331
2332 // Only select the *next* window if some window (which should be the
2333 // first one) is already focused. If nothing is focused, keep the first
2334 // window (which is logically the "previously selected" one).
2335 let keep_first = direction == MruDirection::Forward
2336 && self.niri.layout.focus().is_none();
2337 if !keep_first {
2338 self.niri.window_mru_ui.advance(direction, None);
2339 }
2340
2341 drop(config);
2342 self.niri.queue_redraw_all();
2343 }
2344 }
2345 }
2346 }
2347 Action::MruCloseCurrentWindow => {
2348 if self.niri.window_mru_ui.is_open() {
2349 if let Some(id) = self.niri.window_mru_ui.current_window_id() {
2350 if let Some(w) = self.niri.find_window_by_id(id) {
2351 if let Some(tl) = w.toplevel() {
2352 tl.send_close();
2353 }
2354 }
2355 }
2356 }
2357 }
2358 Action::MruFirst => {
2359 if self.niri.window_mru_ui.is_open() {
2360 self.niri.window_mru_ui.first();
2361 self.niri.queue_redraw_mru_output();
2362 }
2363 }
2364 Action::MruLast => {
2365 if self.niri.window_mru_ui.is_open() {
2366 self.niri.window_mru_ui.last();
2367 self.niri.queue_redraw_mru_output();
2368 }
2369 }
2370 Action::MruSetScope(scope) => {
2371 if self.niri.window_mru_ui.is_open() {
2372 self.niri.window_mru_ui.set_scope(scope);
2373 self.niri.queue_redraw_mru_output();
2374 }
2375 }
2376 Action::MruCycleScope => {
2377 if self.niri.window_mru_ui.is_open() {
2378 self.niri.window_mru_ui.cycle_scope();
2379 self.niri.queue_redraw_mru_output();
2380 }
2381 }
2382 }
2383 }
2384
2385 fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) {
2386 let was_inside_hot_corner = self.niri.pointer_inside_hot_corner;
2387 // Any of the early returns here mean that the pointer is not inside the hot corner.
2388 self.niri.pointer_inside_hot_corner = false;
2389
2390 // We need an output to be able to move the pointer.
2391 if self.niri.global_space.outputs().next().is_none() {
2392 return;
2393 }
2394
2395 let serial = SERIAL_COUNTER.next_serial();
2396
2397 let pointer = self.niri.seat.get_pointer().unwrap();
2398
2399 let pos = pointer.current_location();
2400
2401 // We have an output, so we can compute the new location and focus.
2402 let mut new_pos = pos + event.delta();
2403
2404 // We received an event for the regular pointer, so show it now.
2405 self.niri.pointer_visibility = PointerVisibility::Visible;
2406 self.niri.tablet_cursor_location = None;
2407
2408 // Check if we have an active pointer constraint.
2409 //
2410 // FIXME: ideally this should use the pointer focus with up-to-date global location.
2411 let mut pointer_confined = None;
2412 if let Some(under) = &self.niri.pointer_contents.surface {
2413 // No need to check if the pointer focus surface matches, because here we're checking
2414 // for an already-active constraint, and the constraint is deactivated when the focused
2415 // surface changes.
2416 let pos_within_surface = pos - under.1;
2417
2418 let mut pointer_locked = false;
2419 with_pointer_constraint(&under.0, &pointer, |constraint| {
2420 let Some(constraint) = constraint else { return };
2421 if !constraint.is_active() {
2422 return;
2423 }
2424
2425 // Constraint does not apply if not within region.
2426 if let Some(region) = constraint.region() {
2427 if !region.contains(pos_within_surface.to_i32_round()) {
2428 return;
2429 }
2430 }
2431
2432 match &*constraint {
2433 PointerConstraint::Locked(_locked) => {
2434 pointer_locked = true;
2435 }
2436 PointerConstraint::Confined(confine) => {
2437 pointer_confined = Some((under.clone(), confine.region().cloned()));
2438 }
2439 }
2440 });
2441
2442 // If the pointer is locked, only send relative motion.
2443 if pointer_locked {
2444 pointer.relative_motion(
2445 self,
2446 Some(under.clone()),
2447 &RelativeMotionEvent {
2448 delta: event.delta(),
2449 delta_unaccel: event.delta_unaccel(),
2450 utime: event.time(),
2451 },
2452 );
2453
2454 pointer.frame(self);
2455
2456 // I guess a redraw to hide the tablet cursor could be nice? Doesn't matter too
2457 // much here I think.
2458 return;
2459 }
2460 }
2461
2462 // Warp pointer across the screen during the spatial movement grabs.
2463 let spatial_grab = pointer.with_grab(|_, grab| {
2464 let grab = grab.as_any();
2465 if let Some(grab) = grab.downcast_ref::<SpatialMovementGrab>() {
2466 if let Some(output) = grab.view_offset_output() {
2467 return Some((output.clone(), true));
2468 } else if let Some(output) = grab.workspace_switch_output() {
2469 return Some((output.clone(), false));
2470 }
2471 } else if let Some(grab) = grab.downcast_ref::<MoveGrab>() {
2472 if let Some(output) = grab.view_offset_output() {
2473 return Some((output.clone(), true));
2474 }
2475 }
2476 None
2477 });
2478 if let Some((output, horizontal)) = spatial_grab.flatten() {
2479 if let Some(geo) = self.niri.global_space.output_geometry(&output) {
2480 let geo = geo.to_f64();
2481 if horizontal {
2482 new_pos.x = (new_pos.x - geo.loc.x).rem_euclid(geo.size.w) + geo.loc.x;
2483 new_pos.y = new_pos.y.clamp(geo.loc.y, geo.loc.y + geo.size.h - 1.);
2484 } else {
2485 new_pos.x = new_pos.x.clamp(geo.loc.x, geo.loc.x + geo.size.w - 1.);
2486 new_pos.y = (new_pos.y - geo.loc.y).rem_euclid(geo.size.h) + geo.loc.y;
2487 }
2488 }
2489 }
2490
2491 if self
2492 .niri
2493 .global_space
2494 .output_under(new_pos)
2495 .next()
2496 .is_none()
2497 {
2498 // We ended up outside the outputs and need to clip the movement.
2499 if let Some(output) = self.niri.global_space.output_under(pos).next() {
2500 // The pointer was previously on some output. Clip the movement against its
2501 // boundaries.
2502 let geom = self.niri.global_space.output_geometry(output).unwrap();
2503 new_pos.x = new_pos
2504 .x
2505 .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64);
2506 new_pos.y = new_pos
2507 .y
2508 .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64);
2509 } else {
2510 // The pointer was not on any output in the first place. Find one for it.
2511 // Let's do the simple thing and just put it on the first output.
2512 let output = self.niri.global_space.outputs().next().unwrap();
2513 let geom = self.niri.global_space.output_geometry(output).unwrap();
2514 new_pos = center(geom).to_f64();
2515 }
2516 }
2517
2518 if let Some(output) = self.niri.screenshot_ui.selection_output() {
2519 let geom = self.niri.global_space.output_geometry(output).unwrap();
2520 let mut point = (new_pos - geom.loc.to_f64())
2521 .to_physical(output.current_scale().fractional_scale())
2522 .to_i32_round::<i32>();
2523
2524 let size = output.current_mode().unwrap().size;
2525 let transform = output.current_transform();
2526 let size = transform.transform_size(size);
2527 point.x = point.x.clamp(0, size.w - 1);
2528 point.y = point.y.clamp(0, size.h - 1);
2529
2530 self.niri.screenshot_ui.pointer_motion(point, None);
2531 }
2532
2533 if let Some(mru_output) = self.niri.window_mru_ui.output() {
2534 if let Some((output, pos_within_output)) = self.niri.output_under(new_pos) {
2535 if mru_output == output {
2536 self.niri.window_mru_ui.pointer_motion(pos_within_output);
2537 }
2538 }
2539 }
2540
2541 let under = self.niri.contents_under(new_pos);
2542
2543 // Handle confined pointer.
2544 if let Some((focus_surface, region)) = pointer_confined {
2545 let mut prevent = false;
2546
2547 // Prevent the pointer from leaving the focused surface.
2548 if Some(&focus_surface.0) != under.surface.as_ref().map(|(s, _)| s) {
2549 prevent = true;
2550 }
2551
2552 // Prevent the pointer from leaving the confine region, if any.
2553 if let Some(region) = region {
2554 let new_pos_within_surface = new_pos - focus_surface.1;
2555 if !region.contains(new_pos_within_surface.to_i32_round()) {
2556 prevent = true;
2557 }
2558 }
2559
2560 if prevent {
2561 pointer.relative_motion(
2562 self,
2563 Some(focus_surface),
2564 &RelativeMotionEvent {
2565 delta: event.delta(),
2566 delta_unaccel: event.delta_unaccel(),
2567 utime: event.time(),
2568 },
2569 );
2570
2571 pointer.frame(self);
2572
2573 return;
2574 }
2575 }
2576
2577 self.niri.handle_focus_follows_mouse(&under);
2578
2579 self.niri.pointer_contents.clone_from(&under);
2580
2581 pointer.motion(
2582 self,
2583 under.surface.clone(),
2584 &MotionEvent {
2585 location: new_pos,
2586 serial,
2587 time: event.time_msec(),
2588 },
2589 );
2590
2591 pointer.relative_motion(
2592 self,
2593 under.surface,
2594 &RelativeMotionEvent {
2595 delta: event.delta(),
2596 delta_unaccel: event.delta_unaccel(),
2597 utime: event.time(),
2598 },
2599 );
2600
2601 pointer.frame(self);
2602
2603 // contents_under() will return no surface when the hot corner should trigger, so
2604 // pointer.motion() will set the current focus to None.
2605 if under.hot_corner && pointer.current_focus().is_none() {
2606 if !was_inside_hot_corner
2607 && pointer
2608 .with_grab(|_, grab| grab_allows_hot_corner(grab))
2609 .unwrap_or(true)
2610 {
2611 self.niri.layout.toggle_overview();
2612 }
2613 self.niri.pointer_inside_hot_corner = true;
2614 }
2615
2616 // Activate a new confinement if necessary.
2617 self.niri.maybe_activate_pointer_constraint();
2618
2619 // Inform the layout of an ongoing DnD operation.
2620 let is_dnd_grab = pointer
2621 .with_grab(|_, grab| Self::is_dnd_grab(grab.as_any()))
2622 .unwrap_or(false);
2623 if is_dnd_grab {
2624 if let Some((output, pos_within_output)) = self.niri.output_under(new_pos) {
2625 let output = output.clone();
2626 self.niri.layout.dnd_update(output, pos_within_output);
2627 }
2628 }
2629
2630 // Redraw to update the cursor position.
2631 // FIXME: redraw only outputs overlapping the cursor.
2632 self.niri.queue_redraw_all();
2633 }
2634
2635 fn on_pointer_motion_absolute<I: InputBackend>(
2636 &mut self,
2637 event: I::PointerMotionAbsoluteEvent,
2638 ) {
2639 let was_inside_hot_corner = self.niri.pointer_inside_hot_corner;
2640 // Any of the early returns here mean that the pointer is not inside the hot corner.
2641 self.niri.pointer_inside_hot_corner = false;
2642
2643 let Some(pos) = self.compute_absolute_location(&event, None).or_else(|| {
2644 self.global_bounding_rectangle().map(|output_geo| {
2645 event.position_transformed(output_geo.size) + output_geo.loc.to_f64()
2646 })
2647 }) else {
2648 return;
2649 };
2650
2651 let serial = SERIAL_COUNTER.next_serial();
2652
2653 let pointer = self.niri.seat.get_pointer().unwrap();
2654
2655 if let Some(output) = self.niri.screenshot_ui.selection_output() {
2656 let geom = self.niri.global_space.output_geometry(output).unwrap();
2657 let mut point = (pos - geom.loc.to_f64())
2658 .to_physical(output.current_scale().fractional_scale())
2659 .to_i32_round::<i32>();
2660
2661 let size = output.current_mode().unwrap().size;
2662 let transform = output.current_transform();
2663 let size = transform.transform_size(size);
2664 point.x = point.x.clamp(0, size.w - 1);
2665 point.y = point.y.clamp(0, size.h - 1);
2666
2667 self.niri.screenshot_ui.pointer_motion(point, None);
2668 }
2669
2670 if let Some(mru_output) = self.niri.window_mru_ui.output() {
2671 if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
2672 if mru_output == output {
2673 self.niri.window_mru_ui.pointer_motion(pos_within_output);
2674 }
2675 }
2676 }
2677
2678 let under = self.niri.contents_under(pos);
2679
2680 self.niri.handle_focus_follows_mouse(&under);
2681
2682 self.niri.pointer_contents.clone_from(&under);
2683
2684 pointer.motion(
2685 self,
2686 under.surface,
2687 &MotionEvent {
2688 location: pos,
2689 serial,
2690 time: event.time_msec(),
2691 },
2692 );
2693
2694 pointer.frame(self);
2695
2696 // contents_under() will return no surface when the hot corner should trigger, so
2697 // pointer.motion() will set the current focus to None.
2698 if under.hot_corner && pointer.current_focus().is_none() {
2699 if !was_inside_hot_corner
2700 && pointer
2701 .with_grab(|_, grab| grab_allows_hot_corner(grab))
2702 .unwrap_or(true)
2703 {
2704 self.niri.layout.toggle_overview();
2705 }
2706 self.niri.pointer_inside_hot_corner = true;
2707 }
2708
2709 self.niri.maybe_activate_pointer_constraint();
2710
2711 // We moved the pointer, show it.
2712 self.niri.pointer_visibility = PointerVisibility::Visible;
2713
2714 // We moved the regular pointer, so show it now.
2715 self.niri.tablet_cursor_location = None;
2716
2717 // Inform the layout of an ongoing DnD operation.
2718 let is_dnd_grab = pointer
2719 .with_grab(|_, grab| Self::is_dnd_grab(grab.as_any()))
2720 .unwrap_or(false);
2721 if is_dnd_grab {
2722 if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
2723 let output = output.clone();
2724 self.niri.layout.dnd_update(output, pos_within_output);
2725 }
2726 }
2727
2728 // Redraw to update the cursor position.
2729 // FIXME: redraw only outputs overlapping the cursor.
2730 self.niri.queue_redraw_all();
2731 }
2732
2733 fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) {
2734 let pointer = self.niri.seat.get_pointer().unwrap();
2735
2736 let serial = SERIAL_COUNTER.next_serial();
2737
2738 let button = event.button();
2739
2740 let button_code = event.button_code();
2741
2742 let button_state = event.state();
2743
2744 let mod_key = self.backend.mod_key(&self.niri.config.borrow());
2745
2746 // Ignore release events for mouse clicks that triggered a bind.
2747 if self.niri.suppressed_buttons.remove(&button_code) {
2748 return;
2749 }
2750
2751 if ButtonState::Pressed == button_state {
2752 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
2753 let modifiers = modifiers_from_state(mods);
2754
2755 let mut is_mru_open = false;
2756 if let Some(mru_output) = self.niri.window_mru_ui.output() {
2757 is_mru_open = true;
2758 if let Some(MouseButton::Left) = button {
2759 let location = pointer.current_location();
2760 let (output, pos_within_output) = self.niri.output_under(location).unwrap();
2761 if mru_output == output {
2762 let id = self.niri.window_mru_ui.pointer_motion(pos_within_output);
2763 if id.is_some() {
2764 self.confirm_mru();
2765 } else {
2766 self.niri.cancel_mru();
2767 }
2768 } else {
2769 self.niri.cancel_mru();
2770 }
2771
2772 self.niri.suppressed_buttons.insert(button_code);
2773 return;
2774 }
2775 }
2776
2777 if is_mru_open || self.niri.mods_with_mouse_binds.contains(&modifiers) {
2778 if let Some(bind) = match button {
2779 Some(MouseButton::Left) => Some(Trigger::MouseLeft),
2780 Some(MouseButton::Right) => Some(Trigger::MouseRight),
2781 Some(MouseButton::Middle) => Some(Trigger::MouseMiddle),
2782 Some(MouseButton::Back) => Some(Trigger::MouseBack),
2783 Some(MouseButton::Forward) => Some(Trigger::MouseForward),
2784 _ => None,
2785 }
2786 .and_then(|trigger| {
2787 let config = self.niri.config.borrow();
2788 let bindings =
2789 make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers);
2790 find_configured_bind(bindings, mod_key, trigger, mods)
2791 }) {
2792 self.niri.suppressed_buttons.insert(button_code);
2793 self.handle_bind(bind.clone());
2794 return;
2795 };
2796 }
2797
2798 // We received an event for the regular pointer, so show it now.
2799 self.niri.pointer_visibility = PointerVisibility::Visible;
2800 self.niri.tablet_cursor_location = None;
2801
2802 let is_overview_open = self.niri.layout.is_overview_open();
2803
2804 if is_overview_open && !pointer.is_grabbed() && button == Some(MouseButton::Right) {
2805 if let Some((output, ws)) = self.niri.workspace_under_cursor(true) {
2806 let ws_id = ws.id();
2807 let ws_idx = self.niri.layout.find_workspace_by_id(ws_id).unwrap().0;
2808
2809 self.niri.layout.focus_output(&output);
2810
2811 let location = pointer.current_location();
2812 let start_data = PointerGrabStartData {
2813 focus: None,
2814 button: button_code,
2815 location,
2816 };
2817 self.niri
2818 .layout
2819 .view_offset_gesture_begin(&output, Some(ws_idx), false);
2820 let grab = SpatialMovementGrab::new(start_data, output, ws_id, true);
2821 pointer.set_grab(self, grab, serial, Focus::Clear);
2822 self.niri
2823 .cursor_manager
2824 .set_cursor_image(CursorImageStatus::Named(CursorIcon::AllScroll));
2825
2826 // FIXME: granular.
2827 self.niri.queue_redraw_all();
2828 return;
2829 }
2830 }
2831
2832 if button == Some(MouseButton::Middle) && !pointer.is_grabbed() {
2833 let mod_down = modifiers_from_state(mods).contains(mod_key.to_modifiers());
2834 if mod_down {
2835 let output_ws = if is_overview_open {
2836 self.niri.workspace_under_cursor(true)
2837 } else {
2838 // We don't want to accidentally "catch" the wrong workspace during
2839 // animations.
2840 self.niri.output_under_cursor().and_then(|output| {
2841 let mon = self.niri.layout.monitor_for_output(&output)?;
2842 Some((output, mon.active_workspace_ref()))
2843 })
2844 };
2845
2846 if let Some((output, ws)) = output_ws {
2847 let ws_id = ws.id();
2848
2849 self.niri.layout.focus_output(&output);
2850
2851 let location = pointer.current_location();
2852 let start_data = PointerGrabStartData {
2853 focus: None,
2854 button: button_code,
2855 location,
2856 };
2857 let grab = SpatialMovementGrab::new(start_data, output, ws_id, false);
2858 pointer.set_grab(self, grab, serial, Focus::Clear);
2859 self.niri
2860 .cursor_manager
2861 .set_cursor_image(CursorImageStatus::Named(CursorIcon::AllScroll));
2862
2863 // FIXME: granular.
2864 self.niri.queue_redraw_all();
2865
2866 // Don't activate the window under the cursor to avoid unnecessary
2867 // scrolling when e.g. Mod+MMB clicking on a partially off-screen window.
2868 return;
2869 }
2870 }
2871 }
2872
2873 if let Some(mapped) = self.niri.window_under_cursor() {
2874 let window = mapped.window.clone();
2875
2876 // Check if we need to start an interactive move.
2877 if button == Some(MouseButton::Left) && !pointer.is_grabbed() {
2878 let mod_down = modifiers_from_state(mods).contains(mod_key.to_modifiers());
2879 if is_overview_open || mod_down {
2880 let location = pointer.current_location();
2881
2882 if !is_overview_open {
2883 self.niri.layout.activate_window(&window);
2884 }
2885
2886 let start_data = PointerGrabStartData {
2887 focus: None,
2888 button: button_code,
2889 location,
2890 };
2891 let start_data = PointerOrTouchStartData::Pointer(start_data);
2892 let icon = CursorIcon::Grabbing;
2893 if let Some(grab) =
2894 MoveGrab::new(self, start_data, window.clone(), false, Some(icon))
2895 {
2896 pointer.set_grab(self, grab, serial, Focus::Clear);
2897
2898 // Set the cursor to Grabbing right away for Mod+LMB since it doesn't
2899 // do any other gesture.
2900 //
2901 // In the overview, we click to activate window and close the overview,
2902 // in this case setting the cursor right away would be distracting.
2903 if !is_overview_open {
2904 self.niri
2905 .cursor_manager
2906 .set_cursor_image(CursorImageStatus::Named(icon));
2907 }
2908 }
2909 }
2910 }
2911 // Check if we need to start an interactive resize.
2912 else if button == Some(MouseButton::Right) && !pointer.is_grabbed() {
2913 let mod_down = modifiers_from_state(mods).contains(mod_key.to_modifiers());
2914 if mod_down {
2915 let location = pointer.current_location();
2916 let (output, pos_within_output) = self.niri.output_under(location).unwrap();
2917 let edges = self
2918 .niri
2919 .layout
2920 .resize_edges_under(output, pos_within_output)
2921 .unwrap_or(ResizeEdge::empty());
2922
2923 if !edges.is_empty() {
2924 // See if we got a double resize-click gesture.
2925 // FIXME: deduplicate with resize_request in xdg-shell somehow.
2926 let time = get_monotonic_time();
2927 let last_cell = mapped.last_interactive_resize_start();
2928 let mut last = last_cell.get();
2929 last_cell.set(Some((time, edges)));
2930
2931 // Floating windows don't have either of the double-resize-click
2932 // gestures, so just allow it to resize.
2933 if mapped.is_floating() {
2934 last = None;
2935 last_cell.set(None);
2936 }
2937
2938 if let Some((last_time, last_edges)) = last {
2939 if time.saturating_sub(last_time) <= DOUBLE_CLICK_TIME {
2940 // Allow quick resize after a triple click.
2941 last_cell.set(None);
2942
2943 let intersection = edges.intersection(last_edges);
2944 if intersection.intersects(ResizeEdge::LEFT_RIGHT) {
2945 // FIXME: don't activate once we can pass specific windows
2946 // to actions.
2947 self.niri.layout.activate_window(&window);
2948 self.niri.layout.toggle_full_width();
2949 }
2950 if intersection.intersects(ResizeEdge::TOP_BOTTOM) {
2951 self.niri.layout.activate_window(&window);
2952 self.niri.layout.reset_window_height(Some(&window));
2953 }
2954 // FIXME: granular.
2955 self.niri.queue_redraw_all();
2956 return;
2957 }
2958 }
2959
2960 self.niri.layout.activate_window(&window);
2961
2962 if self
2963 .niri
2964 .layout
2965 .interactive_resize_begin(window.clone(), edges)
2966 {
2967 let start_data = PointerGrabStartData {
2968 focus: None,
2969 button: button_code,
2970 location,
2971 };
2972 let grab = ResizeGrab::new(start_data, window.clone());
2973 pointer.set_grab(self, grab, serial, Focus::Clear);
2974 self.niri.cursor_manager.set_cursor_image(
2975 CursorImageStatus::Named(edges.cursor_icon()),
2976 );
2977 }
2978 }
2979 }
2980 }
2981
2982 if !is_overview_open {
2983 self.niri.layout.activate_window(&window);
2984 }
2985
2986 // FIXME: granular.
2987 self.niri.queue_redraw_all();
2988 } else if let Some((output, ws)) = is_overview_open
2989 .then(|| self.niri.workspace_under_cursor(false))
2990 .flatten()
2991 {
2992 let ws_idx = self.niri.layout.find_workspace_by_id(ws.id()).unwrap().0;
2993
2994 self.niri.layout.focus_output(&output);
2995 self.niri.layout.toggle_overview_to_workspace(ws_idx);
2996
2997 // FIXME: granular.
2998 self.niri.queue_redraw_all();
2999 } else if let Some(output) = self.niri.output_under_cursor() {
3000 self.niri.layout.focus_output(&output);
3001
3002 // FIXME: granular.
3003 self.niri.queue_redraw_all();
3004 }
3005 };
3006
3007 self.update_pointer_contents();
3008
3009 if ButtonState::Pressed == button_state {
3010 let layer_under = self.niri.pointer_contents.layer.clone();
3011 self.niri.focus_layer_surface_if_on_demand(layer_under);
3012 }
3013
3014 if button == Some(MouseButton::Left) && self.niri.screenshot_ui.is_open() {
3015 if button_state == ButtonState::Pressed {
3016 let pos = pointer.current_location();
3017 if let Some((output, _)) = self.niri.output_under(pos) {
3018 let output = output.clone();
3019 let geom = self.niri.global_space.output_geometry(&output).unwrap();
3020 let mut point = (pos - geom.loc.to_f64())
3021 .to_physical(output.current_scale().fractional_scale())
3022 .to_i32_round();
3023
3024 let size = output.current_mode().unwrap().size;
3025 let transform = output.current_transform();
3026 let size = transform.transform_size(size);
3027 point.x = min(size.w - 1, point.x);
3028 point.y = min(size.h - 1, point.y);
3029
3030 if self.niri.screenshot_ui.pointer_down(output, point, None) {
3031 self.niri.queue_redraw_all();
3032 }
3033 }
3034 } else if let Some(capture) = self.niri.screenshot_ui.pointer_up(None) {
3035 if capture {
3036 self.confirm_screenshot(true);
3037 } else {
3038 self.niri.queue_redraw_all();
3039 }
3040 }
3041 }
3042
3043 pointer.button(
3044 self,
3045 &ButtonEvent {
3046 button: button_code,
3047 state: button_state,
3048 serial,
3049 time: event.time_msec(),
3050 },
3051 );
3052 pointer.frame(self);
3053 }
3054
3055 fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) {
3056 let pointer = &self.niri.seat.get_pointer().unwrap();
3057
3058 let source = event.source();
3059
3060 let mod_key = self.backend.mod_key(&self.niri.config.borrow());
3061
3062 // We received an event for the regular pointer, so show it now. This is also needed for
3063 // update_pointer_contents() below to return the real contents, necessary for the pointer
3064 // axis event to reach the window.
3065 self.niri.pointer_visibility = PointerVisibility::Visible;
3066 self.niri.tablet_cursor_location = None;
3067
3068 let timestamp = Duration::from_micros(event.time());
3069
3070 let horizontal_amount_v120 = event.amount_v120(Axis::Horizontal);
3071 let vertical_amount_v120 = event.amount_v120(Axis::Vertical);
3072
3073 let is_overview_open = self.niri.layout.is_overview_open();
3074
3075 // We should only handle scrolling in the overview if the pointer is not over a (top or
3076 // overlay) layer surface.
3077 let should_handle_in_overview = if is_overview_open {
3078 // FIXME: ideally this should happen after updating the pointer contents, which happens
3079 // below. However, our pointer actions are supposed to act on the old surface, before
3080 // updating the pointer contents.
3081 pointer
3082 .current_focus()
3083 .map(|surface| self.niri.find_root_shell_surface(&surface))
3084 .is_none_or(|root| {
3085 !self
3086 .niri
3087 .mapped_layer_surfaces
3088 .keys()
3089 .any(|layer| *layer.wl_surface() == root)
3090 })
3091 } else {
3092 false
3093 };
3094
3095 let is_mru_open = self.niri.window_mru_ui.is_open();
3096
3097 // Handle wheel scroll bindings.
3098 if source == AxisSource::Wheel {
3099 // If we have a scroll bind with current modifiers, then accumulate and don't pass to
3100 // Wayland. If there's no bind, reset the accumulator.
3101 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
3102 let modifiers = modifiers_from_state(mods);
3103 let should_handle = should_handle_in_overview
3104 || is_mru_open
3105 || self.niri.mods_with_wheel_binds.contains(&modifiers);
3106 if should_handle {
3107 let horizontal = horizontal_amount_v120.unwrap_or(0.);
3108 let ticks = self.niri.horizontal_wheel_tracker.accumulate(horizontal);
3109 if ticks != 0 {
3110 let (bind_left, bind_right) =
3111 if should_handle_in_overview && modifiers.is_empty() {
3112 let bind_left = Some(Bind {
3113 key: Key {
3114 trigger: Trigger::WheelScrollLeft,
3115 modifiers: Modifiers::empty(),
3116 },
3117 action: Action::FocusColumnLeftUnderMouse,
3118 repeat: true,
3119 cooldown: None,
3120 allow_when_locked: false,
3121 allow_inhibiting: false,
3122 hotkey_overlay_title: None,
3123 });
3124 let bind_right = Some(Bind {
3125 key: Key {
3126 trigger: Trigger::WheelScrollRight,
3127 modifiers: Modifiers::empty(),
3128 },
3129 action: Action::FocusColumnRightUnderMouse,
3130 repeat: true,
3131 cooldown: None,
3132 allow_when_locked: false,
3133 allow_inhibiting: false,
3134 hotkey_overlay_title: None,
3135 });
3136 (bind_left, bind_right)
3137 } else {
3138 let config = self.niri.config.borrow();
3139 let bindings =
3140 make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers);
3141 let bind_left = find_configured_bind(
3142 bindings.clone(),
3143 mod_key,
3144 Trigger::WheelScrollLeft,
3145 mods,
3146 );
3147 let bind_right = find_configured_bind(
3148 bindings,
3149 mod_key,
3150 Trigger::WheelScrollRight,
3151 mods,
3152 );
3153 (bind_left, bind_right)
3154 };
3155
3156 if let Some(right) = bind_right {
3157 for _ in 0..ticks {
3158 self.handle_bind(right.clone());
3159 }
3160 }
3161 if let Some(left) = bind_left {
3162 for _ in ticks..0 {
3163 self.handle_bind(left.clone());
3164 }
3165 }
3166 }
3167
3168 let vertical = vertical_amount_v120.unwrap_or(0.);
3169 let ticks = self.niri.vertical_wheel_tracker.accumulate(vertical);
3170 if ticks != 0 {
3171 let (bind_up, bind_down) = if should_handle_in_overview && modifiers.is_empty()
3172 {
3173 let bind_up = Some(Bind {
3174 key: Key {
3175 trigger: Trigger::WheelScrollUp,
3176 modifiers: Modifiers::empty(),
3177 },
3178 action: Action::FocusWorkspaceUpUnderMouse,
3179 repeat: true,
3180 cooldown: Some(Duration::from_millis(50)),
3181 allow_when_locked: false,
3182 allow_inhibiting: false,
3183 hotkey_overlay_title: None,
3184 });
3185 let bind_down = Some(Bind {
3186 key: Key {
3187 trigger: Trigger::WheelScrollDown,
3188 modifiers: Modifiers::empty(),
3189 },
3190 action: Action::FocusWorkspaceDownUnderMouse,
3191 repeat: true,
3192 cooldown: Some(Duration::from_millis(50)),
3193 allow_when_locked: false,
3194 allow_inhibiting: false,
3195 hotkey_overlay_title: None,
3196 });
3197 (bind_up, bind_down)
3198 } else if should_handle_in_overview && modifiers == Modifiers::SHIFT {
3199 let bind_up = Some(Bind {
3200 key: Key {
3201 trigger: Trigger::WheelScrollUp,
3202 modifiers: Modifiers::empty(),
3203 },
3204 action: Action::FocusColumnLeftUnderMouse,
3205 repeat: true,
3206 cooldown: Some(Duration::from_millis(50)),
3207 allow_when_locked: false,
3208 allow_inhibiting: false,
3209 hotkey_overlay_title: None,
3210 });
3211 let bind_down = Some(Bind {
3212 key: Key {
3213 trigger: Trigger::WheelScrollDown,
3214 modifiers: Modifiers::empty(),
3215 },
3216 action: Action::FocusColumnRightUnderMouse,
3217 repeat: true,
3218 cooldown: Some(Duration::from_millis(50)),
3219 allow_when_locked: false,
3220 allow_inhibiting: false,
3221 hotkey_overlay_title: None,
3222 });
3223 (bind_up, bind_down)
3224 } else {
3225 let config = self.niri.config.borrow();
3226 let bindings =
3227 make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers);
3228 let bind_up = find_configured_bind(
3229 bindings.clone(),
3230 mod_key,
3231 Trigger::WheelScrollUp,
3232 mods,
3233 );
3234 let bind_down =
3235 find_configured_bind(bindings, mod_key, Trigger::WheelScrollDown, mods);
3236 (bind_up, bind_down)
3237 };
3238
3239 if let Some(down) = bind_down {
3240 for _ in 0..ticks {
3241 self.handle_bind(down.clone());
3242 }
3243 }
3244 if let Some(up) = bind_up {
3245 for _ in ticks..0 {
3246 self.handle_bind(up.clone());
3247 }
3248 }
3249 }
3250
3251 return;
3252 } else {
3253 self.niri.horizontal_wheel_tracker.reset();
3254 self.niri.vertical_wheel_tracker.reset();
3255 }
3256 }
3257
3258 let horizontal_amount = event.amount(Axis::Horizontal);
3259 let vertical_amount = event.amount(Axis::Vertical);
3260
3261 // Handle touchpad scroll bindings.
3262 if source == AxisSource::Finger {
3263 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
3264 let modifiers = modifiers_from_state(mods);
3265
3266 let horizontal = horizontal_amount.unwrap_or(0.);
3267 let vertical = vertical_amount.unwrap_or(0.);
3268
3269 if should_handle_in_overview && modifiers.is_empty() {
3270 let mut redraw = false;
3271
3272 let action = self
3273 .niri
3274 .overview_scroll_swipe_gesture
3275 .update(horizontal, vertical);
3276 let is_vertical = self.niri.overview_scroll_swipe_gesture.is_vertical();
3277
3278 if action.end() {
3279 if is_vertical {
3280 redraw |= self
3281 .niri
3282 .layout
3283 .workspace_switch_gesture_end(Some(true))
3284 .is_some();
3285 } else {
3286 redraw |= self
3287 .niri
3288 .layout
3289 .view_offset_gesture_end(Some(true))
3290 .is_some();
3291 }
3292 } else {
3293 // Maybe begin, then update.
3294 if is_vertical {
3295 if action.begin() {
3296 if let Some(output) = self.niri.output_under_cursor() {
3297 self.niri
3298 .layout
3299 .workspace_switch_gesture_begin(&output, true);
3300 redraw = true;
3301 }
3302 }
3303
3304 let res = self
3305 .niri
3306 .layout
3307 .workspace_switch_gesture_update(vertical, timestamp, true);
3308 if let Some(Some(_)) = res {
3309 redraw = true;
3310 }
3311 } else {
3312 if action.begin() {
3313 if let Some((output, ws)) = self.niri.workspace_under_cursor(true) {
3314 let ws_id = ws.id();
3315 let ws_idx =
3316 self.niri.layout.find_workspace_by_id(ws_id).unwrap().0;
3317
3318 self.niri.layout.view_offset_gesture_begin(
3319 &output,
3320 Some(ws_idx),
3321 true,
3322 );
3323 redraw = true;
3324 }
3325 }
3326
3327 let res = self
3328 .niri
3329 .layout
3330 .view_offset_gesture_update(horizontal, timestamp, true);
3331 if let Some(Some(_)) = res {
3332 redraw = true;
3333 }
3334 }
3335 }
3336
3337 if redraw {
3338 self.niri.queue_redraw_all();
3339 }
3340
3341 return;
3342 } else {
3343 let mut redraw = false;
3344 if self.niri.overview_scroll_swipe_gesture.reset() {
3345 if self.niri.overview_scroll_swipe_gesture.is_vertical() {
3346 redraw |= self
3347 .niri
3348 .layout
3349 .workspace_switch_gesture_end(Some(true))
3350 .is_some();
3351 } else {
3352 redraw |= self
3353 .niri
3354 .layout
3355 .view_offset_gesture_end(Some(true))
3356 .is_some();
3357 }
3358 }
3359 if redraw {
3360 self.niri.queue_redraw_all();
3361 }
3362 }
3363
3364 if is_mru_open || self.niri.mods_with_finger_scroll_binds.contains(&modifiers) {
3365 let ticks = self
3366 .niri
3367 .horizontal_finger_scroll_tracker
3368 .accumulate(horizontal);
3369 if ticks != 0 {
3370 let config = self.niri.config.borrow();
3371 let bindings =
3372 make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers);
3373 let bind_left = find_configured_bind(
3374 bindings.clone(),
3375 mod_key,
3376 Trigger::TouchpadScrollLeft,
3377 mods,
3378 );
3379 let bind_right =
3380 find_configured_bind(bindings, mod_key, Trigger::TouchpadScrollRight, mods);
3381 drop(config);
3382
3383 if let Some(right) = bind_right {
3384 for _ in 0..ticks {
3385 self.handle_bind(right.clone());
3386 }
3387 }
3388 if let Some(left) = bind_left {
3389 for _ in ticks..0 {
3390 self.handle_bind(left.clone());
3391 }
3392 }
3393 }
3394
3395 let ticks = self
3396 .niri
3397 .vertical_finger_scroll_tracker
3398 .accumulate(vertical);
3399 if ticks != 0 {
3400 let config = self.niri.config.borrow();
3401 let bindings =
3402 make_binds_iter(&config, &mut self.niri.window_mru_ui, modifiers);
3403 let bind_up = find_configured_bind(
3404 bindings.clone(),
3405 mod_key,
3406 Trigger::TouchpadScrollUp,
3407 mods,
3408 );
3409 let bind_down =
3410 find_configured_bind(bindings, mod_key, Trigger::TouchpadScrollDown, mods);
3411 drop(config);
3412
3413 if let Some(down) = bind_down {
3414 for _ in 0..ticks {
3415 self.handle_bind(down.clone());
3416 }
3417 }
3418 if let Some(up) = bind_up {
3419 for _ in ticks..0 {
3420 self.handle_bind(up.clone());
3421 }
3422 }
3423 }
3424
3425 return;
3426 } else {
3427 self.niri.horizontal_finger_scroll_tracker.reset();
3428 self.niri.vertical_finger_scroll_tracker.reset();
3429 }
3430 }
3431
3432 self.update_pointer_contents();
3433
3434 let device_scroll_factor = {
3435 let config = self.niri.config.borrow();
3436 match source {
3437 AxisSource::Wheel => config.input.mouse.scroll_factor,
3438 AxisSource::Finger => config.input.touchpad.scroll_factor,
3439 _ => None,
3440 }
3441 };
3442
3443 // Get window-specific scroll factor
3444 let window_scroll_factor = pointer
3445 .current_focus()
3446 .map(|focused| self.niri.find_root_shell_surface(&focused))
3447 .and_then(|root| self.niri.layout.find_window_and_output(&root).unzip().0)
3448 .and_then(|window| window.rules().scroll_factor)
3449 .unwrap_or(1.);
3450
3451 // Determine final scroll factors based on configuration
3452 let (horizontal_factor, vertical_factor) = device_scroll_factor
3453 .map(|x| x.h_v_factors())
3454 .unwrap_or((1.0, 1.0));
3455 let (horizontal_factor, vertical_factor) = (
3456 horizontal_factor * window_scroll_factor,
3457 vertical_factor * window_scroll_factor,
3458 );
3459
3460 let horizontal_amount = horizontal_amount.unwrap_or_else(|| {
3461 // Winit backend, discrete scrolling.
3462 horizontal_amount_v120.unwrap_or(0.0) / 120. * 15.
3463 }) * horizontal_factor;
3464
3465 let vertical_amount = vertical_amount.unwrap_or_else(|| {
3466 // Winit backend, discrete scrolling.
3467 vertical_amount_v120.unwrap_or(0.0) / 120. * 15.
3468 }) * vertical_factor;
3469
3470 let horizontal_amount_v120 = horizontal_amount_v120.map(|x| x * horizontal_factor);
3471 let vertical_amount_v120 = vertical_amount_v120.map(|x| x * vertical_factor);
3472
3473 let mut frame = AxisFrame::new(event.time_msec()).source(source);
3474 if horizontal_amount != 0.0 {
3475 frame = frame
3476 .relative_direction(Axis::Horizontal, event.relative_direction(Axis::Horizontal));
3477 frame = frame.value(Axis::Horizontal, horizontal_amount);
3478 if let Some(v120) = horizontal_amount_v120 {
3479 frame = frame.v120(Axis::Horizontal, v120 as i32);
3480 }
3481 }
3482 if vertical_amount != 0.0 {
3483 frame =
3484 frame.relative_direction(Axis::Vertical, event.relative_direction(Axis::Vertical));
3485 frame = frame.value(Axis::Vertical, vertical_amount);
3486 if let Some(v120) = vertical_amount_v120 {
3487 frame = frame.v120(Axis::Vertical, v120 as i32);
3488 }
3489 }
3490
3491 if source == AxisSource::Finger {
3492 if event.amount(Axis::Horizontal) == Some(0.0) {
3493 frame = frame.stop(Axis::Horizontal);
3494 }
3495 if event.amount(Axis::Vertical) == Some(0.0) {
3496 frame = frame.stop(Axis::Vertical);
3497 }
3498 }
3499
3500 pointer.axis(self, frame);
3501 pointer.frame(self);
3502 }
3503
3504 fn on_tablet_tool_axis<I: InputBackend>(&mut self, event: I::TabletToolAxisEvent)
3505 where
3506 I::Device: 'static, // Needed for downcasting.
3507 {
3508 let Some(pos) = self.compute_tablet_position(&event) else {
3509 return;
3510 };
3511
3512 if let Some(output) = self.niri.screenshot_ui.selection_output() {
3513 let geom = self.niri.global_space.output_geometry(output).unwrap();
3514 let mut point = (pos - geom.loc.to_f64())
3515 .to_physical(output.current_scale().fractional_scale())
3516 .to_i32_round::<i32>();
3517
3518 let size = output.current_mode().unwrap().size;
3519 let transform = output.current_transform();
3520 let size = transform.transform_size(size);
3521 point.x = point.x.clamp(0, size.w - 1);
3522 point.y = point.y.clamp(0, size.h - 1);
3523
3524 self.niri.screenshot_ui.pointer_motion(point, None);
3525 }
3526
3527 if let Some(mru_output) = self.niri.window_mru_ui.output() {
3528 if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
3529 if mru_output == output {
3530 self.niri.window_mru_ui.pointer_motion(pos_within_output);
3531 }
3532 }
3533 }
3534
3535 let under = self.niri.contents_under(pos);
3536
3537 let tablet_seat = self.niri.seat.tablet_seat();
3538 let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device()));
3539 let tool = tablet_seat.get_tool(&event.tool());
3540 if let (Some(tablet), Some(tool)) = (tablet, tool) {
3541 if event.pressure_has_changed() {
3542 tool.pressure(event.pressure());
3543 }
3544 if event.distance_has_changed() {
3545 tool.distance(event.distance());
3546 }
3547 if event.tilt_has_changed() {
3548 tool.tilt(event.tilt());
3549 }
3550 if event.slider_has_changed() {
3551 tool.slider_position(event.slider_position());
3552 }
3553 if event.rotation_has_changed() {
3554 tool.rotation(event.rotation());
3555 }
3556 if event.wheel_has_changed() {
3557 tool.wheel(event.wheel_delta(), event.wheel_delta_discrete());
3558 }
3559
3560 tool.motion(
3561 pos,
3562 under.surface,
3563 &tablet,
3564 SERIAL_COUNTER.next_serial(),
3565 event.time_msec(),
3566 );
3567
3568 self.niri.pointer_visibility = PointerVisibility::Visible;
3569 self.niri.tablet_cursor_location = Some(pos);
3570 }
3571
3572 // Redraw to update the cursor position.
3573 // FIXME: redraw only outputs overlapping the cursor.
3574 self.niri.queue_redraw_all();
3575 }
3576
3577 fn on_tablet_tool_tip<I: InputBackend>(&mut self, event: I::TabletToolTipEvent) {
3578 let tool = self.niri.seat.tablet_seat().get_tool(&event.tool());
3579
3580 let Some(tool) = tool else {
3581 return;
3582 };
3583 let tip_state = event.tip_state();
3584
3585 let is_overview_open = self.niri.layout.is_overview_open();
3586
3587 match tip_state {
3588 TabletToolTipState::Down => {
3589 let serial = SERIAL_COUNTER.next_serial();
3590 tool.tip_down(serial, event.time_msec());
3591
3592 if let Some(pos) = self.niri.tablet_cursor_location {
3593 let under = self.niri.contents_under(pos);
3594
3595 if self.niri.screenshot_ui.is_open() {
3596 if let Some(output) = under.output.clone() {
3597 let geom = self.niri.global_space.output_geometry(&output).unwrap();
3598 let mut point = (pos - geom.loc.to_f64())
3599 .to_physical(output.current_scale().fractional_scale())
3600 .to_i32_round();
3601
3602 let size = output.current_mode().unwrap().size;
3603 let transform = output.current_transform();
3604 let size = transform.transform_size(size);
3605 point.x = min(size.w - 1, point.x);
3606 point.y = min(size.h - 1, point.y);
3607
3608 if self.niri.screenshot_ui.pointer_down(output, point, None) {
3609 self.niri.queue_redraw_all();
3610 }
3611 }
3612 } else if let Some(mru_output) = self.niri.window_mru_ui.output() {
3613 if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
3614 if mru_output == output {
3615 let id = self.niri.window_mru_ui.pointer_motion(pos_within_output);
3616 if id.is_some() {
3617 self.confirm_mru();
3618 } else {
3619 self.niri.cancel_mru();
3620 }
3621 } else {
3622 self.niri.cancel_mru();
3623 }
3624 }
3625 } else if let Some((window, _)) = under.window {
3626 if let Some(output) = is_overview_open.then_some(under.output).flatten() {
3627 let mut workspaces = self.niri.layout.workspaces();
3628 if let Some(ws_idx) = workspaces.find_map(|(_, ws_idx, ws)| {
3629 ws.windows().any(|w| w.window == window).then_some(ws_idx)
3630 }) {
3631 drop(workspaces);
3632 self.niri.layout.focus_output(&output);
3633 self.niri.layout.toggle_overview_to_workspace(ws_idx);
3634 }
3635 }
3636
3637 self.niri.layout.activate_window(&window);
3638
3639 // FIXME: granular.
3640 self.niri.queue_redraw_all();
3641 } else if let Some((output, ws)) = is_overview_open
3642 .then(|| self.niri.workspace_under(false, pos))
3643 .flatten()
3644 {
3645 let ws_idx = self.niri.layout.find_workspace_by_id(ws.id()).unwrap().0;
3646
3647 self.niri.layout.focus_output(&output);
3648 self.niri.layout.toggle_overview_to_workspace(ws_idx);
3649
3650 // FIXME: granular.
3651 self.niri.queue_redraw_all();
3652 } else if let Some(output) = under.output {
3653 self.niri.layout.focus_output(&output);
3654
3655 // FIXME: granular.
3656 self.niri.queue_redraw_all();
3657 }
3658 self.niri.focus_layer_surface_if_on_demand(under.layer);
3659 }
3660 }
3661 TabletToolTipState::Up => {
3662 if let Some(capture) = self.niri.screenshot_ui.pointer_up(None) {
3663 if capture {
3664 self.confirm_screenshot(true);
3665 } else {
3666 self.niri.queue_redraw_all();
3667 }
3668 }
3669
3670 tool.tip_up(event.time_msec());
3671 }
3672 }
3673 }
3674
3675 fn on_tablet_tool_proximity<I: InputBackend>(&mut self, event: I::TabletToolProximityEvent)
3676 where
3677 I::Device: 'static, // Needed for downcasting.
3678 {
3679 let Some(pos) = self.compute_tablet_position(&event) else {
3680 return;
3681 };
3682
3683 let under = self.niri.contents_under(pos);
3684
3685 let tablet_seat = self.niri.seat.tablet_seat();
3686 let display_handle = self.niri.display_handle.clone();
3687 let tool = tablet_seat.add_tool::<Self>(self, &display_handle, &event.tool());
3688 let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device()));
3689 if let Some(tablet) = tablet {
3690 match event.state() {
3691 ProximityState::In => {
3692 if let Some(under) = under.surface {
3693 tool.proximity_in(
3694 pos,
3695 under,
3696 &tablet,
3697 SERIAL_COUNTER.next_serial(),
3698 event.time_msec(),
3699 );
3700 }
3701 self.niri.pointer_visibility = PointerVisibility::Visible;
3702 self.niri.tablet_cursor_location = Some(pos);
3703 }
3704 ProximityState::Out => {
3705 tool.proximity_out(event.time_msec());
3706
3707 // Move the mouse pointer here to avoid discontinuity.
3708 //
3709 // Plus, Wayland SDL2 currently warps the pointer into some weird
3710 // location on proximity out, so this should help it a little.
3711 if let Some(pos) = self.niri.tablet_cursor_location {
3712 self.move_cursor(pos);
3713 }
3714
3715 self.niri.pointer_visibility = PointerVisibility::Visible;
3716 self.niri.tablet_cursor_location = None;
3717 }
3718 }
3719
3720 // FIXME: granular.
3721 self.niri.queue_redraw_all();
3722 }
3723 }
3724
3725 fn on_tablet_tool_button<I: InputBackend>(&mut self, event: I::TabletToolButtonEvent) {
3726 let tool = self.niri.seat.tablet_seat().get_tool(&event.tool());
3727
3728 if let Some(tool) = tool {
3729 tool.button(
3730 event.button(),
3731 event.button_state(),
3732 SERIAL_COUNTER.next_serial(),
3733 event.time_msec(),
3734 );
3735 }
3736 }
3737
3738 fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) {
3739 if self.niri.window_mru_ui.is_open() {
3740 // Don't start swipe gestures while in the MRU.
3741 return;
3742 }
3743
3744 if event.fingers() == 3 {
3745 self.niri.gesture_swipe_3f_cumulative = Some((0., 0.));
3746
3747 // We handled this event.
3748 return;
3749 } else if event.fingers() == 4 {
3750 self.niri.layout.overview_gesture_begin();
3751 self.niri.queue_redraw_all();
3752
3753 // We handled this event.
3754 return;
3755 }
3756
3757 let serial = SERIAL_COUNTER.next_serial();
3758 let pointer = self.niri.seat.get_pointer().unwrap();
3759
3760 if self.update_pointer_contents() {
3761 pointer.frame(self);
3762 }
3763
3764 pointer.gesture_swipe_begin(
3765 self,
3766 &GestureSwipeBeginEvent {
3767 serial,
3768 time: event.time_msec(),
3769 fingers: event.fingers(),
3770 },
3771 );
3772 }
3773
3774 fn on_gesture_swipe_update<I: InputBackend + 'static>(
3775 &mut self,
3776 event: I::GestureSwipeUpdateEvent,
3777 ) where
3778 I::Device: 'static,
3779 {
3780 let mut delta_x = event.delta_x();
3781 let mut delta_y = event.delta_y();
3782
3783 if let Some(libinput_event) =
3784 (&event as &dyn Any).downcast_ref::<input::event::gesture::GestureSwipeUpdateEvent>()
3785 {
3786 delta_x = libinput_event.dx_unaccelerated();
3787 delta_y = libinput_event.dy_unaccelerated();
3788 }
3789
3790 let uninverted_delta_y = delta_y;
3791
3792 let device = event.device();
3793 if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() {
3794 if device.config_scroll_natural_scroll_enabled() {
3795 delta_x = -delta_x;
3796 delta_y = -delta_y;
3797 }
3798 }
3799
3800 let is_overview_open = self.niri.layout.is_overview_open();
3801
3802 if let Some((cx, cy)) = &mut self.niri.gesture_swipe_3f_cumulative {
3803 *cx += delta_x;
3804 *cy += delta_y;
3805
3806 // Check if the gesture moved far enough to decide. Threshold copied from GNOME Shell.
3807 let (cx, cy) = (*cx, *cy);
3808 if cx * cx + cy * cy >= 16. * 16. {
3809 self.niri.gesture_swipe_3f_cumulative = None;
3810
3811 if let Some(output) = self.niri.output_under_cursor() {
3812 if cx.abs() > cy.abs() {
3813 let output_ws = if is_overview_open {
3814 self.niri.workspace_under_cursor(true)
3815 } else {
3816 // We don't want to accidentally "catch" the wrong workspace during
3817 // animations.
3818 self.niri.output_under_cursor().and_then(|output| {
3819 let mon = self.niri.layout.monitor_for_output(&output)?;
3820 Some((output, mon.active_workspace_ref()))
3821 })
3822 };
3823
3824 if let Some((output, ws)) = output_ws {
3825 let ws_idx = self.niri.layout.find_workspace_by_id(ws.id()).unwrap().0;
3826 self.niri
3827 .layout
3828 .view_offset_gesture_begin(&output, Some(ws_idx), true);
3829 }
3830 } else {
3831 self.niri
3832 .layout
3833 .workspace_switch_gesture_begin(&output, true);
3834 }
3835 }
3836 }
3837 }
3838
3839 let timestamp = Duration::from_micros(event.time());
3840
3841 let mut handled = false;
3842 let res = self
3843 .niri
3844 .layout
3845 .workspace_switch_gesture_update(delta_y, timestamp, true);
3846 if let Some(output) = res {
3847 if let Some(output) = output {
3848 self.niri.queue_redraw(&output);
3849 }
3850 handled = true;
3851 }
3852
3853 let res = self
3854 .niri
3855 .layout
3856 .view_offset_gesture_update(delta_x, timestamp, true);
3857 if let Some(output) = res {
3858 if let Some(output) = output {
3859 self.niri.queue_redraw(&output);
3860 }
3861 handled = true;
3862 }
3863
3864 let res = self
3865 .niri
3866 .layout
3867 .overview_gesture_update(-uninverted_delta_y, timestamp);
3868 if let Some(redraw) = res {
3869 if redraw {
3870 self.niri.queue_redraw_all();
3871 }
3872 handled = true;
3873 }
3874
3875 if handled {
3876 // We handled this event.
3877 return;
3878 }
3879
3880 let pointer = self.niri.seat.get_pointer().unwrap();
3881
3882 if self.update_pointer_contents() {
3883 pointer.frame(self);
3884 }
3885
3886 pointer.gesture_swipe_update(
3887 self,
3888 &GestureSwipeUpdateEvent {
3889 time: event.time_msec(),
3890 delta: event.delta(),
3891 },
3892 );
3893 }
3894
3895 fn on_gesture_swipe_end<I: InputBackend>(&mut self, event: I::GestureSwipeEndEvent) {
3896 self.niri.gesture_swipe_3f_cumulative = None;
3897
3898 let mut handled = false;
3899 let res = self.niri.layout.workspace_switch_gesture_end(Some(true));
3900 if let Some(output) = res {
3901 self.niri.queue_redraw(&output);
3902 handled = true;
3903 }
3904
3905 let res = self.niri.layout.view_offset_gesture_end(Some(true));
3906 if let Some(output) = res {
3907 self.niri.queue_redraw(&output);
3908 handled = true;
3909 }
3910
3911 let res = self.niri.layout.overview_gesture_end();
3912 if res {
3913 self.niri.queue_redraw_all();
3914 handled = true;
3915 }
3916
3917 if handled {
3918 // We handled this event.
3919 return;
3920 }
3921
3922 let serial = SERIAL_COUNTER.next_serial();
3923 let pointer = self.niri.seat.get_pointer().unwrap();
3924
3925 if self.update_pointer_contents() {
3926 pointer.frame(self);
3927 }
3928
3929 pointer.gesture_swipe_end(
3930 self,
3931 &GestureSwipeEndEvent {
3932 serial,
3933 time: event.time_msec(),
3934 cancelled: event.cancelled(),
3935 },
3936 );
3937 }
3938
3939 fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) {
3940 let serial = SERIAL_COUNTER.next_serial();
3941 let pointer = self.niri.seat.get_pointer().unwrap();
3942
3943 if self.update_pointer_contents() {
3944 pointer.frame(self);
3945 }
3946
3947 pointer.gesture_pinch_begin(
3948 self,
3949 &GesturePinchBeginEvent {
3950 serial,
3951 time: event.time_msec(),
3952 fingers: event.fingers(),
3953 },
3954 );
3955 }
3956
3957 fn on_gesture_pinch_update<I: InputBackend>(&mut self, event: I::GesturePinchUpdateEvent) {
3958 let pointer = self.niri.seat.get_pointer().unwrap();
3959
3960 if self.update_pointer_contents() {
3961 pointer.frame(self);
3962 }
3963
3964 pointer.gesture_pinch_update(
3965 self,
3966 &GesturePinchUpdateEvent {
3967 time: event.time_msec(),
3968 delta: event.delta(),
3969 scale: event.scale(),
3970 rotation: event.rotation(),
3971 },
3972 );
3973 }
3974
3975 fn on_gesture_pinch_end<I: InputBackend>(&mut self, event: I::GesturePinchEndEvent) {
3976 let serial = SERIAL_COUNTER.next_serial();
3977 let pointer = self.niri.seat.get_pointer().unwrap();
3978
3979 if self.update_pointer_contents() {
3980 pointer.frame(self);
3981 }
3982
3983 pointer.gesture_pinch_end(
3984 self,
3985 &GesturePinchEndEvent {
3986 serial,
3987 time: event.time_msec(),
3988 cancelled: event.cancelled(),
3989 },
3990 );
3991 }
3992
3993 fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) {
3994 let serial = SERIAL_COUNTER.next_serial();
3995 let pointer = self.niri.seat.get_pointer().unwrap();
3996
3997 if self.update_pointer_contents() {
3998 pointer.frame(self);
3999 }
4000
4001 pointer.gesture_hold_begin(
4002 self,
4003 &GestureHoldBeginEvent {
4004 serial,
4005 time: event.time_msec(),
4006 fingers: event.fingers(),
4007 },
4008 );
4009 }
4010
4011 fn on_gesture_hold_end<I: InputBackend>(&mut self, event: I::GestureHoldEndEvent) {
4012 let serial = SERIAL_COUNTER.next_serial();
4013 let pointer = self.niri.seat.get_pointer().unwrap();
4014
4015 if self.update_pointer_contents() {
4016 pointer.frame(self);
4017 }
4018
4019 pointer.gesture_hold_end(
4020 self,
4021 &GestureHoldEndEvent {
4022 serial,
4023 time: event.time_msec(),
4024 cancelled: event.cancelled(),
4025 },
4026 );
4027 }
4028
4029 fn compute_absolute_location<I: InputBackend>(
4030 &self,
4031 evt: &impl AbsolutePositionEvent<I>,
4032 fallback_output: Option<&Output>,
4033 ) -> Option<Point<f64, Logical>> {
4034 let output = evt.device().output(self);
4035 let output = output.as_ref().or(fallback_output)?;
4036 let output_geo = self.niri.global_space.output_geometry(output).unwrap();
4037 let transform = output.current_transform();
4038 let size = transform.invert().transform_size(output_geo.size);
4039 Some(
4040 transform.transform_point_in(evt.position_transformed(size), &size.to_f64())
4041 + output_geo.loc.to_f64(),
4042 )
4043 }
4044
4045 /// Computes the cursor position for the touch event.
4046 ///
4047 /// This function handles the touch output mapping, as well as coordinate transform
4048 fn compute_touch_location<I: InputBackend>(
4049 &self,
4050 evt: &impl AbsolutePositionEvent<I>,
4051 ) -> Option<Point<f64, Logical>> {
4052 self.compute_absolute_location(evt, self.niri.output_for_touch())
4053 }
4054
4055 fn on_touch_down<I: InputBackend>(&mut self, evt: I::TouchDownEvent) {
4056 let Some(handle) = self.niri.seat.get_touch() else {
4057 return;
4058 };
4059 let Some(pos) = self.compute_touch_location(&evt) else {
4060 return;
4061 };
4062 let slot = evt.slot();
4063
4064 let serial = SERIAL_COUNTER.next_serial();
4065
4066 let under = self.niri.contents_under(pos);
4067
4068 let mod_key = self.backend.mod_key(&self.niri.config.borrow());
4069
4070 if self.niri.screenshot_ui.is_open() {
4071 if let Some(output) = under.output.clone() {
4072 let geom = self.niri.global_space.output_geometry(&output).unwrap();
4073 let mut point = (pos - geom.loc.to_f64())
4074 .to_physical(output.current_scale().fractional_scale())
4075 .to_i32_round();
4076
4077 let size = output.current_mode().unwrap().size;
4078 let transform = output.current_transform();
4079 let size = transform.transform_size(size);
4080 point.x = min(size.w - 1, point.x);
4081 point.y = min(size.h - 1, point.y);
4082
4083 if self
4084 .niri
4085 .screenshot_ui
4086 .pointer_down(output, point, Some(slot))
4087 {
4088 self.niri.queue_redraw_all();
4089 }
4090 }
4091 } else if let Some(mru_output) = self.niri.window_mru_ui.output() {
4092 if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
4093 if mru_output == output {
4094 let id = self.niri.window_mru_ui.pointer_motion(pos_within_output);
4095 if id.is_some() {
4096 self.confirm_mru();
4097 } else {
4098 self.niri.cancel_mru();
4099 }
4100 } else {
4101 self.niri.cancel_mru();
4102 }
4103 }
4104 } else if !handle.is_grabbed() {
4105 let mods = self.niri.seat.get_keyboard().unwrap().modifier_state();
4106 let mods = modifiers_from_state(mods);
4107 let mod_down = mods.contains(mod_key.to_modifiers());
4108
4109 if self.niri.layout.is_overview_open()
4110 && !mod_down
4111 && under.layer.is_none()
4112 && under.output.is_some()
4113 {
4114 let (output, pos_within_output) = self.niri.output_under(pos).unwrap();
4115 let output = output.clone();
4116
4117 let mut matched_narrow = true;
4118 let mut ws = self.niri.workspace_under(false, pos);
4119 if ws.is_none() {
4120 matched_narrow = false;
4121 ws = self.niri.workspace_under(true, pos);
4122 }
4123 let ws_id = ws.map(|(_, ws)| ws.id());
4124
4125 let mapped = self.niri.window_under(pos);
4126 let window = mapped.map(|mapped| mapped.window.clone());
4127
4128 let start_data = TouchGrabStartData {
4129 focus: None,
4130 slot,
4131 location: pos,
4132 };
4133 let start_timestamp = Duration::from_micros(evt.time());
4134 let grab = TouchOverviewGrab::new(
4135 start_data,
4136 start_timestamp,
4137 output,
4138 pos_within_output,
4139 ws_id,
4140 matched_narrow,
4141 window,
4142 );
4143 handle.set_grab(self, grab, serial);
4144 } else if let Some((window, _)) = under.window {
4145 self.niri.layout.activate_window(&window);
4146
4147 // Check if we need to start a touch move grab.
4148 if mod_down {
4149 let start_data = TouchGrabStartData {
4150 focus: None,
4151 slot,
4152 location: pos,
4153 };
4154 let start_data = PointerOrTouchStartData::Touch(start_data);
4155 if let Some(grab) = MoveGrab::new(self, start_data, window.clone(), true, None)
4156 {
4157 handle.set_grab(self, grab, serial);
4158 }
4159 }
4160
4161 // FIXME: granular.
4162 self.niri.queue_redraw_all();
4163 } else if let Some(output) = under.output {
4164 self.niri.layout.focus_output(&output);
4165
4166 // FIXME: granular.
4167 self.niri.queue_redraw_all();
4168 }
4169 self.niri.focus_layer_surface_if_on_demand(under.layer);
4170 };
4171
4172 handle.down(
4173 self,
4174 under.surface,
4175 &DownEvent {
4176 slot,
4177 location: pos,
4178 serial,
4179 time: evt.time_msec(),
4180 },
4181 );
4182
4183 // We're using touch, hide the pointer.
4184 self.niri.pointer_visibility = PointerVisibility::Disabled;
4185 }
4186 fn on_touch_up<I: InputBackend>(&mut self, evt: I::TouchUpEvent) {
4187 let Some(handle) = self.niri.seat.get_touch() else {
4188 return;
4189 };
4190 let slot = evt.slot();
4191
4192 if let Some(capture) = self.niri.screenshot_ui.pointer_up(Some(slot)) {
4193 if capture {
4194 self.confirm_screenshot(true);
4195 } else {
4196 self.niri.queue_redraw_all();
4197 }
4198 }
4199
4200 let serial = SERIAL_COUNTER.next_serial();
4201 handle.up(
4202 self,
4203 &UpEvent {
4204 slot,
4205 serial,
4206 time: evt.time_msec(),
4207 },
4208 )
4209 }
4210 fn on_touch_motion<I: InputBackend>(&mut self, evt: I::TouchMotionEvent) {
4211 let Some(handle) = self.niri.seat.get_touch() else {
4212 return;
4213 };
4214 let Some(pos) = self.compute_touch_location(&evt) else {
4215 return;
4216 };
4217 let slot = evt.slot();
4218
4219 if let Some(output) = self.niri.screenshot_ui.selection_output().cloned() {
4220 let geom = self.niri.global_space.output_geometry(&output).unwrap();
4221 let mut point = (pos - geom.loc.to_f64())
4222 .to_physical(output.current_scale().fractional_scale())
4223 .to_i32_round::<i32>();
4224
4225 let size = output.current_mode().unwrap().size;
4226 let transform = output.current_transform();
4227 let size = transform.transform_size(size);
4228 point.x = point.x.clamp(0, size.w - 1);
4229 point.y = point.y.clamp(0, size.h - 1);
4230
4231 self.niri.screenshot_ui.pointer_motion(point, Some(slot));
4232 self.niri.queue_redraw(&output);
4233 }
4234
4235 let under = self.niri.contents_under(pos);
4236 handle.motion(
4237 self,
4238 under.surface,
4239 &TouchMotionEvent {
4240 slot,
4241 location: pos,
4242 time: evt.time_msec(),
4243 },
4244 );
4245
4246 // Inform the layout of an ongoing DnD operation.
4247 let is_dnd_grab = handle
4248 .with_grab(|_, grab| Self::is_dnd_grab(grab.as_any()))
4249 .unwrap_or(false);
4250 if is_dnd_grab {
4251 if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
4252 let output = output.clone();
4253 self.niri.layout.dnd_update(output, pos_within_output);
4254 }
4255 }
4256 }
4257 fn on_touch_frame<I: InputBackend>(&mut self, _evt: I::TouchFrameEvent) {
4258 let Some(handle) = self.niri.seat.get_touch() else {
4259 return;
4260 };
4261 handle.frame(self);
4262 }
4263 fn on_touch_cancel<I: InputBackend>(&mut self, _evt: I::TouchCancelEvent) {
4264 let Some(handle) = self.niri.seat.get_touch() else {
4265 return;
4266 };
4267 handle.cancel(self);
4268 }
4269
4270 fn on_switch_toggle<I: InputBackend>(&mut self, evt: I::SwitchToggleEvent) {
4271 let Some(switch) = evt.switch() else {
4272 return;
4273 };
4274
4275 if switch == Switch::Lid {
4276 let is_closed = evt.state() == SwitchState::On;
4277 trace!("lid switch {}", if is_closed { "closed" } else { "opened" });
4278 self.set_lid_closed(is_closed);
4279 }
4280
4281 let action = {
4282 let bindings = &self.niri.config.borrow().switch_events;
4283 find_configured_switch_action(bindings, switch, evt.state())
4284 };
4285
4286 if let Some(action) = action {
4287 self.do_action(action, true);
4288 }
4289 }
4290
4291 pub fn is_dnd_grab(grab: &dyn Any) -> bool {
4292 // Normal DnD
4293 grab.is::<DnDGrab<Self, WlDataSource, WlSurface>>()
4294 // Null-source DnD: weston-dnd --self-only
4295 || grab.is::<DnDGrab<Self, WlSurface, WlSurface>>()
4296 }
4297}
4298
4299/// Check whether the key should be intercepted and mark intercepted
4300/// pressed keys as `suppressed`, thus preventing `releases` corresponding
4301/// to them from being delivered.
4302#[allow(clippy::too_many_arguments)]
4303fn should_intercept_key<'a>(
4304 suppressed_keys: &mut HashSet<Keycode>,
4305 bindings: impl IntoIterator<Item = &'a Bind>,
4306 mod_key: ModKey,
4307 key_code: Keycode,
4308 modified: Keysym,
4309 raw: Option<Keysym>,
4310 pressed: bool,
4311 mods: ModifiersState,
4312 screenshot_ui: &ScreenshotUi,
4313 disable_power_key_handling: bool,
4314 is_inhibiting_shortcuts: bool,
4315) -> FilterResult<Option<Bind>> {
4316 // Actions are only triggered on presses, release of the key
4317 // shouldn't try to intercept anything unless we have marked
4318 // the key to suppress.
4319 if !pressed && !suppressed_keys.contains(&key_code) {
4320 return FilterResult::Forward;
4321 }
4322
4323 let mut final_bind = find_bind(
4324 bindings,
4325 mod_key,
4326 modified,
4327 raw,
4328 mods,
4329 disable_power_key_handling,
4330 );
4331
4332 // Allow only a subset of compositor actions while the screenshot UI is open, since the user
4333 // cannot see the screen.
4334 if screenshot_ui.is_open() {
4335 let mut use_screenshot_ui_action = true;
4336
4337 if let Some(bind) = &final_bind {
4338 if allowed_during_screenshot(&bind.action) {
4339 use_screenshot_ui_action = false;
4340 }
4341 }
4342
4343 if use_screenshot_ui_action {
4344 if let Some(raw) = raw {
4345 final_bind = screenshot_ui.action(raw, mods).map(|action| Bind {
4346 key: Key {
4347 trigger: Trigger::Keysym(raw),
4348 // Not entirely correct but it doesn't matter in how we currently use it.
4349 modifiers: Modifiers::empty(),
4350 },
4351 action,
4352 repeat: true,
4353 cooldown: None,
4354 allow_when_locked: false,
4355 // The screenshot UI owns the focus anyway, so this doesn't really matter.
4356 // But logically, nothing can inhibit its actions. Only opening it can be
4357 // inhibited.
4358 allow_inhibiting: false,
4359 hotkey_overlay_title: None,
4360 });
4361 }
4362 }
4363 }
4364
4365 match (final_bind, pressed) {
4366 (Some(bind), true) => {
4367 if is_inhibiting_shortcuts && bind.allow_inhibiting {
4368 FilterResult::Forward
4369 } else {
4370 suppressed_keys.insert(key_code);
4371 FilterResult::Intercept(Some(bind))
4372 }
4373 }
4374 (_, false) => {
4375 // By this point, we know that the key was suppressed on press. Even if we're inhibiting
4376 // shortcuts, we should still suppress the release.
4377 // But we don't need to check for shortcuts inhibition here, because
4378 // if it was inhibited on press (forwarded to the client), it wouldn't be suppressed,
4379 // so the release would already have been forwarded at the start of this function.
4380 suppressed_keys.remove(&key_code);
4381 FilterResult::Intercept(None)
4382 }
4383 (None, true) => FilterResult::Forward,
4384 }
4385}
4386
4387fn find_bind<'a>(
4388 bindings: impl IntoIterator<Item = &'a Bind>,
4389 mod_key: ModKey,
4390 modified: Keysym,
4391 raw: Option<Keysym>,
4392 mods: ModifiersState,
4393 disable_power_key_handling: bool,
4394) -> Option<Bind> {
4395 use keysyms::*;
4396
4397 // Handle hardcoded binds.
4398 #[allow(non_upper_case_globals)] // wat
4399 let hardcoded_action = match modified.raw() {
4400 modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => {
4401 let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32;
4402 Some(Action::ChangeVt(vt))
4403 }
4404 KEY_XF86PowerOff if !disable_power_key_handling => Some(Action::Suspend),
4405 _ => None,
4406 };
4407
4408 if let Some(action) = hardcoded_action {
4409 return Some(Bind {
4410 key: Key {
4411 // Not entirely correct but it doesn't matter in how we currently use it.
4412 trigger: Trigger::Keysym(modified),
4413 modifiers: Modifiers::empty(),
4414 },
4415 action,
4416 repeat: true,
4417 cooldown: None,
4418 allow_when_locked: false,
4419 // In a worst-case scenario, the user has no way to unlock the compositor and a
4420 // misbehaving client has a keyboard shortcuts inhibitor, "jailing" the user.
4421 // The user must always be able to change VTs to recover from such a situation.
4422 // It also makes no sense to inhibit the default power key handling.
4423 // Hardcoded binds must never be inhibited.
4424 allow_inhibiting: false,
4425 hotkey_overlay_title: None,
4426 });
4427 }
4428
4429 let trigger = Trigger::Keysym(raw?);
4430 find_configured_bind(bindings, mod_key, trigger, mods)
4431}
4432
4433fn find_configured_bind<'a>(
4434 bindings: impl IntoIterator<Item = &'a Bind>,
4435 mod_key: ModKey,
4436 trigger: Trigger,
4437 mods: ModifiersState,
4438) -> Option<Bind> {
4439 // Handle configured binds.
4440 let mut modifiers = modifiers_from_state(mods);
4441
4442 let mod_down = modifiers_from_state(mods).contains(mod_key.to_modifiers());
4443 if mod_down {
4444 modifiers |= Modifiers::COMPOSITOR;
4445 }
4446
4447 for bind in bindings {
4448 if bind.key.trigger != trigger {
4449 continue;
4450 }
4451
4452 let mut bind_modifiers = bind.key.modifiers;
4453 if bind_modifiers.contains(Modifiers::COMPOSITOR) {
4454 bind_modifiers |= mod_key.to_modifiers();
4455 } else if bind_modifiers.contains(mod_key.to_modifiers()) {
4456 bind_modifiers |= Modifiers::COMPOSITOR;
4457 }
4458
4459 if bind_modifiers == modifiers {
4460 return Some(bind.clone());
4461 }
4462 }
4463
4464 None
4465}
4466
4467fn find_configured_switch_action(
4468 bindings: &SwitchBinds,
4469 switch: Switch,
4470 state: SwitchState,
4471) -> Option<Action> {
4472 let switch_action = match (switch, state) {
4473 (Switch::Lid, SwitchState::Off) => &bindings.lid_open,
4474 (Switch::Lid, SwitchState::On) => &bindings.lid_close,
4475 (Switch::TabletMode, SwitchState::Off) => &bindings.tablet_mode_off,
4476 (Switch::TabletMode, SwitchState::On) => &bindings.tablet_mode_on,
4477 _ => unreachable!(),
4478 };
4479 switch_action
4480 .as_ref()
4481 .map(|switch_action| Action::Spawn(switch_action.spawn.clone()))
4482}
4483
4484fn modifiers_from_state(mods: ModifiersState) -> Modifiers {
4485 let mut modifiers = Modifiers::empty();
4486 if mods.ctrl {
4487 modifiers |= Modifiers::CTRL;
4488 }
4489 if mods.shift {
4490 modifiers |= Modifiers::SHIFT;
4491 }
4492 if mods.alt {
4493 modifiers |= Modifiers::ALT;
4494 }
4495 if mods.logo {
4496 modifiers |= Modifiers::SUPER;
4497 }
4498 if mods.iso_level3_shift {
4499 modifiers |= Modifiers::ISO_LEVEL3_SHIFT;
4500 }
4501 if mods.iso_level5_shift {
4502 modifiers |= Modifiers::ISO_LEVEL5_SHIFT;
4503 }
4504 modifiers
4505}
4506
4507fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool {
4508 match event {
4509 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
4510 InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true,
4511 InputEvent::PointerMotion { .. }
4512 | InputEvent::PointerMotionAbsolute { .. }
4513 | InputEvent::PointerAxis { .. }
4514 | InputEvent::GestureSwipeBegin { .. }
4515 | InputEvent::GesturePinchBegin { .. }
4516 | InputEvent::GestureHoldBegin { .. }
4517 | InputEvent::TouchDown { .. }
4518 | InputEvent::TouchMotion { .. }
4519 | InputEvent::TabletToolAxis { .. }
4520 | InputEvent::TabletToolProximity { .. }
4521 | InputEvent::TabletToolTip { .. }
4522 | InputEvent::TabletToolButton { .. } => true,
4523 // Ignore events like device additions and removals, key releases, gesture ends.
4524 _ => false,
4525 }
4526}
4527
4528fn should_hide_hotkey_overlay<I: InputBackend>(event: &InputEvent<I>) -> bool {
4529 match event {
4530 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
4531 InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true,
4532 InputEvent::PointerAxis { .. }
4533 | InputEvent::GestureSwipeBegin { .. }
4534 | InputEvent::GesturePinchBegin { .. }
4535 | InputEvent::TouchDown { .. }
4536 | InputEvent::TouchMotion { .. }
4537 | InputEvent::TabletToolTip { .. }
4538 | InputEvent::TabletToolButton { .. } => true,
4539 _ => false,
4540 }
4541}
4542
4543fn should_hide_exit_confirm_dialog<I: InputBackend>(event: &InputEvent<I>) -> bool {
4544 match event {
4545 InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true,
4546 InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true,
4547 InputEvent::PointerAxis { .. }
4548 | InputEvent::GestureSwipeBegin { .. }
4549 | InputEvent::GesturePinchBegin { .. }
4550 | InputEvent::TouchDown { .. }
4551 | InputEvent::TouchMotion { .. }
4552 | InputEvent::TabletToolTip { .. }
4553 | InputEvent::TabletToolButton { .. } => true,
4554 _ => false,
4555 }
4556}
4557
4558fn should_notify_activity<I: InputBackend>(event: &InputEvent<I>) -> bool {
4559 !matches!(
4560 event,
4561 InputEvent::DeviceAdded { .. } | InputEvent::DeviceRemoved { .. }
4562 )
4563}
4564
4565fn should_reset_pointer_inactivity_timer<I: InputBackend>(event: &InputEvent<I>) -> bool {
4566 matches!(
4567 event,
4568 InputEvent::PointerAxis { .. }
4569 | InputEvent::PointerButton { .. }
4570 | InputEvent::PointerMotion { .. }
4571 | InputEvent::PointerMotionAbsolute { .. }
4572 | InputEvent::TabletToolAxis { .. }
4573 | InputEvent::TabletToolButton { .. }
4574 | InputEvent::TabletToolProximity { .. }
4575 | InputEvent::TabletToolTip { .. }
4576 )
4577}
4578
4579fn allowed_when_locked(action: &Action) -> bool {
4580 matches!(
4581 action,
4582 Action::Quit(_)
4583 | Action::ChangeVt(_)
4584 | Action::Suspend
4585 | Action::PowerOffMonitors
4586 | Action::PowerOnMonitors
4587 | Action::SwitchLayout(_)
4588 | Action::ToggleKeyboardShortcutsInhibit
4589 )
4590}
4591
4592fn allowed_during_screenshot(action: &Action) -> bool {
4593 matches!(
4594 action,
4595 Action::Quit(_)
4596 | Action::ChangeVt(_)
4597 | Action::Suspend
4598 | Action::PowerOffMonitors
4599 | Action::PowerOnMonitors
4600 // The screenshot UI can handle these.
4601 | Action::MoveColumnLeft
4602 | Action::MoveColumnLeftOrToMonitorLeft
4603 | Action::MoveColumnRight
4604 | Action::MoveColumnRightOrToMonitorRight
4605 | Action::MoveWindowUp
4606 | Action::MoveWindowUpOrToWorkspaceUp
4607 | Action::MoveWindowDown
4608 | Action::MoveWindowDownOrToWorkspaceDown
4609 | Action::MoveColumnToMonitorLeft
4610 | Action::MoveColumnToMonitorRight
4611 | Action::MoveColumnToMonitorUp
4612 | Action::MoveColumnToMonitorDown
4613 | Action::MoveColumnToMonitorPrevious
4614 | Action::MoveColumnToMonitorNext
4615 | Action::MoveColumnToMonitor(_)
4616 | Action::MoveWindowToMonitorLeft
4617 | Action::MoveWindowToMonitorRight
4618 | Action::MoveWindowToMonitorUp
4619 | Action::MoveWindowToMonitorDown
4620 | Action::MoveWindowToMonitorPrevious
4621 | Action::MoveWindowToMonitorNext
4622 | Action::MoveWindowToMonitor(_)
4623 | Action::SetWindowWidth(_)
4624 | Action::SetWindowHeight(_)
4625 | Action::SetColumnWidth(_)
4626 )
4627}
4628
4629fn hardcoded_overview_bind(raw: Keysym, mods: ModifiersState) -> Option<Bind> {
4630 let mods = modifiers_from_state(mods);
4631 if !mods.is_empty() {
4632 return None;
4633 }
4634
4635 let mut repeat = true;
4636 let action = match raw {
4637 Keysym::Escape | Keysym::Return => {
4638 repeat = false;
4639 Action::ToggleOverview
4640 }
4641 Keysym::Left => Action::FocusColumnLeft,
4642 Keysym::Right => Action::FocusColumnRight,
4643 Keysym::Up => Action::FocusWindowOrWorkspaceUp,
4644 Keysym::Down => Action::FocusWindowOrWorkspaceDown,
4645 _ => {
4646 return None;
4647 }
4648 };
4649
4650 Some(Bind {
4651 key: Key {
4652 trigger: Trigger::Keysym(raw),
4653 modifiers: Modifiers::empty(),
4654 },
4655 action,
4656 repeat,
4657 cooldown: None,
4658 allow_when_locked: false,
4659 allow_inhibiting: false,
4660 hotkey_overlay_title: None,
4661 })
4662}
4663
4664pub fn apply_libinput_settings(config: &niri_config::Input, device: &mut input::Device) {
4665 // According to Mutter code, this setting is specific to touchpads.
4666 let is_touchpad = device.config_tap_finger_count() > 0;
4667 if is_touchpad {
4668 let c = &config.touchpad;
4669 let _ = device.config_send_events_set_mode(if c.off {
4670 input::SendEventsMode::DISABLED
4671 } else if c.disabled_on_external_mouse {
4672 input::SendEventsMode::DISABLED_ON_EXTERNAL_MOUSE
4673 } else {
4674 input::SendEventsMode::ENABLED
4675 });
4676 let _ = device.config_tap_set_enabled(c.tap);
4677 let _ = device.config_dwt_set_enabled(c.dwt);
4678 let _ = device.config_dwtp_set_enabled(c.dwtp);
4679 let _ = device.config_tap_set_drag_lock_enabled(c.drag_lock);
4680 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
4681 let _ = device.config_accel_set_speed(c.accel_speed.0);
4682 let _ = device.config_left_handed_set(c.left_handed);
4683 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
4684
4685 if let Some(drag) = c.drag {
4686 let _ = device.config_tap_set_drag_enabled(drag);
4687 } else {
4688 let default = device.config_tap_default_drag_enabled();
4689 let _ = device.config_tap_set_drag_enabled(default);
4690 }
4691
4692 if let Some(accel_profile) = c.accel_profile {
4693 let _ = device.config_accel_set_profile(accel_profile.into());
4694 } else if let Some(default) = device.config_accel_default_profile() {
4695 let _ = device.config_accel_set_profile(default);
4696 }
4697
4698 if let Some(method) = c.scroll_method {
4699 let _ = device.config_scroll_set_method(method.into());
4700
4701 if method == niri_config::ScrollMethod::OnButtonDown {
4702 if let Some(button) = c.scroll_button {
4703 let _ = device.config_scroll_set_button(button);
4704 }
4705 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4706 input::ScrollButtonLockState::Enabled
4707 } else {
4708 input::ScrollButtonLockState::Disabled
4709 });
4710 }
4711 } else if let Some(default) = device.config_scroll_default_method() {
4712 let _ = device.config_scroll_set_method(default);
4713
4714 if default == input::ScrollMethod::OnButtonDown {
4715 if let Some(button) = c.scroll_button {
4716 let _ = device.config_scroll_set_button(button);
4717 }
4718 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4719 input::ScrollButtonLockState::Enabled
4720 } else {
4721 input::ScrollButtonLockState::Disabled
4722 });
4723 }
4724 }
4725
4726 if let Some(tap_button_map) = c.tap_button_map {
4727 let _ = device.config_tap_set_button_map(tap_button_map.into());
4728 } else if let Some(default) = device.config_tap_default_button_map() {
4729 let _ = device.config_tap_set_button_map(default);
4730 }
4731
4732 if let Some(method) = c.click_method {
4733 let _ = device.config_click_set_method(method.into());
4734 } else if let Some(default) = device.config_click_default_method() {
4735 let _ = device.config_click_set_method(default);
4736 }
4737 }
4738
4739 // This is how Mutter tells apart mice.
4740 let mut is_trackball = false;
4741 let mut is_trackpoint = false;
4742 if let Some(udev_device) = unsafe { device.udev_device() } {
4743 if udev_device.property_value("ID_INPUT_TRACKBALL").is_some() {
4744 is_trackball = true;
4745 }
4746 if udev_device
4747 .property_value("ID_INPUT_POINTINGSTICK")
4748 .is_some()
4749 {
4750 is_trackpoint = true;
4751 }
4752 }
4753
4754 let is_mouse = device.has_capability(input::DeviceCapability::Pointer)
4755 && !is_touchpad
4756 && !is_trackball
4757 && !is_trackpoint;
4758 if is_mouse {
4759 let c = &config.mouse;
4760 let _ = device.config_send_events_set_mode(if c.off {
4761 input::SendEventsMode::DISABLED
4762 } else {
4763 input::SendEventsMode::ENABLED
4764 });
4765 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
4766 let _ = device.config_accel_set_speed(c.accel_speed.0);
4767 let _ = device.config_left_handed_set(c.left_handed);
4768 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
4769
4770 if let Some(accel_profile) = c.accel_profile {
4771 let _ = device.config_accel_set_profile(accel_profile.into());
4772 } else if let Some(default) = device.config_accel_default_profile() {
4773 let _ = device.config_accel_set_profile(default);
4774 }
4775
4776 if let Some(method) = c.scroll_method {
4777 let _ = device.config_scroll_set_method(method.into());
4778
4779 if method == niri_config::ScrollMethod::OnButtonDown {
4780 if let Some(button) = c.scroll_button {
4781 let _ = device.config_scroll_set_button(button);
4782 }
4783 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4784 input::ScrollButtonLockState::Enabled
4785 } else {
4786 input::ScrollButtonLockState::Disabled
4787 });
4788 }
4789 } else if let Some(default) = device.config_scroll_default_method() {
4790 let _ = device.config_scroll_set_method(default);
4791
4792 if default == input::ScrollMethod::OnButtonDown {
4793 if let Some(button) = c.scroll_button {
4794 let _ = device.config_scroll_set_button(button);
4795 }
4796 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4797 input::ScrollButtonLockState::Enabled
4798 } else {
4799 input::ScrollButtonLockState::Disabled
4800 });
4801 }
4802 }
4803 }
4804
4805 if is_trackball {
4806 let c = &config.trackball;
4807 let _ = device.config_send_events_set_mode(if c.off {
4808 input::SendEventsMode::DISABLED
4809 } else {
4810 input::SendEventsMode::ENABLED
4811 });
4812 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
4813 let _ = device.config_accel_set_speed(c.accel_speed.0);
4814 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
4815 let _ = device.config_left_handed_set(c.left_handed);
4816
4817 if let Some(accel_profile) = c.accel_profile {
4818 let _ = device.config_accel_set_profile(accel_profile.into());
4819 } else if let Some(default) = device.config_accel_default_profile() {
4820 let _ = device.config_accel_set_profile(default);
4821 }
4822
4823 if let Some(method) = c.scroll_method {
4824 let _ = device.config_scroll_set_method(method.into());
4825
4826 if method == niri_config::ScrollMethod::OnButtonDown {
4827 if let Some(button) = c.scroll_button {
4828 let _ = device.config_scroll_set_button(button);
4829 }
4830 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4831 input::ScrollButtonLockState::Enabled
4832 } else {
4833 input::ScrollButtonLockState::Disabled
4834 });
4835 }
4836 } else if let Some(default) = device.config_scroll_default_method() {
4837 let _ = device.config_scroll_set_method(default);
4838
4839 if default == input::ScrollMethod::OnButtonDown {
4840 if let Some(button) = c.scroll_button {
4841 let _ = device.config_scroll_set_button(button);
4842 }
4843 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4844 input::ScrollButtonLockState::Enabled
4845 } else {
4846 input::ScrollButtonLockState::Disabled
4847 });
4848 }
4849 }
4850 }
4851
4852 if is_trackpoint {
4853 let c = &config.trackpoint;
4854 let _ = device.config_send_events_set_mode(if c.off {
4855 input::SendEventsMode::DISABLED
4856 } else {
4857 input::SendEventsMode::ENABLED
4858 });
4859 let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll);
4860 let _ = device.config_accel_set_speed(c.accel_speed.0);
4861 let _ = device.config_left_handed_set(c.left_handed);
4862 let _ = device.config_middle_emulation_set_enabled(c.middle_emulation);
4863
4864 if let Some(accel_profile) = c.accel_profile {
4865 let _ = device.config_accel_set_profile(accel_profile.into());
4866 } else if let Some(default) = device.config_accel_default_profile() {
4867 let _ = device.config_accel_set_profile(default);
4868 }
4869
4870 if let Some(method) = c.scroll_method {
4871 let _ = device.config_scroll_set_method(method.into());
4872
4873 if method == niri_config::ScrollMethod::OnButtonDown {
4874 if let Some(button) = c.scroll_button {
4875 let _ = device.config_scroll_set_button(button);
4876 }
4877 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4878 input::ScrollButtonLockState::Enabled
4879 } else {
4880 input::ScrollButtonLockState::Disabled
4881 });
4882 }
4883 } else if let Some(default) = device.config_scroll_default_method() {
4884 let _ = device.config_scroll_set_method(default);
4885
4886 if default == input::ScrollMethod::OnButtonDown {
4887 if let Some(button) = c.scroll_button {
4888 let _ = device.config_scroll_set_button(button);
4889 }
4890 let _ = device.config_scroll_set_button_lock(if c.scroll_button_lock {
4891 input::ScrollButtonLockState::Enabled
4892 } else {
4893 input::ScrollButtonLockState::Disabled
4894 });
4895 }
4896 }
4897 }
4898
4899 let is_tablet = device.has_capability(input::DeviceCapability::TabletTool);
4900 if is_tablet {
4901 let c = &config.tablet;
4902 let _ = device.config_send_events_set_mode(if c.off {
4903 input::SendEventsMode::DISABLED
4904 } else {
4905 input::SendEventsMode::ENABLED
4906 });
4907
4908 #[rustfmt::skip]
4909 const IDENTITY_MATRIX: [f32; 6] = [
4910 1., 0., 0.,
4911 0., 1., 0.,
4912 ];
4913
4914 let _ = device.config_calibration_set_matrix(
4915 c.calibration_matrix
4916 .as_deref()
4917 .and_then(|m| m.try_into().ok())
4918 .or(device.config_calibration_default_matrix())
4919 .unwrap_or(IDENTITY_MATRIX),
4920 );
4921
4922 let _ = device.config_left_handed_set(c.left_handed);
4923 }
4924
4925 let is_touch = device.has_capability(input::DeviceCapability::Touch);
4926 if is_touch {
4927 let c = &config.touch;
4928 let _ = device.config_send_events_set_mode(if c.off {
4929 input::SendEventsMode::DISABLED
4930 } else {
4931 input::SendEventsMode::ENABLED
4932 });
4933
4934 #[rustfmt::skip]
4935 const IDENTITY_MATRIX: [f32; 6] = [
4936 1., 0., 0.,
4937 0., 1., 0.,
4938 ];
4939
4940 let _ = device.config_calibration_set_matrix(
4941 c.calibration_matrix
4942 .as_deref()
4943 .and_then(|m| m.try_into().ok())
4944 .or(device.config_calibration_default_matrix())
4945 .unwrap_or(IDENTITY_MATRIX),
4946 );
4947 }
4948}
4949
4950pub fn mods_with_binds(mod_key: ModKey, binds: &Binds, triggers: &[Trigger]) -> HashSet<Modifiers> {
4951 let mut rv = HashSet::new();
4952 for bind in &binds.0 {
4953 if !triggers.contains(&bind.key.trigger) {
4954 continue;
4955 }
4956
4957 let mut mods = bind.key.modifiers;
4958 if mods.contains(Modifiers::COMPOSITOR) {
4959 mods.remove(Modifiers::COMPOSITOR);
4960 mods.insert(mod_key.to_modifiers());
4961 }
4962
4963 rv.insert(mods);
4964 }
4965
4966 rv
4967}
4968
4969pub fn mods_with_mouse_binds(mod_key: ModKey, binds: &Binds) -> HashSet<Modifiers> {
4970 mods_with_binds(
4971 mod_key,
4972 binds,
4973 &[
4974 Trigger::MouseLeft,
4975 Trigger::MouseRight,
4976 Trigger::MouseMiddle,
4977 Trigger::MouseBack,
4978 Trigger::MouseForward,
4979 ],
4980 )
4981}
4982
4983pub fn mods_with_wheel_binds(mod_key: ModKey, binds: &Binds) -> HashSet<Modifiers> {
4984 mods_with_binds(
4985 mod_key,
4986 binds,
4987 &[
4988 Trigger::WheelScrollUp,
4989 Trigger::WheelScrollDown,
4990 Trigger::WheelScrollLeft,
4991 Trigger::WheelScrollRight,
4992 ],
4993 )
4994}
4995
4996pub fn mods_with_finger_scroll_binds(mod_key: ModKey, binds: &Binds) -> HashSet<Modifiers> {
4997 mods_with_binds(
4998 mod_key,
4999 binds,
5000 &[
5001 Trigger::TouchpadScrollUp,
5002 Trigger::TouchpadScrollDown,
5003 Trigger::TouchpadScrollLeft,
5004 Trigger::TouchpadScrollRight,
5005 ],
5006 )
5007}
5008
5009fn grab_allows_hot_corner(grab: &(dyn PointerGrab<State> + 'static)) -> bool {
5010 let grab = grab.as_any();
5011
5012 // We lean on the blocklist approach here since it's not a terribly big deal if hot corner
5013 // works where it shouldn't, but it could prevent some workflows if the hot corner doesn't work
5014 // when it should.
5015 //
5016 // Some notable grabs not mentioned here:
5017 // - DnDGrab allows hot corner to DnD across workspaces.
5018 // - ClickGrab keeps pointer focus on the window, so the hot corner doesn't trigger.
5019 // - Touch grabs: touch doesn't trigger the hot corner.
5020 if grab.is::<ResizeGrab>() || grab.is::<SpatialMovementGrab>() {
5021 return false;
5022 }
5023
5024 if let Some(grab) = grab.downcast_ref::<MoveGrab>() {
5025 // Window move allows hot corner to DnD across workspaces.
5026 if !grab.is_move() {
5027 return false;
5028 }
5029 }
5030
5031 true
5032}
5033
5034/// Returns an iterator over bindings.
5035///
5036/// Includes dynamically populated bindings like the MRU UI.
5037fn make_binds_iter<'a>(
5038 config: &'a Config,
5039 mru: &'a mut WindowMruUi,
5040 mods: Modifiers,
5041) -> impl Iterator<Item = &'a Bind> + Clone {
5042 // Figure out the binds to use depending on whether the MRU is enabled and/or open.
5043 let general_binds = (!mru.is_open()).then_some(config.binds.0.iter());
5044 let general_binds = general_binds.into_iter().flatten();
5045
5046 let mru_binds =
5047 (config.recent_windows.on || mru.is_open()).then_some(config.recent_windows.binds.iter());
5048 let mru_binds = mru_binds.into_iter().flatten();
5049
5050 let mru_open_binds = mru.is_open().then(|| mru.opened_bindings(mods));
5051 let mru_open_binds = mru_open_binds.into_iter().flatten();
5052
5053 // General binds take precedence over the MRU binds.
5054 general_binds.chain(mru_binds).chain(mru_open_binds)
5055}
5056
5057#[cfg(test)]
5058mod tests {
5059 use std::cell::Cell;
5060
5061 use super::*;
5062 use crate::animation::Clock;
5063
5064 #[test]
5065 fn bindings_suppress_keys() {
5066 let close_keysym = Keysym::q;
5067 let bindings = Binds(vec![Bind {
5068 key: Key {
5069 trigger: Trigger::Keysym(close_keysym),
5070 modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL,
5071 },
5072 action: Action::CloseWindow,
5073 repeat: true,
5074 cooldown: None,
5075 allow_when_locked: false,
5076 allow_inhibiting: true,
5077 hotkey_overlay_title: None,
5078 }]);
5079
5080 let comp_mod = ModKey::Super;
5081 let mut suppressed_keys = HashSet::new();
5082
5083 let screenshot_ui = ScreenshotUi::new(Clock::default(), Default::default());
5084 let disable_power_key_handling = false;
5085 let is_inhibiting_shortcuts = Cell::new(false);
5086
5087 // The key_code we pick is arbitrary, the only thing
5088 // that matters is that they are different between cases.
5089
5090 let close_key_code = Keycode::from(close_keysym.raw() + 8u32);
5091 let close_key_event = |suppr: &mut HashSet<Keycode>, mods: ModifiersState, pressed| {
5092 should_intercept_key(
5093 suppr,
5094 &bindings.0,
5095 comp_mod,
5096 close_key_code,
5097 close_keysym,
5098 Some(close_keysym),
5099 pressed,
5100 mods,
5101 &screenshot_ui,
5102 disable_power_key_handling,
5103 is_inhibiting_shortcuts.get(),
5104 )
5105 };
5106
5107 // Key event with the code which can't trigger any action.
5108 let none_key_event = |suppr: &mut HashSet<Keycode>, mods: ModifiersState, pressed| {
5109 should_intercept_key(
5110 suppr,
5111 &bindings.0,
5112 comp_mod,
5113 Keycode::from(Keysym::l.raw() + 8),
5114 Keysym::l,
5115 Some(Keysym::l),
5116 pressed,
5117 mods,
5118 &screenshot_ui,
5119 disable_power_key_handling,
5120 is_inhibiting_shortcuts.get(),
5121 )
5122 };
5123
5124 let mut mods = ModifiersState {
5125 logo: true,
5126 ctrl: true,
5127 ..Default::default()
5128 };
5129
5130 // Action press/release.
5131
5132 let filter = close_key_event(&mut suppressed_keys, mods, true);
5133 assert!(matches!(
5134 filter,
5135 FilterResult::Intercept(Some(Bind {
5136 action: Action::CloseWindow,
5137 ..
5138 }))
5139 ));
5140 assert!(suppressed_keys.contains(&close_key_code));
5141
5142 let filter = close_key_event(&mut suppressed_keys, mods, false);
5143 assert!(matches!(filter, FilterResult::Intercept(None)));
5144 assert!(suppressed_keys.is_empty());
5145
5146 // Remove mod to make it for a binding.
5147
5148 mods.shift = true;
5149 let filter = close_key_event(&mut suppressed_keys, mods, true);
5150 assert!(matches!(filter, FilterResult::Forward));
5151
5152 mods.shift = false;
5153 let filter = close_key_event(&mut suppressed_keys, mods, false);
5154 assert!(matches!(filter, FilterResult::Forward));
5155
5156 // Just none press/release.
5157
5158 let filter = none_key_event(&mut suppressed_keys, mods, true);
5159 assert!(matches!(filter, FilterResult::Forward));
5160
5161 let filter = none_key_event(&mut suppressed_keys, mods, false);
5162 assert!(matches!(filter, FilterResult::Forward));
5163
5164 // Press action, press arbitrary, release action, release arbitrary.
5165
5166 let filter = close_key_event(&mut suppressed_keys, mods, true);
5167 assert!(matches!(
5168 filter,
5169 FilterResult::Intercept(Some(Bind {
5170 action: Action::CloseWindow,
5171 ..
5172 }))
5173 ));
5174
5175 let filter = none_key_event(&mut suppressed_keys, mods, true);
5176 assert!(matches!(filter, FilterResult::Forward));
5177
5178 let filter = close_key_event(&mut suppressed_keys, mods, false);
5179 assert!(matches!(filter, FilterResult::Intercept(None)));
5180
5181 let filter = none_key_event(&mut suppressed_keys, mods, false);
5182 assert!(matches!(filter, FilterResult::Forward));
5183
5184 // Trigger and remove all mods.
5185
5186 let filter = close_key_event(&mut suppressed_keys, mods, true);
5187 assert!(matches!(
5188 filter,
5189 FilterResult::Intercept(Some(Bind {
5190 action: Action::CloseWindow,
5191 ..
5192 }))
5193 ));
5194
5195 mods = Default::default();
5196 let filter = close_key_event(&mut suppressed_keys, mods, false);
5197 assert!(matches!(filter, FilterResult::Intercept(None)));
5198
5199 // Ensure that no keys are being suppressed.
5200 assert!(suppressed_keys.is_empty());
5201
5202 // Now test shortcut inhibiting.
5203
5204 // With inhibited shortcuts, we don't intercept our shortcut.
5205 is_inhibiting_shortcuts.set(true);
5206
5207 mods = ModifiersState {
5208 logo: true,
5209 ctrl: true,
5210 ..Default::default()
5211 };
5212
5213 let filter = close_key_event(&mut suppressed_keys, mods, true);
5214 assert!(matches!(filter, FilterResult::Forward));
5215 assert!(suppressed_keys.is_empty());
5216
5217 let filter = close_key_event(&mut suppressed_keys, mods, false);
5218 assert!(matches!(filter, FilterResult::Forward));
5219 assert!(suppressed_keys.is_empty());
5220
5221 // Toggle it off after pressing the shortcut.
5222 let filter = close_key_event(&mut suppressed_keys, mods, true);
5223 assert!(matches!(filter, FilterResult::Forward));
5224 assert!(suppressed_keys.is_empty());
5225
5226 is_inhibiting_shortcuts.set(false);
5227
5228 let filter = close_key_event(&mut suppressed_keys, mods, false);
5229 assert!(matches!(filter, FilterResult::Forward));
5230 assert!(suppressed_keys.is_empty());
5231
5232 // Toggle it on after pressing the shortcut.
5233 let filter = close_key_event(&mut suppressed_keys, mods, true);
5234 assert!(matches!(
5235 filter,
5236 FilterResult::Intercept(Some(Bind {
5237 action: Action::CloseWindow,
5238 ..
5239 }))
5240 ));
5241 assert!(suppressed_keys.contains(&close_key_code));
5242
5243 is_inhibiting_shortcuts.set(true);
5244
5245 let filter = close_key_event(&mut suppressed_keys, mods, false);
5246 assert!(matches!(filter, FilterResult::Intercept(None)));
5247 assert!(suppressed_keys.is_empty());
5248 }
5249
5250 #[test]
5251 fn comp_mod_handling() {
5252 let bindings = Binds(vec![
5253 Bind {
5254 key: Key {
5255 trigger: Trigger::Keysym(Keysym::q),
5256 modifiers: Modifiers::COMPOSITOR,
5257 },
5258 action: Action::CloseWindow,
5259 repeat: true,
5260 cooldown: None,
5261 allow_when_locked: false,
5262 allow_inhibiting: true,
5263 hotkey_overlay_title: None,
5264 },
5265 Bind {
5266 key: Key {
5267 trigger: Trigger::Keysym(Keysym::h),
5268 modifiers: Modifiers::SUPER,
5269 },
5270 action: Action::FocusColumnLeft,
5271 repeat: true,
5272 cooldown: None,
5273 allow_when_locked: false,
5274 allow_inhibiting: true,
5275 hotkey_overlay_title: None,
5276 },
5277 Bind {
5278 key: Key {
5279 trigger: Trigger::Keysym(Keysym::j),
5280 modifiers: Modifiers::empty(),
5281 },
5282 action: Action::FocusWindowDown,
5283 repeat: true,
5284 cooldown: None,
5285 allow_when_locked: false,
5286 allow_inhibiting: true,
5287 hotkey_overlay_title: None,
5288 },
5289 Bind {
5290 key: Key {
5291 trigger: Trigger::Keysym(Keysym::k),
5292 modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER,
5293 },
5294 action: Action::FocusWindowUp,
5295 repeat: true,
5296 cooldown: None,
5297 allow_when_locked: false,
5298 allow_inhibiting: true,
5299 hotkey_overlay_title: None,
5300 },
5301 Bind {
5302 key: Key {
5303 trigger: Trigger::Keysym(Keysym::l),
5304 modifiers: Modifiers::SUPER | Modifiers::ALT,
5305 },
5306 action: Action::FocusColumnRight,
5307 repeat: true,
5308 cooldown: None,
5309 allow_when_locked: false,
5310 allow_inhibiting: true,
5311 hotkey_overlay_title: None,
5312 },
5313 ]);
5314
5315 assert_eq!(
5316 find_configured_bind(
5317 &bindings.0,
5318 ModKey::Super,
5319 Trigger::Keysym(Keysym::q),
5320 ModifiersState {
5321 logo: true,
5322 ..Default::default()
5323 }
5324 )
5325 .as_ref(),
5326 Some(&bindings.0[0])
5327 );
5328 assert_eq!(
5329 find_configured_bind(
5330 &bindings.0,
5331 ModKey::Super,
5332 Trigger::Keysym(Keysym::q),
5333 ModifiersState::default(),
5334 ),
5335 None,
5336 );
5337
5338 assert_eq!(
5339 find_configured_bind(
5340 &bindings.0,
5341 ModKey::Super,
5342 Trigger::Keysym(Keysym::h),
5343 ModifiersState {
5344 logo: true,
5345 ..Default::default()
5346 }
5347 )
5348 .as_ref(),
5349 Some(&bindings.0[1])
5350 );
5351 assert_eq!(
5352 find_configured_bind(
5353 &bindings.0,
5354 ModKey::Super,
5355 Trigger::Keysym(Keysym::h),
5356 ModifiersState::default(),
5357 ),
5358 None,
5359 );
5360
5361 assert_eq!(
5362 find_configured_bind(
5363 &bindings.0,
5364 ModKey::Super,
5365 Trigger::Keysym(Keysym::j),
5366 ModifiersState {
5367 logo: true,
5368 ..Default::default()
5369 }
5370 ),
5371 None,
5372 );
5373 assert_eq!(
5374 find_configured_bind(
5375 &bindings.0,
5376 ModKey::Super,
5377 Trigger::Keysym(Keysym::j),
5378 ModifiersState::default(),
5379 )
5380 .as_ref(),
5381 Some(&bindings.0[2])
5382 );
5383
5384 assert_eq!(
5385 find_configured_bind(
5386 &bindings.0,
5387 ModKey::Super,
5388 Trigger::Keysym(Keysym::k),
5389 ModifiersState {
5390 logo: true,
5391 ..Default::default()
5392 }
5393 )
5394 .as_ref(),
5395 Some(&bindings.0[3])
5396 );
5397 assert_eq!(
5398 find_configured_bind(
5399 &bindings.0,
5400 ModKey::Super,
5401 Trigger::Keysym(Keysym::k),
5402 ModifiersState::default(),
5403 ),
5404 None,
5405 );
5406
5407 assert_eq!(
5408 find_configured_bind(
5409 &bindings.0,
5410 ModKey::Super,
5411 Trigger::Keysym(Keysym::l),
5412 ModifiersState {
5413 logo: true,
5414 alt: true,
5415 ..Default::default()
5416 }
5417 )
5418 .as_ref(),
5419 Some(&bindings.0[4])
5420 );
5421 assert_eq!(
5422 find_configured_bind(
5423 &bindings.0,
5424 ModKey::Super,
5425 Trigger::Keysym(Keysym::l),
5426 ModifiersState {
5427 logo: true,
5428 ..Default::default()
5429 },
5430 ),
5431 None,
5432 );
5433 }
5434}