Skip to main content

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