simple_window/common/protocols_data/wayland/
wl_keyboard.rs

1//! keyboard input device
2//!
3//! The wl_keyboard interface represents one or more keyboards
4//! associated with a seat.
5//!
6//! Each wl_keyboard has the following logical state:
7//!
8//! - an active surface (possibly null),
9//! - the keys currently logically down,
10//! - the active modifiers,
11//! - the active group.
12//!
13//! By default, the active surface is null, the keys currently logically down
14//! are empty, the active modifiers and the active group are 0.
15
16use {super::super::all_types::*, ::wl_client::builder::prelude::*};
17
18static INTERFACE: wl_interface = wl_interface {
19    name: c"wl_keyboard".as_ptr(),
20    version: 10,
21    method_count: 1,
22    methods: {
23        static MESSAGES: [wl_message; 1] = [wl_message {
24            name: c"release".as_ptr(),
25            signature: c"".as_ptr(),
26            types: {
27                static TYPES: [Option<&'static wl_interface>; 0] = [];
28                TYPES.as_ptr().cast()
29            },
30        }];
31        MESSAGES.as_ptr()
32    },
33    event_count: 6,
34    events: {
35        static MESSAGES: [wl_message; 6] = [
36            wl_message {
37                name: c"keymap".as_ptr(),
38                signature: c"uhu".as_ptr(),
39                types: {
40                    static TYPES: [Option<&'static wl_interface>; 3] = [None, None, None];
41                    TYPES.as_ptr().cast()
42                },
43            },
44            wl_message {
45                name: c"enter".as_ptr(),
46                signature: c"uoa".as_ptr(),
47                types: {
48                    static TYPES: [Option<&'static wl_interface>; 3] =
49                        [None, Some(WlSurface::WL_INTERFACE), None];
50                    TYPES.as_ptr().cast()
51                },
52            },
53            wl_message {
54                name: c"leave".as_ptr(),
55                signature: c"uo".as_ptr(),
56                types: {
57                    static TYPES: [Option<&'static wl_interface>; 2] =
58                        [None, Some(WlSurface::WL_INTERFACE)];
59                    TYPES.as_ptr().cast()
60                },
61            },
62            wl_message {
63                name: c"key".as_ptr(),
64                signature: c"uuuu".as_ptr(),
65                types: {
66                    static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
67                    TYPES.as_ptr().cast()
68                },
69            },
70            wl_message {
71                name: c"modifiers".as_ptr(),
72                signature: c"uuuuu".as_ptr(),
73                types: {
74                    static TYPES: [Option<&'static wl_interface>; 5] =
75                        [None, None, None, None, None];
76                    TYPES.as_ptr().cast()
77                },
78            },
79            wl_message {
80                name: c"repeat_info".as_ptr(),
81                signature: c"ii".as_ptr(),
82                types: {
83                    static TYPES: [Option<&'static wl_interface>; 2] = [None, None];
84                    TYPES.as_ptr().cast()
85                },
86            },
87        ];
88        MESSAGES.as_ptr()
89    },
90};
91
92/// An owned wl_keyboard proxy.
93///
94/// See the documentation of [the module][self] for the interface description.
95#[derive(Clone, Eq, PartialEq)]
96#[repr(transparent)]
97pub struct WlKeyboard {
98    /// This proxy has the interface INTERFACE.
99    proxy: UntypedOwnedProxy,
100}
101
102/// A borrowed wl_keyboard proxy.
103///
104/// See the documentation of [the module][self] for the interface description.
105#[derive(Eq, PartialEq)]
106#[repr(transparent)]
107pub struct WlKeyboardRef {
108    /// This proxy has the interface INTERFACE.
109    proxy: UntypedBorrowedProxy,
110}
111
112// SAFETY: WlKeyboard is a transparent wrapper around UntypedOwnedProxy
113unsafe impl UntypedOwnedProxyWrapper for WlKeyboard {}
114
115// SAFETY: - INTERFACE is a valid wl_interface
116//         - The only invariant is that self.proxy has a compatible interface
117unsafe impl OwnedProxy for WlKeyboard {
118    const INTERFACE: &'static str = "wl_keyboard";
119    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
120    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
121        private::EventHandler(private::NoOpEventHandler);
122    const MAX_VERSION: u32 = 10;
123
124    type Borrowed = WlKeyboardRef;
125    type Api = private::ProxyApi;
126    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
127}
128
129// SAFETY: WlKeyboardRef is a transparent wrapper around UntypedBorrowedProxy
130unsafe impl UntypedBorrowedProxyWrapper for WlKeyboardRef {}
131
132// SAFETY: - The only invariant is that self.proxy has a compatible interface
133unsafe impl BorrowedProxy for WlKeyboardRef {
134    type Owned = WlKeyboard;
135}
136
137impl Deref for WlKeyboard {
138    type Target = WlKeyboardRef;
139
140    fn deref(&self) -> &Self::Target {
141        proxy::low_level::deref(self)
142    }
143}
144
145mod private {
146    pub struct ProxyApi;
147
148    #[allow(dead_code)]
149    pub struct EventHandler<H>(pub(super) H);
150
151    #[allow(dead_code)]
152    pub struct NoOpEventHandler;
153}
154
155impl Debug for WlKeyboard {
156    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
157        write!(f, "wl_keyboard#{}", self.proxy.id())
158    }
159}
160
161impl Debug for WlKeyboardRef {
162    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
163        write!(f, "wl_keyboard#{}", self.proxy.id())
164    }
165}
166
167impl PartialEq<WlKeyboardRef> for WlKeyboard {
168    fn eq(&self, other: &WlKeyboardRef) -> bool {
169        self.proxy == other.proxy
170    }
171}
172
173impl PartialEq<WlKeyboard> for WlKeyboardRef {
174    fn eq(&self, other: &WlKeyboard) -> bool {
175        self.proxy == other.proxy
176    }
177}
178
179#[allow(dead_code)]
180impl WlKeyboard {
181    /// Since when the release request is available.
182    #[allow(dead_code)]
183    pub const REQ__RELEASE__SINCE: u32 = 3;
184
185    /// release the keyboard object
186    #[inline]
187    pub fn release(&self) {
188        let mut args = [];
189        // SAFETY: - self.proxy has the interface INTERFACE
190        //         - 0 < INTERFACE.method_count = 1
191        //         - the request signature is ``
192        unsafe {
193            self.proxy.send_destructor(0, &mut args);
194        }
195    }
196}
197
198impl WlKeyboard {
199    /// Since when the keymap event is available.
200    #[allow(dead_code)]
201    pub const EVT__KEYMAP__SINCE: u32 = 1;
202
203    /// Since when the enter event is available.
204    #[allow(dead_code)]
205    pub const EVT__ENTER__SINCE: u32 = 1;
206
207    /// Since when the leave event is available.
208    #[allow(dead_code)]
209    pub const EVT__LEAVE__SINCE: u32 = 1;
210
211    /// Since when the key event is available.
212    #[allow(dead_code)]
213    pub const EVT__KEY__SINCE: u32 = 1;
214
215    /// Since when the modifiers event is available.
216    #[allow(dead_code)]
217    pub const EVT__MODIFIERS__SINCE: u32 = 1;
218
219    /// Since when the repeat_info event is available.
220    #[allow(dead_code)]
221    pub const EVT__REPEAT_INFO__SINCE: u32 = 4;
222}
223
224/// An event handler for [WlKeyboard] proxies.
225#[allow(dead_code)]
226pub trait WlKeyboardEventHandler {
227    type Data: 'static;
228
229    /// keyboard mapping
230    ///
231    /// This event provides a file descriptor to the client which can be
232    /// memory-mapped in read-only mode to provide a keyboard mapping
233    /// description.
234    ///
235    /// From version 7 onwards, the fd must be mapped with MAP_PRIVATE by
236    /// the recipient, as MAP_SHARED may fail.
237    ///
238    /// # Arguments
239    ///
240    /// - `format`: keymap format
241    /// - `fd`: keymap file descriptor
242    /// - `size`: keymap size, in bytes
243    #[inline]
244    fn keymap(
245        &self,
246        _data: &mut Self::Data,
247        _slf: &WlKeyboardRef,
248        format: WlKeyboardKeymapFormat,
249        fd: OwnedFd,
250        size: u32,
251    ) {
252        let _ = format;
253        let _ = fd;
254        let _ = size;
255    }
256
257    /// enter event
258    ///
259    /// Notification that this seat's keyboard focus is on a certain
260    /// surface.
261    ///
262    /// The compositor must send the wl_keyboard.modifiers event after this
263    /// event.
264    ///
265    /// In the wl_keyboard logical state, this event sets the active surface to
266    /// the surface argument and the keys currently logically down to the keys
267    /// in the keys argument. The compositor must not send this event if the
268    /// wl_keyboard already had an active surface immediately before this event.
269    ///
270    /// Clients should not use the list of pressed keys to emulate key-press
271    /// events. The order of keys in the list is unspecified.
272    ///
273    /// # Arguments
274    ///
275    /// - `serial`: serial number of the enter event
276    /// - `surface`: surface gaining keyboard focus
277    /// - `keys`: the keys currently logically down
278    ///
279    /// All borrowed proxies passed to this function are guaranteed to be
280    /// immutable and non-null.
281    #[inline]
282    fn enter(
283        &self,
284        _data: &mut Self::Data,
285        _slf: &WlKeyboardRef,
286        serial: u32,
287        surface: Option<&WlSurfaceRef>,
288        keys: &[u8],
289    ) {
290        let _ = serial;
291        let _ = surface;
292        let _ = keys;
293    }
294
295    /// leave event
296    ///
297    /// Notification that this seat's keyboard focus is no longer on
298    /// a certain surface.
299    ///
300    /// The leave notification is sent before the enter notification
301    /// for the new focus.
302    ///
303    /// In the wl_keyboard logical state, this event resets all values to their
304    /// defaults. The compositor must not send this event if the active surface
305    /// of the wl_keyboard was not equal to the surface argument immediately
306    /// before this event.
307    ///
308    /// # Arguments
309    ///
310    /// - `serial`: serial number of the leave event
311    /// - `surface`: surface that lost keyboard focus
312    ///
313    /// All borrowed proxies passed to this function are guaranteed to be
314    /// immutable and non-null.
315    #[inline]
316    fn leave(
317        &self,
318        _data: &mut Self::Data,
319        _slf: &WlKeyboardRef,
320        serial: u32,
321        surface: Option<&WlSurfaceRef>,
322    ) {
323        let _ = serial;
324        let _ = surface;
325    }
326
327    /// key event
328    ///
329    /// A key was pressed or released.
330    /// The time argument is a timestamp with millisecond
331    /// granularity, with an undefined base.
332    ///
333    /// The key is a platform-specific key code that can be interpreted
334    /// by feeding it to the keyboard mapping (see the keymap event).
335    ///
336    /// If this event produces a change in modifiers, then the resulting
337    /// wl_keyboard.modifiers event must be sent after this event.
338    ///
339    /// In the wl_keyboard logical state, this event adds the key to the keys
340    /// currently logically down (if the state argument is pressed) or removes
341    /// the key from the keys currently logically down (if the state argument is
342    /// released). The compositor must not send this event if the wl_keyboard
343    /// did not have an active surface immediately before this event. The
344    /// compositor must not send this event if state is pressed (resp. released)
345    /// and the key was already logically down (resp. was not logically down)
346    /// immediately before this event.
347    ///
348    /// Since version 10, compositors may send key events with the "repeated"
349    /// key state when a wl_keyboard.repeat_info event with a rate argument of
350    /// 0 has been received. This allows the compositor to take over the
351    /// responsibility of key repetition.
352    ///
353    /// # Arguments
354    ///
355    /// - `serial`: serial number of the key event
356    /// - `time`: timestamp with millisecond granularity
357    /// - `key`: key that produced the event
358    /// - `state`: physical state of the key
359    #[inline]
360    fn key(
361        &self,
362        _data: &mut Self::Data,
363        _slf: &WlKeyboardRef,
364        serial: u32,
365        time: u32,
366        key: u32,
367        state: WlKeyboardKeyState,
368    ) {
369        let _ = serial;
370        let _ = time;
371        let _ = key;
372        let _ = state;
373    }
374
375    /// modifier and group state
376    ///
377    /// Notifies clients that the modifier and/or group state has
378    /// changed, and it should update its local state.
379    ///
380    /// The compositor may send this event without a surface of the client
381    /// having keyboard focus, for example to tie modifier information to
382    /// pointer focus instead. If a modifier event with pressed modifiers is sent
383    /// without a prior enter event, the client can assume the modifier state is
384    /// valid until it receives the next wl_keyboard.modifiers event. In order to
385    /// reset the modifier state again, the compositor can send a
386    /// wl_keyboard.modifiers event with no pressed modifiers.
387    ///
388    /// In the wl_keyboard logical state, this event updates the modifiers and
389    /// group.
390    ///
391    /// # Arguments
392    ///
393    /// - `serial`: serial number of the modifiers event
394    /// - `mods_depressed`: depressed modifiers
395    /// - `mods_latched`: latched modifiers
396    /// - `mods_locked`: locked modifiers
397    /// - `group`: keyboard layout
398    #[inline]
399    fn modifiers(
400        &self,
401        _data: &mut Self::Data,
402        _slf: &WlKeyboardRef,
403        serial: u32,
404        mods_depressed: u32,
405        mods_latched: u32,
406        mods_locked: u32,
407        group: u32,
408    ) {
409        let _ = serial;
410        let _ = mods_depressed;
411        let _ = mods_latched;
412        let _ = mods_locked;
413        let _ = group;
414    }
415
416    /// repeat rate and delay
417    ///
418    /// Informs the client about the keyboard's repeat rate and delay.
419    ///
420    /// This event is sent as soon as the wl_keyboard object has been created,
421    /// and is guaranteed to be received by the client before any key press
422    /// event.
423    ///
424    /// Negative values for either rate or delay are illegal. A rate of zero
425    /// will disable any repeating (regardless of the value of delay).
426    ///
427    /// This event can be sent later on as well with a new value if necessary,
428    /// so clients should continue listening for the event past the creation
429    /// of wl_keyboard.
430    ///
431    /// # Arguments
432    ///
433    /// - `rate`: the rate of repeating keys in characters per second
434    /// - `delay`: delay in milliseconds since key down until repeating starts
435    #[inline]
436    fn repeat_info(&self, _data: &mut Self::Data, _slf: &WlKeyboardRef, rate: i32, delay: i32) {
437        let _ = rate;
438        let _ = delay;
439    }
440}
441
442impl WlKeyboardEventHandler for private::NoOpEventHandler {
443    type Data = ();
444}
445
446// SAFETY: - INTERFACE is a valid wl_interface
447//         - mutable_type always returns the same value
448unsafe impl<H> EventHandler for private::EventHandler<H>
449where
450    H: WlKeyboardEventHandler,
451{
452    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
453
454    #[inline]
455    fn mutable_type() -> Option<(TypeId, &'static str)> {
456        let id = TypeId::of::<H::Data>();
457        let name = std::any::type_name::<H::Data>();
458        Some((id, name))
459    }
460
461    #[allow(unused_variables)]
462    unsafe fn handle_event(
463        &self,
464        queue: &Queue,
465        data: *mut u8,
466        slf: &UntypedBorrowedProxy,
467        opcode: u32,
468        args: *mut wl_argument,
469    ) {
470        // SAFETY: This function requires that slf has the interface INTERFACE
471        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlKeyboardRef>(slf) };
472        // SAFETY: This function requires that data is `&mut T` where `T`
473        //         has the type id returned by `Self::mutable_type`, i.e.,
474        //         `T = H::Data`.
475        let data: &mut H::Data = unsafe { &mut *data.cast() };
476        match opcode {
477            0 => {
478                // SAFETY: INTERFACE requires that there are 3 arguments
479                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
480                // SAFETY: - INTERFACE requires that args[0] contains a uint
481                let arg0 = unsafe { WlKeyboardKeymapFormat(args[0].u) };
482                // SAFETY: - INTERFACE requires that args[1] contains a file descriptor
483                let arg1 = unsafe { OwnedFd::from_raw_fd(args[1].h) };
484                // SAFETY: - INTERFACE requires that args[2] contains a uint
485                let arg2 = unsafe { args[2].u };
486                self.0.keymap(data, slf, arg0, arg1, arg2);
487            }
488            1 => {
489                // SAFETY: INTERFACE requires that there are 3 arguments
490                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
491                // SAFETY: - INTERFACE requires that args[0] contains a uint
492                let arg0 = unsafe { args[0].u };
493                // SAFETY: - INTERFACE requires that args[1] contains an object
494                let arg1 = unsafe {
495                    if let Some(p) = NonNull::new(args[1].o.cast()) {
496                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
497                    } else {
498                        None
499                    }
500                };
501                // SAFETY: - INTERFACE requires that the object has the interface WlSurface::WL_INTERFACE
502                let arg1 = arg1.as_ref().map(|arg1| unsafe {
503                    proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(arg1)
504                });
505                // SAFETY: - INTERFACE requires that args[2] contains an array
506                let arg2 = unsafe {
507                    let a = &*args[2].a;
508                    std::slice::from_raw_parts(a.data.cast(), a.size)
509                };
510                self.0.enter(data, slf, arg0, arg1, arg2);
511            }
512            2 => {
513                // SAFETY: INTERFACE requires that there are 2 arguments
514                let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
515                // SAFETY: - INTERFACE requires that args[0] contains a uint
516                let arg0 = unsafe { args[0].u };
517                // SAFETY: - INTERFACE requires that args[1] contains an object
518                let arg1 = unsafe {
519                    if let Some(p) = NonNull::new(args[1].o.cast()) {
520                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
521                    } else {
522                        None
523                    }
524                };
525                // SAFETY: - INTERFACE requires that the object has the interface WlSurface::WL_INTERFACE
526                let arg1 = arg1.as_ref().map(|arg1| unsafe {
527                    proxy::low_level::from_untyped_borrowed::<WlSurfaceRef>(arg1)
528                });
529                self.0.leave(data, slf, arg0, arg1);
530            }
531            3 => {
532                // SAFETY: INTERFACE requires that there are 4 arguments
533                let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
534                // SAFETY: - INTERFACE requires that args[0] contains a uint
535                let arg0 = unsafe { args[0].u };
536                // SAFETY: - INTERFACE requires that args[1] contains a uint
537                let arg1 = unsafe { args[1].u };
538                // SAFETY: - INTERFACE requires that args[2] contains a uint
539                let arg2 = unsafe { args[2].u };
540                // SAFETY: - INTERFACE requires that args[3] contains a uint
541                let arg3 = unsafe { WlKeyboardKeyState(args[3].u) };
542                self.0.key(data, slf, arg0, arg1, arg2, arg3);
543            }
544            4 => {
545                // SAFETY: INTERFACE requires that there are 5 arguments
546                let args = unsafe { &*args.cast::<[wl_argument; 5]>() };
547                // SAFETY: - INTERFACE requires that args[0] contains a uint
548                let arg0 = unsafe { args[0].u };
549                // SAFETY: - INTERFACE requires that args[1] contains a uint
550                let arg1 = unsafe { args[1].u };
551                // SAFETY: - INTERFACE requires that args[2] contains a uint
552                let arg2 = unsafe { args[2].u };
553                // SAFETY: - INTERFACE requires that args[3] contains a uint
554                let arg3 = unsafe { args[3].u };
555                // SAFETY: - INTERFACE requires that args[4] contains a uint
556                let arg4 = unsafe { args[4].u };
557                self.0.modifiers(data, slf, arg0, arg1, arg2, arg3, arg4);
558            }
559            5 => {
560                // SAFETY: INTERFACE requires that there are 2 arguments
561                let args = unsafe { &*args.cast::<[wl_argument; 2]>() };
562                // SAFETY: - INTERFACE requires that args[0] contains an int
563                let arg0 = unsafe { args[0].i };
564                // SAFETY: - INTERFACE requires that args[1] contains an int
565                let arg1 = unsafe { args[1].i };
566                self.0.repeat_info(data, slf, arg0, arg1);
567            }
568            _ => {
569                invalid_opcode("wl_keyboard", opcode);
570            }
571        }
572    }
573}
574
575impl<H> CreateEventHandler<H> for private::ProxyApi
576where
577    H: WlKeyboardEventHandler,
578{
579    type EventHandler = private::EventHandler<H>;
580
581    #[inline]
582    fn create_event_handler(handler: H) -> Self::EventHandler {
583        private::EventHandler(handler)
584    }
585}
586
587impl WlKeyboard {
588    /// Since when the keymap_format.no_keymap enum variant is available.
589    #[allow(dead_code)]
590    pub const ENM__KEYMAP_FORMAT_NO_KEYMAP__SINCE: u32 = 1;
591    /// Since when the keymap_format.xkb_v1 enum variant is available.
592    #[allow(dead_code)]
593    pub const ENM__KEYMAP_FORMAT_XKB_V1__SINCE: u32 = 1;
594
595    /// Since when the key_state.released enum variant is available.
596    #[allow(dead_code)]
597    pub const ENM__KEY_STATE_RELEASED__SINCE: u32 = 1;
598    /// Since when the key_state.pressed enum variant is available.
599    #[allow(dead_code)]
600    pub const ENM__KEY_STATE_PRESSED__SINCE: u32 = 1;
601    /// Since when the key_state.repeated enum variant is available.
602    #[allow(dead_code)]
603    pub const ENM__KEY_STATE_REPEATED__SINCE: u32 = 10;
604}
605
606/// keyboard mapping format
607///
608/// This specifies the format of the keymap provided to the
609/// client with the wl_keyboard.keymap event.
610#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
611#[allow(dead_code)]
612pub struct WlKeyboardKeymapFormat(pub u32);
613
614impl WlKeyboardKeymapFormat {
615    /// no keymap; client must understand how to interpret the raw keycode
616    #[allow(dead_code)]
617    pub const NO_KEYMAP: Self = Self(0);
618
619    /// libxkbcommon compatible, null-terminated string; to determine the xkb keycode, clients must add 8 to the key event keycode
620    #[allow(dead_code)]
621    pub const XKB_V1: Self = Self(1);
622}
623
624impl Debug for WlKeyboardKeymapFormat {
625    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
626        let name = match *self {
627            Self::NO_KEYMAP => "NO_KEYMAP",
628            Self::XKB_V1 => "XKB_V1",
629            _ => return Debug::fmt(&self.0, f),
630        };
631        f.write_str(name)
632    }
633}
634
635/// physical key state
636///
637/// Describes the physical state of a key that produced the key event.
638///
639/// Since version 10, the key can be in a "repeated" pseudo-state which
640/// means the same as "pressed", but is used to signal repetition in the
641/// key event.
642///
643/// The key may only enter the repeated state after entering the pressed
644/// state and before entering the released state. This event may be
645/// generated multiple times while the key is down.
646#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
647#[allow(dead_code)]
648pub struct WlKeyboardKeyState(pub u32);
649
650impl WlKeyboardKeyState {
651    /// key is not pressed
652    #[allow(dead_code)]
653    pub const RELEASED: Self = Self(0);
654
655    /// key is pressed
656    #[allow(dead_code)]
657    pub const PRESSED: Self = Self(1);
658
659    /// key was repeated
660    #[allow(dead_code)]
661    pub const REPEATED: Self = Self(2);
662}
663
664impl Debug for WlKeyboardKeyState {
665    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
666        let name = match *self {
667            Self::RELEASED => "RELEASED",
668            Self::PRESSED => "PRESSED",
669            Self::REPEATED => "REPEATED",
670            _ => return Debug::fmt(&self.0, f),
671        };
672        f.write_str(name)
673    }
674}
675
676/// Functional event handlers.
677pub mod event_handlers {
678    use super::*;
679
680    /// Event handler for keymap events.
681    pub struct Keymap<T, F>(F, PhantomData<fn(&mut T)>);
682    impl<T, F> WlKeyboardEventHandler for Keymap<T, F>
683    where
684        T: 'static,
685        F: Fn(&mut T, &WlKeyboardRef, WlKeyboardKeymapFormat, OwnedFd, u32),
686    {
687        type Data = T;
688
689        #[inline]
690        fn keymap(
691            &self,
692            _data: &mut T,
693            _slf: &WlKeyboardRef,
694            format: WlKeyboardKeymapFormat,
695            fd: OwnedFd,
696            size: u32,
697        ) {
698            self.0(_data, _slf, format, fd, size)
699        }
700    }
701
702    /// Event handler for enter events.
703    pub struct Enter<T, F>(F, PhantomData<fn(&mut T)>);
704    impl<T, F> WlKeyboardEventHandler for Enter<T, F>
705    where
706        T: 'static,
707        F: Fn(&mut T, &WlKeyboardRef, u32, Option<&WlSurfaceRef>, &[u8]),
708    {
709        type Data = T;
710
711        #[inline]
712        fn enter(
713            &self,
714            _data: &mut T,
715            _slf: &WlKeyboardRef,
716            serial: u32,
717            surface: Option<&WlSurfaceRef>,
718            keys: &[u8],
719        ) {
720            self.0(_data, _slf, serial, surface, keys)
721        }
722    }
723
724    /// Event handler for leave events.
725    pub struct Leave<T, F>(F, PhantomData<fn(&mut T)>);
726    impl<T, F> WlKeyboardEventHandler for Leave<T, F>
727    where
728        T: 'static,
729        F: Fn(&mut T, &WlKeyboardRef, u32, Option<&WlSurfaceRef>),
730    {
731        type Data = T;
732
733        #[inline]
734        fn leave(
735            &self,
736            _data: &mut T,
737            _slf: &WlKeyboardRef,
738            serial: u32,
739            surface: Option<&WlSurfaceRef>,
740        ) {
741            self.0(_data, _slf, serial, surface)
742        }
743    }
744
745    /// Event handler for key events.
746    pub struct Key<T, F>(F, PhantomData<fn(&mut T)>);
747    impl<T, F> WlKeyboardEventHandler for Key<T, F>
748    where
749        T: 'static,
750        F: Fn(&mut T, &WlKeyboardRef, u32, u32, u32, WlKeyboardKeyState),
751    {
752        type Data = T;
753
754        #[inline]
755        fn key(
756            &self,
757            _data: &mut T,
758            _slf: &WlKeyboardRef,
759            serial: u32,
760            time: u32,
761            key: u32,
762            state: WlKeyboardKeyState,
763        ) {
764            self.0(_data, _slf, serial, time, key, state)
765        }
766    }
767
768    /// Event handler for modifiers events.
769    pub struct Modifiers<T, F>(F, PhantomData<fn(&mut T)>);
770    impl<T, F> WlKeyboardEventHandler for Modifiers<T, F>
771    where
772        T: 'static,
773        F: Fn(&mut T, &WlKeyboardRef, u32, u32, u32, u32, u32),
774    {
775        type Data = T;
776
777        #[inline]
778        fn modifiers(
779            &self,
780            _data: &mut T,
781            _slf: &WlKeyboardRef,
782            serial: u32,
783            mods_depressed: u32,
784            mods_latched: u32,
785            mods_locked: u32,
786            group: u32,
787        ) {
788            self.0(
789                _data,
790                _slf,
791                serial,
792                mods_depressed,
793                mods_latched,
794                mods_locked,
795                group,
796            )
797        }
798    }
799
800    /// Event handler for repeat_info events.
801    pub struct RepeatInfo<T, F>(F, PhantomData<fn(&mut T)>);
802    impl<T, F> WlKeyboardEventHandler for RepeatInfo<T, F>
803    where
804        T: 'static,
805        F: Fn(&mut T, &WlKeyboardRef, i32, i32),
806    {
807        type Data = T;
808
809        #[inline]
810        fn repeat_info(&self, _data: &mut T, _slf: &WlKeyboardRef, rate: i32, delay: i32) {
811            self.0(_data, _slf, rate, delay)
812        }
813    }
814
815    impl WlKeyboard {
816        /// Creates an event handler for keymap events.
817        ///
818        /// The event handler ignores all other events.
819        #[allow(dead_code)]
820        pub fn on_keymap<T, F>(f: F) -> Keymap<T, F>
821        where
822            T: 'static,
823            F: Fn(&mut T, &WlKeyboardRef, WlKeyboardKeymapFormat, OwnedFd, u32),
824        {
825            Keymap(f, PhantomData)
826        }
827
828        /// Creates an event handler for enter events.
829        ///
830        /// The event handler ignores all other events.
831        #[allow(dead_code)]
832        pub fn on_enter<T, F>(f: F) -> Enter<T, F>
833        where
834            T: 'static,
835            F: Fn(&mut T, &WlKeyboardRef, u32, Option<&WlSurfaceRef>, &[u8]),
836        {
837            Enter(f, PhantomData)
838        }
839
840        /// Creates an event handler for leave events.
841        ///
842        /// The event handler ignores all other events.
843        #[allow(dead_code)]
844        pub fn on_leave<T, F>(f: F) -> Leave<T, F>
845        where
846            T: 'static,
847            F: Fn(&mut T, &WlKeyboardRef, u32, Option<&WlSurfaceRef>),
848        {
849            Leave(f, PhantomData)
850        }
851
852        /// Creates an event handler for key events.
853        ///
854        /// The event handler ignores all other events.
855        #[allow(dead_code)]
856        pub fn on_key<T, F>(f: F) -> Key<T, F>
857        where
858            T: 'static,
859            F: Fn(&mut T, &WlKeyboardRef, u32, u32, u32, WlKeyboardKeyState),
860        {
861            Key(f, PhantomData)
862        }
863
864        /// Creates an event handler for modifiers events.
865        ///
866        /// The event handler ignores all other events.
867        #[allow(dead_code)]
868        pub fn on_modifiers<T, F>(f: F) -> Modifiers<T, F>
869        where
870            T: 'static,
871            F: Fn(&mut T, &WlKeyboardRef, u32, u32, u32, u32, u32),
872        {
873            Modifiers(f, PhantomData)
874        }
875
876        /// Creates an event handler for repeat_info events.
877        ///
878        /// The event handler ignores all other events.
879        #[allow(dead_code)]
880        pub fn on_repeat_info<T, F>(f: F) -> RepeatInfo<T, F>
881        where
882            T: 'static,
883            F: Fn(&mut T, &WlKeyboardRef, i32, i32),
884        {
885            RepeatInfo(f, PhantomData)
886        }
887    }
888}