Make x11 dynamic

This commit is contained in:
Pierre Krieger 2015-05-07 13:14:09 +02:00
parent 3e29c6585b
commit 2414f87ceb
5 changed files with 126 additions and 88 deletions

View file

@ -51,13 +51,22 @@ kernel32-sys = "0.1"
[target.i686-unknown-linux-gnu.dependencies] [target.i686-unknown-linux-gnu.dependencies]
osmesa-sys = "0.0.5" osmesa-sys = "0.0.5"
wayland-client = "*" wayland-client = "*"
x11 = "*"
[target.i686-unknown-linux-gnu.dependencies.x11]
version = "*"
features = ["dynamic"]
[target.x86_64-unknown-linux-gnu.dependencies] [target.x86_64-unknown-linux-gnu.dependencies]
osmesa-sys = "0.0.5" osmesa-sys = "0.0.5"
wayland-client = "*" wayland-client = "*"
x11 = "*"
[target.x86_64-unknown-linux-gnu.dependencies.x11]
version = "*"
features = ["dynamic"]
[target.arm-unknown-linux-gnueabihf.dependencies] [target.arm-unknown-linux-gnueabihf.dependencies]
osmesa-sys = "0.0.5" osmesa-sys = "0.0.5"
x11 = "*"
[target.arm-unknown-linux-gnueabihf.dependencies.x11]
version = "*"
features = ["dynamic"]

View file

@ -39,7 +39,7 @@ fn main() {
if target.contains("linux") { if target.contains("linux") {
let mut file = File::create(&dest.join("glx_bindings.rs")).unwrap(); let mut file = File::create(&dest.join("glx_bindings.rs")).unwrap();
gl_generator::generate_bindings(gl_generator::StaticGenerator, gl_generator::generate_bindings(gl_generator::StructGenerator,
gl_generator::registry::Ns::Glx, gl_generator::registry::Ns::Glx,
gl_generator::Fallbacks::All, gl_generator::Fallbacks::All,
khronos_api::GLX_XML, vec![], khronos_api::GLX_XML, vec![],

View file

@ -15,6 +15,7 @@ use std::{mem, ptr};
use api::x11::ffi; use api::x11::ffi;
pub struct Context { pub struct Context {
glx: ffi::glx::Glx,
display: *mut ffi::Display, display: *mut ffi::Display,
window: ffi::Window, window: ffi::Window,
context: ffi::GLXContext, context: ffi::GLXContext,
@ -28,7 +29,7 @@ fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) ->
} }
impl Context { impl Context {
pub fn new(builder: BuilderAttribs, display: *mut ffi::Display, window: ffi::Window, pub fn new(glx: ffi::glx::Glx, builder: BuilderAttribs, display: *mut ffi::Display, window: ffi::Window,
fb_config: ffi::glx::types::GLXFBConfig, mut visual_infos: ffi::glx::types::XVisualInfo) fb_config: ffi::glx::types::GLXFBConfig, mut visual_infos: ffi::glx::types::XVisualInfo)
-> Result<Context, CreationError> -> Result<Context, CreationError>
{ {
@ -74,7 +75,7 @@ impl Context {
// loading the extra GLX functions // loading the extra GLX functions
let extra_functions = ffi::glx_extra::Glx::load_with(|addr| { let extra_functions = ffi::glx_extra::Glx::load_with(|addr| {
with_c_str(addr, |s| { with_c_str(addr, |s| {
ffi::glx::GetProcAddress(s as *const u8) as *const libc::c_void glx.GetProcAddress(s as *const u8) as *const libc::c_void
}) })
}); });
@ -95,7 +96,7 @@ impl Context {
}; };
if context.is_null() { if context.is_null() {
context = ffi::glx::CreateContext(display as *mut _, &mut visual_infos, share, 1) context = glx.CreateContext(display as *mut _, &mut visual_infos, share, 1)
} }
if context.is_null() { if context.is_null() {
@ -107,7 +108,7 @@ impl Context {
// vsync // vsync
if builder.vsync { if builder.vsync {
unsafe { ffi::glx::MakeCurrent(display as *mut _, window, context) }; unsafe { glx.MakeCurrent(display as *mut _, window, context) };
if extra_functions.SwapIntervalEXT.is_loaded() { if extra_functions.SwapIntervalEXT.is_loaded() {
// this should be the most common extension // this should be the most common extension
@ -119,7 +120,7 @@ impl Context {
if builder.strict { if builder.strict {
let mut swap = unsafe { mem::uninitialized() }; let mut swap = unsafe { mem::uninitialized() };
unsafe { unsafe {
ffi::glx::QueryDrawable(display as *mut _, window, glx.QueryDrawable(display as *mut _, window,
ffi::glx_extra::SWAP_INTERVAL_EXT as i32, ffi::glx_extra::SWAP_INTERVAL_EXT as i32,
&mut swap); &mut swap);
} }
@ -145,10 +146,11 @@ impl Context {
return Err(CreationError::OsError(format!("Couldn't find any available vsync extension"))); return Err(CreationError::OsError(format!("Couldn't find any available vsync extension")));
} }
unsafe { ffi::glx::MakeCurrent(display as *mut _, 0, ptr::null()) }; unsafe { glx.MakeCurrent(display as *mut _, 0, ptr::null()) };
} }
Ok(Context { Ok(Context {
glx: glx,
display: display, display: display,
window: window, window: window,
context: context, context: context,
@ -158,27 +160,27 @@ impl Context {
impl GlContext for Context { impl GlContext for Context {
unsafe fn make_current(&self) { unsafe fn make_current(&self) {
let res = ffi::glx::MakeCurrent(self.display as *mut _, self.window, self.context); let res = self.glx.MakeCurrent(self.display as *mut _, self.window, self.context);
if res == 0 { if res == 0 {
panic!("glx::MakeCurrent failed"); panic!("glx::MakeCurrent failed");
} }
} }
fn is_current(&self) -> bool { fn is_current(&self) -> bool {
unsafe { ffi::glx::GetCurrentContext() == self.context } unsafe { self.glx.GetCurrentContext() == self.context }
} }
fn get_proc_address(&self, addr: &str) -> *const libc::c_void { fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
let addr = CString::new(addr.as_bytes()).unwrap(); let addr = CString::new(addr.as_bytes()).unwrap();
let addr = addr.as_ptr(); let addr = addr.as_ptr();
unsafe { unsafe {
ffi::glx::GetProcAddress(addr as *const _) as *const _ self.glx.GetProcAddress(addr as *const _) as *const _
} }
} }
fn swap_buffers(&self) { fn swap_buffers(&self) {
unsafe { unsafe {
ffi::glx::SwapBuffers(self.display as *mut _, self.window) self.glx.SwapBuffers(self.display as *mut _, self.window)
} }
} }
@ -199,7 +201,7 @@ impl Drop for Context {
unsafe { unsafe {
// we don't call MakeCurrent(0, 0) because we are not sure that the context // we don't call MakeCurrent(0, 0) because we are not sure that the context
// is still the current one // is still the current one
ffi::glx::DestroyContext(self.display as *mut _, self.context); self.glx.DestroyContext(self.display as *mut _, self.context);
} }
} }
} }

View file

@ -39,11 +39,11 @@ unsafe extern "C" fn x_error_callback(_: *mut ffi::Display, event: *mut ffi::XEr
0 0
} }
fn ensure_thread_init() { fn ensure_thread_init(xlib: &ffi::Xlib) {
THREAD_INIT.call_once(|| { THREAD_INIT.call_once(|| {
unsafe { unsafe {
ffi::XInitThreads(); (xlib.XInitThreads)();
ffi::XSetErrorHandler(Some(x_error_callback)); (xlib.XSetErrorHandler)(Some(x_error_callback));
} }
}); });
} }
@ -56,6 +56,9 @@ fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) ->
} }
pub struct XWindow { pub struct XWindow {
xlib: ffi::Xlib,
xf86vmode: ffi::Xf86vmode,
xcursor: ffi::Xcursor,
display: *mut ffi::Display, display: *mut ffi::Display,
window: ffi::Window, window: ffi::Window,
pub context: Context, pub context: Context,
@ -86,14 +89,14 @@ impl Drop for XWindow {
self.context = Context::None; self.context = Context::None;
if self.is_fullscreen { if self.is_fullscreen {
ffi::XF86VidModeSwitchToMode(self.display, self.screen_id, self.xf86_desk_mode); (self.xf86vmode.XF86VidModeSwitchToMode)(self.display, self.screen_id, self.xf86_desk_mode);
ffi::XF86VidModeSetViewPort(self.display, self.screen_id, 0, 0); (self.xf86vmode.XF86VidModeSetViewPort)(self.display, self.screen_id, 0, 0);
} }
ffi::XDestroyIC(self.ic); (self.xlib.XDestroyIC)(self.ic);
ffi::XCloseIM(self.im); (self.xlib.XCloseIM)(self.im);
ffi::XDestroyWindow(self.display, self.window); (self.xlib.XDestroyWindow)(self.display, self.window);
ffi::XCloseDisplay(self.display); (self.xlib.XCloseDisplay)(self.display);
} }
} }
} }
@ -117,8 +120,8 @@ impl WindowProxy {
}; };
unsafe { unsafe {
ffi::XSendEvent(self.x.display, self.x.window, 0, 0, mem::transmute(&mut xev)); (self.x.xlib.XSendEvent)(self.x.display, self.x.window, 0, 0, mem::transmute(&mut xev));
ffi::XFlush(self.x.display); (self.x.xlib.XFlush)(self.x.display);
} }
} }
} }
@ -137,10 +140,10 @@ impl<'a> Iterator for PollEventsIterator<'a> {
loop { loop {
let mut xev = unsafe { mem::uninitialized() }; let mut xev = unsafe { mem::uninitialized() };
let res = unsafe { ffi::XCheckMaskEvent(self.window.x.display, -1, &mut xev) }; let res = unsafe { (self.window.x.xlib.XCheckMaskEvent)(self.window.x.display, -1, &mut xev) };
if res == 0 { if res == 0 {
let res = unsafe { ffi::XCheckTypedEvent(self.window.x.display, ffi::ClientMessage, &mut xev) }; let res = unsafe { (self.window.x.xlib.XCheckTypedEvent)(self.window.x.display, ffi::ClientMessage, &mut xev) };
if res == 0 { if res == 0 {
return None; return None;
@ -149,7 +152,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
match xev.get_type() { match xev.get_type() {
ffi::KeymapNotify => { ffi::KeymapNotify => {
unsafe { ffi::XRefreshKeyboardMapping(mem::transmute(&xev)); } unsafe { (self.window.x.xlib.XRefreshKeyboardMapping)(mem::transmute(&xev)); }
}, },
ffi::ClientMessage => { ffi::ClientMessage => {
@ -194,7 +197,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
if event.type_ == ffi::KeyPress { if event.type_ == ffi::KeyPress {
let raw_ev: *mut ffi::XKeyEvent = event; let raw_ev: *mut ffi::XKeyEvent = event;
unsafe { ffi::XFilterEvent(mem::transmute(raw_ev), self.window.x.window) }; unsafe { (self.window.x.xlib.XFilterEvent)(mem::transmute(raw_ev), self.window.x.window) };
} }
let state = if xev.get_type() == ffi::KeyPress { Pressed } else { Released }; let state = if xev.get_type() == ffi::KeyPress { Pressed } else { Released };
@ -204,7 +207,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
let mut buffer: [u8; 16] = [mem::uninitialized(); 16]; let mut buffer: [u8; 16] = [mem::uninitialized(); 16];
let raw_ev: *mut ffi::XKeyEvent = event; let raw_ev: *mut ffi::XKeyEvent = event;
let count = ffi::Xutf8LookupString(self.window.x.ic, mem::transmute(raw_ev), let count = (self.window.x.xlib.Xutf8LookupString)(self.window.x.ic, mem::transmute(raw_ev),
mem::transmute(buffer.as_mut_ptr()), mem::transmute(buffer.as_mut_ptr()),
buffer.len() as libc::c_int, ptr::null_mut(), ptr::null_mut()); buffer.len() as libc::c_int, ptr::null_mut(), ptr::null_mut());
@ -219,7 +222,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
} }
let keysym = unsafe { let keysym = unsafe {
ffi::XKeycodeToKeysym(self.window.x.display, event.keycode as ffi::KeyCode, 0) (self.window.x.xlib.XKeycodeToKeysym)(self.window.x.display, event.keycode as ffi::KeyCode, 0)
}; };
let vkey = events::keycode_to_element(keysym as libc::c_uint); let vkey = events::keycode_to_element(keysym as libc::c_uint);
@ -282,7 +285,7 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
// this will block until an event arrives, but doesn't remove // this will block until an event arrives, but doesn't remove
// it from the queue // it from the queue
let mut xev = unsafe { mem::uninitialized() }; let mut xev = unsafe { mem::uninitialized() };
unsafe { ffi::XPeekEvent(self.window.x.display, &mut xev) }; unsafe { (self.window.x.xlib.XPeekEvent)(self.window.x.display, &mut xev) };
// calling poll_events() // calling poll_events()
if let Some(ev) = self.window.poll_events().next() { if let Some(ev) = self.window.poll_events().next() {
@ -307,12 +310,27 @@ pub struct Window {
impl Window { impl Window {
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
ensure_thread_init(); let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
let xcursor = ffi::Xcursor::open().unwrap(); // FIXME: gracious handling
let xf86vmode = ffi::Xf86vmode::open().unwrap(); // FIXME: gracious handling
let glx = {
let libglx = unsafe { dlopen::dlopen(b"libGL.so\0".as_ptr() as *const _, dlopen::RTLD_NOW) };
if libglx.is_null() {
return Err(CreationError::NotSupported);
}
ffi::glx::Glx::load_with(|sym| {
let sym = CString::new(sym).unwrap();
unsafe { dlopen::dlsym(libglx, sym.as_ptr()) }
})
};
ensure_thread_init(&xlib);
let dimensions = builder.dimensions.unwrap_or((800, 600)); let dimensions = builder.dimensions.unwrap_or((800, 600));
// calling XOpenDisplay // calling XOpenDisplay
let display = unsafe { let display = unsafe {
let display = ffi::XOpenDisplay(ptr::null()); let display = (xlib.XOpenDisplay)(ptr::null());
if display.is_null() { if display.is_null() {
return Err(OsError(format!("XOpenDisplay failed"))); return Err(OsError(format!("XOpenDisplay failed")));
} }
@ -321,7 +339,7 @@ impl Window {
let screen_id = match builder.monitor { let screen_id = match builder.monitor {
Some(MonitorID(monitor)) => monitor as i32, Some(MonitorID(monitor)) => monitor as i32,
None => unsafe { ffi::XDefaultScreen(display) }, None => unsafe { (xlib.XDefaultScreen)(display) },
}; };
// getting the FBConfig // getting the FBConfig
@ -356,13 +374,13 @@ impl Window {
let mut num_fb: libc::c_int = mem::uninitialized(); let mut num_fb: libc::c_int = mem::uninitialized();
let fb = ffi::glx::ChooseFBConfig(display as *mut _, ffi::XDefaultScreen(display), let fb = glx.ChooseFBConfig(display as *mut _, (xlib.XDefaultScreen)(display),
visual_attributes.as_ptr(), &mut num_fb); visual_attributes.as_ptr(), &mut num_fb);
if fb.is_null() { if fb.is_null() {
return Err(OsError(format!("glx::ChooseFBConfig failed"))); return Err(OsError(format!("glx::ChooseFBConfig failed")));
} }
let preferred_fb = *fb; // TODO: choose more wisely let preferred_fb = *fb; // TODO: choose more wisely
ffi::XFree(fb as *mut _); (xlib.XFree)(fb as *mut _);
preferred_fb preferred_fb
}; };
@ -370,7 +388,7 @@ impl Window {
let modes = unsafe { let modes = unsafe {
let mut mode_num: libc::c_int = mem::uninitialized(); let mut mode_num: libc::c_int = mem::uninitialized();
let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized(); let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized();
if ffi::XF86VidModeGetAllModeLines(display, screen_id, &mut mode_num, &mut modes) == 0 { if (xf86vmode.XF86VidModeGetAllModeLines)(display, screen_id, &mut mode_num, &mut modes) == 0 {
return Err(OsError(format!("Could not query the video modes"))); return Err(OsError(format!("Could not query the video modes")));
} }
@ -393,12 +411,12 @@ impl Window {
// getting the visual infos // getting the visual infos
let mut visual_infos: ffi::glx::types::XVisualInfo = unsafe { let mut visual_infos: ffi::glx::types::XVisualInfo = unsafe {
let vi = ffi::glx::GetVisualFromFBConfig(display as *mut _, fb_config); let vi = glx.GetVisualFromFBConfig(display as *mut _, fb_config);
if vi.is_null() { if vi.is_null() {
return Err(OsError(format!("glx::ChooseVisual failed"))); return Err(OsError(format!("glx::ChooseVisual failed")));
} }
let vi_copy = ptr::read(vi as *const _); let vi_copy = ptr::read(vi as *const _);
ffi::XFree(vi as *mut _); (xlib.XFree)(vi as *mut _);
vi_copy vi_copy
}; };
@ -406,7 +424,7 @@ impl Window {
let pixel_format = { let pixel_format = {
let get_attrib = |attrib: libc::c_int| -> i32 { let get_attrib = |attrib: libc::c_int| -> i32 {
let mut value = 0; let mut value = 0;
unsafe { ffi::glx::GetFBConfigAttrib(display as *mut _, fb_config, attrib, &mut value); } unsafe { glx.GetFBConfigAttrib(display as *mut _, fb_config, attrib, &mut value); }
value value
}; };
@ -428,11 +446,11 @@ impl Window {
}; };
// getting the root window // getting the root window
let root = unsafe { ffi::XDefaultRootWindow(display) }; let root = unsafe { (xlib.XDefaultRootWindow)(display) };
// creating the color map // creating the color map
let cmap = unsafe { let cmap = unsafe {
let cmap = ffi::XCreateColormap(display, root, let cmap = (xlib.XCreateColormap)(display, root,
visual_infos.visual as *mut _, ffi::AllocNone); visual_infos.visual as *mut _, ffi::AllocNone);
// TODO: error checking? // TODO: error checking?
cmap cmap
@ -455,15 +473,15 @@ impl Window {
if builder.monitor.is_some() { if builder.monitor.is_some() {
window_attributes |= ffi::CWOverrideRedirect; window_attributes |= ffi::CWOverrideRedirect;
unsafe { unsafe {
ffi::XF86VidModeSwitchToMode(display, screen_id, *modes.offset(best_mode as isize)); (xf86vmode.XF86VidModeSwitchToMode)(display, screen_id, *modes.offset(best_mode as isize));
ffi::XF86VidModeSetViewPort(display, screen_id, 0, 0); (xf86vmode.XF86VidModeSetViewPort)(display, screen_id, 0, 0);
set_win_attr.override_redirect = 1; set_win_attr.override_redirect = 1;
} }
} }
// finally creating the window // finally creating the window
let window = unsafe { let window = unsafe {
let win = ffi::XCreateWindow(display, root, 0, 0, dimensions.0 as libc::c_uint, let win = (xlib.XCreateWindow)(display, root, 0, 0, dimensions.0 as libc::c_uint,
dimensions.1 as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput as libc::c_uint, dimensions.1 as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput as libc::c_uint,
visual_infos.visual as *mut _, window_attributes, visual_infos.visual as *mut _, window_attributes,
&mut set_win_attr); &mut set_win_attr);
@ -473,21 +491,21 @@ impl Window {
// set visibility // set visibility
if builder.visible { if builder.visible {
unsafe { unsafe {
ffi::XMapRaised(display, window); (xlib.XMapRaised)(display, window);
ffi::XFlush(display); (xlib.XFlush)(display);
} }
} }
// creating window, step 2 // creating window, step 2
let wm_delete_window = unsafe { let wm_delete_window = unsafe {
let mut wm_delete_window = with_c_str("WM_DELETE_WINDOW", |delete_window| let mut wm_delete_window = with_c_str("WM_DELETE_WINDOW", |delete_window|
ffi::XInternAtom(display, delete_window, 0) (xlib.XInternAtom)(display, delete_window, 0)
); );
ffi::XSetWMProtocols(display, window, &mut wm_delete_window, 1); (xlib.XSetWMProtocols)(display, window, &mut wm_delete_window, 1);
with_c_str(&*builder.title, |title| {; with_c_str(&*builder.title, |title| {;
ffi::XStoreName(display, window, title); (xlib.XStoreName)(display, window, title);
}); });
ffi::XFlush(display); (xlib.XFlush)(display);
wm_delete_window wm_delete_window
}; };
@ -496,7 +514,7 @@ impl Window {
let im = unsafe { let im = unsafe {
let _lock = GLOBAL_XOPENIM_LOCK.lock().unwrap(); let _lock = GLOBAL_XOPENIM_LOCK.lock().unwrap();
let im = ffi::XOpenIM(display, ptr::null_mut(), ptr::null_mut(), ptr::null_mut()); let im = (xlib.XOpenIM)(display, ptr::null_mut(), ptr::null_mut(), ptr::null_mut());
if im.is_null() { if im.is_null() {
return Err(OsError(format!("XOpenIM failed"))); return Err(OsError(format!("XOpenIM failed")));
} }
@ -507,7 +525,7 @@ impl Window {
let ic = unsafe { let ic = unsafe {
let ic = with_c_str("inputStyle", |input_style| let ic = with_c_str("inputStyle", |input_style|
with_c_str("clientWindow", |client_window| with_c_str("clientWindow", |client_window|
ffi::XCreateIC( (xlib.XCreateIC)(
im, input_style, im, input_style,
ffi::XIMPreeditNothing | ffi::XIMStatusNothing, client_window, ffi::XIMPreeditNothing | ffi::XIMStatusNothing, client_window,
window, ptr::null::<()>() window, ptr::null::<()>()
@ -517,14 +535,14 @@ impl Window {
if ic.is_null() { if ic.is_null() {
return Err(OsError(format!("XCreateIC failed"))); return Err(OsError(format!("XCreateIC failed")));
} }
ffi::XSetICFocus(ic); (xlib.XSetICFocus)(ic);
ic ic
}; };
// Attempt to make keyboard input repeat detectable // Attempt to make keyboard input repeat detectable
unsafe { unsafe {
let mut supported_ptr = ffi::False; let mut supported_ptr = ffi::False;
ffi::XkbSetDetectableAutoRepeat(display, ffi::True, &mut supported_ptr); (xlib.XkbSetDetectableAutoRepeat)(display, ffi::True, &mut supported_ptr);
if supported_ptr == ffi::False { if supported_ptr == ffi::False {
return Err(OsError(format!("XkbSetDetectableAutoRepeat failed"))); return Err(OsError(format!("XkbSetDetectableAutoRepeat failed")));
} }
@ -533,11 +551,11 @@ impl Window {
// Set ICCCM WM_CLASS property based on initial window title // Set ICCCM WM_CLASS property based on initial window title
unsafe { unsafe {
with_c_str(&*builder.title, |c_name| { with_c_str(&*builder.title, |c_name| {
let hint = ffi::XAllocClassHint(); let hint = (xlib.XAllocClassHint)();
(*hint).res_name = c_name as *mut i8; (*hint).res_name = c_name as *mut i8;
(*hint).res_class = c_name as *mut i8; (*hint).res_class = c_name as *mut i8;
ffi::XSetClassHint(display, window, hint); (xlib.XSetClassHint)(display, window, hint);
ffi::XFree(hint as *mut libc::c_void); (xlib.XFree)(hint as *mut libc::c_void);
}); });
} }
@ -545,7 +563,7 @@ impl Window {
// creating the context // creating the context
let context = match builder.gl_version { let context = match builder.gl_version {
GlRequest::Latest | GlRequest::Specific(Api::OpenGl, _) | GlRequest::GlThenGles { .. } => { GlRequest::Latest | GlRequest::Specific(Api::OpenGl, _) | GlRequest::GlThenGles { .. } => {
Context::Glx(try!(GlxContext::new(builder, display, window, Context::Glx(try!(GlxContext::new(glx, builder, display, window,
fb_config, visual_infos))) fb_config, visual_infos)))
}, },
GlRequest::Specific(Api::OpenGlEs, _) => { GlRequest::Specific(Api::OpenGlEs, _) => {
@ -567,6 +585,9 @@ impl Window {
// creating the window object // creating the window object
let window = Window { let window = Window {
x: Arc::new(XWindow { x: Arc::new(XWindow {
xlib: xlib,
xcursor: xcursor,
xf86vmode: xf86vmode,
display: display, display: display,
window: window, window: window,
im: im, im: im,
@ -595,22 +616,22 @@ impl Window {
pub fn set_title(&self, title: &str) { pub fn set_title(&self, title: &str) {
with_c_str(title, |title| unsafe { with_c_str(title, |title| unsafe {
ffi::XStoreName(self.x.display, self.x.window, title); (self.x.xlib.XStoreName)(self.x.display, self.x.window, title);
ffi::XFlush(self.x.display); (self.x.xlib.XFlush)(self.x.display);
}) })
} }
pub fn show(&self) { pub fn show(&self) {
unsafe { unsafe {
ffi::XMapRaised(self.x.display, self.x.window); (self.x.xlib.XMapRaised)(self.x.display, self.x.window);
ffi::XFlush(self.x.display); (self.x.xlib.XFlush)(self.x.display);
} }
} }
pub fn hide(&self) { pub fn hide(&self) {
unsafe { unsafe {
ffi::XUnmapWindow(self.x.display, self.x.window); (self.x.xlib.XUnmapWindow)(self.x.display, self.x.window);
ffi::XFlush(self.x.display); (self.x.xlib.XFlush)(self.x.display);
} }
} }
@ -626,7 +647,7 @@ impl Window {
let mut border: libc::c_uint = mem::uninitialized(); let mut border: libc::c_uint = mem::uninitialized();
let mut depth: libc::c_uint = mem::uninitialized(); let mut depth: libc::c_uint = mem::uninitialized();
if ffi::XGetGeometry(self.x.display, self.x.window, if (self.x.xlib.XGetGeometry)(self.x.display, self.x.window,
&mut root, &mut x, &mut y, &mut width, &mut height, &mut root, &mut x, &mut y, &mut width, &mut height,
&mut border, &mut depth) == 0 &mut border, &mut depth) == 0
{ {
@ -642,7 +663,7 @@ impl Window {
} }
pub fn set_position(&self, x: i32, y: i32) { pub fn set_position(&self, x: i32, y: i32) {
unsafe { ffi::XMoveWindow(self.x.display, self.x.window, x as libc::c_int, y as libc::c_int); } unsafe { (self.x.xlib.XMoveWindow)(self.x.display, self.x.window, x as libc::c_int, y as libc::c_int); }
} }
pub fn get_inner_size(&self) -> Option<(u32, u32)> { pub fn get_inner_size(&self) -> Option<(u32, u32)> {
@ -728,9 +749,9 @@ impl Window {
MouseCursor::ZoomOut => "left_ptr", MouseCursor::ZoomOut => "left_ptr",
}; };
let c_string = CString::new(cursor_name.as_bytes().to_vec()).unwrap(); let c_string = CString::new(cursor_name.as_bytes().to_vec()).unwrap();
let xcursor = ffi::XcursorLibraryLoadCursor(self.x.display, c_string.as_ptr()); let xcursor = (self.x.xcursor.XcursorLibraryLoadCursor)(self.x.display, c_string.as_ptr());
ffi::XDefineCursor (self.x.display, self.x.window, xcursor); (self.x.xlib.XDefineCursor)(self.x.display, self.x.window, xcursor);
ffi::XFlush(self.x.display); (self.x.xlib.XFlush)(self.x.display);
} }
} }
@ -740,7 +761,7 @@ impl Window {
match (state, *cursor_state) { match (state, *cursor_state) {
(CursorState::Normal, CursorState::Grab) => { (CursorState::Normal, CursorState::Grab) => {
unsafe { unsafe {
ffi::XUngrabPointer(self.x.display, ffi::CurrentTime); (self.x.xlib.XUngrabPointer)(self.x.display, ffi::CurrentTime);
*cursor_state = CursorState::Normal; *cursor_state = CursorState::Normal;
Ok(()) Ok(())
} }
@ -750,7 +771,7 @@ impl Window {
unsafe { unsafe {
*cursor_state = CursorState::Grab; *cursor_state = CursorState::Grab;
match ffi::XGrabPointer( match (self.x.xlib.XGrabPointer)(
self.x.display, self.x.window, ffi::False, self.x.display, self.x.window, ffi::False,
(ffi::ButtonPressMask | ffi::ButtonReleaseMask | ffi::EnterWindowMask | (ffi::ButtonPressMask | ffi::ButtonReleaseMask | ffi::EnterWindowMask |
ffi::LeaveWindowMask | ffi::PointerMotionMask | ffi::PointerMotionHintMask | ffi::LeaveWindowMask | ffi::PointerMotionMask | ffi::PointerMotionHintMask |
@ -779,7 +800,7 @@ impl Window {
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> { pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
unsafe { unsafe {
ffi::XWarpPointer(self.x.display, 0, self.x.window, 0, 0, 0, 0, x, y); (self.x.xlib.XWarpPointer)(self.x.display, 0, self.x.window, 0, 0, 0, 0, x, y);
} }
Ok(()) Ok(())

View file

@ -7,14 +7,16 @@ use native_monitor::NativeMonitorId;
pub struct MonitorID(pub u32); pub struct MonitorID(pub u32);
pub fn get_available_monitors() -> VecDeque<MonitorID> { pub fn get_available_monitors() -> VecDeque<MonitorID> {
ensure_thread_init(); let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
ensure_thread_init(&xlib);
let nb_monitors = unsafe { let nb_monitors = unsafe {
let display = ffi::XOpenDisplay(ptr::null()); let display = (xlib.XOpenDisplay)(ptr::null());
if display.is_null() { if display.is_null() {
panic!("get_available_monitors failed"); panic!("get_available_monitors failed");
} }
let nb_monitors = ffi::XScreenCount(display); let nb_monitors = (xlib.XScreenCount)(display);
ffi::XCloseDisplay(display); (xlib.XCloseDisplay)(display);
nb_monitors nb_monitors
}; };
@ -24,14 +26,16 @@ pub fn get_available_monitors() -> VecDeque<MonitorID> {
} }
pub fn get_primary_monitor() -> MonitorID { pub fn get_primary_monitor() -> MonitorID {
ensure_thread_init(); let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
ensure_thread_init(&xlib);
let primary_monitor = unsafe { let primary_monitor = unsafe {
let display = ffi::XOpenDisplay(ptr::null()); let display = (xlib.XOpenDisplay)(ptr::null());
if display.is_null() { if display.is_null() {
panic!("get_available_monitors failed"); panic!("get_available_monitors failed");
} }
let primary_monitor = ffi::XDefaultScreen(display); let primary_monitor = (xlib.XDefaultScreen)(display);
ffi::XCloseDisplay(display); (xlib.XCloseDisplay)(display);
primary_monitor primary_monitor
}; };
@ -50,13 +54,15 @@ impl MonitorID {
} }
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {
let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
let dimensions = unsafe { let dimensions = unsafe {
let display = ffi::XOpenDisplay(ptr::null()); let display = (xlib.XOpenDisplay)(ptr::null());
let MonitorID(screen_num) = *self; let MonitorID(screen_num) = *self;
let screen = ffi::XScreenOfDisplay(display, screen_num as i32); let screen = (xlib.XScreenOfDisplay)(display, screen_num as i32);
let width = ffi::XWidthOfScreen(screen); let width = (xlib.XWidthOfScreen)(screen);
let height = ffi::XHeightOfScreen(screen); let height = (xlib.XHeightOfScreen)(screen);
ffi::XCloseDisplay(display); (xlib.XCloseDisplay)(display);
(width as u32, height as u32) (width as u32, height as u32)
}; };