forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB1dd82a8Ivan Molodetskikh1 year ago2468 commits

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