2014-08-02 18:42:17 +10:00
|
|
|
use {Event, WindowBuilder};
|
2014-07-27 23:10:58 +10:00
|
|
|
use libc;
|
|
|
|
use std::{mem, ptr};
|
2014-07-27 23:25:04 +10:00
|
|
|
use std::sync::atomics::AtomicBool;
|
2014-07-27 23:10:58 +10:00
|
|
|
|
2014-09-19 23:42:47 +10:00
|
|
|
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
|
|
|
|
2014-07-28 22:15:02 +10:00
|
|
|
mod events;
|
2014-07-27 23:10:58 +10:00
|
|
|
mod ffi;
|
2014-09-19 23:42:47 +10:00
|
|
|
mod monitor;
|
2014-07-27 23:10:58 +10:00
|
|
|
|
|
|
|
pub struct Window {
|
|
|
|
display: *mut ffi::Display,
|
|
|
|
window: ffi::Window,
|
2014-08-01 02:42:14 +10:00
|
|
|
im: ffi::XIM,
|
|
|
|
ic: ffi::XIC,
|
2014-07-27 23:10:58 +10:00
|
|
|
context: ffi::GLXContext,
|
2014-07-30 21:29:28 +10:00
|
|
|
is_closed: AtomicBool,
|
2014-07-27 23:25:04 +10:00
|
|
|
wm_delete_window: ffi::Atom,
|
2014-09-04 19:38:33 +10:00
|
|
|
xf86_desk_mode: *mut ffi::XF86VidModeModeInfo,
|
|
|
|
screen_id: libc::c_int,
|
|
|
|
is_fullscreen: bool,
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
2014-08-02 18:42:17 +10:00
|
|
|
pub fn new(builder: WindowBuilder) -> Result<Window, String> {
|
2014-09-04 19:38:33 +10:00
|
|
|
let dimensions = builder.dimensions.unwrap_or((800, 600));
|
2014-08-02 18:42:17 +10:00
|
|
|
|
2014-07-27 23:10:58 +10:00
|
|
|
// calling XOpenDisplay
|
|
|
|
let display = unsafe {
|
|
|
|
let display = ffi::XOpenDisplay(ptr::null());
|
|
|
|
if display.is_null() {
|
|
|
|
return Err(format!("XOpenDisplay failed"));
|
|
|
|
}
|
|
|
|
display
|
|
|
|
};
|
|
|
|
|
2014-09-04 19:38:33 +10:00
|
|
|
let screen_id = unsafe {
|
|
|
|
ffi::XDefaultScreen(display)
|
|
|
|
};
|
2014-08-08 01:35:26 +10:00
|
|
|
|
|
|
|
// getting the FBConfig
|
|
|
|
let fb_config = unsafe {
|
|
|
|
static VISUAL_ATTRIBUTES: [libc::c_int, ..23] = [
|
|
|
|
ffi::GLX_X_RENDERABLE, 1,
|
|
|
|
ffi::GLX_DRAWABLE_TYPE, ffi::GLX_WINDOW_BIT,
|
|
|
|
ffi::GLX_RENDER_TYPE, ffi::GLX_RGBA_BIT,
|
|
|
|
ffi::GLX_X_VISUAL_TYPE, ffi::GLX_TRUE_COLOR,
|
|
|
|
ffi::GLX_RED_SIZE, 8,
|
|
|
|
ffi::GLX_GREEN_SIZE, 8,
|
|
|
|
ffi::GLX_BLUE_SIZE, 8,
|
|
|
|
ffi::GLX_ALPHA_SIZE, 8,
|
|
|
|
ffi::GLX_DEPTH_SIZE, 24,
|
|
|
|
ffi::GLX_STENCIL_SIZE, 8,
|
|
|
|
ffi::GLX_DOUBLEBUFFER, 1,
|
|
|
|
0
|
|
|
|
];
|
|
|
|
|
|
|
|
let mut num_fb: libc::c_int = mem::uninitialized();
|
|
|
|
|
|
|
|
let fb = ffi::glXChooseFBConfig(display, ffi::XDefaultScreen(display),
|
|
|
|
VISUAL_ATTRIBUTES.as_ptr(), &mut num_fb);
|
|
|
|
if fb.is_null() {
|
|
|
|
return Err(format!("glXChooseFBConfig failed"));
|
|
|
|
}
|
|
|
|
let preferred_fb = *fb; // TODO: choose more wisely
|
|
|
|
ffi::XFree(fb as *const libc::c_void);
|
|
|
|
preferred_fb
|
|
|
|
};
|
2014-07-27 23:10:58 +10:00
|
|
|
|
2014-09-04 19:38:33 +10:00
|
|
|
let mut best_mode = -1;
|
|
|
|
let modes = unsafe {
|
|
|
|
let mut mode_num: libc::c_int = mem::uninitialized();
|
|
|
|
let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized();
|
|
|
|
if ffi::XF86VidModeGetAllModeLines(display, screen_id, &mut mode_num, &mut modes) == 0 {
|
|
|
|
return Err(format!("Could not query the video modes"));
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in range(0, mode_num) {
|
|
|
|
let mode: ffi::XF86VidModeModeInfo = **modes.offset(i as int);
|
|
|
|
if mode.hdisplay == dimensions.val0() as u16 && mode.vdisplay == dimensions.val1() as u16 {
|
|
|
|
best_mode = i;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if best_mode == -1 {
|
|
|
|
return Err(format!("Could not find a suitable graphics mode"));
|
|
|
|
}
|
|
|
|
|
|
|
|
modes
|
|
|
|
};
|
|
|
|
|
|
|
|
let xf86_desk_mode = unsafe {
|
|
|
|
*modes.offset(0)
|
|
|
|
};
|
|
|
|
|
2014-07-27 23:10:58 +10:00
|
|
|
// getting the visual infos
|
|
|
|
let visual_infos = unsafe {
|
2014-08-08 01:35:26 +10:00
|
|
|
let vi = ffi::glXGetVisualFromFBConfig(display, fb_config);
|
2014-07-27 23:10:58 +10:00
|
|
|
if vi.is_null() {
|
|
|
|
return Err(format!("glXChooseVisual failed"));
|
|
|
|
}
|
2014-08-08 01:35:26 +10:00
|
|
|
let vi_copy = *vi;
|
|
|
|
ffi::XFree(vi as *const libc::c_void);
|
|
|
|
vi_copy
|
2014-07-27 23:10:58 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
// getting the root window
|
|
|
|
let root = unsafe { ffi::XDefaultRootWindow(display) };
|
|
|
|
|
|
|
|
// creating the color map
|
|
|
|
let cmap = unsafe {
|
|
|
|
let cmap = ffi::XCreateColormap(display, root,
|
2014-08-08 01:35:26 +10:00
|
|
|
visual_infos.visual, ffi::AllocNone);
|
2014-07-27 23:10:58 +10:00
|
|
|
// TODO: error checking?
|
|
|
|
cmap
|
|
|
|
};
|
|
|
|
|
|
|
|
// creating
|
|
|
|
let mut set_win_attr = {
|
|
|
|
let mut swa: ffi::XSetWindowAttributes = unsafe { mem::zeroed() };
|
|
|
|
swa.colormap = cmap;
|
2014-07-27 23:25:04 +10:00
|
|
|
swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask |
|
2014-07-28 22:15:02 +10:00
|
|
|
ffi::VisibilityChangeMask | ffi::KeyPressMask | ffi::PointerMotionMask |
|
2014-09-04 19:38:33 +10:00
|
|
|
ffi::KeyReleaseMask | ffi::ButtonPressMask |
|
2014-08-01 02:42:14 +10:00
|
|
|
ffi::ButtonReleaseMask | ffi::KeymapStateMask;
|
2014-09-04 19:38:33 +10:00
|
|
|
swa.border_pixel = 0;
|
|
|
|
swa.override_redirect = 0;
|
2014-07-27 23:10:58 +10:00
|
|
|
swa
|
|
|
|
};
|
|
|
|
|
2014-09-04 19:38:33 +10:00
|
|
|
let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask;
|
2014-09-18 22:03:03 +10:00
|
|
|
if builder.monitor.is_some() {
|
2014-09-04 19:38:33 +10:00
|
|
|
window_attributes |= ffi::CWOverrideRedirect;
|
|
|
|
unsafe {
|
|
|
|
ffi::XF86VidModeSwitchToMode(display, screen_id, *modes.offset(best_mode as int));
|
|
|
|
ffi::XF86VidModeSetViewPort(display, screen_id, 0, 0);
|
|
|
|
set_win_attr.override_redirect = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 23:10:58 +10:00
|
|
|
// finally creating the window
|
|
|
|
let window = unsafe {
|
2014-09-04 19:38:33 +10:00
|
|
|
let win = ffi::XCreateWindow(display, root, 50, 50, dimensions.val0() as libc::c_uint,
|
2014-08-08 01:35:26 +10:00
|
|
|
dimensions.val1() as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput,
|
2014-09-04 19:38:33 +10:00
|
|
|
visual_infos.visual, window_attributes,
|
2014-07-27 23:54:48 +10:00
|
|
|
&mut set_win_attr);
|
2014-07-27 23:10:58 +10:00
|
|
|
win
|
|
|
|
};
|
|
|
|
|
2014-07-27 23:25:04 +10:00
|
|
|
// creating window, step 2
|
|
|
|
let wm_delete_window = unsafe {
|
|
|
|
use std::c_str::ToCStr;
|
|
|
|
|
|
|
|
ffi::XMapWindow(display, window);
|
|
|
|
let mut wm_delete_window = ffi::XInternAtom(display,
|
|
|
|
"WM_DELETE_WINDOW".to_c_str().as_ptr() as *const libc::c_char, 0);
|
|
|
|
ffi::XSetWMProtocols(display, window, &mut wm_delete_window, 1);
|
2014-08-08 01:36:48 +10:00
|
|
|
ffi::XStoreName(display, window, mem::transmute(builder.title.as_slice().as_ptr()));
|
2014-07-27 23:25:04 +10:00
|
|
|
ffi::XFlush(display);
|
|
|
|
|
|
|
|
wm_delete_window
|
|
|
|
};
|
2014-07-27 23:10:58 +10:00
|
|
|
|
2014-08-08 01:53:41 +10:00
|
|
|
// getting the pointer to glXCreateContextAttribs
|
|
|
|
let create_context_attribs = unsafe {
|
2014-08-12 17:42:48 +10:00
|
|
|
let mut addr = ffi::glXGetProcAddress(b"glXCreateContextAttribs".as_ptr()
|
|
|
|
as *const u8) as *const ();
|
2014-08-08 02:03:56 +10:00
|
|
|
|
2014-08-12 17:42:48 +10:00
|
|
|
if addr.is_null() {
|
|
|
|
addr = ffi::glXGetProcAddress(b"glXCreateContextAttribsARB".as_ptr()
|
2014-08-08 02:05:51 +10:00
|
|
|
as *const u8) as *const ();
|
2014-08-12 17:42:48 +10:00
|
|
|
}
|
2014-09-04 19:38:33 +10:00
|
|
|
|
2014-08-12 17:42:48 +10:00
|
|
|
addr.to_option().map(|addr| {
|
|
|
|
let addr: extern "system" fn(*mut ffi::Display, ffi::GLXFBConfig, ffi::GLXContext,
|
|
|
|
ffi::Bool, *const libc::c_int) -> ffi::GLXContext = mem::transmute(addr);
|
|
|
|
addr
|
|
|
|
})
|
2014-08-08 01:53:41 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
// creating IM
|
2014-08-01 02:42:14 +10:00
|
|
|
let im = unsafe {
|
|
|
|
let im = ffi::XOpenIM(display, ptr::null(), ptr::mut_null(), ptr::mut_null());
|
|
|
|
if im.is_null() {
|
|
|
|
return Err(format!("XOpenIM failed"));
|
|
|
|
}
|
|
|
|
im
|
|
|
|
};
|
|
|
|
|
|
|
|
// creating input context
|
|
|
|
let ic = unsafe {
|
|
|
|
use std::c_str::ToCStr;
|
|
|
|
|
|
|
|
let ic = ffi::XCreateIC(im, "inputStyle".to_c_str().as_ptr(),
|
|
|
|
ffi::XIMPreeditNothing | ffi::XIMStatusNothing, "clientWindow".to_c_str().as_ptr(),
|
|
|
|
window, ptr::null());
|
|
|
|
if ic.is_null() {
|
|
|
|
return Err(format!("XCreateIC failed"));
|
|
|
|
}
|
|
|
|
ffi::XSetICFocus(ic);
|
|
|
|
ic
|
|
|
|
};
|
|
|
|
|
2014-09-15 08:12:37 +10:00
|
|
|
// Attempt to make keyboard input repeat detectable
|
|
|
|
unsafe {
|
|
|
|
let mut supported_ptr = false;
|
|
|
|
ffi::XkbSetDetectableAutoRepeat(display, true, &mut supported_ptr);
|
|
|
|
if !supported_ptr {
|
|
|
|
return Err(format!("XkbSetDetectableAutoRepeat failed"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-27 23:10:58 +10:00
|
|
|
// creating GL context
|
|
|
|
let context = unsafe {
|
2014-08-08 01:53:41 +10:00
|
|
|
let mut attributes = Vec::new();
|
|
|
|
|
|
|
|
if builder.gl_version.is_some() {
|
|
|
|
let version = builder.gl_version.as_ref().unwrap();
|
|
|
|
attributes.push(ffi::GLX_CONTEXT_MAJOR_VERSION);
|
|
|
|
attributes.push(version.val0() as libc::c_int);
|
|
|
|
attributes.push(ffi::GLX_CONTEXT_MINOR_VERSION);
|
|
|
|
attributes.push(version.val1() as libc::c_int);
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes.push(0);
|
2014-09-04 19:38:33 +10:00
|
|
|
|
2014-08-08 01:53:41 +10:00
|
|
|
let context = if create_context_attribs.is_some() {
|
|
|
|
let create_context_attribs = create_context_attribs.unwrap();
|
|
|
|
create_context_attribs(display, fb_config, ptr::null(), 1,
|
|
|
|
attributes.as_ptr())
|
|
|
|
} else {
|
2014-09-04 19:38:33 +10:00
|
|
|
ffi::glXCreateContext(display, &visual_infos, ptr::null(), 1)
|
2014-08-08 01:53:41 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
if context.is_null() {
|
|
|
|
return Err(format!("GL context creation failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
context
|
2014-07-27 23:10:58 +10:00
|
|
|
};
|
|
|
|
|
2014-08-08 02:13:45 +10:00
|
|
|
// creating the window object
|
|
|
|
let window = Window {
|
2014-07-27 23:10:58 +10:00
|
|
|
display: display,
|
|
|
|
window: window,
|
2014-08-01 02:42:14 +10:00
|
|
|
im: im,
|
|
|
|
ic: ic,
|
2014-07-27 23:10:58 +10:00
|
|
|
context: context,
|
2014-07-30 21:29:28 +10:00
|
|
|
is_closed: AtomicBool::new(false),
|
2014-07-27 23:25:04 +10:00
|
|
|
wm_delete_window: wm_delete_window,
|
2014-09-04 19:38:33 +10:00
|
|
|
xf86_desk_mode: xf86_desk_mode,
|
|
|
|
screen_id: screen_id,
|
2014-09-18 22:03:03 +10:00
|
|
|
is_fullscreen: builder.monitor.is_some(),
|
2014-08-08 02:13:45 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
// calling glViewport
|
|
|
|
unsafe {
|
|
|
|
let ptr = window.get_proc_address("glViewport");
|
|
|
|
assert!(!ptr.is_null());
|
|
|
|
let ptr: extern "system" fn(libc::c_int, libc::c_int, libc::c_int, libc::c_int) =
|
|
|
|
mem::transmute(ptr);
|
|
|
|
let dimensions = window.get_inner_size().unwrap();
|
|
|
|
ptr(0, 0, dimensions.val0() as libc::c_int, dimensions.val1() as libc::c_int);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returning
|
|
|
|
Ok(window)
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:29:28 +10:00
|
|
|
pub fn is_closed(&self) -> bool {
|
2014-07-27 23:25:04 +10:00
|
|
|
use std::sync::atomics::Relaxed;
|
2014-07-30 21:29:28 +10:00
|
|
|
self.is_closed.load(Relaxed)
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_title(&self, title: &str) {
|
|
|
|
unsafe {
|
|
|
|
ffi::XStoreName(self.display, self.window,
|
|
|
|
mem::transmute(title.as_slice().as_ptr()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 01:01:58 +10:00
|
|
|
fn get_geometry(&self) -> Option<(int, int, uint, uint)> {
|
|
|
|
unsafe {
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
let mut root: ffi::Window = mem::uninitialized();
|
|
|
|
let mut x: libc::c_int = mem::uninitialized();
|
|
|
|
let mut y: libc::c_int = mem::uninitialized();
|
|
|
|
let mut width: libc::c_uint = mem::uninitialized();
|
|
|
|
let mut height: libc::c_uint = mem::uninitialized();
|
|
|
|
let mut border: libc::c_uint = mem::uninitialized();
|
|
|
|
let mut depth: libc::c_uint = mem::uninitialized();
|
|
|
|
|
|
|
|
if ffi::XGetGeometry(self.display, self.window,
|
|
|
|
&mut root, &mut x, &mut y, &mut width, &mut height,
|
|
|
|
&mut border, &mut depth) == 0
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some((x as int, y as int, width as uint, height as uint))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_position(&self) -> Option<(int, int)> {
|
2014-08-01 01:01:58 +10:00
|
|
|
self.get_geometry().map(|(x, y, _, _)| (x, y))
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
2014-08-08 02:07:48 +10:00
|
|
|
pub fn set_position(&self, x: int, y: int) {
|
2014-08-02 20:47:32 +10:00
|
|
|
unsafe { ffi::XMoveWindow(self.display, self.window, x as libc::c_int, y as libc::c_int) }
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_inner_size(&self) -> Option<(uint, uint)> {
|
2014-08-01 01:01:58 +10:00
|
|
|
self.get_geometry().map(|(_, _, w, h)| (w, h))
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_outer_size(&self) -> Option<(uint, uint)> {
|
2014-07-28 06:31:00 +10:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_inner_size(&self, x: uint, y: uint) {
|
2014-07-27 23:10:58 +10:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_events(&self) -> Vec<Event> {
|
2014-07-27 23:21:42 +10:00
|
|
|
use std::mem;
|
2014-09-04 19:38:33 +10:00
|
|
|
|
2014-07-27 23:25:04 +10:00
|
|
|
let mut events = Vec::new();
|
2014-09-04 19:38:33 +10:00
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
loop {
|
|
|
|
use std::num::Bounded;
|
2014-07-27 23:25:04 +10:00
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
let mut xev = unsafe { mem::uninitialized() };
|
|
|
|
let res = unsafe { ffi::XCheckMaskEvent(self.display, Bounded::max_value(), &mut xev) };
|
2014-07-27 23:25:04 +10:00
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
if res == 0 {
|
|
|
|
let res = unsafe { ffi::XCheckTypedEvent(self.display, ffi::ClientMessage, &mut xev) };
|
2014-07-28 22:15:02 +10:00
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
if res == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 21:25:28 +10:00
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
match xev.type_ {
|
2014-08-01 02:42:14 +10:00
|
|
|
ffi::KeymapNotify => {
|
|
|
|
unsafe { ffi::XRefreshKeyboardMapping(&xev) }
|
|
|
|
},
|
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
ffi::ClientMessage => {
|
|
|
|
use Closed;
|
|
|
|
use std::sync::atomics::Relaxed;
|
|
|
|
|
|
|
|
let client_msg: &ffi::XClientMessageEvent = unsafe { mem::transmute(&xev) };
|
|
|
|
|
|
|
|
if client_msg.l[0] == self.wm_delete_window as libc::c_long {
|
2014-07-30 21:29:28 +10:00
|
|
|
self.is_closed.store(true, Relaxed);
|
2014-07-28 23:06:38 +10:00
|
|
|
events.push(Closed);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ffi::ResizeRequest => {
|
2014-07-30 21:11:49 +10:00
|
|
|
use Resized;
|
2014-07-28 23:06:38 +10:00
|
|
|
let rs_event: &ffi::XResizeRequestEvent = unsafe { mem::transmute(&xev) };
|
2014-07-30 21:11:49 +10:00
|
|
|
events.push(Resized(rs_event.width as uint, rs_event.height as uint));
|
2014-07-28 23:06:38 +10:00
|
|
|
},
|
|
|
|
|
|
|
|
ffi::MotionNotify => {
|
2014-08-14 01:04:57 +10:00
|
|
|
use MouseMoved;
|
2014-07-28 23:06:38 +10:00
|
|
|
let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) };
|
2014-08-14 01:04:57 +10:00
|
|
|
events.push(MouseMoved((event.x as int, event.y as int)));
|
2014-07-28 23:06:38 +10:00
|
|
|
},
|
|
|
|
|
|
|
|
ffi::KeyPress | ffi::KeyRelease => {
|
2014-08-14 01:04:57 +10:00
|
|
|
use {KeyboardInput, Pressed, Released, ReceivedCharacter, KeyModifiers};
|
2014-08-01 02:42:14 +10:00
|
|
|
let event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&xev) };
|
|
|
|
|
|
|
|
if event.type_ == ffi::KeyPress {
|
|
|
|
let raw_ev: *mut ffi::XKeyEvent = event;
|
|
|
|
unsafe { ffi::XFilterEvent(mem::transmute(raw_ev), self.window) };
|
|
|
|
}
|
2014-07-28 23:06:38 +10:00
|
|
|
|
2014-08-14 01:04:57 +10:00
|
|
|
let state = if xev.type_ == ffi::KeyPress { Pressed } else { Released };
|
2014-07-28 23:06:38 +10:00
|
|
|
|
2014-08-01 02:42:14 +10:00
|
|
|
let written = unsafe {
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
let mut buffer: [u8, ..16] = [mem::uninitialized(), ..16];
|
|
|
|
let raw_ev: *mut ffi::XKeyEvent = event;
|
2014-09-04 19:38:33 +10:00
|
|
|
let count = ffi::Xutf8LookupString(self.ic, mem::transmute(raw_ev),
|
2014-08-01 02:42:14 +10:00
|
|
|
mem::transmute(buffer.as_mut_ptr()),
|
|
|
|
buffer.len() as libc::c_int, ptr::mut_null(), ptr::mut_null());
|
|
|
|
|
2014-08-14 01:04:57 +10:00
|
|
|
str::from_utf8(buffer.as_slice().slice_to(count as uint))
|
|
|
|
.unwrap_or("").to_string()
|
2014-08-01 02:42:14 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
for chr in written.as_slice().chars() {
|
|
|
|
events.push(ReceivedCharacter(chr));
|
|
|
|
}
|
|
|
|
|
2014-08-14 01:04:57 +10:00
|
|
|
let keysym = unsafe {
|
|
|
|
ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
let vkey = events::keycode_to_element(keysym as libc::c_uint);
|
|
|
|
|
|
|
|
events.push(KeyboardInput(state, event.keycode as u8,
|
|
|
|
vkey, KeyModifiers::empty()));
|
2014-07-28 23:06:38 +10:00
|
|
|
//
|
|
|
|
},
|
|
|
|
|
|
|
|
ffi::ButtonPress | ffi::ButtonRelease => {
|
2014-08-14 01:04:57 +10:00
|
|
|
use {MouseInput, Pressed, Released};
|
|
|
|
use {LeftMouseButton, RightMouseButton, MiddleMouseButton, OtherMouseButton};
|
2014-07-28 23:06:38 +10:00
|
|
|
let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
|
2014-08-01 01:33:46 +10:00
|
|
|
|
2014-08-14 01:04:57 +10:00
|
|
|
let state = if xev.type_ == ffi::ButtonPress { Pressed } else { Released };
|
|
|
|
|
|
|
|
let button = match event.button {
|
|
|
|
ffi::Button1 => Some(LeftMouseButton),
|
|
|
|
ffi::Button2 => Some(MiddleMouseButton),
|
|
|
|
ffi::Button3 => Some(RightMouseButton),
|
|
|
|
ffi::Button4 => Some(OtherMouseButton(4)),
|
|
|
|
ffi::Button5 => Some(OtherMouseButton(5)),
|
2014-08-01 01:33:46 +10:00
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
2014-08-14 01:04:57 +10:00
|
|
|
match button {
|
2014-09-04 19:38:33 +10:00
|
|
|
Some(button) =>
|
2014-08-14 01:04:57 +10:00
|
|
|
events.push(MouseInput(state, button)),
|
|
|
|
None => ()
|
|
|
|
};
|
2014-07-28 23:06:38 +10:00
|
|
|
},
|
|
|
|
|
|
|
|
_ => ()
|
|
|
|
}
|
2014-07-27 23:25:04 +10:00
|
|
|
}
|
2014-07-27 23:21:42 +10:00
|
|
|
|
2014-07-27 23:25:04 +10:00
|
|
|
events
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
|
2014-07-28 23:06:38 +10:00
|
|
|
pub fn wait_events(&self) -> Vec<Event> {
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// this will block until an event arrives, but doesn't remove
|
|
|
|
// it from the queue
|
|
|
|
let mut xev = unsafe { mem::uninitialized() };
|
|
|
|
unsafe { ffi::XPeekEvent(self.display, &mut xev) };
|
|
|
|
|
|
|
|
// calling poll_events()
|
|
|
|
let ev = self.poll_events();
|
|
|
|
if ev.len() >= 1 {
|
|
|
|
return ev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 02:12:39 +10:00
|
|
|
pub unsafe fn make_current(&self) {
|
|
|
|
let res = ffi::glXMakeCurrent(self.display, self.window, self.context);
|
2014-07-27 23:10:58 +10:00
|
|
|
if res == 0 {
|
|
|
|
fail!("glXMakeCurrent failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_proc_address(&self, addr: &str) -> *const () {
|
|
|
|
use std::c_str::ToCStr;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
addr.with_c_str(|s| {
|
2014-08-22 18:56:49 +10:00
|
|
|
ffi::glXGetProcAddress(mem::transmute(s)) as *const ()
|
2014-07-27 23:10:58 +10:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swap_buffers(&self) {
|
|
|
|
unsafe { ffi::glXSwapBuffers(self.display, self.window) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Window {
|
|
|
|
fn drop(&mut self) {
|
2014-09-04 19:38:33 +10:00
|
|
|
unsafe { ffi::glXMakeCurrent(self.display, 0, ptr::null()); }
|
2014-08-01 02:42:14 +10:00
|
|
|
unsafe { ffi::glXDestroyContext(self.display, self.context); }
|
2014-09-04 19:38:33 +10:00
|
|
|
|
|
|
|
if self.is_fullscreen {
|
|
|
|
unsafe { ffi::XF86VidModeSwitchToMode(self.display, self.screen_id, self.xf86_desk_mode); }
|
|
|
|
unsafe { ffi::XF86VidModeSetViewPort(self.display, self.screen_id, 0, 0); }
|
|
|
|
}
|
|
|
|
|
2014-08-01 02:42:14 +10:00
|
|
|
unsafe { ffi::XDestroyIC(self.ic); }
|
|
|
|
unsafe { ffi::XCloseIM(self.im); }
|
|
|
|
unsafe { ffi::XDestroyWindow(self.display, self.window); }
|
|
|
|
unsafe { ffi::XCloseDisplay(self.display); }
|
2014-07-27 23:10:58 +10:00
|
|
|
}
|
|
|
|
}
|