(MFI_joypad) Cleanups

This commit is contained in:
twinaphex 2020-09-11 12:39:05 +02:00
parent ab9202c0aa
commit 7c36e615c2
1 changed files with 92 additions and 75 deletions

View File

@ -75,8 +75,8 @@ static void apple_gamecontroller_joypad_poll_internal(GCController *controller)
// Use the paused controller handler for iOS versions below 13 // Use the paused controller handler for iOS versions below 13
pause = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_START); pause = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_START);
select = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT); select = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT);
l3 = *buttons & ( 1 << RETRO_DEVICE_ID_JOYPAD_L3 ); l3 = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_L3);
r3 = *buttons & ( 1 << RETRO_DEVICE_ID_JOYPAD_R3 ); r3 = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_R3);
*buttons = 0 | pause | select | l3 | r3; *buttons = 0 | pause | select | l3 | r3;
} }
memset(mfi_axes[slot], 0, sizeof(mfi_axes[0])); memset(mfi_axes[slot], 0, sizeof(mfi_axes[0]));
@ -98,7 +98,8 @@ static void apple_gamecontroller_joypad_poll_internal(GCController *controller)
*buttons |= gp.leftTrigger.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L2) : 0; *buttons |= gp.leftTrigger.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L2) : 0;
*buttons |= gp.rightTrigger.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R2) : 0; *buttons |= gp.rightTrigger.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R2) : 0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 120100 || __TV_OS_VERSION_MAX_ALLOWED >= 120100 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 120100 || __TV_OS_VERSION_MAX_ALLOWED >= 120100
if (@available(iOS 12.1, *)) { if (@available(iOS 12.1, *))
{
*buttons |= gp.leftThumbstickButton.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L3) : 0; *buttons |= gp.leftThumbstickButton.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L3) : 0;
*buttons |= gp.rightThumbstickButton.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R3) : 0; *buttons |= gp.rightThumbstickButton.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R3) : 0;
} }
@ -115,16 +116,16 @@ static void apple_gamecontroller_joypad_poll_internal(GCController *controller)
// LS + Menu => Select // LS + Menu => Select
// LT + Menu => L3 // LT + Menu => L3
// RT + Menu => R3 // RT + Menu => R3
if ( gp.buttonMenu.pressed ) { if (gp.buttonMenu.pressed )
if ( gp.leftShoulder.pressed ) { {
if (gp.leftShoulder.pressed)
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_SELECT; *buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_SELECT;
} else if ( gp.leftTrigger.pressed ) { else if (gp.leftTrigger.pressed)
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_L3; *buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_L3;
} else if ( gp.rightTrigger.pressed ) { else if (gp.rightTrigger.pressed)
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_R3; *buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_R3;
} else { else
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_START; *buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_START;
}
} }
} }
#endif #endif
@ -168,56 +169,68 @@ static void apple_gamecontroller_joypad_register(GCGamepad *gamepad)
apple_gamecontroller_joypad_poll_internal(updateGamepad.controller); apple_gamecontroller_joypad_poll_internal(updateGamepad.controller);
}; };
if (@available(iOS 13, *)) { if (@available(iOS 13, *))
// controllerPausedHandler is deprecated in favor of being able to deal with the menu {
// button as any other button // controllerPausedHandler is deprecated in favor of being able to deal with the menu
return; // button as any other button
} else { return;
}
else
{
gamepad.controller.controllerPausedHandler = ^(GCController *controller) gamepad.controller.controllerPausedHandler = ^(GCController *controller)
{ {
uint32_t slot = (uint32_t)controller.playerIndex; uint32_t slot = (uint32_t)controller.playerIndex;
// Support buttons that aren't supported by the mFi controller via "hotkey" combinations: /* Support buttons that aren't supported by the mFi
// * controller via "hotkey" combinations:
// LS + Menu => Select *
// LT + Menu => L3 * LS + Menu => Select
// RT + Menu => R3 * LT + Menu => L3
// Note that these are just button presses, and it does not simulate holding down the button * RT + Menu => R3
if ( controller.gamepad.leftShoulder.pressed || controller.extendedGamepad.leftShoulder.pressed ) { * Note that these are just button presses, and it
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START); * does not simulate holding down the button
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L); */
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_SELECT); if ( controller.gamepad.leftShoulder.pressed
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ || controller.extendedGamepad.leftShoulder.pressed )
{
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L);
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_SELECT);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_SELECT); mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_SELECT);
}); });
return; return;
} }
if ( controller.extendedGamepad.leftTrigger.pressed ) {
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L2); if (controller.extendedGamepad.leftTrigger.pressed )
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START); {
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_L3); mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L2);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_L3);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L3); mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L3);
}); });
return; return;
} }
if ( controller.extendedGamepad.rightTrigger.pressed ) {
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_R2); if (controller.extendedGamepad.rightTrigger.pressed )
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START); {
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_R3); mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_R2);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_R3);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_R3); mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_R3);
}); });
return; return;
} }
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_START); mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_START);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START); mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
}); });
}
};
} }
} }
@ -233,9 +246,9 @@ static void apple_gamecontroller_joypad_connect(GCController *controller)
? desired_index : 0; ? desired_index : 0;
/* prevent same controller getting set twice */ /* prevent same controller getting set twice */
if ( [mfiControllers containsObject:controller] ) { if ([mfiControllers containsObject:controller])
return; return;
}
if (mfi_controllers[desired_index] != (uint32_t)controller.hash) if (mfi_controllers[desired_index] != (uint32_t)controller.hash)
{ {
/* desired slot is unused, take it */ /* desired slot is unused, take it */
@ -262,24 +275,28 @@ static void apple_gamecontroller_joypad_connect(GCController *controller)
[mfiControllers addObject:controller]; [mfiControllers addObject:controller];
// move any non-game controllers (like the siri remote) to the end // move any non-game controllers (like the siri remote) to the end
if ( mfiControllers.count > 1 ) { if (mfiControllers.count > 1)
NSInteger connectedNonGameControllerIndex = NSNotFound; {
NSUInteger index = 0; int newPlayerIndex = 0;
for (GCController *connectedController in mfiControllers) { NSInteger connectedNonGameControllerIndex = NSNotFound;
if ( connectedController.gamepad == nil && connectedController.extendedGamepad == nil ) { NSUInteger index = 0;
connectedNonGameControllerIndex = index;
} for (GCController *connectedController in mfiControllers)
index++; {
} if ( connectedController.gamepad == nil
if ( connectedNonGameControllerIndex != NSNotFound ) { && connectedController.extendedGamepad == nil )
GCController *nonGameController = [mfiControllers objectAtIndex:connectedNonGameControllerIndex]; connectedNonGameControllerIndex = index;
[mfiControllers removeObjectAtIndex:connectedNonGameControllerIndex]; index++;
[mfiControllers addObject:nonGameController]; }
}
int newPlayerIndex = 0; if (connectedNonGameControllerIndex != NSNotFound)
for (GCController *gc in mfiControllers) { {
gc.playerIndex = newPlayerIndex++; GCController *nonGameController = [mfiControllers objectAtIndex:connectedNonGameControllerIndex];
} [mfiControllers removeObjectAtIndex:connectedNonGameControllerIndex];
[mfiControllers addObject:nonGameController];
}
for (GCController *gc in mfiControllers)
gc.playerIndex = newPlayerIndex++;
} }
apple_gamecontroller_joypad_register(controller.gamepad); apple_gamecontroller_joypad_register(controller.gamepad);
@ -295,7 +312,7 @@ static void apple_gamecontroller_joypad_disconnect(GCController* controller)
return; return;
mfi_controllers[pad] = 0; mfi_controllers[pad] = 0;
if ( [mfiControllers containsObject:controller] ) if ([mfiControllers containsObject:controller])
{ {
[mfiControllers removeObject:controller]; [mfiControllers removeObject:controller];
input_autoconfigure_disconnect(pad, mfi_joypad.ident); input_autoconfigure_disconnect(pad, mfi_joypad.ident);