# SDL: evdev: Use time64-friendly accessors for struct input\_event

**URL:** <https://discourse.libsdl.org/t/sdl-evdev-use-time64-friendly-accessors-for-struct-input-event/44467>\
**Category:** SDL Commits\
**Created:** [June 28, 2023, 7:36pm UTC](https://discourse.libsdl.org/t/sdl-evdev-use-time64-friendly-accessors-for-struct-input-event/44467 "2023-06-28T19:36:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [June 28, 2023, 7:36pm UTC](https://discourse.libsdl.org/t/sdl-evdev-use-time64-friendly-accessors-for-struct-input-event/44467/1 "2023-06-28T19:36:02Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/10fc3b3db715f0e2050e49f39d7d6e932813723c>
>
> 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\>

```diff
From 10fc3b3db715f0e2050e49f39d7d6e932813723c Mon Sep 17 00:00:00 2001
From: Simon McVittie <[EMAIL REDACTED]>
Date: Wed, 28 Jun 2023 17:49:10 +0100
Subject: [PATCH] evdev: Use time64-friendly accessors for struct input_event

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>
---
 src/core/linux/SDL_evdev.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/core/linux/SDL_evdev.c b/src/core/linux/SDL_evdev.c
index 5f4d14e2cbab..e1b691060a42 100644
--- a/src/core/linux/SDL_evdev.c
+++ b/src/core/linux/SDL_evdev.c
@@ -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;
@@ -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);

```
