From 78eb4a5990dadb3402dd3e80d04e2cfd7f9dd6fd Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 9 Nov 2015 01:42:54 -0800 Subject: [PATCH 1/5] Minimum/maximum dimensions for windows in win32 api --- src/api/win32/callback.rs | 59 ++++++++++++++++++++++++++++++++++++--- src/api/win32/init.rs | 8 ++++-- src/api/win32/mod.rs | 14 +++++----- src/lib.rs | 12 ++++++++ src/window.rs | 18 ++++++++++++ 5 files changed, 98 insertions(+), 13 deletions(-) diff --git a/src/api/win32/callback.rs b/src/api/win32/callback.rs index 45de9075..a36320b6 100644 --- a/src/api/win32/callback.rs +++ b/src/api/win32/callback.rs @@ -6,6 +6,7 @@ use std::sync::{Arc, Mutex}; use std::ffi::OsString; use std::os::windows::ffi::OsStringExt; +use WindowAttributes; use CursorState; use Event; use super::event; @@ -19,10 +20,26 @@ use winapi; /// a thread-local variable. thread_local!(pub static CONTEXT_STASH: RefCell> = RefCell::new(None)); +/// Contains information about states and the window for the callback. +#[derive(Clone)] +pub struct WindowState { + pub cursor_state: CursorState, + pub attributes: WindowAttributes +} + pub struct ThreadLocalData { pub win: winapi::HWND, pub sender: Sender, - pub cursor_state: Arc> + pub window_state: Arc> + //pub cursor_state: Arc> +} + +struct MinMaxInfo { + reserved: winapi::POINT, // Do not use/change + max_size: winapi::POINT, + max_position: winapi::POINT, + min_track: winapi::POINT, + max_track: winapi::POINT } /// Checks that the window is the good one, and if so send the event to it. @@ -240,9 +257,9 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, let cstash = cstash.as_ref(); // there's a very bizarre borrow checker bug // possibly related to rust-lang/rust/#23338 - let _cursor_state = if let Some(cstash) = cstash { - if let Ok(cursor_state) = cstash.cursor_state.lock() { - match *cursor_state { + let _window_state = if let Some(cstash) = cstash { + if let Ok(window_state) = cstash.window_state.lock() { + match window_state.cursor_state { CursorState::Normal => { user32::SetCursor(user32::LoadCursorW( ptr::null_mut(), @@ -281,6 +298,40 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, 0 }, + winapi::WM_GETMINMAXINFO => { + let mut mmi = lparam as *mut MinMaxInfo; + //(*mmi).max_position = winapi::POINT { x: -8, y: -8 }; // The upper left corner of the window if it were maximized on the primary monitor. + //(*mmi).max_size = winapi::POINT { x: 200, y: 200 }; // The dimensions of the primary monitor. + + CONTEXT_STASH.with(|context_stash| { + let cstash = context_stash.borrow(); + let cstash = cstash.as_ref(); + + let _window_state = if let Some(cstash) = cstash { + if let Ok(window_state) = cstash.window_state.lock() { + match window_state.attributes.min_dimensions { + Some((width, height)) => { + (*mmi).min_track = winapi::POINT { x: width as i32, y: height as i32 }; + }, + None => { + (*mmi).min_track = winapi::POINT { x: 800, y: 600 }; + } + } + + match window_state.attributes.max_dimensions { + Some((width, height)) => { + (*mmi).max_track = winapi::POINT { x: width as i32, y: height as i32 }; + }, + None => { } + } + } + } else { + return + }; + }); + 0 + }, + x if x == *super::WAKEUP_MSG_ID => { use events::Event::Awakened; send_event(window, Awakened); diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index 67e4e4ea..6b52660d 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -5,6 +5,7 @@ use std::mem; use std::thread; use super::callback; +use super::callback::WindowState; use super::Window; use super::MonitorId; use super::WindowWrapper; @@ -215,7 +216,7 @@ unsafe fn init(title: Vec, window: &WindowAttributes, pf_reqs: &PixelFormat } // Creating a mutex to track the current cursor state - let cursor_state = Arc::new(Mutex::new(CursorState::Normal)); + let cursor_state = CursorState::Normal; // filling the CONTEXT_STASH task-local storage so that we can start receiving events let events_receiver = { @@ -225,7 +226,10 @@ unsafe fn init(title: Vec, window: &WindowAttributes, pf_reqs: &PixelFormat let data = callback::ThreadLocalData { win: real_window.0, sender: tx.take().unwrap(), - cursor_state: cursor_state.clone() + window_state: Arc::new(Mutex::new(WindowState { + cursor_state: cursor_state.clone(), + attributes: window.clone() + })) }; (*context_stash.borrow_mut()) = Some(data); }); diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs index d85ef099..760020c3 100644 --- a/src/api/win32/mod.rs +++ b/src/api/win32/mod.rs @@ -54,7 +54,7 @@ pub struct Window { events_receiver: Receiver, /// The current cursor state. - cursor_state: Arc>, + cursor_state: CursorState, } unsafe impl Send for Window {} @@ -258,14 +258,14 @@ impl Window { } pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> { - let mut current_state = self.cursor_state.lock().unwrap(); + let mut current_state = self.cursor_state; 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) { + let res = match (state, current_state) { (CursorState::Normal, CursorState::Normal) => Ok(()), (CursorState::Hide, CursorState::Hide) => Ok(()), (CursorState::Grab, CursorState::Grab) => Ok(()), @@ -273,7 +273,7 @@ impl Window { (CursorState::Hide, CursorState::Normal) => { unsafe { user32::SetCursor(ptr::null_mut()); - *current_state = CursorState::Hide; + current_state = CursorState::Hide; Ok(()) } }, @@ -281,7 +281,7 @@ impl Window { (CursorState::Normal, CursorState::Hide) => { unsafe { user32::SetCursor(user32::LoadCursorW(ptr::null_mut(), winapi::IDC_ARROW)); - *current_state = CursorState::Normal; + current_state = CursorState::Normal; Ok(()) } }, @@ -298,7 +298,7 @@ impl Window { if user32::ClipCursor(&rect) == 0 { return Err(format!("ClipCursor failed")); } - *current_state = CursorState::Grab; + current_state = CursorState::Grab; Ok(()) } }, @@ -309,7 +309,7 @@ impl Window { if user32::ClipCursor(ptr::null()) == 0 { return Err(format!("ClipCursor failed")); } - *current_state = CursorState::Normal; + current_state = CursorState::Normal; Ok(()) } }, diff --git a/src/lib.rs b/src/lib.rs index 0246d520..1056e8b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -527,6 +527,16 @@ pub struct WindowAttributes { /// The default is `None`. pub dimensions: Option<(u32, u32)>, + /// The minimum dimensions a window can be, If this is `None`, the minimum will be set to 800x600. + /// + /// The default is `None`. + pub min_dimensions: Option<(u32, u32)>, + + /// The maximum dimensions a window can be, If this is `None`, the maximum will be the dimensions of the primary monitor. + /// + /// The default is `None`. + pub max_dimensions: Option<(u32, u32)>, + /// If `Some`, the window will be in fullscreen mode with the given monitor. /// /// The default is `None`. @@ -563,6 +573,8 @@ impl Default for WindowAttributes { fn default() -> WindowAttributes { WindowAttributes { dimensions: None, + min_dimensions: None, + max_dimensions: None, monitor: None, title: "glutin window".to_owned(), visible: true, diff --git a/src/window.rs b/src/window.rs index 86322bd2..f2d65807 100644 --- a/src/window.rs +++ b/src/window.rs @@ -52,6 +52,24 @@ impl<'a> WindowBuilder<'a> { self.window.dimensions = Some((width, height)); self } + + /// Sets a minimum dimension size for the window + /// + /// Width and height are in pixels. + #[inline] + pub fn with_min_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> { + self.window.min_dimensions = Some((width, height)); + self + } + + /// Sets a maximum dimension size for the window + /// + /// Width and height are in pixels. + #[inline] + pub fn with_max_dimensions(mut self, width: u32, height: u32) -> WindowBuilder<'a> { + self.window.max_dimensions = Some((width, height)); + self + } /// Requests a specific title for the window. #[inline] From d6a53cf5d3ba9c41f4bd6255012ac2adfc6487b4 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 9 Nov 2015 02:49:50 -0800 Subject: [PATCH 2/5] Corrections to sharing data --- src/api/win32/callback.rs | 10 ++-------- src/api/win32/init.rs | 16 ++++++++-------- src/api/win32/mod.rs | 13 ++++++++++--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/api/win32/callback.rs b/src/api/win32/callback.rs index a36320b6..bc2b6a64 100644 --- a/src/api/win32/callback.rs +++ b/src/api/win32/callback.rs @@ -10,6 +10,7 @@ use WindowAttributes; use CursorState; use Event; use super::event; +use super::WindowState; use user32; use shell32; @@ -20,13 +21,6 @@ use winapi; /// a thread-local variable. thread_local!(pub static CONTEXT_STASH: RefCell> = RefCell::new(None)); -/// Contains information about states and the window for the callback. -#[derive(Clone)] -pub struct WindowState { - pub cursor_state: CursorState, - pub attributes: WindowAttributes -} - pub struct ThreadLocalData { pub win: winapi::HWND, pub sender: Sender, @@ -257,7 +251,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, let cstash = cstash.as_ref(); // there's a very bizarre borrow checker bug // possibly related to rust-lang/rust/#23338 - let _window_state = if let Some(cstash) = cstash { + let _cursor_state = if let Some(cstash) = cstash { if let Ok(window_state) = cstash.window_state.lock() { match window_state.cursor_state { CursorState::Normal => { diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index 6b52660d..26ccba65 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -5,7 +5,7 @@ use std::mem; use std::thread; use super::callback; -use super::callback::WindowState; +use super::WindowState; use super::Window; use super::MonitorId; use super::WindowWrapper; @@ -215,8 +215,11 @@ unsafe fn init(title: Vec, window: &WindowAttributes, pf_reqs: &PixelFormat user32::SetForegroundWindow(real_window.0); } - // Creating a mutex to track the current cursor state - let cursor_state = CursorState::Normal; + // Creating a mutex to track the current window state + let window_state = Arc::new(Mutex::new(WindowState { + cursor_state: CursorState::Normal, + attributes: window.clone() + })); // filling the CONTEXT_STASH task-local storage so that we can start receiving events let events_receiver = { @@ -226,10 +229,7 @@ unsafe fn init(title: Vec, window: &WindowAttributes, pf_reqs: &PixelFormat let data = callback::ThreadLocalData { win: real_window.0, sender: tx.take().unwrap(), - window_state: Arc::new(Mutex::new(WindowState { - cursor_state: cursor_state.clone(), - attributes: window.clone() - })) + window_state: window_state.clone() }; (*context_stash.borrow_mut()) = Some(data); }); @@ -241,7 +241,7 @@ unsafe fn init(title: Vec, window: &WindowAttributes, pf_reqs: &PixelFormat window: real_window, context: context, events_receiver: events_receiver, - cursor_state: cursor_state, + window_state: window_state, }) } diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs index 760020c3..7a7f42ae 100644 --- a/src/api/win32/mod.rs +++ b/src/api/win32/mod.rs @@ -42,6 +42,13 @@ lazy_static! { static ref WAKEUP_MSG_ID: u32 = unsafe { user32::RegisterWindowMessageA("Glutin::EventID".as_ptr() as *const i8) }; } +/// Contains information about states and the window for the callback. +#[derive(Clone)] +pub struct WindowState { + pub cursor_state: CursorState, + pub attributes: WindowAttributes +} + /// The Win32 implementation of the main `Window` object. pub struct Window { /// Main handle for the window. @@ -53,8 +60,8 @@ pub struct Window { /// Receiver for the events dispatched by the window callback. events_receiver: Receiver, - /// The current cursor state. - cursor_state: CursorState, + /// The current window state. + window_state: Arc>, } unsafe impl Send for Window {} @@ -258,7 +265,7 @@ impl Window { } pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> { - let mut current_state = self.cursor_state; + let mut current_state = self.window_state.lock().unwrap().cursor_state; let foreground_thread_id = unsafe { user32::GetWindowThreadProcessId(self.window.0, ptr::null_mut()) }; let current_thread_id = unsafe { kernel32::GetCurrentThreadId() }; From c6ffedccbdf61cf86591989e61cae73d95251df5 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 9 Nov 2015 03:13:52 -0800 Subject: [PATCH 3/5] Removed comments, fixed Some/None matches, removed unnecessary lock checks --- src/api/win32/callback.rs | 21 ++++++++------------- src/lib.rs | 4 ++-- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/api/win32/callback.rs b/src/api/win32/callback.rs index bc2b6a64..5cd8ade5 100644 --- a/src/api/win32/callback.rs +++ b/src/api/win32/callback.rs @@ -25,7 +25,6 @@ pub struct ThreadLocalData { pub win: winapi::HWND, pub sender: Sender, pub window_state: Arc> - //pub cursor_state: Arc> } struct MinMaxInfo { @@ -295,21 +294,18 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, winapi::WM_GETMINMAXINFO => { let mut mmi = lparam as *mut MinMaxInfo; //(*mmi).max_position = winapi::POINT { x: -8, y: -8 }; // The upper left corner of the window if it were maximized on the primary monitor. - //(*mmi).max_size = winapi::POINT { x: 200, y: 200 }; // The dimensions of the primary monitor. + //(*mmi).max_size = winapi::POINT { x: .., y: .. }; // The dimensions of the primary monitor. CONTEXT_STASH.with(|context_stash| { - let cstash = context_stash.borrow(); - let cstash = cstash.as_ref(); + match context_stash.borrow().as_ref() { + Some(cstash) => { + let window_state = cstash.window_state.lock().unwrap(); - let _window_state = if let Some(cstash) = cstash { - if let Ok(window_state) = cstash.window_state.lock() { match window_state.attributes.min_dimensions { Some((width, height)) => { (*mmi).min_track = winapi::POINT { x: width as i32, y: height as i32 }; }, - None => { - (*mmi).min_track = winapi::POINT { x: 800, y: 600 }; - } + None => { } } match window_state.attributes.max_dimensions { @@ -318,10 +314,9 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT, }, None => { } } - } - } else { - return - }; + }, + None => { } + } }); 0 }, diff --git a/src/lib.rs b/src/lib.rs index 1056e8b5..c5431559 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -527,12 +527,12 @@ pub struct WindowAttributes { /// The default is `None`. pub dimensions: Option<(u32, u32)>, - /// The minimum dimensions a window can be, If this is `None`, the minimum will be set to 800x600. + /// The minimum dimensions a window can be, If this is `None`, the window will have no minimum dimensions (aside from reserved). /// /// The default is `None`. pub min_dimensions: Option<(u32, u32)>, - /// The maximum dimensions a window can be, If this is `None`, the maximum will be the dimensions of the primary monitor. + /// The maximum dimensions a window can be, If this is `None`, the maximum will have no maximum or will be set to the primary monitor's dimensions by the platform. /// /// The default is `None`. pub max_dimensions: Option<(u32, u32)>, From 9a05577df8f3e616de94635ef41e0d8601f3091c Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 9 Nov 2015 03:18:48 -0800 Subject: [PATCH 4/5] Not implemented asserts to prevent silent fails --- src/api/android/mod.rs | 4 ++++ src/api/cocoa/mod.rs | 4 ++++ src/api/wayland/mod.rs | 4 ++++ src/api/x11/window.rs | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/src/api/android/mod.rs b/src/api/android/mod.rs index 3dcea902..f42caeb1 100644 --- a/src/api/android/mod.rs +++ b/src/api/android/mod.rs @@ -120,6 +120,10 @@ impl Window { { use std::{mem, ptr}; + // not implemented + assert!(win_attribs.min_dimensions.is_none()); + assert!(win_attribs.max_dimensions.is_none()); + let opengl = opengl.clone().map_sharing(|w| &w.context); let native_window = unsafe { android_glue::get_native_window() }; diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index c1b22c9b..b7e0239e 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -268,6 +268,10 @@ impl Window { unimplemented!() } + // not implemented + assert!(win_attribs.min_dimensions.is_none()); + assert!(win_attribs.max_dimensions.is_none()); + match opengl.robustness { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { return Err(CreationError::RobustnessNotSupported); diff --git a/src/api/wayland/mod.rs b/src/api/wayland/mod.rs index 2877713e..8084516b 100644 --- a/src/api/wayland/mod.rs +++ b/src/api/wayland/mod.rs @@ -257,6 +257,10 @@ impl Window { { use self::wayland::internals::FFI; + // not implemented + assert!(win_attribs.min_dimensions.is_none()); + assert!(win_attribs.max_dimensions.is_none()); + let wayland_context = match *WAYLAND_CONTEXT { Some(ref c) => c, None => return Err(CreationError::NotSupported), diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index b964fed6..4d2dfb24 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -304,6 +304,10 @@ impl Window { { let dimensions = window_attrs.dimensions.unwrap_or((800, 600)); + // not implemented + assert!(win_attribs.min_dimensions.is_none()); + assert!(win_attribs.max_dimensions.is_none()); + let screen_id = match window_attrs.monitor { Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32, _ => unsafe { (display.xlib.XDefaultScreen)(display.display) }, From 2be3c142f0778a3588982572a997dc669adca2a6 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 9 Nov 2015 03:29:26 -0800 Subject: [PATCH 5/5] Fixed wrong variable binding for WindowAttributes on asserts --- src/api/wayland/mod.rs | 4 ++-- src/api/x11/window.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/wayland/mod.rs b/src/api/wayland/mod.rs index 8084516b..ab71ffae 100644 --- a/src/api/wayland/mod.rs +++ b/src/api/wayland/mod.rs @@ -258,8 +258,8 @@ impl Window { use self::wayland::internals::FFI; // not implemented - assert!(win_attribs.min_dimensions.is_none()); - assert!(win_attribs.max_dimensions.is_none()); + assert!(window.min_dimensions.is_none()); + assert!(window.max_dimensions.is_none()); let wayland_context = match *WAYLAND_CONTEXT { Some(ref c) => c, diff --git a/src/api/x11/window.rs b/src/api/x11/window.rs index 4d2dfb24..09470ff2 100644 --- a/src/api/x11/window.rs +++ b/src/api/x11/window.rs @@ -305,8 +305,8 @@ impl Window { let dimensions = window_attrs.dimensions.unwrap_or((800, 600)); // not implemented - assert!(win_attribs.min_dimensions.is_none()); - assert!(win_attribs.max_dimensions.is_none()); + assert!(window_attrs.min_dimensions.is_none()); + assert!(window_attrs.max_dimensions.is_none()); let screen_id = match window_attrs.monitor { Some(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32,