set_event_handler

Function set_event_handler 

Source
pub fn set_event_handler<P, H>(proxy: &P, handler: H)
where P: OwnedProxy, P::Api: CreateEventHandler<H>, <P::Api as CreateEventHandler<H>>::EventHandler: Send + 'static,
Expand description

Sets the event handler of the proxy.

This function can only be called once for each proxy. This function cannot be called on wrappers.

The event handler must implement Send. Use set_event_handler_local if your event handler does not implement Send.

§Panic

This function panics if

  • the proxy has already been destroyed,
  • the proxy is a wrapper,
  • the proxy is attached to a local queue and the current thread is not the thread in which the queue was created,
  • the proxy already has an event handler, or
  • the event handler accepts a &mut T, T != (), and the queue that the proxy is attached to was not created with this data type.

This function also panics if the interface of the created handler is not the same as the interface of the proxy. However, this cannot happen if you’re using the bindings generated by wl-client-builder.

§Example

let lib = Libwayland::open().unwrap();
let con = lib.connect_to_default_display().unwrap();
let queue = con.create_queue(c"");
let display: WlDisplay = queue.display();
let sync = display.sync();
let done = Arc::new(AtomicBool::new(false));

// Attach the event handler.
let done2 = done.clone();
proxy::set_event_handler(&sync, WlCallback::on_done(move |_, _| {
    done2.store(true, Relaxed);
}));

// Wait for the compositor to send the `done` message.
queue.dispatch_roundtrip_blocking().unwrap();

// The event handler sets the value to `true`.
assert!(done.load(Relaxed));
Examples found in repository?
examples/async-wait/main.rs (lines 25-32)
23fn create_sync(queue: &Queue, n: u64) {
24    let sync = queue.display::<WlDisplay>().sync();
25    proxy::set_event_handler(
26        &sync.clone(),
27        WlCallback::on_done(move |_, _| {
28            println!("done! ({n})");
29            proxy::destroy(&sync);
30            create_sync(proxy::queue(&sync), n + 1);
31        }),
32    );
33}
More examples
Hide additional examples
examples/poll-integration/main.rs (lines 50-57)
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}
examples/get-registry-with-data/main.rs (lines 37-46)
22fn main() {
23    let lib = Libwayland::open().unwrap();
24    let con = lib.connect_to_default_display().unwrap();
25    let (_queue, queue) = con.create_queue_with_data::<State>(c"get-registry");
26
27    // Create a new registry that will receive the globals and can later be used to
28    // bind them.
29    let mut state = State {
30        registry: queue.display::<WlDisplay>().get_registry(),
31        globals: vec![],
32    };
33
34    // Since we only want to create a snapshot, we don't care about
35    // global_remove events. This allows us to use the functional event handler
36    // form.
37    proxy::set_event_handler(
38        &state.registry,
39        WlRegistry::on_global(|state: &mut State, _, name, interface, version| {
40            state.globals.push(Global {
41                name,
42                interface: interface.to_string(),
43                version,
44            });
45        }),
46    );
47    queue.dispatch_roundtrip_blocking(&mut state).unwrap();
48
49    println!("{:#?}", state.globals);
50}
examples/hello-wayland/main.rs (lines 24-30)
9fn main() {
10    // Load the `libwayland-client.so` dynamic library.
11    let lib = Libwayland::open().unwrap();
12    // Connect to the default display determined by the `WAYLAND_DISPLAY` env var.
13    let con = lib.connect_to_default_display().unwrap();
14    // Create a new event queue with the name `hello-wayland`. This name will show up
15    // when debugging applications with `WAYLAND_DEBUG=1`.
16    let queue = con.create_queue(c"hello-wayland");
17    // Get a reference to the `wl_display` singleton. This type was generated with the
18    // `wl-client-builder` crate.
19    let display: WlDisplay = queue.display();
20    // Create a `wl_callback` object. The compositor will immediately respond with a
21    // `wl_callback.done` event.
22    let sync = display.sync();
23    // Set the event handler of the proxy.
24    proxy::set_event_handler(
25        &sync,
26        // When only handling a single event, the following functional form can be used.
27        // In general, and when handling more than one event, the event handler trait must
28        // be implemented. In this case, `WlCallbackEventHandler`.
29        WlCallback::on_done(|_, _| println!("Hello wayland!")),
30    );
31    // Perform a roundtrip to ensure that the `done` event has been dispatched.
32    queue.dispatch_roundtrip_blocking().unwrap();
33}