Skip to content

Latest commit

 

History

History
1080 lines (930 loc) · 33.2 KB

SDL_sysjoystick.c

File metadata and controls

1080 lines (930 loc) · 33.2 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 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_IOKIT
Aug 9, 2018
Aug 9, 2018
25
#include "SDL_events.h"
26
27
28
29
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#include "SDL_sysjoystick_c.h"
Aug 9, 2018
Aug 9, 2018
30
#include "../hidapi/SDL_hidapijoystick_c.h"
31
32
#include "../../haptic/darwin/SDL_syshaptic_c.h" /* For haptic hot plugging */
Aug 9, 2018
Aug 9, 2018
33
34
35
#define SDL_JOYSTICK_RUNLOOP_MODE CFSTR("SDLJoystick")
Aug 9, 2018
Aug 9, 2018
36
37
#define CONVERT_MAGNITUDE(x) (((x)*10000) / 0x7FFF)
38
39
40
41
42
43
/* The base object of the HID Manager API */
static IOHIDManagerRef hidman = NULL;
/* Linked list of all available devices */
static recDevice *gpDeviceList = NULL;
Aug 9, 2018
Aug 9, 2018
44
45
46
47
48
49
50
51
52
53
54
void FreeRumbleEffectData(FFEFFECT *effect)
{
if (!effect) {
return;
}
SDL_free(effect->rgdwAxes);
SDL_free(effect->rglDirection);
SDL_free(effect->lpvTypeSpecificParams);
SDL_free(effect);
}
Feb 4, 2020
Feb 4, 2020
55
FFEFFECT *CreateRumbleEffectData(Sint16 magnitude)
Aug 9, 2018
Aug 9, 2018
56
57
58
59
60
61
62
63
64
65
66
67
{
FFEFFECT *effect;
FFPERIODIC *periodic;
/* Create the effect */
effect = (FFEFFECT *)SDL_calloc(1, sizeof(*effect));
if (!effect) {
return NULL;
}
effect->dwSize = sizeof(*effect);
effect->dwGain = 10000;
effect->dwFlags = FFEFF_OBJECTOFFSETS;
Feb 4, 2020
Feb 4, 2020
68
effect->dwDuration = SDL_MAX_RUMBLE_DURATION_MS * 1000; /* In microseconds. */
Aug 9, 2018
Aug 9, 2018
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
effect->dwTriggerButton = FFEB_NOTRIGGER;
effect->cAxes = 2;
effect->rgdwAxes = (DWORD *)SDL_calloc(effect->cAxes, sizeof(DWORD));
if (!effect->rgdwAxes) {
FreeRumbleEffectData(effect);
return NULL;
}
effect->rglDirection = (LONG *)SDL_calloc(effect->cAxes, sizeof(LONG));
if (!effect->rglDirection) {
FreeRumbleEffectData(effect);
return NULL;
}
effect->dwFlags |= FFEFF_CARTESIAN;
periodic = (FFPERIODIC *)SDL_calloc(1, sizeof(*periodic));
if (!periodic) {
FreeRumbleEffectData(effect);
return NULL;
}
periodic->dwMagnitude = CONVERT_MAGNITUDE(magnitude);
periodic->dwPeriod = 1000000;
effect->cbTypeSpecificParams = sizeof(*periodic);
effect->lpvTypeSpecificParams = periodic;
return effect;
}
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
static recDevice *GetDeviceForIndex(int device_index)
{
recDevice *device = gpDeviceList;
while (device) {
if (!device->removed) {
if (device_index == 0)
break;
--device_index;
}
device = device->pNext;
}
return device;
}
static void
FreeElementList(recElement *pElement)
{
while (pElement) {
recElement *pElementNext = pElement->pNext;
SDL_free(pElement);
pElement = pElementNext;
}
}
static recDevice *
FreeDevice(recDevice *removeDevice)
{
recDevice *pDeviceNext = NULL;
if (removeDevice) {
if (removeDevice->deviceRef) {
Mar 17, 2020
Mar 17, 2020
130
131
132
133
134
135
136
137
138
if (removeDevice->runLoopAttached) {
/* Calling IOHIDDeviceUnscheduleFromRunLoop without a prior,
* paired call to IOHIDDeviceScheduleWithRunLoop can lead
* to crashes in MacOS 10.14.x and earlier. This doesn't
* appear to be a problem in MacOS 10.15.x, but we'll
* do it anyways. (Part-of fix for Bug 5034)
*/
IOHIDDeviceUnscheduleFromRunLoop(removeDevice->deviceRef, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE);
}
Jan 30, 2020
Jan 30, 2020
139
CFRelease(removeDevice->deviceRef);
140
141
142
removeDevice->deviceRef = NULL;
}
Mar 17, 2020
Mar 17, 2020
143
144
145
146
147
148
149
150
151
/* clear out any reference to removeDevice from an associated,
* live instance of SDL_Joystick (Part-of fix for Bug 5034)
*/
SDL_LockJoysticks();
if (removeDevice->joystick) {
removeDevice->joystick->hwdata = NULL;
}
SDL_UnlockJoysticks();
152
153
154
155
156
/* save next device prior to disposing of this device */
pDeviceNext = removeDevice->pNext;
if ( gpDeviceList == removeDevice ) {
gpDeviceList = pDeviceNext;
Feb 5, 2020
Feb 5, 2020
157
} else if (gpDeviceList) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
recDevice *device = gpDeviceList;
while (device->pNext != removeDevice) {
device = device->pNext;
}
device->pNext = pDeviceNext;
}
removeDevice->pNext = NULL;
/* free element lists */
FreeElementList(removeDevice->firstAxis);
FreeElementList(removeDevice->firstButton);
FreeElementList(removeDevice->firstHat);
SDL_free(removeDevice);
}
return pDeviceNext;
}
Feb 5, 2018
Feb 5, 2018
176
177
static SDL_bool
GetHIDElementState(recDevice *pDevice, recElement *pElement, SInt32 *pValue)
178
179
{
SInt32 value = 0;
Feb 5, 2018
Feb 5, 2018
180
int returnValue = SDL_FALSE;
Jan 27, 2020
Jan 27, 2020
182
if (pDevice && pDevice->deviceRef && pElement) {
183
184
185
186
187
188
189
190
191
192
193
IOHIDValueRef valueRef;
if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) {
value = (SInt32) IOHIDValueGetIntegerValue(valueRef);
/* record min and max for auto calibration */
if (value < pElement->minReport) {
pElement->minReport = value;
}
if (value > pElement->maxReport) {
pElement->maxReport = value;
}
Feb 5, 2018
Feb 5, 2018
194
195
196
*pValue = value;
returnValue = SDL_TRUE;
Feb 5, 2018
Feb 5, 2018
199
return returnValue;
Feb 5, 2018
Feb 5, 2018
202
203
static SDL_bool
GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max, SInt32 *pValue)
204
205
206
{
const float deviceScale = max - min;
const float readScale = pElement->maxReport - pElement->minReport;
Feb 5, 2018
Feb 5, 2018
207
208
209
210
211
212
213
214
215
216
217
218
219
int returnValue = SDL_FALSE;
if (GetHIDElementState(pDevice, pElement, pValue))
{
if (readScale == 0) {
returnValue = SDL_TRUE; /* no scaling at all */
}
else
{
*pValue = ((*pValue - pElement->minReport) * deviceScale / readScale) + min;
returnValue = SDL_TRUE;
}
}
return returnValue;
220
221
222
223
224
225
226
}
static void
JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
{
recDevice *device = (recDevice *) ctx;
device->removed = SDL_TRUE;
Jan 30, 2020
Jan 30, 2020
227
228
229
230
231
if (device->deviceRef) {
// deviceRef was invalidated due to the remove
CFRelease(device->deviceRef);
device->deviceRef = NULL;
}
Aug 9, 2018
Aug 9, 2018
232
233
234
235
236
237
238
239
240
241
242
243
244
if (device->ffeffect_ref) {
FFDeviceReleaseEffect(device->ffdevice, device->ffeffect_ref);
device->ffeffect_ref = NULL;
}
if (device->ffeffect) {
FreeRumbleEffectData(device->ffeffect);
device->ffeffect = NULL;
}
if (device->ffdevice) {
FFReleaseDevice(device->ffdevice);
device->ffdevice = NULL;
device->ff_initialized = SDL_FALSE;
}
245
246
247
248
#if SDL_HAPTIC_IOKIT
MacHaptic_MaybeRemoveDevice(device->ffservice);
#endif
Aug 26, 2016
Aug 26, 2016
249
SDL_PrivateJoystickRemoved(device->instance_id);
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
}
static void AddHIDElement(const void *value, void *parameter);
/* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */
static void
AddHIDElements(CFArrayRef array, recDevice *pDevice)
{
const CFRange range = { 0, CFArrayGetCount(array) };
CFArrayApplyFunction(array, range, AddHIDElement, pDevice);
}
static SDL_bool
ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) {
while (listitem) {
if (listitem->cookie == cookie) {
return SDL_TRUE;
}
listitem = listitem->pNext;
}
return SDL_FALSE;
}
/* See if we care about this HID element, and if so, note it in our recDevice. */
static void
AddHIDElement(const void *value, void *parameter)
{
recDevice *pDevice = (recDevice *) parameter;
IOHIDElementRef refElement = (IOHIDElementRef) value;
const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement);
const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
const uint32_t usage = IOHIDElementGetUsage(refElement);
recElement *element = NULL;
recElement **headElement = NULL;
/* look at types of interest */
switch (IOHIDElementGetType(refElement)) {
case kIOHIDElementTypeInput_Misc:
case kIOHIDElementTypeInput_Button:
case kIOHIDElementTypeInput_Axis: {
switch (usagePage) { /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
case kHIDPage_GenericDesktop:
switch (usage) {
case kHIDUsage_GD_X:
case kHIDUsage_GD_Y:
case kHIDUsage_GD_Z:
case kHIDUsage_GD_Rx:
case kHIDUsage_GD_Ry:
case kHIDUsage_GD_Rz:
case kHIDUsage_GD_Slider:
case kHIDUsage_GD_Dial:
case kHIDUsage_GD_Wheel:
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->axes++;
headElement = &(pDevice->firstAxis);
}
}
break;
case kHIDUsage_GD_Hatswitch:
if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->hats++;
headElement = &(pDevice->firstHat);
}
}
break;
Nov 13, 2015
Nov 13, 2015
324
325
326
327
case kHIDUsage_GD_DPadUp:
case kHIDUsage_GD_DPadDown:
case kHIDUsage_GD_DPadRight:
case kHIDUsage_GD_DPadLeft:
Dec 28, 2015
Dec 28, 2015
328
329
case kHIDUsage_GD_Start:
case kHIDUsage_GD_Select:
Aug 8, 2016
Aug 8, 2016
330
case kHIDUsage_GD_SystemMainMenu:
Nov 13, 2015
Nov 13, 2015
331
332
333
334
335
336
337
338
if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->buttons++;
headElement = &(pDevice->firstButton);
}
}
break;
339
340
341
342
343
344
345
}
break;
case kHIDPage_Simulation:
switch (usage) {
case kHIDUsage_Sim_Rudder:
case kHIDUsage_Sim_Throttle:
Jan 26, 2017
Jan 26, 2017
346
347
case kHIDUsage_Sim_Accelerator:
case kHIDUsage_Sim_Brake:
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->axes++;
headElement = &(pDevice->firstAxis);
}
}
break;
default:
break;
}
break;
case kHIDPage_Button:
Nov 13, 2015
Nov 13, 2015
363
case kHIDPage_Consumer: /* e.g. 'pause' button on Steelseries MFi gamepads. */
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
if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->buttons++;
headElement = &(pDevice->firstButton);
}
}
break;
default:
break;
}
}
break;
case kIOHIDElementTypeCollection: {
CFArrayRef array = IOHIDElementGetChildren(refElement);
if (array) {
AddHIDElements(array, pDevice);
}
}
break;
default:
break;
}
if (element && headElement) { /* add to list */
recElement *elementPrevious = NULL;
recElement *elementCurrent = *headElement;
while (elementCurrent && usage >= elementCurrent->usage) {
elementPrevious = elementCurrent;
elementCurrent = elementCurrent->pNext;
}
if (elementPrevious) {
elementPrevious->pNext = element;
} else {
*headElement = element;
}
element->elementRef = refElement;
element->usagePage = usagePage;
element->usage = usage;
element->pNext = elementCurrent;
element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement);
element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
element->cookie = IOHIDElementGetCookie(refElement);
pDevice->elements++;
}
}
}
Mar 17, 2020
Mar 17, 2020
418
419
420
421
static SDL_bool
GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
{
Nov 11, 2016
Nov 11, 2016
422
423
424
Sint32 vendor = 0;
Sint32 product = 0;
Sint32 version = 0;
Mar 13, 2020
Mar 13, 2020
425
char *name;
Dec 10, 2019
Dec 10, 2019
426
427
char manufacturer_string[256];
char product_string[256];
428
429
CFTypeRef refCF = NULL;
CFArrayRef array = NULL;
Nov 11, 2016
Nov 11, 2016
430
Uint16 *guid16 = (Uint16 *)pDevice->guid.data;
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* get usage page and usage */
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsagePageKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage);
}
if (pDevice->usagePage != kHIDPage_GenericDesktop) {
return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
}
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage);
}
if ((pDevice->usage != kHIDUsage_GD_Joystick &&
pDevice->usage != kHIDUsage_GD_GamePad &&
pDevice->usage != kHIDUsage_GD_MultiAxisController)) {
return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
}
Jan 30, 2020
Jan 30, 2020
452
453
454
455
456
457
458
459
460
/* Make sure we retain the use of the IOKit-provided device-object,
lest the device get disconnected and we try to use it. (Fixes
SDL-Bugzilla #4961, aka. https://bugzilla.libsdl.org/show_bug.cgi?id=4961 )
*/
CFRetain(hidDevice);
/* Now that we've CFRetain'ed the device-object (for our use), we'll
save the reference to it.
*/
461
462
463
464
pDevice->deviceRef = hidDevice;
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
if (refCF) {
Nov 11, 2016
Nov 11, 2016
465
CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor);
466
467
468
469
}
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey));
if (refCF) {
Nov 11, 2016
Nov 11, 2016
470
CFNumberGetValue(refCF, kCFNumberSInt32Type, &product);
Nov 11, 2016
Nov 11, 2016
473
474
475
476
477
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVersionNumberKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &version);
}
Dec 12, 2019
Dec 12, 2019
478
/* get device name */
Mar 13, 2020
Mar 13, 2020
479
480
481
482
483
484
485
486
487
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
if ((!refCF) || (!CFStringGetCString(refCF, manufacturer_string, sizeof(manufacturer_string), kCFStringEncodingUTF8))) {
manufacturer_string[0] = '\0';
}
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
if ((!refCF) || (!CFStringGetCString(refCF, product_string, sizeof(product_string), kCFStringEncodingUTF8))) {
product_string[0] = '\0';
}
name = SDL_CreateJoystickName(vendor, product, manufacturer_string, product_string);
Dec 12, 2019
Dec 12, 2019
488
489
if (name) {
SDL_strlcpy(pDevice->product, name, sizeof(pDevice->product));
Mar 13, 2020
Mar 13, 2020
490
SDL_free(name);
Dec 12, 2019
Dec 12, 2019
491
492
}
Aug 9, 2018
Aug 9, 2018
493
#ifdef SDL_JOYSTICK_HIDAPI
Oct 22, 2019
Oct 22, 2019
494
if (HIDAPI_IsDevicePresent(vendor, product, version, pDevice->product)) {
Aug 9, 2018
Aug 9, 2018
495
496
497
498
499
/* The HIDAPI driver is taking care of this device */
return 0;
}
#endif
Nov 11, 2016
Nov 11, 2016
500
SDL_memset(pDevice->guid.data, 0, sizeof(pDevice->guid.data));
Nov 11, 2016
Nov 11, 2016
501
502
if (vendor && product) {
Aug 9, 2018
Aug 9, 2018
503
*guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
Nov 11, 2016
Nov 11, 2016
504
505
506
507
508
509
510
511
*guid16++ = 0;
*guid16++ = SDL_SwapLE16((Uint16)vendor);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16((Uint16)product);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16((Uint16)version);
*guid16++ = 0;
} else {
Aug 9, 2018
Aug 9, 2018
512
*guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_BLUETOOTH);
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
*guid16++ = 0;
SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4);
}
array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone);
if (array) {
AddHIDElements(array, pDevice);
CFRelease(array);
}
return SDL_TRUE;
}
static SDL_bool
JoystickAlreadyKnown(IOHIDDeviceRef ioHIDDeviceObject)
{
recDevice *i;
for (i = gpDeviceList; i != NULL; i = i->pNext) {
if (i->deviceRef == ioHIDDeviceObject) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
static void
JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
{
recDevice *device;
int device_index = 0;
May 21, 2016
May 21, 2016
544
io_service_t ioservice;
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
if (res != kIOReturnSuccess) {
return;
}
if (JoystickAlreadyKnown(ioHIDDeviceObject)) {
return; /* IOKit sent us a duplicate. */
}
device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
if (!device) {
SDL_OutOfMemory();
return;
}
if (!GetDeviceInfo(ioHIDDeviceObject, device)) {
Jan 30, 2020
Jan 30, 2020
561
FreeDevice(device);
562
563
564
return; /* not a device we care about, probably. */
}
Aug 9, 2018
Aug 9, 2018
565
if (SDL_ShouldIgnoreJoystick(device->product, device->guid)) {
Jan 30, 2020
Jan 30, 2020
566
FreeDevice(device);
Aug 9, 2017
Aug 9, 2017
567
568
569
return;
}
570
571
572
/* Get notified when this device is disconnected. */
IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device);
IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE);
Mar 17, 2020
Mar 17, 2020
573
device->runLoopAttached = SDL_TRUE;
574
575
/* Allocate an instance ID for this device */
Aug 9, 2018
Aug 9, 2018
576
device->instance_id = SDL_GetNextJoystickInstanceID();
577
578
/* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */
May 21, 2016
May 21, 2016
579
580
581
ioservice = IOHIDDeviceGetService(ioHIDDeviceObject);
if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) {
device->ffservice = ioservice;
Aug 9, 2018
Aug 9, 2018
582
#if SDL_HAPTIC_IOKIT
May 21, 2016
May 21, 2016
583
MacHaptic_MaybeAddDevice(ioservice);
Aug 9, 2018
Aug 9, 2018
585
}
586
587
588
589
590
591
592
593
594
595
596
597
598
/* Add device to the end of the list */
if ( !gpDeviceList ) {
gpDeviceList = device;
} else {
recDevice *curdevice;
curdevice = gpDeviceList;
while ( curdevice->pNext ) {
++device_index;
curdevice = curdevice->pNext;
}
curdevice->pNext = device;
Sep 13, 2015
Sep 13, 2015
599
++device_index; /* bump by one since we counted by pNext. */
Aug 9, 2018
Aug 9, 2018
602
SDL_PrivateJoystickAdded(device->instance_id);
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
}
static SDL_bool
ConfigHIDManager(CFArrayRef matchingArray)
{
CFRunLoopRef runloop = CFRunLoopGetCurrent();
if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
return SDL_FALSE;
}
IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray);
IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL);
IOHIDManagerScheduleWithRunLoop(hidman, runloop, SDL_JOYSTICK_RUNLOOP_MODE);
while (CFRunLoopRunInMode(SDL_JOYSTICK_RUNLOOP_MODE,0,TRUE) == kCFRunLoopRunHandledSource) {
/* no-op. Callback fires once per existing device. */
}
/* future hotplug events will come through SDL_JOYSTICK_RUNLOOP_MODE now. */
return SDL_TRUE; /* good to go. */
}
static CFDictionaryRef
CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay)
{
CFDictionaryRef retval = NULL;
CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
const void *keys[2] = { (void *) CFSTR(kIOHIDDeviceUsagePageKey), (void *) CFSTR(kIOHIDDeviceUsageKey) };
const void *vals[2] = { (void *) pageNumRef, (void *) usageNumRef };
if (pageNumRef && usageNumRef) {
retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
if (pageNumRef) {
CFRelease(pageNumRef);
}
if (usageNumRef) {
CFRelease(usageNumRef);
}
if (!retval) {
*okay = 0;
}
return retval;
}
static SDL_bool
CreateHIDManager(void)
{
SDL_bool retval = SDL_FALSE;
int okay = 1;
const void *vals[] = {
(void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
(void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
(void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
};
const size_t numElements = SDL_arraysize(vals);
CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, numElements, &kCFTypeArrayCallBacks) : NULL;
size_t i;
for (i = 0; i < numElements; i++) {
if (vals[i]) {
CFRelease((CFTypeRef) vals[i]);
}
}
if (array) {
hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (hidman != NULL) {
retval = ConfigHIDManager(array);
}
CFRelease(array);
}
return retval;
}
Aug 9, 2018
Aug 9, 2018
687
688
static int
DARWIN_JoystickInit(void)
689
690
691
692
693
694
695
696
697
{
if (gpDeviceList) {
return SDL_SetError("Joystick: Device list already inited.");
}
if (!CreateHIDManager()) {
return SDL_SetError("Joystick: Couldn't initialize HID Manager");
}
Aug 9, 2018
Aug 9, 2018
698
return 0;
Aug 9, 2018
Aug 9, 2018
701
702
static int
DARWIN_JoystickGetCount(void)
703
704
705
706
707
708
709
710
711
712
713
714
715
716
{
recDevice *device = gpDeviceList;
int nJoySticks = 0;
while (device) {
if (!device->removed) {
nJoySticks++;
}
device = device->pNext;
}
return nJoySticks;
}
Aug 9, 2018
Aug 9, 2018
717
718
static void
DARWIN_JoystickDetect(void)
719
720
721
722
723
724
725
726
727
728
{
recDevice *device = gpDeviceList;
while (device) {
if (device->removed) {
device = FreeDevice(device);
} else {
device = device->pNext;
}
}
Aug 9, 2018
Aug 9, 2018
729
730
731
732
733
/* run this after the checks above so we don't set device->removed and delete the device before
DARWIN_JoystickUpdate can run to clean up the SDL_Joystick object that owns this device */
while (CFRunLoopRunInMode(SDL_JOYSTICK_RUNLOOP_MODE,0,TRUE) == kCFRunLoopRunHandledSource) {
/* no-op. Pending callbacks will fire in CFRunLoopRunInMode(). */
}
734
735
736
737
}
/* Function to get the device-dependent name of a joystick */
const char *
Aug 9, 2018
Aug 9, 2018
738
DARWIN_JoystickGetDeviceName(int device_index)
739
740
741
742
743
{
recDevice *device = GetDeviceForIndex(device_index);
return device ? device->product : "UNKNOWN";
}
Oct 25, 2018
Oct 25, 2018
744
745
746
747
748
749
static int
DARWIN_JoystickGetDevicePlayerIndex(int device_index)
{
return -1;
}
Dec 21, 2019
Dec 21, 2019
750
751
752
753
754
static void
DARWIN_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
}
Aug 9, 2018
Aug 9, 2018
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
static SDL_JoystickGUID
DARWIN_JoystickGetDeviceGUID( int device_index )
{
recDevice *device = GetDeviceForIndex(device_index);
SDL_JoystickGUID guid;
if (device) {
guid = device->guid;
} else {
SDL_zero(guid);
}
return guid;
}
static SDL_JoystickID
DARWIN_JoystickGetDeviceInstanceID(int device_index)
770
771
772
773
774
{
recDevice *device = GetDeviceForIndex(device_index);
return device ? device->instance_id : 0;
}
Aug 9, 2018
Aug 9, 2018
775
776
static int
DARWIN_JoystickOpen(SDL_Joystick * joystick, int device_index)
777
778
779
780
781
{
recDevice *device = GetDeviceForIndex(device_index);
joystick->instance_id = device->instance_id;
joystick->hwdata = device;
Mar 17, 2020
Mar 17, 2020
782
device->joystick = joystick;
783
784
785
786
787
788
789
790
791
joystick->name = device->product;
joystick->naxes = device->axes;
joystick->nhats = device->hats;
joystick->nballs = 0;
joystick->nbuttons = device->buttons;
return 0;
}
Aug 9, 2018
Aug 9, 2018
792
793
/*
* Like strerror but for force feedback errors.
Aug 9, 2018
Aug 9, 2018
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
static const char *
FFStrError(unsigned int err)
{
switch (err) {
case FFERR_DEVICEFULL:
return "device full";
/* This should be valid, but for some reason isn't defined... */
/* case FFERR_DEVICENOTREG:
return "device not registered"; */
case FFERR_DEVICEPAUSED:
return "device paused";
case FFERR_DEVICERELEASED:
return "device released";
case FFERR_EFFECTPLAYING:
return "effect playing";
case FFERR_EFFECTTYPEMISMATCH:
return "effect type mismatch";
case FFERR_EFFECTTYPENOTSUPPORTED:
return "effect type not supported";
case FFERR_GENERIC:
return "undetermined error";
case FFERR_HASEFFECTS:
return "device has effects";
case FFERR_INCOMPLETEEFFECT:
return "incomplete effect";
case FFERR_INTERNAL:
return "internal fault";
case FFERR_INVALIDDOWNLOADID:
return "invalid download id";
case FFERR_INVALIDPARAM:
return "invalid parameter";
case FFERR_MOREDATA:
return "more data";
case FFERR_NOINTERFACE:
return "interface not supported";
case FFERR_NOTDOWNLOADED:
return "effect is not downloaded";
case FFERR_NOTINITIALIZED:
return "object has not been initialized";
case FFERR_OUTOFMEMORY:
return "out of memory";
case FFERR_UNPLUGGED:
return "device is unplugged";
case FFERR_UNSUPPORTED:
return "function call unsupported";
case FFERR_UNSUPPORTEDAXIS:
return "axis unsupported";
default:
return "unknown error";
}
}
static int
Feb 4, 2020
Feb 4, 2020
849
DARWIN_JoystickInitRumble(recDevice *device, Sint16 magnitude)
Aug 9, 2018
Aug 9, 2018
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
{
HRESULT result;
if (!device->ffdevice) {
result = FFCreateDevice(device->ffservice, &device->ffdevice);
if (result != FF_OK) {
return SDL_SetError("Unable to create force feedback device from service: %s", FFStrError(result));
}
}
/* Reset and then enable actuators */
result = FFDeviceSendForceFeedbackCommand(device->ffdevice, FFSFFC_RESET);
if (result != FF_OK) {
return SDL_SetError("Unable to reset force feedback device: %s", FFStrError(result));
}
result = FFDeviceSendForceFeedbackCommand(device->ffdevice, FFSFFC_SETACTUATORSON);
if (result != FF_OK) {
return SDL_SetError("Unable to enable force feedback actuators: %s", FFStrError(result));
}
/* Create the effect */
Feb 4, 2020
Feb 4, 2020
872
device->ffeffect = CreateRumbleEffectData(magnitude);
Aug 9, 2018
Aug 9, 2018
873
874
875
876
877
878
879
880
881
882
883
884
885
if (!device->ffeffect) {
return SDL_OutOfMemory();
}
result = FFDeviceCreateEffect(device->ffdevice, kFFEffectType_Sine_ID,
device->ffeffect, &device->ffeffect_ref);
if (result != FF_OK) {
return SDL_SetError("Haptic: Unable to create effect: %s", FFStrError(result));
}
return 0;
}
static int
Feb 4, 2020
Feb 4, 2020
886
DARWIN_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
Aug 9, 2018
Aug 9, 2018
887
888
889
890
891
892
{
HRESULT result;
recDevice *device = joystick->hwdata;
/* Scale and average the two rumble strengths */
Sint16 magnitude = (Sint16)(((low_frequency_rumble / 2) + (high_frequency_rumble / 2)) / 2);
Mar 17, 2020
Mar 17, 2020
893
894
895
896
if (!device) {
return SDL_SetError("Rumble failed, device disconnected");
}
Aug 9, 2018
Aug 9, 2018
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
if (!device->ffservice) {
return SDL_Unsupported();
}
if (device->ff_initialized) {
FFPERIODIC *periodic = ((FFPERIODIC *)device->ffeffect->lpvTypeSpecificParams);
periodic->dwMagnitude = CONVERT_MAGNITUDE(magnitude);
result = FFEffectSetParameters(device->ffeffect_ref, device->ffeffect,
(FFEP_DURATION | FFEP_TYPESPECIFICPARAMS));
if (result != FF_OK) {
return SDL_SetError("Unable to update rumble effect: %s", FFStrError(result));
}
} else {
Feb 4, 2020
Feb 4, 2020
912
if (DARWIN_JoystickInitRumble(device, magnitude) < 0) {
Aug 9, 2018
Aug 9, 2018
913
914
915
916
917
918
919
920
921
922
923
924
925
926
return -1;
}
device->ff_initialized = SDL_TRUE;
}
result = FFEffectStart(device->ffeffect_ref, 1, 0);
if (result != FF_OK) {
return SDL_SetError("Unable to run the rumble effect: %s", FFStrError(result));
}
return 0;
}
static void
DARWIN_JoystickUpdate(SDL_Joystick * joystick)
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
{
recDevice *device = joystick->hwdata;
recElement *element;
SInt32 value, range;
int i;
if (!device) {
return;
}
if (device->removed) { /* device was unplugged; ignore it. */
if (joystick->hwdata) {
joystick->hwdata = NULL;
}
return;
}
element = device->firstAxis;
i = 0;
Feb 5, 2018
Feb 5, 2018
946
947
int goodRead = SDL_FALSE;
Feb 5, 2018
Feb 5, 2018
949
950
951
952
953
goodRead = GetHIDScaledCalibratedState(device, element, -32768, 32767, &value);
if (goodRead) {
SDL_PrivateJoystickAxis(joystick, i, value);
}
954
955
956
957
958
959
960
element = element->pNext;
++i;
}
element = device->firstButton;
i = 0;
while (element) {
Feb 5, 2018
Feb 5, 2018
961
962
963
964
965
966
goodRead = GetHIDElementState(device, element, &value);
if (goodRead) {
if (value > 1) { /* handle pressure-sensitive buttons */
value = 1;
}
SDL_PrivateJoystickButton(joystick, i, value);
Feb 5, 2018
Feb 5, 2018
968
969
970
971
972
973
974
element = element->pNext;
++i;
}
element = device->firstHat;
i = 0;
Feb 5, 2018
Feb 5, 2018
975
976
977
978
979
while (element) {
Uint8 pos = 0;
range = (element->max - element->min + 1);
Feb 5, 2018
Feb 5, 2018
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
goodRead = GetHIDElementState(device, element, &value);
if (goodRead) {
value -= element->min;
if (range == 4) { /* 4 position hatswitch - scale up value */
value *= 2;
} else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */
value = -1;
}
switch (value) {
case 0:
pos = SDL_HAT_UP;
break;
case 1:
pos = SDL_HAT_RIGHTUP;
break;
case 2:
pos = SDL_HAT_RIGHT;
break;
case 3:
pos = SDL_HAT_RIGHTDOWN;
break;