joypad_macos.mm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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<GCController *> *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. [self.joypadsQueue addObject:controller];
  292. } else {
  293. [self addMacOSJoypad:controller];
  294. }
  295. }
  296. - (void)controllerWasDisconnected:(NSNotification *)notification {
  297. // Find our joystick, there should be only one in our dictionary.
  298. GCController *controller = (GCController *)notification.object;
  299. if (!controller) {
  300. return;
  301. }
  302. NSArray *keys = [self getAllKeysForController:controller];
  303. for (NSNumber *key in keys) {
  304. // Tell Godot this joystick is no longer there.
  305. int joy_id = [key intValue];
  306. Input::get_singleton()->joy_connection_changed(joy_id, false, "");
  307. // And remove it from our dictionary.
  308. [self.connectedJoypads removeObjectForKey:key];
  309. }
  310. }
  311. - (GCControllerPlayerIndex)getFreePlayerIndex {
  312. bool have_player_1 = false;
  313. bool have_player_2 = false;
  314. bool have_player_3 = false;
  315. bool have_player_4 = false;
  316. if (self.connectedJoypads == nil) {
  317. NSArray *keys = [self.connectedJoypads allKeys];
  318. for (NSNumber *key in keys) {
  319. Joypad *joypad = [self.connectedJoypads objectForKey:key];
  320. GCController *controller = joypad.controller;
  321. if (controller.playerIndex == GCControllerPlayerIndex1) {
  322. have_player_1 = true;
  323. } else if (controller.playerIndex == GCControllerPlayerIndex2) {
  324. have_player_2 = true;
  325. } else if (controller.playerIndex == GCControllerPlayerIndex3) {
  326. have_player_3 = true;
  327. } else if (controller.playerIndex == GCControllerPlayerIndex4) {
  328. have_player_4 = true;
  329. }
  330. }
  331. }
  332. if (!have_player_1) {
  333. return GCControllerPlayerIndex1;
  334. } else if (!have_player_2) {
  335. return GCControllerPlayerIndex2;
  336. } else if (!have_player_3) {
  337. return GCControllerPlayerIndex3;
  338. } else if (!have_player_4) {
  339. return GCControllerPlayerIndex4;
  340. } else {
  341. return GCControllerPlayerIndexUnset;
  342. }
  343. }
  344. - (void)setControllerInputHandler:(GCController *)controller {
  345. // Hook in the callback handler for the correct gamepad profile.
  346. // This is a bit of a weird design choice on Apples part.
  347. // You need to select the most capable gamepad profile for the
  348. // gamepad attached.
  349. if (controller.extendedGamepad != nil) {
  350. // The extended gamepad profile has all the input you could possibly find on
  351. // a gamepad but will only be active if your gamepad actually has all of
  352. // these...
  353. _weakify(self);
  354. _weakify(controller);
  355. controller.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *gamepad, GCControllerElement *element) {
  356. _strongify(self);
  357. _strongify(controller);
  358. int joy_id = [self getJoyIdForController:controller];
  359. if (element == gamepad.buttonA) {
  360. Input::get_singleton()->joy_button(joy_id, JoyButton::A,
  361. gamepad.buttonA.isPressed);
  362. } else if (element == gamepad.buttonB) {
  363. Input::get_singleton()->joy_button(joy_id, JoyButton::B,
  364. gamepad.buttonB.isPressed);
  365. } else if (element == gamepad.buttonX) {
  366. Input::get_singleton()->joy_button(joy_id, JoyButton::X,
  367. gamepad.buttonX.isPressed);
  368. } else if (element == gamepad.buttonY) {
  369. Input::get_singleton()->joy_button(joy_id, JoyButton::Y,
  370. gamepad.buttonY.isPressed);
  371. } else if (element == gamepad.leftShoulder) {
  372. Input::get_singleton()->joy_button(joy_id, JoyButton::LEFT_SHOULDER,
  373. gamepad.leftShoulder.isPressed);
  374. } else if (element == gamepad.rightShoulder) {
  375. Input::get_singleton()->joy_button(joy_id, JoyButton::RIGHT_SHOULDER,
  376. gamepad.rightShoulder.isPressed);
  377. } else if (element == gamepad.dpad) {
  378. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_UP,
  379. gamepad.dpad.up.isPressed);
  380. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_DOWN,
  381. gamepad.dpad.down.isPressed);
  382. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_LEFT,
  383. gamepad.dpad.left.isPressed);
  384. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_RIGHT,
  385. gamepad.dpad.right.isPressed);
  386. }
  387. if (element == gamepad.leftThumbstick) {
  388. float value = gamepad.leftThumbstick.xAxis.value;
  389. Input::get_singleton()->joy_axis(joy_id, JoyAxis::LEFT_X, value);
  390. value = -gamepad.leftThumbstick.yAxis.value;
  391. Input::get_singleton()->joy_axis(joy_id, JoyAxis::LEFT_Y, value);
  392. } else if (element == gamepad.rightThumbstick) {
  393. float value = gamepad.rightThumbstick.xAxis.value;
  394. Input::get_singleton()->joy_axis(joy_id, JoyAxis::RIGHT_X, value);
  395. value = -gamepad.rightThumbstick.yAxis.value;
  396. Input::get_singleton()->joy_axis(joy_id, JoyAxis::RIGHT_Y, value);
  397. } else if (element == gamepad.leftTrigger) {
  398. float value = gamepad.leftTrigger.value;
  399. Input::get_singleton()->joy_axis(joy_id, JoyAxis::TRIGGER_LEFT, value);
  400. } else if (element == gamepad.rightTrigger) {
  401. float value = gamepad.rightTrigger.value;
  402. Input::get_singleton()->joy_axis(joy_id, JoyAxis::TRIGGER_RIGHT, value);
  403. }
  404. if (@available(macOS 10.14.1, *)) {
  405. if (element == gamepad.leftThumbstickButton) {
  406. Input::get_singleton()->joy_button(joy_id, JoyButton::LEFT_STICK,
  407. gamepad.leftThumbstickButton.isPressed);
  408. } else if (element == gamepad.rightThumbstickButton) {
  409. Input::get_singleton()->joy_button(joy_id, JoyButton::RIGHT_STICK,
  410. gamepad.rightThumbstickButton.isPressed);
  411. }
  412. }
  413. if (@available(macOS 10.15, *)) {
  414. if (element == gamepad.buttonOptions) {
  415. Input::get_singleton()->joy_button(joy_id, JoyButton::BACK,
  416. gamepad.buttonOptions.isPressed);
  417. } else if (element == gamepad.buttonMenu) {
  418. Input::get_singleton()->joy_button(joy_id, JoyButton::START,
  419. gamepad.buttonMenu.isPressed);
  420. }
  421. }
  422. if (@available(macOS 11, *)) {
  423. if (element == gamepad.buttonHome) {
  424. Input::get_singleton()->joy_button(joy_id, JoyButton::GUIDE,
  425. gamepad.buttonHome.isPressed);
  426. }
  427. if ([gamepad isKindOfClass:[GCXboxGamepad class]]) {
  428. GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad;
  429. if (element == xboxGamepad.paddleButton1) {
  430. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE1,
  431. xboxGamepad.paddleButton1.isPressed);
  432. } else if (element == xboxGamepad.paddleButton2) {
  433. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE2,
  434. xboxGamepad.paddleButton2.isPressed);
  435. } else if (element == xboxGamepad.paddleButton3) {
  436. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE3,
  437. xboxGamepad.paddleButton3.isPressed);
  438. } else if (element == xboxGamepad.paddleButton4) {
  439. Input::get_singleton()->joy_button(joy_id, JoyButton::PADDLE4,
  440. xboxGamepad.paddleButton4.isPressed);
  441. }
  442. }
  443. }
  444. if (@available(macOS 12, *)) {
  445. if ([gamepad isKindOfClass:[GCXboxGamepad class]]) {
  446. GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad;
  447. if (element == xboxGamepad.buttonShare) {
  448. Input::get_singleton()->joy_button(joy_id, JoyButton::MISC1,
  449. xboxGamepad.buttonShare.isPressed);
  450. }
  451. }
  452. }
  453. };
  454. } else if (controller.microGamepad != nil) {
  455. // Micro gamepads were added in macOS 10.11 and feature just 2 buttons and a d-pad.
  456. _weakify(self);
  457. _weakify(controller);
  458. controller.microGamepad.valueChangedHandler = ^(GCMicroGamepad *gamepad, GCControllerElement *element) {
  459. _strongify(self);
  460. _strongify(controller);
  461. int joy_id = [self getJoyIdForController:controller];
  462. if (element == gamepad.buttonA) {
  463. Input::get_singleton()->joy_button(joy_id, JoyButton::A,
  464. gamepad.buttonA.isPressed);
  465. } else if (element == gamepad.buttonX) {
  466. Input::get_singleton()->joy_button(joy_id, JoyButton::X,
  467. gamepad.buttonX.isPressed);
  468. } else if (element == gamepad.dpad) {
  469. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_UP,
  470. gamepad.dpad.up.isPressed);
  471. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_DOWN,
  472. gamepad.dpad.down.isPressed);
  473. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_LEFT,
  474. gamepad.dpad.left.isPressed);
  475. Input::get_singleton()->joy_button(joy_id, JoyButton::DPAD_RIGHT,
  476. gamepad.dpad.right.isPressed);
  477. }
  478. };
  479. }
  480. // TODO: Need to add support for controller.motion which gives us access to
  481. // the orientation of the device (if supported).
  482. }
  483. @end
  484. void JoypadMacOS::process_joypads() {
  485. if (@available(macOS 11, *)) {
  486. // Process vibrations in macOS 11+.
  487. NSArray *keys = [observer.connectedJoypads allKeys];
  488. for (NSNumber *key in keys) {
  489. int id = key.intValue;
  490. Joypad *joypad = [observer.connectedJoypads objectForKey:key];
  491. if (joypad.force_feedback) {
  492. Input *input = Input::get_singleton();
  493. uint64_t timestamp = input->get_joy_vibration_timestamp(id);
  494. if (timestamp > (unsigned)joypad.ff_effect_timestamp) {
  495. Vector2 strength = input->get_joy_vibration_strength(id);
  496. float duration = input->get_joy_vibration_duration(id);
  497. if (duration == 0) {
  498. duration = GCHapticDurationInfinite;
  499. }
  500. if (strength.x == 0 && strength.y == 0) {
  501. joypad_vibration_stop(joypad, timestamp);
  502. } else {
  503. joypad_vibration_start(joypad, strength.x, strength.y, duration, timestamp);
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }