forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB90ef202Ivan Molodetskikh1 year ago2349 commits

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