wl_client/test_protocols/core/
wl_display.rs

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