wlcs 0.1.0

Bindings/helpers for WLCS (Wayland Conformance Test Suite)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//!
//! Wrappers/helpers for setting up WLCS server integration
//!

use std::{
    ffi::{c_char, c_int},
    os::fd::IntoRawFd,
};

use container_of::container_of;
use wayland_sys::{
    client::{wl_display, wl_proxy},
    common::wl_fixed_t,
    server::wl_event_loop,
};

use crate::{
    ffi_display_server_api::{WlcsDisplayServer, WlcsIntegrationDescriptor, WlcsServerIntegration},
    ffi_pointer_api::WlcsPointer,
    ffi_touch_api::WlcsTouch,
    Pointer, Touch, Wlcs,
};

struct DisplayServerHandle<W: Wlcs> {
    wlcs_display_server: WlcsDisplayServer,
    wlcs: W,
}

struct PointerHandle<W: Wlcs> {
    wlcs_pointer: WlcsPointer,
    p: W::Pointer,
}

struct TouchHandle<W: Wlcs> {
    wlcs_touch: WlcsTouch,
    t: W::Touch,
}

/// Helper function for getting a [`DisplayServerHandle`] from a [`WlcsDisplayServer`] pointer.
///
/// # Safety
///
/// - The pointer must be valid
/// - The library must have initialized the pointer as an instance of a [`DisplayServerHandle`].
/// - The caller has picked a suitable lifetime to ensure the returned mutable reference is not held when
///   control is returned to wlcs
unsafe fn get_display_server_handle_mut<'a, W: Wlcs>(
    ptr: *mut WlcsDisplayServer,
) -> &'a mut DisplayServerHandle<W> {
    unsafe { &mut *container_of!(ptr, DisplayServerHandle<W>, wlcs_display_server) }
}

/// Helper function for getting a [`DisplayServerHandle`] from a [`WlcsDisplayServer`] pointer.
///
/// # Safety
///
/// - The pointer must be valid
/// - The library must have initialized the pointer as an instance of a [`DisplayServerHandle`].
/// - The caller has picked a suitable lifetime to ensure the returned reference is not held when control is
///   returned to wlcs
unsafe fn get_display_server_handle_ref<'a, W: Wlcs>(
    ptr: *const WlcsDisplayServer,
) -> &'a DisplayServerHandle<W> {
    unsafe { &*container_of!(ptr, DisplayServerHandle<W>, wlcs_display_server) }
}

unsafe fn get_pointer_handle<'a, W: Wlcs>(ptr: *mut WlcsPointer) -> &'a mut PointerHandle<W> {
    unsafe { &mut *container_of!(ptr, PointerHandle<W>, wlcs_pointer) }
}

unsafe fn get_touch_handle<'a, W: Wlcs>(ptr: *mut WlcsTouch) -> &'a mut TouchHandle<W> {
    unsafe { &mut *container_of!(ptr, TouchHandle<W>, wlcs_touch) }
}

#[allow(unused)]
unsafe extern "C" fn create_server_ffi<W: Wlcs>(
    _argc: c_int,
    _argv: *mut *const c_char,
) -> *mut WlcsDisplayServer {
    // block the SIGPIPE signal here, we are a cdynlib so Rust does not do it for us
    match std::panic::catch_unwind(|| {
        use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet, Signal};

        unsafe {
            sigaction(
                Signal::SIGPIPE,
                &SigAction::new(SigHandler::SigIgn, SaFlags::empty(), SigSet::empty()),
            )
            .unwrap();
        }

        let wlcs = W::new();
        let dsh = Box::new(DisplayServerHandle {
            wlcs_display_server: wlcs_display_server::<W>(),
            wlcs,
        });
        let handle = Box::into_raw(dsh);
        std::ptr::addr_of_mut!((*handle).wlcs_display_server)
    }) {
        Ok(ptr) => ptr,
        Err(err) => {
            println!(
                "panic in create_server_ffi on ptr: {:p} (type {:?})",
                err.as_ref() as *const _,
                err.type_id()
            );
            std::ptr::null_mut()
        }
    }
}

#[allow(unused)]
unsafe extern "C" fn destroy_server_ffi<W: Wlcs>(ptr: *mut WlcsDisplayServer) {
    if let Err(err) = std::panic::catch_unwind(|| {
        // SAFETY:
        // - wlcs will no longer use the WlcsDisplayServer pointer. This ensures we take back ownership of the
        //   allocation.
        // - The DisplayServerHandle was created using Box::from_raw, ensuring the memory layout is correct.
        let _server = unsafe {
            Box::from_raw(container_of!(
                ptr,
                DisplayServerHandle::<W>,
                wlcs_display_server
            ))
        };
        assert_eq!(_server.wlcs_display_server.version, 3);
    }) {
        println!(
            "panic in destroy_server_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

#[allow(unused)]
unsafe extern "C" fn start_server_ffi<W: Wlcs>(ptr: *mut WlcsDisplayServer) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        server.wlcs.start()
    }) {
        println!(
            "panic in start_server_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

#[allow(unused)]
unsafe extern "C" fn stop_server_ffi<W: Wlcs>(ptr: *mut WlcsDisplayServer) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        server.wlcs.stop();
    }) {
        println!(
            "panic in stop_server_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

#[allow(unused)]
unsafe extern "C" fn create_client_socket_ffi<W: Wlcs>(ptr: *mut WlcsDisplayServer) -> c_int {
    match std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        server.wlcs.create_client_socket()
    }) {
        // WLCS takes ownership of the file descriptor for the client socket.
        Ok(client) => client.map_or(-1, |c| c.into_raw_fd()),
        Err(err) => {
            println!(
                "panic in wlcs_display_server::create_client_socket_ffi on ptr: {:p} (type {:?})",
                err.as_ref() as *const _,
                err.type_id()
            );
            -1
        }
    }
}

unsafe extern "C" fn position_window_absolute_ffi<W: Wlcs>(
    ptr: *mut WlcsDisplayServer,
    display: *mut wl_display,
    surface: *mut wl_proxy,
    x: c_int,
    y: c_int,
) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        server.wlcs.position_window_absolute(display, surface, x, y);
    }) {
        println!(
            "panic in wlcs_display_server::position_window_absolute_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

#[allow(unused)]
unsafe extern "C" fn create_pointer_ffi<W: Wlcs>(ptr: *mut WlcsDisplayServer) -> *mut WlcsPointer {
    match std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        let Some(p) = server.wlcs.create_pointer() else {
            return std::ptr::null_mut();
        };

        let handle: *mut PointerHandle<W> = Box::into_raw(Box::new(PointerHandle {
            wlcs_pointer: wlcs_pointer::<W>(),
            p,
        }));
        std::ptr::addr_of_mut!((*handle).wlcs_pointer)
    }) {
        Ok(ptr) => ptr,
        Err(err) => {
            println!(
                "panic in wlcs_display_server::create_pointer_ffi on ptr: {:p} (type {:?})",
                err.as_ref() as *const _,
                err.type_id()
            );
            std::ptr::null_mut()
        }
    }
}

#[allow(unused)]
unsafe extern "C" fn create_touch_ffi<W: Wlcs>(ptr: *mut WlcsDisplayServer) -> *mut WlcsTouch {
    match std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        let Some(t) = server.wlcs.create_touch() else {
            return std::ptr::null_mut();
        };
        let handle: *mut TouchHandle<W> = Box::into_raw(Box::new(TouchHandle {
            wlcs_touch: wlcs_touch::<W>(),
            t,
        }));
        std::ptr::addr_of_mut!((*handle).wlcs_touch)
    }) {
        Ok(ptr) => ptr,
        Err(err) => {
            println!(
                "panic in wlcs_display_server::create_touch_ffi on ptr: {:p} (type {:?})",
                err.as_ref() as *const _,
                err.type_id()
            );
            std::ptr::null_mut()
        }
    }
}

#[allow(unused)]
unsafe extern "C" fn get_descriptor_ffi<W: Wlcs>(
    ptr: *const WlcsDisplayServer,
) -> *const WlcsIntegrationDescriptor {
    match std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_ref::<W>(ptr) };
        server.wlcs.get_descriptor()
    }) {
        Ok(ptr) => ptr as *const WlcsIntegrationDescriptor,
        Err(err) => {
            println!(
                "panic in wlcs_display_server::get_descriptor_ffi on ptr: {:p} (type {:?})",
                err.as_ref() as *const _,
                err.type_id()
            );
            std::ptr::null_mut()
        }
    }
}

#[allow(unused)]
unsafe extern "C" fn start_on_this_thread_ffi<W: Wlcs>(
    ptr: *mut WlcsDisplayServer,
    event_loop: *mut wl_event_loop,
) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let server = unsafe { get_display_server_handle_mut::<W>(ptr) };
        assert_eq!(server.wlcs_display_server.version, 3);
        server.wlcs.start_on_this_thread(event_loop)
    }) {
        println!(
            "panic in start_on_this_thread_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

const fn wlcs_display_server<W: Wlcs>() -> WlcsDisplayServer {
    WlcsDisplayServer {
        version: 3,
        start: Some(start_server_ffi::<W>),
        stop: Some(stop_server_ffi::<W>),
        create_client_socket: Some(create_client_socket_ffi::<W>),
        position_window_absolute: Some(position_window_absolute_ffi::<W>),
        create_pointer: Some(create_pointer_ffi::<W>),
        create_touch: Some(create_touch_ffi::<W>),
        get_descriptor: Some(get_descriptor_ffi::<W>),
        start_on_this_thread: Some(start_on_this_thread_ffi::<W>),
    }
}

/// Instantiate the WlcsServerIntegration for WLCS FFI.
///
/// This should not be called directly. Instead [`crate::wlcs_server_integration!`] should be used
#[allow(unused)]
pub const fn wlcs_server<W>() -> WlcsServerIntegration
where
    W: Wlcs,
{
    WlcsServerIntegration {
        version: 1,
        create_server: Some(create_server_ffi::<W>),
        destroy_server: Some(destroy_server_ffi::<W>),
    }
}

/// Instantiate the WlcsServerIntegration for the specified type.
///
/// See [`Wlcs`] trait.
#[macro_export]
macro_rules! wlcs_server_integration {
    ($handle: ident) => {
        #[no_mangle]
        static wlcs_server_integration: WlcsServerIntegration = wlcs_server::<$handle>();
    };
}

unsafe extern "C" fn pointer_move_absolute_ffi<W: Wlcs>(
    ptr: *mut WlcsPointer,
    x: wl_fixed_t,
    y: wl_fixed_t,
) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let pointer = unsafe { get_pointer_handle::<W>(ptr) };
        pointer.p.move_absolute(x, y);
    }) {
        println!(
            "panic in pointer_move_absolute_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn pointer_move_relative_ffi<W: Wlcs>(
    ptr: *mut WlcsPointer,
    dx: wl_fixed_t,
    dy: wl_fixed_t,
) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let pointer = unsafe { get_pointer_handle::<W>(ptr) };
        pointer.p.move_relative(dx, dy);
    }) {
        println!(
            "panic in pointer_move_relative_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn pointer_button_up_ffi<W: Wlcs>(ptr: *mut WlcsPointer, button: i32) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let pointer = unsafe { get_pointer_handle::<W>(ptr) };
        pointer.p.button_up(button)
    }) {
        println!(
            "panic in pointer_button_up_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn pointer_button_down_ffi<W: Wlcs>(ptr: *mut WlcsPointer, button: i32) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let pointer = unsafe { get_pointer_handle::<W>(ptr) };
        pointer.p.button_down(button)
    }) {
        println!(
            "panic in pointer_button_down_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn pointer_destroy_ffi<W: Wlcs>(ptr: *mut WlcsPointer) {
    if let Err(err) = std::panic::catch_unwind(|| {
        // SAFETY:
        // - wlcs will no longer use the WlcsPointer pointer. This ensures we take back ownership of the
        //   allocation.
        // - The PointerHandle was created using Box::from_raw, ensuring the memory layout is correct.
        let mut pointer =
            unsafe { Box::from_raw(container_of!(ptr, PointerHandle<W>, wlcs_pointer)) };
        pointer.p.destroy()
    }) {
        println!(
            "panic in pointer_destroy_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

const fn wlcs_pointer<W: Wlcs>() -> WlcsPointer {
    WlcsPointer {
        version: 1,
        move_absolute: Some(pointer_move_absolute_ffi::<W>),
        move_relative: Some(pointer_move_relative_ffi::<W>),
        button_up: Some(pointer_button_up_ffi::<W>),
        button_down: Some(pointer_button_down_ffi::<W>),
        destroy: Some(pointer_destroy_ffi::<W>),
    }
}

unsafe extern "C" fn touch_down_ffi<W: Wlcs>(ptr: *mut WlcsTouch, x: wl_fixed_t, y: wl_fixed_t) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let touch = unsafe { get_touch_handle::<W>(ptr) };
        touch.t.touch_down(x, y);
    }) {
        println!(
            "panic in touch_down_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn touch_move_ffi<W: Wlcs>(ptr: *mut WlcsTouch, x: wl_fixed_t, y: wl_fixed_t) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let touch = unsafe { get_touch_handle::<W>(ptr) };
        touch.t.touch_move(x, y);
    }) {
        println!(
            "panic in touch_down_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn touch_up_ffi<W: Wlcs>(ptr: *mut WlcsTouch) {
    if let Err(err) = std::panic::catch_unwind(|| {
        let touch = unsafe { get_touch_handle::<W>(ptr) };
        touch.t.touch_up();
    }) {
        println!(
            "panic in touch_up_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

unsafe extern "C" fn touch_destroy_ffi<W: Wlcs>(ptr: *mut WlcsTouch) {
    if let Err(err) = std::panic::catch_unwind(|| {
        // SAFETY:
        // - wlcs will no longer use the WlcsTouch pointer. This ensures we take back ownership of the
        //   allocation.
        // - The TouchHandle was created using Box::from_raw, ensuring the memory layout is correct.
        let mut touch = unsafe { Box::from_raw(container_of!(ptr, TouchHandle<W>, wlcs_touch)) };
        touch.t.destroy()
    }) {
        println!(
            "panic in touch_destroy_ffi on ptr: {:p} (type {:?})",
            err.as_ref() as *const _,
            err.type_id()
        );
    }
}

const fn wlcs_touch<W: Wlcs>() -> WlcsTouch {
    WlcsTouch {
        version: 1,
        touch_down: Some(touch_down_ffi::<W>),
        touch_move: Some(touch_move_ffi::<W>),
        touch_up: Some(touch_up_ffi::<W>),
        destroy: Some(touch_destroy_ffi::<W>),
    }
}