Using x11-rs and osmesa-rs.

This commit is contained in:
Daggerbot 2015-04-09 10:38:58 +00:00
parent 70d36a3d3d
commit 2d20050b89
5 changed files with 96 additions and 1616 deletions

View file

@ -47,3 +47,11 @@ winapi = "0.1"
gdi32-sys = "0.1" gdi32-sys = "0.1"
user32-sys = "0.1" user32-sys = "0.1"
kernel32-sys = "0.1" kernel32-sys = "0.1"
[target.i686-unknown-linux-gnu.dependencies]
osmesa-sys = "*"
x11 = "*"
[target.x86_64-unknown-linux-gnu.dependencies]
osmesa-sys = "*"
x11 = "*"

View file

@ -46,6 +46,10 @@ extern crate cocoa;
extern crate core_foundation; extern crate core_foundation;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
extern crate glutin_core_graphics as core_graphics; extern crate glutin_core_graphics as core_graphics;
#[cfg(target_os = "linux")]
extern crate osmesa_sys;
#[cfg(target_os = "linux")]
extern crate x11;
pub use events::*; pub use events::*;
#[cfg(feature = "headless")] #[cfg(feature = "headless")]

File diff suppressed because it is too large Load diff

View file

@ -22,7 +22,7 @@ impl HeadlessContext {
buffer: ::std::iter::repeat(unsafe { mem::uninitialized() }) buffer: ::std::iter::repeat(unsafe { mem::uninitialized() })
.take((dimensions.0 * dimensions.1) as usize).collect(), .take((dimensions.0 * dimensions.1) as usize).collect(),
context: unsafe { context: unsafe {
let ctxt = ffi::OSMesaCreateContext(0x1908, ptr::null()); let ctxt = ffi::OSMesaCreateContext(0x1908, ptr::null_mut());
if ctxt.is_null() { if ctxt.is_null() {
return Err(OsError("OSMesaCreateContext failed".to_string())); return Err(OsError("OSMesaCreateContext failed".to_string()));
} }
@ -49,7 +49,7 @@ impl HeadlessContext {
unsafe { unsafe {
use std::ffi::CString; use std::ffi::CString;
let c_str = CString::new(addr.as_bytes().to_vec()).unwrap(); let c_str = CString::new(addr.as_bytes().to_vec()).unwrap();
ffi::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())) as *const () mem::transmute(ffi::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())))
} }
} }

View file

@ -25,10 +25,8 @@ lazy_static! { // TODO: use a static mutex when that's possible, and put me
static ref GLOBAL_XOPENIM_LOCK: Mutex<()> = Mutex::new(()); static ref GLOBAL_XOPENIM_LOCK: Mutex<()> = Mutex::new(());
} }
fn x_error_callback(_: *mut ffi::Display, event: *mut ffi::XErrorEvent) -> libc::c_int { unsafe extern "C" fn x_error_callback(_: *mut ffi::Display, event: *mut ffi::XErrorEvent) -> libc::c_int {
unsafe {
println!("[glutin] x error code={} major={} minor={}!", (*event).error_code, (*event).request_code, (*event).minor_code); println!("[glutin] x error code={} major={} minor={}!", (*event).error_code, (*event).request_code, (*event).minor_code);
}
0 0
} }
@ -36,7 +34,7 @@ fn ensure_thread_init() {
THREAD_INIT.call_once(|| { THREAD_INIT.call_once(|| {
unsafe { unsafe {
ffi::XInitThreads(); ffi::XInitThreads();
ffi::XSetErrorHandler(x_error_callback); ffi::XSetErrorHandler(Some(x_error_callback));
} }
}); });
} }
@ -69,7 +67,7 @@ impl Drop for XWindow {
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, self.context); ffi::glx::DestroyContext(self.display as *mut _, self.context);
if self.is_fullscreen { if self.is_fullscreen {
ffi::XF86VidModeSwitchToMode(self.display, self.screen_id, self.xf86_desk_mode); ffi::XF86VidModeSwitchToMode(self.display, self.screen_id, self.xf86_desk_mode);
@ -92,14 +90,14 @@ pub struct WindowProxy {
impl WindowProxy { impl WindowProxy {
pub fn wakeup_event_loop(&self) { pub fn wakeup_event_loop(&self) {
let mut xev = ffi::XClientMessageEvent { let mut xev = ffi::XClientMessageEvent {
type_: ffi::ClientMessage, _type: ffi::ClientMessage,
window: self.x.window, window: self.x.window,
format: 32, format: 32,
message_type: 0, message_type: 0,
serial: 0, serial: 0,
send_event: 0, send_event: 0,
display: self.x.display, display: self.x.display,
l: [0, 0, 0, 0, 0], data: unsafe { mem::zeroed() },
}; };
unsafe { unsafe {
@ -133,9 +131,9 @@ impl<'a> Iterator for PollEventsIterator<'a> {
} }
} }
match xev.type_ { match xev.get_type() {
ffi::KeymapNotify => { ffi::KeymapNotify => {
unsafe { ffi::XRefreshKeyboardMapping(&xev) } unsafe { ffi::XRefreshKeyboardMapping(mem::transmute(&xev)); }
}, },
ffi::ClientMessage => { ffi::ClientMessage => {
@ -144,7 +142,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
let client_msg: &ffi::XClientMessageEvent = unsafe { mem::transmute(&xev) }; let client_msg: &ffi::XClientMessageEvent = unsafe { mem::transmute(&xev) };
if client_msg.l[0] == self.window.wm_delete_window as libc::c_long { if client_msg.data.get_long(0) == self.window.wm_delete_window as libc::c_long {
self.window.is_closed.store(true, Relaxed); self.window.is_closed.store(true, Relaxed);
return Some(Closed); return Some(Closed);
} else { } else {
@ -173,12 +171,12 @@ impl<'a> Iterator for PollEventsIterator<'a> {
use events::ElementState::{Pressed, Released}; use events::ElementState::{Pressed, Released};
let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) }; let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) };
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 { ffi::XFilterEvent(mem::transmute(raw_ev), self.window.x.window) };
} }
let state = if xev.type_ == ffi::KeyPress { Pressed } else { Released }; let state = if xev.get_type() == ffi::KeyPress { Pressed } else { Released };
let written = unsafe { let written = unsafe {
use std::str; use std::str;
@ -215,7 +213,7 @@ impl<'a> Iterator for PollEventsIterator<'a> {
let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) }; let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
let state = if xev.type_ == ffi::ButtonPress { Pressed } else { Released }; let state = if xev.get_type() == ffi::ButtonPress { Pressed } else { Released };
let button = match event.button { let button = match event.button {
ffi::Button1 => Some(Left), ffi::Button1 => Some(Left),
@ -307,17 +305,17 @@ impl Window {
// getting the FBConfig // getting the FBConfig
let fb_config = unsafe { let fb_config = unsafe {
let mut visual_attributes = vec![ let mut visual_attributes = vec![
ffi::GLX_X_RENDERABLE, 1, ffi::glx::X_RENDERABLE as libc::c_int, 1,
ffi::GLX_DRAWABLE_TYPE, ffi::GLX_WINDOW_BIT, ffi::glx::DRAWABLE_TYPE as libc::c_int, ffi::glx::WINDOW_BIT as libc::c_int,
ffi::GLX_RENDER_TYPE, ffi::GLX_RGBA_BIT, ffi::glx::RENDER_TYPE as libc::c_int, ffi::glx::RGBA_BIT as libc::c_int,
ffi::GLX_X_VISUAL_TYPE, ffi::GLX_TRUE_COLOR, ffi::glx::X_VISUAL_TYPE as libc::c_int, ffi::glx::TRUE_COLOR as libc::c_int,
ffi::GLX_RED_SIZE, 8, ffi::glx::RED_SIZE as libc::c_int, 8,
ffi::GLX_GREEN_SIZE, 8, ffi::glx::GREEN_SIZE as libc::c_int, 8,
ffi::GLX_BLUE_SIZE, 8, ffi::glx::BLUE_SIZE as libc::c_int, 8,
ffi::GLX_ALPHA_SIZE, 8, ffi::glx::ALPHA_SIZE as libc::c_int, 8,
ffi::GLX_DEPTH_SIZE, 24, ffi::glx::DEPTH_SIZE as libc::c_int, 24,
ffi::GLX_STENCIL_SIZE, 8, ffi::glx::STENCIL_SIZE as libc::c_int, 8,
ffi::GLX_DOUBLEBUFFER, 1, ffi::glx::DOUBLEBUFFER as libc::c_int, 1,
]; ];
if let Some(val) = builder.multisampling { if let Some(val) = builder.multisampling {
@ -331,13 +329,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, ffi::XDefaultScreen(display), let fb = ffi::glx::ChooseFBConfig(display as *mut _, ffi::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 *const libc::c_void); ffi::XFree(fb as *mut ());
preferred_fb preferred_fb
}; };
@ -368,12 +366,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, fb_config); let vi = ffi::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 *const libc::c_void); ffi::XFree(vi as *mut _);
vi_copy vi_copy
}; };
@ -383,7 +381,7 @@ impl Window {
// creating the color map // creating the color map
let cmap = unsafe { let cmap = unsafe {
let cmap = ffi::XCreateColormap(display, root, let cmap = ffi::XCreateColormap(display, root,
visual_infos.visual, ffi::AllocNone); visual_infos.visual as *mut _, ffi::AllocNone);
// TODO: error checking? // TODO: error checking?
cmap cmap
}; };
@ -414,8 +412,8 @@ impl Window {
// 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 = ffi::XCreateWindow(display, root, 0, 0, dimensions.0 as libc::c_uint,
dimensions.1 as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput, dimensions.1 as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput as libc::c_uint,
visual_infos.visual, window_attributes, visual_infos.visual as *mut _, window_attributes,
&mut set_win_attr); &mut set_win_attr);
win win
}; };
@ -446,7 +444,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(), ptr::null_mut(), ptr::null_mut()); let im = ffi::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")));
} }
@ -460,7 +458,7 @@ impl Window {
ffi::XCreateIC( ffi::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::<()>()
) )
) )
); );
@ -473,9 +471,9 @@ impl Window {
// Attempt to make keyboard input repeat detectable // Attempt to make keyboard input repeat detectable
unsafe { unsafe {
let mut supported_ptr = false; let mut supported_ptr = ffi::False;
ffi::XkbSetDetectableAutoRepeat(display, true, &mut supported_ptr); ffi::XkbSetDetectableAutoRepeat(display, ffi::True, &mut supported_ptr);
if !supported_ptr { if supported_ptr == ffi::False {
return Err(OsError(format!("XkbSetDetectableAutoRepeat failed"))); return Err(OsError(format!("XkbSetDetectableAutoRepeat failed")));
} }
} }
@ -488,16 +486,16 @@ impl Window {
match builder.gl_version { match builder.gl_version {
GlRequest::Latest => {}, GlRequest::Latest => {},
GlRequest::Specific(Api::OpenGl, (major, minor)) => { GlRequest::Specific(Api::OpenGl, (major, minor)) => {
attributes.push(ffi::GLX_CONTEXT_MAJOR_VERSION); attributes.push(ffi::glx_extra::CONTEXT_MAJOR_VERSION_ARB as libc::c_int);
attributes.push(major as libc::c_int); attributes.push(major as libc::c_int);
attributes.push(ffi::GLX_CONTEXT_MINOR_VERSION); attributes.push(ffi::glx_extra::CONTEXT_MINOR_VERSION_ARB as libc::c_int);
attributes.push(minor as libc::c_int); attributes.push(minor as libc::c_int);
}, },
GlRequest::Specific(_, _) => panic!("Only OpenGL is supported"), GlRequest::Specific(_, _) => panic!("Only OpenGL is supported"),
GlRequest::GlThenGles { opengl_version: (major, minor), .. } => { GlRequest::GlThenGles { opengl_version: (major, minor), .. } => {
attributes.push(ffi::GLX_CONTEXT_MAJOR_VERSION); attributes.push(ffi::glx_extra::CONTEXT_MAJOR_VERSION_ARB as libc::c_int);
attributes.push(major as libc::c_int); attributes.push(major as libc::c_int);
attributes.push(ffi::GLX_CONTEXT_MINOR_VERSION); attributes.push(ffi::glx_extra::CONTEXT_MINOR_VERSION_ARB as libc::c_int);
attributes.push(minor as libc::c_int); attributes.push(minor as libc::c_int);
}, },
} }
@ -531,7 +529,7 @@ impl Window {
}; };
if context.is_null() { if context.is_null() {
context = ffi::glx::CreateContext(display, &mut visual_infos, share, 1) context = ffi::glx::CreateContext(display as *mut _, &mut visual_infos, share, 1)
} }
if context.is_null() { if context.is_null() {
@ -543,7 +541,7 @@ impl Window {
// vsync // vsync
if builder.vsync { if builder.vsync {
unsafe { ffi::glx::MakeCurrent(display, window, context) }; unsafe { ffi::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
@ -555,7 +553,7 @@ impl Window {
if builder.strict { if builder.strict {
let mut swap = unsafe { mem::uninitialized() }; let mut swap = unsafe { mem::uninitialized() };
unsafe { unsafe {
ffi::glx::QueryDrawable(display, window, ffi::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);
} }
@ -581,7 +579,7 @@ impl Window {
return Err(OsError(format!("Couldn't find any available vsync extension"))); return Err(OsError(format!("Couldn't find any available vsync extension")));
} }
unsafe { ffi::glx::MakeCurrent(display, 0, ptr::null()) }; unsafe { ffi::glx::MakeCurrent(display as *mut _, 0, ptr::null()) };
} }
// creating the window object // creating the window object
@ -661,7 +659,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 { ffi::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)> {
@ -695,7 +693,7 @@ impl Window {
} }
pub unsafe fn make_current(&self) { pub unsafe fn make_current(&self) {
let res = ffi::glx::MakeCurrent(self.x.display, self.x.window, self.x.context); let res = ffi::glx::MakeCurrent(self.x.display as *mut _, self.x.window, self.x.context);
if res == 0 { if res == 0 {
panic!("glx::MakeCurrent failed"); panic!("glx::MakeCurrent failed");
} }
@ -716,7 +714,7 @@ impl Window {
} }
pub fn swap_buffers(&self) { pub fn swap_buffers(&self) {
unsafe { ffi::glx::SwapBuffers(self.x.display, self.x.window) } unsafe { ffi::glx::SwapBuffers(self.x.display as *mut _, self.x.window) }
} }
pub fn platform_display(&self) -> *mut libc::c_void { pub fn platform_display(&self) -> *mut libc::c_void {
@ -799,12 +797,12 @@ impl Window {
*cursor_state = CursorState::Grab; *cursor_state = CursorState::Grab;
match ffi::XGrabPointer( match ffi::XGrabPointer(
self.x.display, self.x.window, 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 |
ffi::Button1MotionMask | ffi::Button2MotionMask | ffi::Button3MotionMask | ffi::Button1MotionMask | ffi::Button2MotionMask | ffi::Button3MotionMask |
ffi::Button4MotionMask | ffi::Button5MotionMask | ffi::ButtonMotionMask | ffi::Button4MotionMask | ffi::Button5MotionMask | ffi::ButtonMotionMask |
ffi::KeymapStateMask, ffi::KeymapStateMask) as libc::c_uint,
ffi::GrabModeAsync, ffi::GrabModeAsync, ffi::GrabModeAsync, ffi::GrabModeAsync,
self.x.window, 0, ffi::CurrentTime self.x.window, 0, ffi::CurrentTime
) { ) {