Corrections to sharing data

This commit is contained in:
Aceeri 2015-11-09 02:49:50 -08:00
parent 78eb4a5990
commit d6a53cf5d3
3 changed files with 20 additions and 19 deletions

View file

@ -10,6 +10,7 @@ use WindowAttributes;
use CursorState; use CursorState;
use Event; use Event;
use super::event; use super::event;
use super::WindowState;
use user32; use user32;
use shell32; use shell32;
@ -20,13 +21,6 @@ use winapi;
/// a thread-local variable. /// a thread-local variable.
thread_local!(pub static CONTEXT_STASH: RefCell<Option<ThreadLocalData>> = RefCell::new(None)); thread_local!(pub static CONTEXT_STASH: RefCell<Option<ThreadLocalData>> = 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 struct ThreadLocalData {
pub win: winapi::HWND, pub win: winapi::HWND,
pub sender: Sender<Event>, pub sender: Sender<Event>,
@ -257,7 +251,7 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
let cstash = cstash.as_ref(); let cstash = cstash.as_ref();
// there's a very bizarre borrow checker bug // there's a very bizarre borrow checker bug
// possibly related to rust-lang/rust/#23338 // 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() { if let Ok(window_state) = cstash.window_state.lock() {
match window_state.cursor_state { match window_state.cursor_state {
CursorState::Normal => { CursorState::Normal => {

View file

@ -5,7 +5,7 @@ use std::mem;
use std::thread; use std::thread;
use super::callback; use super::callback;
use super::callback::WindowState; use super::WindowState;
use super::Window; use super::Window;
use super::MonitorId; use super::MonitorId;
use super::WindowWrapper; use super::WindowWrapper;
@ -215,8 +215,11 @@ unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormat
user32::SetForegroundWindow(real_window.0); user32::SetForegroundWindow(real_window.0);
} }
// Creating a mutex to track the current cursor state // Creating a mutex to track the current window state
let cursor_state = CursorState::Normal; 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 // filling the CONTEXT_STASH task-local storage so that we can start receiving events
let events_receiver = { let events_receiver = {
@ -226,10 +229,7 @@ unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormat
let data = callback::ThreadLocalData { let data = callback::ThreadLocalData {
win: real_window.0, win: real_window.0,
sender: tx.take().unwrap(), sender: tx.take().unwrap(),
window_state: Arc::new(Mutex::new(WindowState { window_state: window_state.clone()
cursor_state: cursor_state.clone(),
attributes: window.clone()
}))
}; };
(*context_stash.borrow_mut()) = Some(data); (*context_stash.borrow_mut()) = Some(data);
}); });
@ -241,7 +241,7 @@ unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormat
window: real_window, window: real_window,
context: context, context: context,
events_receiver: events_receiver, events_receiver: events_receiver,
cursor_state: cursor_state, window_state: window_state,
}) })
} }

View file

@ -42,6 +42,13 @@ lazy_static! {
static ref WAKEUP_MSG_ID: u32 = unsafe { user32::RegisterWindowMessageA("Glutin::EventID".as_ptr() as *const i8) }; 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. /// The Win32 implementation of the main `Window` object.
pub struct Window { pub struct Window {
/// Main handle for the window. /// Main handle for the window.
@ -53,8 +60,8 @@ pub struct Window {
/// Receiver for the events dispatched by the window callback. /// Receiver for the events dispatched by the window callback.
events_receiver: Receiver<Event>, events_receiver: Receiver<Event>,
/// The current cursor state. /// The current window state.
cursor_state: CursorState, window_state: Arc<Mutex<WindowState>>,
} }
unsafe impl Send for Window {} unsafe impl Send for Window {}
@ -258,7 +265,7 @@ impl Window {
} }
pub fn set_cursor_state(&self, state: CursorState) -> Result<(), String> { 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 foreground_thread_id = unsafe { user32::GetWindowThreadProcessId(self.window.0, ptr::null_mut()) };
let current_thread_id = unsafe { kernel32::GetCurrentThreadId() }; let current_thread_id = unsafe { kernel32::GetCurrentThreadId() };