WIP - Make poll_events and run_forever take &mut self
This removes the need for the EventsLoop::interrupt method by inroducing a ControlFlow type. This new type is to be returned by the user's callback and indicates whether the `EventsLoop` should continue waiting for events or break from the loop. Only the wayland, x11 and api_transition backends have been updated so far, and only the wayland backend has actually been tested.
This commit is contained in:
parent
38856b1c60
commit
f2dd2f0752
7 changed files with 103 additions and 95 deletions
|
|
@ -3,7 +3,7 @@
|
|||
use std::collections::VecDeque;
|
||||
use std::sync::Arc;
|
||||
|
||||
use {CreationError, CursorState, EventsLoopClosed, MouseCursor};
|
||||
use {CreationError, CursorState, EventsLoopClosed, MouseCursor, ControlFlow};
|
||||
use libc;
|
||||
|
||||
use self::x11::XConnection;
|
||||
|
|
@ -129,9 +129,10 @@ impl MonitorId {
|
|||
|
||||
impl Window2 {
|
||||
#[inline]
|
||||
pub fn new(events_loop: ::std::sync::Arc<EventsLoop>, window: &::WindowAttributes,
|
||||
pub fn new(events_loop: &EventsLoop,
|
||||
window: &::WindowAttributes,
|
||||
pl_attribs: &PlatformSpecificWindowBuilderAttributes)
|
||||
-> Result<Window2, CreationError>
|
||||
-> Result<Self, CreationError>
|
||||
{
|
||||
match *UNIX_BACKEND {
|
||||
UnixBackend::Wayland(ref ctxt) => {
|
||||
|
|
@ -336,28 +337,21 @@ impl EventsLoop {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn interrupt(&self) {
|
||||
match *self {
|
||||
EventsLoop::Wayland(ref evlp) => evlp.interrupt(),
|
||||
EventsLoop::X(ref evlp) => evlp.interrupt()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_events<F>(&self, callback: F)
|
||||
pub fn poll_events<F>(&mut self, callback: F)
|
||||
where F: FnMut(::Event)
|
||||
{
|
||||
match *self {
|
||||
EventsLoop::Wayland(ref evlp) => evlp.poll_events(callback),
|
||||
EventsLoop::X(ref evlp) => evlp.poll_events(callback)
|
||||
EventsLoop::Wayland(ref mut evlp) => evlp.poll_events(callback),
|
||||
EventsLoop::X(ref mut evlp) => evlp.poll_events(callback)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_forever<F>(&self, callback: F)
|
||||
where F: FnMut(::Event)
|
||||
pub fn run_forever<F>(&mut self, callback: F)
|
||||
where F: FnMut(::Event) -> ControlFlow
|
||||
{
|
||||
match *self {
|
||||
EventsLoop::Wayland(ref evlp) => evlp.run_forever(callback),
|
||||
EventsLoop::X(ref evlp) => evlp.run_forever(callback)
|
||||
EventsLoop::Wayland(ref mut evlp) => evlp.run_forever(callback),
|
||||
EventsLoop::X(ref mut evlp) => evlp.run_forever(callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use {WindowEvent as Event, ElementState, MouseButton, MouseScrollDelta, TouchPhase, ModifiersState, KeyboardInput, EventsLoopClosed};
|
||||
use {WindowEvent as Event, ElementState, MouseButton, MouseScrollDelta, TouchPhase, ModifiersState,
|
||||
KeyboardInput, EventsLoopClosed, ControlFlow};
|
||||
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
use std::sync::atomic::{self, AtomicBool};
|
||||
|
|
@ -53,7 +54,9 @@ impl EventsLoopSink {
|
|||
::std::mem::replace(&mut self.callback, cb)
|
||||
}
|
||||
|
||||
fn with_callback<F: FnOnce(&mut FnMut(::Event))>(&mut self, f: F) {
|
||||
fn with_callback<F>(&mut self, f: F)
|
||||
where F: FnOnce(&mut FnMut(::Event)),
|
||||
{
|
||||
f(&mut *self.callback)
|
||||
}
|
||||
}
|
||||
|
|
@ -67,8 +70,6 @@ pub struct EventsLoop {
|
|||
decorated_ids: Mutex<Vec<(usize, Arc<wl_surface::WlSurface>)>>,
|
||||
// our sink, receiver of callbacks, shared with some handlers
|
||||
sink: Arc<Mutex<EventsLoopSink>>,
|
||||
// trigger interruption of the run
|
||||
interrupted: AtomicBool,
|
||||
// trigger cleanup of the dead surfaces
|
||||
cleanup_needed: Arc<AtomicBool>,
|
||||
// Whether or not there is a pending `Awakened` event to be emitted.
|
||||
|
|
@ -115,7 +116,6 @@ impl EventsLoop {
|
|||
evq: Arc::new(Mutex::new(evq)),
|
||||
decorated_ids: Mutex::new(Vec::new()),
|
||||
sink: sink,
|
||||
interrupted: AtomicBool::new(false),
|
||||
pending_wakeup: Arc::new(AtomicBool::new(false)),
|
||||
cleanup_needed: Arc::new(AtomicBool::new(false)),
|
||||
hid: hid
|
||||
|
|
@ -158,10 +158,6 @@ impl EventsLoop {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn interrupt(&self) {
|
||||
self.interrupted.store(true, atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn prune_dead_windows(&self) {
|
||||
self.decorated_ids.lock().unwrap().retain(|&(_, ref w)| w.is_alive());
|
||||
let mut evq_guard = self.evq.lock().unwrap();
|
||||
|
|
@ -175,7 +171,7 @@ impl EventsLoop {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn poll_events<F>(&self, callback: F)
|
||||
pub fn poll_events<F>(&mut self, callback: F)
|
||||
where F: FnMut(::Event)
|
||||
{
|
||||
// send pending requests to the server...
|
||||
|
|
@ -219,38 +215,45 @@ impl EventsLoop {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run_forever<F>(&self, callback: F)
|
||||
where F: FnMut(::Event)
|
||||
pub fn run_forever<F>(&mut self, mut callback: F)
|
||||
where F: FnMut(::Event) -> ControlFlow,
|
||||
{
|
||||
self.interrupted.store(false, atomic::Ordering::Relaxed);
|
||||
|
||||
// send pending requests to the server...
|
||||
self.ctxt.flush();
|
||||
|
||||
// first of all, get exclusive access to this event queue
|
||||
let mut evq_guard = self.evq.lock().unwrap();
|
||||
|
||||
// Check for control flow by wrapping the callback.
|
||||
let control_flow = ::std::cell::Cell::new(ControlFlow::Continue);
|
||||
let callback = |event| if let ControlFlow::Complete = callback(event) {
|
||||
control_flow.set(ControlFlow::Complete);
|
||||
};
|
||||
|
||||
// set the callback into the sink
|
||||
// we extend the lifetime of the closure to 'static to be able to put it in
|
||||
// the sink, but we'll explicitly drop it at the end of this function, so it's fine
|
||||
let static_cb = unsafe { ::std::mem::transmute(Box::new(callback) as Box<FnMut(_)>) };
|
||||
let old_cb = unsafe { self.sink.lock().unwrap().set_callback(static_cb) };
|
||||
|
||||
while !self.interrupted.load(atomic::Ordering::Relaxed) {
|
||||
loop {
|
||||
self.ctxt.dispatch();
|
||||
evq_guard.dispatch_pending().expect("Wayland connection unexpectedly lost");
|
||||
|
||||
self.emit_pending_wakeup();
|
||||
|
||||
let ids_guard = self.decorated_ids.lock().unwrap();
|
||||
self.sink.lock().unwrap().with_callback(
|
||||
|cb| Self::process_resize(&mut evq_guard, &ids_guard, cb)
|
||||
);
|
||||
self.sink.lock().unwrap()
|
||||
.with_callback(|cb| Self::process_resize(&mut evq_guard, &ids_guard, cb));
|
||||
self.ctxt.flush();
|
||||
|
||||
if self.cleanup_needed.swap(false, atomic::Ordering::Relaxed) {
|
||||
self.prune_dead_windows()
|
||||
}
|
||||
|
||||
if let ControlFlow::Complete = control_flow.get() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// replace the old noop callback
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ pub use self::xdisplay::{XConnection, XNotSupported, XError};
|
|||
pub mod ffi;
|
||||
|
||||
use platform::PlatformSpecificWindowBuilderAttributes;
|
||||
use {CreationError, Event, EventsLoopClosed, WindowEvent, DeviceEvent, AxisId, ButtonId, KeyboardInput};
|
||||
use {CreationError, Event, EventsLoopClosed, WindowEvent, DeviceEvent, AxisId, ButtonId,
|
||||
KeyboardInput, ControlFlow};
|
||||
|
||||
use std::{mem, ptr, slice};
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
|
|
@ -30,10 +31,9 @@ mod xdisplay;
|
|||
// the one generated by the macro.
|
||||
|
||||
pub struct EventsLoop {
|
||||
interrupted: AtomicBool,
|
||||
display: Arc<XConnection>,
|
||||
wm_delete_window: ffi::Atom,
|
||||
windows: Mutex<HashMap<WindowId, WindowData>>,
|
||||
windows: Arc<Mutex<HashMap<WindowId, WindowData>>>,
|
||||
devices: Mutex<HashMap<DeviceId, Device>>,
|
||||
xi2ext: XExtension,
|
||||
pending_wakeup: Arc<AtomicBool>,
|
||||
|
|
@ -81,11 +81,10 @@ impl EventsLoop {
|
|||
let root = unsafe { (display.xlib.XDefaultRootWindow)(display.display) };
|
||||
|
||||
let result = EventsLoop {
|
||||
interrupted: AtomicBool::new(false),
|
||||
pending_wakeup: Arc::new(AtomicBool::new(false)),
|
||||
display: display,
|
||||
wm_delete_window: wm_delete_window,
|
||||
windows: Mutex::new(HashMap::new()),
|
||||
windows: Arc::new(Mutex::new(HashMap::new())),
|
||||
devices: Mutex::new(HashMap::new()),
|
||||
xi2ext: xi2ext,
|
||||
root: root,
|
||||
|
|
@ -118,14 +117,9 @@ impl EventsLoop {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn interrupt(&self) {
|
||||
self.interrupted.store(true, atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn poll_events<F>(&self, mut callback: F)
|
||||
where F: FnMut(Event)
|
||||
{
|
||||
self.interrupted.store(false, atomic::Ordering::Relaxed);
|
||||
let xlib = &self.display.xlib;
|
||||
|
||||
let mut xev = unsafe { mem::uninitialized() };
|
||||
|
|
@ -141,16 +135,12 @@ impl EventsLoop {
|
|||
(xlib.XNextEvent)(self.display.display, &mut xev);
|
||||
}
|
||||
self.process_event(&mut xev, &mut callback);
|
||||
if self.interrupted.load(atomic::Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_forever<F>(&self, mut callback: F)
|
||||
where F: FnMut(Event)
|
||||
pub fn run_forever<F>(&mut self, mut callback: F)
|
||||
where F: FnMut(Event) -> ControlFlow
|
||||
{
|
||||
self.interrupted.store(false, atomic::Ordering::Relaxed);
|
||||
self.pending_wakeup.store(false, atomic::Ordering::Relaxed);
|
||||
|
||||
let xlib = &self.display.xlib;
|
||||
|
|
@ -160,13 +150,25 @@ impl EventsLoop {
|
|||
loop {
|
||||
unsafe { (xlib.XNextEvent)(self.display.display, &mut xev) }; // Blocks as necessary
|
||||
|
||||
let mut control_flow = ControlFlow::Continue;
|
||||
|
||||
if self.pending_wakeup.load(atomic::Ordering::Relaxed) {
|
||||
self.pending_wakeup.store(false, atomic::Ordering::Relaxed);
|
||||
callback(Event::Awakened);
|
||||
control_flow = callback(Event::Awakened);
|
||||
}
|
||||
|
||||
self.process_event(&mut xev, &mut callback);
|
||||
if self.interrupted.load(atomic::Ordering::Relaxed) {
|
||||
// Track whether or not `Complete` was returned when processing the event.
|
||||
{
|
||||
let mut cb = |event| {
|
||||
if let ControlFlow::Complete = callback(event) {
|
||||
control_flow = ControlFlow::Complete;
|
||||
}
|
||||
};
|
||||
|
||||
self.process_event(&mut xev, &mut cb);
|
||||
}
|
||||
|
||||
if let ControlFlow::Complete = control_flow {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -178,7 +180,7 @@ impl EventsLoop {
|
|||
device.name.clone()
|
||||
}
|
||||
|
||||
fn process_event<F>(&self, xev: &mut ffi::XEvent, callback: &mut F)
|
||||
fn process_event<F>(&self, xev: &mut ffi::XEvent, mut callback: F)
|
||||
where F: FnMut(Event)
|
||||
{
|
||||
let xlib = &self.display.xlib;
|
||||
|
|
@ -312,7 +314,11 @@ impl EventsLoop {
|
|||
};
|
||||
|
||||
for chr in written.chars() {
|
||||
callback(Event::WindowEvent { window_id: wid, event: WindowEvent::ReceivedCharacter(chr) })
|
||||
let event = Event::WindowEvent {
|
||||
window_id: wid,
|
||||
event: WindowEvent::ReceivedCharacter(chr),
|
||||
};
|
||||
callback(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -603,7 +609,8 @@ pub struct DeviceId(c_int);
|
|||
|
||||
pub struct Window2 {
|
||||
pub window: Arc<Window>,
|
||||
events_loop: Weak<::platform::EventsLoop>,
|
||||
display: Weak<XConnection>,
|
||||
windows: Weak<Mutex<HashMap<WindowId, WindowData>>>,
|
||||
}
|
||||
|
||||
impl ::std::ops::Deref for Window2 {
|
||||
|
|
@ -620,9 +627,10 @@ lazy_static! { // TODO: use a static mutex when that's possible, and put me
|
|||
}
|
||||
|
||||
impl Window2 {
|
||||
pub fn new(events_loop: Arc<::platform::EventsLoop>,
|
||||
window: &::WindowAttributes, pl_attribs: &PlatformSpecificWindowBuilderAttributes)
|
||||
-> Result<Window2, CreationError>
|
||||
pub fn new(events_loop: &::platform::EventsLoop,
|
||||
window: &::WindowAttributes,
|
||||
pl_attribs: &PlatformSpecificWindowBuilderAttributes)
|
||||
-> Result<Self, CreationError>
|
||||
{
|
||||
let x_events_loop = if let ::platform::EventsLoop::X(ref e) = *events_loop { e } else { unreachable!() };
|
||||
let win = ::std::sync::Arc::new(try!(Window::new(&x_events_loop, window, pl_attribs)));
|
||||
|
|
@ -662,7 +670,8 @@ impl Window2 {
|
|||
|
||||
Ok(Window2 {
|
||||
window: win,
|
||||
events_loop: Arc::downgrade(&events_loop),
|
||||
windows: Arc::downgrade(&x_events_loop.windows),
|
||||
display: Arc::downgrade(&x_events_loop.display),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -674,17 +683,13 @@ impl Window2 {
|
|||
|
||||
impl Drop for Window2 {
|
||||
fn drop(&mut self) {
|
||||
if let Some(ev) = self.events_loop.upgrade() {
|
||||
if let ::platform::EventsLoop::X(ref ev) = *ev {
|
||||
let mut windows = ev.windows.lock().unwrap();
|
||||
|
||||
|
||||
let w = windows.remove(&self.window.id()).unwrap();
|
||||
let _lock = GLOBAL_XOPENIM_LOCK.lock().unwrap();
|
||||
unsafe {
|
||||
(ev.display.xlib.XDestroyIC)(w.ic);
|
||||
(ev.display.xlib.XCloseIM)(w.im);
|
||||
}
|
||||
if let (Some(windows), Some(display)) = (self.windows.upgrade(), self.display.upgrade()) {
|
||||
let mut windows = windows.lock().unwrap();
|
||||
let w = windows.remove(&self.window.id()).unwrap();
|
||||
let _lock = GLOBAL_XOPENIM_LOCK.lock().unwrap();
|
||||
unsafe {
|
||||
(display.xlib.XDestroyIC)(w.ic);
|
||||
(display.xlib.XCloseIM)(w.im);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue