src/joystick/iphoneos/SDL_sysjoystick.m
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 11 Mar 2015 21:14:21 +0100
changeset 9380 07b7c1005a23
parent 9311 a4b031e28de8
child 9433 bd062398b648
permissions -rw-r--r--
Updated internal 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_stdinc.h"
    27 #include "../SDL_sysjoystick.h"
    28 #include "../SDL_joystick_c.h"
    29 
    30 #import <CoreMotion/CoreMotion.h>
    31 
    32 /* needed for SDL_IPHONE_MAX_GFORCE macro */
    33 #import "SDL_config_iphoneos.h"
    34 
    35 const char *accelerometerName = "iOS accelerometer";
    36 
    37 static CMMotionManager *motionManager = nil;
    38 
    39 /* Function to scan the system for joysticks.
    40  * Joystick 0 should be the system default joystick.
    41  * It should return 0, or -1 on an unrecoverable fatal error.
    42  */
    43 int
    44 SDL_SYS_JoystickInit(void)
    45 {
    46     return (1);
    47 }
    48 
    49 int SDL_SYS_NumJoysticks()
    50 {
    51     return 1;
    52 }
    53 
    54 void SDL_SYS_JoystickDetect()
    55 {
    56 }
    57 
    58 /* Function to get the device-dependent name of a joystick */
    59 const char *
    60 SDL_SYS_JoystickNameForDeviceIndex(int device_index)
    61 {
    62     return accelerometerName;
    63 }
    64 
    65 /* Function to perform the mapping from device index to the instance id for this index */
    66 SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
    67 {
    68     return device_index;
    69 }
    70 
    71 /* Function to open a joystick for use.
    72    The joystick to open is specified by the device index.
    73    This should fill the nbuttons and naxes fields of the joystick structure.
    74    It returns 0, or -1 if there is an error.
    75  */
    76 int
    77 SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
    78 {
    79     joystick->naxes = 3;
    80     joystick->nhats = 0;
    81     joystick->nballs = 0;
    82     joystick->nbuttons = 0;
    83 
    84     if (motionManager == nil) {
    85         motionManager = [[CMMotionManager alloc] init];
    86     }
    87 
    88     /* Shorter times between updates can significantly increase CPU usage. */
    89     motionManager.accelerometerUpdateInterval = 0.1;
    90     [motionManager startAccelerometerUpdates];
    91 
    92     return 0;
    93 }
    94 
    95 /* Function to determine is this joystick is attached to the system right now */
    96 SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
    97 {
    98     return SDL_TRUE;
    99 }
   100 
   101 static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
   102 {
   103     const float maxgforce = SDL_IPHONE_MAX_GFORCE;
   104     const SInt16 maxsint16 = 0x7FFF;
   105     CMAcceleration accel;
   106 
   107     if (!motionManager.accelerometerActive) {
   108         return;
   109     }
   110 
   111     accel = [[motionManager accelerometerData] acceleration];
   112 
   113     /*
   114      Convert accelerometer data from floating point to Sint16, which is what
   115      the joystick system expects.
   116 
   117      To do the conversion, the data is first clamped onto the interval
   118      [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
   119      by MAX_SINT16 so that it is mapped to the full range of an Sint16.
   120 
   121      You can customize the clamped range of this function by modifying the
   122      SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
   123 
   124      Once converted to Sint16, the accelerometer data no longer has coherent
   125      units. You can convert the data back to units of g-force by multiplying
   126      it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
   127      */
   128 
   129     /* clamp the data */
   130     accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
   131     accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
   132     accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
   133 
   134     /* pass in data mapped to range of SInt16 */
   135     SDL_PrivateJoystickAxis(joystick, 0, (accel.x / maxgforce) * maxsint16);
   136     SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
   137     SDL_PrivateJoystickAxis(joystick, 2, (accel.z / maxgforce) * maxsint16);
   138 }
   139 
   140 /* Function to update the state of a joystick - called as a device poll.
   141  * This function shouldn't update the joystick structure directly,
   142  * but instead should call SDL_PrivateJoystick*() to deliver events
   143  * and update joystick device state.
   144  */
   145 void
   146 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   147 {
   148     SDL_SYS_AccelerometerUpdate(joystick);
   149 }
   150 
   151 /* Function to close a joystick after use */
   152 void
   153 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   154 {
   155     [motionManager stopAccelerometerUpdates];
   156     joystick->closed = 1;
   157 }
   158 
   159 /* Function to perform any system-specific joystick related cleanup */
   160 void
   161 SDL_SYS_JoystickQuit(void)
   162 {
   163     if (motionManager != nil) {
   164         [motionManager release];
   165         motionManager = nil;
   166     }
   167 }
   168 
   169 SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   170 {
   171     SDL_JoystickGUID guid;
   172     /* the GUID is just the first 16 chars of the name for now */
   173     const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   174     SDL_zero( guid );
   175     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   176     return guid;
   177 }
   178 
   179 SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   180 {
   181     SDL_JoystickGUID guid;
   182     /* the GUID is just the first 16 chars of the name for now */
   183     const char *name = joystick->name;
   184     SDL_zero( guid );
   185     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   186     return guid;
   187 }
   188 
   189 /* vi: set ts=4 sw=4 expandtab: */