forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB97dfd2bIvan Molodetskikh1 year ago2184 commits

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