/
SDL_sysjoystick.m
800 lines (668 loc) · 24.7 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2018 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
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"
/* This is the iOS implementation of the SDL joystick API */
24
25
26
27
#include "SDL_sysjoystick_c.h"
/* needed for SDL_IPHONE_MAX_GFORCE macro */
#include "SDL_config_iphoneos.h"
28
29
#include "SDL_assert.h"
30
#include "SDL_events.h"
31
32
33
34
35
#include "SDL_joystick.h"
#include "SDL_hints.h"
#include "SDL_stdinc.h"
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
36
37
#include "../steam/SDL_steamcontroller.h"
38
39
40
41
42
43
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
#if !TARGET_OS_TV
44
#import <CoreMotion/CoreMotion.h>
45
#endif
46
47
48
#ifdef SDL_JOYSTICK_MFI
#import <GameController/GameController.h>
49
50
51
52
static id connectObserver = nil;
static id disconnectObserver = nil;
#endif /* SDL_JOYSTICK_MFI */
53
54
#if !TARGET_OS_TV
55
static const char *accelerometerName = "iOS Accelerometer";
56
static CMMotionManager *motionManager = nil;
57
#endif /* !TARGET_OS_TV */
58
59
60
static SDL_JoystickDeviceItem *deviceList = NULL;
61
static int numjoysticks = 0;
62
static SDL_JoystickID instancecounter = 0;
63
int SDL_AppleTVRemoteOpenedAsJoystick = 0;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
static SDL_JoystickDeviceItem *
GetDeviceForIndex(int device_index)
{
SDL_JoystickDeviceItem *device = deviceList;
int i = 0;
while (i < device_index) {
if (device == NULL) {
return NULL;
}
device = device->next;
i++;
}
return device;
}
static void
SDL_SYS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
{
#ifdef SDL_JOYSTICK_MFI
86
const Uint16 BUS_BLUETOOTH = 0x05;
87
const Uint16 VENDOR_APPLE = 0x05AC;
88
Uint16 *guid16 = (Uint16 *)device->guid.data;
89
90
91
92
Uint16 vendor = 0;
Uint16 product = 0;
Uint16 version = 0;
Uint8 subtype = 0;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const char *name = NULL;
/* Explicitly retain the controller because SDL_JoystickDeviceItem is a
* struct, and ARC doesn't work with structs. */
device->controller = (__bridge GCController *) CFBridgingRetain(controller);
if (controller.vendorName) {
name = controller.vendorName.UTF8String;
}
if (!name) {
name = "MFi Gamepad";
}
device->name = SDL_strdup(name);
if (controller.extendedGamepad) {
110
111
112
vendor = VENDOR_APPLE;
product = 1;
subtype = 1;
113
114
115
116
device->naxes = 6; /* 2 thumbsticks and 2 triggers */
device->nhats = 1; /* d-pad */
device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
} else if (controller.gamepad) {
117
118
119
vendor = VENDOR_APPLE;
product = 2;
subtype = 2;
120
121
122
123
device->naxes = 0; /* no traditional analog inputs */
device->nhats = 1; /* d-pad */
device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
}
124
125
#if TARGET_OS_TV
else if (controller.microGamepad) {
126
127
128
vendor = VENDOR_APPLE;
product = 3;
subtype = 3;
129
130
131
device->naxes = 2; /* treat the touch surface as two axes */
device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
device->nbuttons = 3; /* AX, pause button */
132
133
controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
134
135
}
#endif /* TARGET_OS_TV */
136
137
138
139
140
/* We only need 16 bits for each of these; space them out to fill 128. */
/* Byteswap so devices get same GUID on little/big endian platforms. */
*guid16++ = SDL_SwapLE16(BUS_BLUETOOTH);
*guid16++ = 0;
141
142
143
144
145
146
*guid16++ = SDL_SwapLE16(vendor);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(product);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(version);
*guid16++ = 0;
147
148
149
150
/* Note that this is an MFI controller and what subtype it is */
device->guid.data[14] = 'm';
device->guid.data[15] = subtype;
151
152
153
154
/* This will be set when the first button press of the controller is
* detected. */
controller.playerIndex = -1;
155
156
#endif /* SDL_JOYSTICK_MFI */
157
158
159
160
161
162
163
}
static void
SDL_SYS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
{
SDL_JoystickDeviceItem *device = deviceList;
164
165
166
167
168
169
170
171
172
#if TARGET_OS_TV
if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
/* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
if (controller && !controller.extendedGamepad && !controller.gamepad && controller.microGamepad) {
return;
}
}
#endif
173
174
175
176
177
178
179
while (device != NULL) {
if (device->controller == controller) {
return;
}
device = device->next;
}
180
device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
181
182
183
184
185
186
187
188
if (device == NULL) {
return;
}
device->accelerometer = accelerometer;
device->instance_id = instancecounter++;
if (accelerometer) {
189
190
191
192
#if TARGET_OS_TV
SDL_free(device);
return;
#else
193
194
195
196
197
198
199
device->name = SDL_strdup(accelerometerName);
device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
device->nhats = 0;
device->nbuttons = 0;
/* Use the accelerometer name as a GUID. */
SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
200
#endif /* TARGET_OS_TV */
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
} else if (controller) {
SDL_SYS_AddMFIJoystickDevice(device, controller);
}
if (deviceList == NULL) {
deviceList = device;
} else {
SDL_JoystickDeviceItem *lastdevice = deviceList;
while (lastdevice->next != NULL) {
lastdevice = lastdevice->next;
}
lastdevice->next = device;
}
++numjoysticks;
217
SDL_PrivateJoystickAdded(numjoysticks - 1);
218
219
}
220
static SDL_JoystickDeviceItem *
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
SDL_SYS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
{
SDL_JoystickDeviceItem *prev = NULL;
SDL_JoystickDeviceItem *next = NULL;
SDL_JoystickDeviceItem *item = deviceList;
if (device == NULL) {
return NULL;
}
next = device->next;
while (item != NULL) {
if (item == device) {
break;
}
prev = item;
item = item->next;
}
/* Unlink the device item from the device list. */
if (prev) {
prev->next = device->next;
} else if (device == deviceList) {
deviceList = device->next;
}
if (device->joystick) {
device->joystick->hwdata = NULL;
}
#ifdef SDL_JOYSTICK_MFI
@autoreleasepool {
if (device->controller) {
/* The controller was explicitly retained in the struct, so it
* should be explicitly released before freeing the struct. */
GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
controller.controllerPausedHandler = nil;
device->controller = nil;
}
}
#endif /* SDL_JOYSTICK_MFI */
--numjoysticks;
266
SDL_PrivateJoystickRemoved(device->instance_id);
267
268
269
270
SDL_free(device->name);
SDL_free(device);
271
272
return next;
}
273
274
#if TARGET_OS_TV
275
static void SDLCALL
276
277
278
279
280
281
282
283
284
285
286
287
288
289
SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
{
BOOL allowRotation = newValue != NULL && *newValue != '0';
@autoreleasepool {
for (GCController *controller in [GCController controllers]) {
if (controller.microGamepad) {
controller.microGamepad.allowsRotation = allowRotation;
}
}
}
}
#endif /* TARGET_OS_TV */
290
291
292
293
294
295
296
297
static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
{
SDL_JoystickDeviceItem *device = (SDL_JoystickDeviceItem *)SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
if (device == NULL) {
return SDL_FALSE;
}
*device_instance = device->instance_id = instancecounter++;
298
299
300
301
302
device->name = SDL_strdup(name);
device->guid = guid;
SDL_GetSteamControllerInputs(&device->nbuttons,
&device->naxes,
&device->nhats);
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
device->m_bSteamController = SDL_TRUE;
if (deviceList == NULL) {
deviceList = device;
} else {
SDL_JoystickDeviceItem *lastdevice = deviceList;
while (lastdevice->next != NULL) {
lastdevice = lastdevice->next;
}
lastdevice->next = device;
}
++numjoysticks;
SDL_PrivateJoystickAdded(numjoysticks - 1);
return SDL_TRUE;
}
static void SteamControllerDisconnectedCallback(int device_instance)
{
SDL_JoystickDeviceItem *item;
326
for (item = deviceList; item; item = item->next) {
327
if (item->instance_id == device_instance) {
328
329
SDL_SYS_RemoveJoystickDevice(item);
break;
330
331
332
333
}
}
}
334
335
336
337
338
339
340
/* Function to scan the system for joysticks.
* Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
*/
int
SDL_SYS_JoystickInit(void)
{
341
342
343
@autoreleasepool {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
344
345
346
SDL_InitSteamControllers(SteamControllerConnectedCallback,
SteamControllerDisconnectedCallback);
347
#if !TARGET_OS_TV
348
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
349
350
351
/* Default behavior, accelerometer as joystick */
SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
}
352
#endif /* !TARGET_OS_TV */
353
354
355
356
357
358
359
360
361
362
363
#ifdef SDL_JOYSTICK_MFI
/* GameController.framework was added in iOS 7. */
if (![GCController class]) {
return numjoysticks;
}
for (GCController *controller in [GCController controllers]) {
SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
}
364
365
366
367
368
#if TARGET_OS_TV
SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
connectObserver = [center addObserverForName:GCControllerDidConnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
}];
disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
SDL_JoystickDeviceItem *device = deviceList;
while (device != NULL) {
if (device->controller == controller) {
SDL_SYS_RemoveJoystickDevice(device);
break;
}
device = device->next;
}
}];
#endif /* SDL_JOYSTICK_MFI */
392
393
394
395
396
}
return numjoysticks;
}
397
398
int
SDL_SYS_NumJoysticks(void)
399
400
401
402
{
return numjoysticks;
}
403
404
void
SDL_SYS_JoystickDetect(void)
405
{
406
SDL_UpdateSteamControllers();
407
408
409
410
411
412
}
/* Function to get the device-dependent name of a joystick */
const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
413
414
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->name : "Unknown";
415
416
417
418
419
}
/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
420
421
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->instance_id : 0;
422
423
424
425
426
427
428
429
430
431
}
/* Function to open a joystick for use.
The joystick to open is specified by the device index.
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)
{
432
433
434
435
436
437
438
439
440
441
442
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
if (device == NULL) {
return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
}
joystick->hwdata = device;
joystick->instance_id = device->instance_id;
joystick->naxes = device->naxes;
joystick->nhats = device->nhats;
joystick->nbuttons = device->nbuttons;
443
joystick->nballs = 0;
444
445
device->joystick = joystick;
446
447
@autoreleasepool {
448
if (device->accelerometer) {
449
#if !TARGET_OS_TV
450
451
452
453
454
455
456
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
/* Shorter times between updates can significantly increase CPU usage. */
motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates];
457
#endif /* !TARGET_OS_TV */
458
459
460
} else {
#ifdef SDL_JOYSTICK_MFI
GCController *controller = device->controller;
461
controller.controllerPausedHandler = ^(GCController *c) {
462
463
464
465
466
if (joystick->hwdata) {
++joystick->hwdata->num_pause_presses;
}
};
#endif /* SDL_JOYSTICK_MFI */
467
468
}
}
469
470
471
if (device->remote) {
++SDL_AppleTVRemoteOpenedAsJoystick;
}
472
473
474
475
476
return 0;
}
/* Function to determine if this joystick is attached to the system right now */
477
478
SDL_bool
SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
479
{
480
return joystick->hwdata != NULL;
481
482
}
483
484
static void
SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
485
{
486
#if !TARGET_OS_TV
487
488
489
490
491
const float maxgforce = SDL_IPHONE_MAX_GFORCE;
const SInt16 maxsint16 = 0x7FFF;
CMAcceleration accel;
@autoreleasepool {
492
if (!motionManager.isAccelerometerActive) {
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
return;
}
accel = motionManager.accelerometerData.acceleration;
}
/*
Convert accelerometer data from floating point to Sint16, which is what
the joystick system expects.
To do the conversion, the data is first clamped onto the interval
[-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
by MAX_SINT16 so that it is mapped to the full range of an Sint16.
You can customize the clamped range of this function by modifying the
SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
Once converted to Sint16, the accelerometer data no longer has coherent
units. You can convert the data back to units of g-force by multiplying
it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
*/
/* clamp the data */
accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
/* pass in data mapped to range of SInt16 */
521
SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
522
SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
523
SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
524
#endif /* !TARGET_OS_TV */
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
}
#ifdef SDL_JOYSTICK_MFI
static Uint8
SDL_SYS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
{
Uint8 hat = 0;
if (dpad.up.isPressed) {
hat |= SDL_HAT_UP;
} else if (dpad.down.isPressed) {
hat |= SDL_HAT_DOWN;
}
if (dpad.left.isPressed) {
hat |= SDL_HAT_LEFT;
} else if (dpad.right.isPressed) {
hat |= SDL_HAT_RIGHT;
}
if (hat == 0) {
return SDL_HAT_CENTERED;
}
return hat;
}
#endif
static void
SDL_SYS_MFIJoystickUpdate(SDL_Joystick * joystick)
{
556
#if SDL_JOYSTICK_MFI
557
558
559
560
@autoreleasepool {
GCController *controller = joystick->hwdata->controller;
Uint8 hatstate = SDL_HAT_CENTERED;
int i;
561
int updateplayerindex = 0;
562
563
564
565
566
if (controller.extendedGamepad) {
GCExtendedGamepad *gamepad = controller.extendedGamepad;
/* Axis order matches the XInput Windows mappings. */
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
Sint16 axes[] = {
(Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
(Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
(Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
(Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
(Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
(Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
};
/* Button order matches the XInput Windows mappings. */
Uint8 buttons[] = {
gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
gamepad.leftShoulder.isPressed,
gamepad.rightShoulder.isPressed,
};
583
584
585
hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
586
587
588
589
590
for (i = 0; i < SDL_arraysize(axes); i++) {
/* The triggers (axes 2 and 5) are resting at -32768 but SDL
* initializes its values to 0. We only want to make sure the
* player index is up to date if the user actually moves an axis. */
if ((i != 2 && i != 5) || axes[i] != -32768) {
591
updateplayerindex |= (joystick->axes[i].value != axes[i]);
592
593
594
595
596
597
598
599
}
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
for (i = 0; i < SDL_arraysize(buttons); i++) {
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
600
601
602
} else if (controller.gamepad) {
GCGamepad *gamepad = controller.gamepad;
603
604
605
606
607
608
609
610
/* Button order matches the XInput Windows mappings. */
Uint8 buttons[] = {
gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
gamepad.leftShoulder.isPressed,
gamepad.rightShoulder.isPressed,
};
611
612
hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
613
614
615
616
for (i = 0; i < SDL_arraysize(buttons); i++) {
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
617
}
618
619
620
621
622
623
624
625
626
627
#if TARGET_OS_TV
else if (controller.microGamepad) {
GCMicroGamepad *gamepad = controller.microGamepad;
Sint16 axes[] = {
(Sint16) (gamepad.dpad.xAxis.value * 32767),
(Sint16) (gamepad.dpad.yAxis.value * -32767),
};
for (i = 0; i < SDL_arraysize(axes); i++) {
628
updateplayerindex |= (joystick->axes[i].value != axes[i]);
629
630
631
632
633
634
635
636
637
638
639
640
641
642
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
Uint8 buttons[] = {
gamepad.buttonA.isPressed,
gamepad.buttonX.isPressed,
};
for (i = 0; i < SDL_arraysize(buttons); i++) {
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
}
#endif /* TARGET_OS_TV */
643
644
645
646
647
if (joystick->nhats > 0) {
updateplayerindex |= (joystick->hats[0] != hatstate);
SDL_PrivateJoystickHat(joystick, 0, hatstate);
}
648
649
for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
650
const Uint8 pausebutton = joystick->nbuttons - 1; /* The pause button is always last. */
651
SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
652
SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
653
updateplayerindex = YES;
654
655
}
joystick->hwdata->num_pause_presses = 0;
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
if (updateplayerindex && controller.playerIndex == -1) {
BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
/* Find the player index of all other connected controllers. */
for (GCController *c in [GCController controllers]) {
if (c != controller && c.playerIndex >= 0) {
usedPlayerIndexSlots[c.playerIndex] = YES;
}
}
/* Set this controller's player index to the first unused index.
* FIXME: This logic isn't great... but SDL doesn't expose this
* concept in its external API, so we don't have much to go on. */
for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
if (!usedPlayerIndexSlots[i]) {
controller.playerIndex = i;
break;
}
}
}
677
}
678
#endif /* SDL_JOYSTICK_MFI */
679
680
681
682
683
684
685
686
687
688
}
/* 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)
{
689
690
691
692
693
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
694
695
696
697
698
if (device->m_bSteamController) {
SDL_UpdateSteamController(joystick);
return;
}
699
700
701
702
703
704
if (device->accelerometer) {
SDL_SYS_AccelerometerUpdate(joystick);
} else if (device->controller) {
SDL_SYS_MFIJoystickUpdate(joystick);
}
705
706
707
708
709
710
}
/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
711
712
713
714
715
716
717
718
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
device->joystick = NULL;
719
@autoreleasepool {
720
if (device->accelerometer) {
721
#if !TARGET_OS_TV
722
[motionManager stopAccelerometerUpdates];
723
#endif /* !TARGET_OS_TV */
724
725
726
727
} else if (device->controller) {
#ifdef SDL_JOYSTICK_MFI
GCController *controller = device->controller;
controller.controllerPausedHandler = nil;
728
controller.playerIndex = -1;
729
730
#endif
}
731
}
732
733
734
if (device->remote) {
--SDL_AppleTVRemoteOpenedAsJoystick;
}
735
736
737
738
739
740
741
}
/* Function to perform any system-specific joystick related cleanup */
void
SDL_SYS_JoystickQuit(void)
{
@autoreleasepool {
742
743
744
745
746
747
748
749
750
751
752
753
#ifdef SDL_JOYSTICK_MFI
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
if (connectObserver) {
[center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
connectObserver = nil;
}
if (disconnectObserver) {
[center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
disconnectObserver = nil;
}
754
755
756
757
758
#if TARGET_OS_TV
SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
759
760
761
762
763
764
#endif /* SDL_JOYSTICK_MFI */
while (deviceList != NULL) {
SDL_SYS_RemoveJoystickDevice(deviceList);
}
765
#if !TARGET_OS_TV
766
motionManager = nil;
767
#endif /* !TARGET_OS_TV */
768
769
}
770
771
SDL_QuitSteamControllers();
772
773
774
numjoysticks = 0;
}
775
776
SDL_JoystickGUID
SDL_SYS_JoystickGetDeviceGUID( int device_index )
777
{
778
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
779
SDL_JoystickGUID guid;
780
781
782
783
784
if (device) {
guid = device->guid;
} else {
SDL_zero(guid);
}
785
786
787
return guid;
}
788
789
SDL_JoystickGUID
SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
790
791
{
SDL_JoystickGUID guid;
792
793
794
795
796
if (joystick->hwdata) {
guid = joystick->hwdata->guid;
} else {
SDL_zero(guid);
}
797
798
799
800
return guid;
}
/* vi: set ts=4 sw=4 expandtab: */