2015-11-16 00:29:34 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2019-02-01 01:40:23 +00:00
|
|
|
*
|
2015-11-16 00:29:34 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <boolean.h>
|
|
|
|
|
|
|
|
#include <AvailabilityMacros.h>
|
2019-02-01 01:40:23 +00:00
|
|
|
|
|
|
|
#include "../../tasks/tasks_internal.h"
|
|
|
|
|
2019-01-26 23:18:32 +00:00
|
|
|
#import <GameController/GameController.h>
|
2023-02-13 20:49:35 +00:00
|
|
|
#import <CoreHaptics/CoreHaptics.h>
|
2015-11-16 00:29:34 +00:00
|
|
|
|
2015-11-16 09:34:02 +00:00
|
|
|
#ifndef MAX_MFI_CONTROLLERS
|
|
|
|
#define MAX_MFI_CONTROLLERS 4
|
|
|
|
#endif
|
|
|
|
|
2023-06-30 20:05:11 +00:00
|
|
|
#if TARGET_OS_IOS
|
|
|
|
#include "../../configuration.h"
|
|
|
|
static UIImpactFeedbackGenerator *deviceFeedbackGenerator;
|
|
|
|
#endif
|
|
|
|
|
2015-11-16 00:29:34 +00:00
|
|
|
enum
|
|
|
|
{
|
2019-02-01 01:40:23 +00:00
|
|
|
GCCONTROLLER_PLAYER_INDEX_UNSET = -1,
|
2015-11-16 00:29:34 +00:00
|
|
|
};
|
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
@class MFIRumbleController;
|
|
|
|
|
2020-08-03 15:31:22 +00:00
|
|
|
/* TODO/FIXME - static globals */
|
|
|
|
static uint32_t mfi_buttons[MAX_USERS];
|
|
|
|
static int16_t mfi_axes[MAX_USERS][4];
|
|
|
|
static uint32_t mfi_controllers[MAX_MFI_CONTROLLERS];
|
2023-02-13 20:49:35 +00:00
|
|
|
static MFIRumbleController *mfi_rumblers[MAX_MFI_CONTROLLERS];
|
2020-08-03 15:31:22 +00:00
|
|
|
static NSMutableArray *mfiControllers;
|
2023-02-13 20:49:35 +00:00
|
|
|
static bool mfi_inited;
|
2020-08-03 15:31:22 +00:00
|
|
|
|
2015-11-16 00:29:34 +00:00
|
|
|
static bool apple_gamecontroller_available(void)
|
|
|
|
{
|
2023-02-13 20:49:35 +00:00
|
|
|
#if defined(IOS)
|
2019-02-01 01:40:23 +00:00
|
|
|
int major, minor;
|
|
|
|
get_ios_version(&major, &minor);
|
2015-11-16 00:29:34 +00:00
|
|
|
|
2019-02-01 01:40:23 +00:00
|
|
|
if (major <= 6)
|
|
|
|
return false;
|
2023-02-13 20:49:35 +00:00
|
|
|
#endif
|
2015-11-16 00:29:34 +00:00
|
|
|
|
2019-02-01 01:40:23 +00:00
|
|
|
return true;
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 15:46:04 +00:00
|
|
|
static void apple_gamecontroller_joypad_poll_internal(GCController *controller, uint32_t slot)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2023-02-20 15:46:04 +00:00
|
|
|
uint32_t *buttons = &mfi_buttons[slot];
|
|
|
|
/* Retain the values from the paused controller handler and pass them through.
|
|
|
|
* The menu button can be pressed/unpressed
|
2023-02-20 12:21:17 +00:00
|
|
|
* like any other button in iOS 13,
|
|
|
|
* so no need to passthrough anything */
|
2019-07-16 12:41:09 +00:00
|
|
|
if (@available(iOS 13, *))
|
2023-02-20 15:46:04 +00:00
|
|
|
*buttons = 0;
|
2019-07-16 12:41:09 +00:00
|
|
|
else
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
/* Use the paused controller handler for iOS versions below 13 */
|
2023-02-20 15:46:04 +00:00
|
|
|
uint32_t pause = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_START);
|
|
|
|
uint32_t select = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT);
|
|
|
|
uint32_t l3 = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_L3);
|
|
|
|
uint32_t r3 = *buttons & (1 << RETRO_DEVICE_ID_JOYPAD_R3);
|
|
|
|
*buttons = 0 | pause | select | l3 | r3;
|
2019-06-05 16:20:51 +00:00
|
|
|
}
|
2019-02-01 01:40:23 +00:00
|
|
|
memset(mfi_axes[slot], 0, sizeof(mfi_axes[0]));
|
|
|
|
|
|
|
|
if (controller.extendedGamepad)
|
|
|
|
{
|
|
|
|
GCExtendedGamepad *gp = (GCExtendedGamepad *)controller.extendedGamepad;
|
|
|
|
|
|
|
|
*buttons |= gp.dpad.up.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;
|
|
|
|
*buttons |= gp.dpad.down.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
|
|
|
|
*buttons |= gp.dpad.left.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
|
|
|
|
*buttons |= gp.dpad.right.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
|
|
|
|
*buttons |= gp.buttonA.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_B) : 0;
|
|
|
|
*buttons |= gp.buttonB.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_A) : 0;
|
|
|
|
*buttons |= gp.buttonX.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_Y) : 0;
|
|
|
|
*buttons |= gp.buttonY.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_X) : 0;
|
|
|
|
*buttons |= gp.leftShoulder.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L) : 0;
|
|
|
|
*buttons |= gp.rightShoulder.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R) : 0;
|
|
|
|
*buttons |= gp.leftTrigger.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L2) : 0;
|
|
|
|
*buttons |= gp.rightTrigger.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R2) : 0;
|
2023-02-13 20:49:35 +00:00
|
|
|
#if OSX || __IPHONE_OS_VERSION_MAX_ALLOWED >= 120100 || __TV_OS_VERSION_MAX_ALLOWED >= 120100
|
2023-05-11 14:52:44 +00:00
|
|
|
if (@available(iOS 12.1, macOS 10.15, tvOS 12.1, *))
|
2020-09-11 10:39:05 +00:00
|
|
|
{
|
2019-06-05 16:20:51 +00:00
|
|
|
*buttons |= gp.leftThumbstickButton.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L3) : 0;
|
|
|
|
*buttons |= gp.rightThumbstickButton.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R3) : 0;
|
|
|
|
}
|
2019-06-05 16:50:02 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
#if OSX || __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000
|
|
|
|
if (@available(iOS 13, tvOS 13, macOS 10.15, *))
|
2019-07-16 12:41:09 +00:00
|
|
|
{
|
2022-10-01 15:56:16 +00:00
|
|
|
/* Support "Options" button present in PS4 / XBox One controllers */
|
2023-02-20 15:46:04 +00:00
|
|
|
*buttons |= gp.buttonOptions.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_SELECT) : 0;
|
2023-02-14 04:35:38 +00:00
|
|
|
if (@available(iOS 14, tvOS 14, macOS 11, *))
|
|
|
|
*buttons |= gp.buttonHome.pressed ? (1 << RARCH_FIRST_CUSTOM_BIND) : 0;
|
2023-02-13 20:49:35 +00:00
|
|
|
|
2022-10-01 15:56:16 +00:00
|
|
|
/* Support buttons that aren't supported by older mFi controller via "hotkey" combinations:
|
|
|
|
*
|
|
|
|
* LS + Menu => Select
|
|
|
|
* LT + Menu => L3
|
|
|
|
* RT + Menu => R3
|
2023-02-20 12:21:17 +00:00
|
|
|
*/
|
2020-09-11 10:39:05 +00:00
|
|
|
if (gp.buttonMenu.pressed )
|
|
|
|
{
|
|
|
|
if (gp.leftShoulder.pressed)
|
2019-06-05 16:20:51 +00:00
|
|
|
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_SELECT;
|
2020-09-11 10:39:05 +00:00
|
|
|
else if (gp.leftTrigger.pressed)
|
2019-06-05 16:20:51 +00:00
|
|
|
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_L3;
|
2020-09-11 10:39:05 +00:00
|
|
|
else if (gp.rightTrigger.pressed)
|
2019-06-05 16:20:51 +00:00
|
|
|
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_R3;
|
2020-09-11 10:39:05 +00:00
|
|
|
else
|
2019-06-05 16:20:51 +00:00
|
|
|
*buttons |= 1 << RETRO_DEVICE_ID_JOYPAD_START;
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 16:50:02 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-20 15:46:04 +00:00
|
|
|
mfi_axes[slot][0] = gp.leftThumbstick.xAxis.value * 32767.0f;
|
|
|
|
mfi_axes[slot][1] = gp.leftThumbstick.yAxis.value * 32767.0f;
|
|
|
|
mfi_axes[slot][2] = gp.rightThumbstick.xAxis.value * 32767.0f;
|
|
|
|
mfi_axes[slot][3] = gp.rightThumbstick.yAxis.value * 32767.0f;
|
2019-02-01 01:40:23 +00:00
|
|
|
|
|
|
|
}
|
2023-05-09 22:32:55 +00:00
|
|
|
else if (controller.microGamepad)
|
|
|
|
{
|
|
|
|
}
|
2022-10-01 15:56:16 +00:00
|
|
|
|
|
|
|
/* GCGamepad is deprecated */
|
2021-04-03 16:28:49 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated"
|
2019-02-01 01:40:23 +00:00
|
|
|
else if (controller.gamepad)
|
|
|
|
{
|
|
|
|
GCGamepad *gp = (GCGamepad *)controller.gamepad;
|
2023-02-20 15:46:04 +00:00
|
|
|
*buttons |= gp.dpad.up.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;
|
|
|
|
*buttons |= gp.dpad.down.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
|
|
|
|
*buttons |= gp.dpad.left.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
|
|
|
|
*buttons |= gp.dpad.right.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
|
|
|
|
*buttons |= gp.buttonA.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_B) : 0;
|
|
|
|
*buttons |= gp.buttonB.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_A) : 0;
|
|
|
|
*buttons |= gp.buttonX.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_Y) : 0;
|
|
|
|
*buttons |= gp.buttonY.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_X) : 0;
|
|
|
|
*buttons |= gp.leftShoulder.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_L) : 0;
|
|
|
|
*buttons |= gp.rightShoulder.pressed ? (1 << RETRO_DEVICE_ID_JOYPAD_R) : 0;
|
2019-02-01 01:40:23 +00:00
|
|
|
}
|
2021-04-03 16:28:49 +00:00
|
|
|
#pragma clang diagnostic pop
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void apple_gamecontroller_joypad_poll(void)
|
|
|
|
{
|
2019-02-01 01:40:23 +00:00
|
|
|
if (!apple_gamecontroller_available())
|
|
|
|
return;
|
2015-11-16 00:29:34 +00:00
|
|
|
|
2019-02-01 01:40:23 +00:00
|
|
|
for (GCController *controller in [GCController controllers])
|
2023-02-20 15:46:04 +00:00
|
|
|
{
|
|
|
|
/* If we have not assigned a slot to this controller yet, ignore it. */
|
2023-02-21 05:10:00 +00:00
|
|
|
if (controller && (controller.playerIndex >= 0) && (controller.playerIndex < MAX_USERS))
|
2023-02-20 15:54:50 +00:00
|
|
|
apple_gamecontroller_joypad_poll_internal(controller, (uint32_t)controller.playerIndex);
|
2023-02-20 15:46:04 +00:00
|
|
|
}
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
static void apple_gamecontroller_joypad_register(GCController *controller)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2021-04-03 16:28:49 +00:00
|
|
|
#ifdef __IPHONE_14_0
|
2022-10-01 15:56:16 +00:00
|
|
|
/* Don't let tvOS or iOS do anything with **our** buttons!!
|
|
|
|
* iOS will start a screen recording if you hold or doubleclick
|
|
|
|
* the OPTIONS button, we don't want that. */
|
2023-07-01 18:56:20 +00:00
|
|
|
if (@available(iOS 14.0, tvOS 14.0, macOS 11, *))
|
2022-10-01 15:56:16 +00:00
|
|
|
{
|
2023-02-13 20:49:35 +00:00
|
|
|
GCExtendedGamepad *gp = (GCExtendedGamepad *)controller.extendedGamepad;
|
2021-04-03 16:28:49 +00:00
|
|
|
gp.buttonOptions.preferredSystemGestureState = GCSystemGestureStateDisabled;
|
2022-10-01 15:56:16 +00:00
|
|
|
gp.buttonMenu.preferredSystemGestureState = GCSystemGestureStateDisabled;
|
2023-07-01 18:56:20 +00:00
|
|
|
gp.buttonHome.preferredSystemGestureState = GCSystemGestureStateDisabled;
|
2021-04-03 16:28:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-01 15:56:16 +00:00
|
|
|
/* controllerPausedHandler is deprecated in favor
|
|
|
|
* of being able to deal with the menu
|
|
|
|
* button as any other button */
|
2020-09-11 10:39:05 +00:00
|
|
|
if (@available(iOS 13, *))
|
|
|
|
return;
|
2022-10-01 15:56:16 +00:00
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
/* GCGamepad is deprecated */
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated"
|
2020-09-11 10:39:05 +00:00
|
|
|
{
|
2023-02-13 20:49:35 +00:00
|
|
|
controller.controllerPausedHandler = ^(GCController *controller)
|
2020-09-11 10:39:05 +00:00
|
|
|
|
2019-06-05 16:20:51 +00:00
|
|
|
{
|
2020-09-11 10:39:05 +00:00
|
|
|
uint32_t slot = (uint32_t)controller.playerIndex;
|
|
|
|
|
|
|
|
/* Support buttons that aren't supported by the mFi
|
|
|
|
* controller via "hotkey" combinations:
|
|
|
|
*
|
|
|
|
* LS + Menu => Select
|
|
|
|
* LT + Menu => L3
|
|
|
|
* RT + Menu => R3
|
|
|
|
* Note that these are just button presses, and it
|
|
|
|
* does not simulate holding down the button
|
|
|
|
*/
|
|
|
|
if ( controller.gamepad.leftShoulder.pressed
|
|
|
|
|| controller.extendedGamepad.leftShoulder.pressed )
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
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);
|
2020-09-11 10:39:05 +00:00
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
2019-06-05 16:20:51 +00:00
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_SELECT);
|
2020-09-11 10:39:05 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (controller.extendedGamepad.leftTrigger.pressed )
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L2);
|
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
|
|
|
|
mfi_buttons[slot] |= (1 << RETRO_DEVICE_ID_JOYPAD_L3);
|
2020-09-11 10:39:05 +00:00
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
2019-06-05 16:20:51 +00:00
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_L3);
|
2020-09-11 10:39:05 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (controller.extendedGamepad.rightTrigger.pressed )
|
|
|
|
{
|
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_R2);
|
|
|
|
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(), ^{
|
2019-06-05 16:20:51 +00:00
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_R3);
|
2020-09-11 10:39:05 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(), ^{
|
|
|
|
mfi_buttons[slot] &= ~(1 << RETRO_DEVICE_ID_JOYPAD_START);
|
|
|
|
});
|
2020-09-24 17:21:08 +00:00
|
|
|
};
|
2019-06-05 16:20:51 +00:00
|
|
|
}
|
2021-04-03 16:28:49 +00:00
|
|
|
#pragma clang diagnostic pop
|
2023-02-13 20:49:35 +00:00
|
|
|
}
|
2015-11-16 00:29:34 +00:00
|
|
|
|
2023-05-09 22:32:55 +00:00
|
|
|
static void mfi_joypad_autodetect_add(unsigned autoconf_pad, const char *display_name)
|
2019-07-24 21:23:37 +00:00
|
|
|
{
|
2023-05-09 22:32:55 +00:00
|
|
|
input_autoconfigure_connect("mFi Controller", display_name, mfi_joypad.ident, autoconf_pad, 0, 0);
|
2019-07-24 21:23:37 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
#define MFI_RUMBLE_AVAIL API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0))
|
|
|
|
@interface MFIRumbleController : NSObject
|
|
|
|
@property (nonatomic, strong, readonly) GCController *controller;
|
|
|
|
@property (nonatomic, strong) CHHapticEngine *engine MFI_RUMBLE_AVAIL;
|
|
|
|
@property (nonatomic, strong, readonly) id<CHHapticPatternPlayer> strongPlayer MFI_RUMBLE_AVAIL;
|
|
|
|
@property (nonatomic, strong, readonly) id<CHHapticPatternPlayer> weakPlayer MFI_RUMBLE_AVAIL;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MFIRumbleController
|
|
|
|
@synthesize strongPlayer = _strongPlayer;
|
2023-02-20 12:21:17 +00:00
|
|
|
@synthesize weakPlayer = _weakPlayer;
|
2023-02-13 20:49:35 +00:00
|
|
|
|
|
|
|
- (instancetype)initWithController:(GCController*)controller MFI_RUMBLE_AVAIL
|
|
|
|
{
|
|
|
|
if (self = [super init])
|
|
|
|
{
|
|
|
|
if (!controller.haptics)
|
|
|
|
return self;
|
|
|
|
|
|
|
|
_controller = controller;
|
|
|
|
|
|
|
|
[self setupEngine];
|
|
|
|
if (!self.engine)
|
|
|
|
return self;
|
|
|
|
|
|
|
|
_strongPlayer = [self createPlayerWithSharpness:1.0f];
|
2023-02-20 12:21:17 +00:00
|
|
|
_weakPlayer = [self createPlayerWithSharpness:0.5f];
|
2023-02-13 20:49:35 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setupEngine MFI_RUMBLE_AVAIL
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
NSError *error;
|
2023-02-13 20:49:35 +00:00
|
|
|
if (self.engine)
|
|
|
|
return;
|
|
|
|
if (!self.controller)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CHHapticEngine *engine = [self.controller.haptics createEngineWithLocality:GCHapticsLocalityDefault];
|
|
|
|
[engine startAndReturnError:&error];
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self.engine = engine;
|
|
|
|
|
|
|
|
__weak MFIRumbleController *weakSelf = self;
|
2023-02-20 12:21:17 +00:00
|
|
|
self.engine.stoppedHandler = ^(CHHapticEngineStoppedReason stoppedReason)
|
|
|
|
{
|
2023-02-13 20:49:35 +00:00
|
|
|
MFIRumbleController *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
[strongSelf shutdown];
|
|
|
|
};
|
|
|
|
self.engine.resetHandler = ^{
|
|
|
|
MFIRumbleController *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
[strongSelf.engine startAndReturnError:nil];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<CHHapticPatternPlayer>)createPlayerWithSharpness:(float)sharpness MFI_RUMBLE_AVAIL
|
|
|
|
{
|
|
|
|
if (!self.controller)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
[self setupEngine];
|
|
|
|
if (!self.engine)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
CHHapticEventParameter *sharp, *intense;
|
|
|
|
CHHapticEvent *event;
|
|
|
|
CHHapticPattern *pattern;
|
|
|
|
NSError *error;
|
|
|
|
|
2023-02-20 12:21:17 +00:00
|
|
|
sharp = [[CHHapticEventParameter alloc]
|
2023-02-13 20:49:35 +00:00
|
|
|
initWithParameterID:CHHapticEventParameterIDHapticSharpness
|
|
|
|
value:sharpness];
|
|
|
|
intense = [[CHHapticEventParameter alloc]
|
|
|
|
initWithParameterID:CHHapticEventParameterIDHapticIntensity
|
|
|
|
value:1.0f];
|
2023-02-20 12:21:17 +00:00
|
|
|
event = [[CHHapticEvent alloc]
|
2023-02-13 20:49:35 +00:00
|
|
|
initWithEventType:CHHapticEventTypeHapticContinuous
|
|
|
|
parameters:[NSArray arrayWithObjects:sharp, intense, nil]
|
|
|
|
relativeTime:0
|
|
|
|
duration:GCHapticDurationInfinite];
|
|
|
|
pattern = [[CHHapticPattern alloc]
|
|
|
|
initWithEvents:[NSArray arrayWithObject:event]
|
|
|
|
parameters:[[NSArray alloc] init]
|
|
|
|
error:&error];
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
id<CHHapticPatternPlayer> player = [self.engine createPlayerWithPattern:pattern error:&error];
|
|
|
|
if (error)
|
|
|
|
return nil;
|
2023-02-14 19:37:16 +00:00
|
|
|
[player stopAtTime:0 error:&error];
|
2023-02-13 20:49:35 +00:00
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<CHHapticPatternPlayer>)strongPlayer
|
|
|
|
{
|
|
|
|
_strongPlayer = _strongPlayer ?: [self createPlayerWithSharpness:1.0];
|
|
|
|
return _strongPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<CHHapticPatternPlayer>)weakPlayer
|
|
|
|
{
|
|
|
|
_weakPlayer = _weakPlayer ?: [self createPlayerWithSharpness:0.5f];
|
|
|
|
return _weakPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)shutdown
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
if (@available(iOS 14, tvOS 14, macOS 11, *))
|
|
|
|
{
|
|
|
|
_weakPlayer = nil;
|
2023-02-13 20:49:35 +00:00
|
|
|
_strongPlayer = nil;
|
2023-02-20 12:21:17 +00:00
|
|
|
self.engine = nil;
|
2023-02-13 20:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
static void apple_gamecontroller_joypad_setup_haptics(GCController *controller)
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
if (@available(iOS 14, tvOS 14, macOS 11, *))
|
2023-02-13 20:49:35 +00:00
|
|
|
mfi_rumblers[controller.playerIndex] = [[MFIRumbleController alloc] initWithController:controller];
|
|
|
|
}
|
|
|
|
|
2015-11-16 00:29:34 +00:00
|
|
|
static void apple_gamecontroller_joypad_connect(GCController *controller)
|
|
|
|
{
|
2015-11-16 09:34:02 +00:00
|
|
|
signed desired_index = (int32_t)controller.playerIndex;
|
2023-02-20 12:21:17 +00:00
|
|
|
if (!(desired_index >= 0 && desired_index < MAX_MFI_CONTROLLERS))
|
|
|
|
desired_index = 0;
|
2019-02-01 01:40:23 +00:00
|
|
|
|
2023-02-20 12:21:17 +00:00
|
|
|
/* Prevent same controller getting set twice */
|
2020-09-11 10:39:05 +00:00
|
|
|
if ([mfiControllers containsObject:controller])
|
2019-02-05 16:43:14 +00:00
|
|
|
return;
|
2020-09-11 10:39:05 +00:00
|
|
|
|
2017-10-29 07:20:39 +00:00
|
|
|
if (mfi_controllers[desired_index] != (uint32_t)controller.hash)
|
2015-11-16 10:14:36 +00:00
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
/* Desired slot is unused, take it */
|
2015-11-16 10:14:36 +00:00
|
|
|
if (!mfi_controllers[desired_index])
|
|
|
|
{
|
2015-11-16 10:12:00 +00:00
|
|
|
controller.playerIndex = desired_index;
|
2017-02-26 09:33:03 +00:00
|
|
|
mfi_controllers[desired_index] = (uint32_t)controller.hash;
|
2015-11-16 10:12:00 +00:00
|
|
|
}
|
2015-11-16 10:14:36 +00:00
|
|
|
else
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
/* Find a new slot for this controller that's unused */
|
2019-02-01 01:40:23 +00:00
|
|
|
unsigned i;
|
2019-02-03 23:49:35 +00:00
|
|
|
|
2019-02-01 01:40:23 +00:00
|
|
|
for (i = 0; i < MAX_MFI_CONTROLLERS; ++i)
|
|
|
|
{
|
|
|
|
if (mfi_controllers[i])
|
|
|
|
continue;
|
|
|
|
|
2023-02-20 12:21:17 +00:00
|
|
|
mfi_controllers[i] = (uint32_t)controller.hash;
|
2019-02-01 01:40:23 +00:00
|
|
|
controller.playerIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-16 10:12:00 +00:00
|
|
|
}
|
2015-11-16 09:34:02 +00:00
|
|
|
|
2019-01-26 23:18:32 +00:00
|
|
|
[mfiControllers addObject:controller];
|
2023-02-13 20:49:35 +00:00
|
|
|
|
2023-02-20 12:21:17 +00:00
|
|
|
/* Move any non-game controllers (like the Siri remote) to the end */
|
2020-09-11 10:39:05 +00:00
|
|
|
if (mfiControllers.count > 1)
|
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
int newPlayerIndex = 0;
|
2020-09-11 10:39:05 +00:00
|
|
|
NSInteger connectedNonGameControllerIndex = NSNotFound;
|
2023-02-20 12:21:17 +00:00
|
|
|
NSUInteger index = 0;
|
2020-09-11 10:39:05 +00:00
|
|
|
|
|
|
|
for (GCController *connectedController in mfiControllers)
|
|
|
|
{
|
2023-02-13 20:49:35 +00:00
|
|
|
if ( connectedController.microGamepad != nil
|
2023-02-15 10:59:06 +00:00
|
|
|
&& connectedController.extendedGamepad == nil )
|
2020-09-11 10:39:05 +00:00
|
|
|
connectedNonGameControllerIndex = index;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connectedNonGameControllerIndex != NSNotFound)
|
|
|
|
{
|
|
|
|
GCController *nonGameController = [mfiControllers objectAtIndex:connectedNonGameControllerIndex];
|
|
|
|
[mfiControllers removeObjectAtIndex:connectedNonGameControllerIndex];
|
|
|
|
[mfiControllers addObject:nonGameController];
|
|
|
|
}
|
|
|
|
for (GCController *gc in mfiControllers)
|
|
|
|
gc.playerIndex = newPlayerIndex++;
|
2019-01-26 23:18:32 +00:00
|
|
|
}
|
2019-02-01 01:40:23 +00:00
|
|
|
|
2023-05-09 22:32:55 +00:00
|
|
|
if (controller.microGamepad && !controller.extendedGamepad)
|
|
|
|
return;
|
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
apple_gamecontroller_joypad_register(controller);
|
|
|
|
apple_gamecontroller_joypad_setup_haptics(controller);
|
2023-05-09 22:32:55 +00:00
|
|
|
mfi_joypad_autodetect_add((unsigned)controller.playerIndex, [controller.vendorName cStringUsingEncoding:NSUTF8StringEncoding]);
|
2015-11-16 09:34:02 +00:00
|
|
|
}
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void apple_gamecontroller_joypad_disconnect(GCController* controller)
|
|
|
|
{
|
2019-02-01 01:40:23 +00:00
|
|
|
signed pad = (int32_t)controller.playerIndex;
|
|
|
|
|
|
|
|
if (pad == GCCONTROLLER_PLAYER_INDEX_UNSET)
|
|
|
|
return;
|
2015-11-16 10:14:36 +00:00
|
|
|
|
2023-02-20 12:21:17 +00:00
|
|
|
mfi_rumblers[pad] = nil;
|
2019-02-01 01:40:23 +00:00
|
|
|
mfi_controllers[pad] = 0;
|
2020-09-11 10:39:05 +00:00
|
|
|
if ([mfiControllers containsObject:controller])
|
2019-07-24 21:23:37 +00:00
|
|
|
{
|
|
|
|
[mfiControllers removeObject:controller];
|
|
|
|
input_autoconfigure_disconnect(pad, mfi_joypad.ident);
|
|
|
|
}
|
2019-02-01 01:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-01-16 19:05:53 +00:00
|
|
|
void *apple_gamecontroller_joypad_init(void *data)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2023-02-13 20:49:35 +00:00
|
|
|
if (mfi_inited)
|
2020-09-22 00:30:47 +00:00
|
|
|
return (void*)-1;
|
2023-06-30 20:05:11 +00:00
|
|
|
|
|
|
|
#if TARGET_OS_IOS
|
|
|
|
if (!deviceFeedbackGenerator)
|
|
|
|
deviceFeedbackGenerator = [[UIImpactFeedbackGenerator alloc] init];
|
|
|
|
[deviceFeedbackGenerator prepare];
|
|
|
|
#endif
|
|
|
|
|
2020-09-22 00:30:47 +00:00
|
|
|
if (!apple_gamecontroller_available())
|
|
|
|
return NULL;
|
|
|
|
mfiControllers = [[NSMutableArray alloc] initWithCapacity:MAX_MFI_CONTROLLERS];
|
2015-11-16 00:29:34 +00:00
|
|
|
#ifdef __IPHONE_7_0
|
2020-09-22 00:30:47 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidConnectNotification
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
usingBlock:^(NSNotification *note)
|
|
|
|
{
|
|
|
|
apple_gamecontroller_joypad_connect([note object]);
|
|
|
|
}];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidDisconnectNotification
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
usingBlock:^(NSNotification *note)
|
|
|
|
{
|
|
|
|
apple_gamecontroller_joypad_disconnect([note object]);
|
|
|
|
} ];
|
2015-11-16 00:29:34 +00:00
|
|
|
#endif
|
2023-02-13 20:49:35 +00:00
|
|
|
mfi_inited = true;
|
2020-09-22 00:30:47 +00:00
|
|
|
return (void*)-1;
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 01:18:12 +00:00
|
|
|
static void apple_gamecontroller_joypad_destroy(void) { }
|
2015-11-16 00:29:34 +00:00
|
|
|
|
2021-08-08 19:48:19 +00:00
|
|
|
static int32_t apple_gamecontroller_joypad_button(
|
2020-07-18 17:51:14 +00:00
|
|
|
unsigned port, uint16_t joykey)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2020-07-18 17:51:14 +00:00
|
|
|
if (port >= DEFAULT_MAX_PADS)
|
|
|
|
return 0;
|
2020-07-19 01:18:12 +00:00
|
|
|
/* Check hat. */
|
2020-07-19 10:33:54 +00:00
|
|
|
else if (GET_HAT_DIR(joykey))
|
2020-07-19 01:18:12 +00:00
|
|
|
return 0;
|
2020-07-19 10:33:54 +00:00
|
|
|
else if (joykey < 32)
|
|
|
|
return ((mfi_buttons[port] & (1 << joykey)) != 0);
|
2020-07-19 01:18:12 +00:00
|
|
|
return 0;
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 18:21:12 +00:00
|
|
|
static void apple_gamecontroller_joypad_get_buttons(unsigned port,
|
2020-07-19 01:18:12 +00:00
|
|
|
input_bits_t *state)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2019-02-01 01:40:23 +00:00
|
|
|
BITS_COPY16_PTR(state, mfi_buttons[port]);
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 20:38:23 +00:00
|
|
|
static int16_t apple_gamecontroller_joypad_axis(
|
|
|
|
unsigned port, uint32_t joyaxis)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2019-02-01 01:40:23 +00:00
|
|
|
if (AXIS_NEG_GET(joyaxis) < 4)
|
|
|
|
{
|
2023-02-19 17:42:40 +00:00
|
|
|
int16_t axis = AXIS_NEG_GET(joyaxis);
|
2023-02-20 09:10:07 +00:00
|
|
|
int16_t val = mfi_axes[port][axis];
|
|
|
|
if (val < 0)
|
|
|
|
return val;
|
2019-02-01 01:40:23 +00:00
|
|
|
}
|
2023-02-23 12:15:14 +00:00
|
|
|
else if (AXIS_POS_GET(joyaxis) < 4)
|
2019-02-01 01:40:23 +00:00
|
|
|
{
|
2023-02-19 17:42:40 +00:00
|
|
|
int16_t axis = AXIS_POS_GET(joyaxis);
|
2023-02-20 09:10:07 +00:00
|
|
|
int16_t val = mfi_axes[port][axis];
|
|
|
|
if (val > 0)
|
|
|
|
return val;
|
2019-02-01 01:40:23 +00:00
|
|
|
}
|
2023-02-19 17:42:40 +00:00
|
|
|
return 0;
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 01:18:12 +00:00
|
|
|
static int16_t apple_gamecontroller_joypad_state(
|
|
|
|
rarch_joypad_info_t *joypad_info,
|
|
|
|
const struct retro_keybind *binds,
|
|
|
|
unsigned port)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
int16_t ret = 0;
|
2020-07-29 03:31:23 +00:00
|
|
|
uint16_t port_idx = joypad_info->joy_idx;
|
2020-07-19 01:18:12 +00:00
|
|
|
|
2023-02-20 09:10:07 +00:00
|
|
|
if (port_idx < DEFAULT_MAX_PADS)
|
2020-07-19 01:18:12 +00:00
|
|
|
{
|
2023-02-20 09:10:07 +00:00
|
|
|
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
|
|
|
|
{
|
|
|
|
/* Auto-binds are per joypad, not per user. */
|
|
|
|
const uint64_t joykey = (binds[i].joykey != NO_BTN)
|
|
|
|
? binds[i].joykey : joypad_info->auto_binds[i].joykey;
|
|
|
|
const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
|
|
|
|
? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
|
|
|
|
if ( (uint16_t)joykey != NO_BTN
|
|
|
|
&& !GET_HAT_DIR(i)
|
|
|
|
&& (i < 32)
|
|
|
|
&& ((mfi_buttons[port_idx] & (1 << i)) != 0)
|
|
|
|
)
|
|
|
|
ret |= ( 1 << i);
|
|
|
|
else if (joyaxis != AXIS_NONE &&
|
|
|
|
((float)abs(apple_gamecontroller_joypad_axis(port_idx, joyaxis))
|
|
|
|
/ 0x8000) > joypad_info->axis_threshold)
|
|
|
|
ret |= (1 << i);
|
|
|
|
}
|
2020-07-19 01:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-13 20:49:35 +00:00
|
|
|
static bool apple_gamecontroller_joypad_set_rumble(unsigned pad,
|
|
|
|
enum retro_rumble_effect type, uint16_t strength)
|
|
|
|
{
|
2023-06-30 20:05:11 +00:00
|
|
|
#if TARGET_OS_IOS
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
bool enable_device_vibration = settings->bools.enable_device_vibration;
|
|
|
|
|
|
|
|
if (enable_device_vibration && pad == 0)
|
|
|
|
{
|
|
|
|
[deviceFeedbackGenerator impactOccurredWithIntensity:((float)strength)/65535.0f];
|
|
|
|
[deviceFeedbackGenerator prepare];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-20 12:21:17 +00:00
|
|
|
if (pad < MAX_MFI_CONTROLLERS)
|
|
|
|
{
|
|
|
|
if (@available(iOS 14, tvOS 14, macOS 11, *))
|
|
|
|
{
|
|
|
|
MFIRumbleController *rumble = mfi_rumblers[pad];
|
|
|
|
if (rumble)
|
|
|
|
{
|
|
|
|
NSError *error;
|
|
|
|
id<CHHapticPatternPlayer> player = (type == RETRO_RUMBLE_STRONG ? rumble.strongPlayer : rumble.weakPlayer);
|
|
|
|
if (player)
|
|
|
|
{
|
|
|
|
if (strength == 0)
|
|
|
|
[player stopAtTime:0 error:&error];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float str = (float)strength / 65535.0f;
|
|
|
|
CHHapticDynamicParameter *param = [[CHHapticDynamicParameter alloc]
|
|
|
|
initWithParameterID:CHHapticDynamicParameterIDHapticIntensityControl
|
|
|
|
value:str
|
|
|
|
relativeTime:0];
|
|
|
|
[player sendParameters:[NSArray arrayWithObject:param] atTime:0 error:&error];
|
|
|
|
if (!error)
|
|
|
|
[player startAtTime:0 error:&error];
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 20:49:35 +00:00
|
|
|
}
|
2023-02-20 12:21:17 +00:00
|
|
|
return false;
|
2023-02-13 20:49:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:39:38 +00:00
|
|
|
static bool apple_gamecontroller_joypad_query_pad(unsigned pad)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2019-02-01 01:40:23 +00:00
|
|
|
return pad < MAX_USERS;
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:39:38 +00:00
|
|
|
static const char *apple_gamecontroller_joypad_name(unsigned pad)
|
2015-11-16 00:29:34 +00:00
|
|
|
{
|
2023-02-20 12:21:17 +00:00
|
|
|
if (pad < MAX_USERS)
|
|
|
|
return "mFi Controller";
|
|
|
|
return NULL;
|
2015-11-16 00:29:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 01:39:38 +00:00
|
|
|
input_device_driver_t mfi_joypad = {
|
2019-02-01 01:40:23 +00:00
|
|
|
apple_gamecontroller_joypad_init,
|
|
|
|
apple_gamecontroller_joypad_query_pad,
|
|
|
|
apple_gamecontroller_joypad_destroy,
|
|
|
|
apple_gamecontroller_joypad_button,
|
2020-07-19 01:18:12 +00:00
|
|
|
apple_gamecontroller_joypad_state,
|
2019-02-01 01:40:23 +00:00
|
|
|
apple_gamecontroller_joypad_get_buttons,
|
|
|
|
apple_gamecontroller_joypad_axis,
|
|
|
|
apple_gamecontroller_joypad_poll,
|
2023-02-13 20:49:35 +00:00
|
|
|
apple_gamecontroller_joypad_set_rumble,
|
2021-09-04 14:29:54 +00:00
|
|
|
NULL,
|
2019-02-01 01:40:23 +00:00
|
|
|
apple_gamecontroller_joypad_name,
|
|
|
|
"mfi",
|
2015-11-16 00:29:34 +00:00
|
|
|
};
|