winit/src/api_transition.rs

135 lines
4.8 KiB
Rust
Raw Normal View History

2017-01-28 15:00:17 +01:00
//! This temporary module generates types that wrap around the old API (winit v5 and below) and
//! expose the new API (winit v6 and above).
//!
//! This is temporary so that existing backends can smoothly transition. After all implementations
//! have finished transitionning, this module should disappear.
macro_rules! gen_api_transition {
() => {
pub struct EventsLoop {
windows: ::std::sync::Mutex<Vec<::std::sync::Arc<Window>>>,
2017-01-28 15:09:01 +01:00
interrupted: ::std::sync::atomic::AtomicBool,
awakened: ::std::sync::Arc<::std::sync::atomic::AtomicBool>,
}
pub struct EventsLoopProxy {
awakened: ::std::sync::Weak<::std::sync::atomic::AtomicBool>,
2017-01-28 15:00:17 +01:00
}
impl EventsLoop {
pub fn new() -> EventsLoop {
EventsLoop {
windows: ::std::sync::Mutex::new(vec![]),
2017-01-28 15:09:01 +01:00
interrupted: ::std::sync::atomic::AtomicBool::new(false),
awakened: ::std::sync::Arc::new(::std::sync::atomic::AtomicBool::new(false)),
2017-01-28 15:00:17 +01:00
}
}
2017-01-28 15:09:01 +01:00
pub fn interrupt(&self) {
self.interrupted.store(true, ::std::sync::atomic::Ordering::Relaxed);
}
2017-01-28 15:00:17 +01:00
pub fn poll_events<F>(&self, mut callback: F)
where F: FnMut(::Event)
{
if self.awakened.load(::std::sync::atomic::Ordering::Relaxed) {
self.awakened.store(false, ::std::sync::atomic::Ordering::Relaxed);
callback(::Event::Awakened);
}
2017-01-28 15:05:36 +01:00
let mut windows = self.windows.lock().unwrap();
for window in windows.iter() {
for event in window.poll_events() {
callback(::Event::WindowEvent {
2017-02-03 09:13:11 +01:00
window_id: ::WindowId(WindowId(&**window as *const Window as usize)),
2017-01-28 15:05:36 +01:00
event: event,
})
}
}
2017-01-28 15:00:17 +01:00
}
pub fn run_forever<F>(&self, mut callback: F)
where F: FnMut(::Event)
{
2017-01-28 15:09:01 +01:00
self.interrupted.store(false, ::std::sync::atomic::Ordering::Relaxed);
self.awakened.store(false, ::std::sync::atomic::Ordering::Relaxed);
2017-01-28 15:09:01 +01:00
2017-01-28 15:05:36 +01:00
// Yeah that's a very bad implementation.
loop {
self.poll_events(|e| callback(e));
::std::thread::sleep_ms(5);
2017-01-28 15:09:01 +01:00
if self.interrupted.load(::std::sync::atomic::Ordering::Relaxed) {
break;
}
2017-01-28 15:05:36 +01:00
}
2017-01-28 15:00:17 +01:00
}
pub fn create_proxy(&self) -> EventsLoopProxy {
EventsLoopProxy {
awakened: ::std::sync::Arc::downgrade(&self.awakened),
}
}
}
impl EventsLoopProxy {
pub fn wakeup(&self) -> Result<(), ::EventsLoopClosed> {
match self.awakened.upgrade() {
None => Err(::EventsLoopClosed),
Some(awakened) => {
awakened.store(true, ::std::sync::atomic::Ordering::Relaxed);
Ok(())
},
}
}
2017-01-28 15:00:17 +01:00
}
2017-02-03 09:13:11 +01:00
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowId(usize);
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId;
2017-01-28 15:00:17 +01:00
pub struct Window2 {
2017-01-28 15:58:14 +01:00
pub window: ::std::sync::Arc<Window>,
events_loop: ::std::sync::Weak<EventsLoop>,
2017-01-28 15:00:17 +01:00
}
impl ::std::ops::Deref for Window2 {
type Target = Window;
#[inline]
fn deref(&self) -> &Window {
&*self.window
}
}
impl Window2 {
pub fn new(events_loop: ::std::sync::Arc<EventsLoop>, window: &::WindowAttributes,
pl_attribs: &PlatformSpecificWindowBuilderAttributes)
-> Result<Window2, CreationError>
{
let win = ::std::sync::Arc::new(try!(Window::new(window, pl_attribs)));
events_loop.windows.lock().unwrap().push(win.clone());
Ok(Window2 {
window: win,
events_loop: ::std::sync::Arc::downgrade(&events_loop),
2017-01-28 15:00:17 +01:00
})
}
#[inline]
2017-02-03 09:13:11 +01:00
pub fn id(&self) -> WindowId {
WindowId(&*self.window as *const Window as usize)
2017-01-28 15:00:17 +01:00
}
}
impl Drop for Window2 {
fn drop(&mut self) {
if let Some(ev) = self.events_loop.upgrade() {
let mut windows = ev.windows.lock().unwrap();
windows.retain(|w| &**w as *const Window != &*self.window as *const _);
}
}
}
2017-01-28 15:00:17 +01:00
};
}