druid-shell 0.8.0

Platform abstracting application shell used for Druid toolkit.
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2022 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::VecDeque;
use wayland_client::protocol::wl_pointer;
use wayland_client::protocol::wl_surface::{self, WlSurface};
use wayland_client::{self as wl};
use wayland_cursor::CursorImageBuffer;
use wayland_cursor::CursorTheme;

use crate::keyboard::Modifiers;
use crate::kurbo::{Point, Vec2};
use crate::mouse;

use super::application::Data;

// Button constants (linux specific)
const BTN_LEFT: u32 = 0x110;
const BTN_RIGHT: u32 = 0x111;
const BTN_MIDDLE: u32 = 0x112;

// used to keep track of click event counts.
#[derive(Debug, Clone)]
struct ClickDebouncer {
    timestamp: std::time::Instant,
    count: u8,
    previous: mouse::MouseButton,
}

impl Default for ClickDebouncer {
    fn default() -> Self {
        Self {
            timestamp: std::time::Instant::now(),
            count: 1,
            previous: mouse::MouseButton::None,
        }
    }
}

impl ClickDebouncer {
    // this threshold was arbitrarily chosen based on experimention.
    // there is likely a better default based on research to use.
    // during experimentation this allowed one to get to around 4 clicks.
    // but likely heavily dependent on the machine.
    const THRESHOLD: std::time::Duration = std::time::Duration::from_millis(500);

    fn reset(ts: std::time::Instant, btn: mouse::MouseButton) -> Self {
        Self {
            timestamp: ts,
            count: 1,
            previous: btn,
        }
    }

    fn debounce(&mut self, current: MouseEvtKind) -> MouseEvtKind {
        let ts = std::time::Instant::now();

        // reset counting and button.
        if self.timestamp + ClickDebouncer::THRESHOLD < ts {
            *self = ClickDebouncer::default();
        }

        match current {
            MouseEvtKind::Up(mut evt) if self.previous == evt.button => {
                evt.count = self.count;
                MouseEvtKind::Up(evt)
            }
            MouseEvtKind::Down(mut evt) if self.previous == evt.button => {
                self.count += 1;
                evt.count = self.count;
                MouseEvtKind::Down(evt)
            }
            MouseEvtKind::Down(evt) if self.previous != evt.button => {
                *self = ClickDebouncer::reset(ts, evt.button);
                MouseEvtKind::Down(evt)
            }
            MouseEvtKind::Leave => {
                *self = ClickDebouncer::reset(ts, mouse::MouseButton::None);
                current
            }
            _ => current,
        }
    }
}

/// Collect up mouse events then emit them together on a pointer frame.
pub(crate) struct Pointer {
    /// The image surface which contains the cursor image.
    pub(crate) cursor_surface: wl::Main<WlSurface>,
    /// Events that have occurred since the last frame.
    pub(crate) queued_events: std::cell::RefCell<VecDeque<PointerEvent>>,
    /// Currently pressed buttons
    buttons: std::cell::RefCell<mouse::MouseButtons>,
    /// Current position
    pos: std::cell::Cell<Point>,
    wl_pointer: std::cell::RefCell<Option<wl_pointer::WlPointer>>,
    // used to keep track of the current clicking
    clickevent: std::cell::RefCell<ClickDebouncer>,
    /// cursor theme data.
    theme: std::cell::RefCell<CursorTheme>,
    /// Cache the current cursor, so we can see if it changed
    current_cursor: std::cell::RefCell<mouse::Cursor>,
}

/// Raw wayland pointer events.
#[derive(Debug)]
pub(crate) enum PointerEvent {
    /// Mouse moved/entered
    Motion {
        pointer: wl_pointer::WlPointer,
        point: Point,
    },
    /// Mouse button pressed/released
    Button {
        button: u32,
        state: wl_pointer::ButtonState,
    },
    /// Axis movement
    Axis { axis: wl_pointer::Axis, value: f64 },
    /// Mouse left
    Leave,
}

/// An enum that we will convert into the different callbacks.
#[derive(Debug)]
pub(crate) enum MouseEvtKind {
    Move(mouse::MouseEvent),
    Up(mouse::MouseEvent),
    Down(mouse::MouseEvent),
    Leave,
    Wheel(mouse::MouseEvent),
}

#[allow(unused)]
impl Pointer {
    /// Create a new pointer
    pub fn new(theme: CursorTheme, cursor: wl::Main<WlSurface>) -> Self {
        // ignore all events
        cursor.quick_assign(|a1, event, a2| {
            tracing::trace!("pointer surface event {:?} {:?} {:?}", a1, event, a2);
        });

        Pointer {
            theme: std::cell::RefCell::new(theme),
            buttons: std::cell::RefCell::new(mouse::MouseButtons::new()),
            pos: std::cell::Cell::new(Point::ZERO), // will get set before we emit any events
            queued_events: std::cell::RefCell::new(VecDeque::with_capacity(3)), // should be enough most of the time
            cursor_surface: cursor,
            wl_pointer: std::cell::RefCell::new(None),
            current_cursor: std::cell::RefCell::new(mouse::Cursor::Arrow),
            clickevent: std::cell::RefCell::new(ClickDebouncer::default()),
        }
    }

    pub fn attach(&self, current: wl_pointer::WlPointer) {
        tracing::trace!("attaching pointer reference {:?}", current);
        self.wl_pointer.replace(Some(current));
    }

    #[inline]
    pub fn push(&self, event: PointerEvent) {
        self.queued_events.borrow_mut().push_back(event);
    }

    #[inline]
    pub fn pop(&self) -> Option<PointerEvent> {
        self.queued_events.borrow_mut().pop_front()
    }

    #[inline]
    pub fn cursor(&self) -> &WlSurface {
        &self.cursor_surface
    }

    pub fn replace(&self, cursor: &mouse::Cursor) {
        let current = self.current_cursor.borrow().clone();
        let cursor = cursor.clone();

        // Setting a new cursor involves communicating with the server, so don't do it if we
        // don't have to.
        if current == cursor {
            return;
        }

        let b = self.wl_pointer.borrow_mut();
        let wl_pointer = match &*b {
            None => return,
            Some(p) => p,
        };

        tracing::trace!("replacing cursor {:?} -> {:?}", current, cursor);
        let buffer = match self.get_cursor_buffer(&cursor) {
            None => return,
            Some(b) => b,
        };

        let (hot_x, hot_y) = buffer.hotspot();
        self.current_cursor.replace(cursor);
        wl_pointer.set_cursor(0, Some(&self.cursor_surface), hot_x as i32, hot_y as i32);
        self.cursor_surface.attach(Some(&*buffer), 0, 0);

        if self.cursor_surface.as_ref().version() >= wl_surface::REQ_DAMAGE_BUFFER_SINCE {
            self.cursor_surface.damage_buffer(0, 0, i32::MAX, i32::MAX);
        } else {
            self.cursor_surface.damage(0, 0, i32::MAX, i32::MAX);
        }

        self.cursor_surface.commit();
    }

    fn get_cursor_buffer(&self, cursor: &mouse::Cursor) -> Option<CursorImageBuffer> {
        #[allow(deprecated)]
        match cursor {
            mouse::Cursor::Arrow => self.unpack_image_buffer("left_ptr"),
            mouse::Cursor::IBeam => self.unpack_image_buffer("xterm"),
            mouse::Cursor::Crosshair => self.unpack_image_buffer("cross"),
            mouse::Cursor::OpenHand => self.unpack_image_buffer("openhand"),
            mouse::Cursor::NotAllowed => self.unpack_image_buffer("X_cursor"),
            mouse::Cursor::ResizeLeftRight => self.unpack_image_buffer("row-resize"),
            mouse::Cursor::ResizeUpDown => self.unpack_image_buffer("col-resize"),
            mouse::Cursor::Pointer => self.unpack_image_buffer("pointer"),
            mouse::Cursor::Custom(_) => {
                tracing::warn!("custom cursors not implemented");
                self.unpack_image_buffer("left_ptr")
            }
        }
    }

    // Just use the first image, people using animated cursors have already made bad life
    // choices and shouldn't expect it to work.
    fn unpack_image_buffer(&self, name: &str) -> Option<CursorImageBuffer> {
        self.theme
            .borrow_mut()
            .get_cursor(name)
            .map(|c| c[c.frame_and_duration(0).frame_index].clone())
    }

    pub(super) fn consume(
        appdata: std::sync::Arc<Data>,
        source: wl_pointer::WlPointer,
        event: wl_pointer::Event,
    ) {
        match event {
            wl_pointer::Event::Enter {
                surface,
                surface_x,
                surface_y,
                ..
            } => {
                appdata.pointer.push(PointerEvent::Motion {
                    point: Point::new(surface_x, surface_y),
                    pointer: source,
                });
            }
            wl_pointer::Event::Leave { surface, .. } => {
                appdata.pointer.push(PointerEvent::Leave);
            }
            wl_pointer::Event::Motion {
                surface_x,
                surface_y,
                ..
            } => {
                appdata.pointer.push(PointerEvent::Motion {
                    point: Point::new(surface_x, surface_y),
                    pointer: source,
                });
            }
            wl_pointer::Event::Button { button, state, .. } => {
                appdata.pointer.push(PointerEvent::Button { button, state });
            }
            wl_pointer::Event::Axis { axis, value, .. } => {
                appdata.pointer.push(PointerEvent::Axis { axis, value });
            }
            wl_pointer::Event::Frame => {
                let winhandle = match appdata.acquire_current_window().and_then(|w| w.data()) {
                    Some(w) => w,
                    None => {
                        tracing::warn!("dropping mouse events, no window available");
                        appdata.pointer.queued_events.borrow_mut().clear();
                        return;
                    }
                };
                let mut winhandle = winhandle.handler.borrow_mut();

                // (re-entrancy) call user code
                while let Some(event) = appdata.pointer.dequeue() {
                    match event {
                        MouseEvtKind::Move(evt) => winhandle.mouse_move(&evt),
                        MouseEvtKind::Up(evt) => winhandle.mouse_up(&evt),
                        MouseEvtKind::Down(evt) => winhandle.mouse_down(&evt),
                        MouseEvtKind::Wheel(evt) => winhandle.wheel(&evt),
                        MouseEvtKind::Leave => winhandle.mouse_leave(),
                    }
                }
            }
            evt => {
                log::warn!("Unhandled pointer event: {:?}", evt);
            }
        }
    }

    fn dequeue(&self) -> Option<MouseEvtKind> {
        use wl_pointer::{Axis, ButtonState};
        // sometimes we need to ignore an event and move on
        loop {
            let event = self.queued_events.borrow_mut().pop_front()?;
            tracing::trace!("mouse event {:?}", event);
            match event {
                PointerEvent::Motion { pointer, point } => {
                    self.pos.replace(point);
                    return Some(MouseEvtKind::Move(mouse::MouseEvent {
                        pos: point,
                        buttons: *self.buttons.borrow(),
                        mods: Modifiers::empty(),
                        count: 0,
                        focus: false,
                        button: mouse::MouseButton::None,
                        wheel_delta: Vec2::ZERO,
                    }));
                }
                PointerEvent::Button { button, state } => {
                    let button = match linux_to_mouse_button(button) {
                        // Skip unsupported buttons.
                        None => {
                            tracing::debug!("unsupported button click {:?}", button);
                            continue;
                        }
                        Some(b) => b,
                    };
                    let evt = match state {
                        ButtonState::Pressed => {
                            self.buttons.borrow_mut().insert(button);
                            self.clickevent.borrow_mut().debounce(MouseEvtKind::Down(
                                mouse::MouseEvent {
                                    pos: self.pos.get(),
                                    buttons: *self.buttons.borrow(),
                                    mods: Modifiers::empty(),
                                    count: 1,
                                    focus: false,
                                    button,
                                    wheel_delta: Vec2::ZERO,
                                },
                            ))
                        }
                        ButtonState::Released => {
                            self.buttons.borrow_mut().remove(button);
                            self.clickevent.borrow_mut().debounce(MouseEvtKind::Up(
                                mouse::MouseEvent {
                                    pos: self.pos.get(),
                                    buttons: *self.buttons.borrow(),
                                    mods: Modifiers::empty(),
                                    count: 0,
                                    focus: false,
                                    button,
                                    wheel_delta: Vec2::ZERO,
                                },
                            ))
                        }
                        _ => {
                            log::error!("mouse button changed, but not pressed or released");
                            continue;
                        }
                    };
                    return Some(evt);
                }
                PointerEvent::Axis { axis, value } => {
                    let wheel_delta = match axis {
                        Axis::VerticalScroll => Vec2::new(0., value),
                        Axis::HorizontalScroll => Vec2::new(value, 0.),
                        _ => {
                            log::error!("axis direction not vertical or horizontal");
                            continue;
                        }
                    };
                    return Some(MouseEvtKind::Wheel(mouse::MouseEvent {
                        pos: self.pos.get(),
                        buttons: *self.buttons.borrow(),
                        mods: Modifiers::empty(),
                        count: 0,
                        focus: false,
                        button: mouse::MouseButton::None,
                        wheel_delta,
                    }));
                }
                PointerEvent::Leave => {
                    // The parent will remove us.
                    return Some(MouseEvtKind::Leave);
                }
            }
        }
    }
}

impl Drop for Pointer {
    fn drop(&mut self) {
        self.cursor_surface.destroy();
    }
}

#[inline]
fn linux_to_mouse_button(button: u32) -> Option<mouse::MouseButton> {
    match button {
        BTN_LEFT => Some(mouse::MouseButton::Left),
        BTN_RIGHT => Some(mouse::MouseButton::Right),
        BTN_MIDDLE => Some(mouse::MouseButton::Middle),
        _ => None,
    }
}