Implement pointer-constraints
Browse files at this commit86c4c1368e3ca2e01d7179e5fb86f8c4bdbe2cc4Parent: 17c23dc3 changed files +140 -5
- src/handlers/mod.rs +16 -5
- src/input.rs +90 -0
- src/niri.rs +34 -0
src/handlers/mod.rsmodified+16 -5
| @@ -11,7 +11,7 @@ use std::thread; | |||
| 11 | use smithay::backend::allocator::dmabuf::Dmabuf; | 11 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| 12 | use smithay::backend::renderer::ImportDma; | 12 | use smithay::backend::renderer::ImportDma; |
| 13 | use smithay::desktop::{PopupKind, PopupManager}; | 13 | use smithay::desktop::{PopupKind, PopupManager}; |
| 14 | -use smithay::input::pointer::{CursorIcon, CursorImageStatus}; | 14 | +use smithay::input::pointer::{CursorIcon, CursorImageStatus, PointerHandle}; |
| 15 | use smithay::input::{Seat, SeatHandler, SeatState}; | 15 | use smithay::input::{Seat, SeatHandler, SeatState}; |
| 16 | use smithay::output::Output; | 16 | use smithay::output::Output; |
| 17 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; | 17 | use smithay::reexports::wayland_server::protocol::wl_data_source::WlDataSource; |
| @@ -22,6 +22,7 @@ use smithay::utils::{Logical, Rectangle, Size}; | |||
| 22 | use smithay::wayland::compositor::{send_surface_state, with_states}; | 22 | use smithay::wayland::compositor::{send_surface_state, with_states}; |
| 23 | use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier}; | 23 | use smithay::wayland::dmabuf::{DmabufGlobal, DmabufHandler, DmabufState, ImportNotifier}; |
| 24 | use smithay::wayland::input_method::{InputMethodHandler, PopupSurface}; | 24 | use smithay::wayland::input_method::{InputMethodHandler, PopupSurface}; |
| 25 | +use smithay::wayland::pointer_constraints::PointerConstraintsHandler; | ||
| 25 | use smithay::wayland::selection::data_device::{ | 26 | use smithay::wayland::selection::data_device::{ |
| 26 | set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState, | 27 | set_data_device_focus, ClientDndGrabHandler, DataDeviceHandler, DataDeviceState, |
| 27 | ServerDndGrabHandler, | 28 | ServerDndGrabHandler, |
| @@ -36,10 +37,10 @@ use smithay::wayland::session_lock::{ | |||
| 36 | }; | 37 | }; |
| 37 | use smithay::{ | 38 | use smithay::{ |
| 38 | delegate_cursor_shape, delegate_data_control, delegate_data_device, delegate_dmabuf, | 39 | delegate_cursor_shape, delegate_data_control, delegate_data_device, delegate_dmabuf, |
| 39 | - delegate_input_method_manager, delegate_output, delegate_pointer_gestures, | 40 | + delegate_input_method_manager, delegate_output, delegate_pointer_constraints, |
| 40 | - delegate_presentation, delegate_primary_selection, delegate_relative_pointer, delegate_seat, | 41 | + delegate_pointer_gestures, delegate_presentation, delegate_primary_selection, |
| 41 | - delegate_session_lock, delegate_tablet_manager, delegate_text_input_manager, | 42 | + delegate_relative_pointer, delegate_seat, delegate_session_lock, delegate_tablet_manager, |
| 42 | - delegate_virtual_keyboard_manager, | 43 | + delegate_text_input_manager, delegate_virtual_keyboard_manager, |
| 43 | }; | 44 | }; |
| 44 | 45 | ||
| 45 | use crate::layout::output_size; | 46 | use crate::layout::output_size; |
| @@ -78,6 +79,16 @@ delegate_pointer_gestures!(State); | |||
| 78 | delegate_relative_pointer!(State); | 79 | delegate_relative_pointer!(State); |
| 79 | delegate_text_input_manager!(State); | 80 | delegate_text_input_manager!(State); |
| 80 | 81 | ||
| 82 | +impl PointerConstraintsHandler for State { | ||
| 83 | + fn new_constraint(&mut self, _surface: &WlSurface, pointer: &PointerHandle<Self>) { | ||
| 84 | + self.niri.maybe_activate_pointer_constraint( | ||
| 85 | + pointer.current_location(), | ||
| 86 | + &self.niri.pointer_focus, | ||
| 87 | + ); | ||
| 88 | + } | ||
| 89 | +} | ||
| 90 | +delegate_pointer_constraints!(State); | ||
| 91 | + | ||
| 81 | impl InputMethodHandler for State { | 92 | impl InputMethodHandler for State { |
| 82 | fn new_popup(&mut self, surface: PopupSurface) { | 93 | fn new_popup(&mut self, surface: PopupSurface) { |
| 83 | let popup = PopupKind::from(surface.clone()); | 94 | let popup = PopupKind::from(surface.clone()); |
src/input.rsmodified+90 -0
| @@ -17,6 +17,7 @@ use smithay::input::pointer::{ | |||
| 17 | }; | 17 | }; |
| 18 | use smithay::reexports::input; | 18 | use smithay::reexports::input; |
| 19 | use smithay::utils::{Logical, Point, SERIAL_COUNTER}; | 19 | use smithay::utils::{Logical, Point, SERIAL_COUNTER}; |
| 20 | +use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; | ||
| 20 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; | 21 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| 21 | 22 | ||
| 22 | use crate::config::{Action, Binds, LayoutAction, Modifiers}; | 23 | use crate::config::{Action, Binds, LayoutAction, Modifiers}; |
| @@ -509,6 +510,56 @@ impl State { | |||
| 509 | // We received an event for the regular pointer, so show it now. | 510 | // We received an event for the regular pointer, so show it now. |
| 510 | self.niri.tablet_cursor_location = None; | 511 | self.niri.tablet_cursor_location = None; |
| 511 | 512 | ||
| 513 | + // Check if we have an active pointer constraint. | ||
| 514 | + let mut pointer_confined = None; | ||
| 515 | + if let Some(focus) = self.niri.pointer_focus.as_ref() { | ||
| 516 | + let focus_surface_loc = focus.surface.1; | ||
| 517 | + let pos_within_surface = pos.to_i32_round() - focus_surface_loc; | ||
| 518 | + | ||
| 519 | + let mut pointer_locked = false; | ||
| 520 | + with_pointer_constraint(&focus.surface.0, &pointer, |constraint| { | ||
| 521 | + let Some(constraint) = constraint else { return }; | ||
| 522 | + if !constraint.is_active() { | ||
| 523 | + return; | ||
| 524 | + } | ||
| 525 | + | ||
| 526 | + // Constraint does not apply if not within region. | ||
| 527 | + if let Some(region) = constraint.region() { | ||
| 528 | + if !region.contains(pos_within_surface) { | ||
| 529 | + return; | ||
| 530 | + } | ||
| 531 | + } | ||
| 532 | + | ||
| 533 | + match &*constraint { | ||
| 534 | + PointerConstraint::Locked(_locked) => { | ||
| 535 | + pointer_locked = true; | ||
| 536 | + } | ||
| 537 | + PointerConstraint::Confined(confine) => { | ||
| 538 | + pointer_confined = Some((focus.surface.clone(), confine.region().cloned())); | ||
| 539 | + } | ||
| 540 | + } | ||
| 541 | + }); | ||
| 542 | + | ||
| 543 | + // If the pointer is locked, only send relative motion. | ||
| 544 | + if pointer_locked { | ||
| 545 | + pointer.relative_motion( | ||
| 546 | + self, | ||
| 547 | + Some(focus.surface.clone()), | ||
| 548 | + &RelativeMotionEvent { | ||
| 549 | + delta: event.delta(), | ||
| 550 | + delta_unaccel: event.delta_unaccel(), | ||
| 551 | + utime: event.time(), | ||
| 552 | + }, | ||
| 553 | + ); | ||
| 554 | + | ||
| 555 | + pointer.frame(self); | ||
| 556 | + | ||
| 557 | + // I guess a redraw to hide the tablet cursor could be nice? Doesn't matter too | ||
| 558 | + // much here I think. | ||
| 559 | + return; | ||
| 560 | + } | ||
| 561 | + } | ||
| 562 | + | ||
| 512 | if self | 563 | if self |
| 513 | .niri | 564 | .niri |
| 514 | .global_space | 565 | .global_space |
| @@ -552,6 +603,44 @@ impl State { | |||
| 552 | } | 603 | } |
| 553 | 604 | ||
| 554 | let under = self.niri.surface_under_and_global_space(new_pos); | 605 | let under = self.niri.surface_under_and_global_space(new_pos); |
| 606 | + | ||
| 607 | + // Handle confined pointer. | ||
| 608 | + if let Some((focus_surface, region)) = pointer_confined { | ||
| 609 | + let mut prevent = false; | ||
| 610 | + | ||
| 611 | + // Prevent the pointer from leaving the focused surface. | ||
| 612 | + if Some(&focus_surface.0) != under.as_ref().map(|x| &x.surface.0) { | ||
| 613 | + prevent = true; | ||
| 614 | + } | ||
| 615 | + | ||
| 616 | + // Prevent the pointer from leaving the confine region, if any. | ||
| 617 | + if let Some(region) = region { | ||
| 618 | + let new_pos_within_surface = new_pos.to_i32_round() - focus_surface.1; | ||
| 619 | + if !region.contains(new_pos_within_surface) { | ||
| 620 | + prevent = true; | ||
| 621 | + } | ||
| 622 | + } | ||
| 623 | + | ||
| 624 | + if prevent { | ||
| 625 | + pointer.relative_motion( | ||
| 626 | + self, | ||
| 627 | + Some(focus_surface), | ||
| 628 | + &RelativeMotionEvent { | ||
| 629 | + delta: event.delta(), | ||
| 630 | + delta_unaccel: event.delta_unaccel(), | ||
| 631 | + utime: event.time(), | ||
| 632 | + }, | ||
| 633 | + ); | ||
| 634 | + | ||
| 635 | + pointer.frame(self); | ||
| 636 | + | ||
| 637 | + return; | ||
| 638 | + } | ||
| 639 | + } | ||
| 640 | + | ||
| 641 | + // Activate a new confinement if necessary. | ||
| 642 | + self.niri.maybe_activate_pointer_constraint(new_pos, &under); | ||
| 643 | + | ||
| 555 | self.niri.pointer_focus = under.clone(); | 644 | self.niri.pointer_focus = under.clone(); |
| 556 | let under = under.map(|u| u.surface); | 645 | let under = under.map(|u| u.surface); |
| 557 | 646 | ||
| @@ -614,6 +703,7 @@ impl State { | |||
| 614 | } | 703 | } |
| 615 | 704 | ||
| 616 | let under = self.niri.surface_under_and_global_space(pos); | 705 | let under = self.niri.surface_under_and_global_space(pos); |
| 706 | + self.niri.maybe_activate_pointer_constraint(pos, &under); | ||
| 617 | self.niri.pointer_focus = under.clone(); | 707 | self.niri.pointer_focus = under.clone(); |
| 618 | let under = under.map(|u| u.surface); | 708 | let under = under.map(|u| u.surface); |
| 619 | 709 | ||
src/niri.rsmodified+34 -0
| @@ -61,6 +61,7 @@ use smithay::wayland::cursor_shape::CursorShapeManagerState; | |||
| 61 | use smithay::wayland::dmabuf::DmabufFeedback; | 61 | use smithay::wayland::dmabuf::DmabufFeedback; |
| 62 | use smithay::wayland::input_method::InputMethodManagerState; | 62 | use smithay::wayland::input_method::InputMethodManagerState; |
| 63 | use smithay::wayland::output::OutputManagerState; | 63 | use smithay::wayland::output::OutputManagerState; |
| 64 | +use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraintsState}; | ||
| 64 | use smithay::wayland::pointer_gestures::PointerGesturesState; | 65 | use smithay::wayland::pointer_gestures::PointerGesturesState; |
| 65 | use smithay::wayland::presentation::PresentationState; | 66 | use smithay::wayland::presentation::PresentationState; |
| 66 | use smithay::wayland::relative_pointer::RelativePointerManagerState; | 67 | use smithay::wayland::relative_pointer::RelativePointerManagerState; |
| @@ -142,6 +143,7 @@ pub struct Niri { | |||
| 142 | pub virtual_keyboard_state: VirtualKeyboardManagerState, | 143 | pub virtual_keyboard_state: VirtualKeyboardManagerState, |
| 143 | pub pointer_gestures_state: PointerGesturesState, | 144 | pub pointer_gestures_state: PointerGesturesState, |
| 144 | pub relative_pointer_state: RelativePointerManagerState, | 145 | pub relative_pointer_state: RelativePointerManagerState, |
| 146 | + pub pointer_constraints_state: PointerConstraintsState, | ||
| 145 | pub data_device_state: DataDeviceState, | 147 | pub data_device_state: DataDeviceState, |
| 146 | pub primary_selection_state: PrimarySelectionState, | 148 | pub primary_selection_state: PrimarySelectionState, |
| 147 | pub data_control_state: DataControlState, | 149 | pub data_control_state: DataControlState, |
| @@ -299,6 +301,8 @@ impl State { | |||
| 299 | 301 | ||
| 300 | pub fn move_cursor(&mut self, location: Point<f64, Logical>) { | 302 | pub fn move_cursor(&mut self, location: Point<f64, Logical>) { |
| 301 | let under = self.niri.surface_under_and_global_space(location); | 303 | let under = self.niri.surface_under_and_global_space(location); |
| 304 | + self.niri | ||
| 305 | + .maybe_activate_pointer_constraint(location, &under); | ||
| 302 | self.niri.pointer_focus = under.clone(); | 306 | self.niri.pointer_focus = under.clone(); |
| 303 | let under = under.map(|u| u.surface); | 307 | let under = under.map(|u| u.surface); |
| 304 | 308 | ||
| @@ -355,6 +359,9 @@ impl State { | |||
| 355 | return false; | 359 | return false; |
| 356 | } | 360 | } |
| 357 | 361 | ||
| 362 | + self.niri | ||
| 363 | + .maybe_activate_pointer_constraint(location, &under); | ||
| 364 | + | ||
| 358 | self.niri.pointer_focus = under.clone(); | 365 | self.niri.pointer_focus = under.clone(); |
| 359 | let under = under.map(|u| u.surface); | 366 | let under = under.map(|u| u.surface); |
| 360 | 367 | ||
| @@ -642,6 +649,7 @@ impl Niri { | |||
| 642 | let tablet_state = TabletManagerState::new::<State>(&display_handle); | 649 | let tablet_state = TabletManagerState::new::<State>(&display_handle); |
| 643 | let pointer_gestures_state = PointerGesturesState::new::<State>(&display_handle); | 650 | let pointer_gestures_state = PointerGesturesState::new::<State>(&display_handle); |
| 644 | let relative_pointer_state = RelativePointerManagerState::new::<State>(&display_handle); | 651 | let relative_pointer_state = RelativePointerManagerState::new::<State>(&display_handle); |
| 652 | + let pointer_constraints_state = PointerConstraintsState::new::<State>(&display_handle); | ||
| 645 | let data_device_state = DataDeviceState::new::<State>(&display_handle); | 653 | let data_device_state = DataDeviceState::new::<State>(&display_handle); |
| 646 | let primary_selection_state = PrimarySelectionState::new::<State>(&display_handle); | 654 | let primary_selection_state = PrimarySelectionState::new::<State>(&display_handle); |
| 647 | let data_control_state = DataControlState::new::<State, _>( | 655 | let data_control_state = DataControlState::new::<State, _>( |
| @@ -754,6 +762,7 @@ impl Niri { | |||
| 754 | tablet_state, | 762 | tablet_state, |
| 755 | pointer_gestures_state, | 763 | pointer_gestures_state, |
| 756 | relative_pointer_state, | 764 | relative_pointer_state, |
| 765 | + pointer_constraints_state, | ||
| 757 | data_device_state, | 766 | data_device_state, |
| 758 | primary_selection_state, | 767 | primary_selection_state, |
| 759 | data_control_state, | 768 | data_control_state, |
| @@ -2352,6 +2361,31 @@ impl Niri { | |||
| 2352 | 2361 | ||
| 2353 | output_state.lock_surface = Some(surface); | 2362 | output_state.lock_surface = Some(surface); |
| 2354 | } | 2363 | } |
| 2364 | + | ||
| 2365 | + pub fn maybe_activate_pointer_constraint( | ||
| 2366 | + &self, | ||
| 2367 | + new_pos: Point<f64, Logical>, | ||
| 2368 | + new_under: &Option<PointerFocus>, | ||
| 2369 | + ) { | ||
| 2370 | + let Some(under) = new_under else { return }; | ||
| 2371 | + let pointer = &self.seat.get_pointer().unwrap(); | ||
| 2372 | + with_pointer_constraint(&under.surface.0, pointer, |constraint| { | ||
| 2373 | + let Some(constraint) = constraint else { return }; | ||
| 2374 | + if constraint.is_active() { | ||
| 2375 | + return; | ||
| 2376 | + } | ||
| 2377 | + | ||
| 2378 | + // Constraint does not apply if not within region. | ||
| 2379 | + if let Some(region) = constraint.region() { | ||
| 2380 | + let new_pos_within_surface = new_pos.to_i32_round() - under.surface.1; | ||
| 2381 | + if !region.contains(new_pos_within_surface) { | ||
| 2382 | + return; | ||
| 2383 | + } | ||
| 2384 | + } | ||
| 2385 | + | ||
| 2386 | + constraint.activate(); | ||
| 2387 | + }); | ||
| 2388 | + } | ||
| 2355 | } | 2389 | } |
| 2356 | 2390 | ||
| 2357 | render_elements! { | 2391 | render_elements! { |