error handling and refactoring unsafe code

main
Zynh0722 2024-01-31 01:42:09 -08:00
parent c2fa7f4af2
commit 1c84e15ccb
3 changed files with 19 additions and 2 deletions

1
Cargo.lock generated
View File

@ -94,6 +94,7 @@ dependencies = [
"as-raw-xcb-connection", "as-raw-xcb-connection",
"tiny-xlib", "tiny-xlib",
"x11rb", "x11rb",
"x11rb-protocol",
] ]
[[package]] [[package]]

View File

@ -9,4 +9,5 @@ edition = "2021"
tiny-xlib = "0.2.2" tiny-xlib = "0.2.2"
as-raw-xcb-connection = "1.0.1" as-raw-xcb-connection = "1.0.1"
x11rb = { version = "0.13.0", features = ["extra-traits", "allow-unsafe-code"] } x11rb = { version = "0.13.0", features = ["extra-traits", "allow-unsafe-code"] }
x11rb-protocol = { version = "0.13.0", features = ["std"] }
anyhow = "1.0.79" anyhow = "1.0.79"

View File

@ -4,6 +4,15 @@ use as_raw_xcb_connection::AsRawXcbConnection;
use tiny_xlib::Display; use tiny_xlib::Display;
use x11rb::xcb_ffi::XCBConnection; use x11rb::xcb_ffi::XCBConnection;
fn xcb_connection_from_display(display: &Display) -> anyhow::Result<XCBConnection> {
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<()> { fn main() -> anyhow::Result<()> {
let mut args = std::env::args().skip(1); let mut args = std::env::args().skip(1);
if let Some(arg) = args.next() { 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 // 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 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 // TODO: Check if there are other wms running