Skip to main content

wayland_server/
lib.rs

1//! Interface for interacting with the Wayland protocol, server-side.
2//!
3//! ## General concepts
4//!
5//! This crate is structured around four main objects: the [`Display`] and [`DisplayHandle`] structs,
6//! resources (objects implementing the [`Resource`] trait), and the [`Dispatch`] trait.
7//!
8//! The [`Display`] is the heart of this crate, it represents the protocol state of your Wayland server, and
9//! takes care of processing messages from clients. You'll need to integrate it in your event loop (see its
10//! documentation for details). From it you can retrieve the [`DisplayHandle`], which is a clonable handle to
11//! the Wayland state and is the type used to actually interact with the protocol.
12//!
13//! Each of the Wayland object you can manipulate is represented by a struct implementing the [`Resource`]
14//! trait. Thos structs are automatically generated from the wayland XML protocol specification. This crate
15//! provides the types generated from the core protocol in the [`protocol`] module. For other standard
16//! protocols, see the `wayland-protocols` crate.
17//!
18//! ## Request dispatching and the [`Dispatch`] trait
19//!
20//! The request dispatching logic provided by this crate is build around the [`Dispatch`] trait. During the
21//! dispatching process (in [`Display::dispatch_clients()`]), all requests sent by clients are read from
22//! their respective process and delivered to your processing logic, by invoking methods on the various
23//! [`Dispatch`] implementations of your `State` struct. In this paradigm, your `State` needs to implement
24//! `Dispatch<O, _>` for every Wayland object `O` it needs to process events for.
25//!
26//! However, implementing all those traits on your own is a lot of (often uninteresting) work. To make this
27//! easier a another library (such as Smithay) can provide generic [`Dispatch`] implementations that you
28//! for a user-data type it defines, so can reuse on your own app. See the documentation of those traits
29//! for details.
30//!
31//! ## Globals
32//!
33//! The entry point of the protocol for clients goes through the protocol globals. Each global represents a
34//! capability of your compositor, a peripheral it has access to, or a protocol extension it supports.
35//! Globals are created by you using [`DisplayHandle::create_global()`], and require your `State` to
36//! implement the [`GlobalDispatch`] trait for the interface associated with that global.
37//!
38//! ## Logging
39//!
40//! This crate can generate some runtime error message (notably when a protocol error occurs). By default
41//! those messages are printed to stderr. If you activate the `log` cargo feature, they will instead be
42//! piped through the `log` crate.
43//!
44//! ## Advanced use
45//!
46//! ### Bypassing [`Dispatch`]
47//!
48//! It may be that for some of your objects, handling them via the [`Dispatch`] trait is impractical. In
49//! those contexts, this crate also provides some escape-hatches to directly interface with the low-level
50//! APIs from `wayland-backend`, allowing you to register callbacks for those objects by directly providing
51//! implementations of the backend [`ObjectData`][backend::ObjectData] trait.
52//! See [`Client::create_resource_from_objdata()`] and [`DataInit::custom_init()`].
53//!
54//! ### Interaction with FFI
55//!
56//! It can happen that you'll need to interact with Wayland states accross FFI, such as for example when
57//! interfacing with the graphics stack for enabling hardware acceleration for clients.
58//!
59//! In this case, you'll need enable the `system` feature to use the `libwayland` backend of
60//! `wayland-backend`.
61//!
62//! Then, you'll generally need:
63//!
64//! - The `*mut wl_display` pointer, that you can retrieve by first retrieving the
65//!   [`Backend`][backend::Backend] using [`Display::backend()`], and then invoke
66//!   [`.handle()`][backend::Backend::handle()][`.display_ptr()`][backend::Handle::display_ptr()].
67//! - The `*mut wl_resource` pointers for the objects you need to share, by first getting the
68//!   [`ObjectId`] using the [`Resource::id()`] method, and then
69//!   the [`ObjectId::as_ptr()`] method.
70//!
71//! If you need to receive pointers from FFI, you can make [`ObjectId`]s from the `*mut wl_resource` pointers
72//! using [`ObjectId::from_ptr()`], and then make the resources using [`Resource::from_id()`].
73#![forbid(improper_ctypes, unsafe_op_in_unsafe_fn)]
74// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc -p <crate>
75#![cfg_attr(docsrs, feature(doc_cfg))]
76
77use std::{
78    fmt,
79    hash::{Hash, Hasher},
80    os::unix::io::OwnedFd,
81};
82use wayland_backend::{
83    protocol::{Interface, Message},
84    server::{InvalidId, ObjectId, WeakHandle},
85};
86
87mod client;
88mod dispatch;
89mod display;
90mod global;
91mod socket;
92
93pub use client::Client;
94pub use dispatch::{DataInit, Dispatch, New, ResourceData};
95pub use display::{Display, DisplayHandle};
96pub use global::GlobalDispatch;
97pub use socket::{BindError, ListeningSocket};
98
99/// Backend reexports
100pub mod backend {
101    pub use wayland_backend::protocol;
102    pub use wayland_backend::server::{
103        Backend, ClientData, ClientId, Credentials, DisconnectReason, GlobalHandler, GlobalId,
104        Handle, InitError, InvalidId, ObjectData, ObjectId, WeakHandle,
105    };
106    pub use wayland_backend::smallvec;
107}
108
109/// Generated protocol definitions
110///
111/// This module is automatically generated from the `wayland.xml` protocol specification, and contains the
112/// interface definitions for the core Wayland protocol.
113#[allow(missing_docs)]
114pub mod protocol {
115    use self::__interfaces::*;
116    use crate as wayland_server;
117    pub mod __interfaces {
118        wayland_scanner::generate_interfaces!("wayland.xml");
119    }
120    wayland_scanner::generate_server_code!("wayland.xml");
121}
122
123// internal imports for dispatching logging depending on the `log` feature
124#[cfg(feature = "log")]
125#[allow(unused_imports)]
126use log::{debug as log_debug, error as log_error, info as log_info, warn as log_warn};
127#[cfg(not(feature = "log"))]
128#[allow(unused_imports)]
129use std::{
130    eprintln as log_error, eprintln as log_warn, eprintln as log_info, eprintln as log_debug,
131};
132
133/// Trait representing a Wayland interface
134pub trait Resource: Clone + std::fmt::Debug + Sized + 'static {
135    /// The event enum for this interface
136    type Event<'a>;
137    /// The request enum for this interface
138    type Request;
139
140    /// The interface description
141    fn interface() -> &'static Interface;
142
143    /// The ID of this object
144    fn id(&self) -> ObjectId;
145
146    /// The client owning this object
147    ///
148    /// Returns [`None`] if the object is no longer alive.
149    fn client(&self) -> Option<Client> {
150        let handle = self.handle().upgrade()?;
151        let client_id = handle.get_client(self.id()).ok()?;
152        let dh = DisplayHandle::from(handle);
153        Client::from_id(&dh, client_id).ok()
154    }
155
156    /// The version of this object
157    fn version(&self) -> u32;
158
159    /// Checks if the Wayland object associated with this proxy is still alive
160    #[inline]
161    fn is_alive(&self) -> bool {
162        if let Some(handle) = self.handle().upgrade() {
163            handle.object_info(self.id()).is_ok()
164        } else {
165            false
166        }
167    }
168
169    /// Access the user-data associated with this object
170    fn data<U: 'static>(&self) -> Option<&U> {
171        Some(&self.object_data()?.downcast_ref::<ResourceData<Self, U>>()?.udata)
172    }
173
174    /// Access the raw data associated with this object.
175    ///
176    /// It is given to you as a `dyn Any`, and you are responsible for downcasting it.
177    ///
178    /// For objects created using the scanner-generated methods, this will be an instance of the
179    /// [`ResourceData`] type.
180    fn object_data(&self) -> Option<&std::sync::Arc<dyn std::any::Any + Send + Sync>>;
181
182    /// Access the backend handle associated with this object
183    fn handle(&self) -> &backend::WeakHandle;
184
185    /// Create an object resource from its ID
186    ///
187    /// Returns an error this the provided object ID does not correspond to the `Self` interface.
188    ///
189    /// **Note:** This method is mostly meant as an implementation detail to be used by code generated by
190    /// wayland-scanner.
191    fn from_id(dh: &DisplayHandle, id: ObjectId) -> Result<Self, InvalidId>;
192
193    /// Send an event to this object
194    fn send_event(&self, evt: Self::Event<'_>) -> Result<(), InvalidId> {
195        let handle = DisplayHandle::from(self.handle().upgrade().ok_or(InvalidId)?);
196        handle.send_event(self, evt)
197    }
198
199    /// Trigger a protocol error on this object
200    ///
201    /// The `code` is intended to be from the `Error` enum declared alongside that object interface.
202    ///
203    /// A protocol error is fatal to the Wayland connection, and the client will be disconnected.
204    #[inline]
205    fn post_error(&self, code: impl Into<u32>, error: impl Into<String>) {
206        if let Some(dh) = self.handle().upgrade().map(DisplayHandle::from) {
207            dh.post_error(self, code.into(), error.into());
208        }
209    }
210
211    /// Parse a event for this object
212    ///
213    /// **Note:** This method is mostly meant as an implementation detail to be used by code generated by
214    /// wayland-scanner.
215    fn parse_request(
216        dh: &DisplayHandle,
217        msg: Message<ObjectId, OwnedFd>,
218    ) -> Result<(Self, Self::Request), DispatchError>;
219
220    /// Serialize an event for this object
221    ///
222    /// **Note:** This method is mostly meant as an implementation detail to be used by code generated by
223    /// wayland-scanner.
224    fn write_event<'a>(
225        &self,
226        dh: &DisplayHandle,
227        req: Self::Event<'a>,
228    ) -> Result<Message<ObjectId, std::os::unix::io::BorrowedFd<'a>>, InvalidId>;
229
230    /// Creates a weak handle to this object
231    ///
232    /// This weak handle will not keep the user-data associated with the object alive,
233    /// and can be converted back to a full resource using [`Weak::upgrade()`].
234    ///
235    /// This can be of use if you need to store resources in the used data of other objects and want
236    /// to be sure to avoid reference cycles that would cause memory leaks.
237    #[inline]
238    fn downgrade(&self) -> Weak<Self> {
239        Weak { handle: self.handle().clone(), id: self.id(), _iface: std::marker::PhantomData }
240    }
241
242    #[doc(hidden)]
243    fn __set_object_data(
244        &mut self,
245        odata: std::sync::Arc<dyn std::any::Any + Send + Sync + 'static>,
246    );
247}
248
249/// An error generated if an illegal request was received from a client
250#[derive(Debug)]
251pub enum DispatchError {
252    /// The received message does not match the specification for the object's interface.
253    BadMessage {
254        /// The id of the target object
255        sender_id: ObjectId,
256        /// The interface of the target object
257        interface: &'static str,
258        /// The opcode number
259        opcode: u16,
260    },
261}
262
263impl std::error::Error for DispatchError {}
264
265impl fmt::Display for DispatchError {
266    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
267        match self {
268            DispatchError::BadMessage { sender_id, interface, opcode } => {
269                write!(f, "Bad message for object {interface}@{sender_id} on opcode {opcode}",)
270            }
271        }
272    }
273}
274
275/// A weak handle to a Wayland object
276///
277/// This handle does not keep the underlying user data alive, and can be converted back to a full resource
278/// using [`Weak::upgrade()`].
279#[derive(Debug, Clone)]
280pub struct Weak<I> {
281    handle: WeakHandle,
282    id: ObjectId,
283    _iface: std::marker::PhantomData<I>,
284}
285
286impl<I: Resource> Weak<I> {
287    /// Try to upgrade with weak handle back into a full resource.
288    ///
289    /// This will fail if either:
290    /// - the object represented by this handle has already been destroyed at the protocol level
291    /// - the Wayland connection has already been closed
292    #[inline]
293    pub fn upgrade(&self) -> Result<I, InvalidId> {
294        let handle = self.handle.upgrade().ok_or(InvalidId)?;
295        // Check if the object has been destroyed
296        handle.object_info(self.id.clone())?;
297        let d_handle = DisplayHandle::from(handle);
298        I::from_id(&d_handle, self.id.clone())
299    }
300
301    /// Check if this resource is still alive
302    ///
303    /// This will return `false` if either:
304    /// - the object represented by this handle has already been destroyed at the protocol level
305    /// - the Wayland connection has already been closed
306    #[inline]
307    pub fn is_alive(&self) -> bool {
308        let Some(handle) = self.handle.upgrade() else {
309            return false;
310        };
311        handle.object_info(self.id.clone()).is_ok()
312    }
313
314    /// The underlying [`ObjectId`]
315    pub fn id(&self) -> ObjectId {
316        self.id.clone()
317    }
318}
319
320impl<I> PartialEq for Weak<I> {
321    #[inline]
322    fn eq(&self, other: &Self) -> bool {
323        self.id == other.id
324    }
325}
326
327impl<I> Eq for Weak<I> {}
328
329impl<I> Hash for Weak<I> {
330    #[inline]
331    fn hash<H: Hasher>(&self, state: &mut H) {
332        self.id.hash(state);
333    }
334}
335
336impl<I: Resource> PartialEq<I> for Weak<I> {
337    #[inline]
338    fn eq(&self, other: &I) -> bool {
339        self.id == other.id()
340    }
341}