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