mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-25 06:41:31 +11:00
cursor state on w32
This commit is contained in:
parent
a1b504d16c
commit
e51dd82478
|
@ -1,30 +1,41 @@
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::ptr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
use std::sync::{
|
||||||
|
Arc,
|
||||||
|
Mutex
|
||||||
|
};
|
||||||
|
|
||||||
|
use CursorState;
|
||||||
use Event;
|
use Event;
|
||||||
use super::event;
|
use super::event;
|
||||||
|
|
||||||
use user32;
|
use user32;
|
||||||
use winapi;
|
use winapi;
|
||||||
|
|
||||||
/// Stores the current window and its events dispatcher.
|
/// There's no parameters passed to the callback function, so it needs to get
|
||||||
///
|
/// its context (the HWND, the Sender for events, etc.) stashed in
|
||||||
/// We only have one window per thread. We still store the HWND in case where we
|
/// a thread-local variable.
|
||||||
/// receive an event for another window.
|
thread_local!(pub static CONTEXT_STASH: RefCell<Option<ThreadLocalData>> = RefCell::new(None));
|
||||||
thread_local!(pub static WINDOW: Rc<RefCell<Option<(winapi::HWND, Sender<Event>)>>> = Rc::new(RefCell::new(None)));
|
|
||||||
|
pub struct ThreadLocalData {
|
||||||
|
pub win: winapi::HWND,
|
||||||
|
pub sender: Sender<Event>,
|
||||||
|
pub cursor_state: Arc<Mutex<CursorState>>
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks that the window is the good one, and if so send the event to it.
|
/// Checks that the window is the good one, and if so send the event to it.
|
||||||
fn send_event(input_window: winapi::HWND, event: Event) {
|
fn send_event(input_window: winapi::HWND, event: Event) {
|
||||||
WINDOW.with(|window| {
|
CONTEXT_STASH.with(|context_stash| {
|
||||||
let window = window.borrow();
|
let context_stash = context_stash.borrow();
|
||||||
let stored = match *window {
|
let stored = match *context_stash {
|
||||||
None => return,
|
None => return,
|
||||||
Some(ref v) => v
|
Some(ref v) => v
|
||||||
};
|
};
|
||||||
|
|
||||||
let &(ref win, ref sender) = stored;
|
let &ThreadLocalData { ref win, ref sender, .. } = stored;
|
||||||
|
|
||||||
if win != &input_window {
|
if win != &input_window {
|
||||||
return;
|
return;
|
||||||
|
@ -45,13 +56,15 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
|
||||||
winapi::WM_DESTROY => {
|
winapi::WM_DESTROY => {
|
||||||
use events::Event::Closed;
|
use events::Event::Closed;
|
||||||
|
|
||||||
WINDOW.with(|w| {
|
CONTEXT_STASH.with(|context_stash| {
|
||||||
let w = w.borrow();
|
let context_stash = context_stash.borrow();
|
||||||
let &(ref win, _) = match *w {
|
let stored = match *context_stash {
|
||||||
None => return,
|
None => return,
|
||||||
Some(ref v) => v
|
Some(ref v) => v
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let &ThreadLocalData { ref win, .. } = stored;
|
||||||
|
|
||||||
if win == &window {
|
if win == &window {
|
||||||
user32::PostQuitMessage(0);
|
user32::PostQuitMessage(0);
|
||||||
}
|
}
|
||||||
|
@ -209,6 +222,38 @@ pub unsafe extern "system" fn callback(window: winapi::HWND, msg: winapi::UINT,
|
||||||
0
|
0
|
||||||
},
|
},
|
||||||
|
|
||||||
|
winapi::WM_SETCURSOR => {
|
||||||
|
CONTEXT_STASH.with(|context_stash| {
|
||||||
|
let cstash = context_stash.borrow();
|
||||||
|
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 {
|
||||||
|
CursorState::Normal => {
|
||||||
|
unsafe {
|
||||||
|
user32::SetCursor(user32::LoadCursorW(
|
||||||
|
ptr::null_mut(),
|
||||||
|
winapi::IDC_ARROW));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
CursorState::Grab | CursorState::Hide => {
|
||||||
|
unsafe {
|
||||||
|
user32::SetCursor(ptr::null_mut());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
};
|
||||||
|
|
||||||
|
// let &ThreadLocalData { ref cursor_state, .. } = stored;
|
||||||
|
});
|
||||||
|
0
|
||||||
|
},
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
user32::DefWindowProcW(window, msg, wparam, lparam)
|
user32::DefWindowProcW(window, msg, wparam, lparam)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
|
use std::sync::{
|
||||||
|
Arc,
|
||||||
|
Mutex
|
||||||
|
};
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os;
|
use std::os;
|
||||||
|
@ -21,7 +25,6 @@ use PixelFormat;
|
||||||
|
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::sync::mpsc::channel;
|
use std::sync::mpsc::channel;
|
||||||
use std::sync::Mutex;
|
|
||||||
|
|
||||||
use libc;
|
use libc;
|
||||||
use super::gl;
|
use super::gl;
|
||||||
|
@ -221,12 +224,20 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
|
||||||
user32::SetForegroundWindow(real_window.0);
|
user32::SetForegroundWindow(real_window.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// filling the WINDOW task-local storage so that we can start receiving events
|
// Creating a mutex to track the current cursor state
|
||||||
|
let cursor_state = Arc::new(Mutex::new(CursorState::Normal));
|
||||||
|
|
||||||
|
// filling the CONTEXT_STASH task-local storage so that we can start receiving events
|
||||||
let events_receiver = {
|
let events_receiver = {
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut tx = Some(tx);
|
let mut tx = Some(tx);
|
||||||
callback::WINDOW.with(|window| {
|
callback::CONTEXT_STASH.with(|context_stash| {
|
||||||
(*window.borrow_mut()) = Some((real_window.0, tx.take().unwrap()));
|
let data = callback::ThreadLocalData {
|
||||||
|
win: real_window.0,
|
||||||
|
sender: tx.take().unwrap(),
|
||||||
|
cursor_state: cursor_state.clone()
|
||||||
|
};
|
||||||
|
(*context_stash.borrow_mut()) = Some(data);
|
||||||
});
|
});
|
||||||
rx
|
rx
|
||||||
};
|
};
|
||||||
|
@ -252,7 +263,7 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
|
||||||
gl_library: gl_library,
|
gl_library: gl_library,
|
||||||
events_receiver: events_receiver,
|
events_receiver: events_receiver,
|
||||||
is_closed: AtomicBool::new(false),
|
is_closed: AtomicBool::new(false),
|
||||||
cursor_state: Mutex::new(CursorState::Normal),
|
cursor_state: cursor_state,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,10 @@ use std::sync::atomic::AtomicBool;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::sync::Mutex;
|
use std::sync::{
|
||||||
|
Arc,
|
||||||
|
Mutex
|
||||||
|
};
|
||||||
use std::sync::mpsc::Receiver;
|
use std::sync::mpsc::Receiver;
|
||||||
use libc;
|
use libc;
|
||||||
use {CreationError, Event, MouseCursor};
|
use {CreationError, Event, MouseCursor};
|
||||||
|
@ -47,7 +50,7 @@ pub struct Window {
|
||||||
is_closed: AtomicBool,
|
is_closed: AtomicBool,
|
||||||
|
|
||||||
/// The current cursor state.
|
/// The current cursor state.
|
||||||
cursor_state: Mutex<CursorState>,
|
cursor_state: Arc<Mutex<CursorState>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Window {}
|
unsafe impl Send for Window {}
|
||||||
|
|
Loading…
Reference in a new issue