From ace4808facf085c4ccd068d33b9ab9f018e9072e Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 4 May 2015 07:32:02 +0200 Subject: [PATCH] Remove the "headless" feature --- .travis.yml | 2 +- Cargo.toml | 4 +-- src/api/caca/mod.rs | 11 +++++-- src/api/cocoa/mod.rs | 3 -- src/api/osmesa/mod.rs | 22 ++++++++++--- src/platform/android/mod.rs | 4 --- src/platform/emscripten/mod.rs | 5 --- src/platform/linux/mod.rs | 56 ++++++++++++++++++++++++++++++++-- 8 files changed, 83 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1a0c82bd..c8c0ae3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ env: script: - cargo build --verbose - - cargo test --no-run --verbose + - cargo test --verbose - cargo test --verbose --features "headless" --no-default-features os: diff --git a/Cargo.toml b/Cargo.toml index 765089c6..4c936a67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,9 +50,9 @@ user32-sys = "0.1" kernel32-sys = "0.1" [target.i686-unknown-linux-gnu.dependencies] -osmesa-sys = "*" +osmesa-sys = "0.0.5" x11 = "*" [target.x86_64-unknown-linux-gnu.dependencies] -osmesa-sys = "*" +osmesa-sys = "0.0.5" x11 = "*" diff --git a/src/api/caca/mod.rs b/src/api/caca/mod.rs index da7810aa..1e3840c6 100644 --- a/src/api/caca/mod.rs +++ b/src/api/caca/mod.rs @@ -1,7 +1,7 @@ -#![cfg(all(any(target_os = "linux", target_os = "freebsd"), feature="headless"))] +#![cfg(any(target_os = "linux", target_os = "freebsd"))] use libc; -use api::osmesa::OsMesaContext; +use api::osmesa::{OsMesaContext, OsMesaCreationError}; use Api; use BuilderAttribs; @@ -83,7 +83,12 @@ impl<'a> Iterator for WaitEventsIterator<'a> { impl Window { pub fn new(builder: BuilderAttribs) -> Result { - let opengl = try!(OsMesaContext::new(builder)); + let opengl = match OsMesaContext::new(builder) { + Err(OsMesaCreationError::NotSupported) => return Err(CreationError::NotSupported), + Err(OsMesaCreationError::CreationError(e)) => return Err(e), + Ok(c) => c + }; + let opengl_dimensions = opengl.get_dimensions(); let libcaca = match ffi::LibCaca::open(&Path::new("libcaca.so.0")) { diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 645b9953..620230f3 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -1,6 +1,5 @@ #![cfg(target_os = "macos")] -#[cfg(feature = "headless")] pub use self::headless::HeadlessContext; use {CreationError, Event, MouseCursor, CursorState}; @@ -48,8 +47,6 @@ pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; mod monitor; mod event; - -#[cfg(feature = "headless")] mod headless; static mut shift_pressed: bool = false; diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index 61eebc10..22df72a8 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -1,11 +1,10 @@ -#![cfg(all(any(target_os = "linux", target_os = "freebsd"), feature="headless"))] +#![cfg(any(target_os = "linux", target_os = "freebsd"))] extern crate osmesa_sys; use Api; use BuilderAttribs; use CreationError; -use CreationError::OsError; use GlContext; use PixelFormat; use libc; @@ -19,8 +18,23 @@ pub struct OsMesaContext { height: u32, } +pub enum OsMesaCreationError { + CreationError(CreationError), + NotSupported, +} + +impl From for OsMesaCreationError { + fn from(e: CreationError) -> OsMesaCreationError { + OsMesaCreationError::CreationError(e) + } +} + impl OsMesaContext { - pub fn new(builder: BuilderAttribs) -> Result { + pub fn new(builder: BuilderAttribs) -> Result { + if let Err(_) = osmesa_sys::OsMesa::try_loading() { + return Err(OsMesaCreationError::NotSupported); + } + let dimensions = builder.dimensions.unwrap(); Ok(OsMesaContext { @@ -31,7 +45,7 @@ impl OsMesaContext { context: unsafe { let ctxt = osmesa_sys::OSMesaCreateContext(0x1908, ptr::null_mut()); if ctxt.is_null() { - return Err(OsError("OSMesaCreateContext failed".to_string())); + return Err(CreationError::OsError("OSMesaCreateContext failed".to_string()).into()); } ctxt } diff --git a/src/platform/android/mod.rs b/src/platform/android/mod.rs index 082608f4..a1b94167 100644 --- a/src/platform/android/mod.rs +++ b/src/platform/android/mod.rs @@ -2,10 +2,8 @@ pub use api::android::*; -#[cfg(feature = "headless")] pub struct HeadlessContext(i32); -#[cfg(feature = "headless")] impl HeadlessContext { /// See the docs in the crate root file. pub fn new(_builder: BuilderAttribs) -> Result { @@ -32,7 +30,5 @@ impl HeadlessContext { } } -#[cfg(feature = "headless")] unsafe impl Send for HeadlessContext {} -#[cfg(feature = "headless")] unsafe impl Sync for HeadlessContext {} diff --git a/src/platform/emscripten/mod.rs b/src/platform/emscripten/mod.rs index f9b011a3..56a7e9f6 100644 --- a/src/platform/emscripten/mod.rs +++ b/src/platform/emscripten/mod.rs @@ -5,10 +5,8 @@ use GlContext; pub use api::emscripten::{Window, WindowProxy, MonitorID, get_available_monitors}; pub use api::emscripten::{get_primary_monitor, WaitEventsIterator, PollEventsIterator}; -#[cfg(feature = "headless")] pub struct HeadlessContext(Window); -#[cfg(feature = "headless")] impl HeadlessContext { /// See the docs in the crate root file. pub fn new(builder: BuilderAttribs) -> Result { @@ -16,7 +14,6 @@ impl HeadlessContext { } } -#[cfg(feature = "headless")] impl GlContext for HeadlessContext { unsafe fn make_current(&self) { self.0.make_current() @@ -43,7 +40,5 @@ impl GlContext for HeadlessContext { } } -#[cfg(feature = "headless")] unsafe impl Send for HeadlessContext {} -#[cfg(feature = "headless")] unsafe impl Sync for HeadlessContext {} diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 63a0118d..1e12a80f 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,7 +1,13 @@ #![cfg(target_os = "linux")] -#[cfg(feature = "headless")] -pub use api::osmesa::OsMesaContext as HeadlessContext; +use Api; +use BuilderAttribs; +use CreationError; +use GlContext; +use PixelFormat; +use libc; + +use api::osmesa::{self, OsMesaContext}; #[cfg(feature = "window")] pub use api::x11::{Window, WindowProxy, MonitorID, get_available_monitors, get_primary_monitor}; @@ -12,3 +18,49 @@ pub use api::x11::{WaitEventsIterator, PollEventsIterator}; pub type Window = (); // TODO: hack to make things work #[cfg(not(feature = "window"))] pub type MonitorID = (); // TODO: hack to make things work + +pub struct HeadlessContext(OsMesaContext); + +impl HeadlessContext { + pub fn new(builder: BuilderAttribs) -> Result { + match OsMesaContext::new(builder) { + Ok(c) => return Ok(HeadlessContext(c)), + Err(osmesa::OsMesaCreationError::NotSupported) => (), + Err(osmesa::OsMesaCreationError::CreationError(e)) => return Err(e), + }; + + Err(CreationError::NotSupported) + } +} + +impl GlContext for HeadlessContext { + #[inline] + unsafe fn make_current(&self) { + self.0.make_current() + } + + #[inline] + fn is_current(&self) -> bool { + self.0.is_current() + } + + #[inline] + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { + self.0.get_proc_address(addr) + } + + #[inline] + fn swap_buffers(&self) { + self.0.swap_buffers() + } + + #[inline] + fn get_api(&self) -> Api { + self.0.get_api() + } + + #[inline] + fn get_pixel_format(&self) -> PixelFormat { + self.0.get_pixel_format() + } +}