forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB978c4e6Ivan Molodetskikh1 year ago2501 commits

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