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