1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::{
    os::unix::io::{AsFd, AsRawFd, BorrowedFd},
    os::unix::net::UnixStream,
    sync::Arc,
};

use wayland_backend::{
    protocol::ObjectInfo,
    server::{Backend, ClientData, GlobalId, Handle, InitError, InvalidId, ObjectId},
};

use crate::{
    global::{GlobalData, GlobalDispatch},
    Client, Resource,
};

/// The Wayland display
///
/// This struct is the core of your Wayland compositor. You'll use it in your event loop to drive the
/// protocol processing of all your clients. All other interactions with the protocol itself are done
/// through the [`DisplayHandle`] struct, on which the `State` type parameter is erased for convenience.
///
/// ## Usage
///
/// The main loop of a Wayland compositor generally needs to wait on several sources of events, using
/// tools like `epoll` (on Linux). The Wayland display can be integrated in this mechanism by getting the
/// file descriptor as from [`.backend()`][Self::backend()][`.poll_fd()`][Backend::poll_fd()] and invoking
/// the [`dispatch_clients()`][Self::dispatch_clients()] method whenever it becomes readable.
///
/// To ensure all clients receive the events your compositor sends them, you also need to regularly invoke
/// the [`flush_clients()`][Self::flush_clients()] method, which will write the outgoing buffers into the
/// sockets.
#[derive(Debug)]
pub struct Display<State: 'static> {
    backend: Backend<State>,
}

impl<State: 'static> Display<State> {
    /// Create a new Wayland display
    ///
    /// Can only fail if both the `server_system` and `dlopen` features of `wayland-backend` were enabled,
    /// and the `libwayland-server.so` library could not be found.
    pub fn new() -> Result<Display<State>, InitError> {
        Ok(Display { backend: Backend::new()? })
    }

    /// Retrieve a [`DisplayHandle`] for this [`Display`].
    ///
    /// This is the type with which all of your interactions with the Wayland protocol are done.
    pub fn handle(&self) -> DisplayHandle {
        DisplayHandle { handle: self.backend.handle() }
    }

    /// Dispatch all requests received from clients to their respective callbacks.
    ///
    /// The `state` argument is the main state of your compositor, which will be accessible from most of your
    /// callbacks.
    pub fn dispatch_clients(&mut self, state: &mut State) -> std::io::Result<usize> {
        self.backend.dispatch_all_clients(state)
    }

    /// Flush outgoing buffers into their respective sockets.
    pub fn flush_clients(&mut self) -> std::io::Result<()> {
        self.backend.flush(None)
    }

    /// Access the underlying [`Backend`] of this [`Display`]
    pub fn backend(&mut self) -> &mut Backend<State> {
        &mut self.backend
    }
}

impl<State> AsFd for Display<State> {
    /// Provides fd from [`Backend::poll_fd`] for polling.
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.backend.poll_fd()
    }
}

/// A handle to the Wayland display
///
/// A display handle may be constructed from a [`Handle`] using it's [`From`] implementation.
#[derive(Clone)]
pub struct DisplayHandle {
    pub(crate) handle: Handle,
}

impl std::fmt::Debug for DisplayHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DisplayHandle").finish_non_exhaustive()
    }
}

impl DisplayHandle {
    /// Returns the underlying [`Handle`] from `wayland-backend`.
    pub fn backend_handle(&self) -> Handle {
        self.handle.clone()
    }

    /// Insert a new client in your [`Display`]
    ///
    /// This client will be associated with the provided [`ClientData`], that you can then retrieve from
    /// it via [`Client::get_data()`], and its requests will be processed by the [`Display`] and your
    /// callbacks.
    pub fn insert_client(
        &mut self,
        stream: UnixStream,
        data: Arc<dyn ClientData>,
    ) -> std::io::Result<Client> {
        let id = self.handle.insert_client(stream, data.clone())?;
        Ok(Client { id, data })
    }

    /// Retrieve the [`Client`] which owns the object represented by the given ID
    pub fn get_client(&self, id: ObjectId) -> Result<Client, InvalidId> {
        let client_id = self.handle.get_client(id)?;
        Client::from_id(self, client_id)
    }

    /// Create a new protocol global
    ///
    /// This global will be advertized to clients through the `wl_registry` according to the rules
    /// defined by your [`GlobalDispatch`] implementation for the given interface. Whenever a client
    /// binds this global, the associated [`GlobalDispatch::bind()`] method will be invoked on your
    /// `State`.
    pub fn create_global<State, I: Resource + 'static, U: Send + Sync + 'static>(
        &self,
        version: u32,
        data: U,
    ) -> GlobalId
    where
        State: GlobalDispatch<I, U> + 'static,
    {
        self.handle.create_global::<State>(
            I::interface(),
            version,
            Arc::new(GlobalData { data, _types: std::marker::PhantomData }),
        )
    }

    /// Disable this global
    ///
    /// Clients will be notified of the global removal, and it will not be advertized to new clients. However
    /// the state associated with this global is not freed, so clients which already know about it can still
    /// bind it.
    pub fn disable_global<State: 'static>(&self, id: GlobalId) {
        self.handle.disable_global::<State>(id)
    }

    /// Remove this global
    ///
    /// Clients will be notified of the global removal if it was not already disabled. The state associated
    /// with this global is freed, meaning clients trying to bind it will receive a protocol error.
    ///
    /// When removing a global, it is recommended to first disable it using
    /// [`disable_global()`][Self::disable_global()] to allow some time for clients to register that
    /// the global is getting removed, to avoid a race where a client would be killed because it bound a global
    /// at the same as the server decided to remove it. After the global has been disabled for some time (like
    /// a few seconds) it should be safe to actually remove it.
    pub fn remove_global<State: 'static>(&self, id: GlobalId) {
        self.handle.remove_global::<State>(id)
    }

    /// Access the protocol information for a Wayland object
    ///
    /// Returns an error if the object is no longer valid.
    pub fn object_info(&self, id: ObjectId) -> Result<ObjectInfo, InvalidId> {
        self.handle.object_info(id)
    }

    /// Send an event to given Wayland object
    ///
    /// This is intended to be a low-level method. You can alternatively use the methods on the
    /// type representing your object, or [`Resource::send_event()`], which may be more convenient.
    pub fn send_event<I: Resource>(
        &self,
        resource: &I,
        event: I::Event<'_>,
    ) -> Result<(), InvalidId> {
        let msg = resource.write_event(self, event)?;
        let msg = msg.map_fd(|fd| fd.as_raw_fd());
        self.handle.send_event(msg)
    }

    /// Trigger a protocol error on this object
    ///
    /// This is intended to be a low-level method. See [`Resource::post_error()`], for a more convenient
    /// method.
    pub fn post_error<I: Resource>(&self, resource: &I, code: u32, error: String) {
        self.handle.post_error(resource.id(), code, std::ffi::CString::new(error).unwrap())
    }

    /// Access the object data associated with this object
    ///
    /// This is intended to be a low-level method. See [`Resource::object_data()`], for a more convenient
    /// method.
    pub fn get_object_data(
        &self,
        id: ObjectId,
    ) -> Result<Arc<dyn std::any::Any + Send + Sync + 'static>, InvalidId> {
        self.handle.get_object_data_any(id)
    }

    /// Flush outgoing buffers into their respective sockets.
    pub fn flush_clients(&mut self) -> std::io::Result<()> {
        self.handle.flush(None)
    }
}

impl From<Handle> for DisplayHandle {
    /// Creates a [`DisplayHandle`] using a [`Handle`] from `wayland-backend`.
    fn from(handle: Handle) -> Self {
        Self { handle }
    }
}