From 82f5cd82729d12dbc8aec7e3bad3ed59dfd1f8e1 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 24 Dec 2015 10:57:08 +0100 Subject: Add better X error handling --- src/api/x11/xdisplay.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/api/x11/xdisplay.rs') 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, pub egl: Option, pub display: *mut ffi::Display, + pub latest_error: Mutex>, } 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 { -- cgit v1.2.3