mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 22:01:31 +11:00
parent
91b0eeae8d
commit
fd20346829
|
@ -1,6 +1,8 @@
|
|||
use std::kinds::marker::NoSend;
|
||||
extern crate native;
|
||||
|
||||
use self::native::NativeTaskBuilder;
|
||||
use std::task::TaskBuilder;
|
||||
use std::sync::atomics::AtomicBool;
|
||||
use std::sync::Mutex;
|
||||
use std::ptr;
|
||||
use super::{event, ffi};
|
||||
use super::{MonitorID, Window};
|
||||
|
@ -8,7 +10,7 @@ use {Event, Hints};
|
|||
|
||||
/// Stores the list of all the windows.
|
||||
/// Only available on callback thread.
|
||||
local_data_key!(pub WINDOWS_LIST: Mutex<Vec<(ffi::HWND, Sender<Event>)>>)
|
||||
local_data_key!(WINDOW: (ffi::HWND, Sender<Event>))
|
||||
|
||||
pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||
_hints: &Hints, monitor: Option<MonitorID>)
|
||||
|
@ -17,12 +19,12 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
use std::mem;
|
||||
use std::os;
|
||||
|
||||
// initializing WINDOWS_LIST if needed
|
||||
// this is safe because WINDOWS_LIST is task-local
|
||||
if WINDOWS_LIST.get().is_none() {
|
||||
WINDOWS_LIST.replace(Some(Mutex::new(Vec::new())));
|
||||
}
|
||||
let title = title.to_string();
|
||||
//let hints = hints.clone();
|
||||
|
||||
let (tx, rx) = channel();
|
||||
|
||||
TaskBuilder::new().native().spawn(proc() {
|
||||
// registering the window class
|
||||
let class_name: Vec<u16> = "Window Class".utf16_units().collect::<Vec<u16>>()
|
||||
.append_one(0);
|
||||
|
@ -44,8 +46,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
|
||||
if unsafe { ffi::RegisterClassExW(&class) } == 0 {
|
||||
use std::os;
|
||||
return Err(format!("RegisterClassEx function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("RegisterClassEx function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
|
||||
// building a RECT object with coordinates
|
||||
|
@ -78,7 +81,8 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
let result = unsafe { ffi::ChangeDisplaySettingsExW(monitor.get_system_name().as_ptr(),
|
||||
&mut screen_settings, ptr::mut_null(), ffi::CDS_FULLSCREEN, ptr::mut_null()) };
|
||||
if result != ffi::DISP_CHANGE_SUCCESSFUL {
|
||||
return Err(format!("ChangeDisplaySettings failed: {}", result))
|
||||
tx.send(Err(format!("ChangeDisplaySettings failed: {}", result)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,7 +100,7 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
// creating the window
|
||||
let handle = unsafe {
|
||||
let handle = ffi::CreateWindowExW(ex_style, class_name.as_ptr(),
|
||||
title.utf16_units().collect::<Vec<u16>>().append_one(0).as_ptr() as ffi::LPCWSTR,
|
||||
title.as_slice().utf16_units().collect::<Vec<u16>>().append_one(0).as_ptr() as ffi::LPCWSTR,
|
||||
style | ffi::WS_VISIBLE | ffi::WS_CLIPSIBLINGS | ffi::WS_CLIPCHILDREN,
|
||||
if monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT},
|
||||
if monitor.is_some() { 0 } else { ffi::CW_USEDEFAULT},
|
||||
|
@ -106,8 +110,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
|
||||
if handle.is_null() {
|
||||
use std::os;
|
||||
return Err(format!("CreateWindowEx function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("CreateWindowEx function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
|
||||
handle
|
||||
|
@ -121,9 +126,7 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
// adding it to WINDOWS_LIST
|
||||
let events_receiver = {
|
||||
let (tx, rx) = channel();
|
||||
let list = WINDOWS_LIST.get().unwrap();
|
||||
let mut list = list.lock();
|
||||
list.push((handle, tx));
|
||||
WINDOW.replace(Some((handle, tx)));
|
||||
rx
|
||||
};
|
||||
|
||||
|
@ -131,8 +134,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
let hdc = {
|
||||
let hdc = unsafe { ffi::GetDC(handle) };
|
||||
if hdc.is_null() {
|
||||
return Err(format!("GetDC function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("GetDC function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
hdc
|
||||
};
|
||||
|
@ -145,8 +149,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
if unsafe { ffi::DescribePixelFormat(hdc, 1,
|
||||
mem::size_of::<ffi::PIXELFORMATDESCRIPTOR>() as ffi::UINT, &mut output) } == 0
|
||||
{
|
||||
return Err(format!("DescribePixelFormat function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("DescribePixelFormat function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
|
||||
output
|
||||
|
@ -155,8 +160,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
// calling SetPixelFormat
|
||||
unsafe {
|
||||
if ffi::SetPixelFormat(hdc, 1, &pixel_format) == 0 {
|
||||
return Err(format!("SetPixelFormat function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("SetPixelFormat function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,8 +170,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
let context = {
|
||||
let ctxt = unsafe { ffi::wglCreateContext(hdc) };
|
||||
if ctxt.is_null() {
|
||||
return Err(format!("wglCreateContext function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("wglCreateContext function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
ctxt
|
||||
};
|
||||
|
@ -175,32 +182,52 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
|||
let name = "opengl32.dll".utf16_units().collect::<Vec<u16>>().append_one(0).as_ptr();
|
||||
let lib = unsafe { ffi::LoadLibraryW(name) };
|
||||
if lib.is_null() {
|
||||
return Err(format!("LoadLibrary function failed: {}",
|
||||
os::error_string(os::errno() as uint)))
|
||||
tx.send(Err(format!("LoadLibrary function failed: {}",
|
||||
os::error_string(os::errno() as uint))));
|
||||
return;
|
||||
}
|
||||
lib
|
||||
};
|
||||
|
||||
// building the struct
|
||||
Ok(Window{
|
||||
tx.send(Ok(Window{
|
||||
window: handle,
|
||||
hdc: hdc,
|
||||
context: context,
|
||||
gl_library: gl_library,
|
||||
events_receiver: events_receiver,
|
||||
is_closed: AtomicBool::new(false),
|
||||
nosend: NoSend,
|
||||
})
|
||||
}));
|
||||
|
||||
// starting the events loop
|
||||
loop {
|
||||
let mut msg = unsafe { mem::uninitialized() };
|
||||
|
||||
if unsafe { ffi::GetMessageW(&mut msg, ptr::mut_null(), 0, 0) } == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
unsafe { ffi::TranslateMessage(&msg) };
|
||||
unsafe { ffi::DispatchMessageW(&msg) };
|
||||
}
|
||||
});
|
||||
|
||||
rx.recv()
|
||||
}
|
||||
|
||||
fn send_event(window: ffi::HWND, event: Event) {
|
||||
let locked = WINDOWS_LIST.get().unwrap();
|
||||
let mut locked = locked.lock();
|
||||
locked.retain(|&(ref val, ref sender)| {
|
||||
if val != &window { return true }
|
||||
let stored = match WINDOW.get() {
|
||||
None => return,
|
||||
Some(v) => v
|
||||
};
|
||||
|
||||
sender.send_opt(event).is_ok()
|
||||
});
|
||||
let &(ref win, ref sender) = stored.deref();
|
||||
|
||||
if win != &window {
|
||||
return;
|
||||
}
|
||||
|
||||
sender.send_opt(event).ok(); // ignoring if closed
|
||||
}
|
||||
|
||||
extern "stdcall" fn callback(window: ffi::HWND, msg: ffi::UINT,
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
use std::kinds::marker::NoSend;
|
||||
use std::sync::atomics::AtomicBool;
|
||||
use std::ptr;
|
||||
use {Event, Hints};
|
||||
|
||||
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
||||
pub use self::init::WINDOWS_LIST;
|
||||
|
||||
mod event;
|
||||
mod ffi;
|
||||
|
@ -18,7 +16,6 @@ pub struct Window {
|
|||
gl_library: ffi::HMODULE,
|
||||
events_receiver: Receiver<Event>,
|
||||
is_closed: AtomicBool,
|
||||
nosend: NoSend,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
|
@ -106,19 +103,6 @@ impl Window {
|
|||
|
||||
// TODO: return iterator
|
||||
pub fn poll_events(&self) -> Vec<Event> {
|
||||
use std::mem;
|
||||
|
||||
loop {
|
||||
let mut msg = unsafe { mem::uninitialized() };
|
||||
|
||||
if unsafe { ffi::PeekMessageW(&mut msg, ptr::mut_null(), 0, 0, 0x1) } == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
unsafe { ffi::TranslateMessage(&msg) };
|
||||
unsafe { ffi::DispatchMessageW(&msg) };
|
||||
}
|
||||
|
||||
let mut events = Vec::new();
|
||||
loop {
|
||||
match self.events_receiver.try_recv() {
|
||||
|
@ -137,12 +121,16 @@ impl Window {
|
|||
|
||||
// TODO: return iterator
|
||||
pub fn wait_events(&self) -> Vec<Event> {
|
||||
loop {
|
||||
unsafe { ffi::WaitMessage() };
|
||||
|
||||
let events = self.poll_events();
|
||||
if events.len() >= 1 {
|
||||
return events
|
||||
match self.events_receiver.recv_opt() {
|
||||
Ok(ev) => {
|
||||
let mut result = self.poll_events();
|
||||
result.insert(0, ev);
|
||||
result
|
||||
},
|
||||
Err(_) => {
|
||||
use std::sync::atomics::Relaxed;
|
||||
self.is_closed.store(true, Relaxed);
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue