Skip to content

Instantly share code, notes, and snippets.

@taoky
Last active March 28, 2024 18:17
Show Gist options
  • Save taoky/7ab72e6880c6bf94b96a7bd619bc1539 to your computer and use it in GitHub Desktop.
Save taoky/7ab72e6880c6bf94b96a7bd619bc1539 to your computer and use it in GitHub Desktop.
wl_registry_add_listener example (Rust)
[package]
name = "globals"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4.21"
simple_logger = "4.3.3"
wayland-client = "0.31.2"
use simple_logger::SimpleLogger;
use wayland_client::{protocol::wl_registry, Connection, Dispatch, QueueHandle};
struct AppData;
impl Dispatch<wl_registry::WlRegistry, ()> for AppData {
fn event(
_state: &mut Self,
_: &wl_registry::WlRegistry,
event: wl_registry::Event,
_: &(),
_: &Connection,
_: &QueueHandle<AppData>,
) {
// When receiving events from the wl_registry, we are only interested in the
// `global` event, which signals a new available global.
// When receiving this event, we just print its characteristics in this example.
if let wl_registry::Event::Global { name, interface, version } = event {
log::info!("[{}] {} (v{})", name, interface, version);
} else if let wl_registry::Event::GlobalRemove { name } = event {
log::info!("[{}] removed", name);
}
}
}
fn main() {
SimpleLogger::new().init().unwrap();
let conn = Connection::connect_to_env().unwrap();
let display = conn.display();
let mut event_queue = conn.new_event_queue();
let qh = event_queue.handle();
let _registry = display.get_registry(&qh, ());
// event_queue.roundtrip(&mut AppData).unwrap();
loop {
event_queue.blocking_dispatch(&mut AppData).unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment