Skip to main content

get_registry/common/protocols_data/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    type Data: 'static;
243
244    /// announce global object
245    ///
246    /// Notify the client of global objects.
247    ///
248    /// The event notifies the client that a global object with
249    /// the given name is now available, and it implements the
250    /// given version of the given interface.
251    ///
252    /// # Arguments
253    ///
254    /// - `name`: numeric name of the global object
255    /// - `interface`: interface implemented by the object
256    /// - `version`: interface version
257    #[inline]
258    fn global(
259        &self,
260        _data: &mut Self::Data,
261        _slf: &WlRegistryRef,
262        name: u32,
263        interface: &str,
264        version: u32,
265    ) {
266        let _ = name;
267        let _ = interface;
268        let _ = version;
269    }
270
271    /// announce removal of global object
272    ///
273    /// Notify the client of removed global objects.
274    ///
275    /// This event notifies the client that the global identified
276    /// by name is no longer available.  If the client bound to
277    /// the global using the bind request, the client should now
278    /// destroy that object.
279    ///
280    /// The object remains valid and requests to the object will be
281    /// ignored until the client destroys it, to avoid races between
282    /// the global going away and a client sending a request to it.
283    ///
284    /// # Arguments
285    ///
286    /// - `name`: numeric name of the global object
287    #[inline]
288    fn global_remove(&self, _data: &mut Self::Data, _slf: &WlRegistryRef, name: u32) {
289        let _ = name;
290    }
291}
292
293impl WlRegistryEventHandler for private::NoOpEventHandler {
294    type Data = ();
295}
296
297// SAFETY: - INTERFACE is a valid wl_interface
298//         - mutable_type always returns the same value
299unsafe impl<H> EventHandler for private::EventHandler<H>
300where
301    H: WlRegistryEventHandler,
302{
303    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
304
305    #[inline]
306    fn mutable_type() -> Option<(TypeId, &'static str)> {
307        let id = TypeId::of::<H::Data>();
308        let name = std::any::type_name::<H::Data>();
309        Some((id, name))
310    }
311
312    #[allow(unused_variables)]
313    unsafe fn handle_event(
314        &self,
315        queue: &Queue,
316        data: *mut u8,
317        slf: &UntypedBorrowedProxy,
318        opcode: u32,
319        args: *mut wl_argument,
320    ) {
321        // SAFETY: This function requires that slf has the interface INTERFACE
322        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlRegistryRef>(slf) };
323        // SAFETY: This function requires that data is `&mut T` where `T`
324        //         has the type id returned by `Self::mutable_type`, i.e.,
325        //         `T = H::Data`.
326        let data: &mut H::Data = unsafe { &mut *data.cast() };
327        match opcode {
328            0 => {
329                // SAFETY: INTERFACE requires that there are 3 arguments
330                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
331                // SAFETY: - INTERFACE requires that args[0] contains a uint
332                let arg0 = unsafe { args[0].u };
333                // SAFETY: - INTERFACE requires that args[1] contains a string
334                //         - if the pointer is not null, then it is a c string
335                let arg1 = unsafe { convert_string_arg("wl_registry", "interface", args[1].s) };
336                // SAFETY: - INTERFACE requires that args[2] contains a uint
337                let arg2 = unsafe { args[2].u };
338                self.0.global(data, slf, arg0, arg1, arg2);
339            }
340            1 => {
341                // SAFETY: INTERFACE requires that there are 1 arguments
342                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
343                // SAFETY: - INTERFACE requires that args[0] contains a uint
344                let arg0 = unsafe { args[0].u };
345                self.0.global_remove(data, slf, arg0);
346            }
347            _ => {
348                invalid_opcode("wl_registry", opcode);
349            }
350        }
351    }
352}
353
354impl<H> CreateEventHandler<H> for private::ProxyApi
355where
356    H: WlRegistryEventHandler,
357{
358    type EventHandler = private::EventHandler<H>;
359
360    #[inline]
361    fn create_event_handler(handler: H) -> Self::EventHandler {
362        private::EventHandler(handler)
363    }
364}
365
366/// Functional event handlers.
367pub mod event_handlers {
368    use super::*;
369
370    /// Event handler for global events.
371    pub struct Global<T, F>(F, PhantomData<fn(&mut T)>);
372    impl<T, F> WlRegistryEventHandler for Global<T, F>
373    where
374        T: 'static,
375        F: Fn(&mut T, &WlRegistryRef, u32, &str, u32),
376    {
377        type Data = T;
378
379        #[inline]
380        fn global(
381            &self,
382            _data: &mut T,
383            _slf: &WlRegistryRef,
384            name: u32,
385            interface: &str,
386            version: u32,
387        ) {
388            self.0(_data, _slf, name, interface, version)
389        }
390    }
391
392    /// Event handler for global_remove events.
393    pub struct GlobalRemove<T, F>(F, PhantomData<fn(&mut T)>);
394    impl<T, F> WlRegistryEventHandler for GlobalRemove<T, F>
395    where
396        T: 'static,
397        F: Fn(&mut T, &WlRegistryRef, u32),
398    {
399        type Data = T;
400
401        #[inline]
402        fn global_remove(&self, _data: &mut T, _slf: &WlRegistryRef, name: u32) {
403            self.0(_data, _slf, name)
404        }
405    }
406
407    impl WlRegistry {
408        /// Creates an event handler for global events.
409        ///
410        /// The event handler ignores all other events.
411        #[allow(dead_code)]
412        pub fn on_global<T, F>(f: F) -> Global<T, F>
413        where
414            T: 'static,
415            F: Fn(&mut T, &WlRegistryRef, u32, &str, u32),
416        {
417            Global(f, PhantomData)
418        }
419
420        /// Creates an event handler for global_remove events.
421        ///
422        /// The event handler ignores all other events.
423        #[allow(dead_code)]
424        pub fn on_global_remove<T, F>(f: F) -> GlobalRemove<T, F>
425        where
426            T: 'static,
427            F: Fn(&mut T, &WlRegistryRef, u32),
428        {
429            GlobalRemove(f, PhantomData)
430        }
431    }
432}