/
SDL_hidapijoystick.c
1121 lines (984 loc) · 41.8 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.
*/
#include "../../SDL_internal.h"
#ifdef SDL_JOYSTICK_HIDAPI
#include "SDL_endian.h"
#include "SDL_hints.h"
#include "SDL_log.h"
28
#include "SDL_mutex.h"
29
#include "SDL_thread.h"
30
31
32
33
#include "SDL_timer.h"
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
34
#include "../../SDL_hints_c.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#if defined(__WIN32__)
#include "../../core/windows/SDL_windows.h"
#endif
#if defined(__MACOSX__)
#include <CoreFoundation/CoreFoundation.h>
#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/USBSpec.h>
#endif
#if defined(__LINUX__)
#include "../../core/linux/SDL_udev.h"
#ifdef SDL_USE_LIBUDEV
#include <poll.h>
#endif
#endif
53
54
struct joystick_hwdata
55
{
56
57
58
SDL_HIDAPI_DeviceDriver *driver;
void *context;
59
SDL_mutex *mutex;
60
61
62
63
64
65
hid_device *dev;
};
typedef struct _SDL_HIDAPI_Device
{
SDL_JoystickID instance_id;
66
67
68
69
char *name;
char *path;
Uint16 vendor_id;
Uint16 product_id;
70
Uint16 version;
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
SDL_JoystickGUID guid;
int interface_number; /* Available on Windows and Linux */
Uint16 usage_page; /* Available on Windows and Mac OS X */
Uint16 usage; /* Available on Windows and Mac OS X */
SDL_HIDAPI_DeviceDriver *driver;
/* Used during scanning for device changes */
SDL_bool seen;
struct _SDL_HIDAPI_Device *next;
} SDL_HIDAPI_Device;
static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
#ifdef SDL_JOYSTICK_HIDAPI_PS4
&SDL_HIDAPI_DriverPS4,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_STEAM
&SDL_HIDAPI_DriverSteam,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_SWITCH
&SDL_HIDAPI_DriverSwitch,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_XBOX360
&SDL_HIDAPI_DriverXbox360,
#endif
#ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
&SDL_HIDAPI_DriverXboxOne,
#endif
};
100
static int SDL_HIDAPI_numdrivers = 0;
101
102
static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
static int SDL_HIDAPI_numjoysticks = 0;
103
static SDL_bool initialized = SDL_FALSE;
104
105
106
107
108
#if defined(SDL_USE_LIBUDEV)
static const SDL_UDEV_Symbols * usyms = NULL;
#endif
109
110
111
112
113
114
115
static struct
{
SDL_bool m_bHaveDevicesChanged;
SDL_bool m_bCanGetNotifications;
Uint32 m_unLastDetect;
#if defined(__WIN32__)
116
SDL_threadID m_nThreadID;
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
WNDCLASSEXA m_wndClass;
HWND m_hwndMsg;
HDEVNOTIFY m_hNotify;
double m_flLastWin32MessageCheck;
#endif
#if defined(__MACOSX__)
IONotificationPortRef m_notificationPort;
mach_port_t m_notificationMach;
#endif
#if defined(SDL_USE_LIBUDEV)
struct udev *m_pUdev;
struct udev_monitor *m_pUdevMonitor;
int m_nUdevFd;
#endif
} SDL_HIDAPI_discovery;
#ifdef __WIN32__
struct _DEV_BROADCAST_HDR
{
DWORD dbch_size;
DWORD dbch_devicetype;
DWORD dbch_reserved;
};
typedef struct _DEV_BROADCAST_DEVICEINTERFACE_A
{
DWORD dbcc_size;
DWORD dbcc_devicetype;
DWORD dbcc_reserved;
GUID dbcc_classguid;
char dbcc_name[ 1 ];
} DEV_BROADCAST_DEVICEINTERFACE_A, *PDEV_BROADCAST_DEVICEINTERFACE_A;
typedef struct _DEV_BROADCAST_HDR DEV_BROADCAST_HDR;
#define DBT_DEVICEARRIVAL 0x8000 /* system detected a new device */
155
#define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
156
157
158
159
#define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
#define DBT_DEVNODES_CHANGED 0x0007
#define DBT_CONFIGCHANGED 0x0018
#define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
160
#define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
161
162
163
164
165
166
167
168
169
170
#include <initguid.h>
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
static LRESULT CALLBACK ControllerWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_DEVICECHANGE:
switch (wParam) {
case DBT_DEVICEARRIVAL:
171
case DBT_DEVICEREMOVECOMPLETE:
172
if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
173
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
174
175
176
}
break;
}
177
return TRUE;
178
179
180
181
182
183
184
185
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
#endif /* __WIN32__ */
#if defined(__MACOSX__)
186
static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
187
188
{
/* Must drain the iterator, or we won't receive new notifications */
189
190
io_object_t entry;
while ((entry = IOIteratorNext(portIterator)) != 0) {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
IOObjectRelease(entry);
*(SDL_bool*)context = SDL_TRUE;
}
}
#endif /* __MACOSX__ */
static void
HIDAPI_InitializeDiscovery()
{
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_FALSE;
SDL_HIDAPI_discovery.m_unLastDetect = 0;
#if defined(__WIN32__)
SDL_HIDAPI_discovery.m_nThreadID = SDL_ThreadID();
SDL_memset(&SDL_HIDAPI_discovery.m_wndClass, 0x0, sizeof(SDL_HIDAPI_discovery.m_wndClass));
SDL_HIDAPI_discovery.m_wndClass.hInstance = GetModuleHandle(NULL);
209
SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
210
211
212
213
SDL_HIDAPI_discovery.m_wndClass.lpfnWndProc = ControllerWndProc; /* This function is called by windows */
SDL_HIDAPI_discovery.m_wndClass.cbSize = sizeof(WNDCLASSEX);
RegisterClassExA(&SDL_HIDAPI_discovery.m_wndClass);
214
SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
{
DEV_BROADCAST_DEVICEINTERFACE_A devBroadcast;
SDL_memset( &devBroadcast, 0x0, sizeof( devBroadcast ) );
devBroadcast.dbcc_size = sizeof( devBroadcast );
devBroadcast.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
devBroadcast.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
/* DEVICE_NOTIFY_ALL_INTERFACE_CLASSES is important, makes GUID_DEVINTERFACE_USB_DEVICE ignored,
* but that seems to be necessary to get a notice after each individual usb input device actually
* installs, rather than just as the composite device is seen.
*/
SDL_HIDAPI_discovery.m_hNotify = RegisterDeviceNotification( SDL_HIDAPI_discovery.m_hwndMsg, &devBroadcast, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
SDL_HIDAPI_discovery.m_bCanGetNotifications = ( SDL_HIDAPI_discovery.m_hNotify != 0 );
}
231
232
233
234
235
236
237
238
239
240
#endif /* __WIN32__ */
#if defined(__MACOSX__)
SDL_HIDAPI_discovery.m_notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
if (SDL_HIDAPI_discovery.m_notificationPort) {
{
CFMutableDictionaryRef matchingDict = IOServiceMatching("IOUSBDevice");
/* Note: IOServiceAddMatchingNotification consumes the reference to matchingDict */
io_iterator_t portIterator = 0;
241
io_object_t entry;
242
243
if (IOServiceAddMatchingNotification(SDL_HIDAPI_discovery.m_notificationPort, kIOMatchedNotification, matchingDict, CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator) == 0) {
/* Must drain the existing iterator, or we won't receive new notifications */
244
while ((entry = IOIteratorNext(portIterator)) != 0) {
245
246
247
248
249
250
251
252
253
254
255
256
IOObjectRelease(entry);
}
} else {
IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
SDL_HIDAPI_discovery.m_notificationPort = nil;
}
}
{
CFMutableDictionaryRef matchingDict = IOServiceMatching("IOBluetoothDevice");
/* Note: IOServiceAddMatchingNotification consumes the reference to matchingDict */
io_iterator_t portIterator = 0;
257
io_object_t entry;
258
259
if (IOServiceAddMatchingNotification(SDL_HIDAPI_discovery.m_notificationPort, kIOMatchedNotification, matchingDict, CallbackIOServiceFunc, &SDL_HIDAPI_discovery.m_bHaveDevicesChanged, &portIterator) == 0) {
/* Must drain the existing iterator, or we won't receive new notifications */
260
while ((entry = IOIteratorNext(portIterator)) != 0) {
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
IOObjectRelease(entry);
}
} else {
IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
SDL_HIDAPI_discovery.m_notificationPort = nil;
}
}
}
SDL_HIDAPI_discovery.m_notificationMach = MACH_PORT_NULL;
if (SDL_HIDAPI_discovery.m_notificationPort) {
SDL_HIDAPI_discovery.m_notificationMach = IONotificationPortGetMachPort(SDL_HIDAPI_discovery.m_notificationPort);
}
SDL_HIDAPI_discovery.m_bCanGetNotifications = (SDL_HIDAPI_discovery.m_notificationMach != MACH_PORT_NULL);
#endif // __MACOSX__
#if defined(SDL_USE_LIBUDEV)
SDL_HIDAPI_discovery.m_pUdev = NULL;
SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
SDL_HIDAPI_discovery.m_nUdevFd = -1;
284
285
286
287
usyms = SDL_UDEV_GetUdevSyms();
if (usyms) {
SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
}
288
if (SDL_HIDAPI_discovery.m_pUdev) {
289
SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
290
if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
291
292
usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
293
SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
}
}
#endif /* SDL_USE_LIBUDEV */
}
static void
HIDAPI_UpdateDiscovery()
{
if (!SDL_HIDAPI_discovery.m_bCanGetNotifications) {
const Uint32 SDL_HIDAPI_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
Uint32 now = SDL_GetTicks();
if (!SDL_HIDAPI_discovery.m_unLastDetect || SDL_TICKS_PASSED(now, SDL_HIDAPI_discovery.m_unLastDetect + SDL_HIDAPI_DETECT_INTERVAL_MS)) {
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
SDL_HIDAPI_discovery.m_unLastDetect = now;
}
return;
}
313
314
#if defined(__WIN32__)
#if 0 /* just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan. */
315
316
317
318
319
320
321
322
323
324
325
/* We'll only get messages on the same thread that created the window */
if (SDL_ThreadID() == SDL_HIDAPI_discovery.m_nThreadID) {
MSG msg;
while (PeekMessage(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0, PM_NOREMOVE)) {
if (GetMessageA(&msg, SDL_HIDAPI_discovery.m_hwndMsg, 0, 0) != 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
#endif
326
#endif /* __WIN32__ */
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#if defined(__MACOSX__)
if (SDL_HIDAPI_discovery.m_notificationPort) {
struct { mach_msg_header_t hdr; char payload[ 4096 ]; } msg;
while (mach_msg(&msg.hdr, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, sizeof(msg), SDL_HIDAPI_discovery.m_notificationMach, 0, MACH_PORT_NULL) == KERN_SUCCESS) {
IODispatchCalloutFromMessage(NULL, &msg.hdr, SDL_HIDAPI_discovery.m_notificationPort);
}
}
#endif
#if defined(SDL_USE_LIBUDEV)
if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
/* Drain all notification events.
* We don't expect a lot of device notifications so just
* do a new discovery on any kind or number of notifications.
* This could be made more restrictive if necessary.
*/
for (;;) {
struct pollfd PollUdev;
346
struct udev_device *pUdevDevice;
347
348
349
350
351
352
353
PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
PollUdev.events = POLLIN;
if (poll(&PollUdev, 1, 0) != 1) {
break;
}
354
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
355
356
pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
357
if (pUdevDevice) {
358
usyms->udev_device_unref(pUdevDevice);
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
}
}
}
#endif
}
static void
HIDAPI_ShutdownDiscovery()
{
#if defined(__WIN32__)
if (SDL_HIDAPI_discovery.m_hNotify)
UnregisterDeviceNotification(SDL_HIDAPI_discovery.m_hNotify);
if (SDL_HIDAPI_discovery.m_hwndMsg) {
DestroyWindow(SDL_HIDAPI_discovery.m_hwndMsg);
}
UnregisterClassA(SDL_HIDAPI_discovery.m_wndClass.lpszClassName, SDL_HIDAPI_discovery.m_wndClass.hInstance);
#endif
#if defined(__MACOSX__)
if (SDL_HIDAPI_discovery.m_notificationPort) {
IONotificationPortDestroy(SDL_HIDAPI_discovery.m_notificationPort);
}
#endif
#if defined(SDL_USE_LIBUDEV)
386
if (usyms) {
387
388
389
390
391
392
if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
}
if (SDL_HIDAPI_discovery.m_pUdev) {
usyms->udev_unref(SDL_HIDAPI_discovery.m_pUdev);
}
393
394
SDL_UDEV_ReleaseUdevSyms();
usyms = NULL;
395
396
397
398
}
#endif
}
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const char *
HIDAPI_XboxControllerName(Uint16 vendor_id, Uint16 product_id)
{
static struct
{
Uint32 vidpid;
const char *name;
} names[] = {
{ MAKE_VIDPID(0x0079, 0x18d4), "GPD Win 2 X-Box Controller" },
{ MAKE_VIDPID(0x044f, 0xb326), "Thrustmaster Gamepad GP XID" },
{ MAKE_VIDPID(0x045e, 0x028e), "Microsoft X-Box 360 pad" },
{ MAKE_VIDPID(0x045e, 0x028f), "Microsoft X-Box 360 pad v2" },
{ MAKE_VIDPID(0x045e, 0x0291), "Xbox 360 Wireless Receiver (XBOX)" },
413
{ MAKE_VIDPID(0x045e, 0x02a1), "Microsoft X-Box 360 pad" },
414
415
{ MAKE_VIDPID(0x045e, 0x02d1), "Microsoft X-Box One pad" },
{ MAKE_VIDPID(0x045e, 0x02dd), "Microsoft X-Box One pad (Firmware 2015)" },
416
{ MAKE_VIDPID(0x045e, 0x02e0), "Microsoft X-Box One S pad" },
417
418
{ MAKE_VIDPID(0x045e, 0x02e3), "Microsoft X-Box One Elite pad" },
{ MAKE_VIDPID(0x045e, 0x02ea), "Microsoft X-Box One S pad" },
419
420
421
422
{ MAKE_VIDPID(0x045e, 0x02fd), "Microsoft X-Box One S pad" },
{ MAKE_VIDPID(0x045e, 0x02ff), "Microsoft X-Box One Elite pad" },
{ MAKE_VIDPID(0x045e, 0x0b00), "Microsoft X-Box One Elite Series 2 pad" },
{ MAKE_VIDPID(0x045e, 0x0b05), "Microsoft X-Box One Elite Series 2 pad" },
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
464
465
466
467
468
{ MAKE_VIDPID(0x045e, 0x0719), "Xbox 360 Wireless Receiver" },
{ MAKE_VIDPID(0x046d, 0xc21d), "Logitech Gamepad F310" },
{ MAKE_VIDPID(0x046d, 0xc21e), "Logitech Gamepad F510" },
{ MAKE_VIDPID(0x046d, 0xc21f), "Logitech Gamepad F710" },
{ MAKE_VIDPID(0x046d, 0xc242), "Logitech Chillstream Controller" },
{ MAKE_VIDPID(0x046d, 0xcaa3), "Logitech DriveFx Racing Wheel" },
{ MAKE_VIDPID(0x056e, 0x2004), "Elecom JC-U3613M" },
{ MAKE_VIDPID(0x06a3, 0xf51a), "Saitek P3600" },
{ MAKE_VIDPID(0x0738, 0x4716), "Mad Catz Wired Xbox 360 Controller" },
{ MAKE_VIDPID(0x0738, 0x4718), "Mad Catz Street Fighter IV FightStick SE" },
{ MAKE_VIDPID(0x0738, 0x4726), "Mad Catz Xbox 360 Controller" },
{ MAKE_VIDPID(0x0738, 0x4728), "Mad Catz Street Fighter IV FightPad" },
{ MAKE_VIDPID(0x0738, 0x4736), "Mad Catz MicroCon Gamepad" },
{ MAKE_VIDPID(0x0738, 0x4738), "Mad Catz Wired Xbox 360 Controller (SFIV)" },
{ MAKE_VIDPID(0x0738, 0x4740), "Mad Catz Beat Pad" },
{ MAKE_VIDPID(0x0738, 0x4758), "Mad Catz Arcade Game Stick" },
{ MAKE_VIDPID(0x0738, 0x4a01), "Mad Catz FightStick TE 2" },
{ MAKE_VIDPID(0x0738, 0x9871), "Mad Catz Portable Drum" },
{ MAKE_VIDPID(0x0738, 0xb726), "Mad Catz Xbox controller - MW2" },
{ MAKE_VIDPID(0x0738, 0xb738), "Mad Catz MVC2TE Stick 2" },
{ MAKE_VIDPID(0x0738, 0xbeef), "Mad Catz JOYTECH NEO SE Advanced GamePad" },
{ MAKE_VIDPID(0x0738, 0xcb02), "Saitek Cyborg Rumble Pad - PC/Xbox 360" },
{ MAKE_VIDPID(0x0738, 0xcb03), "Saitek P3200 Rumble Pad - PC/Xbox 360" },
{ MAKE_VIDPID(0x0738, 0xcb29), "Saitek Aviator Stick AV8R02" },
{ MAKE_VIDPID(0x0738, 0xf738), "Super SFIV FightStick TE S" },
{ MAKE_VIDPID(0x07ff, 0xffff), "Mad Catz GamePad" },
{ MAKE_VIDPID(0x0e6f, 0x0105), "HSM3 Xbox360 dancepad" },
{ MAKE_VIDPID(0x0e6f, 0x0113), "Afterglow AX.1 Gamepad for Xbox 360" },
{ MAKE_VIDPID(0x0e6f, 0x011f), "Rock Candy Gamepad Wired Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0131), "PDP EA Sports Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0133), "Xbox 360 Wired Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0139), "Afterglow Prismatic Wired Controller" },
{ MAKE_VIDPID(0x0e6f, 0x013a), "PDP Xbox One Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0146), "Rock Candy Wired Controller for Xbox One" },
{ MAKE_VIDPID(0x0e6f, 0x0147), "PDP Marvel Xbox One Controller" },
{ MAKE_VIDPID(0x0e6f, 0x015c), "PDP Xbox One Arcade Stick" },
{ MAKE_VIDPID(0x0e6f, 0x0161), "PDP Xbox One Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0162), "PDP Xbox One Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0163), "PDP Xbox One Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0164), "PDP Battlefield One" },
{ MAKE_VIDPID(0x0e6f, 0x0165), "PDP Titanfall 2" },
{ MAKE_VIDPID(0x0e6f, 0x0201), "Pelican PL-3601 'TSZ' Wired Xbox 360 Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0213), "Afterglow Gamepad for Xbox 360" },
{ MAKE_VIDPID(0x0e6f, 0x021f), "Rock Candy Gamepad for Xbox 360" },
{ MAKE_VIDPID(0x0e6f, 0x0246), "Rock Candy Gamepad for Xbox One 2015" },
{ MAKE_VIDPID(0x0e6f, 0x02a4), "PDP Wired Controller for Xbox One - Stealth Series" },
469
{ MAKE_VIDPID(0x0e6f, 0x02a6), "PDP Wired Controller for Xbox One - Camo Series" },
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
{ MAKE_VIDPID(0x0e6f, 0x02ab), "PDP Controller for Xbox One" },
{ MAKE_VIDPID(0x0e6f, 0x0301), "Logic3 Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0346), "Rock Candy Gamepad for Xbox One 2016" },
{ MAKE_VIDPID(0x0e6f, 0x0401), "Logic3 Controller" },
{ MAKE_VIDPID(0x0e6f, 0x0413), "Afterglow AX.1 Gamepad for Xbox 360" },
{ MAKE_VIDPID(0x0e6f, 0x0501), "PDP Xbox 360 Controller" },
{ MAKE_VIDPID(0x0e6f, 0xf900), "PDP Afterglow AX.1" },
{ MAKE_VIDPID(0x0f0d, 0x000a), "Hori Co. DOA4 FightStick" },
{ MAKE_VIDPID(0x0f0d, 0x000c), "Hori PadEX Turbo" },
{ MAKE_VIDPID(0x0f0d, 0x000d), "Hori Fighting Stick EX2" },
{ MAKE_VIDPID(0x0f0d, 0x0016), "Hori Real Arcade Pro.EX" },
{ MAKE_VIDPID(0x0f0d, 0x001b), "Hori Real Arcade Pro VX" },
{ MAKE_VIDPID(0x0f0d, 0x0063), "Hori Real Arcade Pro Hayabusa (USA) Xbox One" },
{ MAKE_VIDPID(0x0f0d, 0x0067), "HORIPAD ONE" },
{ MAKE_VIDPID(0x0f0d, 0x0078), "Hori Real Arcade Pro V Kai Xbox One" },
485
486
487
{ MAKE_VIDPID(0x0f0d, 0x0084), "HORI Fighting Commander" },
{ MAKE_VIDPID(0x0f0d, 0x0085), "HORI Fighting Commander" },
{ MAKE_VIDPID(0x0f0d, 0x0086), "HORI Fighting Commander" },
488
489
{ MAKE_VIDPID(0x1038, 0x1430), "SteelSeries Stratus Duo" },
{ MAKE_VIDPID(0x1038, 0x1431), "SteelSeries Stratus Duo" },
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
{ MAKE_VIDPID(0x11c9, 0x55f0), "Nacon GC-100XF" },
{ MAKE_VIDPID(0x12ab, 0x0004), "Honey Bee Xbox360 dancepad" },
{ MAKE_VIDPID(0x12ab, 0x0301), "PDP AFTERGLOW AX.1" },
{ MAKE_VIDPID(0x12ab, 0x0303), "Mortal Kombat Klassic FightStick" },
{ MAKE_VIDPID(0x1430, 0x4748), "RedOctane Guitar Hero X-plorer" },
{ MAKE_VIDPID(0x1430, 0xf801), "RedOctane Controller" },
{ MAKE_VIDPID(0x146b, 0x0601), "BigBen Interactive XBOX 360 Controller" },
{ MAKE_VIDPID(0x1532, 0x0037), "Razer Sabertooth" },
{ MAKE_VIDPID(0x1532, 0x0a00), "Razer Atrox Arcade Stick" },
{ MAKE_VIDPID(0x1532, 0x0a03), "Razer Wildcat" },
{ MAKE_VIDPID(0x15e4, 0x3f00), "Power A Mini Pro Elite" },
{ MAKE_VIDPID(0x15e4, 0x3f0a), "Xbox Airflo wired controller" },
{ MAKE_VIDPID(0x15e4, 0x3f10), "Batarang Xbox 360 controller" },
{ MAKE_VIDPID(0x162e, 0xbeef), "Joytech Neo-Se Take2" },
{ MAKE_VIDPID(0x1689, 0xfd00), "Razer Onza Tournament Edition" },
{ MAKE_VIDPID(0x1689, 0xfd01), "Razer Onza Classic Edition" },
{ MAKE_VIDPID(0x1689, 0xfe00), "Razer Sabertooth" },
{ MAKE_VIDPID(0x1bad, 0x0002), "Harmonix Rock Band Guitar" },
{ MAKE_VIDPID(0x1bad, 0x0003), "Harmonix Rock Band Drumkit" },
{ MAKE_VIDPID(0x1bad, 0x0130), "Ion Drum Rocker" },
{ MAKE_VIDPID(0x1bad, 0xf016), "Mad Catz Xbox 360 Controller" },
{ MAKE_VIDPID(0x1bad, 0xf018), "Mad Catz Street Fighter IV SE Fighting Stick" },
{ MAKE_VIDPID(0x1bad, 0xf019), "Mad Catz Brawlstick for Xbox 360" },
{ MAKE_VIDPID(0x1bad, 0xf021), "Mad Cats Ghost Recon FS GamePad" },
{ MAKE_VIDPID(0x1bad, 0xf023), "MLG Pro Circuit Controller (Xbox)" },
{ MAKE_VIDPID(0x1bad, 0xf025), "Mad Catz Call Of Duty" },
{ MAKE_VIDPID(0x1bad, 0xf027), "Mad Catz FPS Pro" },
{ MAKE_VIDPID(0x1bad, 0xf028), "Street Fighter IV FightPad" },
{ MAKE_VIDPID(0x1bad, 0xf02e), "Mad Catz Fightpad" },
{ MAKE_VIDPID(0x1bad, 0xf030), "Mad Catz Xbox 360 MC2 MicroCon Racing Wheel" },
{ MAKE_VIDPID(0x1bad, 0xf036), "Mad Catz MicroCon GamePad Pro" },
{ MAKE_VIDPID(0x1bad, 0xf038), "Street Fighter IV FightStick TE" },
{ MAKE_VIDPID(0x1bad, 0xf039), "Mad Catz MvC2 TE" },
{ MAKE_VIDPID(0x1bad, 0xf03a), "Mad Catz SFxT Fightstick Pro" },
{ MAKE_VIDPID(0x1bad, 0xf03d), "Street Fighter IV Arcade Stick TE - Chun Li" },
{ MAKE_VIDPID(0x1bad, 0xf03e), "Mad Catz MLG FightStick TE" },
{ MAKE_VIDPID(0x1bad, 0xf03f), "Mad Catz FightStick SoulCaliber" },
{ MAKE_VIDPID(0x1bad, 0xf042), "Mad Catz FightStick TES+" },
{ MAKE_VIDPID(0x1bad, 0xf080), "Mad Catz FightStick TE2" },
{ MAKE_VIDPID(0x1bad, 0xf501), "HoriPad EX2 Turbo" },
{ MAKE_VIDPID(0x1bad, 0xf502), "Hori Real Arcade Pro.VX SA" },
{ MAKE_VIDPID(0x1bad, 0xf503), "Hori Fighting Stick VX" },
{ MAKE_VIDPID(0x1bad, 0xf504), "Hori Real Arcade Pro. EX" },
{ MAKE_VIDPID(0x1bad, 0xf505), "Hori Fighting Stick EX2B" },
{ MAKE_VIDPID(0x1bad, 0xf506), "Hori Real Arcade Pro.EX Premium VLX" },
{ MAKE_VIDPID(0x1bad, 0xf900), "Harmonix Xbox 360 Controller" },
{ MAKE_VIDPID(0x1bad, 0xf901), "Gamestop Xbox 360 Controller" },
{ MAKE_VIDPID(0x1bad, 0xf903), "Tron Xbox 360 controller" },
{ MAKE_VIDPID(0x1bad, 0xf904), "PDP Versus Fighting Pad" },
{ MAKE_VIDPID(0x1bad, 0xf906), "MortalKombat FightStick" },
{ MAKE_VIDPID(0x1bad, 0xfa01), "MadCatz GamePad" },
{ MAKE_VIDPID(0x1bad, 0xfd00), "Razer Onza TE" },
{ MAKE_VIDPID(0x1bad, 0xfd01), "Razer Onza" },
{ MAKE_VIDPID(0x24c6, 0x5000), "Razer Atrox Arcade Stick" },
{ MAKE_VIDPID(0x24c6, 0x5300), "PowerA MINI PROEX Controller" },
{ MAKE_VIDPID(0x24c6, 0x5303), "Xbox Airflo wired controller" },
{ MAKE_VIDPID(0x24c6, 0x530a), "Xbox 360 Pro EX Controller" },
{ MAKE_VIDPID(0x24c6, 0x531a), "PowerA Pro Ex" },
{ MAKE_VIDPID(0x24c6, 0x5397), "FUS1ON Tournament Controller" },
{ MAKE_VIDPID(0x24c6, 0x541a), "PowerA Xbox One Mini Wired Controller" },
{ MAKE_VIDPID(0x24c6, 0x542a), "Xbox ONE spectra" },
{ MAKE_VIDPID(0x24c6, 0x543a), "PowerA Xbox One wired controller" },
{ MAKE_VIDPID(0x24c6, 0x5500), "Hori XBOX 360 EX 2 with Turbo" },
{ MAKE_VIDPID(0x24c6, 0x5501), "Hori Real Arcade Pro VX-SA" },
{ MAKE_VIDPID(0x24c6, 0x5502), "Hori Fighting Stick VX Alt" },
{ MAKE_VIDPID(0x24c6, 0x5503), "Hori Fighting Edge" },
{ MAKE_VIDPID(0x24c6, 0x5506), "Hori SOULCALIBUR V Stick" },
{ MAKE_VIDPID(0x24c6, 0x550d), "Hori GEM Xbox controller" },
{ MAKE_VIDPID(0x24c6, 0x550e), "Hori Real Arcade Pro V Kai 360" },
559
{ MAKE_VIDPID(0x24c6, 0x5510), "Hori Fighting Commander ONE" },
560
561
{ MAKE_VIDPID(0x24c6, 0x551a), "PowerA FUSION Pro Controller" },
{ MAKE_VIDPID(0x24c6, 0x561a), "PowerA FUSION Controller" },
562
{ MAKE_VIDPID(0x24c6, 0x591a), "PowerA FUSION Pro Controller" },
563
564
565
566
{ MAKE_VIDPID(0x24c6, 0x5b00), "ThrustMaster Ferrari 458 Racing Wheel" },
{ MAKE_VIDPID(0x24c6, 0x5b02), "Thrustmaster, Inc. GPX Controller" },
{ MAKE_VIDPID(0x24c6, 0x5b03), "Thrustmaster Ferrari 458 Racing Wheel" },
{ MAKE_VIDPID(0x24c6, 0x5d04), "Razer Sabertooth" },
567
{ MAKE_VIDPID(0x24c6, 0x791a), "PowerA Fusion Fight Pad" },
568
569
570
571
{ MAKE_VIDPID(0x24c6, 0xfafa), "Aplay Controller" },
{ MAKE_VIDPID(0x24c6, 0xfafb), "Aplay Controller" },
{ MAKE_VIDPID(0x24c6, 0xfafc), "Afterglow Gamepad 1" },
{ MAKE_VIDPID(0x24c6, 0xfafd), "Afterglow Gamepad 3" },
572
{ MAKE_VIDPID(0x24c6, 0xfafe), "Rock Candy Gamepad for Xbox 360" },
573
{ MAKE_VIDPID(0x2e24, 0x1688), "Hyperkin X91" },
574
575
576
577
578
579
580
581
582
583
584
585
};
int i;
Uint32 vidpid = MAKE_VIDPID(vendor_id, product_id);
for (i = 0; i < SDL_arraysize(names); ++i) {
if (vidpid == names[i].vidpid) {
return names[i].name;
}
}
return NULL;
}
586
static SDL_bool
587
HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
588
589
590
591
592
{
int i;
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
593
if (driver->enabled && driver->IsSupportedDevice(vendor_id, product_id, version, -1, name)) {
594
595
596
597
598
599
return SDL_TRUE;
}
}
return SDL_FALSE;
}
600
601
602
static SDL_HIDAPI_DeviceDriver *
HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
{
603
604
605
606
const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
const Uint16 USAGE_JOYSTICK = 0x0004;
const Uint16 USAGE_GAMEPAD = 0x0005;
const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
607
608
609
610
611
612
int i;
if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
return NULL;
}
613
614
615
616
617
618
619
if (device->usage_page && device->usage_page != USAGE_PAGE_GENERIC_DESKTOP) {
return NULL;
}
if (device->usage && device->usage != USAGE_JOYSTICK && device->usage != USAGE_GAMEPAD && device->usage != USAGE_MULTIAXISCONTROLLER) {
return NULL;
}
620
621
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
622
if (driver->enabled && driver->IsSupportedDevice(device->vendor_id, device->product_id, device->version, device->interface_number, device->name)) {
623
624
625
626
627
628
629
return driver;
}
}
return NULL;
}
static SDL_HIDAPI_Device *
630
HIDAPI_GetJoystickByIndex(int device_index)
631
632
633
634
{
SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
while (device) {
if (device->driver) {
635
if (device_index == 0) {
636
637
break;
}
638
--device_index;
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
}
device = device->next;
}
return device;
}
static SDL_HIDAPI_Device *
HIDAPI_GetJoystickByInfo(const char *path, Uint16 vendor_id, Uint16 product_id)
{
SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
while (device) {
if (device->vendor_id == vendor_id && device->product_id == product_id &&
SDL_strcmp(device->path, path) == 0) {
break;
}
device = device->next;
}
return device;
}
static void SDLCALL
SDL_HIDAPIDriverHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
int i;
SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
664
SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
if (SDL_strcmp(name, SDL_HINT_JOYSTICK_HIDAPI) == 0) {
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
driver->enabled = SDL_GetHintBoolean(driver->hint, enabled);
}
} else {
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
if (SDL_strcmp(name, driver->hint) == 0) {
driver->enabled = enabled;
break;
}
}
}
681
682
683
684
685
686
687
688
SDL_HIDAPI_numdrivers = 0;
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
if (driver->enabled) {
++SDL_HIDAPI_numdrivers;
}
}
689
690
691
692
/* Update device list if driver availability changes */
while (device) {
if (device->driver) {
if (!device->driver->enabled) {
693
694
695
696
697
device->driver = NULL;
--SDL_HIDAPI_numjoysticks;
SDL_PrivateJoystickRemoved(device->instance_id);
698
699
700
701
}
} else {
device->driver = HIDAPI_GetDeviceDriver(device);
if (device->driver) {
702
703
704
705
706
device->instance_id = SDL_GetNextJoystickInstanceID();
++SDL_HIDAPI_numjoysticks;
SDL_PrivateJoystickAdded(device->instance_id);
707
708
709
710
711
712
713
714
715
716
717
718
719
}
}
device = device->next;
}
}
static void HIDAPI_JoystickDetect(void);
static int
HIDAPI_JoystickInit(void)
{
int i;
720
721
722
723
if (initialized) {
return 0;
}
724
725
726
727
728
729
730
731
732
733
734
if (hid_init() < 0) {
SDL_SetError("Couldn't initialize hidapi");
return -1;
}
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
SDL_AddHintCallback(driver->hint, SDL_HIDAPIDriverHintChanged, NULL);
}
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI,
SDL_HIDAPIDriverHintChanged, NULL);
735
HIDAPI_InitializeDiscovery();
736
HIDAPI_JoystickDetect();
737
738
739
initialized = SDL_TRUE;
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
return 0;
}
static int
HIDAPI_JoystickGetCount(void)
{
return SDL_HIDAPI_numjoysticks;
}
static void
HIDAPI_AddDevice(struct hid_device_info *info)
{
SDL_HIDAPI_Device *device;
SDL_HIDAPI_Device *curr, *last = NULL;
for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
continue;
}
device = (SDL_HIDAPI_Device *)SDL_calloc(1, sizeof(*device));
if (!device) {
return;
}
763
device->instance_id = -1;
764
765
766
device->seen = SDL_TRUE;
device->vendor_id = info->vendor_id;
device->product_id = info->product_id;
767
device->version = info->release_number;
768
769
770
771
772
773
774
device->interface_number = info->interface_number;
device->usage_page = info->usage_page;
device->usage = info->usage;
{
/* FIXME: Is there any way to tell whether this is a Bluetooth device? */
const Uint16 vendor = device->vendor_id;
const Uint16 product = device->product_id;
775
const Uint16 version = device->version;
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
Uint16 *guid16 = (Uint16 *)device->guid.data;
*guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(vendor);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(product);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(version);
*guid16++ = 0;
/* Note that this is a HIDAPI device for special handling elsewhere */
device->guid.data[14] = 'h';
device->guid.data[15] = 0;
}
792
/* Need the device name before getting the driver to know whether to ignore this device */
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
if (!device->name && info->manufacturer_string && info->product_string) {
char *manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
char *product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
if (!manufacturer_string && !product_string) {
if (sizeof(wchar_t) == sizeof(Uint16)) {
manufacturer_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
product_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
} else if (sizeof(wchar_t) == sizeof(Uint32)) {
manufacturer_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
product_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
}
}
if (manufacturer_string && product_string) {
size_t name_size = (SDL_strlen(manufacturer_string) + 1 + SDL_strlen(product_string) + 1);
device->name = (char *)SDL_malloc(name_size);
if (device->name) {
SDL_snprintf(device->name, name_size, "%s %s", manufacturer_string, product_string);
}
}
if (manufacturer_string) {
SDL_free(manufacturer_string);
}
if (product_string) {
SDL_free(product_string);
}
}
if (!device->name) {
size_t name_size = (6 + 1 + 6 + 1);
device->name = (char *)SDL_malloc(name_size);
if (!device->name) {
SDL_free(device);
return;
}
SDL_snprintf(device->name, name_size, "0x%.4x/0x%.4x", info->vendor_id, info->product_id);
}
829
830
831
832
833
834
835
836
837
838
device->driver = HIDAPI_GetDeviceDriver(device);
if (device->driver) {
const char *name = device->driver->GetDeviceName(device->vendor_id, device->product_id);
if (name) {
SDL_free(device->name);
device->name = SDL_strdup(name);
}
}
839
840
841
842
843
844
845
846
device->path = SDL_strdup(info->path);
if (!device->path) {
SDL_free(device->name);
SDL_free(device);
return;
}
#ifdef DEBUG_HIDAPI
847
SDL_Log("Adding HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, interface %d, usage page 0x%.4x, usage 0x%.4x, driver = %s\n", device->name, device->vendor_id, device->product_id, device->version, device->interface_number, device->usage_page, device->usage, device->driver ? device->driver->hint : "NONE");
848
849
850
851
852
853
854
855
856
857
#endif
/* Add it to the list */
if (last) {
last->next = device;
} else {
SDL_HIDAPI_devices = device;
}
if (device->driver) {
858
859
860
861
862
863
/* It's a joystick! */
device->instance_id = SDL_GetNextJoystickInstanceID();
++SDL_HIDAPI_numjoysticks;
SDL_PrivateJoystickAdded(device->instance_id);
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
}
}
static void
HIDAPI_DelDevice(SDL_HIDAPI_Device *device, SDL_bool send_event)
{
SDL_HIDAPI_Device *curr, *last;
for (curr = SDL_HIDAPI_devices, last = NULL; curr; last = curr, curr = curr->next) {
if (curr == device) {
if (last) {
last->next = curr->next;
} else {
SDL_HIDAPI_devices = curr->next;
}
880
881
882
883
884
if (device->driver && send_event) {
/* Need to decrement the joystick count before we post the event */
--SDL_HIDAPI_numjoysticks;
SDL_PrivateJoystickRemoved(device->instance_id);
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
}
SDL_free(device->name);
SDL_free(device->path);
SDL_free(device);
return;
}
}
}
static void
HIDAPI_UpdateDeviceList(void)
{
SDL_HIDAPI_Device *device;
struct hid_device_info *devs, *info;
/* Prepare the existing device list */
device = SDL_HIDAPI_devices;
while (device) {
device->seen = SDL_FALSE;
device = device->next;
}
/* Enumerate the devices */
909
910
911
912
913
914
915
916
917
918
if (SDL_HIDAPI_numdrivers > 0) {
devs = hid_enumerate(0, 0);
if (devs) {
for (info = devs; info; info = info->next) {
device = HIDAPI_GetJoystickByInfo(info->path, info->vendor_id, info->product_id);
if (device) {
device->seen = SDL_TRUE;
} else {
HIDAPI_AddDevice(info);
}
919
}
920
hid_free_enumeration(devs);
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
}
}
/* Remove any devices that weren't seen */
device = SDL_HIDAPI_devices;
while (device) {
SDL_HIDAPI_Device *next = device->next;
if (!device->seen) {
HIDAPI_DelDevice(device, SDL_TRUE);
}
device = next;
}
}
SDL_bool
937
HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
938
939
940
{
SDL_HIDAPI_Device *device;
941
942
943
944
945
/* Make sure we're initialized, as this could be called from other drivers during startup */
if (HIDAPI_JoystickInit() < 0) {
return SDL_FALSE;
}
946
/* Don't update the device list for devices we know aren't supported */
947
if (!HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name)) {
948
949
950
return SDL_FALSE;
}
951
952
953
/* Make sure the device list is completely up to date when we check for device presence */
HIDAPI_UpdateDeviceList();
954
955
956
957
/* Note that this isn't a perfect check - there may be multiple devices with 0 VID/PID,
or a different name than we have it listed here, etc, but if we support the device
and we have something similar in our device list, mark it as present.
*/
958
959
960
961
962
963
964
965
966
967
968
969
970
device = SDL_HIDAPI_devices;
while (device) {
if (device->vendor_id == vendor_id && device->product_id == product_id && device->driver) {
return SDL_TRUE;
}
device = device->next;
}
return SDL_FALSE;
}
static void
HIDAPI_JoystickDetect(void)
{
971
972
973
HIDAPI_UpdateDiscovery();
if (SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
/* FIXME: We probably need to schedule an update in a few seconds as well */
974
HIDAPI_UpdateDeviceList();
975
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_FALSE;
976
977
978
979
980
981
}
}
static const char *
HIDAPI_JoystickGetDeviceName(int device_index)
{
982
return HIDAPI_GetJoystickByIndex(device_index)->name;
983
984
}
985
986
987
static int
HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
{
988
return -1;
989
990
}
991
992
993
static SDL_JoystickGUID
HIDAPI_JoystickGetDeviceGUID(int device_index)
{
994
return HIDAPI_GetJoystickByIndex(device_index)->guid;
995
996
997
998
999
}
static SDL_JoystickID
HIDAPI_JoystickGetDeviceInstanceID(int device_index)
{
1000
return HIDAPI_GetJoystickByIndex(device_index)->instance_id;