From 0c1861e9b09d37d9a3f541d0b8c827d5439bb5c6 Mon Sep 17 00:00:00 2001 From: Ella Stanforth Date: Tue, 11 Mar 2025 09:24:20 +0000 Subject: [PATCH] Switch to x11-dl (#333) Everything else in servo uses x11-dl and this allows building on systems without X11 libraries installed. --- Cargo.toml | 5 ++--- src/platform/unix/x11/connection.rs | 23 +++++++++++++++++------ src/platform/unix/x11/surface.rs | 4 ++-- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 82e7ce9c..b085ebaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ sm-angle-default = ["sm-angle"] sm-no-wgl = ["sm-angle-default"] sm-test = [] sm-wayland-default = [] -sm-x11 = ["x11"] +sm-x11 = ["x11-dl"] sm-raw-window-handle-generic = [] sm-raw-window-handle-05 = ["dep:rwh_05"] sm-raw-window-handle-06 = ["dep:rwh_06"] @@ -69,9 +69,8 @@ objc = "0.2" version = "0.30" features = ["client", "dlopen", "egl"] -[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_env = "ohos"))))'.dependencies.x11] +[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_env = "ohos"))))'.dependencies.x11-dl] version = "2.3.0" -features = ["xlib"] optional = true # Ensure that we have a static libEGL.lib present for linking with EGL bindings. diff --git a/src/platform/unix/x11/connection.rs b/src/platform/unix/x11/connection.rs index 3eae82df..fb1f3b35 100644 --- a/src/platform/unix/x11/connection.rs +++ b/src/platform/unix/x11/connection.rs @@ -18,7 +18,7 @@ use std::marker::PhantomData; use std::os::raw::c_void; use std::ptr; use std::sync::{Arc, Once}; -use x11::xlib::{Display, XCloseDisplay, XInitThreads, XLockDisplay, XOpenDisplay, XUnlockDisplay}; +use x11_dl::xlib::{Display, Xlib}; static X_THREADS_INIT: Once = Once::new(); @@ -31,6 +31,7 @@ pub struct Connection { unsafe impl Send for Connection {} pub(crate) struct NativeConnectionWrapper { + pub(crate) xlib: Xlib, pub(crate) egl_display: EGLDisplay, x11_display: *mut Display, x11_display_is_owned: bool, @@ -54,7 +55,7 @@ impl Drop for NativeConnectionWrapper { fn drop(&mut self) { unsafe { if self.x11_display_is_owned { - XCloseDisplay(self.x11_display); + (self.xlib.XCloseDisplay)(self.x11_display); } self.x11_display = ptr::null_mut(); } @@ -66,11 +67,13 @@ impl Connection { #[inline] pub fn new() -> Result { unsafe { + let xlib = Xlib::open().map_err(|_| Error::ConnectionFailed)?; + X_THREADS_INIT.call_once(|| { - XInitThreads(); + (xlib.XInitThreads)(); }); - let x11_display = XOpenDisplay(ptr::null()); + let x11_display = (xlib.XOpenDisplay)(ptr::null()); if x11_display.is_null() { return Err(Error::ConnectionFailed); } @@ -79,6 +82,7 @@ impl Connection { Ok(Connection { native_connection: Arc::new(NativeConnectionWrapper { + xlib, x11_display, x11_display_is_owned: true, egl_display, @@ -102,8 +106,10 @@ impl Connection { pub unsafe fn from_native_connection( native_connection: NativeConnection, ) -> Result { + let xlib = Xlib::open().map_err(|_| Error::ConnectionFailed)?; Ok(Connection { native_connection: Arc::new(NativeConnectionWrapper { + xlib, egl_display: native_connection.egl_display, x11_display: native_connection.x11_display, x11_display_is_owned: false, @@ -112,10 +118,12 @@ impl Connection { } fn from_x11_display(x11_display: *mut Display, is_owned: bool) -> Result { + let xlib = Xlib::open().map_err(|_| Error::ConnectionFailed)?; unsafe { let egl_display = create_egl_display(x11_display); Ok(Connection { native_connection: Arc::new(NativeConnectionWrapper { + xlib, egl_display, x11_display, x11_display_is_owned: is_owned, @@ -271,8 +279,10 @@ impl NativeConnectionWrapper { pub(crate) fn lock_display(&self) -> DisplayGuard { unsafe { let display = self.x11_display; - XLockDisplay(display); + let xlib = &self.xlib; + (xlib.XLockDisplay)(display); DisplayGuard { + xlib, display, phantom: PhantomData, } @@ -281,6 +291,7 @@ impl NativeConnectionWrapper { } pub(crate) struct DisplayGuard<'a> { + xlib: &'a Xlib, display: *mut Display, phantom: PhantomData<&'a ()>, } @@ -288,7 +299,7 @@ pub(crate) struct DisplayGuard<'a> { impl<'a> Drop for DisplayGuard<'a> { fn drop(&mut self) { unsafe { - XUnlockDisplay(self.display); + (self.xlib.XUnlockDisplay)(self.display); } } } diff --git a/src/platform/unix/x11/surface.rs b/src/platform/unix/x11/surface.rs index 8319229f..c3e1bb2c 100644 --- a/src/platform/unix/x11/surface.rs +++ b/src/platform/unix/x11/surface.rs @@ -15,7 +15,7 @@ use euclid::default::Size2D; use glow::Texture; use std::marker::PhantomData; use std::os::raw::c_void; -use x11::xlib::{Window, XGetGeometry}; +use x11_dl::xlib::Window; // FIXME(pcwalton): Is this right, or should it be `TEXTURE_EXTERNAL_OES`? const SURFACE_GL_TEXTURE_TARGET: u32 = gl::TEXTURE_2D; @@ -113,7 +113,7 @@ impl Device { let display_guard = self.native_connection.lock_display(); let (mut root_window, mut x, mut y, mut width, mut height) = (0, 0, 0, 0, 0); let (mut border_width, mut depth) = (0, 0); - XGetGeometry( + (self.native_connection.xlib.XGetGeometry)( display_guard.display(), x11_window, &mut root_window,