Skip to content

Commit

Permalink
Fully invert windows control flow so win32 calls into winit's callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Osspial committed Nov 9, 2018
1 parent 2c607ff commit a0fef1a
Show file tree
Hide file tree
Showing 6 changed files with 510 additions and 204 deletions.
19 changes: 8 additions & 11 deletions examples/timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate winit;
use std::time::{Duration, Instant};
use winit::{Event, WindowEvent, StartCause, ControlFlow};

fn main() {
let events_loop = winit::EventLoop::new();
Expand All @@ -13,20 +14,16 @@ fn main() {
println!("{:?}", event);

match event {
winit::Event::NewEvents(winit::StartCause::Init) =>
*control_flow = winit::ControlFlow::WaitTimeout(Duration::new(1, 0)),
winit::Event::NewEvents(winit::StartCause::TimeoutExpired{..}) => {
*control_flow = winit::ControlFlow::WaitTimeout(Duration::new(1, 0));
Event::NewEvents(StartCause::Init) =>
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0)),
Event::NewEvents(StartCause::ResumeTimeReached{..}) => {
*control_flow = ControlFlow::WaitUntil(Instant::now() + Duration::new(1, 0));
println!("\nTimer\n");
},
winit::Event::NewEvents(winit::StartCause::WaitCancelled{start, requested_duration}) => {
println!("{:?}", Instant::now() - start);
*control_flow = winit::ControlFlow::WaitTimeout(requested_duration.unwrap().checked_sub(Instant::now() - start).unwrap_or(Duration::new(0, 0)));
}
winit::Event::WindowEvent {
event: winit::WindowEvent::CloseRequested,
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = winit::ControlFlow::Exit,
} => *control_flow = ControlFlow::Exit,
_ => ()
}
});
Expand Down
16 changes: 8 additions & 8 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::{Duration, Instant};
use std::time::Instant;
use std::path::PathBuf;

use {DeviceId, LogicalPosition, LogicalSize, WindowId};
Expand Down Expand Up @@ -48,19 +48,19 @@ impl<T> Event<T> {

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StartCause {
/// Sent if the time specified by `ControlFlow::WaitTimeout` has been elapsed. Contains the
/// moment the timeout was requested and the requested duration of the timeout. The actual
/// duration is guaranteed to be greater than or equal to the requested timeout.
TimeoutExpired {
/// Sent if the time specified by `ControlFlow::WaitUntil` has been reached. Contains the
/// moment the timeout was requested and the requested resume time. The actual resume time is
/// guaranteed to be equal to or after the requested resume time.
ResumeTimeReached {
start: Instant,
requested_duration: Duration,
requested_resume: Instant
},

/// Sent if the OS has new events to send to the window, after a wait was requested. Contains
/// the moment the wait was requested, and if a wait timout was requested, its duration.
/// the moment the wait was requested and the resume time, if requested.
WaitCancelled {
start: Instant,
requested_duration: Option<Duration>
requested_resume: Option<Instant>
},

/// Sent if the event loop is being resumed after the loop's control flow was set to
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extern crate percent_encoding;
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
extern crate smithay_client_toolkit as sctk;

use std::time::Duration;
use std::time::Instant;
pub(crate) use dpi::*; // TODO: Actually change the imports throughout the codebase.
pub use events::*;
pub use window::{AvailableMonitorsIter, MonitorId};
Expand Down Expand Up @@ -186,8 +186,8 @@ pub enum ControlFlow {
/// When the current loop iteration finishes, suspend the thread until another event arrives.
Wait,
/// When the current loop iteration finishes, suspend the thread until either another event
/// arrives or the timeout expires.
WaitTimeout(Duration),
/// arrives or the given time is reached.
WaitUntil(Instant),
/// When the current loop iteration finishes, immediately begin a new iteration regardless of
/// whether or not new events are available to process.
Poll,
Expand Down
8 changes: 4 additions & 4 deletions src/platform/windows/drop_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct FileDropHandlerData {
pub interface: IDropTarget,
refcount: AtomicUsize,
window: HWND,
event_sender: Sender<Event<()>>
// event_sender: Sender<Event<()>>
}

pub struct FileDropHandler {
Expand All @@ -33,14 +33,14 @@ pub struct FileDropHandler {

#[allow(non_snake_case)]
impl FileDropHandler {
pub fn new(window: HWND, event_sender: Sender<Event<()>>) -> FileDropHandler {
pub fn new(window: HWND/*, event_sender: Sender<Event<()>>*/) -> FileDropHandler {
let data = Box::new(FileDropHandlerData {
interface: IDropTarget {
lpVtbl: &DROP_TARGET_VTBL as *const IDropTargetVtbl,
},
refcount: AtomicUsize::new(1),
window,
event_sender,
// event_sender,
});
FileDropHandler {
data: Box::into_raw(data),
Expand Down Expand Up @@ -186,7 +186,7 @@ impl FileDropHandler {

impl FileDropHandlerData {
fn send_event(&self, event: Event<()>) {
self.event_sender.send(event).ok();
// self.event_sender.send(event).ok();
}
}

Expand Down
Loading

0 comments on commit a0fef1a

Please sign in to comment.