wl_client/test_protocols/core/
wl_registry.rs

1use {super::super::all_types::*, crate::builder::prelude::*};
2
3static INTERFACE: wl_interface = wl_interface {
4    name: c"wl_registry".as_ptr(),
5    version: 1,
6    method_count: 1,
7    methods: {
8        static MESSAGES: [wl_message; 1] = [wl_message {
9            name: c"bind".as_ptr(),
10            signature: c"usun".as_ptr(),
11            types: {
12                static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
13                TYPES.as_ptr().cast()
14            },
15        }];
16        MESSAGES.as_ptr()
17    },
18    event_count: 2,
19    events: {
20        static MESSAGES: [wl_message; 2] = [
21            wl_message {
22                name: c"global".as_ptr(),
23                signature: c"usu".as_ptr(),
24                types: {
25                    static TYPES: [Option<&'static wl_interface>; 3] = [None, None, None];
26                    TYPES.as_ptr().cast()
27                },
28            },
29            wl_message {
30                name: c"global_remove".as_ptr(),
31                signature: c"u".as_ptr(),
32                types: {
33                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
34                    TYPES.as_ptr().cast()
35                },
36            },
37        ];
38        MESSAGES.as_ptr()
39    },
40};
41
42/// An owned wl_registry proxy.
43///
44/// See the documentation of [the module][self] for the interface description.
45#[derive(Clone, Eq, PartialEq)]
46#[repr(transparent)]
47pub struct WlRegistry {
48    /// This proxy has the interface INTERFACE.
49    proxy: UntypedOwnedProxy,
50}
51
52/// A borrowed wl_registry proxy.
53///
54/// See the documentation of [the module][self] for the interface description.
55#[derive(Eq, PartialEq)]
56#[repr(transparent)]
57pub struct WlRegistryRef {
58    /// This proxy has the interface INTERFACE.
59    proxy: UntypedBorrowedProxy,
60}
61
62// SAFETY: WlRegistry is a transparent wrapper around UntypedOwnedProxy
63unsafe impl UntypedOwnedProxyWrapper for WlRegistry {}
64
65// SAFETY: - INTERFACE is a valid wl_interface
66//         - The only invariant is that self.proxy has a compatible interface
67unsafe impl OwnedProxy for WlRegistry {
68    const INTERFACE: &'static str = "wl_registry";
69    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
70    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
71        private::EventHandler(private::NoOpEventHandler);
72    const MAX_VERSION: u32 = 1;
73
74    type Borrowed = WlRegistryRef;
75    type Api = private::ProxyApi;
76    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
77}
78
79// SAFETY: WlRegistryRef is a transparent wrapper around UntypedBorrowedProxy
80unsafe impl UntypedBorrowedProxyWrapper for WlRegistryRef {}
81
82// SAFETY: - The only invariant is that self.proxy has a compatible interface
83unsafe impl BorrowedProxy for WlRegistryRef {
84    type Owned = WlRegistry;
85}
86
87impl Deref for WlRegistry {
88    type Target = WlRegistryRef;
89
90    fn deref(&self) -> &Self::Target {
91        proxy::low_level::deref(self)
92    }
93}
94
95mod private {
96    pub struct ProxyApi;
97
98    #[allow(dead_code)]
99    pub struct EventHandler<H>(pub(super) H);
100
101    #[allow(dead_code)]
102    pub struct NoOpEventHandler;
103}
104
105impl Debug for WlRegistry {
106    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
107        write!(f, "wl_registry#{}", self.proxy.id())
108    }
109}
110
111impl Debug for WlRegistryRef {
112    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
113        write!(f, "wl_registry#{}", self.proxy.id())
114    }
115}
116
117impl PartialEq<WlRegistryRef> for WlRegistry {
118    fn eq(&self, other: &WlRegistryRef) -> bool {
119        self.proxy == other.proxy
120    }
121}
122
123impl PartialEq<WlRegistry> for WlRegistryRef {
124    fn eq(&self, other: &WlRegistry) -> bool {
125        self.proxy == other.proxy
126    }
127}
128
129#[allow(dead_code)]
130impl WlRegistry {
131    /// Since when the bind request is available.
132    #[allow(dead_code)]
133    pub const REQ__BIND__SINCE: u32 = 1;
134
135    /// # Arguments
136    ///
137    /// - `name`:
138    #[inline]
139    pub fn bind<P: OwnedProxy>(&self, name: u32, version: u32) -> P {
140        let (arg0, arg1) = (name, version);
141        let mut args = [
142            wl_argument { u: arg0 },
143            wl_argument {
144                s: P::WL_INTERFACE.name,
145            },
146            wl_argument { u: arg1 },
147            wl_argument { n: 0 },
148        ];
149        // SAFETY: - self.proxy has the interface INTERFACE
150        //         - 0 < INTERFACE.method_count = 1
151        //         - the request signature is `usun`
152        //         - OwnedProxy::WL_INTERFACE is always a valid interface
153        let data = unsafe {
154            self.proxy
155                .send_constructor::<false>(0, &mut args, P::WL_INTERFACE, Some(version))
156        };
157        // SAFETY: data has the interface P::WL_INTERFACE
158        unsafe { proxy::low_level::from_untyped_owned(data) }
159    }
160}
161
162#[allow(dead_code)]
163impl WlRegistryRef {
164    /// # Arguments
165    ///
166    /// - `_queue`: The queue that the returned proxy is assigned to.
167    /// - `name`:
168    #[inline]
169    pub fn bind<P: OwnedProxy>(&self, _queue: &Queue, name: u32, version: u32) -> P {
170        let (arg0, arg1) = (name, version);
171        let mut args = [
172            wl_argument { u: arg0 },
173            wl_argument {
174                s: P::WL_INTERFACE.name,
175            },
176            wl_argument { u: arg1 },
177            wl_argument { n: 0 },
178        ];
179        // SAFETY: - self.proxy has the interface INTERFACE
180        //         - 0 < INTERFACE.method_count = 1
181        //         - the request signature is `usun`
182        //         - OwnedProxy::WL_INTERFACE is always a valid interface
183        let data = unsafe {
184            self.proxy
185                .send_constructor(_queue, 0, &mut args, P::WL_INTERFACE, Some(version))
186        };
187        // SAFETY: data has the interface P::WL_INTERFACE
188        unsafe { proxy::low_level::from_untyped_owned(data) }
189    }
190}
191
192impl WlRegistry {
193    /// Since when the global event is available.
194    #[allow(dead_code)]
195    pub const EVT__GLOBAL__SINCE: u32 = 1;
196
197    /// Since when the global_remove event is available.
198    #[allow(dead_code)]
199    pub const EVT__GLOBAL_REMOVE__SINCE: u32 = 1;
200}
201
202/// An event handler for [WlRegistry] proxies.
203#[allow(dead_code)]
204pub trait WlRegistryEventHandler {
205    /// # Arguments
206    ///
207    /// - `name`:
208    /// - `interface`:
209    /// - `version`:
210    #[inline]
211    fn global(&self, _slf: &WlRegistryRef, name: u32, interface: &str, version: u32) {
212        let _ = name;
213        let _ = interface;
214        let _ = version;
215    }
216
217    /// # Arguments
218    ///
219    /// - `name`:
220    #[inline]
221    fn global_remove(&self, _slf: &WlRegistryRef, name: u32) {
222        let _ = name;
223    }
224}
225
226impl WlRegistryEventHandler for private::NoOpEventHandler {}
227
228// SAFETY: INTERFACE is a valid wl_interface
229unsafe impl<H> EventHandler for private::EventHandler<H>
230where
231    H: WlRegistryEventHandler,
232{
233    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
234
235    #[allow(unused_variables)]
236    unsafe fn handle_event(
237        &self,
238        queue: &Queue,
239        slf: &UntypedBorrowedProxy,
240        opcode: u32,
241        args: *mut wl_argument,
242    ) {
243        // SAFETY: This function required that slf has the interface INTERFACE
244        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlRegistryRef>(slf) };
245        match opcode {
246            0 => {
247                // SAFETY: INTERFACE requires that there are 3 arguments
248                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
249                // SAFETY: - INTERFACE requires that args[0] contains a uint
250                let arg0 = unsafe { args[0].u };
251                // SAFETY: - INTERFACE requires that args[1] contains a string
252                //         - if the pointer is not null, then it is a c string
253                let arg1 = unsafe { convert_string_arg("wl_registry", "interface", args[1].s) };
254                // SAFETY: - INTERFACE requires that args[2] contains a uint
255                let arg2 = unsafe { args[2].u };
256                self.0.global(slf, arg0, arg1, arg2);
257            }
258            1 => {
259                // SAFETY: INTERFACE requires that there are 1 arguments
260                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
261                // SAFETY: - INTERFACE requires that args[0] contains a uint
262                let arg0 = unsafe { args[0].u };
263                self.0.global_remove(slf, arg0);
264            }
265            _ => {
266                invalid_opcode("wl_registry", opcode);
267            }
268        }
269    }
270}
271
272impl<H> CreateEventHandler<H> for private::ProxyApi
273where
274    H: WlRegistryEventHandler,
275{
276    type EventHandler = private::EventHandler<H>;
277
278    #[inline]
279    fn create_event_handler(handler: H) -> Self::EventHandler {
280        private::EventHandler(handler)
281    }
282}
283
284/// Functional event handlers.
285pub mod event_handlers {
286    use super::*;
287
288    /// Event handler for global events.
289    pub struct Global<F>(F);
290    impl<F> WlRegistryEventHandler for Global<F>
291    where
292        F: Fn(&WlRegistryRef, u32, &str, u32),
293    {
294        #[inline]
295        fn global(&self, _slf: &WlRegistryRef, name: u32, interface: &str, version: u32) {
296            self.0(_slf, name, interface, version)
297        }
298    }
299
300    /// Event handler for global_remove events.
301    pub struct GlobalRemove<F>(F);
302    impl<F> WlRegistryEventHandler for GlobalRemove<F>
303    where
304        F: Fn(&WlRegistryRef, u32),
305    {
306        #[inline]
307        fn global_remove(&self, _slf: &WlRegistryRef, name: u32) {
308            self.0(_slf, name)
309        }
310    }
311
312    impl WlRegistry {
313        /// Creates an event handler for global events.
314        ///
315        /// The event handler ignores all other events.
316        #[allow(dead_code)]
317        pub fn on_global<F>(f: F) -> Global<F>
318        where
319            F: Fn(&WlRegistryRef, u32, &str, u32),
320        {
321            Global(f)
322        }
323
324        /// Creates an event handler for global_remove events.
325        ///
326        /// The event handler ignores all other events.
327        #[allow(dead_code)]
328        pub fn on_global_remove<F>(f: F) -> GlobalRemove<F>
329        where
330            F: Fn(&WlRegistryRef, u32),
331        {
332            GlobalRemove(f)
333        }
334    }
335}