From e7636255592500b51a6294065d12d4bb18ac83e0 Mon Sep 17 00:00:00 2001
From: Bart Ribbers <bribbers@disroot.org>
Date: Tue, 28 Dec 2021 21:10:46 +0100
Subject: [PATCH] Fix build on 32-bit platforms with time64 kernels

On 32-bit arches input_event->time is undefined when using a time64
kernel. Instead read the input_event_sec and input_event_usec values
directly and save those instead
---
 lib/core.c | 45 ++++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/lib/core.c b/lib/core.c
index d6b6200..f348d81 100644
--- a/lib/core.c
+++ b/lib/core.c
@@ -941,7 +941,8 @@ static int read_core(struct xwii_iface *dev, struct xwii_event *ev)
 	}
 
 	memset(ev, 0, sizeof(*ev));
-	memcpy(&ev->time, &input.time, sizeof(struct timeval));
+	memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+	memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 	ev->type = XWII_EVENT_KEY;
 	ev->v.key.code = key;
 	ev->v.key.state = input.value;
@@ -971,7 +972,8 @@ static int read_accel(struct xwii_iface *dev, struct xwii_event *ev)
 
 	if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(ev->v.abs, &dev->accel_cache, sizeof(dev->accel_cache));
 		ev->type = XWII_EVENT_ACCEL;
 		return 0;
@@ -1013,7 +1015,8 @@ static int read_ir(struct xwii_iface *dev, struct xwii_event *ev)
 
 	if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->ir_cache, sizeof(dev->ir_cache));
 		ev->type = XWII_EVENT_IR;
 		return 0;
@@ -1065,7 +1068,8 @@ static int read_mp(struct xwii_iface *dev, struct xwii_event *ev)
 
 	if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 
 		ev->v.abs[0].x = dev->mp_cache.x - dev->mp_normalizer.x / 100;
 		ev->v.abs[0].y = dev->mp_cache.y - dev->mp_normalizer.y / 100;
@@ -1132,7 +1136,8 @@ static int read_nunchuk(struct xwii_iface *dev, struct xwii_event *ev)
 		}
 
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		ev->type = XWII_EVENT_NUNCHUK_KEY;
 		ev->v.key.code = key;
 		ev->v.key.state = input.value;
@@ -1150,7 +1155,8 @@ static int read_nunchuk(struct xwii_iface *dev, struct xwii_event *ev)
 			dev->nunchuk_cache[1].z = input.value;
 	} else if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->nunchuk_cache,
 		       sizeof(dev->nunchuk_cache));
 		ev->type = XWII_EVENT_NUNCHUK_MOVE;
@@ -1238,7 +1244,8 @@ static int read_classic(struct xwii_iface *dev, struct xwii_event *ev)
 		}
 
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		ev->type = XWII_EVENT_CLASSIC_CONTROLLER_KEY;
 		ev->v.key.code = key;
 		ev->v.key.state = input.value;
@@ -1258,7 +1265,8 @@ static int read_classic(struct xwii_iface *dev, struct xwii_event *ev)
 			dev->classic_cache[2].x = input.value;
 	} else if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->classic_cache,
 		       sizeof(dev->classic_cache));
 		ev->type = XWII_EVENT_CLASSIC_CONTROLLER_MOVE;
@@ -1292,7 +1300,8 @@ static int read_bboard(struct xwii_iface *dev, struct xwii_event *ev)
 
 	if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->bboard_cache,
 		       sizeof(dev->bboard_cache));
 		ev->type = XWII_EVENT_BALANCE_BOARD;
@@ -1421,7 +1430,8 @@ static int read_pro(struct xwii_iface *dev, struct xwii_event *ev)
 		}
 
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		ev->type = XWII_EVENT_PRO_CONTROLLER_KEY;
 		ev->v.key.code = key;
 		ev->v.key.state = input.value;
@@ -1437,7 +1447,8 @@ static int read_pro(struct xwii_iface *dev, struct xwii_event *ev)
 			dev->pro_cache[1].y = input.value;
 	} else if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->pro_cache,
 		       sizeof(dev->pro_cache));
 		ev->type = XWII_EVENT_PRO_CONTROLLER_MOVE;
@@ -1486,7 +1497,8 @@ static int read_drums(struct xwii_iface *dev, struct xwii_event *ev)
 		}
 
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		ev->type = XWII_EVENT_DRUMS_KEY;
 		ev->v.key.code = key;
 		ev->v.key.state = input.value;
@@ -1533,7 +1545,8 @@ static int read_drums(struct xwii_iface *dev, struct xwii_event *ev)
 			dev->drums_cache[XWII_DRUMS_ABS_HI_HAT].x = input.value;
 	} else if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->drums_cache,
 		       sizeof(dev->drums_cache));
 		ev->type = XWII_EVENT_DRUMS_MOVE;
@@ -1623,7 +1636,8 @@ static int read_guitar(struct xwii_iface *dev, struct xwii_event *ev)
 		}
 
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		ev->type = XWII_EVENT_GUITAR_KEY;
 		ev->v.key.code = key;
 		ev->v.key.state = input.value;
@@ -1645,7 +1659,8 @@ static int read_guitar(struct xwii_iface *dev, struct xwii_event *ev)
 			dev->guitar_cache[2].x = input.value;
 	} else if (input.type == EV_SYN) {
 		memset(ev, 0, sizeof(*ev));
-		memcpy(&ev->time, &input.time, sizeof(struct timeval));
+		memcpy(&ev->time.tv_sec, &input.input_event_sec, sizeof(struct timeval));
+		memcpy(&ev->time.tv_usec, &input.input_event_usec, sizeof(struct timeval));
 		memcpy(&ev->v.abs, dev->guitar_cache,
 		       sizeof(dev->guitar_cache));
 		ev->type = XWII_EVENT_GUITAR_MOVE;
