[Patch] Event timestamp propagation for x11

Hi All,

The issue of propagating the real OS timestamps for events was already
raised on this list [1], but it seems that no progress has been made
since. Event timestamps would be really useful to handle user input with
better precision; however, in their current form they are virtually
useless since they are set to SDL_GetTicks() during
SDL_PollEvent/SDL_WaitEvent, which gives no real information about the
time the event happened.

It has been noted by Sam that the actual problem is in matching the OS
timestamp to the SDL “ticks”. I’d like to offer a solution to it for the
X11 backend. Since the X11 and the SDL timestamps are both 32-bit
numbers of milliseconds, the difference between them should be constant
at any time. So, we can compute this difference during initialization,
and then use it to translate the timestamps.

To get the current X11 server time, we can create a dummy window and
call XChangeProperty on it, which will trigger a PropertyNotify event
with a timestamp. Then subtract this timestamp from the SDL timestamp
and voila: we have the difference. And then we can repeat the
experiment, say, 100 times, and take the average.

Please see the attached patch for an implementation (it’s made against
the latest Mercurial version). I am also attaching a simple test program
which prints the difference between the event timestamps and
SDL_GetTicks() in a loop which sleeps 100ms between iterations. Here’s
its typical output with vanilla SDL:

[FRAME] TYPE DELTA(ms)
…
[ 14] MOUSEMOTION 0
[ 14] MOUSEMOTION 0
[ 14] MOUSEMOTION 0
[ 14] MOUSEMOTION 0

I.e. the timestamps are pretty useless since they match the current
time. Here is the output with the patch applied:

[FRAME] TYPE DELTA(ms)
…
[ 11] MOUSEMOTION 54
[ 11] MOUSEMOTION 46
[ 11] MOUSEMOTION 38
[ 11] MOUSEMOTION 30

Which looks much closer to the truth.

Best,
Greg

[1] http://lists.libsdl.org/pipermail/sdl-libsdl.org/2013-July/089373.html

-------------- next part --------------
A non-text attachment was scrubbed…
Name: sdl-timestamps.diff
Type: text/x-patch
Size: 18701 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20140211/199c8d03/attachment-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed…
Name: test_sdl_timestamps.c
Type: text/x-csrc
Size: 1152 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20140211/199c8d03/attachment-0001.c

1 Like

I think the info about the time when the event was really created by the OS is very useful. Any progress on this?

1 Like