x11: Improve xlib error reporting

This commit is contained in:
Emilio Cobos Álvarez 2015-12-14 23:05:07 +01:00
parent 4f0aeafbed
commit a69ded9c8a
2 changed files with 22 additions and 13 deletions

View file

@ -23,24 +23,18 @@ pub struct XConnection {
unsafe impl Send for XConnection {} unsafe impl Send for XConnection {}
unsafe impl Sync for XConnection {} unsafe impl Sync for XConnection {}
pub type XErrorHandler = Option<unsafe extern fn(*mut ffi::Display, *mut ffi::XErrorEvent) -> libc::c_int>;
impl XConnection { impl XConnection {
pub fn new() -> Result<XConnection, XNotSupported> { pub fn new(error_handler: XErrorHandler) -> Result<XConnection, XNotSupported> {
// opening the libraries // opening the libraries
let xlib = try!(ffi::Xlib::open()); let xlib = try!(ffi::Xlib::open());
let xcursor = try!(ffi::Xcursor::open()); let xcursor = try!(ffi::Xcursor::open());
let xf86vmode = try!(ffi::Xf86vmode::open()); let xf86vmode = try!(ffi::Xf86vmode::open());
let xinput2 = try!(ffi::XInput2::open()); let xinput2 = try!(ffi::XInput2::open());
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.XInitThreads)() };
unsafe { (xlib.XSetErrorHandler)(Some(x_error_callback)) }; unsafe { (xlib.XSetErrorHandler)(error_handler) };
// TODO: use something safer than raw "dlopen" // TODO: use something safer than raw "dlopen"
let glx = { let glx = {

View file

@ -33,7 +33,7 @@ lazy_static!(
if false && wayland::is_available() { if false && wayland::is_available() {
Backend::Wayland Backend::Wayland
} else { } else {
match XConnection::new() { match XConnection::new(Some(x_error_callback)) {
Ok(x) => Backend::X(Arc::new(x)), Ok(x) => Backend::X(Arc::new(x)),
Err(e) => Backend::Error(e), Err(e) => Backend::Error(e),
} }
@ -115,7 +115,7 @@ impl MonitorId {
match self { match self {
&MonitorId::X(ref m) => m.get_native_identifier(), &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: &MonitorId::None => unimplemented!() // FIXME:
} }
} }
@ -124,7 +124,7 @@ impl MonitorId {
match self { match self {
&MonitorId::X(ref m) => m.get_dimensions(), &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: &MonitorId::None => (800, 600), // FIXME:
} }
} }
} }
@ -390,3 +390,18 @@ impl GlContext for Window {
} }
} }
} }
unsafe extern "C" fn x_error_callback(dpy: *mut x11::ffi::Display, event: *mut x11::ffi::XErrorEvent)
-> libc::c_int
{
use std::ffi::CStr;
if let Backend::X(ref x) = *BACKEND {
let mut buff: Vec<u8> = Vec::with_capacity(1024);
(x.xlib.XGetErrorText)(dpy, (*event).error_code as i32, buff.as_mut_ptr() as *mut i8, buff.capacity() as i32);
let error = CStr::from_ptr(buff.as_mut_ptr() as *const i8).to_string_lossy();
println!("[glutin] x error code={} major={} minor={}: {}!", (*event).error_code, (*event).request_code, (*event).minor_code, error);
}
0
}