joypad_ios.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**************************************************************************/
  2. /* joypad_ios.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_ios.h"
  31. #import "godot_view.h"
  32. #import "os_ios.h"
  33. #include "core/config/project_settings.h"
  34. #import "drivers/coreaudio/audio_driver_coreaudio.h"
  35. #include "main/main.h"
  36. JoypadIOS::JoypadIOS() {
  37. observer = [[JoypadIOSObserver alloc] init];
  38. [observer startObserving];
  39. }
  40. JoypadIOS::~JoypadIOS() {
  41. if (observer) {
  42. [observer finishObserving];
  43. observer = nil;
  44. }
  45. }
  46. void JoypadIOS::start_processing() {
  47. if (observer) {
  48. [observer startProcessing];
  49. }
  50. }
  51. @interface JoypadIOSObserver ()
  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 JoypadIOSObserver
  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 = Input::get_singleton()->get_unused_joy_id();
  120. if (joy_id == -1) {
  121. print_verbose("Couldn't retrieve new joy ID.");
  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. Input::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. print_verbose("Couldn't retrieve new controller.");
  140. return;
  141. }
  142. if ([[self.connectedJoypads allKeysForObject:controller] count] > 0) {
  143. print_verbose("Controller is already registered.");
  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. Input::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. Input::get_singleton()->joy_button(joy_id, JoyButton::A,
  214. gamepad.buttonA.isPressed);
  215. } else if (element == gamepad.buttonB) {
  216. Input::get_singleton()->joy_button(joy_id, JoyButton::B,
  217. gamepad.buttonB.isPressed);
  218. } else if (element == gamepad.buttonX) {
  219. Input::get_singleton()->joy_button(joy_id, JoyButton::X,
  220. gamepad.buttonX.isPressed);
  221. } else if (element == gamepad.buttonY) {
  222. Input::get_singleton()->joy_button(joy_id, JoyButton::Y,
  223. gamepad.buttonY.isPressed);
  224. } else if (element == gamepad.leftShoulder) {
  225. Input::get_singleton()->joy_button(joy_id, JoyButton::LEFT_SHOULDER,
  226. gamepad.leftShoulder.isPressed);
  227. } else if (element == gamepad.rightShoulder) {
  228. Input::get_singleton()->joy_button(joy_id, JoyButton::RIGHT_SHOULDER,
  229. gamepad.rightShoulder.isPressed);
  230. } else if (element == gamepad.dpad) {
  231. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_UP,
  232. gamepad.dpad.up.isPressed);
  233. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_DOWN,
  234. gamepad.dpad.down.isPressed);
  235. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_LEFT,
  236. gamepad.dpad.left.isPressed);
  237. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_RIGHT,
  238. gamepad.dpad.right.isPressed);
  239. }
  240. if (element == gamepad.leftThumbstick) {
  241. float value = gamepad.leftThumbstick.xAxis.value;
  242. Input::get_singleton()->joy_axis(joy_id, JoyAxis::LEFT_X, value);
  243. value = -gamepad.leftThumbstick.yAxis.value;
  244. Input::get_singleton()->joy_axis(joy_id, JoyAxis::LEFT_Y, value);
  245. } else if (element == gamepad.rightThumbstick) {
  246. float value = gamepad.rightThumbstick.xAxis.value;
  247. Input::get_singleton()->joy_axis(joy_id, JoyAxis::RIGHT_X, value);
  248. value = -gamepad.rightThumbstick.yAxis.value;
  249. Input::get_singleton()->joy_axis(joy_id, JoyAxis::RIGHT_Y, value);
  250. } else if (element == gamepad.leftTrigger) {
  251. float value = gamepad.leftTrigger.value;
  252. Input::get_singleton()->joy_axis(joy_id, JoyAxis::TRIGGER_LEFT, value);
  253. } else if (element == gamepad.rightTrigger) {
  254. float value = gamepad.rightTrigger.value;
  255. Input::get_singleton()->joy_axis(joy_id, JoyAxis::TRIGGER_RIGHT, value);
  256. }
  257. if (@available(iOS 13, *)) {
  258. // iOS uses 'buttonOptions' and 'buttonMenu' names for BACK and START joy buttons.
  259. if (element == gamepad.buttonOptions) {
  260. Input::get_singleton()->joy_button(joy_id, JoyButton::BACK,
  261. gamepad.buttonOptions.isPressed);
  262. } else if (element == gamepad.buttonMenu) {
  263. Input::get_singleton()->joy_button(joy_id, JoyButton::START,
  264. gamepad.buttonMenu.isPressed);
  265. }
  266. }
  267. if (@available(iOS 14, *)) {
  268. // iOS uses 'buttonHome' for the GUIDE joy button.
  269. if (element == gamepad.buttonHome) {
  270. Input::get_singleton()->joy_button(joy_id, JoyButton::GUIDE,
  271. gamepad.buttonHome.isPressed);
  272. }
  273. }
  274. };
  275. } else if (controller.microGamepad != nil) {
  276. // micro gamepads were added in OS 9 and feature just 2 buttons and a d-pad
  277. _weakify(self);
  278. _weakify(controller);
  279. controller.microGamepad.valueChangedHandler = ^(GCMicroGamepad *gamepad, GCControllerElement *element) {
  280. _strongify(self);
  281. _strongify(controller);
  282. int joy_id = [self getJoyIdForController:controller];
  283. if (element == gamepad.buttonA) {
  284. Input::get_singleton()->joy_button(joy_id, JoyButton::A,
  285. gamepad.buttonA.isPressed);
  286. } else if (element == gamepad.buttonX) {
  287. Input::get_singleton()->joy_button(joy_id, JoyButton::X,
  288. gamepad.buttonX.isPressed);
  289. } else if (element == gamepad.dpad) {
  290. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_UP,
  291. gamepad.dpad.up.isPressed);
  292. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_DOWN,
  293. gamepad.dpad.down.isPressed);
  294. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_LEFT, gamepad.dpad.left.isPressed);
  295. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_RIGHT, gamepad.dpad.right.isPressed);
  296. }
  297. };
  298. }
  299. ///@TODO need to add support for controller.motion which gives us access to
  300. /// the orientation of the device (if supported)
  301. ///@TODO need to add support for controllerPausedHandler which should be a
  302. /// toggle
  303. }
  304. @end