wl-client 0.2.0

Safe client-side libwayland wrapper
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use {
    crate::ffi::{
        wl_argument, wl_dispatcher_func_t, wl_display, wl_event_queue, wl_interface, wl_proxy,
    },
    libloading::Library,
    parking_lot::Mutex,
    std::{
        ffi::{c_char, c_int, c_void},
        io, mem,
        sync::LazyLock,
    },
};

/// A reference to the `libwayland-client.so` dynamic library.
///
/// You can obtain a reference by calling [`Self::open`].
pub struct Libwayland {
    syms: Symbols,
    syms_opt: SymbolsOpt,
}

/// # Safety
///
/// The types and names must match the libwayland ABI.
macro_rules! unsafe_symbols {
    (
        $symbols:ident;
        $(
            $name:ident: $ty:ty,
        )*
    ) => {
        struct $symbols {
            $($name: $ty,)*
        }

        impl $symbols {
            /// # Safety
            ///
            /// `lib` must be libwayland-client.so.
            unsafe fn load(lib: &Library) -> Result<Self, libloading::Error> {
                macro_rules! get {
                    ($v:ident: Option<$ty2:ty>) => {
                        $v.ok().map(|v| *v)
                    };
                    ($v:ident: $ty2:ty) => {
                        *$v?
                    };
                }
                Ok(Self {
                    $(
                        $name: {
                            // SAFETY: By the requirements of this macro, $name has the
                            //         requested type.
                            let v = unsafe {
                                lib.get(concat!(stringify!($name), "\0").as_bytes())
                            };
                            get!(v: $ty)
                        },
                    )*
                })
            }
        }
    };
}

/// # Safety
///
/// - The types and names must match the libwayland API.
/// - Each function must document the safety requirements of the libwayland function.
macro_rules! unsafe_fwd {
    (
        $(
            $(
                #[$attr:meta]
            )*
            fn $name:ident($($arg:ident: $ty:ty),+$(,)?) $(-> $ret:ty)?;
        )*
    ) => {
        // SAFETY: The requirement is forwarded to the caller.
        unsafe_symbols! {
            Symbols;
            $(
                $name: unsafe extern "C" fn($($arg: $ty,)*) $(-> $ret)?,
            )*
        }
        impl Libwayland {
            $(
                $(
                    #[$attr]
                )*
                #[inline(always)]
                pub(crate) unsafe fn $name(&self $(, $arg: $ty)*) $(-> $ret)? {
                    // SAFETY: - $name matches the API of libwayland
                    //         - All safety requirements of the libwayland function are
                    //           forwarded to the caller of this function.
                    unsafe {
                        (self.syms.$name)($($arg,)*)
                    }
                }
            )*
        }
    };
}

// SAFETY: There functions are as described in wayland-client-core.h.
unsafe_symbols! {
    SymbolsOpt;
    wl_display_create_queue_with_name: Option<
        unsafe extern "C" fn(display: *mut wl_display, name: *const c_char) -> *mut wl_event_queue,
    >,
    wl_proxy_get_queue: Option<unsafe extern "C" fn(proxy: *mut wl_proxy) -> *mut wl_event_queue>,
    wl_proxy_get_display: Option<unsafe extern "C" fn(proxy: *mut wl_proxy) -> *mut wl_display>,
}

// SAFETY: - There functions are as described in wayland-client-core.h.
//         - The safety requirements are documented as far as I know them.
unsafe_fwd! {
    /// Destroys a queue.
    ///
    /// # Safety
    ///
    /// - queue must be a valid pointer
    /// - all attached proxies must leak
    fn wl_event_queue_destroy(queue: *mut wl_event_queue);

    /// Sends a request.
    ///
    /// If flags contains WL_MARSHAL_FLAG_DESTROY, then this function destroys the proxy.
    ///
    /// If interface is not null, then this function returns a new non-wrapper proxy with
    /// that interface.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer
    /// - opcode must be a valid request opcode for the interface of the proxy
    /// - args must be a pointer to an array of wl_arguments
    /// - the array must conform to the interface + opcode of the proxy
    /// - if interface is not null, it must be a valid pointer to a valid interface definition
    /// - if interface is not null, then args must contain exactly on new_id element
    /// - if interface is null, then args must not contain any new_id element
    /// - flags must be 0 or WL_MARSHAL_FLAG_DESTROY
    /// - if flags contains WL_MARSHAL_FLAG_DESTROY, then proxy must not be a wrapper
    fn wl_proxy_marshal_array_flags(
        proxy: *mut wl_proxy,
        opcode: u32,
        interface: *const wl_interface,
        version: u32,
        flags: u32,
        args: *mut wl_argument,
    ) -> *mut wl_proxy;

    /// Creates a new wrapper proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer.
    fn wl_proxy_create_wrapper(proxy: *mut c_void) -> *mut c_void;

    /// Destroys a wrapper proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer to a wrapper
    fn wl_proxy_wrapper_destroy(proxy: *mut c_void);

    /// Destroys a non-wrapper proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer to a wrapper
    fn wl_proxy_destroy(proxy: *mut wl_proxy);

    /// Sets the dispatcher of the proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer to a non-wrapper
    /// - this modifies the unprotected, mutable fields of the wl_proxy and access must be
    ///   externally synchronized
    /// - the caller must ensure that the safety requirements of the dispatcher_func are
    ///   satisfied whenever it is called
    fn wl_proxy_add_dispatcher(
        proxy: *mut wl_proxy,
        dispatcher_func: Option<wl_dispatcher_func_t>,
        dispatcher_data: *const c_void,
        data: *mut c_void,
    );

    /// Returns the ID of the proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer
    fn wl_proxy_get_id(proxy: *mut wl_proxy) -> u32;

    /// Returns the version of the proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer
    fn wl_proxy_get_version(proxy: *mut wl_proxy) -> u32;

    /// Sets the queue of the proxy.
    ///
    /// # Safety
    ///
    /// - proxy must be a valid pointer
    /// - queue must be a valid pointer
    /// - the queue must not be destroyed while it has proxies attached
    /// - the queue and the proxy must belong to the same wl_display
    fn wl_proxy_set_queue(proxy: *mut wl_proxy, queue: *mut wl_event_queue);

    /// Connects to the wayland socket from the environment.
    ///
    /// # Safety
    ///
    /// - name must be null or a c string
    fn wl_display_connect(name: *const c_char) -> *mut wl_display;

    /// Connects to an existing file descriptor.
    ///
    /// # Safety
    ///
    /// - `fd` must be a valid file descriptor.
    /// - This function takes ownership of the file descriptor.
    fn wl_display_connect_to_fd(fd: c_int) -> *mut wl_display;

    /// Disconnects the display.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    /// - all queues and proxies must have been destroyed or must leak
    fn wl_display_disconnect(display: *mut wl_display);

    /// Dispatches a queue.
    ///
    /// # Safety
    ///
    /// - display and queue must be valid pointers
    /// - the queue must belong to the display
    /// - this accesses the unprotected, mutable fields of all proxies that were attached
    ///   to the queue before the start of the previous queue dispatch.
    ///   access must be externally synchronized.
    fn wl_display_dispatch_queue_pending(
        display: *mut wl_display,
        queue: *mut wl_event_queue,
    ) -> c_int;

    /// Flushes the display.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    fn wl_display_flush(display: *mut wl_display) -> c_int;

    /// Creates a ticket to read from the display fd.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    /// - queue must be a valid pointer
    /// - the queue must belong to the display
    fn wl_display_prepare_read_queue(
        display: *mut wl_display,
        queue: *mut wl_event_queue,
    ) -> c_int;

    /// Creates a ticket to read from the display fd.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    fn wl_display_prepare_read(
        display: *mut wl_display,
    ) -> c_int;

    /// Consumes a ticket to read from the display fd without reading from the display fd.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    /// - the caller must own a ticket
    fn wl_display_cancel_read(display: *mut wl_display);

    /// Consumes a ticket to read from the display fd and reads from the display fd.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    /// - the caller must own a ticket
    fn wl_display_read_events(display: *mut wl_display) -> c_int;

    /// Returns the file descriptor of the display.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    fn wl_display_get_fd(display: *mut wl_display) -> c_int;

    /// Creates a new queue.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    fn wl_display_create_queue(display: *mut wl_display) -> *mut wl_event_queue;

    /// Returns the errno of the last error that occurred.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    fn wl_display_get_error(display: *mut wl_display) -> c_int;
}

impl Libwayland {
    /// Obtains a reference to `libwayland-client.so`.
    #[inline]
    pub fn open() -> io::Result<&'static Self> {
        static LIB: LazyLock<Result<Libwayland, Mutex<Option<libloading::Error>>>> =
            LazyLock::new(|| Libwayland::open_new().map_err(|e| Mutex::new(Some(e))));
        #[cold]
        fn map_error(e: &Mutex<Option<libloading::Error>>) -> io::Error {
            match e.lock().take() {
                Some(e) => io::Error::new(io::ErrorKind::NotFound, e),
                None => io::Error::from(io::ErrorKind::NotFound),
            }
        }
        match &*LIB {
            Ok(l) => Ok(l),
            Err(e) => Err(map_error(e)),
        }
    }

    #[cold]
    fn open_new() -> Result<Self, libloading::Error> {
        // SAFETY: No way to verify this. We just have to hope that libwayland-client.so
        //         is the libwayland-client.so that we've bound against.
        let lib = unsafe { Library::new("libwayland-client.so.0")? };
        // SAFETY: lib is libwayland-client.so.
        let syms = unsafe { Symbols::load(&lib)? };
        // SAFETY: lib is libwayland-client.so.
        let syms_opt = unsafe { SymbolsOpt::load(&lib)? };
        mem::forget(lib);
        Ok(Libwayland { syms, syms_opt })
    }

    /// Creates a new queue.
    ///
    /// # Safety
    ///
    /// - display must be a valid pointer
    /// - name must be null or a valid c string
    pub(crate) unsafe fn wl_display_create_queue_with_name(
        &self,
        display: *mut wl_display,
        name: *const c_char,
    ) -> *mut wl_event_queue {
        if let Some(f) = self.syms_opt.wl_display_create_queue_with_name {
            // SAFETY: The requirements are forwarded to the caller of this function.
            unsafe { f(display, name) }
        } else {
            // SAFETY: The requirements are forwarded to the caller of this function.
            unsafe { self.wl_display_create_queue(display) }
        }
    }
}

mod polyfills {
    use {
        crate::{
            Libwayland,
            ffi::{wl_display, wl_event_queue, wl_interface, wl_proxy},
        },
        std::ffi::c_void,
    };

    #[repr(C)]
    struct real_wl_object {
        _interface: *const wl_interface,
        _implementation: *const c_void,
        _id: u32,
    }

    #[repr(C)]
    struct real_wl_proxy {
        _object: real_wl_object,
        display: *mut wl_display,
        queue: *mut wl_event_queue,
    }

    impl Libwayland {
        /// Get the queue of a proxy.
        ///
        /// # Safety
        ///
        /// - proxy must be a valid pointer
        #[inline]
        pub(crate) unsafe fn wl_proxy_get_queue(
            &self,
            proxy: *mut wl_proxy,
        ) -> *mut wl_event_queue {
            if let Some(f) = self.syms_opt.wl_proxy_get_queue {
                // SAFETY: The requirements are forwarded to the caller of this function.
                return unsafe { f(proxy) };
            }
            let proxy = proxy.cast::<real_wl_proxy>();
            // SAFETY: We have a hard dependency on wl_proxy_marshal_array_flags which was
            //         added in 2021. wl_proxy_get_queue was added in 2023. Between these
            //         two dates, the layout of wl_proxy has always been as described above.
            //         (Modulo trailing fields after the queue field.)
            // NOTE: We cannot use this code for all versions of libwayland since the layout
            //       might change in the future.
            unsafe { (*proxy).queue }
        }

        /// Get the display of a proxy.
        ///
        /// # Safety
        ///
        /// - proxy must be a valid pointer
        #[inline]
        pub(crate) unsafe fn wl_proxy_get_display(&self, proxy: *mut wl_proxy) -> *mut wl_display {
            if let Some(f) = self.syms_opt.wl_proxy_get_display {
                // SAFETY: The requirements are forwarded to the caller of this function.
                return unsafe { f(proxy) };
            }
            let proxy = proxy.cast::<real_wl_proxy>();
            // SAFETY: We have a hard dependency on wl_proxy_marshal_array_flags which was
            //         added in 2021. wl_proxy_get_display was added in 2023. Between these
            //         two dates, the layout of wl_proxy has always been as described above.
            //         (Modulo trailing fields after the queue field.)
            // NOTE: We cannot use this code for all versions of libwayland since the layout
            //       might change in the future.
            unsafe { (*proxy).display }
        }
    }
}