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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
use std::env; use std::ffi::{OsStr, OsString}; use std::io::{Error as IoError, ErrorKind, Result as IoResult}; use std::os::unix::io::{IntoRawFd, RawFd}; use std::path::PathBuf; #[cfg(feature = "use_system_lib")] use wayland_sys::server::wl_display; use crate::imp::DisplayInner; use crate::{Client, Filter, Global, Interface, Main, Resource}; /// The wayland display /// /// This is the core of your wayland server, this object must /// be kept alive as long as your server is running. It allows /// you to manage listening sockets and clients. pub struct Display { inner: DisplayInner, } impl Display { /// Create a new display /// /// This method provides you a `Display` and inserts it into an existing /// `calloop::EventLoop`. /// /// Note that at this point, your server is not yet ready to receive connections, /// your need to add listening sockets using the `add_socket*` methods. #[allow(clippy::new_without_default)] pub fn new() -> Display { Display { inner: DisplayInner::new() } } /// Create a new global object /// /// This object will be advertised to all clients, and they will /// be able to instantiate it from their registries. /// /// Your filter will receinve an event whenever a client instantiates /// this global. /// /// The version specified is the **highest supported version**, you must /// be able to handle clients that choose to instantiate this global with /// a lower version number. pub fn create_global<I, E>(&mut self, version: u32, filter: Filter<E>) -> Global<I> where I: Interface + AsRef<Resource<I>> + From<Resource<I>>, E: From<(Main<I>, u32)> + 'static, { assert!( version <= I::VERSION, "Cannot create global {} with version {}, maximum protocol version is {}.", I::NAME, version, I::VERSION ); Global::create(self.inner.create_global( version, move |main, id, ddata| filter.send((main, id).into(), ddata), None::<fn(_) -> bool>, )) } /// Create a new global object with a client filter /// /// This object will only be advertized to clients for which your /// client filter closure returns `true`. Note that this client filter cannot /// access the `DispatchData` as it may be invoked outside of a dispatch. As /// such it should only rely on the client-associated user-data to make its /// decision. /// /// Your event filter will be receive an event whenever a client instantiates /// this global. /// /// The version specified is the **highest supported version**, you must /// be able to handle clients that choose to instantiate this global with /// a lower version number. pub fn create_global_with_filter<I, E, F>( &mut self, version: u32, filter: Filter<E>, mut client_filter: F, ) -> Global<I> where I: Interface + AsRef<Resource<I>> + From<Resource<I>>, E: From<(Main<I>, u32)> + 'static, F: FnMut(Client) -> bool + 'static, { assert!( version <= I::VERSION, "Cannot create global {} with version {}, maximum protocol version is {}.", I::NAME, version, I::VERSION ); Global::create(self.inner.create_global( version, move |main, id, ddata| filter.send((main, id).into(), ddata), Some(move |client_inner| client_filter(Client::make(client_inner))), )) } /// Flush events to the clients /// /// Will send as many pending events as possible to the respective sockets of the clients. /// Will not block, but might not send everything if the socket buffer fills up. /// /// The provided `data` will be mutably accessible from all the callbacks that may be called /// during this (destructors notably) via the [`DispatchData`](struct.DispatchData.html) mechanism. /// If you don't need global data, you can just provide a `&mut ()` there. pub fn flush_clients<T: std::any::Any>(&mut self, data: &mut T) { let data = crate::DispatchData::wrap(data); self.inner.flush_clients(data) } /// Dispatches all pending messages to their respective filters /// /// This method will block waiting for messages until one of these occur: /// /// - Some messages are received, in which case all pending messages are processed /// - The timeout is reached /// - An error occurs /// /// If `timeout` is a duration of 0, this function will only process pending messages and then /// return, not blocking. /// /// The provided `data` will be mutably accessible from all the callbacks, via the /// [`DispatchData`](struct.DispatchData.html) mechanism. If you don't need global data, you /// can just provide a `&mut ()` there. /// /// In general for good performance you will want to integrate the `Display` into your own event loop, /// monitoring the file descriptor retrieved by the `get_poll_fd()` method, and only calling this method /// when messages are available, with a timeout of `0`. pub fn dispatch<T: std::any::Any>( &mut self, timeout: std::time::Duration, data: &mut T, ) -> IoResult<()> { let data = crate::DispatchData::wrap(data); let ms = timeout.as_millis(); let clamped_timeout = if ms > std::i32::MAX as u128 { std::i32::MAX } else { ms as i32 }; self.inner.dispatch(clamped_timeout, data) } /// Retrieve the underlying file descriptor /// /// This file descriptor can be monitored for activity with a poll/epoll like mechanism. /// When it becomes readable, this means that there are pending messages that would be dispatched if /// you call `dispatch` with a timeout of 0. /// /// You should not use this file descriptor for any other purpose than monitoring it. pub fn get_poll_fd(&self) -> RawFd { self.inner.get_poll_fd() } } impl Display { /// Add a listening socket to this display /// /// Wayland clients will be able to connect to your compositor from this socket. /// /// Socket will be created in the directory specified by the environment variable /// `XDG_RUNTIME_DIR`. /// /// If a name is provided, it is used. Otherwise, if `WAYLAND_DISPLAY` environment /// variable is set, its contents are used as socket name. Otherwise, `wayland-0` is used. /// /// Errors if `name` contains an interior null, or if `XDG_RUNTIME_DIR` is not set, /// or if specified could not be bound (either it is already used or the compositor /// does not have the rights to create it). pub fn add_socket<S>(&mut self, name: Option<S>) -> IoResult<()> where S: AsRef<OsStr>, { self.inner.add_socket(name) } /// Add an automatically named listening socket to this display /// /// Wayland clients will be able to connect to your compositor from this socket. /// /// Socket will be created in the directory specified by the environment variable /// `XDG_RUNTIME_DIR`. The directory is scanned for any name in the form `wayland-$d` with /// `0 <= $d < 32` and the first available one is used. /// /// Errors if `XDG_RUNTIME_DIR` is not set, or all 32 names are already in use. pub fn add_socket_auto(&mut self) -> IoResult<OsString> { self.inner.add_socket_auto() } /// Add existing listening socket to this display /// /// Wayland clients will be able to connect to your compositor from this socket. /// /// The existing socket fd must already be created, opened, and locked. /// The fd must be properly set to CLOEXEC and bound to a socket file /// with both bind() and listen() already called. An error is returned /// otherwise. pub fn add_socket_from<T>(&mut self, socket: T) -> IoResult<()> where T: IntoRawFd, { unsafe { self.add_socket_fd(socket.into_raw_fd()) } } /// Add existing listening socket to this display from a raw file descriptor /// /// Wayland clients will be able to connect to your compositor from this socket. /// /// The library takes ownership of the provided socket if this method returns /// successfully. /// /// # Safety /// /// The existing socket fd must already be created, opened, and locked. /// The fd must be properly set to CLOEXEC and bound to a socket file /// with both bind() and listen() already called. An error is returned /// otherwise. pub unsafe fn add_socket_fd(&mut self, fd: RawFd) -> IoResult<()> { self.inner.add_socket_fd(fd) } /// Create a new client to this display from an already-existing connected Fd /// /// # Safety /// /// The provided file descriptor must be associated to a valid client connection. pub unsafe fn create_client<T: std::any::Any>(&mut self, fd: RawFd, data: &mut T) -> Client { let data = crate::DispatchData::wrap(data); Client::make(self.inner.create_client(fd, data)) } } #[cfg(feature = "use_system_lib")] impl Display { /// Retrieve a pointer from the C lib to this `wl_display` pub fn c_ptr(&self) -> *mut wl_display { self.inner.ptr() } } pub(crate) fn get_runtime_dir() -> IoResult<PathBuf> { match env::var_os("XDG_RUNTIME_DIR") { Some(s) => Ok(s.into()), None => Err(IoError::new(ErrorKind::NotFound, "XDG_RUNTIME_DIR env variable is not set")), } }