get_registry/
main.rs

1use {
2    crate::common::protocols::wayland::{wl_display::WlDisplay, wl_registry::WlRegistry},
3    parking_lot::Mutex,
4    wl_client::{Libwayland, Queue},
5};
6
7#[path = "../common/mod.rs"]
8mod common;
9
10fn main() {
11    let lib = Libwayland::open().unwrap();
12    let con = lib.connect_to_default_display().unwrap();
13    let queue = con.create_queue(c"get-registry");
14
15    let (_, snapshot) = get_registry_snapshot(&queue);
16    println!("{:#?}", snapshot);
17}
18
19#[expect(dead_code)]
20#[derive(Debug)]
21struct Global {
22    pub name: u32,
23    pub interface: String,
24    pub version: u32,
25}
26
27fn get_registry_snapshot(queue: &Queue) -> (WlRegistry, Vec<Global>) {
28    // Create a new registry that will receive the globals and can later be used to
29    // bind them.
30    let registry = queue.display::<WlDisplay>().get_registry();
31    let globals = Mutex::new(vec![]);
32    // Since we don't care about registry events after this function returns, we can
33    // use a dispatch scope. The event handlers in this scope will not be called after
34    // the function returns.
35    queue.dispatch_scope_blocking(|scope| {
36        scope.set_event_handler(
37            &registry,
38            // Since we only want to create a snapshot, we don't care about
39            // global_remove events. This allows us to use the functional event handler
40            // form.
41            WlRegistry::on_global(|_, name, interface, version| {
42                globals.lock().push(Global {
43                    name,
44                    interface: interface.to_string(),
45                    version,
46                });
47            }),
48        );
49        queue.dispatch_roundtrip_blocking().unwrap();
50    });
51    // The event handler will no longer be called after this function returns but
52    // the registry can still be used to bind globals.
53    (registry, globals.into_inner())
54}