From f082c68b2ff564a7c63edba3551fadfe3b02970a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 1 Jun 2023 10:48:15 -0700
Subject: [PATCH] Updated with upstream suggestions in
https://github.com/libusb/hidapi/pull/582
---
src/hidapi/libusb/hid.c | 9 ++------
src/hidapi/libusb/hidapi_thread_pthread.h | 25 ++++++++++++++++-------
src/hidapi/libusb/hidapi_thread_sdl.h | 20 +++++++++---------
3 files changed, 30 insertions(+), 24 deletions(-)
diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c
index df33dcce9715..ad7eea6aa238 100644
--- a/src/hidapi/libusb/hid.c
+++ b/src/hidapi/libusb/hid.c
@@ -1672,14 +1672,9 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
else if (milliseconds > 0) {
/* Non-blocking, but called with timeout. */
int res;
- struct timespec ts;
+ hidapi_timespec ts;
hidapi_thread_gettime(&ts);
- ts.tv_sec += milliseconds / 1000;
- ts.tv_nsec += (milliseconds % 1000) * 1000000;
- if (ts.tv_nsec >= 1000000000L) {
- ts.tv_sec++;
- ts.tv_nsec -= 1000000000L;
- }
+ hidapi_thread_addtime(&ts, milliseconds);
while (!dev->input_reports && !dev->shutdown_thread) {
res = hidapi_thread_cond_timedwait(&dev->thread_state, &ts);
diff --git a/src/hidapi/libusb/hidapi_thread_pthread.h b/src/hidapi/libusb/hidapi_thread_pthread.h
index 720ee676d2a8..0abe733e5959 100644
--- a/src/hidapi/libusb/hidapi_thread_pthread.h
+++ b/src/hidapi/libusb/hidapi_thread_pthread.h
@@ -7,7 +7,9 @@
libusb/hidapi Team
- Copyright 2022, All Rights Reserved.
+ Sam Lantinga
+
+ Copyright 2023, All Rights Reserved.
At the discretion of the user of this library,
this software may be licensed under the terms of the
@@ -66,15 +68,13 @@ static int pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_mutex_lock(&barrier->mutex);
++(barrier->count);
- if(barrier->count >= barrier->trip_count)
- {
+ if(barrier->count >= barrier->trip_count) {
barrier->count = 0;
pthread_cond_broadcast(&barrier->cond);
pthread_mutex_unlock(&barrier->mutex);
return 1;
}
- else
- {
+ else {
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
pthread_mutex_unlock(&barrier->mutex);
return 0;
@@ -85,6 +85,8 @@ static int pthread_barrier_wait(pthread_barrier_t *barrier)
#define HIDAPI_THREAD_TIMED_OUT ETIMEDOUT
+typedef struct timespec hidapi_timespec;
+
typedef struct
{
pthread_t thread;
@@ -126,7 +128,7 @@ static void hidapi_thread_cond_wait(hidapi_thread_state *state)
pthread_cond_wait(&state->condition, &state->mutex);
}
-static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, struct timespec *ts)
+static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
{
return pthread_cond_timedwait(&state->condition, &state->mutex, ts);
}
@@ -156,8 +158,17 @@ static void hidapi_thread_join(hidapi_thread_state *state)
pthread_join(state->thread, NULL);
}
-static void hidapi_thread_gettime(struct timespec *ts)
+static void hidapi_thread_gettime(hidapi_timespec *ts)
{
clock_gettime(CLOCK_REALTIME, ts);
}
+static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
+{
+ ts->tv_sec += milliseconds / 1000;
+ ts->tv_nsec += (milliseconds % 1000) * 1000000;
+ if (ts->tv_nsec >= 1000000000L) {
+ ts->tv_sec++;
+ ts->tv_nsec -= 1000000000L;
+ }
+}
diff --git a/src/hidapi/libusb/hidapi_thread_sdl.h b/src/hidapi/libusb/hidapi_thread_sdl.h
index 4c9d04e802e1..f96eacf8c928 100644
--- a/src/hidapi/libusb/hidapi_thread_sdl.h
+++ b/src/hidapi/libusb/hidapi_thread_sdl.h
@@ -76,6 +76,8 @@ static int SDL_WaitThreadBarrier(SDL_ThreadBarrier *barrier)
#define HIDAPI_THREAD_TIMED_OUT SDL_MUTEX_TIMEDOUT
+typedef Uint64 hidapi_timespec;
+
typedef struct
{
SDL_Thread *thread;
@@ -123,16 +125,12 @@ static void hidapi_thread_cond_wait(hidapi_thread_state *state)
SDL_WaitCondition(state->condition, state->mutex);
}
-static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, struct timespec *ts)
+static int hidapi_thread_cond_timedwait(hidapi_thread_state *state, hidapi_timespec *ts)
{
- Uint64 end_time;
Sint64 timeout_ns;
Sint32 timeout_ms;
- end_time = ts->tv_sec;
- end_time *= 1000000000L;
- end_time += ts->tv_nsec;
- timeout_ns = (Sint64)(end_time - SDL_GetTicksNS());
+ timeout_ns = (Sint64)(*ts - SDL_GetTicksNS());
if (timeout_ns <= 0) {
timeout_ms = 0;
} else {
@@ -189,10 +187,12 @@ static void hidapi_thread_join(hidapi_thread_state *state)
SDL_WaitThread(state->thread, NULL);
}
-static void hidapi_thread_gettime(struct timespec *ts)
+static void hidapi_thread_gettime(hidapi_timespec *ts)
{
- Uint64 ns = SDL_GetTicksNS();
+ *ts = SDL_GetTicksNS();
+}
- ts->tv_sec = ns / 1000000000L;
- ts->tv_nsec = ns % 1000000000L;
+static void hidapi_thread_addtime(hidapi_timespec *ts, int milliseconds)
+{
+ *ts += SDL_MS_TO_NS(milliseconds);
}