simple_window/common/protocols_data/tablet_v2/
zwp_tablet_v2.rs

1//! graphics tablet device
2//!
3//! The wp_tablet interface represents one graphics tablet device. The
4//! tablet interface itself does not generate events; all events are
5//! generated by wp_tablet_tool objects when in proximity above a tablet.
6//!
7//! A tablet has a number of static characteristics, e.g. device name and
8//! pid/vid. These capabilities are sent in an event sequence after the
9//! wp_tablet_seat.tablet_added event. This initial event sequence is
10//! terminated by a wp_tablet.done event.
11
12use {super::super::all_types::*, ::wl_client::builder::prelude::*};
13
14static INTERFACE: wl_interface = wl_interface {
15    name: c"zwp_tablet_v2".as_ptr(),
16    version: 1,
17    method_count: 1,
18    methods: {
19        static MESSAGES: [wl_message; 1] = [wl_message {
20            name: c"destroy".as_ptr(),
21            signature: c"".as_ptr(),
22            types: {
23                static TYPES: [Option<&'static wl_interface>; 0] = [];
24                TYPES.as_ptr().cast()
25            },
26        }];
27        MESSAGES.as_ptr()
28    },
29    event_count: 5,
30    events: {
31        static MESSAGES: [wl_message; 5] = [
32            wl_message {
33                name: c"name".as_ptr(),
34                signature: c"s".as_ptr(),
35                types: {
36                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
37                    TYPES.as_ptr().cast()
38                },
39            },
40            wl_message {
41                name: c"id".as_ptr(),
42                signature: c"uu".as_ptr(),
43                types: {
44                    static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
45                    TYPES.as_ptr().cast()
46                },
47            },
48            wl_message {
49                name: c"path".as_ptr(),
50                signature: c"s".as_ptr(),
51                types: {
52                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
53                    TYPES.as_ptr().cast()
54                },
55            },
56            wl_message {
57                name: c"done".as_ptr(),
58                signature: c"".as_ptr(),
59                types: {
60                    static TYPES: [Option<&'static wl_interface>; 0] = [];
61                    TYPES.as_ptr().cast()
62                },
63            },
64            wl_message {
65                name: c"removed".as_ptr(),
66                signature: c"".as_ptr(),
67                types: {
68                    static TYPES: [Option<&'static wl_interface>; 0] = [];
69                    TYPES.as_ptr().cast()
70                },
71            },
72        ];
73        MESSAGES.as_ptr()
74    },
75};
76
77/// An owned zwp_tablet_v2 proxy.
78///
79/// See the documentation of [the module][self] for the interface description.
80#[derive(Clone, Eq, PartialEq)]
81#[repr(transparent)]
82pub struct ZwpTabletV2 {
83    /// This proxy has the interface INTERFACE.
84    proxy: UntypedOwnedProxy,
85}
86
87/// A borrowed zwp_tablet_v2 proxy.
88///
89/// See the documentation of [the module][self] for the interface description.
90#[derive(Eq, PartialEq)]
91#[repr(transparent)]
92pub struct ZwpTabletV2Ref {
93    /// This proxy has the interface INTERFACE.
94    proxy: UntypedBorrowedProxy,
95}
96
97// SAFETY: ZwpTabletV2 is a transparent wrapper around UntypedOwnedProxy
98unsafe impl UntypedOwnedProxyWrapper for ZwpTabletV2 {}
99
100// SAFETY: - INTERFACE is a valid wl_interface
101//         - The only invariant is that self.proxy has a compatible interface
102unsafe impl OwnedProxy for ZwpTabletV2 {
103    const INTERFACE: &'static str = "zwp_tablet_v2";
104    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
105    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
106        private::EventHandler(private::NoOpEventHandler);
107    const MAX_VERSION: u32 = 1;
108
109    type Borrowed = ZwpTabletV2Ref;
110    type Api = private::ProxyApi;
111    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
112}
113
114// SAFETY: ZwpTabletV2Ref is a transparent wrapper around UntypedBorrowedProxy
115unsafe impl UntypedBorrowedProxyWrapper for ZwpTabletV2Ref {}
116
117// SAFETY: - The only invariant is that self.proxy has a compatible interface
118unsafe impl BorrowedProxy for ZwpTabletV2Ref {
119    type Owned = ZwpTabletV2;
120}
121
122impl Deref for ZwpTabletV2 {
123    type Target = ZwpTabletV2Ref;
124
125    fn deref(&self) -> &Self::Target {
126        proxy::low_level::deref(self)
127    }
128}
129
130mod private {
131    pub struct ProxyApi;
132
133    #[allow(dead_code)]
134    pub struct EventHandler<H>(pub(super) H);
135
136    #[allow(dead_code)]
137    pub struct NoOpEventHandler;
138}
139
140impl Debug for ZwpTabletV2 {
141    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
142        write!(f, "zwp_tablet_v2#{}", self.proxy.id())
143    }
144}
145
146impl Debug for ZwpTabletV2Ref {
147    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
148        write!(f, "zwp_tablet_v2#{}", self.proxy.id())
149    }
150}
151
152impl PartialEq<ZwpTabletV2Ref> for ZwpTabletV2 {
153    fn eq(&self, other: &ZwpTabletV2Ref) -> bool {
154        self.proxy == other.proxy
155    }
156}
157
158impl PartialEq<ZwpTabletV2> for ZwpTabletV2Ref {
159    fn eq(&self, other: &ZwpTabletV2) -> bool {
160        self.proxy == other.proxy
161    }
162}
163
164#[allow(dead_code)]
165impl ZwpTabletV2 {
166    /// Since when the destroy request is available.
167    #[allow(dead_code)]
168    pub const REQ__DESTROY__SINCE: u32 = 1;
169
170    /// destroy the tablet object
171    ///
172    /// This destroys the client's resource for this tablet object.
173    #[inline]
174    pub fn destroy(&self) {
175        let mut args = [];
176        // SAFETY: - self.proxy has the interface INTERFACE
177        //         - 0 < INTERFACE.method_count = 1
178        //         - the request signature is ``
179        unsafe {
180            self.proxy.send_destructor(0, &mut args);
181        }
182    }
183}
184
185impl ZwpTabletV2 {
186    /// Since when the name event is available.
187    #[allow(dead_code)]
188    pub const EVT__NAME__SINCE: u32 = 1;
189
190    /// Since when the id event is available.
191    #[allow(dead_code)]
192    pub const EVT__ID__SINCE: u32 = 1;
193
194    /// Since when the path event is available.
195    #[allow(dead_code)]
196    pub const EVT__PATH__SINCE: u32 = 1;
197
198    /// Since when the done event is available.
199    #[allow(dead_code)]
200    pub const EVT__DONE__SINCE: u32 = 1;
201
202    /// Since when the removed event is available.
203    #[allow(dead_code)]
204    pub const EVT__REMOVED__SINCE: u32 = 1;
205}
206
207/// An event handler for [ZwpTabletV2] proxies.
208#[allow(dead_code)]
209pub trait ZwpTabletV2EventHandler {
210    type Data: 'static;
211
212    /// tablet device name
213    ///
214    /// A descriptive name for the tablet device.
215    ///
216    /// 	If the device has no descriptive name, this event is not sent.
217    ///
218    /// 	This event is sent in the initial burst of events before the
219    /// wp_tablet.done event.
220    ///
221    /// # Arguments
222    ///
223    /// - `name`: the device name
224    #[inline]
225    fn name(&self, _data: &mut Self::Data, _slf: &ZwpTabletV2Ref, name: &str) {
226        let _ = name;
227    }
228
229    /// tablet device USB vendor/product id
230    ///
231    /// The USB vendor and product IDs for the tablet device.
232    ///
233    /// If the device has no USB vendor/product ID, this event is not sent.
234    /// This can happen for virtual devices or non-USB devices, for instance.
235    ///
236    /// This event is sent in the initial burst of events before the
237    /// wp_tablet.done event.
238    ///
239    /// # Arguments
240    ///
241    /// - `vid`: USB vendor id
242    /// - `pid`: USB product id
243    #[inline]
244    fn id(&self, _data: &mut Self::Data, _slf: &ZwpTabletV2Ref, vid: u32, pid: u32) {
245        let _ = vid;
246        let _ = pid;
247    }
248
249    /// path to the device
250    ///
251    /// A system-specific device path that indicates which device is behind
252    /// this wp_tablet. This information may be used to gather additional
253    /// information about the device, e.g. through libwacom.
254    ///
255    /// A device may have more than one device path. If so, multiple
256    /// wp_tablet.path events are sent. A device may be emulated and not
257    /// have a device path, and in that case this event will not be sent.
258    ///
259    /// The format of the path is unspecified, it may be a device node, a
260    /// sysfs path, or some other identifier. It is up to the client to
261    /// identify the string provided.
262    ///
263    /// This event is sent in the initial burst of events before the
264    /// wp_tablet.done event.
265    ///
266    /// # Arguments
267    ///
268    /// - `path`: path to local device
269    #[inline]
270    fn path(&self, _data: &mut Self::Data, _slf: &ZwpTabletV2Ref, path: &str) {
271        let _ = path;
272    }
273
274    /// tablet description events sequence complete
275    ///
276    /// This event is sent immediately to signal the end of the initial
277    /// burst of descriptive events. A client may consider the static
278    /// description of the tablet to be complete and finalize initialization
279    /// of the tablet.
280    #[inline]
281    fn done(&self, _data: &mut Self::Data, _slf: &ZwpTabletV2Ref) {}
282
283    /// tablet removed event
284    ///
285    /// Sent when the tablet has been removed from the system. When a tablet
286    /// is removed, some tools may be removed.
287    ///
288    /// When this event is received, the client must wp_tablet.destroy
289    /// the object.
290    #[inline]
291    fn removed(&self, _data: &mut Self::Data, _slf: &ZwpTabletV2Ref) {}
292}
293
294impl ZwpTabletV2EventHandler for private::NoOpEventHandler {
295    type Data = ();
296}
297
298// SAFETY: - INTERFACE is a valid wl_interface
299//         - mutable_type always returns the same value
300unsafe impl<H> EventHandler for private::EventHandler<H>
301where
302    H: ZwpTabletV2EventHandler,
303{
304    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
305
306    #[inline]
307    fn mutable_type() -> Option<(TypeId, &'static str)> {
308        let id = TypeId::of::<H::Data>();
309        let name = std::any::type_name::<H::Data>();
310        Some((id, name))
311    }
312
313    #[allow(unused_variables)]
314    unsafe fn handle_event(
315        &self,
316        queue: &Queue,
317        data: *mut u8,
318        slf: &UntypedBorrowedProxy,
319        opcode: u32,
320        args: *mut wl_argument,
321    ) {
322        // SAFETY: This function requires that slf has the interface INTERFACE
323        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<ZwpTabletV2Ref>(slf) };
324        // SAFETY: This function requires that data is `&mut T` where `T`
325        //         has the type id returned by `Self::mutable_type`, i.e.,
326        //         `T = H::Data`.
327        let data: &mut H::Data = unsafe { &mut *data.cast() };
328        match opcode {
329            0 => {
330                // SAFETY: INTERFACE requires that there are 1 arguments
331                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
332                // SAFETY: - INTERFACE requires that args[0] contains a string
333                //         - if the pointer is not null, then it is a c string
334                let arg0 = unsafe { convert_string_arg("zwp_tablet_v2", "name", args[0].s) };
335                self.0.name(data, slf, arg0);
336            }
337            1 => {
338                // SAFETY: INTERFACE requires that there are 2 arguments
339                let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
340                // SAFETY: - INTERFACE requires that args[0] contains a uint
341                let arg0 = unsafe { args[0].u };
342                // SAFETY: - INTERFACE requires that args[1] contains a uint
343                let arg1 = unsafe { args[1].u };
344                self.0.id(data, slf, arg0, arg1);
345            }
346            2 => {
347                // SAFETY: INTERFACE requires that there are 1 arguments
348                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
349                // SAFETY: - INTERFACE requires that args[0] contains a string
350                //         - if the pointer is not null, then it is a c string
351                let arg0 = unsafe { convert_string_arg("zwp_tablet_v2", "path", args[0].s) };
352                self.0.path(data, slf, arg0);
353            }
354            3 => {
355                self.0.done(data, slf);
356            }
357            4 => {
358                self.0.removed(data, slf);
359            }
360            _ => {
361                invalid_opcode("zwp_tablet_v2", opcode);
362            }
363        }
364    }
365}
366
367impl<H> CreateEventHandler<H> for private::ProxyApi
368where
369    H: ZwpTabletV2EventHandler,
370{
371    type EventHandler = private::EventHandler<H>;
372
373    #[inline]
374    fn create_event_handler(handler: H) -> Self::EventHandler {
375        private::EventHandler(handler)
376    }
377}
378
379/// Functional event handlers.
380pub mod event_handlers {
381    use super::*;
382
383    /// Event handler for name events.
384    pub struct Name<T, F>(F, PhantomData<fn(&mut T)>);
385    impl<T, F> ZwpTabletV2EventHandler for Name<T, F>
386    where
387        T: 'static,
388        F: Fn(&mut T, &ZwpTabletV2Ref, &str),
389    {
390        type Data = T;
391
392        #[inline]
393        fn name(&self, _data: &mut T, _slf: &ZwpTabletV2Ref, name: &str) {
394            self.0(_data, _slf, name)
395        }
396    }
397
398    /// Event handler for id events.
399    pub struct Id<T, F>(F, PhantomData<fn(&mut T)>);
400    impl<T, F> ZwpTabletV2EventHandler for Id<T, F>
401    where
402        T: 'static,
403        F: Fn(&mut T, &ZwpTabletV2Ref, u32, u32),
404    {
405        type Data = T;
406
407        #[inline]
408        fn id(&self, _data: &mut T, _slf: &ZwpTabletV2Ref, vid: u32, pid: u32) {
409            self.0(_data, _slf, vid, pid)
410        }
411    }
412
413    /// Event handler for path events.
414    pub struct Path<T, F>(F, PhantomData<fn(&mut T)>);
415    impl<T, F> ZwpTabletV2EventHandler for Path<T, F>
416    where
417        T: 'static,
418        F: Fn(&mut T, &ZwpTabletV2Ref, &str),
419    {
420        type Data = T;
421
422        #[inline]
423        fn path(&self, _data: &mut T, _slf: &ZwpTabletV2Ref, path: &str) {
424            self.0(_data, _slf, path)
425        }
426    }
427
428    /// Event handler for done events.
429    pub struct Done<T, F>(F, PhantomData<fn(&mut T)>);
430    impl<T, F> ZwpTabletV2EventHandler for Done<T, F>
431    where
432        T: 'static,
433        F: Fn(&mut T, &ZwpTabletV2Ref),
434    {
435        type Data = T;
436
437        #[inline]
438        fn done(&self, _data: &mut T, _slf: &ZwpTabletV2Ref) {
439            self.0(_data, _slf)
440        }
441    }
442
443    /// Event handler for removed events.
444    pub struct Removed<T, F>(F, PhantomData<fn(&mut T)>);
445    impl<T, F> ZwpTabletV2EventHandler for Removed<T, F>
446    where
447        T: 'static,
448        F: Fn(&mut T, &ZwpTabletV2Ref),
449    {
450        type Data = T;
451
452        #[inline]
453        fn removed(&self, _data: &mut T, _slf: &ZwpTabletV2Ref) {
454            self.0(_data, _slf)
455        }
456    }
457
458    impl ZwpTabletV2 {
459        /// Creates an event handler for name events.
460        ///
461        /// The event handler ignores all other events.
462        #[allow(dead_code)]
463        pub fn on_name<T, F>(f: F) -> Name<T, F>
464        where
465            T: 'static,
466            F: Fn(&mut T, &ZwpTabletV2Ref, &str),
467        {
468            Name(f, PhantomData)
469        }
470
471        /// Creates an event handler for id events.
472        ///
473        /// The event handler ignores all other events.
474        #[allow(dead_code)]
475        pub fn on_id<T, F>(f: F) -> Id<T, F>
476        where
477            T: 'static,
478            F: Fn(&mut T, &ZwpTabletV2Ref, u32, u32),
479        {
480            Id(f, PhantomData)
481        }
482
483        /// Creates an event handler for path events.
484        ///
485        /// The event handler ignores all other events.
486        #[allow(dead_code)]
487        pub fn on_path<T, F>(f: F) -> Path<T, F>
488        where
489            T: 'static,
490            F: Fn(&mut T, &ZwpTabletV2Ref, &str),
491        {
492            Path(f, PhantomData)
493        }
494
495        /// Creates an event handler for done events.
496        ///
497        /// The event handler ignores all other events.
498        #[allow(dead_code)]
499        pub fn on_done<T, F>(f: F) -> Done<T, F>
500        where
501            T: 'static,
502            F: Fn(&mut T, &ZwpTabletV2Ref),
503        {
504            Done(f, PhantomData)
505        }
506
507        /// Creates an event handler for removed events.
508        ///
509        /// The event handler ignores all other events.
510        #[allow(dead_code)]
511        pub fn on_removed<T, F>(f: F) -> Removed<T, F>
512        where
513            T: 'static,
514            F: Fn(&mut T, &ZwpTabletV2Ref),
515        {
516            Removed(f, PhantomData)
517        }
518    }
519}