Skip to content

Commit

Permalink
event timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
evmar committed Jun 6, 2024
1 parent 73ca666 commit 8cd9666
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
48 changes: 31 additions & 17 deletions cli/src/sdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,44 @@ fn map_button(b: sdl2::mouse::MouseButton) -> Option<win32::MouseButton> {
}

fn message_from_event(hwnd: u32, event: sdl2::event::Event) -> Option<win32::Message> {
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(
Expand Down
3 changes: 2 additions & 1 deletion web/glue/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ fn message_from_event(event: web_sys::Event) -> anyhow::Result<win32::Message> {
.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::<web_sys::MouseEvent>())?;
Expand All @@ -199,7 +200,7 @@ fn message_from_event(event: web_sys::Event) -> anyhow::Result<win32::Message> {
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)]
Expand Down
1 change: 1 addition & 0 deletions win32/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions win32/src/winapi/kernel32/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8cd9666

Please sign in to comment.