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