diff options
author | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-09-20 08:42:32 +0200 |
---|---|---|
committer | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-09-20 08:42:32 +0200 |
commit | b9a4f5fbe90a94fbca2e6816b92a129dabfe81fd (patch) | |
tree | 0a825a6212ea192840276d7bb4214322ceb4d209 | |
parent | 27385894ac81d28dd984764c65914ca512043c16 (diff) | |
download | glutin-b9a4f5fbe90a94fbca2e6816b92a129dabfe81fd.tar.gz glutin-b9a4f5fbe90a94fbca2e6816b92a129dabfe81fd.zip |
Err when a X connection cannot be created instead of panicking
-rw-r--r-- | src/api/x11/mod.rs | 2 | ||||
-rw-r--r-- | src/api/x11/xdisplay.rs | 22 | ||||
-rw-r--r-- | src/lib.rs | 11 | ||||
-rw-r--r-- | src/platform/linux/api_dispatch.rs | 25 |
4 files changed, 48 insertions, 12 deletions
diff --git a/src/api/x11/mod.rs b/src/api/x11/mod.rs index 39b99f3..a7c997b 100644 --- a/src/api/x11/mod.rs +++ b/src/api/x11/mod.rs @@ -2,7 +2,7 @@ pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; pub use self::window::{Window, XWindow, PollEventsIterator, WaitEventsIterator, Context, WindowProxy}; -pub use self::xdisplay::XConnection; +pub use self::xdisplay::{XConnection, XNotSupported}; pub mod ffi; diff --git a/src/api/x11/xdisplay.rs b/src/api/x11/xdisplay.rs index c960c00..d48d947 100644 --- a/src/api/x11/xdisplay.rs +++ b/src/api/x11/xdisplay.rs @@ -1,4 +1,6 @@ use std::ptr; +use std::fmt; +use std::error::Error; use std::ffi::CString; use libc; @@ -21,10 +23,6 @@ pub struct XConnection { unsafe impl Send for XConnection {} unsafe impl Sync for XConnection {} -/// Error returned if this system doesn't have XLib or can't create an X connection. -#[derive(Copy, Clone, Debug)] -pub struct XNotSupported; - impl XConnection { pub fn new() -> Result<XConnection, XNotSupported> { // opening the libraries @@ -104,3 +102,19 @@ impl Drop for XConnection { unsafe { (self.xlib.XCloseDisplay)(self.display) }; } } + +/// Error returned if this system doesn't have XLib or can't create an X connection. +#[derive(Copy, Clone, Debug)] +pub struct XNotSupported; + +impl Error for XNotSupported { + fn description(&self) -> &str { + "The X windowing system could not be initialized" + } +} + +impl fmt::Display for XNotSupported { + fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { + formatter.write_str(self.description()) + } +} @@ -106,11 +106,12 @@ pub trait GlContext { } /// Error that can happen while creating a window or a headless renderer. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Debug)] pub enum CreationError { OsError(String), /// TODO: remove this error NotSupported, + NoBackendAvailable(Box<std::error::Error + Send>), RobustnessNotSupported, OpenGlVersionNotSupported, NoAvailablePixelFormat, @@ -121,6 +122,7 @@ impl CreationError { match *self { CreationError::OsError(ref text) => &text, CreationError::NotSupported => "Some of the requested attributes are not supported", + CreationError::NoBackendAvailable(_) => "No backend is available", CreationError::RobustnessNotSupported => "You requested robustness, but it is \ not supported.", CreationError::OpenGlVersionNotSupported => "The requested OpenGL version is not \ @@ -141,6 +143,13 @@ impl std::error::Error for CreationError { fn description(&self) -> &str { self.to_string() } + + fn cause(&self) -> Option<&std::error::Error> { + match *self { + CreationError::NoBackendAvailable(ref err) => Some(&**err), + _ => None + } + } } /// Error that can happen when manipulating an OpenGL context. diff --git a/src/platform/linux/api_dispatch.rs b/src/platform/linux/api_dispatch.rs index e580ab1..f25bda9 100644 --- a/src/platform/linux/api_dispatch.rs +++ b/src/platform/linux/api_dispatch.rs @@ -19,10 +19,12 @@ use libc; use api::wayland; use api::x11; use api::x11::XConnection; +use api::x11::XNotSupported; enum Backend { X(Arc<XConnection>), - Wayland + Wayland, + Error(XNotSupported), } lazy_static!( @@ -31,7 +33,10 @@ lazy_static!( if false && wayland::is_available() { Backend::Wayland } else { - Backend::X(Arc::new(XConnection::new().unwrap())) + match XConnection::new() { + Ok(x) => Backend::X(Arc::new(x)), + Err(e) => Backend::Error(e), + } } }; ); @@ -65,7 +70,9 @@ pub enum MonitorID { #[doc(hidden)] X(x11::MonitorID), #[doc(hidden)] - Wayland(wayland::MonitorID) + Wayland(wayland::MonitorID), + #[doc(hidden)] + None, } pub fn get_available_monitors() -> VecDeque<MonitorID> { @@ -78,6 +85,7 @@ pub fn get_available_monitors() -> VecDeque<MonitorID> { .into_iter() .map(MonitorID::X) .collect(), + Backend::Error(_) => { let mut d = VecDeque::new(); d.push_back(MonitorID::None); d}, } } @@ -85,6 +93,7 @@ pub fn get_primary_monitor() -> MonitorID { match *BACKEND { Backend::Wayland => MonitorID::Wayland(wayland::get_primary_monitor()), Backend::X(ref connec) => MonitorID::X(x11::get_primary_monitor(connec)), + Backend::Error(_) => MonitorID::None, } } @@ -92,21 +101,24 @@ impl MonitorID { pub fn get_name(&self) -> Option<String> { match self { &MonitorID::X(ref m) => m.get_name(), - &MonitorID::Wayland(ref m) => m.get_name() + &MonitorID::Wayland(ref m) => m.get_name(), + &MonitorID::None => None, } } pub fn get_native_identifier(&self) -> ::native_monitor::NativeMonitorId { match self { &MonitorID::X(ref m) => m.get_native_identifier(), - &MonitorID::Wayland(ref m) => m.get_native_identifier() + &MonitorID::Wayland(ref m) => m.get_native_identifier(), + &MonitorID::None => unimplemented!() // FIXME: } } pub fn get_dimensions(&self) -> (u32, u32) { match self { &MonitorID::X(ref m) => m.get_dimensions(), - &MonitorID::Wayland(ref m) => m.get_dimensions() + &MonitorID::Wayland(ref m) => m.get_dimensions(), + &MonitorID::None => (800, 600), // FIXME: } } } @@ -153,6 +165,7 @@ impl Window { match *BACKEND { Backend::Wayland => wayland::Window::new(builder).map(Window::Wayland), Backend::X(ref connec) => x11::Window::new(connec, builder).map(Window::X), + Backend::Error(ref error) => Err(CreationError::NoBackendAvailable(Box::new(error.clone()))) } } |