Keep track of output and window in PointerFocus separately
Browse files at this commitd970abead82abab8ac2596662bc4d8e41a53e003Parent: 4f6ed9d2 changed files +65 -61
- src/input.rs +15 -26
- src/niri.rs +50 -35
src/input.rsmodified+15 -26
| @@ -778,12 +778,11 @@ impl State { | ||
| 778 | 778 | |
| 779 | 779 | // Check if we have an active pointer constraint. |
| 780 | 780 | let mut pointer_confined = None; |
| 781 | - if let Some(focus) = self.niri.pointer_focus.as_ref() { | |
| 782 | - let focus_surface_loc = focus.surface.1; | |
| 783 | - let pos_within_surface = pos.to_i32_round() - focus_surface_loc; | |
| 781 | + if let Some(focus) = &self.niri.pointer_focus.surface { | |
| 782 | + let pos_within_surface = pos.to_i32_round() - focus.1; | |
| 784 | 783 | |
| 785 | 784 | let mut pointer_locked = false; |
| 786 | - with_pointer_constraint(&focus.surface.0, &pointer, |constraint| { | |
| 785 | + with_pointer_constraint(&focus.0, &pointer, |constraint| { | |
| 787 | 786 | let Some(constraint) = constraint else { return }; |
| 788 | 787 | if !constraint.is_active() { |
| 789 | 788 | return; |
| @@ -801,7 +800,7 @@ impl State { | ||
| 801 | 800 | pointer_locked = true; |
| 802 | 801 | } |
| 803 | 802 | PointerConstraint::Confined(confine) => { |
| 804 | - pointer_confined = Some((focus.surface.clone(), confine.region().cloned())); | |
| 803 | + pointer_confined = Some((focus.clone(), confine.region().cloned())); | |
| 805 | 804 | } |
| 806 | 805 | } |
| 807 | 806 | }); |
| @@ -810,7 +809,7 @@ impl State { | ||
| 810 | 809 | if pointer_locked { |
| 811 | 810 | pointer.relative_motion( |
| 812 | 811 | self, |
| 813 | - Some(focus.surface.clone()), | |
| 812 | + Some(focus.clone()), | |
| 814 | 813 | &RelativeMotionEvent { |
| 815 | 814 | delta: event.delta(), |
| 816 | 815 | delta_unaccel: event.delta_unaccel(), |
| @@ -875,7 +874,7 @@ impl State { | ||
| 875 | 874 | let mut prevent = false; |
| 876 | 875 | |
| 877 | 876 | // Prevent the pointer from leaving the focused surface. |
| 878 | - if Some(&focus_surface.0) != under.as_ref().map(|x| &x.surface.0) { | |
| 877 | + if Some(&focus_surface.0) != under.surface.as_ref().map(|(s, _)| s) { | |
| 879 | 878 | prevent = true; |
| 880 | 879 | } |
| 881 | 880 | |
| @@ -908,11 +907,10 @@ impl State { | ||
| 908 | 907 | self.niri.maybe_activate_pointer_constraint(new_pos, &under); |
| 909 | 908 | |
| 910 | 909 | self.niri.pointer_focus.clone_from(&under); |
| 911 | - let under = under.map(|u| u.surface); | |
| 912 | 910 | |
| 913 | 911 | pointer.motion( |
| 914 | 912 | self, |
| 915 | - under.clone(), | |
| 913 | + under.surface.clone(), | |
| 916 | 914 | &MotionEvent { |
| 917 | 915 | location: new_pos, |
| 918 | 916 | serial, |
| @@ -922,7 +920,7 @@ impl State { | ||
| 922 | 920 | |
| 923 | 921 | pointer.relative_motion( |
| 924 | 922 | self, |
| 925 | - under, | |
| 923 | + under.surface, | |
| 926 | 924 | &RelativeMotionEvent { |
| 927 | 925 | delta: event.delta(), |
| 928 | 926 | delta_unaccel: event.delta_unaccel(), |
| @@ -971,11 +969,10 @@ impl State { | ||
| 971 | 969 | let under = self.niri.surface_under_and_global_space(pos); |
| 972 | 970 | self.niri.maybe_activate_pointer_constraint(pos, &under); |
| 973 | 971 | self.niri.pointer_focus.clone_from(&under); |
| 974 | - let under = under.map(|u| u.surface); | |
| 975 | 972 | |
| 976 | 973 | pointer.motion( |
| 977 | 974 | self, |
| 978 | - under, | |
| 975 | + under.surface, | |
| 979 | 976 | &MotionEvent { |
| 980 | 977 | location: pos, |
| 981 | 978 | serial, |
| @@ -1113,7 +1110,6 @@ impl State { | ||
| 1113 | 1110 | }; |
| 1114 | 1111 | |
| 1115 | 1112 | let under = self.niri.surface_under_and_global_space(pos); |
| 1116 | - let under = under.map(|u| u.surface); | |
| 1117 | 1113 | |
| 1118 | 1114 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 1119 | 1115 | let tablet = tablet_seat.get_tablet(&TabletDescriptor::from(&event.device())); |
| @@ -1140,7 +1136,7 @@ impl State { | ||
| 1140 | 1136 | |
| 1141 | 1137 | tool.motion( |
| 1142 | 1138 | pos, |
| 1143 | - under, | |
| 1139 | + under.surface, | |
| 1144 | 1140 | &tablet, |
| 1145 | 1141 | SERIAL_COUNTER.next_serial(), |
| 1146 | 1142 | event.time_msec(), |
| @@ -1195,7 +1191,6 @@ impl State { | ||
| 1195 | 1191 | }; |
| 1196 | 1192 | |
| 1197 | 1193 | let under = self.niri.surface_under_and_global_space(pos); |
| 1198 | - let under = under.map(|u| u.surface); | |
| 1199 | 1194 | |
| 1200 | 1195 | let tablet_seat = self.niri.seat.tablet_seat(); |
| 1201 | 1196 | let tool = tablet_seat.add_tool::<Self>(&self.niri.display_handle, &event.tool()); |
| @@ -1203,7 +1198,7 @@ impl State { | ||
| 1203 | 1198 | if let Some(tablet) = tablet { |
| 1204 | 1199 | match event.state() { |
| 1205 | 1200 | ProximityState::In => { |
| 1206 | - if let Some(under) = under { | |
| 1201 | + if let Some(under) = under.surface { | |
| 1207 | 1202 | tool.proximity_in( |
| 1208 | 1203 | pos, |
| 1209 | 1204 | under, |
| @@ -1538,13 +1533,10 @@ impl State { | ||
| 1538 | 1533 | }; |
| 1539 | 1534 | |
| 1540 | 1535 | let serial = SERIAL_COUNTER.next_serial(); |
| 1541 | - let under = self | |
| 1542 | - .niri | |
| 1543 | - .surface_under_and_global_space(touch_location) | |
| 1544 | - .map(|under| under.surface); | |
| 1536 | + let under = self.niri.surface_under_and_global_space(touch_location); | |
| 1545 | 1537 | handle.down( |
| 1546 | 1538 | self, |
| 1547 | - under, | |
| 1539 | + under.surface, | |
| 1548 | 1540 | &DownEvent { |
| 1549 | 1541 | slot: evt.slot(), |
| 1550 | 1542 | location: touch_location, |
| @@ -1574,13 +1566,10 @@ impl State { | ||
| 1574 | 1566 | let Some(touch_location) = self.compute_touch_location(&evt) else { |
| 1575 | 1567 | return; |
| 1576 | 1568 | }; |
| 1577 | - let under = self | |
| 1578 | - .niri | |
| 1579 | - .surface_under_and_global_space(touch_location) | |
| 1580 | - .map(|under| under.surface); | |
| 1569 | + let under = self.niri.surface_under_and_global_space(touch_location); | |
| 1581 | 1570 | handle.motion( |
| 1582 | 1571 | self, |
| 1583 | - under, | |
| 1572 | + under.surface, | |
| 1584 | 1573 | &TouchMotionEvent { |
| 1585 | 1574 | slot: evt.slot(), |
| 1586 | 1575 | location: touch_location, |
src/niri.rsmodified+50 -35
| @@ -201,7 +201,7 @@ pub struct Niri { | ||
| 201 | 201 | pub cursor_texture_cache: CursorTextureCache, |
| 202 | 202 | pub cursor_shape_manager_state: CursorShapeManagerState, |
| 203 | 203 | pub dnd_icon: Option<WlSurface>, |
| 204 | - pub pointer_focus: Option<PointerFocus>, | |
| 204 | + pub pointer_focus: PointerFocus, | |
| 205 | 205 | pub tablet_cursor_location: Option<Point<f64, Logical>>, |
| 206 | 206 | pub gesture_swipe_3f_cumulative: Option<(f64, f64)>, |
| 207 | 207 | |
| @@ -292,10 +292,14 @@ pub enum KeyboardFocus { | ||
| 292 | 292 | ScreenshotUi, |
| 293 | 293 | } |
| 294 | 294 | |
| 295 | -#[derive(Clone, PartialEq, Eq)] | |
| 295 | +#[derive(Default, Clone, PartialEq, Eq)] | |
| 296 | 296 | pub struct PointerFocus { |
| 297 | - pub output: Output, | |
| 298 | - pub surface: (WlSurface, Point<i32, Logical>), | |
| 297 | + // Output under pointer. | |
| 298 | + pub output: Option<Output>, | |
| 299 | + // Surface under pointer and its location in global coordinate space. | |
| 300 | + pub surface: Option<(WlSurface, Point<i32, Logical>)>, | |
| 301 | + // If surface belongs to a window, this is that window. | |
| 302 | + pub window: Option<Window>, | |
| 299 | 303 | } |
| 300 | 304 | |
| 301 | 305 | #[derive(Default)] |
| @@ -421,12 +425,11 @@ impl State { | ||
| 421 | 425 | self.niri |
| 422 | 426 | .maybe_activate_pointer_constraint(location, &under); |
| 423 | 427 | self.niri.pointer_focus.clone_from(&under); |
| 424 | - let under = under.map(|u| u.surface); | |
| 425 | 428 | |
| 426 | 429 | let pointer = &self.niri.seat.get_pointer().unwrap(); |
| 427 | 430 | pointer.motion( |
| 428 | 431 | self, |
| 429 | - under, | |
| 432 | + under.surface, | |
| 430 | 433 | &MotionEvent { |
| 431 | 434 | location, |
| 432 | 435 | serial: SERIAL_COUNTER.next_serial(), |
| @@ -560,11 +563,10 @@ impl State { | ||
| 560 | 563 | .maybe_activate_pointer_constraint(location, &under); |
| 561 | 564 | |
| 562 | 565 | self.niri.pointer_focus.clone_from(&under); |
| 563 | - let under = under.map(|u| u.surface); | |
| 564 | 566 | |
| 565 | 567 | pointer.motion( |
| 566 | 568 | self, |
| 567 | - under, | |
| 569 | + under.surface, | |
| 568 | 570 | &MotionEvent { |
| 569 | 571 | location, |
| 570 | 572 | serial: SERIAL_COUNTER.next_serial(), |
| @@ -1225,7 +1227,7 @@ impl Niri { | ||
| 1225 | 1227 | cursor_texture_cache: Default::default(), |
| 1226 | 1228 | cursor_shape_manager_state, |
| 1227 | 1229 | dnd_icon: None, |
| 1228 | - pointer_focus: None, | |
| 1230 | + pointer_focus: PointerFocus::default(), | |
| 1229 | 1231 | tablet_cursor_location: None, |
| 1230 | 1232 | gesture_swipe_3f_cumulative: None, |
| 1231 | 1233 | |
| @@ -1622,32 +1624,39 @@ impl Niri { | ||
| 1622 | 1624 | /// Pointer needs location in global space, and focused window location compatible with that |
| 1623 | 1625 | /// global space. We don't have a global space for all windows, but this function converts the |
| 1624 | 1626 | /// window location temporarily to the current global space. |
| 1625 | - pub fn surface_under_and_global_space( | |
| 1626 | - &mut self, | |
| 1627 | - pos: Point<f64, Logical>, | |
| 1628 | - ) -> Option<PointerFocus> { | |
| 1629 | - let (output, pos_within_output) = self.output_under(pos)?; | |
| 1627 | + pub fn surface_under_and_global_space(&mut self, pos: Point<f64, Logical>) -> PointerFocus { | |
| 1628 | + let mut rv = PointerFocus::default(); | |
| 1629 | + | |
| 1630 | + let Some((output, pos_within_output)) = self.output_under(pos) else { | |
| 1631 | + return rv; | |
| 1632 | + }; | |
| 1633 | + rv.output = Some(output.clone()); | |
| 1630 | 1634 | let output_pos_in_global_space = self.global_space.output_geometry(output).unwrap().loc; |
| 1631 | 1635 | |
| 1632 | 1636 | if self.is_locked() { |
| 1633 | - let state = self.output_state.get(output)?; | |
| 1634 | - let surface = state.lock_surface.as_ref()?; | |
| 1635 | - // We put lock surfaces at (0, 0). | |
| 1636 | - let point = pos_within_output; | |
| 1637 | - let (surface, point) = under_from_surface_tree( | |
| 1637 | + let Some(state) = self.output_state.get(output) else { | |
| 1638 | + return rv; | |
| 1639 | + }; | |
| 1640 | + let Some(surface) = state.lock_surface.as_ref() else { | |
| 1641 | + return rv; | |
| 1642 | + }; | |
| 1643 | + | |
| 1644 | + rv.surface = under_from_surface_tree( | |
| 1638 | 1645 | surface.wl_surface(), |
| 1639 | - point, | |
| 1646 | + pos_within_output, | |
| 1647 | + // We put lock surfaces at (0, 0). | |
| 1640 | 1648 | (0, 0), |
| 1641 | 1649 | WindowSurfaceType::ALL, |
| 1642 | - )?; | |
| 1643 | - return Some(PointerFocus { | |
| 1644 | - output: output.clone(), | |
| 1645 | - surface: (surface, point + output_pos_in_global_space), | |
| 1650 | + ) | |
| 1651 | + .map(|(surface, pos_within_output)| { | |
| 1652 | + (surface, pos_within_output + output_pos_in_global_space) | |
| 1646 | 1653 | }); |
| 1654 | + | |
| 1655 | + return rv; | |
| 1647 | 1656 | } |
| 1648 | 1657 | |
| 1649 | 1658 | if self.screenshot_ui.is_open() { |
| 1650 | - return None; | |
| 1659 | + return rv; | |
| 1651 | 1660 | } |
| 1652 | 1661 | |
| 1653 | 1662 | let layers = layer_map_for_output(output); |
| @@ -1665,6 +1674,7 @@ impl Niri { | ||
| 1665 | 1674 | (surface, pos_within_layer + layer_pos_within_output) |
| 1666 | 1675 | }) |
| 1667 | 1676 | }) |
| 1677 | + .map(|s| (s, None)) | |
| 1668 | 1678 | }; |
| 1669 | 1679 | |
| 1670 | 1680 | let window_under = || { |
| @@ -1680,6 +1690,7 @@ impl Niri { | ||
| 1680 | 1690 | .map(|(s, pos_within_window)| { |
| 1681 | 1691 | (s, pos_within_window + win_pos_within_output) |
| 1682 | 1692 | }) |
| 1693 | + .map(|s| (s, Some(window.clone()))) | |
| 1683 | 1694 | }) |
| 1684 | 1695 | }; |
| 1685 | 1696 | |
| @@ -1697,16 +1708,18 @@ impl Niri { | ||
| 1697 | 1708 | .or_else(window_under); |
| 1698 | 1709 | } |
| 1699 | 1710 | |
| 1700 | - let (surface, surface_pos_within_output) = under | |
| 1711 | + let Some(((surface, surface_pos_within_output), window)) = under | |
| 1701 | 1712 | .or_else(|| layer_surface_under(Layer::Bottom)) |
| 1702 | - .or_else(|| layer_surface_under(Layer::Background))?; | |
| 1713 | + .or_else(|| layer_surface_under(Layer::Background)) | |
| 1714 | + else { | |
| 1715 | + return rv; | |
| 1716 | + }; | |
| 1703 | 1717 | |
| 1704 | 1718 | let surface_loc_in_global_space = surface_pos_within_output + output_pos_in_global_space; |
| 1705 | 1719 | |
| 1706 | - Some(PointerFocus { | |
| 1707 | - output: output.clone(), | |
| 1708 | - surface: (surface, surface_loc_in_global_space), | |
| 1709 | - }) | |
| 1720 | + rv.surface = Some((surface, surface_loc_in_global_space)); | |
| 1721 | + rv.window = window; | |
| 1722 | + rv | |
| 1710 | 1723 | } |
| 1711 | 1724 | |
| 1712 | 1725 | pub fn output_under_cursor(&self) -> Option<Output> { |
| @@ -3231,11 +3244,13 @@ impl Niri { | ||
| 3231 | 3244 | pub fn maybe_activate_pointer_constraint( |
| 3232 | 3245 | &self, |
| 3233 | 3246 | new_pos: Point<f64, Logical>, |
| 3234 | - new_under: &Option<PointerFocus>, | |
| 3247 | + new_under: &PointerFocus, | |
| 3235 | 3248 | ) { |
| 3236 | - let Some(under) = new_under else { return }; | |
| 3249 | + let Some((surface, surface_loc)) = &new_under.surface else { | |
| 3250 | + return; | |
| 3251 | + }; | |
| 3237 | 3252 | let pointer = &self.seat.get_pointer().unwrap(); |
| 3238 | - with_pointer_constraint(&under.surface.0, pointer, |constraint| { | |
| 3253 | + with_pointer_constraint(surface, pointer, |constraint| { | |
| 3239 | 3254 | let Some(constraint) = constraint else { return }; |
| 3240 | 3255 | if constraint.is_active() { |
| 3241 | 3256 | return; |
| @@ -3243,7 +3258,7 @@ impl Niri { | ||
| 3243 | 3258 | |
| 3244 | 3259 | // Constraint does not apply if not within region. |
| 3245 | 3260 | if let Some(region) = constraint.region() { |
| 3246 | - let new_pos_within_surface = new_pos.to_i32_round() - under.surface.1; | |
| 3261 | + let new_pos_within_surface = new_pos.to_i32_round() - *surface_loc; | |
| 3247 | 3262 | if !region.contains(new_pos_within_surface) { |
| 3248 | 3263 | return; |
| 3249 | 3264 | } |