Commit 62e39b5fa7b5ea73ead7682f2db47e2655643dec

Sam Lantinga 2020-11-23T20:57:14

Fixed building with an older SDK and macOS target

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