winit/src/win32/mod.rs

299 lines
8.5 KiB
Rust
Raw Normal View History

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