1 | //! Relative pointer. |
2 | |
3 | use std::ops::Deref; |
4 | |
5 | use sctk::reexports::client::globals::{BindError, GlobalList}; |
6 | use sctk::reexports::client::{delegate_dispatch, Dispatch}; |
7 | use sctk::reexports::client::{Connection, QueueHandle}; |
8 | use sctk::reexports::protocols::wp::relative_pointer::zv1::{ |
9 | client::zwp_relative_pointer_manager_v1::ZwpRelativePointerManagerV1, |
10 | client::zwp_relative_pointer_v1::{self, ZwpRelativePointerV1}, |
11 | }; |
12 | |
13 | use sctk::globals::GlobalData; |
14 | |
15 | use crate::event::DeviceEvent; |
16 | use crate::platform_impl::wayland::state::WinitState; |
17 | |
18 | /// Wrapper around the relative pointer. |
19 | pub struct RelativePointerState { |
20 | manager: ZwpRelativePointerManagerV1, |
21 | } |
22 | |
23 | impl RelativePointerState { |
24 | /// Create new relative pointer manager. |
25 | pub fn new( |
26 | globals: &GlobalList, |
27 | queue_handle: &QueueHandle<WinitState>, |
28 | ) -> Result<Self, BindError> { |
29 | let manager: as Try>::Output = globals.bind(qh:queue_handle, version:1..=1, udata:GlobalData)?; |
30 | Ok(Self { manager }) |
31 | } |
32 | } |
33 | |
34 | impl Deref for RelativePointerState { |
35 | type Target = ZwpRelativePointerManagerV1; |
36 | |
37 | fn deref(&self) -> &Self::Target { |
38 | &self.manager |
39 | } |
40 | } |
41 | |
42 | impl Dispatch<ZwpRelativePointerManagerV1, GlobalData, WinitState> for RelativePointerState { |
43 | fn event( |
44 | _state: &mut WinitState, |
45 | _proxy: &ZwpRelativePointerManagerV1, |
46 | _event: <ZwpRelativePointerManagerV1 as wayland_client::Proxy>::Event, |
47 | _data: &GlobalData, |
48 | _conn: &Connection, |
49 | _qhandle: &QueueHandle<WinitState>, |
50 | ) { |
51 | } |
52 | } |
53 | |
54 | impl Dispatch<ZwpRelativePointerV1, GlobalData, WinitState> for RelativePointerState { |
55 | fn event( |
56 | state: &mut WinitState, |
57 | _proxy: &ZwpRelativePointerV1, |
58 | event: <ZwpRelativePointerV1 as wayland_client::Proxy>::Event, |
59 | _data: &GlobalData, |
60 | _conn: &Connection, |
61 | _qhandle: &QueueHandle<WinitState>, |
62 | ) { |
63 | let (dx_unaccel, dy_unaccel) = match event { |
64 | zwp_relative_pointer_v1::Event::RelativeMotion { |
65 | dx_unaccel, |
66 | dy_unaccel, |
67 | .. |
68 | } => (dx_unaccel, dy_unaccel), |
69 | _ => return, |
70 | }; |
71 | state.events_sink.push_device_event( |
72 | DeviceEvent::Motion { |
73 | axis: 0, |
74 | value: dx_unaccel, |
75 | }, |
76 | super::DeviceId, |
77 | ); |
78 | state.events_sink.push_device_event( |
79 | DeviceEvent::Motion { |
80 | axis: 1, |
81 | value: dy_unaccel, |
82 | }, |
83 | super::DeviceId, |
84 | ); |
85 | state.events_sink.push_device_event( |
86 | DeviceEvent::MouseMotion { |
87 | delta: (dx_unaccel, dy_unaccel), |
88 | }, |
89 | super::DeviceId, |
90 | ); |
91 | } |
92 | } |
93 | |
94 | delegate_dispatch!(WinitState: [ZwpRelativePointerV1: GlobalData] => RelativePointerState); |
95 | delegate_dispatch!(WinitState: [ZwpRelativePointerManagerV1: GlobalData] => RelativePointerState); |
96 | |