SDL_sysjoystick.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. /* This is the iOS implementation of the SDL joystick API */
  20. #include "SDL_sysjoystick_c.h"
  21. /* needed for SDL_IPHONE_MAX_GFORCE macro */
  22. #include "SDL_config_iphoneos.h"
  23. #include "SDL_assert.h"
  24. #include "SDL_events.h"
  25. #include "SDL_joystick.h"
  26. #include "SDL_hints.h"
  27. #include "SDL_stdinc.h"
  28. #include "../SDL_sysjoystick.h"
  29. #include "../SDL_joystick_c.h"
  30. #if !SDL_EVENTS_DISABLED
  31. #include "../../events/SDL_events_c.h"
  32. #endif
  33. #if !TARGET_OS_TV
  34. #import <CoreMotion/CoreMotion.h>
  35. #endif
  36. #ifdef SDL_JOYSTICK_MFI
  37. #import <GameController/GameController.h>
  38. static id connectObserver = nil;
  39. static id disconnectObserver = nil;
  40. #endif /* SDL_JOYSTICK_MFI */
  41. #if !TARGET_OS_TV
  42. static const char *accelerometerName = "iOS Accelerometer";
  43. static CMMotionManager *motionManager = nil;
  44. #endif /* !TARGET_OS_TV */
  45. static SDL_JoystickDeviceItem *deviceList = NULL;
  46. static int numjoysticks = 0;
  47. int SDL_AppleTVRemoteOpenedAsJoystick = 0;
  48. static SDL_JoystickDeviceItem *
  49. GetDeviceForIndex(int device_index)
  50. {
  51. SDL_JoystickDeviceItem *device = deviceList;
  52. int i = 0;
  53. while (i < device_index) {
  54. if (device == NULL) {
  55. return NULL;
  56. }
  57. device = device->next;
  58. i++;
  59. }
  60. return device;
  61. }
  62. static void
  63. IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
  64. {
  65. #ifdef SDL_JOYSTICK_MFI
  66. const Uint16 VENDOR_APPLE = 0x05AC;
  67. Uint16 *guid16 = (Uint16 *)device->guid.data;
  68. Uint16 vendor = 0;
  69. Uint16 product = 0;
  70. Uint16 version = 0;
  71. Uint8 subtype = 0;
  72. const char *name = NULL;
  73. /* Explicitly retain the controller because SDL_JoystickDeviceItem is a
  74. * struct, and ARC doesn't work with structs. */
  75. device->controller = (__bridge GCController *) CFBridgingRetain(controller);
  76. if (controller.vendorName) {
  77. name = controller.vendorName.UTF8String;
  78. }
  79. if (!name) {
  80. name = "MFi Gamepad";
  81. }
  82. device->name = SDL_strdup(name);
  83. if (controller.extendedGamepad) {
  84. vendor = VENDOR_APPLE;
  85. product = 1;
  86. subtype = 1;
  87. device->naxes = 6; /* 2 thumbsticks and 2 triggers */
  88. device->nhats = 1; /* d-pad */
  89. device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
  90. } else if (controller.gamepad) {
  91. vendor = VENDOR_APPLE;
  92. product = 2;
  93. subtype = 2;
  94. device->naxes = 0; /* no traditional analog inputs */
  95. device->nhats = 1; /* d-pad */
  96. device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
  97. }
  98. #if TARGET_OS_TV
  99. else if (controller.microGamepad) {
  100. vendor = VENDOR_APPLE;
  101. product = 3;
  102. subtype = 3;
  103. device->naxes = 2; /* treat the touch surface as two axes */
  104. device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
  105. device->nbuttons = 3; /* AX, pause button */
  106. controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
  107. }
  108. #endif /* TARGET_OS_TV */
  109. /* We only need 16 bits for each of these; space them out to fill 128. */
  110. /* Byteswap so devices get same GUID on little/big endian platforms. */
  111. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_BLUETOOTH);
  112. *guid16++ = 0;
  113. *guid16++ = SDL_SwapLE16(vendor);
  114. *guid16++ = 0;
  115. *guid16++ = SDL_SwapLE16(product);
  116. *guid16++ = 0;
  117. *guid16++ = SDL_SwapLE16(version);
  118. *guid16++ = 0;
  119. /* Note that this is an MFI controller and what subtype it is */
  120. device->guid.data[14] = 'm';
  121. device->guid.data[15] = subtype;
  122. /* This will be set when the first button press of the controller is
  123. * detected. */
  124. controller.playerIndex = -1;
  125. #endif /* SDL_JOYSTICK_MFI */
  126. }
  127. static void
  128. IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
  129. {
  130. SDL_JoystickDeviceItem *device = deviceList;
  131. #if TARGET_OS_TV
  132. if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
  133. /* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
  134. if (controller && !controller.extendedGamepad && !controller.gamepad && controller.microGamepad) {
  135. return;
  136. }
  137. }
  138. #endif
  139. while (device != NULL) {
  140. if (device->controller == controller) {
  141. return;
  142. }
  143. device = device->next;
  144. }
  145. device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
  146. if (device == NULL) {
  147. return;
  148. }
  149. device->accelerometer = accelerometer;
  150. device->instance_id = SDL_GetNextJoystickInstanceID();
  151. if (accelerometer) {
  152. #if TARGET_OS_TV
  153. SDL_free(device);
  154. return;
  155. #else
  156. device->name = SDL_strdup(accelerometerName);
  157. device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
  158. device->nhats = 0;
  159. device->nbuttons = 0;
  160. /* Use the accelerometer name as a GUID. */
  161. SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
  162. #endif /* TARGET_OS_TV */
  163. } else if (controller) {
  164. IOS_AddMFIJoystickDevice(device, controller);
  165. }
  166. if (deviceList == NULL) {
  167. deviceList = device;
  168. } else {
  169. SDL_JoystickDeviceItem *lastdevice = deviceList;
  170. while (lastdevice->next != NULL) {
  171. lastdevice = lastdevice->next;
  172. }
  173. lastdevice->next = device;
  174. }
  175. ++numjoysticks;
  176. SDL_PrivateJoystickAdded(device->instance_id);
  177. }
  178. static SDL_JoystickDeviceItem *
  179. IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
  180. {
  181. SDL_JoystickDeviceItem *prev = NULL;
  182. SDL_JoystickDeviceItem *next = NULL;
  183. SDL_JoystickDeviceItem *item = deviceList;
  184. if (device == NULL) {
  185. return NULL;
  186. }
  187. next = device->next;
  188. while (item != NULL) {
  189. if (item == device) {
  190. break;
  191. }
  192. prev = item;
  193. item = item->next;
  194. }
  195. /* Unlink the device item from the device list. */
  196. if (prev) {
  197. prev->next = device->next;
  198. } else if (device == deviceList) {
  199. deviceList = device->next;
  200. }
  201. if (device->joystick) {
  202. device->joystick->hwdata = NULL;
  203. }
  204. #ifdef SDL_JOYSTICK_MFI
  205. @autoreleasepool {
  206. if (device->controller) {
  207. /* The controller was explicitly retained in the struct, so it
  208. * should be explicitly released before freeing the struct. */
  209. GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
  210. controller.controllerPausedHandler = nil;
  211. device->controller = nil;
  212. }
  213. }
  214. #endif /* SDL_JOYSTICK_MFI */
  215. --numjoysticks;
  216. SDL_PrivateJoystickRemoved(device->instance_id);
  217. SDL_free(device->name);
  218. SDL_free(device);
  219. return next;
  220. }
  221. #if TARGET_OS_TV
  222. static void SDLCALL
  223. SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
  224. {
  225. BOOL allowRotation = newValue != NULL && *newValue != '0';
  226. @autoreleasepool {
  227. for (GCController *controller in [GCController controllers]) {
  228. if (controller.microGamepad) {
  229. controller.microGamepad.allowsRotation = allowRotation;
  230. }
  231. }
  232. }
  233. }
  234. #endif /* TARGET_OS_TV */
  235. static int
  236. IOS_JoystickInit(void)
  237. {
  238. @autoreleasepool {
  239. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  240. #if !TARGET_OS_TV
  241. if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
  242. /* Default behavior, accelerometer as joystick */
  243. IOS_AddJoystickDevice(nil, SDL_TRUE);
  244. }
  245. #endif /* !TARGET_OS_TV */
  246. #ifdef SDL_JOYSTICK_MFI
  247. /* GameController.framework was added in iOS 7. */
  248. if (![GCController class]) {
  249. return 0;
  250. }
  251. for (GCController *controller in [GCController controllers]) {
  252. IOS_AddJoystickDevice(controller, SDL_FALSE);
  253. }
  254. #if TARGET_OS_TV
  255. SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
  256. SDL_AppleTVRemoteRotationHintChanged, NULL);
  257. #endif /* TARGET_OS_TV */
  258. connectObserver = [center addObserverForName:GCControllerDidConnectNotification
  259. object:nil
  260. queue:nil
  261. usingBlock:^(NSNotification *note) {
  262. GCController *controller = note.object;
  263. IOS_AddJoystickDevice(controller, SDL_FALSE);
  264. }];
  265. disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
  266. object:nil
  267. queue:nil
  268. usingBlock:^(NSNotification *note) {
  269. GCController *controller = note.object;
  270. SDL_JoystickDeviceItem *device = deviceList;
  271. while (device != NULL) {
  272. if (device->controller == controller) {
  273. IOS_RemoveJoystickDevice(device);
  274. break;
  275. }
  276. device = device->next;
  277. }
  278. }];
  279. #endif /* SDL_JOYSTICK_MFI */
  280. }
  281. return 0;
  282. }
  283. static int
  284. IOS_JoystickGetCount(void)
  285. {
  286. return numjoysticks;
  287. }
  288. static void
  289. IOS_JoystickDetect(void)
  290. {
  291. }
  292. static const char *
  293. IOS_JoystickGetDeviceName(int device_index)
  294. {
  295. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  296. return device ? device->name : "Unknown";
  297. }
  298. static int
  299. IOS_JoystickGetDevicePlayerIndex(int device_index)
  300. {
  301. return -1;
  302. }
  303. static SDL_JoystickGUID
  304. IOS_JoystickGetDeviceGUID( int device_index )
  305. {
  306. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  307. SDL_JoystickGUID guid;
  308. if (device) {
  309. guid = device->guid;
  310. } else {
  311. SDL_zero(guid);
  312. }
  313. return guid;
  314. }
  315. static SDL_JoystickID
  316. IOS_JoystickGetDeviceInstanceID(int device_index)
  317. {
  318. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  319. return device ? device->instance_id : -1;
  320. }
  321. static int
  322. IOS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  323. {
  324. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  325. if (device == NULL) {
  326. return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
  327. }
  328. joystick->hwdata = device;
  329. joystick->instance_id = device->instance_id;
  330. joystick->naxes = device->naxes;
  331. joystick->nhats = device->nhats;
  332. joystick->nbuttons = device->nbuttons;
  333. joystick->nballs = 0;
  334. device->joystick = joystick;
  335. @autoreleasepool {
  336. if (device->accelerometer) {
  337. #if !TARGET_OS_TV
  338. if (motionManager == nil) {
  339. motionManager = [[CMMotionManager alloc] init];
  340. }
  341. /* Shorter times between updates can significantly increase CPU usage. */
  342. motionManager.accelerometerUpdateInterval = 0.1;
  343. [motionManager startAccelerometerUpdates];
  344. #endif /* !TARGET_OS_TV */
  345. } else {
  346. #ifdef SDL_JOYSTICK_MFI
  347. GCController *controller = device->controller;
  348. controller.controllerPausedHandler = ^(GCController *c) {
  349. if (joystick->hwdata) {
  350. ++joystick->hwdata->num_pause_presses;
  351. }
  352. };
  353. #endif /* SDL_JOYSTICK_MFI */
  354. }
  355. }
  356. if (device->remote) {
  357. ++SDL_AppleTVRemoteOpenedAsJoystick;
  358. }
  359. return 0;
  360. }
  361. static void
  362. IOS_AccelerometerUpdate(SDL_Joystick * joystick)
  363. {
  364. #if !TARGET_OS_TV
  365. const float maxgforce = SDL_IPHONE_MAX_GFORCE;
  366. const SInt16 maxsint16 = 0x7FFF;
  367. CMAcceleration accel;
  368. @autoreleasepool {
  369. if (!motionManager.isAccelerometerActive) {
  370. return;
  371. }
  372. accel = motionManager.accelerometerData.acceleration;
  373. }
  374. /*
  375. Convert accelerometer data from floating point to Sint16, which is what
  376. the joystick system expects.
  377. To do the conversion, the data is first clamped onto the interval
  378. [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
  379. by MAX_SINT16 so that it is mapped to the full range of an Sint16.
  380. You can customize the clamped range of this function by modifying the
  381. SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
  382. Once converted to Sint16, the accelerometer data no longer has coherent
  383. units. You can convert the data back to units of g-force by multiplying
  384. it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
  385. */
  386. /* clamp the data */
  387. accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
  388. accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
  389. accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
  390. /* pass in data mapped to range of SInt16 */
  391. SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
  392. SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
  393. SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
  394. #endif /* !TARGET_OS_TV */
  395. }
  396. #ifdef SDL_JOYSTICK_MFI
  397. static Uint8
  398. IOS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
  399. {
  400. Uint8 hat = 0;
  401. if (dpad.up.isPressed) {
  402. hat |= SDL_HAT_UP;
  403. } else if (dpad.down.isPressed) {
  404. hat |= SDL_HAT_DOWN;
  405. }
  406. if (dpad.left.isPressed) {
  407. hat |= SDL_HAT_LEFT;
  408. } else if (dpad.right.isPressed) {
  409. hat |= SDL_HAT_RIGHT;
  410. }
  411. if (hat == 0) {
  412. return SDL_HAT_CENTERED;
  413. }
  414. return hat;
  415. }
  416. #endif
  417. static void
  418. IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
  419. {
  420. #if SDL_JOYSTICK_MFI
  421. @autoreleasepool {
  422. GCController *controller = joystick->hwdata->controller;
  423. Uint8 hatstate = SDL_HAT_CENTERED;
  424. int i;
  425. int updateplayerindex = 0;
  426. if (controller.extendedGamepad) {
  427. GCExtendedGamepad *gamepad = controller.extendedGamepad;
  428. /* Axis order matches the XInput Windows mappings. */
  429. Sint16 axes[] = {
  430. (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
  431. (Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
  432. (Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
  433. (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
  434. (Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
  435. (Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
  436. };
  437. /* Button order matches the XInput Windows mappings. */
  438. Uint8 buttons[] = {
  439. gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
  440. gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
  441. gamepad.leftShoulder.isPressed,
  442. gamepad.rightShoulder.isPressed,
  443. };
  444. hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
  445. for (i = 0; i < SDL_arraysize(axes); i++) {
  446. /* The triggers (axes 2 and 5) are resting at -32768 but SDL
  447. * initializes its values to 0. We only want to make sure the
  448. * player index is up to date if the user actually moves an axis. */
  449. if ((i != 2 && i != 5) || axes[i] != -32768) {
  450. updateplayerindex |= (joystick->axes[i].value != axes[i]);
  451. }
  452. SDL_PrivateJoystickAxis(joystick, i, axes[i]);
  453. }
  454. for (i = 0; i < SDL_arraysize(buttons); i++) {
  455. updateplayerindex |= (joystick->buttons[i] != buttons[i]);
  456. SDL_PrivateJoystickButton(joystick, i, buttons[i]);
  457. }
  458. } else if (controller.gamepad) {
  459. GCGamepad *gamepad = controller.gamepad;
  460. /* Button order matches the XInput Windows mappings. */
  461. Uint8 buttons[] = {
  462. gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
  463. gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
  464. gamepad.leftShoulder.isPressed,
  465. gamepad.rightShoulder.isPressed,
  466. };
  467. hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
  468. for (i = 0; i < SDL_arraysize(buttons); i++) {
  469. updateplayerindex |= (joystick->buttons[i] != buttons[i]);
  470. SDL_PrivateJoystickButton(joystick, i, buttons[i]);
  471. }
  472. }
  473. #if TARGET_OS_TV
  474. else if (controller.microGamepad) {
  475. GCMicroGamepad *gamepad = controller.microGamepad;
  476. Sint16 axes[] = {
  477. (Sint16) (gamepad.dpad.xAxis.value * 32767),
  478. (Sint16) (gamepad.dpad.yAxis.value * -32767),
  479. };
  480. for (i = 0; i < SDL_arraysize(axes); i++) {
  481. updateplayerindex |= (joystick->axes[i].value != axes[i]);
  482. SDL_PrivateJoystickAxis(joystick, i, axes[i]);
  483. }
  484. Uint8 buttons[] = {
  485. gamepad.buttonA.isPressed,
  486. gamepad.buttonX.isPressed,
  487. };
  488. for (i = 0; i < SDL_arraysize(buttons); i++) {
  489. updateplayerindex |= (joystick->buttons[i] != buttons[i]);
  490. SDL_PrivateJoystickButton(joystick, i, buttons[i]);
  491. }
  492. }
  493. #endif /* TARGET_OS_TV */
  494. if (joystick->nhats > 0) {
  495. updateplayerindex |= (joystick->hats[0] != hatstate);
  496. SDL_PrivateJoystickHat(joystick, 0, hatstate);
  497. }
  498. for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
  499. const Uint8 pausebutton = joystick->nbuttons - 1; /* The pause button is always last. */
  500. SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
  501. SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
  502. updateplayerindex = YES;
  503. }
  504. joystick->hwdata->num_pause_presses = 0;
  505. if (updateplayerindex && controller.playerIndex == -1) {
  506. BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
  507. /* Find the player index of all other connected controllers. */
  508. for (GCController *c in [GCController controllers]) {
  509. if (c != controller && c.playerIndex >= 0) {
  510. usedPlayerIndexSlots[c.playerIndex] = YES;
  511. }
  512. }
  513. /* Set this controller's player index to the first unused index.
  514. * FIXME: This logic isn't great... but SDL doesn't expose this
  515. * concept in its external API, so we don't have much to go on. */
  516. for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
  517. if (!usedPlayerIndexSlots[i]) {
  518. controller.playerIndex = i;
  519. break;
  520. }
  521. }
  522. }
  523. }
  524. #endif /* SDL_JOYSTICK_MFI */
  525. }
  526. static int
  527. IOS_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  528. {
  529. return SDL_Unsupported();
  530. }
  531. static void
  532. IOS_JoystickUpdate(SDL_Joystick * joystick)
  533. {
  534. SDL_JoystickDeviceItem *device = joystick->hwdata;
  535. if (device == NULL) {
  536. return;
  537. }
  538. if (device->accelerometer) {
  539. IOS_AccelerometerUpdate(joystick);
  540. } else if (device->controller) {
  541. IOS_MFIJoystickUpdate(joystick);
  542. }
  543. }
  544. static void
  545. IOS_JoystickClose(SDL_Joystick * joystick)
  546. {
  547. SDL_JoystickDeviceItem *device = joystick->hwdata;
  548. if (device == NULL) {
  549. return;
  550. }
  551. device->joystick = NULL;
  552. @autoreleasepool {
  553. if (device->accelerometer) {
  554. #if !TARGET_OS_TV
  555. [motionManager stopAccelerometerUpdates];
  556. #endif /* !TARGET_OS_TV */
  557. } else if (device->controller) {
  558. #ifdef SDL_JOYSTICK_MFI
  559. GCController *controller = device->controller;
  560. controller.controllerPausedHandler = nil;
  561. controller.playerIndex = -1;
  562. #endif
  563. }
  564. }
  565. if (device->remote) {
  566. --SDL_AppleTVRemoteOpenedAsJoystick;
  567. }
  568. }
  569. static void
  570. IOS_JoystickQuit(void)
  571. {
  572. @autoreleasepool {
  573. #ifdef SDL_JOYSTICK_MFI
  574. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  575. if (connectObserver) {
  576. [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
  577. connectObserver = nil;
  578. }
  579. if (disconnectObserver) {
  580. [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
  581. disconnectObserver = nil;
  582. }
  583. #if TARGET_OS_TV
  584. SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
  585. SDL_AppleTVRemoteRotationHintChanged, NULL);
  586. #endif /* TARGET_OS_TV */
  587. #endif /* SDL_JOYSTICK_MFI */
  588. while (deviceList != NULL) {
  589. IOS_RemoveJoystickDevice(deviceList);
  590. }
  591. #if !TARGET_OS_TV
  592. motionManager = nil;
  593. #endif /* !TARGET_OS_TV */
  594. }
  595. numjoysticks = 0;
  596. }
  597. SDL_JoystickDriver SDL_IOS_JoystickDriver =
  598. {
  599. IOS_JoystickInit,
  600. IOS_JoystickGetCount,
  601. IOS_JoystickDetect,
  602. IOS_JoystickGetDeviceName,
  603. IOS_JoystickGetDevicePlayerIndex,
  604. IOS_JoystickGetDeviceGUID,
  605. IOS_JoystickGetDeviceInstanceID,
  606. IOS_JoystickOpen,
  607. IOS_JoystickRumble,
  608. IOS_JoystickUpdate,
  609. IOS_JoystickClose,
  610. IOS_JoystickQuit,
  611. };
  612. /* vi: set ts=4 sw=4 expandtab: */