wl_client/test_protocols/core/
wl_callback.rs

1use {super::super::all_types::*, crate::builder::prelude::*};
2
3static INTERFACE: wl_interface = wl_interface {
4    name: c"wl_callback".as_ptr(),
5    version: 1,
6    method_count: 0,
7    methods: ptr::null(),
8    event_count: 1,
9    events: {
10        static MESSAGES: [wl_message; 1] = [wl_message {
11            name: c"done".as_ptr(),
12            signature: c"u".as_ptr(),
13            types: {
14                static TYPES: [Option<&'static wl_interface>; 1] = [None];
15                TYPES.as_ptr().cast()
16            },
17        }];
18        MESSAGES.as_ptr()
19    },
20};
21
22/// An owned wl_callback proxy.
23///
24/// See the documentation of [the module][self] for the interface description.
25#[derive(Clone, Eq, PartialEq)]
26#[repr(transparent)]
27pub struct WlCallback {
28    /// This proxy has the interface INTERFACE.
29    proxy: UntypedOwnedProxy,
30}
31
32/// A borrowed wl_callback proxy.
33///
34/// See the documentation of [the module][self] for the interface description.
35#[derive(Eq, PartialEq)]
36#[repr(transparent)]
37pub struct WlCallbackRef {
38    /// This proxy has the interface INTERFACE.
39    proxy: UntypedBorrowedProxy,
40}
41
42// SAFETY: WlCallback is a transparent wrapper around UntypedOwnedProxy
43unsafe impl UntypedOwnedProxyWrapper for WlCallback {}
44
45// SAFETY: - INTERFACE is a valid wl_interface
46//         - The only invariant is that self.proxy has a compatible interface
47unsafe impl OwnedProxy for WlCallback {
48    const INTERFACE: &'static str = "wl_callback";
49    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
50    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
51        private::EventHandler(private::NoOpEventHandler);
52    const MAX_VERSION: u32 = 1;
53
54    type Borrowed = WlCallbackRef;
55    type Api = private::ProxyApi;
56    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
57}
58
59// SAFETY: WlCallbackRef is a transparent wrapper around UntypedBorrowedProxy
60unsafe impl UntypedBorrowedProxyWrapper for WlCallbackRef {}
61
62// SAFETY: - The only invariant is that self.proxy has a compatible interface
63unsafe impl BorrowedProxy for WlCallbackRef {
64    type Owned = WlCallback;
65}
66
67impl Deref for WlCallback {
68    type Target = WlCallbackRef;
69
70    fn deref(&self) -> &Self::Target {
71        proxy::low_level::deref(self)
72    }
73}
74
75mod private {
76    pub struct ProxyApi;
77
78    #[allow(dead_code)]
79    pub struct EventHandler<H>(pub(super) H);
80
81    #[allow(dead_code)]
82    pub struct NoOpEventHandler;
83}
84
85impl Debug for WlCallback {
86    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87        write!(f, "wl_callback#{}", self.proxy.id())
88    }
89}
90
91impl Debug for WlCallbackRef {
92    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93        write!(f, "wl_callback#{}", self.proxy.id())
94    }
95}
96
97impl PartialEq<WlCallbackRef> for WlCallback {
98    fn eq(&self, other: &WlCallbackRef) -> bool {
99        self.proxy == other.proxy
100    }
101}
102
103impl PartialEq<WlCallback> for WlCallbackRef {
104    fn eq(&self, other: &WlCallback) -> bool {
105        self.proxy == other.proxy
106    }
107}
108
109impl WlCallback {
110    /// Since when the done event is available.
111    #[allow(dead_code)]
112    pub const EVT__DONE__SINCE: u32 = 1;
113}
114
115/// An event handler for [WlCallback] proxies.
116#[allow(dead_code)]
117pub trait WlCallbackEventHandler {
118    /// # Arguments
119    ///
120    /// - `callback_data`:
121    #[inline]
122    fn done(&self, _slf: &WlCallbackRef, callback_data: u32) {
123        let _ = callback_data;
124    }
125}
126
127impl WlCallbackEventHandler for private::NoOpEventHandler {}
128
129// SAFETY: INTERFACE is a valid wl_interface
130unsafe impl<H> EventHandler for private::EventHandler<H>
131where
132    H: WlCallbackEventHandler,
133{
134    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
135
136    #[allow(unused_variables)]
137    unsafe fn handle_event(
138        &self,
139        queue: &Queue,
140        slf: &UntypedBorrowedProxy,
141        opcode: u32,
142        args: *mut wl_argument,
143    ) {
144        // SAFETY: This function required that slf has the interface INTERFACE
145        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlCallbackRef>(slf) };
146        match opcode {
147            0 => {
148                // SAFETY: INTERFACE requires that there are 1 arguments
149                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
150                // SAFETY: - INTERFACE requires that args[0] contains a uint
151                let arg0 = unsafe { args[0].u };
152                self.0.done(slf, arg0);
153            }
154            _ => {
155                invalid_opcode("wl_callback", opcode);
156            }
157        }
158    }
159}
160
161impl<H> CreateEventHandler<H> for private::ProxyApi
162where
163    H: WlCallbackEventHandler,
164{
165    type EventHandler = private::EventHandler<H>;
166
167    #[inline]
168    fn create_event_handler(handler: H) -> Self::EventHandler {
169        private::EventHandler(handler)
170    }
171}
172
173/// Functional event handlers.
174pub mod event_handlers {
175    use super::*;
176
177    /// Event handler for done events.
178    pub struct Done<F>(F);
179    impl<F> WlCallbackEventHandler for Done<F>
180    where
181        F: Fn(&WlCallbackRef, u32),
182    {
183        #[inline]
184        fn done(&self, _slf: &WlCallbackRef, callback_data: u32) {
185            self.0(_slf, callback_data)
186        }
187    }
188
189    impl WlCallback {
190        /// Creates an event handler for done events.
191        ///
192        /// The event handler ignores all other events.
193        #[allow(dead_code)]
194        pub fn on_done<F>(f: F) -> Done<F>
195        where
196            F: Fn(&WlCallbackRef, u32),
197        {
198            Done(f)
199        }
200    }
201}