Skip to main content

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