From 8cd96665786bae5d2001b186ecfc1571f367a3fd Mon Sep 17 00:00:00 2001 From: Evan Martin Date: Wed, 5 Jun 2024 18:39:44 -0700 Subject: [PATCH] event timestamps --- cli/src/sdl.rs | 48 ++++++++++++++++++++----------- web/glue/src/host.rs | 3 +- win32/src/host.rs | 1 + win32/src/winapi/kernel32/time.rs | 4 +++ 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/cli/src/sdl.rs b/cli/src/sdl.rs index 9d448110..ba1498a2 100644 --- a/cli/src/sdl.rs +++ b/cli/src/sdl.rs @@ -13,30 +13,44 @@ fn map_button(b: sdl2::mouse::MouseButton) -> Option { } fn message_from_event(hwnd: u32, event: sdl2::event::Event) -> Option { - let detail = match event { - sdl2::event::Event::Quit { .. } => win32::MessageDetail::Quit, + let (time, detail) = match event { + sdl2::event::Event::Quit { timestamp } => (timestamp, win32::MessageDetail::Quit), sdl2::event::Event::MouseButtonDown { - mouse_btn, x, y, .. - } => win32::MessageDetail::Mouse(win32::MouseMessage { - down: true, - button: map_button(mouse_btn)?, - x: x as u32, - y: y as u32, - }), + timestamp, + mouse_btn, + x, + y, + .. + } => ( + timestamp, + win32::MessageDetail::Mouse(win32::MouseMessage { + down: true, + button: map_button(mouse_btn)?, + x: x as u32, + y: y as u32, + }), + ), sdl2::event::Event::MouseButtonUp { - mouse_btn, x, y, .. - } => win32::MessageDetail::Mouse(win32::MouseMessage { - down: false, - button: map_button(mouse_btn)?, - x: x as u32, - y: y as u32, - }), + timestamp, + mouse_btn, + x, + y, + .. + } => ( + timestamp, + win32::MessageDetail::Mouse(win32::MouseMessage { + down: false, + button: map_button(mouse_btn)?, + x: x as u32, + y: y as u32, + }), + ), _ => { // log::warn!("unhandled event: {:?}", event); return None; } }; - Some(win32::Message { hwnd, detail }) + Some(win32::Message { hwnd, detail, time }) } fn message_from_events( diff --git a/web/glue/src/host.rs b/web/glue/src/host.rs index 12d9cc57..431b8347 100644 --- a/web/glue/src/host.rs +++ b/web/glue/src/host.rs @@ -185,6 +185,7 @@ fn message_from_event(event: web_sys::Event) -> anyhow::Result { .unwrap() .as_f64() .unwrap() as u32; + let time = event.time_stamp() as u32; let detail = match event.type_().as_str() { "mousedown" => { let mut event = map_mousevent(event.unchecked_into::())?; @@ -199,7 +200,7 @@ fn message_from_event(event: web_sys::Event) -> anyhow::Result { ty => bail!("unhandled event type {ty}"), }; log::info!("msg: {:?}", detail); - Ok(win32::Message { hwnd, detail }) + Ok(win32::Message { hwnd, detail, time }) } #[wasm_bindgen(typescript_custom_section)] diff --git a/win32/src/host.rs b/win32/src/host.rs index 88cf0b90..73f21055 100644 --- a/win32/src/host.rs +++ b/win32/src/host.rs @@ -74,6 +74,7 @@ pub enum MessageDetail { pub struct Message { pub hwnd: u32, pub detail: MessageDetail, + pub time: u32, // in units of Host::time() } pub trait Host { diff --git a/win32/src/winapi/kernel32/time.rs b/win32/src/winapi/kernel32/time.rs index 214bd40e..7367eace 100644 --- a/win32/src/winapi/kernel32/time.rs +++ b/win32/src/winapi/kernel32/time.rs @@ -61,6 +61,10 @@ pub fn GetLocalTime(_machine: &mut Machine, lpSystemTime: Option<&mut SYSTEMTIME #[win32_derive::dllexport] pub async fn Sleep(machine: &mut Machine, dwMilliseconds: u32) -> u32 { + if dwMilliseconds == 0 { + return 0; + } + #[cfg(feature = "x86-emu")] { let until = machine.host.time() + dwMilliseconds;