joypad_macos.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /**************************************************************************/
  2. /* joypad_macos.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_macos.h"
  31. #include <Foundation/Foundation.h>
  32. #import "os_macos.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/string/ustring.h"
  36. #include "main/main.h"
  37. @implementation RumbleMotor
  38. - (instancetype)initWithController:(GCController *)controller locality:(GCHapticsLocality)locality {
  39. self = [super init];
  40. self.engine = [controller.haptics createEngineWithLocality:locality];
  41. self.player = nil;
  42. return self;
  43. }
  44. - (void)execute_pattern:(CHHapticPattern *)pattern {
  45. NSError *error;
  46. id<CHHapticPatternPlayer> player = [self.engine createPlayerWithPattern:pattern error:&error];
  47. // When all players have stopped for an engine, stop the engine.
  48. [self.engine notifyWhenPlayersFinished:^CHHapticEngineFinishedAction(NSError *_Nullable error) {
  49. return CHHapticEngineFinishedActionStopEngine;
  50. }];
  51. self.player = player;
  52. // Starts the engine and returns if an error was encountered.
  53. if (![self.engine startAndReturnError:&error]) {
  54. print_verbose("Couldn't start controller haptic engine");
  55. return;
  56. }
  57. if (![self.player startAtTime:0 error:&error]) {
  58. print_verbose("Couldn't execute controller haptic pattern");
  59. }
  60. }
  61. - (void)stop {
  62. NSError *error;
  63. [self.player stopAtTime:0 error:&error];
  64. self.player = nil;
  65. }
  66. @end
  67. @implementation RumbleContext
  68. - (instancetype)init {
  69. self = [super init];
  70. self.weak_motor = nil;
  71. self.strong_motor = nil;
  72. return self;
  73. }
  74. - (bool)hasMotors {
  75. return self.weak_motor != nil && self.strong_motor != nil;
  76. }
  77. - (bool)hasActivePlayers {
  78. if (![self hasMotors]) {
  79. return NO;
  80. }
  81. return self.weak_motor.player != nil && self.strong_motor.player != nil;
  82. }
  83. @end
  84. @implementation Joypad
  85. - (instancetype)init {
  86. self = [super init];
  87. return self;
  88. }
  89. - (instancetype)init:(GCController *)controller {
  90. self = [super init];
  91. self.controller = controller;
  92. if (@available(macOS 11, *)) {
  93. // Haptics within the controller is only available in macOS 11+.
  94. self.rumble_context = [[RumbleContext alloc] init];
  95. // Create Weak and Strong motors for controller.
  96. self.rumble_context.weak_motor = [[RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityRightHandle];
  97. self.rumble_context.strong_motor = [[RumbleMotor alloc] initWithController:controller locality:GCHapticsLocalityLeftHandle];
  98. // If the rumble motors aren't available, disable force feedback.
  99. if (![self.rumble_context hasMotors]) {
  100. self.force_feedback = NO;
  101. } else {
  102. self.force_feedback = YES;
  103. }
  104. } else {
  105. self.force_feedback = NO;
  106. }
  107. self.ff_effect_timestamp = 0;
  108. return self;
  109. }
  110. @end
  111. JoypadMacOS::JoypadMacOS() {
  112. observer = [[JoypadMacOSObserver alloc] init];
  113. [observer startObserving];
  114. if (@available(macOS 11.3, *)) {
  115. GCController.shouldMonitorBackgroundEvents = YES;
  116. }
  117. }
  118. JoypadMacOS::~JoypadMacOS() {
  119. if (observer) {
  120. [observer finishObserving];
  121. observer = nil;
  122. }
  123. }
  124. void JoypadMacOS::start_processing() {
  125. if (observer) {
  126. [observer startProcessing];
  127. }
  128. process_joypads();
  129. }
  130. API_AVAILABLE(macosx(10.15))
  131. CHHapticPattern *get_vibration_pattern(float p_magnitude, float p_duration) {
  132. // Creates a vibration pattern with an intensity and duration.
  133. NSDictionary *hapticDict = @{
  134. CHHapticPatternKeyPattern : @[
  135. @{
  136. CHHapticPatternKeyEvent : @{
  137. CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous,
  138. CHHapticPatternKeyTime : @(CHHapticTimeImmediate),
  139. CHHapticPatternKeyEventDuration : [NSNumber numberWithFloat:p_duration],
  140. CHHapticPatternKeyEventParameters : @[
  141. @{
  142. CHHapticPatternKeyParameterID : CHHapticEventParameterIDHapticIntensity,
  143. CHHapticPatternKeyParameterValue : [NSNumber numberWithFloat:p_magnitude]
  144. },
  145. ],
  146. },
  147. },
  148. ],
  149. };
  150. NSError *error;
  151. CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict error:&error];
  152. return pattern;
  153. }
  154. void JoypadMacOS::joypad_vibration_start(Joypad *p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) {
  155. if (!p_joypad.force_feedback || p_weak_magnitude < 0.f || p_weak_magnitude > 1.f || p_strong_magnitude < 0.f || p_strong_magnitude > 1.f) {
  156. return;
  157. }
  158. // If there is active vibration players, stop them.
  159. if ([p_joypad.rumble_context hasActivePlayers]) {
  160. joypad_vibration_stop(p_joypad, p_timestamp);
  161. }
  162. // Gets the default vibration pattern and creates a player for each motor.
  163. CHHapticPattern *weak_pattern = get_vibration_pattern(p_weak_magnitude, p_duration);
  164. CHHapticPattern *strong_pattern = get_vibration_pattern(p_strong_magnitude, p_duration);
  165. RumbleMotor *weak_motor = p_joypad.rumble_context.weak_motor;
  166. RumbleMotor *strong_motor = p_joypad.rumble_context.strong_motor;
  167. [weak_motor execute_pattern:weak_pattern];
  168. [strong_motor execute_pattern:strong_pattern];
  169. p_joypad.ff_effect_timestamp = p_timestamp;
  170. }
  171. void JoypadMacOS::joypad_vibration_stop(Joypad *p_joypad, uint64_t p_timestamp) {
  172. if (!p_joypad.force_feedback) {
  173. return;
  174. }
  175. // If there is no active vibration players, exit.
  176. if (![p_joypad.rumble_context hasActivePlayers]) {
  177. return;
  178. }
  179. RumbleMotor *weak_motor = p_joypad.rumble_context.weak_motor;
  180. RumbleMotor *strong_motor = p_joypad.rumble_context.strong_motor;
  181. [weak_motor stop];
  182. [strong_motor stop];
  183. p_joypad.ff_effect_timestamp = p_timestamp;
  184. }
  185. @interface JoypadMacOSObserver ()
  186. @property(assign, nonatomic) BOOL isObserving;
  187. @property(assign, nonatomic) BOOL isProcessing;
  188. @property(strong, nonatomic) NSMutableDictionary<NSNumber *, Joypad *> *connectedJoypads;
  189. @property(strong, nonatomic) NSMutableArray<Joypad *> *joypadsQueue;
  190. @end
  191. @implementation JoypadMacOSObserver
  192. - (instancetype)init {
  193. self = [super init];
  194. if (self) {
  195. [self godot_commonInit];
  196. }
  197. return self;
  198. }
  199. - (void)godot_commonInit {
  200. self.isObserving = NO;
  201. self.isProcessing = NO;
  202. }
  203. - (void)startProcessing {
  204. self.isProcessing = YES;
  205. for (GCController *controller in self.joypadsQueue) {
  206. [self addMacOSJoypad:controller];
  207. }
  208. [self.joypadsQueue removeAllObjects];
  209. }
  210. - (void)startObserving {
  211. if (self.isObserving) {
  212. return;
  213. }
  214. self.isObserving = YES;
  215. self.connectedJoypads = [NSMutableDictionary dictionary];
  216. self.joypadsQueue = [NSMutableArray array];
  217. // Get told when controllers connect, this will be called right away for
  218. // already connected controllers.
  219. [[NSNotificationCenter defaultCenter]
  220. addObserver:self
  221. selector:@selector(controllerWasConnected:)
  222. name:GCControllerDidConnectNotification
  223. object:nil];
  224. // Get told when controllers disconnect.
  225. [[NSNotificationCenter defaultCenter]
  226. addObserver:self
  227. selector:@selector(controllerWasDisconnected:)
  228. name:GCControllerDidDisconnectNotification
  229. object:nil];
  230. }
  231. - (void)finishObserving {
  232. if (self.isObserving) {
  233. [[NSNotificationCenter defaultCenter] removeObserver:self];
  234. }
  235. self.isObserving = NO;
  236. self.isProcessing = NO;
  237. self.connectedJoypads = nil;
  238. self.joypadsQueue = nil;
  239. }
  240. - (void)dealloc {
  241. [self finishObserving];
  242. }
  243. - (NSArray<NSNumber *> *)getAllKeysForController:(GCController *)controller {
  244. NSArray *keys = [self.connectedJoypads allKeys];
  245. NSMutableArray *final_keys = [NSMutableArray array];
  246. for (NSNumber *key in keys) {
  247. Joypad *joypad = [self.connectedJoypads objectForKey:key];
  248. if (joypad.controller == controller) {
  249. [final_keys addObject:key];
  250. }
  251. }
  252. return final_keys;
  253. }
  254. - (int)getJoyIdForController:(GCController *)controller {
  255. NSArray *keys = [self getAllKeysForController:controller];
  256. for (NSNumber *key in keys) {
  257. int joy_id = [key intValue];
  258. return joy_id;
  259. }
  260. return -1;
  261. }
  262. - (void)addMacOSJoypad:(GCController *)controller {
  263. // Get a new id for our controller.
  264. int joy_id = Input::get_singleton()->get_unused_joy_id();
  265. if (joy_id == -1) {
  266. print_verbose("Couldn't retrieve new joy ID.");
  267. return;
  268. }
  269. // Assign our player index.
  270. if (controller.playerIndex == GCControllerPlayerIndexUnset) {
  271. controller.playerIndex = [self getFreePlayerIndex];
  272. }
  273. // Tell Godot about our new controller.
  274. Input::get_singleton()->joy_connection_changed(joy_id, true, String::utf8([controller.vendorName UTF8String]));
  275. Joypad *joypad = [[Joypad alloc] init:controller];
  276. // Add it to our dictionary, this will retain our controllers.
  277. [self.connectedJoypads setObject:joypad forKey:[NSNumber numberWithInt:joy_id]];
  278. // Set our input handler.
  279. [self setControllerInputHandler:controller];
  280. }
  281. - (void)controllerWasConnected:(NSNotification *)notification {
  282. // Get our controller.
  283. GCController *controller = (GCController *)notification.object;
  284. if (!controller) {
  285. print_verbose("Couldn't retrieve new controller.");
  286. return;
  287. }
  288. if ([[self getAllKeysForController:controller] count] > 0) {
  289. print_verbose("Controller is already registered.");
  290. } else if (!self.isProcessing) {
  291. Joypad *joypad = [[Joypad alloc] init:controller];
  292. [self.joypadsQueue addObject:joypad];
  293. } else {
  294. [self addMacOSJoypad:controller];
  295. }
  296. }
  297. - (void)controllerWasDisconnected:(NSNotification *)notification {
  298. // Find our joystick, there should be only one in our dictionary.
  299. GCController *controller = (GCController *)notification.object;
  300. if (!controller) {
  301. return;
  302. }
  303. NSArray *keys = [self getAllKeysForController:controller];
  304. for (NSNumber *key in keys) {
  305. // Tell Godot this joystick is no longer there.
  306. int joy_id = [key intValue];
  307. Input::get_singleton()->joy_connection_changed(joy_id, false, "");
  308. // And remove it from our dictionary.
  309. [self.connectedJoypads removeObjectForKey:key];
  310. }
  311. }
  312. - (GCControllerPlayerIndex)getFreePlayerIndex {
  313. bool have_player_1 = false;
  314. bool have_player_2 = false;
  315. bool have_player_3 = false;
  316. bool have_player_4 = false;
  317. if (self.connectedJoypads == nil) {
  318. NSArray *keys = [self.connectedJoypads allKeys];
  319. for (NSNumber *key in keys) {
  320. Joypad *joypad = [self.connectedJoypads objectForKey:key];
  321. GCController *controller = joypad.controller;
  322. if (controller.playerIndex == GCControllerPlayerIndex1) {
  323. have_player_1 = true;
  324. } else if (controller.playerIndex == GCControllerPlayerIndex2) {
  325. have_player_2 = true;
  326. } else if (controller.playerIndex == GCControllerPlayerIndex3) {
  327. have_player_3 = true;
  328. } else if (controller.playerIndex == GCControllerPlayerIndex4) {
  329. have_player_4 = true;
  330. }
  331. }
  332. }
  333. if (!have_player_1) {
  334. return GCControllerPlayerIndex1;
  335. } else if (!have_player_2) {
  336. return GCControllerPlayerIndex2;
  337. } else if (!have_player_3) {
  338. return GCControllerPlayerIndex3;
  339. } else if (!have_player_4) {
  340. return GCControllerPlayerIndex4;
  341. } else {
  342. return GCControllerPlayerIndexUnset;
  343. }
  344. }
  345. - (void)setControllerInputHandler:(GCController *)controller {
  346. // Hook in the callback handler for the correct gamepad profile.
  347. // This is a bit of a weird design choice on Apples part.
  348. // You need to select the most capable gamepad profile for the
  349. // gamepad attached.
  350. if (controller.extendedGamepad != nil) {
  351. // The extended gamepad profile has all the input you could possibly find on
  352. // a gamepad but will only be active if your gamepad actually has all of
  353. // these...
  354. _weakify(self);
  355. _weakify(controller);
  356. controller.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element) {
  357. _strongify(self);
  358. _strongify(controller);
  359. int joy_id = [self getJoyIdForController:controller];
  360. if (element == gamepad.buttonA) {
  361. Input::get_singleton()->joy_button(joy_id, JoyButton::A,
  362. gamepad.buttonA.isPressed);
  363. } else if (element == gamepad.buttonB) {
  364. Input::get_singleton()->joy_button(joy_id, JoyButton::B,
  365. gamepad.buttonB.isPressed);
  366. } else if (element == gamepad.buttonX) {
  367. Input::get_singleton()->joy_button(joy_id, JoyButton::X,
  368. gamepad.buttonX.isPressed);
  369. } else if (element == gamepad.buttonY) {
  370. Input::get_singleton()->joy_button(joy_id, JoyButton::Y,
  371. gamepad.buttonY.isPressed);
  372. } else if (element == gamepad.leftShoulder) {
  373. Input::get_singleton()->joy_button(joy_id, JoyButton::LEFT_SHOULDER,
  374. gamepad.leftShoulder.isPressed);
  375. } else if (element == gamepad.rightShoulder) {
  376. Input::get_singleton()->joy_button(joy_id, JoyButton::RIGHT_SHOULDER,
  377. gamepad.rightShoulder.isPressed);
  378. } else if (element == gamepad.dpad) {
  379. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_UP,
  380. gamepad.dpad.up.isPressed);
  381. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_DOWN,
  382. gamepad.dpad.down.isPressed);
  383. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_LEFT,
  384. gamepad.dpad.left.isPressed);
  385. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_RIGHT,
  386. gamepad.dpad.right.isPressed);
  387. }
  388. if (element == gamepad.leftThumbstick) {
  389. float value = gamepad.leftThumbstick.xAxis.value;
  390. Input::get_singleton()->joy_axis(joy_id, JoyAxis::LEFT_X, value);
  391. value = -gamepad.leftThumbstick.yAxis.value;
  392. Input::get_singleton()->joy_axis(joy_id, JoyAxis::LEFT_Y, value);
  393. } else if (element == gamepad.rightThumbstick) {
  394. float value = gamepad.rightThumbstick.xAxis.value;
  395. Input::get_singleton()->joy_axis(joy_id, JoyAxis::RIGHT_X, value);
  396. value = -gamepad.rightThumbstick.yAxis.value;
  397. Input::get_singleton()->joy_axis(joy_id, JoyAxis::RIGHT_Y, value);
  398. } else if (element == gamepad.leftTrigger) {
  399. float value = gamepad.leftTrigger.value;
  400. Input::get_singleton()->joy_axis(joy_id, JoyAxis::TRIGGER_LEFT, value);
  401. } else if (element == gamepad.rightTrigger) {
  402. float value = gamepad.rightTrigger.value;
  403. Input::get_singleton()->joy_axis(joy_id, JoyAxis::TRIGGER_RIGHT, value);
  404. }
  405. if (@available(macOS 10.14.1, *)) {
  406. if (element == gamepad.leftThumbstickButton) {
  407. Input::get_singleton()->joy_button(joy_id, JoyButton::LEFT_STICK,
  408. gamepad.leftThumbstickButton.isPressed);
  409. } else if (element == gamepad.rightThumbstickButton) {
  410. Input::get_singleton()->joy_button(joy_id, JoyButton::RIGHT_STICK,
  411. gamepad.rightThumbstickButton.isPressed);
  412. }
  413. }
  414. if (@available(macOS 10.15, *)) {
  415. if (element == gamepad.buttonOptions) {
  416. Input::get_singleton()->joy_button(joy_id, JoyButton::BACK,
  417. gamepad.buttonOptions.isPressed);
  418. } else if (element == gamepad.buttonMenu) {
  419. Input::get_singleton()->joy_button(joy_id, JoyButton::START,
  420. gamepad.buttonMenu.isPressed);
  421. }
  422. }
  423. if (@available(macOS 11, *)) {
  424. if (element == gamepad.buttonHome) {
  425. Input::get_singleton()->joy_button(joy_id, JoyButton::GUIDE,
  426. gamepad.buttonHome.isPressed);
  427. }
  428. if ([gamepad isKindOfClass:[GCXboxGamepad class]]) {
  429. GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad;
  430. if (element == xboxGamepad.paddleButton1) {
  431. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE1,
  432. xboxGamepad.paddleButton1.isPressed);
  433. } else if (element == xboxGamepad.paddleButton2) {
  434. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE2,
  435. xboxGamepad.paddleButton2.isPressed);
  436. } else if (element == xboxGamepad.paddleButton3) {
  437. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE3,
  438. xboxGamepad.paddleButton3.isPressed);
  439. } else if (element == xboxGamepad.paddleButton4) {
  440. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE4,
  441. xboxGamepad.paddleButton4.isPressed);
  442. }
  443. }
  444. }
  445. if (@available(macOS 12, *)) {
  446. if ([gamepad isKindOfClass:[GCXboxGamepad class]]) {
  447. GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad;
  448. if (element == xboxGamepad.buttonShare) {
  449. Input::get_singleton()->joy_button(joy_id, JoyButton::MISC1,
  450. xboxGamepad.buttonShare.isPressed);
  451. }
  452. }
  453. }
  454. };
  455. } else if (controller.microGamepad != nil) {
  456. // Micro gamepads were added in macOS 10.11 and feature just 2 buttons and a d-pad.
  457. _weakify(self);
  458. _weakify(controller);
  459. controller.microGamepad.valueChangedHandler = ^(GCMicroGamepad *gamepad, GCControllerElement *element) {
  460. _strongify(self);
  461. _strongify(controller);
  462. int joy_id = [self getJoyIdForController:controller];
  463. if (element == gamepad.buttonA) {
  464. Input::get_singleton()->joy_button(joy_id, JoyButton::A,
  465. gamepad.buttonA.isPressed);
  466. } else if (element == gamepad.buttonX) {
  467. Input::get_singleton()->joy_button(joy_id, JoyButton::X,
  468. gamepad.buttonX.isPressed);
  469. } else if (element == gamepad.dpad) {
  470. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_UP,
  471. gamepad.dpad.up.isPressed);
  472. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_DOWN,
  473. gamepad.dpad.down.isPressed);
  474. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_LEFT,
  475. gamepad.dpad.left.isPressed);
  476. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_RIGHT,
  477. gamepad.dpad.right.isPressed);
  478. }
  479. };
  480. }
  481. // TODO: Need to add support for controller.motion which gives us access to
  482. // the orientation of the device (if supported).
  483. }
  484. @end
  485. void JoypadMacOS::process_joypads() {
  486. if (@available(macOS 11, *)) {
  487. // Process vibrations in macOS 11+.
  488. NSArray *keys = [observer.connectedJoypads allKeys];
  489. for (NSNumber *key in keys) {
  490. int id = key.intValue;
  491. Joypad *joypad = [observer.connectedJoypads objectForKey:key];
  492. if (joypad.force_feedback) {
  493. Input *input = Input::get_singleton();
  494. uint64_t timestamp = input->get_joy_vibration_timestamp(id);
  495. if (timestamp > (unsigned)joypad.ff_effect_timestamp) {
  496. Vector2 strength = input->get_joy_vibration_strength(id);
  497. float duration = input->get_joy_vibration_duration(id);
  498. if (duration == 0) {
  499. duration = GCHapticDurationInfinite;
  500. }
  501. if (strength.x == 0 && strength.y == 0) {
  502. joypad_vibration_stop(joypad, timestamp);
  503. } else {
  504. joypad_vibration_start(joypad, strength.x, strength.y, duration, timestamp);
  505. }
  506. }
  507. }
  508. }
  509. }
  510. }