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;
|
|
|
|
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
use super::ffi;
|
|
|
|
use api::egl::ffi::egl::Egl;
|
|
|
|
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 glx: Option<ffi::glx::Glx>,
|
|
|
|
pub egl: Option<Egl>,
|
|
|
|
pub display: *mut ffi::Display,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for XConnection {}
|
|
|
|
unsafe impl Sync for XConnection {}
|
|
|
|
|
|
|
|
impl XConnection {
|
|
|
|
pub fn new() -> Result<XConnection, XNotSupported> {
|
|
|
|
// 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 extern "C" fn x_error_callback(_: *mut ffi::Display, event: *mut ffi::XErrorEvent)
|
|
|
|
-> libc::c_int
|
|
|
|
{
|
|
|
|
println!("[glutin] x error code={} major={} minor={}!", (*event).error_code,
|
|
|
|
(*event).request_code, (*event).minor_code);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { (xlib.XInitThreads)() };
|
|
|
|
unsafe { (xlib.XSetErrorHandler)(Some(x_error_callback)) };
|
|
|
|
|
|
|
|
// TODO: use something safer than raw "dlopen"
|
|
|
|
let glx = {
|
|
|
|
let mut libglx = unsafe { dlopen::dlopen(b"libGL.so.1\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
|
|
|
|
if libglx.is_null() {
|
|
|
|
libglx = unsafe { dlopen::dlopen(b"libGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
|
|
|
|
}
|
|
|
|
|
|
|
|
if libglx.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(ffi::glx::Glx::load_with(|sym| {
|
|
|
|
let sym = CString::new(sym).unwrap();
|
|
|
|
unsafe { dlopen::dlsym(libglx, sym.as_ptr()) }
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: use something safer than raw "dlopen"
|
|
|
|
let egl = {
|
2015-07-29 04:44:21 +10:00
|
|
|
let mut libegl = unsafe { dlopen::dlopen(b"libEGL.so.1\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
|
|
|
|
if libegl.is_null() {
|
|
|
|
libegl = unsafe { dlopen::dlopen(b"libEGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
|
|
|
|
}
|
2015-05-17 19:19:06 +10:00
|
|
|
|
|
|
|
if libegl.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Egl::load_with(|sym| {
|
|
|
|
let sym = CString::new(sym).unwrap();
|
|
|
|
unsafe { dlopen::dlsym(libegl, sym.as_ptr()) }
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
glx: glx,
|
|
|
|
egl: egl,
|
|
|
|
display: display,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// 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())
|
|
|
|
}
|
|
|
|
}
|