Skip to content

Commit 4a08505

Browse files
authored
feat: add x11 feature flag to allow disabling x11 dependencies (#1103)
1 parent 986f07a commit 4a08505

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

.changes/x11-feature.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
tao: minor
3+
---
4+
5+
Added `x11` feature flag (enabled by default).

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ documentation = "https://docs.rs/tao"
1616
categories = [ "gui" ]
1717

1818
[package.metadata.docs.rs]
19-
features = [ "rwh_04", "rwh_05", "rwh_06", "serde" ]
19+
features = [ "rwh_04", "rwh_05", "rwh_06", "serde", "x11" ]
2020
default-target = "x86_64-unknown-linux-gnu"
2121
targets = [
2222
"i686-pc-windows-msvc",
@@ -27,11 +27,12 @@ targets = [
2727
]
2828

2929
[features]
30-
default = [ "rwh_06" ]
30+
default = [ "rwh_06", "x11" ]
3131
serde = [ "dep:serde", "dpi/serde" ]
3232
rwh_04 = [ "dep:rwh_04" ]
3333
rwh_05 = [ "dep:rwh_05" ]
3434
rwh_06 = [ "dep:rwh_06" ]
35+
x11 = [ "dep:gdkx11-sys", "dep:x11-dl" ]
3536

3637
[workspace]
3738
members = [ "tao-macros" ]
@@ -147,8 +148,8 @@ scopeguard = "1.2"
147148

148149
[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
149150
gtk = "0.18"
150-
gdkx11-sys = "0.18"
151+
gdkx11-sys = { version = "0.18", optional = true }
151152
gdkwayland-sys = "0.18.0"
152-
x11-dl = "2.21"
153+
x11-dl = { version = "2.21", optional = true }
153154
parking_lot = "0.12"
154155
dlopen2 = "0.7.0"

src/platform/unix.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@
1010
target_os = "openbsd"
1111
))]
1212

13+
#[cfg(feature = "x11")]
1314
use std::{os::raw::c_int, sync::Arc};
1415

1516
// XConnection utilities
1617
#[doc(hidden)]
18+
#[cfg(feature = "x11")]
1719
pub use crate::platform_impl::x11;
1820

21+
#[cfg(feature = "x11")]
22+
use crate::platform_impl::x11::xdisplay::XError;
1923
pub use crate::platform_impl::EventLoop as UnixEventLoop;
2024
use crate::{
2125
error::{ExternalError, OsError},
2226
event_loop::{EventLoopBuilder, EventLoopWindowTarget},
2327
monitor::MonitorHandle,
24-
platform_impl::{x11::xdisplay::XError, Parent, Window as UnixWindow},
28+
platform_impl::{Parent, Window as UnixWindow},
2529
window::{Window, WindowBuilder},
2630
};
2731

32+
#[cfg(feature = "x11")]
2833
use self::x11::xdisplay::XConnection;
2934

3035
/// Additional methods on `EventLoop` that are specific to Unix.
@@ -201,8 +206,10 @@ pub trait EventLoopWindowTargetExtUnix {
201206
fn is_wayland(&self) -> bool;
202207

203208
/// True if the `EventLoopWindowTarget` uses X11.
209+
#[cfg(feature = "x11")]
204210
fn is_x11(&self) -> bool;
205211

212+
#[cfg(feature = "x11")]
206213
fn xlib_xconnection(&self) -> Option<Arc<XConnection>>;
207214

208215
// /// Returns a pointer to the `wl_display` object of wayland that is used by this
@@ -226,11 +233,13 @@ impl<T> EventLoopWindowTargetExtUnix for EventLoopWindowTarget<T> {
226233
self.p.is_wayland()
227234
}
228235

236+
#[cfg(feature = "x11")]
229237
#[inline]
230238
fn is_x11(&self) -> bool {
231239
!self.p.is_wayland()
232240
}
233241

242+
#[cfg(feature = "x11")]
234243
#[inline]
235244
fn xlib_xconnection(&self) -> Option<Arc<XConnection>> {
236245
if self.is_x11() {
@@ -266,6 +275,7 @@ impl<T> EventLoopWindowTargetExtUnix for EventLoopWindowTarget<T> {
266275
}
267276
}
268277

278+
#[cfg(feature = "x11")]
269279
unsafe extern "C" fn x_error_callback(
270280
_display: *mut x11::ffi::Display,
271281
event: *mut x11::ffi::XErrorEvent,

src/platform_impl/linux/event_loop.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use gtk::{
2424
Settings,
2525
};
2626

27+
#[cfg(feature = "x11")]
28+
use crate::platform_impl::platform::device;
2729
use crate::{
2830
dpi::{LogicalPosition, LogicalSize, PhysicalPosition},
2931
error::ExternalError,
@@ -33,7 +35,7 @@ use crate::{
3335
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
3436
keyboard::ModifiersState,
3537
monitor::MonitorHandle as RootMonitorHandle,
36-
platform_impl::platform::{device, DEVICE_ID},
38+
platform_impl::platform::DEVICE_ID,
3739
window::{
3840
CursorIcon, Fullscreen, ProgressBarState, ResizeDirection, Theme, WindowId as RootWindowId,
3941
},
@@ -123,6 +125,7 @@ impl<T> EventLoopWindowTarget<T> {
123125
let display_handle = rwh_06::WaylandDisplayHandle::new(display);
124126
Ok(rwh_06::RawDisplayHandle::Wayland(display_handle))
125127
} else {
128+
#[cfg(feature = "x11")]
126129
unsafe {
127130
if let Ok(xlib) = x11_dl::xlib::Xlib::open() {
128131
let display = (xlib.XOpenDisplay)(std::ptr::null());
@@ -134,13 +137,16 @@ impl<T> EventLoopWindowTarget<T> {
134137
Err(rwh_06::HandleError::Unavailable)
135138
}
136139
}
140+
#[cfg(not(feature = "x11"))]
141+
Err(rwh_06::HandleError::Unavailable)
137142
}
138143
}
139144

140145
pub fn is_wayland(&self) -> bool {
141146
self.display.backend().is_wayland()
142147
}
143148

149+
#[cfg(feature = "x11")]
144150
pub fn is_x11(&self) -> bool {
145151
self.display.backend().is_x11()
146152
}
@@ -249,6 +255,7 @@ impl<T: 'static> EventLoop<T> {
249255
};
250256

251257
// Spawn x11 thread to receive Device events.
258+
#[cfg(feature = "x11")]
252259
let run_device_thread = if window_target.is_x11() {
253260
let (device_tx, device_rx) = glib::MainContext::channel(glib::Priority::default());
254261
let user_event_tx = user_event_tx.clone();
@@ -272,6 +279,8 @@ impl<T: 'static> EventLoop<T> {
272279
} else {
273280
None
274281
};
282+
#[cfg(not(feature = "x11"))]
283+
let run_device_thread = None;
275284

276285
let mut taskbar = TaskbarIndicator::new();
277286
let is_wayland = window_target.is_wayland();

src/platform_impl/linux/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
33
// SPDX-License-Identifier: Apache-2.0
44

5+
#[cfg(feature = "x11")]
56
mod device;
67
mod event_loop;
78
mod icon;
@@ -13,6 +14,7 @@ mod window;
1314

1415
pub mod taskbar;
1516
pub mod wayland;
17+
#[cfg(feature = "x11")]
1618
pub mod x11;
1719

1820
pub use self::keycode::{keycode_from_scancode, keycode_to_scancode};

src/platform_impl/linux/window.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,14 @@ impl Window {
952952
let window_handle = rwh_06::WaylandWindowHandle::new(surface);
953953
Ok(rwh_06::RawWindowHandle::Wayland(window_handle))
954954
} else {
955-
let xid = unsafe { gdk_x11_sys::gdk_x11_window_get_xid(window.as_ptr() as *mut _) };
956-
let window_handle = rwh_06::XlibWindowHandle::new(xid);
957-
Ok(rwh_06::RawWindowHandle::Xlib(window_handle))
955+
#[cfg(feature = "x11")]
956+
{
957+
let xid = unsafe { gdk_x11_sys::gdk_x11_window_get_xid(window.as_ptr() as *mut _) };
958+
let window_handle = rwh_06::XlibWindowHandle::new(xid);
959+
Ok(rwh_06::RawWindowHandle::Xlib(window_handle))
960+
}
961+
#[cfg(not(feature = "x11"))]
962+
Err(rwh_06::HandleError::Unavailable)
958963
}
959964
} else {
960965
Err(rwh_06::HandleError::Unavailable)
@@ -972,6 +977,7 @@ impl Window {
972977
let display_handle = rwh_06::WaylandDisplayHandle::new(display);
973978
Ok(rwh_06::RawDisplayHandle::Wayland(display_handle))
974979
} else {
980+
#[cfg(feature = "x11")]
975981
if let Ok(xlib) = x11_dl::xlib::Xlib::open() {
976982
unsafe {
977983
let display = (xlib.XOpenDisplay)(std::ptr::null());
@@ -983,6 +989,8 @@ impl Window {
983989
} else {
984990
Err(rwh_06::HandleError::Unavailable)
985991
}
992+
#[cfg(not(feature = "x11"))]
993+
Err(rwh_06::HandleError::Unavailable)
986994
}
987995
}
988996

0 commit comments

Comments
 (0)