2014-10-22 08:04:13 +02:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2014-07-27 10:55:37 +02:00
|
|
|
use std::ptr;
|
2015-01-08 09:28:22 +01:00
|
|
|
use std::ffi::CString;
|
2015-01-01 23:09:16 -08:00
|
|
|
use std::collections::RingBuf;
|
2015-01-03 23:11:59 +01:00
|
|
|
use std::sync::mpsc::Receiver;
|
2014-10-24 12:54:58 +02:00
|
|
|
use libc;
|
2015-01-12 19:45:20 -08:00
|
|
|
use {CreationError, Event, MouseCursor};
|
2014-10-04 19:17:02 +02:00
|
|
|
|
2014-12-28 15:08:41 +01:00
|
|
|
use BuilderAttribs;
|
2014-07-27 10:55:37 +02:00
|
|
|
|
2015-02-16 10:01:47 +01:00
|
|
|
pub use self::headless::HeadlessContext;
|
2014-07-31 10:52:05 +02:00
|
|
|
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
|
|
|
|
|
2014-12-01 18:24:15 +01:00
|
|
|
use winapi;
|
2015-01-25 21:52:17 -05:00
|
|
|
use user32;
|
|
|
|
|
use kernel32;
|
|
|
|
|
use gdi32;
|
2014-12-01 18:24:15 +01:00
|
|
|
|
2015-02-16 10:08:08 +01:00
|
|
|
mod callback;
|
2014-07-27 18:55:14 +02:00
|
|
|
mod event;
|
2014-12-01 18:24:15 +01:00
|
|
|
mod gl;
|
2015-02-16 10:01:47 +01:00
|
|
|
mod headless;
|
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-08-01 23:02:26 +02:00
|
|
|
/// The Win32 implementation of the main `Window` object.
|
2014-07-27 10:55:37 +02:00
|
|
|
pub struct Window {
|
2014-08-01 23:02:26 +02:00
|
|
|
/// Main handle for the window.
|
2014-12-01 18:24:15 +01:00
|
|
|
window: winapi::HWND,
|
2014-08-01 23:02:26 +02:00
|
|
|
|
|
|
|
|
/// This represents a "draw context" for the surface of the window.
|
2014-12-01 18:24:15 +01:00
|
|
|
hdc: winapi::HDC,
|
2014-08-01 23:02:26 +02:00
|
|
|
|
|
|
|
|
/// OpenGL context.
|
2014-12-01 18:24:15 +01:00
|
|
|
context: winapi::HGLRC,
|
2014-08-01 23:02:26 +02: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-12-01 18:24:15 +01:00
|
|
|
gl_library: winapi::HMODULE,
|
2014-08-01 23:02:26 +02:00
|
|
|
|
|
|
|
|
/// Receiver for the events dispatched by the window callback.
|
2014-07-27 10:55:37 +02:00
|
|
|
events_receiver: Receiver<Event>,
|
2014-08-01 23:02:26 +02:00
|
|
|
|
|
|
|
|
/// 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.
|
2014-12-28 15:08:41 +01:00
|
|
|
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
|
2015-01-14 10:04:28 +01:00
|
|
|
let (builder, sharing) = builder.extract_non_static();
|
|
|
|
|
let sharing = sharing.map(|w| init::ContextHack(w.context));
|
|
|
|
|
init::new_window(builder, sharing)
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
2014-10-04 19:17:02 +02:00
|
|
|
}
|
2014-07-27 10:55:37 +02:00
|
|
|
|
2015-01-03 23:11:59 +01:00
|
|
|
#[derive(Clone)]
|
2014-12-17 14:49:11 +10:00
|
|
|
pub struct WindowProxy;
|
|
|
|
|
|
|
|
|
|
impl WindowProxy {
|
|
|
|
|
pub fn wakeup_event_loop(&self) {
|
2014-12-19 05:00:43 +10:00
|
|
|
unimplemented!()
|
2014-12-17 14:49:11 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2015-01-05 14:30:42 +01:00
|
|
|
use std::sync::atomic::Ordering::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.
|
2015-01-01 23:09:16 -08:00
|
|
|
///
|
2014-07-27 10:55:37 +02:00
|
|
|
/// Calls SetWindowText on the HWND.
|
|
|
|
|
pub fn set_title(&self, text: &str) {
|
|
|
|
|
unsafe {
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::SetWindowTextW(self.window,
|
2014-10-11 17:58:17 +02:00
|
|
|
text.utf16_units().chain(Some(0).into_iter())
|
2014-12-01 18:24:15 +01:00
|
|
|
.collect::<Vec<u16>>().as_ptr() as winapi::LPCWSTR);
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-01 09:02:01 +01:00
|
|
|
pub fn show(&self) {
|
|
|
|
|
unsafe {
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::ShowWindow(self.window, winapi::SW_SHOW);
|
2014-11-01 09:02:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn hide(&self) {
|
|
|
|
|
unsafe {
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::ShowWindow(self.window, winapi::SW_HIDE);
|
2014-11-01 09:02:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 11:21:12 +02:00
|
|
|
/// See the docs in the crate root file.
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn get_position(&self) -> Option<(i32, i32)> {
|
2014-07-30 13:10:17 +02:00
|
|
|
use std::mem;
|
2014-07-27 22:46:30 +02:00
|
|
|
|
2014-12-01 18:24:15 +01:00
|
|
|
let mut placement: winapi::WINDOWPLACEMENT = unsafe { mem::zeroed() };
|
|
|
|
|
placement.length = mem::size_of::<winapi::WINDOWPLACEMENT>() as winapi::UINT;
|
2014-07-27 22:46:30 +02:00
|
|
|
|
2015-01-25 21:52:17 -05:00
|
|
|
if unsafe { user32::GetWindowPlacement(self.window, &mut placement) } == 0 {
|
2014-07-30 13:10:17 +02:00
|
|
|
return None
|
2014-07-27 22:46:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let ref rect = placement.rcNormalPosition;
|
2015-01-13 15:21:36 +03:00
|
|
|
Some((rect.left as i32, rect.top as i32))
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-22 11:21:12 +02:00
|
|
|
/// See the docs in the crate root file.
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn set_position(&self, x: i32, y: i32) {
|
2014-07-27 10:55:37 +02:00
|
|
|
use libc;
|
|
|
|
|
|
|
|
|
|
unsafe {
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::SetWindowPos(self.window, ptr::null_mut(), x as libc::c_int, y as libc::c_int,
|
2014-12-01 18:24:15 +01:00
|
|
|
0, 0, winapi::SWP_NOZORDER | winapi::SWP_NOSIZE);
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::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.
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
|
2014-07-30 13:10:17 +02:00
|
|
|
use std::mem;
|
2014-12-01 18:24:15 +01:00
|
|
|
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
|
2014-07-27 22:36:44 +02:00
|
|
|
|
2015-01-25 21:52:17 -05:00
|
|
|
if unsafe { user32::GetClientRect(self.window, &mut rect) } == 0 {
|
2014-07-30 13:10:17 +02:00
|
|
|
return None
|
2014-07-27 22:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 13:10:17 +02:00
|
|
|
Some((
|
2015-01-13 15:21:36 +03:00
|
|
|
(rect.right - rect.left) as u32,
|
|
|
|
|
(rect.bottom - rect.top) as u32
|
2014-07-30 13:10:17 +02:00
|
|
|
))
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-22 11:21:12 +02:00
|
|
|
/// See the docs in the crate root file.
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
|
2014-07-30 13:10:17 +02:00
|
|
|
use std::mem;
|
2014-12-01 18:24:15 +01:00
|
|
|
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
|
2014-07-27 22:36:44 +02:00
|
|
|
|
2015-01-25 21:52:17 -05:00
|
|
|
if unsafe { user32::GetWindowRect(self.window, &mut rect) } == 0 {
|
2014-07-30 13:10:17 +02:00
|
|
|
return None
|
2014-07-27 22:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-30 13:10:17 +02:00
|
|
|
Some((
|
2015-01-13 15:21:36 +03:00
|
|
|
(rect.right - rect.left) as u32,
|
|
|
|
|
(rect.bottom - rect.top) as u32
|
2014-07-30 13:10:17 +02:00
|
|
|
))
|
2014-07-27 22:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-22 11:21:12 +02:00
|
|
|
/// See the docs in the crate root file.
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn set_inner_size(&self, x: u32, y: u32) {
|
2014-07-27 10:55:37 +02:00
|
|
|
use libc;
|
|
|
|
|
|
|
|
|
|
unsafe {
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::SetWindowPos(self.window, ptr::null_mut(), 0, 0, x as libc::c_int,
|
2014-12-01 18:24:15 +01:00
|
|
|
y as libc::c_int, winapi::SWP_NOZORDER | winapi::SWP_NOREPOSITION);
|
2015-01-25 21:52:17 -05:00
|
|
|
user32::UpdateWindow(self.window);
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-17 14:49:11 +10: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-10-25 11:22:52 +02:00
|
|
|
pub fn poll_events(&self) -> PollEventsIterator {
|
|
|
|
|
PollEventsIterator {
|
|
|
|
|
window: self,
|
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-10-25 11:22:52 +02:00
|
|
|
pub fn wait_events(&self) -> WaitEventsIterator {
|
|
|
|
|
WaitEventsIterator {
|
|
|
|
|
window: self,
|
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) {
|
2014-09-12 15:51:43 +02:00
|
|
|
// TODO: check return value
|
2014-12-01 18:24:15 +01:00
|
|
|
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 () {
|
2015-01-08 09:28:22 +01:00
|
|
|
let addr = CString::from_slice(addr.as_bytes());
|
|
|
|
|
let addr = addr.as_slice_with_nul().as_ptr();
|
2014-07-27 10:55:37 +02:00
|
|
|
|
|
|
|
|
unsafe {
|
2015-01-08 09:28:22 +01:00
|
|
|
let p = gl::wgl::GetProcAddress(addr) as *const ();
|
|
|
|
|
if !p.is_null() { return p; }
|
2015-01-25 21:52:17 -05:00
|
|
|
kernel32::GetProcAddress(self.gl_library, addr) 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 {
|
2015-01-25 21:52:17 -05:00
|
|
|
gdi32::SwapBuffers(self.hdc);
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-10-24 15:20:25 +10:00
|
|
|
|
|
|
|
|
pub fn platform_display(&self) -> *mut libc::c_void {
|
2014-10-25 10:31:25 +02:00
|
|
|
unimplemented!()
|
2014-10-24 15:20:25 +10:00
|
|
|
}
|
2014-11-18 17:55:26 +01:00
|
|
|
|
2015-02-20 12:32:40 -08:00
|
|
|
pub fn platform_window(&self) -> *mut libc::c_void {
|
|
|
|
|
self.window as *mut libc::c_void
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-18 17:55:26 +01:00
|
|
|
/// See the docs in the crate root file.
|
|
|
|
|
pub fn get_api(&self) -> ::Api {
|
|
|
|
|
::Api::OpenGl
|
|
|
|
|
}
|
2014-12-16 15:49:22 +10:00
|
|
|
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
|
2014-12-16 15:49:22 +10:00
|
|
|
}
|
2015-01-12 16:22:37 -08:00
|
|
|
|
|
|
|
|
pub fn set_cursor(&self, cursor: MouseCursor) {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
2014-12-19 11:27:03 +10:00
|
|
|
|
|
|
|
|
pub fn hidpi_factor(&self) -> f32 {
|
|
|
|
|
1.0
|
|
|
|
|
}
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-25 11:22:52 +02:00
|
|
|
pub struct PollEventsIterator<'a> {
|
|
|
|
|
window: &'a Window,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for PollEventsIterator<'a> {
|
|
|
|
|
type Item = Event;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Event> {
|
|
|
|
|
use events::Event::Closed;
|
|
|
|
|
|
2015-02-11 15:34:08 +01:00
|
|
|
match self.window.events_receiver.try_recv() {
|
2014-10-25 11:22:52 +02:00
|
|
|
Ok(Closed) => {
|
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
|
|
|
|
self.window.is_closed.store(true, Relaxed);
|
|
|
|
|
Some(Closed)
|
|
|
|
|
},
|
|
|
|
|
Ok(ev) => Some(ev),
|
|
|
|
|
Err(_) => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct WaitEventsIterator<'a> {
|
|
|
|
|
window: &'a Window,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Iterator for WaitEventsIterator<'a> {
|
|
|
|
|
type Item = Event;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Event> {
|
|
|
|
|
use events::Event::Closed;
|
|
|
|
|
|
|
|
|
|
match self.window.events_receiver.recv() {
|
|
|
|
|
Ok(Closed) => {
|
|
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
|
|
|
|
self.window.is_closed.store(true, Relaxed);
|
|
|
|
|
Some(Closed)
|
|
|
|
|
},
|
|
|
|
|
Ok(ev) => Some(ev),
|
|
|
|
|
Err(_) => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 10:55:37 +02:00
|
|
|
#[unsafe_destructor]
|
|
|
|
|
impl Drop for Window {
|
|
|
|
|
fn drop(&mut self) {
|
2014-08-03 17:33:48 +02:00
|
|
|
use std::ptr;
|
2015-02-18 08:58:37 +01:00
|
|
|
// we don't call MakeCurrent(0, 0) because we are not sure that the context
|
|
|
|
|
// is still the current one
|
2015-01-25 21:52:17 -05:00
|
|
|
unsafe { user32::PostMessageW(self.window, winapi::WM_DESTROY, 0, 0); }
|
2014-12-01 18:24:15 +01:00
|
|
|
unsafe { gl::wgl::DeleteContext(self.context as *const libc::c_void); }
|
2015-01-25 21:52:17 -05:00
|
|
|
unsafe { user32::DestroyWindow(self.window); }
|
2014-07-27 10:55:37 +02:00
|
|
|
}
|
|
|
|
|
}
|