wayland_backend/sys/client_impl/
mod.rs

1//! Client-side implementation of a Wayland protocol backend using `libwayland`
2
3use std::{
4    collections::HashSet,
5    ffi::CStr,
6    os::raw::{c_int, c_void},
7    os::unix::{
8        io::{BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
9        net::UnixStream,
10    },
11    ptr,
12    sync::{
13        atomic::{AtomicBool, Ordering},
14        Arc, Mutex, MutexGuard, Weak,
15    },
16};
17
18use crate::{
19    core_interfaces::WL_DISPLAY_INTERFACE,
20    debug,
21    debug::has_debug_client_env,
22    protocol::{
23        check_for_signature, same_interface, AllowNull, Argument, ArgumentType, Interface, Message,
24        ObjectInfo, ProtocolError, ANONYMOUS_INTERFACE,
25    },
26};
27use scoped_tls::scoped_thread_local;
28use smallvec::SmallVec;
29
30use wayland_sys::{client::*, common::*, ffi_dispatch};
31
32use super::{free_arrays, RUST_MANAGED};
33
34use super::client::*;
35
36scoped_thread_local! {
37    // scoped_tls does not allow unsafe_op_in_unsafe_fn internally
38    #[allow(unsafe_op_in_unsafe_fn)]
39    static BACKEND: Backend
40}
41
42/// An ID representing a Wayland object
43#[derive(Clone)]
44pub struct InnerObjectId {
45    id: u32,
46    ptr: *mut wl_proxy,
47    alive: Option<Arc<AtomicBool>>,
48    interface: &'static Interface,
49}
50
51unsafe impl Send for InnerObjectId {}
52unsafe impl Sync for InnerObjectId {}
53
54impl std::cmp::PartialEq for InnerObjectId {
55    fn eq(&self, other: &Self) -> bool {
56        match (&self.alive, &other.alive) {
57            (Some(ref a), Some(ref b)) => {
58                // this is an object we manage
59                Arc::ptr_eq(a, b)
60            }
61            (None, None) => {
62                // this is an external (un-managed) object
63                ptr::eq(self.ptr, other.ptr)
64                    && self.id == other.id
65                    && same_interface(self.interface, other.interface)
66            }
67            _ => false,
68        }
69    }
70}
71
72impl std::cmp::Eq for InnerObjectId {}
73
74impl std::hash::Hash for InnerObjectId {
75    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
76        self.id.hash(state);
77        self.ptr.hash(state);
78        self.alive
79            .as_ref()
80            .map(|arc| &**arc as *const AtomicBool)
81            .unwrap_or(std::ptr::null())
82            .hash(state);
83    }
84}
85
86impl InnerObjectId {
87    pub fn is_null(&self) -> bool {
88        self.ptr.is_null()
89    }
90
91    pub fn interface(&self) -> &'static Interface {
92        self.interface
93    }
94
95    pub fn protocol_id(&self) -> u32 {
96        self.id
97    }
98
99    pub unsafe fn from_ptr(
100        interface: &'static Interface,
101        ptr: *mut wl_proxy,
102    ) -> Result<Self, InvalidId> {
103        // Safety: the provided pointer must be a valid wayland object
104        let ptr_iface_name = unsafe {
105            CStr::from_ptr(ffi_dispatch!(wayland_client_handle(), wl_proxy_get_class, ptr))
106        };
107        // Safety: the code generated by wayland-scanner is valid
108        let provided_iface_name = unsafe {
109            CStr::from_ptr(
110                interface
111                    .c_ptr
112                    .expect("[wayland-backend-sys] Cannot use Interface without c_ptr!")
113                    .name,
114            )
115        };
116        if ptr_iface_name != provided_iface_name {
117            return Err(InvalidId);
118        }
119
120        let id = ffi_dispatch!(wayland_client_handle(), wl_proxy_get_id, ptr);
121
122        // Test if the proxy is managed by us.
123        let is_rust_managed = ptr::eq(
124            ffi_dispatch!(wayland_client_handle(), wl_proxy_get_listener, ptr),
125            &RUST_MANAGED as *const u8 as *const _,
126        );
127
128        let alive = if is_rust_managed {
129            // Safety: the object is rust_managed, so its user-data pointer must be valid
130            let udata = unsafe {
131                &*(ffi_dispatch!(wayland_client_handle(), wl_proxy_get_user_data, ptr)
132                    as *mut ProxyUserData)
133            };
134            Some(udata.alive.clone())
135        } else {
136            None
137        };
138
139        Ok(Self { id, ptr, alive, interface })
140    }
141
142    pub fn as_ptr(&self) -> *mut wl_proxy {
143        if self.alive.as_ref().map(|alive| alive.load(Ordering::Acquire)).unwrap_or(true) {
144            self.ptr
145        } else {
146            std::ptr::null_mut()
147        }
148    }
149}
150
151impl std::fmt::Display for InnerObjectId {
152    #[cfg_attr(coverage, coverage(off))]
153    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154        write!(f, "{}@{}", self.interface.name, self.id)
155    }
156}
157
158impl std::fmt::Debug for InnerObjectId {
159    #[cfg_attr(coverage, coverage(off))]
160    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
161        write!(f, "ObjectId({})", self)
162    }
163}
164
165struct ProxyUserData {
166    alive: Arc<AtomicBool>,
167    data: Arc<dyn ObjectData>,
168    interface: &'static Interface,
169}
170
171#[derive(Debug)]
172struct ConnectionState {
173    display: *mut wl_display,
174    owns_display: bool,
175    evq: *mut wl_event_queue,
176    display_id: InnerObjectId,
177    last_error: Option<WaylandError>,
178    known_proxies: HashSet<*mut wl_proxy>,
179}
180
181unsafe impl Send for ConnectionState {}
182
183#[derive(Debug)]
184struct Dispatcher;
185
186#[derive(Debug)]
187struct Inner {
188    state: Mutex<ConnectionState>,
189    dispatch_lock: Mutex<Dispatcher>,
190    debug: bool,
191}
192
193#[derive(Clone, Debug)]
194pub struct InnerBackend {
195    inner: Arc<Inner>,
196}
197
198#[derive(Clone, Debug)]
199pub struct WeakInnerBackend {
200    inner: Weak<Inner>,
201}
202
203impl InnerBackend {
204    fn lock_state(&self) -> MutexGuard<ConnectionState> {
205        self.inner.state.lock().unwrap()
206    }
207
208    pub fn downgrade(&self) -> WeakInnerBackend {
209        WeakInnerBackend { inner: Arc::downgrade(&self.inner) }
210    }
211
212    pub fn display_ptr(&self) -> *mut wl_display {
213        self.inner.state.lock().unwrap().display
214    }
215}
216
217impl WeakInnerBackend {
218    pub fn upgrade(&self) -> Option<InnerBackend> {
219        Weak::upgrade(&self.inner).map(|inner| InnerBackend { inner })
220    }
221}
222
223impl PartialEq for InnerBackend {
224    fn eq(&self, rhs: &Self) -> bool {
225        Arc::ptr_eq(&self.inner, &rhs.inner)
226    }
227}
228
229impl Eq for InnerBackend {}
230
231unsafe impl Send for InnerBackend {}
232unsafe impl Sync for InnerBackend {}
233
234impl InnerBackend {
235    pub fn connect(stream: UnixStream) -> Result<Self, NoWaylandLib> {
236        if !is_lib_available() {
237            return Err(NoWaylandLib);
238        }
239        let display = unsafe {
240            ffi_dispatch!(wayland_client_handle(), wl_display_connect_to_fd, stream.into_raw_fd())
241        };
242        if display.is_null() {
243            panic!("[wayland-backend-sys] libwayland reported an allocation failure.");
244        }
245        // set the log trampoline
246        #[cfg(feature = "log")]
247        unsafe {
248            ffi_dispatch!(
249                wayland_client_handle(),
250                wl_log_set_handler_client,
251                wl_log_trampoline_to_rust_client
252            );
253        }
254        Ok(Self::from_display(display, true))
255    }
256
257    pub unsafe fn from_foreign_display(display: *mut wl_display) -> Self {
258        Self::from_display(display, false)
259    }
260
261    fn from_display(display: *mut wl_display, owned: bool) -> Self {
262        let evq =
263            unsafe { ffi_dispatch!(wayland_client_handle(), wl_display_create_queue, display) };
264        let display_alive = owned.then(|| Arc::new(AtomicBool::new(true)));
265        Self {
266            inner: Arc::new(Inner {
267                state: Mutex::new(ConnectionState {
268                    display,
269                    evq,
270                    display_id: InnerObjectId {
271                        id: 1,
272                        ptr: display as *mut wl_proxy,
273                        alive: display_alive,
274                        interface: &WL_DISPLAY_INTERFACE,
275                    },
276                    owns_display: owned,
277                    last_error: None,
278                    known_proxies: HashSet::new(),
279                }),
280                debug: has_debug_client_env(),
281                dispatch_lock: Mutex::new(Dispatcher),
282            }),
283        }
284    }
285
286    pub fn flush(&self) -> Result<(), WaylandError> {
287        let mut guard = self.lock_state();
288        guard.no_last_error()?;
289        let ret =
290            unsafe { ffi_dispatch!(wayland_client_handle(), wl_display_flush, guard.display) };
291        if ret < 0 {
292            Err(guard.store_if_not_wouldblock_and_return_error(std::io::Error::last_os_error()))
293        } else {
294            Ok(())
295        }
296    }
297
298    pub fn poll_fd(&self) -> BorrowedFd {
299        let guard = self.lock_state();
300        unsafe {
301            BorrowedFd::borrow_raw(ffi_dispatch!(
302                wayland_client_handle(),
303                wl_display_get_fd,
304                guard.display
305            ))
306        }
307    }
308
309    pub fn dispatch_inner_queue(&self) -> Result<usize, WaylandError> {
310        self.inner.dispatch_lock.lock().unwrap().dispatch_pending(self.inner.clone())
311    }
312}
313
314impl ConnectionState {
315    #[inline]
316    fn no_last_error(&self) -> Result<(), WaylandError> {
317        if let Some(ref err) = self.last_error {
318            Err(err.clone())
319        } else {
320            Ok(())
321        }
322    }
323
324    #[inline]
325    fn store_and_return_error(&mut self, err: std::io::Error) -> WaylandError {
326        // check if it was actually a protocol error
327        let err = if err.raw_os_error() == Some(rustix::io::Errno::PROTO.raw_os_error()) {
328            let mut object_id = 0;
329            let mut interface = std::ptr::null();
330            let code = unsafe {
331                ffi_dispatch!(
332                    wayland_client_handle(),
333                    wl_display_get_protocol_error,
334                    self.display,
335                    &mut interface,
336                    &mut object_id
337                )
338            };
339            let object_interface = unsafe {
340                if interface.is_null() {
341                    String::new()
342                } else {
343                    let cstr = std::ffi::CStr::from_ptr((*interface).name);
344                    cstr.to_string_lossy().into()
345                }
346            };
347            WaylandError::Protocol(ProtocolError {
348                code,
349                object_id,
350                object_interface,
351                message: String::new(),
352            })
353        } else {
354            WaylandError::Io(err)
355        };
356        crate::log_error!("{}", err);
357        self.last_error = Some(err.clone());
358        err
359    }
360
361    #[inline]
362    fn store_if_not_wouldblock_and_return_error(&mut self, e: std::io::Error) -> WaylandError {
363        if e.kind() != std::io::ErrorKind::WouldBlock {
364            self.store_and_return_error(e)
365        } else {
366            e.into()
367        }
368    }
369}
370
371impl Dispatcher {
372    fn dispatch_pending(&self, inner: Arc<Inner>) -> Result<usize, WaylandError> {
373        let (display, evq) = {
374            let guard = inner.state.lock().unwrap();
375            (guard.display, guard.evq)
376        };
377        let backend = Backend { backend: InnerBackend { inner } };
378
379        // We erase the lifetime of the Handle to be able to store it in the tls,
380        // it's safe as it'll only last until the end of this function call anyway
381        let ret = BACKEND.set(&backend, || unsafe {
382            ffi_dispatch!(wayland_client_handle(), wl_display_dispatch_queue_pending, display, evq)
383        });
384        if ret < 0 {
385            Err(backend
386                .backend
387                .inner
388                .state
389                .lock()
390                .unwrap()
391                .store_if_not_wouldblock_and_return_error(std::io::Error::last_os_error()))
392        } else {
393            Ok(ret as usize)
394        }
395    }
396}
397
398#[derive(Debug)]
399pub struct InnerReadEventsGuard {
400    inner: Arc<Inner>,
401    display: *mut wl_display,
402    done: bool,
403}
404
405impl InnerReadEventsGuard {
406    pub fn try_new(backend: InnerBackend) -> Option<Self> {
407        let (display, evq) = {
408            let guard = backend.lock_state();
409            (guard.display, guard.evq)
410        };
411
412        let ret = unsafe {
413            ffi_dispatch!(wayland_client_handle(), wl_display_prepare_read_queue, display, evq)
414        };
415        if ret < 0 {
416            None
417        } else {
418            Some(Self { inner: backend.inner, display, done: false })
419        }
420    }
421
422    pub fn connection_fd(&self) -> BorrowedFd {
423        unsafe {
424            BorrowedFd::borrow_raw(ffi_dispatch!(
425                wayland_client_handle(),
426                wl_display_get_fd,
427                self.display
428            ))
429        }
430    }
431
432    pub fn read(mut self) -> Result<usize, WaylandError> {
433        self.read_non_dispatch()?;
434        // The read occured, dispatch pending events.
435        self.inner.dispatch_lock.lock().unwrap().dispatch_pending(self.inner.clone())
436    }
437
438    pub fn read_non_dispatch(&mut self) -> Result<(), WaylandError> {
439        self.done = true;
440        let ret =
441            unsafe { ffi_dispatch!(wayland_client_handle(), wl_display_read_events, self.display) };
442        if ret < 0 {
443            // we have done the reading, and there is an error
444            Err(self
445                .inner
446                .state
447                .lock()
448                .unwrap()
449                .store_if_not_wouldblock_and_return_error(std::io::Error::last_os_error()))
450        } else {
451            Ok(())
452        }
453    }
454}
455
456impl Drop for InnerReadEventsGuard {
457    fn drop(&mut self) {
458        if !self.done {
459            unsafe {
460                ffi_dispatch!(wayland_client_handle(), wl_display_cancel_read, self.display);
461            }
462        }
463    }
464}
465
466impl InnerBackend {
467    pub fn display_id(&self) -> ObjectId {
468        ObjectId { id: self.lock_state().display_id.clone() }
469    }
470
471    pub fn last_error(&self) -> Option<WaylandError> {
472        self.lock_state().last_error.clone()
473    }
474
475    pub fn info(&self, ObjectId { id }: ObjectId) -> Result<ObjectInfo, InvalidId> {
476        if !id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(true) || id.ptr.is_null()
477        {
478            return Err(InvalidId);
479        }
480
481        let version = if id.id == 1 {
482            // special case the display, because libwayland returns a version of 0 for it
483            1
484        } else {
485            unsafe { ffi_dispatch!(wayland_client_handle(), wl_proxy_get_version, id.ptr) }
486        };
487
488        Ok(ObjectInfo { id: id.id, interface: id.interface, version })
489    }
490
491    pub fn null_id() -> ObjectId {
492        ObjectId {
493            id: InnerObjectId {
494                ptr: std::ptr::null_mut(),
495                interface: &ANONYMOUS_INTERFACE,
496                id: 0,
497                alive: None,
498            },
499        }
500    }
501
502    pub fn send_request(
503        &self,
504        Message { sender_id: ObjectId { id }, opcode, args }: Message<ObjectId, RawFd>,
505        data: Option<Arc<dyn ObjectData>>,
506        child_spec: Option<(&'static Interface, u32)>,
507    ) -> Result<ObjectId, InvalidId> {
508        let mut guard = self.lock_state();
509        // check that the argument list is valid
510        let message_desc = match id.interface.requests.get(opcode as usize) {
511            Some(msg) => msg,
512            None => {
513                panic!("Unknown opcode {} for object {}@{}.", opcode, id.interface.name, id.id);
514            }
515        };
516
517        if !id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(true) || id.ptr.is_null()
518        {
519            if self.inner.debug {
520                debug::print_send_message(id.interface.name, id.id, message_desc.name, &args, true);
521            }
522            return Err(InvalidId);
523        }
524
525        let parent_version = if id.id == 1 {
526            1
527        } else {
528            unsafe { ffi_dispatch!(wayland_client_handle(), wl_proxy_get_version, id.ptr) }
529        };
530
531        if !check_for_signature(message_desc.signature, &args) {
532            panic!(
533                "Unexpected signature for request {}@{}.{}: expected {:?}, got {:?}.",
534                id.interface.name, id.id, message_desc.name, message_desc.signature, args
535            );
536        }
537
538        // Prepare the child object data
539        let child_spec = if message_desc
540            .signature
541            .iter()
542            .any(|arg| matches!(arg, ArgumentType::NewId))
543        {
544            if let Some((iface, version)) = child_spec {
545                if let Some(child_interface) = message_desc.child_interface {
546                    if !same_interface(child_interface, iface) {
547                        panic!(
548                            "Wrong placeholder used when sending request {}@{}.{}: expected interface {} but got {}",
549                            id.interface.name,
550                            id.id,
551                            message_desc.name,
552                            child_interface.name,
553                            iface.name
554                        );
555                    }
556                    if version != parent_version {
557                        panic!(
558                            "Wrong placeholder used when sending request {}@{}.{}: expected version {} but got {}",
559                            id.interface.name,
560                            id.id,
561                            message_desc.name,
562                            parent_version,
563                            version
564                        );
565                    }
566                }
567                Some((iface, version))
568            } else if let Some(child_interface) = message_desc.child_interface {
569                Some((child_interface, parent_version))
570            } else {
571                panic!(
572                    "Wrong placeholder used when sending request {}@{}.{}: target interface must be specified for a generic constructor.",
573                    id.interface.name,
574                    id.id,
575                    message_desc.name
576                );
577            }
578        } else {
579            None
580        };
581
582        let child_interface_ptr = child_spec
583            .as_ref()
584            .map(|(i, _)| {
585                i.c_ptr.expect("[wayland-backend-sys] Cannot use Interface without c_ptr!")
586                    as *const _
587            })
588            .unwrap_or(std::ptr::null());
589        let child_version = child_spec.as_ref().map(|(_, v)| *v).unwrap_or(parent_version);
590
591        // check that all input objects are valid and create the [wl_argument]
592        let mut argument_list = SmallVec::<[wl_argument; 4]>::with_capacity(args.len());
593        let mut arg_interfaces = message_desc.arg_interfaces.iter();
594        for (i, arg) in args.iter().enumerate() {
595            match *arg {
596                Argument::Uint(u) => argument_list.push(wl_argument { u }),
597                Argument::Int(i) => argument_list.push(wl_argument { i }),
598                Argument::Fixed(f) => argument_list.push(wl_argument { f }),
599                Argument::Fd(h) => argument_list.push(wl_argument { h }),
600                Argument::Array(ref a) => {
601                    let a = Box::new(wl_array {
602                        size: a.len(),
603                        alloc: a.len(),
604                        data: a.as_ptr() as *mut _,
605                    });
606                    argument_list.push(wl_argument { a: Box::into_raw(a) })
607                }
608                Argument::Str(Some(ref s)) => argument_list.push(wl_argument { s: s.as_ptr() }),
609                Argument::Str(None) => argument_list.push(wl_argument { s: std::ptr::null() }),
610                Argument::Object(ref o) => {
611                    let next_interface = arg_interfaces.next().unwrap();
612                    if !o.id.ptr.is_null() {
613                        if !o.id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(true) {
614                            unsafe { free_arrays(message_desc.signature, &argument_list) };
615                            return Err(InvalidId);
616                        }
617                        if !same_interface(next_interface, o.id.interface) {
618                            panic!("Request {}@{}.{} expects an argument of interface {} but {} was provided instead.", id.interface.name, id.id, message_desc.name, next_interface.name, o.id.interface.name);
619                        }
620                    } else if !matches!(
621                        message_desc.signature[i],
622                        ArgumentType::Object(AllowNull::Yes)
623                    ) {
624                        panic!(
625                            "Request {}@{}.{} expects an non-null object argument.",
626                            id.interface.name, id.id, message_desc.name
627                        );
628                    }
629                    argument_list.push(wl_argument { o: o.id.ptr as *const _ })
630                }
631                Argument::NewId(_) => argument_list.push(wl_argument { n: 0 }),
632            }
633        }
634
635        let ret = if child_spec.is_none() {
636            unsafe {
637                ffi_dispatch!(
638                    wayland_client_handle(),
639                    wl_proxy_marshal_array,
640                    id.ptr,
641                    opcode as u32,
642                    argument_list.as_mut_ptr(),
643                )
644            }
645            std::ptr::null_mut()
646        } else {
647            // We are a guest Backend, need to use a wrapper
648            unsafe {
649                let wrapped_ptr =
650                    ffi_dispatch!(wayland_client_handle(), wl_proxy_create_wrapper, id.ptr);
651                ffi_dispatch!(wayland_client_handle(), wl_proxy_set_queue, wrapped_ptr, guard.evq);
652                let ret = ffi_dispatch!(
653                    wayland_client_handle(),
654                    wl_proxy_marshal_array_constructor_versioned,
655                    wrapped_ptr,
656                    opcode as u32,
657                    argument_list.as_mut_ptr(),
658                    child_interface_ptr,
659                    child_version
660                );
661                ffi_dispatch!(wayland_client_handle(), wl_proxy_wrapper_destroy, wrapped_ptr);
662                ret
663            }
664        };
665
666        unsafe {
667            free_arrays(message_desc.signature, &argument_list);
668        }
669
670        if ret.is_null() && child_spec.is_some() {
671            panic!("[wayland-backend-sys] libwayland reported an allocation failure.");
672        }
673
674        // initialize the proxy
675        let child_id = if let Some((child_interface, _)) = child_spec {
676            let data = match data {
677                Some(data) => data,
678                None => {
679                    // we destroy this proxy before panicking to avoid a leak, as it cannot be destroyed by the
680                    // main destructor given it does not yet have a proper user-data
681                    unsafe {
682                        ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, ret);
683                    }
684                    panic!(
685                        "Sending a request creating an object without providing an object data."
686                    );
687                }
688            };
689
690            unsafe { self.manage_object_internal(child_interface, ret, data, &mut guard) }
691        } else {
692            Self::null_id()
693        };
694
695        if message_desc.is_destructor {
696            if let Some(ref alive) = id.alive {
697                let udata = unsafe {
698                    Box::from_raw(ffi_dispatch!(
699                        wayland_client_handle(),
700                        wl_proxy_get_user_data,
701                        id.ptr
702                    ) as *mut ProxyUserData)
703                };
704                unsafe {
705                    ffi_dispatch!(
706                        wayland_client_handle(),
707                        wl_proxy_set_user_data,
708                        id.ptr,
709                        std::ptr::null_mut()
710                    );
711                }
712                alive.store(false, Ordering::Release);
713                udata.data.destroyed(ObjectId { id: id.clone() });
714            }
715
716            guard.known_proxies.remove(&id.ptr);
717
718            unsafe {
719                ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, id.ptr);
720            }
721        }
722
723        Ok(child_id)
724    }
725
726    pub fn get_data(&self, ObjectId { id }: ObjectId) -> Result<Arc<dyn ObjectData>, InvalidId> {
727        if !id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(false) {
728            return Err(InvalidId);
729        }
730
731        if id.id == 1 {
732            // special case the display whose object data is not accessible
733            return Ok(Arc::new(DumbObjectData));
734        }
735
736        let udata = unsafe {
737            &*(ffi_dispatch!(wayland_client_handle(), wl_proxy_get_user_data, id.ptr)
738                as *mut ProxyUserData)
739        };
740        Ok(udata.data.clone())
741    }
742
743    pub fn set_data(
744        &self,
745        ObjectId { id }: ObjectId,
746        data: Arc<dyn ObjectData>,
747    ) -> Result<(), InvalidId> {
748        if !id.alive.as_ref().map(|a| a.load(Ordering::Acquire)).unwrap_or(false) {
749            return Err(InvalidId);
750        }
751
752        // Cannot touch the user_data of the display
753        if id.id == 1 {
754            return Err(InvalidId);
755        }
756
757        let udata = unsafe {
758            &mut *(ffi_dispatch!(wayland_client_handle(), wl_proxy_get_user_data, id.ptr)
759                as *mut ProxyUserData)
760        };
761
762        udata.data = data;
763
764        Ok(())
765    }
766
767    /// Start managing a Wayland object.
768    ///
769    /// Safety: This will change the event queue the proxy is associated with.
770    /// Changing the event queue of an existing proxy is not thread-safe.
771    /// If another thread is concurrently reading the wayland socket and the
772    /// proxy already received an event it might get enqueued on the old event queue.
773    pub unsafe fn manage_object(
774        &self,
775        interface: &'static Interface,
776        proxy: *mut wl_proxy,
777        data: Arc<dyn ObjectData>,
778    ) -> ObjectId {
779        let mut guard = self.lock_state();
780        unsafe {
781            ffi_dispatch!(wayland_client_handle(), wl_proxy_set_queue, proxy, guard.evq);
782            self.manage_object_internal(interface, proxy, data, &mut guard)
783        }
784    }
785
786    /// Start managing a Wayland object.
787    ///
788    /// Opposed to [`Self::manage_object`], this does not acquire any guards.
789    unsafe fn manage_object_internal(
790        &self,
791        interface: &'static Interface,
792        proxy: *mut wl_proxy,
793        data: Arc<dyn ObjectData>,
794        guard: &mut MutexGuard<ConnectionState>,
795    ) -> ObjectId {
796        let alive = Arc::new(AtomicBool::new(true));
797        let object_id = ObjectId {
798            id: InnerObjectId {
799                ptr: proxy,
800                alive: Some(alive.clone()),
801                id: unsafe { ffi_dispatch!(wayland_client_handle(), wl_proxy_get_id, proxy) },
802                interface,
803            },
804        };
805
806        guard.known_proxies.insert(proxy);
807
808        let udata = Box::new(ProxyUserData { alive, data, interface });
809        unsafe {
810            ffi_dispatch!(
811                wayland_client_handle(),
812                wl_proxy_add_dispatcher,
813                proxy,
814                dispatcher_func,
815                &RUST_MANAGED as *const u8 as *const c_void,
816                Box::into_raw(udata) as *mut c_void
817            );
818        }
819
820        object_id
821    }
822}
823
824unsafe extern "C" fn dispatcher_func(
825    _: *const c_void,
826    proxy: *mut c_void,
827    opcode: u32,
828    _: *const wl_message,
829    args: *const wl_argument,
830) -> c_int {
831    let proxy = proxy as *mut wl_proxy;
832
833    // Safety: if our dispatcher fun is called, then the associated proxy must be rust_managed and have a valid user_data
834    let udata_ptr = unsafe {
835        ffi_dispatch!(wayland_client_handle(), wl_proxy_get_user_data, proxy) as *mut ProxyUserData
836    };
837    let udata = unsafe { &mut *udata_ptr };
838
839    let interface = udata.interface;
840    let message_desc = match interface.events.get(opcode as usize) {
841        Some(desc) => desc,
842        None => {
843            crate::log_error!("Unknown event opcode {} for interface {}.", opcode, interface.name);
844            return -1;
845        }
846    };
847
848    let mut parsed_args =
849        SmallVec::<[Argument<ObjectId, OwnedFd>; 4]>::with_capacity(message_desc.signature.len());
850    let mut arg_interfaces = message_desc.arg_interfaces.iter().copied();
851    let mut created = None;
852    // Safety (args deference): the args array provided by libwayland is well-formed
853    for (i, typ) in message_desc.signature.iter().enumerate() {
854        match typ {
855            ArgumentType::Uint => parsed_args.push(Argument::Uint(unsafe { (*args.add(i)).u })),
856            ArgumentType::Int => parsed_args.push(Argument::Int(unsafe { (*args.add(i)).i })),
857            ArgumentType::Fixed => parsed_args.push(Argument::Fixed(unsafe { (*args.add(i)).f })),
858            ArgumentType::Fd => {
859                parsed_args.push(Argument::Fd(unsafe { OwnedFd::from_raw_fd((*args.add(i)).h) }))
860            }
861            ArgumentType::Array => {
862                let array = unsafe { &*((*args.add(i)).a) };
863                // Safety: the array provided by libwayland must be valid
864                let content =
865                    unsafe { std::slice::from_raw_parts(array.data as *mut u8, array.size) };
866                parsed_args.push(Argument::Array(Box::new(content.into())));
867            }
868            ArgumentType::Str(_) => {
869                let ptr = unsafe { (*args.add(i)).s };
870                // Safety: the c-string provided by libwayland must be valid
871                if !ptr.is_null() {
872                    let cstr = unsafe { std::ffi::CStr::from_ptr(ptr) };
873                    parsed_args.push(Argument::Str(Some(Box::new(cstr.into()))));
874                } else {
875                    parsed_args.push(Argument::Str(None));
876                }
877            }
878            ArgumentType::Object(_) => {
879                let obj = unsafe { (*args.add(i)).o as *mut wl_proxy };
880                if !obj.is_null() {
881                    // retrieve the object relevant info
882                    let obj_id = ffi_dispatch!(wayland_client_handle(), wl_proxy_get_id, obj);
883                    // check if this is a local or distant proxy
884                    let next_interface = arg_interfaces.next().unwrap_or(&ANONYMOUS_INTERFACE);
885                    let listener =
886                        ffi_dispatch!(wayland_client_handle(), wl_proxy_get_listener, obj);
887                    if ptr::eq(listener, &RUST_MANAGED as *const u8 as *const c_void) {
888                        // Safety: the object is rust-managed, its user-data must be valid
889                        let obj_udata = unsafe {
890                            &*(ffi_dispatch!(wayland_client_handle(), wl_proxy_get_user_data, obj)
891                                as *mut ProxyUserData)
892                        };
893                        if !same_interface(next_interface, obj_udata.interface) {
894                            crate::log_error!(
895                                "Received object {}@{} in {}.{} but expected interface {}.",
896                                obj_udata.interface.name,
897                                obj_id,
898                                interface.name,
899                                message_desc.name,
900                                next_interface.name,
901                            );
902                            return -1;
903                        }
904                        parsed_args.push(Argument::Object(ObjectId {
905                            id: InnerObjectId {
906                                alive: Some(obj_udata.alive.clone()),
907                                ptr: obj,
908                                id: obj_id,
909                                interface: obj_udata.interface,
910                            },
911                        }));
912                    } else {
913                        parsed_args.push(Argument::Object(ObjectId {
914                            id: InnerObjectId {
915                                alive: None,
916                                id: obj_id,
917                                ptr: obj,
918                                interface: next_interface,
919                            },
920                        }));
921                    }
922                } else {
923                    // libwayland-client.so checks nulls for us
924                    parsed_args.push(Argument::Object(ObjectId {
925                        id: InnerObjectId {
926                            alive: None,
927                            id: 0,
928                            ptr: std::ptr::null_mut(),
929                            interface: &ANONYMOUS_INTERFACE,
930                        },
931                    }))
932                }
933            }
934            ArgumentType::NewId => {
935                let obj = unsafe { (*args.add(i)).o as *mut wl_proxy };
936                // this is a newid, it needs to be initialized
937                if !obj.is_null() {
938                    let child_interface = message_desc.child_interface.unwrap_or_else(|| {
939                        crate::log_warn!(
940                            "Event {}.{} creates an anonymous object.",
941                            interface.name,
942                            opcode
943                        );
944                        &ANONYMOUS_INTERFACE
945                    });
946                    let child_alive = Arc::new(AtomicBool::new(true));
947                    let child_id = InnerObjectId {
948                        ptr: obj,
949                        alive: Some(child_alive.clone()),
950                        id: ffi_dispatch!(wayland_client_handle(), wl_proxy_get_id, obj),
951                        interface: child_interface,
952                    };
953                    let child_udata = Box::into_raw(Box::new(ProxyUserData {
954                        alive: child_alive,
955                        data: Arc::new(UninitObjectData),
956                        interface: child_interface,
957                    }));
958                    created = Some((child_id.clone(), child_udata));
959                    ffi_dispatch!(
960                        wayland_client_handle(),
961                        wl_proxy_add_dispatcher,
962                        obj,
963                        dispatcher_func,
964                        &RUST_MANAGED as *const u8 as *const c_void,
965                        child_udata as *mut c_void
966                    );
967                    parsed_args.push(Argument::NewId(ObjectId { id: child_id }));
968                } else {
969                    parsed_args.push(Argument::NewId(ObjectId {
970                        id: InnerObjectId {
971                            id: 0,
972                            ptr: std::ptr::null_mut(),
973                            alive: None,
974                            interface: &ANONYMOUS_INTERFACE,
975                        },
976                    }))
977                }
978            }
979        }
980    }
981
982    let proxy_id = ffi_dispatch!(wayland_client_handle(), wl_proxy_get_id, proxy);
983    let id = ObjectId {
984        id: InnerObjectId {
985            alive: Some(udata.alive.clone()),
986            ptr: proxy,
987            id: proxy_id,
988            interface: udata.interface,
989        },
990    };
991
992    let ret = BACKEND.with(|backend| {
993        let mut guard = backend.backend.lock_state();
994        if let Some((ref new_id, _)) = created {
995            guard.known_proxies.insert(new_id.ptr);
996        }
997        if message_desc.is_destructor {
998            guard.known_proxies.remove(&proxy);
999        }
1000        std::mem::drop(guard);
1001        udata.data.clone().event(
1002            backend,
1003            Message { sender_id: id.clone(), opcode: opcode as u16, args: parsed_args },
1004        )
1005    });
1006
1007    if message_desc.is_destructor {
1008        // Safety: the udata_ptr must be valid as we are in a rust-managed object, and we are done with using udata
1009        let udata = unsafe { Box::from_raw(udata_ptr) };
1010        ffi_dispatch!(wayland_client_handle(), wl_proxy_set_user_data, proxy, std::ptr::null_mut());
1011        udata.alive.store(false, Ordering::Release);
1012        udata.data.destroyed(id);
1013        ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, proxy);
1014    }
1015
1016    match (created, ret) {
1017        (Some((_, child_udata_ptr)), Some(child_data)) => {
1018            // Safety: child_udata_ptr is valid, we created it earlier
1019            unsafe {
1020                (*child_udata_ptr).data = child_data;
1021            }
1022        }
1023        (Some((child_id, _)), None) => {
1024            panic!("Callback creating object {} did not provide any object data.", child_id);
1025        }
1026        (None, Some(_)) => {
1027            panic!("An object data was returned from a callback not creating any object");
1028        }
1029        (None, None) => {}
1030    }
1031
1032    0
1033}
1034
1035#[cfg(feature = "log")]
1036extern "C" {
1037    fn wl_log_trampoline_to_rust_client(fmt: *const std::os::raw::c_char, list: *const c_void);
1038}
1039
1040impl Drop for ConnectionState {
1041    fn drop(&mut self) {
1042        // Cleanup the objects we know about, libwayland will discard any future message
1043        // they receive.
1044        for proxy_ptr in self.known_proxies.drain() {
1045            let _ = unsafe {
1046                Box::from_raw(ffi_dispatch!(
1047                    wayland_client_handle(),
1048                    wl_proxy_get_user_data,
1049                    proxy_ptr
1050                ) as *mut ProxyUserData)
1051            };
1052            unsafe {
1053                ffi_dispatch!(wayland_client_handle(), wl_proxy_destroy, proxy_ptr);
1054            }
1055        }
1056        unsafe { ffi_dispatch!(wayland_client_handle(), wl_event_queue_destroy, self.evq) }
1057        if self.owns_display {
1058            // we own the connection, close it
1059            unsafe { ffi_dispatch!(wayland_client_handle(), wl_display_disconnect, self.display) }
1060        }
1061    }
1062}