wl_client/test_protocols/core/
wl_seat.rs

1use {super::super::all_types::*, crate::builder::prelude::*};
2
3static INTERFACE: wl_interface = wl_interface {
4    name: c"wl_seat".as_ptr(),
5    version: 10,
6    method_count: 2,
7    methods: {
8        static MESSAGES: [wl_message; 2] = [
9            wl_message {
10                name: c"get_keyboard".as_ptr(),
11                signature: c"n".as_ptr(),
12                types: {
13                    static TYPES: [Option<&'static wl_interface>; 1] =
14                        [Some(WlKeyboard::WL_INTERFACE)];
15                    TYPES.as_ptr().cast()
16                },
17            },
18            wl_message {
19                name: c"release".as_ptr(),
20                signature: c"".as_ptr(),
21                types: {
22                    static TYPES: [Option<&'static wl_interface>; 0] = [];
23                    TYPES.as_ptr().cast()
24                },
25            },
26        ];
27        MESSAGES.as_ptr()
28    },
29    event_count: 1,
30    events: {
31        static MESSAGES: [wl_message; 1] = [wl_message {
32            name: c"capabilities".as_ptr(),
33            signature: c"u".as_ptr(),
34            types: {
35                static TYPES: [Option<&'static wl_interface>; 1] = [None];
36                TYPES.as_ptr().cast()
37            },
38        }];
39        MESSAGES.as_ptr()
40    },
41};
42
43/// An owned wl_seat proxy.
44///
45/// See the documentation of [the module][self] for the interface description.
46#[derive(Clone, Eq, PartialEq)]
47#[repr(transparent)]
48pub struct WlSeat {
49    /// This proxy has the interface INTERFACE.
50    proxy: UntypedOwnedProxy,
51}
52
53/// A borrowed wl_seat proxy.
54///
55/// See the documentation of [the module][self] for the interface description.
56#[derive(Eq, PartialEq)]
57#[repr(transparent)]
58pub struct WlSeatRef {
59    /// This proxy has the interface INTERFACE.
60    proxy: UntypedBorrowedProxy,
61}
62
63// SAFETY: WlSeat is a transparent wrapper around UntypedOwnedProxy
64unsafe impl UntypedOwnedProxyWrapper for WlSeat {}
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 WlSeat {
69    const INTERFACE: &'static str = "wl_seat";
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 = WlSeatRef;
76    type Api = private::ProxyApi;
77    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
78}
79
80// SAFETY: WlSeatRef is a transparent wrapper around UntypedBorrowedProxy
81unsafe impl UntypedBorrowedProxyWrapper for WlSeatRef {}
82
83// SAFETY: - The only invariant is that self.proxy has a compatible interface
84unsafe impl BorrowedProxy for WlSeatRef {
85    type Owned = WlSeat;
86}
87
88impl Deref for WlSeat {
89    type Target = WlSeatRef;
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 WlSeat {
107    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
108        write!(f, "wl_seat#{}", self.proxy.id())
109    }
110}
111
112impl Debug for WlSeatRef {
113    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
114        write!(f, "wl_seat#{}", self.proxy.id())
115    }
116}
117
118impl PartialEq<WlSeatRef> for WlSeat {
119    fn eq(&self, other: &WlSeatRef) -> bool {
120        self.proxy == other.proxy
121    }
122}
123
124impl PartialEq<WlSeat> for WlSeatRef {
125    fn eq(&self, other: &WlSeat) -> bool {
126        self.proxy == other.proxy
127    }
128}
129
130#[allow(dead_code)]
131impl WlSeat {
132    /// Since when the get_keyboard request is available.
133    #[allow(dead_code)]
134    pub const REQ__GET_KEYBOARD__SINCE: u32 = 1;
135
136    #[inline]
137    pub fn get_keyboard(&self) -> WlKeyboard {
138        let mut args = [wl_argument { n: 0 }];
139        // SAFETY: - self.proxy has the interface INTERFACE
140        //         - 0 < INTERFACE.method_count = 2
141        //         - the request signature is `n`
142        //         - OwnedProxy::WL_INTERFACE is always a valid interface
143        let data = unsafe {
144            self.proxy
145                .send_constructor::<false>(0, &mut args, WlKeyboard::WL_INTERFACE, None)
146        };
147        // SAFETY: data has the interface WlKeyboard::WL_INTERFACE
148        unsafe { proxy::low_level::from_untyped_owned(data) }
149    }
150
151    /// Since when the release request is available.
152    #[allow(dead_code)]
153    pub const REQ__RELEASE__SINCE: u32 = 5;
154
155    #[inline]
156    pub fn release(&self) {
157        let mut args = [];
158        // SAFETY: - self.proxy has the interface INTERFACE
159        //         - 1 < INTERFACE.method_count = 2
160        //         - the request signature is ``
161        unsafe {
162            self.proxy.send_destructor(1, &mut args);
163        }
164    }
165}
166
167#[allow(dead_code)]
168impl WlSeatRef {
169    /// # Arguments
170    ///
171    /// - `_queue`: The queue that the returned proxy is assigned to.
172    #[inline]
173    pub fn get_keyboard(&self, _queue: &Queue) -> WlKeyboard {
174        let mut args = [wl_argument { n: 0 }];
175        // SAFETY: - self.proxy has the interface INTERFACE
176        //         - 0 < INTERFACE.method_count = 2
177        //         - the request signature is `n`
178        //         - OwnedProxy::WL_INTERFACE is always a valid interface
179        let data = unsafe {
180            self.proxy
181                .send_constructor(_queue, 0, &mut args, WlKeyboard::WL_INTERFACE, None)
182        };
183        // SAFETY: data has the interface WlKeyboard::WL_INTERFACE
184        unsafe { proxy::low_level::from_untyped_owned(data) }
185    }
186}
187
188impl WlSeat {
189    /// Since when the capabilities event is available.
190    #[allow(dead_code)]
191    pub const EVT__CAPABILITIES__SINCE: u32 = 1;
192}
193
194/// An event handler for [WlSeat] proxies.
195#[allow(dead_code)]
196pub trait WlSeatEventHandler {
197    /// # Arguments
198    ///
199    /// - `capabilities`:
200    #[inline]
201    fn capabilities(&self, _slf: &WlSeatRef, capabilities: WlSeatCapability) {
202        let _ = capabilities;
203    }
204}
205
206impl WlSeatEventHandler for private::NoOpEventHandler {}
207
208// SAFETY: INTERFACE is a valid wl_interface
209unsafe impl<H> EventHandler for private::EventHandler<H>
210where
211    H: WlSeatEventHandler,
212{
213    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
214
215    #[allow(unused_variables)]
216    unsafe fn handle_event(
217        &self,
218        queue: &Queue,
219        slf: &UntypedBorrowedProxy,
220        opcode: u32,
221        args: *mut wl_argument,
222    ) {
223        // SAFETY: This function required that slf has the interface INTERFACE
224        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlSeatRef>(slf) };
225        match opcode {
226            0 => {
227                // SAFETY: INTERFACE requires that there are 1 arguments
228                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
229                // SAFETY: - INTERFACE requires that args[0] contains a uint
230                let arg0 = unsafe { WlSeatCapability(args[0].u) };
231                self.0.capabilities(slf, arg0);
232            }
233            _ => {
234                invalid_opcode("wl_seat", opcode);
235            }
236        }
237    }
238}
239
240impl<H> CreateEventHandler<H> for private::ProxyApi
241where
242    H: WlSeatEventHandler,
243{
244    type EventHandler = private::EventHandler<H>;
245
246    #[inline]
247    fn create_event_handler(handler: H) -> Self::EventHandler {
248        private::EventHandler(handler)
249    }
250}
251
252impl WlSeat {
253    /// Since when the capability.keyboard enum variant is available.
254    #[allow(dead_code)]
255    pub const ENM__CAPABILITY_KEYBOARD__SINCE: u32 = 1;
256}
257
258#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
259#[allow(dead_code)]
260pub struct WlSeatCapability(pub u32);
261
262/// An iterator over the set bits in a [WlSeatCapability].
263///
264/// You can construct this with the `IntoIterator` implementation of `WlSeatCapability`.
265#[derive(Clone, Debug)]
266pub struct WlSeatCapabilityIter(pub u32);
267
268impl WlSeatCapability {
269    #[allow(dead_code)]
270    pub const KEYBOARD: Self = Self(2);
271}
272
273#[allow(dead_code)]
274impl WlSeatCapability {
275    #[inline]
276    pub const fn empty() -> Self {
277        Self(0)
278    }
279
280    #[inline]
281    #[must_use]
282    pub const fn is_empty(self) -> bool {
283        self.0 == 0
284    }
285
286    #[inline]
287    #[must_use]
288    pub const fn contains(self, other: Self) -> bool {
289        self.0 & other.0 == other.0
290    }
291
292    #[inline]
293    #[must_use]
294    pub const fn intersects(self, other: Self) -> bool {
295        self.0 & other.0 != 0
296    }
297
298    #[inline]
299    pub const fn insert(&mut self, other: Self) {
300        *self = self.union(other);
301    }
302
303    #[inline]
304    pub const fn remove(&mut self, other: Self) {
305        *self = self.difference(other);
306    }
307
308    #[inline]
309    pub const fn toggle(&mut self, other: Self) {
310        *self = self.symmetric_difference(other);
311    }
312
313    #[inline]
314    pub const fn set(&mut self, other: Self, value: bool) {
315        if value {
316            self.insert(other);
317        } else {
318            self.remove(other);
319        }
320    }
321
322    #[inline]
323    #[must_use]
324    pub const fn intersection(self, other: Self) -> Self {
325        Self(self.0 & other.0)
326    }
327
328    #[inline]
329    #[must_use]
330    pub const fn union(self, other: Self) -> Self {
331        Self(self.0 | other.0)
332    }
333
334    #[inline]
335    #[must_use]
336    pub const fn difference(self, other: Self) -> Self {
337        Self(self.0 & !other.0)
338    }
339
340    #[inline]
341    #[must_use]
342    pub const fn complement(self) -> Self {
343        Self(!self.0)
344    }
345
346    #[inline]
347    #[must_use]
348    pub const fn symmetric_difference(self, other: Self) -> Self {
349        Self(self.0 ^ other.0)
350    }
351
352    #[inline]
353    pub const fn all_known() -> Self {
354        #[allow(clippy::eq_op, clippy::identity_op)]
355        Self(0 | 2)
356    }
357}
358
359impl Iterator for WlSeatCapabilityIter {
360    type Item = WlSeatCapability;
361
362    fn next(&mut self) -> Option<Self::Item> {
363        if self.0 == 0 {
364            return None;
365        }
366        let bit = 1 << self.0.trailing_zeros();
367        self.0 &= !bit;
368        Some(WlSeatCapability(bit))
369    }
370}
371
372impl IntoIterator for WlSeatCapability {
373    type Item = WlSeatCapability;
374    type IntoIter = WlSeatCapabilityIter;
375
376    fn into_iter(self) -> Self::IntoIter {
377        WlSeatCapabilityIter(self.0)
378    }
379}
380
381impl BitAnd for WlSeatCapability {
382    type Output = Self;
383
384    fn bitand(self, rhs: Self) -> Self::Output {
385        self.intersection(rhs)
386    }
387}
388
389impl BitAndAssign for WlSeatCapability {
390    fn bitand_assign(&mut self, rhs: Self) {
391        *self = self.intersection(rhs);
392    }
393}
394
395impl BitOr for WlSeatCapability {
396    type Output = Self;
397
398    fn bitor(self, rhs: Self) -> Self::Output {
399        self.union(rhs)
400    }
401}
402
403impl BitOrAssign for WlSeatCapability {
404    fn bitor_assign(&mut self, rhs: Self) {
405        *self = self.union(rhs);
406    }
407}
408
409impl BitXor for WlSeatCapability {
410    type Output = Self;
411
412    fn bitxor(self, rhs: Self) -> Self::Output {
413        self.symmetric_difference(rhs)
414    }
415}
416
417impl BitXorAssign for WlSeatCapability {
418    fn bitxor_assign(&mut self, rhs: Self) {
419        *self = self.symmetric_difference(rhs);
420    }
421}
422
423impl Sub for WlSeatCapability {
424    type Output = Self;
425
426    fn sub(self, rhs: Self) -> Self::Output {
427        self.difference(rhs)
428    }
429}
430
431impl SubAssign for WlSeatCapability {
432    fn sub_assign(&mut self, rhs: Self) {
433        *self = self.difference(rhs);
434    }
435}
436
437impl Not for WlSeatCapability {
438    type Output = Self;
439
440    fn not(self) -> Self::Output {
441        self.complement()
442    }
443}
444
445impl Debug for WlSeatCapability {
446    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
447        let mut v = self.0;
448        let mut first = true;
449        if v & 2 == 2 {
450            v &= !2;
451            if first {
452                first = false;
453            } else {
454                f.write_str(" | ")?;
455            }
456            f.write_str("KEYBOARD")?;
457        }
458        if v != 0 {
459            if first {
460                first = false;
461            } else {
462                f.write_str(" | ")?;
463            }
464            write!(f, "0x{v:032x}")?;
465        }
466        if first {
467            f.write_str("0")?;
468        }
469        Ok(())
470    }
471}
472
473/// Functional event handlers.
474pub mod event_handlers {
475    use super::*;
476
477    /// Event handler for capabilities events.
478    pub struct Capabilities<F>(F);
479    impl<F> WlSeatEventHandler for Capabilities<F>
480    where
481        F: Fn(&WlSeatRef, WlSeatCapability),
482    {
483        #[inline]
484        fn capabilities(&self, _slf: &WlSeatRef, capabilities: WlSeatCapability) {
485            self.0(_slf, capabilities)
486        }
487    }
488
489    impl WlSeat {
490        /// Creates an event handler for capabilities events.
491        ///
492        /// The event handler ignores all other events.
493        #[allow(dead_code)]
494        pub fn on_capabilities<F>(f: F) -> Capabilities<F>
495        where
496            F: Fn(&WlSeatRef, WlSeatCapability),
497        {
498            Capabilities(f)
499        }
500    }
501}