src/joystick/iphoneos/SDL_sysjoystick.m
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 15 Apr 2015 21:29:55 +0200
changeset 9561 d8ad01792399
parent 9536 3c87b8f9dc58
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed typo in internal joystick documentation comments.
     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  * Joystick 0 should be the system default joystick.
    43  * It should return 0, or -1 on an unrecoverable fatal error.
    44  */
    45 int
    46 SDL_SYS_JoystickInit(void)
    47 {
    48     const char *hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
    49     if (!hint || SDL_atoi(hint)) {
    50         /* Default behavior, accelerometer as joystick */
    51         numjoysticks = 1;
    52     }
    53 
    54     return numjoysticks;
    55 }
    56 
    57 int SDL_SYS_NumJoysticks()
    58 {
    59     return numjoysticks;
    60 }
    61 
    62 void SDL_SYS_JoystickDetect()
    63 {
    64 }
    65 
    66 /* Function to get the device-dependent name of a joystick */
    67 const char *
    68 SDL_SYS_JoystickNameForDeviceIndex(int device_index)
    69 {
    70     return accelerometerName;
    71 }
    72 
    73 /* Function to perform the mapping from device index to the instance id for this index */
    74 SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
    75 {
    76     return device_index;
    77 }
    78 
    79 /* Function to open a joystick for use.
    80    The joystick to open is specified by the device index.
    81    This should fill the nbuttons and naxes fields of the joystick structure.
    82    It returns 0, or -1 if there is an error.
    83  */
    84 int
    85 SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
    86 {
    87     joystick->naxes = 3;
    88     joystick->nhats = 0;
    89     joystick->nballs = 0;
    90     joystick->nbuttons = 0;
    91 
    92     @autoreleasepool {
    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 
   102     return 0;
   103 }
   104 
   105 /* Function to determine if this joystick is attached to the system right now */
   106 SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   107 {
   108     return SDL_TRUE;
   109 }
   110 
   111 static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
   112 {
   113     const float maxgforce = SDL_IPHONE_MAX_GFORCE;
   114     const SInt16 maxsint16 = 0x7FFF;
   115     CMAcceleration accel;
   116 
   117     @autoreleasepool {
   118         if (!motionManager.accelerometerActive) {
   119             return;
   120         }
   121 
   122         accel = motionManager.accelerometerData.acceleration;
   123     }
   124 
   125     /*
   126      Convert accelerometer data from floating point to Sint16, which is what
   127      the joystick system expects.
   128 
   129      To do the conversion, the data is first clamped onto the interval
   130      [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
   131      by MAX_SINT16 so that it is mapped to the full range of an Sint16.
   132 
   133      You can customize the clamped range of this function by modifying the
   134      SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
   135 
   136      Once converted to Sint16, the accelerometer data no longer has coherent
   137      units. You can convert the data back to units of g-force by multiplying
   138      it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
   139      */
   140 
   141     /* clamp the data */
   142     accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
   143     accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
   144     accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
   145 
   146     /* pass in data mapped to range of SInt16 */
   147     SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
   148     SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
   149     SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
   150 }
   151 
   152 /* Function to update the state of a joystick - called as a device poll.
   153  * This function shouldn't update the joystick structure directly,
   154  * but instead should call SDL_PrivateJoystick*() to deliver events
   155  * and update joystick device state.
   156  */
   157 void
   158 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   159 {
   160     SDL_SYS_AccelerometerUpdate(joystick);
   161 }
   162 
   163 /* Function to close a joystick after use */
   164 void
   165 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   166 {
   167     @autoreleasepool {
   168         [motionManager stopAccelerometerUpdates];
   169     }
   170 }
   171 
   172 /* Function to perform any system-specific joystick related cleanup */
   173 void
   174 SDL_SYS_JoystickQuit(void)
   175 {
   176     @autoreleasepool {
   177         motionManager = nil;
   178     }
   179 
   180     numjoysticks = 0;
   181 }
   182 
   183 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   184 {
   185     SDL_JoystickGUID guid;
   186     /* the GUID is just the first 16 chars of the name for now */
   187     const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   188     SDL_zero( guid );
   189     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   190     return guid;
   191 }
   192 
   193 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   194 {
   195     SDL_JoystickGUID guid;
   196     /* the GUID is just the first 16 chars of the name for now */
   197     const char *name = joystick->name;
   198     SDL_zero( guid );
   199     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   200     return guid;
   201 }
   202 
   203 /* vi: set ts=4 sw=4 expandtab: */