wl_client/ffi.rs
1//! libwayland FFI interface.
2//!
3//! This module contains the type definitions necessary to interact with libwayland.
4
5#![expect(non_camel_case_types)]
6
7use isnt::std_1::primitive::IsntConstPtrExt;
8use std::ffi::CStr;
9use std::ffi::c_char;
10use std::ffi::c_int;
11use std::ffi::c_void;
12use std::ptr;
13
14#[cfg(test)]
15mod tests;
16
17/// The libwayland `wl_message` type.
18#[repr(C)]
19pub struct wl_message {
20 pub name: *const c_char,
21 pub signature: *const c_char,
22 pub types: *const *const wl_interface,
23}
24
25// SAFETY: pointers not implementing Sync is only a lint
26unsafe impl Sync for wl_message {}
27// SAFETY: pointers not implementing Send is only a lint
28unsafe impl Send for wl_message {}
29
30/// The libwayland `wl_interface` type.
31#[repr(C)]
32pub struct wl_interface {
33 pub name: *const c_char,
34 pub version: c_int,
35 pub method_count: c_int,
36 pub methods: *const wl_message,
37 pub event_count: c_int,
38 pub events: *const wl_message,
39}
40
41// SAFETY: pointers not implementing Send is only a lint
42unsafe impl Send for wl_interface {}
43// SAFETY: pointers not implementing Sync is only a lint
44unsafe impl Sync for wl_interface {}
45
46/// The libwayland `wl_array` type.
47#[repr(C)]
48pub struct wl_array {
49 pub size: usize,
50 pub alloc: usize,
51 pub data: *mut c_void,
52}
53
54// SAFETY: pointers not implementing Send is only a lint
55unsafe impl Send for wl_array {}
56// SAFETY: pointers not implementing Sync is only a lint
57unsafe impl Sync for wl_array {}
58
59/// The libwayland `wl_fixed_t` type.
60pub type wl_fixed_t = i32;
61
62pub(crate) type wl_dispatcher_func_t = unsafe extern "C" fn(
63 user_data: *const c_void,
64 target: *mut c_void,
65 opcode: u32,
66 msg: *const wl_message,
67 args: *mut wl_argument,
68) -> c_int;
69
70/// The libwayland `wl_object` type.
71///
72/// This is always a `wl_proxy` in the context of this crate.
73#[repr(C)]
74pub struct wl_object(());
75
76/// The libwayland `wl_argument` type.
77#[derive(Copy, Clone)]
78#[repr(C)]
79pub union wl_argument {
80 pub i: i32,
81 pub u: u32,
82 pub f: wl_fixed_t,
83 pub s: *const c_char,
84 pub o: *mut wl_object,
85 pub n: u32,
86 pub a: *mut wl_array,
87 pub h: i32,
88}
89
90// SAFETY: pointers not implementing Send is only a lint
91unsafe impl Send for wl_argument {}
92// SAFETY: pointers not implementing Sync is only a lint
93unsafe impl Sync for wl_argument {}
94
95/// The libwayland `wl_display` type.
96///
97/// This type morally has the following fields:
98///
99/// - `mutex: a mutex`
100#[repr(C)]
101pub struct wl_display(());
102
103/// The libwayland `wl_proxy` type.
104///
105/// This type morally has the following fields:
106///
107/// - `interface: *const wl_interface`
108/// - `display: *mut wl_display`,
109/// - `queue: *mut wl_event_queue`,
110/// - `user_data: *mut c_void`,
111/// - `dispatcher: *mut c_void`,
112/// - `dispatcher_data: *mut c_void`,
113///
114/// The `interface` and `display` fields are immutable.
115///
116/// The `queue` field is mutable and access is protected by the `wl_display.mutex`.
117///
118/// The `user_data`, `dispatcher`, and `dispatcher_data` fields are mutable and access is
119/// not synchronized.
120#[repr(C)]
121pub struct wl_proxy(());
122
123/// The libwayland `wl_event_display` type.
124///
125/// This type morally has the following fields:
126///
127/// - `events: Vec<..>`
128///
129/// where each event is an event that has been read from the socket but which has not yet
130/// been dispatched.
131///
132/// Access to `events` is protected by the `wl_display.mutex`.
133///
134/// The mutex is dropped before an event is dispatched.
135#[repr(C)]
136pub struct wl_event_queue(());
137
138/// Atomically destroys a proxy while sending a request.
139///
140/// If this is passed into wl_proxy_marshal_array_flags, the message is sent and the proxy
141/// is destroyed without dropping the display mutex.
142///
143/// This is required when sending a destructor message for a server-created object. Since
144/// otherwise the server might send a new object with the same ID before we have a chance
145/// to call wl_proxy_destroy. This would cause a fatal error in libwayland.
146pub(crate) const WL_MARSHAL_FLAG_DESTROY: u32 = 1 << 0;
147
148/// # Safety
149///
150/// `l` and `r` must be valid `wl_interface` definitions.
151pub(crate) unsafe fn interface_compatible(l: &wl_interface, r: &wl_interface) -> bool {
152 if ptr::eq(l, r) {
153 return true;
154 }
155 macro_rules! cmp_sig {
156 ($count:ident, $messages:ident) => {
157 if l.$count != r.$count {
158 return false;
159 }
160 for i in 0..l.$count as usize {
161 // SAFETY: This function requires that l and r are valid interface definitions.
162 let l_msg = unsafe { &*l.$messages.add(i) };
163 // SAFETY: Dito
164 let r_msg = unsafe { &*r.$messages.add(i) };
165 // SAFETY: Dito
166 let l_sig = unsafe { CStr::from_ptr(l_msg.signature) };
167 // SAFETY: Dito
168 let r_sig = unsafe { CStr::from_ptr(r_msg.signature) };
169 if l_sig != r_sig {
170 return false;
171 }
172 }
173 };
174 }
175 cmp_sig!(method_count, methods);
176 cmp_sig!(event_count, events);
177 for i in 0..l.event_count as usize {
178 // SAFETY: This function requires that l and r are valid interface definitions.
179 let l_msg = unsafe { &*l.events.add(i) };
180 // SAFETY: Dito
181 let r_msg = unsafe { &*r.events.add(i) };
182 // SAFETY: Dito
183 let sig = unsafe { CStr::from_ptr(l_msg.signature) };
184 let mut idx = 0;
185 for &b in sig.to_bytes() {
186 if b == b'?' {
187 continue;
188 }
189 if b == b'o' || b == b'n' {
190 // SAFETY: Dito
191 let l_if = unsafe { *l_msg.types.add(idx as usize) };
192 // SAFETY: Dito
193 let r_if = unsafe { *r_msg.types.add(idx as usize) };
194 if l_if.is_null() != r_if.is_null() {
195 return false;
196 }
197 if l_if.is_not_null() {
198 // SAFETY: Dito
199 let l_if = unsafe { &*l_if };
200 // SAFETY: Dito
201 let r_if = unsafe { &*r_if };
202 // SAFETY: Dito
203 if unsafe { !interface_compatible(l_if, r_if) } {
204 return false;
205 }
206 }
207 }
208 idx += 1;
209 }
210 }
211 true
212}