From 46db6bfff79bf926dafe558586923e138f464ab8 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Tue, 31 Oct 2023 19:53:01 -0700 Subject: [PATCH 1/3] Enable creating an X11 display without connection For pure-Rust connections, there is no XCB connection that can be passed into the display constructor. Thus, this PR enables the display to be created without needing an XCB or Xlib handle, by providing `None` in the constructor. cc rust-windowing/raw-window-handle#120 Signed-off-by: John Nunley --- src/x11.rs | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/x11.rs b/src/x11.rs index 0711d340..eaa088f0 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -61,37 +61,36 @@ impl X11DisplayImpl { RawDisplayHandle::Xcb(xcb_handle) => xcb_handle, RawDisplayHandle::Xlib(xlib) => { // Convert to an XCB handle. - let display = match xlib.display { - Some(display) => display, - None => return Err(SoftBufferError::IncompleteDisplayHandle.into()), - }; - - // Get the underlying XCB connection. - // SAFETY: The user has asserted that the display handle is valid. - let connection = unsafe { - let display = tiny_xlib::Display::from_ptr(display.as_ptr()); - NonNull::new_unchecked(display.as_raw_xcb_connection()) - }; + let connection = xlib.display.map(|display| { + // Get the underlying XCB connection. + // SAFETY: The user has asserted that the display handle is valid. + unsafe { + let display = tiny_xlib::Display::from_ptr(display.as_ptr()); + NonNull::new_unchecked(display.as_raw_xcb_connection()).cast() + } + }); // Construct the equivalent XCB display and window handles. - XcbDisplayHandle::new(Some(connection.cast()), xlib.screen) + XcbDisplayHandle::new(connection, xlib.screen) } _ => return Err(InitError::Unsupported(display)), }; // Validate the display handle to ensure we can use it. let connection = match xcb_handle.connection { - Some(conn) => conn, - None => return Err(SoftBufferError::IncompleteDisplayHandle.into()), - }; - - // Wrap the display handle in an x11rb connection. - // SAFETY: We don't own the connection, so don't drop it. We also assert that the connection is valid. - let connection = { - let result = - unsafe { XCBConnection::from_raw_xcb_connection(connection.as_ptr(), false) }; - - result.swbuf_err("Failed to wrap XCB connection")? + Some(connection) => { + // Wrap the display handle in an x11rb connection. + // SAFETY: We don't own the connection, so don't drop it. We also assert that the connection is valid. + let result = + unsafe { XCBConnection::from_raw_xcb_connection(connection.as_ptr(), false) }; + + result.swbuf_err("Failed to wrap XCB connection")? + }, + None => { + // The user didn't provide an XCB connection, so create our own. + log::info!("no XCB connection provided by the user, so spawning our own"); + XCBConnection::connect(None).swbuf_err("Failed to spawn XCB connection")?.0 + } }; let is_shm_available = is_shm_available(&connection); From a9f595865cf108103cc0f03d9176fa685dd532f0 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Tue, 31 Oct 2023 20:18:38 -0700 Subject: [PATCH 2/3] fmt Signed-off-by: John Nunley --- src/x11.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/x11.rs b/src/x11.rs index eaa088f0..d3d399a2 100644 --- a/src/x11.rs +++ b/src/x11.rs @@ -85,11 +85,13 @@ impl X11DisplayImpl { unsafe { XCBConnection::from_raw_xcb_connection(connection.as_ptr(), false) }; result.swbuf_err("Failed to wrap XCB connection")? - }, + } None => { // The user didn't provide an XCB connection, so create our own. log::info!("no XCB connection provided by the user, so spawning our own"); - XCBConnection::connect(None).swbuf_err("Failed to spawn XCB connection")?.0 + XCBConnection::connect(None) + .swbuf_err("Failed to spawn XCB connection")? + .0 } }; From db0e10acbeaf13be18422281a409a59920a0d282 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Wed, 1 Nov 2023 19:48:14 -0700 Subject: [PATCH 3/3] Add a test case Signed-off-by: John Nunley --- examples/libxcb.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/libxcb.rs b/examples/libxcb.rs index 88ef5f28..53efc5d3 100644 --- a/examples/libxcb.rs +++ b/examples/libxcb.rs @@ -6,7 +6,7 @@ mod example { DisplayHandle, RawDisplayHandle, RawWindowHandle, WindowHandle, XcbDisplayHandle, XcbWindowHandle, }; - use std::{num::NonZeroU32, ptr::NonNull}; + use std::{env, num::NonZeroU32, ptr::NonNull}; use x11rb::{ connection::Connection, protocol::{ @@ -24,7 +24,11 @@ mod example { // x11rb doesn't use raw-window-handle yet, so just create our own. let display_handle = XcbDisplayHandle::new( - NonNull::new(conn.get_raw_xcb_connection() as *mut _), + if env::var_os("SOFTBUFFER_NO_DISPLAY").is_some() { + None + } else { + NonNull::new(conn.get_raw_xcb_connection() as *mut _) + }, screen as _, );