Documentation Index
Fetch the complete documentation index at: https://mintlify.com/smithay/wayland-rs/llms.txt
Use this file to discover all available pages before exploring further.
wayland-backend is the lowest-level crate in the wayland-rs stack. It implements the Wayland wire protocol — serialising and deserialising messages, tracking object lifetimes, and dispatching events to user-supplied callbacks — in two interchangeable implementations called backends. The rs module contains a pure-Rust implementation that has no native dependencies. The sys module wraps the system libwayland-client and libwayland-server libraries for situations where you need to share a Wayland connection with C code or require behavioural parity with the reference implementation. Both backends expose an identical API, so switching between them is a compile-time decision that does not require source changes in most programs.
The two backends
- rs — Pure Rust
- sys — System libwayland
The Prefer the
rs module implements the Wayland binary protocol entirely in Rust. It reads and writes Unix domain socket messages, parses the wire format, and calls ObjectData callbacks without touching any C library.rs backend when you want a single static binary, when running unit tests or CI without a Wayland installation, or when targeting embedded systems where libwayland is not available.Automatic backend selection
You rarely interact withwayland_backend::rs or wayland_backend::sys directly. Both wayland-client and wayland-server re-export the backend that is currently active as their top-level client / server modules. The selection rule is simple:
- If the
client_systemfeature is enabled anywhere in the dependency tree, thesysclient backend is used; otherwise thersbackend is used. - The same applies for
server_systemon the server side.
wayland_backend::client and let a downstream crate (such as wayland-egl, which requires client_system) transparently upgrade the whole dependency tree to the system backend without any source changes on your part.
Cargo features
The
dlopen feature is implemented by wayland-sys. When enabled, wayland-sys uses once_cell to load the library lazily the first time it is needed, and all feature-gated symbols resolve to function-pointer calls at runtime instead of link-time symbols. If the library cannot be found, Backend::connect() returns a NoWaylandLib error.Core client types
The client-side backend API revolves around four types. All four are re-exported from whatever backend is active.| Type | Description |
|---|---|
Backend | The client connection. Clone it freely — all clones share the same underlying socket. |
WeakBackend | A non-owning reference to a Backend. Upgrade with .upgrade(). |
ObjectId | A stable handle to a Wayland protocol object. Implements Clone, Eq, and Hash. |
ReadEventsGuard | Synchronises multi-threaded socket reads. Created by Backend::prepare_read(). |
Connecting and reading events
ObjectData trait
Every Wayland object created through the backend must be associated with an Arc<dyn ObjectData>. The trait has two required methods:
Core server types
| Type | Description |
|---|---|
Backend<D> | The server. Parameterised by your application state type D. |
Handle | Cloneable view into the server for creating globals, sending events, and managing objects. |
ObjectId | Identifies a specific object owned by a specific client. |
ClientId | Identifies a connected client. |
GlobalId | Identifies an advertised global. |
Server dispatch loop
The dlopen feature explained
Without dlopen, the build script links your binary directly against libwayland-client.so or libwayland-server.so. Starting your application on a system that lacks those libraries produces an ELF loader error before main() even runs.
With dlopen, the library is opened at runtime via dlopen(3). Your binary starts successfully even on a system without Wayland. Wayland functionality is gated behind the is_lib_available() check from wayland-sys, and a missing library surfaces as a NoWaylandLib error when you first call Backend::connect().
Version-gated features
Some libwayland APIs were added in specific library versions.wayland-backend gates them behind Cargo features so that your binary can still run on older systems when those features are disabled.
| Feature | Minimum library version | Unlocks |
|---|---|---|
libwayland_client_1_23 | libwayland-client 1.23 | Backend::set_max_buffer_size() |
libwayland_server_1_22 | libwayland-server 1.22 | Handle::global_name() |
libwayland_server_1_23 | libwayland-server 1.23 | Handle::set_default_max_buffer_size(), Handle::set_client_max_buffer_size() |
libwayland_server_1_23 implies libwayland_server_1_22.
raw-window-handle integration
Enable the rwh_06 feature on wayland-backend together with client_system to get a [HasDisplayHandle] implementation on client::Backend: