SDL_sysjoystick.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <[email protected]>
  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 "../../../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. #include "../usb_ids.h"
  31. #if !SDL_EVENTS_DISABLED
  32. #include "../../events/SDL_events_c.h"
  33. #endif
  34. #if !TARGET_OS_TV
  35. #import <CoreMotion/CoreMotion.h>
  36. #endif
  37. #ifdef SDL_JOYSTICK_MFI
  38. #import <GameController/GameController.h>
  39. static id connectObserver = nil;
  40. static id disconnectObserver = nil;
  41. #include <Availability.h>
  42. #include <objc/message.h>
  43. /* remove compilation warnings for strict builds by defining these selectors, even though
  44. * they are only ever used indirectly through objc_msgSend
  45. */
  46. @interface GCExtendedGamepad (SDL)
  47. #if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 121000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 121000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1401000))
  48. @property (nonatomic, readonly, nullable) GCControllerButtonInput *leftThumbstickButton;
  49. @property (nonatomic, readonly, nullable) GCControllerButtonInput *rightThumbstickButton;
  50. #endif
  51. #if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 130000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1500000))
  52. @property (nonatomic, readonly) GCControllerButtonInput *buttonMenu;
  53. @property (nonatomic, readonly, nullable) GCControllerButtonInput *buttonOptions;
  54. #endif
  55. #if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 140000) || (__MAC_OS_VERSION_MAX_ALLOWED > 1500000))
  56. @property (nonatomic, readonly, nullable) GCControllerButtonInput *buttonHome;
  57. #endif
  58. @end
  59. @interface GCMicroGamepad (SDL)
  60. #if !((__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 130000) || (__MAC_OS_VERSION_MAX_ALLOWED >= 1500000))
  61. @property (nonatomic, readonly) GCControllerButtonInput *buttonMenu;
  62. #endif
  63. @end
  64. #if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000) || (__APPLETV_OS_VERSION_MAX_ALLOWED >= 140000) || (__MAC_OS_VERSION_MAX_ALLOWED > 1500000)
  65. #define ENABLE_MFI_BATTERY
  66. #define ENABLE_MFI_RUMBLE
  67. #define ENABLE_MFI_LIGHT
  68. #define ENABLE_PHYSICAL_INPUT_PROFILE
  69. #endif
  70. #ifdef ENABLE_MFI_RUMBLE
  71. #import <CoreHaptics/CoreHaptics.h>
  72. #endif
  73. #endif /* SDL_JOYSTICK_MFI */
  74. #if !TARGET_OS_TV
  75. static const char *accelerometerName = "iOS Accelerometer";
  76. static CMMotionManager *motionManager = nil;
  77. #endif /* !TARGET_OS_TV */
  78. static SDL_JoystickDeviceItem *deviceList = NULL;
  79. static int numjoysticks = 0;
  80. int SDL_AppleTVRemoteOpenedAsJoystick = 0;
  81. static SDL_JoystickDeviceItem *
  82. GetDeviceForIndex(int device_index)
  83. {
  84. SDL_JoystickDeviceItem *device = deviceList;
  85. int i = 0;
  86. while (i < device_index) {
  87. if (device == NULL) {
  88. return NULL;
  89. }
  90. device = device->next;
  91. i++;
  92. }
  93. return device;
  94. }
  95. static void
  96. IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
  97. {
  98. #ifdef SDL_JOYSTICK_MFI
  99. Uint16 *guid16 = (Uint16 *)device->guid.data;
  100. Uint16 vendor = 0;
  101. Uint16 product = 0;
  102. Uint8 subtype = 0;
  103. const char *name = NULL;
  104. /* Explicitly retain the controller because SDL_JoystickDeviceItem is a
  105. * struct, and ARC doesn't work with structs. */
  106. device->controller = (__bridge GCController *) CFBridgingRetain(controller);
  107. if (controller.vendorName) {
  108. name = controller.vendorName.UTF8String;
  109. }
  110. if (!name) {
  111. name = "MFi Gamepad";
  112. }
  113. device->name = SDL_CreateJoystickName(0, 0, NULL, name);
  114. if (controller.extendedGamepad) {
  115. GCExtendedGamepad *gamepad = controller.extendedGamepad;
  116. BOOL is_xbox = [controller.vendorName containsString: @"Xbox"];
  117. BOOL is_ps4 = [controller.vendorName containsString: @"DUALSHOCK"];
  118. #if TARGET_OS_TV
  119. BOOL is_MFi = (!is_xbox && !is_ps4);
  120. #endif
  121. int nbuttons = 0;
  122. /* These buttons are part of the original MFi spec */
  123. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
  124. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B);
  125. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_X);
  126. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_Y);
  127. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
  128. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
  129. nbuttons += 6;
  130. /* These buttons are available on some newer controllers */
  131. #pragma clang diagnostic push
  132. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  133. if ([gamepad respondsToSelector:@selector(leftThumbstickButton)] && gamepad.leftThumbstickButton) {
  134. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK);
  135. ++nbuttons;
  136. }
  137. if ([gamepad respondsToSelector:@selector(rightThumbstickButton)] && gamepad.rightThumbstickButton) {
  138. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK);
  139. ++nbuttons;
  140. }
  141. if ([gamepad respondsToSelector:@selector(buttonOptions)] && gamepad.buttonOptions) {
  142. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_BACK);
  143. ++nbuttons;
  144. }
  145. if ([gamepad respondsToSelector:@selector(buttonHome)] && gamepad.buttonHome) {
  146. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_GUIDE);
  147. ++nbuttons;
  148. }
  149. BOOL has_direct_menu = [gamepad respondsToSelector:@selector(buttonMenu)] && gamepad.buttonMenu;
  150. #if TARGET_OS_TV
  151. /* On tvOS MFi controller menu button brings you to the home screen */
  152. if (is_MFi) {
  153. has_direct_menu = FALSE;
  154. }
  155. #endif
  156. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
  157. ++nbuttons;
  158. if (!has_direct_menu) {
  159. device->uses_pause_handler = SDL_TRUE;
  160. }
  161. #ifdef ENABLE_PHYSICAL_INPUT_PROFILE
  162. if ([controller respondsToSelector:@selector(physicalInputProfile)]) {
  163. if (controller.physicalInputProfile.buttons[GCInputDualShockTouchpadButton] != nil) {
  164. device->has_dualshock_touchpad = SDL_TRUE;
  165. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_MISC1);
  166. ++nbuttons;
  167. }
  168. if (controller.physicalInputProfile.buttons[GCInputXboxPaddleOne] != nil) {
  169. device->has_xbox_paddles = SDL_TRUE;
  170. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE1);
  171. ++nbuttons;
  172. }
  173. if (controller.physicalInputProfile.buttons[GCInputXboxPaddleTwo] != nil) {
  174. device->has_xbox_paddles = SDL_TRUE;
  175. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE2);
  176. ++nbuttons;
  177. }
  178. if (controller.physicalInputProfile.buttons[GCInputXboxPaddleThree] != nil) {
  179. device->has_xbox_paddles = SDL_TRUE;
  180. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE3);
  181. ++nbuttons;
  182. }
  183. if (controller.physicalInputProfile.buttons[GCInputXboxPaddleFour] != nil) {
  184. device->has_xbox_paddles = SDL_TRUE;
  185. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_PADDLE4);
  186. ++nbuttons;
  187. }
  188. }
  189. #endif
  190. #pragma clang diagnostic pop
  191. if (is_xbox) {
  192. vendor = USB_VENDOR_MICROSOFT;
  193. if (device->has_xbox_paddles) {
  194. /* Assume Xbox One Elite Series 2 Controller unless/until GCController flows VID/PID */
  195. product = USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH;
  196. subtype = 1;
  197. } else {
  198. /* Assume Xbox One S BLE Controller unless/until GCController flows VID/PID */
  199. product = USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH;
  200. subtype = 0;
  201. }
  202. } else if (is_ps4) {
  203. /* Assume DS4 Slim unless/until GCController flows VID/PID */
  204. vendor = USB_VENDOR_SONY;
  205. product = USB_PRODUCT_SONY_DS4_SLIM;
  206. if ( device->has_dualshock_touchpad ) {
  207. subtype = 1;
  208. } else {
  209. subtype = 0;
  210. }
  211. } else {
  212. vendor = USB_VENDOR_APPLE;
  213. product = 1;
  214. subtype = 1;
  215. }
  216. device->naxes = 6; /* 2 thumbsticks and 2 triggers */
  217. device->nhats = 1; /* d-pad */
  218. device->nbuttons = nbuttons;
  219. } else if (controller.gamepad) {
  220. int nbuttons = 0;
  221. /* These buttons are part of the original MFi spec */
  222. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
  223. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B);
  224. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_X);
  225. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_Y);
  226. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
  227. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
  228. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
  229. nbuttons += 7;
  230. device->uses_pause_handler = SDL_TRUE;
  231. vendor = USB_VENDOR_APPLE;
  232. product = 2;
  233. subtype = 2;
  234. device->naxes = 0; /* no traditional analog inputs */
  235. device->nhats = 1; /* d-pad */
  236. device->nbuttons = nbuttons;
  237. }
  238. #if TARGET_OS_TV
  239. else if (controller.microGamepad) {
  240. int nbuttons = 0;
  241. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_A);
  242. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_B); /* Button X on microGamepad */
  243. nbuttons += 2;
  244. device->button_mask |= (1 << SDL_CONTROLLER_BUTTON_START);
  245. ++nbuttons;
  246. device->uses_pause_handler = SDL_TRUE;
  247. vendor = USB_VENDOR_APPLE;
  248. product = 3;
  249. subtype = 3;
  250. device->naxes = 2; /* treat the touch surface as two axes */
  251. device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
  252. device->nbuttons = nbuttons;
  253. controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
  254. }
  255. #endif /* TARGET_OS_TV */
  256. /* We only need 16 bits for each of these; space them out to fill 128. */
  257. /* Byteswap so devices get same GUID on little/big endian platforms. */
  258. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_BLUETOOTH);
  259. *guid16++ = 0;
  260. *guid16++ = SDL_SwapLE16(vendor);
  261. *guid16++ = 0;
  262. *guid16++ = SDL_SwapLE16(product);
  263. *guid16++ = 0;
  264. *guid16++ = SDL_SwapLE16(device->button_mask);
  265. if (subtype != 0) {
  266. /* Note that this is an MFI controller and what subtype it is */
  267. device->guid.data[14] = 'm';
  268. device->guid.data[15] = subtype;
  269. }
  270. /* This will be set when the first button press of the controller is
  271. * detected. */
  272. controller.playerIndex = -1;
  273. #endif /* SDL_JOYSTICK_MFI */
  274. }
  275. static void
  276. IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
  277. {
  278. SDL_JoystickDeviceItem *device = deviceList;
  279. #if TARGET_OS_TV
  280. if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
  281. /* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
  282. if (controller && !controller.extendedGamepad && !controller.gamepad && controller.microGamepad) {
  283. return;
  284. }
  285. }
  286. #endif
  287. while (device != NULL) {
  288. if (device->controller == controller) {
  289. return;
  290. }
  291. device = device->next;
  292. }
  293. device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
  294. if (device == NULL) {
  295. return;
  296. }
  297. device->accelerometer = accelerometer;
  298. device->instance_id = SDL_GetNextJoystickInstanceID();
  299. if (accelerometer) {
  300. #if TARGET_OS_TV
  301. SDL_free(device);
  302. return;
  303. #else
  304. device->name = SDL_strdup(accelerometerName);
  305. device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
  306. device->nhats = 0;
  307. device->nbuttons = 0;
  308. /* Use the accelerometer name as a GUID. */
  309. SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
  310. #endif /* TARGET_OS_TV */
  311. } else if (controller) {
  312. IOS_AddMFIJoystickDevice(device, controller);
  313. }
  314. if (deviceList == NULL) {
  315. deviceList = device;
  316. } else {
  317. SDL_JoystickDeviceItem *lastdevice = deviceList;
  318. while (lastdevice->next != NULL) {
  319. lastdevice = lastdevice->next;
  320. }
  321. lastdevice->next = device;
  322. }
  323. ++numjoysticks;
  324. SDL_PrivateJoystickAdded(device->instance_id);
  325. }
  326. static SDL_JoystickDeviceItem *
  327. IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
  328. {
  329. SDL_JoystickDeviceItem *prev = NULL;
  330. SDL_JoystickDeviceItem *next = NULL;
  331. SDL_JoystickDeviceItem *item = deviceList;
  332. if (device == NULL) {
  333. return NULL;
  334. }
  335. next = device->next;
  336. while (item != NULL) {
  337. if (item == device) {
  338. break;
  339. }
  340. prev = item;
  341. item = item->next;
  342. }
  343. /* Unlink the device item from the device list. */
  344. if (prev) {
  345. prev->next = device->next;
  346. } else if (device == deviceList) {
  347. deviceList = device->next;
  348. }
  349. if (device->joystick) {
  350. device->joystick->hwdata = NULL;
  351. }
  352. #ifdef SDL_JOYSTICK_MFI
  353. @autoreleasepool {
  354. if (device->controller) {
  355. /* The controller was explicitly retained in the struct, so it
  356. * should be explicitly released before freeing the struct. */
  357. GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
  358. controller.controllerPausedHandler = nil;
  359. device->controller = nil;
  360. }
  361. }
  362. #endif /* SDL_JOYSTICK_MFI */
  363. --numjoysticks;
  364. SDL_PrivateJoystickRemoved(device->instance_id);
  365. SDL_free(device->name);
  366. SDL_free(device);
  367. return next;
  368. }
  369. #if TARGET_OS_TV
  370. static void SDLCALL
  371. SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
  372. {
  373. BOOL allowRotation = newValue != NULL && *newValue != '0';
  374. @autoreleasepool {
  375. for (GCController *controller in [GCController controllers]) {
  376. if (controller.microGamepad) {
  377. controller.microGamepad.allowsRotation = allowRotation;
  378. }
  379. }
  380. }
  381. }
  382. #endif /* TARGET_OS_TV */
  383. static int
  384. IOS_JoystickInit(void)
  385. {
  386. @autoreleasepool {
  387. #if !TARGET_OS_TV
  388. if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
  389. /* Default behavior, accelerometer as joystick */
  390. IOS_AddJoystickDevice(nil, SDL_TRUE);
  391. }
  392. #endif /* !TARGET_OS_TV */
  393. #ifdef SDL_JOYSTICK_MFI
  394. /* GameController.framework was added in iOS 7. */
  395. if (![GCController class]) {
  396. return 0;
  397. }
  398. for (GCController *controller in [GCController controllers]) {
  399. IOS_AddJoystickDevice(controller, SDL_FALSE);
  400. }
  401. #if TARGET_OS_TV
  402. SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
  403. SDL_AppleTVRemoteRotationHintChanged, NULL);
  404. #endif /* TARGET_OS_TV */
  405. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  406. connectObserver = [center addObserverForName:GCControllerDidConnectNotification
  407. object:nil
  408. queue:nil
  409. usingBlock:^(NSNotification *note) {
  410. GCController *controller = note.object;
  411. IOS_AddJoystickDevice(controller, SDL_FALSE);
  412. }];
  413. disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
  414. object:nil
  415. queue:nil
  416. usingBlock:^(NSNotification *note) {
  417. GCController *controller = note.object;
  418. SDL_JoystickDeviceItem *device = deviceList;
  419. while (device != NULL) {
  420. if (device->controller == controller) {
  421. IOS_RemoveJoystickDevice(device);
  422. break;
  423. }
  424. device = device->next;
  425. }
  426. }];
  427. #endif /* SDL_JOYSTICK_MFI */
  428. }
  429. return 0;
  430. }
  431. static int
  432. IOS_JoystickGetCount(void)
  433. {
  434. return numjoysticks;
  435. }
  436. static void
  437. IOS_JoystickDetect(void)
  438. {
  439. }
  440. static const char *
  441. IOS_JoystickGetDeviceName(int device_index)
  442. {
  443. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  444. return device ? device->name : "Unknown";
  445. }
  446. static int
  447. IOS_JoystickGetDevicePlayerIndex(int device_index)
  448. {
  449. #ifdef SDL_JOYSTICK_MFI
  450. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  451. if (device && device->controller) {
  452. return (int)device->controller.playerIndex;
  453. }
  454. #endif
  455. return -1;
  456. }
  457. static void
  458. IOS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  459. {
  460. #ifdef SDL_JOYSTICK_MFI
  461. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  462. if (device && device->controller) {
  463. device->controller.playerIndex = player_index;
  464. }
  465. #endif
  466. }
  467. static SDL_JoystickGUID
  468. IOS_JoystickGetDeviceGUID( int device_index )
  469. {
  470. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  471. SDL_JoystickGUID guid;
  472. if (device) {
  473. guid = device->guid;
  474. } else {
  475. SDL_zero(guid);
  476. }
  477. return guid;
  478. }
  479. static SDL_JoystickID
  480. IOS_JoystickGetDeviceInstanceID(int device_index)
  481. {
  482. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  483. return device ? device->instance_id : -1;
  484. }
  485. static int
  486. IOS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  487. {
  488. SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
  489. if (device == NULL) {
  490. return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
  491. }
  492. joystick->hwdata = device;
  493. joystick->instance_id = device->instance_id;
  494. joystick->naxes = device->naxes;
  495. joystick->nhats = device->nhats;
  496. joystick->nbuttons = device->nbuttons;
  497. joystick->nballs = 0;
  498. device->joystick = joystick;
  499. @autoreleasepool {
  500. if (device->accelerometer) {
  501. #if !TARGET_OS_TV
  502. if (motionManager == nil) {
  503. motionManager = [[CMMotionManager alloc] init];
  504. }
  505. /* Shorter times between updates can significantly increase CPU usage. */
  506. motionManager.accelerometerUpdateInterval = 0.1;
  507. [motionManager startAccelerometerUpdates];
  508. #endif /* !TARGET_OS_TV */
  509. } else {
  510. #ifdef SDL_JOYSTICK_MFI
  511. if (device->uses_pause_handler) {
  512. GCController *controller = device->controller;
  513. controller.controllerPausedHandler = ^(GCController *c) {
  514. if (joystick->hwdata) {
  515. ++joystick->hwdata->num_pause_presses;
  516. }
  517. };
  518. }
  519. #endif /* SDL_JOYSTICK_MFI */
  520. }
  521. }
  522. if (device->remote) {
  523. ++SDL_AppleTVRemoteOpenedAsJoystick;
  524. }
  525. return 0;
  526. }
  527. static void
  528. IOS_AccelerometerUpdate(SDL_Joystick * joystick)
  529. {
  530. #if !TARGET_OS_TV
  531. const float maxgforce = SDL_IPHONE_MAX_GFORCE;
  532. const SInt16 maxsint16 = 0x7FFF;
  533. CMAcceleration accel;
  534. @autoreleasepool {
  535. if (!motionManager.isAccelerometerActive) {
  536. return;
  537. }
  538. accel = motionManager.accelerometerData.acceleration;
  539. }
  540. /*
  541. Convert accelerometer data from floating point to Sint16, which is what
  542. the joystick system expects.
  543. To do the conversion, the data is first clamped onto the interval
  544. [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
  545. by MAX_SINT16 so that it is mapped to the full range of an Sint16.
  546. You can customize the clamped range of this function by modifying the
  547. SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
  548. Once converted to Sint16, the accelerometer data no longer has coherent
  549. units. You can convert the data back to units of g-force by multiplying
  550. it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
  551. */
  552. /* clamp the data */
  553. accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
  554. accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
  555. accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
  556. /* pass in data mapped to range of SInt16 */
  557. SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
  558. SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
  559. SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
  560. #endif /* !TARGET_OS_TV */
  561. }
  562. #ifdef SDL_JOYSTICK_MFI
  563. static Uint8
  564. IOS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
  565. {
  566. Uint8 hat = 0;
  567. if (dpad.up.isPressed) {
  568. hat |= SDL_HAT_UP;
  569. } else if (dpad.down.isPressed) {
  570. hat |= SDL_HAT_DOWN;
  571. }
  572. if (dpad.left.isPressed) {
  573. hat |= SDL_HAT_LEFT;
  574. } else if (dpad.right.isPressed) {
  575. hat |= SDL_HAT_RIGHT;
  576. }
  577. if (hat == 0) {
  578. return SDL_HAT_CENTERED;
  579. }
  580. return hat;
  581. }
  582. #endif
  583. static void
  584. IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
  585. {
  586. #if SDL_JOYSTICK_MFI
  587. @autoreleasepool {
  588. GCController *controller = joystick->hwdata->controller;
  589. Uint8 hatstate = SDL_HAT_CENTERED;
  590. int i;
  591. int pause_button_index = 0;
  592. if (controller.extendedGamepad) {
  593. GCExtendedGamepad *gamepad = controller.extendedGamepad;
  594. /* Axis order matches the XInput Windows mappings. */
  595. Sint16 axes[] = {
  596. (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
  597. (Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
  598. (Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
  599. (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
  600. (Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
  601. (Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
  602. };
  603. /* Button order matches the XInput Windows mappings. */
  604. Uint8 buttons[joystick->nbuttons];
  605. int button_count = 0;
  606. /* These buttons are part of the original MFi spec */
  607. buttons[button_count++] = gamepad.buttonA.isPressed;
  608. buttons[button_count++] = gamepad.buttonB.isPressed;
  609. buttons[button_count++] = gamepad.buttonX.isPressed;
  610. buttons[button_count++] = gamepad.buttonY.isPressed;
  611. buttons[button_count++] = gamepad.leftShoulder.isPressed;
  612. buttons[button_count++] = gamepad.rightShoulder.isPressed;
  613. /* These buttons are available on some newer controllers */
  614. #pragma clang diagnostic push
  615. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  616. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK)) {
  617. buttons[button_count++] = gamepad.leftThumbstickButton.isPressed;
  618. }
  619. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK)) {
  620. buttons[button_count++] = gamepad.rightThumbstickButton.isPressed;
  621. }
  622. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
  623. buttons[button_count++] = gamepad.buttonOptions.isPressed;
  624. }
  625. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_GUIDE)) {
  626. buttons[button_count++] = gamepad.buttonHome.isPressed;
  627. }
  628. /* This must be the last button, so we can optionally handle it with pause_button_index below */
  629. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
  630. if (joystick->hwdata->uses_pause_handler) {
  631. pause_button_index = button_count;
  632. buttons[button_count++] = joystick->delayed_guide_button;
  633. } else {
  634. buttons[button_count++] = gamepad.buttonMenu.isPressed;
  635. }
  636. }
  637. #ifdef ENABLE_PHYSICAL_INPUT_PROFILE
  638. if (joystick->hwdata->has_dualshock_touchpad) {
  639. buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputDualShockTouchpadButton].isPressed;
  640. }
  641. if (joystick->hwdata->has_xbox_paddles) {
  642. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE1)) {
  643. buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleOne].isPressed;
  644. }
  645. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE2)) {
  646. buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleTwo].isPressed;
  647. }
  648. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE3)) {
  649. buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleThree].isPressed;
  650. }
  651. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_PADDLE4)) {
  652. buttons[button_count++] = controller.physicalInputProfile.buttons[GCInputXboxPaddleFour].isPressed;
  653. }
  654. /*
  655. SDL_Log("Paddles: [%d,%d,%d,%d]",
  656. controller.physicalInputProfile.buttons[GCInputXboxPaddleOne].isPressed,
  657. controller.physicalInputProfile.buttons[GCInputXboxPaddleTwo].isPressed,
  658. controller.physicalInputProfile.buttons[GCInputXboxPaddleThree].isPressed,
  659. controller.physicalInputProfile.buttons[GCInputXboxPaddleFour].isPressed);
  660. */
  661. }
  662. #endif
  663. #pragma clang diagnostic pop
  664. hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
  665. for (i = 0; i < SDL_arraysize(axes); i++) {
  666. SDL_PrivateJoystickAxis(joystick, i, axes[i]);
  667. }
  668. for (i = 0; i < button_count; i++) {
  669. SDL_PrivateJoystickButton(joystick, i, buttons[i]);
  670. }
  671. } else if (controller.gamepad) {
  672. GCGamepad *gamepad = controller.gamepad;
  673. /* Button order matches the XInput Windows mappings. */
  674. Uint8 buttons[joystick->nbuttons];
  675. int button_count = 0;
  676. buttons[button_count++] = gamepad.buttonA.isPressed;
  677. buttons[button_count++] = gamepad.buttonB.isPressed;
  678. buttons[button_count++] = gamepad.buttonX.isPressed;
  679. buttons[button_count++] = gamepad.buttonY.isPressed;
  680. buttons[button_count++] = gamepad.leftShoulder.isPressed;
  681. buttons[button_count++] = gamepad.rightShoulder.isPressed;
  682. pause_button_index = button_count;
  683. buttons[button_count++] = joystick->delayed_guide_button;
  684. hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
  685. for (i = 0; i < button_count; i++) {
  686. SDL_PrivateJoystickButton(joystick, i, buttons[i]);
  687. }
  688. }
  689. #if TARGET_OS_TV
  690. else if (controller.microGamepad) {
  691. GCMicroGamepad *gamepad = controller.microGamepad;
  692. Sint16 axes[] = {
  693. (Sint16) (gamepad.dpad.xAxis.value * 32767),
  694. (Sint16) (gamepad.dpad.yAxis.value * -32767),
  695. };
  696. for (i = 0; i < SDL_arraysize(axes); i++) {
  697. SDL_PrivateJoystickAxis(joystick, i, axes[i]);
  698. }
  699. Uint8 buttons[joystick->nbuttons];
  700. int button_count = 0;
  701. buttons[button_count++] = gamepad.buttonA.isPressed;
  702. buttons[button_count++] = gamepad.buttonX.isPressed;
  703. #pragma clang diagnostic push
  704. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  705. /* This must be the last button, so we can optionally handle it with pause_button_index below */
  706. if (joystick->hwdata->button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
  707. if (joystick->hwdata->uses_pause_handler) {
  708. pause_button_index = button_count;
  709. buttons[button_count++] = joystick->delayed_guide_button;
  710. } else {
  711. buttons[button_count++] = gamepad.buttonMenu.isPressed;
  712. }
  713. }
  714. #pragma clang diagnostic pop
  715. for (i = 0; i < button_count; i++) {
  716. SDL_PrivateJoystickButton(joystick, i, buttons[i]);
  717. }
  718. }
  719. #endif /* TARGET_OS_TV */
  720. if (joystick->nhats > 0) {
  721. SDL_PrivateJoystickHat(joystick, 0, hatstate);
  722. }
  723. if (joystick->hwdata->uses_pause_handler) {
  724. for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
  725. SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_PRESSED);
  726. SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_RELEASED);
  727. }
  728. joystick->hwdata->num_pause_presses = 0;
  729. }
  730. #ifdef ENABLE_MFI_BATTERY
  731. if (@available(iOS 14.0, tvOS 14.0, *)) {
  732. GCDeviceBattery *battery = controller.battery;
  733. if (battery) {
  734. SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
  735. switch (battery.batteryState) {
  736. case GCDeviceBatteryStateDischarging:
  737. {
  738. float power_level = battery.batteryLevel;
  739. if (power_level <= 0.05f) {
  740. ePowerLevel = SDL_JOYSTICK_POWER_EMPTY;
  741. } else if (power_level <= 0.20f) {
  742. ePowerLevel = SDL_JOYSTICK_POWER_LOW;
  743. } else if (power_level <= 0.70f) {
  744. ePowerLevel = SDL_JOYSTICK_POWER_MEDIUM;
  745. } else {
  746. ePowerLevel = SDL_JOYSTICK_POWER_FULL;
  747. }
  748. }
  749. break;
  750. case GCDeviceBatteryStateCharging:
  751. ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
  752. break;
  753. case GCDeviceBatteryStateFull:
  754. ePowerLevel = SDL_JOYSTICK_POWER_FULL;
  755. break;
  756. default:
  757. break;
  758. }
  759. SDL_PrivateJoystickBatteryLevel(joystick, ePowerLevel);
  760. }
  761. }
  762. #endif /* ENABLE_MFI_BATTERY */
  763. }
  764. #endif /* SDL_JOYSTICK_MFI */
  765. }
  766. #ifdef ENABLE_MFI_RUMBLE
  767. @interface SDL_RumbleMotor : NSObject
  768. @end
  769. @implementation SDL_RumbleMotor {
  770. CHHapticEngine *engine API_AVAILABLE(ios(13.0), tvos(14.0));
  771. id<CHHapticPatternPlayer> player API_AVAILABLE(ios(13.0), tvos(14.0));
  772. bool active;
  773. }
  774. -(void)cleanup
  775. {
  776. if (self->player != nil) {
  777. [self->player cancelAndReturnError:nil];
  778. self->player = nil;
  779. }
  780. if (self->engine != nil) {
  781. [self->engine stopWithCompletionHandler:nil];
  782. self->engine = nil;
  783. }
  784. }
  785. -(int)setIntensity:(float)intensity
  786. {
  787. @autoreleasepool {
  788. if (@available(iOS 14.0, tvOS 14.0, *)) {
  789. NSError *error;
  790. if (self->engine == nil) {
  791. return SDL_SetError("Haptics engine was stopped");
  792. }
  793. if (intensity == 0.0f) {
  794. if (self->player && self->active) {
  795. [self->player stopAtTime:0 error:&error];
  796. }
  797. self->active = false;
  798. return 0;
  799. }
  800. if (self->player == nil) {
  801. CHHapticEventParameter *param = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:1.0f];
  802. CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:[NSArray arrayWithObjects:param, nil] relativeTime:0 duration:GCHapticDurationInfinite];
  803. CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:[NSArray arrayWithObject:event] parameters:[[NSArray alloc] init] error:&error];
  804. if (error != nil) {
  805. return SDL_SetError("Couldn't create haptic pattern: %s", [error.localizedDescription UTF8String]);
  806. }
  807. self->player = [self->engine createPlayerWithPattern:pattern error:&error];
  808. if (error != nil) {
  809. return SDL_SetError("Couldn't create haptic player: %s", [error.localizedDescription UTF8String]);
  810. }
  811. self->active = false;
  812. }
  813. CHHapticDynamicParameter *param = [[CHHapticDynamicParameter alloc] initWithParameterID:CHHapticDynamicParameterIDHapticIntensityControl value:intensity relativeTime:0];
  814. [self->player sendParameters:[NSArray arrayWithObject:param] atTime:0 error:&error];
  815. if (error != nil) {
  816. return SDL_SetError("Couldn't update haptic player: %s", [error.localizedDescription UTF8String]);
  817. }
  818. if (!self->active) {
  819. [self->player startAtTime:0 error:&error];
  820. self->active = true;
  821. }
  822. }
  823. return 0;
  824. }
  825. }
  826. -(id) initWithController:(GCController*)controller locality:(GCHapticsLocality)locality API_AVAILABLE(ios(14.0), tvos(14.0))
  827. {
  828. @autoreleasepool {
  829. NSError *error;
  830. self->engine = [controller.haptics createEngineWithLocality:locality];
  831. if (self->engine == nil) {
  832. SDL_SetError("Couldn't create haptics engine");
  833. return nil;
  834. }
  835. [self->engine startAndReturnError:&error];
  836. if (error != nil) {
  837. SDL_SetError("Couldn't start haptics engine");
  838. return nil;
  839. }
  840. __weak typeof(self) weakSelf = self;
  841. self->engine.stoppedHandler = ^(CHHapticEngineStoppedReason stoppedReason) {
  842. SDL_RumbleMotor *_this = weakSelf;
  843. if (_this == nil) {
  844. return;
  845. }
  846. _this->player = nil;
  847. _this->engine = nil;
  848. };
  849. self->engine.resetHandler = ^{
  850. SDL_RumbleMotor *_this = weakSelf;
  851. if (_this == nil) {
  852. return;
  853. }
  854. _this->player = nil;
  855. [_this->engine startAndReturnError:nil];
  856. };
  857. return self;
  858. }
  859. }
  860. @end
  861. @interface SDL_RumbleContext : NSObject
  862. @end
  863. @implementation SDL_RumbleContext {
  864. SDL_RumbleMotor *low_frequency_motor;
  865. SDL_RumbleMotor *high_frequency_motor;
  866. }
  867. -(id) initWithLowFrequencyMotor:(SDL_RumbleMotor*)low_frequency_motor andHighFrequencyMotor:(SDL_RumbleMotor*)high_frequency_motor
  868. {
  869. self->low_frequency_motor = low_frequency_motor;
  870. self->high_frequency_motor = high_frequency_motor;
  871. return self;
  872. }
  873. -(int) rumbleWithLowFrequency:(Uint16)low_frequency_rumble andHighFrequency:(Uint16)high_frequency_rumble
  874. {
  875. int result = 0;
  876. result += [self->low_frequency_motor setIntensity:((float)low_frequency_rumble / 65535.0f)];
  877. result += [self->high_frequency_motor setIntensity:((float)high_frequency_rumble / 65535.0f)];
  878. return ((result < 0) ? -1 : 0);
  879. }
  880. -(void)cleanup
  881. {
  882. [self->low_frequency_motor cleanup];
  883. [self->high_frequency_motor cleanup];
  884. }
  885. @end
  886. static SDL_RumbleContext *IOS_JoystickInitRumble(GCController *controller)
  887. {
  888. @autoreleasepool {
  889. if (@available(iOS 14.0, tvOS 14.0, *)) {
  890. SDL_RumbleMotor *low_frequency_motor = [[SDL_RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityLeftHandle];
  891. SDL_RumbleMotor *high_frequency_motor = [[SDL_RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityRightHandle];
  892. if (low_frequency_motor && high_frequency_motor) {
  893. return [[SDL_RumbleContext alloc] initWithLowFrequencyMotor:low_frequency_motor andHighFrequencyMotor:high_frequency_motor];
  894. }
  895. }
  896. }
  897. return nil;
  898. }
  899. #endif /* ENABLE_MFI_RUMBLE */
  900. static int
  901. IOS_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  902. {
  903. #ifdef ENABLE_MFI_RUMBLE
  904. SDL_JoystickDeviceItem *device = joystick->hwdata;
  905. if (@available(iOS 14.0, tvOS 14.0, *)) {
  906. if (!device->rumble && device->controller && device->controller.haptics) {
  907. SDL_RumbleContext *rumble = IOS_JoystickInitRumble(device->controller);
  908. if (rumble) {
  909. device->rumble = (void *)CFBridgingRetain(rumble);
  910. }
  911. }
  912. }
  913. if (device->rumble) {
  914. SDL_RumbleContext *rumble = (__bridge SDL_RumbleContext *)device->rumble;
  915. return [rumble rumbleWithLowFrequency:low_frequency_rumble andHighFrequency:high_frequency_rumble];
  916. } else {
  917. return SDL_Unsupported();
  918. }
  919. #else
  920. return SDL_Unsupported();
  921. #endif
  922. }
  923. static SDL_bool
  924. IOS_JoystickHasLED(SDL_Joystick * joystick)
  925. {
  926. #ifdef ENABLE_MFI_LIGHT
  927. @autoreleasepool {
  928. if (@available(iOS 14.0, tvOS 14.0, *)) {
  929. GCController *controller = joystick->hwdata->controller;
  930. GCDeviceLight *light = controller.light;
  931. if (light) {
  932. return SDL_TRUE;
  933. }
  934. }
  935. }
  936. #endif /* ENABLE_MFI_LIGHT */
  937. return SDL_FALSE;
  938. }
  939. static int
  940. IOS_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
  941. {
  942. #ifdef ENABLE_MFI_LIGHT
  943. @autoreleasepool {
  944. if (@available(iOS 14.0, tvOS 14.0, *)) {
  945. GCController *controller = joystick->hwdata->controller;
  946. GCDeviceLight *light = controller.light;
  947. if (light) {
  948. light.color = [[GCColor alloc] initWithRed:(float)red / 255.0f
  949. green:(float)green / 255.0f
  950. blue:(float)blue / 255.0f];
  951. return 0;
  952. }
  953. }
  954. }
  955. #endif /* ENABLE_MFI_LIGHT */
  956. return SDL_Unsupported();
  957. }
  958. static void
  959. IOS_JoystickUpdate(SDL_Joystick * joystick)
  960. {
  961. SDL_JoystickDeviceItem *device = joystick->hwdata;
  962. if (device == NULL) {
  963. return;
  964. }
  965. if (device->accelerometer) {
  966. IOS_AccelerometerUpdate(joystick);
  967. } else if (device->controller) {
  968. IOS_MFIJoystickUpdate(joystick);
  969. }
  970. }
  971. static void
  972. IOS_JoystickClose(SDL_Joystick * joystick)
  973. {
  974. SDL_JoystickDeviceItem *device = joystick->hwdata;
  975. if (device == NULL) {
  976. return;
  977. }
  978. device->joystick = NULL;
  979. @autoreleasepool {
  980. #ifdef ENABLE_MFI_RUMBLE
  981. if (device->rumble) {
  982. SDL_RumbleContext *rumble = (__bridge SDL_RumbleContext *)device->rumble;
  983. [rumble cleanup];
  984. CFRelease(device->rumble);
  985. device->rumble = NULL;
  986. }
  987. #endif /* ENABLE_MFI_RUMBLE */
  988. if (device->accelerometer) {
  989. #if !TARGET_OS_TV
  990. [motionManager stopAccelerometerUpdates];
  991. #endif /* !TARGET_OS_TV */
  992. } else if (device->controller) {
  993. #ifdef SDL_JOYSTICK_MFI
  994. GCController *controller = device->controller;
  995. controller.controllerPausedHandler = nil;
  996. controller.playerIndex = -1;
  997. #endif
  998. }
  999. }
  1000. if (device->remote) {
  1001. --SDL_AppleTVRemoteOpenedAsJoystick;
  1002. }
  1003. }
  1004. static void
  1005. IOS_JoystickQuit(void)
  1006. {
  1007. @autoreleasepool {
  1008. #ifdef SDL_JOYSTICK_MFI
  1009. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  1010. if (connectObserver) {
  1011. [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
  1012. connectObserver = nil;
  1013. }
  1014. if (disconnectObserver) {
  1015. [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
  1016. disconnectObserver = nil;
  1017. }
  1018. #if TARGET_OS_TV
  1019. SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
  1020. SDL_AppleTVRemoteRotationHintChanged, NULL);
  1021. #endif /* TARGET_OS_TV */
  1022. #endif /* SDL_JOYSTICK_MFI */
  1023. while (deviceList != NULL) {
  1024. IOS_RemoveJoystickDevice(deviceList);
  1025. }
  1026. #if !TARGET_OS_TV
  1027. motionManager = nil;
  1028. #endif /* !TARGET_OS_TV */
  1029. }
  1030. numjoysticks = 0;
  1031. }
  1032. static SDL_bool
  1033. IOS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1034. {
  1035. return SDL_FALSE;
  1036. }
  1037. SDL_JoystickDriver SDL_IOS_JoystickDriver =
  1038. {
  1039. IOS_JoystickInit,
  1040. IOS_JoystickGetCount,
  1041. IOS_JoystickDetect,
  1042. IOS_JoystickGetDeviceName,
  1043. IOS_JoystickGetDevicePlayerIndex,
  1044. IOS_JoystickSetDevicePlayerIndex,
  1045. IOS_JoystickGetDeviceGUID,
  1046. IOS_JoystickGetDeviceInstanceID,
  1047. IOS_JoystickOpen,
  1048. IOS_JoystickRumble,
  1049. IOS_JoystickHasLED,
  1050. IOS_JoystickSetLED,
  1051. IOS_JoystickUpdate,
  1052. IOS_JoystickClose,
  1053. IOS_JoystickQuit,
  1054. IOS_JoystickGetGamepadMapping
  1055. };
  1056. /* vi: set ts=4 sw=4 expandtab: */