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