Allow usage of XWayland

Will prefer X11 over wayland when the environment variable
`WINIT_PREFER_UNIX_BACKEND=x11` is set.
This commit is contained in:
Alex Butler 2017-06-21 18:34:16 +01:00
parent a08347eef0
commit 9804cad7dd

View file

@ -2,6 +2,7 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::Arc; use std::sync::Arc;
use std::env;
use {CreationError, CursorState, EventsLoopClosed, MouseCursor, ControlFlow}; use {CreationError, CursorState, EventsLoopClosed, MouseCursor, ControlFlow};
use libc; use libc;
@ -15,6 +16,10 @@ mod dlopen;
pub mod wayland; pub mod wayland;
pub mod x11; 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)] #[derive(Clone, Default)]
pub struct PlatformSpecificWindowBuilderAttributes { pub struct PlatformSpecificWindowBuilderAttributes {
pub visual_infos: Option<XVisualInfo>, pub visual_infos: Option<XVisualInfo>,
@ -29,14 +34,26 @@ pub enum UnixBackend {
lazy_static!( lazy_static!(
pub static ref UNIX_BACKEND: UnixBackend = { pub static ref UNIX_BACKEND: UnixBackend = {
if let Some(ctxt) = wayland::WaylandContext::init() { #[inline]
UnixBackend::Wayland(Arc::new(ctxt)) fn x_backend() -> UnixBackend {
} else {
match XConnection::new(Some(x_error_callback)) { match XConnection::new(Some(x_error_callback)) {
Ok(x) => UnixBackend::X(Arc::new(x)), Ok(x) => UnixBackend::X(Arc::new(x)),
Err(e) => UnixBackend::Error(e), 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()
}
},
}
}; };
); );