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