poll_integration/
main.rs

1use {
2    crate::common::protocols::wayland::{wl_callback::WlCallback, wl_display::WlDisplay},
3    mio::{Interest, Token, unix::SourceFd},
4    std::os::fd::AsRawFd,
5    wl_client::{Libwayland, Queue, proxy},
6};
7
8#[path = "../common/mod.rs"]
9mod common;
10
11fn main() {
12    let lib = Libwayland::open().unwrap();
13    let con = lib.connect_to_default_display().unwrap();
14    let queue = con.create_local_queue(c"poll-integration");
15
16    // The watcher exposes a file descriptor that will become readable when the queue
17    // has new events.
18    let watcher = queue.create_watcher().unwrap();
19    let token = Token(0);
20
21    create_sync(&queue, 1);
22
23    let mut events = mio::Events::with_capacity(2);
24    let mut poll = mio::Poll::new().unwrap();
25    poll.registry()
26        .register(
27            &mut SourceFd(&watcher.as_raw_fd()),
28            token,
29            Interest::READABLE,
30        )
31        .unwrap();
32
33    loop {
34        // Flush requests before polling.
35        con.flush().unwrap();
36        poll.poll(&mut events, None).unwrap();
37        for event in events.iter() {
38            if event.token() == token {
39                queue.dispatch_pending().unwrap();
40                // Reset the watcher to clear the readability status.
41                watcher.reset().unwrap();
42            }
43        }
44        events.clear();
45    }
46}
47
48fn create_sync(queue: &Queue, n: u64) {
49    let sync = queue.display::<WlDisplay>().sync();
50    proxy::set_event_handler(
51        &sync.clone(),
52        WlCallback::on_done(move |_, _| {
53            println!("done! ({n})");
54            proxy::destroy(&sync);
55            create_sync(proxy::queue(&sync), n + 1);
56        }),
57    );
58}