Skip to content

Commit

Permalink
X11: Fix ResumeTimeReached being fired early (#1505)
Browse files Browse the repository at this point in the history
* X11: Fix `ResumeTimeReached` being fired early

* Update CHANGELOG.md

Co-authored-by: Osspial <[email protected]>
  • Loading branch information
murarth and Osspial authored Mar 12, 2020
1 parent 7e04273 commit c2aed19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- On X11, fix `ResumeTimeReached` being fired too early.

# 0.22.0 (2020-03-09)

- On Windows, fix minor timing issue in wait_until_time_or_msg
Expand Down
54 changes: 19 additions & 35 deletions src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,16 @@ impl<T: 'static> EventLoop<T> {
{
let mut control_flow = ControlFlow::default();
let mut events = Events::with_capacity(8);

callback(
crate::event::Event::NewEvents(crate::event::StartCause::Init),
&self.target,
&mut control_flow,
);
let mut cause = StartCause::Init;

loop {
sticky_exit_callback(
crate::event::Event::NewEvents(cause),
&self.target,
&mut control_flow,
&mut callback,
);

// Process all pending events
self.drain_events(&mut callback, &mut control_flow);

Expand Down Expand Up @@ -326,7 +328,7 @@ impl<T: 'static> EventLoop<T> {
}

let start = Instant::now();
let (mut cause, deadline, timeout);
let (deadline, timeout);

match control_flow {
ControlFlow::Exit => break,
Expand Down Expand Up @@ -357,38 +359,20 @@ impl<T: 'static> EventLoop<T> {
}
}

if self.event_processor.poll() {
// If the XConnection already contains buffered events, we don't
// need to wait for data on the socket.
// However, we still need to check for user events.
self.poll
.poll(&mut events, Some(Duration::from_millis(0)))
.unwrap();
events.clear();

callback(
crate::event::Event::NewEvents(cause),
&self.target,
&mut control_flow,
);
} else {
// If the XConnection already contains buffered events, we don't
// need to wait for data on the socket.
if !self.event_processor.poll() {
self.poll.poll(&mut events, timeout).unwrap();
events.clear();
}

let wait_cancelled = deadline.map_or(false, |deadline| Instant::now() < deadline);
let wait_cancelled = deadline.map_or(false, |deadline| Instant::now() < deadline);

if wait_cancelled {
cause = StartCause::WaitCancelled {
start,
requested_resume: deadline,
};
}

callback(
crate::event::Event::NewEvents(cause),
&self.target,
&mut control_flow,
);
if wait_cancelled {
cause = StartCause::WaitCancelled {
start,
requested_resume: deadline,
};
}
}

Expand Down

0 comments on commit c2aed19

Please sign in to comment.