2015-05-17 19:19:06 +10:00
|
|
|
use std::ptr;
|
2015-09-20 16:42:32 +10:00
|
|
|
use std::fmt;
|
|
|
|
use std::error::Error;
|
2015-05-17 19:19:06 +10:00
|
|
|
use std::ffi::CString;
|
2015-12-24 20:57:08 +11:00
|
|
|
use std::sync::Mutex;
|
2015-05-17 19:19:06 +10:00
|
|
|
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
use super::ffi;
|
|
|
|
use api::dlopen;
|
|
|
|
|
|
|
|
/// A connection to an X server.
|
|
|
|
pub struct XConnection {
|
|
|
|
pub xlib: ffi::Xlib,
|
|
|
|
pub xf86vmode: ffi::Xf86vmode,
|
|
|
|
pub xcursor: ffi::Xcursor,
|
2015-06-23 07:49:48 +10:00
|
|
|
pub xinput2: ffi::XInput2,
|
2015-05-17 19:19:06 +10:00
|
|
|
pub display: *mut ffi::Display,
|
2015-12-24 20:57:08 +11:00
|
|
|
pub latest_error: Mutex<Option<XError>>,
|
2015-05-17 19:19:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for XConnection {}
|
|
|
|
unsafe impl Sync for XConnection {}
|
|
|
|
|
2015-12-15 09:05:07 +11:00
|
|
|
pub type XErrorHandler = Option<unsafe extern fn(*mut ffi::Display, *mut ffi::XErrorEvent) -> libc::c_int>;
|
|
|
|
|
2015-05-17 19:19:06 +10:00
|
|
|
impl XConnection {
|
2015-12-15 09:05:07 +11:00
|
|
|
pub fn new(error_handler: XErrorHandler) -> Result<XConnection, XNotSupported> {
|
2015-05-17 19:19:06 +10:00
|
|
|
// opening the libraries
|
2015-09-20 16:48:53 +10:00
|
|
|
let xlib = try!(ffi::Xlib::open());
|
|
|
|
let xcursor = try!(ffi::Xcursor::open());
|
|
|
|
let xf86vmode = try!(ffi::Xf86vmode::open());
|
|
|
|
let xinput2 = try!(ffi::XInput2::open());
|
2015-05-17 19:19:06 +10:00
|
|
|
|
|
|
|
unsafe { (xlib.XInitThreads)() };
|
2015-12-15 09:05:07 +11:00
|
|
|
unsafe { (xlib.XSetErrorHandler)(error_handler) };
|
2015-05-17 19:19:06 +10:00
|
|
|
|
|
|
|
// calling XOpenDisplay
|
|
|
|
let display = unsafe {
|
|
|
|
let display = (xlib.XOpenDisplay)(ptr::null());
|
|
|
|
if display.is_null() {
|
2015-09-20 16:48:53 +10:00
|
|
|
return Err(XNotSupported::XOpenDisplayFailed);
|
2015-05-17 19:19:06 +10:00
|
|
|
}
|
|
|
|
display
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(XConnection {
|
|
|
|
xlib: xlib,
|
|
|
|
xf86vmode: xf86vmode,
|
|
|
|
xcursor: xcursor,
|
2015-06-23 07:49:48 +10:00
|
|
|
xinput2: xinput2,
|
2015-05-17 19:19:06 +10:00
|
|
|
display: display,
|
2015-12-24 20:57:08 +11:00
|
|
|
latest_error: Mutex::new(None),
|
2015-05-17 19:19:06 +10:00
|
|
|
})
|
|
|
|
}
|
2015-12-24 20:57:08 +11:00
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
2015-05-17 19:19:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for XConnection {
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-05-17 19:19:06 +10:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { (self.xlib.XCloseDisplay)(self.display) };
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 16:42:32 +10:00
|
|
|
|
2015-12-24 20:57:08 +11:00
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 16:42:32 +10:00
|
|
|
/// Error returned if this system doesn't have XLib or can't create an X connection.
|
2015-09-20 16:48:53 +10:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum XNotSupported {
|
|
|
|
/// Failed to load one or several shared libraries.
|
|
|
|
LibraryOpenError(ffi::OpenError),
|
|
|
|
/// Connecting to the X server with `XOpenDisplay` failed.
|
|
|
|
XOpenDisplayFailed, // TODO: add better message
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ffi::OpenError> for XNotSupported {
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-09-20 16:48:53 +10:00
|
|
|
fn from(err: ffi::OpenError) -> XNotSupported {
|
|
|
|
XNotSupported::LibraryOpenError(err)
|
|
|
|
}
|
|
|
|
}
|
2015-09-20 16:42:32 +10:00
|
|
|
|
|
|
|
impl Error for XNotSupported {
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-09-20 16:42:32 +10:00
|
|
|
fn description(&self) -> &str {
|
2015-09-20 16:48:53 +10:00
|
|
|
match *self {
|
|
|
|
XNotSupported::LibraryOpenError(_) => "Failed to load one of xlib's shared libraries",
|
|
|
|
XNotSupported::XOpenDisplayFailed => "Failed to open connection to X server",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-09-20 16:48:53 +10:00
|
|
|
fn cause(&self) -> Option<&Error> {
|
|
|
|
match *self {
|
|
|
|
XNotSupported::LibraryOpenError(ref err) => Some(err),
|
|
|
|
_ => None
|
|
|
|
}
|
2015-09-20 16:42:32 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for XNotSupported {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
formatter.write_str(self.description())
|
|
|
|
}
|
|
|
|
}
|