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
use std::sync::Mutex;

use wayland_client::protocol::wl_seat::WlSeat;

use wayland_client::protocol::wl_surface::WlSurface;
use wayland_client::protocol::wl_touch::{Event as TouchEvent, WlTouch};
use wayland_client::{Connection, Dispatch, QueueHandle};

use crate::seat::SeatState;

#[derive(Debug)]
pub struct TouchData {
    seat: WlSeat,

    inner: Mutex<TouchDataInner>,
}

impl TouchData {
    /// Create the new touch data associated with the given seat.
    pub fn new(seat: WlSeat) -> Self {
        Self { seat, inner: Default::default() }
    }

    /// Get the associated seat from the data.
    pub fn seat(&self) -> &WlSeat {
        &self.seat
    }

    /// Serial from the latest touch down event.
    pub fn latest_down_serial(&self) -> Option<u32> {
        self.inner.lock().unwrap().latest_down
    }
}

#[derive(Debug, Default)]
pub(crate) struct TouchDataInner {
    events: Vec<TouchEvent>,
    active_touch_points: Vec<i32>,

    /// The serial of the latest touch down event
    latest_down: Option<u32>,
}

#[macro_export]
macro_rules! delegate_touch {
    ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
        $crate::delegate_touch!(@{ $(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty }; touch: $crate::seat::touch::TouchData);
    };
    ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty, touch: [$($td:ty),* $(,)?]) => {
        $crate::delegate_touch!(@{ $(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty }; [ $($td),* ]);
    };
    (@{$($ty:tt)*}; touch: $td:ty) => {
        $crate::reexports::client::delegate_dispatch!($($ty)*:
            [
                $crate::reexports::client::protocol::wl_touch::WlTouch: $td
            ] => $crate::seat::SeatState
        );
    };
    (@$ty:tt; [$($td:ty),*] ) => {
        $(
            $crate::delegate_touch!(@$ty, touch: $td);
        )*
    };
}

pub trait TouchDataExt: Send + Sync {
    fn touch_data(&self) -> &TouchData;
}

impl TouchDataExt for TouchData {
    fn touch_data(&self) -> &TouchData {
        self
    }
}

pub trait TouchHandler: Sized {
    /// New touch point.
    ///
    /// Indicates a new touch point has appeared on the surface, starting a touch sequence. The ID
    /// associated with this event identifies this touch point for devices with multi-touch and
    /// will be referenced in future events.
    ///
    /// The associated touch ID ceases to be valid after the touch up event with the associated ID
    /// and may be reused for other touch points after that.
    ///
    /// Coordinates are surface-local.
    #[allow(clippy::too_many_arguments)]
    fn down(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        touch: &WlTouch,
        serial: u32,
        time: u32,
        surface: WlSurface,
        id: i32,
        position: (f64, f64),
    );

    /// End of touch sequence.
    fn up(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        touch: &WlTouch,
        serial: u32,
        time: u32,
        id: i32,
    );

    /// Touch point motion.
    ///
    /// Coordinates are surface-local.
    fn motion(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        touch: &WlTouch,
        time: u32,
        id: i32,
        position: (f64, f64),
    );

    /// Touch point shape change.
    ///
    /// The shape of a touch point is approximated by an ellipse through the major and minor axis
    /// length. Major always represents the larger of the two axis and is orthogonal to minor.
    ///
    /// The dimensions are specified in surface-local coordinates and the locations reported by
    /// other events always report the center of the ellipse.
    fn shape(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        touch: &WlTouch,
        id: i32,
        major: f64,
        minor: f64,
    );

    /// Touch point shape orientation.
    ///
    /// The orientation describes the clockwise angle of a touch point's major axis to the positive
    /// surface y-axis and is normalized to the -180° to +180° range.
    fn orientation(
        &mut self,
        conn: &Connection,
        qh: &QueueHandle<Self>,
        touch: &WlTouch,
        id: i32,
        orientation: f64,
    );

    /// Cancel active touch sequence.
    ///
    /// This indicates that the compositor has cancelled the active touch sequence, for example due
    /// to detection of a touch gesture.
    fn cancel(&mut self, conn: &Connection, qh: &QueueHandle<Self>, touch: &WlTouch);
}

impl<D, U> Dispatch<WlTouch, U, D> for SeatState
where
    D: Dispatch<WlTouch, U> + TouchHandler,
    U: TouchDataExt,
{
    fn event(
        data: &mut D,
        touch: &WlTouch,
        event: TouchEvent,
        udata: &U,
        conn: &Connection,
        qh: &QueueHandle<D>,
    ) {
        let udata = udata.touch_data();
        let mut guard: std::sync::MutexGuard<'_, TouchDataInner> = udata.inner.lock().unwrap();

        let mut save_event = false;
        let mut process_events = false;

        match &event {
            // Buffer events until frame is received.
            TouchEvent::Down { serial, id, .. } => {
                guard.latest_down = Some(*serial);
                save_event = true;
                if let Err(insert_pos) = guard.active_touch_points.binary_search(id) {
                    guard.active_touch_points.insert(insert_pos, *id);
                }
            }
            TouchEvent::Up { id, .. } => {
                save_event = true;
                if let Ok(remove_pos) = guard.active_touch_points.binary_search(id) {
                    guard.active_touch_points.remove(remove_pos);
                }

                // Weston doesn't always send a frame even after the last touch point was released:
                // https://gitlab.freedesktop.org/wayland/weston/-/issues/44
                // Work around this by processing pending events when there are no more touch points
                // active.
                if guard.active_touch_points.is_empty() {
                    process_events = true;
                }
            }
            TouchEvent::Motion { .. }
            | TouchEvent::Shape { .. }
            | TouchEvent::Orientation { .. } => {
                save_event = true;
            }
            // Process all buffered events.
            TouchEvent::Frame => {
                process_events = true;
            }
            TouchEvent::Cancel => {
                guard.events.clear();
                guard.active_touch_points.clear();

                data.cancel(conn, qh, touch);
            }
            _ => unreachable!(),
        }

        if save_event {
            guard.events.push(event);
        }

        if process_events {
            for event in guard.events.drain(..) {
                process_framed_event(data, touch, conn, qh, event);
            }
        }
    }
}

/// Process a single frame-buffered touch event.
fn process_framed_event<D>(
    data: &mut D,
    touch: &WlTouch,
    conn: &Connection,
    qh: &QueueHandle<D>,
    event: TouchEvent,
) where
    D: TouchHandler,
{
    match event {
        TouchEvent::Down { serial, time, surface, id, x, y } => {
            data.down(conn, qh, touch, serial, time, surface, id, (x, y));
        }
        TouchEvent::Up { serial, time, id } => {
            data.up(conn, qh, touch, serial, time, id);
        }
        TouchEvent::Motion { time, id, x, y } => {
            data.motion(conn, qh, touch, time, id, (x, y));
        }
        TouchEvent::Shape { id, major, minor } => {
            data.shape(conn, qh, touch, id, major, minor);
        }
        TouchEvent::Orientation { id, orientation } => {
            data.orientation(conn, qh, touch, id, orientation);
        }
        // No other events should be frame-buffered.
        _ => unreachable!(),
    }
}