Skip to content

Commit

Permalink
Switch to x11-dl
Browse files Browse the repository at this point in the history
Everything else in servo uses x11-dl and this allows building on systems
without X11 libraries installed.
  • Loading branch information
Ella-0 committed Mar 11, 2025
1 parent f7688b4 commit 9a6c706
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 17 additions & 6 deletions src/platform/unix/x11/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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,
Expand All @@ -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();
}
Expand All @@ -66,11 +67,13 @@ impl Connection {
#[inline]
pub fn new() -> Result<Connection, Error> {
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);
}
Expand All @@ -79,6 +82,7 @@ impl Connection {

Ok(Connection {
native_connection: Arc::new(NativeConnectionWrapper {
xlib,
x11_display,
x11_display_is_owned: true,
egl_display,
Expand All @@ -102,8 +106,10 @@ impl Connection {
pub unsafe fn from_native_connection(
native_connection: NativeConnection,
) -> Result<Connection, Error> {
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,
Expand All @@ -112,10 +118,12 @@ impl Connection {
}

fn from_x11_display(x11_display: *mut Display, is_owned: bool) -> Result<Connection, Error> {
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,
Expand Down Expand Up @@ -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,
}
Expand All @@ -281,14 +291,15 @@ impl NativeConnectionWrapper {
}

pub(crate) struct DisplayGuard<'a> {
xlib: &'a Xlib,
display: *mut Display,
phantom: PhantomData<&'a ()>,
}

impl<'a> Drop for DisplayGuard<'a> {
fn drop(&mut self) {
unsafe {
XUnlockDisplay(self.display);
(self.xlib.XUnlockDisplay)(self.display);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/unix/x11/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9a6c706

Please sign in to comment.