forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB38df8d4Cole Helbling1 year ago2269 commits

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