src/joystick/iphoneos/SDL_sysjoystick.m
author Alex Szpakowski <slime73@gmail.com>
Tue, 15 Jul 2014 02:01:43 -0300
branchiOS-improvements
changeset 9489 6cd0275146b3
parent 8921 4a9feef61c85
child 9490 9eab796ea0b8
permissions -rw-r--r--
iOS now respects SDL_HINT_ACCELEROMETER_AS_JOYSTICK.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 /* This is the iOS implementation of the SDL joystick API */
    24 
    25 #include "SDL_joystick.h"
    26 #include "SDL_hints.h"
    27 #include "SDL_stdinc.h"
    28 #include "../SDL_sysjoystick.h"
    29 #include "../SDL_joystick_c.h"
    30 
    31 #import <CoreMotion/CoreMotion.h>
    32 
    33 /* needed for SDL_IPHONE_MAX_GFORCE macro */
    34 #import "SDL_config_iphoneos.h"
    35 
    36 const char *accelerometerName = "iOS Accelerometer";
    37 
    38 static CMMotionManager *motionManager = nil;
    39 static int numjoysticks = 0;
    40 
    41 /* Function to scan the system for joysticks.
    42  * This function should set SDL_numjoysticks to the number of available
    43  * joysticks.  Joystick 0 should be the system default joystick.
    44  * It should return 0, or -1 on an unrecoverable fatal error.
    45  */
    46 int
    47 SDL_SYS_JoystickInit(void)
    48 {
    49     const char *hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
    50     if (!hint || SDL_atoi(hint)) {
    51         /* Default behavior, accelerometer as joystick */
    52         numjoysticks++;
    53     }
    54 
    55     return numjoysticks;
    56 }
    57 
    58 int SDL_SYS_NumJoysticks()
    59 {
    60     return numjoysticks;
    61 }
    62 
    63 void SDL_SYS_JoystickDetect()
    64 {
    65 }
    66 
    67 /* Function to get the device-dependent name of a joystick */
    68 const char *
    69 SDL_SYS_JoystickNameForDeviceIndex(int device_index)
    70 {
    71     return accelerometerName;
    72 }
    73 
    74 /* Function to perform the mapping from device index to the instance id for this index */
    75 SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
    76 {
    77     return device_index;
    78 }
    79 
    80 /* Function to open a joystick for use.
    81    The joystick to open is specified by the index field of the joystick.
    82    This should fill the nbuttons and naxes fields of the joystick structure.
    83    It returns 0, or -1 if there is an error.
    84  */
    85 int
    86 SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
    87 {
    88     joystick->naxes = 3;
    89     joystick->nhats = 0;
    90     joystick->nballs = 0;
    91     joystick->nbuttons = 0;
    92 
    93     if (motionManager == nil) {
    94         motionManager = [[CMMotionManager alloc] init];
    95     }
    96 
    97     /* Shorter times between updates can significantly increase CPU usage. */
    98     motionManager.accelerometerUpdateInterval = 0.1;
    99     [motionManager startAccelerometerUpdates];
   100 
   101     return 0;
   102 }
   103 
   104 /* Function to determine is this joystick is attached to the system right now */
   105 SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   106 {
   107     return SDL_TRUE;
   108 }
   109 
   110 static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
   111 {
   112     const float maxgforce = SDL_IPHONE_MAX_GFORCE;
   113     const SInt16 maxsint16 = 0x7FFF;
   114     CMAcceleration accel;
   115 
   116     if (!motionManager.accelerometerActive) {
   117         return;
   118     }
   119 
   120     accel = [[motionManager accelerometerData] acceleration];
   121 
   122     /*
   123      Convert accelerometer data from floating point to Sint16, which is what
   124      the joystick system expects.
   125 
   126      To do the conversion, the data is first clamped onto the interval
   127      [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
   128      by MAX_SINT16 so that it is mapped to the full range of an Sint16.
   129 
   130      You can customize the clamped range of this function by modifying the
   131      SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
   132 
   133      Once converted to Sint16, the accelerometer data no longer has coherent
   134      units. You can convert the data back to units of g-force by multiplying
   135      it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
   136      */
   137 
   138     /* clamp the data */
   139     accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
   140     accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
   141     accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
   142 
   143     /* pass in data mapped to range of SInt16 */
   144     SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
   145     SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
   146     SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
   147 }
   148 
   149 /* Function to update the state of a joystick - called as a device poll.
   150  * This function shouldn't update the joystick structure directly,
   151  * but instead should call SDL_PrivateJoystick*() to deliver events
   152  * and update joystick device state.
   153  */
   154 void
   155 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   156 {
   157     SDL_SYS_AccelerometerUpdate(joystick);
   158 }
   159 
   160 /* Function to close a joystick after use */
   161 void
   162 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   163 {
   164     [motionManager stopAccelerometerUpdates];
   165     joystick->closed = 1;
   166 }
   167 
   168 /* Function to perform any system-specific joystick related cleanup */
   169 void
   170 SDL_SYS_JoystickQuit(void)
   171 {
   172     if (motionManager != nil) {
   173         [motionManager release];
   174         motionManager = nil;
   175     }
   176 
   177     numjoysticks = 0;
   178 }
   179 
   180 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   181 {
   182     SDL_JoystickGUID guid;
   183     /* the GUID is just the first 16 chars of the name for now */
   184     const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   185     SDL_zero( guid );
   186     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   187     return guid;
   188 }
   189 
   190 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   191 {
   192     SDL_JoystickGUID guid;
   193     /* the GUID is just the first 16 chars of the name for now */
   194     const char *name = joystick->name;
   195     SDL_zero( guid );
   196     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   197     return guid;
   198 }
   199 
   200 /* vi: set ts=4 sw=4 expandtab: */