aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/x11/xdisplay.rs
diff options
context:
space:
mode:
authorPierre Krieger <pierre.krieger1708@gmail.com>2015-12-24 10:57:08 +0100
committerPierre Krieger <pierre.krieger1708@gmail.com>2015-12-30 14:58:07 +0100
commit82f5cd82729d12dbc8aec7e3bad3ed59dfd1f8e1 (patch)
treedcfb1e3fd82e1a1df1e20969290e3073dd70505e /src/api/x11/xdisplay.rs
parent8236564a5224e38cd6e345df8ffaf343bfa24320 (diff)
downloadglutin-82f5cd82729d12dbc8aec7e3bad3ed59dfd1f8e1.tar.gz
glutin-82f5cd82729d12dbc8aec7e3bad3ed59dfd1f8e1.zip
Add better X error handling
Diffstat (limited to 'src/api/x11/xdisplay.rs')
-rw-r--r--src/api/x11/xdisplay.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/api/x11/xdisplay.rs b/src/api/x11/xdisplay.rs
index 8205c66..607406f 100644
--- a/src/api/x11/xdisplay.rs
+++ b/src/api/x11/xdisplay.rs
@@ -2,6 +2,7 @@ use std::ptr;
use std::fmt;
use std::error::Error;
use std::ffi::CString;
+use std::sync::Mutex;
use libc;
@@ -18,6 +19,7 @@ pub struct XConnection {
pub glx: Option<ffi::glx::Glx>,
pub egl: Option<Egl>,
pub display: *mut ffi::Display,
+ pub latest_error: Mutex<Option<XError>>,
}
unsafe impl Send for XConnection {}
@@ -87,8 +89,27 @@ impl XConnection {
glx: glx,
egl: egl,
display: display,
+ latest_error: Mutex::new(None),
})
}
+
+ /// Checks whether an error has been triggered by the previous function calls.
+ #[inline]
+ pub fn check_errors(&self) -> Result<(), XError> {
+ let error = self.latest_error.lock().unwrap().take();
+
+ if let Some(error) = error {
+ Err(error)
+ } else {
+ Ok(())
+ }
+ }
+
+ /// Ignores any previous error.
+ #[inline]
+ pub fn ignore_error(&self) {
+ *self.latest_error.lock().unwrap() = None;
+ }
}
impl Drop for XConnection {
@@ -98,6 +119,29 @@ impl Drop for XConnection {
}
}
+/// Error triggered by xlib.
+#[derive(Debug, Clone)]
+pub struct XError {
+ pub description: String,
+ pub error_code: u8,
+ pub request_code: u8,
+ pub minor_code: u8,
+}
+
+impl Error for XError {
+ #[inline]
+ fn description(&self) -> &str {
+ &self.description
+ }
+}
+
+impl fmt::Display for XError {
+ fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ write!(formatter, "X error: {} (code: {}, request code: {}, minor code: {})",
+ self.description, self.error_code, self.request_code, self.minor_code)
+ }
+}
+
/// Error returned if this system doesn't have XLib or can't create an X connection.
#[derive(Clone, Debug)]
pub enum XNotSupported {