Skip to main content

wayland_client/
lib.rs

1//! Interface for interacting with the Wayland protocol, client-side.
2//!
3//! ## General concepts
4//!
5//! This crate is structured around four main objects: the [`Connection`] and [`EventQueue`] structs,
6//! proxies (objects implementing the [`Proxy`] trait), and the [`Dispatch`] trait.
7//!
8//! The [`Connection`] is the heart of this crate. It represents your connection to the Wayland server, and
9//! you'll generally initialize it using the [`Connection::connect_to_env()`] method, which will
10//! attempt to open a Wayland connection following the configuration specified by the ! environment.
11//!
12//! Once you have a [`Connection`], you can create an [`EventQueue`] from it. This [`EventQueue`] will take
13//! care of processing events from the Wayland server and delivering them to your processing logic, in the form
14//! of a state struct with several [`Dispatch`] implementations (see below).
15//!
16//! Each of the Wayland objects you can manipulate is represented by a struct implementing the [`Proxy`]
17//! trait. Those structs are automatically generated from the wayland XML protocol specification. This crate
18//! provides the types generated from the core protocol in the [`protocol`] module. For other standard
19//! protocols, see the `wayland-protocols` crate.
20//!
21//! ## Event dispatching
22//!
23//! The core event dispatching logic provided by this crate is built around the [`EventQueue`] struct. In
24//! this paradigm, receiving and processing events is a two-step process:
25//!
26//! - First, events are read from the Wayland socket. For each event, the backend figures out which [`EventQueue`]
27//!   manages it, and enqueues the event in an internal buffer of that queue.
28//! - Then, the [`EventQueue`] empties its internal buffer by sequentially invoking the appropriate
29//!   [`Dispatch::event()`] method on the `State` value that was provided to it.
30//!
31//! The main goal of this structure is to make your `State` accessible without synchronization to most of
32//! your event-processing logic, to reduce the plumbing costs. See [`EventQueue`]'s documentation for
33//! explanations of how to use it to drive your event loop, and when and how to use multiple
34//! event queues in your app.
35//!
36//! ### The [`Dispatch`] trait and dispatch delegation
37//!
38//! In this paradigm, your `State` needs to implement `Dispatch<O, _>` for every Wayland object `O` it needs to
39//! process events for. This is ensured by the fact that, whenever creating an object using the methods on
40//! an other object, you need to pass a [`QueueHandle<State>`] from the [`EventQueue`] that will be
41//! managing the newly created object.
42//!
43//! However, implementing all those traits on your own is a lot of (often uninteresting) work. To make this
44//! easier another library (such as Smithay's Client Toolkit) can provide generic [`Dispatch`] implementations
45//! for a user-data type it defines, that you can reuse in your own appSee the documentation of those traits
46//! for details.
47//!
48//! ## Getting started example
49//!
50//! As an overview of how this crate is used, here is a commented example of a program that connects to the
51//! Wayland server and lists the globals this server advertised through the `wl_registry`:
52//!
53//! ```rust,no_run
54//! use wayland_client::{protocol::wl_registry, Connection, Dispatch, QueueHandle};
55//! // This struct represents the state of our app. This simple app does not
56//! // need any state, but this type still supports the `Dispatch` implementations.
57//! struct AppData;
58//!
59//! // Implement `Dispatch<WlRegistry, ()> for our state. This provides the logic
60//! // to be able to process events for the wl_registry interface.
61//! //
62//! // The second type parameter is the user-data of our implementation. It is a
63//! // mechanism that allows you to associate a value to each particular Wayland
64//! // object, and allow different dispatching logic depending on the type of the
65//! // associated value.
66//! //
67//! // In this example, we just use () as we don't have any value to associate. See
68//! // the `Dispatch` documentation for more details about this.
69//! impl Dispatch<wl_registry::WlRegistry, AppData> for () {
70//!     fn event(
71//!         &self,
72//!         _state: &mut AppData,
73//!         _: &wl_registry::WlRegistry,
74//!         event: wl_registry::Event,
75//!         _: &Connection,
76//!         _: &QueueHandle<AppData>,
77//!     ) {
78//!         // When receiving events from the wl_registry, we are only interested in the
79//!         // `global` event, which signals a new available global.
80//!         // When receiving this event, we just print its characteristics in this example.
81//!         if let wl_registry::Event::Global { name, interface, version } = event {
82//!             println!("[{}] {} (v{})", name, interface, version);
83//!         }
84//!     }
85//! }
86//!
87//! // The main function of our program
88//! fn main() {
89//!     // Create a Wayland connection by connecting to the server through the
90//!     // environment-provided configuration.
91//!     let conn = Connection::connect_to_env().unwrap();
92//!
93//!     // Retrieve the WlDisplay Wayland object from the connection. This object is
94//!     // the starting point of any Wayland program, from which all other objects will
95//!     // be created.
96//!     let display = conn.display();
97//!
98//!     // Create an event queue for our event processing
99//!     let mut event_queue = conn.new_event_queue();
100//!     // And get its handle to associate new objects to it
101//!     let qh = event_queue.handle();
102//!
103//!     // Create a wl_registry object by sending the wl_display.get_registry request.
104//!     // This method takes two arguments: a handle to the queue that the newly created
105//!     // wl_registry will be assigned to, and the user-data that should be associated
106//!     // with this registry (here it is () as we don't need user-data).
107//!     let _registry = display.get_registry(&qh, ());
108//!
109//!     // At this point everything is ready, and we just need to wait to receive the events
110//!     // from the wl_registry. Our callback will print the advertised globals.
111//!     println!("Advertised globals:");
112//!
113//!     // To actually receive the events, we invoke the `roundtrip` method. This method
114//!     // is special and you will generally only invoke it during the setup of your program:
115//!     // it will block until the server has received and processed all the messages you've
116//!     // sent up to now.
117//!     //
118//!     // In our case, that means it'll block until the server has received our
119//!     // wl_display.get_registry request, and as a reaction has sent us a batch of
120//!     // wl_registry.global events.
121//!     //
122//!     // `roundtrip` will then empty the internal buffer of the queue it has been invoked
123//!     // on, and thus invoke our `Dispatch` implementation that prints the list of advertised
124//!     // globals.
125//!     event_queue.roundtrip(&mut AppData).unwrap();
126//! }
127//! ```
128//!
129//! ## Advanced use
130//!
131//! ### Bypassing [`Dispatch`]
132//!
133//! It may be that for some of your objects, handling them via the [`EventQueue`] is impractical. For example,
134//! if processing the events from those objects doesn't require accessing some global state, and/or you need to
135//! handle them in a context where cranking an event loop is impractical.
136//!
137//! In those contexts, this crate also provides some escape hatches to directly interface with the low-level
138//! APIs from `wayland-backend`, allowing you to register callbacks for those objects that will be invoked
139//! whenever they receive an event and *any* event queue from the program is being dispatched. Those
140//! callbacks are more constrained: they don't get a `&mut State` reference, and must be threadsafe. See
141//! [`Proxy::send_constructor()`] and [`ObjectData`] for details about how to
142//! assign such callbacks to objects.
143//!
144//! ### Interaction with FFI
145//!
146//! It can happen that you'll need to interact with Wayland states accross FFI. A typical example would be if
147//! you need to use the [`raw-window-handle`](https://docs.rs/raw-window-handle/) crate.
148//!
149//! In this case, you'll need enable the `system` feature to use the `libwayland` backend of
150//! `wayland-backend`.
151//!
152//! - If you need to send pointers to FFI, you can retrive the `*mut wl_proxy` pointers from the proxies by
153//!   first getting the [`ObjectId`] using the [`Proxy::id()`] method, and then
154//!   using the [`ObjectId::as_ptr()`] method.
155//  - If you need to receive pointers from FFI, you need to first create a
156//    [`Backend`][backend::Backend] from the `*mut wl_display` using
157//    [`Backend::from_external_display()`][backend::Backend::from_foreign_display()], and then
158//    make it into a [`Connection`] using [`Connection::from_backend()`]. Similarly, you can make
159//    [`ObjectId`]s from the `*mut wl_proxy` pointers using [`ObjectId::from_ptr()`], and then make
160//    the proxies using [`Proxy::from_id()`].
161
162#![allow(clippy::needless_doctest_main)]
163#![warn(missing_docs, missing_debug_implementations)]
164#![forbid(improper_ctypes, unsafe_op_in_unsafe_fn)]
165#![cfg_attr(unstable_coverage, feature(coverage_attribute))]
166// Doc feature labels can be tested locally by running RUSTDOCFLAGS="--cfg=docsrs" cargo +nightly doc -p <crate>
167#![cfg_attr(docsrs, feature(doc_cfg))]
168
169use std::{
170    fmt,
171    hash::{Hash, Hasher},
172    os::unix::io::{BorrowedFd, OwnedFd},
173    sync::Arc,
174};
175use wayland_backend::{
176    client::{InvalidId, ObjectData, ObjectId, WaylandError, WeakBackend},
177    protocol::{Interface, Message},
178};
179
180mod conn;
181mod event_queue;
182pub mod globals;
183
184/// Backend reexports
185pub mod backend {
186    pub use wayland_backend::client::{
187        Backend, InvalidId, NoWaylandLib, ObjectData, ObjectId, ReadEventsGuard, WaylandError,
188        WeakBackend,
189    };
190    pub use wayland_backend::protocol;
191    pub use wayland_backend::smallvec;
192}
193
194pub use conn::{ConnectError, Connection};
195pub use event_queue::{
196    Dispatch, EventQueue, Noop, NoopIgnore, QueueFreezeGuard, QueueHandle, QueueProxyData,
197};
198
199// internal imports for dispatching logging depending on the `log` feature
200#[cfg(feature = "log")]
201#[allow(unused_imports)]
202use log::{debug as log_debug, error as log_error, info as log_info, warn as log_warn};
203#[cfg(not(feature = "log"))]
204#[allow(unused_imports)]
205use std::{
206    eprintln as log_error, eprintln as log_warn, eprintln as log_info, eprintln as log_debug,
207};
208
209/// Generated protocol definitions
210///
211/// This module is automatically generated from the `wayland.xml` protocol specification,
212/// and contains the interface definitions for the core Wayland protocol.
213#[allow(missing_docs)]
214pub mod protocol {
215    use self::__interfaces::*;
216    use crate as wayland_client;
217    pub mod __interfaces {
218        wayland_scanner::generate_interfaces!("wayland.xml");
219    }
220    wayland_scanner::generate_client_code!("wayland.xml");
221}
222
223/// Trait representing a Wayland interface
224pub trait Proxy: Clone + std::fmt::Debug + Sized + 'static {
225    /// The event enum for this interface
226    type Event;
227    /// The request enum for this interface
228    type Request<'a>;
229
230    /// The interface description
231    fn interface() -> &'static Interface;
232
233    /// The ID of this object
234    fn id(&self) -> ObjectId;
235
236    /// The version of this object
237    fn version(&self) -> u32;
238
239    /// Checks if the Wayland object associated with this proxy is still alive
240    fn is_alive(&self) -> bool {
241        if let Some(backend) = self.backend().upgrade() {
242            backend.info(self.id()).is_ok()
243        } else {
244            false
245        }
246    }
247
248    /// Access the user-data associated with this object
249    fn data<U: Send + Sync + 'static>(&self) -> Option<&U> {
250        self.object_data()?.data_as_any().downcast_ref::<U>()
251    }
252
253    /// Access the raw data associated with this object.
254    ///
255    /// For objects created using the scanner-generated methods, this will be an instance of the
256    /// [`QueueProxyData`] type.
257    fn object_data(&self) -> Option<&Arc<dyn ObjectData>>;
258
259    /// Access the backend associated with this object
260    fn backend(&self) -> &backend::WeakBackend;
261
262    /// Create an object proxy from its ID
263    ///
264    /// Returns an error this the provided object ID does not correspond to
265    /// the `Self` interface.
266    ///
267    /// **Note:** This method is mostly meant as an implementation detail to be
268    /// used by code generated by wayland-scanner.
269    fn from_id(conn: &Connection, id: ObjectId) -> Result<Self, InvalidId>;
270
271    /// Create an inert object proxy
272    ///
273    /// **Note:** This method is mostly meant as an implementation detail to be
274    /// used by code generated by wayland-scanner.
275    fn inert(backend: backend::WeakBackend) -> Self;
276
277    /// Send a request for this object.
278    ///
279    /// It is an error to use this function on requests that create objects; use
280    /// [`send_constructor()`][Self::send_constructor()] for such requests.
281    fn send_request(&self, req: Self::Request<'_>) -> Result<(), InvalidId> {
282        let conn = Connection::from_backend(self.backend().upgrade().ok_or(InvalidId)?);
283        let id = conn.send_request(self, req, None)?;
284        debug_assert!(id.is_null());
285        Ok(())
286    }
287
288    /// Send a request for this object that creates another object.
289    ///
290    /// It is an error to use this function on requests that do not create objects; use
291    /// [`send_request()`][Self::send_request()] for such requests.
292    fn send_constructor<I: Proxy>(
293        &self,
294        req: Self::Request<'_>,
295        data: Arc<dyn ObjectData>,
296    ) -> Result<I, InvalidId> {
297        let conn = Connection::from_backend(self.backend().upgrade().ok_or(InvalidId)?);
298        let id = conn.send_request(self, req, Some(data))?;
299        Proxy::from_id(&conn, id)
300    }
301
302    /// Parse a event for this object
303    ///
304    /// **Note:** This method is mostly meant as an implementation detail to be
305    /// used by code generated by wayland-scanner.
306    fn parse_event(
307        conn: &Connection,
308        msg: Message<ObjectId, OwnedFd>,
309    ) -> Result<(Self, Self::Event), DispatchError>;
310
311    /// Serialize a request for this object
312    ///
313    /// **Note:** This method is mostly meant as an implementation detail to be
314    /// used by code generated by wayland-scanner.
315    #[allow(clippy::type_complexity)]
316    fn write_request<'a>(
317        &self,
318        conn: &Connection,
319        req: Self::Request<'a>,
320    ) -> Result<(Message<ObjectId, BorrowedFd<'a>>, Option<(&'static Interface, u32)>), InvalidId>;
321
322    /// Creates a weak handle to this object
323    ///
324    /// This weak handle will not keep the user-data associated with the object alive,
325    /// and can be converted back to a full proxy using [`Weak::upgrade()`].
326    ///
327    /// This can be of use if you need to store proxies in the used data of other objects and want
328    /// to be sure to avoid reference cycles that would cause memory leaks.
329    fn downgrade(&self) -> Weak<Self> {
330        Weak { backend: self.backend().clone(), id: self.id(), _iface: std::marker::PhantomData }
331    }
332}
333
334/// Wayland dispatching error
335#[derive(Debug)]
336pub enum DispatchError {
337    /// The received message does not match the specification for the object's interface.
338    BadMessage {
339        /// The id of the target object
340        sender_id: ObjectId,
341        /// The interface of the target object
342        interface: &'static str,
343        /// The opcode number
344        opcode: u16,
345    },
346    /// The backend generated an error
347    Backend(WaylandError),
348}
349
350impl std::error::Error for DispatchError {
351    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
352        match self {
353            DispatchError::BadMessage { .. } => Option::None,
354            DispatchError::Backend(source) => Some(source),
355        }
356    }
357}
358
359impl fmt::Display for DispatchError {
360    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
361        match self {
362            DispatchError::BadMessage { sender_id, interface, opcode } => {
363                write!(f, "Bad message for object {interface}@{sender_id} on opcode {opcode}")
364            }
365            DispatchError::Backend(source) => {
366                write!(f, "Backend error: {source}")
367            }
368        }
369    }
370}
371
372impl From<WaylandError> for DispatchError {
373    fn from(source: WaylandError) -> Self {
374        DispatchError::Backend(source)
375    }
376}
377
378/// A weak handle to a Wayland object
379///
380/// This handle does not keep the underlying user data alive, and can be converted back to a full proxy
381/// using [`Weak::upgrade()`].
382#[derive(Debug, Clone)]
383pub struct Weak<I> {
384    backend: WeakBackend,
385    id: ObjectId,
386    _iface: std::marker::PhantomData<I>,
387}
388
389impl<I: Proxy> Weak<I> {
390    /// Try to upgrade with weak handle back into a full proxy.
391    ///
392    /// This will fail if either:
393    /// - the object represented by this handle has already been destroyed at the protocol level
394    /// - the Wayland connection has already been closed
395    pub fn upgrade(&self) -> Result<I, InvalidId> {
396        let backend = self.backend.upgrade().ok_or(InvalidId)?;
397        // Check if the object has been destroyed
398        backend.info(self.id.clone())?;
399        let conn = Connection::from_backend(backend);
400        I::from_id(&conn, self.id.clone())
401    }
402
403    /// The underlying [`ObjectId`]
404    pub fn id(&self) -> ObjectId {
405        self.id.clone()
406    }
407}
408
409impl<I> PartialEq for Weak<I> {
410    fn eq(&self, other: &Self) -> bool {
411        self.id == other.id
412    }
413}
414
415impl<I> Eq for Weak<I> {}
416
417impl<I> Hash for Weak<I> {
418    fn hash<H: Hasher>(&self, state: &mut H) {
419        self.id.hash(state);
420    }
421}
422
423impl<I: Proxy> PartialEq<I> for Weak<I> {
424    fn eq(&self, other: &I) -> bool {
425        self.id == other.id()
426    }
427}