Skip to content

Commit

Permalink
Add cargo features for X11 and Wayland
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoffart authored Oct 7, 2020
1 parent e3553a5 commit 1977fdd
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- X11 and Wayland are now optional features (enabled by default)

# Version 0.25.0 (2020-10-02)

- Updated winit dependency to 0.23.0. See [winit's CHANGELOG](https://github.com/rust-windowing/winit/blob/master/CHANGELOG.md#0230-2020-10-02) for more info.
Expand Down
11 changes: 7 additions & 4 deletions glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ features = ["serde"]

[features]
serde = ["winit/serde"]
x11 = ["winit/x11", "glutin_glx_sys"]
wayland = ["winit/wayland", "wayland-client", "wayland-egl"]
default = ["x11", "wayland"]

[dependencies]
lazy_static = "1.3"
winit = "0.23.0"
winit = { version = "0.23.0", default-features = false }

[target.'cfg(target_os = "android")'.dependencies]
android_glue = "0.2"
Expand Down Expand Up @@ -54,10 +57,10 @@ parking_lot = "0.11"

[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
osmesa-sys = "0.1"
wayland-client = { version = "0.28", features = ["dlopen"] }
wayland-egl = "0.28"
wayland-client = { version = "0.28", features = ["dlopen"], optional = true }
wayland-egl = { version = "0.28", optional = true }
libloading = "0.6.1"
glutin_egl_sys = { version = "0.1.4", path = "../glutin_egl_sys" }
glutin_glx_sys = { version = "0.1.6", path = "../glutin_glx_sys" }
glutin_glx_sys = { version = "0.1.6", path = "../glutin_glx_sys", optional = true }
parking_lot = "0.11"
log = "0.4"
2 changes: 2 additions & 0 deletions glutin/src/api/egl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ pub struct ContextPrototype<'a> {
target_os = "netbsd",
target_os = "openbsd",
))]
#[cfg(feature = "x11")]
pub fn get_native_visual_id(
display: ffi::egl::types::EGLDisplay,
config_id: ffi::egl::types::EGLConfig,
Expand Down Expand Up @@ -894,6 +895,7 @@ impl<'a> ContextPrototype<'a> {
target_os = "netbsd",
target_os = "openbsd",
))]
#[cfg(feature = "x11")]
pub fn get_native_visual_id(&self) -> ffi::egl::types::EGLint {
get_native_visual_id(self.display, self.config_id)
}
Expand Down
1 change: 1 addition & 0 deletions glutin/src/api/glx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
target_os = "netbsd",
target_os = "openbsd",
))]
#![cfg(feature = "x11")]

mod make_current_guard;
mod glx {
Expand Down
1 change: 1 addition & 0 deletions glutin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl CreationError {
target_os = "netbsd",
target_os = "openbsd",
))]
#[cfg(feature = "x11")]
pub(crate) fn append(self, err: CreationError) -> Self {
match self {
CreationError::CreationErrors(mut errs) => {
Expand Down
1 change: 1 addition & 0 deletions glutin/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::platform::ContextTraitExt;
pub use crate::platform_impl::{HeadlessContextExt, RawContextExt, RawHandle};
use crate::{Context, ContextCurrentState};
pub use glutin_egl_sys::EGLContext;
#[cfg(feature = "x11")]
pub use glutin_glx_sys::GLXContext;

pub use winit::platform::unix::*;
Expand Down
75 changes: 65 additions & 10 deletions glutin/src/platform_impl/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
target_os = "openbsd",
))]

#[cfg(not(any(feature = "x11", feature = "wayland")))]
compile_error!(
"at least one of the 'x11' or 'wayland' features must be enabled"
);

mod wayland;
mod x11;

#[cfg(feature = "x11")]
use self::x11::X11Context;
use crate::api::osmesa;
use crate::{
Api, ContextCurrentState, ContextError, CreationError, GlAttributes,
NotCurrent, PixelFormat, PixelFormatRequirements, Rect,
};
#[cfg(feature = "x11")]
pub use x11::utils as x11_utils;

#[cfg(feature = "x11")]
use crate::platform::unix::x11::XConnection;
use crate::platform::unix::EventLoopWindowTargetExtUnix;
use winit::dpi;
Expand All @@ -25,27 +33,33 @@ use winit::window::{Window, WindowBuilder};

use std::marker::PhantomData;
use std::os::raw;
#[cfg(feature = "x11")]
use std::sync::Arc;

/// Context handles available on Unix-like platforms.
#[derive(Clone, Debug)]
pub enum RawHandle {
/// Context handle for a glx context.
#[cfg(feature = "x11")]
Glx(glutin_glx_sys::GLXContext),
/// Context handle for a egl context.
Egl(glutin_egl_sys::EGLContext),
}

#[derive(Debug)]
pub enum ContextType {
#[cfg(feature = "x11")]
X11,
#[cfg(feature = "wayland")]
Wayland,
OsMesa,
}

#[derive(Debug)]
pub enum Context {
#[cfg(feature = "x11")]
X11(x11::Context),
#[cfg(feature = "wayland")]
Wayland(wayland::Context),
OsMesa(osmesa::OsMesaContext),
}
Expand All @@ -66,6 +80,7 @@ impl Context {
));
}
},
#[cfg(feature = "x11")]
ContextType::X11 => match *c {
Context::X11(_) => Ok(()),
_ => {
Expand All @@ -75,6 +90,7 @@ impl Context {
));
}
},
#[cfg(feature = "wayland")]
ContextType::Wayland => match *c {
Context::Wayland(_) => Ok(()),
_ => {
Expand All @@ -97,24 +113,28 @@ impl Context {
pf_reqs: &PixelFormatRequirements,
gl_attr: &GlAttributes<&Context>,
) -> Result<(Window, Self), CreationError> {
#[cfg(feature = "wayland")]
if el.is_wayland() {
Context::is_compatible(&gl_attr.sharing, ContextType::Wayland)?;

let gl_attr = gl_attr.clone().map_sharing(|ctx| match *ctx {
Context::Wayland(ref ctx) => ctx,
_ => unreachable!(),
});
wayland::Context::new(wb, el, pf_reqs, &gl_attr)
.map(|(win, context)| (win, Context::Wayland(context)))
} else {
return wayland::Context::new(wb, el, pf_reqs, &gl_attr)
.map(|(win, context)| (win, Context::Wayland(context)));
}
#[cfg(feature = "x11")]
if el.is_x11() {
Context::is_compatible(&gl_attr.sharing, ContextType::X11)?;
let gl_attr = gl_attr.clone().map_sharing(|ctx| match *ctx {
Context::X11(ref ctx) => ctx,
_ => unreachable!(),
});
x11::Context::new(wb, el, pf_reqs, &gl_attr)
.map(|(win, context)| (win, Context::X11(context)))
return x11::Context::new(wb, el, pf_reqs, &gl_attr)
.map(|(win, context)| (win, Context::X11(context)));
}
panic!("glutin was not compiled with support for this display server")
}

#[inline]
Expand All @@ -133,29 +153,37 @@ impl Context {
gl_attr: &GlAttributes<&Context>,
size: Option<dpi::PhysicalSize<u32>>,
) -> Result<Self, CreationError> {
#[cfg(feature = "wayland")]
if el.is_wayland() {
Context::is_compatible(&gl_attr.sharing, ContextType::Wayland)?;
let gl_attr = gl_attr.clone().map_sharing(|ctx| match *ctx {
Context::Wayland(ref ctx) => ctx,
_ => unreachable!(),
});
wayland::Context::new_headless(&el, pf_reqs, &gl_attr, size)
.map(|ctx| Context::Wayland(ctx))
} else {
return wayland::Context::new_headless(
&el, pf_reqs, &gl_attr, size,
)
.map(|ctx| Context::Wayland(ctx));
}
#[cfg(feature = "x11")]
if el.is_x11() {
Context::is_compatible(&gl_attr.sharing, ContextType::X11)?;
let gl_attr = gl_attr.clone().map_sharing(|ctx| match *ctx {
Context::X11(ref ctx) => ctx,
_ => unreachable!(),
});
x11::Context::new_headless(&el, pf_reqs, &gl_attr, size)
.map(|ctx| Context::X11(ctx))
return x11::Context::new_headless(&el, pf_reqs, &gl_attr, size)
.map(|ctx| Context::X11(ctx));
}
panic!("glutin was not compiled with support for this display server")
}

#[inline]
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.make_current(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.make_current(),
Context::OsMesa(ref ctx) => ctx.make_current(),
}
Expand All @@ -164,7 +192,9 @@ impl Context {
#[inline]
pub unsafe fn make_not_current(&self) -> Result<(), ContextError> {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.make_not_current(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.make_not_current(),
Context::OsMesa(ref ctx) => ctx.make_not_current(),
}
Expand All @@ -173,7 +203,9 @@ impl Context {
#[inline]
pub fn is_current(&self) -> bool {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.is_current(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.is_current(),
Context::OsMesa(ref ctx) => ctx.is_current(),
}
Expand All @@ -182,7 +214,9 @@ impl Context {
#[inline]
pub fn get_api(&self) -> Api {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.get_api(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.get_api(),
Context::OsMesa(ref ctx) => ctx.get_api(),
}
Expand All @@ -191,10 +225,12 @@ impl Context {
#[inline]
pub unsafe fn raw_handle(&self) -> RawHandle {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => match *ctx.raw_handle() {
X11Context::Glx(ref ctx) => RawHandle::Glx(ctx.raw_handle()),
X11Context::Egl(ref ctx) => RawHandle::Egl(ctx.raw_handle()),
},
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => RawHandle::Egl(ctx.raw_handle()),
Context::OsMesa(ref ctx) => RawHandle::Egl(ctx.raw_handle()),
}
Expand All @@ -203,16 +239,21 @@ impl Context {
#[inline]
pub unsafe fn get_egl_display(&self) -> Option<*const raw::c_void> {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.get_egl_display(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.get_egl_display(),
_ => None,
}
}

#[inline]
pub fn resize(&self, width: u32, height: u32) {
#![allow(unused)]
match *self {
#[cfg(feature = "x11")]
Context::X11(_) => (),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.resize(width, height),
_ => unreachable!(),
}
Expand All @@ -221,7 +262,9 @@ impl Context {
#[inline]
pub fn get_proc_address(&self, addr: &str) -> *const core::ffi::c_void {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.get_proc_address(addr),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.get_proc_address(addr),
Context::OsMesa(ref ctx) => ctx.get_proc_address(addr),
}
Expand All @@ -230,7 +273,9 @@ impl Context {
#[inline]
pub fn swap_buffers(&self) -> Result<(), ContextError> {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.swap_buffers(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.swap_buffers(),
_ => unreachable!(),
}
Expand All @@ -242,7 +287,9 @@ impl Context {
rects: &[Rect],
) -> Result<(), ContextError> {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.swap_buffers_with_damage(rects),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.swap_buffers_with_damage(rects),
_ => unreachable!(),
}
Expand All @@ -251,7 +298,9 @@ impl Context {
#[inline]
pub fn swap_buffers_with_damage_supported(&self) -> bool {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.swap_buffers_with_damage_supported(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => {
ctx.swap_buffers_with_damage_supported()
}
Expand All @@ -262,7 +311,9 @@ impl Context {
#[inline]
pub fn get_pixel_format(&self) -> PixelFormat {
match *self {
#[cfg(feature = "x11")]
Context::X11(ref ctx) => ctx.get_pixel_format(),
#[cfg(feature = "wayland")]
Context::Wayland(ref ctx) => ctx.get_pixel_format(),
_ => unreachable!(),
}
Expand Down Expand Up @@ -359,6 +410,7 @@ pub trait RawContextExt {
/// Unsafe behaviour might happen if you:
/// - Provide us with invalid parameters.
/// - The surface/display_ptr is destroyed before the context
#[cfg(feature = "wayland")]
unsafe fn build_raw_wayland_context(
self,
display_ptr: *const wayland::wl_display,
Expand All @@ -374,6 +426,7 @@ pub trait RawContextExt {
/// Unsafe behaviour might happen if you:
/// - Provide us with invalid parameters.
/// - The xwin is destroyed before the context
#[cfg(feature = "x11")]
unsafe fn build_raw_x11_context(
self,
xconn: Arc<XConnection>,
Expand All @@ -387,6 +440,7 @@ impl<'a, T: ContextCurrentState> RawContextExt
for crate::ContextBuilder<'a, T>
{
#[inline]
#[cfg(feature = "wayland")]
unsafe fn build_raw_wayland_context(
self,
display_ptr: *const wayland::wl_display,
Expand Down Expand Up @@ -424,6 +478,7 @@ impl<'a, T: ContextCurrentState> RawContextExt
}

#[inline]
#[cfg(feature = "x11")]
unsafe fn build_raw_x11_context(
self,
xconn: Arc<XConnection>,
Expand Down
2 changes: 2 additions & 0 deletions glutin/src/platform_impl/unix/wayland.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "wayland")]

use crate::api::egl::{
Context as EglContext, NativeDisplay, SurfaceType as EglSurfaceType,
};
Expand Down
2 changes: 2 additions & 0 deletions glutin/src/platform_impl/unix/x11.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "x11")]

use crate::api::egl::{
self, Context as EglContext, NativeDisplay, SurfaceType as EglSurfaceType,
EGL,
Expand Down

0 comments on commit 1977fdd

Please sign in to comment.