Skip to content

Latest commit

 

History

History
863 lines (727 loc) · 28 KB

SDL_sysjoystick.m

File metadata and controls

863 lines (727 loc) · 28 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
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 */
Sep 21, 2015
Sep 21, 2015
24
25
26
#include "SDL_sysjoystick_c.h"
/* needed for SDL_IPHONE_MAX_GFORCE macro */
Jan 30, 2020
Jan 30, 2020
27
#include "../../../include/SDL_config_iphoneos.h"
Sep 22, 2017
Sep 22, 2017
29
#include "SDL_assert.h"
Aug 26, 2016
Aug 26, 2016
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"
Sep 22, 2017
Sep 22, 2017
36
Sep 14, 2016
Sep 14, 2016
38
39
40
41
42
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
#if !TARGET_OS_TV
43
#import <CoreMotion/CoreMotion.h>
Sep 14, 2016
Sep 14, 2016
44
#endif
Sep 21, 2015
Sep 21, 2015
46
47
#ifdef SDL_JOYSTICK_MFI
#import <GameController/GameController.h>
Sep 21, 2015
Sep 21, 2015
49
50
static id connectObserver = nil;
static id disconnectObserver = nil;
Jun 6, 2019
Jun 6, 2019
51
52
53
54
#include <Availability.h>
#include <objc/message.h>
Jun 14, 2019
Jun 14, 2019
55
56
57
/* remove compilation warnings for strict builds by defining these selectors, even though
* they are only ever used indirectly through objc_msgSend
*/
Jun 6, 2019
Jun 6, 2019
58
@interface GCExtendedGamepad (SDL)
Jun 14, 2019
Jun 14, 2019
59
60
61
62
63
#if (__IPHONE_OS_VERSION_MAX_ALLOWED < 121000) || (__APPLETV_OS_VERSION_MAX_ALLOWED < 121000) || (__MAC_OS_VERSION_MAX_ALLOWED < 1401000)
@property (nonatomic, readonly, nullable) GCControllerButtonInput *leftThumbstickButton;
@property (nonatomic, readonly, nullable) GCControllerButtonInput *rightThumbstickButton;
#endif
#if (__IPHONE_OS_VERSION_MAX_ALLOWED < 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED < 130000) || (__MAC_OS_VERSION_MAX_ALLOWED < 1500000)
Jun 6, 2019
Jun 6, 2019
64
65
66
@property (nonatomic, readonly) GCControllerButtonInput *buttonMenu;
@property (nonatomic, readonly, nullable) GCControllerButtonInput *buttonOptions;
#endif
Jun 14, 2019
Jun 14, 2019
67
68
69
70
@end
@interface GCMicroGamepad (SDL)
#if (__IPHONE_OS_VERSION_MAX_ALLOWED < 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED < 130000) || (__MAC_OS_VERSION_MAX_ALLOWED < 1500000)
@property (nonatomic, readonly) GCControllerButtonInput *buttonMenu;
Jun 6, 2019
Jun 6, 2019
71
72
73
#endif
@end
Sep 21, 2015
Sep 21, 2015
74
#endif /* SDL_JOYSTICK_MFI */
Sep 14, 2016
Sep 14, 2016
76
#if !TARGET_OS_TV
Sep 21, 2015
Sep 21, 2015
77
static const char *accelerometerName = "iOS Accelerometer";
78
static CMMotionManager *motionManager = nil;
Sep 14, 2016
Sep 14, 2016
79
#endif /* !TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
80
81
82
static SDL_JoystickDeviceItem *deviceList = NULL;
83
static int numjoysticks = 0;
Feb 7, 2018
Feb 7, 2018
84
int SDL_AppleTVRemoteOpenedAsJoystick = 0;
Sep 21, 2015
Sep 21, 2015
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
Aug 9, 2018
Aug 9, 2018
104
IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
Sep 21, 2015
Sep 21, 2015
105
106
{
#ifdef SDL_JOYSTICK_MFI
Mar 9, 2018
Mar 9, 2018
107
const Uint16 VENDOR_APPLE = 0x05AC;
Jun 6, 2019
Jun 6, 2019
108
109
const Uint16 VENDOR_MICROSOFT = 0x045e;
const Uint16 VENDOR_SONY = 0x054C;
Mar 8, 2018
Mar 8, 2018
110
Uint16 *guid16 = (Uint16 *)device->guid.data;
Mar 9, 2018
Mar 9, 2018
111
112
113
Uint16 vendor = 0;
Uint16 product = 0;
Uint8 subtype = 0;
Mar 8, 2018
Mar 8, 2018
114
Sep 21, 2015
Sep 21, 2015
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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) {
Jun 14, 2019
Jun 14, 2019
131
GCExtendedGamepad *gamepad = controller.extendedGamepad;
Dec 6, 2019
Dec 6, 2019
132
133
BOOL is_xbox = [controller.vendorName containsString: @"Xbox"];
BOOL is_ps4 = [controller.vendorName containsString: @"DUALSHOCK"];
Jan 30, 2020
Jan 30, 2020
134
#if TARGET_OS_TV
Dec 6, 2019
Dec 6, 2019
135
BOOL is_MFi = (!is_xbox && !is_ps4);
Jan 30, 2020
Jan 30, 2020
136
#endif
Jun 14, 2019
Jun 14, 2019
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
int nbuttons = 0;
/* These buttons are part of the original MFi spec */
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_X);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_Y);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
nbuttons += 6;
/* These buttons are available on some newer controllers */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
if ([gamepad respondsToSelector:@selector(leftThumbstickButton)] && gamepad.leftThumbstickButton) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK);
++nbuttons;
}
if ([gamepad respondsToSelector:@selector(rightThumbstickButton)] && gamepad.rightThumbstickButton) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK);
++nbuttons;
Jun 6, 2019
Jun 6, 2019
158
}
Jun 14, 2019
Jun 14, 2019
159
160
161
162
if ([gamepad respondsToSelector:@selector(buttonOptions)] && gamepad.buttonOptions) {
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_BACK);
++nbuttons;
}
Dec 6, 2019
Dec 6, 2019
163
164
165
166
167
168
169
170
171
172
BOOL has_direct_menu = [gamepad respondsToSelector:@selector(buttonMenu)] && gamepad.buttonMenu;
#if TARGET_OS_TV
/* On tvOS MFi controller menu button brings you to the home screen */
if (is_MFi) {
has_direct_menu = FALSE;
}
#endif
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
++nbuttons;
if (!has_direct_menu) {
Jun 14, 2019
Jun 14, 2019
173
174
175
device->uses_pause_handler = SDL_TRUE;
}
#pragma clang diagnostic pop
Jun 6, 2019
Jun 6, 2019
176
Dec 6, 2019
Dec 6, 2019
177
if (is_xbox) {
Jun 6, 2019
Jun 6, 2019
178
vendor = VENDOR_MICROSOFT;
Jun 14, 2019
Jun 14, 2019
179
product = 0x02E0; /* Assume Xbox One S BLE Controller unless/until GCController flows VID/PID */
Dec 6, 2019
Dec 6, 2019
180
} else if (is_ps4) {
Jun 6, 2019
Jun 6, 2019
181
vendor = VENDOR_SONY;
Jun 14, 2019
Jun 14, 2019
182
product = 0x09CC; /* Assume DS4 Slim unless/until GCController flows VID/PID */
Jun 6, 2019
Jun 6, 2019
183
184
185
186
187
} else {
vendor = VENDOR_APPLE;
product = 1;
subtype = 1;
}
Jun 14, 2019
Jun 14, 2019
188
Sep 21, 2015
Sep 21, 2015
189
190
device->naxes = 6; /* 2 thumbsticks and 2 triggers */
device->nhats = 1; /* d-pad */
Jun 6, 2019
Jun 6, 2019
191
device->nbuttons = nbuttons;
Jun 14, 2019
Jun 14, 2019
192
Sep 21, 2015
Sep 21, 2015
193
} else if (controller.gamepad) {
Jun 14, 2019
Jun 14, 2019
194
195
196
197
198
199
200
201
202
203
204
205
206
int nbuttons = 0;
/* These buttons are part of the original MFi spec */
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_X);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_Y);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
nbuttons += 7;
device->uses_pause_handler = SDL_TRUE;
Mar 9, 2018
Mar 9, 2018
207
208
209
vendor = VENDOR_APPLE;
product = 2;
subtype = 2;
Sep 21, 2015
Sep 21, 2015
210
211
device->naxes = 0; /* no traditional analog inputs */
device->nhats = 1; /* d-pad */
Jun 14, 2019
Jun 14, 2019
212
device->nbuttons = nbuttons;
Sep 21, 2015
Sep 21, 2015
213
}
Sep 14, 2016
Sep 14, 2016
214
215
#if TARGET_OS_TV
else if (controller.microGamepad) {
Jun 14, 2019
Jun 14, 2019
216
217
218
219
220
221
int nbuttons = 0;
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B); /* Button X on microGamepad */
nbuttons += 2;
Dec 6, 2019
Dec 6, 2019
222
223
224
device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
++nbuttons;
device->uses_pause_handler = SDL_TRUE;
Jun 14, 2019
Jun 14, 2019
225
Mar 9, 2018
Mar 9, 2018
226
227
228
vendor = VENDOR_APPLE;
product = 3;
subtype = 3;
Sep 14, 2016
Sep 14, 2016
229
230
device->naxes = 2; /* treat the touch surface as two axes */
device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
Jun 14, 2019
Jun 14, 2019
231
device->nbuttons = nbuttons;
Sep 17, 2016
Sep 17, 2016
232
Oct 8, 2016
Oct 8, 2016
233
controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
Sep 14, 2016
Sep 14, 2016
234
235
}
#endif /* TARGET_OS_TV */
Dec 11, 2015
Dec 11, 2015
236
Mar 8, 2018
Mar 8, 2018
237
238
/* 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. */
Aug 9, 2018
Aug 9, 2018
239
*guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_BLUETOOTH);
Mar 8, 2018
Mar 8, 2018
240
*guid16++ = 0;
Mar 9, 2018
Mar 9, 2018
241
242
243
244
*guid16++ = SDL_SwapLE16(vendor);
*guid16++ = 0;
*guid16++ = SDL_SwapLE16(product);
*guid16++ = 0;
Mar 8, 2018
Mar 8, 2018
245
Jun 14, 2019
Jun 14, 2019
246
247
248
249
250
251
252
*guid16++ = SDL_SwapLE16(device->button_mask);
if (subtype != 0) {
/* Note that this is an MFI controller and what subtype it is */
device->guid.data[14] = 'm';
device->guid.data[15] = subtype;
}
Mar 8, 2018
Mar 8, 2018
253
Dec 11, 2015
Dec 11, 2015
254
255
256
/* This will be set when the first button press of the controller is
* detected. */
controller.playerIndex = -1;
Sep 14, 2016
Sep 14, 2016
257
258
#endif /* SDL_JOYSTICK_MFI */
Sep 21, 2015
Sep 21, 2015
259
260
261
}
static void
Aug 9, 2018
Aug 9, 2018
262
IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
Sep 21, 2015
Sep 21, 2015
263
264
265
{
SDL_JoystickDeviceItem *device = deviceList;
Feb 6, 2018
Feb 6, 2018
266
267
268
269
270
271
272
273
274
#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
Sep 21, 2015
Sep 21, 2015
275
276
277
278
279
280
281
while (device != NULL) {
if (device->controller == controller) {
return;
}
device = device->next;
}
Feb 6, 2018
Feb 6, 2018
282
device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
Sep 21, 2015
Sep 21, 2015
283
284
285
286
287
if (device == NULL) {
return;
}
device->accelerometer = accelerometer;
Aug 9, 2018
Aug 9, 2018
288
device->instance_id = SDL_GetNextJoystickInstanceID();
Sep 21, 2015
Sep 21, 2015
289
290
if (accelerometer) {
Sep 14, 2016
Sep 14, 2016
291
292
293
294
#if TARGET_OS_TV
SDL_free(device);
return;
#else
Sep 21, 2015
Sep 21, 2015
295
296
297
298
299
300
301
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)));
Sep 14, 2016
Sep 14, 2016
302
#endif /* TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
303
} else if (controller) {
Aug 9, 2018
Aug 9, 2018
304
IOS_AddMFIJoystickDevice(device, controller);
Sep 21, 2015
Sep 21, 2015
305
306
307
308
309
310
311
312
313
314
315
316
317
318
}
if (deviceList == NULL) {
deviceList = device;
} else {
SDL_JoystickDeviceItem *lastdevice = deviceList;
while (lastdevice->next != NULL) {
lastdevice = lastdevice->next;
}
lastdevice->next = device;
}
++numjoysticks;
Aug 9, 2018
Aug 9, 2018
319
SDL_PrivateJoystickAdded(device->instance_id);
Sep 21, 2015
Sep 21, 2015
320
321
}
Sep 25, 2015
Sep 25, 2015
322
static SDL_JoystickDeviceItem *
Aug 9, 2018
Aug 9, 2018
323
IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
Sep 21, 2015
Sep 21, 2015
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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
{
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;
Sep 22, 2017
Sep 22, 2017
368
SDL_PrivateJoystickRemoved(device->instance_id);
Sep 21, 2015
Sep 21, 2015
369
Sep 21, 2015
Sep 21, 2015
370
371
372
SDL_free(device->name);
SDL_free(device);
Sep 21, 2015
Sep 21, 2015
373
374
return next;
}
Sep 17, 2016
Sep 17, 2016
376
#if TARGET_OS_TV
Aug 14, 2017
Aug 14, 2017
377
static void SDLCALL
Sep 17, 2016
Sep 17, 2016
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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 */
Aug 9, 2018
Aug 9, 2018
392
393
static int
IOS_JoystickInit(void)
Sep 21, 2015
Sep 21, 2015
395
396
397
@autoreleasepool {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
Sep 14, 2016
Sep 14, 2016
398
#if !TARGET_OS_TV
Oct 8, 2016
Oct 8, 2016
399
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
Sep 21, 2015
Sep 21, 2015
400
/* Default behavior, accelerometer as joystick */
Aug 9, 2018
Aug 9, 2018
401
IOS_AddJoystickDevice(nil, SDL_TRUE);
Sep 21, 2015
Sep 21, 2015
402
}
Sep 14, 2016
Sep 14, 2016
403
#endif /* !TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
404
405
406
407
#ifdef SDL_JOYSTICK_MFI
/* GameController.framework was added in iOS 7. */
if (![GCController class]) {
Aug 9, 2018
Aug 9, 2018
408
return 0;
Sep 21, 2015
Sep 21, 2015
409
410
411
}
for (GCController *controller in [GCController controllers]) {
Aug 9, 2018
Aug 9, 2018
412
IOS_AddJoystickDevice(controller, SDL_FALSE);
Sep 21, 2015
Sep 21, 2015
413
414
}
Sep 17, 2016
Sep 17, 2016
415
416
417
418
419
#if TARGET_OS_TV
SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
420
421
422
423
424
connectObserver = [center addObserverForName:GCControllerDidConnectNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
GCController *controller = note.object;
Aug 9, 2018
Aug 9, 2018
425
IOS_AddJoystickDevice(controller, SDL_FALSE);
Sep 21, 2015
Sep 21, 2015
426
427
428
429
430
431
432
433
434
435
}];
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) {
Aug 9, 2018
Aug 9, 2018
436
IOS_RemoveJoystickDevice(device);
Sep 21, 2015
Sep 21, 2015
437
438
439
440
441
442
break;
}
device = device->next;
}
}];
#endif /* SDL_JOYSTICK_MFI */
Aug 9, 2018
Aug 9, 2018
445
return 0;
Aug 9, 2018
Aug 9, 2018
448
449
static int
IOS_JoystickGetCount(void)
450
451
452
453
{
return numjoysticks;
}
Aug 9, 2018
Aug 9, 2018
454
455
static void
IOS_JoystickDetect(void)
Aug 9, 2018
Aug 9, 2018
459
460
static const char *
IOS_JoystickGetDeviceName(int device_index)
Sep 21, 2015
Sep 21, 2015
462
463
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? device->name : "Unknown";
Oct 25, 2018
Oct 25, 2018
466
467
468
static int
IOS_JoystickGetDevicePlayerIndex(int device_index)
{
Jun 14, 2019
Jun 14, 2019
469
470
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
return device ? (int)device->controller.playerIndex : -1;
Oct 25, 2018
Oct 25, 2018
471
472
}
Dec 21, 2019
Dec 21, 2019
473
474
475
476
477
478
479
480
481
static void
IOS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
if (device) {
device->controller.playerIndex = player_index;
}
}
Aug 9, 2018
Aug 9, 2018
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
static SDL_JoystickGUID
IOS_JoystickGetDeviceGUID( int device_index )
{
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
SDL_JoystickGUID guid;
if (device) {
guid = device->guid;
} else {
SDL_zero(guid);
}
return guid;
}
static SDL_JoystickID
IOS_JoystickGetDeviceInstanceID(int device_index)
Sep 21, 2015
Sep 21, 2015
498
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
Aug 9, 2018
Aug 9, 2018
499
return device ? device->instance_id : -1;
Aug 9, 2018
Aug 9, 2018
502
503
static int
IOS_JoystickOpen(SDL_Joystick * joystick, int device_index)
Sep 21, 2015
Sep 21, 2015
505
506
507
508
509
510
511
512
513
514
515
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;
516
joystick->nballs = 0;
Sep 21, 2015
Sep 21, 2015
517
518
device->joystick = joystick;
519
520
@autoreleasepool {
Sep 21, 2015
Sep 21, 2015
521
if (device->accelerometer) {
Sep 14, 2016
Sep 14, 2016
522
#if !TARGET_OS_TV
Sep 21, 2015
Sep 21, 2015
523
524
525
526
527
528
529
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
/* Shorter times between updates can significantly increase CPU usage. */
motionManager.accelerometerUpdateInterval = 0.1;
[motionManager startAccelerometerUpdates];
Sep 14, 2016
Sep 14, 2016
530
#endif /* !TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
531
532
} else {
#ifdef SDL_JOYSTICK_MFI
Jun 14, 2019
Jun 14, 2019
533
534
535
536
537
538
539
540
if (device->uses_pause_handler) {
GCController *controller = device->controller;
controller.controllerPausedHandler = ^(GCController *c) {
if (joystick->hwdata) {
++joystick->hwdata->num_pause_presses;
}
};
}
Sep 21, 2015
Sep 21, 2015
541
#endif /* SDL_JOYSTICK_MFI */
Feb 7, 2018
Feb 7, 2018
544
545
546
if (device->remote) {
++SDL_AppleTVRemoteOpenedAsJoystick;
}
547
548
549
550
return 0;
}
Sep 25, 2015
Sep 25, 2015
551
static void
Aug 9, 2018
Aug 9, 2018
552
IOS_AccelerometerUpdate(SDL_Joystick * joystick)
Sep 14, 2016
Sep 14, 2016
554
#if !TARGET_OS_TV
555
556
557
558
559
const float maxgforce = SDL_IPHONE_MAX_GFORCE;
const SInt16 maxsint16 = 0x7FFF;
CMAcceleration accel;
@autoreleasepool {
Sep 21, 2015
Sep 21, 2015
560
if (!motionManager.isAccelerometerActive) {
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
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 */
Sep 21, 2015
Sep 21, 2015
589
SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
590
SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
Sep 21, 2015
Sep 21, 2015
591
SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
Sep 14, 2016
Sep 14, 2016
592
#endif /* !TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
593
594
595
596
}
#ifdef SDL_JOYSTICK_MFI
static Uint8
Aug 9, 2018
Aug 9, 2018
597
IOS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
Sep 21, 2015
Sep 21, 2015
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
{
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
Aug 9, 2018
Aug 9, 2018
622
IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
Sep 21, 2015
Sep 21, 2015
623
{
Sep 14, 2016
Sep 14, 2016
624
#if SDL_JOYSTICK_MFI
Sep 21, 2015
Sep 21, 2015
625
626
627
628
@autoreleasepool {
GCController *controller = joystick->hwdata->controller;
Uint8 hatstate = SDL_HAT_CENTERED;
int i;
Jun 14, 2019
Jun 14, 2019
629
int pause_button_index = 0;
Sep 21, 2015
Sep 21, 2015
630
631
632
633
634
if (controller.extendedGamepad) {
GCExtendedGamepad *gamepad = controller.extendedGamepad;
/* Axis order matches the XInput Windows mappings. */
Dec 11, 2015
Dec 11, 2015
635
636
637
638
639
640
641
642
643
644
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. */
Jun 6, 2019
Jun 6, 2019
645
Uint8 buttons[joystick->nbuttons];
Jun 14, 2019
Jun 14, 2019
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
int button_count = 0;
/* These buttons are part of the original MFi spec */
buttons[button_count++] = gamepad.buttonA.isPressed;
buttons[button_count++] = gamepad.buttonB.isPressed;
buttons[button_count++] = gamepad.buttonX.isPressed;
buttons[button_count++] = gamepad.buttonY.isPressed;
buttons[button_count++] = gamepad.leftShoulder.isPressed;
buttons[button_count++] = gamepad.rightShoulder.isPressed;
/* These buttons are available on some newer controllers */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK)) {
buttons[button_count++] = gamepad.leftThumbstickButton.isPressed;
Jun 6, 2019
Jun 6, 2019
661
}
Jun 14, 2019
Jun 14, 2019
662
663
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK)) {
buttons[button_count++] = gamepad.rightThumbstickButton.isPressed;
Jun 6, 2019
Jun 6, 2019
664
}
Jun 14, 2019
Jun 14, 2019
665
666
667
668
669
670
671
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
buttons[button_count++] = gamepad.buttonOptions.isPressed;
}
/* This must be the last button, so we can optionally handle it with pause_button_index below */
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
if (joystick->hwdata->uses_pause_handler) {
pause_button_index = button_count;
Jul 13, 2019
Jul 13, 2019
672
buttons[button_count++] = joystick->delayed_guide_button;
Jun 14, 2019
Jun 14, 2019
673
674
675
676
677
} else {
buttons[button_count++] = gamepad.buttonMenu.isPressed;
}
}
#pragma clang diagnostic pop
Sep 21, 2015
Sep 21, 2015
678
Aug 9, 2018
Aug 9, 2018
679
hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
Sep 21, 2015
Sep 21, 2015
680
Dec 11, 2015
Dec 11, 2015
681
682
683
684
for (i = 0; i < SDL_arraysize(axes); i++) {
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
Jun 14, 2019
Jun 14, 2019
685
for (i = 0; i < button_count; i++) {
Dec 11, 2015
Dec 11, 2015
686
687
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
Sep 21, 2015
Sep 21, 2015
688
689
690
} else if (controller.gamepad) {
GCGamepad *gamepad = controller.gamepad;
Dec 11, 2015
Dec 11, 2015
691
/* Button order matches the XInput Windows mappings. */
Jun 14, 2019
Jun 14, 2019
692
693
694
695
696
697
698
699
700
Uint8 buttons[joystick->nbuttons];
int button_count = 0;
buttons[button_count++] = gamepad.buttonA.isPressed;
buttons[button_count++] = gamepad.buttonB.isPressed;
buttons[button_count++] = gamepad.buttonX.isPressed;
buttons[button_count++] = gamepad.buttonY.isPressed;
buttons[button_count++] = gamepad.leftShoulder.isPressed;
buttons[button_count++] = gamepad.rightShoulder.isPressed;
pause_button_index = button_count;
Jul 13, 2019
Jul 13, 2019
701
buttons[button_count++] = joystick->delayed_guide_button;
Dec 11, 2015
Dec 11, 2015
702
Aug 9, 2018
Aug 9, 2018
703
hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
Sep 21, 2015
Sep 21, 2015
704
Jun 14, 2019
Jun 14, 2019
705
for (i = 0; i < button_count; i++) {
Dec 11, 2015
Dec 11, 2015
706
707
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
Sep 21, 2015
Sep 21, 2015
708
}
Sep 14, 2016
Sep 14, 2016
709
710
711
712
713
714
715
716
717
718
719
720
721
#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++) {
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
}
Jun 14, 2019
Jun 14, 2019
722
723
724
725
726
727
728
729
730
731
Uint8 buttons[joystick->nbuttons];
int button_count = 0;
buttons[button_count++] = gamepad.buttonA.isPressed;
buttons[button_count++] = gamepad.buttonX.isPressed;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
/* This must be the last button, so we can optionally handle it with pause_button_index below */
if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
if (joystick->hwdata->uses_pause_handler) {
pause_button_index = button_count;
Jul 13, 2019
Jul 13, 2019
732
buttons[button_count++] = joystick->delayed_guide_button;
Jun 14, 2019
Jun 14, 2019
733
734
735
736
737
} else {
buttons[button_count++] = gamepad.buttonMenu.isPressed;
}
}
#pragma clang diagnostic pop
Sep 14, 2016
Sep 14, 2016
738
Jun 14, 2019
Jun 14, 2019
739
for (i = 0; i < button_count; i++) {
Sep 14, 2016
Sep 14, 2016
740
741
742
743
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
}
}
#endif /* TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
744
Dec 11, 2015
Dec 11, 2015
745
746
747
if (joystick->nhats > 0) {
SDL_PrivateJoystickHat(joystick, 0, hatstate);
}
Sep 21, 2015
Sep 21, 2015
748
Jun 14, 2019
Jun 14, 2019
749
750
751
752
753
754
if (joystick->hwdata->uses_pause_handler) {
for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_PRESSED);
SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_RELEASED);
}
joystick->hwdata->num_pause_presses = 0;
Sep 21, 2015
Sep 21, 2015
755
756
}
}
Sep 14, 2016
Sep 14, 2016
757
#endif /* SDL_JOYSTICK_MFI */
Aug 9, 2018
Aug 9, 2018
760
static int
Feb 4, 2020
Feb 4, 2020
761
IOS_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
Aug 9, 2018
Aug 9, 2018
762
763
764
765
766
767
{
return SDL_Unsupported();
}
static void
IOS_JoystickUpdate(SDL_Joystick * joystick)
Sep 21, 2015
Sep 21, 2015
769
770
771
772
773
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
Sep 22, 2017
Sep 22, 2017
774
Sep 21, 2015
Sep 21, 2015
775
if (device->accelerometer) {
Aug 9, 2018
Aug 9, 2018
776
IOS_AccelerometerUpdate(joystick);
Sep 21, 2015
Sep 21, 2015
777
} else if (device->controller) {
Aug 9, 2018
Aug 9, 2018
778
IOS_MFIJoystickUpdate(joystick);
Sep 21, 2015
Sep 21, 2015
779
}
Aug 9, 2018
Aug 9, 2018
782
783
static void
IOS_JoystickClose(SDL_Joystick * joystick)
Sep 21, 2015
Sep 21, 2015
785
786
787
788
789
790
791
792
SDL_JoystickDeviceItem *device = joystick->hwdata;
if (device == NULL) {
return;
}
device->joystick = NULL;
Sep 21, 2015
Sep 21, 2015
794
if (device->accelerometer) {
Sep 14, 2016
Sep 14, 2016
795
#if !TARGET_OS_TV
Sep 21, 2015
Sep 21, 2015
796
[motionManager stopAccelerometerUpdates];
Sep 14, 2016
Sep 14, 2016
797
#endif /* !TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
798
799
800
801
} else if (device->controller) {
#ifdef SDL_JOYSTICK_MFI
GCController *controller = device->controller;
controller.controllerPausedHandler = nil;
Dec 11, 2015
Dec 11, 2015
802
controller.playerIndex = -1;
Sep 21, 2015
Sep 21, 2015
803
804
#endif
}
Feb 7, 2018
Feb 7, 2018
806
807
808
if (device->remote) {
--SDL_AppleTVRemoteOpenedAsJoystick;
}
Aug 9, 2018
Aug 9, 2018
811
812
static void
IOS_JoystickQuit(void)
813
814
{
@autoreleasepool {
Sep 21, 2015
Sep 21, 2015
815
816
817
818
819
820
821
822
823
824
825
826
#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;
}
Sep 17, 2016
Sep 17, 2016
827
828
829
830
831
#if TARGET_OS_TV
SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
Sep 21, 2015
Sep 21, 2015
832
833
834
#endif /* SDL_JOYSTICK_MFI */
while (deviceList != NULL) {
Aug 9, 2018
Aug 9, 2018
835
IOS_RemoveJoystickDevice(deviceList);
Sep 21, 2015
Sep 21, 2015
836
837
}
Sep 14, 2016
Sep 14, 2016
838
#if !TARGET_OS_TV
839
motionManager = nil;
Sep 14, 2016
Sep 14, 2016
840
#endif /* !TARGET_OS_TV */
841
842
843
844
845
}
numjoysticks = 0;
}
Aug 9, 2018
Aug 9, 2018
846
SDL_JoystickDriver SDL_IOS_JoystickDriver =
Aug 9, 2018
Aug 9, 2018
848
849
850
851
IOS_JoystickInit,
IOS_JoystickGetCount,
IOS_JoystickDetect,
IOS_JoystickGetDeviceName,
Oct 25, 2018
Oct 25, 2018
852
IOS_JoystickGetDevicePlayerIndex,
Dec 21, 2019
Dec 21, 2019
853
IOS_JoystickSetDevicePlayerIndex,
Aug 9, 2018
Aug 9, 2018
854
855
856
857
858
859
860
861
IOS_JoystickGetDeviceGUID,
IOS_JoystickGetDeviceInstanceID,
IOS_JoystickOpen,
IOS_JoystickRumble,
IOS_JoystickUpdate,
IOS_JoystickClose,
IOS_JoystickQuit,
};
862
863
/* vi: set ts=4 sw=4 expandtab: */