winit-sonoma-fix/src/x11/mod.rs

382 lines
12 KiB
Rust
Raw Normal View History

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-07-28 22:15:02 +10:00
mod events;
2014-07-27 23:10:58 +10:00
mod ffi;
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-07-27 23:10:58 +10:00
}
pub struct MonitorID(uint);
2014-07-31 17:56:53 +10:00
pub fn get_available_monitors() -> Vec<MonitorID> {
unimplemented!()
}
pub fn get_primary_monitor() -> MonitorID {
unimplemented!()
}
2014-07-31 18:52:05 +10:00
impl MonitorID {
pub fn get_name(&self) -> Option<String> {
Some("<Unknown>".to_string())
}
}
2014-07-27 23:10:58 +10:00
impl Window {
pub fn new(builder: WindowBuilder) -> Result<Window, String> {
// TODO: temporary
let dimensions = Some(builder.dimensions);
let title = builder.title.as_slice();
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
};
// TODO: set error handler
static VISUAL_ATTRIBUTES: [libc::c_int, ..5] = [
ffi::GLX_RGBA,
ffi::GLX_DEPTH_SIZE,
24,
ffi::GLX_DOUBLEBUFFER,
0
];
// getting the visual infos
let visual_infos = unsafe {
let vi = ffi::glXChooseVisual(display, 0, VISUAL_ATTRIBUTES.as_ptr());
if vi.is_null() {
return Err(format!("glXChooseVisual failed"));
}
vi
};
// getting the root window
let root = unsafe { ffi::XDefaultRootWindow(display) };
// creating the color map
let cmap = unsafe {
let cmap = ffi::XCreateColormap(display, root,
(*visual_infos).visual, ffi::AllocNone);
// 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 |
ffi::KeyPressMask | ffi::KeyReleaseMask | ffi::ButtonPressMask |
2014-08-01 02:42:14 +10:00
ffi::ButtonReleaseMask | ffi::KeymapStateMask;
2014-07-27 23:10:58 +10:00
swa
};
// finally creating the window
let window = unsafe {
2014-07-27 23:54:48 +10:00
let dimensions = dimensions.unwrap_or((800, 600));
let win = ffi::XCreateWindow(display, root, 50, 50, dimensions.val0() as libc::c_uint,
dimensions.val1() as libc::c_uint, 0, (*visual_infos).depth, ffi::InputOutput,
(*visual_infos).visual, ffi::CWColormap | ffi::CWEventMask,
&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);
ffi::XStoreName(display, window, mem::transmute(title.as_slice().as_ptr()));
ffi::XFlush(display);
wm_delete_window
};
2014-07-27 23:10:58 +10:00
2014-08-01 02:42:14 +10:00
// creating
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-07-27 23:10:58 +10:00
// creating GL context
let context = unsafe {
ffi::glXCreateContext(display, visual_infos, ptr::null(), 1)
};
// returning
Ok(Window{
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-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()));
}
}
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))
}
}
pub fn get_position(&self) -> Option<(int, int)> {
self.get_geometry().map(|(x, y, _, _)| (x, y))
2014-07-27 23:10:58 +10:00
}
pub fn set_position(&self, x: uint, y: uint) {
unimplemented!()
}
pub fn get_inner_size(&self) -> Option<(uint, uint)> {
self.get_geometry().map(|(_, _, w, h)| (w, h))
2014-07-27 23:10:58 +10:00
}
pub fn get_outer_size(&self) -> Option<(uint, uint)> {
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-07-28 23:06:38 +10:00
2014-07-27 23:25:04 +10:00
let mut events = Vec::new();
2014-07-27 23:21:42 +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 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 => {
use CursorPositionChanged;
let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) };
events.push(CursorPositionChanged(event.x as uint, event.y as uint));
},
ffi::KeyPress | ffi::KeyRelease => {
2014-08-01 02:42:14 +10:00
use {Pressed, Released, ReceivedCharacter};
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
let keysym = unsafe { ffi::XKeycodeToKeysym(self.display, event.keycode as ffi::KeyCode, 0) };
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;
let count = ffi::Xutf8LookupString(self.ic, mem::transmute(raw_ev),
mem::transmute(buffer.as_mut_ptr()),
buffer.len() as libc::c_int, ptr::mut_null(), ptr::mut_null());
str::from_utf8(buffer.as_slice().slice_to(count as uint)).unwrap_or("").to_string()
};
for chr in written.as_slice().chars() {
events.push(ReceivedCharacter(chr));
}
2014-07-28 23:06:38 +10:00
match events::keycode_to_element(keysym as libc::c_uint) {
Some(elem) if xev.type_ == ffi::KeyPress => {
events.push(Pressed(elem));
},
Some(elem) if xev.type_ == ffi::KeyRelease => {
events.push(Released(elem));
},
_ => ()
}
//
},
ffi::ButtonPress | ffi::ButtonRelease => {
use {Pressed, Released};
use events;
2014-07-28 23:06:38 +10:00
let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
let elem = match event.button {
ffi::Button1 => Some(events::Button1),
ffi::Button2 => Some(events::Button2),
ffi::Button3 => Some(events::Button3),
ffi::Button4 => Some(events::Button4),
ffi::Button5 => Some(events::Button5),
_ => None
};
if elem.is_some() {
let elem = elem.unwrap();
2014-08-01 02:42:14 +10:00
if xev.type_ == ffi::ButtonPress {
events.push(Pressed(elem));
} else if xev.type_ == ffi::ButtonRelease {
events.push(Released(elem));
}
}
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| {
let p = ffi::glXGetProcAddress(mem::transmute(s)) as *const ();
if !p.is_null() { return p; }
println!("{}", p);
p
})
}
}
pub fn swap_buffers(&self) {
unsafe { ffi::glXSwapBuffers(self.display, self.window) }
}
}
impl Drop for Window {
fn drop(&mut self) {
2014-08-01 02:42:14 +10:00
unsafe { ffi::glXDestroyContext(self.display, self.context); }
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
}
}