diff options
Diffstat (limited to 'src/api/win32')
-rw-r--r-- | src/api/win32/callback.rs | 22 | ||||
-rw-r--r-- | src/api/win32/init.rs | 75 | ||||
-rw-r--r-- | src/api/win32/mod.rs | 21 |
3 files changed, 65 insertions, 53 deletions
diff --git a/src/api/win32/callback.rs b/src/api/win32/callback.rs index c6e7763..da8e1a6 100644 --- a/src/api/win32/callback.rs +++ b/src/api/win32/callback.rs @@ -102,6 +102,14 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, 0 }, + // Prevents default windows menu hotkeys playing unwanted + // "ding" sounds. Alternatively could check for WM_SYSCOMMAND + // with wparam being SC_KEYMENU, but this may prevent some + // other unwanted default hotkeys as well. + winapi::WM_SYSCHAR => { + 0 + } + winapi::WM_MOUSEMOVE => { use events::Event::MouseMoved; @@ -126,15 +134,19 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, 0 }, - winapi::WM_KEYDOWN => { + winapi::WM_KEYDOWN | winapi::WM_SYSKEYDOWN => { use events::Event::KeyboardInput; use events::ElementState::Pressed; - let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam); - send_event(window, KeyboardInput(Pressed, scancode, vkey)); - 0 + if msg == winapi::WM_SYSKEYDOWN && wparam as i32 == winapi::VK_F4 { + user32::DefWindowProcW(window, msg, wparam, lparam) + } else { + let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam); + send_event(window, KeyboardInput(Pressed, scancode, vkey)); + 0 + } }, - winapi::WM_KEYUP => { + winapi::WM_KEYUP | winapi::WM_SYSKEYUP => { use events::Event::KeyboardInput; use events::ElementState::Released; let (scancode, vkey) = event::vkeycode_to_element(wparam, lparam); diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index c3225b6..7c7eb83 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -11,11 +11,13 @@ use super::WindowWrapper; use super::Context; use Api; -use BuilderAttribs; use CreationError; use CreationError::OsError; use CursorState; +use GlAttributes; use GlRequest; +use PixelFormatRequirements; +use WindowAttributes; use std::ffi::{OsStr}; use std::os::windows::ffi::OsStrExt; @@ -31,6 +33,7 @@ use api::egl; use api::egl::Context as EglContext; use api::egl::ffi::egl::Egl; +#[derive(Clone)] pub enum RawContext { Egl(egl::ffi::egl::types::EGLContext), Wgl(winapi::HGLRC), @@ -39,15 +42,18 @@ pub enum RawContext { unsafe impl Send for RawContext {} unsafe impl Sync for RawContext {} -pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<RawContext>, - egl: Option<&Egl>) +pub fn new_window(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<RawContext>, egl: Option<&Egl>) -> Result<Window, CreationError> { let egl = egl.map(|e| e.clone()); + let window = window.clone(); + let pf_reqs = pf_reqs.clone(); + let opengl = opengl.clone(); // initializing variables to be sent to the task - let title = OsStr::new(&builder.title).encode_wide().chain(Some(0).into_iter()) + let title = OsStr::new(&window.title).encode_wide().chain(Some(0).into_iter()) .collect::<Vec<_>>(); let (tx, rx) = channel(); @@ -57,7 +63,7 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<R thread::spawn(move || { unsafe { // creating and sending the `Window` - match init(title, builder, builder_sharelists, egl) { + match init(title, &window, &pf_reqs, &opengl, egl) { Ok(w) => tx.send(Ok(w)).ok(), Err(e) => { tx.send(Err(e)).ok(); @@ -83,29 +89,36 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<R rx.recv().unwrap() } -unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, - builder_sharelists: Option<RawContext>, egl: Option<Egl>) +unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<RawContext>, egl: Option<Egl>) -> Result<Window, CreationError> { + let opengl = opengl.clone().map_sharing(|sharelists| { + match sharelists { + RawContext::Wgl(c) => c, + _ => unimplemented!() + } + }); + // registering the window class let class_name = register_window_class(); // building a RECT object with coordinates let mut rect = winapi::RECT { - left: 0, right: builder.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, - top: 0, bottom: builder.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG, + left: 0, right: window.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, + top: 0, bottom: window.dimensions.unwrap_or((1024, 768)).1 as winapi::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 window.monitor.is_some() { + let monitor = window.monitor.as_ref().unwrap(); try!(switch_to_fullscreen(&mut rect, monitor)); } // computing the style and extended style of the window - let (ex_style, style) = if builder.monitor.is_some() || builder.decorations == false { + let (ex_style, style) = if window.monitor.is_some() || window.decorations == false { (winapi::WS_EX_APPWINDOW, winapi::WS_POPUP | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN) } else { (winapi::WS_EX_APPWINDOW | winapi::WS_EX_WINDOWEDGE, @@ -117,19 +130,19 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, // creating the real window this time, by using the functions in `extra_functions` let real_window = { - let (width, height) = if builder.monitor.is_some() || builder.dimensions.is_some() { + let (width, height) = if window.monitor.is_some() || window.dimensions.is_some() { (Some(rect.right - rect.left), Some(rect.bottom - rect.top)) } else { (None, None) }; - let (x, y) = if builder.monitor.is_some() { + let (x, y) = if window.monitor.is_some() { (Some(rect.left), Some(rect.top)) } else { (None, None) }; - let style = if !builder.visible || builder.headless { + let style = if !window.visible { style } else { style | winapi::WS_VISIBLE @@ -159,51 +172,33 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, }; // creating the OpenGL context - let context = match builder.gl_version { + let context = match opengl.version { GlRequest::Specific(Api::OpenGlEs, (_major, _minor)) => { if let Some(egl) = egl { - if let Ok(c) = EglContext::new(egl, &builder, + if let Ok(c) = EglContext::new(egl, &pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), egl::NativeDisplay::Other(Some(ptr::null()))) .and_then(|p| p.finish(real_window.0)) { Context::Egl(c) } else { - let builder_sharelists = match builder_sharelists { - None => None, - Some(RawContext::Wgl(c)) => Some(c), - _ => unimplemented!() - }; - - try!(WglContext::new(&builder, real_window.0, builder_sharelists) + try!(WglContext::new(&pf_reqs, &opengl, real_window.0) .map(Context::Wgl)) } } else { // falling back to WGL, which is always available - let builder_sharelists = match builder_sharelists { - None => None, - Some(RawContext::Wgl(c)) => Some(c), - _ => unimplemented!() - }; - - try!(WglContext::new(&builder, real_window.0, builder_sharelists) + try!(WglContext::new(&pf_reqs, &opengl, real_window.0) .map(Context::Wgl)) } }, _ => { - let builder_sharelists = match builder_sharelists { - None => None, - Some(RawContext::Wgl(c)) => Some(c), - _ => unimplemented!() - }; - - try!(WglContext::new(&builder, real_window.0, builder_sharelists).map(Context::Wgl)) + try!(WglContext::new(&pf_reqs, &opengl, real_window.0).map(Context::Wgl)) } }; // making the window transparent - if builder.transparent { + if window.transparent { let bb = winapi::DWM_BLURBEHIND { dwFlags: 0x1, // FIXME: DWM_BB_ENABLE; fEnable: 1, @@ -215,7 +210,7 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, } // calling SetForegroundWindow if fullscreen - if builder.monitor.is_some() { + if window.monitor.is_some() { user32::SetForegroundWindow(real_window.0); } diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs index dea6357..f580950 100644 --- a/src/api/win32/mod.rs +++ b/src/api/win32/mod.rs @@ -13,11 +13,13 @@ use libc; use ContextError; use {CreationError, Event, MouseCursor}; use CursorState; +use GlAttributes; use GlContext; use Api; use PixelFormat; -use BuilderAttribs; +use PixelFormatRequirements; +use WindowAttributes; pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; @@ -83,15 +85,18 @@ impl WindowProxy { impl Window { /// See the docs in the crate root file. - pub fn new(builder: BuilderAttribs, egl: Option<&Egl>) -> Result<Window, CreationError> { - let (builder, sharing) = builder.extract_non_static(); - - let sharing = sharing.map(|w| match w.context { - Context::Wgl(ref c) => RawContext::Wgl(c.get_hglrc()), - Context::Egl(_) => unimplemented!(), // FIXME: + 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(builder, sharing, egl) + init::new_window(window, pf_reqs, &opengl, egl) } /// See the docs in the crate root file. |