forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB66be4bfIvan Molodetskikh1 month ago2940 commits

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