winit/src/api/win32/mod.rs

436 lines
12 KiB
Rust
Raw Normal View History

2015-04-24 09:51:23 +02:00
#![cfg(target_os = "windows")]
use std::mem;
2014-07-27 10:55:37 +02:00
use std::ptr;
2015-04-03 08:33:51 +02:00
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
2015-04-01 10:04:43 -07:00
use std::sync::{
Arc,
Mutex
};
2015-01-03 23:11:59 +01:00
use std::sync::mpsc::Receiver;
use libc;
use ContextError;
2015-01-12 19:45:20 -08:00
use {CreationError, Event, MouseCursor};
use CursorState;
use GlAttributes;
2015-04-30 13:23:37 +02:00
use GlContext;
2014-10-04 19:17:02 +02:00
2015-04-30 13:23:37 +02:00
use Api;
2015-04-11 09:06:08 +02:00
use PixelFormat;
use PixelFormatRequirements;
use WindowAttributes;
2014-07-27 10:55:37 +02:00
2015-09-24 09:11:59 +02:00
pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
2014-07-31 10:52:05 +02:00
use winapi;
use user32;
use kernel32;
2015-05-20 22:38:06 +02:00
use api::wgl::Context as WglContext;
use api::egl::Context as EglContext;
use api::egl::ffi::egl::Egl;
use self::init::RawContext;
2015-02-16 10:08:08 +01:00
mod callback;
2014-07-27 18:55:14 +02:00
mod event;
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
2015-09-22 14:02:36 -04:00
lazy_static! {
static ref WAKEUP_MSG_ID: u32 = unsafe { user32::RegisterWindowMessageA("Glutin::EventID".as_ptr() as *const i8) };
}
/// 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: WindowWrapper,
/// OpenGL context.
context: Context,
/// Receiver for the events dispatched by the window callback.
2014-07-27 10:55:37 +02:00
events_receiver: Receiver<Event>,
/// The current cursor state.
2015-04-01 10:04:43 -07:00
cursor_state: Arc<Mutex<CursorState>>,
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 {}
enum Context {
Egl(EglContext),
Wgl(WglContext),
}
/// A simple wrapper that destroys the window when it is destroyed.
2015-03-21 13:43:14 +01:00
// FIXME: remove `pub` (https://github.com/rust-lang/rust/issues/23585)
#[doc(hidden)]
pub struct WindowWrapper(pub winapi::HWND, pub winapi::HDC);
impl Drop for WindowWrapper {
2015-09-21 14:42:05 +02:00
#[inline]
fn drop(&mut self) {
unsafe {
user32::DestroyWindow(self.0);
}
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)]
2015-09-22 14:02:36 -04:00
pub struct WindowProxy {
hwnd: winapi::HWND,
}
unsafe impl Send for WindowProxy {}
unsafe impl Sync for WindowProxy {}
impl WindowProxy {
2015-09-21 14:42:05 +02:00
#[inline]
pub fn wakeup_event_loop(&self) {
2015-09-22 14:02:36 -04:00
unsafe {
user32::PostMessageA(self.hwnd, *WAKEUP_MSG_ID, 0, 0);
}
}
}
2014-10-04 19:17:02 +02:00
impl Window {
/// See the docs in the crate root file.
pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>, egl: Option<&Egl>)
-> Result<Window, CreationError>
{
let opengl = opengl.clone().map_sharing(|sharing| {
match sharing.context {
Context::Wgl(ref c) => RawContext::Wgl(c.get_hglrc()),
Context::Egl(_) => unimplemented!(), // FIXME:
}
});
init::new_window(window, pf_reqs, &opengl, egl)
}
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) {
2015-04-03 18:36:57 +02:00
let text = OsStr::new(text).encode_wide().chain(Some(0).into_iter())
.collect::<Vec<_>>();
2015-04-03 08:33:51 +02:00
2014-07-27 10:55:37 +02:00
unsafe {
2015-04-03 08:33:51 +02:00
user32::SetWindowTextW(self.window.0, text.as_ptr() as winapi::LPCWSTR);
2014-07-27 10:55:37 +02:00
}
}
2015-09-21 14:42:05 +02:00
#[inline]
pub fn show(&self) {
unsafe {
user32::ShowWindow(self.window.0, winapi::SW_SHOW);
}
}
2015-09-21 14:42:05 +02:00
#[inline]
pub fn hide(&self) {
unsafe {
user32::ShowWindow(self.window.0, 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<(i32, i32)> {
use std::mem;
let mut placement: winapi::WINDOWPLACEMENT = unsafe { mem::zeroed() };
placement.length = mem::size_of::<winapi::WINDOWPLACEMENT>() as winapi::UINT;
if unsafe { user32::GetWindowPlacement(self.window.0, &mut placement) } == 0 {
return None
}
let ref rect = placement.rcNormalPosition;
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.
pub fn set_position(&self, x: i32, y: i32) {
2014-07-27 10:55:37 +02:00
use libc;
unsafe {
user32::SetWindowPos(self.window.0, ptr::null_mut(), x as libc::c_int, y as libc::c_int,
0, 0, winapi::SWP_NOZORDER | winapi::SWP_NOSIZE);
user32::UpdateWindow(self.window.0);
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-09-21 14:42:05 +02:00
#[inline]
pub fn get_inner_size(&self) -> Option<(u32, u32)> {
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
2014-07-27 22:36:44 +02:00
if unsafe { user32::GetClientRect(self.window.0, &mut rect) } == 0 {
return None
2014-07-27 22:36:44 +02:00
}
Some((
(rect.right - rect.left) as u32,
(rect.bottom - rect.top) as u32
))
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-09-21 14:42:05 +02:00
#[inline]
pub fn get_outer_size(&self) -> Option<(u32, u32)> {
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
2014-07-27 22:36:44 +02:00
if unsafe { user32::GetWindowRect(self.window.0, &mut rect) } == 0 {
return None
2014-07-27 22:36:44 +02:00
}
Some((
(rect.right - rect.left) as u32,
(rect.bottom - rect.top) as u32
))
}
2014-08-22 11:21:12 +02:00
/// See the docs in the crate root file.
pub fn set_inner_size(&self, x: u32, y: u32) {
2014-07-27 10:55:37 +02:00
use libc;
unsafe {
// Calculate the outer size based upon the specified inner size
let mut rect = winapi::RECT { top: 0, left: 0, bottom: y as winapi::LONG, right: x as winapi::LONG };
let dw_style = user32::GetWindowLongA(self.window.0, winapi::GWL_STYLE) as winapi::DWORD;
let b_menu = !user32::GetMenu(self.window.0).is_null() as winapi::BOOL;
let dw_style_ex = user32::GetWindowLongA(self.window.0, winapi::GWL_EXSTYLE) as winapi::DWORD;
user32::AdjustWindowRectEx(&mut rect, dw_style, b_menu, dw_style_ex);
let outer_x = (rect.right - rect.left).abs() as libc::c_int;
let outer_y = (rect.top - rect.bottom).abs() as libc::c_int;
user32::SetWindowPos(self.window.0, ptr::null_mut(), 0, 0, outer_x, outer_y,
winapi::SWP_NOZORDER | winapi::SWP_NOREPOSITION | winapi::SWP_NOMOVE);
user32::UpdateWindow(self.window.0);
2014-07-27 10:55:37 +02:00
}
}
2015-09-21 14:42:05 +02:00
#[inline]
pub fn create_window_proxy(&self) -> WindowProxy {
2015-09-22 14:02:36 -04:00
WindowProxy { hwnd: self.window.0 }
}
2014-08-22 11:21:12 +02:00
/// See the docs in the crate root file.
2015-09-21 14:42:05 +02:00
#[inline]
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.
2015-09-21 14:42:05 +02:00
#[inline]
pub fn wait_events(&self) -> WaitEventsIterator {
WaitEventsIterator {
window: self,
2014-07-27 18:35:42 +02:00
}
}
2015-09-21 14:42:05 +02:00
#[inline]
pub fn platform_display(&self) -> *mut libc::c_void {
// What should this return on win32?
// It could be GetDC(NULL), but that requires a ReleaseDC()
// to avoid leaking the DC.
ptr::null_mut()
}
2014-11-18 17:55:26 +01:00
2015-09-21 14:42:05 +02:00
#[inline]
pub fn platform_window(&self) -> *mut libc::c_void {
self.window.0 as *mut libc::c_void
}
2015-09-21 14:42:05 +02:00
#[inline]
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-03-01 13:18:36 +01:00
pub fn set_cursor(&self, _cursor: MouseCursor) {
unimplemented!()
}
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> {
let mut current_state = self.cursor_state.lock().unwrap();
2015-03-26 19:01:27 +01:00
let foreground_thread_id = unsafe { user32::GetWindowThreadProcessId(self.window.0, ptr::null_mut()) };
let current_thread_id = unsafe { kernel32::GetCurrentThreadId() };
unsafe { user32::AttachThreadInput(foreground_thread_id, current_thread_id, 1) };
let res = match (state, *current_state) {
(CursorState::Normal, CursorState::Normal) => Ok(()),
(CursorState::Hide, CursorState::Hide) => Ok(()),
(CursorState::Grab, CursorState::Grab) => Ok(()),
(CursorState::Hide, CursorState::Normal) => {
unsafe {
user32::SetCursor(ptr::null_mut());
*current_state = CursorState::Hide;
Ok(())
}
},
(CursorState::Normal, CursorState::Hide) => {
unsafe {
user32::SetCursor(user32::LoadCursorW(ptr::null_mut(), winapi::IDC_ARROW));
*current_state = CursorState::Normal;
Ok(())
}
},
(CursorState::Grab, CursorState::Normal) => {
unsafe {
user32::SetCursor(ptr::null_mut());
let mut rect = mem::uninitialized();
2015-03-30 12:56:08 +02:00
if user32::GetClientRect(self.window.0, &mut rect) == 0 {
return Err(format!("GetWindowRect failed"));
}
2015-03-30 12:56:08 +02:00
user32::ClientToScreen(self.window.0, mem::transmute(&mut rect.left));
user32::ClientToScreen(self.window.0, mem::transmute(&mut rect.right));
if user32::ClipCursor(&rect) == 0 {
return Err(format!("ClipCursor failed"));
}
*current_state = CursorState::Grab;
Ok(())
}
},
(CursorState::Normal, CursorState::Grab) => {
unsafe {
user32::SetCursor(user32::LoadCursorW(ptr::null_mut(), winapi::IDC_ARROW));
if user32::ClipCursor(ptr::null()) == 0 {
return Err(format!("ClipCursor failed"));
}
*current_state = CursorState::Normal;
Ok(())
}
},
_ => unimplemented!(),
2015-03-26 19:01:27 +01:00
};
unsafe { user32::AttachThreadInput(foreground_thread_id, current_thread_id, 0) };
res
}
2015-09-21 14:42:05 +02:00
#[inline]
pub fn hidpi_factor(&self) -> f32 {
1.0
}
2015-03-10 10:29:07 +01:00
pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> {
let mut point = winapi::POINT {
x: x,
y: y,
};
unsafe {
if user32::ClientToScreen(self.window.0, &mut point) == 0 {
return Err(());
}
if user32::SetCursorPos(point.x, point.y) == 0 {
return Err(());
}
}
Ok(())
}
2014-07-27 10:55:37 +02:00
}
2015-04-30 13:23:37 +02:00
impl GlContext for Window {
2015-09-21 14:42:05 +02:00
#[inline]
unsafe fn make_current(&self) -> Result<(), ContextError> {
match self.context {
Context::Wgl(ref c) => c.make_current(),
Context::Egl(ref c) => c.make_current(),
}
2015-04-30 13:23:37 +02:00
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
fn is_current(&self) -> bool {
match self.context {
Context::Wgl(ref c) => c.is_current(),
Context::Egl(ref c) => c.is_current(),
}
2015-04-30 13:23:37 +02:00
}
2015-09-21 14:42:05 +02:00
#[inline]
fn get_proc_address(&self, addr: &str) -> *const () {
match self.context {
Context::Wgl(ref c) => c.get_proc_address(addr),
Context::Egl(ref c) => c.get_proc_address(addr),
}
2015-04-30 13:23:37 +02:00
}
2015-09-21 14:42:05 +02:00
#[inline]
fn swap_buffers(&self) -> Result<(), ContextError> {
match self.context {
Context::Wgl(ref c) => c.swap_buffers(),
Context::Egl(ref c) => c.swap_buffers(),
}
2015-04-30 13:23:37 +02:00
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
fn get_api(&self) -> Api {
match self.context {
Context::Wgl(ref c) => c.get_api(),
Context::Egl(ref c) => c.get_api(),
}
2015-04-30 13:23:37 +02:00
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
fn get_pixel_format(&self) -> PixelFormat {
match self.context {
Context::Wgl(ref c) => c.get_pixel_format(),
Context::Egl(ref c) => c.get_pixel_format(),
}
2015-04-30 13:23:37 +02:00
}
}
pub struct PollEventsIterator<'a> {
window: &'a Window,
}
impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event;
2015-09-21 14:42:05 +02:00
#[inline]
fn next(&mut self) -> Option<Event> {
2015-06-16 13:48:08 +02:00
self.window.events_receiver.try_recv().ok()
}
}
pub struct WaitEventsIterator<'a> {
window: &'a Window,
}
impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;
2015-09-21 14:42:05 +02:00
#[inline]
fn next(&mut self) -> Option<Event> {
2015-06-16 13:48:08 +02:00
self.window.events_receiver.recv().ok()
}
}
2014-07-27 10:55:37 +02:00
impl Drop for Window {
2015-09-21 14:42:05 +02:00
#[inline]
2014-07-27 10:55:37 +02:00
fn drop(&mut self) {
unsafe {
// we don't call MakeCurrent(0, 0) because we are not sure that the context
// is still the current one
user32::PostMessageW(self.window.0, winapi::WM_DESTROY, 0, 0);
}
2014-07-27 10:55:37 +02:00
}
}