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