diff --git a/Cargo.lock b/Cargo.lock index 099fded..222e8ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,7 @@ dependencies = [ "as-raw-xcb-connection", "tiny-xlib", "x11rb", + "x11rb-protocol", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index dc4d6fb..d2d3dc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" tiny-xlib = "0.2.2" as-raw-xcb-connection = "1.0.1" x11rb = { version = "0.13.0", features = ["extra-traits", "allow-unsafe-code"] } +x11rb-protocol = { version = "0.13.0", features = ["std"] } anyhow = "1.0.79" diff --git a/src/main.rs b/src/main.rs index 5ee2d11..c894f96 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,15 @@ use as_raw_xcb_connection::AsRawXcbConnection; use tiny_xlib::Display; use x11rb::xcb_ffi::XCBConnection; +fn xcb_connection_from_display(display: &Display) -> anyhow::Result { + let xcb_conn = display.as_raw_xcb_connection(); + + // Safety: taking a pointer straight from the safe tiny_x11 to the safe x11rb + unsafe { + XCBConnection::from_raw_xcb_connection(xcb_conn.cast(), false).map_err(|e| anyhow!(e)) + } +} + fn main() -> anyhow::Result<()> { let mut args = std::env::args().skip(1); if let Some(arg) = args.next() { @@ -19,8 +28,14 @@ fn main() -> anyhow::Result<()> { // Here we open a display and get an x11rb connection, we could also just use xcb I think let display = Display::new(None).context("Failed to open display")?; - let xcb_conn = display.as_raw_xcb_connection(); - let xcb_conn = unsafe { XCBConnection::from_raw_xcb_connection(xcb_conn.cast(), false)? }; + + let _conn = xcb_connection_from_display(&display)?; + + // Registering a baby error handler for now + tiny_xlib::register_error_handler(Box::new(|_, error| { + println!("X11 error: {:?}", error); + false + }))?; // TODO: Check if there are other wms running