| 1 |
/**************************************************************************/ |
| 2 |
/* joypad_apple.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 |
|
| 31 |
#import "joypad_apple.h" |
| 32 |
|
| 33 |
#import <CoreHaptics/CoreHaptics.h> |
| 34 |
#import <os/log.h> |
| 35 |
|
| 36 |
#include "core/config/project_settings.h" |
| 37 |
#include "main/main.h" |
| 38 |
|
| 39 |
class API_AVAILABLE(macos(11), ios(14.0), tvos(14.0)) RumbleMotor { |
| 40 |
CHHapticEngine *engine; |
| 41 |
id<CHHapticPatternPlayer> player; |
| 42 |
bool is_started; |
| 43 |
|
| 44 |
RumbleMotor(GCController *p_controller, GCHapticsLocality p_locality) { |
| 45 |
engine = [p_controller.haptics createEngineWithLocality:p_locality]; |
| 46 |
engine.autoShutdownEnabled = YES; |
| 47 |
} |
| 48 |
|
| 49 |
public: |
| 50 |
static RumbleMotor *create(GCController *p_controller, GCHapticsLocality p_locality) { |
| 51 |
if ([p_controller.haptics.supportedLocalities containsObject:p_locality]) { |
| 52 |
return memnew(RumbleMotor(p_controller, p_locality)); |
| 53 |
} |
| 54 |
return nullptr; |
| 55 |
} |
| 56 |
|
| 57 |
_ALWAYS_INLINE_ bool has_active_player() { |
| 58 |
return player != nil; |
| 59 |
} |
| 60 |
|
| 61 |
void execute_pattern(CHHapticPattern *p_pattern) { |
| 62 |
NSError *error; |
| 63 |
if (!is_started) { |
| 64 |
ERR_FAIL_COND_MSG(![engine startAndReturnError:&error], "Couldn't start controller haptic engine: " + String::utf8(error.localizedDescription.UTF8String)); |
| 65 |
is_started = YES; |
| 66 |
} |
| 67 |
|
| 68 |
player = [engine createPlayerWithPattern:p_pattern error:&error]; |
| 69 |
ERR_FAIL_COND_MSG(error, "Couldn't create controller haptic pattern player: " + String::utf8(error.localizedDescription.UTF8String)); |
| 70 |
ERR_FAIL_COND_MSG(![player startAtTime:CHHapticTimeImmediate error:&error], "Couldn't execute controller haptic pattern: " + String::utf8(error.localizedDescription.UTF8String)); |
| 71 |
} |
| 72 |
|
| 73 |
void stop() { |
| 74 |
id<CHHapticPatternPlayer> old_player = player; |
| 75 |
player = nil; |
| 76 |
|
| 77 |
NSError *error; |
| 78 |
ERR_FAIL_COND_MSG(![old_player stopAtTime:CHHapticTimeImmediate error:&error], "Couldn't stop controller haptic pattern: " + String::utf8(error.localizedDescription.UTF8String)); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
class API_AVAILABLE(macos(11), ios(14.0), tvos(14.0)) RumbleContext { |
| 83 |
RumbleMotor *weak_motor; |
| 84 |
RumbleMotor *strong_motor; |
| 85 |
|
| 86 |
public: |
| 87 |
RumbleContext(GCController *p_controller) { |
| 88 |
weak_motor = RumbleMotor::create(p_controller, GCHapticsLocalityRightHandle); |
| 89 |
strong_motor = RumbleMotor::create(p_controller, GCHapticsLocalityLeftHandle); |
| 90 |
} |
| 91 |
|
| 92 |
~RumbleContext() { |
| 93 |
if (weak_motor) { |
| 94 |
memdelete(weak_motor); |
| 95 |
} |
| 96 |
if (strong_motor) { |
| 97 |
memdelete(strong_motor); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
_ALWAYS_INLINE_ bool has_motors() { |
| 102 |
return weak_motor != nullptr && strong_motor != nullptr; |
| 103 |
} |
| 104 |
|
| 105 |
_ALWAYS_INLINE_ bool has_active_players() { |
| 106 |
if (!has_motors()) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
return (weak_motor && weak_motor->has_active_player()) || (strong_motor && strong_motor->has_active_player()); |
| 110 |
} |
| 111 |
|
| 112 |
void stop() { |
| 113 |
if (weak_motor) { |
| 114 |
weak_motor->stop(); |
| 115 |
} |
| 116 |
if (strong_motor) { |
| 117 |
strong_motor->stop(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void play_weak_pattern(CHHapticPattern *p_pattern) { |
| 122 |
if (weak_motor) { |
| 123 |
weak_motor->execute_pattern(p_pattern); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void play_strong_pattern(CHHapticPattern *p_pattern) { |
| 128 |
if (strong_motor) { |
| 129 |
strong_motor->execute_pattern(p_pattern); |
| 130 |
} |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
GameController::GameController(int p_joy_id, GCController *p_controller) : |
| 135 |
joy_id(p_joy_id), controller(p_controller) { |
| 136 |
force_feedback = NO; |
| 137 |
|
| 138 |
for (int i = 0; i < (int)JoyAxis::MAX; i++) { |
| 139 |
axis_value[i] = 0.0; |
| 140 |
} |
| 141 |
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { |
| 142 |
if (controller.haptics != nil) { |
| 143 |
// Create a rumble context for the controller. |
| 144 |
rumble_context = memnew(RumbleContext(p_controller)); |
| 145 |
|
| 146 |
// If the rumble motors aren't available, disable force feedback. |
| 147 |
force_feedback = rumble_context->has_motors(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { |
| 152 |
if ([controller.productCategory isEqualToString:@"Switch Pro Controller"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L/R)"]) { |
| 153 |
double_nintendo_joycon_layout = true; |
| 154 |
} |
| 155 |
|
| 156 |
if ([controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (L)"] || [controller.productCategory isEqualToString:@"Nintendo Switch Joy-Con (R)"]) { |
| 157 |
single_nintendo_joycon_layout = true; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
int l_joy_id = joy_id; |
| 162 |
|
| 163 |
auto BUTTON = [l_joy_id](JoyButton p_button) { |
| 164 |
return ^(GCControllerButtonInput *button, float value, BOOL pressed) { |
| 165 |
Input::get_singleton()->joy_button(l_joy_id, p_button, pressed); |
| 166 |
}; |
| 167 |
}; |
| 168 |
|
| 169 |
auto JOYSTICK_LEFT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) { |
| 170 |
if (axis_value[(int)JoyAxis::LEFT_X] != xValue) { |
| 171 |
axis_changed_mask |= (1 << (int)JoyAxis::LEFT_X); |
| 172 |
axis_value[(int)JoyAxis::LEFT_X] = xValue; |
| 173 |
} |
| 174 |
if (axis_value[(int)JoyAxis::LEFT_Y] != -yValue) { |
| 175 |
axis_changed_mask |= (1 << (int)JoyAxis::LEFT_Y); |
| 176 |
axis_value[(int)JoyAxis::LEFT_Y] = -yValue; |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
auto JOYSTICK_RIGHT = ^(GCControllerDirectionPad *dpad, float xValue, float yValue) { |
| 181 |
if (axis_value[(int)JoyAxis::RIGHT_X] != xValue) { |
| 182 |
axis_changed_mask |= (1 << (int)JoyAxis::RIGHT_X); |
| 183 |
axis_value[(int)JoyAxis::RIGHT_X] = xValue; |
| 184 |
} |
| 185 |
if (axis_value[(int)JoyAxis::RIGHT_Y] != -yValue) { |
| 186 |
axis_changed_mask |= (1 << (int)JoyAxis::RIGHT_Y); |
| 187 |
axis_value[(int)JoyAxis::RIGHT_Y] = -yValue; |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
auto TRIGGER_LEFT = ^(GCControllerButtonInput *button, float value, BOOL pressed) { |
| 192 |
if (axis_value[(int)JoyAxis::TRIGGER_LEFT] != value) { |
| 193 |
axis_changed_mask |= (1 << (int)JoyAxis::TRIGGER_LEFT); |
| 194 |
axis_value[(int)JoyAxis::TRIGGER_LEFT] = value; |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
auto TRIGGER_RIGHT = ^(GCControllerButtonInput *button, float value, BOOL pressed) { |
| 199 |
if (axis_value[(int)JoyAxis::TRIGGER_RIGHT] != value) { |
| 200 |
axis_changed_mask |= (1 << (int)JoyAxis::TRIGGER_RIGHT); |
| 201 |
axis_value[(int)JoyAxis::TRIGGER_RIGHT] = value; |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { |
| 206 |
if (controller.physicalInputProfile != nil) { |
| 207 |
GCPhysicalInputProfile *profile = controller.physicalInputProfile; |
| 208 |
|
| 209 |
GCControllerButtonInput *buttonA = profile.buttons[GCInputButtonA]; |
| 210 |
GCControllerButtonInput *buttonB = profile.buttons[GCInputButtonB]; |
| 211 |
GCControllerButtonInput *buttonX = profile.buttons[GCInputButtonX]; |
| 212 |
GCControllerButtonInput *buttonY = profile.buttons[GCInputButtonY]; |
| 213 |
if (double_nintendo_joycon_layout) { |
| 214 |
if (buttonA) { |
| 215 |
buttonA.pressedChangedHandler = BUTTON(JoyButton::B); |
| 216 |
} |
| 217 |
if (buttonB) { |
| 218 |
buttonB.pressedChangedHandler = BUTTON(JoyButton::A); |
| 219 |
} |
| 220 |
if (buttonX) { |
| 221 |
buttonX.pressedChangedHandler = BUTTON(JoyButton::Y); |
| 222 |
} |
| 223 |
if (buttonY) { |
| 224 |
buttonY.pressedChangedHandler = BUTTON(JoyButton::X); |
| 225 |
} |
| 226 |
} else if (single_nintendo_joycon_layout) { |
| 227 |
if (buttonA) { |
| 228 |
buttonA.pressedChangedHandler = BUTTON(JoyButton::A); |
| 229 |
} |
| 230 |
if (buttonB) { |
| 231 |
buttonB.pressedChangedHandler = BUTTON(JoyButton::X); |
| 232 |
} |
| 233 |
if (buttonX) { |
| 234 |
buttonX.pressedChangedHandler = BUTTON(JoyButton::B); |
| 235 |
} |
| 236 |
if (buttonY) { |
| 237 |
buttonY.pressedChangedHandler = BUTTON(JoyButton::Y); |
| 238 |
} |
| 239 |
} else { |
| 240 |
if (buttonA) { |
| 241 |
buttonA.pressedChangedHandler = BUTTON(JoyButton::A); |
| 242 |
} |
| 243 |
if (buttonB) { |
| 244 |
buttonB.pressedChangedHandler = BUTTON(JoyButton::B); |
| 245 |
} |
| 246 |
if (buttonX) { |
| 247 |
buttonX.pressedChangedHandler = BUTTON(JoyButton::X); |
| 248 |
} |
| 249 |
if (buttonY) { |
| 250 |
buttonY.pressedChangedHandler = BUTTON(JoyButton::Y); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
GCControllerButtonInput *leftThumbstickButton = profile.buttons[GCInputLeftThumbstickButton]; |
| 255 |
GCControllerButtonInput *rightThumbstickButton = profile.buttons[GCInputRightThumbstickButton]; |
| 256 |
if (leftThumbstickButton) { |
| 257 |
leftThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::LEFT_STICK); |
| 258 |
} |
| 259 |
if (rightThumbstickButton) { |
| 260 |
rightThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::RIGHT_STICK); |
| 261 |
} |
| 262 |
|
| 263 |
GCControllerButtonInput *leftShoulder = profile.buttons[GCInputLeftShoulder]; |
| 264 |
GCControllerButtonInput *rightShoulder = profile.buttons[GCInputRightShoulder]; |
| 265 |
if (leftShoulder) { |
| 266 |
leftShoulder.pressedChangedHandler = BUTTON(JoyButton::LEFT_SHOULDER); |
| 267 |
} |
| 268 |
if (rightShoulder) { |
| 269 |
rightShoulder.pressedChangedHandler = BUTTON(JoyButton::RIGHT_SHOULDER); |
| 270 |
} |
| 271 |
|
| 272 |
GCControllerButtonInput *leftTrigger = profile.buttons[GCInputLeftTrigger]; |
| 273 |
GCControllerButtonInput *rightTrigger = profile.buttons[GCInputRightTrigger]; |
| 274 |
if (leftTrigger) { |
| 275 |
leftTrigger.valueChangedHandler = TRIGGER_LEFT; |
| 276 |
} |
| 277 |
if (rightTrigger) { |
| 278 |
rightTrigger.valueChangedHandler = TRIGGER_RIGHT; |
| 279 |
} |
| 280 |
|
| 281 |
GCControllerButtonInput *buttonMenu = profile.buttons[GCInputButtonMenu]; |
| 282 |
GCControllerButtonInput *buttonHome = profile.buttons[GCInputButtonHome]; |
| 283 |
GCControllerButtonInput *buttonOptions = profile.buttons[GCInputButtonOptions]; |
| 284 |
if (buttonMenu) { |
| 285 |
buttonMenu.pressedChangedHandler = BUTTON(JoyButton::START); |
| 286 |
} |
| 287 |
if (buttonHome) { |
| 288 |
buttonHome.pressedChangedHandler = BUTTON(JoyButton::GUIDE); |
| 289 |
} |
| 290 |
if (buttonOptions) { |
| 291 |
buttonOptions.pressedChangedHandler = BUTTON(JoyButton::BACK); |
| 292 |
} |
| 293 |
|
| 294 |
// Xbox controller buttons. |
| 295 |
if (@available(macOS 12.0, iOS 15.0, tvOS 15.0, *)) { |
| 296 |
GCControllerButtonInput *buttonShare = profile.buttons[GCInputButtonShare]; |
| 297 |
if (buttonShare) { |
| 298 |
buttonShare.pressedChangedHandler = BUTTON(JoyButton::MISC1); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
GCControllerButtonInput *paddleButton1 = profile.buttons[GCInputXboxPaddleOne]; |
| 303 |
GCControllerButtonInput *paddleButton2 = profile.buttons[GCInputXboxPaddleTwo]; |
| 304 |
GCControllerButtonInput *paddleButton3 = profile.buttons[GCInputXboxPaddleThree]; |
| 305 |
GCControllerButtonInput *paddleButton4 = profile.buttons[GCInputXboxPaddleFour]; |
| 306 |
if (paddleButton1) { |
| 307 |
paddleButton1.pressedChangedHandler = BUTTON(JoyButton::PADDLE1); |
| 308 |
} |
| 309 |
if (paddleButton2) { |
| 310 |
paddleButton2.pressedChangedHandler = BUTTON(JoyButton::PADDLE2); |
| 311 |
} |
| 312 |
if (paddleButton3) { |
| 313 |
paddleButton3.pressedChangedHandler = BUTTON(JoyButton::PADDLE3); |
| 314 |
} |
| 315 |
if (paddleButton4) { |
| 316 |
paddleButton4.pressedChangedHandler = BUTTON(JoyButton::PADDLE4); |
| 317 |
} |
| 318 |
|
| 319 |
GCControllerDirectionPad *leftThumbstick = profile.dpads[GCInputLeftThumbstick]; |
| 320 |
if (leftThumbstick) { |
| 321 |
leftThumbstick.valueChangedHandler = JOYSTICK_LEFT; |
| 322 |
} |
| 323 |
|
| 324 |
GCControllerDirectionPad *rightThumbstick = profile.dpads[GCInputRightThumbstick]; |
| 325 |
if (rightThumbstick) { |
| 326 |
rightThumbstick.valueChangedHandler = JOYSTICK_RIGHT; |
| 327 |
} |
| 328 |
|
| 329 |
GCControllerDirectionPad *dpad = nil; |
| 330 |
if (controller.extendedGamepad != nil) { |
| 331 |
dpad = controller.extendedGamepad.dpad; |
| 332 |
} else if (controller.microGamepad != nil) { |
| 333 |
dpad = controller.microGamepad.dpad; |
| 334 |
} |
| 335 |
if (dpad) { |
| 336 |
dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP); |
| 337 |
dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN); |
| 338 |
dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT); |
| 339 |
dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT); |
| 340 |
} |
| 341 |
} |
| 342 |
} else if (controller.extendedGamepad != nil) { |
| 343 |
GCExtendedGamepad *gamepad = controller.extendedGamepad; |
| 344 |
|
| 345 |
if (double_nintendo_joycon_layout) { |
| 346 |
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::B); |
| 347 |
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::A); |
| 348 |
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::Y); |
| 349 |
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::X); |
| 350 |
} else if (single_nintendo_joycon_layout) { |
| 351 |
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A); |
| 352 |
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::X); |
| 353 |
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::B); |
| 354 |
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y); |
| 355 |
} else { |
| 356 |
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A); |
| 357 |
gamepad.buttonB.pressedChangedHandler = BUTTON(JoyButton::B); |
| 358 |
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::X); |
| 359 |
gamepad.buttonY.pressedChangedHandler = BUTTON(JoyButton::Y); |
| 360 |
} |
| 361 |
|
| 362 |
gamepad.leftShoulder.pressedChangedHandler = BUTTON(JoyButton::LEFT_SHOULDER); |
| 363 |
gamepad.rightShoulder.pressedChangedHandler = BUTTON(JoyButton::RIGHT_SHOULDER); |
| 364 |
gamepad.dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP); |
| 365 |
gamepad.dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN); |
| 366 |
gamepad.dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT); |
| 367 |
gamepad.dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT); |
| 368 |
|
| 369 |
gamepad.leftThumbstick.valueChangedHandler = JOYSTICK_LEFT; |
| 370 |
gamepad.rightThumbstick.valueChangedHandler = JOYSTICK_RIGHT; |
| 371 |
gamepad.leftTrigger.valueChangedHandler = TRIGGER_LEFT; |
| 372 |
gamepad.rightTrigger.valueChangedHandler = TRIGGER_RIGHT; |
| 373 |
|
| 374 |
if (@available(macOS 10.14.1, iOS 12.1, tvOS 12.1, *)) { |
| 375 |
gamepad.leftThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::LEFT_STICK); |
| 376 |
gamepad.rightThumbstickButton.pressedChangedHandler = BUTTON(JoyButton::RIGHT_STICK); |
| 377 |
} |
| 378 |
|
| 379 |
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { |
| 380 |
gamepad.buttonOptions.pressedChangedHandler = BUTTON(JoyButton::BACK); |
| 381 |
gamepad.buttonMenu.pressedChangedHandler = BUTTON(JoyButton::START); |
| 382 |
} |
| 383 |
|
| 384 |
if (@available(macOS 11, iOS 14.0, tvOS 14.0, *)) { |
| 385 |
gamepad.buttonHome.pressedChangedHandler = BUTTON(JoyButton::GUIDE); |
| 386 |
if ([gamepad isKindOfClass:[GCXboxGamepad class]]) { |
| 387 |
GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad; |
| 388 |
xboxGamepad.paddleButton1.pressedChangedHandler = BUTTON(JoyButton::PADDLE1); |
| 389 |
xboxGamepad.paddleButton2.pressedChangedHandler = BUTTON(JoyButton::PADDLE2); |
| 390 |
xboxGamepad.paddleButton3.pressedChangedHandler = BUTTON(JoyButton::PADDLE3); |
| 391 |
xboxGamepad.paddleButton4.pressedChangedHandler = BUTTON(JoyButton::PADDLE4); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (@available(macOS 12, iOS 15.0, tvOS 15.0, *)) { |
| 396 |
if ([gamepad isKindOfClass:[GCXboxGamepad class]]) { |
| 397 |
GCXboxGamepad *xboxGamepad = (GCXboxGamepad *)gamepad; |
| 398 |
xboxGamepad.buttonShare.pressedChangedHandler = BUTTON(JoyButton::MISC1); |
| 399 |
} |
| 400 |
} |
| 401 |
} else if (controller.microGamepad != nil) { |
| 402 |
GCMicroGamepad *gamepad = controller.microGamepad; |
| 403 |
|
| 404 |
gamepad.buttonA.pressedChangedHandler = BUTTON(JoyButton::A); |
| 405 |
gamepad.buttonX.pressedChangedHandler = BUTTON(JoyButton::X); |
| 406 |
gamepad.dpad.up.pressedChangedHandler = BUTTON(JoyButton::DPAD_UP); |
| 407 |
gamepad.dpad.down.pressedChangedHandler = BUTTON(JoyButton::DPAD_DOWN); |
| 408 |
gamepad.dpad.left.pressedChangedHandler = BUTTON(JoyButton::DPAD_LEFT); |
| 409 |
gamepad.dpad.right.pressedChangedHandler = BUTTON(JoyButton::DPAD_RIGHT); |
| 410 |
} |
| 411 |
|
| 412 |
// TODO: Need to add support for controller.motion which gives us access to |
| 413 |
// the orientation of the device (if supported). |
| 414 |
} |
| 415 |
|
| 416 |
GameController::~GameController() { |
| 417 |
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { |
| 418 |
if (rumble_context) { |
| 419 |
memdelete(rumble_context); |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
JoypadApple::JoypadApple() { |
| 425 |
connect_observer = [NSNotificationCenter.defaultCenter |
| 426 |
addObserverForName:GCControllerDidConnectNotification |
| 427 |
object:nil |
| 428 |
queue:NSOperationQueue.mainQueue |
| 429 |
usingBlock:^(NSNotification *notification) { |
| 430 |
GCController *controller = notification.object; |
| 431 |
if (!controller) { |
| 432 |
return; |
| 433 |
} |
| 434 |
add_joypad(controller); |
| 435 |
}]; |
| 436 |
|
| 437 |
disconnect_observer = [NSNotificationCenter.defaultCenter |
| 438 |
addObserverForName:GCControllerDidDisconnectNotification |
| 439 |
object:nil |
| 440 |
queue:NSOperationQueue.mainQueue |
| 441 |
usingBlock:^(NSNotification *notification) { |
| 442 |
GCController *controller = notification.object; |
| 443 |
if (!controller) { |
| 444 |
return; |
| 445 |
} |
| 446 |
remove_joypad(controller); |
| 447 |
}]; |
| 448 |
|
| 449 |
if (@available(macOS 11.3, iOS 14.5, tvOS 14.5, *)) { |
| 450 |
GCController.shouldMonitorBackgroundEvents = YES; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
JoypadApple::~JoypadApple() { |
| 455 |
for (KeyValue<int, GameController *> &E : joypads) { |
| 456 |
memdelete(E.value); |
| 457 |
E.value = nullptr; |
| 458 |
} |
| 459 |
|
| 460 |
[NSNotificationCenter.defaultCenter removeObserver:connect_observer]; |
| 461 |
[NSNotificationCenter.defaultCenter removeObserver:disconnect_observer]; |
| 462 |
} |
| 463 |
|
| 464 |
// Finds the rightmost set bit in a number, n. |
| 465 |
// variation of https://www.geeksforgeeks.org/position-of-rightmost-set-bit/ |
| 466 |
int rightmost_one(int n) { |
| 467 |
return __builtin_ctz(n & -n) + 1; |
| 468 |
} |
| 469 |
|
| 470 |
GCControllerPlayerIndex JoypadApple::get_free_player_index() { |
| 471 |
// player_set will be a bitfield where each bit represents a player index. |
| 472 |
__block uint32_t player_set = 0; |
| 473 |
for (const KeyValue<GCController *, int> &E : controller_to_joy_id) { |
| 474 |
player_set |= 1U << E.key.playerIndex; |
| 475 |
} |
| 476 |
|
| 477 |
// invert, as we want to find the first unset player index. |
| 478 |
int n = rightmost_one((int)(~player_set)); |
| 479 |
if (n >= 5) { |
| 480 |
return GCControllerPlayerIndexUnset; |
| 481 |
} |
| 482 |
|
| 483 |
return (GCControllerPlayerIndex)(n - 1); |
| 484 |
} |
| 485 |
|
| 486 |
void JoypadApple::add_joypad(GCController *p_controller) { |
| 487 |
if (controller_to_joy_id.has(p_controller)) { |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
// Get a new id for our controller. |
| 492 |
int joy_id = Input::get_singleton()->get_unused_joy_id(); |
| 493 |
|
| 494 |
if (joy_id == -1) { |
| 495 |
print_verbose("Couldn't retrieve new joy ID."); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
// Assign our player index. |
| 500 |
if (p_controller.playerIndex == GCControllerPlayerIndexUnset) { |
| 501 |
p_controller.playerIndex = get_free_player_index(); |
| 502 |
} |
| 503 |
|
| 504 |
// Tell Godot about our new controller. |
| 505 |
char const *device_name; |
| 506 |
if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { |
| 507 |
device_name = p_controller.productCategory.UTF8String; |
| 508 |
} else { |
| 509 |
device_name = p_controller.vendorName.UTF8String; |
| 510 |
} |
| 511 |
Input::get_singleton()->joy_connection_changed(joy_id, true, String::utf8(device_name)); |
| 512 |
|
| 513 |
// Assign our player index. |
| 514 |
joypads.insert(joy_id, memnew(GameController(joy_id, p_controller))); |
| 515 |
controller_to_joy_id.insert(p_controller, joy_id); |
| 516 |
} |
| 517 |
|
| 518 |
void JoypadApple::remove_joypad(GCController *p_controller) { |
| 519 |
if (!controller_to_joy_id.has(p_controller)) { |
| 520 |
return; |
| 521 |
} |
| 522 |
|
| 523 |
int joy_id = controller_to_joy_id[p_controller]; |
| 524 |
controller_to_joy_id.erase(p_controller); |
| 525 |
|
| 526 |
// Tell Godot this joystick is no longer there. |
| 527 |
Input::get_singleton()->joy_connection_changed(joy_id, false, ""); |
| 528 |
|
| 529 |
// And remove it from our dictionary. |
| 530 |
GameController **old = joypads.getptr(joy_id); |
| 531 |
memdelete(*old); |
| 532 |
*old = nullptr; |
| 533 |
joypads.erase(joy_id); |
| 534 |
} |
| 535 |
|
| 536 |
API_AVAILABLE(macos(10.15), ios(13.0), tvos(14.0)) |
| 537 |
CHHapticPattern *get_vibration_pattern(float p_magnitude, float p_duration) { |
| 538 |
// Creates a vibration pattern with an intensity and duration. |
| 539 |
NSDictionary *hapticDict = @{ |
| 540 |
CHHapticPatternKeyPattern : @[ |
| 541 |
@{ |
| 542 |
CHHapticPatternKeyEvent : @{ |
| 543 |
CHHapticPatternKeyEventType : CHHapticEventTypeHapticContinuous, |
| 544 |
CHHapticPatternKeyTime : @(CHHapticTimeImmediate), |
| 545 |
CHHapticPatternKeyEventDuration : [NSNumber numberWithFloat:p_duration], |
| 546 |
|
| 547 |
CHHapticPatternKeyEventParameters : @[ |
| 548 |
@{ |
| 549 |
CHHapticPatternKeyParameterID : CHHapticEventParameterIDHapticIntensity, |
| 550 |
CHHapticPatternKeyParameterValue : [NSNumber numberWithFloat:p_magnitude] |
| 551 |
}, |
| 552 |
], |
| 553 |
}, |
| 554 |
}, |
| 555 |
], |
| 556 |
}; |
| 557 |
NSError *error; |
| 558 |
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithDictionary:hapticDict error:&error]; |
| 559 |
return pattern; |
| 560 |
} |
| 561 |
|
| 562 |
void JoypadApple::joypad_vibration_start(GameController &p_joypad, float p_weak_magnitude, float p_strong_magnitude, float p_duration, uint64_t p_timestamp) { |
| 563 |
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) { |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
// If there is active vibration players, stop them. |
| 568 |
if (p_joypad.rumble_context->has_active_players()) { |
| 569 |
joypad_vibration_stop(p_joypad, p_timestamp); |
| 570 |
} |
| 571 |
|
| 572 |
// Gets the default vibration pattern and creates a player for each motor. |
| 573 |
CHHapticPattern *weak_pattern = get_vibration_pattern(p_weak_magnitude, p_duration); |
| 574 |
CHHapticPattern *strong_pattern = get_vibration_pattern(p_strong_magnitude, p_duration); |
| 575 |
|
| 576 |
p_joypad.rumble_context->play_weak_pattern(weak_pattern); |
| 577 |
p_joypad.rumble_context->play_strong_pattern(strong_pattern); |
| 578 |
|
| 579 |
p_joypad.ff_effect_timestamp = p_timestamp; |
| 580 |
} |
| 581 |
|
| 582 |
void JoypadApple::joypad_vibration_stop(GameController &p_joypad, uint64_t p_timestamp) { |
| 583 |
if (!p_joypad.force_feedback) { |
| 584 |
return; |
| 585 |
} |
| 586 |
// If there is no active vibration players, exit. |
| 587 |
if (!p_joypad.rumble_context->has_active_players()) { |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
p_joypad.rumble_context->stop(); |
| 592 |
|
| 593 |
p_joypad.ff_effect_timestamp = p_timestamp; |
| 594 |
} |
| 595 |
|
| 596 |
void JoypadApple::process_joypads() { |
| 597 |
Input *input = Input::get_singleton(); |
| 598 |
|
| 599 |
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { |
| 600 |
for (KeyValue<int, GameController *> &E : joypads) { |
| 601 |
int id = E.key; |
| 602 |
GameController &joypad = *E.value; |
| 603 |
|
| 604 |
uint32_t changed = joypad.axis_changed_mask; |
| 605 |
joypad.axis_changed_mask = 0; |
| 606 |
// Loop over changed axes. |
| 607 |
while (changed) { |
| 608 |
// Find the index of the next set bit. |
| 609 |
uint32_t i = (uint32_t)__builtin_ctzll(changed); |
| 610 |
// Clear the set bit. |
| 611 |
changed &= (changed - 1); |
| 612 |
input->joy_axis(id, (JoyAxis)i, joypad.axis_value[i]); |
| 613 |
} |
| 614 |
|
| 615 |
if (joypad.force_feedback) { |
| 616 |
uint64_t timestamp = input->get_joy_vibration_timestamp(id); |
| 617 |
|
| 618 |
if (timestamp > (unsigned)joypad.ff_effect_timestamp) { |
| 619 |
Vector2 strength = input->get_joy_vibration_strength(id); |
| 620 |
float duration = input->get_joy_vibration_duration(id); |
| 621 |
if (duration == 0) { |
| 622 |
duration = GCHapticDurationInfinite; |
| 623 |
} |
| 624 |
|
| 625 |
if (strength.x == 0 && strength.y == 0) { |
| 626 |
joypad_vibration_stop(joypad, timestamp); |
| 627 |
} else { |
| 628 |
joypad_vibration_start(joypad, strength.x, strength.y, duration, timestamp); |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
} |