Skip to content

Latest commit

 

History

History
1037 lines (886 loc) · 32.3 KB

SDL_hidapijoystick.c

File metadata and controls

1037 lines (886 loc) · 32.3 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 5, 2019
Jan 5, 2019
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
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
Dec 19, 2019
Dec 19, 2019
25
#include "SDL_assert.h"
26
27
28
#include "SDL_endian.h"
#include "SDL_hints.h"
#include "SDL_log.h"
Oct 3, 2018
Oct 3, 2018
29
#include "SDL_mutex.h"
Aug 16, 2018
Aug 16, 2018
30
#include "SDL_thread.h"
31
32
33
34
#include "SDL_timer.h"
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
Nov 14, 2019
Nov 14, 2019
35
#include "../../SDL_hints_c.h"
Aug 16, 2018
Aug 16, 2018
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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
Jun 19, 2019
Jun 19, 2019
55
struct joystick_hwdata
Dec 19, 2019
Dec 19, 2019
57
SDL_HIDAPI_Device *device;
Jun 19, 2019
Jun 19, 2019
58
59
};
60
61
62
63
64
65
66
67
68
69
70
71
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,
Dec 19, 2019
Dec 19, 2019
72
&SDL_HIDAPI_DriverXbox360W,
73
74
75
76
#endif
#ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
&SDL_HIDAPI_DriverXboxOne,
#endif
Dec 19, 2019
Dec 19, 2019
77
78
79
#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
&SDL_HIDAPI_DriverGameCube,
#endif
Jul 25, 2019
Jul 25, 2019
81
static int SDL_HIDAPI_numdrivers = 0;
Dec 19, 2019
Dec 19, 2019
82
static SDL_mutex *SDL_HIDAPI_mutex;
83
84
static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
static int SDL_HIDAPI_numjoysticks = 0;
Jul 31, 2019
Jul 31, 2019
85
static SDL_bool initialized = SDL_FALSE;
Dec 19, 2019
Dec 19, 2019
86
static SDL_bool shutting_down = SDL_FALSE;
Aug 16, 2018
Aug 16, 2018
87
Sep 24, 2018
Sep 24, 2018
88
89
90
91
#if defined(SDL_USE_LIBUDEV)
static const SDL_UDEV_Symbols * usyms = NULL;
#endif
Aug 16, 2018
Aug 16, 2018
92
93
94
95
96
97
98
static struct
{
SDL_bool m_bHaveDevicesChanged;
SDL_bool m_bCanGetNotifications;
Uint32 m_unLastDetect;
#if defined(__WIN32__)
Aug 16, 2018
Aug 16, 2018
99
SDL_threadID m_nThreadID;
Aug 16, 2018
Aug 16, 2018
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
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 */
Aug 30, 2018
Aug 30, 2018
138
#define DBT_DEVICEREMOVECOMPLETE 0x8004 /* device was removed from the system */
Aug 16, 2018
Aug 16, 2018
139
140
141
142
#define DBT_DEVTYP_DEVICEINTERFACE 0x00000005 /* device interface class */
#define DBT_DEVNODES_CHANGED 0x0007
#define DBT_CONFIGCHANGED 0x0018
#define DBT_DEVICETYPESPECIFIC 0x8005 /* type specific event */
Aug 16, 2018
Aug 16, 2018
143
#define DBT_DEVINSTSTARTED 0x8008 /* device installed and started */
Aug 16, 2018
Aug 16, 2018
144
145
146
147
148
149
150
151
152
153
#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:
Aug 30, 2018
Aug 30, 2018
154
case DBT_DEVICEREMOVECOMPLETE:
Aug 16, 2018
Aug 16, 2018
155
if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
Aug 16, 2018
Aug 16, 2018
156
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
Aug 16, 2018
Aug 16, 2018
157
158
159
}
break;
}
Aug 16, 2018
Aug 16, 2018
160
return TRUE;
Aug 16, 2018
Aug 16, 2018
161
162
163
164
165
166
167
168
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
#endif /* __WIN32__ */
#if defined(__MACOSX__)
Aug 16, 2018
Aug 16, 2018
169
static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
Aug 16, 2018
Aug 16, 2018
170
171
{
/* Must drain the iterator, or we won't receive new notifications */
Aug 16, 2018
Aug 16, 2018
172
173
io_object_t entry;
while ((entry = IOIteratorNext(portIterator)) != 0) {
Aug 16, 2018
Aug 16, 2018
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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);
Aug 16, 2018
Aug 16, 2018
192
SDL_HIDAPI_discovery.m_wndClass.lpszClassName = "SDL_HIDAPI_DEVICE_DETECTION";
Aug 16, 2018
Aug 16, 2018
193
194
195
196
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);
Aug 16, 2018
Aug 16, 2018
197
SDL_HIDAPI_discovery.m_hwndMsg = CreateWindowExA(0, "SDL_HIDAPI_DEVICE_DETECTION", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
Aug 16, 2018
Aug 16, 2018
198
Aug 16, 2018
Aug 16, 2018
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{
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 );
}
Aug 16, 2018
Aug 16, 2018
214
215
216
217
218
219
220
221
222
223
#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;
Aug 16, 2018
Aug 16, 2018
224
io_object_t entry;
Aug 16, 2018
Aug 16, 2018
225
226
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 */
Aug 16, 2018
Aug 16, 2018
227
while ((entry = IOIteratorNext(portIterator)) != 0) {
Aug 16, 2018
Aug 16, 2018
228
229
230
231
232
233
234
235
236
237
238
239
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;
Aug 16, 2018
Aug 16, 2018
240
io_object_t entry;
Aug 16, 2018
Aug 16, 2018
241
242
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 */
Aug 16, 2018
Aug 16, 2018
243
while ((entry = IOIteratorNext(portIterator)) != 0) {
Aug 16, 2018
Aug 16, 2018
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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;
Sep 24, 2018
Sep 24, 2018
267
268
269
270
usyms = SDL_UDEV_GetUdevSyms();
if (usyms) {
SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
}
Aug 16, 2018
Aug 16, 2018
271
if (SDL_HIDAPI_discovery.m_pUdev) {
Sep 24, 2018
Sep 24, 2018
272
SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
Aug 16, 2018
Aug 16, 2018
273
if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
Sep 24, 2018
Sep 24, 2018
274
275
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);
Sep 7, 2018
Sep 7, 2018
276
SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
Aug 16, 2018
Aug 16, 2018
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
}
}
#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;
}
Oct 27, 2018
Oct 27, 2018
296
297
#if defined(__WIN32__)
#if 0 /* just let the usual SDL_PumpEvents loop dispatch these, fixing bug 4286. --ryan. */
Aug 16, 2018
Aug 16, 2018
298
299
300
301
302
303
304
305
306
307
308
/* 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
Oct 27, 2018
Oct 27, 2018
309
#endif /* __WIN32__ */
Aug 16, 2018
Aug 16, 2018
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#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;
Sep 7, 2018
Sep 7, 2018
329
struct udev_device *pUdevDevice;
Aug 16, 2018
Aug 16, 2018
330
331
332
333
334
335
336
PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
PollUdev.events = POLLIN;
if (poll(&PollUdev, 1, 0) != 1) {
break;
}
Sep 7, 2018
Sep 7, 2018
337
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
Aug 16, 2018
Aug 16, 2018
338
Sep 24, 2018
Sep 24, 2018
339
pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
Aug 16, 2018
Aug 16, 2018
340
if (pUdevDevice) {
Sep 24, 2018
Sep 24, 2018
341
usyms->udev_device_unref(pUdevDevice);
Aug 16, 2018
Aug 16, 2018
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
}
}
}
#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)
Sep 24, 2018
Sep 24, 2018
369
if (usyms) {
Sep 25, 2018
Sep 25, 2018
370
371
372
373
374
375
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);
}
Sep 24, 2018
Sep 24, 2018
376
377
SDL_UDEV_ReleaseUdevSyms();
usyms = NULL;
Aug 16, 2018
Aug 16, 2018
378
379
380
381
}
#endif
}
Dec 19, 2019
Dec 19, 2019
382
383
384
static void HIDAPI_JoystickDetect(void);
static void HIDAPI_JoystickClose(SDL_Joystick * joystick);
Aug 9, 2018
Aug 9, 2018
385
static SDL_bool
Oct 22, 2019
Oct 22, 2019
386
HIDAPI_IsDeviceSupported(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
Aug 9, 2018
Aug 9, 2018
387
388
389
390
391
{
int i;
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
Oct 22, 2019
Oct 22, 2019
392
if (driver->enabled && driver->IsSupportedDevice(vendor_id, product_id, version, -1, name)) {
Aug 9, 2018
Aug 9, 2018
393
394
395
396
397
398
return SDL_TRUE;
}
}
return SDL_FALSE;
}
399
400
401
static SDL_HIDAPI_DeviceDriver *
HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
{
Sep 1, 2018
Sep 1, 2018
402
403
404
405
const Uint16 USAGE_PAGE_GENERIC_DESKTOP = 0x0001;
const Uint16 USAGE_JOYSTICK = 0x0004;
const Uint16 USAGE_GAMEPAD = 0x0005;
const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
406
407
408
409
410
411
int i;
if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
return NULL;
}
Sep 1, 2018
Sep 1, 2018
412
413
414
415
416
417
418
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;
}
419
420
for (i = 0; i < SDL_arraysize(SDL_HIDAPI_drivers); ++i) {
SDL_HIDAPI_DeviceDriver *driver = SDL_HIDAPI_drivers[i];
Oct 22, 2019
Oct 22, 2019
421
if (driver->enabled && driver->IsSupportedDevice(device->vendor_id, device->product_id, device->version, device->interface_number, device->name)) {
422
423
424
425
426
427
428
return driver;
}
}
return NULL;
}
static SDL_HIDAPI_Device *
Dec 19, 2019
Dec 19, 2019
429
HIDAPI_GetDeviceByIndex(int device_index, SDL_JoystickID *pJoystickID)
430
431
432
433
{
SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
while (device) {
if (device->driver) {
Dec 19, 2019
Dec 19, 2019
434
435
436
437
438
if (device_index < device->num_joysticks) {
if (pJoystickID) {
*pJoystickID = device->joysticks[device_index];
}
return device;
Dec 19, 2019
Dec 19, 2019
440
device_index -= device->num_joysticks;
441
442
443
}
device = device->next;
}
Dec 19, 2019
Dec 19, 2019
444
return NULL;
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
}
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;
}
Dec 19, 2019
Dec 19, 2019
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
static void
HIDAPI_SetupDeviceDriver(SDL_HIDAPI_Device *device)
{
if (device->driver) {
/* Already setup */
return;
}
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);
}
}
/* Initialize the device, which may cause a connected event */
if (device->driver && !device->driver->InitDevice(device)) {
device->driver = NULL;
}
}
static void
HIDAPI_CleanupDeviceDriver(SDL_HIDAPI_Device *device)
{
int i;
if (!device->driver) {
/* Already cleaned up */
return;
}
/* Disconnect any joysticks */
for (i = 0; i < device->num_joysticks; ++i) {
SDL_Joystick *joystick = SDL_JoystickFromInstanceID(device->joysticks[i]);
if (joystick) {
HIDAPI_JoystickClose(joystick);
}
HIDAPI_JoystickDisconnected(device, device->joysticks[i]);
}
device->driver->FreeDevice(device);
device->driver = NULL;
}
507
508
509
510
511
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;
Nov 14, 2019
Nov 14, 2019
512
SDL_bool enabled = SDL_GetStringBoolean(hint, SDL_TRUE);
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
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;
}
}
}
Jul 25, 2019
Jul 25, 2019
529
530
531
532
533
534
535
536
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;
}
}
537
/* Update device list if driver availability changes */
Dec 19, 2019
Dec 19, 2019
538
SDL_LockMutex(SDL_HIDAPI_mutex);
Jun 19, 2019
Jun 19, 2019
539
Dec 19, 2019
Dec 19, 2019
540
541
542
while (device) {
if (device->driver && !device->driver->enabled) {
HIDAPI_CleanupDeviceDriver(device);
Dec 19, 2019
Dec 19, 2019
544
HIDAPI_SetupDeviceDriver(device);
545
546
547
device = device->next;
}
Dec 19, 2019
Dec 19, 2019
548
549
SDL_UnlockMutex(SDL_HIDAPI_mutex);
}
550
551
552
553
554
555
static int
HIDAPI_JoystickInit(void)
{
int i;
Jul 31, 2019
Jul 31, 2019
556
557
558
559
if (initialized) {
return 0;
}
560
561
562
563
564
if (hid_init() < 0) {
SDL_SetError("Couldn't initialize hidapi");
return -1;
}
Dec 19, 2019
Dec 19, 2019
565
566
SDL_HIDAPI_mutex = SDL_CreateMutex();
567
568
569
570
571
572
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);
Aug 16, 2018
Aug 16, 2018
573
HIDAPI_InitializeDiscovery();
574
HIDAPI_JoystickDetect();
Dec 19, 2019
Dec 19, 2019
575
HIDAPI_UpdateDevices();
Jul 31, 2019
Jul 31, 2019
576
577
578
initialized = SDL_TRUE;
Dec 19, 2019
Dec 19, 2019
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
SDL_bool
HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID)
{
SDL_JoystickID joystickID;
SDL_JoystickID *joysticks = (SDL_JoystickID *)SDL_realloc(device->joysticks, (device->num_joysticks + 1)*sizeof(*device->joysticks));
if (!joysticks) {
return SDL_FALSE;
}
joystickID = SDL_GetNextJoystickInstanceID();
device->joysticks = joysticks;
device->joysticks[device->num_joysticks++] = joystickID;
++SDL_HIDAPI_numjoysticks;
SDL_PrivateJoystickAdded(joystickID);
if (pJoystickID) {
*pJoystickID = joystickID;
}
return SDL_TRUE;
}
void
HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID)
{
int i;
for (i = 0; i < device->num_joysticks; ++i) {
if (device->joysticks[i] == joystickID) {
SDL_memcpy(&device->joysticks[i], &device->joysticks[i+1], device->num_joysticks - i - 1);
--device->num_joysticks;
--SDL_HIDAPI_numjoysticks;
if (!shutting_down) {
SDL_PrivateJoystickRemoved(joystickID);
}
return;
}
}
}
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
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;
}
Dec 19, 2019
Dec 19, 2019
643
644
645
646
647
device->path = SDL_strdup(info->path);
if (!device->path) {
SDL_free(device);
return;
}
648
649
650
device->seen = SDL_TRUE;
device->vendor_id = info->vendor_id;
device->product_id = info->product_id;
Aug 30, 2018
Aug 30, 2018
651
device->version = info->release_number;
652
653
654
655
656
657
658
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;
Aug 16, 2018
Aug 16, 2018
659
const Uint16 version = device->version;
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
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;
}
Sep 14, 2018
Sep 14, 2018
676
/* Need the device name before getting the driver to know whether to ignore this device */
Dec 12, 2019
Dec 12, 2019
677
678
679
680
681
682
if (!device->name) {
const char *name = SDL_GetCustomJoystickName(device->vendor_id, device->product_id);
if (name) {
device->name = SDL_strdup(name);
}
}
683
if (!device->name && info->manufacturer_string && info->product_string) {
Dec 14, 2019
Dec 14, 2019
684
const char *manufacturer_remapped;
685
686
687
688
689
690
691
692
693
694
695
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));
}
}
Dec 12, 2019
Dec 12, 2019
696
Dec 14, 2019
Dec 14, 2019
697
698
699
700
701
manufacturer_remapped = SDL_GetCustomJoystickManufacturer(manufacturer_string);
if (manufacturer_remapped != manufacturer_string) {
SDL_free(manufacturer_string);
manufacturer_string = SDL_strdup(manufacturer_remapped);
}
Dec 12, 2019
Dec 12, 2019
702
Dec 14, 2019
Dec 14, 2019
703
704
if (manufacturer_string && product_string) {
size_t name_size = (SDL_strlen(manufacturer_string) + 1 + SDL_strlen(product_string) + 1);
705
706
device->name = (char *)SDL_malloc(name_size);
if (device->name) {
Dec 10, 2019
Dec 10, 2019
707
708
709
710
711
if (SDL_strncasecmp(manufacturer_string, product_string, SDL_strlen(manufacturer_string)) == 0) {
SDL_strlcpy(device->name, product_string, name_size);
} else {
SDL_snprintf(device->name, name_size, "%s %s", manufacturer_string, product_string);
}
712
713
714
715
716
717
718
719
720
721
722
723
724
}
}
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) {
Dec 19, 2019
Dec 19, 2019
725
SDL_free(device->path);
726
727
728
729
730
731
732
733
734
735
736
737
738
SDL_free(device);
return;
}
SDL_snprintf(device->name, name_size, "0x%.4x/0x%.4x", info->vendor_id, info->product_id);
}
/* Add it to the list */
if (last) {
last->next = device;
} else {
SDL_HIDAPI_devices = device;
}
Dec 19, 2019
Dec 19, 2019
739
740
741
#ifdef DEBUG_HIDAPI
SDL_Log("Added 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");
#endif
Jun 19, 2019
Jun 19, 2019
742
Dec 19, 2019
Dec 19, 2019
743
HIDAPI_SetupDeviceDriver(device);
Dec 19, 2019
Dec 19, 2019
748
HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
749
750
751
752
753
754
755
756
757
758
{
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;
}
Dec 19, 2019
Dec 19, 2019
759
HIDAPI_CleanupDeviceDriver(device);
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
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;
Dec 19, 2019
Dec 19, 2019
775
776
SDL_LockMutex(SDL_HIDAPI_mutex);
777
778
779
780
781
782
783
784
/* Prepare the existing device list */
device = SDL_HIDAPI_devices;
while (device) {
device->seen = SDL_FALSE;
device = device->next;
}
/* Enumerate the devices */
Jul 25, 2019
Jul 25, 2019
785
786
787
788
789
790
791
792
793
794
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);
}
Jul 25, 2019
Jul 25, 2019
796
hid_free_enumeration(devs);
797
798
799
800
801
802
803
804
805
}
}
/* Remove any devices that weren't seen */
device = SDL_HIDAPI_devices;
while (device) {
SDL_HIDAPI_Device *next = device->next;
if (!device->seen) {
Dec 19, 2019
Dec 19, 2019
806
HIDAPI_DelDevice(device);
807
808
809
}
device = next;
}
Dec 19, 2019
Dec 19, 2019
810
811
SDL_UnlockMutex(SDL_HIDAPI_mutex);
Oct 22, 2019
Oct 22, 2019
815
HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
816
817
818
{
SDL_HIDAPI_Device *device;
Jul 31, 2019
Jul 31, 2019
819
820
821
822
823
/* Make sure we're initialized, as this could be called from other drivers during startup */
if (HIDAPI_JoystickInit() < 0) {
return SDL_FALSE;
}
Aug 9, 2018
Aug 9, 2018
824
/* Don't update the device list for devices we know aren't supported */
Oct 22, 2019
Oct 22, 2019
825
if (!HIDAPI_IsDeviceSupported(vendor_id, product_id, version, name)) {
Aug 9, 2018
Aug 9, 2018
826
827
828
return SDL_FALSE;
}
829
830
831
/* Make sure the device list is completely up to date when we check for device presence */
HIDAPI_UpdateDeviceList();
Oct 22, 2019
Oct 22, 2019
832
833
834
835
/* 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.
*/
836
837
838
839
840
841
842
843
844
845
846
847
848
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)
{
Aug 16, 2018
Aug 16, 2018
849
850
851
HIDAPI_UpdateDiscovery();
if (SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
/* FIXME: We probably need to schedule an update in a few seconds as well */
852
HIDAPI_UpdateDeviceList();
Aug 16, 2018
Aug 16, 2018
853
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_FALSE;
Dec 19, 2019
Dec 19, 2019
855
856
857
858
859
860
}
void
HIDAPI_UpdateDevices(void)
{
SDL_HIDAPI_Device *device;
Dec 19, 2019
Dec 19, 2019
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/* Update the devices, which may change connected joysticks and send events */
SDL_LockMutex(SDL_HIDAPI_mutex);
/* Prepare the existing device list */
device = SDL_HIDAPI_devices;
while (device) {
if (device->driver) {
device->driver->UpdateDevice(device);
}
device = device->next;
}
SDL_UnlockMutex(SDL_HIDAPI_mutex);
875
876
877
878
879
}
static const char *
HIDAPI_JoystickGetDeviceName(int device_index)
{
Dec 19, 2019
Dec 19, 2019
880
881
882
883
884
885
886
887
888
889
890
891
SDL_HIDAPI_Device *device;
const char *name = NULL;
SDL_LockMutex(SDL_HIDAPI_mutex);
device = HIDAPI_GetDeviceByIndex(device_index, NULL);
if (device) {
/* FIXME: The device could be freed after this name is returned... */
name = device->name;
}
SDL_UnlockMutex(SDL_HIDAPI_mutex);
return name;
Oct 25, 2018
Oct 25, 2018
894
895
896
static int
HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
{
Jun 19, 2019
Jun 19, 2019
897
return -1;
Oct 25, 2018
Oct 25, 2018
898
899
}
900
901
902
static SDL_JoystickGUID
HIDAPI_JoystickGetDeviceGUID(int device_index)
{
Dec 19, 2019
Dec 19, 2019
903
904
905
906
907
908
909
910
911
912
913
914
915
SDL_HIDAPI_Device *device;
SDL_JoystickGUID guid;
SDL_LockMutex(SDL_HIDAPI_mutex);
device = HIDAPI_GetDeviceByIndex(device_index, NULL);
if (device) {
SDL_memcpy(&guid, &device->guid, sizeof(guid));
} else {
SDL_zero(guid);
}
SDL_UnlockMutex(SDL_HIDAPI_mutex);
return guid;
916
917
918
919
920
}
static SDL_JoystickID
HIDAPI_JoystickGetDeviceInstanceID(int device_index)
{
Dec 19, 2019
Dec 19, 2019
921
922
923
924
925
SDL_JoystickID joystickID;
SDL_LockMutex(SDL_HIDAPI_mutex);
HIDAPI_GetDeviceByIndex(device_index, &joystickID);
SDL_UnlockMutex(SDL_HIDAPI_mutex);
return joystickID;
926
927
928
929
930
}
static int
HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
Dec 19, 2019
Dec 19, 2019
931
932
SDL_JoystickID joystickID;
SDL_HIDAPI_Device *device = HIDAPI_GetDeviceByIndex(device_index, &joystickID);
Jun 19, 2019
Jun 19, 2019
933
struct joystick_hwdata *hwdata;
Jun 19, 2019
Jun 19, 2019
935
936
937
938
hwdata = (struct joystick_hwdata *)SDL_calloc(1, sizeof(*hwdata));
if (!hwdata) {
return SDL_OutOfMemory();
}
Dec 19, 2019
Dec 19, 2019
939
hwdata->device = device;
Jun 19, 2019
Jun 19, 2019
940
Dec 19, 2019
Dec 19, 2019
941
if (!device->driver->OpenJoystick(device, joystick)) {
Jun 19, 2019
Jun 19, 2019
942
SDL_free(hwdata);
Jun 19, 2019
Jun 19, 2019
946
joystick->hwdata = hwdata;
947
948
949
950
951
952
return 0;
}
static int
HIDAPI_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
Oct 3, 2018
Oct 3, 2018
953
954
int result;
Dec 19, 2019
Dec 19, 2019
955
956
957
958
959
960
961
962
963
964
965
SDL_LockMutex(SDL_HIDAPI_mutex);
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
result = device->driver->RumbleJoystick(device, joystick, low_frequency_rumble, high_frequency_rumble, duration_ms);
} else {
SDL_SetError("Rumble failed, device disconnected");
result = -1;
}
SDL_UnlockMutex(SDL_HIDAPI_mutex);
Oct 3, 2018
Oct 3, 2018
966
return result;
967
968
969
970
971
}
static void
HIDAPI_JoystickUpdate(SDL_Joystick * joystick)
{
Dec 19, 2019
Dec 19, 2019
972
/* This is handled in SDL_HIDAPI_UpdateDevices() */
973
974
975
976
977
}
static void
HIDAPI_JoystickClose(SDL_Joystick * joystick)
{
Dec 19, 2019
Dec 19, 2019
978
979
980
SDL_LockMutex(SDL_HIDAPI_mutex);
if (joystick->hwdata) {
SDL_HIDAPI_Device *device = joystick->hwdata->device;
Jun 19, 2019
Jun 19, 2019
981
Dec 19, 2019
Dec 19, 2019
982
983
984
985
986
987
device->driver->CloseJoystick(device, joystick);
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
}
SDL_UnlockMutex(SDL_HIDAPI_mutex);
988
989
990
991
992
993
994
}
static void
HIDAPI_JoystickQuit(void)
{
int i;
Dec 19, 2019
Dec 19, 2019
995
996
shutting_down = SDL_TRUE;
Aug 16, 2018
Aug 16, 2018
997
998
HIDAPI_ShutdownDiscovery();
999
while (SDL_HIDAPI_devices) {
Dec 19, 2019
Dec 19, 2019
1000
HIDAPI_DelDevice(SDL_HIDAPI_devices);