2014-10-22 17:04:13 +11:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2014-07-27 18:55:37 +10:00
|
|
|
use std::ptr;
|
2014-10-24 21:54:58 +11:00
|
|
|
use libc;
|
2014-11-06 02:42:18 +11:00
|
|
|
use {CreationError, Event};
|
2014-10-05 04:17:02 +11:00
|
|
|
|
|
|
|
#[cfg(feature = "window")]
|
|
|
|
use WindowBuilder;
|
|
|
|
|
|
|
|
#[cfg(feature = "headless")]
|
|
|
|
use HeadlessRendererBuilder;
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-07-31 18:52:05 +10:00
|
|
|
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
|
|
|
|
2014-07-28 02:55:14 +10:00
|
|
|
mod event;
|
2014-07-27 18:55:37 +10:00
|
|
|
mod ffi;
|
2014-08-01 04:14:36 +10:00
|
|
|
mod init;
|
2014-07-31 18:52:05 +10:00
|
|
|
mod monitor;
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-10-05 04:17:02 +11:00
|
|
|
///
|
|
|
|
#[cfg(feature = "headless")]
|
|
|
|
pub struct HeadlessContext(Window);
|
|
|
|
|
|
|
|
#[cfg(feature = "headless")]
|
|
|
|
impl HeadlessContext {
|
|
|
|
/// See the docs in the crate root file.
|
2014-11-06 02:42:18 +11:00
|
|
|
pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, CreationError> {
|
2014-10-05 04:17:02 +11:00
|
|
|
let HeadlessRendererBuilder { dimensions, gl_version } = builder;
|
2014-10-26 20:32:51 +11:00
|
|
|
init::new_window(Some(dimensions), "".to_string(), None, gl_version, false, true)
|
2014-10-05 04:17:02 +11:00
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 07:02:26 +10:00
|
|
|
/// The Win32 implementation of the main `Window` object.
|
2014-07-27 18:55:37 +10:00
|
|
|
pub struct Window {
|
2014-08-02 07:02:26 +10:00
|
|
|
/// Main handle for the window.
|
2014-07-27 18:55:37 +10:00
|
|
|
window: ffi::HWND,
|
2014-08-02 07:02:26 +10:00
|
|
|
|
|
|
|
/// This represents a "draw context" for the surface of the window.
|
2014-07-27 18:55:37 +10:00
|
|
|
hdc: ffi::HDC,
|
2014-08-02 07:02:26 +10:00
|
|
|
|
|
|
|
/// OpenGL context.
|
2014-07-27 18:55:37 +10:00
|
|
|
context: ffi::HGLRC,
|
2014-08-02 07:02:26 +10:00
|
|
|
|
|
|
|
/// Binded to `opengl32.dll`.
|
|
|
|
///
|
|
|
|
/// `wglGetProcAddress` returns null for GL 1.1 functions because they are
|
|
|
|
/// already defined by the system. This module contains them.
|
2014-07-27 18:55:37 +10:00
|
|
|
gl_library: ffi::HMODULE,
|
2014-08-02 07:02:26 +10:00
|
|
|
|
|
|
|
/// Receiver for the events dispatched by the window callback.
|
2014-07-27 18:55:37 +10:00
|
|
|
events_receiver: Receiver<Event>,
|
2014-08-02 07:02:26 +10:00
|
|
|
|
|
|
|
/// True if a `Closed` event has been received.
|
2014-07-30 21:29:28 +10:00
|
|
|
is_closed: AtomicBool,
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
|
2014-10-05 04:17:02 +11:00
|
|
|
#[cfg(feature = "window")]
|
2014-07-27 18:55:37 +10:00
|
|
|
impl Window {
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-11-06 02:42:18 +11:00
|
|
|
pub fn new(builder: WindowBuilder) -> Result<Window, CreationError> {
|
2014-11-01 19:02:01 +11:00
|
|
|
let WindowBuilder { dimensions, title, monitor, gl_version, vsync, visible } = builder;
|
|
|
|
init::new_window(dimensions, title, monitor, gl_version, vsync, !visible)
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
2014-10-05 04:17:02 +11:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
|
2014-10-05 04:17:02 +11:00
|
|
|
impl Window {
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-30 21:29:28 +10:00
|
|
|
pub fn is_closed(&self) -> bool {
|
2014-10-22 17:04:13 +11:00
|
|
|
use std::sync::atomic::Relaxed;
|
2014-07-30 21:29:28 +10:00
|
|
|
self.is_closed.load(Relaxed)
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-08-02 07:02:26 +10:00
|
|
|
///
|
2014-07-27 18:55:37 +10:00
|
|
|
/// Calls SetWindowText on the HWND.
|
|
|
|
pub fn set_title(&self, text: &str) {
|
|
|
|
unsafe {
|
|
|
|
ffi::SetWindowTextW(self.window,
|
2014-10-12 02:58:17 +11:00
|
|
|
text.utf16_units().chain(Some(0).into_iter())
|
|
|
|
.collect::<Vec<u16>>().as_ptr() as ffi::LPCWSTR);
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-01 19:02:01 +11:00
|
|
|
pub fn show(&self) {
|
|
|
|
unsafe {
|
|
|
|
ffi::ShowWindow(self.window, ffi::SW_SHOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hide(&self) {
|
|
|
|
unsafe {
|
|
|
|
ffi::ShowWindow(self.window, ffi::SW_HIDE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_position(&self) -> Option<(int, int)> {
|
|
|
|
use std::mem;
|
2014-07-28 06:46:30 +10:00
|
|
|
|
|
|
|
let mut placement: ffi::WINDOWPLACEMENT = unsafe { mem::zeroed() };
|
|
|
|
placement.length = mem::size_of::<ffi::WINDOWPLACEMENT>() as ffi::UINT;
|
|
|
|
|
|
|
|
if unsafe { ffi::GetWindowPlacement(self.window, &mut placement) } == 0 {
|
2014-07-30 21:10:17 +10:00
|
|
|
return None
|
2014-07-28 06:46:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
let ref rect = placement.rcNormalPosition;
|
2014-07-30 21:10:17 +10:00
|
|
|
Some((rect.left as int, rect.top as int))
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-08-08 02:07:48 +10:00
|
|
|
pub fn set_position(&self, x: int, y: int) {
|
2014-07-27 18:55:37 +10:00
|
|
|
use libc;
|
|
|
|
|
|
|
|
unsafe {
|
2014-09-12 23:51:43 +10:00
|
|
|
ffi::SetWindowPos(self.window, ptr::null(), x as libc::c_int, y as libc::c_int,
|
2014-07-27 18:55:37 +10:00
|
|
|
0, 0, ffi::SWP_NOZORDER | ffi::SWP_NOSIZE);
|
|
|
|
ffi::UpdateWindow(self.window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_inner_size(&self) -> Option<(uint, uint)> {
|
|
|
|
use std::mem;
|
2014-07-28 06:36:44 +10:00
|
|
|
let mut rect: ffi::RECT = unsafe { mem::uninitialized() };
|
|
|
|
|
|
|
|
if unsafe { ffi::GetClientRect(self.window, &mut rect) } == 0 {
|
2014-07-30 21:10:17 +10:00
|
|
|
return None
|
2014-07-28 06:36:44 +10:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:10:17 +10:00
|
|
|
Some((
|
2014-07-28 06:36:44 +10:00
|
|
|
(rect.right - rect.left) as uint,
|
|
|
|
(rect.bottom - rect.top) as uint
|
2014-07-30 21:10:17 +10:00
|
|
|
))
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-30 21:10:17 +10:00
|
|
|
pub fn get_outer_size(&self) -> Option<(uint, uint)> {
|
|
|
|
use std::mem;
|
2014-07-28 06:36:44 +10:00
|
|
|
let mut rect: ffi::RECT = unsafe { mem::uninitialized() };
|
|
|
|
|
|
|
|
if unsafe { ffi::GetWindowRect(self.window, &mut rect) } == 0 {
|
2014-07-30 21:10:17 +10:00
|
|
|
return None
|
2014-07-28 06:36:44 +10:00
|
|
|
}
|
|
|
|
|
2014-07-30 21:10:17 +10:00
|
|
|
Some((
|
2014-07-28 06:36:44 +10:00
|
|
|
(rect.right - rect.left) as uint,
|
|
|
|
(rect.bottom - rect.top) as uint
|
2014-07-30 21:10:17 +10:00
|
|
|
))
|
2014-07-28 06:31:00 +10:00
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-28 06:31:00 +10:00
|
|
|
pub fn set_inner_size(&self, x: uint, y: uint) {
|
2014-07-27 18:55:37 +10:00
|
|
|
use libc;
|
|
|
|
|
|
|
|
unsafe {
|
2014-09-12 23:51:43 +10:00
|
|
|
ffi::SetWindowPos(self.window, ptr::null(), 0, 0, x as libc::c_int,
|
2014-07-27 18:55:37 +10:00
|
|
|
y as libc::c_int, ffi::SWP_NOZORDER | ffi::SWP_NOREPOSITION);
|
|
|
|
ffi::UpdateWindow(self.window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-27 18:55:37 +10:00
|
|
|
// TODO: return iterator
|
|
|
|
pub fn poll_events(&self) -> Vec<Event> {
|
|
|
|
let mut events = Vec::new();
|
|
|
|
loop {
|
|
|
|
match self.events_receiver.try_recv() {
|
|
|
|
Ok(ev) => events.push(ev),
|
|
|
|
Err(_) => break
|
|
|
|
}
|
|
|
|
}
|
2014-07-28 03:58:27 +10:00
|
|
|
|
2014-08-04 01:27:48 +10:00
|
|
|
// if one of the received events is `Closed`, setting `is_closed` to true
|
2014-07-28 03:58:27 +10:00
|
|
|
if events.iter().find(|e| match e { &&::Closed => true, _ => false }).is_some() {
|
2014-10-22 17:04:13 +11:00
|
|
|
use std::sync::atomic::Relaxed;
|
2014-07-30 21:29:28 +10:00
|
|
|
self.is_closed.store(true, Relaxed);
|
2014-07-28 03:58:27 +10:00
|
|
|
}
|
2014-07-28 02:35:42 +10:00
|
|
|
|
2014-07-27 18:55:37 +10:00
|
|
|
events
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-28 02:35:42 +10:00
|
|
|
// TODO: return iterator
|
|
|
|
pub fn wait_events(&self) -> Vec<Event> {
|
2014-08-01 04:55:30 +10:00
|
|
|
match self.events_receiver.recv_opt() {
|
|
|
|
Ok(ev) => {
|
2014-08-04 01:27:48 +10:00
|
|
|
// if the received event is `Closed`, setting `is_closed` to true
|
|
|
|
match ev {
|
|
|
|
::Closed => {
|
2014-10-22 17:04:13 +11:00
|
|
|
use std::sync::atomic::Relaxed;
|
2014-08-04 01:27:48 +10:00
|
|
|
self.is_closed.store(true, Relaxed);
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
|
|
|
|
// looing for other possible events in the queue
|
2014-08-01 04:55:30 +10:00
|
|
|
let mut result = self.poll_events();
|
|
|
|
result.insert(0, ev);
|
|
|
|
result
|
|
|
|
},
|
2014-08-04 01:27:48 +10:00
|
|
|
|
2014-08-01 04:55:30 +10:00
|
|
|
Err(_) => {
|
2014-10-22 17:04:13 +11:00
|
|
|
use std::sync::atomic::Relaxed;
|
2014-08-01 04:55:30 +10:00
|
|
|
self.is_closed.store(true, Relaxed);
|
|
|
|
vec![]
|
2014-07-28 02:35:42 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-31 02:12:39 +10:00
|
|
|
pub unsafe fn make_current(&self) {
|
2014-09-12 23:51:43 +10:00
|
|
|
// TODO: check return value
|
|
|
|
ffi::wgl::MakeCurrent(self.hdc, self.context);
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-27 18:55:37 +10:00
|
|
|
pub fn get_proc_address(&self, addr: &str) -> *const () {
|
|
|
|
use std::c_str::ToCStr;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
addr.with_c_str(|s| {
|
2014-09-12 23:51:43 +10:00
|
|
|
let p = ffi::wgl::GetProcAddress(s) as *const ();
|
2014-07-27 18:55:37 +10:00
|
|
|
if !p.is_null() { return p; }
|
|
|
|
ffi::GetProcAddress(self.gl_library, s) as *const ()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 19:21:12 +10:00
|
|
|
/// See the docs in the crate root file.
|
2014-07-27 18:55:37 +10:00
|
|
|
pub fn swap_buffers(&self) {
|
|
|
|
unsafe {
|
2014-08-15 23:34:30 +10:00
|
|
|
ffi::SwapBuffers(self.hdc);
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
}
|
2014-10-24 16:20:25 +11:00
|
|
|
|
|
|
|
pub fn platform_display(&self) -> *mut libc::c_void {
|
2014-10-25 19:31:25 +11:00
|
|
|
unimplemented!()
|
2014-10-24 16:20:25 +11:00
|
|
|
}
|
2014-07-27 18:55:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl Drop for Window {
|
|
|
|
fn drop(&mut self) {
|
2014-08-04 01:33:48 +10:00
|
|
|
use std::ptr;
|
2014-10-12 05:52:48 +11:00
|
|
|
unsafe { ffi::PostMessageW(self.window, ffi::WM_DESTROY, 0, 0); }
|
2014-09-12 23:51:43 +10:00
|
|
|
unsafe { ffi::wgl::MakeCurrent(ptr::null(), ptr::null()); }
|
|
|
|
unsafe { ffi::wgl::DeleteContext(self.context); }
|
2014-07-27 18:55:37 +10:00
|
|
|
unsafe { ffi::DestroyWindow(self.window); }
|
|
|
|
}
|
|
|
|
}
|