wl_client/test_protocols/core/
wl_keyboard.rs

1use {super::super::all_types::*, crate::builder::prelude::*};
2
3static INTERFACE: wl_interface = wl_interface {
4    name: c"wl_keyboard".as_ptr(),
5    version: 10,
6    method_count: 1,
7    methods: {
8        static MESSAGES: [wl_message; 1] = [wl_message {
9            name: c"release".as_ptr(),
10            signature: c"".as_ptr(),
11            types: {
12                static TYPES: [Option<&'static wl_interface>; 0] = [];
13                TYPES.as_ptr().cast()
14            },
15        }];
16        MESSAGES.as_ptr()
17    },
18    event_count: 2,
19    events: {
20        static MESSAGES: [wl_message; 2] = [
21            wl_message {
22                name: c"key".as_ptr(),
23                signature: c"uuuu".as_ptr(),
24                types: {
25                    static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
26                    TYPES.as_ptr().cast()
27                },
28            },
29            wl_message {
30                name: c"modifiers".as_ptr(),
31                signature: c"uuuuu".as_ptr(),
32                types: {
33                    static TYPES: [Option<&'static wl_interface>; 5] =
34                        [None, None, None, None, None];
35                    TYPES.as_ptr().cast()
36                },
37            },
38        ];
39        MESSAGES.as_ptr()
40    },
41};
42
43/// An owned wl_keyboard proxy.
44///
45/// See the documentation of [the module][self] for the interface description.
46#[derive(Clone, Eq, PartialEq)]
47#[repr(transparent)]
48pub struct WlKeyboard {
49    /// This proxy has the interface INTERFACE.
50    proxy: UntypedOwnedProxy,
51}
52
53/// A borrowed wl_keyboard proxy.
54///
55/// See the documentation of [the module][self] for the interface description.
56#[derive(Eq, PartialEq)]
57#[repr(transparent)]
58pub struct WlKeyboardRef {
59    /// This proxy has the interface INTERFACE.
60    proxy: UntypedBorrowedProxy,
61}
62
63// SAFETY: WlKeyboard is a transparent wrapper around UntypedOwnedProxy
64unsafe impl UntypedOwnedProxyWrapper for WlKeyboard {}
65
66// SAFETY: - INTERFACE is a valid wl_interface
67//         - The only invariant is that self.proxy has a compatible interface
68unsafe impl OwnedProxy for WlKeyboard {
69    const INTERFACE: &'static str = "wl_keyboard";
70    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
71    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
72        private::EventHandler(private::NoOpEventHandler);
73    const MAX_VERSION: u32 = 10;
74
75    type Borrowed = WlKeyboardRef;
76    type Api = private::ProxyApi;
77    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
78}
79
80// SAFETY: WlKeyboardRef is a transparent wrapper around UntypedBorrowedProxy
81unsafe impl UntypedBorrowedProxyWrapper for WlKeyboardRef {}
82
83// SAFETY: - The only invariant is that self.proxy has a compatible interface
84unsafe impl BorrowedProxy for WlKeyboardRef {
85    type Owned = WlKeyboard;
86}
87
88impl Deref for WlKeyboard {
89    type Target = WlKeyboardRef;
90
91    fn deref(&self) -> &Self::Target {
92        proxy::low_level::deref(self)
93    }
94}
95
96mod private {
97    pub struct ProxyApi;
98
99    #[allow(dead_code)]
100    pub struct EventHandler<H>(pub(super) H);
101
102    #[allow(dead_code)]
103    pub struct NoOpEventHandler;
104}
105
106impl Debug for WlKeyboard {
107    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
108        write!(f, "wl_keyboard#{}", self.proxy.id())
109    }
110}
111
112impl Debug for WlKeyboardRef {
113    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
114        write!(f, "wl_keyboard#{}", self.proxy.id())
115    }
116}
117
118impl PartialEq<WlKeyboardRef> for WlKeyboard {
119    fn eq(&self, other: &WlKeyboardRef) -> bool {
120        self.proxy == other.proxy
121    }
122}
123
124impl PartialEq<WlKeyboard> for WlKeyboardRef {
125    fn eq(&self, other: &WlKeyboard) -> bool {
126        self.proxy == other.proxy
127    }
128}
129
130#[allow(dead_code)]
131impl WlKeyboard {
132    /// Since when the release request is available.
133    #[allow(dead_code)]
134    pub const REQ__RELEASE__SINCE: u32 = 3;
135
136    #[inline]
137    pub fn release(&self) {
138        let mut args = [];
139        // SAFETY: - self.proxy has the interface INTERFACE
140        //         - 0 < INTERFACE.method_count = 1
141        //         - the request signature is ``
142        unsafe {
143            self.proxy.send_destructor(0, &mut args);
144        }
145    }
146}
147
148impl WlKeyboard {
149    /// Since when the key event is available.
150    #[allow(dead_code)]
151    pub const EVT__KEY__SINCE: u32 = 1;
152
153    /// Since when the modifiers event is available.
154    #[allow(dead_code)]
155    pub const EVT__MODIFIERS__SINCE: u32 = 1;
156}
157
158/// An event handler for [WlKeyboard] proxies.
159#[allow(dead_code)]
160pub trait WlKeyboardEventHandler {
161    /// # Arguments
162    ///
163    /// - `serial`:
164    /// - `time`:
165    /// - `key`:
166    /// - `state`:
167    #[inline]
168    fn key(
169        &self,
170        _slf: &WlKeyboardRef,
171        serial: u32,
172        time: u32,
173        key: u32,
174        state: WlKeyboardKeyState,
175    ) {
176        let _ = serial;
177        let _ = time;
178        let _ = key;
179        let _ = state;
180    }
181
182    /// # Arguments
183    ///
184    /// - `serial`:
185    /// - `mods_depressed`:
186    /// - `mods_latched`:
187    /// - `mods_locked`:
188    /// - `group`:
189    #[inline]
190    fn modifiers(
191        &self,
192        _slf: &WlKeyboardRef,
193        serial: u32,
194        mods_depressed: u32,
195        mods_latched: u32,
196        mods_locked: u32,
197        group: u32,
198    ) {
199        let _ = serial;
200        let _ = mods_depressed;
201        let _ = mods_latched;
202        let _ = mods_locked;
203        let _ = group;
204    }
205}
206
207impl WlKeyboardEventHandler for private::NoOpEventHandler {}
208
209// SAFETY: INTERFACE is a valid wl_interface
210unsafe impl<H> EventHandler for private::EventHandler<H>
211where
212    H: WlKeyboardEventHandler,
213{
214    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
215
216    #[allow(unused_variables)]
217    unsafe fn handle_event(
218        &self,
219        queue: &Queue,
220        slf: &UntypedBorrowedProxy,
221        opcode: u32,
222        args: *mut wl_argument,
223    ) {
224        // SAFETY: This function required that slf has the interface INTERFACE
225        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlKeyboardRef>(slf) };
226        match opcode {
227            0 => {
228                // SAFETY: INTERFACE requires that there are 4 arguments
229                let args = unsafe { &*args.cast::<[wl_argument; 4]>() };
230                // SAFETY: - INTERFACE requires that args[0] contains a uint
231                let arg0 = unsafe { args[0].u };
232                // SAFETY: - INTERFACE requires that args[1] contains a uint
233                let arg1 = unsafe { args[1].u };
234                // SAFETY: - INTERFACE requires that args[2] contains a uint
235                let arg2 = unsafe { args[2].u };
236                // SAFETY: - INTERFACE requires that args[3] contains a uint
237                let arg3 = unsafe { WlKeyboardKeyState(args[3].u) };
238                self.0.key(slf, arg0, arg1, arg2, arg3);
239            }
240            1 => {
241                // SAFETY: INTERFACE requires that there are 5 arguments
242                let args = unsafe { &*args.cast::<[wl_argument; 5]>() };
243                // SAFETY: - INTERFACE requires that args[0] contains a uint
244                let arg0 = unsafe { args[0].u };
245                // SAFETY: - INTERFACE requires that args[1] contains a uint
246                let arg1 = unsafe { args[1].u };
247                // SAFETY: - INTERFACE requires that args[2] contains a uint
248                let arg2 = unsafe { args[2].u };
249                // SAFETY: - INTERFACE requires that args[3] contains a uint
250                let arg3 = unsafe { args[3].u };
251                // SAFETY: - INTERFACE requires that args[4] contains a uint
252                let arg4 = unsafe { args[4].u };
253                self.0.modifiers(slf, arg0, arg1, arg2, arg3, arg4);
254            }
255            _ => {
256                invalid_opcode("wl_keyboard", opcode);
257            }
258        }
259    }
260}
261
262impl<H> CreateEventHandler<H> for private::ProxyApi
263where
264    H: WlKeyboardEventHandler,
265{
266    type EventHandler = private::EventHandler<H>;
267
268    #[inline]
269    fn create_event_handler(handler: H) -> Self::EventHandler {
270        private::EventHandler(handler)
271    }
272}
273
274impl WlKeyboard {
275    /// Since when the key_state.released enum variant is available.
276    #[allow(dead_code)]
277    pub const ENM__KEY_STATE_RELEASED__SINCE: u32 = 1;
278    /// Since when the key_state.pressed enum variant is available.
279    #[allow(dead_code)]
280    pub const ENM__KEY_STATE_PRESSED__SINCE: u32 = 1;
281    /// Since when the key_state.repeated enum variant is available.
282    #[allow(dead_code)]
283    pub const ENM__KEY_STATE_REPEATED__SINCE: u32 = 1;
284}
285
286#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
287#[allow(dead_code)]
288pub struct WlKeyboardKeyState(pub u32);
289
290impl WlKeyboardKeyState {
291    #[allow(dead_code)]
292    pub const RELEASED: Self = Self(0);
293
294    #[allow(dead_code)]
295    pub const PRESSED: Self = Self(1);
296
297    #[allow(dead_code)]
298    pub const REPEATED: Self = Self(2);
299}
300
301impl Debug for WlKeyboardKeyState {
302    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
303        let name = match *self {
304            Self::RELEASED => "RELEASED",
305            Self::PRESSED => "PRESSED",
306            Self::REPEATED => "REPEATED",
307            _ => return Debug::fmt(&self.0, f),
308        };
309        f.write_str(name)
310    }
311}
312
313/// Functional event handlers.
314pub mod event_handlers {
315    use super::*;
316
317    /// Event handler for key events.
318    pub struct Key<F>(F);
319    impl<F> WlKeyboardEventHandler for Key<F>
320    where
321        F: Fn(&WlKeyboardRef, u32, u32, u32, WlKeyboardKeyState),
322    {
323        #[inline]
324        fn key(
325            &self,
326            _slf: &WlKeyboardRef,
327            serial: u32,
328            time: u32,
329            key: u32,
330            state: WlKeyboardKeyState,
331        ) {
332            self.0(_slf, serial, time, key, state)
333        }
334    }
335
336    /// Event handler for modifiers events.
337    pub struct Modifiers<F>(F);
338    impl<F> WlKeyboardEventHandler for Modifiers<F>
339    where
340        F: Fn(&WlKeyboardRef, u32, u32, u32, u32, u32),
341    {
342        #[inline]
343        fn modifiers(
344            &self,
345            _slf: &WlKeyboardRef,
346            serial: u32,
347            mods_depressed: u32,
348            mods_latched: u32,
349            mods_locked: u32,
350            group: u32,
351        ) {
352            self.0(
353                _slf,
354                serial,
355                mods_depressed,
356                mods_latched,
357                mods_locked,
358                group,
359            )
360        }
361    }
362
363    impl WlKeyboard {
364        /// Creates an event handler for key events.
365        ///
366        /// The event handler ignores all other events.
367        #[allow(dead_code)]
368        pub fn on_key<F>(f: F) -> Key<F>
369        where
370            F: Fn(&WlKeyboardRef, u32, u32, u32, WlKeyboardKeyState),
371        {
372            Key(f)
373        }
374
375        /// Creates an event handler for modifiers events.
376        ///
377        /// The event handler ignores all other events.
378        #[allow(dead_code)]
379        pub fn on_modifiers<F>(f: F) -> Modifiers<F>
380        where
381            F: Fn(&WlKeyboardRef, u32, u32, u32, u32, u32),
382        {
383            Modifiers(f)
384        }
385    }
386}