niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·c658424·Ivan Molodetskikh·2 years ago·1277 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::{Action, Bind, Binds, Key, Modifiers, Trigger}; |
| 10 | use niri_ipc::LayoutSwitchTarget; |
| 11 | use smithay::backend::input::{ |
| 12 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, |
| 13 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, |
| 14 | InputBackend, InputEvent, KeyState, KeyboardKeyEvent, MouseButton, PointerAxisEvent, |
| 15 | PointerButtonEvent, PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent, |
| 16 | TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState, TouchEvent, |
| 17 | }; |
| 18 | use smithay::backend::libinput::LibinputInputBackend; |
| 19 | use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState}; |
| 20 | use smithay::input::pointer::{ |
| 21 | AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, Focus, GestureHoldBeginEvent, |
| 22 | GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, |
| 23 | GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent, |
| 24 | GrabStartData as PointerGrabStartData, MotionEvent, RelativeMotionEvent, |
| 25 | }; |
| 26 | use smithay::input::touch::{DownEvent, MotionEvent as TouchMotionEvent, UpEvent}; |
| 27 | use smithay::utils::{Logical, Point, Rectangle, SERIAL_COUNTER}; |
| 28 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; |
| 29 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| 30 | |
| 31 | use self::resize_grab::ResizeGrab; |
| 32 | use self::spatial_movement_grab::SpatialMovementGrab; |
| 33 | use crate::niri::State; |
| 34 | use crate::ui::screenshot_ui::ScreenshotUi; |
| 35 | use crate::utils::spawning::spawn; |
| 36 | use crate::utils::{center, get_monotonic_time, ResizeEdge}; |
| 37 | |
| 38 | pub mod resize_grab; |
| 39 | pub mod scroll_tracker; |
| 40 | pub mod spatial_movement_grab; |
| 41 | pub mod swipe_tracker; |
| 42 | |
| 43 | pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400); |
| 44 | |
| 45 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 46 | pub enum CompositorMod { |
| 47 | Super, |
| 48 | Alt, |
| 49 | } |
| 50 | |
| 51 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 52 | pub struct TabletData { |
| 53 | pub aspect_ratio: f64, |
| 54 | } |
| 55 | |
| 56 | impl State { |
| 57 | pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>) |
| 58 | where |
| 59 | I::Device: 'static, // Needed for downcasting. |
| 60 | { |
| 61 | let _span = tracy_client::span!("process_input_event"); |
| 62 | |
| 63 | // A bit of a hack, but animation end runs some logic (i.e. workspace clean-up) and it |
| 64 | // doesn't always trigger due to damage, etc. So run it here right before it might prove |
| 65 | // important. Besides, animations affect the input, so it's best to have up-to-date values |
| 66 | // here. |
| 67 | self.niri.layout.advance_animations(get_monotonic_time()); |
| 68 | |
| 69 | if self.niri.monitors_active { |
| 70 | // Notify the idle-notifier of activity. |
| 71 | if should_notify_activity(&event) { |
| 72 | let _span = tracy_client::span!("IdleNotifierState::notify_activity"); |
| 73 | self.niri |
| 74 | .idle_notifier_state |
| 75 | .notify_activity(&self.niri.seat); |
| 76 | } |
| 77 | } else { |
| 78 | // Power on monitors if they were off. |
| 79 | if should_activate_monitors(&event) { |
| 80 | self.niri.activate_monitors(&mut self.backend); |
| 81 | |
| 82 | // Notify the idle-notifier of activity only if we're also powering on the |
| 83 | // monitors. |
| 84 | self.niri |
| 85 | .idle_notifier_state |
| 86 | .notify_activity(&self.niri.seat); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | let hide_hotkey_overlay = |
| 91 | self.niri.hotkey_overlay.is_open() && should_hide_hotkey_overlay(&event); |
| 92 | |
| 93 | let hide_exit_confirm_dialog = self |
| 94 | .niri |
| 95 | .exit_confirm_dialog |
| 96 | .as_ref() |
| 97 | .map_or(false, |d| d.is_open()) |
| 98 | && should_hide_exit_confirm_dialog(&event); |
| 99 | |
| 100 | use InputEvent::*; |
| 101 | match event { |
| 102 | DeviceAdded { device } => self.on_device_added(device), |
| 103 | DeviceRemoved { device } => self.on_device_removed(device), |
| 104 | Keyboard { event } => self.on_keyboard::<I>(event), |
| 105 | PointerMotion { event } => self.on_pointer_motion::<I>(event), |
| 106 | PointerMotionAbsolute { event } => self.on_pointer_motion_absolute::<I>(event), |
| 107 | PointerButton { event } => self.on_pointer_button::<I>(event), |
| 108 | PointerAxis { event } => self.on_pointer_axis::<I>(event), |
| 109 | TabletToolAxis { event } => self.on_tablet_tool_axis::<I>(event), |
| 110 | TabletToolTip { event } => self.on_tablet_tool_tip::<I>(event), |
| 111 | TabletToolProximity { event } => self.on_tablet_tool_proximity::<I>(event), |
| 112 | TabletToolButton { event } => self.on_tablet_tool_button::<I>(event), |
| 113 | GestureSwipeBegin { event } => self.on_gesture_swipe_begin::<I>(event), |
| 114 | GestureSwipeUpdate { event } => self.on_gesture_swipe_update::<I>(event), |
| 115 | GestureSwipeEnd { event } => self.on_gesture_swipe_end::<I>(event), |
| 116 | GesturePinchBegin { event } => self.on_gesture_pinch_begin::<I>(event), |
| 117 | GesturePinchUpdate { event } => self.on_gesture_pinch_update::<I>(event), |
| 118 | GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event), |
| 119 | GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event), |
| 120 | GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event), |
| 121 | TouchDown { event } => self.on_touch_down::<I>(event), |
| 122 | TouchMotion { event } => self.on_touch_motion::<I>(event), |
| 123 | TouchUp { event } => self.on_touch_up::<I>(event), |
| 124 | TouchCancel { event } => self.on_touch_cancel::<I>(event), |
| 125 | TouchFrame { event } => self.on_touch_frame::<I>(event), |
| 126 | SwitchToggle { .. } => (), |
| 127 | Special(_) => (), |
| 128 | } |
| 129 | |
| 130 | // Do this last so that screenshot still gets it. |
| 131 | // FIXME: do this in a less cursed fashion somehow. |
| 132 | if hide_hotkey_overlay && self.niri.hotkey_overlay.hide() { |
| 133 | self.niri.queue_redraw_all(); |
| 134 | } |
| 135 | |
| 136 | if let Some(dialog) = &mut self.niri.exit_confirm_dialog { |
| 137 | if hide_exit_confirm_dialog && dialog.hide() { |
| 138 | self.niri.queue_redraw_all(); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| 144 | let _span = tracy_client::span!("process_libinput_event"); |
| 145 | |
| 146 | match event { |
| 147 | InputEvent::DeviceAdded { device } => { |
| 148 | self.niri.devices.insert(device.clone()); |
| 149 | |
| 150 | if device.has_capability(input::DeviceCapability::TabletTool) { |
| 151 | match device.size() { |
| 152 | Some((w, h)) => { |
| 153 | let aspect_ratio = w / h; |
| 154 | let data = TabletData { aspect_ratio }; |
| 155 | self.niri.tablets.insert(device.clone(), data); |
| 156 | } |
| 157 | None => { |
| 158 | warn!("tablet tool device has no size"); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if device.has_capability(input::DeviceCapability::Keyboard) { |
| 164 | if let Some(led_state) = self |
| 165 | .niri |
| 166 | .seat |
| 167 | .get_keyboard() |
| 168 | .map(|keyboard| keyboard.led_state()) |
| 169 | { |
| 170 | device.led_update(led_state.into()); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if device.has_capability(input::DeviceCapability::Touch) { |
| 175 | self.niri.touch.insert(device.clone()); |
| 176 | } |
| 177 | |
| 178 | apply_libinput_settings(&self.niri.config.borrow().input, device); |
| 179 | } |
| 180 | InputEvent::DeviceRemoved { device } => { |
| 181 | self.niri.touch.remove(device); |
| 182 | self.niri.tablets.remove(device); |
| 183 | self.niri.devices.remove(device); |
| 184 | } |
| 185 | _ => (), |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn on_device_added(&mut self, device: impl Device) { |
| 190 | if device.has_capability(DeviceCapability::TabletTool) { |
| 191 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 192 | |
| 193 | let desc = TabletDescriptor::from(&device); |
| 194 | tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc); |
| 195 | } |
| 196 | if device.has_capability(DeviceCapability::Touch) && self.niri.seat.get_touch().is_none() { |
| 197 | self.niri.seat.add_touch(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | fn on_device_removed(&mut self, device: impl Device) { |
| 202 | if device.has_capability(DeviceCapability::TabletTool) { |
| 203 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 204 | |
| 205 | let desc = TabletDescriptor::from(&device); |
| 206 | tablet_seat.remove_tablet(&desc); |
| 207 | |
| 208 | // If there are no tablets in seat we can remove all tools |
| 209 | if tablet_seat.count_tablets() == 0 { |
| 210 | tablet_seat.clear_tools(); |
| 211 | } |
| 212 | } |
| 213 | if device.has_capability(DeviceCapability::Touch) && self.niri.touch.is_empty() { |
| 214 | self.niri.seat.remove_touch(); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /// Computes the rectangle that covers all outputs in global space. |
| 219 | fn global_bounding_rectangle(&self) -> Option<Rectangle<i32, Logical>> { |
| 220 | self.niri.global_space.outputs().fold( |
| 221 | None, |
| 222 | |acc: Option<Rectangle<i32, Logical>>, output| { |
| 223 | self.niri |
| 224 | .global_space |
| 225 | .output_geometry(output) |
| 226 | .map(|geo| acc.map(|acc| acc.merge(geo)).unwrap_or(geo)) |
| 227 | }, |
| 228 | ) |
| 229 | } |
| 230 | |
| 231 | /// Computes the cursor position for the tablet event. |
| 232 | /// |
| 233 | /// This function handles the tablet output mapping, as well as coordinate clamping and aspect |
| 234 | /// ratio correction. |
| 235 | fn compute_tablet_position<I: InputBackend>( |
| 236 | &self, |
| 237 | event: &(impl Event<I> + TabletToolEvent<I>), |
| 238 | ) -> Option<Point<f64, Logical>> |
| 239 | where |
| 240 | I::Device: 'static, |
| 241 | { |
| 242 | let (target_geo, keep_ratio, px) = if let Some(output) = self.niri.output_for_tablet() { |
| 243 | ( |
| 244 | self.niri.global_space.output_geometry(output).unwrap(), |
| 245 | true, |
| 246 | 1. / output.current_scale().fractional_scale(), |
| 247 | ) |
| 248 | } else { |
| 249 | let geo = self.global_bounding_rectangle()?; |
| 250 | |
| 251 | // FIXME: this 1 px size should ideally somehow be computed for the rightmost output |
| 252 | // corresponding to the position on the right when clamping. |
| 253 | let output = self.niri.global_space.outputs().next().unwrap(); |
| 254 | let scale = output.current_scale().fractional_scale(); |
| 255 | |
| 256 | // Do not keep ratio for the unified mode as this is what OpenTabletDriver expects. |
| 257 | (geo, false, 1. / scale) |
| 258 | }; |
| 259 | |
| 260 | let mut pos = event.position_transformed(target_geo.size); |
| 261 | |
| 262 | if keep_ratio { |
| 263 | pos.x /= target_geo.size.w as f64; |
| 264 | pos.y /= target_geo.size.h as f64; |
| 265 | |
| 266 | let device = event.device(); |
| 267 | if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() { |
| 268 | if let Some(data) = self.niri.tablets.get(device) { |
| 269 | // This code does the same thing as mutter with "keep aspect ratio" enabled. |
| 270 | let output_aspect_ratio = target_geo.size.w as f64 / target_geo.size.h as f64; |
| 271 | let ratio = data.aspect_ratio / output_aspect_ratio; |
| 272 | |
| 273 | if ratio > 1. { |
| 274 | pos.x *= ratio; |
| 275 | } else { |
| 276 | pos.y /= ratio; |
| 277 | } |
| 278 | } |
| 279 | }; |
| 280 | |
| 281 | pos.x *= target_geo.size.w as f64; |
| 282 | pos.y *= target_geo.size.h as f64; |
| 283 | } |
| 284 | |
| 285 | pos.x = pos.x.clamp(0.0, target_geo.size.w as f64 - px); |
| 286 | pos.y = pos.y.clamp(0.0, target_geo.size.h as f64 - px); |
| 287 | Some(pos + target_geo.loc.to_f64()) |
| 288 | } |
| 289 | |
| 290 | fn on_keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) { |
| 291 | let comp_mod = self.backend.mod_key(); |
| 292 | |
| 293 | let serial = SERIAL_COUNTER.next_serial(); |
| 294 | let time = Event::time_msec(&event); |
| 295 | let pressed = event.state() == KeyState::Pressed; |
| 296 | |
| 297 | // Stop bind key repeat on any release. This won't work 100% correctly in cases like: |
| 298 | // 1. Press Mod |
| 299 | // 2. Press Left (repeat starts) |
| 300 | // 3. Press PgDown (new repeat starts) |
| 301 | // 4. Release Left (PgDown repeat stops) |
| 302 | // But it's good enough for now. |
| 303 | // FIXME: handle this properly. |
| 304 | if !pressed { |
| 305 | if let Some(token) = self.niri.bind_repeat_timer.take() { |
| 306 | self.niri.event_loop.remove(token); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | let Some(Some(bind)) = self.niri.seat.get_keyboard().unwrap().input( |
| 311 | self, |
| 312 | event.key_code(), |
| 313 | event.state(), |
| 314 | serial, |
| 315 | time, |
| 316 | |this, mods, keysym| { |
| 317 | let bindings = &this.niri.config.borrow().binds; |
| 318 | let key_code = event.key_code(); |
| 319 | let modified = keysym.modified_sym(); |
| 320 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); |
| 321 | |
| 322 | if let Some(dialog) = &this.niri.exit_confirm_dialog { |
| 323 | if dialog.is_open() && pressed && raw == Some(Keysym::Return) { |
| 324 | info!("quitting after confirming exit dialog"); |
| 325 | this.niri.stop_signal.stop(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | should_intercept_key( |
| 330 | &mut this.niri.suppressed_keys, |
| 331 | bindings, |
| 332 | comp_mod, |
| 333 | key_code, |
| 334 | modified, |
| 335 | raw, |
| 336 | pressed, |
| 337 | *mods, |
| 338 | &this.niri.screenshot_ui, |
| 339 | this.niri.config.borrow().input.disable_power_key_handling, |
| 340 | ) |
| 341 | }, |
| 342 | ) else { |
| 343 | return; |
| 344 | }; |
| 345 | |
| 346 | if !pressed { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | self.handle_bind(bind.clone()); |
| 351 | |
| 352 | // Start the key repeat timer if necessary. |
| 353 | if !bind.repeat { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | // Stop the previous key repeat if any. |
| 358 | if let Some(token) = self.niri.bind_repeat_timer.take() { |
| 359 | self.niri.event_loop.remove(token); |
| 360 | } |
| 361 | |
| 362 | let config = self.niri.config.borrow(); |
| 363 | let config = &config.input.keyboard; |
| 364 | |
| 365 | let repeat_rate = config.repeat_rate; |
| 366 | if repeat_rate == 0 { |
| 367 | return; |
| 368 | } |
| 369 | let repeat_duration = Duration::from_secs_f64(1. / f64::from(repeat_rate)); |
| 370 | |
| 371 | let repeat_timer = |
| 372 | Timer::from_duration(Duration::from_millis(u64::from(config.repeat_delay))); |
| 373 | |
| 374 | let token = self |
| 375 | .niri |
| 376 | .event_loop |
| 377 | .insert_source(repeat_timer, move |_, _, state| { |
| 378 | state.handle_bind(bind.clone()); |
| 379 | TimeoutAction::ToDuration(repeat_duration) |
| 380 | }) |
| 381 | .unwrap(); |
| 382 | |
| 383 | self.niri.bind_repeat_timer = Some(token); |
| 384 | } |
| 385 | |
| 386 | pub fn handle_bind(&mut self, bind: Bind) { |
| 387 | let Some(cooldown) = bind.cooldown else { |
| 388 | self.do_action(bind.action, bind.allow_when_locked); |
| 389 | return; |
| 390 | }; |
| 391 | |
| 392 | // Check this first so that it doesn't trigger the cooldown. |
| 393 | if self.niri.is_locked() && !(bind.allow_when_locked || allowed_when_locked(&bind.action)) { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | match self.niri.bind_cooldown_timers.entry(bind.key) { |
| 398 | // The bind is on cooldown. |
| 399 | Entry::Occupied(_) => (), |
| 400 | Entry::Vacant(entry) => { |
| 401 | let timer = Timer::from_duration(cooldown); |
| 402 | let token = self |
| 403 | .niri |
| 404 | .event_loop |
| 405 | .insert_source(timer, move |_, _, state| { |
| 406 | if state.niri.bind_cooldown_timers.remove(&bind.key).is_none() { |
| 407 | error!("bind cooldown timer entry disappeared"); |
| 408 | } |
| 409 | TimeoutAction::Drop |
| 410 | }) |
| 411 | .unwrap(); |
| 412 | entry.insert(token); |
| 413 | |
| 414 | self.do_action(bind.action, bind.allow_when_locked); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | pub fn do_action(&mut self, action: Action, allow_when_locked: bool) { |
| 420 | if self.niri.is_locked() && !(allow_when_locked || allowed_when_locked(&action)) { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | if let Some(touch) = self.niri.seat.get_touch() { |
| 425 | touch.cancel(self); |
| 426 | } |
| 427 | |
| 428 | match action { |
| 429 | Action::Quit(skip_confirmation) => { |
| 430 | if !skip_confirmation { |
| 431 | if let Some(dialog) = &mut self.niri.exit_confirm_dialog { |
| 432 | if dialog.show() { |
| 433 | self.niri.queue_redraw_all(); |
| 434 | } |
| 435 | return; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | info!("quitting as requested"); |
| 440 | self.niri.stop_signal.stop() |
| 441 | } |
| 442 | Action::ChangeVt(vt) => { |
| 443 | self.backend.change_vt(vt); |
| 444 | // Changing VT may not deliver the key releases, so clear the state. |
| 445 | self.niri.suppressed_keys.clear(); |
| 446 | } |
| 447 | Action::Suspend => { |
| 448 | self.backend.suspend(); |
| 449 | // Suspend may not deliver the key releases, so clear the state. |
| 450 | self.niri.suppressed_keys.clear(); |
| 451 | } |
| 452 | Action::PowerOffMonitors => { |
| 453 | self.niri.deactivate_monitors(&mut self.backend); |
| 454 | } |
| 455 | Action::ToggleDebugTint => { |
| 456 | self.backend.toggle_debug_tint(); |
| 457 | self.niri.queue_redraw_all(); |
| 458 | } |
| 459 | Action::DebugToggleOpaqueRegions => { |
| 460 | self.niri.debug_draw_opaque_regions = !self.niri.debug_draw_opaque_regions; |
| 461 | self.niri.queue_redraw_all(); |
| 462 | } |
| 463 | Action::DebugToggleDamage => { |
| 464 | self.niri.debug_toggle_damage(); |
| 465 | } |
| 466 | Action::Spawn(command) => { |
| 467 | spawn(command); |
| 468 | } |
| 469 | Action::DoScreenTransition(delay_ms) => { |
| 470 | self.backend.with_primary_renderer(|renderer| { |
| 471 | self.niri.do_screen_transition(renderer, delay_ms); |
| 472 | }); |
| 473 | } |
| 474 | Action::ScreenshotScreen => { |
| 475 | let active = self.niri.layout.active_output().cloned(); |
| 476 | if let Some(active) = active { |
| 477 | self.backend.with_primary_renderer(|renderer| { |
| 478 | if let Err(err) = self.niri.screenshot(renderer, &active) { |
| 479 | warn!("error taking screenshot: {err:?}"); |
| 480 | } |
| 481 | }); |
| 482 | } |
| 483 | } |
| 484 | Action::ConfirmScreenshot => { |
| 485 | self.backend.with_primary_renderer(|renderer| { |
| 486 | match self.niri.screenshot_ui.capture(renderer) { |
| 487 | Ok((size, pixels)) => { |
| 488 | if let Err(err) = self.niri.save_screenshot(size, pixels) { |
| 489 | warn!("error saving screenshot: {err:?}"); |
| 490 | } |
| 491 | } |
| 492 | Err(err) => { |
| 493 | warn!("error capturing screenshot: {err:?}"); |
| 494 | } |
| 495 | } |
| 496 | }); |
| 497 | |
| 498 | self.niri.screenshot_ui.close(); |
| 499 | self.niri |
| 500 | .cursor_manager |
| 501 | .set_cursor_image(CursorImageStatus::default_named()); |
| 502 | self.niri.queue_redraw_all(); |
| 503 | } |
| 504 | Action::CancelScreenshot => { |
| 505 | self.niri.screenshot_ui.close(); |
| 506 | self.niri |
| 507 | .cursor_manager |
| 508 | .set_cursor_image(CursorImageStatus::default_named()); |
| 509 | self.niri.queue_redraw_all(); |
| 510 | } |
| 511 | Action::ScreenshotTogglePointer => { |
| 512 | self.niri.screenshot_ui.toggle_pointer(); |
| 513 | self.niri.queue_redraw_all(); |
| 514 | } |
| 515 | Action::Screenshot => { |
| 516 | self.open_screenshot_ui(); |
| 517 | } |
| 518 | Action::ScreenshotWindow => { |
| 519 | let active = self.niri.layout.active_window(); |
| 520 | if let Some((mapped, output)) = active { |
| 521 | self.backend.with_primary_renderer(|renderer| { |
| 522 | if let Err(err) = self.niri.screenshot_window(renderer, output, mapped) { |
| 523 | warn!("error taking screenshot: {err:?}"); |
| 524 | } |
| 525 | }); |
| 526 | } |
| 527 | } |
| 528 | Action::CloseWindow => { |
| 529 | if let Some(mapped) = self.niri.layout.focus() { |
| 530 | mapped.toplevel().send_close(); |
| 531 | } |
| 532 | } |
| 533 | Action::FullscreenWindow => { |
| 534 | let focus = self.niri.layout.focus().map(|m| m.window.clone()); |
| 535 | if let Some(window) = focus { |
| 536 | self.niri.layout.toggle_fullscreen(&window); |
| 537 | // FIXME: granular |
| 538 | self.niri.queue_redraw_all(); |
| 539 | } |
| 540 | } |
| 541 | Action::SwitchLayout(action) => { |
| 542 | self.niri.seat.get_keyboard().unwrap().with_xkb_state( |
| 543 | self, |
| 544 | |mut state| match action { |
| 545 | LayoutSwitchTarget::Next => state.cycle_next_layout(), |
| 546 | LayoutSwitchTarget::Prev => state.cycle_prev_layout(), |
| 547 | }, |
| 548 | ); |
| 549 | } |
| 550 | Action::MoveColumnLeft => { |
| 551 | self.niri.layout.move_left(); |
| 552 | self.maybe_warp_cursor_to_focus(); |
| 553 | // FIXME: granular |
| 554 | self.niri.queue_redraw_all(); |
| 555 | } |
| 556 | Action::MoveColumnRight => { |
| 557 | self.niri.layout.move_right(); |
| 558 | self.maybe_warp_cursor_to_focus(); |
| 559 | // FIXME: granular |
| 560 | self.niri.queue_redraw_all(); |
| 561 | } |
| 562 | Action::MoveColumnToFirst => { |
| 563 | self.niri.layout.move_column_to_first(); |
| 564 | self.maybe_warp_cursor_to_focus(); |
| 565 | // FIXME: granular |
| 566 | self.niri.queue_redraw_all(); |
| 567 | } |
| 568 | Action::MoveColumnToLast => { |
| 569 | self.niri.layout.move_column_to_last(); |
| 570 | self.maybe_warp_cursor_to_focus(); |
| 571 | // FIXME: granular |
| 572 | self.niri.queue_redraw_all(); |
| 573 | } |
| 574 | Action::MoveColumnLeftOrToMonitorLeft => { |
| 575 | if let Some(output) = self.niri.output_left() { |
| 576 | if self.niri.layout.move_column_left_or_to_output(&output) |
| 577 | && !self.maybe_warp_cursor_to_focus_centered() |
| 578 | { |
| 579 | self.move_cursor_to_output(&output); |
| 580 | } else { |
| 581 | self.maybe_warp_cursor_to_focus(); |
| 582 | } |
| 583 | } else { |
| 584 | self.niri.layout.move_left(); |
| 585 | self.maybe_warp_cursor_to_focus(); |
| 586 | } |
| 587 | |
| 588 | // FIXME: granular |
| 589 | self.niri.queue_redraw_all(); |
| 590 | } |
| 591 | Action::MoveColumnRightOrToMonitorRight => { |
| 592 | if let Some(output) = self.niri.output_right() { |
| 593 | if self.niri.layout.move_column_right_or_to_output(&output) |
| 594 | && !self.maybe_warp_cursor_to_focus_centered() |
| 595 | { |
| 596 | self.move_cursor_to_output(&output); |
| 597 | } else { |
| 598 | self.maybe_warp_cursor_to_focus(); |
| 599 | } |
| 600 | } else { |
| 601 | self.niri.layout.move_right(); |
| 602 | self.maybe_warp_cursor_to_focus(); |
| 603 | } |
| 604 | |
| 605 | // FIXME: granular |
| 606 | self.niri.queue_redraw_all(); |
| 607 | } |
| 608 | Action::MoveWindowDown => { |
| 609 | self.niri.layout.move_down(); |
| 610 | self.maybe_warp_cursor_to_focus(); |
| 611 | // FIXME: granular |
| 612 | self.niri.queue_redraw_all(); |
| 613 | } |
| 614 | Action::MoveWindowUp => { |
| 615 | self.niri.layout.move_up(); |
| 616 | self.maybe_warp_cursor_to_focus(); |
| 617 | // FIXME: granular |
| 618 | self.niri.queue_redraw_all(); |
| 619 | } |
| 620 | Action::MoveWindowDownOrToWorkspaceDown => { |
| 621 | self.niri.layout.move_down_or_to_workspace_down(); |
| 622 | self.maybe_warp_cursor_to_focus(); |
| 623 | // FIXME: granular |
| 624 | self.niri.queue_redraw_all(); |
| 625 | } |
| 626 | Action::MoveWindowUpOrToWorkspaceUp => { |
| 627 | self.niri.layout.move_up_or_to_workspace_up(); |
| 628 | self.maybe_warp_cursor_to_focus(); |
| 629 | // FIXME: granular |
| 630 | self.niri.queue_redraw_all(); |
| 631 | } |
| 632 | Action::ConsumeOrExpelWindowLeft => { |
| 633 | self.niri.layout.consume_or_expel_window_left(); |
| 634 | self.maybe_warp_cursor_to_focus(); |
| 635 | // FIXME: granular |
| 636 | self.niri.queue_redraw_all(); |
| 637 | } |
| 638 | Action::ConsumeOrExpelWindowRight => { |
| 639 | self.niri.layout.consume_or_expel_window_right(); |
| 640 | self.maybe_warp_cursor_to_focus(); |
| 641 | // FIXME: granular |
| 642 | self.niri.queue_redraw_all(); |
| 643 | } |
| 644 | Action::FocusColumnLeft => { |
| 645 | self.niri.layout.focus_left(); |
| 646 | self.maybe_warp_cursor_to_focus(); |
| 647 | // FIXME: granular |
| 648 | self.niri.queue_redraw_all(); |
| 649 | } |
| 650 | Action::FocusColumnRight => { |
| 651 | self.niri.layout.focus_right(); |
| 652 | self.maybe_warp_cursor_to_focus(); |
| 653 | // FIXME: granular |
| 654 | self.niri.queue_redraw_all(); |
| 655 | } |
| 656 | Action::FocusColumnFirst => { |
| 657 | self.niri.layout.focus_column_first(); |
| 658 | self.maybe_warp_cursor_to_focus(); |
| 659 | // FIXME: granular |
| 660 | self.niri.queue_redraw_all(); |
| 661 | } |
| 662 | Action::FocusColumnLast => { |
| 663 | self.niri.layout.focus_column_last(); |
| 664 | self.maybe_warp_cursor_to_focus(); |
| 665 | // FIXME: granular |
| 666 | self.niri.queue_redraw_all(); |
| 667 | } |
| 668 | Action::FocusColumnRightOrFirst => { |
| 669 | self.niri.layout.focus_column_right_or_first(); |
| 670 | self.maybe_warp_cursor_to_focus(); |
| 671 | // FIXME: granular |
| 672 | self.niri.queue_redraw_all(); |
| 673 | } |
| 674 | Action::FocusColumnLeftOrLast => { |
| 675 | self.niri.layout.focus_column_left_or_last(); |
| 676 | self.maybe_warp_cursor_to_focus(); |
| 677 | // FIXME: granular |
| 678 | self.niri.queue_redraw_all(); |
| 679 | } |
| 680 | Action::FocusWindowOrMonitorUp => { |
| 681 | if let Some(output) = self.niri.output_up() { |
| 682 | if self.niri.layout.focus_window_up_or_output(&output) |
| 683 | && !self.maybe_warp_cursor_to_focus_centered() |
| 684 | { |
| 685 | self.move_cursor_to_output(&output); |
| 686 | } else { |
| 687 | self.maybe_warp_cursor_to_focus(); |
| 688 | } |
| 689 | } else { |
| 690 | self.niri.layout.focus_up(); |
| 691 | self.maybe_warp_cursor_to_focus(); |
| 692 | } |
| 693 | |
| 694 | // FIXME: granular |
| 695 | self.niri.queue_redraw_all(); |
| 696 | } |
| 697 | Action::FocusWindowOrMonitorDown => { |
| 698 | if let Some(output) = self.niri.output_down() { |
| 699 | if self.niri.layout.focus_window_down_or_output(&output) |
| 700 | && !self.maybe_warp_cursor_to_focus_centered() |
| 701 | { |
| 702 | self.move_cursor_to_output(&output); |
| 703 | } else { |
| 704 | self.maybe_warp_cursor_to_focus(); |
| 705 | } |
| 706 | } else { |
| 707 | self.niri.layout.focus_down(); |
| 708 | self.maybe_warp_cursor_to_focus(); |
| 709 | } |
| 710 | |
| 711 | // FIXME: granular |
| 712 | self.niri.queue_redraw_all(); |
| 713 | } |
| 714 | Action::FocusColumnOrMonitorLeft => { |
| 715 | if let Some(output) = self.niri.output_left() { |
| 716 | if self.niri.layout.focus_column_left_or_output(&output) |
| 717 | && !self.maybe_warp_cursor_to_focus_centered() |
| 718 | { |
| 719 | self.move_cursor_to_output(&output); |
| 720 | } else { |
| 721 | self.maybe_warp_cursor_to_focus(); |
| 722 | } |
| 723 | } else { |
| 724 | self.niri.layout.focus_left(); |
| 725 | self.maybe_warp_cursor_to_focus(); |
| 726 | } |
| 727 | |
| 728 | // FIXME: granular |
| 729 | self.niri.queue_redraw_all(); |
| 730 | } |
| 731 | Action::FocusColumnOrMonitorRight => { |
| 732 | if let Some(output) = self.niri.output_right() { |
| 733 | if self.niri.layout.focus_column_right_or_output(&output) |
| 734 | && !self.maybe_warp_cursor_to_focus_centered() |
| 735 | { |
| 736 | self.move_cursor_to_output(&output); |
| 737 | } else { |
| 738 | self.maybe_warp_cursor_to_focus(); |
| 739 | } |
| 740 | } else { |
| 741 | self.niri.layout.focus_right(); |
| 742 | self.maybe_warp_cursor_to_focus(); |
| 743 | } |
| 744 | |
| 745 | // FIXME: granular |
| 746 | self.niri.queue_redraw_all(); |
| 747 | } |
| 748 | Action::FocusWindowDown => { |
| 749 | self.niri.layout.focus_down(); |
| 750 | self.maybe_warp_cursor_to_focus(); |
| 751 | // FIXME: granular |
| 752 | self.niri.queue_redraw_all(); |
| 753 | } |
| 754 | Action::FocusWindowUp => { |
| 755 | self.niri.layout.focus_up(); |
| 756 | self.maybe_warp_cursor_to_focus(); |
| 757 | // FIXME: granular |
| 758 | self.niri.queue_redraw_all(); |
| 759 | } |
| 760 | Action::FocusWindowDownOrColumnLeft => { |
| 761 | self.niri.layout.focus_down_or_left(); |
| 762 | self.maybe_warp_cursor_to_focus(); |
| 763 | // FIXME: granular |
| 764 | self.niri.queue_redraw_all(); |
| 765 | } |
| 766 | Action::FocusWindowDownOrColumnRight => { |
| 767 | self.niri.layout.focus_down_or_right(); |
| 768 | self.maybe_warp_cursor_to_focus(); |
| 769 | // FIXME: granular |
| 770 | self.niri.queue_redraw_all(); |
| 771 | } |
| 772 | Action::FocusWindowUpOrColumnLeft => { |
| 773 | self.niri.layout.focus_up_or_left(); |
| 774 | self.maybe_warp_cursor_to_focus(); |
| 775 | // FIXME: granular |
| 776 | self.niri.queue_redraw_all(); |
| 777 | } |
| 778 | Action::FocusWindowUpOrColumnRight => { |
| 779 | self.niri.layout.focus_up_or_right(); |
| 780 | self.maybe_warp_cursor_to_focus(); |
| 781 | // FIXME: granular |
| 782 | self.niri.queue_redraw_all(); |
| 783 | } |
| 784 | Action::FocusWindowOrWorkspaceDown => { |
| 785 | self.niri.layout.focus_window_or_workspace_down(); |
| 786 | self.maybe_warp_cursor_to_focus(); |
| 787 | // FIXME: granular |
| 788 | self.niri.queue_redraw_all(); |
| 789 | } |
| 790 | Action::FocusWindowOrWorkspaceUp => { |
| 791 | self.niri.layout.focus_window_or_workspace_up(); |
| 792 | self.maybe_warp_cursor_to_focus(); |
| 793 | // FIXME: granular |
| 794 | self.niri.queue_redraw_all(); |
| 795 | } |
| 796 | Action::MoveWindowToWorkspaceDown => { |
| 797 | self.niri.layout.move_to_workspace_down(); |
| 798 | self.maybe_warp_cursor_to_focus(); |
| 799 | // FIXME: granular |
| 800 | self.niri.queue_redraw_all(); |
| 801 | } |
| 802 | Action::MoveWindowToWorkspaceUp => { |
| 803 | self.niri.layout.move_to_workspace_up(); |
| 804 | self.maybe_warp_cursor_to_focus(); |
| 805 | // FIXME: granular |
| 806 | self.niri.queue_redraw_all(); |
| 807 | } |
| 808 | Action::MoveWindowToWorkspace(reference) => { |
| 809 | if let Some((output, index)) = self.niri.find_output_and_workspace_index(reference) |
| 810 | { |
| 811 | if let Some(output) = output { |
| 812 | self.niri.layout.move_to_workspace_on_output(&output, index); |
| 813 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 814 | self.move_cursor_to_output(&output); |
| 815 | } |
| 816 | } else { |
| 817 | self.niri.layout.move_to_workspace(index); |
| 818 | self.maybe_warp_cursor_to_focus(); |
| 819 | } |
| 820 | |
| 821 | // FIXME: granular |
| 822 | self.niri.queue_redraw_all(); |
| 823 | } |
| 824 | } |
| 825 | Action::MoveColumnToWorkspaceDown => { |
| 826 | self.niri.layout.move_column_to_workspace_down(); |
| 827 | self.maybe_warp_cursor_to_focus(); |
| 828 | // FIXME: granular |
| 829 | self.niri.queue_redraw_all(); |
| 830 | } |
| 831 | Action::MoveColumnToWorkspaceUp => { |
| 832 | self.niri.layout.move_column_to_workspace_up(); |
| 833 | self.maybe_warp_cursor_to_focus(); |
| 834 | // FIXME: granular |
| 835 | self.niri.queue_redraw_all(); |
| 836 | } |
| 837 | Action::MoveColumnToWorkspace(reference) => { |
| 838 | if let Some((output, index)) = self.niri.find_output_and_workspace_index(reference) |
| 839 | { |
| 840 | if let Some(output) = output { |
| 841 | self.niri |
| 842 | .layout |
| 843 | .move_column_to_workspace_on_output(&output, index); |
| 844 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 845 | self.move_cursor_to_output(&output); |
| 846 | } |
| 847 | } else { |
| 848 | self.niri.layout.move_column_to_workspace(index); |
| 849 | self.maybe_warp_cursor_to_focus(); |
| 850 | } |
| 851 | |
| 852 | // FIXME: granular |
| 853 | self.niri.queue_redraw_all(); |
| 854 | } |
| 855 | } |
| 856 | Action::FocusWorkspaceDown => { |
| 857 | self.niri.layout.switch_workspace_down(); |
| 858 | self.maybe_warp_cursor_to_focus(); |
| 859 | // FIXME: granular |
| 860 | self.niri.queue_redraw_all(); |
| 861 | } |
| 862 | Action::FocusWorkspaceUp => { |
| 863 | self.niri.layout.switch_workspace_up(); |
| 864 | self.maybe_warp_cursor_to_focus(); |
| 865 | // FIXME: granular |
| 866 | self.niri.queue_redraw_all(); |
| 867 | } |
| 868 | Action::FocusWorkspace(reference) => { |
| 869 | if let Some((output, index)) = self.niri.find_output_and_workspace_index(reference) |
| 870 | { |
| 871 | if let Some(output) = output { |
| 872 | self.niri.layout.focus_output(&output); |
| 873 | self.niri.layout.switch_workspace(index); |
| 874 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 875 | self.move_cursor_to_output(&output); |
| 876 | } |
| 877 | } else { |
| 878 | let config = &self.niri.config; |
| 879 | if config.borrow().input.workspace_auto_back_and_forth { |
| 880 | self.niri.layout.switch_workspace_auto_back_and_forth(index); |
| 881 | } else { |
| 882 | self.niri.layout.switch_workspace(index); |
| 883 | } |
| 884 | self.maybe_warp_cursor_to_focus(); |
| 885 | } |
| 886 | |
| 887 | // FIXME: granular |
| 888 | self.niri.queue_redraw_all(); |
| 889 | } |
| 890 | } |
| 891 | Action::FocusWorkspacePrevious => { |
| 892 | self.niri.layout.switch_workspace_previous(); |
| 893 | // FIXME: granular |
| 894 | self.niri.queue_redraw_all(); |
| 895 | } |
| 896 | Action::MoveWorkspaceDown => { |
| 897 | self.niri.layout.move_workspace_down(); |
| 898 | // FIXME: granular |
| 899 | self.niri.queue_redraw_all(); |
| 900 | } |
| 901 | Action::MoveWorkspaceUp => { |
| 902 | self.niri.layout.move_workspace_up(); |
| 903 | // FIXME: granular |
| 904 | self.niri.queue_redraw_all(); |
| 905 | } |
| 906 | Action::ConsumeWindowIntoColumn => { |
| 907 | self.niri.layout.consume_into_column(); |
| 908 | // This does not cause immediate focus or window size change, so warping mouse to |
| 909 | // focus won't do anything here. |
| 910 | // FIXME: granular |
| 911 | self.niri.queue_redraw_all(); |
| 912 | } |
| 913 | Action::ExpelWindowFromColumn => { |
| 914 | self.niri.layout.expel_from_column(); |
| 915 | self.maybe_warp_cursor_to_focus(); |
| 916 | // FIXME: granular |
| 917 | self.niri.queue_redraw_all(); |
| 918 | } |
| 919 | Action::SwitchPresetColumnWidth => { |
| 920 | self.niri.layout.toggle_width(); |
| 921 | } |
| 922 | Action::CenterColumn => { |
| 923 | self.niri.layout.center_column(); |
| 924 | // FIXME: granular |
| 925 | self.niri.queue_redraw_all(); |
| 926 | } |
| 927 | Action::MaximizeColumn => { |
| 928 | self.niri.layout.toggle_full_width(); |
| 929 | } |
| 930 | Action::FocusMonitorLeft => { |
| 931 | if let Some(output) = self.niri.output_left() { |
| 932 | self.niri.layout.focus_output(&output); |
| 933 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 934 | self.move_cursor_to_output(&output); |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | Action::FocusMonitorRight => { |
| 939 | if let Some(output) = self.niri.output_right() { |
| 940 | self.niri.layout.focus_output(&output); |
| 941 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 942 | self.move_cursor_to_output(&output); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | Action::FocusMonitorDown => { |
| 947 | if let Some(output) = self.niri.output_down() { |
| 948 | self.niri.layout.focus_output(&output); |
| 949 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 950 | self.move_cursor_to_output(&output); |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | Action::FocusMonitorUp => { |
| 955 | if let Some(output) = self.niri.output_up() { |
| 956 | self.niri.layout.focus_output(&output); |
| 957 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 958 | self.move_cursor_to_output(&output); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | Action::MoveWindowToMonitorLeft => { |
| 963 | if let Some(output) = self.niri.output_left() { |
| 964 | self.niri.layout.move_to_output(&output); |
| 965 | self.niri.layout.focus_output(&output); |
| 966 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 967 | self.move_cursor_to_output(&output); |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | Action::MoveWindowToMonitorRight => { |
| 972 | if let Some(output) = self.niri.output_right() { |
| 973 | self.niri.layout.move_to_output(&output); |
| 974 | self.niri.layout.focus_output(&output); |
| 975 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 976 | self.move_cursor_to_output(&output); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | Action::MoveWindowToMonitorDown => { |
| 981 | if let Some(output) = self.niri.output_down() { |
| 982 | self.niri.layout.move_to_output(&output); |
| 983 | self.niri.layout.focus_output(&output); |
| 984 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 985 | self.move_cursor_to_output(&output); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | Action::MoveWindowToMonitorUp => { |
| 990 | if let Some(output) = self.niri.output_up() { |
| 991 | self.niri.layout.move_to_output(&output); |
| 992 | self.niri.layout.focus_output(&output); |
| 993 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 994 | self.move_cursor_to_output(&output); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | Action::MoveColumnToMonitorLeft => { |
| 999 | if let Some(output) = self.niri.output_left() { |
| 1000 | self.niri.layout.move_column_to_output(&output); |
| 1001 | self.niri.layout.focus_output(&output); |
| 1002 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1003 | self.move_cursor_to_output(&output); |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | Action::MoveColumnToMonitorRight => { |
| 1008 | if let Some(output) = self.niri.output_right() { |
| 1009 | self.niri.layout.move_column_to_output(&output); |
| 1010 | self.niri.layout.focus_output(&output); |
| 1011 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1012 | self.move_cursor_to_output(&output); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | Action::MoveColumnToMonitorDown => { |
| 1017 | if let Some(output) = self.niri.output_down() { |
| 1018 | self.niri.layout.move_column_to_output(&output); |
| 1019 | self.niri.layout.focus_output(&output); |
| 1020 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1021 | self.move_cursor_to_output(&output); |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | Action::MoveColumnToMonitorUp => { |
| 1026 | if let Some(output) = self.niri.output_up() { |
| 1027 | self.niri.layout.move_column_to_output(&output); |
| 1028 | self.niri.layout.focus_output(&output); |
| 1029 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1030 | self.move_cursor_to_output(&output); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | Action::SetColumnWidth(change) => { |
| 1035 | self.niri.layout.set_column_width(change); |
| 1036 | } |
| 1037 | Action::SetWindowHeight(change) => { |
| 1038 | self.niri.layout.set_window_height(change); |
| 1039 | } |
| 1040 | Action::ResetWindowHeight => { |
| 1041 | self.niri.layout.reset_window_height(); |
| 1042 | } |
| 1043 | Action::ShowHotkeyOverlay => { |
| 1044 | if self.niri.hotkey_overlay.show() { |
| 1045 | self.niri.queue_redraw_all(); |
| 1046 | } |
| 1047 | } |
| 1048 | Action::MoveWorkspaceToMonitorLeft => { |
| 1049 | if let Some(output) = self.niri.output_left() { |
| 1050 | self.niri.layout.move_workspace_to_output(&output); |
| 1051 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1052 | self.move_cursor_to_output(&output); |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | Action::MoveWorkspaceToMonitorRight => { |
| 1057 | if let Some(output) = self.niri.output_right() { |
| 1058 | self.niri.layout.move_workspace_to_output(&output); |
| 1059 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1060 | self.move_cursor_to_output(&output); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | Action::MoveWorkspaceToMonitorDown => { |
| 1065 | if let Some(output) = self.niri.output_down() { |
| 1066 | self.niri.layout.move_workspace_to_output(&output); |
| 1067 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1068 | self.move_cursor_to_output(&output); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | Action::MoveWorkspaceToMonitorUp => { |
| 1073 | if let Some(output) = self.niri.output_up() { |
| 1074 | self.niri.layout.move_workspace_to_output(&output); |
| 1075 | if !self.maybe_warp_cursor_to_focus_centered() { |
| 1076 | self.move_cursor_to_output(&output); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) { |
| 1084 | // We need an output to be able to move the pointer. |
| 1085 | if self.niri.global_space.outputs().next().is_none() { |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | let serial = SERIAL_COUNTER.next_serial(); |
| 1090 | |
| 1091 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1092 | |
| 1093 | let pos = pointer.current_location(); |
| 1094 | |
| 1095 | // We have an output, so we can compute the new location and focus. |
| 1096 | let mut new_pos = pos + event.delta(); |
| 1097 | |
| 1098 | // We received an event for the regular pointer, so show it now. |
| 1099 | self.niri.pointer_hidden = false; |
| 1100 | self.niri.tablet_cursor_location = None; |
| 1101 | |
| 1102 | // Check if we have an active pointer constraint. |
| 1103 | let mut pointer_confined = None; |
| 1104 | if let Some(focus) = &self.niri.pointer_focus.surface { |
| 1105 | let pos_within_surface = pos - focus.1; |
| 1106 | |
| 1107 | let mut pointer_locked = false; |
| 1108 | with_pointer_constraint(&focus.0, &pointer, |constraint| { |
| 1109 | let Some(constraint) = constraint else { return }; |
| 1110 | if !constraint.is_active() { |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | // Constraint does not apply if not within region. |
| 1115 | if let Some(region) = constraint.region() { |
| 1116 | if !region.contains(pos_within_surface.to_i32_round()) { |
| 1117 | return; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | match &*constraint { |
| 1122 | PointerConstraint::Locked(_locked) => { |
| 1123 | pointer_locked = true; |
| 1124 | } |
| 1125 | PointerConstraint::Confined(confine) => { |
| 1126 | pointer_confined = Some((focus.clone(), confine.region().cloned())); |
| 1127 | } |
| 1128 | } |
| 1129 | }); |
| 1130 | |
| 1131 | // If the pointer is locked, only send relative motion. |
| 1132 | if pointer_locked { |
| 1133 | pointer.relative_motion( |
| 1134 | self, |
| 1135 | Some(focus.clone()), |
| 1136 | &RelativeMotionEvent { |
| 1137 | delta: event.delta(), |
| 1138 | delta_unaccel: event.delta_unaccel(), |
| 1139 | utime: event.time(), |
| 1140 | }, |
| 1141 | ); |
| 1142 | |
| 1143 | pointer.frame(self); |
| 1144 | |
| 1145 | // I guess a redraw to hide the tablet cursor could be nice? Doesn't matter too |
| 1146 | // much here I think. |
| 1147 | return; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | if self |
| 1152 | .niri |
| 1153 | .global_space |
| 1154 | .output_under(new_pos) |
| 1155 | .next() |
| 1156 | .is_none() |
| 1157 | { |
| 1158 | // We ended up outside the outputs and need to clip the movement. |
| 1159 | if let Some(output) = self.niri.global_space.output_under(pos).next() { |
| 1160 | // The pointer was previously on some output. Clip the movement against its |
| 1161 | // boundaries. |
| 1162 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 1163 | new_pos.x = new_pos |
| 1164 | .x |
| 1165 | .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); |
| 1166 | new_pos.y = new_pos |
| 1167 | .y |
| 1168 | .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); |
| 1169 | } else { |
| 1170 | // The pointer was not on any output in the first place. Find one for it. |
| 1171 | // Let's do the simple thing and just put it on the first output. |
| 1172 | let output = self.niri.global_space.outputs().next().unwrap(); |
| 1173 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 1174 | new_pos = center(geom).to_f64(); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 1179 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 1180 | let mut point = (new_pos - geom.loc.to_f64()) |
| 1181 | .to_physical(output.current_scale().fractional_scale()) |
| 1182 | .to_i32_round::<i32>(); |
| 1183 | |
| 1184 | let size = output.current_mode().unwrap().size; |
| 1185 | let transform = output.current_transform(); |
| 1186 | let size = transform.transform_size(size); |
| 1187 | point.x = point.x.clamp(0, size.w - 1); |
| 1188 | point.y = point.y.clamp(0, size.h - 1); |
| 1189 | |
| 1190 | self.niri.screenshot_ui.pointer_motion(point); |
| 1191 | } |
| 1192 | |
| 1193 | let under = self.niri.surface_under_and_global_space(new_pos); |
| 1194 | |
| 1195 | // Handle confined pointer. |
| 1196 | if let Some((focus_surface, region)) = pointer_confined { |
| 1197 | let mut prevent = false; |
| 1198 | |
| 1199 | // Prevent the pointer from leaving the focused surface. |
| 1200 | if Some(&focus_surface.0) != under.surface.as_ref().map(|(s, _)| s) { |
| 1201 | prevent = true; |
| 1202 | } |
| 1203 | |
| 1204 | // Prevent the pointer from leaving the confine region, if any. |
| 1205 | if let Some(region) = region { |
| 1206 | let new_pos_within_surface = new_pos - focus_surface.1; |
| 1207 | if !region.contains(new_pos_within_surface.to_i32_round()) { |
| 1208 | prevent = true; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | if prevent { |
| 1213 | pointer.relative_motion( |
| 1214 | self, |
| 1215 | Some(focus_surface), |
| 1216 | &RelativeMotionEvent { |
| 1217 | delta: event.delta(), |
| 1218 | delta_unaccel: event.delta_unaccel(), |
| 1219 | utime: event.time(), |
| 1220 | }, |
| 1221 | ); |
| 1222 | |
| 1223 | pointer.frame(self); |
| 1224 | |
| 1225 | return; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | self.niri.handle_focus_follows_mouse(&under); |
| 1230 | |
| 1231 | // Activate a new confinement if necessary. |
| 1232 | self.niri.maybe_activate_pointer_constraint(new_pos, &under); |
| 1233 | |
| 1234 | self.niri.pointer_focus.clone_from(&under); |
| 1235 | |
| 1236 | pointer.motion( |
| 1237 | self, |
| 1238 | under.surface.clone(), |
| 1239 | &MotionEvent { |
| 1240 | location: new_pos, |
| 1241 | serial, |
| 1242 | time: event.time_msec(), |
| 1243 | }, |
| 1244 | ); |
| 1245 | |
| 1246 | pointer.relative_motion( |
| 1247 | self, |
| 1248 | under.surface, |
| 1249 | &RelativeMotionEvent { |
| 1250 | delta: event.delta(), |
| 1251 | delta_unaccel: event.delta_unaccel(), |
| 1252 | utime: event.time(), |
| 1253 | }, |
| 1254 | ); |
| 1255 | |
| 1256 | pointer.frame(self); |
| 1257 | |
| 1258 | // Redraw to update the cursor position. |
| 1259 | // FIXME: redraw only outputs overlapping the cursor. |
| 1260 | self.niri.queue_redraw_all(); |
| 1261 | } |
| 1262 | |
| 1263 | fn on_pointer_motion_absolute<I: InputBackend>( |
| 1264 | &mut self, |
| 1265 | event: I::PointerMotionAbsoluteEvent, |
| 1266 | ) { |
| 1267 | let Some(output_geo) = self.global_bounding_rectangle() else { |
| 1268 | return; |
| 1269 | }; |
| 1270 | |
| 1271 | let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); |
| 1272 | |
| 1273 | let serial = SERIAL_COUNTER.next_serial(); |
| 1274 | |
| 1275 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1276 | |
| 1277 | if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 1278 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 1279 | let mut point = (pos - geom.loc.to_f64()) |
| 1280 | .to_physical(output.current_scale().fractional_scale()) |
| 1281 | .to_i32_round::<i32>(); |
| 1282 | |
| 1283 | let size = output.current_mode().unwrap().size; |
| 1284 | let transform = output.current_transform(); |
| 1285 | let size = transform.transform_size(size); |
| 1286 | point.x = point.x.clamp(0, size.w - 1); |
| 1287 | point.y = point.y.clamp(0, size.h - 1); |
| 1288 | |
| 1289 | self.niri.screenshot_ui.pointer_motion(point); |
| 1290 | } |
| 1291 | |
| 1292 | let under = self.niri.surface_under_and_global_space(pos); |
| 1293 | |
| 1294 | self.niri.handle_focus_follows_mouse(&under); |
| 1295 | |
| 1296 | self.niri.maybe_activate_pointer_constraint(pos, &under); |
| 1297 | self.niri.pointer_focus.clone_from(&under); |
| 1298 | |
| 1299 | pointer.motion( |
| 1300 | self, |
| 1301 | under.surface, |
| 1302 | &MotionEvent { |
| 1303 | location: pos, |
| 1304 | serial, |
| 1305 | time: event.time_msec(), |
| 1306 | }, |
| 1307 | ); |
| 1308 | |
| 1309 | pointer.frame(self); |
| 1310 | |
| 1311 | // We moved the pointer, show it. |
| 1312 | self.niri.pointer_hidden = false; |
| 1313 | |
| 1314 | // We moved the regular pointer, so show it now. |
| 1315 | self.niri.tablet_cursor_location = None; |
| 1316 | |
| 1317 | // Redraw to update the cursor position. |
| 1318 | // FIXME: redraw only outputs overlapping the cursor. |
| 1319 | self.niri.queue_redraw_all(); |
| 1320 | } |
| 1321 | |
| 1322 | fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) { |
| 1323 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1324 | |
| 1325 | let serial = SERIAL_COUNTER.next_serial(); |
| 1326 | |
| 1327 | let button = event.button_code(); |
| 1328 | |
| 1329 | let button_state = event.state(); |
| 1330 | |
| 1331 | if ButtonState::Pressed == button_state { |
| 1332 | if let Some(mapped) = self.niri.window_under_cursor() { |
| 1333 | let window = mapped.window.clone(); |
| 1334 | |
| 1335 | // Check if we need to start an interactive resize. |
| 1336 | if event.button() == Some(MouseButton::Right) && !pointer.is_grabbed() { |
| 1337 | let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); |
| 1338 | let mod_down = match self.backend.mod_key() { |
| 1339 | CompositorMod::Super => mods.logo, |
| 1340 | CompositorMod::Alt => mods.alt, |
| 1341 | }; |
| 1342 | if mod_down { |
| 1343 | let location = pointer.current_location(); |
| 1344 | let (output, pos_within_output) = self.niri.output_under(location).unwrap(); |
| 1345 | let edges = self |
| 1346 | .niri |
| 1347 | .layout |
| 1348 | .resize_edges_under(output, pos_within_output) |
| 1349 | .unwrap(); |
| 1350 | |
| 1351 | if !edges.is_empty() { |
| 1352 | // See if we got a double resize-click gesture. |
| 1353 | // FIXME: deduplicate with resize_request in xdg-shell somehow. |
| 1354 | let time = get_monotonic_time(); |
| 1355 | let last_cell = mapped.last_interactive_resize_start(); |
| 1356 | let last = last_cell.get(); |
| 1357 | last_cell.set(Some((time, edges))); |
| 1358 | if let Some((last_time, last_edges)) = last { |
| 1359 | if time.saturating_sub(last_time) <= DOUBLE_CLICK_TIME { |
| 1360 | // Allow quick resize after a triple click. |
| 1361 | last_cell.set(None); |
| 1362 | |
| 1363 | let intersection = edges.intersection(last_edges); |
| 1364 | if intersection.intersects(ResizeEdge::LEFT_RIGHT) { |
| 1365 | // FIXME: don't activate once we can pass specific windows |
| 1366 | // to actions. |
| 1367 | self.niri.layout.activate_window(&window); |
| 1368 | self.niri.layout.toggle_full_width(); |
| 1369 | } |
| 1370 | if intersection.intersects(ResizeEdge::TOP_BOTTOM) { |
| 1371 | // FIXME: don't activate once we can pass specific windows |
| 1372 | // to actions. |
| 1373 | self.niri.layout.activate_window(&window); |
| 1374 | self.niri.layout.reset_window_height(); |
| 1375 | } |
| 1376 | // FIXME: granular. |
| 1377 | self.niri.queue_redraw_all(); |
| 1378 | return; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | self.niri.layout.activate_window(&window); |
| 1383 | |
| 1384 | if self |
| 1385 | .niri |
| 1386 | .layout |
| 1387 | .interactive_resize_begin(window.clone(), edges) |
| 1388 | { |
| 1389 | let start_data = PointerGrabStartData { |
| 1390 | focus: None, |
| 1391 | button: event.button_code(), |
| 1392 | location, |
| 1393 | }; |
| 1394 | let grab = ResizeGrab::new(start_data, window.clone()); |
| 1395 | pointer.set_grab(self, grab, serial, Focus::Clear); |
| 1396 | self.niri.pointer_grab_ongoing = true; |
| 1397 | self.niri.cursor_manager.set_cursor_image( |
| 1398 | CursorImageStatus::Named(edges.cursor_icon()), |
| 1399 | ); |
| 1400 | } |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | self.niri.layout.activate_window(&window); |
| 1406 | |
| 1407 | // FIXME: granular. |
| 1408 | self.niri.queue_redraw_all(); |
| 1409 | } else if let Some(output) = self.niri.output_under_cursor() { |
| 1410 | self.niri.layout.activate_output(&output); |
| 1411 | |
| 1412 | // FIXME: granular. |
| 1413 | self.niri.queue_redraw_all(); |
| 1414 | } |
| 1415 | |
| 1416 | if event.button() == Some(MouseButton::Middle) && !pointer.is_grabbed() { |
| 1417 | let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); |
| 1418 | let mod_down = match self.backend.mod_key() { |
| 1419 | CompositorMod::Super => mods.logo, |
| 1420 | CompositorMod::Alt => mods.alt, |
| 1421 | }; |
| 1422 | if mod_down { |
| 1423 | if let Some(output) = self.niri.output_under_cursor() { |
| 1424 | let location = pointer.current_location(); |
| 1425 | let start_data = PointerGrabStartData { |
| 1426 | focus: None, |
| 1427 | button: event.button_code(), |
| 1428 | location, |
| 1429 | }; |
| 1430 | let grab = SpatialMovementGrab::new(start_data, output); |
| 1431 | pointer.set_grab(self, grab, serial, Focus::Clear); |
| 1432 | self.niri.pointer_grab_ongoing = true; |
| 1433 | self.niri |
| 1434 | .cursor_manager |
| 1435 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::AllScroll)); |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | }; |
| 1440 | |
| 1441 | self.update_pointer_focus(); |
| 1442 | |
| 1443 | if ButtonState::Pressed == button_state { |
| 1444 | let layer_focus = self.niri.pointer_focus.layer.clone(); |
| 1445 | self.niri.focus_layer_surface_if_on_demand(layer_focus); |
| 1446 | } |
| 1447 | |
| 1448 | if let Some(button) = event.button() { |
| 1449 | let pos = pointer.current_location(); |
| 1450 | if let Some((output, _)) = self.niri.output_under(pos) { |
| 1451 | let output = output.clone(); |
| 1452 | let geom = self.niri.global_space.output_geometry(&output).unwrap(); |
| 1453 | let mut point = (pos - geom.loc.to_f64()) |
| 1454 | .to_physical(output.current_scale().fractional_scale()) |
| 1455 | .to_i32_round(); |
| 1456 | |
| 1457 | let size = output.current_mode().unwrap().size; |
| 1458 | let transform = output.current_transform(); |
| 1459 | let size = transform.transform_size(size); |
| 1460 | point.x = min(size.w - 1, point.x); |
| 1461 | point.y = min(size.h - 1, point.y); |
| 1462 | |
| 1463 | if self |
| 1464 | .niri |
| 1465 | .screenshot_ui |
| 1466 | .pointer_button(output, point, button, button_state) |
| 1467 | { |
| 1468 | self.niri.queue_redraw_all(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | pointer.button( |
| 1474 | self, |
| 1475 | &ButtonEvent { |
| 1476 | button, |
| 1477 | state: button_state, |
| 1478 | serial, |
| 1479 | time: event.time_msec(), |
| 1480 | }, |
| 1481 | ); |
| 1482 | pointer.frame(self); |
| 1483 | } |
| 1484 | |
| 1485 | fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) { |
| 1486 | let source = event.source(); |
| 1487 | |
| 1488 | let horizontal_amount_v120 = event.amount_v120(Axis::Horizontal); |
| 1489 | let vertical_amount_v120 = event.amount_v120(Axis::Vertical); |
| 1490 | |
| 1491 | // Handle wheel scroll bindings. |
| 1492 | if source == AxisSource::Wheel { |
| 1493 | // If we have a scroll bind with current modifiers, then accumulate and don't pass to |
| 1494 | // Wayland. If there's no bind, reset the accumulator. |
| 1495 | let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); |
| 1496 | let modifiers = modifiers_from_state(mods); |
| 1497 | if self.niri.mods_with_wheel_binds.contains(&modifiers) { |
| 1498 | let comp_mod = self.backend.mod_key(); |
| 1499 | |
| 1500 | let horizontal = horizontal_amount_v120.unwrap_or(0.); |
| 1501 | let ticks = self.niri.horizontal_wheel_tracker.accumulate(horizontal); |
| 1502 | if ticks != 0 { |
| 1503 | let config = self.niri.config.borrow(); |
| 1504 | let bindings = &config.binds; |
| 1505 | let bind_left = |
| 1506 | find_configured_bind(bindings, comp_mod, Trigger::WheelScrollLeft, mods); |
| 1507 | let bind_right = |
| 1508 | find_configured_bind(bindings, comp_mod, Trigger::WheelScrollRight, mods); |
| 1509 | drop(config); |
| 1510 | |
| 1511 | if let Some(right) = bind_right { |
| 1512 | for _ in 0..ticks { |
| 1513 | self.handle_bind(right.clone()); |
| 1514 | } |
| 1515 | } |
| 1516 | if let Some(left) = bind_left { |
| 1517 | for _ in ticks..0 { |
| 1518 | self.handle_bind(left.clone()); |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | let vertical = vertical_amount_v120.unwrap_or(0.); |
| 1524 | let ticks = self.niri.vertical_wheel_tracker.accumulate(vertical); |
| 1525 | if ticks != 0 { |
| 1526 | let config = self.niri.config.borrow(); |
| 1527 | let bindings = &config.binds; |
| 1528 | let bind_up = |
| 1529 | find_configured_bind(bindings, comp_mod, Trigger::WheelScrollUp, mods); |
| 1530 | let bind_down = |
| 1531 | find_configured_bind(bindings, comp_mod, Trigger::WheelScrollDown, mods); |
| 1532 | drop(config); |
| 1533 | |
| 1534 | if let Some(down) = bind_down { |
| 1535 | for _ in 0..ticks { |
| 1536 | self.handle_bind(down.clone()); |
| 1537 | } |
| 1538 | } |
| 1539 | if let Some(up) = bind_up { |
| 1540 | for _ in ticks..0 { |
| 1541 | self.handle_bind(up.clone()); |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | return; |
| 1547 | } else { |
| 1548 | self.niri.horizontal_wheel_tracker.reset(); |
| 1549 | self.niri.vertical_wheel_tracker.reset(); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | let horizontal_amount = event.amount(Axis::Horizontal); |
| 1554 | let vertical_amount = event.amount(Axis::Vertical); |
| 1555 | |
| 1556 | // Handle touchpad scroll bindings. |
| 1557 | if source == AxisSource::Finger { |
| 1558 | let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); |
| 1559 | let modifiers = modifiers_from_state(mods); |
| 1560 | if self.niri.mods_with_finger_scroll_binds.contains(&modifiers) { |
| 1561 | let comp_mod = self.backend.mod_key(); |
| 1562 | |
| 1563 | let horizontal = horizontal_amount.unwrap_or(0.); |
| 1564 | let ticks = self |
| 1565 | .niri |
| 1566 | .horizontal_finger_scroll_tracker |
| 1567 | .accumulate(horizontal); |
| 1568 | if ticks != 0 { |
| 1569 | let config = self.niri.config.borrow(); |
| 1570 | let bindings = &config.binds; |
| 1571 | let bind_left = |
| 1572 | find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollLeft, mods); |
| 1573 | let bind_right = find_configured_bind( |
| 1574 | bindings, |
| 1575 | comp_mod, |
| 1576 | Trigger::TouchpadScrollRight, |
| 1577 | mods, |
| 1578 | ); |
| 1579 | drop(config); |
| 1580 | |
| 1581 | if let Some(right) = bind_right { |
| 1582 | for _ in 0..ticks { |
| 1583 | self.handle_bind(right.clone()); |
| 1584 | } |
| 1585 | } |
| 1586 | if let Some(left) = bind_left { |
| 1587 | for _ in ticks..0 { |
| 1588 | self.handle_bind(left.clone()); |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | let vertical = vertical_amount.unwrap_or(0.); |
| 1594 | let ticks = self |
| 1595 | .niri |
| 1596 | .vertical_finger_scroll_tracker |
| 1597 | .accumulate(vertical); |
| 1598 | if ticks != 0 { |
| 1599 | let config = self.niri.config.borrow(); |
| 1600 | let bindings = &config.binds; |
| 1601 | let bind_up = |
| 1602 | find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollUp, mods); |
| 1603 | let bind_down = |
| 1604 | find_configured_bind(bindings, comp_mod, Trigger::TouchpadScrollDown, mods); |
| 1605 | drop(config); |
| 1606 | |
| 1607 | if let Some(down) = bind_down { |
| 1608 | for _ in 0..ticks { |
| 1609 | self.handle_bind(down.clone()); |
| 1610 | } |
| 1611 | } |
| 1612 | if let Some(up) = bind_up { |
| 1613 | for _ in ticks..0 { |
| 1614 | self.handle_bind(up.clone()); |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | return; |
| 1620 | } else { |
| 1621 | self.niri.horizontal_finger_scroll_tracker.reset(); |
| 1622 | self.niri.vertical_finger_scroll_tracker.reset(); |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | let horizontal_amount = horizontal_amount.unwrap_or_else(|| { |
| 1627 | // Winit backend, discrete scrolling. |
| 1628 | horizontal_amount_v120.unwrap_or(0.0) / 120. * 15. |
| 1629 | }); |
| 1630 | let vertical_amount = vertical_amount.unwrap_or_else(|| { |
| 1631 | // Winit backend, discrete scrolling. |
| 1632 | vertical_amount_v120.unwrap_or(0.0) / 120. * 15. |
| 1633 | }); |
| 1634 | |
| 1635 | let mut frame = AxisFrame::new(event.time_msec()).source(source); |
| 1636 | if horizontal_amount != 0.0 { |
| 1637 | frame = frame |
| 1638 | .relative_direction(Axis::Horizontal, event.relative_direction(Axis::Horizontal)); |
| 1639 | frame = frame.value(Axis::Horizontal, horizontal_amount); |
| 1640 | if let Some(v120) = horizontal_amount_v120 { |
| 1641 | frame = frame.v120(Axis::Horizontal, v120 as i32); |
| 1642 | } |
| 1643 | } |
| 1644 | if vertical_amount != 0.0 { |
| 1645 | frame = |
| 1646 | frame.relative_direction(Axis::Vertical, event.relative_direction(Axis::Vertical)); |
| 1647 | frame = frame.value(Axis::Vertical, vertical_amount); |
| 1648 | if let Some(v120) = vertical_amount_v120 { |
| 1649 | frame = frame.v120(Axis::Vertical, v120 as i32); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | if source == AxisSource::Finger { |
| 1654 | if event.amount(Axis::Horizontal) == Some(0.0) { |
| 1655 | frame = frame.stop(Axis::Horizontal); |
| 1656 | } |
| 1657 | if event.amount(Axis::Vertical) == Some(0.0) { |
| 1658 | frame = frame.stop(Axis::Vertical); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | self.update_pointer_focus(); |
| 1663 | |
| 1664 | let pointer = &self.niri.seat.get_pointer().unwrap(); |
| 1665 | pointer.axis(self, frame); |
| 1666 | pointer.frame(self); |
| 1667 | } |
| 1668 | |
| 1669 | fn on_tablet_tool_axis<I: InputBackend>(&mut self, event: I::TabletToolAxisEvent) |
| 1670 | where |
| 1671 | I::Device: 'static, // Needed for downcasting. |
| 1672 | { |
| 1673 | let Some(pos) = self.compute_tablet_position(&event) else { |
| 1674 | return; |
| 1675 | }; |
| 1676 | |
| 1677 | let under = self.niri.surface_under_and_global_space(pos); |
| 1678 | |
| 1679 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 1680 | let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); |
| 1681 | let tool = tablet_seat.get_tool(&event.tool()); |
| 1682 | if let (Some(tablet), Some(tool)) = (tablet, tool) { |
| 1683 | if event.pressure_has_changed() { |
| 1684 | tool.pressure(event.pressure()); |
| 1685 | } |
| 1686 | if event.distance_has_changed() { |
| 1687 | tool.distance(event.distance()); |
| 1688 | } |
| 1689 | if event.tilt_has_changed() { |
| 1690 | tool.tilt(event.tilt()); |
| 1691 | } |
| 1692 | if event.slider_has_changed() { |
| 1693 | tool.slider_position(event.slider_position()); |
| 1694 | } |
| 1695 | if event.rotation_has_changed() { |
| 1696 | tool.rotation(event.rotation()); |
| 1697 | } |
| 1698 | if event.wheel_has_changed() { |
| 1699 | tool.wheel(event.wheel_delta(), event.wheel_delta_discrete()); |
| 1700 | } |
| 1701 | |
| 1702 | tool.motion( |
| 1703 | pos, |
| 1704 | under.surface, |
| 1705 | &tablet, |
| 1706 | SERIAL_COUNTER.next_serial(), |
| 1707 | event.time_msec(), |
| 1708 | ); |
| 1709 | |
| 1710 | self.niri.pointer_hidden = false; |
| 1711 | self.niri.tablet_cursor_location = Some(pos); |
| 1712 | } |
| 1713 | |
| 1714 | // Redraw to update the cursor position. |
| 1715 | // FIXME: redraw only outputs overlapping the cursor. |
| 1716 | self.niri.queue_redraw_all(); |
| 1717 | } |
| 1718 | |
| 1719 | fn on_tablet_tool_tip<I: InputBackend>(&mut self, event: I::TabletToolTipEvent) { |
| 1720 | let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); |
| 1721 | |
| 1722 | if let Some(tool) = tool { |
| 1723 | match event.tip_state() { |
| 1724 | TabletToolTipState::Down => { |
| 1725 | let serial = SERIAL_COUNTER.next_serial(); |
| 1726 | tool.tip_down(serial, event.time_msec()); |
| 1727 | |
| 1728 | if let Some(pos) = self.niri.tablet_cursor_location { |
| 1729 | let under = self.niri.surface_under_and_global_space(pos); |
| 1730 | if let Some(window) = under.window { |
| 1731 | self.niri.layout.activate_window(&window); |
| 1732 | |
| 1733 | // FIXME: granular. |
| 1734 | self.niri.queue_redraw_all(); |
| 1735 | } else if let Some(output) = under.output { |
| 1736 | self.niri.layout.activate_output(&output); |
| 1737 | |
| 1738 | // FIXME: granular. |
| 1739 | self.niri.queue_redraw_all(); |
| 1740 | } |
| 1741 | self.niri.focus_layer_surface_if_on_demand(under.layer); |
| 1742 | } |
| 1743 | } |
| 1744 | TabletToolTipState::Up => { |
| 1745 | tool.tip_up(event.time_msec()); |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | fn on_tablet_tool_proximity<I: InputBackend>(&mut self, event: I::TabletToolProximityEvent) |
| 1752 | where |
| 1753 | I::Device: 'static, // Needed for downcasting. |
| 1754 | { |
| 1755 | let Some(pos) = self.compute_tablet_position(&event) else { |
| 1756 | return; |
| 1757 | }; |
| 1758 | |
| 1759 | let under = self.niri.surface_under_and_global_space(pos); |
| 1760 | |
| 1761 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 1762 | let tool = tablet_seat.add_tool::<Self>(&self.niri.display_handle, &event.tool()); |
| 1763 | let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); |
| 1764 | if let Some(tablet) = tablet { |
| 1765 | match event.state() { |
| 1766 | ProximityState::In => { |
| 1767 | if let Some(under) = under.surface { |
| 1768 | tool.proximity_in( |
| 1769 | pos, |
| 1770 | under, |
| 1771 | &tablet, |
| 1772 | SERIAL_COUNTER.next_serial(), |
| 1773 | event.time_msec(), |
| 1774 | ); |
| 1775 | } |
| 1776 | self.niri.pointer_hidden = false; |
| 1777 | self.niri.tablet_cursor_location = Some(pos); |
| 1778 | } |
| 1779 | ProximityState::Out => { |
| 1780 | tool.proximity_out(event.time_msec()); |
| 1781 | |
| 1782 | // Move the mouse pointer here to avoid discontinuity. |
| 1783 | // |
| 1784 | // Plus, Wayland SDL2 currently warps the pointer into some weird |
| 1785 | // location on proximity out, so this should help it a little. |
| 1786 | if let Some(pos) = self.niri.tablet_cursor_location { |
| 1787 | self.move_cursor(pos); |
| 1788 | } |
| 1789 | |
| 1790 | self.niri.tablet_cursor_location = None; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | // FIXME: granular. |
| 1795 | self.niri.queue_redraw_all(); |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | fn on_tablet_tool_button<I: InputBackend>(&mut self, event: I::TabletToolButtonEvent) { |
| 1800 | let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); |
| 1801 | |
| 1802 | if let Some(tool) = tool { |
| 1803 | tool.button( |
| 1804 | event.button(), |
| 1805 | event.button_state(), |
| 1806 | SERIAL_COUNTER.next_serial(), |
| 1807 | event.time_msec(), |
| 1808 | ); |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) { |
| 1813 | if event.fingers() == 3 { |
| 1814 | self.niri.gesture_swipe_3f_cumulative = Some((0., 0.)); |
| 1815 | |
| 1816 | // We handled this event. |
| 1817 | return; |
| 1818 | } |
| 1819 | |
| 1820 | let serial = SERIAL_COUNTER.next_serial(); |
| 1821 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1822 | |
| 1823 | if self.update_pointer_focus() { |
| 1824 | pointer.frame(self); |
| 1825 | } |
| 1826 | |
| 1827 | pointer.gesture_swipe_begin( |
| 1828 | self, |
| 1829 | &GestureSwipeBeginEvent { |
| 1830 | serial, |
| 1831 | time: event.time_msec(), |
| 1832 | fingers: event.fingers(), |
| 1833 | }, |
| 1834 | ); |
| 1835 | } |
| 1836 | |
| 1837 | fn on_gesture_swipe_update<I: InputBackend + 'static>( |
| 1838 | &mut self, |
| 1839 | event: I::GestureSwipeUpdateEvent, |
| 1840 | ) where |
| 1841 | I::Device: 'static, |
| 1842 | { |
| 1843 | let mut delta_x = event.delta_x(); |
| 1844 | let mut delta_y = event.delta_y(); |
| 1845 | |
| 1846 | if let Some(libinput_event) = |
| 1847 | (&event as &dyn Any).downcast_ref::<input::event::gesture::GestureSwipeUpdateEvent>() |
| 1848 | { |
| 1849 | delta_x = libinput_event.dx_unaccelerated(); |
| 1850 | delta_y = libinput_event.dy_unaccelerated(); |
| 1851 | } |
| 1852 | |
| 1853 | let device = event.device(); |
| 1854 | if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() { |
| 1855 | if device.config_scroll_natural_scroll_enabled() { |
| 1856 | delta_x = -delta_x; |
| 1857 | delta_y = -delta_y; |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | if let Some((cx, cy)) = &mut self.niri.gesture_swipe_3f_cumulative { |
| 1862 | *cx += delta_x; |
| 1863 | *cy += delta_y; |
| 1864 | |
| 1865 | // Check if the gesture moved far enough to decide. Threshold copied from GNOME Shell. |
| 1866 | let (cx, cy) = (*cx, *cy); |
| 1867 | if cx * cx + cy * cy >= 16. * 16. { |
| 1868 | self.niri.gesture_swipe_3f_cumulative = None; |
| 1869 | |
| 1870 | if let Some(output) = self.niri.output_under_cursor() { |
| 1871 | if cx.abs() > cy.abs() { |
| 1872 | self.niri.layout.view_offset_gesture_begin(&output, true); |
| 1873 | } else { |
| 1874 | self.niri |
| 1875 | .layout |
| 1876 | .workspace_switch_gesture_begin(&output, true); |
| 1877 | } |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | let timestamp = Duration::from_micros(event.time()); |
| 1883 | |
| 1884 | let mut handled = false; |
| 1885 | let res = self |
| 1886 | .niri |
| 1887 | .layout |
| 1888 | .workspace_switch_gesture_update(delta_y, timestamp, true); |
| 1889 | if let Some(output) = res { |
| 1890 | if let Some(output) = output { |
| 1891 | self.niri.queue_redraw(&output); |
| 1892 | } |
| 1893 | handled = true; |
| 1894 | } |
| 1895 | |
| 1896 | let res = self |
| 1897 | .niri |
| 1898 | .layout |
| 1899 | .view_offset_gesture_update(delta_x, timestamp, true); |
| 1900 | if let Some(output) = res { |
| 1901 | if let Some(output) = output { |
| 1902 | self.niri.queue_redraw(&output); |
| 1903 | } |
| 1904 | handled = true; |
| 1905 | } |
| 1906 | |
| 1907 | if handled { |
| 1908 | // We handled this event. |
| 1909 | return; |
| 1910 | } |
| 1911 | |
| 1912 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1913 | |
| 1914 | if self.update_pointer_focus() { |
| 1915 | pointer.frame(self); |
| 1916 | } |
| 1917 | |
| 1918 | pointer.gesture_swipe_update( |
| 1919 | self, |
| 1920 | &GestureSwipeUpdateEvent { |
| 1921 | time: event.time_msec(), |
| 1922 | delta: event.delta(), |
| 1923 | }, |
| 1924 | ); |
| 1925 | } |
| 1926 | |
| 1927 | fn on_gesture_swipe_end<I: InputBackend>(&mut self, event: I::GestureSwipeEndEvent) { |
| 1928 | self.niri.gesture_swipe_3f_cumulative = None; |
| 1929 | |
| 1930 | let mut handled = false; |
| 1931 | let res = self |
| 1932 | .niri |
| 1933 | .layout |
| 1934 | .workspace_switch_gesture_end(event.cancelled(), Some(true)); |
| 1935 | if let Some(output) = res { |
| 1936 | self.niri.queue_redraw(&output); |
| 1937 | handled = true; |
| 1938 | } |
| 1939 | |
| 1940 | let res = self |
| 1941 | .niri |
| 1942 | .layout |
| 1943 | .view_offset_gesture_end(event.cancelled(), Some(true)); |
| 1944 | if let Some(output) = res { |
| 1945 | self.niri.queue_redraw(&output); |
| 1946 | handled = true; |
| 1947 | } |
| 1948 | |
| 1949 | if handled { |
| 1950 | // We handled this event. |
| 1951 | return; |
| 1952 | } |
| 1953 | |
| 1954 | let serial = SERIAL_COUNTER.next_serial(); |
| 1955 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1956 | |
| 1957 | if self.update_pointer_focus() { |
| 1958 | pointer.frame(self); |
| 1959 | } |
| 1960 | |
| 1961 | pointer.gesture_swipe_end( |
| 1962 | self, |
| 1963 | &GestureSwipeEndEvent { |
| 1964 | serial, |
| 1965 | time: event.time_msec(), |
| 1966 | cancelled: event.cancelled(), |
| 1967 | }, |
| 1968 | ); |
| 1969 | } |
| 1970 | |
| 1971 | fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) { |
| 1972 | let serial = SERIAL_COUNTER.next_serial(); |
| 1973 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1974 | |
| 1975 | if self.update_pointer_focus() { |
| 1976 | pointer.frame(self); |
| 1977 | } |
| 1978 | |
| 1979 | pointer.gesture_pinch_begin( |
| 1980 | self, |
| 1981 | &GesturePinchBeginEvent { |
| 1982 | serial, |
| 1983 | time: event.time_msec(), |
| 1984 | fingers: event.fingers(), |
| 1985 | }, |
| 1986 | ); |
| 1987 | } |
| 1988 | |
| 1989 | fn on_gesture_pinch_update<I: InputBackend>(&mut self, event: I::GesturePinchUpdateEvent) { |
| 1990 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1991 | |
| 1992 | if self.update_pointer_focus() { |
| 1993 | pointer.frame(self); |
| 1994 | } |
| 1995 | |
| 1996 | pointer.gesture_pinch_update( |
| 1997 | self, |
| 1998 | &GesturePinchUpdateEvent { |
| 1999 | time: event.time_msec(), |
| 2000 | delta: event.delta(), |
| 2001 | scale: event.scale(), |
| 2002 | rotation: event.rotation(), |
| 2003 | }, |
| 2004 | ); |
| 2005 | } |
| 2006 | |
| 2007 | fn on_gesture_pinch_end<I: InputBackend>(&mut self, event: I::GesturePinchEndEvent) { |
| 2008 | let serial = SERIAL_COUNTER.next_serial(); |
| 2009 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 2010 | |
| 2011 | if self.update_pointer_focus() { |
| 2012 | pointer.frame(self); |
| 2013 | } |
| 2014 | |
| 2015 | pointer.gesture_pinch_end( |
| 2016 | self, |
| 2017 | &GesturePinchEndEvent { |
| 2018 | serial, |
| 2019 | time: event.time_msec(), |
| 2020 | cancelled: event.cancelled(), |
| 2021 | }, |
| 2022 | ); |
| 2023 | } |
| 2024 | |
| 2025 | fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) { |
| 2026 | let serial = SERIAL_COUNTER.next_serial(); |
| 2027 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 2028 | |
| 2029 | if self.update_pointer_focus() { |
| 2030 | pointer.frame(self); |
| 2031 | } |
| 2032 | |
| 2033 | pointer.gesture_hold_begin( |
| 2034 | self, |
| 2035 | &GestureHoldBeginEvent { |
| 2036 | serial, |
| 2037 | time: event.time_msec(), |
| 2038 | fingers: event.fingers(), |
| 2039 | }, |
| 2040 | ); |
| 2041 | } |
| 2042 | |
| 2043 | fn on_gesture_hold_end<I: InputBackend>(&mut self, event: I::GestureHoldEndEvent) { |
| 2044 | let serial = SERIAL_COUNTER.next_serial(); |
| 2045 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 2046 | |
| 2047 | if self.update_pointer_focus() { |
| 2048 | pointer.frame(self); |
| 2049 | } |
| 2050 | |
| 2051 | pointer.gesture_hold_end( |
| 2052 | self, |
| 2053 | &GestureHoldEndEvent { |
| 2054 | serial, |
| 2055 | time: event.time_msec(), |
| 2056 | cancelled: event.cancelled(), |
| 2057 | }, |
| 2058 | ); |
| 2059 | } |
| 2060 | |
| 2061 | /// Computes the cursor position for the touch event. |
| 2062 | /// |
| 2063 | /// This function handles the touch output mapping, as well as coordinate transform |
| 2064 | fn compute_touch_location<I: InputBackend, E: AbsolutePositionEvent<I>>( |
| 2065 | &self, |
| 2066 | evt: &E, |
| 2067 | ) -> Option<Point<f64, Logical>> { |
| 2068 | let output = self.niri.output_for_touch()?; |
| 2069 | let output_geo = self.niri.global_space.output_geometry(output).unwrap(); |
| 2070 | let transform = output.current_transform(); |
| 2071 | let size = transform.invert().transform_size(output_geo.size); |
| 2072 | Some( |
| 2073 | transform.transform_point_in(evt.position_transformed(size), &size.to_f64()) |
| 2074 | + output_geo.loc.to_f64(), |
| 2075 | ) |
| 2076 | } |
| 2077 | |
| 2078 | fn on_touch_down<I: InputBackend>(&mut self, evt: I::TouchDownEvent) { |
| 2079 | let Some(handle) = self.niri.seat.get_touch() else { |
| 2080 | return; |
| 2081 | }; |
| 2082 | let Some(touch_location) = self.compute_touch_location(&evt) else { |
| 2083 | return; |
| 2084 | }; |
| 2085 | |
| 2086 | let under = self.niri.surface_under_and_global_space(touch_location); |
| 2087 | |
| 2088 | if !handle.is_grabbed() { |
| 2089 | if let Some(window) = under.window { |
| 2090 | self.niri.layout.activate_window(&window); |
| 2091 | |
| 2092 | // FIXME: granular. |
| 2093 | self.niri.queue_redraw_all(); |
| 2094 | } else if let Some(output) = under.output { |
| 2095 | self.niri.layout.activate_output(&output); |
| 2096 | |
| 2097 | // FIXME: granular. |
| 2098 | self.niri.queue_redraw_all(); |
| 2099 | } |
| 2100 | self.niri.focus_layer_surface_if_on_demand(under.layer); |
| 2101 | }; |
| 2102 | |
| 2103 | let serial = SERIAL_COUNTER.next_serial(); |
| 2104 | handle.down( |
| 2105 | self, |
| 2106 | under.surface, |
| 2107 | &DownEvent { |
| 2108 | slot: evt.slot(), |
| 2109 | location: touch_location, |
| 2110 | serial, |
| 2111 | time: evt.time_msec(), |
| 2112 | }, |
| 2113 | ); |
| 2114 | |
| 2115 | // We're using touch, hide the pointer. |
| 2116 | self.niri.pointer_hidden = true; |
| 2117 | } |
| 2118 | fn on_touch_up<I: InputBackend>(&mut self, evt: I::TouchUpEvent) { |
| 2119 | let Some(handle) = self.niri.seat.get_touch() else { |
| 2120 | return; |
| 2121 | }; |
| 2122 | let serial = SERIAL_COUNTER.next_serial(); |
| 2123 | handle.up( |
| 2124 | self, |
| 2125 | &UpEvent { |
| 2126 | slot: evt.slot(), |
| 2127 | serial, |
| 2128 | time: evt.time_msec(), |
| 2129 | }, |
| 2130 | ) |
| 2131 | } |
| 2132 | fn on_touch_motion<I: InputBackend>(&mut self, evt: I::TouchMotionEvent) { |
| 2133 | let Some(handle) = self.niri.seat.get_touch() else { |
| 2134 | return; |
| 2135 | }; |
| 2136 | let Some(touch_location) = self.compute_touch_location(&evt) else { |
| 2137 | return; |
| 2138 | }; |
| 2139 | let under = self.niri.surface_under_and_global_space(touch_location); |
| 2140 | handle.motion( |
| 2141 | self, |
| 2142 | under.surface, |
| 2143 | &TouchMotionEvent { |
| 2144 | slot: evt.slot(), |
| 2145 | location: touch_location, |
| 2146 | time: evt.time_msec(), |
| 2147 | }, |
| 2148 | ); |
| 2149 | } |
| 2150 | fn on_touch_frame<I: InputBackend>(&mut self, _evt: I::TouchFrameEvent) { |
| 2151 | let Some(handle) = self.niri.seat.get_touch() else { |
| 2152 | return; |
| 2153 | }; |
| 2154 | handle.frame(self); |
| 2155 | } |
| 2156 | fn on_touch_cancel<I: InputBackend>(&mut self, _evt: I::TouchCancelEvent) { |
| 2157 | let Some(handle) = self.niri.seat.get_touch() else { |
| 2158 | return; |
| 2159 | }; |
| 2160 | handle.cancel(self); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | /// Check whether the key should be intercepted and mark intercepted |
| 2165 | /// pressed keys as `suppressed`, thus preventing `releases` corresponding |
| 2166 | /// to them from being delivered. |
| 2167 | #[allow(clippy::too_many_arguments)] |
| 2168 | fn should_intercept_key( |
| 2169 | suppressed_keys: &mut HashSet<u32>, |
| 2170 | bindings: &Binds, |
| 2171 | comp_mod: CompositorMod, |
| 2172 | key_code: u32, |
| 2173 | modified: Keysym, |
| 2174 | raw: Option<Keysym>, |
| 2175 | pressed: bool, |
| 2176 | mods: ModifiersState, |
| 2177 | screenshot_ui: &ScreenshotUi, |
| 2178 | disable_power_key_handling: bool, |
| 2179 | ) -> FilterResult<Option<Bind>> { |
| 2180 | // Actions are only triggered on presses, release of the key |
| 2181 | // shouldn't try to intercept anything unless we have marked |
| 2182 | // the key to suppress. |
| 2183 | if !pressed && !suppressed_keys.contains(&key_code) { |
| 2184 | return FilterResult::Forward; |
| 2185 | } |
| 2186 | |
| 2187 | let mut final_bind = find_bind( |
| 2188 | bindings, |
| 2189 | comp_mod, |
| 2190 | modified, |
| 2191 | raw, |
| 2192 | mods, |
| 2193 | disable_power_key_handling, |
| 2194 | ); |
| 2195 | |
| 2196 | // Allow only a subset of compositor actions while the screenshot UI is open, since the user |
| 2197 | // cannot see the screen. |
| 2198 | if screenshot_ui.is_open() { |
| 2199 | let mut use_screenshot_ui_action = true; |
| 2200 | |
| 2201 | if let Some(bind) = &final_bind { |
| 2202 | if allowed_during_screenshot(&bind.action) { |
| 2203 | use_screenshot_ui_action = false; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | if use_screenshot_ui_action { |
| 2208 | if let Some(raw) = raw { |
| 2209 | final_bind = screenshot_ui.action(raw, mods).map(|action| Bind { |
| 2210 | key: Key { |
| 2211 | trigger: Trigger::Keysym(raw), |
| 2212 | // Not entirely correct but it doesn't matter in how we currently use it. |
| 2213 | modifiers: Modifiers::empty(), |
| 2214 | }, |
| 2215 | action, |
| 2216 | repeat: true, |
| 2217 | cooldown: None, |
| 2218 | allow_when_locked: false, |
| 2219 | }); |
| 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | match (final_bind, pressed) { |
| 2225 | (Some(bind), true) => { |
| 2226 | suppressed_keys.insert(key_code); |
| 2227 | FilterResult::Intercept(Some(bind)) |
| 2228 | } |
| 2229 | (_, false) => { |
| 2230 | suppressed_keys.remove(&key_code); |
| 2231 | FilterResult::Intercept(None) |
| 2232 | } |
| 2233 | (None, true) => FilterResult::Forward, |
| 2234 | } |
| 2235 | } |
| 2236 | |
| 2237 | fn find_bind( |
| 2238 | bindings: &Binds, |
| 2239 | comp_mod: CompositorMod, |
| 2240 | modified: Keysym, |
| 2241 | raw: Option<Keysym>, |
| 2242 | mods: ModifiersState, |
| 2243 | disable_power_key_handling: bool, |
| 2244 | ) -> Option<Bind> { |
| 2245 | use keysyms::*; |
| 2246 | |
| 2247 | // Handle hardcoded binds. |
| 2248 | #[allow(non_upper_case_globals)] // wat |
| 2249 | let hardcoded_action = match modified.raw() { |
| 2250 | modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => { |
| 2251 | let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; |
| 2252 | Some(Action::ChangeVt(vt)) |
| 2253 | } |
| 2254 | KEY_XF86PowerOff if !disable_power_key_handling => Some(Action::Suspend), |
| 2255 | _ => None, |
| 2256 | }; |
| 2257 | |
| 2258 | if let Some(action) = hardcoded_action { |
| 2259 | return Some(Bind { |
| 2260 | key: Key { |
| 2261 | // Not entirely correct but it doesn't matter in how we currently use it. |
| 2262 | trigger: Trigger::Keysym(modified), |
| 2263 | modifiers: Modifiers::empty(), |
| 2264 | }, |
| 2265 | action, |
| 2266 | repeat: true, |
| 2267 | cooldown: None, |
| 2268 | allow_when_locked: false, |
| 2269 | }); |
| 2270 | } |
| 2271 | |
| 2272 | let trigger = Trigger::Keysym(raw?); |
| 2273 | find_configured_bind(bindings, comp_mod, trigger, mods) |
| 2274 | } |
| 2275 | |
| 2276 | fn find_configured_bind( |
| 2277 | bindings: &Binds, |
| 2278 | comp_mod: CompositorMod, |
| 2279 | trigger: Trigger, |
| 2280 | mods: ModifiersState, |
| 2281 | ) -> Option<Bind> { |
| 2282 | // Handle configured binds. |
| 2283 | let mut modifiers = modifiers_from_state(mods); |
| 2284 | |
| 2285 | let (mod_down, comp_mod) = match comp_mod { |
| 2286 | CompositorMod::Super => (mods.logo, Modifiers::SUPER), |
| 2287 | CompositorMod::Alt => (mods.alt, Modifiers::ALT), |
| 2288 | }; |
| 2289 | if mod_down { |
| 2290 | modifiers |= Modifiers::COMPOSITOR; |
| 2291 | } |
| 2292 | |
| 2293 | for bind in &bindings.0 { |
| 2294 | if bind.key.trigger != trigger { |
| 2295 | continue; |
| 2296 | } |
| 2297 | |
| 2298 | let mut bind_modifiers = bind.key.modifiers; |
| 2299 | if bind_modifiers.contains(Modifiers::COMPOSITOR) { |
| 2300 | bind_modifiers |= comp_mod; |
| 2301 | } else if bind_modifiers.contains(comp_mod) { |
| 2302 | bind_modifiers |= Modifiers::COMPOSITOR; |
| 2303 | } |
| 2304 | |
| 2305 | if bind_modifiers == modifiers { |
| 2306 | return Some(bind.clone()); |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | None |
| 2311 | } |
| 2312 | |
| 2313 | fn modifiers_from_state(mods: ModifiersState) -> Modifiers { |
| 2314 | let mut modifiers = Modifiers::empty(); |
| 2315 | if mods.ctrl { |
| 2316 | modifiers |= Modifiers::CTRL; |
| 2317 | } |
| 2318 | if mods.shift { |
| 2319 | modifiers |= Modifiers::SHIFT; |
| 2320 | } |
| 2321 | if mods.alt { |
| 2322 | modifiers |= Modifiers::ALT; |
| 2323 | } |
| 2324 | if mods.logo { |
| 2325 | modifiers |= Modifiers::SUPER; |
| 2326 | } |
| 2327 | if mods.iso_level3_shift { |
| 2328 | modifiers |= Modifiers::ISO_LEVEL3_SHIFT; |
| 2329 | } |
| 2330 | if mods.iso_level5_shift { |
| 2331 | modifiers |= Modifiers::ISO_LEVEL5_SHIFT; |
| 2332 | } |
| 2333 | modifiers |
| 2334 | } |
| 2335 | |
| 2336 | fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool { |
| 2337 | match event { |
| 2338 | InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, |
| 2339 | InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true, |
| 2340 | InputEvent::PointerMotion { .. } |
| 2341 | | InputEvent::PointerMotionAbsolute { .. } |
| 2342 | | InputEvent::PointerAxis { .. } |
| 2343 | | InputEvent::GestureSwipeBegin { .. } |
| 2344 | | InputEvent::GesturePinchBegin { .. } |
| 2345 | | InputEvent::GestureHoldBegin { .. } |
| 2346 | | InputEvent::TouchDown { .. } |
| 2347 | | InputEvent::TouchMotion { .. } |
| 2348 | | InputEvent::TabletToolAxis { .. } |
| 2349 | | InputEvent::TabletToolProximity { .. } |
| 2350 | | InputEvent::TabletToolTip { .. } |
| 2351 | | InputEvent::TabletToolButton { .. } => true, |
| 2352 | // Ignore events like device additions and removals, key releases, gesture ends. |
| 2353 | _ => false, |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | fn should_hide_hotkey_overlay<I: InputBackend>(event: &InputEvent<I>) -> bool { |
| 2358 | match event { |
| 2359 | InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, |
| 2360 | InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true, |
| 2361 | InputEvent::PointerAxis { .. } |
| 2362 | | InputEvent::GestureSwipeBegin { .. } |
| 2363 | | InputEvent::GesturePinchBegin { .. } |
| 2364 | | InputEvent::TouchDown { .. } |
| 2365 | | InputEvent::TouchMotion { .. } |
| 2366 | | InputEvent::TabletToolTip { .. } |
| 2367 | | InputEvent::TabletToolButton { .. } => true, |
| 2368 | _ => false, |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | fn should_hide_exit_confirm_dialog<I: InputBackend>(event: &InputEvent<I>) -> bool { |
| 2373 | match event { |
| 2374 | InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, |
| 2375 | InputEvent::PointerButton { event } if event.state() == ButtonState::Pressed => true, |
| 2376 | InputEvent::PointerAxis { .. } |
| 2377 | | InputEvent::GestureSwipeBegin { .. } |
| 2378 | | InputEvent::GesturePinchBegin { .. } |
| 2379 | | InputEvent::TouchDown { .. } |
| 2380 | | InputEvent::TouchMotion { .. } |
| 2381 | | InputEvent::TabletToolTip { .. } |
| 2382 | | InputEvent::TabletToolButton { .. } => true, |
| 2383 | _ => false, |
| 2384 | } |
| 2385 | } |
| 2386 | |
| 2387 | fn should_notify_activity<I: InputBackend>(event: &InputEvent<I>) -> bool { |
| 2388 | !matches!( |
| 2389 | event, |
| 2390 | InputEvent::DeviceAdded { .. } | InputEvent::DeviceRemoved { .. } |
| 2391 | ) |
| 2392 | } |
| 2393 | |
| 2394 | fn allowed_when_locked(action: &Action) -> bool { |
| 2395 | matches!( |
| 2396 | action, |
| 2397 | Action::Quit(_) |
| 2398 | | Action::ChangeVt(_) |
| 2399 | | Action::Suspend |
| 2400 | | Action::PowerOffMonitors |
| 2401 | | Action::SwitchLayout(_) |
| 2402 | ) |
| 2403 | } |
| 2404 | |
| 2405 | fn allowed_during_screenshot(action: &Action) -> bool { |
| 2406 | matches!( |
| 2407 | action, |
| 2408 | Action::Quit(_) | Action::ChangeVt(_) | Action::Suspend | Action::PowerOffMonitors |
| 2409 | ) |
| 2410 | } |
| 2411 | |
| 2412 | pub fn apply_libinput_settings(config: &niri_config::Input, device: &mut input::Device) { |
| 2413 | // According to Mutter code, this setting is specific to touchpads. |
| 2414 | let is_touchpad = device.config_tap_finger_count() > 0; |
| 2415 | if is_touchpad { |
| 2416 | let c = &config.touchpad; |
| 2417 | let _ = device.config_send_events_set_mode(if c.off { |
| 2418 | input::SendEventsMode::DISABLED |
| 2419 | } else if c.disabled_on_external_mouse { |
| 2420 | input::SendEventsMode::DISABLED_ON_EXTERNAL_MOUSE |
| 2421 | } else { |
| 2422 | input::SendEventsMode::ENABLED |
| 2423 | }); |
| 2424 | let _ = device.config_tap_set_enabled(c.tap); |
| 2425 | let _ = device.config_dwt_set_enabled(c.dwt); |
| 2426 | let _ = device.config_dwtp_set_enabled(c.dwtp); |
| 2427 | let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); |
| 2428 | let _ = device.config_accel_set_speed(c.accel_speed); |
| 2429 | let _ = device.config_left_handed_set(c.left_handed); |
| 2430 | let _ = device.config_middle_emulation_set_enabled(c.middle_emulation); |
| 2431 | |
| 2432 | if let Some(accel_profile) = c.accel_profile { |
| 2433 | let _ = device.config_accel_set_profile(accel_profile.into()); |
| 2434 | } else if let Some(default) = device.config_accel_default_profile() { |
| 2435 | let _ = device.config_accel_set_profile(default); |
| 2436 | } |
| 2437 | |
| 2438 | if let Some(method) = c.scroll_method { |
| 2439 | let _ = device.config_scroll_set_method(method.into()); |
| 2440 | } else if let Some(default) = device.config_scroll_default_method() { |
| 2441 | let _ = device.config_scroll_set_method(default); |
| 2442 | } |
| 2443 | |
| 2444 | if let Some(tap_button_map) = c.tap_button_map { |
| 2445 | let _ = device.config_tap_set_button_map(tap_button_map.into()); |
| 2446 | } else if let Some(default) = device.config_tap_default_button_map() { |
| 2447 | let _ = device.config_tap_set_button_map(default); |
| 2448 | } |
| 2449 | |
| 2450 | if let Some(method) = c.click_method { |
| 2451 | let _ = device.config_click_set_method(method.into()); |
| 2452 | } else if let Some(default) = device.config_click_default_method() { |
| 2453 | let _ = device.config_click_set_method(default); |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | // This is how Mutter tells apart mice. |
| 2458 | let mut is_trackball = false; |
| 2459 | let mut is_trackpoint = false; |
| 2460 | if let Some(udev_device) = unsafe { device.udev_device() } { |
| 2461 | if udev_device.property_value("ID_INPUT_TRACKBALL").is_some() { |
| 2462 | is_trackball = true; |
| 2463 | } |
| 2464 | if udev_device |
| 2465 | .property_value("ID_INPUT_POINTINGSTICK") |
| 2466 | .is_some() |
| 2467 | { |
| 2468 | is_trackpoint = true; |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | let is_mouse = device.has_capability(input::DeviceCapability::Pointer) |
| 2473 | && !is_touchpad |
| 2474 | && !is_trackball |
| 2475 | && !is_trackpoint; |
| 2476 | if is_mouse { |
| 2477 | let c = &config.mouse; |
| 2478 | let _ = device.config_send_events_set_mode(if c.off { |
| 2479 | input::SendEventsMode::DISABLED |
| 2480 | } else { |
| 2481 | input::SendEventsMode::ENABLED |
| 2482 | }); |
| 2483 | let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); |
| 2484 | let _ = device.config_accel_set_speed(c.accel_speed); |
| 2485 | let _ = device.config_left_handed_set(c.left_handed); |
| 2486 | let _ = device.config_middle_emulation_set_enabled(c.middle_emulation); |
| 2487 | |
| 2488 | if let Some(accel_profile) = c.accel_profile { |
| 2489 | let _ = device.config_accel_set_profile(accel_profile.into()); |
| 2490 | } else if let Some(default) = device.config_accel_default_profile() { |
| 2491 | let _ = device.config_accel_set_profile(default); |
| 2492 | } |
| 2493 | |
| 2494 | if let Some(method) = c.scroll_method { |
| 2495 | let _ = device.config_scroll_set_method(method.into()); |
| 2496 | } else if let Some(default) = device.config_scroll_default_method() { |
| 2497 | let _ = device.config_scroll_set_method(default); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | if is_trackpoint { |
| 2502 | let c = &config.trackpoint; |
| 2503 | let _ = device.config_send_events_set_mode(if c.off { |
| 2504 | input::SendEventsMode::DISABLED |
| 2505 | } else { |
| 2506 | input::SendEventsMode::ENABLED |
| 2507 | }); |
| 2508 | let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); |
| 2509 | let _ = device.config_accel_set_speed(c.accel_speed); |
| 2510 | let _ = device.config_middle_emulation_set_enabled(c.middle_emulation); |
| 2511 | |
| 2512 | if let Some(accel_profile) = c.accel_profile { |
| 2513 | let _ = device.config_accel_set_profile(accel_profile.into()); |
| 2514 | } else if let Some(default) = device.config_accel_default_profile() { |
| 2515 | let _ = device.config_accel_set_profile(default); |
| 2516 | } |
| 2517 | |
| 2518 | if let Some(method) = c.scroll_method { |
| 2519 | let _ = device.config_scroll_set_method(method.into()); |
| 2520 | } else if let Some(default) = device.config_scroll_default_method() { |
| 2521 | let _ = device.config_scroll_set_method(default); |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | let is_tablet = device.has_capability(input::DeviceCapability::TabletTool); |
| 2526 | if is_tablet { |
| 2527 | let c = &config.tablet; |
| 2528 | let _ = device.config_send_events_set_mode(if c.off { |
| 2529 | input::SendEventsMode::DISABLED |
| 2530 | } else { |
| 2531 | input::SendEventsMode::ENABLED |
| 2532 | }); |
| 2533 | let _ = device.config_left_handed_set(c.left_handed); |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | pub fn mods_with_binds( |
| 2538 | comp_mod: CompositorMod, |
| 2539 | binds: &Binds, |
| 2540 | triggers: &[Trigger], |
| 2541 | ) -> HashSet<Modifiers> { |
| 2542 | let comp_mod = match comp_mod { |
| 2543 | CompositorMod::Super => Modifiers::SUPER, |
| 2544 | CompositorMod::Alt => Modifiers::ALT, |
| 2545 | }; |
| 2546 | |
| 2547 | let mut rv = HashSet::new(); |
| 2548 | for bind in &binds.0 { |
| 2549 | if !triggers.iter().any(|trigger| bind.key.trigger == *trigger) { |
| 2550 | continue; |
| 2551 | } |
| 2552 | |
| 2553 | let mut mods = bind.key.modifiers; |
| 2554 | if mods.contains(Modifiers::COMPOSITOR) { |
| 2555 | mods.remove(Modifiers::COMPOSITOR); |
| 2556 | mods.insert(comp_mod); |
| 2557 | } |
| 2558 | |
| 2559 | rv.insert(mods); |
| 2560 | } |
| 2561 | |
| 2562 | rv |
| 2563 | } |
| 2564 | |
| 2565 | pub fn mods_with_wheel_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { |
| 2566 | mods_with_binds( |
| 2567 | comp_mod, |
| 2568 | binds, |
| 2569 | &[ |
| 2570 | Trigger::WheelScrollUp, |
| 2571 | Trigger::WheelScrollDown, |
| 2572 | Trigger::WheelScrollLeft, |
| 2573 | Trigger::WheelScrollRight, |
| 2574 | ], |
| 2575 | ) |
| 2576 | } |
| 2577 | |
| 2578 | pub fn mods_with_finger_scroll_binds(comp_mod: CompositorMod, binds: &Binds) -> HashSet<Modifiers> { |
| 2579 | mods_with_binds( |
| 2580 | comp_mod, |
| 2581 | binds, |
| 2582 | &[ |
| 2583 | Trigger::TouchpadScrollUp, |
| 2584 | Trigger::TouchpadScrollDown, |
| 2585 | Trigger::TouchpadScrollLeft, |
| 2586 | Trigger::TouchpadScrollRight, |
| 2587 | ], |
| 2588 | ) |
| 2589 | } |
| 2590 | |
| 2591 | #[cfg(test)] |
| 2592 | mod tests { |
| 2593 | use super::*; |
| 2594 | |
| 2595 | #[test] |
| 2596 | fn bindings_suppress_keys() { |
| 2597 | let close_keysym = Keysym::q; |
| 2598 | let bindings = Binds(vec![Bind { |
| 2599 | key: Key { |
| 2600 | trigger: Trigger::Keysym(close_keysym), |
| 2601 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, |
| 2602 | }, |
| 2603 | action: Action::CloseWindow, |
| 2604 | repeat: true, |
| 2605 | cooldown: None, |
| 2606 | allow_when_locked: false, |
| 2607 | }]); |
| 2608 | |
| 2609 | let comp_mod = CompositorMod::Super; |
| 2610 | let mut suppressed_keys = HashSet::new(); |
| 2611 | |
| 2612 | let screenshot_ui = ScreenshotUi::new(Default::default()); |
| 2613 | let disable_power_key_handling = false; |
| 2614 | |
| 2615 | // The key_code we pick is arbitrary, the only thing |
| 2616 | // that matters is that they are different between cases. |
| 2617 | |
| 2618 | let close_key_code = close_keysym.into(); |
| 2619 | let close_key_event = |suppr: &mut HashSet<u32>, mods: ModifiersState, pressed| { |
| 2620 | should_intercept_key( |
| 2621 | suppr, |
| 2622 | &bindings, |
| 2623 | comp_mod, |
| 2624 | close_key_code, |
| 2625 | close_keysym, |
| 2626 | Some(close_keysym), |
| 2627 | pressed, |
| 2628 | mods, |
| 2629 | &screenshot_ui, |
| 2630 | disable_power_key_handling, |
| 2631 | ) |
| 2632 | }; |
| 2633 | |
| 2634 | // Key event with the code which can't trigger any action. |
| 2635 | let none_key_event = |suppr: &mut HashSet<u32>, mods: ModifiersState, pressed| { |
| 2636 | should_intercept_key( |
| 2637 | suppr, |
| 2638 | &bindings, |
| 2639 | comp_mod, |
| 2640 | Keysym::l.into(), |
| 2641 | Keysym::l, |
| 2642 | Some(Keysym::l), |
| 2643 | pressed, |
| 2644 | mods, |
| 2645 | &screenshot_ui, |
| 2646 | disable_power_key_handling, |
| 2647 | ) |
| 2648 | }; |
| 2649 | |
| 2650 | let mut mods = ModifiersState { |
| 2651 | logo: true, |
| 2652 | ctrl: true, |
| 2653 | ..Default::default() |
| 2654 | }; |
| 2655 | |
| 2656 | // Action press/release. |
| 2657 | |
| 2658 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2659 | assert!(matches!( |
| 2660 | filter, |
| 2661 | FilterResult::Intercept(Some(Bind { |
| 2662 | action: Action::CloseWindow, |
| 2663 | .. |
| 2664 | })) |
| 2665 | )); |
| 2666 | assert!(suppressed_keys.contains(&close_key_code)); |
| 2667 | |
| 2668 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 2669 | assert!(matches!(filter, FilterResult::Intercept(None))); |
| 2670 | assert!(suppressed_keys.is_empty()); |
| 2671 | |
| 2672 | // Remove mod to make it for a binding. |
| 2673 | |
| 2674 | mods.shift = true; |
| 2675 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2676 | assert!(matches!(filter, FilterResult::Forward)); |
| 2677 | |
| 2678 | mods.shift = false; |
| 2679 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 2680 | assert!(matches!(filter, FilterResult::Forward)); |
| 2681 | |
| 2682 | // Just none press/release. |
| 2683 | |
| 2684 | let filter = none_key_event(&mut suppressed_keys, mods, true); |
| 2685 | assert!(matches!(filter, FilterResult::Forward)); |
| 2686 | |
| 2687 | let filter = none_key_event(&mut suppressed_keys, mods, false); |
| 2688 | assert!(matches!(filter, FilterResult::Forward)); |
| 2689 | |
| 2690 | // Press action, press arbitrary, release action, release arbitrary. |
| 2691 | |
| 2692 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2693 | assert!(matches!( |
| 2694 | filter, |
| 2695 | FilterResult::Intercept(Some(Bind { |
| 2696 | action: Action::CloseWindow, |
| 2697 | .. |
| 2698 | })) |
| 2699 | )); |
| 2700 | |
| 2701 | let filter = none_key_event(&mut suppressed_keys, mods, true); |
| 2702 | assert!(matches!(filter, FilterResult::Forward)); |
| 2703 | |
| 2704 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 2705 | assert!(matches!(filter, FilterResult::Intercept(None))); |
| 2706 | |
| 2707 | let filter = none_key_event(&mut suppressed_keys, mods, false); |
| 2708 | assert!(matches!(filter, FilterResult::Forward)); |
| 2709 | |
| 2710 | // Trigger and remove all mods. |
| 2711 | |
| 2712 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 2713 | assert!(matches!( |
| 2714 | filter, |
| 2715 | FilterResult::Intercept(Some(Bind { |
| 2716 | action: Action::CloseWindow, |
| 2717 | .. |
| 2718 | })) |
| 2719 | )); |
| 2720 | |
| 2721 | mods = Default::default(); |
| 2722 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 2723 | assert!(matches!(filter, FilterResult::Intercept(None))); |
| 2724 | |
| 2725 | // Ensure that no keys are being suppressed. |
| 2726 | assert!(suppressed_keys.is_empty()); |
| 2727 | } |
| 2728 | |
| 2729 | #[test] |
| 2730 | fn comp_mod_handling() { |
| 2731 | let bindings = Binds(vec![ |
| 2732 | Bind { |
| 2733 | key: Key { |
| 2734 | trigger: Trigger::Keysym(Keysym::q), |
| 2735 | modifiers: Modifiers::COMPOSITOR, |
| 2736 | }, |
| 2737 | action: Action::CloseWindow, |
| 2738 | repeat: true, |
| 2739 | cooldown: None, |
| 2740 | allow_when_locked: false, |
| 2741 | }, |
| 2742 | Bind { |
| 2743 | key: Key { |
| 2744 | trigger: Trigger::Keysym(Keysym::h), |
| 2745 | modifiers: Modifiers::SUPER, |
| 2746 | }, |
| 2747 | action: Action::FocusColumnLeft, |
| 2748 | repeat: true, |
| 2749 | cooldown: None, |
| 2750 | allow_when_locked: false, |
| 2751 | }, |
| 2752 | Bind { |
| 2753 | key: Key { |
| 2754 | trigger: Trigger::Keysym(Keysym::j), |
| 2755 | modifiers: Modifiers::empty(), |
| 2756 | }, |
| 2757 | action: Action::FocusWindowDown, |
| 2758 | repeat: true, |
| 2759 | cooldown: None, |
| 2760 | allow_when_locked: false, |
| 2761 | }, |
| 2762 | Bind { |
| 2763 | key: Key { |
| 2764 | trigger: Trigger::Keysym(Keysym::k), |
| 2765 | modifiers: Modifiers::COMPOSITOR | Modifiers::SUPER, |
| 2766 | }, |
| 2767 | action: Action::FocusWindowUp, |
| 2768 | repeat: true, |
| 2769 | cooldown: None, |
| 2770 | allow_when_locked: false, |
| 2771 | }, |
| 2772 | Bind { |
| 2773 | key: Key { |
| 2774 | trigger: Trigger::Keysym(Keysym::l), |
| 2775 | modifiers: Modifiers::SUPER | Modifiers::ALT, |
| 2776 | }, |
| 2777 | action: Action::FocusColumnRight, |
| 2778 | repeat: true, |
| 2779 | cooldown: None, |
| 2780 | allow_when_locked: false, |
| 2781 | }, |
| 2782 | ]); |
| 2783 | |
| 2784 | assert_eq!( |
| 2785 | find_configured_bind( |
| 2786 | &bindings, |
| 2787 | CompositorMod::Super, |
| 2788 | Trigger::Keysym(Keysym::q), |
| 2789 | ModifiersState { |
| 2790 | logo: true, |
| 2791 | ..Default::default() |
| 2792 | } |
| 2793 | ) |
| 2794 | .as_ref(), |
| 2795 | Some(&bindings.0[0]) |
| 2796 | ); |
| 2797 | assert_eq!( |
| 2798 | find_configured_bind( |
| 2799 | &bindings, |
| 2800 | CompositorMod::Super, |
| 2801 | Trigger::Keysym(Keysym::q), |
| 2802 | ModifiersState::default(), |
| 2803 | ), |
| 2804 | None, |
| 2805 | ); |
| 2806 | |
| 2807 | assert_eq!( |
| 2808 | find_configured_bind( |
| 2809 | &bindings, |
| 2810 | CompositorMod::Super, |
| 2811 | Trigger::Keysym(Keysym::h), |
| 2812 | ModifiersState { |
| 2813 | logo: true, |
| 2814 | ..Default::default() |
| 2815 | } |
| 2816 | ) |
| 2817 | .as_ref(), |
| 2818 | Some(&bindings.0[1]) |
| 2819 | ); |
| 2820 | assert_eq!( |
| 2821 | find_configured_bind( |
| 2822 | &bindings, |
| 2823 | CompositorMod::Super, |
| 2824 | Trigger::Keysym(Keysym::h), |
| 2825 | ModifiersState::default(), |
| 2826 | ), |
| 2827 | None, |
| 2828 | ); |
| 2829 | |
| 2830 | assert_eq!( |
| 2831 | find_configured_bind( |
| 2832 | &bindings, |
| 2833 | CompositorMod::Super, |
| 2834 | Trigger::Keysym(Keysym::j), |
| 2835 | ModifiersState { |
| 2836 | logo: true, |
| 2837 | ..Default::default() |
| 2838 | } |
| 2839 | ), |
| 2840 | None, |
| 2841 | ); |
| 2842 | assert_eq!( |
| 2843 | find_configured_bind( |
| 2844 | &bindings, |
| 2845 | CompositorMod::Super, |
| 2846 | Trigger::Keysym(Keysym::j), |
| 2847 | ModifiersState::default(), |
| 2848 | ) |
| 2849 | .as_ref(), |
| 2850 | Some(&bindings.0[2]) |
| 2851 | ); |
| 2852 | |
| 2853 | assert_eq!( |
| 2854 | find_configured_bind( |
| 2855 | &bindings, |
| 2856 | CompositorMod::Super, |
| 2857 | Trigger::Keysym(Keysym::k), |
| 2858 | ModifiersState { |
| 2859 | logo: true, |
| 2860 | ..Default::default() |
| 2861 | } |
| 2862 | ) |
| 2863 | .as_ref(), |
| 2864 | Some(&bindings.0[3]) |
| 2865 | ); |
| 2866 | assert_eq!( |
| 2867 | find_configured_bind( |
| 2868 | &bindings, |
| 2869 | CompositorMod::Super, |
| 2870 | Trigger::Keysym(Keysym::k), |
| 2871 | ModifiersState::default(), |
| 2872 | ), |
| 2873 | None, |
| 2874 | ); |
| 2875 | |
| 2876 | assert_eq!( |
| 2877 | find_configured_bind( |
| 2878 | &bindings, |
| 2879 | CompositorMod::Super, |
| 2880 | Trigger::Keysym(Keysym::l), |
| 2881 | ModifiersState { |
| 2882 | logo: true, |
| 2883 | alt: true, |
| 2884 | ..Default::default() |
| 2885 | } |
| 2886 | ) |
| 2887 | .as_ref(), |
| 2888 | Some(&bindings.0[4]) |
| 2889 | ); |
| 2890 | assert_eq!( |
| 2891 | find_configured_bind( |
| 2892 | &bindings, |
| 2893 | CompositorMod::Super, |
| 2894 | Trigger::Keysym(Keysym::l), |
| 2895 | ModifiersState { |
| 2896 | logo: true, |
| 2897 | ..Default::default() |
| 2898 | }, |
| 2899 | ), |
| 2900 | None, |
| 2901 | ); |
| 2902 | } |
| 2903 | } |