Fixed bug 2603 - iOS: update joystick accelerometer code to use CoreMotion instead of the deprecated UIAccelerometer
authorSam Lantinga <slouken@libsdl.org>
Wed, 25 Jun 2014 00:20:21 -0700
changeset 89214a9feef61c85
parent 8920 21ccd40c778a
child 8922 dfb6f8611ebe
Fixed bug 2603 - iOS: update joystick accelerometer code to use CoreMotion instead of the deprecated UIAccelerometer

Alex Szpakowski

SDL's code for exposing the accelerometer as a joystick on iOS currently uses UIAccelerometer, which was superseded by the CoreMotion framework and deprecated since iOS 5.

The UIAccelerometer code still works (for now), but it also throws deprecation warnings whenever SDL is built for iOS, since SDL's deployment target is no longer below iOS 5.

I've created a patch which replaces the old UIAccelerometer code with a replacement based on the CoreMotion framework. It has identical functionality (to SDL users), however iOS apps are now required to link to the CoreMotion framework when using SDL.
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
include/SDL_config_iphoneos.h
premake/README-ios.txt
src/joystick/iphoneos/SDLUIAccelerationDelegate.h
src/joystick/iphoneos/SDLUIAccelerationDelegate.m
src/joystick/iphoneos/SDL_sysjoystick.m
     1.1 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Tue Jun 24 13:31:25 2014 -0700
     1.2 +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Wed Jun 25 00:20:21 2014 -0700
     1.3 @@ -175,8 +175,6 @@
     1.4  		FD6526800DE8FCDD002AD96B /* SDL_timer.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA2E0DD52EDC00FB1D6B /* SDL_timer.c */; };
     1.5  		FD6526810DE8FCDD002AD96B /* SDL_systimer.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA310DD52EDC00FB1D6B /* SDL_systimer.c */; };
     1.6  		FD689F030E26E5B600F90B21 /* SDL_sysjoystick.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */; };
     1.7 -		FD689F040E26E5B600F90B21 /* SDLUIAccelerationDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */; };
     1.8 -		FD689F050E26E5B600F90B21 /* SDLUIAccelerationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */; };
     1.9  		FD689F1C0E26E5D900F90B21 /* SDL_uikitevents.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689F0C0E26E5D900F90B21 /* SDL_uikitevents.h */; };
    1.10  		FD689F1D0E26E5D900F90B21 /* SDL_uikitevents.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F0D0E26E5D900F90B21 /* SDL_uikitevents.m */; };
    1.11  		FD689F1E0E26E5D900F90B21 /* SDL_uikitopengles.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689F0E0E26E5D900F90B21 /* SDL_uikitopengles.h */; };
    1.12 @@ -353,8 +351,6 @@
    1.13  		FD5F9D200E0E08B3008E885B /* SDL_sysjoystick.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sysjoystick.h; sourceTree = "<group>"; };
    1.14  		FD6526630DE8FCCB002AD96B /* libSDL2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDL2.a; sourceTree = BUILT_PRODUCTS_DIR; };
    1.15  		FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_sysjoystick.m; sourceTree = "<group>"; };
    1.16 -		FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDLUIAccelerationDelegate.h; sourceTree = "<group>"; };
    1.17 -		FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDLUIAccelerationDelegate.m; sourceTree = "<group>"; };
    1.18  		FD689F0C0E26E5D900F90B21 /* SDL_uikitevents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitevents.h; sourceTree = "<group>"; };
    1.19  		FD689F0D0E26E5D900F90B21 /* SDL_uikitevents.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_uikitevents.m; sourceTree = "<group>"; };
    1.20  		FD689F0E0E26E5D900F90B21 /* SDL_uikitopengles.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitopengles.h; sourceTree = "<group>"; };
    1.21 @@ -639,8 +635,6 @@
    1.22  			isa = PBXGroup;
    1.23  			children = (
    1.24  				FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */,
    1.25 -				FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */,
    1.26 -				FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */,
    1.27  			);
    1.28  			path = iphoneos;
    1.29  			sourceTree = "<group>";
    1.30 @@ -972,7 +966,6 @@
    1.31  				FDA686000DF244C800F98A1A /* SDL_nullvideo.h in Headers */,
    1.32  				FD5F9D300E0E08B3008E885B /* SDL_joystick_c.h in Headers */,
    1.33  				FD5F9D310E0E08B3008E885B /* SDL_sysjoystick.h in Headers */,
    1.34 -				FD689F040E26E5B600F90B21 /* SDLUIAccelerationDelegate.h in Headers */,
    1.35  				FD689F1C0E26E5D900F90B21 /* SDL_uikitevents.h in Headers */,
    1.36  				FD689F1E0E26E5D900F90B21 /* SDL_uikitopengles.h in Headers */,
    1.37  				FD689F200E26E5D900F90B21 /* SDL_uikitvideo.h in Headers */,
    1.38 @@ -1181,7 +1174,6 @@
    1.39  				FDA685FF0DF244C800F98A1A /* SDL_nullvideo.c in Sources */,
    1.40  				FD5F9D2F0E0E08B3008E885B /* SDL_joystick.c in Sources */,
    1.41  				FD689F030E26E5B600F90B21 /* SDL_sysjoystick.m in Sources */,
    1.42 -				FD689F050E26E5B600F90B21 /* SDLUIAccelerationDelegate.m in Sources */,
    1.43  				FD689F1D0E26E5D900F90B21 /* SDL_uikitevents.m in Sources */,
    1.44  				FD689F1F0E26E5D900F90B21 /* SDL_uikitopengles.m in Sources */,
    1.45  				FD689F210E26E5D900F90B21 /* SDL_uikitvideo.m in Sources */,
     2.1 --- a/include/SDL_config_iphoneos.h	Tue Jun 24 13:31:25 2014 -0700
     2.2 +++ b/include/SDL_config_iphoneos.h	Wed Jun 25 00:20:21 2014 -0700
     2.3 @@ -149,7 +149,7 @@
     2.4  #define SDL_JOYSTICK_DISABLED 0
     2.5  
     2.6  /* Set max recognized G-force from accelerometer
     2.7 -   See src/joystick/uikit/SDLUIAccelerationDelegate.m for notes on why this is needed
     2.8 +   See src/joystick/uikit/SDL_sysjoystick.m for notes on why this is needed
     2.9   */
    2.10  #define SDL_IPHONE_MAX_GFORCE 5.0
    2.11  
     3.1 --- a/premake/README-ios.txt	Tue Jun 24 13:31:25 2014 -0700
     3.2 +++ b/premake/README-ios.txt	Wed Jun 25 00:20:21 2014 -0700
     3.3 @@ -25,9 +25,10 @@
     3.4    -UIKit.framework
     3.5    -Foundation.framework
     3.6    -CoreAudio.framework
     3.7 +  -CoreMotion.framework
     3.8  
     3.9  All of these frameworks are part of the iOS SDK, not part of the core OS X
    3.10  system.
    3.11  
    3.12  Run the clean script to clear out the directory of Xcode-related files
    3.13 -and binaries.
    3.14 \ No newline at end of file
    3.15 +and binaries.
     4.1 --- a/src/joystick/iphoneos/SDLUIAccelerationDelegate.h	Tue Jun 24 13:31:25 2014 -0700
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,44 +0,0 @@
     4.4 -/*
     4.5 -  Simple DirectMedia Layer
     4.6 -  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4.7 -
     4.8 -  This software is provided 'as-is', without any express or implied
     4.9 -  warranty.  In no event will the authors be held liable for any damages
    4.10 -  arising from the use of this software.
    4.11 -
    4.12 -  Permission is granted to anyone to use this software for any purpose,
    4.13 -  including commercial applications, and to alter it and redistribute it
    4.14 -  freely, subject to the following restrictions:
    4.15 -
    4.16 -  1. The origin of this software must not be misrepresented; you must not
    4.17 -     claim that you wrote the original software. If you use this software
    4.18 -     in a product, an acknowledgment in the product documentation would be
    4.19 -     appreciated but is not required.
    4.20 -  2. Altered source versions must be plainly marked as such, and must not be
    4.21 -     misrepresented as being the original software.
    4.22 -  3. This notice may not be removed or altered from any source distribution.
    4.23 -*/
    4.24 -
    4.25 -#import <UIKit/UIKit.h>
    4.26 -#import <SDL_types.h>
    4.27 -
    4.28 -/* *INDENT-OFF* */
    4.29 -@interface SDLUIAccelerationDelegate: NSObject <UIAccelerometerDelegate> {
    4.30 -
    4.31 -	UIAccelerationValue x, y, z;
    4.32 -	BOOL isRunning;
    4.33 -	BOOL hasNewData;
    4.34 -	
    4.35 -}
    4.36 -
    4.37 -+(SDLUIAccelerationDelegate *)sharedDelegate;
    4.38 --(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;
    4.39 --(void)getLastOrientation:(Sint16 *)data;
    4.40 --(void)startup;
    4.41 --(void)shutdown;
    4.42 --(BOOL)isRunning;
    4.43 --(BOOL)hasNewData;
    4.44 --(void)setHasNewData:(BOOL)value;
    4.45 -
    4.46 -@end
    4.47 -/* *INDENT-ON* */
     5.1 --- a/src/joystick/iphoneos/SDLUIAccelerationDelegate.m	Tue Jun 24 13:31:25 2014 -0700
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,141 +0,0 @@
     5.4 -/*
     5.5 -  Simple DirectMedia Layer
     5.6 -  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     5.7 -
     5.8 -  This software is provided 'as-is', without any express or implied
     5.9 -  warranty.  In no event will the authors be held liable for any damages
    5.10 -  arising from the use of this software.
    5.11 -
    5.12 -  Permission is granted to anyone to use this software for any purpose,
    5.13 -  including commercial applications, and to alter it and redistribute it
    5.14 -  freely, subject to the following restrictions:
    5.15 -
    5.16 -  1. The origin of this software must not be misrepresented; you must not
    5.17 -     claim that you wrote the original software. If you use this software
    5.18 -     in a product, an acknowledgment in the product documentation would be
    5.19 -     appreciated but is not required.
    5.20 -  2. Altered source versions must be plainly marked as such, and must not be
    5.21 -     misrepresented as being the original software.
    5.22 -  3. This notice may not be removed or altered from any source distribution.
    5.23 -*/
    5.24 -
    5.25 -#import "SDLUIAccelerationDelegate.h"
    5.26 -/* needed for SDL_IPHONE_MAX_GFORCE macro */
    5.27 -#import "../../../include/SDL_config_iphoneos.h"
    5.28 -
    5.29 -static SDLUIAccelerationDelegate *sharedDelegate=nil;
    5.30 -
    5.31 -@implementation SDLUIAccelerationDelegate
    5.32 -
    5.33 -/*
    5.34 -    Returns a shared instance of the SDLUIAccelerationDelegate, creating the shared delegate if it doesn't exist yet.
    5.35 -*/
    5.36 -+(SDLUIAccelerationDelegate *)sharedDelegate {
    5.37 -    if (sharedDelegate == nil) {
    5.38 -        sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
    5.39 -    }
    5.40 -    return sharedDelegate;
    5.41 -}
    5.42 -/*
    5.43 -    UIAccelerometerDelegate delegate method.  Invoked by the UIAccelerometer instance when it has new data for us.
    5.44 -    We just take the data and mark that we have new data available so that the joystick system will pump it to the
    5.45 -    events system when SDL_SYS_JoystickUpdate is called.
    5.46 -*/
    5.47 --(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    5.48 -
    5.49 -    x = acceleration.x;
    5.50 -    y = acceleration.y;
    5.51 -    z = acceleration.z;
    5.52 -
    5.53 -    hasNewData = YES;
    5.54 -}
    5.55 -/*
    5.56 -    getLastOrientation -- put last obtained accelerometer data into Sint16 array
    5.57 -
    5.58 -    Called from the joystick system when it needs the accelerometer data.
    5.59 -    Function grabs the last data sent to the accelerometer and converts it
    5.60 -    from floating point to Sint16, which is what the joystick system expects.
    5.61 -
    5.62 -    To do the conversion, the data is first clamped onto the interval
    5.63 -    [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
    5.64 -    by MAX_SINT16 so that it is mapped to the full range of an Sint16.
    5.65 -
    5.66 -    You can customize the clamped range of this function by modifying the
    5.67 -    SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
    5.68 -
    5.69 -    Once converted to Sint16, the accelerometer data no longer has coherent units.
    5.70 -    You can convert the data back to units of g-force by multiplying it
    5.71 -    in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
    5.72 - */
    5.73 --(void)getLastOrientation:(Sint16 *)data {
    5.74 -
    5.75 -    #define MAX_SINT16 0x7FFF
    5.76 -
    5.77 -    /* clamp the data */
    5.78 -    if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE;
    5.79 -    else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE;
    5.80 -    if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE;
    5.81 -    else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE;
    5.82 -    if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE;
    5.83 -    else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE;
    5.84 -
    5.85 -    /* pass in data mapped to range of SInt16 */
    5.86 -    data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
    5.87 -    data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
    5.88 -    data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
    5.89 -
    5.90 -}
    5.91 -
    5.92 -/*
    5.93 -    Initialize SDLUIAccelerationDelegate.  Since we don't have any data yet,
    5.94 -    just set our last received data to zero, and indicate we don't have any;
    5.95 -*/
    5.96 --(id)init {
    5.97 -    self = [super init];
    5.98 -    x = y = z = 0.0;
    5.99 -    hasNewData = NO;
   5.100 -    return self;
   5.101 -}
   5.102 -
   5.103 --(void)dealloc {
   5.104 -    sharedDelegate = nil;
   5.105 -    [self shutdown];
   5.106 -    [super dealloc];
   5.107 -}
   5.108 -
   5.109 -/*
   5.110 -    Lets our delegate start receiving accelerometer updates.
   5.111 -*/
   5.112 --(void)startup {
   5.113 -    [UIAccelerometer sharedAccelerometer].delegate = self;
   5.114 -    isRunning = YES;
   5.115 -}
   5.116 -/*
   5.117 -    Stops our delegate from receiving accelerometer updates.
   5.118 -*/
   5.119 --(void)shutdown {
   5.120 -    if ([UIAccelerometer sharedAccelerometer].delegate == self) {
   5.121 -        [UIAccelerometer sharedAccelerometer].delegate = nil;
   5.122 -    }
   5.123 -    isRunning = NO;
   5.124 -}
   5.125 -/*
   5.126 -    Our we currently receiving accelerometer updates?
   5.127 -*/
   5.128 --(BOOL)isRunning {
   5.129 -    return isRunning;
   5.130 -}
   5.131 -/*
   5.132 -    Do we have any data that hasn't been pumped into SDL's event system?
   5.133 -*/
   5.134 --(BOOL)hasNewData {
   5.135 -    return hasNewData;
   5.136 -}
   5.137 -/*
   5.138 -    When the joystick system grabs the new data, it sets this to NO.
   5.139 -*/
   5.140 --(void)setHasNewData:(BOOL)value {
   5.141 -    hasNewData = value;
   5.142 -}
   5.143 -
   5.144 -@end
     6.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Jun 24 13:31:25 2014 -0700
     6.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Wed Jun 25 00:20:21 2014 -0700
     6.3 @@ -23,11 +23,18 @@
     6.4  /* This is the iOS implementation of the SDL joystick API */
     6.5  
     6.6  #include "SDL_joystick.h"
     6.7 +#include "SDL_stdinc.h"
     6.8  #include "../SDL_sysjoystick.h"
     6.9  #include "../SDL_joystick_c.h"
    6.10 -#import "SDLUIAccelerationDelegate.h"
    6.11  
    6.12 -const char *accelerometerName = "iPhone accelerometer";
    6.13 +#import <CoreMotion/CoreMotion.h>
    6.14 +
    6.15 +/* needed for SDL_IPHONE_MAX_GFORCE macro */
    6.16 +#import "SDL_config_iphoneos.h"
    6.17 +
    6.18 +const char *accelerometerName = "iOS accelerometer";
    6.19 +
    6.20 +static CMMotionManager *motionManager = nil;
    6.21  
    6.22  /* Function to scan the system for joysticks.
    6.23   * This function should set SDL_numjoysticks to the number of available
    6.24 @@ -74,7 +81,15 @@
    6.25      joystick->nhats = 0;
    6.26      joystick->nballs = 0;
    6.27      joystick->nbuttons = 0;
    6.28 -    [[SDLUIAccelerationDelegate sharedDelegate] startup];
    6.29 +
    6.30 +    if (motionManager == nil) {
    6.31 +        motionManager = [[CMMotionManager alloc] init];
    6.32 +    }
    6.33 +
    6.34 +    /* Shorter times between updates can significantly increase CPU usage. */
    6.35 +    motionManager.accelerometerUpdateInterval = 0.1;
    6.36 +    [motionManager startAccelerometerUpdates];
    6.37 +
    6.38      return 0;
    6.39  }
    6.40  
    6.41 @@ -84,6 +99,45 @@
    6.42      return SDL_TRUE;
    6.43  }
    6.44  
    6.45 +static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
    6.46 +{
    6.47 +    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
    6.48 +    const SInt16 maxsint16 = 0x7FFF;
    6.49 +    CMAcceleration accel;
    6.50 +
    6.51 +    if (!motionManager.accelerometerActive) {
    6.52 +        return;
    6.53 +    }
    6.54 +
    6.55 +    accel = [[motionManager accelerometerData] acceleration];
    6.56 +
    6.57 +    /*
    6.58 +     Convert accelerometer data from floating point to Sint16, which is what
    6.59 +     the joystick system expects.
    6.60 +
    6.61 +     To do the conversion, the data is first clamped onto the interval
    6.62 +     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
    6.63 +     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
    6.64 +
    6.65 +     You can customize the clamped range of this function by modifying the
    6.66 +     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
    6.67 +
    6.68 +     Once converted to Sint16, the accelerometer data no longer has coherent
    6.69 +     units. You can convert the data back to units of g-force by multiplying
    6.70 +     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
    6.71 +     */
    6.72 +
    6.73 +    /* clamp the data */
    6.74 +    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
    6.75 +    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
    6.76 +    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
    6.77 +
    6.78 +    /* pass in data mapped to range of SInt16 */
    6.79 +    SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
    6.80 +    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
    6.81 +    SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
    6.82 +}
    6.83 +
    6.84  /* Function to update the state of a joystick - called as a device poll.
    6.85   * This function shouldn't update the joystick structure directly,
    6.86   * but instead should call SDL_PrivateJoystick*() to deliver events
    6.87 @@ -92,37 +146,25 @@
    6.88  void
    6.89  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    6.90  {
    6.91 -
    6.92 -    Sint16 orientation[3];
    6.93 -
    6.94 -    if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
    6.95 -
    6.96 -        [[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
    6.97 -        [[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
    6.98 -
    6.99 -        SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
   6.100 -        SDL_PrivateJoystickAxis(joystick, 1, -orientation[1]);
   6.101 -        SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
   6.102 -
   6.103 -    }
   6.104 -
   6.105 -    return;
   6.106 +    SDL_SYS_AccelerometerUpdate(joystick);
   6.107  }
   6.108  
   6.109  /* Function to close a joystick after use */
   6.110  void
   6.111  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   6.112  {
   6.113 -    if ([[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
   6.114 -        [[SDLUIAccelerationDelegate sharedDelegate] shutdown];
   6.115 -    }
   6.116 -    SDL_SetError("No joystick open with that index");
   6.117 +    [motionManager stopAccelerometerUpdates];
   6.118 +    joystick->closed = 1;
   6.119  }
   6.120  
   6.121  /* Function to perform any system-specific joystick related cleanup */
   6.122  void
   6.123  SDL_SYS_JoystickQuit(void)
   6.124  {
   6.125 +    if (motionManager != nil) {
   6.126 +        [motionManager release];
   6.127 +        motionManager = nil;
   6.128 +    }
   6.129  }
   6.130  
   6.131  SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )