/
SDL_hidapi_libusb.h
238 lines (199 loc) · 6.84 KB
/
SDL_hidapi_libusb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* Define standard library functions in terms of SDL */
#define HIDAPI_USING_SDL_RUNTIME
#define free SDL_free
#define iconv_t SDL_iconv_t
#define ICONV_CONST
#define iconv(a,b,c,d,e) SDL_iconv(a, (const char **)b, c, d, e)
#define iconv_open SDL_iconv_open
#define iconv_close SDL_iconv_close
#define malloc SDL_malloc
#define realloc SDL_realloc
#define setlocale(X, Y) NULL
#define snprintf SDL_snprintf
#define strcmp SDL_strcmp
#define strdup SDL_strdup
#define strncpy SDL_strlcpy
#ifdef tolower
#undef tolower
#endif
#define tolower SDL_tolower
#define wcsdup SDL_wcsdup
#ifndef __FreeBSD__
/* this is awkwardly inlined, so we need to re-implement it here
* so we can override the libusb_control_transfer call */
static int SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
uint8_t descriptor_index, uint16_t lang_id,
unsigned char *data, int length)
{
return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | 0x0, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | descriptor_index, lang_id,
data, (uint16_t)length, 1000); /* Endpoint 0 IN */
}
#define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
#endif /* __FreeBSD__ */
#define HIDAPI_THREAD_STATE_DEFINED
/* Barrier implementation because Android/Bionic don't have pthread_barrier.
This implementation came from Brent Priddy and was posted on
StackOverflow. It is used with his permission. */
typedef struct _SDL_ThreadBarrier
{
SDL_Mutex *mutex;
SDL_Condition *cond;
Uint32 count;
Uint32 trip_count;
} SDL_ThreadBarrier;
static int SDL_CreateThreadBarrier(SDL_ThreadBarrier *barrier, Uint32 count)
{
SDL_assert(barrier != NULL);
SDL_assert(count != 0);
barrier->mutex = SDL_CreateMutex();
if (barrier->mutex == NULL) {
return -1; /* Error set by CreateMutex */
}
barrier->cond = SDL_CreateCondition();
if (barrier->cond == NULL) {
return -1; /* Error set by CreateCond */
}
barrier->trip_count = count;
barrier->count = 0;
return 0;
}
static void SDL_DestroyThreadBarrier(SDL_ThreadBarrier *barrier)
{
SDL_DestroyCondition(barrier->cond);
SDL_DestroyMutex(barrier->mutex);
}
static int SDL_WaitThreadBarrier(SDL_ThreadBarrier *barrier)
{
SDL_LockMutex(barrier->mutex);
barrier->count += 1;
if (barrier->count >= barrier->trip_count) {
barrier->count = 0;
SDL_BroadcastCondition(barrier->cond);
SDL_UnlockMutex(barrier->mutex);
return 1;
}
SDL_WaitCondition(barrier->cond, barrier->mutex);
SDL_UnlockMutex(barrier->mutex);
return 0;
}
#include "../thread/SDL_systhread.h"
#define THREAD_STATE_WAIT_TIMED_OUT SDL_MUTEX_TIMEDOUT
typedef struct
{
SDL_Thread *thread;
SDL_Mutex *mutex; /* Protects input_reports */
SDL_Condition *condition;
SDL_ThreadBarrier barrier; /* Ensures correct startup sequence */
} hid_device_thread_state;
static void thread_state_init(hid_device_thread_state *state)
{
state->mutex = SDL_CreateMutex();
state->condition = SDL_CreateCondition();
SDL_CreateThreadBarrier(&state->barrier, 2);
}
static void thread_state_free(hid_device_thread_state *state)
{
SDL_DestroyThreadBarrier(&state->barrier);
SDL_DestroyCondition(state->condition);
SDL_DestroyMutex(state->mutex);
}
static void thread_state_push_cleanup(void (*routine)(void *), void *arg)
{
/* There isn't an equivalent in SDL, and it's only useful for threads calling hid_read_timeout() */
}
static void thread_state_pop_cleanup(int execute)
{
}
static void thread_state_lock(hid_device_thread_state *state)
{
SDL_LockMutex(state->mutex);
}
static void thread_state_unlock(hid_device_thread_state *state)
{
SDL_UnlockMutex(state->mutex);
}
static void thread_state_wait_condition(hid_device_thread_state *state)
{
SDL_WaitCondition(state->condition, state->mutex);
}
static int thread_state_wait_condition_timeout(hid_device_thread_state *state, struct 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());
if (timeout_ns <= 0) {
timeout_ms = 0;
} else {
timeout_ms = (Sint32)SDL_NS_TO_MS(timeout_ns);
}
return SDL_WaitConditionTimeout(state->condition, state->mutex, timeout_ms);
}
static void thread_state_signal_condition(hid_device_thread_state *state)
{
SDL_SignalCondition(state->condition);
}
static void thread_state_broadcast_condition(hid_device_thread_state *state)
{
SDL_BroadcastCondition(state->condition);
}
static void thread_state_wait_barrier(hid_device_thread_state *state)
{
SDL_WaitThreadBarrier(&state->barrier);
}
typedef struct
{
void *(*func)(void*);
void *func_arg;
} RunInputThreadParam;
static int RunInputThread(void *param)
{
RunInputThreadParam *data = (RunInputThreadParam *)param;
void *(*func)(void*) = data->func;
void *func_arg = data->func_arg;
SDL_free(data);
func(func_arg);
return 0;
}
static void thread_state_create_thread(hid_device_thread_state *state, void *(*func)(void*), void *func_arg)
{
RunInputThreadParam *param = (RunInputThreadParam *)malloc(sizeof(*param));
/* Note that the hidapi code didn't check for thread creation failure.
* We'll crash if malloc() fails
*/
param->func = func;
param->func_arg = func_arg;
state->thread = SDL_CreateThreadInternal(RunInputThread, "libusb", 0, param);
}
static void thread_state_join_thread(hid_device_thread_state *state)
{
SDL_WaitThread(state->thread, NULL);
}
static void thread_state_get_current_time(struct timespec *ts)
{
Uint64 ns = SDL_GetTicksNS();
ts->tv_sec = ns / 1000000000L;
ts->tv_nsec = ns % 1000000000L;
}
#undef HIDAPI_H__
#include "libusb/hid.c"