mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-24 06:11:30 +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::atomics::AtomicBool;
|
||||||
use std::sync::Mutex;
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use super::{event, ffi};
|
use super::{event, ffi};
|
||||||
use super::{MonitorID, Window};
|
use super::{MonitorID, Window};
|
||||||
|
@ -8,7 +10,7 @@ use {Event, Hints};
|
||||||
|
|
||||||
/// Stores the list of all the windows.
|
/// Stores the list of all the windows.
|
||||||
/// Only available on callback thread.
|
/// 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,
|
pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
_hints: &Hints, monitor: Option<MonitorID>)
|
_hints: &Hints, monitor: Option<MonitorID>)
|
||||||
|
@ -17,12 +19,12 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os;
|
use std::os;
|
||||||
|
|
||||||
// initializing WINDOWS_LIST if needed
|
let title = title.to_string();
|
||||||
// this is safe because WINDOWS_LIST is task-local
|
//let hints = hints.clone();
|
||||||
if WINDOWS_LIST.get().is_none() {
|
|
||||||
WINDOWS_LIST.replace(Some(Mutex::new(Vec::new())));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
|
||||||
|
TaskBuilder::new().native().spawn(proc() {
|
||||||
// registering the window class
|
// registering the window class
|
||||||
let class_name: Vec<u16> = "Window Class".utf16_units().collect::<Vec<u16>>()
|
let class_name: Vec<u16> = "Window Class".utf16_units().collect::<Vec<u16>>()
|
||||||
.append_one(0);
|
.append_one(0);
|
||||||
|
@ -44,8 +46,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
|
|
||||||
if unsafe { ffi::RegisterClassExW(&class) } == 0 {
|
if unsafe { ffi::RegisterClassExW(&class) } == 0 {
|
||||||
use std::os;
|
use std::os;
|
||||||
return Err(format!("RegisterClassEx function failed: {}",
|
tx.send(Err(format!("RegisterClassEx function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
os::error_string(os::errno() as uint))));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// building a RECT object with coordinates
|
// 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(),
|
let result = unsafe { ffi::ChangeDisplaySettingsExW(monitor.get_system_name().as_ptr(),
|
||||||
&mut screen_settings, ptr::mut_null(), ffi::CDS_FULLSCREEN, ptr::mut_null()) };
|
&mut screen_settings, ptr::mut_null(), ffi::CDS_FULLSCREEN, ptr::mut_null()) };
|
||||||
if result != ffi::DISP_CHANGE_SUCCESSFUL {
|
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
|
// creating the window
|
||||||
let handle = unsafe {
|
let handle = unsafe {
|
||||||
let handle = ffi::CreateWindowExW(ex_style, class_name.as_ptr(),
|
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,
|
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},
|
||||||
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() {
|
if handle.is_null() {
|
||||||
use std::os;
|
use std::os;
|
||||||
return Err(format!("CreateWindowEx function failed: {}",
|
tx.send(Err(format!("CreateWindowEx function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
os::error_string(os::errno() as uint))));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle
|
handle
|
||||||
|
@ -121,9 +126,7 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
// adding it to WINDOWS_LIST
|
// adding it to WINDOWS_LIST
|
||||||
let events_receiver = {
|
let events_receiver = {
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let list = WINDOWS_LIST.get().unwrap();
|
WINDOW.replace(Some((handle, tx)));
|
||||||
let mut list = list.lock();
|
|
||||||
list.push((handle, tx));
|
|
||||||
rx
|
rx
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -131,8 +134,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
let hdc = {
|
let hdc = {
|
||||||
let hdc = unsafe { ffi::GetDC(handle) };
|
let hdc = unsafe { ffi::GetDC(handle) };
|
||||||
if hdc.is_null() {
|
if hdc.is_null() {
|
||||||
return Err(format!("GetDC function failed: {}",
|
tx.send(Err(format!("GetDC function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
os::error_string(os::errno() as uint))));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
hdc
|
hdc
|
||||||
};
|
};
|
||||||
|
@ -145,8 +149,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
if unsafe { ffi::DescribePixelFormat(hdc, 1,
|
if unsafe { ffi::DescribePixelFormat(hdc, 1,
|
||||||
mem::size_of::<ffi::PIXELFORMATDESCRIPTOR>() as ffi::UINT, &mut output) } == 0
|
mem::size_of::<ffi::PIXELFORMATDESCRIPTOR>() as ffi::UINT, &mut output) } == 0
|
||||||
{
|
{
|
||||||
return Err(format!("DescribePixelFormat function failed: {}",
|
tx.send(Err(format!("DescribePixelFormat function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
os::error_string(os::errno() as uint))));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
output
|
output
|
||||||
|
@ -155,8 +160,9 @@ pub fn new_window(dimensions: Option<(uint, uint)>, title: &str,
|
||||||
// calling SetPixelFormat
|
// calling SetPixelFormat
|
||||||
unsafe {
|
unsafe {
|
||||||
if ffi::SetPixelFormat(hdc, 1, &pixel_format) == 0 {
|
if ffi::SetPixelFormat(hdc, 1, &pixel_format) == 0 {
|
||||||
return Err(format!("SetPixelFormat function failed: {}",
|
tx.send(Err(format!("SetPixelFormat function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
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 context = {
|
||||||
let ctxt = unsafe { ffi::wglCreateContext(hdc) };
|
let ctxt = unsafe { ffi::wglCreateContext(hdc) };
|
||||||
if ctxt.is_null() {
|
if ctxt.is_null() {
|
||||||
return Err(format!("wglCreateContext function failed: {}",
|
tx.send(Err(format!("wglCreateContext function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
os::error_string(os::errno() as uint))));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
ctxt
|
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 name = "opengl32.dll".utf16_units().collect::<Vec<u16>>().append_one(0).as_ptr();
|
||||||
let lib = unsafe { ffi::LoadLibraryW(name) };
|
let lib = unsafe { ffi::LoadLibraryW(name) };
|
||||||
if lib.is_null() {
|
if lib.is_null() {
|
||||||
return Err(format!("LoadLibrary function failed: {}",
|
tx.send(Err(format!("LoadLibrary function failed: {}",
|
||||||
os::error_string(os::errno() as uint)))
|
os::error_string(os::errno() as uint))));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
lib
|
lib
|
||||||
};
|
};
|
||||||
|
|
||||||
// building the struct
|
// building the struct
|
||||||
Ok(Window{
|
tx.send(Ok(Window{
|
||||||
window: handle,
|
window: handle,
|
||||||
hdc: hdc,
|
hdc: hdc,
|
||||||
context: context,
|
context: context,
|
||||||
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),
|
||||||
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) {
|
fn send_event(window: ffi::HWND, event: Event) {
|
||||||
let locked = WINDOWS_LIST.get().unwrap();
|
let stored = match WINDOW.get() {
|
||||||
let mut locked = locked.lock();
|
None => return,
|
||||||
locked.retain(|&(ref val, ref sender)| {
|
Some(v) => v
|
||||||
if val != &window { return true }
|
};
|
||||||
|
|
||||||
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,
|
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::sync::atomics::AtomicBool;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use {Event, Hints};
|
use {Event, Hints};
|
||||||
|
|
||||||
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor};
|
||||||
pub use self::init::WINDOWS_LIST;
|
|
||||||
|
|
||||||
mod event;
|
mod event;
|
||||||
mod ffi;
|
mod ffi;
|
||||||
|
@ -18,7 +16,6 @@ pub struct Window {
|
||||||
gl_library: ffi::HMODULE,
|
gl_library: ffi::HMODULE,
|
||||||
events_receiver: Receiver<Event>,
|
events_receiver: Receiver<Event>,
|
||||||
is_closed: AtomicBool,
|
is_closed: AtomicBool,
|
||||||
nosend: NoSend,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
|
@ -106,19 +103,6 @@ impl Window {
|
||||||
|
|
||||||
// TODO: return iterator
|
// TODO: return iterator
|
||||||
pub fn poll_events(&self) -> Vec<Event> {
|
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();
|
let mut events = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
match self.events_receiver.try_recv() {
|
match self.events_receiver.try_recv() {
|
||||||
|
@ -137,12 +121,16 @@ impl Window {
|
||||||
|
|
||||||
// TODO: return iterator
|
// TODO: return iterator
|
||||||
pub fn wait_events(&self) -> Vec<Event> {
|
pub fn wait_events(&self) -> Vec<Event> {
|
||||||
loop {
|
match self.events_receiver.recv_opt() {
|
||||||
unsafe { ffi::WaitMessage() };
|
Ok(ev) => {
|
||||||
|
let mut result = self.poll_events();
|
||||||
let events = self.poll_events();
|
result.insert(0, ev);
|
||||||
if events.len() >= 1 {
|
result
|
||||||
return events
|
},
|
||||||
|
Err(_) => {
|
||||||
|
use std::sync::atomics::Relaxed;
|
||||||
|
self.is_closed.store(true, Relaxed);
|
||||||
|
vec![]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue