Commit 0f2bf62935fac88eb2c6700b072fcb69844d36ee

Sam Lantinga 2021-11-09T06:20:46

Fixed building hidapi when libusb is available

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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
diff --git a/src/hidapi/SDL_hidapi.c b/src/hidapi/SDL_hidapi.c
index c4466b8..f1d607f 100644
--- a/src/hidapi/SDL_hidapi.c
+++ b/src/hidapi/SDL_hidapi.c
@@ -37,6 +37,8 @@
 
 /* Platform HIDAPI Implementation */
 
+#define hid_device                      PLATFORM_hid_device
+#define hid_device_                     PLATFORM_hid_device_
 #define hid_init                        PLATFORM_hid_init
 #define hid_exit                        PLATFORM_hid_exit
 #define hid_enumerate                   PLATFORM_hid_enumerate
@@ -109,6 +111,8 @@ static const SDL_UDEV_Symbols *udev_ctx = NULL;
 #define udev_ctx 1
 #endif
 
+#undef hid_device
+#undef hid_device_
 #undef hid_init
 #undef hid_exit
 #undef hid_enumerate
@@ -142,6 +146,8 @@ static const SDL_UDEV_Symbols *udev_ctx = NULL;
 
 /* DRIVER HIDAPI Implementation */
 
+#define hid_device                      DRIVER_hid_device
+#define hid_device_                     DRIVER_hid_device_
 #define hid_init                        DRIVER_hid_init
 #define hid_exit                        DRIVER_hid_exit
 #define hid_enumerate                   DRIVER_hid_enumerate
@@ -168,6 +174,8 @@ static const SDL_UDEV_Symbols *udev_ctx = NULL;
 #error Need a driver hid.c for this platform!
 #endif
 
+#undef hid_device
+#undef hid_device_
 #undef hid_init
 #undef hid_exit
 #undef hid_enumerate
@@ -275,6 +283,8 @@ static struct
 #define libusb_handle_events                   libusb_ctx.handle_events
 #define libusb_handle_events_completed         libusb_ctx.handle_events_completed
 
+#define hid_device                      LIBUSB_hid_device
+#define hid_device_                     LIBUSB_hid_device_
 #define hid_init                        LIBUSB_hid_init
 #define hid_exit                        LIBUSB_hid_exit
 #define hid_enumerate                   LIBUSB_hid_enumerate
@@ -323,6 +333,8 @@ SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
 #undef HIDAPI_H__
 #include "libusb/hid.c"
 
+#undef hid_device
+#undef hid_device_
 #undef hid_init
 #undef hid_exit
 #undef hid_enumerate
@@ -355,18 +367,18 @@ SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
 /* Shared HIDAPI Implementation */
 
 struct hidapi_backend {
-    int  (*hid_write)(SDL_hid_device* device, const unsigned char* data, size_t length);
-    int  (*hid_read_timeout)(SDL_hid_device* device, unsigned char* data, size_t length, int milliseconds);
-    int  (*hid_read)(SDL_hid_device* device, unsigned char* data, size_t length);
-    int  (*hid_set_nonblocking)(SDL_hid_device* device, int nonblock);
-    int  (*hid_send_feature_report)(SDL_hid_device* device, const unsigned char* data, size_t length);
-    int  (*hid_get_feature_report)(SDL_hid_device* device, unsigned char* data, size_t length);
-    void (*hid_close)(SDL_hid_device* device);
-    int  (*hid_get_manufacturer_string)(SDL_hid_device* device, wchar_t* string, size_t maxlen);
-    int  (*hid_get_product_string)(SDL_hid_device* device, wchar_t* string, size_t maxlen);
-    int  (*hid_get_serial_number_string)(SDL_hid_device* device, wchar_t* string, size_t maxlen);
-    int  (*hid_get_indexed_string)(SDL_hid_device* device, int string_index, wchar_t* string, size_t maxlen);
-    const wchar_t* (*hid_error)(SDL_hid_device* device);
+    int  (*hid_write)(void* device, const unsigned char* data, size_t length);
+    int  (*hid_read_timeout)(void* device, unsigned char* data, size_t length, int milliseconds);
+    int  (*hid_read)(void* device, unsigned char* data, size_t length);
+    int  (*hid_set_nonblocking)(void* device, int nonblock);
+    int  (*hid_send_feature_report)(void* device, const unsigned char* data, size_t length);
+    int  (*hid_get_feature_report)(void* device, unsigned char* data, size_t length);
+    void (*hid_close)(void* device);
+    int  (*hid_get_manufacturer_string)(void* device, wchar_t* string, size_t maxlen);
+    int  (*hid_get_product_string)(void* device, wchar_t* string, size_t maxlen);
+    int  (*hid_get_serial_number_string)(void* device, wchar_t* string, size_t maxlen);
+    int  (*hid_get_indexed_string)(void* device, int string_index, wchar_t* string, size_t maxlen);
+    const wchar_t* (*hid_error)(void* device);
 };
 
 #if HAVE_PLATFORM_BACKEND
@@ -420,43 +432,30 @@ static const struct hidapi_backend LIBUSB_Backend = {
 };
 #endif /* SDL_LIBUSB_DYNAMIC */
 
-typedef struct _HIDDeviceWrapper HIDDeviceWrapper;
-struct _HIDDeviceWrapper
+struct SDL_hid_device_
 {
     const void *magic;
-    SDL_hid_device *device;
+    void *device;
     const struct hidapi_backend *backend;
 };
 static char device_magic;
 
 #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
 
-static HIDDeviceWrapper *
-CreateHIDDeviceWrapper(SDL_hid_device *device, const struct hidapi_backend *backend)
+static SDL_hid_device *
+CreateHIDDeviceWrapper(void *device, const struct hidapi_backend *backend)
 {
-    HIDDeviceWrapper *wrapper = (HIDDeviceWrapper *)SDL_malloc(sizeof(*wrapper));
+    SDL_hid_device *wrapper = (SDL_hid_device *)SDL_malloc(sizeof(*wrapper));
     wrapper->magic = &device_magic;
     wrapper->device = device;
     wrapper->backend = backend;
     return wrapper;
 }
 
-static SDL_hid_device *
-WrapHIDDevice(HIDDeviceWrapper *wrapper)
-{
-    return (SDL_hid_device *)wrapper;
-}
-
 #endif /* HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || SDL_LIBUSB_DYNAMIC */
 
-static HIDDeviceWrapper *
-UnwrapHIDDevice(SDL_hid_device *device)
-{
-    return (HIDDeviceWrapper *)device;
-}
-
 static void
-DeleteHIDDeviceWrapper(HIDDeviceWrapper *device)
+DeleteHIDDeviceWrapper(SDL_hid_device *device)
 {
     device->magic = NULL;
     SDL_free(device);
@@ -777,7 +776,7 @@ void SDL_hid_free_enumeration(struct SDL_hid_device_info *devs)
 SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
 {
 #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
-    SDL_hid_device *pDevice = NULL;
+    void *pDevice = NULL;
 
     if (SDL_hid_init() != 0) {
         return NULL;
@@ -785,27 +784,21 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
 
 #if HAVE_PLATFORM_BACKEND
     if (udev_ctx &&
-        (pDevice = (SDL_hid_device*) PLATFORM_hid_open(vendor_id, product_id, serial_number)) != NULL) {
-
-        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
-        return WrapHIDDevice(wrapper);
+        (pDevice = PLATFORM_hid_open(vendor_id, product_id, serial_number)) != NULL) {
+        return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
     }
 #endif /* HAVE_PLATFORM_BACKEND */
 
 #if HAVE_DRIVER_BACKEND
-    if ((pDevice = (SDL_hid_device*) DRIVER_hid_open(vendor_id, product_id, serial_number)) != NULL) {
-
-        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
-        return WrapHIDDevice(wrapper);
+    if ((pDevice = DRIVER_hid_open(vendor_id, product_id, serial_number)) != NULL) {
+        return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
     }
 #endif /* HAVE_DRIVER_BACKEND */
 
 #ifdef SDL_LIBUSB_DYNAMIC
     if (libusb_ctx.libhandle &&
-        (pDevice = (SDL_hid_device*) LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
-
-        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
-        return WrapHIDDevice(wrapper);
+        (pDevice = LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
+        return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
     }
 #endif /* SDL_LIBUSB_DYNAMIC */
 
@@ -817,7 +810,7 @@ SDL_hid_device *SDL_hid_open(unsigned short vendor_id, unsigned short product_id
 SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */)
 {
 #if HAVE_PLATFORM_BACKEND || HAVE_DRIVER_BACKEND || defined(SDL_LIBUSB_DYNAMIC)
-    SDL_hid_device *pDevice = NULL;
+    void *pDevice = NULL;
 
     if (SDL_hid_init() != 0) {
         return NULL;
@@ -825,27 +818,21 @@ SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */
 
 #if HAVE_PLATFORM_BACKEND
     if (udev_ctx &&
-        (pDevice = (SDL_hid_device*) PLATFORM_hid_open_path(path, bExclusive)) != NULL) {
-
-        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
-        return WrapHIDDevice(wrapper);
+        (pDevice = PLATFORM_hid_open_path(path, bExclusive)) != NULL) {
+        return CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
     }
 #endif /* HAVE_PLATFORM_BACKEND */
 
 #if HAVE_DRIVER_BACKEND
-    if ((pDevice = (SDL_hid_device*) DRIVER_hid_open_path(path, bExclusive)) != NULL) {
-
-        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
-        return WrapHIDDevice(wrapper);
+    if ((pDevice = DRIVER_hid_open_path(path, bExclusive)) != NULL) {
+        return CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
     }
 #endif /* HAVE_DRIVER_BACKEND */
 
 #ifdef SDL_LIBUSB_DYNAMIC
     if (libusb_ctx.libhandle &&
-        (pDevice = (SDL_hid_device*) LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
-
-        HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
-        return WrapHIDDevice(wrapper);
+        (pDevice = LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
+        return CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
     }
 #endif /* SDL_LIBUSB_DYNAMIC */
 
@@ -856,150 +843,138 @@ SDL_hid_device *SDL_hid_open_path(const char *path, int bExclusive /* = false */
 
 int SDL_hid_write(SDL_hid_device *device, const unsigned char *data, size_t length)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_write(wrapper->device, data, length);
+    result = device->backend->hid_write(device->device, data, length);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_read_timeout(SDL_hid_device *device, unsigned char *data, size_t length, int milliseconds)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_read_timeout(wrapper->device, data, length, milliseconds);
+    result = device->backend->hid_read_timeout(device->device, data, length, milliseconds);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_read(SDL_hid_device *device, unsigned char *data, size_t length)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_read(wrapper->device, data, length);
+    result = device->backend->hid_read(device->device, data, length);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_set_nonblocking(SDL_hid_device *device, int nonblock)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_set_nonblocking(wrapper->device, nonblock);
+    result = device->backend->hid_set_nonblocking(device->device, nonblock);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_send_feature_report(SDL_hid_device *device, const unsigned char *data, size_t length)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_send_feature_report(wrapper->device, data, length);
+    result = device->backend->hid_send_feature_report(device->device, data, length);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_get_feature_report(SDL_hid_device *device, unsigned char *data, size_t length)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_get_feature_report(wrapper->device, data, length);
+    result = device->backend->hid_get_feature_report(device->device, data, length);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 void SDL_hid_close(SDL_hid_device *device)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
-
-    CHECK_DEVICE_MAGIC(wrapper,);
+    CHECK_DEVICE_MAGIC(device,);
 
-    wrapper->backend->hid_close(wrapper->device);
-    DeleteHIDDeviceWrapper(wrapper);
+    device->backend->hid_close(device->device);
+    DeleteHIDDeviceWrapper(device);
 }
 
 int SDL_hid_get_manufacturer_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_get_manufacturer_string(wrapper->device, string, maxlen);
+    result = device->backend->hid_get_manufacturer_string(device->device, string, maxlen);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_get_product_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_get_product_string(wrapper->device, string, maxlen);
+    result = device->backend->hid_get_product_string(device->device, string, maxlen);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_get_serial_number_string(SDL_hid_device *device, wchar_t *string, size_t maxlen)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_get_serial_number_string(wrapper->device, string, maxlen);
+    result = device->backend->hid_get_serial_number_string(device->device, string, maxlen);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
 
 int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t *string, size_t maxlen)
 {
-    HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
     int result;
 
-    CHECK_DEVICE_MAGIC(wrapper, -1);
+    CHECK_DEVICE_MAGIC(device, -1);
 
-    result = wrapper->backend->hid_get_indexed_string(wrapper->device, string_index, string, maxlen);
+    result = device->backend->hid_get_indexed_string(device->device, string_index, string, maxlen);
     if (result < 0) {
-        SDL_SetHIDAPIError(wrapper->backend->hid_error(wrapper->device));
+        SDL_SetHIDAPIError(device->backend->hid_error(device->device));
     }
     return result;
 }
@@ -1009,7 +984,7 @@ int SDL_hid_get_indexed_string(SDL_hid_device *device, int string_index, wchar_t
 void SDL_EnableGameCubeAdaptors(void)
 {
 #ifdef SDL_LIBUSB_DYNAMIC
-    libusb_context *usb_context = NULL;
+    libusb_context *context = NULL;
     libusb_device **devs = NULL;
     libusb_device_handle *handle = NULL;
     struct libusb_device_descriptor desc;
@@ -1020,8 +995,8 @@ void SDL_EnableGameCubeAdaptors(void)
         return;
     }
 
-    if (libusb_init(&usb_context) == 0) {
-        num_devs = libusb_get_device_list(usb_context, &devs);
+    if (libusb_init(&context) == 0) {
+        num_devs = libusb_get_device_list(context, &devs);
         for (i = 0; i < num_devs; ++i) {
             if (libusb_get_device_descriptor(devs[i], &desc) != 0) {
                 continue;
@@ -1055,7 +1030,7 @@ void SDL_EnableGameCubeAdaptors(void)
 
         libusb_free_device_list(devs, 1);
 
-        libusb_exit(usb_context);
+        libusb_exit(context);
     }
 #endif /* SDL_LIBUSB_DYNAMIC */
 }
diff --git a/src/hidapi/hidapi/hidapi.h b/src/hidapi/hidapi/hidapi.h
index 01d87e3..3743c4e 100644
--- a/src/hidapi/hidapi/hidapi.h
+++ b/src/hidapi/hidapi/hidapi.h
@@ -31,8 +31,6 @@
 
 #ifdef SDL_hidapi_h_
 #define SDL_HIDAPI_IMPLEMENTATION
-#define hid_device_ SDL_hid_device_
-#define hid_device SDL_hid_device
 #define hid_device_info SDL_hid_device_info
 #endif
 
@@ -57,10 +55,10 @@ extern "C" {
 namespace NAMESPACE {
 #endif
 
-#ifndef SDL_HIDAPI_IMPLEMENTATION
 		struct hid_device_;
 		typedef struct hid_device_ hid_device; /**< opaque hidapi structure */
 
+#ifndef SDL_HIDAPI_IMPLEMENTATION
 		/** hidapi info structure */
 		struct hid_device_info {
 			/** Platform-specific device path */