Skip to content

Latest commit

 

History

History
824 lines (695 loc) · 25.1 KB

SDL_sysjoystick.c

File metadata and controls

824 lines (695 loc) · 25.1 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 2, 2014
Feb 2, 2014
3
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
*/
Nov 25, 2013
Nov 25, 2013
21
#include "../../SDL_internal.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifdef SDL_JOYSTICK_IOKIT
#include <IOKit/hid/IOHIDLib.h>
/* For force feedback testing. */
#include <ForceFeedback/ForceFeedback.h>
#include <ForceFeedback/ForceFeedbackConstants.h>
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#include "SDL_sysjoystick_c.h"
#include "SDL_events.h"
Feb 4, 2014
Feb 4, 2014
36
#include "../../haptic/darwin/SDL_syshaptic_c.h" /* For haptic hot plugging */
37
38
39
40
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
Feb 22, 2014
Feb 22, 2014
41
42
/* The base object of the HID Manager API */
static IOHIDManagerRef hidman = NULL;
43
44
45
/* Linked list of all available devices */
static recDevice *gpDeviceList = NULL;
Feb 22, 2014
Feb 22, 2014
46
47
/* if SDL_TRUE then a device was added since the last update call */
48
49
50
51
52
53
static SDL_bool s_bDeviceAdded = SDL_FALSE;
static SDL_bool s_bDeviceRemoved = SDL_FALSE;
/* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
static int s_joystick_instance_id = -1;
Feb 22, 2014
Feb 22, 2014
54
55
static void
Feb 22, 2014
Feb 22, 2014
56
FreeElementList(recElement *pElement)
57
{
Feb 22, 2014
Feb 22, 2014
58
59
60
61
62
while (pElement) {
recElement *pElementNext = pElement->pNext;
SDL_free(pElement);
pElement = pElementNext;
}
63
64
}
Feb 22, 2014
Feb 22, 2014
65
66
67
68
69
70
71
static recDevice *
FreeDevice(recDevice *removeDevice)
{
recDevice *pDeviceNext = NULL;
if (removeDevice) {
/* save next device prior to disposing of this device */
pDeviceNext = removeDevice->pNext;
72
Feb 22, 2014
Feb 22, 2014
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
if ( gpDeviceList == removeDevice ) {
gpDeviceList = pDeviceNext;
} else {
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;
}
93
94
static SInt32
Feb 22, 2014
Feb 22, 2014
95
GetHIDElementState(recDevice *pDevice, recElement *pElement)
96
{
Feb 22, 2014
Feb 22, 2014
97
98
99
100
101
102
103
SInt32 value = 0;
if (pDevice && pElement) {
IOHIDValueRef valueRef;
if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) {
value = (SInt32) IOHIDValueGetIntegerValue(valueRef);
104
/* record min and max for auto calibration */
Feb 22, 2014
Feb 22, 2014
105
106
107
108
109
110
if (value < pElement->minReport) {
pElement->minReport = value;
}
if (value > pElement->maxReport) {
pElement->maxReport = value;
}
111
112
113
}
}
Feb 22, 2014
Feb 22, 2014
114
return value;
115
116
117
}
static SInt32
Feb 22, 2014
Feb 22, 2014
118
GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max)
119
{
Feb 22, 2014
Feb 22, 2014
120
121
122
123
const float deviceScale = max - min;
const float readScale = pElement->maxReport - pElement->minReport;
const SInt32 value = GetHIDElementState(pDevice, pElement);
if (readScale == 0) {
124
return value; /* no scaling at all */
Feb 22, 2014
Feb 22, 2014
125
126
}
return ((value - pElement->minReport) * deviceScale / readScale) + min;
127
128
129
130
}
static void
Feb 22, 2014
Feb 22, 2014
131
JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
132
{
Feb 22, 2014
Feb 22, 2014
133
recDevice *device = (recDevice *) ctx;
134
device->removed = 1;
Feb 4, 2014
Feb 4, 2014
135
#if SDL_HAPTIC_IOKIT
Feb 5, 2014
Feb 5, 2014
136
MacHaptic_MaybeRemoveDevice(device->ffservice);
Feb 4, 2014
Feb 4, 2014
137
#endif
138
139
140
141
s_bDeviceRemoved = SDL_TRUE;
}
Feb 22, 2014
Feb 22, 2014
142
static void AddHIDElement(const void *value, void *parameter);
143
Feb 22, 2014
Feb 22, 2014
144
145
146
/* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */
static void
AddHIDElements(CFArrayRef array, recDevice *pDevice)
147
{
Feb 22, 2014
Feb 22, 2014
148
149
const CFRange range = { 0, CFArrayGetCount(array) };
CFArrayApplyFunction(array, range, AddHIDElement, pDevice);
150
151
}
Feb 23, 2014
Feb 23, 2014
152
153
154
155
156
157
158
159
160
161
162
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;
}
Feb 22, 2014
Feb 22, 2014
163
/* See if we care about this HID element, and if so, note it in our recDevice. */
164
static void
Feb 22, 2014
Feb 22, 2014
165
AddHIDElement(const void *value, void *parameter)
166
{
Feb 22, 2014
Feb 22, 2014
167
168
169
recDevice *pDevice = (recDevice *) parameter;
IOHIDElementRef refElement = (IOHIDElementRef) value;
const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
170
Feb 22, 2014
Feb 22, 2014
171
if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
Feb 23, 2014
Feb 23, 2014
172
const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement);
Feb 22, 2014
Feb 22, 2014
173
174
175
176
const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
const uint32_t usage = IOHIDElementGetUsage(refElement);
recElement *element = NULL;
recElement **headElement = NULL;
177
178
/* look at types of interest */
Feb 22, 2014
Feb 22, 2014
179
180
181
182
switch (IOHIDElementGetType(refElement)) {
case kIOHIDElementTypeInput_Misc:
case kIOHIDElementTypeInput_Button:
case kIOHIDElementTypeInput_Axis: {
183
switch (usagePage) { /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
Feb 22, 2014
Feb 22, 2014
184
185
186
187
188
189
190
191
192
193
194
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:
Feb 23, 2014
Feb 23, 2014
195
196
197
198
199
200
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->axes++;
headElement = &(pDevice->firstAxis);
}
Feb 22, 2014
Feb 22, 2014
201
202
203
204
}
break;
case kHIDUsage_GD_Hatswitch:
Feb 23, 2014
Feb 23, 2014
205
206
207
208
209
210
if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->hats++;
headElement = &(pDevice->firstHat);
}
Feb 22, 2014
Feb 22, 2014
211
212
}
break;
213
}
Feb 22, 2014
Feb 22, 2014
214
215
216
217
218
219
break;
case kHIDPage_Simulation:
switch (usage) {
case kHIDUsage_Sim_Rudder:
case kHIDUsage_Sim_Throttle:
Feb 23, 2014
Feb 23, 2014
220
221
222
223
224
225
if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->axes++;
headElement = &(pDevice->firstAxis);
}
Feb 22, 2014
Feb 22, 2014
226
227
228
229
230
231
232
233
234
}
break;
default:
break;
}
break;
case kHIDPage_Button:
Feb 23, 2014
Feb 23, 2014
235
236
237
238
239
240
if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
element = (recElement *) SDL_calloc(1, sizeof (recElement));
if (element) {
pDevice->buttons++;
headElement = &(pDevice->firstButton);
}
Feb 22, 2014
Feb 22, 2014
241
242
243
244
245
}
break;
default:
break;
246
247
}
}
Feb 22, 2014
Feb 22, 2014
248
break;
249
Feb 22, 2014
Feb 22, 2014
250
251
252
253
254
255
256
257
258
259
case kIOHIDElementTypeCollection: {
CFArrayRef array = IOHIDElementGetChildren(refElement);
if (array) {
AddHIDElements(array, pDevice);
}
}
break;
default:
break;
260
261
}
Feb 22, 2014
Feb 22, 2014
262
263
264
265
266
267
268
269
270
271
272
273
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;
}
274
Feb 22, 2014
Feb 22, 2014
275
276
277
278
element->elementRef = refElement;
element->usagePage = usagePage;
element->usage = usage;
element->pNext = elementCurrent;
279
Feb 22, 2014
Feb 22, 2014
280
281
element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement);
element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
Feb 23, 2014
Feb 23, 2014
282
element->cookie = IOHIDElementGetCookie(refElement);
283
Feb 22, 2014
Feb 22, 2014
284
285
pDevice->elements++;
}
286
287
288
}
}
Feb 22, 2014
Feb 22, 2014
289
290
static SDL_bool
GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
291
{
Feb 22, 2014
Feb 22, 2014
292
293
294
Uint32 *guid32 = NULL;
CFTypeRef refCF = NULL;
CFArrayRef array = NULL;
295
Feb 22, 2014
Feb 22, 2014
296
297
298
299
300
301
302
303
/* 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 */
}
304
Feb 22, 2014
Feb 22, 2014
305
306
307
308
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage);
}
309
Feb 22, 2014
Feb 22, 2014
310
311
312
313
314
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 */
}
315
Feb 22, 2014
Feb 22, 2014
316
pDevice->deviceRef = hidDevice;
317
Feb 22, 2014
Feb 22, 2014
318
319
320
321
322
323
324
325
326
/* get device name */
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
if (!refCF) {
/* Maybe we can't get "AwesomeJoystick2000", but we can get "Logitech"? */
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
}
if ((!refCF) || (!CFStringGetCString(refCF, pDevice->product, sizeof (pDevice->product), kCFStringEncodingUTF8))) {
SDL_strlcpy(pDevice->product, "Unidentified joystick", sizeof (pDevice->product));
}
327
Feb 22, 2014
Feb 22, 2014
328
329
330
331
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[0]);
}
332
Feb 22, 2014
Feb 22, 2014
333
334
335
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[8]);
336
337
}
Feb 22, 2014
Feb 22, 2014
338
339
340
341
342
343
344
345
346
347
348
/* Check to make sure we have a vendor and product ID
If we don't, use the same algorithm as the Linux code for Bluetooth devices */
guid32 = (Uint32*)pDevice->guid.data;
if (!guid32[0] && !guid32[1]) {
/* If we don't have a vendor and product ID this is probably a Bluetooth device */
const Uint16 BUS_BLUETOOTH = 0x05;
Uint16 *guid16 = (Uint16 *)guid32;
*guid16++ = BUS_BLUETOOTH;
*guid16++ = 0;
SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4);
}
349
Feb 22, 2014
Feb 22, 2014
350
351
352
353
array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone);
if (array) {
AddHIDElements(array, pDevice);
CFRelease(array);
354
}
Feb 22, 2014
Feb 22, 2014
355
356
return SDL_TRUE;
357
358
359
360
}
static void
Feb 22, 2014
Feb 22, 2014
361
JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
362
{
Feb 22, 2014
Feb 22, 2014
363
364
if (res != kIOReturnSuccess) {
return;
365
366
}
Feb 22, 2014
Feb 22, 2014
367
recDevice *device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
368
Feb 22, 2014
Feb 22, 2014
369
370
371
if (!device) {
SDL_OutOfMemory();
return;
372
373
}
Feb 22, 2014
Feb 22, 2014
374
375
376
if (!GetDeviceInfo(ioHIDDeviceObject, device)) {
SDL_free(device);
return; /* not a device we care about, probably. */
377
378
}
Feb 22, 2014
Feb 22, 2014
379
380
381
382
/* Get notified when this device is disconnected. */
IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device);
IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
383
384
385
/* Allocate an instance ID for this device */
device->instance_id = ++s_joystick_instance_id;
Feb 22, 2014
Feb 22, 2014
386
387
388
389
390
/* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */
if (IOHIDDeviceGetService != NULL) { /* weak reference: available in 10.6 and later. */
const io_service_t ioservice = IOHIDDeviceGetService(ioHIDDeviceObject);
if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) {
device->ffservice = ioservice;
Feb 4, 2014
Feb 4, 2014
391
#if SDL_HAPTIC_IOKIT
Feb 22, 2014
Feb 22, 2014
392
MacHaptic_MaybeAddDevice(ioservice);
Feb 4, 2014
Feb 4, 2014
393
#endif
Feb 22, 2014
Feb 22, 2014
394
}
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
}
device->send_open_event = 1;
s_bDeviceAdded = SDL_TRUE;
/* Add device to the end of the list */
if ( !gpDeviceList )
{
gpDeviceList = device;
}
else
{
recDevice *curdevice;
curdevice = gpDeviceList;
while ( curdevice->pNext )
{
curdevice = curdevice->pNext;
}
curdevice->pNext = device;
}
Feb 22, 2014
Feb 22, 2014
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
}
static SDL_bool
ConfigHIDManager(CFArrayRef matchingArray)
{
CFRunLoopRef runloop = CFRunLoopGetCurrent();
/* Run in a custom RunLoop mode just while initializing,
so we can detect sticks without messing with everything else. */
CFStringRef tempRunLoopMode = CFSTR("SDLJoystickInit");
if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
return SDL_FALSE;
}
IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL);
IOHIDManagerScheduleWithRunLoop(hidman, runloop, tempRunLoopMode);
IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray);
while (CFRunLoopRunInMode(tempRunLoopMode,0,TRUE)==kCFRunLoopRunHandledSource) {
/* no-op. Callback fires once per existing device. */
}
438
Feb 22, 2014
Feb 22, 2014
439
440
441
442
443
/* Put this in the normal RunLoop mode now, for future hotplug events. */
IOHIDManagerUnscheduleFromRunLoop(hidman, runloop, tempRunLoopMode);
IOHIDManagerScheduleWithRunLoop(hidman, runloop, kCFRunLoopDefaultMode);
return SDL_TRUE; /* good to go. */
444
445
446
}
Feb 22, 2014
Feb 22, 2014
447
448
static CFDictionaryRef
CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay)
449
{
Feb 22, 2014
Feb 22, 2014
450
451
452
453
454
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 };
455
Feb 22, 2014
Feb 22, 2014
456
457
458
459
460
461
462
463
if (pageNumRef && usageNumRef) {
retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
if (pageNumRef) {
CFRelease(pageNumRef);
}
if (usageNumRef) {
Feb 23, 2014
Feb 23, 2014
464
CFRelease(usageNumRef);
Feb 22, 2014
Feb 22, 2014
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
}
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]);
491
492
}
}
Feb 22, 2014
Feb 22, 2014
493
494
495
496
497
498
499
500
501
502
if (array) {
hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (hidman != NULL) {
retval = ConfigHIDManager(array);
}
CFRelease(array);
}
return retval;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
}
/* Function to scan the system for joysticks.
* Joystick 0 should be the system default joystick.
* This function should return the number of available joysticks, or -1
* on an unrecoverable fatal error.
*/
int
SDL_SYS_JoystickInit(void)
{
if (gpDeviceList) {
return SDL_SetError("Joystick: Device list already inited.");
}
Feb 22, 2014
Feb 22, 2014
518
519
if (!CreateHIDManager()) {
return SDL_SetError("Joystick: Couldn't initialize HID Manager");
520
521
522
523
524
525
526
527
528
529
530
531
}
return SDL_SYS_NumJoysticks();
}
/* Function to return the number of joystick devices plugged in right now */
int
SDL_SYS_NumJoysticks()
{
recDevice *device = gpDeviceList;
int nJoySticks = 0;
Feb 22, 2014
Feb 22, 2014
532
533
while (device) {
if (!device->removed) {
534
nJoySticks++;
Feb 22, 2014
Feb 22, 2014
535
}
536
537
538
539
540
541
542
543
544
545
546
device = device->pNext;
}
return nJoySticks;
}
/* Function to cause any queued joystick insertions to be processed
*/
void
SDL_SYS_JoystickDetect()
{
Feb 22, 2014
Feb 22, 2014
547
if (s_bDeviceAdded || s_bDeviceRemoved) {
548
549
550
551
552
recDevice *device = gpDeviceList;
s_bDeviceAdded = SDL_FALSE;
s_bDeviceRemoved = SDL_FALSE;
int device_index = 0;
/* send notifications */
Feb 22, 2014
Feb 22, 2014
553
554
while (device) {
if (device->send_open_event) {
555
device->send_open_event = 0;
Feb 22, 2014
Feb 22, 2014
556
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
}
Feb 22, 2014
Feb 22, 2014
572
573
574
if (device->removed) {
const int instance_id = device->instance_id;
device = FreeDevice(device);
575
Feb 22, 2014
Feb 22, 2014
576
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
577
578
579
580
581
#if !SDL_EVENTS_DISABLED
SDL_Event event;
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
Feb 22, 2014
Feb 22, 2014
582
event.jdevice.which = instance_id;
583
584
585
586
587
588
589
if ((SDL_EventOK == NULL)
|| (*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
Feb 22, 2014
Feb 22, 2014
590
} else {
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
device = device->pNext;
device_index++;
}
}
}
}
SDL_bool
SDL_SYS_JoystickNeedsPolling()
{
return s_bDeviceAdded || s_bDeviceRemoved;
}
/* Function to get the device-dependent name of a joystick */
const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
recDevice *device = gpDeviceList;
Feb 22, 2014
Feb 22, 2014
610
while (device_index-- > 0) {
611
device = device->pNext;
Feb 22, 2014
Feb 22, 2014
612
}
613
614
615
616
617
618
619
620
621
622
623
624
return device->product;
}
/* Function to return the instance id of the joystick at device_index
*/
SDL_JoystickID
SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
recDevice *device = gpDeviceList;
int index;
Feb 22, 2014
Feb 22, 2014
625
for (index = device_index; index > 0; index--) {
626
device = device->pNext;
Feb 22, 2014
Feb 22, 2014
627
}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
return device->instance_id;
}
/* Function to open a joystick for use.
* The joystick to open is specified by the index field of the joystick.
* This should fill the nbuttons and naxes fields of the joystick structure.
* It returns 0, or -1 if there is an error.
*/
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
recDevice *device = gpDeviceList;
int index;
Feb 22, 2014
Feb 22, 2014
643
for (index = device_index; index > 0; index--) {
644
device = device->pNext;
Feb 22, 2014
Feb 22, 2014
645
}
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
joystick->instance_id = device->instance_id;
joystick->hwdata = device;
joystick->name = device->product;
joystick->naxes = device->axes;
joystick->nhats = device->hats;
joystick->nballs = 0;
joystick->nbuttons = device->buttons;
return 0;
}
/* Function to query if the joystick is currently attached
* It returns 1 if attached, 0 otherwise.
*/
SDL_bool
SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
{
recDevice *device = gpDeviceList;
Feb 22, 2014
Feb 22, 2014
666
667
while (device) {
if (joystick->instance_id == device->instance_id) {
668
return SDL_TRUE;
Feb 22, 2014
Feb 22, 2014
669
}
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
device = device->pNext;
}
return SDL_FALSE;
}
/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events
* and update joystick device state.
*/
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
recDevice *device = joystick->hwdata;
recElement *element;
SInt32 value, range;
int i;
Feb 22, 2014
Feb 22, 2014
689
if (!device) {
690
return;
Feb 22, 2014
Feb 22, 2014
691
}
692
693
694
695
696
697
698
699
700
701
702
if (device->removed) { /* device was unplugged; ignore it. */
joystick->closed = 1;
joystick->uncentered = 1;
joystick->hwdata = NULL;
return;
}
element = device->firstAxis;
i = 0;
while (element) {
Feb 22, 2014
Feb 22, 2014
703
704
value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
if (value != joystick->axes[i]) {
705
SDL_PrivateJoystickAxis(joystick, i, value);
Feb 22, 2014
Feb 22, 2014
706
}
707
708
709
710
711
712
713
element = element->pNext;
++i;
}
element = device->firstButton;
i = 0;
while (element) {
Feb 22, 2014
Feb 22, 2014
714
715
value = GetHIDElementState(device, element);
if (value > 1) { /* handle pressure-sensitive buttons */
716
value = 1;
Feb 22, 2014
Feb 22, 2014
717
718
}
if (value != joystick->buttons[i]) {
719
SDL_PrivateJoystickButton(joystick, i, value);
Feb 22, 2014
Feb 22, 2014
720
}
721
722
723
724
725
726
727
728
729
730
element = element->pNext;
++i;
}
element = device->firstHat;
i = 0;
while (element) {
Uint8 pos = 0;
range = (element->max - element->min + 1);
Feb 22, 2014
Feb 22, 2014
731
732
value = GetHIDElementState(device, element) - element->min;
if (range == 4) { /* 4 position hatswitch - scale up value */
733
value *= 2;
Feb 22, 2014
Feb 22, 2014
734
} else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */
735
value = -1;
Feb 22, 2014
Feb 22, 2014
736
}
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
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;
case 4:
pos = SDL_HAT_DOWN;
break;
case 5:
pos = SDL_HAT_LEFTDOWN;
break;
case 6:
pos = SDL_HAT_LEFT;
break;
case 7:
pos = SDL_HAT_LEFTUP;
break;
default:
/* Every other value is mapped to center. We do that because some
* joysticks use 8 and some 15 for this value, and apparently
* there are even more variants out there - so we try to be generous.
*/
pos = SDL_HAT_CENTERED;
break;
}
Feb 22, 2014
Feb 22, 2014
770
771
if (pos != joystick->hats[i]) {
772
SDL_PrivateJoystickHat(joystick, i, pos);
Feb 22, 2014
Feb 22, 2014
773
774
}
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
element = element->pNext;
++i;
}
}
/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
joystick->closed = 1;
}
/* Function to perform any system-specific joystick related cleanup */
void
SDL_SYS_JoystickQuit(void)
{
Feb 22, 2014
Feb 22, 2014
791
792
793
while (FreeDevice(gpDeviceList)) {
/* spin */
}
794
Feb 22, 2014
Feb 22, 2014
795
796
797
798
if (hidman) {
IOHIDManagerClose(hidman, kIOHIDOptionsTypeNone);
CFRelease(hidman);
hidman = NULL;
799
}
Feb 22, 2014
Feb 22, 2014
800
801
s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
802
803
804
805
806
807
808
809
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{
recDevice *device = gpDeviceList;
int index;
Feb 22, 2014
Feb 22, 2014
810
for (index = device_index; index > 0; index--) {
811
device = device->pNext;
Feb 22, 2014
Feb 22, 2014
812
}
813
814
815
816
817
818
819
820
821
822
823
824
return device->guid;
}
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
{
return joystick->hwdata->guid;
}
#endif /* SDL_JOYSTICK_IOKIT */
/* vi: set ts=4 sw=4 expandtab: */