From d6a53cf5d3ba9c41f4bd6255012ac2adfc6487b4 Mon Sep 17 00:00:00 2001 From: Aceeri Date: Mon, 9 Nov 2015 02:49:50 -0800 Subject: 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(-) (limited to 'src/api/win32') diff --git a/src/api/win32/callback.rs b/src/api/win32/callback.rs index a36320b..bc2b6a6 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 6b52660..26ccba6 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 760020c..7a7f42a 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() }; -- cgit v1.2.3