1//! Bindings to the client library `libwayland-server.so`
2//!
3//! The generated handle is named `wayland_server_handle()`
4
5#![cfg_attr(rustfmt, rustfmt_skip)]
6
7use super::common::*;
8#[cfg(feature = "server")]
9use libc::{gid_t, pid_t, uid_t};
10#[cfg(feature = "server")]
11use std::os::raw::c_char;
12use std::os::raw::{c_int, c_void};
13#[cfg(all(feature = "server", feature = "dlopen"))]
14use once_cell::sync::Lazy;
15
16pub enum wl_client {}
17pub enum wl_display {}
18pub enum wl_event_loop {}
19pub enum wl_event_source {}
20pub enum wl_global {}
21pub enum wl_resource {}
22pub enum wl_shm_buffer {}
23
24pub type wl_event_loop_fd_func_t = unsafe extern "C" fn(c_int, u32, *mut c_void) -> c_int;
25pub type wl_event_loop_timer_func_t = unsafe extern "C" fn(*mut c_void) -> c_int;
26pub type wl_event_loop_signal_func_t = unsafe extern "C" fn(c_int, *mut c_void) -> c_int;
27pub type wl_event_loop_idle_func_t = unsafe extern "C" fn(*mut c_void) -> ();
28pub type wl_global_bind_func_t = unsafe extern "C" fn(*mut wl_client, *mut c_void, u32, u32) -> ();
29pub type wl_notify_func_t = unsafe extern "C" fn(*mut wl_listener, *mut c_void) -> ();
30pub type wl_resource_destroy_func_t = unsafe extern "C" fn(*mut wl_resource) -> ();
31pub type wl_display_global_filter_func_t =
32 unsafe extern "C" fn(*const wl_client, *const wl_global, *mut c_void) -> bool;
33pub type wl_client_for_each_resource_iterator_func_t =
34 unsafe extern "C" fn(*mut wl_resource, *mut c_void) -> c_int;
35
36#[repr(C)]
37pub struct wl_listener {
38 pub link: wl_list,
39 pub notify: wl_notify_func_t,
40}
41
42#[repr(C)]
43pub struct wl_signal {
44 pub listener_list: wl_list,
45}
46
47#[cfg(feature = "server")]
48external_library!(WaylandServer, "wayland-server",
49 functions:
50 // wl_client
51 fn wl_client_flush(*mut wl_client) -> (),
52 fn wl_client_destroy(*mut wl_client) -> (),
53 fn wl_client_get_display(*mut wl_client) -> *mut wl_display,
54 fn wl_client_get_credentials(*mut wl_client, *mut pid_t, *mut uid_t, *mut gid_t) -> (),
55 fn wl_client_get_object(*mut wl_client, u32) -> *mut wl_resource,
56 fn wl_client_add_destroy_listener(*mut wl_client, *mut wl_listener) -> (),
57 fn wl_client_get_destroy_listener(*mut wl_client, wl_notify_func_t) -> *mut wl_listener,
58 fn wl_client_post_no_memory(*mut wl_client) -> (),
59 fn wl_resource_create(*mut wl_client, *const wl_interface, c_int, u32) -> *mut wl_resource,
60 fn wl_client_get_link(*mut wl_client) -> *mut wl_list,
61 fn wl_client_from_link(*mut wl_list) -> *mut wl_client,
62 fn wl_client_for_each_resource(*mut wl_client, wl_client_for_each_resource_iterator_func_t, *mut c_void) -> (),
63 // wl_display
64 fn wl_client_create(*mut wl_display, c_int) -> *mut wl_client,
65 fn wl_display_create() -> *mut wl_display,
66 fn wl_display_destroy(*mut wl_display) -> (),
67 fn wl_display_destroy_clients(*mut wl_display) -> (),
68 fn wl_display_get_serial(*mut wl_display) -> u32,
69 fn wl_display_next_serial(*mut wl_display) -> u32,
70 fn wl_display_add_socket(*mut wl_display, *const c_char) -> c_int,
71 fn wl_display_add_socket_auto(*mut wl_display) -> *const c_char,
72 fn wl_display_add_socket_fd(*mut wl_display, c_int) -> c_int,
73 fn wl_display_add_shm_format(*mut wl_display, u32) -> *mut u32,
74 fn wl_display_get_event_loop(*mut wl_display) -> *mut wl_event_loop,
75 fn wl_display_terminate(*mut wl_display) -> (),
76 fn wl_display_run(*mut wl_display) -> (),
77 fn wl_display_flush_clients(*mut wl_display) -> (),
78 fn wl_display_add_destroy_listener(*mut wl_display, *mut wl_listener) -> (),
79 fn wl_display_get_destroy_listener(*mut wl_display, wl_notify_func_t) -> *mut wl_listener,
80 fn wl_global_create(*mut wl_display, *const wl_interface, c_int, *mut c_void, wl_global_bind_func_t) -> *mut wl_global,
81 fn wl_display_init_shm(*mut wl_display) -> c_int,
82 fn wl_display_add_client_created_listener(*mut wl_display, *mut wl_listener) -> (),
83 fn wl_display_set_global_filter(*mut wl_display, wl_display_global_filter_func_t, *mut c_void) -> (),
84 fn wl_display_get_client_list(*mut wl_display) -> *mut wl_list,
85 // wl_event_loop
86 fn wl_event_loop_create() -> *mut wl_event_loop,
87 fn wl_event_loop_destroy(*mut wl_event_loop) -> (),
88 fn wl_event_loop_add_fd(*mut wl_event_loop, c_int, u32, wl_event_loop_fd_func_t, *mut c_void) -> *mut wl_event_source,
89 fn wl_event_loop_add_timer(*mut wl_event_loop, wl_event_loop_timer_func_t, *mut c_void) -> *mut wl_event_source,
90 fn wl_event_loop_add_signal(*mut wl_event_loop, c_int, wl_event_loop_signal_func_t, *mut c_void) -> *mut wl_event_source,
91 fn wl_event_loop_dispatch(*mut wl_event_loop, c_int) -> c_int,
92 fn wl_event_loop_dispatch_idle(*mut wl_event_loop) -> (),
93 fn wl_event_loop_add_idle(*mut wl_event_loop, wl_event_loop_idle_func_t, *mut c_void) -> *mut wl_event_source,
94 fn wl_event_loop_get_fd(*mut wl_event_loop) -> c_int,
95 fn wl_event_loop_add_destroy_listener(*mut wl_event_loop, *mut wl_listener) -> (),
96 fn wl_event_loop_get_destroy_listener(*mut wl_event_loop, wl_notify_func_t) -> *mut wl_listener,
97 // wl_event_source
98 fn wl_event_source_fd_update(*mut wl_event_source, u32) -> c_int,
99 fn wl_event_source_timer_update(*mut wl_event_source, c_int) -> c_int,
100 fn wl_event_source_remove(*mut wl_event_source) -> c_int,
101 fn wl_event_source_check(*mut wl_event_source) -> (),
102 // wl_global
103 fn wl_global_remove(*mut wl_global) -> (),
104 fn wl_global_destroy(*mut wl_global) -> (),
105 fn wl_global_get_user_data(*const wl_global) -> *mut c_void,
106 // wl_resource
107 fn wl_resource_post_event_array(*mut wl_resource, u32, *mut wl_argument) -> (),
108 fn wl_resource_queue_event_array(*mut wl_resource, u32, *mut wl_argument) -> (),
109 fn wl_resource_post_no_memory(*mut wl_resource) -> (),
110 fn wl_resource_set_implementation(*mut wl_resource, *const c_void, *mut c_void, Option<wl_resource_destroy_func_t>) -> (),
111 fn wl_resource_set_dispatcher(*mut wl_resource, wl_dispatcher_func_t, *const c_void, *mut c_void, Option<wl_resource_destroy_func_t>) -> (),
112 fn wl_resource_destroy(*mut wl_resource) -> (),
113 fn wl_resource_get_client(*mut wl_resource) -> *mut wl_client,
114 fn wl_resource_get_id(*mut wl_resource) -> u32,
115 fn wl_resource_get_link(*mut wl_resource) -> *mut wl_list,
116 fn wl_resource_from_link(*mut wl_list) -> *mut wl_resource,
117 fn wl_resource_find_for_client(*mut wl_list, *mut wl_client) -> (),
118 fn wl_resource_set_user_data(*mut wl_resource, *mut c_void) -> (),
119 fn wl_resource_get_user_data(*mut wl_resource) -> *mut c_void,
120 fn wl_resource_get_version(*mut wl_resource) -> c_int,
121 fn wl_resource_get_class(*mut wl_resource) -> *const c_char,
122 fn wl_resource_set_destructor(*mut wl_resource, Option<wl_resource_destroy_func_t>) -> (),
123 fn wl_resource_instance_of(*mut wl_resource, *const wl_interface, *const c_void) -> c_int,
124 fn wl_resource_add_destroy_listener(*mut wl_resource, *mut wl_listener) -> (),
125 fn wl_resource_get_destroy_listener(*mut wl_resource, wl_notify_func_t) -> *mut wl_listener,
126 // wl_shm
127 fn wl_shm_buffer_begin_access(*mut wl_shm_buffer) -> (),
128 fn wl_shm_buffer_end_access(*mut wl_shm_buffer) -> (),
129 fn wl_shm_buffer_get(*mut wl_resource) -> *mut wl_shm_buffer,
130 fn wl_shm_buffer_get_data(*mut wl_shm_buffer) -> *mut c_void,
131 fn wl_shm_buffer_get_stride(*mut wl_shm_buffer) -> i32,
132 fn wl_shm_buffer_get_format(*mut wl_shm_buffer) -> u32,
133 fn wl_shm_buffer_get_width(*mut wl_shm_buffer) -> i32,
134 fn wl_shm_buffer_get_height(*mut wl_shm_buffer) -> i32,
135 // wl_log
136 fn wl_log_set_handler_server(wl_log_func_t) -> (),
137 // wl_list
138 fn wl_list_init(*mut wl_list) -> (),
139 fn wl_list_insert(*mut wl_list, *mut wl_list) -> (),
140 fn wl_list_remove(*mut wl_list) -> (),
141 fn wl_list_length(*const wl_list) -> c_int,
142 fn wl_list_empty(*const wl_list) -> c_int,
143 fn wl_list_insert_list(*mut wl_list,*mut wl_list) -> (),
144
145 // arrays
146 fn wl_array_init(*mut wl_array) -> (),
147 fn wl_array_release(*mut wl_array) -> (),
148 fn wl_array_add(*mut wl_array,usize) -> (),
149 fn wl_array_copy(*mut wl_array, *mut wl_array) -> (),
150 varargs:
151 fn wl_resource_post_event(*mut wl_resource, u32) -> (),
152 fn wl_resource_queue_event(*mut wl_resource, u32) -> (),
153 fn wl_resource_post_error(*mut wl_resource, u32, *const c_char) -> (),
154);
155
156#[cfg(all(feature = "server", feature = "dlopen"))]
157pub fn wayland_server_option() -> Option<&'static WaylandServer> {
158 static WAYLAND_SERVER_OPTION: Lazy<Option<WaylandServer>> = Lazy::new(||{
159 let versions = ["libwayland-server.so.0", "libwayland-server.so"];
160 for ver in &versions {
161 match unsafe { WaylandServer::open(ver) } {
162 Ok(h) => return Some(h),
163 Err(::dlib::DlError::CantOpen(_)) => continue,
164 Err(::dlib::DlError::MissingSymbol(s)) => {
165 log::error!("Found library {} cannot be used: symbol {} is missing.", ver, s);
166 return None;
167 }
168 }
169 }
170 None
171 });
172
173 WAYLAND_SERVER_OPTION.as_ref()
174}
175
176#[cfg(all(feature = "server", feature = "dlopen"))]
177pub fn wayland_server_handle() -> &'static WaylandServer {
178 static WAYLAND_SERVER_HANDLE: Lazy<&'static WaylandServer> = Lazy::new(|| wayland_server_option().expect("Library libwayland-server.so could not be loaded."));
179
180 &WAYLAND_SERVER_HANDLE
181}
182
183#[cfg(all(feature = "server", not(feature = "dlopen")))]
184pub fn is_lib_available() -> bool {
185 true
186}
187#[cfg(all(feature = "server", feature = "dlopen"))]
188pub fn is_lib_available() -> bool {
189 wayland_server_option().is_some()
190}
191
192#[cfg(feature = "server")]
193pub mod signal {
194 #![allow(clippy::cast_ptr_alignment, clippy::missing_safety_doc)]
195 #[cfg(feature = "dlopen")]
196 use super::wayland_server_handle as wsh;
197 #[cfg(not(feature = "dlopen"))]
198 use super::{wl_list_init, wl_list_insert};
199 use super::{wl_listener, wl_notify_func_t, wl_signal};
200 use crate::common::wl_list;
201 use std::os::raw::c_void;
202 use std::ptr;
203
204 macro_rules! container_of(
205 ($ptr: expr, $container: ident, $field: ident) => {
206 ($ptr as *mut u8).sub(memoffset::offset_of!($container, $field)) as *mut $container
207 }
208 );
209
210 macro_rules! list_for_each(
211 ($pos: ident, $head:expr, $container: ident, $field: ident, $action: block) => {
212 let mut $pos = container_of!((*$head).next, $container, $field);
213 while &mut (*$pos).$field as *mut _ != $head {
214 $action;
215 $pos = container_of!((*$pos).$field.next, $container, $field);
216 }
217 }
218 );
219
220 macro_rules! list_for_each_safe(
221 ($pos: ident, $head: expr, $container: ident, $field: ident, $action: block) => {
222 let mut $pos = container_of!((*$head).next, $container, $field);
223 let mut tmp = container_of!((*$pos).$field.next, $container, $field);
224 while &mut (*$pos).$field as *mut _ != $head {
225 $action;
226 $pos = tmp;
227 tmp = container_of!((*$pos).$field.next, $container, $field);
228 }
229 }
230 );
231
232 pub unsafe fn wl_signal_init(signal: *mut wl_signal) {
233 // Safety: signal is a valid initialized wl_signal
234 ffi_dispatch!(wsh(), wl_list_init, unsafe { &mut (*signal).listener_list });
235 }
236
237 pub unsafe fn wl_signal_add(signal: *mut wl_signal, listener: *mut wl_listener) {
238 // Safety: signal and listener are valid pointers
239 ffi_dispatch!(
240 wsh(),
241 wl_list_insert,
242 unsafe { (*signal).listener_list.prev },
243 unsafe { &mut (*listener).link }
244 )
245 }
246
247 pub unsafe fn wl_signal_get(
248 signal: *mut wl_signal,
249 notify: wl_notify_func_t,
250 ) -> *mut wl_listener {
251 // Safety: the signal pointer is valid
252 unsafe {
253 list_for_each!(l, &mut (*signal).listener_list as *mut wl_list, wl_listener, link, {
254 if (*l).notify == notify {
255 return l;
256 }
257 });
258 }
259
260 ptr::null_mut()
261 }
262
263 pub unsafe fn wl_signal_emit(signal: *mut wl_signal, data: *mut c_void) {
264 // Safety: the signal pointer is valid
265 unsafe {
266 list_for_each_safe!(l, &mut (*signal).listener_list as *mut wl_list, wl_listener, link, {
267 ((*l).notify)(l, data);
268 });
269 }
270 }
271
272 #[repr(C)]
273 struct ListenerWithUserData {
274 listener: wl_listener,
275 user_data: *mut c_void,
276 }
277
278 pub fn rust_listener_create(notify: wl_notify_func_t) -> *mut wl_listener {
279 let data = Box::into_raw(Box::new(ListenerWithUserData {
280 listener: wl_listener {
281 link: wl_list { prev: ptr::null_mut(), next: ptr::null_mut() },
282 notify,
283 },
284 user_data: ptr::null_mut(),
285 }));
286
287 // SAFETY: data is a valid pointer for `ListenerWithUserData`.
288 unsafe { std::ptr::addr_of_mut!((*data).listener) }
289 }
290
291 pub unsafe fn rust_listener_get_user_data(listener: *mut wl_listener) -> *mut c_void {
292 // Safety: listener is a valid pointer to a listener created by rust_listener_create
293 let data = unsafe { container_of!(listener, ListenerWithUserData, listener) };
294 unsafe { (*data).user_data }
295 }
296
297 pub unsafe fn rust_listener_set_user_data(listener: *mut wl_listener, user_data: *mut c_void) {
298 // Safety: listener is a valid pointer to a listener created by rust_listener_create
299 let data = unsafe { container_of!(listener, ListenerWithUserData, listener) };
300 unsafe { (*data).user_data = user_data }
301 }
302
303 pub unsafe fn rust_listener_destroy(listener: *mut wl_listener) {
304 // Safety: listener is a valid pointer to a listener created by rust_listener_create
305 let data = unsafe { container_of!(listener, ListenerWithUserData, listener) };
306 let _ = unsafe { Box::from_raw(data) };
307 }
308}
309