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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//! Handlers for the pointers we're using.

use std::cell::RefCell;
use std::rc::Rc;

use sctk::reexports::client::protocol::wl_pointer::{self, Event as PointerEvent};
use sctk::reexports::client::protocol::wl_seat::WlSeat;
use sctk::reexports::protocols::unstable::relative_pointer::v1::client::zwp_relative_pointer_v1::Event as RelativePointerEvent;

use sctk::seat::pointer::ThemedPointer;

use crate::dpi::LogicalPosition;
use crate::event::{
    DeviceEvent, ElementState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent,
};
use crate::platform_impl::wayland::event_loop::WinitState;
use crate::platform_impl::wayland::{self, DeviceId};

use super::{PointerData, WinitPointer};

// These values are comming from <linux/input-event-codes.h>.
const BTN_LEFT: u32 = 0x110;
const BTN_RIGHT: u32 = 0x111;
const BTN_MIDDLE: u32 = 0x112;

#[inline]
pub(super) fn handle_pointer(
    pointer: ThemedPointer,
    event: PointerEvent,
    pointer_data: &Rc<RefCell<PointerData>>,
    winit_state: &mut WinitState,
    seat: WlSeat,
) {
    let event_sink = &mut winit_state.event_sink;
    let mut pointer_data = pointer_data.borrow_mut();
    match event {
        PointerEvent::Enter {
            surface,
            surface_x,
            surface_y,
            serial,
            ..
        } => {
            pointer_data.latest_serial.replace(serial);
            pointer_data.latest_enter_serial.replace(serial);

            let window_id = wayland::make_wid(&surface);
            let window_handle = match winit_state.window_map.get_mut(&window_id) {
                Some(window_handle) => window_handle,
                None => return,
            };

            let scale_factor = window_handle.scale_factor();
            pointer_data.surface = Some(surface);

            // Notify window that pointer entered the surface.
            let winit_pointer = WinitPointer {
                pointer,
                confined_pointer: Rc::downgrade(&pointer_data.confined_pointer),
                locked_pointer: Rc::downgrade(&pointer_data.locked_pointer),
                pointer_constraints: pointer_data.pointer_constraints.clone(),
                latest_serial: pointer_data.latest_serial.clone(),
                latest_enter_serial: pointer_data.latest_enter_serial.clone(),
                seat,
            };
            window_handle.pointer_entered(winit_pointer);

            event_sink.push_window_event(
                WindowEvent::CursorEntered {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                },
                window_id,
            );

            let position = LogicalPosition::new(surface_x, surface_y).to_physical(scale_factor);

            event_sink.push_window_event(
                WindowEvent::CursorMoved {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                    position,
                    modifiers: *pointer_data.modifiers_state.borrow(),
                },
                window_id,
            );
        }
        PointerEvent::Leave { surface, serial } => {
            pointer_data.surface = None;
            pointer_data.latest_serial.replace(serial);

            let window_id = wayland::make_wid(&surface);

            let window_handle = match winit_state.window_map.get_mut(&window_id) {
                Some(window_handle) => window_handle,
                None => return,
            };

            // Notify a window that pointer is no longer observing it.
            let winit_pointer = WinitPointer {
                pointer,
                confined_pointer: Rc::downgrade(&pointer_data.confined_pointer),
                locked_pointer: Rc::downgrade(&pointer_data.locked_pointer),
                pointer_constraints: pointer_data.pointer_constraints.clone(),
                latest_serial: pointer_data.latest_serial.clone(),
                latest_enter_serial: pointer_data.latest_enter_serial.clone(),
                seat,
            };
            window_handle.pointer_left(winit_pointer);

            event_sink.push_window_event(
                WindowEvent::CursorLeft {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                },
                window_id,
            );
        }
        PointerEvent::Motion {
            surface_x,
            surface_y,
            ..
        } => {
            let surface = match pointer_data.surface.as_ref() {
                Some(surface) => surface,
                None => return,
            };

            let window_id = wayland::make_wid(surface);
            let window_handle = match winit_state.window_map.get(&window_id) {
                Some(w) => w,
                _ => return,
            };

            let scale_factor = window_handle.scale_factor();
            let position = LogicalPosition::new(surface_x, surface_y).to_physical(scale_factor);

            event_sink.push_window_event(
                WindowEvent::CursorMoved {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                    position,
                    modifiers: *pointer_data.modifiers_state.borrow(),
                },
                window_id,
            );
        }
        PointerEvent::Button {
            button,
            state,
            serial,
            ..
        } => {
            pointer_data.latest_serial.replace(serial);
            let window_id = match pointer_data.surface.as_ref().map(wayland::make_wid) {
                Some(window_id) => window_id,
                None => return,
            };

            let state = match state {
                wl_pointer::ButtonState::Pressed => ElementState::Pressed,
                wl_pointer::ButtonState::Released => ElementState::Released,
                _ => unreachable!(),
            };

            let button = match button {
                BTN_LEFT => MouseButton::Left,
                BTN_RIGHT => MouseButton::Right,
                BTN_MIDDLE => MouseButton::Middle,
                button => MouseButton::Other(button as u16),
            };

            event_sink.push_window_event(
                WindowEvent::MouseInput {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                    state,
                    button,
                    modifiers: *pointer_data.modifiers_state.borrow(),
                },
                window_id,
            );
        }
        PointerEvent::Axis { axis, value, .. } => {
            let surface = match pointer_data.surface.as_ref() {
                Some(surface) => surface,
                None => return,
            };

            let window_id = wayland::make_wid(surface);
            let window_handle = match winit_state.window_map.get(&window_id) {
                Some(w) => w,
                _ => return,
            };

            if pointer.as_ref().version() < 5 {
                let (mut x, mut y) = (0.0, 0.0);

                // Old seat compatibility.
                match axis {
                    // Wayland sign convention is the inverse of winit.
                    wl_pointer::Axis::VerticalScroll => y -= value as f32,
                    wl_pointer::Axis::HorizontalScroll => x -= value as f32,
                    _ => unreachable!(),
                }

                let scale_factor = window_handle.scale_factor();
                let delta = LogicalPosition::new(x as f64, y as f64).to_physical(scale_factor);

                event_sink.push_window_event(
                    WindowEvent::MouseWheel {
                        device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                            DeviceId,
                        )),
                        delta: MouseScrollDelta::PixelDelta(delta),
                        phase: TouchPhase::Moved,
                        modifiers: *pointer_data.modifiers_state.borrow(),
                    },
                    window_id,
                );
            } else {
                let (mut x, mut y) = pointer_data.axis_data.axis_buffer.unwrap_or((0.0, 0.0));
                match axis {
                    // Wayland sign convention is the inverse of winit.
                    wl_pointer::Axis::VerticalScroll => y -= value as f32,
                    wl_pointer::Axis::HorizontalScroll => x -= value as f32,
                    _ => unreachable!(),
                }

                pointer_data.axis_data.axis_buffer = Some((x, y));

                pointer_data.axis_data.axis_state = match pointer_data.axis_data.axis_state {
                    TouchPhase::Started | TouchPhase::Moved => TouchPhase::Moved,
                    _ => TouchPhase::Started,
                }
            }
        }
        PointerEvent::AxisDiscrete { axis, discrete } => {
            let (mut x, mut y) = pointer_data
                .axis_data
                .axis_discrete_buffer
                .unwrap_or((0., 0.));

            match axis {
                // Wayland sign convention is the inverse of winit.
                wl_pointer::Axis::VerticalScroll => y -= discrete as f32,
                wl_pointer::Axis::HorizontalScroll => x -= discrete as f32,
                _ => unreachable!(),
            }

            pointer_data.axis_data.axis_discrete_buffer = Some((x, y));

            pointer_data.axis_data.axis_state = match pointer_data.axis_data.axis_state {
                TouchPhase::Started | TouchPhase::Moved => TouchPhase::Moved,
                _ => TouchPhase::Started,
            }
        }
        PointerEvent::AxisSource { .. } => (),
        PointerEvent::AxisStop { .. } => {
            pointer_data.axis_data.axis_state = TouchPhase::Ended;
        }
        PointerEvent::Frame => {
            let axis_buffer = pointer_data.axis_data.axis_buffer.take();
            let axis_discrete_buffer = pointer_data.axis_data.axis_discrete_buffer.take();

            let surface = match pointer_data.surface.as_ref() {
                Some(surface) => surface,
                None => return,
            };
            let window_id = wayland::make_wid(surface);
            let window_handle = match winit_state.window_map.get(&window_id) {
                Some(w) => w,
                _ => return,
            };

            let window_event = if let Some((x, y)) = axis_discrete_buffer {
                WindowEvent::MouseWheel {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                    delta: MouseScrollDelta::LineDelta(x, y),
                    phase: pointer_data.axis_data.axis_state,
                    modifiers: *pointer_data.modifiers_state.borrow(),
                }
            } else if let Some((x, y)) = axis_buffer {
                let scale_factor = window_handle.scale_factor();
                let delta = LogicalPosition::new(x, y).to_physical(scale_factor);

                WindowEvent::MouseWheel {
                    device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(
                        DeviceId,
                    )),
                    delta: MouseScrollDelta::PixelDelta(delta),
                    phase: pointer_data.axis_data.axis_state,
                    modifiers: *pointer_data.modifiers_state.borrow(),
                }
            } else {
                return;
            };

            event_sink.push_window_event(window_event, window_id);
        }
        _ => (),
    }
}

#[inline]
pub(super) fn handle_relative_pointer(event: RelativePointerEvent, winit_state: &mut WinitState) {
    if let RelativePointerEvent::RelativeMotion {
        dx_unaccel,
        dy_unaccel,
        ..
    } = event
    {
        winit_state.event_sink.push_device_event(
            DeviceEvent::MouseMotion {
                delta: (dx_unaccel, dy_unaccel),
            },
            DeviceId,
        )
    }
}