Skip to main content

get_registry/common/protocols_data/wayland/
wl_seat.rs

1//! group of input devices
2//!
3//! A seat is a group of keyboards, pointer and touch devices. This
4//! object is published as a global during start up, or when such a
5//! device is hot plugged.  A seat typically has a pointer and
6//! maintains a keyboard focus and a pointer focus.
7
8use super::super::all_types::*;
9use ::wl_client::builder::prelude::*;
10
11static INTERFACE: wl_interface = wl_interface {
12    name: c"wl_seat".as_ptr(),
13    version: 10,
14    method_count: 4,
15    methods: {
16        static MESSAGES: [wl_message; 4] = [
17            wl_message {
18                name: c"get_pointer".as_ptr(),
19                signature: c"n".as_ptr(),
20                types: {
21                    static TYPES: [Option<&'static wl_interface>; 1] =
22                        [Some(WlPointer::WL_INTERFACE)];
23                    TYPES.as_ptr().cast()
24                },
25            },
26            wl_message {
27                name: c"get_keyboard".as_ptr(),
28                signature: c"n".as_ptr(),
29                types: {
30                    static TYPES: [Option<&'static wl_interface>; 1] =
31                        [Some(WlKeyboard::WL_INTERFACE)];
32                    TYPES.as_ptr().cast()
33                },
34            },
35            wl_message {
36                name: c"get_touch".as_ptr(),
37                signature: c"n".as_ptr(),
38                types: {
39                    static TYPES: [Option<&'static wl_interface>; 1] =
40                        [Some(WlTouch::WL_INTERFACE)];
41                    TYPES.as_ptr().cast()
42                },
43            },
44            wl_message {
45                name: c"release".as_ptr(),
46                signature: c"".as_ptr(),
47                types: {
48                    static TYPES: [Option<&'static wl_interface>; 0] = [];
49                    TYPES.as_ptr().cast()
50                },
51            },
52        ];
53        MESSAGES.as_ptr()
54    },
55    event_count: 2,
56    events: {
57        static MESSAGES: [wl_message; 2] = [
58            wl_message {
59                name: c"capabilities".as_ptr(),
60                signature: c"u".as_ptr(),
61                types: {
62                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
63                    TYPES.as_ptr().cast()
64                },
65            },
66            wl_message {
67                name: c"name".as_ptr(),
68                signature: c"s".as_ptr(),
69                types: {
70                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
71                    TYPES.as_ptr().cast()
72                },
73            },
74        ];
75        MESSAGES.as_ptr()
76    },
77};
78
79/// An owned wl_seat proxy.
80///
81/// See the documentation of [the module][self] for the interface description.
82#[derive(Clone, Eq, PartialEq)]
83#[repr(transparent)]
84pub struct WlSeat {
85    /// This proxy has the interface INTERFACE.
86    proxy: UntypedOwnedProxy,
87}
88
89/// A borrowed wl_seat proxy.
90///
91/// See the documentation of [the module][self] for the interface description.
92#[derive(Eq, PartialEq)]
93#[repr(transparent)]
94pub struct WlSeatRef {
95    /// This proxy has the interface INTERFACE.
96    proxy: UntypedBorrowedProxy,
97}
98
99// SAFETY: WlSeat is a transparent wrapper around UntypedOwnedProxy
100unsafe impl UntypedOwnedProxyWrapper for WlSeat {}
101
102// SAFETY: - INTERFACE is a valid wl_interface
103//         - The only invariant is that self.proxy has a compatible interface
104unsafe impl OwnedProxy for WlSeat {
105    const INTERFACE: &'static str = "wl_seat";
106    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
107    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
108        private::EventHandler(private::NoOpEventHandler);
109    const MAX_VERSION: u32 = 10;
110
111    type Borrowed = WlSeatRef;
112    type Api = private::ProxyApi;
113    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
114}
115
116// SAFETY: WlSeatRef is a transparent wrapper around UntypedBorrowedProxy
117unsafe impl UntypedBorrowedProxyWrapper for WlSeatRef {}
118
119// SAFETY: - The only invariant is that self.proxy has a compatible interface
120unsafe impl BorrowedProxy for WlSeatRef {
121    type Owned = WlSeat;
122}
123
124impl Deref for WlSeat {
125    type Target = WlSeatRef;
126
127    fn deref(&self) -> &Self::Target {
128        proxy::low_level::deref(self)
129    }
130}
131
132mod private {
133    pub struct ProxyApi;
134
135    #[allow(dead_code)]
136    pub struct EventHandler<H>(pub(super) H);
137
138    #[allow(dead_code)]
139    pub struct NoOpEventHandler;
140}
141
142impl Debug for WlSeat {
143    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
144        write!(f, "wl_seat#{}", self.proxy.id())
145    }
146}
147
148impl Debug for WlSeatRef {
149    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
150        write!(f, "wl_seat#{}", self.proxy.id())
151    }
152}
153
154impl PartialEq<WlSeatRef> for WlSeat {
155    fn eq(&self, other: &WlSeatRef) -> bool {
156        self.proxy == other.proxy
157    }
158}
159
160impl PartialEq<WlSeat> for WlSeatRef {
161    fn eq(&self, other: &WlSeat) -> bool {
162        self.proxy == other.proxy
163    }
164}
165
166#[allow(dead_code)]
167impl WlSeat {
168    /// Since when the get_pointer request is available.
169    #[allow(dead_code)]
170    pub const REQ__GET_POINTER__SINCE: u32 = 1;
171
172    /// Since when the get_keyboard request is available.
173    #[allow(dead_code)]
174    pub const REQ__GET_KEYBOARD__SINCE: u32 = 1;
175
176    /// Since when the get_touch request is available.
177    #[allow(dead_code)]
178    pub const REQ__GET_TOUCH__SINCE: u32 = 1;
179
180    /// Since when the release request is available.
181    #[allow(dead_code)]
182    pub const REQ__RELEASE__SINCE: u32 = 5;
183}
184
185#[allow(dead_code)]
186impl WlSeat {
187    /// return pointer object
188    ///
189    /// The ID provided will be initialized to the wl_pointer interface
190    /// for this seat.
191    ///
192    /// This request only takes effect if the seat has the pointer
193    /// capability, or has had the pointer capability in the past.
194    /// It is a protocol violation to issue this request on a seat that has
195    /// never had the pointer capability. The missing_capability error will
196    /// be sent in this case.
197    #[inline]
198    pub fn get_pointer(&self) -> WlPointer {
199        let mut args = [wl_argument { n: 0 }];
200        // SAFETY: - self.proxy has the interface INTERFACE
201        //         - 0 < INTERFACE.method_count = 4
202        //         - the request signature is `n`
203        //         - OwnedProxy::WL_INTERFACE is always a valid interface
204        let data = unsafe {
205            self.proxy
206                .send_constructor::<false>(0, &mut args, WlPointer::WL_INTERFACE, None)
207        };
208        // SAFETY: data has the interface WlPointer::WL_INTERFACE
209        unsafe { proxy::low_level::from_untyped_owned(data) }
210    }
211
212    /// return keyboard object
213    ///
214    /// The ID provided will be initialized to the wl_keyboard interface
215    /// for this seat.
216    ///
217    /// This request only takes effect if the seat has the keyboard
218    /// capability, or has had the keyboard capability in the past.
219    /// It is a protocol violation to issue this request on a seat that has
220    /// never had the keyboard capability. The missing_capability error will
221    /// be sent in this case.
222    #[inline]
223    pub fn get_keyboard(&self) -> WlKeyboard {
224        let mut args = [wl_argument { n: 0 }];
225        // SAFETY: - self.proxy has the interface INTERFACE
226        //         - 1 < INTERFACE.method_count = 4
227        //         - the request signature is `n`
228        //         - OwnedProxy::WL_INTERFACE is always a valid interface
229        let data = unsafe {
230            self.proxy
231                .send_constructor::<false>(1, &mut args, WlKeyboard::WL_INTERFACE, None)
232        };
233        // SAFETY: data has the interface WlKeyboard::WL_INTERFACE
234        unsafe { proxy::low_level::from_untyped_owned(data) }
235    }
236
237    /// return touch object
238    ///
239    /// The ID provided will be initialized to the wl_touch interface
240    /// for this seat.
241    ///
242    /// This request only takes effect if the seat has the touch
243    /// capability, or has had the touch capability in the past.
244    /// It is a protocol violation to issue this request on a seat that has
245    /// never had the touch capability. The missing_capability error will
246    /// be sent in this case.
247    #[inline]
248    pub fn get_touch(&self) -> WlTouch {
249        let mut args = [wl_argument { n: 0 }];
250        // SAFETY: - self.proxy has the interface INTERFACE
251        //         - 2 < INTERFACE.method_count = 4
252        //         - the request signature is `n`
253        //         - OwnedProxy::WL_INTERFACE is always a valid interface
254        let data = unsafe {
255            self.proxy
256                .send_constructor::<false>(2, &mut args, WlTouch::WL_INTERFACE, None)
257        };
258        // SAFETY: data has the interface WlTouch::WL_INTERFACE
259        unsafe { proxy::low_level::from_untyped_owned(data) }
260    }
261
262    /// release the seat object
263    ///
264    /// Using this request a client can tell the server that it is not going to
265    /// use the seat object anymore.
266    #[inline]
267    pub fn release(&self) {
268        let mut args = [];
269        // SAFETY: - self.proxy has the interface INTERFACE
270        //         - 3 < INTERFACE.method_count = 4
271        //         - the request signature is ``
272        unsafe {
273            self.proxy.send_destructor(3, &mut args);
274        }
275    }
276}
277
278#[allow(dead_code)]
279impl WlSeatRef {
280    /// return pointer object
281    ///
282    /// The ID provided will be initialized to the wl_pointer interface
283    /// for this seat.
284    ///
285    /// This request only takes effect if the seat has the pointer
286    /// capability, or has had the pointer capability in the past.
287    /// It is a protocol violation to issue this request on a seat that has
288    /// never had the pointer capability. The missing_capability error will
289    /// be sent in this case.
290    ///
291    /// # Arguments
292    ///
293    /// - `_queue`: The queue that the returned proxy is assigned to.
294    #[inline]
295    pub fn get_pointer(&self, _queue: &Queue) -> WlPointer {
296        let mut args = [wl_argument { n: 0 }];
297        // SAFETY: - self.proxy has the interface INTERFACE
298        //         - 0 < INTERFACE.method_count = 4
299        //         - the request signature is `n`
300        //         - OwnedProxy::WL_INTERFACE is always a valid interface
301        let data = unsafe {
302            self.proxy
303                .send_constructor(_queue, 0, &mut args, WlPointer::WL_INTERFACE, None)
304        };
305        // SAFETY: data has the interface WlPointer::WL_INTERFACE
306        unsafe { proxy::low_level::from_untyped_owned(data) }
307    }
308
309    /// return keyboard object
310    ///
311    /// The ID provided will be initialized to the wl_keyboard interface
312    /// for this seat.
313    ///
314    /// This request only takes effect if the seat has the keyboard
315    /// capability, or has had the keyboard capability in the past.
316    /// It is a protocol violation to issue this request on a seat that has
317    /// never had the keyboard capability. The missing_capability error will
318    /// be sent in this case.
319    ///
320    /// # Arguments
321    ///
322    /// - `_queue`: The queue that the returned proxy is assigned to.
323    #[inline]
324    pub fn get_keyboard(&self, _queue: &Queue) -> WlKeyboard {
325        let mut args = [wl_argument { n: 0 }];
326        // SAFETY: - self.proxy has the interface INTERFACE
327        //         - 1 < INTERFACE.method_count = 4
328        //         - the request signature is `n`
329        //         - OwnedProxy::WL_INTERFACE is always a valid interface
330        let data = unsafe {
331            self.proxy
332                .send_constructor(_queue, 1, &mut args, WlKeyboard::WL_INTERFACE, None)
333        };
334        // SAFETY: data has the interface WlKeyboard::WL_INTERFACE
335        unsafe { proxy::low_level::from_untyped_owned(data) }
336    }
337
338    /// return touch object
339    ///
340    /// The ID provided will be initialized to the wl_touch interface
341    /// for this seat.
342    ///
343    /// This request only takes effect if the seat has the touch
344    /// capability, or has had the touch capability in the past.
345    /// It is a protocol violation to issue this request on a seat that has
346    /// never had the touch capability. The missing_capability error will
347    /// be sent in this case.
348    ///
349    /// # Arguments
350    ///
351    /// - `_queue`: The queue that the returned proxy is assigned to.
352    #[inline]
353    pub fn get_touch(&self, _queue: &Queue) -> WlTouch {
354        let mut args = [wl_argument { n: 0 }];
355        // SAFETY: - self.proxy has the interface INTERFACE
356        //         - 2 < INTERFACE.method_count = 4
357        //         - the request signature is `n`
358        //         - OwnedProxy::WL_INTERFACE is always a valid interface
359        let data = unsafe {
360            self.proxy
361                .send_constructor(_queue, 2, &mut args, WlTouch::WL_INTERFACE, None)
362        };
363        // SAFETY: data has the interface WlTouch::WL_INTERFACE
364        unsafe { proxy::low_level::from_untyped_owned(data) }
365    }
366}
367
368impl WlSeat {
369    /// Since when the capabilities event is available.
370    #[allow(dead_code)]
371    pub const EVT__CAPABILITIES__SINCE: u32 = 1;
372
373    /// Since when the name event is available.
374    #[allow(dead_code)]
375    pub const EVT__NAME__SINCE: u32 = 2;
376}
377
378/// An event handler for [WlSeat] proxies.
379#[allow(dead_code)]
380pub trait WlSeatEventHandler {
381    type Data: 'static;
382
383    /// seat capabilities changed
384    ///
385    /// This is emitted whenever a seat gains or loses the pointer,
386    /// keyboard or touch capabilities.  The argument is a capability
387    /// enum containing the complete set of capabilities this seat has.
388    ///
389    /// When the pointer capability is added, a client may create a
390    /// wl_pointer object using the wl_seat.get_pointer request. This object
391    /// will receive pointer events until the capability is removed in the
392    /// future.
393    ///
394    /// When the pointer capability is removed, a client should destroy the
395    /// wl_pointer objects associated with the seat where the capability was
396    /// removed, using the wl_pointer.release request. No further pointer
397    /// events will be received on these objects.
398    ///
399    /// In some compositors, if a seat regains the pointer capability and a
400    /// client has a previously obtained wl_pointer object of version 4 or
401    /// less, that object may start sending pointer events again. This
402    /// behavior is considered a misinterpretation of the intended behavior
403    /// and must not be relied upon by the client. wl_pointer objects of
404    /// version 5 or later must not send events if created before the most
405    /// recent event notifying the client of an added pointer capability.
406    ///
407    /// The above behavior also applies to wl_keyboard and wl_touch with the
408    /// keyboard and touch capabilities, respectively.
409    ///
410    /// # Arguments
411    ///
412    /// - `capabilities`: capabilities of the seat
413    #[inline]
414    fn capabilities(
415        &self,
416        _data: &mut Self::Data,
417        _slf: &WlSeatRef,
418        capabilities: WlSeatCapability,
419    ) {
420        let _ = capabilities;
421    }
422
423    /// unique identifier for this seat
424    ///
425    /// In a multi-seat configuration the seat name can be used by clients to
426    /// help identify which physical devices the seat represents.
427    ///
428    /// The seat name is a UTF-8 string with no convention defined for its
429    /// contents. Each name is unique among all wl_seat globals. The name is
430    /// only guaranteed to be unique for the current compositor instance.
431    ///
432    /// The same seat names are used for all clients. Thus, the name can be
433    /// shared across processes to refer to a specific wl_seat global.
434    ///
435    /// The name event is sent after binding to the seat global. This event is
436    /// only sent once per seat object, and the name does not change over the
437    /// lifetime of the wl_seat global.
438    ///
439    /// Compositors may re-use the same seat name if the wl_seat global is
440    /// destroyed and re-created later.
441    ///
442    /// # Arguments
443    ///
444    /// - `name`: seat identifier
445    #[inline]
446    fn name(&self, _data: &mut Self::Data, _slf: &WlSeatRef, name: &str) {
447        let _ = name;
448    }
449}
450
451impl WlSeatEventHandler for private::NoOpEventHandler {
452    type Data = ();
453}
454
455// SAFETY: - INTERFACE is a valid wl_interface
456//         - mutable_type always returns the same value
457unsafe impl<H> EventHandler for private::EventHandler<H>
458where
459    H: WlSeatEventHandler,
460{
461    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
462
463    #[inline]
464    fn mutable_type() -> Option<(TypeId, &'static str)> {
465        let id = TypeId::of::<H::Data>();
466        let name = std::any::type_name::<H::Data>();
467        Some((id, name))
468    }
469
470    #[allow(unused_variables)]
471    unsafe fn handle_event(
472        &self,
473        queue: &Queue,
474        data: *mut u8,
475        slf: &UntypedBorrowedProxy,
476        opcode: u32,
477        args: *mut wl_argument,
478    ) {
479        // SAFETY: This function requires that slf has the interface INTERFACE
480        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlSeatRef>(slf) };
481        // SAFETY: This function requires that data is `&mut T` where `T`
482        //         has the type id returned by `Self::mutable_type`, i.e.,
483        //         `T = H::Data`.
484        let data: &mut H::Data = unsafe { &mut *data.cast() };
485        match opcode {
486            0 => {
487                // SAFETY: INTERFACE requires that there are 1 arguments
488                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
489                // SAFETY: - INTERFACE requires that args[0] contains a uint
490                let arg0 = unsafe { WlSeatCapability(args[0].u) };
491                self.0.capabilities(data, slf, arg0);
492            }
493            1 => {
494                // SAFETY: INTERFACE requires that there are 1 arguments
495                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
496                // SAFETY: - INTERFACE requires that args[0] contains a string
497                //         - if the pointer is not null, then it is a c string
498                let arg0 = unsafe { convert_string_arg("wl_seat", "name", args[0].s) };
499                self.0.name(data, slf, arg0);
500            }
501            _ => {
502                invalid_opcode("wl_seat", opcode);
503            }
504        }
505    }
506}
507
508impl<H> CreateEventHandler<H> for private::ProxyApi
509where
510    H: WlSeatEventHandler,
511{
512    type EventHandler = private::EventHandler<H>;
513
514    #[inline]
515    fn create_event_handler(handler: H) -> Self::EventHandler {
516        private::EventHandler(handler)
517    }
518}
519
520impl WlSeat {
521    /// Since when the capability.pointer enum variant is available.
522    #[allow(dead_code)]
523    pub const ENM__CAPABILITY_POINTER__SINCE: u32 = 1;
524    /// Since when the capability.keyboard enum variant is available.
525    #[allow(dead_code)]
526    pub const ENM__CAPABILITY_KEYBOARD__SINCE: u32 = 1;
527    /// Since when the capability.touch enum variant is available.
528    #[allow(dead_code)]
529    pub const ENM__CAPABILITY_TOUCH__SINCE: u32 = 1;
530
531    /// Since when the error.missing_capability enum variant is available.
532    #[allow(dead_code)]
533    pub const ENM__ERROR_MISSING_CAPABILITY__SINCE: u32 = 1;
534}
535
536/// seat capability bitmask
537///
538/// This is a bitmask of capabilities this seat has; if a member is
539/// set, then it is present on the seat.
540#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
541#[allow(dead_code)]
542pub struct WlSeatCapability(pub u32);
543
544/// An iterator over the set bits in a [WlSeatCapability].
545///
546/// You can construct this with the `IntoIterator` implementation of `WlSeatCapability`.
547#[derive(Clone, Debug)]
548#[allow(dead_code)]
549pub struct WlSeatCapabilityIter(pub u32);
550
551impl WlSeatCapability {
552    /// the seat has pointer devices
553    #[allow(dead_code)]
554    pub const POINTER: Self = Self(1);
555
556    /// the seat has one or more keyboards
557    #[allow(dead_code)]
558    pub const KEYBOARD: Self = Self(2);
559
560    /// the seat has touch devices
561    #[allow(dead_code)]
562    pub const TOUCH: Self = Self(4);
563}
564
565#[allow(dead_code)]
566impl WlSeatCapability {
567    #[inline]
568    pub const fn empty() -> Self {
569        Self(0)
570    }
571
572    #[inline]
573    #[must_use]
574    pub const fn is_empty(self) -> bool {
575        self.0 == 0
576    }
577
578    #[inline]
579    #[must_use]
580    pub const fn contains(self, other: Self) -> bool {
581        self.0 & other.0 == other.0
582    }
583
584    #[inline]
585    #[must_use]
586    pub const fn intersects(self, other: Self) -> bool {
587        self.0 & other.0 != 0
588    }
589
590    #[inline]
591    pub const fn insert(&mut self, other: Self) {
592        *self = self.union(other);
593    }
594
595    #[inline]
596    pub const fn remove(&mut self, other: Self) {
597        *self = self.difference(other);
598    }
599
600    #[inline]
601    pub const fn toggle(&mut self, other: Self) {
602        *self = self.symmetric_difference(other);
603    }
604
605    #[inline]
606    pub const fn set(&mut self, other: Self, value: bool) {
607        if value {
608            self.insert(other);
609        } else {
610            self.remove(other);
611        }
612    }
613
614    #[inline]
615    #[must_use]
616    pub const fn intersection(self, other: Self) -> Self {
617        Self(self.0 & other.0)
618    }
619
620    #[inline]
621    #[must_use]
622    pub const fn union(self, other: Self) -> Self {
623        Self(self.0 | other.0)
624    }
625
626    #[inline]
627    #[must_use]
628    pub const fn difference(self, other: Self) -> Self {
629        Self(self.0 & !other.0)
630    }
631
632    #[inline]
633    #[must_use]
634    pub const fn complement(self) -> Self {
635        Self(!self.0)
636    }
637
638    #[inline]
639    #[must_use]
640    pub const fn symmetric_difference(self, other: Self) -> Self {
641        Self(self.0 ^ other.0)
642    }
643
644    #[inline]
645    pub const fn all_known() -> Self {
646        #[allow(clippy::eq_op, clippy::identity_op)]
647        Self(0 | 1 | 2 | 4)
648    }
649}
650
651impl Iterator for WlSeatCapabilityIter {
652    type Item = WlSeatCapability;
653
654    fn next(&mut self) -> Option<Self::Item> {
655        if self.0 == 0 {
656            return None;
657        }
658        let bit = 1 << self.0.trailing_zeros();
659        self.0 &= !bit;
660        Some(WlSeatCapability(bit))
661    }
662}
663
664impl IntoIterator for WlSeatCapability {
665    type Item = WlSeatCapability;
666    type IntoIter = WlSeatCapabilityIter;
667
668    fn into_iter(self) -> Self::IntoIter {
669        WlSeatCapabilityIter(self.0)
670    }
671}
672
673impl BitAnd for WlSeatCapability {
674    type Output = Self;
675
676    fn bitand(self, rhs: Self) -> Self::Output {
677        self.intersection(rhs)
678    }
679}
680
681impl BitAndAssign for WlSeatCapability {
682    fn bitand_assign(&mut self, rhs: Self) {
683        *self = self.intersection(rhs);
684    }
685}
686
687impl BitOr for WlSeatCapability {
688    type Output = Self;
689
690    fn bitor(self, rhs: Self) -> Self::Output {
691        self.union(rhs)
692    }
693}
694
695impl BitOrAssign for WlSeatCapability {
696    fn bitor_assign(&mut self, rhs: Self) {
697        *self = self.union(rhs);
698    }
699}
700
701impl BitXor for WlSeatCapability {
702    type Output = Self;
703
704    fn bitxor(self, rhs: Self) -> Self::Output {
705        self.symmetric_difference(rhs)
706    }
707}
708
709impl BitXorAssign for WlSeatCapability {
710    fn bitxor_assign(&mut self, rhs: Self) {
711        *self = self.symmetric_difference(rhs);
712    }
713}
714
715impl Sub for WlSeatCapability {
716    type Output = Self;
717
718    fn sub(self, rhs: Self) -> Self::Output {
719        self.difference(rhs)
720    }
721}
722
723impl SubAssign for WlSeatCapability {
724    fn sub_assign(&mut self, rhs: Self) {
725        *self = self.difference(rhs);
726    }
727}
728
729impl Not for WlSeatCapability {
730    type Output = Self;
731
732    fn not(self) -> Self::Output {
733        self.complement()
734    }
735}
736
737impl Debug for WlSeatCapability {
738    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
739        let mut v = self.0;
740        let mut first = true;
741        if v & 1 == 1 {
742            v &= !1;
743            if first {
744                first = false;
745            } else {
746                f.write_str(" | ")?;
747            }
748            f.write_str("POINTER")?;
749        }
750        if v & 2 == 2 {
751            v &= !2;
752            if first {
753                first = false;
754            } else {
755                f.write_str(" | ")?;
756            }
757            f.write_str("KEYBOARD")?;
758        }
759        if v & 4 == 4 {
760            v &= !4;
761            if first {
762                first = false;
763            } else {
764                f.write_str(" | ")?;
765            }
766            f.write_str("TOUCH")?;
767        }
768        if v != 0 {
769            if first {
770                first = false;
771            } else {
772                f.write_str(" | ")?;
773            }
774            write!(f, "0x{v:032x}")?;
775        }
776        if first {
777            f.write_str("0")?;
778        }
779        Ok(())
780    }
781}
782
783/// wl_seat error values
784///
785/// These errors can be emitted in response to wl_seat requests.
786#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
787#[allow(dead_code)]
788pub struct WlSeatError(pub u32);
789
790impl WlSeatError {
791    /// get_pointer, get_keyboard or get_touch called on seat without the matching capability
792    #[allow(dead_code)]
793    pub const MISSING_CAPABILITY: Self = Self(0);
794}
795
796impl Debug for WlSeatError {
797    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
798        let name = match *self {
799            Self::MISSING_CAPABILITY => "MISSING_CAPABILITY",
800            _ => return Debug::fmt(&self.0, f),
801        };
802        f.write_str(name)
803    }
804}
805
806/// Functional event handlers.
807pub mod event_handlers {
808    use super::*;
809
810    /// Event handler for capabilities events.
811    pub struct Capabilities<T, F>(F, PhantomData<fn(&mut T)>);
812    impl<T, F> WlSeatEventHandler for Capabilities<T, F>
813    where
814        T: 'static,
815        F: Fn(&mut T, &WlSeatRef, WlSeatCapability),
816    {
817        type Data = T;
818
819        #[inline]
820        fn capabilities(&self, _data: &mut T, _slf: &WlSeatRef, capabilities: WlSeatCapability) {
821            self.0(_data, _slf, capabilities)
822        }
823    }
824
825    /// Event handler for name events.
826    pub struct Name<T, F>(F, PhantomData<fn(&mut T)>);
827    impl<T, F> WlSeatEventHandler for Name<T, F>
828    where
829        T: 'static,
830        F: Fn(&mut T, &WlSeatRef, &str),
831    {
832        type Data = T;
833
834        #[inline]
835        fn name(&self, _data: &mut T, _slf: &WlSeatRef, name: &str) {
836            self.0(_data, _slf, name)
837        }
838    }
839
840    impl WlSeat {
841        /// Creates an event handler for capabilities events.
842        ///
843        /// The event handler ignores all other events.
844        #[allow(dead_code)]
845        pub fn on_capabilities<T, F>(f: F) -> Capabilities<T, F>
846        where
847            T: 'static,
848            F: Fn(&mut T, &WlSeatRef, WlSeatCapability),
849        {
850            Capabilities(f, PhantomData)
851        }
852
853        /// Creates an event handler for name events.
854        ///
855        /// The event handler ignores all other events.
856        #[allow(dead_code)]
857        pub fn on_name<T, F>(f: F) -> Name<T, F>
858        where
859            T: 'static,
860            F: Fn(&mut T, &WlSeatRef, &str),
861        {
862            Name(f, PhantomData)
863        }
864    }
865}