|
@@ -42,7 +42,8 @@
|
|
|
#include "../../events/SDL_events_c.h"
|
|
|
#endif
|
|
|
|
|
|
-#if !TARGET_OS_TV
|
|
|
+#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#define SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
#import <CoreMotion/CoreMotion.h>
|
|
|
#endif
|
|
|
|
|
@@ -100,10 +101,10 @@ static id disconnectObserver = nil;
|
|
|
|
|
|
#endif /* SDL_JOYSTICK_MFI */
|
|
|
|
|
|
-#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
static const char *accelerometerName = "iOS Accelerometer";
|
|
|
static CMMotionManager *motionManager = nil;
|
|
|
-#endif /* !TARGET_OS_TV */
|
|
|
+#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER */
|
|
|
|
|
|
static SDL_JoystickDeviceItem *deviceList = NULL;
|
|
|
|
|
@@ -127,10 +128,10 @@ GetDeviceForIndex(int device_index)
|
|
|
return device;
|
|
|
}
|
|
|
|
|
|
+#ifdef SDL_JOYSTICK_MFI
|
|
|
static void
|
|
|
IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
|
|
|
{
|
|
|
-#ifdef SDL_JOYSTICK_MFI
|
|
|
Uint16 *guid16 = (Uint16 *)device->guid.data;
|
|
|
Uint16 vendor = 0;
|
|
|
Uint16 product = 0;
|
|
@@ -328,10 +329,10 @@ IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controlle
|
|
|
/* This will be set when the first button press of the controller is
|
|
|
* detected. */
|
|
|
controller.playerIndex = -1;
|
|
|
-
|
|
|
-#endif /* SDL_JOYSTICK_MFI */
|
|
|
}
|
|
|
+#endif /* SDL_JOYSTICK_MFI */
|
|
|
|
|
|
+#if defined(SDL_JOYSTICK_iOS_ACCELEROMETER) || defined(SDL_JOYSTICK_MFI)
|
|
|
static void
|
|
|
IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
|
|
|
{
|
|
@@ -362,10 +363,7 @@ IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
|
|
|
device->instance_id = SDL_GetNextJoystickInstanceID();
|
|
|
|
|
|
if (accelerometer) {
|
|
|
-#if TARGET_OS_TV || TARGET_OS_OSX
|
|
|
- SDL_free(device);
|
|
|
- return;
|
|
|
-#else
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
device->name = SDL_strdup(accelerometerName);
|
|
|
device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
|
|
|
device->nhats = 0;
|
|
@@ -373,9 +371,17 @@ IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
|
|
|
|
|
|
/* Use the accelerometer name as a GUID. */
|
|
|
SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
|
|
|
-#endif /* TARGET_OS_TV */
|
|
|
+#else
|
|
|
+ SDL_free(device);
|
|
|
+ return;
|
|
|
+#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER */
|
|
|
} else if (controller) {
|
|
|
+#ifdef SDL_JOYSTICK_MFI
|
|
|
IOS_AddMFIJoystickDevice(device, controller);
|
|
|
+#else
|
|
|
+ SDL_free(device);
|
|
|
+ return;
|
|
|
+#endif /* SDL_JOYSTICK_MFI */
|
|
|
}
|
|
|
|
|
|
if (deviceList == NULL) {
|
|
@@ -392,6 +398,7 @@ IOS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
|
|
|
|
|
|
SDL_PrivateJoystickAdded(device->instance_id);
|
|
|
}
|
|
|
+#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER || SDL_JOYSTICK_MFI */
|
|
|
|
|
|
static SDL_JoystickDeviceItem *
|
|
|
IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
|
|
@@ -467,12 +474,12 @@ static int
|
|
|
IOS_JoystickInit(void)
|
|
|
{
|
|
|
if (@available(macos 11.0, *)) @autoreleasepool {
|
|
|
-#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
|
|
|
/* Default behavior, accelerometer as joystick */
|
|
|
IOS_AddJoystickDevice(nil, SDL_TRUE);
|
|
|
}
|
|
|
-#endif /* !TARGET_OS_TV */
|
|
|
+#endif
|
|
|
|
|
|
#ifdef SDL_JOYSTICK_MFI
|
|
|
/* GameController.framework was added in iOS 7. */
|
|
@@ -606,7 +613,7 @@ IOS_JoystickOpen(SDL_Joystick *joystick, int device_index)
|
|
|
|
|
|
@autoreleasepool {
|
|
|
if (device->accelerometer) {
|
|
|
-#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
if (motionManager == nil) {
|
|
|
motionManager = [[CMMotionManager alloc] init];
|
|
|
}
|
|
@@ -614,7 +621,7 @@ IOS_JoystickOpen(SDL_Joystick *joystick, int device_index)
|
|
|
/* Shorter times between updates can significantly increase CPU usage. */
|
|
|
motionManager.accelerometerUpdateInterval = 0.1;
|
|
|
[motionManager startAccelerometerUpdates];
|
|
|
-#endif /* !TARGET_OS_TV */
|
|
|
+#endif
|
|
|
} else {
|
|
|
#ifdef SDL_JOYSTICK_MFI
|
|
|
if (device->uses_pause_handler) {
|
|
@@ -652,7 +659,7 @@ IOS_JoystickOpen(SDL_Joystick *joystick, int device_index)
|
|
|
static void
|
|
|
IOS_AccelerometerUpdate(SDL_Joystick *joystick)
|
|
|
{
|
|
|
-#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
const float maxgforce = SDL_IPHONE_MAX_GFORCE;
|
|
|
const SInt16 maxsint16 = 0x7FFF;
|
|
|
CMAcceleration accel;
|
|
@@ -690,7 +697,7 @@ IOS_AccelerometerUpdate(SDL_Joystick *joystick)
|
|
|
SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
|
|
|
SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
|
|
|
SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
|
|
|
-#endif /* !TARGET_OS_TV */
|
|
|
+#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER */
|
|
|
}
|
|
|
|
|
|
#ifdef SDL_JOYSTICK_MFI
|
|
@@ -1306,9 +1313,9 @@ IOS_JoystickClose(SDL_Joystick *joystick)
|
|
|
#endif /* ENABLE_MFI_RUMBLE */
|
|
|
|
|
|
if (device->accelerometer) {
|
|
|
-#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
[motionManager stopAccelerometerUpdates];
|
|
|
-#endif /* !TARGET_OS_TV */
|
|
|
+#endif
|
|
|
} else if (device->controller) {
|
|
|
#ifdef SDL_JOYSTICK_MFI
|
|
|
GCController *controller = device->controller;
|
|
@@ -1349,9 +1356,9 @@ IOS_JoystickQuit(void)
|
|
|
IOS_RemoveJoystickDevice(deviceList);
|
|
|
}
|
|
|
|
|
|
-#if !TARGET_OS_TV && !TARGET_OS_OSX
|
|
|
+#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
|
|
|
motionManager = nil;
|
|
|
-#endif /* !TARGET_OS_TV */
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
numjoysticks = 0;
|
|
@@ -1367,11 +1374,12 @@ IOS_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
|
|
|
extern SDL_bool IOS_SupportedHIDDevice(IOHIDDeviceRef device);
|
|
|
SDL_bool IOS_SupportedHIDDevice(IOHIDDeviceRef device)
|
|
|
{
|
|
|
+#ifdef SDL_JOYSTICK_MFI
|
|
|
if (@available(macOS 11.0, *)) {
|
|
|
return [GCController supportsHIDDevice:device] ? SDL_TRUE : SDL_FALSE;
|
|
|
- } else {
|
|
|
- return SDL_FALSE;
|
|
|
}
|
|
|
+#endif
|
|
|
+ return SDL_FALSE;
|
|
|
}
|
|
|
#endif
|
|
|
|