src/joystick/iphoneos/SDL_sysjoystick.m
changeset 8921 4a9feef61c85
parent 8860 c4133d635375
child 9311 a4b031e28de8
child 9489 6cd0275146b3
     1.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Jun 24 13:31:25 2014 -0700
     1.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Wed Jun 25 00:20:21 2014 -0700
     1.3 @@ -23,11 +23,18 @@
     1.4  /* This is the iOS implementation of the SDL joystick API */
     1.5  
     1.6  #include "SDL_joystick.h"
     1.7 +#include "SDL_stdinc.h"
     1.8  #include "../SDL_sysjoystick.h"
     1.9  #include "../SDL_joystick_c.h"
    1.10 -#import "SDLUIAccelerationDelegate.h"
    1.11  
    1.12 -const char *accelerometerName = "iPhone accelerometer";
    1.13 +#import <CoreMotion/CoreMotion.h>
    1.14 +
    1.15 +/* needed for SDL_IPHONE_MAX_GFORCE macro */
    1.16 +#import "SDL_config_iphoneos.h"
    1.17 +
    1.18 +const char *accelerometerName = "iOS accelerometer";
    1.19 +
    1.20 +static CMMotionManager *motionManager = nil;
    1.21  
    1.22  /* Function to scan the system for joysticks.
    1.23   * This function should set SDL_numjoysticks to the number of available
    1.24 @@ -74,7 +81,15 @@
    1.25      joystick->nhats = 0;
    1.26      joystick->nballs = 0;
    1.27      joystick->nbuttons = 0;
    1.28 -    [[SDLUIAccelerationDelegate sharedDelegate] startup];
    1.29 +
    1.30 +    if (motionManager == nil) {
    1.31 +        motionManager = [[CMMotionManager alloc] init];
    1.32 +    }
    1.33 +
    1.34 +    /* Shorter times between updates can significantly increase CPU usage. */
    1.35 +    motionManager.accelerometerUpdateInterval = 0.1;
    1.36 +    [motionManager startAccelerometerUpdates];
    1.37 +
    1.38      return 0;
    1.39  }
    1.40  
    1.41 @@ -84,6 +99,45 @@
    1.42      return SDL_TRUE;
    1.43  }
    1.44  
    1.45 +static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
    1.46 +{
    1.47 +    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
    1.48 +    const SInt16 maxsint16 = 0x7FFF;
    1.49 +    CMAcceleration accel;
    1.50 +
    1.51 +    if (!motionManager.accelerometerActive) {
    1.52 +        return;
    1.53 +    }
    1.54 +
    1.55 +    accel = [[motionManager accelerometerData] acceleration];
    1.56 +
    1.57 +    /*
    1.58 +     Convert accelerometer data from floating point to Sint16, which is what
    1.59 +     the joystick system expects.
    1.60 +
    1.61 +     To do the conversion, the data is first clamped onto the interval
    1.62 +     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
    1.63 +     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
    1.64 +
    1.65 +     You can customize the clamped range of this function by modifying the
    1.66 +     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
    1.67 +
    1.68 +     Once converted to Sint16, the accelerometer data no longer has coherent
    1.69 +     units. You can convert the data back to units of g-force by multiplying
    1.70 +     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
    1.71 +     */
    1.72 +
    1.73 +    /* clamp the data */
    1.74 +    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
    1.75 +    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
    1.76 +    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
    1.77 +
    1.78 +    /* pass in data mapped to range of SInt16 */
    1.79 +    SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
    1.80 +    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
    1.81 +    SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
    1.82 +}
    1.83 +
    1.84  /* Function to update the state of a joystick - called as a device poll.
    1.85   * This function shouldn't update the joystick structure directly,
    1.86   * but instead should call SDL_PrivateJoystick*() to deliver events
    1.87 @@ -92,37 +146,25 @@
    1.88  void
    1.89  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    1.90  {
    1.91 -
    1.92 -    Sint16 orientation[3];
    1.93 -
    1.94 -    if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
    1.95 -
    1.96 -        [[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
    1.97 -        [[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
    1.98 -
    1.99 -        SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
   1.100 -        SDL_PrivateJoystickAxis(joystick, 1, -orientation[1]);
   1.101 -        SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
   1.102 -
   1.103 -    }
   1.104 -
   1.105 -    return;
   1.106 +    SDL_SYS_AccelerometerUpdate(joystick);
   1.107  }
   1.108  
   1.109  /* Function to close a joystick after use */
   1.110  void
   1.111  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.112  {
   1.113 -    if ([[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
   1.114 -        [[SDLUIAccelerationDelegate sharedDelegate] shutdown];
   1.115 -    }
   1.116 -    SDL_SetError("No joystick open with that index");
   1.117 +    [motionManager stopAccelerometerUpdates];
   1.118 +    joystick->closed = 1;
   1.119  }
   1.120  
   1.121  /* Function to perform any system-specific joystick related cleanup */
   1.122  void
   1.123  SDL_SYS_JoystickQuit(void)
   1.124  {
   1.125 +    if (motionManager != nil) {
   1.126 +        [motionManager release];
   1.127 +        motionManager = nil;
   1.128 +    }
   1.129  }
   1.130  
   1.131  SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )