Added missing files for the Android build
authorSam Lantinga <slouken@libsdl.org>
Tue, 19 Mar 2013 23:07:49 -0700
changeset 7019a713101e1d25
parent 7018 9cef1005df5f
child 7020 a500a9dbfb41
Added missing files for the Android build
src/joystick/android/SDL_androidjoystick.c
src/joystick/android/SDL_androidjoystick.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/android/SDL_androidjoystick.c	Tue Mar 19 23:07:49 2013 -0700
     1.3 @@ -0,0 +1,244 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#ifdef SDL_JOYSTICK_ANDROID
    1.28 +
    1.29 +/* This is the system specific header for the SDL joystick API */
    1.30 +#include <stdio.h>              /* For the definition of NULL */
    1.31 +
    1.32 +#include "SDL_error.h"
    1.33 +#include "SDL_events.h"
    1.34 +#include "SDL_joystick.h"
    1.35 +#include "../SDL_sysjoystick.h"
    1.36 +#include "../SDL_joystick_c.h"
    1.37 +#include "../../core/android/SDL_android.h"
    1.38 +
    1.39 +//HACK!!
    1.40 +static SDL_Joystick **SYS_Joysticks;
    1.41 +static char **SYS_JoystickNames;
    1.42 +static int SYS_numjoysticks;
    1.43 +
    1.44 +/* Function to convert Android keyCodes into SDL ones.
    1.45 + * This code manipulation is done to get a sequential list of codes.
    1.46 + */
    1.47 +int
    1.48 +keycode_to_SDL(int keycode)
    1.49 +{
    1.50 +    /* D-Pad key codes (API 1):
    1.51 +     * KEYCODE_DPAD_UP=19, KEYCODE_DPAD_DOWN 
    1.52 +     * KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_CENTER
    1.53 +     */
    1.54 +    if(keycode < 96)
    1.55 +        return keycode-19;
    1.56 +    /* Some gamepad buttons (API 9):
    1.57 +     * KEYCODE_BUTTON_A=96, KEYCODE_BUTTON_B, KEYCODE_BUTTON_C,
    1.58 +     * KEYCODE_BUTTON_X, KEYCODE_BUTTON_Y, KEYCODE_BUTTON_Z,
    1.59 +     * KEYCODE_BUTTON_L1, KEYCODE_BUTTON_L2,
    1.60 +     * KEYCODE_BUTTON_R1, KEYCODE_BUTTON_R2,
    1.61 +     * KEYCODE_BUTTON_THUMBL, KEYCODE_BUTTON_THUMBR,
    1.62 +     * KEYCODE_BUTTON_START, KEYCODE_BUTTON_SELECT, KEYCODE_BUTTON_MODE
    1.63 +     */
    1.64 +    else if(keycode < 188)
    1.65 +        return keycode-91;
    1.66 +    /* More gamepad buttons (API 12):
    1.67 +     * KEYCODE_BUTTON_1=188 to KEYCODE_BUTTON_16
    1.68 +     */
    1.69 +    else
    1.70 +        return keycode-168;
    1.71 +}
    1.72 +
    1.73 +/* Function to scan the system for joysticks.
    1.74 + * This function should set SYS_numjoysticks to the number of available
    1.75 + * joysticks.  Joystick 0 should be the system default joystick.
    1.76 + * It should return 0, or -1 on an unrecoverable fatal error.
    1.77 + */
    1.78 +int
    1.79 +SDL_SYS_JoystickInit(void)
    1.80 +{
    1.81 +    int i = 0;
    1.82 +    if (Android_JNI_JoystickInit() < 0)
    1.83 +        return (-1);
    1.84 +    SYS_numjoysticks = Android_JNI_GetNumJoysticks();
    1.85 +    SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *));
    1.86 +    if (SYS_Joysticks == NULL)
    1.87 +    {
    1.88 +        SDL_OutOfMemory();
    1.89 +        return (-1);
    1.90 +    }
    1.91 +    SYS_JoystickNames = (char **)SDL_malloc(SYS_numjoysticks*sizeof(char *));
    1.92 +    if (SYS_JoystickNames == NULL)
    1.93 +    {
    1.94 +        SDL_free(SYS_Joysticks);
    1.95 +        SYS_Joysticks = NULL;
    1.96 +        SDL_OutOfMemory();
    1.97 +        return (-1);
    1.98 +    }
    1.99 +    SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *)));
   1.100 +    SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *)));
   1.101 +
   1.102 +    for (i = 0; i < SYS_numjoysticks; i++)
   1.103 +    {
   1.104 +        SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
   1.105 +    }
   1.106 +
   1.107 +    return (SYS_numjoysticks);
   1.108 +}
   1.109 +
   1.110 +int SDL_SYS_NumJoysticks()
   1.111 +{
   1.112 +    return SYS_numjoysticks;
   1.113 +}
   1.114 +
   1.115 +void SDL_SYS_JoystickDetect()
   1.116 +{
   1.117 +}
   1.118 +
   1.119 +SDL_bool SDL_SYS_JoystickNeedsPolling()
   1.120 +{
   1.121 +    return SDL_FALSE;
   1.122 +}
   1.123 +
   1.124 +/* Function to get the device-dependent name of a joystick */
   1.125 +const char *
   1.126 +SDL_SYS_JoystickNameForDeviceIndex(int device_index)
   1.127 +{
   1.128 +    return SYS_JoystickNames[device_index];
   1.129 +}
   1.130 +
   1.131 +/* Function to perform the mapping from device index to the instance id for this index */
   1.132 +SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
   1.133 +{
   1.134 +    return device_index;
   1.135 +}
   1.136 +
   1.137 +/* Function to open a joystick for use.
   1.138 +   The joystick to open is specified by the index field of the joystick.
   1.139 +   This should fill the nbuttons and naxes fields of the joystick structure.
   1.140 +   It returns 0, or -1 if there is an error.
   1.141 + */
   1.142 +int
   1.143 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
   1.144 +{
   1.145 +    if( device_index < SYS_numjoysticks )
   1.146 +    {
   1.147 +        // TODO: How to get the rest of the info??
   1.148 +        // 36 is the maximum number of handled buttons
   1.149 +        joystick->nbuttons = 36;
   1.150 +        joystick->nhats = 0;
   1.151 +        joystick->nballs = 0;
   1.152 +        joystick->naxes = Android_JNI_GetJoystickNumOfAxes(device_index);
   1.153 +    }
   1.154 +    else
   1.155 +    {
   1.156 +        return -1;
   1.157 +    }
   1.158 +
   1.159 +    // Extremely hacky
   1.160 +    SYS_Joysticks[device_index] = joystick;
   1.161 +
   1.162 +    return 0;
   1.163 +}
   1.164 +
   1.165 +
   1.166 +/* Function to update the state of a joystick - called as a device poll.
   1.167 + * This function shouldn't update the joystick structure directly,
   1.168 + * but instead should call SDL_PrivateJoystick*() to deliver events
   1.169 + * and update joystick device state.
   1.170 + */
   1.171 +void
   1.172 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   1.173 +{
   1.174 +}
   1.175 +
   1.176 +/* Function to determine is this joystick is attached to the system right now */
   1.177 +SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   1.178 +{
   1.179 +    return SDL_TRUE;
   1.180 +}
   1.181 +
   1.182 +/* Function to close a joystick after use */
   1.183 +void
   1.184 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.185 +{
   1.186 +}
   1.187 +
   1.188 +/* Function to perform any system-specific joystick related cleanup */
   1.189 +void
   1.190 +SDL_SYS_JoystickQuit(void)
   1.191 +{
   1.192 +    SDL_free(SYS_JoystickNames);
   1.193 +    SDL_free(SYS_Joysticks);
   1.194 +    SYS_JoystickNames = NULL;
   1.195 +    SYS_Joysticks = NULL;
   1.196 +    Android_JNI_JoystickQuit();
   1.197 +}
   1.198 +
   1.199 +SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   1.200 +{
   1.201 +    SDL_JoystickGUID guid;
   1.202 +    // the GUID is just the first 16 chars of the name for now
   1.203 +    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   1.204 +    SDL_zero( guid );
   1.205 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.206 +    return guid;
   1.207 +}
   1.208 +
   1.209 +SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   1.210 +{
   1.211 +    SDL_JoystickGUID guid;
   1.212 +    // the GUID is just the first 16 chars of the name for now
   1.213 +    const char *name = joystick->name;
   1.214 +    SDL_zero( guid );
   1.215 +    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.216 +    return guid;
   1.217 +}
   1.218 +
   1.219 +int
   1.220 +Android_OnPadDown(int padId, int keycode)
   1.221 +{
   1.222 +    SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_PRESSED);
   1.223 +
   1.224 +    return 0;
   1.225 +}
   1.226 +
   1.227 +int
   1.228 +Android_OnPadUp(int padId, int keycode)
   1.229 +{
   1.230 +    SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_RELEASED);
   1.231 +
   1.232 +    return 0;
   1.233 +}
   1.234 +
   1.235 +int
   1.236 +Android_OnJoy(int joyId, int axisnum, float value)
   1.237 +{
   1.238 +    // Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0]
   1.239 +    // TODO: Are the reported values right?
   1.240 +    SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axisnum, (Sint16) (32767.*value) );
   1.241 +
   1.242 +    return 0;
   1.243 +}
   1.244 +
   1.245 +#endif /* SDL_JOYSTICK_ANDROID */
   1.246 +
   1.247 +/* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/joystick/android/SDL_androidjoystick.h	Tue Mar 19 23:07:49 2013 -0700
     2.3 @@ -0,0 +1,27 @@
     2.4 +/*
     2.5 +  Simple DirectMedia Layer
     2.6 +  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     2.7 +
     2.8 +  This software is provided 'as-is', without any express or implied
     2.9 +  warranty.  In no event will the authors be held liable for any damages
    2.10 +  arising from the use of this software.
    2.11 +
    2.12 +  Permission is granted to anyone to use this software for any purpose,
    2.13 +  including commercial applications, and to alter it and redistribute it
    2.14 +  freely, subject to the following restrictions:
    2.15 +
    2.16 +  1. The origin of this software must not be misrepresented; you must not
    2.17 +     claim that you wrote the original software. If you use this software
    2.18 +     in a product, an acknowledgment in the product documentation would be
    2.19 +     appreciated but is not required.
    2.20 +  2. Altered source versions must be plainly marked as such, and must not be
    2.21 +     misrepresented as being the original software.
    2.22 +  3. This notice may not be removed or altered from any source distribution.
    2.23 +*/
    2.24 +#include "SDL_config.h"
    2.25 +
    2.26 +extern int Android_OnPadDown(int padId, int keycode);
    2.27 +extern int Android_OnPadUp(int padId, int keycode);
    2.28 +extern int Android_OnJoy(int joyId, int axisnum, float value);
    2.29 +
    2.30 +/* vi: set ts=4 sw=4 expandtab: */