X11: Fix events not being reported when using run_return (#1245)

* X11: Fix events not being reported using `run_return`

* Adapt examples to be more practical

* Add CHANGELOG entry
This commit is contained in:
Murarth 2019-11-10 11:24:43 -07:00 committed by Hal Gentz
parent c66784995d
commit 1ed15c7ec7
5 changed files with 88 additions and 78 deletions

View file

@ -66,6 +66,13 @@ impl<T: 'static> EventProcessor<T> {
self.with_window(window_id, |_| ()).is_some()
}
pub(super) fn poll(&self) -> bool {
let wt = get_xtarget(&self.target);
let result = unsafe { (wt.xconn.xlib.XPending)(wt.xconn.display) };
result != 0
}
pub(super) unsafe fn poll_one_event(&mut self, event_ptr: *mut ffi::XEvent) -> bool {
let wt = get_xtarget(&self.target);
// This function is used to poll and remove a single event

View file

@ -26,6 +26,7 @@ use std::{
rc::Rc,
slice,
sync::{mpsc, Arc, Mutex, Weak},
time::{Duration, Instant},
};
use libc::{self, setlocale, LC_CTYPE};
@ -38,7 +39,7 @@ use self::{
};
use crate::{
error::OsError as RootOsError,
event::{Event, WindowEvent},
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
platform_impl::{platform::sticky_exit_callback, PlatformSpecificWindowBuilderAttributes},
window::WindowAttributes,
@ -262,6 +263,8 @@ impl<T: 'static> EventLoop<T> {
);
loop {
self.drain_events();
// Empty the event buffer
{
let mut guard = self.pending_events.borrow_mut();
@ -309,69 +312,58 @@ impl<T: 'static> EventLoop<T> {
);
}
let start = Instant::now();
let (mut cause, deadline, mut timeout);
match control_flow {
ControlFlow::Exit => break,
ControlFlow::Poll => {
// non-blocking dispatch
self.inner_loop
.dispatch(Some(::std::time::Duration::from_millis(0)), &mut ())
.unwrap();
callback(
crate::event::Event::NewEvents(crate::event::StartCause::Poll),
&self.target,
&mut control_flow,
);
cause = StartCause::Poll;
deadline = None;
timeout = Some(Duration::from_millis(0));
}
ControlFlow::Wait => {
self.inner_loop.dispatch(None, &mut ()).unwrap();
callback(
crate::event::Event::NewEvents(crate::event::StartCause::WaitCancelled {
start: ::std::time::Instant::now(),
requested_resume: None,
}),
&self.target,
&mut control_flow,
);
}
ControlFlow::WaitUntil(deadline) => {
let start = ::std::time::Instant::now();
// compute the blocking duration
let duration = if deadline > start {
deadline - start
} else {
::std::time::Duration::from_millis(0)
cause = StartCause::WaitCancelled {
start,
requested_resume: None,
};
self.inner_loop.dispatch(Some(duration), &mut ()).unwrap();
let now = std::time::Instant::now();
if now < deadline {
callback(
crate::event::Event::NewEvents(
crate::event::StartCause::WaitCancelled {
start,
requested_resume: Some(deadline),
},
),
&self.target,
&mut control_flow,
);
deadline = None;
timeout = None;
}
ControlFlow::WaitUntil(wait_deadline) => {
cause = StartCause::ResumeTimeReached {
start,
requested_resume: wait_deadline,
};
timeout = if wait_deadline > start {
Some(wait_deadline - start)
} else {
callback(
crate::event::Event::NewEvents(
crate::event::StartCause::ResumeTimeReached {
start,
requested_resume: deadline,
},
),
&self.target,
&mut control_flow,
);
}
Some(Duration::from_millis(0))
};
deadline = Some(wait_deadline);
}
}
// If the user callback had any interaction with the X server,
// it may have received and buffered some user input events.
self.drain_events();
if self.events_waiting() {
timeout = Some(Duration::from_millis(0));
}
self.inner_loop.dispatch(timeout, &mut ()).unwrap();
if let Some(deadline) = deadline {
if deadline > Instant::now() {
cause = StartCause::WaitCancelled {
start,
requested_resume: Some(deadline),
};
}
}
callback(
crate::event::Event::NewEvents(cause),
&self.target,
&mut control_flow,
);
}
callback(
@ -395,6 +387,10 @@ impl<T: 'static> EventLoop<T> {
drain_events(&mut processor, &mut pending_events);
}
fn events_waiting(&self) -> bool {
!self.pending_events.borrow().is_empty() || self.event_processor.borrow().poll()
}
}
fn drain_events<T: 'static>(