Fixed building with an older SDK and macOS target
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
diff --git a/include/SDL_config_macosx.h b/include/SDL_config_macosx.h
index 55ac0e1..938d1c9 100644
--- a/include/SDL_config_macosx.h
+++ b/include/SDL_config_macosx.h
@@ -147,6 +147,15 @@
#define SDL_JOYSTICK_MFI 1
#define SDL_HAPTIC_IOKIT 1
+/* The MFI controller support requires ARC Objective C runtime */
+#if TARGET_OS_OSX
+# if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
+# undef SDL_JOYSTICK_MFI
+# elif defined(__i386__) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12
+# undef SDL_JOYSTICK_MFI
+# endif
+#endif
+
/* Enable the dummy sensor driver */
#define SDL_SENSOR_DUMMY 1
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index bb46c09..35b87c8 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -69,7 +69,7 @@ static SDL_JoystickDriver *SDL_joystick_drivers[] = {
#ifdef SDL_JOYSTICK_IOKIT
&SDL_DARWIN_JoystickDriver,
#endif
-#if (defined(__IPHONEOS__) || defined(__TVOS__)) && !defined(SDL_JOYSTICK_DISABLED)
+#if (defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)) && !defined(SDL_JOYSTICK_DISABLED)
&SDL_IOS_JoystickDriver,
#endif
#ifdef SDL_JOYSTICK_ANDROID
diff --git a/src/joystick/iphoneos/SDL_mfijoystick.m b/src/joystick/iphoneos/SDL_mfijoystick.m
index dc3ecff..efdee50 100644
--- a/src/joystick/iphoneos/SDL_mfijoystick.m
+++ b/src/joystick/iphoneos/SDL_mfijoystick.m
@@ -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