wayland_bridge/
relative_pointer.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::client::{Client, EventQueue};
6use crate::object::{NewObjectExt, ObjectRef, RequestReceiver};
7use anyhow::Error;
8use fuchsia_wayland_core as wl;
9use zwp_relative_pointer_v1_server_protocol::{
10    ZwpRelativePointerManagerV1, ZwpRelativePointerManagerV1Request, ZwpRelativePointerV1,
11    ZwpRelativePointerV1Event, ZwpRelativePointerV1Request,
12};
13
14/// An implementation of the zwp_relative_pointer_v1 global.
15pub struct RelativePointerManager;
16
17impl RequestReceiver<ZwpRelativePointerManagerV1> for RelativePointerManager {
18    fn receive(
19        this: ObjectRef<Self>,
20        request: ZwpRelativePointerManagerV1Request,
21        client: &mut Client,
22    ) -> Result<(), Error> {
23        match request {
24            ZwpRelativePointerManagerV1Request::Destroy => {
25                client.delete_id(this.id())?;
26            }
27            ZwpRelativePointerManagerV1Request::GetRelativePointer { id, pointer: _ } => {
28                let relative_pointer = id.implement(client, RelativePointer)?;
29                // Assert here because if we successfully implemented the
30                // interface the given id is valid. Any failure here indicates
31                // a coherence issue between the client object map and the set
32                // of bound pointers.
33                assert!(client.input_dispatcher.relative_pointers.add(relative_pointer));
34            }
35        }
36        Ok(())
37    }
38}
39
40pub struct RelativePointer;
41
42impl RelativePointer {
43    pub fn post_relative_motion(
44        this: ObjectRef<Self>,
45        event_queue: &EventQueue,
46        time_in_us: u64,
47        dx: wl::Fixed,
48        dy: wl::Fixed,
49    ) -> Result<(), Error> {
50        event_queue.post(
51            this.id(),
52            ZwpRelativePointerV1Event::RelativeMotion {
53                utime_hi: (time_in_us >> 32) as u32,
54                utime_lo: (time_in_us & 0xffffffff) as u32,
55                dx,
56                dy,
57                dx_unaccel: dx,
58                dy_unaccel: dy,
59            },
60        )
61    }
62}
63
64impl RequestReceiver<ZwpRelativePointerV1> for RelativePointer {
65    fn receive(
66        this: ObjectRef<Self>,
67        request: ZwpRelativePointerV1Request,
68        client: &mut Client,
69    ) -> Result<(), Error> {
70        match request {
71            ZwpRelativePointerV1Request::Destroy => {
72                client.input_dispatcher.relative_pointers.remove(this);
73                client.delete_id(this.id())?;
74            }
75        }
76        Ok(())
77    }
78}