Skip to main content

get_registry/common/protocols/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    /// done event
128    ///
129    /// Notify the client when the related request is done.
130    ///
131    /// # Arguments
132    ///
133    /// - `callback_data`: request-specific data for the callback
134    #[inline]
135    fn done(&self, _slf: &WlCallbackRef, callback_data: u32) {
136        let _ = callback_data;
137    }
138}
139
140impl WlCallbackEventHandler for private::NoOpEventHandler {}
141
142// SAFETY: - INTERFACE is a valid wl_interface
143unsafe impl<H> EventHandler for private::EventHandler<H>
144where
145    H: WlCallbackEventHandler,
146{
147    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
148
149    #[allow(unused_variables)]
150    unsafe fn handle_event(
151        &self,
152        queue: &Queue,
153        data: *mut u8,
154        slf: &UntypedBorrowedProxy,
155        opcode: u32,
156        args: *mut wl_argument,
157    ) {
158        // SAFETY: This function requires that slf has the interface INTERFACE
159        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlCallbackRef>(slf) };
160        match opcode {
161            0 => {
162                // SAFETY: INTERFACE requires that there are 1 arguments
163                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
164                // SAFETY: - INTERFACE requires that args[0] contains a uint
165                let arg0 = unsafe { args[0].u };
166                self.0.done(slf, arg0);
167            }
168            _ => {
169                invalid_opcode("wl_callback", opcode);
170            }
171        }
172    }
173}
174
175impl<H> CreateEventHandler<H> for private::ProxyApi
176where
177    H: WlCallbackEventHandler,
178{
179    type EventHandler = private::EventHandler<H>;
180
181    #[inline]
182    fn create_event_handler(handler: H) -> Self::EventHandler {
183        private::EventHandler(handler)
184    }
185}
186
187/// Functional event handlers.
188pub mod event_handlers {
189    use super::*;
190
191    /// Event handler for done events.
192    pub struct Done<F>(F);
193    impl<F> WlCallbackEventHandler for Done<F>
194    where
195        F: Fn(&WlCallbackRef, u32),
196    {
197        #[inline]
198        fn done(&self, _slf: &WlCallbackRef, callback_data: u32) {
199            self.0(_slf, callback_data)
200        }
201    }
202
203    impl WlCallback {
204        /// Creates an event handler for done events.
205        ///
206        /// The event handler ignores all other events.
207        #[allow(dead_code)]
208        pub fn on_done<F>(f: F) -> Done<F>
209        where
210            F: Fn(&WlCallbackRef, u32),
211        {
212            Done(f)
213        }
214    }
215}