Skip to content

Commit

Permalink
evdev: Use time64-friendly accessors for struct input_event
Browse files Browse the repository at this point in the history
On 32-bit platforms such as i386, if SDL is compiled with -D_TIME_BITS=64
to opt-in to ABIs that will not stop working in 2038, the fields in
this struct change their naming and interpretation.

The Linux header <linux/input.h> defines macros input_event_sec and
input_event_usec which resolve to the right struct field to look at.
The actual field names and types are an implementation detail,
historically signed 32-bit time.tv_sec and time.tv_usec on 32-bit
platforms, but becoming unsigned __sec and __usec when using 64-bit
time (which makes them able to represent times up to 2106).

Signed-off-by: Simon McVittie <smcv@collabora.com>
  • Loading branch information
smcv authored and slouken committed Jun 28, 2023
1 parent e7327b6 commit 10fc3b3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/core/linux/SDL_evdev.c
Expand Up @@ -59,6 +59,16 @@
#define REL_HWHEEL_HI_RES 0x0c
#endif

/* The field to look up in struct input_event for integer seconds */
#ifndef input_event_sec
#define input_event_sec time.tv_sec
#endif

/* The field to look up in struct input_event for fractional seconds */
#ifndef input_event_usec
#define input_event_usec time.tv_usec
#endif

typedef struct SDL_evdevlist_item
{
char *path;
Expand Down Expand Up @@ -879,9 +889,9 @@ Uint64 SDL_EVDEV_GetEventTimestamp(struct input_event *event)

/* The kernel internally has nanosecond timestamps, but converts it
to microseconds when delivering the events */
timestamp = event->time.tv_sec;
timestamp = event->input_event_sec;
timestamp *= SDL_NS_PER_SECOND;
timestamp += SDL_US_TO_NS(event->time.tv_usec);
timestamp += SDL_US_TO_NS(event->input_event_usec);

if (!timestamp_offset) {
timestamp_offset = (now - timestamp);
Expand Down

0 comments on commit 10fc3b3

Please sign in to comment.