Skip to main content

async_roundtrip/
main.rs

1use crate::common::protocols::wayland::wl_display::WlDisplay;
2use crate::common::protocols::wayland::wl_registry::WlRegistry;
3use std::cell::Cell;
4use wl_client::Libwayland;
5
6#[path = "../common/mod.rs"]
7mod common;
8
9#[tokio::main(flavor = "current_thread")]
10async fn main() {
11    let lib = Libwayland::open().unwrap();
12    let con = lib.connect_to_default_display().unwrap();
13    let queue = con.create_local_queue(c"async-roundtrip");
14    let registry = queue.display::<WlDisplay>().get_registry();
15    let num_globals = Cell::new(0);
16    queue
17        .dispatch_scope_async(async |scope| {
18            scope.set_event_handler_local(
19                &registry,
20                WlRegistry::on_global(|_, _, _, _| {
21                    num_globals.set(num_globals.get() + 1);
22                }),
23            );
24            // This function can be used to perform an async roundtrip. It is
25            // compatible with any async runtime. This example also demonstrates
26            // that this works in combination with scoped event handlers.
27            queue.dispatch_roundtrip_async().await.unwrap();
28        })
29        .await;
30    println!("number of globals: {}", num_globals.get());
31}