From 9804cad7dd86a3661bc9f3996f8fc1a84e7af5ab Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Wed, 21 Jun 2017 18:34:16 +0100 Subject: [PATCH] Allow usage of XWayland Will prefer X11 over wayland when the environment variable `WINIT_PREFER_UNIX_BACKEND=x11` is set. --- src/platform/linux/mod.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 6bc4719d..f914a2b0 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -2,6 +2,7 @@ use std::collections::VecDeque; use std::sync::Arc; +use std::env; use {CreationError, CursorState, EventsLoopClosed, MouseCursor, ControlFlow}; use libc; @@ -15,6 +16,10 @@ mod dlopen; pub mod wayland; pub mod x11; +/// Environment variable that indicates the X11 backend should be used +/// even if Wayland is available. In this case XWayland will be used. +const BACKEND_PREFERENCE_ENV_VAR: &str = "WINIT_PREFER_UNIX_BACKEND"; + #[derive(Clone, Default)] pub struct PlatformSpecificWindowBuilderAttributes { pub visual_infos: Option, @@ -25,18 +30,30 @@ pub enum UnixBackend { X(Arc), Wayland(Arc), Error(XNotSupported), -} +} lazy_static!( pub static ref UNIX_BACKEND: UnixBackend = { - if let Some(ctxt) = wayland::WaylandContext::init() { - UnixBackend::Wayland(Arc::new(ctxt)) - } else { + #[inline] + fn x_backend() -> UnixBackend { match XConnection::new(Some(x_error_callback)) { Ok(x) => UnixBackend::X(Arc::new(x)), Err(e) => UnixBackend::Error(e), } } + match env::var(BACKEND_PREFERENCE_ENV_VAR) { + Ok(ref s) if s == "x11" => { + println!("{}: using x11 backend", BACKEND_PREFERENCE_ENV_VAR); + x_backend() + }, + _ => { + if let Some(ctxt) = wayland::WaylandContext::init() { + UnixBackend::Wayland(Arc::new(ctxt)) + } else { + x_backend() + } + }, + } }; );