From 1954ac407fd06f2b2b2b1877e9ec4a91e4fcf04e Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 31 Jul 2024 18:34:42 -0700 Subject: [PATCH] Don't mix keyboard and mouse raw input timestamps We want to keep mouse timestamps consistently using the same interval, and it's helpful to know when multiple keyboard events come in at the same time. --- src/video/windows/SDL_windowsevents.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c index fd41ab15bc..c5d3603972 100644 --- a/src/video/windows/SDL_windowsevents.c +++ b/src/video/windows/SDL_windowsevents.c @@ -785,25 +785,31 @@ void WIN_PollRawInput(SDL_VideoDevice *_this) now = SDL_GetTicksNS(); if (total > 0) { - Uint64 timestamp, increment; + Uint64 mouse_timestamp, mouse_increment; Uint64 delta = (now - data->last_rawinput_poll); - if (total > 1 && delta <= SDL_MS_TO_NS(100)) { + UINT total_mouse = 0; + for (i = 0, input = (RAWINPUT *)data->rawinput; i < total; ++i, input = NEXTRAWINPUTBLOCK(input)) { + if (input->header.dwType == RIM_TYPEMOUSE) { + ++total_mouse; + } + } + if (total_mouse > 1 && delta <= SDL_MS_TO_NS(100)) { /* We'll spread these events over the time since the last poll */ - timestamp = data->last_rawinput_poll; - increment = delta / total; + mouse_timestamp = data->last_rawinput_poll; + mouse_increment = delta / total_mouse; } else { /* Do we want to track the update rate per device? */ - timestamp = now; - increment = 0; + mouse_timestamp = now; + mouse_increment = 0; } for (i = 0, input = (RAWINPUT *)data->rawinput; i < total; ++i, input = NEXTRAWINPUTBLOCK(input)) { - timestamp += increment; if (input->header.dwType == RIM_TYPEMOUSE) { RAWMOUSE *rawmouse = (RAWMOUSE *)((BYTE *)input + data->rawinput_offset); - WIN_HandleRawMouseInput(timestamp, data, input->header.hDevice, rawmouse); + mouse_timestamp += mouse_increment; + WIN_HandleRawMouseInput(mouse_timestamp, data, input->header.hDevice, rawmouse); } else if (input->header.dwType == RIM_TYPEKEYBOARD) { RAWKEYBOARD *rawkeyboard = (RAWKEYBOARD *)((BYTE *)input + data->rawinput_offset); - WIN_HandleRawKeyboardInput(timestamp, data, input->header.hDevice, rawkeyboard); + WIN_HandleRawKeyboardInput(now, data, input->header.hDevice, rawkeyboard); } } }