forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

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