aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.travis.yml6
-rw-r--r--Cargo.toml4
-rw-r--r--examples/fullscreen.rs4
-rw-r--r--examples/multiwindow.rs5
-rw-r--r--examples/support/mod.rs2
-rw-r--r--examples/window.rs4
-rw-r--r--src/lib.rs82
-rw-r--r--src/osx/mod.rs34
-rw-r--r--src/win32/init.rs40
-rw-r--r--src/win32/mod.rs39
-rw-r--r--src/x11/ffi.rs24
-rw-r--r--src/x11/headless.rs47
-rw-r--r--src/x11/mod.rs502
-rw-r--r--src/x11/window/events.rs (renamed from src/x11/events.rs)2
-rw-r--r--src/x11/window/mod.rs499
-rw-r--r--src/x11/window/monitor.rs (renamed from src/x11/monitor.rs)2
-rw-r--r--tests/headless.rs13
17 files changed, 791 insertions, 518 deletions
diff --git a/.travis.yml b/.travis.yml
index f209793..7b05a9a 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,7 +1,11 @@
language: rust
install:
- - sudo apt-get install libXxf86vm-dev
+ - sudo apt-get install libXxf86vm-dev libosmesa6-dev
+
+script:
+ - cargo build --verbose
+ - cargo test --verbose --features "headless" --no-default-features
os:
- linux
diff --git a/Cargo.toml b/Cargo.toml
index e9bcb98..b50db83 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,7 +5,9 @@ version = "0.0.1"
authors = ["tomaka <pierre.krieger1708@gmail.com>"]
[features]
-default = ["cocoa"]
+default = ["cocoa", "window"]
+window = []
+headless = []
[dependencies.compile_msg]
git = "https://github.com/huonw/compile_msg"
diff --git a/examples/fullscreen.rs b/examples/fullscreen.rs
index cbe0303..011a7d3 100644
--- a/examples/fullscreen.rs
+++ b/examples/fullscreen.rs
@@ -14,6 +14,10 @@ mod support;
#[cfg(target_os = "android")]
android_start!(main)
+#[cfg(not(feature = "window"))]
+fn main() { println!("This example requires glutin to be compiled with the `window` feature"); }
+
+#[cfg(feature = "window")]
fn main() {
// enumerating monitors
let monitor = {
diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs
index ad50312..ab48b6b 100644
--- a/examples/multiwindow.rs
+++ b/examples/multiwindow.rs
@@ -12,6 +12,10 @@ mod support;
#[cfg(target_os = "android")]
android_start!(main)
+#[cfg(not(feature = "window"))]
+fn main() { println!("This example requires glutin to be compiled with the `window` feature"); }
+
+#[cfg(feature = "window")]
fn main() {
let window1 = glutin::Window::new().unwrap();
let window2 = glutin::Window::new().unwrap();
@@ -30,6 +34,7 @@ fn main() {
});
}
+#[cfg(feature = "window")]
fn run(window: glutin::Window, color: (f32, f32, f32, f32)) {
unsafe { window.make_current() };
diff --git a/examples/support/mod.rs b/examples/support/mod.rs
index 154b01e..f27c4e7 100644
--- a/examples/support/mod.rs
+++ b/examples/support/mod.rs
@@ -1,3 +1,5 @@
+#![cfg(feature = "window")]
+
#[phase(plugin)]
extern crate gl_generator;
diff --git a/examples/window.rs b/examples/window.rs
index b2ad82e..769712a 100644
--- a/examples/window.rs
+++ b/examples/window.rs
@@ -12,6 +12,10 @@ mod support;
#[cfg(target_os = "android")]
android_start!(main)
+#[cfg(not(feature = "window"))]
+fn main() { println!("This example requires glutin to be compiled with the `window` feature"); }
+
+#[cfg(feature = "window")]
fn main() {
let window = glutin::Window::new().unwrap();
diff --git a/src/lib.rs b/src/lib.rs
index e41d01e..8a318a5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,4 @@
+#![feature(tuple_indexing)]
#![feature(unsafe_destructor)]
#![feature(globs)]
#![feature(phase)]
@@ -17,6 +18,16 @@
//!
//! The second way allows you to customize the way your window and GL context
//! will look and behave.
+//!
+//! # Features
+//!
+//! This crate has two Cargo features: `window` and `headless`.
+//!
+//! - `window` allows you to create regular windows and enables the `WindowBuilder` object.
+//! - `headless` allows you to do headless rendering, and enables
+//! the `HeadlessRendererBuilder` object.
+//!
+//! By default only `window` is enabled.
#[phase(plugin)] extern crate compile_msg;
#[phase(plugin)] extern crate gl_generator;
@@ -55,9 +66,11 @@ mod events;
compile_error!("Only the `windows`, `linux` and `macos` platforms are supported")
/// Identifier for a monitor.
+#[cfg(feature = "window")]
pub struct MonitorID(winimpl::MonitorID);
/// Object that allows you to build windows.
+#[cfg(feature = "window")]
pub struct WindowBuilder {
dimensions: Option<(uint, uint)>,
title: String,
@@ -65,6 +78,7 @@ pub struct WindowBuilder {
gl_version: Option<(uint, uint)>,
}
+#[cfg(feature = "window")]
impl WindowBuilder {
/// Initializes a new `WindowBuilder` with default values.
pub fn new() -> WindowBuilder {
@@ -128,6 +142,41 @@ impl WindowBuilder {
}
}
+/// Object that allows you to build headless contexts.
+#[cfg(feature = "headless")]
+pub struct HeadlessRendererBuilder {
+ dimensions: (uint, uint),
+ gl_version: Option<(uint, uint)>,
+}
+
+#[cfg(feature = "headless")]
+impl HeadlessRendererBuilder {
+ /// Initializes a new `HeadlessRendererBuilder` with default values.
+ pub fn new(width: uint, height: uint) -> HeadlessRendererBuilder {
+ HeadlessRendererBuilder {
+ dimensions: (width, height),
+ gl_version: None,
+ }
+ }
+
+ /// Requests to use a specific OpenGL version.
+ ///
+ /// Version is a (major, minor) pair. For example to request OpenGL 3.3
+ /// you would pass `(3, 3)`.
+ pub fn with_gl_version(mut self, version: (uint, uint)) -> HeadlessRendererBuilder {
+ self.gl_version = Some(version);
+ self
+ }
+
+ /// Builds the headless context.
+ ///
+ /// Error should be very rare and only occur in case of permission denied, incompatible system,
+ /// out of memory, etc.
+ pub fn build(self) -> Result<HeadlessContext, String> {
+ winimpl::HeadlessContext::new(self).map(|w| HeadlessContext { context: w })
+ }
+}
+
/// Represents an OpenGL context and the Window or environment around it.
///
/// # Example
@@ -150,16 +199,19 @@ impl WindowBuilder {
/// std::io::timer::sleep(17);
/// }
/// ```
+#[cfg(feature = "window")]
pub struct Window {
window: winimpl::Window,
}
+#[cfg(feature = "window")]
impl Default for Window {
fn default() -> Window {
Window::new().unwrap()
}
}
+#[cfg(feature = "window")]
impl Window {
/// Creates a new OpenGL context, and a Window for platforms where this is appropriate.
///
@@ -168,6 +220,7 @@ impl Window {
/// Error should be very rare and only occur in case of permission denied, incompatible system,
/// out of memory, etc.
#[inline]
+ #[cfg(feature = "window")]
pub fn new() -> Result<Window, String> {
let builder = WindowBuilder::new();
builder.build()
@@ -295,6 +348,30 @@ impl Window {
}
}
+/// Represents a headless OpenGL context.
+#[cfg(feature = "headless")]
+pub struct HeadlessContext {
+ context: winimpl::HeadlessContext,
+}
+
+#[cfg(feature = "headless")]
+impl HeadlessContext {
+ /// Creates a new OpenGL context
+ /// Sets the context as the current context.
+ #[inline]
+ pub unsafe fn make_current(&self) {
+ self.context.make_current()
+ }
+
+ /// Returns the address of an OpenGL function.
+ ///
+ /// Contrary to `wglGetProcAddress`, all available OpenGL functions return an address.
+ #[inline]
+ pub fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
+ self.context.get_proc_address(addr) as *const libc::c_void
+ }
+}
+
/// An iterator for the `poll_events` function.
// Implementation note: we retreive the list once, then serve each element by one by one.
// This may change in the future.
@@ -324,10 +401,12 @@ impl<'a> Iterator<Event> for WaitEventsIterator<'a> {
/// An iterator for the list of available monitors.
// Implementation note: we retreive the list once, then serve each element by one by one.
// This may change in the future.
+#[cfg(feature = "window")]
pub struct AvailableMonitorsIter {
data: Vec<winimpl::MonitorID>,
}
+#[cfg(feature = "window")]
impl Iterator<MonitorID> for AvailableMonitorsIter {
fn next(&mut self) -> Option<MonitorID> {
self.data.remove(0).map(|id| MonitorID(id))
@@ -335,16 +414,19 @@ impl Iterator<MonitorID> for AvailableMonitorsIter {
}
/// Returns the list of all available monitors.
+#[cfg(feature = "window")]
pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = winimpl::get_available_monitors();
AvailableMonitorsIter{ data: data }
}
/// Returns the primary monitor of the system.
+#[cfg(feature = "window")]
pub fn get_primary_monitor() -> MonitorID {
MonitorID(winimpl::get_primary_monitor())
}
+#[cfg(feature = "window")]
impl MonitorID {
/// Returns a human-readable name of the monitor.
pub fn get_name(&self) -> Option<String> {
diff --git a/src/osx/mod.rs b/src/osx/mod.rs
index d41c9c7..78d147f 100644
--- a/src/osx/mod.rs
+++ b/src/osx/mod.rs
@@ -1,4 +1,10 @@
-use {Event, WindowBuilder};
+use Event;
+
+#[cfg(feature = "window")]
+use WindowBuilder;
+
+#[cfg(feature = "headless")]
+use HeadlessRendererBuilder;
use cocoa::base::{id, NSUInteger, nil};
use cocoa::appkit::*;
@@ -11,6 +17,14 @@ pub struct Window {
context: id,
}
+pub struct HeadlessContext(Window);
+
+impl Deref<Window> for HeadlessContext {
+ fn deref(&self) -> &Window {
+ &self.0
+ }
+}
+
pub struct MonitorID;
pub fn get_available_monitors() -> Vec<MonitorID> {
@@ -31,14 +45,28 @@ impl MonitorID {
}
}
+#[cfg(feature = "window")]
impl Window {
- pub fn new(_builder: WindowBuilder) -> Result<Window, String> {
+ pub fn new(builder: WindowBuilder) -> Result<Window, String> {
+ Window::new_impl(builder.dimensions, builder.title.as_slice(), true)
+ }
+}
+
+#[cfg(feature = "headless")]
+impl HeadlessContext {
+ pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, String> {
+ Window::new_impl(Some(builder.dimensions), "", false)
+ .map(|w| HeadlessContext(w))
+ }
+}
+impl Window {
+ fn new_impl(dimensions: Option<(uint, uint)>, title: &str, visible: bool) -> Result<Window, String> {
let app = match Window::create_app() {
Some(app) => app,
None => { return Err(format!("Couldn't create NSApplication")); },
};
- let window = match Window::create_window(_builder.dimensions.unwrap_or((800, 600)), _builder.title.as_slice()) {
+ let window = match Window::create_window(dimensions.unwrap_or((800, 600)), title) {
Some(window) => window,
None => { return Err(format!("Couldn't create NSWindow")); },
};
diff --git a/src/win32/init.rs b/src/win32/init.rs
index 7ad4dca..740bc02 100644
--- a/src/win32/init.rs
+++ b/src/win32/init.rs
@@ -6,7 +6,7 @@ use std::sync::atomics::AtomicBool;
use std::ptr;
use super::{event, ffi};
use super::Window;
-use {Event, WindowBuilder};
+use Event;
/// Stores the current window and its events dispatcher.
///
@@ -14,12 +14,16 @@ use {Event, WindowBuilder};
/// receive an event for another window.
local_data_key!(WINDOW: (ffi::HWND, Sender<Event>))
-pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
+pub fn new_window(builder_dimensions: Option<(uint, uint)>, builder_title: String,
+ builder_monitor: Option<super::MonitorID>,
+ builder_gl_version: Option<(uint, uint)>,
+ builder_headless: bool) -> Result<Window, String>
+{
use std::mem;
use std::os;
// initializing variables to be sent to the task
- let title = builder.title.as_slice().utf16_units()
+ let title = builder_title.as_slice().utf16_units()
.collect::<Vec<u16>>().append_one(0); // title to utf16
//let hints = hints.clone();
let (tx, rx) = channel();
@@ -59,15 +63,15 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
// building a RECT object with coordinates
let mut rect = ffi::RECT {
- left: 0, right: builder.dimensions.unwrap_or((1024, 768)).val0() as ffi::LONG,
- top: 0, bottom: builder.dimensions.unwrap_or((1024, 768)).val1() as ffi::LONG,
+ left: 0, right: builder_dimensions.unwrap_or((1024, 768)).val0() as ffi::LONG,
+ top: 0, bottom: builder_dimensions.unwrap_or((1024, 768)).val1() as ffi::LONG,
};
// switching to fullscreen if necessary
// this means adjusting the window's position so that it overlaps the right monitor,
// and change the monitor's resolution if necessary
- if builder.monitor.is_some() {
- let monitor = builder.monitor.as_ref().unwrap();
+ if builder_monitor.is_some() {
+ let monitor = builder_monitor.as_ref().unwrap();
// adjusting the rect
{
@@ -96,7 +100,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
}
// computing the style and extended style of the window
- let (ex_style, style) = if builder.monitor.is_some() {
+ let (ex_style, style) = if builder_monitor.is_some() {
(ffi::WS_EX_APPWINDOW, ffi::WS_POPUP | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN)
} else {
(ffi::WS_EX_APPWINDOW | ffi::WS_EX_WINDOWEDGE,
@@ -227,17 +231,23 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
// creating the real window this time
let real_window = unsafe {
- let (width, height) = if builder.monitor.is_some() || builder.dimensions.is_some() {
+ let (width, height) = if builder_monitor.is_some() || builder_dimensions.is_some() {
(Some(rect.right - rect.left), Some(rect.bottom - rect.top))
} else {
(None, None)
};
+ let style = if builder_headless {
+ style
+ } else {
+ style | ffi::WS_VISIBLE
+ };
+
let handle = ffi::CreateWindowExW(ex_style, class_name.as_ptr(),
title.as_ptr() as ffi::LPCWSTR,
- style | ffi::WS_VISIBLE | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN,
- if builder.monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT },
- if builder.monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT },
+ style | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN,
+ if builder_monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT },
+ if builder_monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT },
width.unwrap_or(ffi::CW_USEDEFAULT), height.unwrap_or(ffi::CW_USEDEFAULT),
ptr::null(), ptr::null(), ffi::GetModuleHandleW(ptr::null()),
ptr::null_mut());
@@ -280,8 +290,8 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
let mut attributes = Vec::new();
- if builder.gl_version.is_some() {
- let version = builder.gl_version.as_ref().unwrap();
+ if builder_gl_version.is_some() {
+ let version = builder_gl_version.as_ref().unwrap();
attributes.push(ffi::wgl_extra::CONTEXT_MAJOR_VERSION_ARB as libc::c_int);
attributes.push(version.val0() as libc::c_int);
attributes.push(ffi::wgl_extra::CONTEXT_MINOR_VERSION_ARB as libc::c_int);
@@ -310,7 +320,7 @@ pub fn new_window(builder: WindowBuilder) -> Result<Window, String> {
};
// calling SetForegroundWindow if fullscreen
- if builder.monitor.is_some() {
+ if builder_monitor.is_some() {
unsafe { ffi::SetForegroundWindow(real_window) };
}
diff --git a/src/win32/mod.rs b/src/win32/mod.rs
index faff374..af28011 100644
--- a/src/win32/mod.rs
+++ b/src/win32/mod.rs
@@ -1,6 +1,12 @@
use std::sync::atomics::AtomicBool;
use std::ptr;
-use {Event, WindowBuilder};
+use Event;
+
+#[cfg(feature = "window")]
+use WindowBuilder;
+
+#[cfg(feature = "headless")]
+use HeadlessRendererBuilder;
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
@@ -9,6 +15,30 @@ mod ffi;
mod init;
mod monitor;
+///
+#[cfg(feature = "headless")]
+pub struct HeadlessContext(Window);
+
+#[cfg(feature = "headless")]
+impl HeadlessContext {
+ /// See the docs in the crate root file.
+ pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, String> {
+ let HeadlessRendererBuilder { dimensions, gl_version } = builder;
+ init::new_window(Some(dimensions), "".to_string(), None, gl_version, true)
+ .map(|w| HeadlessContext(w))
+ }
+
+ /// See the docs in the crate root file.
+ pub unsafe fn make_current(&self) {
+ self.0.make_current()
+ }
+
+ /// See the docs in the crate root file.
+ pub fn get_proc_address(&self, addr: &str) -> *const () {
+ self.0.get_proc_address(addr)
+ }
+}
+
/// The Win32 implementation of the main `Window` object.
pub struct Window {
/// Main handle for the window.
@@ -33,12 +63,16 @@ pub struct Window {
is_closed: AtomicBool,
}
+#[cfg(feature = "window")]
impl Window {
/// See the docs in the crate root file.
pub fn new(builder: WindowBuilder) -> Result<Window, String> {
- init::new_window(builder)
+ let WindowBuilder { dimensions, title, monitor, gl_version } = builder;
+ init::new_window(dimensions, title, monitor, gl_version, false)
}
+}
+impl Window {
/// See the docs in the crate root file.
pub fn is_closed(&self) -> bool {
use std::sync::atomics::Relaxed;
@@ -200,6 +234,7 @@ impl Window {
}
}
+#[cfg(feature = "window")]
#[unsafe_destructor]
impl Drop for Window {
fn drop(&mut self) {
diff --git a/src/x11/ffi.rs b/src/x11/ffi.rs
index 1f72943..71a6cd9 100644
--- a/src/x11/ffi.rs
+++ b/src/x11/ffi.rs
@@ -20,6 +20,7 @@ pub type GLXPixmap = XID;
pub type GLXWindow = XID;
pub type KeyCode = libc::c_ulong;
pub type KeySym = XID;
+pub type OSMesaContext = *const ();
pub type Pixmap = XID;
pub type Status = libc::c_int; // TODO: not sure
pub type Time = libc::c_ulong;
@@ -1356,6 +1357,29 @@ pub struct XF86VidModeModeInfo {
private: libc::c_long,
}
+#[cfg(feature = "headless")]
+#[link(name = "OSMesa")]
+extern "C" {
+ pub fn OSMesaCreateContext(format: libc::c_uint, sharelist: OSMesaContext) -> OSMesaContext;
+ pub fn OSMesaCreateContextExt(format: libc::c_uint, depthBits: libc::c_int,
+ stencilBits: libc::c_int, accumBits: libc::c_int, sharelist: OSMesaContext)
+ -> OSMesaContext;
+ pub fn OSMesaDestroyContext(ctx: OSMesaContext);
+ pub fn OSMesaMakeCurrent(ctx: OSMesaContext, buffer: *mut libc::c_void, type_: libc::c_uint,
+ width: libc::c_int, height: libc::c_int) -> libc::c_uchar;
+ pub fn OSMesaGetCurrentContext() -> OSMesaContext;
+ pub fn OSMesaPixelStore(pname: libc::c_int, value: libc::c_int);
+ pub fn OSMesaGetIntegerv(pname: libc::c_int, value: *mut libc::c_int);
+ pub fn OSMesaGetDepthBuffer(c: OSMesaContext, width: *mut libc::c_int,
+ height: *mut libc::c_int, bytesPerValue: *mut libc::c_int,
+ buffer: *mut *mut libc::c_void);
+ pub fn OSMesaGetColorBuffer(c: OSMesaContext, width: *mut libc::c_int,
+ height: *mut libc::c_int, format: *mut libc::c_int, buffer: *mut *mut libc::c_void);
+ pub fn OSMesaGetProcAddress(funcName: *const libc::c_char) -> *const libc::c_void;
+ pub fn OSMesaColorClamp(enable: libc::c_uchar);
+}
+
+#[cfg(feature = "window")]
#[link(name = "GL")]
#[link(name = "X11")]
#[link(name = "Xxf86vm")]
diff --git a/src/x11/headless.rs b/src/x11/headless.rs
new file mode 100644
index 0000000..4f4eb16
--- /dev/null
+++ b/src/x11/headless.rs
@@ -0,0 +1,47 @@
+use HeadlessRendererBuilder;
+use libc;
+use std::{mem, ptr};
+use super::ffi;
+
+pub struct HeadlessContext {
+ context: ffi::OSMesaContext,
+ buffer: Vec<u32>,
+ width: uint,
+ height: uint,
+}
+
+impl HeadlessContext {
+ pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, String> {
+ Ok(HeadlessContext {
+ width: builder.dimensions.0,
+ height: builder.dimensions.1,
+ buffer: Vec::from_elem(builder.dimensions.0 * builder.dimensions.1, unsafe { mem::uninitialized() }),
+ context: unsafe {
+ // TODO: check errors
+ ffi::OSMesaCreateContext(0x1908, ptr::null())
+ }
+ })
+ }
+
+ pub unsafe fn make_current(&self) {
+ ffi::OSMesaMakeCurrent(self.context,
+ self.buffer.as_ptr() as *mut libc::c_void,
+ 0x1401, self.width as libc::c_int, self.height as libc::c_int);
+ }
+
+ pub fn get_proc_address(&self, addr: &str) -> *const () {
+ use std::c_str::ToCStr;
+
+ unsafe {
+ addr.with_c_str(|s| {
+ ffi::OSMesaGetProcAddress(mem::transmute(s)) as *const ()
+ })
+ }
+ }
+}
+
+impl Drop for HeadlessContext {
+ fn drop(&mut self) {
+ unsafe { ffi::OSMesaDestroyContext(self.context) }
+ }
+}
diff --git a/src/x11/mod.rs b/src/x11/mod.rs
index 80a84f7..9d9d25c 100644
--- a/src/x11/mod.rs
+++ b/src/x11/mod.rs
@@ -1,499 +1,13 @@
-use {Event, WindowBuilder};
-use libc;
-use std::{mem, ptr};
-use std::sync::atomics::AtomicBool;
+#[cfg(feature = "headless")]
+pub use self::headless::HeadlessContext;
-pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
+#[cfg(feature = "window")]
+pub use self::window::{Window, MonitorID, get_available_monitors, get_primary_monitor};
-mod events;
mod ffi;
-mod monitor;
-pub struct Window {
- display: *mut ffi::Display,
- window: ffi::Window,
- im: ffi::XIM,
- ic: ffi::XIC,
- context: ffi::GLXContext,
- is_closed: AtomicBool,
- wm_delete_window: ffi::Atom,
- xf86_desk_mode: *mut ffi::XF86VidModeModeInfo,
- screen_id: libc::c_int,
- is_fullscreen: bool,
-}
+#[cfg(feature = "headless")]
+mod headless;
-impl Window {
- pub fn new(builder: WindowBuilder) -> Result<Window, String> {
- let dimensions = builder.dimensions.unwrap_or((800, 600));
-
- // calling XOpenDisplay
- let display = unsafe {
- let display = ffi::XOpenDisplay(ptr::null());
- if display.is_null() {
- return Err(format!("XOpenDisplay failed"));
- }
- display
- };
-
- let screen_id = match builder.monitor {
- Some(MonitorID(monitor)) => monitor as i32,
- None => unsafe { ffi::XDefaultScreen(display) },
- };
-
- // 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
- };
-
- 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)
- };
-
- // getting the visual infos
- let visual_infos = unsafe {
- let vi = ffi::glXGetVisualFromFBConfig(display, fb_config);
- if vi.is_null() {
- return Err(format!("glXChooseVisual failed"));
- }
- let vi_copy = *vi;
- ffi::XFree(vi as *const libc::c_void);
- vi_copy
- };
-
- // 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;
- swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask |
- ffi::VisibilityChangeMask | ffi::KeyPressMask | ffi::PointerMotionMask |
- ffi::KeyReleaseMask | ffi::ButtonPressMask |
- ffi::ButtonReleaseMask | ffi::KeymapStateMask;
- swa.border_pixel = 0;
- swa.override_redirect = 0;
- swa
- };
-
- let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask;
- if builder.monitor.is_some() {
- 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;
- }
- }
-
- // finally creating the window
- let window = unsafe {
- let win = ffi::XCreateWindow(display, root, 0, 0, dimensions.val0() as libc::c_uint,
- dimensions.val1() as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput,
- visual_infos.visual, window_attributes,
- &mut set_win_attr);
- win
- };
-
- // 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(builder.title.as_slice().as_ptr()));
- ffi::XFlush(display);
-
- wm_delete_window
- };
-
- // getting the pointer to glXCreateContextAttribs
- let create_context_attribs = unsafe {
- let mut addr = ffi::glXGetProcAddress(b"glXCreateContextAttribs".as_ptr()
- as *const u8) as *const ();
-
- if addr.is_null() {
- addr = ffi::glXGetProcAddress(b"glXCreateContextAttribsARB".as_ptr()
- as *const u8) as *const ();
- }
-
- addr.as_ref().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
- })
- };
-
- // creating IM
- let im = unsafe {
- let im = ffi::XOpenIM(display, ptr::null(), ptr::null_mut(), ptr::null_mut());
- 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
- };
-
- // 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"));
- }
- }
-
-
- // creating GL context
- let context = unsafe {
- 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);
-
- 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 {
- ffi::glXCreateContext(display, &visual_infos, ptr::null(), 1)
- };
-
- if context.is_null() {
- return Err(format!("GL context creation failed"));
- }
-
- context
- };
-
- // creating the window object
- let window = Window {
- display: display,
- window: window,
- im: im,
- ic: ic,
- context: context,
- is_closed: AtomicBool::new(false),
- wm_delete_window: wm_delete_window,
- xf86_desk_mode: xf86_desk_mode,
- screen_id: screen_id,
- is_fullscreen: builder.monitor.is_some(),
- };
-
- // 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)
- }
-
- pub fn is_closed(&self) -> bool {
- use std::sync::atomics::Relaxed;
- self.is_closed.load(Relaxed)
- }
-
- 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))
- }
-
- pub fn set_position(&self, x: int, y: int) {
- unsafe { ffi::XMoveWindow(self.display, self.window, x as libc::c_int, y as libc::c_int) }
- }
-
- pub fn get_inner_size(&self) -> Option<(uint, uint)> {
- self.get_geometry().map(|(_, _, w, h)| (w, h))
- }
-
- pub fn get_outer_size(&self) -> Option<(uint, uint)> {
- unimplemented!()
- }
-
- pub fn set_inner_size(&self, _x: uint, _y: uint) {
- unimplemented!()
- }
-
- pub fn poll_events(&self) -> Vec<Event> {
- use std::mem;
-
- let mut events = Vec::new();
-
- loop {
- use std::num::Bounded;
-
- let mut xev = unsafe { mem::uninitialized() };
- let res = unsafe { ffi::XCheckMaskEvent(self.display, Bounded::max_value(), &mut xev) };
-
- if res == 0 {
- let res = unsafe { ffi::XCheckTypedEvent(self.display, ffi::ClientMessage, &mut xev) };
-
- if res == 0 {
- break
- }
- }
-
- match xev.type_ {
- ffi::KeymapNotify => {
- unsafe { ffi::XRefreshKeyboardMapping(&xev) }
- },
-
- 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 {
- self.is_closed.store(true, Relaxed);
- events.push(Closed);
- }
- },
-
- ffi::ResizeRequest => {
- use Resized;
- let rs_event: &ffi::XResizeRequestEvent = unsafe { mem::transmute(&xev) };
- events.push(Resized(rs_event.width as uint, rs_event.height as uint));
- },
-
- ffi::MotionNotify => {
- use MouseMoved;
- let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) };
- events.push(MouseMoved((event.x as int, event.y as int)));
- },
-
- ffi::KeyPress | ffi::KeyRelease => {
- use {KeyboardInput, Pressed, Released, ReceivedCharacter, KeyModifiers};
- 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) };
- }
-
- let state = if xev.type_ == ffi::KeyPress { Pressed } else { Released };
-
- 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::null_mut(), ptr::null_mut());
-
- 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));
- }
-
- 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()));
- //
- },
-
- ffi::ButtonPress | ffi::ButtonRelease => {
- use {MouseInput, Pressed, Released};
- use {LeftMouseButton, RightMouseButton, MiddleMouseButton, OtherMouseButton};
- let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
-
- 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)),
- _ => None
- };
-
- match button {
- Some(button) =>
- events.push(MouseInput(state, button)),
- None => ()
- };
- },
-
- _ => ()
- }
- }
-
- events
- }
-
- 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;
- }
- }
- }
-
- pub unsafe fn make_current(&self) {
- let res = ffi::glXMakeCurrent(self.display, self.window, self.context);
- 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| {
- ffi::glXGetProcAddress(mem::transmute(s)) as *const ()
- })
- }
- }
-
- pub fn swap_buffers(&self) {
- unsafe { ffi::glXSwapBuffers(self.display, self.window) }
- }
-}
-
-impl Drop for Window {
- fn drop(&mut self) {
- unsafe { ffi::glXMakeCurrent(self.display, 0, ptr::null()); }
- unsafe { ffi::glXDestroyContext(self.display, self.context); }
-
- 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); }
- }
-
- unsafe { ffi::XDestroyIC(self.ic); }
- unsafe { ffi::XCloseIM(self.im); }
- unsafe { ffi::XDestroyWindow(self.display, self.window); }
- unsafe { ffi::XCloseDisplay(self.display); }
- }
-}
+#[cfg(feature = "window")]
+mod window;
diff --git a/src/x11/events.rs b/src/x11/window/events.rs
index d5b559a..4d74eed 100644
--- a/src/x11/events.rs
+++ b/src/x11/window/events.rs
@@ -1,5 +1,5 @@
use {events, libc};
-use super::ffi;
+use super::super::ffi;
use VirtualKeyCode;
pub fn keycode_to_element(scancode: libc::c_uint) -> Option<VirtualKeyCode> {
diff --git a/src/x11/window/mod.rs b/src/x11/window/mod.rs
new file mode 100644
index 0000000..58439e8
--- /dev/null
+++ b/src/x11/window/mod.rs
@@ -0,0 +1,499 @@
+use {Event, WindowBuilder};
+use libc;
+use std::{mem, ptr};
+use std::sync::atomics::AtomicBool;
+use super::ffi;
+
+pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
+
+mod events;
+mod monitor;
+
+pub struct Window {
+ display: *mut ffi::Display,
+ window: ffi::Window,
+ im: ffi::XIM,
+ ic: ffi::XIC,
+ context: ffi::GLXContext,
+ is_closed: AtomicBool,
+ wm_delete_window: ffi::Atom,
+ xf86_desk_mode: *mut ffi::XF86VidModeModeInfo,
+ screen_id: libc::c_int,
+ is_fullscreen: bool,
+}
+
+impl Window {
+ pub fn new(builder: WindowBuilder) -> Result<Window, String> {
+ let dimensions = builder.dimensions.unwrap_or((800, 600));
+
+ // calling XOpenDisplay
+ let display = unsafe {
+ let display = ffi::XOpenDisplay(ptr::null());
+ if display.is_null() {
+ return Err(format!("XOpenDisplay failed"));
+ }
+ display
+ };
+
+ let screen_id = match builder.monitor {
+ Some(MonitorID(monitor)) => monitor as i32,
+ None => unsafe { ffi::XDefaultScreen(display) },
+ };
+
+ // 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
+ };
+
+ 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)
+ };
+
+ // getting the visual infos
+ let visual_infos = unsafe {
+ let vi = ffi::glXGetVisualFromFBConfig(display, fb_config);
+ if vi.is_null() {
+ return Err(format!("glXChooseVisual failed"));
+ }
+ let vi_copy = *vi;
+ ffi::XFree(vi as *const libc::c_void);
+ vi_copy
+ };
+
+ // 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;
+ swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask |
+ ffi::VisibilityChangeMask | ffi::KeyPressMask | ffi::PointerMotionMask |
+ ffi::KeyReleaseMask | ffi::ButtonPressMask |
+ ffi::ButtonReleaseMask | ffi::KeymapStateMask;
+ swa.border_pixel = 0;
+ swa.override_redirect = 0;
+ swa
+ };
+
+ let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask;
+ if builder.monitor.is_some() {
+ 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;
+ }
+ }
+
+ // finally creating the window
+ let window = unsafe {
+ let win = ffi::XCreateWindow(display, root, 0, 0, dimensions.val0() as libc::c_uint,
+ dimensions.val1() as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput,
+ visual_infos.visual, window_attributes,
+ &mut set_win_attr);
+ win
+ };
+
+ // 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(builder.title.as_slice().as_ptr()));
+ ffi::XFlush(display);
+
+ wm_delete_window
+ };
+
+ // getting the pointer to glXCreateContextAttribs
+ let create_context_attribs = unsafe {
+ let mut addr = ffi::glXGetProcAddress(b"glXCreateContextAttribs".as_ptr()
+ as *const u8) as *const ();
+
+ if addr.is_null() {
+ addr = ffi::glXGetProcAddress(b"glXCreateContextAttribsARB".as_ptr()
+ as *const u8) as *const ();
+ }
+
+ addr.as_ref().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
+ })
+ };
+
+ // creating IM
+ let im = unsafe {
+ let im = ffi::XOpenIM(display, ptr::null(), ptr::null_mut(), ptr::null_mut());
+ 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
+ };
+
+ // 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"));
+ }
+ }
+
+
+ // creating GL context
+ let context = unsafe {
+ 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);
+
+ 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 {
+ ffi::glXCreateContext(display, &visual_infos, ptr::null(), 1)
+ };
+
+ if context.is_null() {
+ return Err(format!("GL context creation failed"));
+ }
+
+ context
+ };
+
+ // creating the window object
+ let window = Window {
+ display: display,
+ window: window,
+ im: im,
+ ic: ic,
+ context: context,
+ is_closed: AtomicBool::new(false),
+ wm_delete_window: wm_delete_window,
+ xf86_desk_mode: xf86_desk_mode,
+ screen_id: screen_id,
+ is_fullscreen: builder.monitor.is_some(),
+ };
+
+ // 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)
+ }
+
+ pub fn is_closed(&self) -> bool {
+ use std::sync::atomics::Relaxed;
+ self.is_closed.load(Relaxed)
+ }
+
+ 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))
+ }
+
+ pub fn set_position(&self, x: int, y: int) {
+ unsafe { ffi::XMoveWindow(self.display, self.window, x as libc::c_int, y as libc::c_int) }
+ }
+
+ pub fn get_inner_size(&self) -> Option<(uint, uint)> {
+ self.get_geometry().map(|(_, _, w, h)| (w, h))
+ }
+
+ pub fn get_outer_size(&self) -> Option<(uint, uint)> {
+ unimplemented!()
+ }
+
+ pub fn set_inner_size(&self, _x: uint, _y: uint) {
+ unimplemented!()
+ }
+
+ pub fn poll_events(&self) -> Vec<Event> {
+ use std::mem;
+
+ let mut events = Vec::new();
+
+ loop {
+ use std::num::Bounded;
+
+ let mut xev = unsafe { mem::uninitialized() };
+ let res = unsafe { ffi::XCheckMaskEvent(self.display, Bounded::max_value(), &mut xev) };
+
+ if res == 0 {
+ let res = unsafe { ffi::XCheckTypedEvent(self.display, ffi::ClientMessage, &mut xev) };
+
+ if res == 0 {
+ break
+ }
+ }
+
+ match xev.type_ {
+ ffi::KeymapNotify => {
+ unsafe { ffi::XRefreshKeyboardMapping(&xev) }
+ },
+
+ 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 {
+ self.is_closed.store(true, Relaxed);
+ events.push(Closed);
+ }
+ },
+
+ ffi::ResizeRequest => {
+ use Resized;
+ let rs_event: &ffi::XResizeRequestEvent = unsafe { mem::transmute(&xev) };
+ events.push(Resized(rs_event.width as uint, rs_event.height as uint));
+ },
+
+ ffi::MotionNotify => {
+ use MouseMoved;
+ let event: &ffi::XMotionEvent = unsafe { mem::transmute(&xev) };
+ events.push(MouseMoved((event.x as int, event.y as int)));
+ },
+
+ ffi::KeyPress | ffi::KeyRelease => {
+ use {KeyboardInput, Pressed, Released, ReceivedCharacter, KeyModifiers};
+ 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) };
+ }
+
+ let state = if xev.type_ == ffi::KeyPress { Pressed } else { Released };
+
+ 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::null_mut(), ptr::null_mut());
+
+ 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));
+ }
+
+ 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()));
+ //
+ },
+
+ ffi::ButtonPress | ffi::ButtonRelease => {
+ use {MouseInput, Pressed, Released};
+ use {LeftMouseButton, RightMouseButton, MiddleMouseButton, OtherMouseButton};
+ let event: &ffi::XButtonEvent = unsafe { mem::transmute(&xev) };
+
+ 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)),
+ _ => None
+ };
+
+ match button {
+ Some(button) =>
+ events.push(MouseInput(state, button)),
+ None => ()
+ };
+ },
+
+ _ => ()
+ }
+ }
+
+ events
+ }
+
+ 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;
+ }
+ }
+ }
+
+ pub unsafe fn make_current(&self) {
+ let res = ffi::glXMakeCurrent(self.display, self.window, self.context);
+ 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| {
+ ffi::glXGetProcAddress(mem::transmute(s)) as *const ()
+ })
+ }
+ }
+
+ pub fn swap_buffers(&self) {
+ unsafe { ffi::glXSwapBuffers(self.display, self.window) }
+ }
+}
+
+impl Drop for Window {
+ fn drop(&mut self) {
+ unsafe { ffi::glXMakeCurrent(self.display, 0, ptr::null()); }
+ unsafe { ffi::glXDestroyContext(self.display, self.context); }
+
+ 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); }
+ }
+
+ unsafe { ffi::XDestroyIC(self.ic); }
+ unsafe { ffi::XCloseIM(self.im); }
+ unsafe { ffi::XDestroyWindow(self.display, self.window); }
+ unsafe { ffi::XCloseDisplay(self.display); }
+ }
+}
diff --git a/src/x11/monitor.rs b/src/x11/window/monitor.rs
index 3b382c2..b72ed15 100644
--- a/src/x11/monitor.rs
+++ b/src/x11/window/monitor.rs
@@ -1,5 +1,5 @@
use std::{ptr};
-use super::ffi;
+use super::super::ffi;
pub struct MonitorID(pub uint);
diff --git a/tests/headless.rs b/tests/headless.rs
new file mode 100644
index 0000000..b9ae598
--- /dev/null
+++ b/tests/headless.rs
@@ -0,0 +1,13 @@
+#![feature(phase)]
+#![feature(tuple_indexing)]
+
+extern crate glutin;
+
+#[cfg(feature = "headless")]
+#[test]
+fn main() {
+ let window = glutin::HeadlessRendererBuilder::new(1024, 768).build().unwrap();
+
+ unsafe { window.make_current() };
+
+}