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