Xwayland for the pen only when its X server answers; hand the chosen backend to the title bar and UI runtime

A DISPLAY left over from another session could force X11 with no X server to connect to, and
winit can't fall back to Wayland then. The title bar decision and the UI runtime read the
backend from the environment and saw Wayland while the window opened on Xwayland.
This commit is contained in:
vangel76 committed 2026-11-26 21:58:59 +01:00
1 parent 3a25b45ea6
commit c54e1eb75e
6 files changed
+44 -15

No files matched your search

Generated
+1
View File
@@ -5819,6 +5819,7 @@ dependencies = [
name = "texturecraft"
version = "0.6.1"
dependencies = [
"craft-host",
"craft-image-codecs",
"craft-menu",
"craft-update",
+2
View File
@@ -23,6 +23,8 @@ serde_json = { workspace = true }
log = { workspace = true }
# The message box for errors that end the app (a Windows release build has no console).
rfd = { workspace = true }
# The window's backend (Xwayland for a pen, src/xwayland_pen.rs) for the title bar and the UI runtime.
craft-host = { workspace = true }
# Reads the saved System title bar setting before the window opens (eframe's settings format).
ron = "0.12"
+9 -4
View File
@@ -395,7 +395,13 @@ fn main() {
prefs.apply_work_threads();
prefs.apply_resource_limits();
let session = initial_session(&args);
let frame_decision = TitleBar::decide(prefs.system_title_bar);
// winit picks Wayland whenever `WAYLAND_DISPLAY` is set; a pen works only through Xwayland there
#[cfg(target_os = "linux")]
let pen_needs_x11 = xwayland_pen::opens_through_xwayland();
#[cfg(not(target_os = "linux"))]
let pen_needs_x11 = false;
let backend = if pen_needs_x11 { craft_host::target::Backend::X11 } else { craft_host::target::Backend::from_env() };
let frame_decision = TitleBar::decide(prefs.system_title_bar, backend);
let title_bar = TitleBar::of(&frame_decision.frame);
let mut viewport = egui::ViewportBuilder::default()
.with_app_id(os_window::app_id())
@@ -407,9 +413,8 @@ fn main() {
}
let viewport = texturecraft_ui_egui::titlebar::viewport(&frame_decision, viewport);
let mut options = eframe::NativeOptions { viewport, renderer: eframe::Renderer::Wgpu, persistence_path, ..Default::default() };
// winit picks Wayland whenever `WAYLAND_DISPLAY` is set; a pen works only through Xwayland there
#[cfg(target_os = "linux")]
if xwayland_pen::opens_through_xwayland() {
if pen_needs_x11 {
use winit::platform::x11::EventLoopBuilderExtX11 as _;
options.event_loop_builder = Some(Box::new(|builder| {
builder.with_x11();
@@ -460,7 +465,7 @@ fn main() {
if let Err(e) = craft_menu::mac::install_app_menu(&cc.egui_ctx, window_name) {
log::warn!("the app menu could not be installed: {e}");
}
texturecraft_ui_egui::framework::install(&cc.egui_ctx, frame_decision)?;
texturecraft_ui_egui::framework::install(&cc.egui_ctx, frame_decision, backend)?;
let frame_guard = window_frame::service(cc, matches!(title_bar, TitleBar::App(_)));
let mut app = TextureCraftApp::new(cc, session);
app.title_bar = title_bar;
+26 -5
View File
@@ -31,11 +31,13 @@ const X11_LIBRARIES: &[&str] =
&["libX11.so.6", "libX11-xcb.so.1", "libxcb.so.1", "libXcursor.so.1", "libXi.so.6", "libxkbcommon.so.0", "libxkbcommon-x11.so.0"];
/// Whether the window opens through Xwayland: a pen is attached on a Wayland desktop and Xwayland
/// can run the window. Says so on stderr when it does.
/// can run the window (the X11 libraries are installed and the X server answers). Says so on
/// stderr when it does.
pub fn opens_through_xwayland() -> bool {
let var = |name: &str| std::env::var(name).ok();
let pen = || std::fs::read_to_string("/proc/bus/input/devices").is_ok_and(|devices| has_pen(&devices));
if !pen_needs_xwayland(var, pen, x11_libraries_available) {
let xwayland_ready = || x11_libraries_available() && var("DISPLAY").is_some_and(|display| x_server_answers(&display));
if !pen_needs_xwayland(var, pen, xwayland_ready) {
return false;
}
@@ -45,11 +47,29 @@ pub fn opens_through_xwayland() -> bool {
/// [`opens_through_xwayland`]'s decision. winit picks Wayland when `WAYLAND_DISPLAY` or
/// `WAYLAND_SOCKET` is set; Xwayland needs `DISPLAY`. The system probes run only on Wayland.
fn pen_needs_xwayland(var: impl Fn(&str) -> Option<String>, pen: impl FnOnce() -> bool, x11_libraries: impl FnOnce() -> bool) -> bool {
fn pen_needs_xwayland(var: impl Fn(&str) -> Option<String>, pen: impl FnOnce() -> bool, xwayland_ready: impl FnOnce() -> bool) -> bool {
let set = |name: &str| var(name).is_some_and(|value| !value.is_empty());
let wayland = set("WAYLAND_DISPLAY") || set("WAYLAND_SOCKET");
let keeps_wayland = var(NATIVE_WAYLAND_VARIABLE).is_some_and(|value| !value.is_empty() && value != "0");
wayland && !keeps_wayland && set("DISPLAY") && pen() && x11_libraries()
wayland && !keeps_wayland && set("DISPLAY") && pen() && xwayland_ready()
}
/// Does the X server `display` names accept a connection? winit can't fall back to Wayland once
/// X11 is chosen, so a `DISPLAY` left over from another session must keep the window on Wayland.
/// Only a local display is tried, by its socket path or the abstract socket of the same name.
fn x_server_answers(display: &str) -> bool {
use std::os::linux::net::SocketAddrExt as _;
use std::os::unix::net::{SocketAddr, UnixStream};
let Some(socket) = local_x_socket(display) else { return false };
UnixStream::connect(&socket).is_ok() || SocketAddr::from_abstract_name(socket.as_bytes()).and_then(|address| UnixStream::connect_addr(&address)).is_ok()
}
/// The socket of a local X display (`:0`, `:0.0`, `unix:0`); `None` for a remote one
fn local_x_socket(display: &str) -> Option<String> {
let number = display.strip_prefix(':').or_else(|| display.strip_prefix("unix:"))?;
let number: u32 = number.split('.').next()?.parse().ok()?;
Some(format!("/tmp/.X11-unix/X{number}"))
}
/// Is a pen among the input devices `/proc/bus/input/devices` lists? A pen's `B: KEY=` bitmap has
@@ -141,10 +161,11 @@ mod tests {
assert!(pen_needs_xwayland(env(WAYLAND), || true, || true));
assert!(!pen_needs_xwayland(env(WAYLAND), || false, || true), "no pen");
assert!(!pen_needs_xwayland(env(WAYLAND), || true, || false), "no X11 libraries");
assert!(!pen_needs_xwayland(env(WAYLAND), || true, || false), "Xwayland can't run the window");
assert!(!pen_needs_xwayland(env(&[("WAYLAND_DISPLAY", "wayland-0")]), || true, || true), "no Xwayland");
assert!(!pen_needs_xwayland(env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0"), (NATIVE_WAYLAND_VARIABLE, "1")]), || true, || true), "kept");
assert!(pen_needs_xwayland(env(&[("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0"), (NATIVE_WAYLAND_VARIABLE, "0")]), || true, || true));
assert!(!pen_needs_xwayland(env(&[("DISPLAY", ":0")]), || panic!("probed off Wayland"), || panic!("probed off Wayland")), "X11 already has the pen");
assert_eq!([":1.0", "unix:0", "remote:0"].map(local_x_socket), [Some("/tmp/.X11-unix/X1".into()), Some("/tmp/.X11-unix/X0".into()), None]);
}
}
+3 -3
View File
@@ -52,10 +52,10 @@ fn install_with(ctx: &egui::Context, shell: ShellInfo, windows: Arc<dyn WindowSy
}
/// Installs the runtime on the desktop window's context, once, before the first pass, with the window's `frame`
/// ([`crate::titlebar::TitleBar::decide`]).
/// ([`crate::titlebar::TitleBar::decide`]) and the `backend` the window opened on.
#[cfg(not(target_arch = "wasm32"))]
pub fn install(ctx: &egui::Context, frame: Decision) -> Result<(), String> {
let shell = ShellInfo::default().backend(craft_host::target::Backend::from_env());
pub fn install(ctx: &egui::Context, frame: Decision, backend: craft_host::target::Backend) -> Result<(), String> {
let shell = ShellInfo::default().backend(backend);
install_with(ctx, shell, Arc::new(craft_window::system::SystemWindows), Some(frame))
}
+3 -3
View File
@@ -68,12 +68,12 @@ const NIGHTLY_ICONS: [(f32, &[u8]); 5] = [
impl TitleBar {
/// Decide who draws the title bar; call once at startup, before the window is created, and
/// hand the decision to the window ([`viewport`]) and the runtime (`framework::install`).
/// `system` is Settings › Interface › System title bar.
pub fn decide(system: bool) -> Decision {
/// `system` is Settings › Interface › System title bar; `backend` is the one the window opens on.
pub fn decide(system: bool, backend: craft_host::target::Backend) -> Decision {
let choice = craft_window::Choice::probe(PREFIX);
let wanted = Self::from_choice(choice.frame(), choice.overridden(), system);
let pref = if wanted == TitleBar::System { TitleBarPref::System } else { TitleBarPref::Integrated };
craft_window::policy::decide_for(PREFIX, craft_host::target::Backend::from_env(), pref)
craft_window::policy::decide_for(PREFIX, backend, pref)
}
/// The bar a decided `frame` gives.