Skip to main content

get_registry/common/protocols_data/wayland/
wl_callback.rs

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