1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! All pointer related handling.

use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};

use sctk::reexports::client::protocol::wl_pointer::WlPointer;
use sctk::reexports::client::protocol::wl_seat::WlSeat;
use sctk::reexports::client::protocol::wl_surface::WlSurface;
use sctk::reexports::client::Attached;
use sctk::reexports::protocols::unstable::relative_pointer::v1::client::zwp_relative_pointer_manager_v1::ZwpRelativePointerManagerV1;
use sctk::reexports::protocols::unstable::relative_pointer::v1::client::zwp_relative_pointer_v1::ZwpRelativePointerV1;
use sctk::reexports::protocols::unstable::pointer_constraints::v1::client::zwp_pointer_constraints_v1::{ZwpPointerConstraintsV1, Lifetime};
use sctk::reexports::protocols::unstable::pointer_constraints::v1::client::zwp_confined_pointer_v1::ZwpConfinedPointerV1;

use sctk::seat::pointer::{ThemeManager, ThemedPointer};

use crate::event::ModifiersState;
use crate::platform_impl::wayland::event_loop::WinitState;
use crate::window::CursorIcon;

mod data;
mod handlers;

use data::PointerData;

/// A proxy to Wayland pointer, which serves requests from a `WindowHandle`.
pub struct WinitPointer {
    pointer: ThemedPointer,

    /// Create confined pointers.
    pointer_constraints: Option<Attached<ZwpPointerConstraintsV1>>,

    /// Cursor to handle confine requests.
    confined_pointer: Weak<RefCell<Option<ZwpConfinedPointerV1>>>,

    /// Latest observed serial in pointer events.
    latest_serial: Rc<Cell<u32>>,
}

impl PartialEq for WinitPointer {
    fn eq(&self, other: &Self) -> bool {
        *self.pointer == *other.pointer
    }
}

impl Eq for WinitPointer {}

impl WinitPointer {
    /// Set the cursor icon.
    ///
    /// Providing `None` will hide the cursor.
    pub fn set_cursor(&self, cursor_icon: Option<CursorIcon>) {
        let cursor_icon = match cursor_icon {
            Some(cursor_icon) => cursor_icon,
            None => {
                // Hide the cursor.
                (*self.pointer).set_cursor(self.latest_serial.get(), None, 0, 0);
                return;
            }
        };

        let cursors: &[&str] = match cursor_icon {
            CursorIcon::Alias => &["link"],
            CursorIcon::Arrow => &["arrow"],
            CursorIcon::Cell => &["plus"],
            CursorIcon::Copy => &["copy"],
            CursorIcon::Crosshair => &["crosshair"],
            CursorIcon::Default => &["left_ptr"],
            CursorIcon::Hand => &["hand"],
            CursorIcon::Help => &["question_arrow"],
            CursorIcon::Move => &["move"],
            CursorIcon::Grab => &["openhand", "grab"],
            CursorIcon::Grabbing => &["closedhand", "grabbing"],
            CursorIcon::Progress => &["progress"],
            CursorIcon::AllScroll => &["all-scroll"],
            CursorIcon::ContextMenu => &["context-menu"],

            CursorIcon::NoDrop => &["no-drop", "circle"],
            CursorIcon::NotAllowed => &["crossed_circle"],

            // Resize cursors
            CursorIcon::EResize => &["right_side"],
            CursorIcon::NResize => &["top_side"],
            CursorIcon::NeResize => &["top_right_corner"],
            CursorIcon::NwResize => &["top_left_corner"],
            CursorIcon::SResize => &["bottom_side"],
            CursorIcon::SeResize => &["bottom_right_corner"],
            CursorIcon::SwResize => &["bottom_left_corner"],
            CursorIcon::WResize => &["left_side"],
            CursorIcon::EwResize => &["h_double_arrow"],
            CursorIcon::NsResize => &["v_double_arrow"],
            CursorIcon::NwseResize => &["bd_double_arrow", "size_bdiag"],
            CursorIcon::NeswResize => &["fd_double_arrow", "size_fdiag"],
            CursorIcon::ColResize => &["split_h", "h_double_arrow"],
            CursorIcon::RowResize => &["split_v", "v_double_arrow"],
            CursorIcon::Text => &["text", "xterm"],
            CursorIcon::VerticalText => &["vertical-text"],

            CursorIcon::Wait => &["watch"],

            CursorIcon::ZoomIn => &["zoom-in"],
            CursorIcon::ZoomOut => &["zoom-out"],
        };

        let serial = Some(self.latest_serial.get());
        for cursor in cursors {
            if self.pointer.set_cursor(cursor, serial).is_ok() {
                break;
            }
        }
    }

    /// Confine the pointer to a surface.
    pub fn confine(&self, surface: &WlSurface) {
        let pointer_constraints = match &self.pointer_constraints {
            Some(pointer_constraints) => pointer_constraints,
            None => return,
        };

        let confined_pointer = match self.confined_pointer.upgrade() {
            Some(confined_pointer) => confined_pointer,
            // A pointer is gone.
            None => return,
        };

        *confined_pointer.borrow_mut() = Some(init_confined_pointer(
            &pointer_constraints,
            &surface,
            &*self.pointer,
        ));
    }

    /// Tries to unconfine the pointer if the current pointer is confined.
    pub fn unconfine(&self) {
        let confined_pointer = match self.confined_pointer.upgrade() {
            Some(confined_pointer) => confined_pointer,
            // A pointer is gone.
            None => return,
        };

        let mut confined_pointer = confined_pointer.borrow_mut();

        if let Some(confined_pointer) = confined_pointer.take() {
            confined_pointer.destroy();
        }
    }
}

/// A pointer wrapper for easy releasing and managing pointers.
pub(super) struct Pointers {
    /// A pointer itself.
    pointer: ThemedPointer,

    /// A relative pointer handler.
    relative_pointer: Option<ZwpRelativePointerV1>,

    /// Confined pointer.
    confined_pointer: Rc<RefCell<Option<ZwpConfinedPointerV1>>>,
}

impl Pointers {
    pub(super) fn new(
        seat: &Attached<WlSeat>,
        theme_manager: &ThemeManager,
        relative_pointer_manager: &Option<Attached<ZwpRelativePointerManagerV1>>,
        pointer_constraints: &Option<Attached<ZwpPointerConstraintsV1>>,
        modifiers_state: Rc<RefCell<ModifiersState>>,
    ) -> Self {
        let confined_pointer = Rc::new(RefCell::new(None));
        let pointer_data = Rc::new(RefCell::new(PointerData::new(
            confined_pointer.clone(),
            pointer_constraints.clone(),
            modifiers_state,
        )));
        let pointer = theme_manager.theme_pointer_with_impl(
            seat,
            move |event, pointer, mut dispatch_data| {
                let winit_state = dispatch_data.get::<WinitState>().unwrap();
                handlers::handle_pointer(pointer, event, &pointer_data, winit_state);
            },
        );

        // Setup relative_pointer if it's available.
        let relative_pointer = match relative_pointer_manager.as_ref() {
            Some(relative_pointer_manager) => {
                Some(init_relative_pointer(&relative_pointer_manager, &*pointer))
            }
            None => None,
        };

        Self {
            pointer,
            relative_pointer,
            confined_pointer,
        }
    }
}

impl Drop for Pointers {
    fn drop(&mut self) {
        // Drop relative pointer.
        if let Some(relative_pointer) = self.relative_pointer.take() {
            relative_pointer.destroy();
        }

        // Drop confined pointer.
        if let Some(confined_pointer) = self.confined_pointer.borrow_mut().take() {
            confined_pointer.destroy();
        }

        // Drop the pointer itself in case it's possible.
        if self.pointer.as_ref().version() >= 3 {
            self.pointer.release();
        }
    }
}

pub(super) fn init_relative_pointer(
    relative_pointer_manager: &ZwpRelativePointerManagerV1,
    pointer: &WlPointer,
) -> ZwpRelativePointerV1 {
    let relative_pointer = relative_pointer_manager.get_relative_pointer(&*pointer);
    relative_pointer.quick_assign(move |_, event, mut dispatch_data| {
        let winit_state = dispatch_data.get::<WinitState>().unwrap();
        handlers::handle_relative_pointer(event, winit_state);
    });

    relative_pointer.detach()
}

pub(super) fn init_confined_pointer(
    pointer_constraints: &Attached<ZwpPointerConstraintsV1>,
    surface: &WlSurface,
    pointer: &WlPointer,
) -> ZwpConfinedPointerV1 {
    let confined_pointer =
        pointer_constraints.confine_pointer(surface, pointer, None, Lifetime::Persistent.to_raw());

    confined_pointer.quick_assign(move |_, _, _| {});

    confined_pointer.detach()
}