Skip to main content

get_registry/common/protocols/wayland/
wl_registry.rs

1//! global registry object
2//!
3//! The singleton global registry object.  The server has a number of
4//! global objects that are available to all clients.  These objects
5//! typically represent an actual object in the server (for example,
6//! an input device) or they are singleton objects that provide
7//! extension functionality.
8//!
9//! When a client creates a registry object, the registry object
10//! will emit a global event for each global currently in the
11//! registry.  Globals come and go as a result of device or
12//! monitor hotplugs, reconfiguration or other events, and the
13//! registry will send out global and global_remove events to
14//! keep the client up to date with the changes.  To mark the end
15//! of the initial burst of events, the client can use the
16//! wl_display.sync request immediately after calling
17//! wl_display.get_registry.
18//!
19//! A client can bind to a global object by using the bind
20//! request.  This creates a client-side handle that lets the object
21//! emit events to the client and lets the client invoke requests on
22//! the object.
23
24use super::super::all_types::*;
25use ::wl_client::builder::prelude::*;
26
27static INTERFACE: wl_interface = wl_interface {
28    name: c"wl_registry".as_ptr(),
29    version: 1,
30    method_count: 1,
31    methods: {
32        static MESSAGES: [wl_message; 1] = [wl_message {
33            name: c"bind".as_ptr(),
34            signature: c"usun".as_ptr(),
35            types: {
36                static TYPES: [Option<&'static wl_interface>; 4] = [None, None, None, None];
37                TYPES.as_ptr().cast()
38            },
39        }];
40        MESSAGES.as_ptr()
41    },
42    event_count: 2,
43    events: {
44        static MESSAGES: [wl_message; 2] = [
45            wl_message {
46                name: c"global".as_ptr(),
47                signature: c"usu".as_ptr(),
48                types: {
49                    static TYPES: [Option<&'static wl_interface>; 3] = [None, None, None];
50                    TYPES.as_ptr().cast()
51                },
52            },
53            wl_message {
54                name: c"global_remove".as_ptr(),
55                signature: c"u".as_ptr(),
56                types: {
57                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
58                    TYPES.as_ptr().cast()
59                },
60            },
61        ];
62        MESSAGES.as_ptr()
63    },
64};
65
66/// An owned wl_registry proxy.
67///
68/// See the documentation of [the module][self] for the interface description.
69#[derive(Clone, Eq, PartialEq)]
70#[repr(transparent)]
71pub struct WlRegistry {
72    /// This proxy has the interface INTERFACE.
73    proxy: UntypedOwnedProxy,
74}
75
76/// A borrowed wl_registry proxy.
77///
78/// See the documentation of [the module][self] for the interface description.
79#[derive(Eq, PartialEq)]
80#[repr(transparent)]
81pub struct WlRegistryRef {
82    /// This proxy has the interface INTERFACE.
83    proxy: UntypedBorrowedProxy,
84}
85
86// SAFETY: WlRegistry is a transparent wrapper around UntypedOwnedProxy
87unsafe impl UntypedOwnedProxyWrapper for WlRegistry {}
88
89// SAFETY: - INTERFACE is a valid wl_interface
90//         - The only invariant is that self.proxy has a compatible interface
91unsafe impl OwnedProxy for WlRegistry {
92    const INTERFACE: &'static str = "wl_registry";
93    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
94    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
95        private::EventHandler(private::NoOpEventHandler);
96    const MAX_VERSION: u32 = 1;
97
98    type Borrowed = WlRegistryRef;
99    type Api = private::ProxyApi;
100    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
101}
102
103// SAFETY: WlRegistryRef is a transparent wrapper around UntypedBorrowedProxy
104unsafe impl UntypedBorrowedProxyWrapper for WlRegistryRef {}
105
106// SAFETY: - The only invariant is that self.proxy has a compatible interface
107unsafe impl BorrowedProxy for WlRegistryRef {
108    type Owned = WlRegistry;
109}
110
111impl Deref for WlRegistry {
112    type Target = WlRegistryRef;
113
114    fn deref(&self) -> &Self::Target {
115        proxy::low_level::deref(self)
116    }
117}
118
119mod private {
120    pub struct ProxyApi;
121
122    #[allow(dead_code)]
123    pub struct EventHandler<H>(pub(super) H);
124
125    #[allow(dead_code)]
126    pub struct NoOpEventHandler;
127}
128
129impl Debug for WlRegistry {
130    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
131        write!(f, "wl_registry#{}", self.proxy.id())
132    }
133}
134
135impl Debug for WlRegistryRef {
136    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
137        write!(f, "wl_registry#{}", self.proxy.id())
138    }
139}
140
141impl PartialEq<WlRegistryRef> for WlRegistry {
142    fn eq(&self, other: &WlRegistryRef) -> bool {
143        self.proxy == other.proxy
144    }
145}
146
147impl PartialEq<WlRegistry> for WlRegistryRef {
148    fn eq(&self, other: &WlRegistry) -> bool {
149        self.proxy == other.proxy
150    }
151}
152
153#[allow(dead_code)]
154impl WlRegistry {
155    /// Since when the bind request is available.
156    #[allow(dead_code)]
157    pub const REQ__BIND__SINCE: u32 = 1;
158}
159
160#[allow(dead_code)]
161impl WlRegistry {
162    /// bind an object to the display
163    ///
164    /// Binds a new, client-created object to the server using the
165    /// specified name as the identifier.
166    ///
167    /// # Arguments
168    ///
169    /// - `name`: unique numeric name of the object
170    #[inline]
171    pub fn bind<P: OwnedProxy>(&self, name: u32, version: u32) -> P {
172        let (arg0, arg1) = (name, version);
173        let mut args = [
174            wl_argument { u: arg0 },
175            wl_argument {
176                s: P::WL_INTERFACE.name,
177            },
178            wl_argument { u: arg1 },
179            wl_argument { n: 0 },
180        ];
181        // SAFETY: - self.proxy has the interface INTERFACE
182        //         - 0 < INTERFACE.method_count = 1
183        //         - the request signature is `usun`
184        //         - OwnedProxy::WL_INTERFACE is always a valid interface
185        let data = unsafe {
186            self.proxy
187                .send_constructor::<false>(0, &mut args, P::WL_INTERFACE, Some(version))
188        };
189        // SAFETY: data has the interface P::WL_INTERFACE
190        unsafe { proxy::low_level::from_untyped_owned(data) }
191    }
192}
193
194#[allow(dead_code)]
195impl WlRegistryRef {
196    /// bind an object to the display
197    ///
198    /// Binds a new, client-created object to the server using the
199    /// specified name as the identifier.
200    ///
201    /// # Arguments
202    ///
203    /// - `_queue`: The queue that the returned proxy is assigned to.
204    /// - `name`: unique numeric name of the object
205    #[inline]
206    pub fn bind<P: OwnedProxy>(&self, _queue: &Queue, name: u32, version: u32) -> P {
207        let (arg0, arg1) = (name, version);
208        let mut args = [
209            wl_argument { u: arg0 },
210            wl_argument {
211                s: P::WL_INTERFACE.name,
212            },
213            wl_argument { u: arg1 },
214            wl_argument { n: 0 },
215        ];
216        // SAFETY: - self.proxy has the interface INTERFACE
217        //         - 0 < INTERFACE.method_count = 1
218        //         - the request signature is `usun`
219        //         - OwnedProxy::WL_INTERFACE is always a valid interface
220        let data = unsafe {
221            self.proxy
222                .send_constructor(_queue, 0, &mut args, P::WL_INTERFACE, Some(version))
223        };
224        // SAFETY: data has the interface P::WL_INTERFACE
225        unsafe { proxy::low_level::from_untyped_owned(data) }
226    }
227}
228
229impl WlRegistry {
230    /// Since when the global event is available.
231    #[allow(dead_code)]
232    pub const EVT__GLOBAL__SINCE: u32 = 1;
233
234    /// Since when the global_remove event is available.
235    #[allow(dead_code)]
236    pub const EVT__GLOBAL_REMOVE__SINCE: u32 = 1;
237}
238
239/// An event handler for [WlRegistry] proxies.
240#[allow(dead_code)]
241pub trait WlRegistryEventHandler {
242    /// announce global object
243    ///
244    /// Notify the client of global objects.
245    ///
246    /// The event notifies the client that a global object with
247    /// the given name is now available, and it implements the
248    /// given version of the given interface.
249    ///
250    /// # Arguments
251    ///
252    /// - `name`: numeric name of the global object
253    /// - `interface`: interface implemented by the object
254    /// - `version`: interface version
255    #[inline]
256    fn global(&self, _slf: &WlRegistryRef, name: u32, interface: &str, version: u32) {
257        let _ = name;
258        let _ = interface;
259        let _ = version;
260    }
261
262    /// announce removal of global object
263    ///
264    /// Notify the client of removed global objects.
265    ///
266    /// This event notifies the client that the global identified
267    /// by name is no longer available.  If the client bound to
268    /// the global using the bind request, the client should now
269    /// destroy that object.
270    ///
271    /// The object remains valid and requests to the object will be
272    /// ignored until the client destroys it, to avoid races between
273    /// the global going away and a client sending a request to it.
274    ///
275    /// # Arguments
276    ///
277    /// - `name`: numeric name of the global object
278    #[inline]
279    fn global_remove(&self, _slf: &WlRegistryRef, name: u32) {
280        let _ = name;
281    }
282}
283
284impl WlRegistryEventHandler for private::NoOpEventHandler {}
285
286// SAFETY: - INTERFACE is a valid wl_interface
287unsafe impl<H> EventHandler for private::EventHandler<H>
288where
289    H: WlRegistryEventHandler,
290{
291    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
292
293    #[allow(unused_variables)]
294    unsafe fn handle_event(
295        &self,
296        queue: &Queue,
297        data: *mut u8,
298        slf: &UntypedBorrowedProxy,
299        opcode: u32,
300        args: *mut wl_argument,
301    ) {
302        // SAFETY: This function requires that slf has the interface INTERFACE
303        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlRegistryRef>(slf) };
304        match opcode {
305            0 => {
306                // SAFETY: INTERFACE requires that there are 3 arguments
307                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
308                // SAFETY: - INTERFACE requires that args[0] contains a uint
309                let arg0 = unsafe { args[0].u };
310                // SAFETY: - INTERFACE requires that args[1] contains a string
311                //         - if the pointer is not null, then it is a c string
312                let arg1 = unsafe { convert_string_arg("wl_registry", "interface", args[1].s) };
313                // SAFETY: - INTERFACE requires that args[2] contains a uint
314                let arg2 = unsafe { args[2].u };
315                self.0.global(slf, arg0, arg1, arg2);
316            }
317            1 => {
318                // SAFETY: INTERFACE requires that there are 1 arguments
319                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
320                // SAFETY: - INTERFACE requires that args[0] contains a uint
321                let arg0 = unsafe { args[0].u };
322                self.0.global_remove(slf, arg0);
323            }
324            _ => {
325                invalid_opcode("wl_registry", opcode);
326            }
327        }
328    }
329}
330
331impl<H> CreateEventHandler<H> for private::ProxyApi
332where
333    H: WlRegistryEventHandler,
334{
335    type EventHandler = private::EventHandler<H>;
336
337    #[inline]
338    fn create_event_handler(handler: H) -> Self::EventHandler {
339        private::EventHandler(handler)
340    }
341}
342
343/// Functional event handlers.
344pub mod event_handlers {
345    use super::*;
346
347    /// Event handler for global events.
348    pub struct Global<F>(F);
349    impl<F> WlRegistryEventHandler for Global<F>
350    where
351        F: Fn(&WlRegistryRef, u32, &str, u32),
352    {
353        #[inline]
354        fn global(&self, _slf: &WlRegistryRef, name: u32, interface: &str, version: u32) {
355            self.0(_slf, name, interface, version)
356        }
357    }
358
359    /// Event handler for global_remove events.
360    pub struct GlobalRemove<F>(F);
361    impl<F> WlRegistryEventHandler for GlobalRemove<F>
362    where
363        F: Fn(&WlRegistryRef, u32),
364    {
365        #[inline]
366        fn global_remove(&self, _slf: &WlRegistryRef, name: u32) {
367            self.0(_slf, name)
368        }
369    }
370
371    impl WlRegistry {
372        /// Creates an event handler for global events.
373        ///
374        /// The event handler ignores all other events.
375        #[allow(dead_code)]
376        pub fn on_global<F>(f: F) -> Global<F>
377        where
378            F: Fn(&WlRegistryRef, u32, &str, u32),
379        {
380            Global(f)
381        }
382
383        /// Creates an event handler for global_remove events.
384        ///
385        /// The event handler ignores all other events.
386        #[allow(dead_code)]
387        pub fn on_global_remove<F>(f: F) -> GlobalRemove<F>
388        where
389            F: Fn(&WlRegistryRef, u32),
390        {
391            GlobalRemove(f)
392        }
393    }
394}