niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·be6e25f·Ivan Molodetskikh·2 years ago·468 commits
| 1 | use std::any::Any; |
| 2 | use std::collections::HashSet; |
| 3 | |
| 4 | use smithay::backend::input::{ |
| 5 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, |
| 6 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, |
| 7 | InputBackend, InputEvent, KeyState, KeyboardKeyEvent, PointerAxisEvent, PointerButtonEvent, |
| 8 | PointerMotionEvent, ProximityState, TabletToolButtonEvent, TabletToolEvent, |
| 9 | TabletToolProximityEvent, TabletToolTipEvent, TabletToolTipState, |
| 10 | }; |
| 11 | use smithay::backend::libinput::LibinputInputBackend; |
| 12 | use smithay::input::keyboard::{keysyms, FilterResult, Keysym, ModifiersState}; |
| 13 | use smithay::input::pointer::{ |
| 14 | AxisFrame, ButtonEvent, CursorImageStatus, GestureHoldBeginEvent, GestureHoldEndEvent, |
| 15 | GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent, GestureSwipeBeginEvent, |
| 16 | GestureSwipeEndEvent, GestureSwipeUpdateEvent, MotionEvent, RelativeMotionEvent, |
| 17 | }; |
| 18 | use smithay::reexports::input; |
| 19 | use smithay::utils::{Logical, Point, SERIAL_COUNTER}; |
| 20 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; |
| 21 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| 22 | |
| 23 | use crate::config::{Action, Binds, LayoutAction, Modifiers}; |
| 24 | use crate::niri::State; |
| 25 | use crate::screenshot_ui::ScreenshotUi; |
| 26 | use crate::utils::{center, get_monotonic_time, spawn}; |
| 27 | |
| 28 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 29 | pub enum CompositorMod { |
| 30 | Super, |
| 31 | Alt, |
| 32 | } |
| 33 | |
| 34 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 35 | pub struct TabletData { |
| 36 | pub aspect_ratio: f64, |
| 37 | } |
| 38 | |
| 39 | impl State { |
| 40 | pub fn process_input_event<I: InputBackend>(&mut self, event: InputEvent<I>) |
| 41 | where |
| 42 | I::Device: 'static, // Needed for downcasting. |
| 43 | { |
| 44 | let _span = tracy_client::span!("process_input_event"); |
| 45 | |
| 46 | // A bit of a hack, but animation end runs some logic (i.e. workspace clean-up) and it |
| 47 | // doesn't always trigger due to damage, etc. So run it here right before it might prove |
| 48 | // important. Besides, animations affect the input, so it's best to have up-to-date values |
| 49 | // here. |
| 50 | self.niri.layout.advance_animations(get_monotonic_time()); |
| 51 | |
| 52 | // Power on monitors if they were off. |
| 53 | if should_activate_monitors(&event) { |
| 54 | self.niri.activate_monitors(&self.backend); |
| 55 | } |
| 56 | |
| 57 | use InputEvent::*; |
| 58 | match event { |
| 59 | DeviceAdded { device } => self.on_device_added(device), |
| 60 | DeviceRemoved { device } => self.on_device_removed(device), |
| 61 | Keyboard { event } => self.on_keyboard::<I>(event), |
| 62 | PointerMotion { event } => self.on_pointer_motion::<I>(event), |
| 63 | PointerMotionAbsolute { event } => self.on_pointer_motion_absolute::<I>(event), |
| 64 | PointerButton { event } => self.on_pointer_button::<I>(event), |
| 65 | PointerAxis { event } => self.on_pointer_axis::<I>(event), |
| 66 | TabletToolAxis { event } => self.on_tablet_tool_axis::<I>(event), |
| 67 | TabletToolTip { event } => self.on_tablet_tool_tip::<I>(event), |
| 68 | TabletToolProximity { event } => self.on_tablet_tool_proximity::<I>(event), |
| 69 | TabletToolButton { event } => self.on_tablet_tool_button::<I>(event), |
| 70 | GestureSwipeBegin { event } => self.on_gesture_swipe_begin::<I>(event), |
| 71 | GestureSwipeUpdate { event } => self.on_gesture_swipe_update::<I>(event), |
| 72 | GestureSwipeEnd { event } => self.on_gesture_swipe_end::<I>(event), |
| 73 | GesturePinchBegin { event } => self.on_gesture_pinch_begin::<I>(event), |
| 74 | GesturePinchUpdate { event } => self.on_gesture_pinch_update::<I>(event), |
| 75 | GesturePinchEnd { event } => self.on_gesture_pinch_end::<I>(event), |
| 76 | GestureHoldBegin { event } => self.on_gesture_hold_begin::<I>(event), |
| 77 | GestureHoldEnd { event } => self.on_gesture_hold_end::<I>(event), |
| 78 | TouchDown { .. } => (), |
| 79 | TouchMotion { .. } => (), |
| 80 | TouchUp { .. } => (), |
| 81 | TouchCancel { .. } => (), |
| 82 | TouchFrame { .. } => (), |
| 83 | Special(_) => (), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn process_libinput_event(&mut self, event: &mut InputEvent<LibinputInputBackend>) { |
| 88 | let _span = tracy_client::span!("process_libinput_event"); |
| 89 | |
| 90 | match event { |
| 91 | InputEvent::DeviceAdded { device } => { |
| 92 | // According to Mutter code, this setting is specific to touchpads. |
| 93 | let is_touchpad = device.config_tap_finger_count() > 0; |
| 94 | if is_touchpad { |
| 95 | let c = &self.niri.config.borrow().input.touchpad; |
| 96 | let _ = device.config_tap_set_enabled(c.tap); |
| 97 | let _ = device.config_scroll_set_natural_scroll_enabled(c.natural_scroll); |
| 98 | let _ = device.config_accel_set_speed(c.accel_speed); |
| 99 | } |
| 100 | |
| 101 | if device.has_capability(input::DeviceCapability::TabletTool) { |
| 102 | match device.size() { |
| 103 | Some((w, h)) => { |
| 104 | let aspect_ratio = w / h; |
| 105 | let data = TabletData { aspect_ratio }; |
| 106 | self.niri.tablets.insert(device.clone(), data); |
| 107 | } |
| 108 | None => { |
| 109 | warn!("tablet tool device has no size"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | InputEvent::DeviceRemoved { device } => { |
| 115 | self.niri.tablets.remove(device); |
| 116 | } |
| 117 | _ => (), |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn on_device_added(&mut self, device: impl Device) { |
| 122 | if device.has_capability(DeviceCapability::TabletTool) { |
| 123 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 124 | |
| 125 | let desc = TabletDescriptor::from(&device); |
| 126 | tablet_seat.add_tablet::<Self>(&self.niri.display_handle, &desc); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | fn on_device_removed(&mut self, device: impl Device) { |
| 131 | if device.has_capability(DeviceCapability::TabletTool) { |
| 132 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 133 | |
| 134 | let desc = TabletDescriptor::from(&device); |
| 135 | tablet_seat.remove_tablet(&desc); |
| 136 | |
| 137 | // If there are no tablets in seat we can remove all tools |
| 138 | if tablet_seat.count_tablets() == 0 { |
| 139 | tablet_seat.clear_tools(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// Computes the cursor position for the tablet event. |
| 145 | /// |
| 146 | /// This function handles the tablet output mapping, as well as coordinate clamping and aspect |
| 147 | /// ratio correction. |
| 148 | fn compute_tablet_position<I: InputBackend>( |
| 149 | &self, |
| 150 | event: &(impl Event<I> + TabletToolEvent<I>), |
| 151 | ) -> Option<Point<f64, Logical>> |
| 152 | where |
| 153 | I::Device: 'static, |
| 154 | { |
| 155 | let output = self.niri.output_for_tablet()?; |
| 156 | let output_geo = self.niri.global_space.output_geometry(output).unwrap(); |
| 157 | |
| 158 | let mut pos = event.position_transformed(output_geo.size); |
| 159 | pos.x /= output_geo.size.w as f64; |
| 160 | pos.y /= output_geo.size.h as f64; |
| 161 | |
| 162 | let device = event.device(); |
| 163 | if let Some(device) = (&device as &dyn Any).downcast_ref::<input::Device>() { |
| 164 | if let Some(data) = self.niri.tablets.get(device) { |
| 165 | // This code does the same thing as mutter with "keep aspect ratio" enabled. |
| 166 | let output_aspect_ratio = output_geo.size.w as f64 / output_geo.size.h as f64; |
| 167 | let ratio = data.aspect_ratio / output_aspect_ratio; |
| 168 | |
| 169 | if ratio > 1. { |
| 170 | pos.x *= ratio; |
| 171 | } else { |
| 172 | pos.y /= ratio; |
| 173 | } |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | pos.x *= output_geo.size.w as f64; |
| 178 | pos.y *= output_geo.size.h as f64; |
| 179 | pos.x = pos.x.clamp(0.0, output_geo.size.w as f64 - 1.); |
| 180 | pos.y = pos.y.clamp(0.0, output_geo.size.h as f64 - 1.); |
| 181 | Some(pos + output_geo.loc.to_f64()) |
| 182 | } |
| 183 | |
| 184 | fn on_keyboard<I: InputBackend>(&mut self, event: I::KeyboardKeyEvent) { |
| 185 | let comp_mod = self.backend.mod_key(); |
| 186 | |
| 187 | let serial = SERIAL_COUNTER.next_serial(); |
| 188 | let time = Event::time_msec(&event); |
| 189 | let pressed = event.state() == KeyState::Pressed; |
| 190 | |
| 191 | let Some(Some(action)) = self.niri.seat.get_keyboard().unwrap().input( |
| 192 | self, |
| 193 | event.key_code(), |
| 194 | event.state(), |
| 195 | serial, |
| 196 | time, |
| 197 | |this, mods, keysym| { |
| 198 | let bindings = &this.niri.config.borrow().binds; |
| 199 | let key_code = event.key_code(); |
| 200 | let modified = keysym.modified_sym(); |
| 201 | let raw = keysym.raw_latin_sym_or_raw_current_sym(); |
| 202 | should_intercept_key( |
| 203 | &mut this.niri.suppressed_keys, |
| 204 | bindings, |
| 205 | comp_mod, |
| 206 | key_code, |
| 207 | modified, |
| 208 | raw, |
| 209 | pressed, |
| 210 | *mods, |
| 211 | &this.niri.screenshot_ui, |
| 212 | this.niri.config.borrow().input.disable_power_key_handling, |
| 213 | ) |
| 214 | }, |
| 215 | ) else { |
| 216 | return; |
| 217 | }; |
| 218 | |
| 219 | // Filter actions when the key is released or the session is locked. |
| 220 | if !pressed { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | if self.niri.is_locked() && !allowed_when_locked(&action) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | match action { |
| 229 | Action::Quit => { |
| 230 | info!("quitting because quit bind was pressed"); |
| 231 | self.niri.stop_signal.stop() |
| 232 | } |
| 233 | Action::ChangeVt(vt) => { |
| 234 | self.backend.change_vt(vt); |
| 235 | // Changing `VT` may not deliver the key releases, so clear the state. |
| 236 | self.niri.suppressed_keys.clear(); |
| 237 | } |
| 238 | Action::Suspend => { |
| 239 | self.backend.suspend(); |
| 240 | // Suspend may not deliver the key releases, so clear the state. |
| 241 | self.niri.suppressed_keys.clear(); |
| 242 | } |
| 243 | Action::PowerOffMonitors => { |
| 244 | self.niri.deactivate_monitors(&self.backend); |
| 245 | } |
| 246 | Action::ToggleDebugTint => { |
| 247 | self.backend.toggle_debug_tint(); |
| 248 | } |
| 249 | Action::Spawn(command) => { |
| 250 | spawn(command); |
| 251 | } |
| 252 | Action::ScreenshotScreen => { |
| 253 | let active = self.niri.layout.active_output().cloned(); |
| 254 | if let Some(active) = active { |
| 255 | if let Some(renderer) = self.backend.renderer() { |
| 256 | if let Err(err) = self.niri.screenshot(renderer, &active) { |
| 257 | warn!("error taking screenshot: {err:?}"); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | Action::ConfirmScreenshot => { |
| 263 | if let Some(renderer) = self.backend.renderer() { |
| 264 | match self.niri.screenshot_ui.capture(renderer) { |
| 265 | Ok((size, pixels)) => { |
| 266 | if let Err(err) = self.niri.save_screenshot(size, pixels) { |
| 267 | warn!("error saving screenshot: {err:?}"); |
| 268 | } |
| 269 | } |
| 270 | Err(err) => { |
| 271 | warn!("error capturing screenshot: {err:?}"); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | self.niri.screenshot_ui.close(); |
| 277 | self.niri |
| 278 | .cursor_manager |
| 279 | .set_cursor_image(CursorImageStatus::default_named()); |
| 280 | self.niri.queue_redraw_all(); |
| 281 | } |
| 282 | Action::CancelScreenshot => { |
| 283 | self.niri.screenshot_ui.close(); |
| 284 | self.niri |
| 285 | .cursor_manager |
| 286 | .set_cursor_image(CursorImageStatus::default_named()); |
| 287 | self.niri.queue_redraw_all(); |
| 288 | } |
| 289 | Action::Screenshot => { |
| 290 | if let Some(renderer) = self.backend.renderer() { |
| 291 | self.niri.open_screenshot_ui(renderer); |
| 292 | } |
| 293 | } |
| 294 | Action::ScreenshotWindow => { |
| 295 | let active = self.niri.layout.active_window(); |
| 296 | if let Some((window, output)) = active { |
| 297 | if let Some(renderer) = self.backend.renderer() { |
| 298 | if let Err(err) = self.niri.screenshot_window(renderer, output, window) { |
| 299 | warn!("error taking screenshot: {err:?}"); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | Action::CloseWindow => { |
| 305 | if let Some(window) = self.niri.layout.focus() { |
| 306 | window.toplevel().send_close(); |
| 307 | } |
| 308 | } |
| 309 | Action::FullscreenWindow => { |
| 310 | let focus = self.niri.layout.focus().cloned(); |
| 311 | if let Some(window) = focus { |
| 312 | self.niri.layout.toggle_fullscreen(&window); |
| 313 | } |
| 314 | } |
| 315 | Action::SwitchLayout(action) => { |
| 316 | self.niri.seat.get_keyboard().unwrap().with_xkb_state( |
| 317 | self, |
| 318 | |mut state| match action { |
| 319 | LayoutAction::Next => state.cycle_next_layout(), |
| 320 | LayoutAction::Prev => state.cycle_prev_layout(), |
| 321 | }, |
| 322 | ); |
| 323 | } |
| 324 | Action::MoveColumnLeft => { |
| 325 | self.niri.layout.move_left(); |
| 326 | // FIXME: granular |
| 327 | self.niri.queue_redraw_all(); |
| 328 | } |
| 329 | Action::MoveColumnRight => { |
| 330 | self.niri.layout.move_right(); |
| 331 | // FIXME: granular |
| 332 | self.niri.queue_redraw_all(); |
| 333 | } |
| 334 | Action::MoveColumnToFirst => { |
| 335 | self.niri.layout.move_column_to_first(); |
| 336 | // FIXME: granular |
| 337 | self.niri.queue_redraw_all(); |
| 338 | } |
| 339 | Action::MoveColumnToLast => { |
| 340 | self.niri.layout.move_column_to_last(); |
| 341 | // FIXME: granular |
| 342 | self.niri.queue_redraw_all(); |
| 343 | } |
| 344 | Action::MoveWindowDown => { |
| 345 | self.niri.layout.move_down(); |
| 346 | // FIXME: granular |
| 347 | self.niri.queue_redraw_all(); |
| 348 | } |
| 349 | Action::MoveWindowUp => { |
| 350 | self.niri.layout.move_up(); |
| 351 | // FIXME: granular |
| 352 | self.niri.queue_redraw_all(); |
| 353 | } |
| 354 | Action::MoveWindowDownOrToWorkspaceDown => { |
| 355 | self.niri.layout.move_down_or_to_workspace_down(); |
| 356 | // FIXME: granular |
| 357 | self.niri.queue_redraw_all(); |
| 358 | } |
| 359 | Action::MoveWindowUpOrToWorkspaceUp => { |
| 360 | self.niri.layout.move_up_or_to_workspace_up(); |
| 361 | // FIXME: granular |
| 362 | self.niri.queue_redraw_all(); |
| 363 | } |
| 364 | Action::FocusColumnLeft => { |
| 365 | self.niri.layout.focus_left(); |
| 366 | } |
| 367 | Action::FocusColumnRight => { |
| 368 | self.niri.layout.focus_right(); |
| 369 | } |
| 370 | Action::FocusColumnFirst => { |
| 371 | self.niri.layout.focus_column_first(); |
| 372 | } |
| 373 | Action::FocusColumnLast => { |
| 374 | self.niri.layout.focus_column_last(); |
| 375 | } |
| 376 | Action::FocusWindowDown => { |
| 377 | self.niri.layout.focus_down(); |
| 378 | } |
| 379 | Action::FocusWindowUp => { |
| 380 | self.niri.layout.focus_up(); |
| 381 | } |
| 382 | Action::FocusWindowOrWorkspaceDown => { |
| 383 | self.niri.layout.focus_window_or_workspace_down(); |
| 384 | // FIXME: granular |
| 385 | self.niri.queue_redraw_all(); |
| 386 | } |
| 387 | Action::FocusWindowOrWorkspaceUp => { |
| 388 | self.niri.layout.focus_window_or_workspace_up(); |
| 389 | // FIXME: granular |
| 390 | self.niri.queue_redraw_all(); |
| 391 | } |
| 392 | Action::MoveWindowToWorkspaceDown => { |
| 393 | self.niri.layout.move_to_workspace_down(); |
| 394 | // FIXME: granular |
| 395 | self.niri.queue_redraw_all(); |
| 396 | } |
| 397 | Action::MoveWindowToWorkspaceUp => { |
| 398 | self.niri.layout.move_to_workspace_up(); |
| 399 | // FIXME: granular |
| 400 | self.niri.queue_redraw_all(); |
| 401 | } |
| 402 | Action::MoveWindowToWorkspace(idx) => { |
| 403 | let idx = idx.saturating_sub(1) as usize; |
| 404 | self.niri.layout.move_to_workspace(idx); |
| 405 | // FIXME: granular |
| 406 | self.niri.queue_redraw_all(); |
| 407 | } |
| 408 | Action::FocusWorkspaceDown => { |
| 409 | self.niri.layout.switch_workspace_down(); |
| 410 | // FIXME: granular |
| 411 | self.niri.queue_redraw_all(); |
| 412 | } |
| 413 | Action::FocusWorkspaceUp => { |
| 414 | self.niri.layout.switch_workspace_up(); |
| 415 | // FIXME: granular |
| 416 | self.niri.queue_redraw_all(); |
| 417 | } |
| 418 | Action::FocusWorkspace(idx) => { |
| 419 | let idx = idx.saturating_sub(1) as usize; |
| 420 | self.niri.layout.switch_workspace(idx); |
| 421 | // FIXME: granular |
| 422 | self.niri.queue_redraw_all(); |
| 423 | } |
| 424 | Action::MoveWorkspaceDown => { |
| 425 | self.niri.layout.move_workspace_down(); |
| 426 | // FIXME: granular |
| 427 | self.niri.queue_redraw_all(); |
| 428 | } |
| 429 | Action::MoveWorkspaceUp => { |
| 430 | self.niri.layout.move_workspace_up(); |
| 431 | // FIXME: granular |
| 432 | self.niri.queue_redraw_all(); |
| 433 | } |
| 434 | Action::ConsumeWindowIntoColumn => { |
| 435 | self.niri.layout.consume_into_column(); |
| 436 | // FIXME: granular |
| 437 | self.niri.queue_redraw_all(); |
| 438 | } |
| 439 | Action::ExpelWindowFromColumn => { |
| 440 | self.niri.layout.expel_from_column(); |
| 441 | // FIXME: granular |
| 442 | self.niri.queue_redraw_all(); |
| 443 | } |
| 444 | Action::SwitchPresetColumnWidth => { |
| 445 | self.niri.layout.toggle_width(); |
| 446 | } |
| 447 | Action::CenterColumn => { |
| 448 | self.niri.layout.center_column(); |
| 449 | // FIXME: granular |
| 450 | self.niri.queue_redraw_all(); |
| 451 | } |
| 452 | Action::MaximizeColumn => { |
| 453 | self.niri.layout.toggle_full_width(); |
| 454 | } |
| 455 | Action::FocusMonitorLeft => { |
| 456 | if let Some(output) = self.niri.output_left() { |
| 457 | self.niri.layout.focus_output(&output); |
| 458 | self.move_cursor_to_output(&output); |
| 459 | } |
| 460 | } |
| 461 | Action::FocusMonitorRight => { |
| 462 | if let Some(output) = self.niri.output_right() { |
| 463 | self.niri.layout.focus_output(&output); |
| 464 | self.move_cursor_to_output(&output); |
| 465 | } |
| 466 | } |
| 467 | Action::FocusMonitorDown => { |
| 468 | if let Some(output) = self.niri.output_down() { |
| 469 | self.niri.layout.focus_output(&output); |
| 470 | self.move_cursor_to_output(&output); |
| 471 | } |
| 472 | } |
| 473 | Action::FocusMonitorUp => { |
| 474 | if let Some(output) = self.niri.output_up() { |
| 475 | self.niri.layout.focus_output(&output); |
| 476 | self.move_cursor_to_output(&output); |
| 477 | } |
| 478 | } |
| 479 | Action::MoveWindowToMonitorLeft => { |
| 480 | if let Some(output) = self.niri.output_left() { |
| 481 | self.niri.layout.move_to_output(&output); |
| 482 | self.move_cursor_to_output(&output); |
| 483 | } |
| 484 | } |
| 485 | Action::MoveWindowToMonitorRight => { |
| 486 | if let Some(output) = self.niri.output_right() { |
| 487 | self.niri.layout.move_to_output(&output); |
| 488 | self.move_cursor_to_output(&output); |
| 489 | } |
| 490 | } |
| 491 | Action::MoveWindowToMonitorDown => { |
| 492 | if let Some(output) = self.niri.output_down() { |
| 493 | self.niri.layout.move_to_output(&output); |
| 494 | self.move_cursor_to_output(&output); |
| 495 | } |
| 496 | } |
| 497 | Action::MoveWindowToMonitorUp => { |
| 498 | if let Some(output) = self.niri.output_up() { |
| 499 | self.niri.layout.move_to_output(&output); |
| 500 | self.move_cursor_to_output(&output); |
| 501 | } |
| 502 | } |
| 503 | Action::SetColumnWidth(change) => { |
| 504 | self.niri.layout.set_column_width(change); |
| 505 | } |
| 506 | Action::SetWindowHeight(change) => { |
| 507 | self.niri.layout.set_window_height(change); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | fn on_pointer_motion<I: InputBackend>(&mut self, event: I::PointerMotionEvent) { |
| 513 | // We need an output to be able to move the pointer. |
| 514 | if self.niri.global_space.outputs().next().is_none() { |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | let serial = SERIAL_COUNTER.next_serial(); |
| 519 | |
| 520 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 521 | |
| 522 | let pos = pointer.current_location(); |
| 523 | |
| 524 | // We have an output, so we can compute the new location and focus. |
| 525 | let mut new_pos = pos + event.delta(); |
| 526 | |
| 527 | // We received an event for the regular pointer, so show it now. |
| 528 | self.niri.tablet_cursor_location = None; |
| 529 | |
| 530 | // Check if we have an active pointer constraint. |
| 531 | let mut pointer_confined = None; |
| 532 | if let Some(focus) = self.niri.pointer_focus.as_ref() { |
| 533 | let focus_surface_loc = focus.surface.1; |
| 534 | let pos_within_surface = pos.to_i32_round() - focus_surface_loc; |
| 535 | |
| 536 | let mut pointer_locked = false; |
| 537 | with_pointer_constraint(&focus.surface.0, &pointer, |constraint| { |
| 538 | let Some(constraint) = constraint else { return }; |
| 539 | if !constraint.is_active() { |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | // Constraint does not apply if not within region. |
| 544 | if let Some(region) = constraint.region() { |
| 545 | if !region.contains(pos_within_surface) { |
| 546 | return; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | match &*constraint { |
| 551 | PointerConstraint::Locked(_locked) => { |
| 552 | pointer_locked = true; |
| 553 | } |
| 554 | PointerConstraint::Confined(confine) => { |
| 555 | pointer_confined = Some((focus.surface.clone(), confine.region().cloned())); |
| 556 | } |
| 557 | } |
| 558 | }); |
| 559 | |
| 560 | // If the pointer is locked, only send relative motion. |
| 561 | if pointer_locked { |
| 562 | pointer.relative_motion( |
| 563 | self, |
| 564 | Some(focus.surface.clone()), |
| 565 | &RelativeMotionEvent { |
| 566 | delta: event.delta(), |
| 567 | delta_unaccel: event.delta_unaccel(), |
| 568 | utime: event.time(), |
| 569 | }, |
| 570 | ); |
| 571 | |
| 572 | pointer.frame(self); |
| 573 | |
| 574 | // I guess a redraw to hide the tablet cursor could be nice? Doesn't matter too |
| 575 | // much here I think. |
| 576 | return; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if self |
| 581 | .niri |
| 582 | .global_space |
| 583 | .output_under(new_pos) |
| 584 | .next() |
| 585 | .is_none() |
| 586 | { |
| 587 | // We ended up outside the outputs and need to clip the movement. |
| 588 | if let Some(output) = self.niri.global_space.output_under(pos).next() { |
| 589 | // The pointer was previously on some output. Clip the movement against its |
| 590 | // boundaries. |
| 591 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 592 | new_pos.x = new_pos |
| 593 | .x |
| 594 | .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); |
| 595 | new_pos.y = new_pos |
| 596 | .y |
| 597 | .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); |
| 598 | } else { |
| 599 | // The pointer was not on any output in the first place. Find one for it. |
| 600 | // Let's do the simple thing and just put it on the first output. |
| 601 | let output = self.niri.global_space.outputs().next().unwrap(); |
| 602 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 603 | new_pos = center(geom).to_f64(); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 608 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 609 | let mut point = new_pos; |
| 610 | point.x = point |
| 611 | .x |
| 612 | .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); |
| 613 | point.y = point |
| 614 | .y |
| 615 | .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); |
| 616 | let point = (point - geom.loc.to_f64()) |
| 617 | .to_physical(output.current_scale().fractional_scale()) |
| 618 | .to_i32_round(); |
| 619 | self.niri.screenshot_ui.pointer_motion(point); |
| 620 | } |
| 621 | |
| 622 | let under = self.niri.surface_under_and_global_space(new_pos); |
| 623 | |
| 624 | // Handle confined pointer. |
| 625 | if let Some((focus_surface, region)) = pointer_confined { |
| 626 | let mut prevent = false; |
| 627 | |
| 628 | // Prevent the pointer from leaving the focused surface. |
| 629 | if Some(&focus_surface.0) != under.as_ref().map(|x| &x.surface.0) { |
| 630 | prevent = true; |
| 631 | } |
| 632 | |
| 633 | // Prevent the pointer from leaving the confine region, if any. |
| 634 | if let Some(region) = region { |
| 635 | let new_pos_within_surface = new_pos.to_i32_round() - focus_surface.1; |
| 636 | if !region.contains(new_pos_within_surface) { |
| 637 | prevent = true; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if prevent { |
| 642 | pointer.relative_motion( |
| 643 | self, |
| 644 | Some(focus_surface), |
| 645 | &RelativeMotionEvent { |
| 646 | delta: event.delta(), |
| 647 | delta_unaccel: event.delta_unaccel(), |
| 648 | utime: event.time(), |
| 649 | }, |
| 650 | ); |
| 651 | |
| 652 | pointer.frame(self); |
| 653 | |
| 654 | return; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // Activate a new confinement if necessary. |
| 659 | self.niri.maybe_activate_pointer_constraint(new_pos, &under); |
| 660 | |
| 661 | self.niri.pointer_focus = under.clone(); |
| 662 | let under = under.map(|u| u.surface); |
| 663 | |
| 664 | pointer.motion( |
| 665 | self, |
| 666 | under.clone(), |
| 667 | &MotionEvent { |
| 668 | location: new_pos, |
| 669 | serial, |
| 670 | time: event.time_msec(), |
| 671 | }, |
| 672 | ); |
| 673 | |
| 674 | pointer.relative_motion( |
| 675 | self, |
| 676 | under, |
| 677 | &RelativeMotionEvent { |
| 678 | delta: event.delta(), |
| 679 | delta_unaccel: event.delta_unaccel(), |
| 680 | utime: event.time(), |
| 681 | }, |
| 682 | ); |
| 683 | |
| 684 | pointer.frame(self); |
| 685 | |
| 686 | // Redraw to update the cursor position. |
| 687 | // FIXME: redraw only outputs overlapping the cursor. |
| 688 | self.niri.queue_redraw_all(); |
| 689 | } |
| 690 | |
| 691 | fn on_pointer_motion_absolute<I: InputBackend>( |
| 692 | &mut self, |
| 693 | event: I::PointerMotionAbsoluteEvent, |
| 694 | ) { |
| 695 | let Some(output) = self.niri.global_space.outputs().next() else { |
| 696 | return; |
| 697 | }; |
| 698 | |
| 699 | let output_geo = self.niri.global_space.output_geometry(output).unwrap(); |
| 700 | |
| 701 | let pos = event.position_transformed(output_geo.size) + output_geo.loc.to_f64(); |
| 702 | |
| 703 | let serial = SERIAL_COUNTER.next_serial(); |
| 704 | |
| 705 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 706 | |
| 707 | if let Some(output) = self.niri.screenshot_ui.selection_output() { |
| 708 | let geom = self.niri.global_space.output_geometry(output).unwrap(); |
| 709 | let mut point = pos; |
| 710 | point.x = point |
| 711 | .x |
| 712 | .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); |
| 713 | point.y = point |
| 714 | .y |
| 715 | .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); |
| 716 | let point = (point - geom.loc.to_f64()) |
| 717 | .to_physical(output.current_scale().fractional_scale()) |
| 718 | .to_i32_round(); |
| 719 | self.niri.screenshot_ui.pointer_motion(point); |
| 720 | } |
| 721 | |
| 722 | let under = self.niri.surface_under_and_global_space(pos); |
| 723 | self.niri.maybe_activate_pointer_constraint(pos, &under); |
| 724 | self.niri.pointer_focus = under.clone(); |
| 725 | let under = under.map(|u| u.surface); |
| 726 | |
| 727 | pointer.motion( |
| 728 | self, |
| 729 | under, |
| 730 | &MotionEvent { |
| 731 | location: pos, |
| 732 | serial, |
| 733 | time: event.time_msec(), |
| 734 | }, |
| 735 | ); |
| 736 | |
| 737 | pointer.frame(self); |
| 738 | |
| 739 | // We moved the regular pointer, so show it now. |
| 740 | self.niri.tablet_cursor_location = None; |
| 741 | |
| 742 | // Redraw to update the cursor position. |
| 743 | // FIXME: redraw only outputs overlapping the cursor. |
| 744 | self.niri.queue_redraw_all(); |
| 745 | } |
| 746 | |
| 747 | fn on_pointer_button<I: InputBackend>(&mut self, event: I::PointerButtonEvent) { |
| 748 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 749 | |
| 750 | let serial = SERIAL_COUNTER.next_serial(); |
| 751 | |
| 752 | let button = event.button_code(); |
| 753 | |
| 754 | let button_state = event.state(); |
| 755 | |
| 756 | if ButtonState::Pressed == button_state && !pointer.is_grabbed() { |
| 757 | if let Some(window) = self.niri.window_under_cursor() { |
| 758 | let window = window.clone(); |
| 759 | self.niri.layout.activate_window(&window); |
| 760 | } else if let Some(output) = self.niri.output_under_cursor() { |
| 761 | self.niri.layout.activate_output(&output); |
| 762 | } |
| 763 | }; |
| 764 | |
| 765 | self.update_pointer_focus(); |
| 766 | |
| 767 | if let Some(button) = event.button() { |
| 768 | let pos = pointer.current_location(); |
| 769 | if let Some((output, _)) = self.niri.output_under(pos) { |
| 770 | let output = output.clone(); |
| 771 | let geom = self.niri.global_space.output_geometry(&output).unwrap(); |
| 772 | let mut point = pos; |
| 773 | // Re-clamp as pointer can be within 0.5 from the limit which will round up |
| 774 | // to a wrong value. |
| 775 | point.x = point |
| 776 | .x |
| 777 | .clamp(geom.loc.x as f64, (geom.loc.x + geom.size.w - 1) as f64); |
| 778 | point.y = point |
| 779 | .y |
| 780 | .clamp(geom.loc.y as f64, (geom.loc.y + geom.size.h - 1) as f64); |
| 781 | let point = (point - geom.loc.to_f64()) |
| 782 | .to_physical(output.current_scale().fractional_scale()) |
| 783 | .to_i32_round(); |
| 784 | if self |
| 785 | .niri |
| 786 | .screenshot_ui |
| 787 | .pointer_button(output, point, button, button_state) |
| 788 | { |
| 789 | self.niri.queue_redraw_all(); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | pointer.button( |
| 795 | self, |
| 796 | &ButtonEvent { |
| 797 | button, |
| 798 | state: button_state, |
| 799 | serial, |
| 800 | time: event.time_msec(), |
| 801 | }, |
| 802 | ); |
| 803 | pointer.frame(self); |
| 804 | } |
| 805 | |
| 806 | fn on_pointer_axis<I: InputBackend>(&mut self, event: I::PointerAxisEvent) { |
| 807 | let source = event.source(); |
| 808 | |
| 809 | let horizontal_amount = event |
| 810 | .amount(Axis::Horizontal) |
| 811 | .unwrap_or_else(|| event.amount_v120(Axis::Horizontal).unwrap_or(0.0) * 3.0 / 120.); |
| 812 | let vertical_amount = event |
| 813 | .amount(Axis::Vertical) |
| 814 | .unwrap_or_else(|| event.amount_v120(Axis::Vertical).unwrap_or(0.0) * 3.0 / 120.); |
| 815 | let horizontal_amount_discrete = event.amount_v120(Axis::Horizontal); |
| 816 | let vertical_amount_discrete = event.amount_v120(Axis::Vertical); |
| 817 | |
| 818 | let mut frame = AxisFrame::new(event.time_msec()).source(source); |
| 819 | if horizontal_amount != 0.0 { |
| 820 | frame = frame.value(Axis::Horizontal, horizontal_amount); |
| 821 | if let Some(discrete) = horizontal_amount_discrete { |
| 822 | frame = frame.v120(Axis::Horizontal, discrete as i32); |
| 823 | } |
| 824 | } |
| 825 | if vertical_amount != 0.0 { |
| 826 | frame = frame.value(Axis::Vertical, vertical_amount); |
| 827 | if let Some(discrete) = vertical_amount_discrete { |
| 828 | frame = frame.v120(Axis::Vertical, discrete as i32); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if source == AxisSource::Finger { |
| 833 | if event.amount(Axis::Horizontal) == Some(0.0) { |
| 834 | frame = frame.stop(Axis::Horizontal); |
| 835 | } |
| 836 | if event.amount(Axis::Vertical) == Some(0.0) { |
| 837 | frame = frame.stop(Axis::Vertical); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | self.update_pointer_focus(); |
| 842 | |
| 843 | let pointer = &self.niri.seat.get_pointer().unwrap(); |
| 844 | pointer.axis(self, frame); |
| 845 | pointer.frame(self); |
| 846 | } |
| 847 | |
| 848 | fn on_tablet_tool_axis<I: InputBackend>(&mut self, event: I::TabletToolAxisEvent) |
| 849 | where |
| 850 | I::Device: 'static, // Needed for downcasting. |
| 851 | { |
| 852 | let Some(pos) = self.compute_tablet_position(&event) else { |
| 853 | return; |
| 854 | }; |
| 855 | |
| 856 | let under = self.niri.surface_under_and_global_space(pos); |
| 857 | let under = under.map(|u| u.surface); |
| 858 | |
| 859 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 860 | let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); |
| 861 | let tool = tablet_seat.get_tool(&event.tool()); |
| 862 | if let (Some(tablet), Some(tool)) = (tablet, tool) { |
| 863 | if event.pressure_has_changed() { |
| 864 | tool.pressure(event.pressure()); |
| 865 | } |
| 866 | if event.distance_has_changed() { |
| 867 | tool.distance(event.distance()); |
| 868 | } |
| 869 | if event.tilt_has_changed() { |
| 870 | tool.tilt(event.tilt()); |
| 871 | } |
| 872 | if event.slider_has_changed() { |
| 873 | tool.slider_position(event.slider_position()); |
| 874 | } |
| 875 | if event.rotation_has_changed() { |
| 876 | tool.rotation(event.rotation()); |
| 877 | } |
| 878 | if event.wheel_has_changed() { |
| 879 | tool.wheel(event.wheel_delta(), event.wheel_delta_discrete()); |
| 880 | } |
| 881 | |
| 882 | tool.motion( |
| 883 | pos, |
| 884 | under, |
| 885 | &tablet, |
| 886 | SERIAL_COUNTER.next_serial(), |
| 887 | event.time_msec(), |
| 888 | ); |
| 889 | |
| 890 | self.niri.tablet_cursor_location = Some(pos); |
| 891 | } |
| 892 | |
| 893 | // Redraw to update the cursor position. |
| 894 | // FIXME: redraw only outputs overlapping the cursor. |
| 895 | self.niri.queue_redraw_all(); |
| 896 | } |
| 897 | |
| 898 | fn on_tablet_tool_tip<I: InputBackend>(&mut self, event: I::TabletToolTipEvent) { |
| 899 | let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); |
| 900 | |
| 901 | if let Some(tool) = tool { |
| 902 | match event.tip_state() { |
| 903 | TabletToolTipState::Down => { |
| 904 | let serial = SERIAL_COUNTER.next_serial(); |
| 905 | tool.tip_down(serial, event.time_msec()); |
| 906 | |
| 907 | if let Some(pos) = self.niri.tablet_cursor_location { |
| 908 | if let Some(window) = self.niri.window_under(pos) { |
| 909 | let window = window.clone(); |
| 910 | self.niri.layout.activate_window(&window); |
| 911 | |
| 912 | // FIXME: granular. |
| 913 | self.niri.queue_redraw_all(); |
| 914 | } else if let Some((output, _)) = self.niri.output_under(pos) { |
| 915 | let output = output.clone(); |
| 916 | self.niri.layout.activate_output(&output); |
| 917 | |
| 918 | // FIXME: granular. |
| 919 | self.niri.queue_redraw_all(); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | TabletToolTipState::Up => { |
| 924 | tool.tip_up(event.time_msec()); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | fn on_tablet_tool_proximity<I: InputBackend>(&mut self, event: I::TabletToolProximityEvent) |
| 931 | where |
| 932 | I::Device: 'static, // Needed for downcasting. |
| 933 | { |
| 934 | let Some(pos) = self.compute_tablet_position(&event) else { |
| 935 | return; |
| 936 | }; |
| 937 | |
| 938 | let under = self.niri.surface_under_and_global_space(pos); |
| 939 | let under = under.map(|u| u.surface); |
| 940 | |
| 941 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 942 | let tool = tablet_seat.add_tool::<Self>(&self.niri.display_handle, &event.tool()); |
| 943 | let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); |
| 944 | if let Some(tablet) = tablet { |
| 945 | match event.state() { |
| 946 | ProximityState::In => { |
| 947 | if let Some(under) = under { |
| 948 | tool.proximity_in( |
| 949 | pos, |
| 950 | under, |
| 951 | &tablet, |
| 952 | SERIAL_COUNTER.next_serial(), |
| 953 | event.time_msec(), |
| 954 | ); |
| 955 | } |
| 956 | self.niri.tablet_cursor_location = Some(pos); |
| 957 | } |
| 958 | ProximityState::Out => { |
| 959 | tool.proximity_out(event.time_msec()); |
| 960 | |
| 961 | // Move the mouse pointer here to avoid discontinuity. |
| 962 | // |
| 963 | // Plus, Wayland SDL2 currently warps the pointer into some weird |
| 964 | // location on proximity out, so this shuold help it a little. |
| 965 | if let Some(pos) = self.niri.tablet_cursor_location { |
| 966 | self.move_cursor(pos); |
| 967 | } |
| 968 | |
| 969 | self.niri.tablet_cursor_location = None; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // FIXME: granular. |
| 974 | self.niri.queue_redraw_all(); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | fn on_tablet_tool_button<I: InputBackend>(&mut self, event: I::TabletToolButtonEvent) { |
| 979 | let tool = self.niri.seat.tablet_seat().get_tool(&event.tool()); |
| 980 | |
| 981 | if let Some(tool) = tool { |
| 982 | tool.button( |
| 983 | event.button(), |
| 984 | event.button_state(), |
| 985 | SERIAL_COUNTER.next_serial(), |
| 986 | event.time_msec(), |
| 987 | ); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | fn on_gesture_swipe_begin<I: InputBackend>(&mut self, event: I::GestureSwipeBeginEvent) { |
| 992 | if event.fingers() == 3 { |
| 993 | if let Some(output) = self.niri.output_under_cursor() { |
| 994 | self.niri.layout.workspace_switch_gesture_begin(&output); |
| 995 | |
| 996 | // FIXME: granular. This one is awkward because this can cancel a gesture on |
| 997 | // multiple other outputs in theory. |
| 998 | self.niri.queue_redraw_all(); |
| 999 | } |
| 1000 | |
| 1001 | // We handled this event. |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | let serial = SERIAL_COUNTER.next_serial(); |
| 1006 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1007 | |
| 1008 | if self.update_pointer_focus() { |
| 1009 | pointer.frame(self); |
| 1010 | } |
| 1011 | |
| 1012 | pointer.gesture_swipe_begin( |
| 1013 | self, |
| 1014 | &GestureSwipeBeginEvent { |
| 1015 | serial, |
| 1016 | time: event.time_msec(), |
| 1017 | fingers: event.fingers(), |
| 1018 | }, |
| 1019 | ); |
| 1020 | } |
| 1021 | |
| 1022 | fn on_gesture_swipe_update<I: InputBackend>(&mut self, event: I::GestureSwipeUpdateEvent) { |
| 1023 | let res = self |
| 1024 | .niri |
| 1025 | .layout |
| 1026 | .workspace_switch_gesture_update(event.delta_y()); |
| 1027 | if let Some(output) = res { |
| 1028 | if let Some(output) = output { |
| 1029 | self.niri.queue_redraw(output); |
| 1030 | } |
| 1031 | |
| 1032 | // We handled this event. |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| 1036 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1037 | |
| 1038 | if self.update_pointer_focus() { |
| 1039 | pointer.frame(self); |
| 1040 | } |
| 1041 | |
| 1042 | pointer.gesture_swipe_update( |
| 1043 | self, |
| 1044 | &GestureSwipeUpdateEvent { |
| 1045 | time: event.time_msec(), |
| 1046 | delta: event.delta(), |
| 1047 | }, |
| 1048 | ); |
| 1049 | } |
| 1050 | |
| 1051 | fn on_gesture_swipe_end<I: InputBackend>(&mut self, event: I::GestureSwipeEndEvent) { |
| 1052 | let res = self |
| 1053 | .niri |
| 1054 | .layout |
| 1055 | .workspace_switch_gesture_end(event.cancelled()); |
| 1056 | if let Some(output) = res { |
| 1057 | self.niri.queue_redraw(output); |
| 1058 | |
| 1059 | // We handled this event. |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | let serial = SERIAL_COUNTER.next_serial(); |
| 1064 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1065 | |
| 1066 | if self.update_pointer_focus() { |
| 1067 | pointer.frame(self); |
| 1068 | } |
| 1069 | |
| 1070 | pointer.gesture_swipe_end( |
| 1071 | self, |
| 1072 | &GestureSwipeEndEvent { |
| 1073 | serial, |
| 1074 | time: event.time_msec(), |
| 1075 | cancelled: event.cancelled(), |
| 1076 | }, |
| 1077 | ); |
| 1078 | } |
| 1079 | |
| 1080 | fn on_gesture_pinch_begin<I: InputBackend>(&mut self, event: I::GesturePinchBeginEvent) { |
| 1081 | let serial = SERIAL_COUNTER.next_serial(); |
| 1082 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1083 | |
| 1084 | if self.update_pointer_focus() { |
| 1085 | pointer.frame(self); |
| 1086 | } |
| 1087 | |
| 1088 | pointer.gesture_pinch_begin( |
| 1089 | self, |
| 1090 | &GesturePinchBeginEvent { |
| 1091 | serial, |
| 1092 | time: event.time_msec(), |
| 1093 | fingers: event.fingers(), |
| 1094 | }, |
| 1095 | ); |
| 1096 | } |
| 1097 | |
| 1098 | fn on_gesture_pinch_update<I: InputBackend>(&mut self, event: I::GesturePinchUpdateEvent) { |
| 1099 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1100 | |
| 1101 | if self.update_pointer_focus() { |
| 1102 | pointer.frame(self); |
| 1103 | } |
| 1104 | |
| 1105 | pointer.gesture_pinch_update( |
| 1106 | self, |
| 1107 | &GesturePinchUpdateEvent { |
| 1108 | time: event.time_msec(), |
| 1109 | delta: event.delta(), |
| 1110 | scale: event.scale(), |
| 1111 | rotation: event.rotation(), |
| 1112 | }, |
| 1113 | ); |
| 1114 | } |
| 1115 | |
| 1116 | fn on_gesture_pinch_end<I: InputBackend>(&mut self, event: I::GesturePinchEndEvent) { |
| 1117 | let serial = SERIAL_COUNTER.next_serial(); |
| 1118 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1119 | |
| 1120 | if self.update_pointer_focus() { |
| 1121 | pointer.frame(self); |
| 1122 | } |
| 1123 | |
| 1124 | pointer.gesture_pinch_end( |
| 1125 | self, |
| 1126 | &GesturePinchEndEvent { |
| 1127 | serial, |
| 1128 | time: event.time_msec(), |
| 1129 | cancelled: event.cancelled(), |
| 1130 | }, |
| 1131 | ); |
| 1132 | } |
| 1133 | |
| 1134 | fn on_gesture_hold_begin<I: InputBackend>(&mut self, event: I::GestureHoldBeginEvent) { |
| 1135 | let serial = SERIAL_COUNTER.next_serial(); |
| 1136 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1137 | |
| 1138 | if self.update_pointer_focus() { |
| 1139 | pointer.frame(self); |
| 1140 | } |
| 1141 | |
| 1142 | pointer.gesture_hold_begin( |
| 1143 | self, |
| 1144 | &GestureHoldBeginEvent { |
| 1145 | serial, |
| 1146 | time: event.time_msec(), |
| 1147 | fingers: event.fingers(), |
| 1148 | }, |
| 1149 | ); |
| 1150 | } |
| 1151 | |
| 1152 | fn on_gesture_hold_end<I: InputBackend>(&mut self, event: I::GestureHoldEndEvent) { |
| 1153 | let serial = SERIAL_COUNTER.next_serial(); |
| 1154 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 1155 | |
| 1156 | if self.update_pointer_focus() { |
| 1157 | pointer.frame(self); |
| 1158 | } |
| 1159 | |
| 1160 | pointer.gesture_hold_end( |
| 1161 | self, |
| 1162 | &GestureHoldEndEvent { |
| 1163 | serial, |
| 1164 | time: event.time_msec(), |
| 1165 | cancelled: event.cancelled(), |
| 1166 | }, |
| 1167 | ); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /// Check whether the key should be intercepted and mark intercepted |
| 1172 | /// pressed keys as `suppressed`, thus preventing `releases` corresponding |
| 1173 | /// to them from being delivered. |
| 1174 | #[allow(clippy::too_many_arguments)] |
| 1175 | fn should_intercept_key( |
| 1176 | suppressed_keys: &mut HashSet<u32>, |
| 1177 | bindings: &Binds, |
| 1178 | comp_mod: CompositorMod, |
| 1179 | key_code: u32, |
| 1180 | modified: Keysym, |
| 1181 | raw: Option<Keysym>, |
| 1182 | pressed: bool, |
| 1183 | mods: ModifiersState, |
| 1184 | screenshot_ui: &ScreenshotUi, |
| 1185 | disable_power_key_handling: bool, |
| 1186 | ) -> FilterResult<Option<Action>> { |
| 1187 | // Actions are only triggered on presses, release of the key |
| 1188 | // shouldn't try to intercept anything unless we have marked |
| 1189 | // the key to suppress. |
| 1190 | if !pressed && !suppressed_keys.contains(&key_code) { |
| 1191 | return FilterResult::Forward; |
| 1192 | } |
| 1193 | |
| 1194 | let mut final_action = action( |
| 1195 | bindings, |
| 1196 | comp_mod, |
| 1197 | modified, |
| 1198 | raw, |
| 1199 | mods, |
| 1200 | disable_power_key_handling, |
| 1201 | ); |
| 1202 | |
| 1203 | // Allow only a subset of compositor actions while the screenshot UI is open, since the user |
| 1204 | // cannot see the screen. |
| 1205 | if screenshot_ui.is_open() { |
| 1206 | let mut use_screenshot_ui_action = true; |
| 1207 | |
| 1208 | if let Some(action) = &final_action { |
| 1209 | if allowed_during_screenshot(action) { |
| 1210 | use_screenshot_ui_action = false; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | if use_screenshot_ui_action { |
| 1215 | final_action = screenshot_ui.action(raw, mods); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | match (final_action, pressed) { |
| 1220 | (Some(action), true) => { |
| 1221 | suppressed_keys.insert(key_code); |
| 1222 | FilterResult::Intercept(Some(action)) |
| 1223 | } |
| 1224 | (_, false) => { |
| 1225 | suppressed_keys.remove(&key_code); |
| 1226 | FilterResult::Intercept(None) |
| 1227 | } |
| 1228 | (None, true) => FilterResult::Forward, |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | fn action( |
| 1233 | bindings: &Binds, |
| 1234 | comp_mod: CompositorMod, |
| 1235 | modified: Keysym, |
| 1236 | raw: Option<Keysym>, |
| 1237 | mods: ModifiersState, |
| 1238 | disable_power_key_handling: bool, |
| 1239 | ) -> Option<Action> { |
| 1240 | use keysyms::*; |
| 1241 | |
| 1242 | // Handle hardcoded binds. |
| 1243 | #[allow(non_upper_case_globals)] // wat |
| 1244 | match modified.raw() { |
| 1245 | modified @ KEY_XF86Switch_VT_1..=KEY_XF86Switch_VT_12 => { |
| 1246 | let vt = (modified - KEY_XF86Switch_VT_1 + 1) as i32; |
| 1247 | return Some(Action::ChangeVt(vt)); |
| 1248 | } |
| 1249 | KEY_XF86PowerOff if !disable_power_key_handling => return Some(Action::Suspend), |
| 1250 | _ => (), |
| 1251 | } |
| 1252 | |
| 1253 | // Handle configured binds. |
| 1254 | let mut modifiers = Modifiers::empty(); |
| 1255 | if mods.ctrl { |
| 1256 | modifiers |= Modifiers::CTRL; |
| 1257 | } |
| 1258 | if mods.shift { |
| 1259 | modifiers |= Modifiers::SHIFT; |
| 1260 | } |
| 1261 | if mods.alt { |
| 1262 | modifiers |= Modifiers::ALT; |
| 1263 | } |
| 1264 | if mods.logo { |
| 1265 | modifiers |= Modifiers::SUPER; |
| 1266 | } |
| 1267 | |
| 1268 | let (mod_down, mut comp_mod) = match comp_mod { |
| 1269 | CompositorMod::Super => (mods.logo, Modifiers::SUPER), |
| 1270 | CompositorMod::Alt => (mods.alt, Modifiers::ALT), |
| 1271 | }; |
| 1272 | if mod_down { |
| 1273 | modifiers |= Modifiers::COMPOSITOR; |
| 1274 | } else { |
| 1275 | comp_mod = Modifiers::empty(); |
| 1276 | } |
| 1277 | |
| 1278 | let Some(raw) = raw else { |
| 1279 | return None; |
| 1280 | }; |
| 1281 | |
| 1282 | for bind in &bindings.0 { |
| 1283 | if bind.key.keysym != raw { |
| 1284 | continue; |
| 1285 | } |
| 1286 | |
| 1287 | if bind.key.modifiers | comp_mod == modifiers { |
| 1288 | return bind.actions.first().cloned(); |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | None |
| 1293 | } |
| 1294 | |
| 1295 | fn should_activate_monitors<I: InputBackend>(event: &InputEvent<I>) -> bool { |
| 1296 | match event { |
| 1297 | InputEvent::Keyboard { event } if event.state() == KeyState::Pressed => true, |
| 1298 | InputEvent::PointerMotion { .. } |
| 1299 | | InputEvent::PointerMotionAbsolute { .. } |
| 1300 | | InputEvent::PointerButton { .. } |
| 1301 | | InputEvent::PointerAxis { .. } |
| 1302 | | InputEvent::GestureSwipeBegin { .. } |
| 1303 | | InputEvent::GesturePinchBegin { .. } |
| 1304 | | InputEvent::GestureHoldBegin { .. } |
| 1305 | | InputEvent::TouchDown { .. } |
| 1306 | | InputEvent::TouchMotion { .. } |
| 1307 | | InputEvent::TabletToolAxis { .. } |
| 1308 | | InputEvent::TabletToolProximity { .. } |
| 1309 | | InputEvent::TabletToolTip { .. } |
| 1310 | | InputEvent::TabletToolButton { .. } => true, |
| 1311 | // Ignore events like device additions and removals, key releases, gesture ends. |
| 1312 | _ => false, |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | fn allowed_when_locked(action: &Action) -> bool { |
| 1317 | matches!( |
| 1318 | action, |
| 1319 | Action::Quit |
| 1320 | | Action::ChangeVt(_) |
| 1321 | | Action::Suspend |
| 1322 | | Action::PowerOffMonitors |
| 1323 | | Action::SwitchLayout(_) |
| 1324 | ) |
| 1325 | } |
| 1326 | |
| 1327 | fn allowed_during_screenshot(action: &Action) -> bool { |
| 1328 | matches!( |
| 1329 | action, |
| 1330 | Action::Quit | Action::ChangeVt(_) | Action::Suspend | Action::PowerOffMonitors |
| 1331 | ) |
| 1332 | } |
| 1333 | |
| 1334 | #[cfg(test)] |
| 1335 | mod tests { |
| 1336 | use super::*; |
| 1337 | use crate::config::{Action, Bind, Binds, Key, Modifiers}; |
| 1338 | |
| 1339 | #[test] |
| 1340 | fn bindings_suppress_keys() { |
| 1341 | let close_keysym = Keysym::q; |
| 1342 | let bindings = Binds(vec![Bind { |
| 1343 | key: Key { |
| 1344 | keysym: close_keysym, |
| 1345 | modifiers: Modifiers::COMPOSITOR | Modifiers::CTRL, |
| 1346 | }, |
| 1347 | actions: vec![Action::CloseWindow], |
| 1348 | }]); |
| 1349 | |
| 1350 | let comp_mod = CompositorMod::Super; |
| 1351 | let mut suppressed_keys = HashSet::new(); |
| 1352 | |
| 1353 | let screenshot_ui = ScreenshotUi::new(); |
| 1354 | let disable_power_key_handling = false; |
| 1355 | |
| 1356 | // The key_code we pick is arbitrary, the only thing |
| 1357 | // that matters is that they are different between cases. |
| 1358 | |
| 1359 | let close_key_code = close_keysym.into(); |
| 1360 | let close_key_event = |suppr: &mut HashSet<u32>, mods: ModifiersState, pressed| { |
| 1361 | should_intercept_key( |
| 1362 | suppr, |
| 1363 | &bindings, |
| 1364 | comp_mod, |
| 1365 | close_key_code, |
| 1366 | close_keysym, |
| 1367 | Some(close_keysym), |
| 1368 | pressed, |
| 1369 | mods, |
| 1370 | &screenshot_ui, |
| 1371 | disable_power_key_handling, |
| 1372 | ) |
| 1373 | }; |
| 1374 | |
| 1375 | // Key event with the code which can't trigger any action. |
| 1376 | let none_key_event = |suppr: &mut HashSet<u32>, mods: ModifiersState, pressed| { |
| 1377 | should_intercept_key( |
| 1378 | suppr, |
| 1379 | &bindings, |
| 1380 | comp_mod, |
| 1381 | Keysym::l.into(), |
| 1382 | Keysym::l, |
| 1383 | Some(Keysym::l), |
| 1384 | pressed, |
| 1385 | mods, |
| 1386 | &screenshot_ui, |
| 1387 | disable_power_key_handling, |
| 1388 | ) |
| 1389 | }; |
| 1390 | |
| 1391 | let mut mods = ModifiersState { |
| 1392 | logo: true, |
| 1393 | ctrl: true, |
| 1394 | ..Default::default() |
| 1395 | }; |
| 1396 | |
| 1397 | // Action press/release. |
| 1398 | |
| 1399 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 1400 | assert!(matches!( |
| 1401 | filter, |
| 1402 | FilterResult::Intercept(Some(Action::CloseWindow)) |
| 1403 | )); |
| 1404 | assert!(suppressed_keys.contains(&close_key_code)); |
| 1405 | |
| 1406 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 1407 | assert!(matches!(filter, FilterResult::Intercept(None))); |
| 1408 | assert!(suppressed_keys.is_empty()); |
| 1409 | |
| 1410 | // Remove mod to make it for a binding. |
| 1411 | |
| 1412 | mods.shift = true; |
| 1413 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 1414 | assert!(matches!(filter, FilterResult::Forward)); |
| 1415 | |
| 1416 | mods.shift = false; |
| 1417 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 1418 | assert!(matches!(filter, FilterResult::Forward)); |
| 1419 | |
| 1420 | // Just none press/release. |
| 1421 | |
| 1422 | let filter = none_key_event(&mut suppressed_keys, mods, true); |
| 1423 | assert!(matches!(filter, FilterResult::Forward)); |
| 1424 | |
| 1425 | let filter = none_key_event(&mut suppressed_keys, mods, false); |
| 1426 | assert!(matches!(filter, FilterResult::Forward)); |
| 1427 | |
| 1428 | // Press action, press arbitrary, release action, release arbitrary. |
| 1429 | |
| 1430 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 1431 | assert!(matches!( |
| 1432 | filter, |
| 1433 | FilterResult::Intercept(Some(Action::CloseWindow)) |
| 1434 | )); |
| 1435 | |
| 1436 | let filter = none_key_event(&mut suppressed_keys, mods, true); |
| 1437 | assert!(matches!(filter, FilterResult::Forward)); |
| 1438 | |
| 1439 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 1440 | assert!(matches!(filter, FilterResult::Intercept(None))); |
| 1441 | |
| 1442 | let filter = none_key_event(&mut suppressed_keys, mods, false); |
| 1443 | assert!(matches!(filter, FilterResult::Forward)); |
| 1444 | |
| 1445 | // Trigger and remove all mods. |
| 1446 | |
| 1447 | let filter = close_key_event(&mut suppressed_keys, mods, true); |
| 1448 | assert!(matches!( |
| 1449 | filter, |
| 1450 | FilterResult::Intercept(Some(Action::CloseWindow)) |
| 1451 | )); |
| 1452 | |
| 1453 | mods = Default::default(); |
| 1454 | let filter = close_key_event(&mut suppressed_keys, mods, false); |
| 1455 | assert!(matches!(filter, FilterResult::Intercept(None))); |
| 1456 | |
| 1457 | // Ensure that no keys are being suppressed. |
| 1458 | assert!(suppressed_keys.is_empty()); |
| 1459 | } |
| 1460 | } |