joypad_iphone.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**************************************************************************/
  2. /* joypad_iphone.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #import "joypad_iphone.h"
  31. #include "core/project_settings.h"
  32. #include "drivers/coreaudio/audio_driver_coreaudio.h"
  33. #import "godot_view.h"
  34. #include "main/main.h"
  35. #include "os_iphone.h"
  36. JoypadIPhone::JoypadIPhone() {
  37. observer = [[JoypadIPhoneObserver alloc] init];
  38. [observer startObserving];
  39. }
  40. JoypadIPhone::~JoypadIPhone() {
  41. if (observer) {
  42. [observer finishObserving];
  43. observer = nil;
  44. }
  45. }
  46. void JoypadIPhone::start_processing() {
  47. if (observer) {
  48. [observer startProcessing];
  49. }
  50. }
  51. @interface JoypadIPhoneObserver ()
  52. @property(assign, nonatomic) BOOL isObserving;
  53. @property(assign, nonatomic) BOOL isProcessing;
  54. @property(strong, nonatomic) NSMutableDictionary *connectedJoypads;
  55. @property(strong, nonatomic) NSMutableArray *joypadsQueue;
  56. @end
  57. @implementation JoypadIPhoneObserver
  58. - (instancetype)init {
  59. self = [super init];
  60. if (self) {
  61. [self godot_commonInit];
  62. }
  63. return self;
  64. }
  65. - (void)godot_commonInit {
  66. self.isObserving = NO;
  67. self.isProcessing = NO;
  68. }
  69. - (void)startProcessing {
  70. self.isProcessing = YES;
  71. for (GCController *controller in self.joypadsQueue) {
  72. [self addiOSJoypad:controller];
  73. }
  74. [self.joypadsQueue removeAllObjects];
  75. }
  76. - (void)startObserving {
  77. if (self.isObserving) {
  78. return;
  79. }
  80. self.isObserving = YES;
  81. self.connectedJoypads = [NSMutableDictionary dictionary];
  82. self.joypadsQueue = [NSMutableArray array];
  83. // get told when controllers connect, this will be called right away for
  84. // already connected controllers
  85. [[NSNotificationCenter defaultCenter]
  86. addObserver:self
  87. selector:@selector(controllerWasConnected:)
  88. name:GCControllerDidConnectNotification
  89. object:nil];
  90. // get told when controllers disconnect
  91. [[NSNotificationCenter defaultCenter]
  92. addObserver:self
  93. selector:@selector(controllerWasDisconnected:)
  94. name:GCControllerDidDisconnectNotification
  95. object:nil];
  96. }
  97. - (void)finishObserving {
  98. if (self.isObserving) {
  99. [[NSNotificationCenter defaultCenter] removeObserver:self];
  100. }
  101. self.isObserving = NO;
  102. self.isProcessing = NO;
  103. self.connectedJoypads = nil;
  104. self.joypadsQueue = nil;
  105. }
  106. - (void)dealloc {
  107. [self finishObserving];
  108. }
  109. - (int)getJoyIdForController:(GCController *)controller {
  110. NSArray *keys = [self.connectedJoypads allKeysForObject:controller];
  111. for (NSNumber *key in keys) {
  112. int joy_id = [key intValue];
  113. return joy_id;
  114. };
  115. return -1;
  116. };
  117. - (void)addiOSJoypad:(GCController *)controller {
  118. // get a new id for our controller
  119. int joy_id = OSIPhone::get_singleton()->get_unused_joy_id();
  120. if (joy_id == -1) {
  121. printf("Couldn't retrieve new joy id\n");
  122. return;
  123. }
  124. // assign our player index
  125. if (controller.playerIndex == GCControllerPlayerIndexUnset) {
  126. controller.playerIndex = [self getFreePlayerIndex];
  127. };
  128. // tell Godot about our new controller
  129. OSIPhone::get_singleton()->joy_connection_changed(joy_id, true, String::utf8([controller.vendorName UTF8String]));
  130. // add it to our dictionary, this will retain our controllers
  131. [self.connectedJoypads setObject:controller forKey:[NSNumber numberWithInt:joy_id]];
  132. // set our input handler
  133. [self setControllerInputHandler:controller];
  134. }
  135. - (void)controllerWasConnected:(NSNotification *)notification {
  136. // get our controller
  137. GCController *controller = (GCController *)notification.object;
  138. if (!controller) {
  139. printf("Couldn't retrieve new controller\n");
  140. return;
  141. }
  142. if ([[self.connectedJoypads allKeysForObject:controller] count] > 0) {
  143. printf("Controller is already registered\n");
  144. } else if (!self.isProcessing) {
  145. [self.joypadsQueue addObject:controller];
  146. } else {
  147. [self addiOSJoypad:controller];
  148. }
  149. }
  150. - (void)controllerWasDisconnected:(NSNotification *)notification {
  151. // find our joystick, there should be only one in our dictionary
  152. GCController *controller = (GCController *)notification.object;
  153. if (!controller) {
  154. return;
  155. }
  156. NSArray *keys = [self.connectedJoypads allKeysForObject:controller];
  157. for (NSNumber *key in keys) {
  158. // tell Godot this joystick is no longer there
  159. int joy_id = [key intValue];
  160. OSIPhone::get_singleton()->joy_connection_changed(joy_id, false, "");
  161. // and remove it from our dictionary
  162. [self.connectedJoypads removeObjectForKey:key];
  163. };
  164. };
  165. - (GCControllerPlayerIndex)getFreePlayerIndex {
  166. bool have_player_1 = false;
  167. bool have_player_2 = false;
  168. bool have_player_3 = false;
  169. bool have_player_4 = false;
  170. if (self.connectedJoypads == nil) {
  171. NSArray *keys = [self.connectedJoypads allKeys];
  172. for (NSNumber *key in keys) {
  173. GCController *controller = [self.connectedJoypads objectForKey:key];
  174. if (controller.playerIndex == GCControllerPlayerIndex1) {
  175. have_player_1 = true;
  176. } else if (controller.playerIndex == GCControllerPlayerIndex2) {
  177. have_player_2 = true;
  178. } else if (controller.playerIndex == GCControllerPlayerIndex3) {
  179. have_player_3 = true;
  180. } else if (controller.playerIndex == GCControllerPlayerIndex4) {
  181. have_player_4 = true;
  182. };
  183. };
  184. };
  185. if (!have_player_1) {
  186. return GCControllerPlayerIndex1;
  187. } else if (!have_player_2) {
  188. return GCControllerPlayerIndex2;
  189. } else if (!have_player_3) {
  190. return GCControllerPlayerIndex3;
  191. } else if (!have_player_4) {
  192. return GCControllerPlayerIndex4;
  193. } else {
  194. return GCControllerPlayerIndexUnset;
  195. };
  196. }
  197. - (void)setControllerInputHandler:(GCController *)controller {
  198. // Hook in the callback handler for the correct gamepad profile.
  199. // This is a bit of a weird design choice on Apples part.
  200. // You need to select the most capable gamepad profile for the
  201. // gamepad attached.
  202. if (controller.extendedGamepad != nil) {
  203. // The extended gamepad profile has all the input you could possibly find on
  204. // a gamepad but will only be active if your gamepad actually has all of
  205. // these...
  206. _weakify(self);
  207. _weakify(controller);
  208. controller.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element) {
  209. _strongify(self);
  210. _strongify(controller);
  211. int joy_id = [self getJoyIdForController:controller];
  212. if (element == gamepad.buttonA) {
  213. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_0,
  214. gamepad.buttonA.isPressed);
  215. } else if (element == gamepad.buttonB) {
  216. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_1,
  217. gamepad.buttonB.isPressed);
  218. } else if (element == gamepad.buttonX) {
  219. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_2,
  220. gamepad.buttonX.isPressed);
  221. } else if (element == gamepad.buttonY) {
  222. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_3,
  223. gamepad.buttonY.isPressed);
  224. } else if (element == gamepad.leftShoulder) {
  225. OSIPhone::get_singleton()->joy_button(joy_id, JOY_L,
  226. gamepad.leftShoulder.isPressed);
  227. } else if (element == gamepad.rightShoulder) {
  228. OSIPhone::get_singleton()->joy_button(joy_id, JOY_R,
  229. gamepad.rightShoulder.isPressed);
  230. } else if (element == gamepad.leftTrigger) {
  231. OSIPhone::get_singleton()->joy_button(joy_id, JOY_L2,
  232. gamepad.leftTrigger.isPressed);
  233. } else if (element == gamepad.rightTrigger) {
  234. OSIPhone::get_singleton()->joy_button(joy_id, JOY_R2,
  235. gamepad.rightTrigger.isPressed);
  236. } else if (element == gamepad.dpad) {
  237. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_UP,
  238. gamepad.dpad.up.isPressed);
  239. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_DOWN,
  240. gamepad.dpad.down.isPressed);
  241. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_LEFT,
  242. gamepad.dpad.left.isPressed);
  243. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_RIGHT,
  244. gamepad.dpad.right.isPressed);
  245. };
  246. if (element == gamepad.leftThumbstick) {
  247. float value = gamepad.leftThumbstick.xAxis.value;
  248. OSIPhone::get_singleton()->joy_axis(joy_id, JOY_ANALOG_LX, value);
  249. value = -gamepad.leftThumbstick.yAxis.value;
  250. OSIPhone::get_singleton()->joy_axis(joy_id, JOY_ANALOG_LY, value);
  251. } else if (element == gamepad.rightThumbstick) {
  252. float value = gamepad.rightThumbstick.xAxis.value;
  253. OSIPhone::get_singleton()->joy_axis(joy_id, JOY_ANALOG_RX, value);
  254. value = -gamepad.rightThumbstick.yAxis.value;
  255. OSIPhone::get_singleton()->joy_axis(joy_id, JOY_ANALOG_RY, value);
  256. } else if (element == gamepad.leftTrigger) {
  257. float value = gamepad.leftTrigger.value;
  258. OSIPhone::get_singleton()->joy_axis(joy_id, JOY_ANALOG_L2, value);
  259. } else if (element == gamepad.rightTrigger) {
  260. float value = gamepad.rightTrigger.value;
  261. OSIPhone::get_singleton()->joy_axis(joy_id, JOY_ANALOG_R2, value);
  262. }
  263. if (@available(iOS 13, *)) {
  264. if (element == gamepad.buttonOptions) {
  265. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_10,
  266. gamepad.buttonOptions.isPressed);
  267. } else if (element == gamepad.buttonMenu) {
  268. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_11,
  269. gamepad.buttonMenu.isPressed);
  270. }
  271. }
  272. if (@available(iOS 14, *)) {
  273. if (element == gamepad.buttonHome) {
  274. OSIPhone::get_singleton()->joy_button(joy_id, JOY_GUIDE,
  275. gamepad.buttonHome.isPressed);
  276. }
  277. }
  278. };
  279. } else if (controller.microGamepad != nil) {
  280. // micro gamepads were added in OS 9 and feature just 2 buttons and a d-pad
  281. _weakify(self);
  282. _weakify(controller);
  283. controller.microGamepad.valueChangedHandler = ^(GCMicroGamepad *gamepad, GCControllerElement *element) {
  284. _strongify(self);
  285. _strongify(controller);
  286. int joy_id = [self getJoyIdForController:controller];
  287. if (element == gamepad.buttonA) {
  288. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_0,
  289. gamepad.buttonA.isPressed);
  290. } else if (element == gamepad.buttonX) {
  291. OSIPhone::get_singleton()->joy_button(joy_id, JOY_BUTTON_2,
  292. gamepad.buttonX.isPressed);
  293. } else if (element == gamepad.dpad) {
  294. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_UP,
  295. gamepad.dpad.up.isPressed);
  296. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_DOWN,
  297. gamepad.dpad.down.isPressed);
  298. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_LEFT,
  299. gamepad.dpad.left.isPressed);
  300. OSIPhone::get_singleton()->joy_button(joy_id, JOY_DPAD_RIGHT,
  301. gamepad.dpad.right.isPressed);
  302. }
  303. };
  304. }
  305. ///@TODO need to add support for controller.motion which gives us access to
  306. /// the orientation of the device (if supported)
  307. ///@TODO need to add support for controllerPausedHandler which should be a
  308. /// toggle
  309. };
  310. @end