wayland_backend/sys/
mod.rs

1//! Implementations of the Wayland backends using the system `libwayland`
2
3use std::sync::Arc;
4
5use wayland_sys::client::wl_proxy;
6use wayland_sys::common::{wl_argument, wl_array};
7
8use crate::protocol::{ArgumentType, Interface};
9
10#[cfg(any(test, feature = "client_system"))]
11mod client_impl;
12#[cfg(any(test, feature = "server_system"))]
13mod server_impl;
14
15/// Magic static for wayland objects managed by wayland-client or wayland-server
16///
17/// This static serves no purpose other than existing at a stable address.
18static RUST_MANAGED: u8 = 42;
19
20unsafe fn free_arrays(signature: &[ArgumentType], arglist: &[wl_argument]) {
21    for (typ, arg) in signature.iter().zip(arglist.iter()) {
22        if let ArgumentType::Array = typ {
23            // Safety: the arglist provided arglist must be valid for associated signature
24            // and contains pointers to boxed arrays as appropriate
25            let _ = unsafe { Box::from_raw(arg.a as *mut wl_array) };
26        }
27    }
28}
29
30/// Client-side implementation of a Wayland protocol backend using `libwayland`
31///
32/// Entrypoints are:
33/// - [`Backend::connect()`][client::Backend::connect()] method if you're creating the Wayland connection
34/// - [`Backend::from_foreign_display()`][client::Backend::from_foreign_display()] if you're interacting with an
35///   already existing Wayland connection through FFI.
36#[cfg(any(test, feature = "client_system"))]
37#[path = "../client_api.rs"]
38pub mod client;
39#[cfg(any(test, feature = "client_system"))]
40use client::{ObjectData, ObjectId};
41
42// API complements for FFI
43
44#[cfg(any(test, feature = "client_system"))]
45impl client::ObjectId {
46    /// Creates an object id from a libwayland-client pointer.
47    ///
48    /// # Errors
49    ///
50    /// This function returns an [`InvalidId`][client::InvalidId] error if the interface of the proxy does
51    /// not match the provided interface.
52    ///
53    /// # Safety
54    ///
55    /// The provided pointer must be a valid pointer to a `wl_resource` and remain valid for as
56    /// long as the retrieved `ObjectId` is used.
57    pub unsafe fn from_ptr(
58        interface: &'static crate::protocol::Interface,
59        ptr: *mut wayland_sys::client::wl_proxy,
60    ) -> Result<Self, client::InvalidId> {
61        Ok(Self { id: unsafe { client_impl::InnerObjectId::from_ptr(interface, ptr) }? })
62    }
63
64    /// Get the underlying libwayland pointer for this object
65    ///
66    /// Returns `NULL` if the proxy has already been destroyed.
67    // TODO(breaking): Return `Result`
68    pub fn as_ptr(&self) -> *mut wayland_sys::client::wl_proxy {
69        self.id.as_ptr().map_or(std::ptr::null_mut(), |p| p.as_ptr())
70    }
71
72    /// Get the underlying display pointer for this object.
73    ///
74    /// This pointer is associated with the original display this object
75    /// belongs to.
76    #[cfg(feature = "libwayland_client_1_23")]
77    pub fn display_ptr(
78        &self,
79    ) -> Result<std::ptr::NonNull<wayland_sys::client::wl_display>, crate::types::client::InvalidId>
80    {
81        self.id.display_ptr()
82    }
83}
84
85#[cfg(any(test, feature = "client_system"))]
86impl client::Backend {
87    /// Creates a Backend from a foreign `*mut wl_display`.
88    ///
89    /// This is useful if you are writing a library that is expected to plug itself into an existing
90    /// Wayland connection.
91    ///
92    /// This will initialize the [`Backend`][Self] in "guest" mode, meaning it will not close the
93    /// connection on drop. After the [`Backend`][Self] is dropped, if the server sends an event
94    /// to an object that was created from it, that event will be silently discarded. This may lead to
95    /// protocol errors if the server expects an answer to that event, as such you should make sure to
96    /// cleanup your Wayland state before dropping the [`Backend`][Self].
97    ///
98    /// # Safety
99    ///
100    /// You need to ensure the `*mut wl_display` remains live as long as the  [`Backend`][Self]
101    /// (or its clones) exist.
102    pub unsafe fn from_foreign_display(display: *mut wayland_sys::client::wl_display) -> Self {
103        Self { backend: unsafe { client_impl::InnerBackend::from_foreign_display(display) } }
104    }
105
106    /// Returns the underlying `wl_display` pointer to this backend.
107    ///
108    /// This pointer is needed to interface with EGL, Vulkan and other C libraries.
109    ///
110    /// This pointer is only valid for the lifetime of the backend.
111    pub fn display_ptr(&self) -> *mut wayland_sys::client::wl_display {
112        self.backend.display_ptr()
113    }
114
115    /// Take over handling for a proxy created by a third party.
116    ///
117    /// # Safety
118    ///
119    /// There must never be more than one party managing an object. This is only
120    /// safe to call when a third party gave you ownership of an unmanaged proxy.
121    ///
122    /// The caller is also responsible for making sure the passed interface matches
123    /// the proxy.
124    #[inline]
125    pub unsafe fn manage_object(
126        &self,
127        interface: &'static Interface,
128        proxy: *mut wl_proxy,
129        data: Arc<dyn ObjectData>,
130    ) -> ObjectId {
131        unsafe { self.backend.manage_object(interface, proxy, data) }
132    }
133}
134
135#[cfg(any(test, feature = "client_system"))]
136impl client::ReadEventsGuard {
137    /// The same as [`read`], but doesn't dispatch events.
138    ///
139    /// To dispatch them, use [`dispatch_inner_queue`].
140    ///
141    /// [`read`]: client::ReadEventsGuard::read
142    /// [`dispatch_inner_queue`]: client::Backend::dispatch_inner_queue
143    pub fn read_without_dispatch(mut self) -> Result<(), client::WaylandError> {
144        self.guard.read_non_dispatch()
145    }
146}
147
148// SAFETY:
149// - The display_ptr will not change for the lifetime of the backend.
150// - The display_ptr will be valid, either because we have created the pointer or the caller which created the
151//   backend has ensured the pointer is valid when `Backend::from_foreign_display` was called.
152#[cfg(feature = "raw-window-handle")]
153unsafe impl raw_window_handle::HasRawDisplayHandle for client::Backend {
154    fn raw_display_handle(&self) -> raw_window_handle::RawDisplayHandle {
155        let mut handle = raw_window_handle::WaylandDisplayHandle::empty();
156        handle.display = self.display_ptr().cast();
157        raw_window_handle::RawDisplayHandle::Wayland(handle)
158    }
159}
160
161#[cfg(all(feature = "rwh_06", feature = "client_system"))]
162impl rwh_06::HasDisplayHandle for client::Backend {
163    fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
164        use std::ptr::NonNull;
165
166        // SAFETY:
167        // - The display_ptr will be valid, either because we have created the pointer or the caller which created the
168        //   backend has ensured the pointer is valid when `Backend::from_foreign_display` was called.
169        let ptr = unsafe { NonNull::new_unchecked(self.display_ptr().cast()) };
170        let handle = rwh_06::WaylandDisplayHandle::new(ptr);
171        let raw = rwh_06::RawDisplayHandle::Wayland(handle);
172
173        // SAFETY:
174        // - The display_ptr will be valid, either because we have created the pointer or the caller which created the
175        //   backend has ensured the pointer is valid when `Backend::from_foreign_display` was called.
176        // - The lifetime assigned to the DisplayHandle borrows the Backend, ensuring the display pointer
177        //   is valid..
178        // - The display_ptr will not change for the lifetime of the backend.
179        Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw) })
180    }
181}
182
183/// Server-side implementation of a Wayland protocol backend using `libwayland`
184///
185/// The main entrypoint is the [`Backend::new()`][server::Backend::new()] method.
186#[cfg(any(test, feature = "server_system"))]
187#[path = "../server_api.rs"]
188pub mod server;
189
190#[cfg(any(test, feature = "server_system"))]
191impl server::ObjectId {
192    /// Creates an object from a C pointer.
193    ///
194    /// # Errors
195    ///
196    /// This function returns an [`InvalidId`][server::InvalidId] error if the interface of the
197    /// resource does not match the provided interface.
198    ///
199    /// # Safety
200    ///
201    /// The provided pointer must be a valid pointer to a `wl_resource` and remain valid for as
202    /// long as the retrieved `ObjectId` is used.
203    pub unsafe fn from_ptr(
204        interface: &'static crate::protocol::Interface,
205        ptr: *mut wayland_sys::server::wl_resource,
206    ) -> Result<Self, server::InvalidId> {
207        Ok(Self { id: unsafe { server_impl::InnerObjectId::from_ptr(Some(interface), ptr) }? })
208    }
209
210    /// Returns the pointer that represents this object.
211    ///
212    /// The pointer may be used to interoperate with libwayland.
213    ///
214    /// Returns `NULL` if the resource has already been destroyed.
215    // TODO(breaking): Return `Result`
216    pub fn as_ptr(&self) -> *mut wayland_sys::server::wl_resource {
217        self.id.as_ptr().map_or(std::ptr::null_mut(), |p| p.as_ptr())
218    }
219}
220
221#[cfg(any(test, feature = "server_system"))]
222impl server::Handle {
223    /// Access the underlying `*mut wl_display` pointer
224    pub fn display_ptr(&self) -> *mut wayland_sys::server::wl_display {
225        self.handle.display_ptr()
226    }
227}