Skip to content

Commit

Permalink
Fixed building with an older SDK and macOS target
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Nov 24, 2020
1 parent 268aa45 commit 62e39b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
9 changes: 9 additions & 0 deletions include/SDL_config_macosx.h
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/joystick/SDL_joystick.c
Expand Up @@ -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
Expand Down
54 changes: 31 additions & 23 deletions src/joystick/iphoneos/SDL_mfijoystick.m
Expand Up @@ -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

Expand Down Expand Up @@ -100,10 +101,10 @@ @interface GCMicroGamepad (SDL)

#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;

Expand All @@ -127,10 +128,10 @@ @interface GCMicroGamepad (SDL)
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;
Expand Down Expand Up @@ -328,10 +329,10 @@ @interface GCMicroGamepad (SDL)
/* 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)
{
Expand Down Expand Up @@ -362,20 +363,25 @@ @interface GCMicroGamepad (SDL)
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;
device->nbuttons = 0;

/* 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) {
Expand All @@ -392,6 +398,7 @@ @interface GCMicroGamepad (SDL)

SDL_PrivateJoystickAdded(device->instance_id);
}
#endif /* SDL_JOYSTICK_iOS_ACCELEROMETER || SDL_JOYSTICK_MFI */

static SDL_JoystickDeviceItem *
IOS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
Expand Down Expand Up @@ -467,12 +474,12 @@ @interface GCMicroGamepad (SDL)
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. */
Expand Down Expand Up @@ -606,15 +613,15 @@ @interface GCMicroGamepad (SDL)

@autoreleasepool {
if (device->accelerometer) {
#if !TARGET_OS_TV && !TARGET_OS_OSX
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}

/* 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) {
Expand Down Expand Up @@ -652,7 +659,7 @@ @interface GCMicroGamepad (SDL)
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;
Expand Down Expand Up @@ -690,7 +697,7 @@ @interface GCMicroGamepad (SDL)
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
Expand Down Expand Up @@ -1306,9 +1313,9 @@ -(void)cleanup
#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;
Expand Down Expand Up @@ -1349,9 +1356,9 @@ -(void)cleanup
IOS_RemoveJoystickDevice(deviceList);
}

#if !TARGET_OS_TV && !TARGET_OS_OSX
#ifdef SDL_JOYSTICK_iOS_ACCELEROMETER
motionManager = nil;
#endif /* !TARGET_OS_TV */
#endif
}

numjoysticks = 0;
Expand All @@ -1367,11 +1374,12 @@ -(void)cleanup
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

Expand Down

0 comments on commit 62e39b5

Please sign in to comment.