Skip to main content

get_registry/common/protocols/wayland/
wl_display.rs

1//! core global object
2//!
3//! The core global object.  This is a special singleton object.  It
4//! is used for internal Wayland protocol features.
5
6use super::super::all_types::*;
7use ::wl_client::builder::prelude::*;
8
9static INTERFACE: wl_interface = wl_interface {
10    name: c"wl_display".as_ptr(),
11    version: 1,
12    method_count: 2,
13    methods: {
14        static MESSAGES: [wl_message; 2] = [
15            wl_message {
16                name: c"sync".as_ptr(),
17                signature: c"n".as_ptr(),
18                types: {
19                    static TYPES: [Option<&'static wl_interface>; 1] =
20                        [Some(WlCallback::WL_INTERFACE)];
21                    TYPES.as_ptr().cast()
22                },
23            },
24            wl_message {
25                name: c"get_registry".as_ptr(),
26                signature: c"n".as_ptr(),
27                types: {
28                    static TYPES: [Option<&'static wl_interface>; 1] =
29                        [Some(WlRegistry::WL_INTERFACE)];
30                    TYPES.as_ptr().cast()
31                },
32            },
33        ];
34        MESSAGES.as_ptr()
35    },
36    event_count: 2,
37    events: {
38        static MESSAGES: [wl_message; 2] = [
39            wl_message {
40                name: c"error".as_ptr(),
41                signature: c"ous".as_ptr(),
42                types: {
43                    static TYPES: [Option<&'static wl_interface>; 3] = [None, None, None];
44                    TYPES.as_ptr().cast()
45                },
46            },
47            wl_message {
48                name: c"delete_id".as_ptr(),
49                signature: c"u".as_ptr(),
50                types: {
51                    static TYPES: [Option<&'static wl_interface>; 1] = [None];
52                    TYPES.as_ptr().cast()
53                },
54            },
55        ];
56        MESSAGES.as_ptr()
57    },
58};
59
60/// An owned wl_display proxy.
61///
62/// See the documentation of [the module][self] for the interface description.
63#[derive(Clone, Eq, PartialEq)]
64#[repr(transparent)]
65pub struct WlDisplay {
66    /// This proxy has the interface INTERFACE.
67    proxy: UntypedOwnedProxy,
68}
69
70/// A borrowed wl_display proxy.
71///
72/// See the documentation of [the module][self] for the interface description.
73#[derive(Eq, PartialEq)]
74#[repr(transparent)]
75pub struct WlDisplayRef {
76    /// This proxy has the interface INTERFACE.
77    proxy: UntypedBorrowedProxy,
78}
79
80// SAFETY: WlDisplay is a transparent wrapper around UntypedOwnedProxy
81unsafe impl UntypedOwnedProxyWrapper for WlDisplay {}
82
83// SAFETY: - INTERFACE is a valid wl_interface
84//         - The only invariant is that self.proxy has a compatible interface
85unsafe impl OwnedProxy for WlDisplay {
86    const INTERFACE: &'static str = "wl_display";
87    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
88    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
89        private::EventHandler(private::NoOpEventHandler);
90    const MAX_VERSION: u32 = 1;
91
92    type Borrowed = WlDisplayRef;
93    type Api = private::ProxyApi;
94    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
95}
96
97// SAFETY: WlDisplayRef is a transparent wrapper around UntypedBorrowedProxy
98unsafe impl UntypedBorrowedProxyWrapper for WlDisplayRef {}
99
100// SAFETY: - The only invariant is that self.proxy has a compatible interface
101unsafe impl BorrowedProxy for WlDisplayRef {
102    type Owned = WlDisplay;
103}
104
105impl Deref for WlDisplay {
106    type Target = WlDisplayRef;
107
108    fn deref(&self) -> &Self::Target {
109        proxy::low_level::deref(self)
110    }
111}
112
113mod private {
114    pub struct ProxyApi;
115
116    #[allow(dead_code)]
117    pub struct EventHandler<H>(pub(super) H);
118
119    #[allow(dead_code)]
120    pub struct NoOpEventHandler;
121}
122
123impl Debug for WlDisplay {
124    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
125        write!(f, "wl_display#{}", self.proxy.id())
126    }
127}
128
129impl Debug for WlDisplayRef {
130    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
131        write!(f, "wl_display#{}", self.proxy.id())
132    }
133}
134
135impl PartialEq<WlDisplayRef> for WlDisplay {
136    fn eq(&self, other: &WlDisplayRef) -> bool {
137        self.proxy == other.proxy
138    }
139}
140
141impl PartialEq<WlDisplay> for WlDisplayRef {
142    fn eq(&self, other: &WlDisplay) -> bool {
143        self.proxy == other.proxy
144    }
145}
146
147#[allow(dead_code)]
148impl WlDisplay {
149    /// Since when the sync request is available.
150    #[allow(dead_code)]
151    pub const REQ__SYNC__SINCE: u32 = 1;
152
153    /// Since when the get_registry request is available.
154    #[allow(dead_code)]
155    pub const REQ__GET_REGISTRY__SINCE: u32 = 1;
156}
157
158#[allow(dead_code)]
159impl WlDisplay {
160    /// asynchronous roundtrip
161    ///
162    /// The sync request asks the server to emit the 'done' event
163    /// on the returned wl_callback object.  Since requests are
164    /// handled in-order and events are delivered in-order, this can
165    /// be used as a barrier to ensure all previous requests and the
166    /// resulting events have been handled.
167    ///
168    /// The object returned by this request will be destroyed by the
169    /// compositor after the callback is fired and as such the client must not
170    /// attempt to use it after that point.
171    ///
172    /// The callback_data passed in the callback is undefined and should be ignored.
173    #[inline]
174    pub fn sync(&self) -> WlCallback {
175        let mut args = [wl_argument { n: 0 }];
176        // SAFETY: - self.proxy has the interface INTERFACE
177        //         - 0 < INTERFACE.method_count = 2
178        //         - the request signature is `n`
179        //         - OwnedProxy::WL_INTERFACE is always a valid interface
180        let data = unsafe {
181            self.proxy
182                .send_constructor::<false>(0, &mut args, WlCallback::WL_INTERFACE, None)
183        };
184        // SAFETY: data has the interface WlCallback::WL_INTERFACE
185        unsafe { proxy::low_level::from_untyped_owned(data) }
186    }
187
188    /// get global registry object
189    ///
190    /// This request creates a registry object that allows the client
191    /// to list and bind the global objects available from the
192    /// compositor.
193    ///
194    /// It should be noted that the server side resources consumed in
195    /// response to a get_registry request can only be released when the
196    /// client disconnects, not when the client side proxy is destroyed.
197    /// Therefore, clients should invoke get_registry as infrequently as
198    /// possible to avoid wasting memory.
199    #[inline]
200    pub fn get_registry(&self) -> WlRegistry {
201        let mut args = [wl_argument { n: 0 }];
202        // SAFETY: - self.proxy has the interface INTERFACE
203        //         - 1 < INTERFACE.method_count = 2
204        //         - the request signature is `n`
205        //         - OwnedProxy::WL_INTERFACE is always a valid interface
206        let data = unsafe {
207            self.proxy
208                .send_constructor::<false>(1, &mut args, WlRegistry::WL_INTERFACE, None)
209        };
210        // SAFETY: data has the interface WlRegistry::WL_INTERFACE
211        unsafe { proxy::low_level::from_untyped_owned(data) }
212    }
213}
214
215#[allow(dead_code)]
216impl WlDisplayRef {
217    /// asynchronous roundtrip
218    ///
219    /// The sync request asks the server to emit the 'done' event
220    /// on the returned wl_callback object.  Since requests are
221    /// handled in-order and events are delivered in-order, this can
222    /// be used as a barrier to ensure all previous requests and the
223    /// resulting events have been handled.
224    ///
225    /// The object returned by this request will be destroyed by the
226    /// compositor after the callback is fired and as such the client must not
227    /// attempt to use it after that point.
228    ///
229    /// The callback_data passed in the callback is undefined and should be ignored.
230    ///
231    /// # Arguments
232    ///
233    /// - `_queue`: The queue that the returned proxy is assigned to.
234    #[inline]
235    pub fn sync(&self, _queue: &Queue) -> WlCallback {
236        let mut args = [wl_argument { n: 0 }];
237        // SAFETY: - self.proxy has the interface INTERFACE
238        //         - 0 < INTERFACE.method_count = 2
239        //         - the request signature is `n`
240        //         - OwnedProxy::WL_INTERFACE is always a valid interface
241        let data = unsafe {
242            self.proxy
243                .send_constructor(_queue, 0, &mut args, WlCallback::WL_INTERFACE, None)
244        };
245        // SAFETY: data has the interface WlCallback::WL_INTERFACE
246        unsafe { proxy::low_level::from_untyped_owned(data) }
247    }
248
249    /// get global registry object
250    ///
251    /// This request creates a registry object that allows the client
252    /// to list and bind the global objects available from the
253    /// compositor.
254    ///
255    /// It should be noted that the server side resources consumed in
256    /// response to a get_registry request can only be released when the
257    /// client disconnects, not when the client side proxy is destroyed.
258    /// Therefore, clients should invoke get_registry as infrequently as
259    /// possible to avoid wasting memory.
260    ///
261    /// # Arguments
262    ///
263    /// - `_queue`: The queue that the returned proxy is assigned to.
264    #[inline]
265    pub fn get_registry(&self, _queue: &Queue) -> WlRegistry {
266        let mut args = [wl_argument { n: 0 }];
267        // SAFETY: - self.proxy has the interface INTERFACE
268        //         - 1 < INTERFACE.method_count = 2
269        //         - the request signature is `n`
270        //         - OwnedProxy::WL_INTERFACE is always a valid interface
271        let data = unsafe {
272            self.proxy
273                .send_constructor(_queue, 1, &mut args, WlRegistry::WL_INTERFACE, None)
274        };
275        // SAFETY: data has the interface WlRegistry::WL_INTERFACE
276        unsafe { proxy::low_level::from_untyped_owned(data) }
277    }
278}
279
280impl WlDisplay {
281    /// Since when the error event is available.
282    #[allow(dead_code)]
283    pub const EVT__ERROR__SINCE: u32 = 1;
284
285    /// Since when the delete_id event is available.
286    #[allow(dead_code)]
287    pub const EVT__DELETE_ID__SINCE: u32 = 1;
288}
289
290/// An event handler for [WlDisplay] proxies.
291#[allow(dead_code)]
292pub trait WlDisplayEventHandler {
293    /// fatal error event
294    ///
295    /// The error event is sent out when a fatal (non-recoverable)
296    /// error has occurred.  The object_id argument is the object
297    /// where the error occurred, most often in response to a request
298    /// to that object.  The code identifies the error and is defined
299    /// by the object interface.  As such, each interface defines its
300    /// own set of error codes.  The message is a brief description
301    /// of the error, for (debugging) convenience.
302    ///
303    /// # Arguments
304    ///
305    /// - `object_id`: object where the error occurred
306    /// - `code`: error code
307    /// - `message`: error description
308    ///
309    /// All borrowed proxies passed to this function are guaranteed to be
310    /// immutable and non-null.
311    #[inline]
312    fn error(
313        &self,
314        _slf: &WlDisplayRef,
315        object_id: Option<&UntypedBorrowedProxy>,
316        code: u32,
317        message: &str,
318    ) {
319        let _ = object_id;
320        let _ = code;
321        let _ = message;
322    }
323
324    /// acknowledge object ID deletion
325    ///
326    /// This event is used internally by the object ID management
327    /// logic. When a client deletes an object that it had created,
328    /// the server will send this event to acknowledge that it has
329    /// seen the delete request. When the client receives this event,
330    /// it will know that it can safely reuse the object ID.
331    ///
332    /// # Arguments
333    ///
334    /// - `id`: deleted object ID
335    #[inline]
336    fn delete_id(&self, _slf: &WlDisplayRef, id: u32) {
337        let _ = id;
338    }
339}
340
341impl WlDisplayEventHandler for private::NoOpEventHandler {}
342
343// SAFETY: - INTERFACE is a valid wl_interface
344unsafe impl<H> EventHandler for private::EventHandler<H>
345where
346    H: WlDisplayEventHandler,
347{
348    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
349
350    #[allow(unused_variables)]
351    unsafe fn handle_event(
352        &self,
353        queue: &Queue,
354        data: *mut u8,
355        slf: &UntypedBorrowedProxy,
356        opcode: u32,
357        args: *mut wl_argument,
358    ) {
359        // SAFETY: This function requires that slf has the interface INTERFACE
360        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlDisplayRef>(slf) };
361        match opcode {
362            0 => {
363                // SAFETY: INTERFACE requires that there are 3 arguments
364                let args = unsafe { &*args.cast::<[wl_argument; 3]>() };
365                // SAFETY: - INTERFACE requires that args[0] contains an object
366                let arg0 = unsafe {
367                    if let Some(p) = NonNull::new(args[0].o.cast()) {
368                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
369                    } else {
370                        None
371                    }
372                };
373                let arg0 = arg0.as_ref();
374                // SAFETY: - INTERFACE requires that args[1] contains a uint
375                let arg1 = unsafe { args[1].u };
376                // SAFETY: - INTERFACE requires that args[2] contains a string
377                //         - if the pointer is not null, then it is a c string
378                let arg2 = unsafe { convert_string_arg("wl_display", "message", args[2].s) };
379                self.0.error(slf, arg0, arg1, arg2);
380            }
381            1 => {
382                // SAFETY: INTERFACE requires that there are 1 arguments
383                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
384                // SAFETY: - INTERFACE requires that args[0] contains a uint
385                let arg0 = unsafe { args[0].u };
386                self.0.delete_id(slf, arg0);
387            }
388            _ => {
389                invalid_opcode("wl_display", opcode);
390            }
391        }
392    }
393}
394
395impl<H> CreateEventHandler<H> for private::ProxyApi
396where
397    H: WlDisplayEventHandler,
398{
399    type EventHandler = private::EventHandler<H>;
400
401    #[inline]
402    fn create_event_handler(handler: H) -> Self::EventHandler {
403        private::EventHandler(handler)
404    }
405}
406
407impl WlDisplay {
408    /// Since when the error.invalid_object enum variant is available.
409    #[allow(dead_code)]
410    pub const ENM__ERROR_INVALID_OBJECT__SINCE: u32 = 1;
411    /// Since when the error.invalid_method enum variant is available.
412    #[allow(dead_code)]
413    pub const ENM__ERROR_INVALID_METHOD__SINCE: u32 = 1;
414    /// Since when the error.no_memory enum variant is available.
415    #[allow(dead_code)]
416    pub const ENM__ERROR_NO_MEMORY__SINCE: u32 = 1;
417    /// Since when the error.implementation enum variant is available.
418    #[allow(dead_code)]
419    pub const ENM__ERROR_IMPLEMENTATION__SINCE: u32 = 1;
420}
421
422/// global error values
423///
424/// These errors are global and can be emitted in response to any
425/// server request.
426#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
427#[allow(dead_code)]
428pub struct WlDisplayError(pub u32);
429
430impl WlDisplayError {
431    /// server couldn't find object
432    #[allow(dead_code)]
433    pub const INVALID_OBJECT: Self = Self(0);
434
435    /// method doesn't exist on the specified interface or malformed request
436    #[allow(dead_code)]
437    pub const INVALID_METHOD: Self = Self(1);
438
439    /// server is out of memory
440    #[allow(dead_code)]
441    pub const NO_MEMORY: Self = Self(2);
442
443    /// implementation error in compositor
444    #[allow(dead_code)]
445    pub const IMPLEMENTATION: Self = Self(3);
446}
447
448impl Debug for WlDisplayError {
449    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
450        let name = match *self {
451            Self::INVALID_OBJECT => "INVALID_OBJECT",
452            Self::INVALID_METHOD => "INVALID_METHOD",
453            Self::NO_MEMORY => "NO_MEMORY",
454            Self::IMPLEMENTATION => "IMPLEMENTATION",
455            _ => return Debug::fmt(&self.0, f),
456        };
457        f.write_str(name)
458    }
459}
460
461/// Functional event handlers.
462pub mod event_handlers {
463    use super::*;
464
465    /// Event handler for error events.
466    pub struct Error<F>(F);
467    impl<F> WlDisplayEventHandler for Error<F>
468    where
469        F: Fn(&WlDisplayRef, Option<&UntypedBorrowedProxy>, u32, &str),
470    {
471        #[inline]
472        fn error(
473            &self,
474            _slf: &WlDisplayRef,
475            object_id: Option<&UntypedBorrowedProxy>,
476            code: u32,
477            message: &str,
478        ) {
479            self.0(_slf, object_id, code, message)
480        }
481    }
482
483    /// Event handler for delete_id events.
484    pub struct DeleteId<F>(F);
485    impl<F> WlDisplayEventHandler for DeleteId<F>
486    where
487        F: Fn(&WlDisplayRef, u32),
488    {
489        #[inline]
490        fn delete_id(&self, _slf: &WlDisplayRef, id: u32) {
491            self.0(_slf, id)
492        }
493    }
494
495    impl WlDisplay {
496        /// Creates an event handler for error events.
497        ///
498        /// The event handler ignores all other events.
499        #[allow(dead_code)]
500        pub fn on_error<F>(f: F) -> Error<F>
501        where
502            F: Fn(&WlDisplayRef, Option<&UntypedBorrowedProxy>, u32, &str),
503        {
504            Error(f)
505        }
506
507        /// Creates an event handler for delete_id events.
508        ///
509        /// The event handler ignores all other events.
510        #[allow(dead_code)]
511        pub fn on_delete_id<F>(f: F) -> DeleteId<F>
512        where
513            F: Fn(&WlDisplayRef, u32),
514        {
515            DeleteId(f)
516        }
517    }
518}