wl_client/test_protocols/core/
wl_root.rs

1use {super::super::all_types::*, crate::builder::prelude::*};
2
3static INTERFACE: wl_interface = wl_interface {
4    name: c"wl_root".as_ptr(),
5    version: 1,
6    method_count: 7,
7    methods: {
8        static MESSAGES: [wl_message; 7] = [
9            wl_message {
10                name: c"create_dummy".as_ptr(),
11                signature: c"n".as_ptr(),
12                types: {
13                    static TYPES: [Option<&'static wl_interface>; 1] =
14                        [Some(WlDummy::WL_INTERFACE)];
15                    TYPES.as_ptr().cast()
16                },
17            },
18            wl_message {
19                name: c"ping_dummy".as_ptr(),
20                signature: c"o".as_ptr(),
21                types: {
22                    static TYPES: [Option<&'static wl_interface>; 1] =
23                        [Some(WlDummy::WL_INTERFACE)];
24                    TYPES.as_ptr().cast()
25                },
26            },
27            wl_message {
28                name: c"destroy".as_ptr(),
29                signature: c"".as_ptr(),
30                types: {
31                    static TYPES: [Option<&'static wl_interface>; 0] = [];
32                    TYPES.as_ptr().cast()
33                },
34            },
35            wl_message {
36                name: c"get_server_name".as_ptr(),
37                signature: c"n".as_ptr(),
38                types: {
39                    static TYPES: [Option<&'static wl_interface>; 1] =
40                        [Some(WlString::WL_INTERFACE)];
41                    TYPES.as_ptr().cast()
42                },
43            },
44            wl_message {
45                name: c"send_new_dummy".as_ptr(),
46                signature: c"".as_ptr(),
47                types: {
48                    static TYPES: [Option<&'static wl_interface>; 0] = [];
49                    TYPES.as_ptr().cast()
50                },
51            },
52            wl_message {
53                name: c"echo".as_ptr(),
54                signature: c"ns".as_ptr(),
55                types: {
56                    static TYPES: [Option<&'static wl_interface>; 2] =
57                        [Some(WlString::WL_INTERFACE), None];
58                    TYPES.as_ptr().cast()
59                },
60            },
61            wl_message {
62                name: c"bind".as_ptr(),
63                signature: c"sun".as_ptr(),
64                types: {
65                    static TYPES: [Option<&'static wl_interface>; 3] = [None, None, None];
66                    TYPES.as_ptr().cast()
67                },
68            },
69        ];
70        MESSAGES.as_ptr()
71    },
72    event_count: 2,
73    events: {
74        static MESSAGES: [wl_message; 2] = [
75            wl_message {
76                name: c"pong_dummy".as_ptr(),
77                signature: c"o".as_ptr(),
78                types: {
79                    static TYPES: [Option<&'static wl_interface>; 1] =
80                        [Some(WlDummy::WL_INTERFACE)];
81                    TYPES.as_ptr().cast()
82                },
83            },
84            wl_message {
85                name: c"new_dummy".as_ptr(),
86                signature: c"n".as_ptr(),
87                types: {
88                    static TYPES: [Option<&'static wl_interface>; 1] =
89                        [Some(WlDummy::WL_INTERFACE)];
90                    TYPES.as_ptr().cast()
91                },
92            },
93        ];
94        MESSAGES.as_ptr()
95    },
96};
97
98/// An owned wl_root proxy.
99///
100/// See the documentation of [the module][self] for the interface description.
101#[derive(Clone, Eq, PartialEq)]
102#[repr(transparent)]
103pub struct WlRoot {
104    /// This proxy has the interface INTERFACE.
105    proxy: UntypedOwnedProxy,
106}
107
108/// A borrowed wl_root proxy.
109///
110/// See the documentation of [the module][self] for the interface description.
111#[derive(Eq, PartialEq)]
112#[repr(transparent)]
113pub struct WlRootRef {
114    /// This proxy has the interface INTERFACE.
115    proxy: UntypedBorrowedProxy,
116}
117
118// SAFETY: WlRoot is a transparent wrapper around UntypedOwnedProxy
119unsafe impl UntypedOwnedProxyWrapper for WlRoot {}
120
121// SAFETY: - INTERFACE is a valid wl_interface
122//         - The only invariant is that self.proxy has a compatible interface
123unsafe impl OwnedProxy for WlRoot {
124    const INTERFACE: &'static str = "wl_root";
125    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
126    const NO_OP_EVENT_HANDLER: Self::NoOpEventHandler =
127        private::EventHandler(private::NoOpEventHandler);
128    const MAX_VERSION: u32 = 1;
129
130    type Borrowed = WlRootRef;
131    type Api = private::ProxyApi;
132    type NoOpEventHandler = private::EventHandler<private::NoOpEventHandler>;
133}
134
135// SAFETY: WlRootRef is a transparent wrapper around UntypedBorrowedProxy
136unsafe impl UntypedBorrowedProxyWrapper for WlRootRef {}
137
138// SAFETY: - The only invariant is that self.proxy has a compatible interface
139unsafe impl BorrowedProxy for WlRootRef {
140    type Owned = WlRoot;
141}
142
143impl Deref for WlRoot {
144    type Target = WlRootRef;
145
146    fn deref(&self) -> &Self::Target {
147        proxy::low_level::deref(self)
148    }
149}
150
151mod private {
152    pub struct ProxyApi;
153
154    #[allow(dead_code)]
155    pub struct EventHandler<H>(pub(super) H);
156
157    #[allow(dead_code)]
158    pub struct NoOpEventHandler;
159}
160
161impl Debug for WlRoot {
162    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
163        write!(f, "wl_root#{}", self.proxy.id())
164    }
165}
166
167impl Debug for WlRootRef {
168    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
169        write!(f, "wl_root#{}", self.proxy.id())
170    }
171}
172
173impl PartialEq<WlRootRef> for WlRoot {
174    fn eq(&self, other: &WlRootRef) -> bool {
175        self.proxy == other.proxy
176    }
177}
178
179impl PartialEq<WlRoot> for WlRootRef {
180    fn eq(&self, other: &WlRoot) -> bool {
181        self.proxy == other.proxy
182    }
183}
184
185#[allow(dead_code)]
186impl WlRoot {
187    /// Since when the create_dummy request is available.
188    #[allow(dead_code)]
189    pub const REQ__CREATE_DUMMY__SINCE: u32 = 1;
190
191    #[inline]
192    pub fn create_dummy(&self) -> WlDummy {
193        let mut args = [wl_argument { n: 0 }];
194        // SAFETY: - self.proxy has the interface INTERFACE
195        //         - 0 < INTERFACE.method_count = 7
196        //         - the request signature is `n`
197        //         - OwnedProxy::WL_INTERFACE is always a valid interface
198        let data = unsafe {
199            self.proxy
200                .send_constructor::<false>(0, &mut args, WlDummy::WL_INTERFACE, None)
201        };
202        // SAFETY: data has the interface WlDummy::WL_INTERFACE
203        unsafe { proxy::low_level::from_untyped_owned(data) }
204    }
205
206    /// Since when the destroy request is available.
207    #[allow(dead_code)]
208    pub const REQ__DESTROY__SINCE: u32 = 1;
209
210    #[inline]
211    pub fn destroy(&self) {
212        let mut args = [];
213        // SAFETY: - self.proxy has the interface INTERFACE
214        //         - 2 < INTERFACE.method_count = 7
215        //         - the request signature is ``
216        unsafe {
217            self.proxy.send_destructor(2, &mut args);
218        }
219    }
220
221    /// Since when the get_server_name request is available.
222    #[allow(dead_code)]
223    pub const REQ__GET_SERVER_NAME__SINCE: u32 = 1;
224
225    #[inline]
226    pub fn get_server_name(&self) -> WlString {
227        let mut args = [wl_argument { n: 0 }];
228        // SAFETY: - self.proxy has the interface INTERFACE
229        //         - 3 < INTERFACE.method_count = 7
230        //         - the request signature is `n`
231        //         - OwnedProxy::WL_INTERFACE is always a valid interface
232        let data = unsafe {
233            self.proxy
234                .send_constructor::<false>(3, &mut args, WlString::WL_INTERFACE, None)
235        };
236        // SAFETY: data has the interface WlString::WL_INTERFACE
237        unsafe { proxy::low_level::from_untyped_owned(data) }
238    }
239
240    /// Since when the echo request is available.
241    #[allow(dead_code)]
242    pub const REQ__ECHO__SINCE: u32 = 1;
243
244    /// # Arguments
245    ///
246    /// - `str`:
247    #[inline]
248    pub fn echo(&self, str: &str) -> WlString {
249        let (arg1,) = (str,);
250        with_cstr_cache(|cache| {
251            let str1_offset = cache.len();
252            cache.extend_from_slice(arg1.as_bytes());
253            cache.push(0);
254            let str1 = cache[str1_offset..].as_ptr().cast();
255            let mut args = [wl_argument { n: 0 }, wl_argument { s: str1 }];
256            // SAFETY: - self.proxy has the interface INTERFACE
257            //         - 5 < INTERFACE.method_count = 7
258            //         - the request signature is `ns`
259            //         - OwnedProxy::WL_INTERFACE is always a valid interface
260            let data = unsafe {
261                self.proxy
262                    .send_constructor::<false>(5, &mut args, WlString::WL_INTERFACE, None)
263            };
264            // SAFETY: data has the interface WlString::WL_INTERFACE
265            unsafe { proxy::low_level::from_untyped_owned(data) }
266        })
267    }
268
269    /// Since when the bind request is available.
270    #[allow(dead_code)]
271    pub const REQ__BIND__SINCE: u32 = 1;
272
273    #[inline]
274    pub fn bind<P: OwnedProxy>(&self, version: u32) -> P {
275        let (arg0,) = (version,);
276        let mut args = [
277            wl_argument {
278                s: P::WL_INTERFACE.name,
279            },
280            wl_argument { u: arg0 },
281            wl_argument { n: 0 },
282        ];
283        // SAFETY: - self.proxy has the interface INTERFACE
284        //         - 6 < INTERFACE.method_count = 7
285        //         - the request signature is `sun`
286        //         - OwnedProxy::WL_INTERFACE is always a valid interface
287        let data = unsafe {
288            self.proxy
289                .send_constructor::<false>(6, &mut args, P::WL_INTERFACE, Some(version))
290        };
291        // SAFETY: data has the interface P::WL_INTERFACE
292        unsafe { proxy::low_level::from_untyped_owned(data) }
293    }
294}
295
296#[allow(dead_code)]
297impl WlRootRef {
298    /// # Arguments
299    ///
300    /// - `_queue`: The queue that the returned proxy is assigned to.
301    #[inline]
302    pub fn create_dummy(&self, _queue: &Queue) -> WlDummy {
303        let mut args = [wl_argument { n: 0 }];
304        // SAFETY: - self.proxy has the interface INTERFACE
305        //         - 0 < INTERFACE.method_count = 7
306        //         - the request signature is `n`
307        //         - OwnedProxy::WL_INTERFACE is always a valid interface
308        let data = unsafe {
309            self.proxy
310                .send_constructor(_queue, 0, &mut args, WlDummy::WL_INTERFACE, None)
311        };
312        // SAFETY: data has the interface WlDummy::WL_INTERFACE
313        unsafe { proxy::low_level::from_untyped_owned(data) }
314    }
315
316    /// # Arguments
317    ///
318    /// - `id`:
319    #[inline]
320    pub fn ping_dummy(&self, id: &WlDummyRef) {
321        let (arg0,) = (id,);
322        let obj0_lock = proxy::lock(arg0);
323        let obj0 = check_argument_proxy("id", obj0_lock.wl_proxy());
324        let mut args = [wl_argument { o: obj0 }];
325        // SAFETY: - self.proxy has the interface INTERFACE
326        //         - 1 < INTERFACE.method_count = 7
327        //         - the request signature is `o`
328        unsafe {
329            self.proxy.send_request(1, &mut args);
330        }
331    }
332
333    /// # Arguments
334    ///
335    /// - `_queue`: The queue that the returned proxy is assigned to.
336    #[inline]
337    pub fn get_server_name(&self, _queue: &Queue) -> WlString {
338        let mut args = [wl_argument { n: 0 }];
339        // SAFETY: - self.proxy has the interface INTERFACE
340        //         - 3 < INTERFACE.method_count = 7
341        //         - the request signature is `n`
342        //         - OwnedProxy::WL_INTERFACE is always a valid interface
343        let data = unsafe {
344            self.proxy
345                .send_constructor(_queue, 3, &mut args, WlString::WL_INTERFACE, None)
346        };
347        // SAFETY: data has the interface WlString::WL_INTERFACE
348        unsafe { proxy::low_level::from_untyped_owned(data) }
349    }
350
351    #[inline]
352    pub fn send_new_dummy(&self) {
353        let mut args = [];
354        // SAFETY: - self.proxy has the interface INTERFACE
355        //         - 4 < INTERFACE.method_count = 7
356        //         - the request signature is ``
357        unsafe {
358            self.proxy.send_request(4, &mut args);
359        }
360    }
361
362    /// # Arguments
363    ///
364    /// - `_queue`: The queue that the returned proxy is assigned to.
365    /// - `str`:
366    #[inline]
367    pub fn echo(&self, _queue: &Queue, str: &str) -> WlString {
368        let (arg1,) = (str,);
369        with_cstr_cache(|cache| {
370            let str1_offset = cache.len();
371            cache.extend_from_slice(arg1.as_bytes());
372            cache.push(0);
373            let str1 = cache[str1_offset..].as_ptr().cast();
374            let mut args = [wl_argument { n: 0 }, wl_argument { s: str1 }];
375            // SAFETY: - self.proxy has the interface INTERFACE
376            //         - 5 < INTERFACE.method_count = 7
377            //         - the request signature is `ns`
378            //         - OwnedProxy::WL_INTERFACE is always a valid interface
379            let data = unsafe {
380                self.proxy
381                    .send_constructor(_queue, 5, &mut args, WlString::WL_INTERFACE, None)
382            };
383            // SAFETY: data has the interface WlString::WL_INTERFACE
384            unsafe { proxy::low_level::from_untyped_owned(data) }
385        })
386    }
387
388    /// # Arguments
389    ///
390    /// - `_queue`: The queue that the returned proxy is assigned to.
391    #[inline]
392    pub fn bind<P: OwnedProxy>(&self, _queue: &Queue, version: u32) -> P {
393        let (arg0,) = (version,);
394        let mut args = [
395            wl_argument {
396                s: P::WL_INTERFACE.name,
397            },
398            wl_argument { u: arg0 },
399            wl_argument { n: 0 },
400        ];
401        // SAFETY: - self.proxy has the interface INTERFACE
402        //         - 6 < INTERFACE.method_count = 7
403        //         - the request signature is `sun`
404        //         - OwnedProxy::WL_INTERFACE is always a valid interface
405        let data = unsafe {
406            self.proxy
407                .send_constructor(_queue, 6, &mut args, P::WL_INTERFACE, Some(version))
408        };
409        // SAFETY: data has the interface P::WL_INTERFACE
410        unsafe { proxy::low_level::from_untyped_owned(data) }
411    }
412}
413
414impl WlRoot {
415    /// Since when the pong_dummy event is available.
416    #[allow(dead_code)]
417    pub const EVT__PONG_DUMMY__SINCE: u32 = 1;
418
419    /// Since when the new_dummy event is available.
420    #[allow(dead_code)]
421    pub const EVT__NEW_DUMMY__SINCE: u32 = 1;
422}
423
424/// An event handler for [WlRoot] proxies.
425#[allow(dead_code)]
426pub trait WlRootEventHandler {
427    /// # Arguments
428    ///
429    /// - `id`:
430    ///
431    /// All borrowed proxies passed to this function are guaranteed to be
432    /// immutable and non-null.
433    #[inline]
434    fn pong_dummy(&self, _slf: &WlRootRef, id: Option<&WlDummyRef>) {
435        let _ = id;
436    }
437
438    /// # Arguments
439    ///
440    /// - `id`:
441    #[inline]
442    fn new_dummy(&self, _slf: &WlRootRef, id: WlDummy) {
443        let _ = id;
444    }
445}
446
447impl WlRootEventHandler for private::NoOpEventHandler {}
448
449// SAFETY: INTERFACE is a valid wl_interface
450unsafe impl<H> EventHandler for private::EventHandler<H>
451where
452    H: WlRootEventHandler,
453{
454    const WL_INTERFACE: &'static wl_interface = &INTERFACE;
455
456    #[allow(unused_variables)]
457    unsafe fn handle_event(
458        &self,
459        queue: &Queue,
460        slf: &UntypedBorrowedProxy,
461        opcode: u32,
462        args: *mut wl_argument,
463    ) {
464        // SAFETY: This function required that slf has the interface INTERFACE
465        let slf = unsafe { proxy::low_level::from_untyped_borrowed::<WlRootRef>(slf) };
466        match opcode {
467            0 => {
468                // SAFETY: INTERFACE requires that there are 1 arguments
469                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
470                // SAFETY: - INTERFACE requires that args[0] contains an object
471                let arg0 = unsafe {
472                    if let Some(p) = NonNull::new(args[0].o.cast()) {
473                        Some(UntypedBorrowedProxy::new_immutable(queue.libwayland(), p))
474                    } else {
475                        None
476                    }
477                };
478                // SAFETY: - INTERFACE requires that the object has the interface WlDummy::WL_INTERFACE
479                let arg0 = arg0.as_ref().map(|arg0| unsafe {
480                    proxy::low_level::from_untyped_borrowed::<WlDummyRef>(arg0)
481                });
482                self.0.pong_dummy(slf, arg0);
483            }
484            1 => {
485                // SAFETY: INTERFACE requires that there are 1 arguments
486                let args = unsafe { &*args.cast::<[wl_argument; 1]>() };
487                // SAFETY: - INTERFACE requires that args[0] contains an object
488                //         - ownership is transferred to this function
489                //         - INTERFACE requires that the object has the interface WlDummy::WL_INTERFACE
490                let arg0 = unsafe {
491                    UntypedOwnedProxy::from_plain_wl_proxy(
492                        queue,
493                        NonNull::new_unchecked(args[0].o.cast()),
494                        WlDummy::WL_INTERFACE,
495                    )
496                };
497                // SAFETY: - INTERFACE requires that the object has the interface WlDummy::WL_INTERFACE
498                let arg0 = unsafe { proxy::low_level::from_untyped_owned::<WlDummy>(arg0) };
499                self.0.new_dummy(slf, arg0);
500            }
501            _ => {
502                invalid_opcode("wl_root", opcode);
503            }
504        }
505    }
506}
507
508impl<H> CreateEventHandler<H> for private::ProxyApi
509where
510    H: WlRootEventHandler,
511{
512    type EventHandler = private::EventHandler<H>;
513
514    #[inline]
515    fn create_event_handler(handler: H) -> Self::EventHandler {
516        private::EventHandler(handler)
517    }
518}
519
520/// Functional event handlers.
521pub mod event_handlers {
522    use super::*;
523
524    /// Event handler for pong_dummy events.
525    pub struct PongDummy<F>(F);
526    impl<F> WlRootEventHandler for PongDummy<F>
527    where
528        F: Fn(&WlRootRef, Option<&WlDummyRef>),
529    {
530        #[inline]
531        fn pong_dummy(&self, _slf: &WlRootRef, id: Option<&WlDummyRef>) {
532            self.0(_slf, id)
533        }
534    }
535
536    /// Event handler for new_dummy events.
537    pub struct NewDummy<F>(F);
538    impl<F> WlRootEventHandler for NewDummy<F>
539    where
540        F: Fn(&WlRootRef, WlDummy),
541    {
542        #[inline]
543        fn new_dummy(&self, _slf: &WlRootRef, id: WlDummy) {
544            self.0(_slf, id)
545        }
546    }
547
548    impl WlRoot {
549        /// Creates an event handler for pong_dummy events.
550        ///
551        /// The event handler ignores all other events.
552        #[allow(dead_code)]
553        pub fn on_pong_dummy<F>(f: F) -> PongDummy<F>
554        where
555            F: Fn(&WlRootRef, Option<&WlDummyRef>),
556        {
557            PongDummy(f)
558        }
559
560        /// Creates an event handler for new_dummy events.
561        ///
562        /// The event handler ignores all other events.
563        #[allow(dead_code)]
564        pub fn on_new_dummy<F>(f: F) -> NewDummy<F>
565        where
566            F: Fn(&WlRootRef, WlDummy),
567        {
568            NewDummy(f)
569        }
570    }
571}