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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use {
    crate::{
        Libwayland, proxy,
        test_protocol_helpers::get_root,
        test_protocols::core::{
            wl_callback::{WlCallback, WlCallbackEventHandler, WlCallbackRef},
            wl_display::WlDisplay,
            wl_dummy::WlDummyRef,
            wl_root::{WlRootEventHandler, WlRootRef},
        },
    },
    futures_util::join,
    std::{
        cell::Cell,
        ops::Deref,
        rc::Rc,
        sync::{
            Arc, Barrier,
            atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed},
        },
        thread,
        time::Duration,
    },
};

#[test]
fn sync() {
    let libwayland = Libwayland::open().unwrap();
    let con = libwayland.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let display: WlDisplay = queue.display();
    let callback = display.sync();
    let ponged = Arc::new(AtomicBool::new(false));
    proxy::set_event_handler(&callback, Eh(callback.clone(), ponged.clone()));
    queue.dispatch_roundtrip_blocking().unwrap();
    assert!(ponged.load(Relaxed));

    struct Eh(WlCallback, Arc<AtomicBool>);
    impl WlCallbackEventHandler for Eh {
        fn done(&self, _slf: &WlCallbackRef, _callback_data: u32) {
            self.1.store(true, Relaxed);
            proxy::destroy(&self.0);
        }
    }
}

#[test]
fn sync_local() {
    let libwayland = Libwayland::open().unwrap();
    let con = libwayland.connect_to_default_display().unwrap();
    let queue = con.create_local_queue(c"abc");
    let display: WlDisplay = queue.display();
    let callback = display.sync();
    let ponged = Rc::new(Cell::new(false));
    proxy::set_event_handler_local(&callback, Eh(callback.clone(), ponged.clone()));
    queue.dispatch_roundtrip_blocking().unwrap();
    assert!(ponged.get());

    struct Eh(WlCallback, Rc<Cell<bool>>);
    impl WlCallbackEventHandler for Eh {
        fn done(&self, _slf: &WlCallbackRef, _callback_data: u32) {
            self.1.set(true);
            proxy::destroy(&self.0);
        }
    }
}

#[test]
fn ping_pong_non_destroyed() {
    let libwayland = Libwayland::open().unwrap();
    let con = libwayland.connect_to_default_display().unwrap();
    let queue = con.create_local_queue(c"abc");
    let root = get_root(&queue);
    let dummy = root.create_dummy();
    proxy::set_event_handler_local(&root, Eh);
    root.ping_dummy(&dummy);
    queue.dispatch_roundtrip_blocking().unwrap();

    struct Eh;
    impl WlRootEventHandler for Eh {
        fn pong_dummy(&self, _slf: &WlRootRef, id: Option<&WlDummyRef>) {
            assert!(id.is_some());
        }
    }
}

#[test]
fn ping_pong_destroyed() {
    let libwayland = Libwayland::open().unwrap();
    let con = libwayland.connect_to_default_display().unwrap();
    let queue = con.create_local_queue(c"abc");
    let root = get_root(&queue);
    let dummy = root.create_dummy();
    proxy::set_event_handler_local(&root, Eh);
    root.ping_dummy(&dummy);
    dummy.destroy();
    queue.dispatch_roundtrip_blocking().unwrap();

    struct Eh;
    impl WlRootEventHandler for Eh {
        fn pong_dummy(&self, _slf: &WlRootRef, id: Option<&WlDummyRef>) {
            assert!(id.is_none());
        }
    }
}

#[test]
fn construct_from_ref() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let sync = queue.display::<WlDisplay>().deref().sync(&queue);
    let done = Arc::new(AtomicBool::new(false));
    proxy::set_event_handler(&sync, Eh(sync.clone(), done.clone()));
    queue.dispatch_roundtrip_blocking().unwrap();
    assert!(done.load(Relaxed));

    struct Eh(WlCallback, Arc<AtomicBool>);
    impl WlCallbackEventHandler for Eh {
        fn done(&self, _slf: &WlCallbackRef, _callback_data: u32) {
            proxy::destroy(&self.0);
            self.1.store(true, Relaxed);
        }
    }
}

#[test]
fn construct_from_ref_wrapper() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_local_queue(c"abc");
    let sync = queue.display::<WlDisplay>().deref().sync(&queue);
    {
        assert_eq!(proxy::queue(&sync), &*queue);
        let ptr = proxy::wl_proxy(&*sync).unwrap().as_ptr();
        unsafe {
            assert_eq!(lib.wl_proxy_get_queue(ptr), queue.wl_event_queue().as_ptr());
        }
    }
    let done = Rc::new(Cell::new(false));
    proxy::set_event_handler_local(&sync, Eh(sync.clone(), done.clone()));
    queue.dispatch_roundtrip_blocking().unwrap();
    assert!(done.get());

    struct Eh(WlCallback, Rc<Cell<bool>>);
    impl WlCallbackEventHandler for Eh {
        fn done(&self, _slf: &WlCallbackRef, _callback_data: u32) {
            proxy::destroy(&self.0);
            self.1.set(true);
        }
    }
}

#[test]
#[should_panic(expected = "Queue is not a local queue")]
fn set_local_dispatcher_on_non_local_queue() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler_local(&sync, Eh);

    struct Eh;
    impl WlCallbackEventHandler for Eh {}
}

#[test]
fn dispatch_without_flush() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let count = Arc::new(AtomicUsize::new(0));
    let sync1 = queue.display::<WlDisplay>().sync();
    con.create_queue(c"aoe")
        .dispatch_roundtrip_blocking()
        .unwrap();
    let sync2 = queue.display::<WlDisplay>().sync();

    proxy::set_event_handler(&sync1, Eh(sync1.clone(), count.clone()));
    proxy::set_event_handler(&sync2, Eh(sync2.clone(), count.clone()));

    queue.dispatch_blocking().unwrap();
    assert_eq!(count.load(Relaxed), 1);

    queue.dispatch_blocking().unwrap();
    assert_eq!(count.load(Relaxed), 2);

    struct Eh(WlCallback, Arc<AtomicUsize>);
    impl WlCallbackEventHandler for Eh {
        fn done(&self, _slf: &WlCallbackRef, _callback_data: u32) {
            proxy::destroy(&self.0);
            self.1.fetch_add(1, Relaxed);
        }
    }
}

#[test]
#[should_panic(expected = "Proxy has already been destroyed")]
fn dispatch_on_destroyed() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let root = get_root(&queue);
    let dummy = root.create_dummy();
    assert!(proxy::wl_proxy(&*dummy).is_some());
    dummy.destroy();
    assert!(proxy::wl_proxy(&*dummy).is_none());
    dummy.destroy();
}

#[test]
#[should_panic(expected = "Proxy is a wrapper")]
fn event_handler_on_wrapper() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let root = get_root(&queue);
    let wrapper = queue.wrap_proxy(&*root);
    proxy::set_event_handler(&wrapper, Eh);

    struct Eh;
    impl WlRootEventHandler for Eh {}
}

#[test]
#[should_panic(expected = "Proxy has already been destroyed")]
fn event_handler_on_destroyed() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let root = get_root(&queue);
    root.destroy();
    proxy::set_event_handler(&root, Eh);

    struct Eh;
    impl WlRootEventHandler for Eh {}
}

#[test]
#[should_panic(expected = "Proxy already has an event handler")]
fn multiple_event_handlers() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let root = get_root(&queue);
    proxy::set_event_handler(&root, Eh);
    proxy::set_event_handler(&root, Eh);

    struct Eh;
    impl WlRootEventHandler for Eh {}
}

#[test]
fn idempotent_destroy() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let root = get_root(&queue);
    root.destroy();
    proxy::destroy(&root);
    proxy::destroy(&root);
}

#[test]
fn constructor_destructor() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let root = get_root(&queue);
    let dummy1 = root.create_dummy();
    assert!(proxy::wl_proxy(&*dummy1).is_some());
    let dummy2 = dummy1.recycle();
    assert!(proxy::wl_proxy(&*dummy1).is_none());
    dummy2.destroy();
}

#[test]
fn drop_queue() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let root = {
        let queue = con.create_queue(c"aoeu");
        get_root(&queue)
    };
    proxy::set_event_handler(&root, Eh);
    drop(root);

    struct Eh;
    impl WlRootEventHandler for Eh {}
}

#[test]
fn leak() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler(&sync, Eh(sync.clone()));

    struct Eh(#[allow(dead_code)] WlCallback);
    impl WlCallbackEventHandler for Eh {}
}

#[test]
fn id() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler(&sync, Eh1(sync.clone()));
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler(&sync, Eh2(sync.clone()));
    queue.dispatch_roundtrip_blocking().unwrap();

    struct Eh1(#[allow(dead_code)] WlCallback);
    impl WlCallbackEventHandler for Eh1 {
        fn done(&self, slf: &WlCallbackRef, _callback_data: u32) {
            let id = proxy::id(slf);
            assert_ne!(id, 0);
            assert_eq!(id, proxy::id(slf));
            assert_eq!(id, proxy::id(&*self.0));
            proxy::destroy(&self.0);
            assert_eq!(id, proxy::id(slf));
            assert_eq!(id, proxy::id(&*self.0));
        }
    }

    struct Eh2(#[allow(dead_code)] WlCallback);
    impl WlCallbackEventHandler for Eh2 {
        fn done(&self, slf: &WlCallbackRef, _callback_data: u32) {
            assert_ne!(proxy::id(slf), 0);
            proxy::destroy(&self.0);
            assert_eq!(proxy::id(&*self.0), 0);
        }
    }
}

#[test]
fn eq() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler(&sync, Eh1(sync.clone()));
    queue.dispatch_roundtrip_blocking().unwrap();

    struct Eh1(#[allow(dead_code)] WlCallback);
    impl WlCallbackEventHandler for Eh1 {
        fn done(&self, slf: &WlCallbackRef, _callback_data: u32) {
            assert_eq!(slf, slf);
            assert_eq!(&self.0, slf);
            assert_eq!(slf, &self.0);
            assert_eq!(&self.0, &self.0);

            proxy::destroy(&self.0);

            assert_eq!(slf, slf);
            assert_ne!(&self.0, slf);
            assert_ne!(slf, &self.0);
            assert_eq!(&self.0, &self.0);
        }
    }
}

#[test]
fn wrap_owned_pointer() {
    let lib = Libwayland::open().unwrap();
    let con1 = lib.connect_to_default_display().unwrap();
    assert!(con1.is_owned());
    let con2 = con1.take_ownership().unwrap();
    assert!(con1.is_borrowed());
    drop(con1);
    let con3 = unsafe { lib.wrap_owned_pointer(con2).unwrap() };
    assert!(con3.is_owned());
    con3.create_queue(c"")
        .dispatch_roundtrip_blocking()
        .unwrap();
    // rely on miri to report either a memory leak or double free
}

#[test]
fn wrap_borrowed_pointer() {
    let lib = Libwayland::open().unwrap();
    let con1 = lib.connect_to_default_display().unwrap();
    let queue1 = con1.create_queue(c"");
    assert!(con1.is_owned());
    let con2 = unsafe { lib.wrap_borrowed_pointer(con1.wl_display()).unwrap() };
    let queue2 = con2.create_queue(c"");
    assert!(con2.is_borrowed());
    queue1.dispatch_roundtrip_blocking().unwrap();
    queue2.dispatch_roundtrip_blocking().unwrap();
    // rely on miri to report either a memory leak or double free
}

#[test]
fn drop_during_dispatch() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_local_queue(c"");
    let display: WlDisplay = queue.display();
    let sync = display.sync();
    let rc = Rc::new(Cell::new(0));
    proxy::set_event_handler_local(
        &sync,
        Eh {
            display,
            drop_count: rc.clone(),
            depth: 1,
            cb: Cell::new(Some(sync.clone())),
        },
    );
    drop(sync);
    queue.dispatch_roundtrip_blocking().unwrap();
    assert_eq!(rc.get(), 10);

    struct Eh {
        display: WlDisplay,
        drop_count: Rc<Cell<usize>>,
        depth: usize,
        cb: Cell<Option<WlCallback>>,
    }
    impl WlCallbackEventHandler for Eh {
        fn done(&self, _slf: &WlCallbackRef, _callback_data: u32) {
            self.cb.take();
            assert_eq!(self.drop_count.get(), 0);
            if self.depth < 10 {
                let sync = self.display.sync();
                let queue = proxy::queue(&sync).clone();
                proxy::set_event_handler_local(
                    &sync,
                    Eh {
                        display: self.display.clone(),
                        drop_count: self.drop_count.clone(),
                        depth: self.depth + 1,
                        cb: Cell::new(Some(sync.clone())),
                    },
                );
                drop(sync);
                queue.dispatch_roundtrip_blocking().unwrap();
            }
        }
    }
    impl Drop for Eh {
        fn drop(&mut self) {
            let dc = self.drop_count.get();
            self.drop_count.set(dc + 1);
        }
    }
}

#[test]
fn drop_large_copy() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"");
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler(&sync, Eh(0));

    struct Eh(#[allow(dead_code)] usize);
    impl WlCallbackEventHandler for Eh {}
}

#[test]
fn drop_conn_with_other_other_thread_local_queue() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    thread::scope(|s| {
        s.spawn(|| {
            let queue = con.create_local_queue(c"");
            let sync = queue.display::<WlDisplay>().sync();
            proxy::set_event_handler_local(&sync, Eh(sync.clone()));
        });
    });

    struct Eh(#[allow(dead_code)] WlCallback);
    impl WlCallbackEventHandler for Eh {}
    impl Drop for Eh {
        fn drop(&mut self) {
            // nothing
        }
    }
}

#[test]
fn create_in_different_queue() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let q1 = con.create_queue(c"");
    let q2 = con.create_queue(c"");
    let dp1 = q1.display::<WlDisplay>();
    assert_eq!(proxy::queue(&dp1), &*q1);
    assert_ne!(proxy::queue(&dp1), &*q2);
    let s1 = dp1.sync();
    assert_eq!(proxy::queue(&s1), &*q1);
    assert_ne!(proxy::queue(&s1), &*q2);
    let s2 = dp1.deref().sync(&q2);
    assert_ne!(proxy::queue(&s2), &*q1);
    assert_eq!(proxy::queue(&s2), &*q2);
}

#[tokio::test]
async fn async_roundtrip_tokio() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"queue name");
    let done = Arc::new(AtomicBool::new(false));
    let done2 = done.clone();
    let sync = queue.display::<WlDisplay>().sync();
    proxy::set_event_handler(
        &sync,
        WlCallback::on_done(move |_, _| done2.store(true, Relaxed)),
    );
    queue.dispatch_roundtrip_async().await.unwrap();
    assert!(done.load(Relaxed));
}

#[tokio::test]
async fn dispatch_async() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"queue name");
    let _sync = queue.display::<WlDisplay>().sync();
    queue.dispatch_async().await.unwrap();
}

#[test]
#[should_panic(expected = "leaked_memory")]
fn leak_no_event_handler() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"queue name");
    let root = get_root(&queue);
    root.send_new_dummy();
    queue.dispatch_roundtrip_blocking().unwrap();
}

#[test]
fn leak_no_op_event_handler() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"queue name");
    let root = get_root(&queue);
    proxy::set_event_handler_no_op(&root);
    root.send_new_dummy();
    queue.dispatch_roundtrip_blocking().unwrap();
}

#[tokio::test]
async fn join_multiple_read_locks() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"queue name");
    let display: WlDisplay = queue.display();
    let barrier1 = Arc::new(Barrier::new(2));
    let barrier2 = barrier1.clone();
    let thread = thread::spawn(move || {
        thread::sleep(Duration::from_secs(1));
        let _sync = display.sync();
        proxy::queue(&display).connection().flush().unwrap();
        barrier2.wait();
    });
    let (r1, r2) = join! {
        queue.wait_for_events(),
        queue.wait_for_events(),
    };
    barrier1.wait();
    thread.join().unwrap();
    r1.unwrap();
    r2.unwrap();
}

#[test]
fn dispatch_lock() {
    let lib = Libwayland::open().unwrap();
    let con = lib.connect_to_default_display().unwrap();
    let queue = con.create_queue(c"queue name");

    let lock = queue.lock_dispatch();

    let thread = {
        let queue = queue.clone();
        thread::spawn(move || {
            // this dispatch will not start until the lock is dropped.
            queue.dispatch_roundtrip_blocking().unwrap();
        })
    };

    // this dispatch starts immediately since the lock is re-entrant
    queue.dispatch_roundtrip_blocking().unwrap();

    drop(lock);
    thread.join().unwrap();
}