src/joystick/android/SDL_sysjoystick.c
author Gabriel Jacobo <gabomdq@gmail.com>
Sun, 10 Nov 2013 20:13:27 -0300
changeset 7944 f19c0aca56aa
parent 7930 5a1a7cdf3fa2
child 7956 965b7e09c5a8
permissions -rw-r--r--
Fixes bugs #2213 and #2214, improves the Android joystick code button handling
gabomdq@7041
     1
/*
gabomdq@7041
     2
  Simple DirectMedia Layer
gabomdq@7041
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
gabomdq@7041
     4
gabomdq@7041
     5
  This software is provided 'as-is', without any express or implied
gabomdq@7041
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@7041
     7
  arising from the use of this software.
gabomdq@7041
     8
gabomdq@7041
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@7041
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@7041
    11
  freely, subject to the following restrictions:
gabomdq@7041
    12
gabomdq@7041
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@7041
    14
     claim that you wrote the original software. If you use this software
gabomdq@7041
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@7041
    16
     appreciated but is not required.
gabomdq@7041
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@7041
    18
     misrepresented as being the original software.
gabomdq@7041
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@7041
    20
*/
gabomdq@7041
    21
gabomdq@7041
    22
#include "SDL_config.h"
gabomdq@7041
    23
gabomdq@7041
    24
#ifdef SDL_JOYSTICK_ANDROID
gabomdq@7041
    25
gabomdq@7041
    26
/* This is the system specific header for the SDL joystick API */
gabomdq@7041
    27
#include <stdio.h>              /* For the definition of NULL */
gabomdq@7041
    28
gabomdq@7041
    29
#include "SDL_error.h"
gabomdq@7041
    30
#include "SDL_events.h"
gabomdq@7041
    31
#include "SDL_joystick.h"
gabomdq@7907
    32
#include "SDL_hints.h"
gabomdq@7944
    33
#include "SDL_assert.h"
gabomdq@7041
    34
#include "../SDL_sysjoystick.h"
gabomdq@7041
    35
#include "../SDL_joystick_c.h"
gabomdq@7041
    36
#include "../../core/android/SDL_android.h"
gabomdq@7944
    37
#include "android/keycodes.h"
gabomdq@7944
    38
gabomdq@7944
    39
/* As of platform android-14, android/keycodes.h is missing these defines */
gabomdq@7944
    40
#ifndef AKEYCODE_BUTTON_1
gabomdq@7944
    41
#define AKEYCODE_BUTTON_1 188
gabomdq@7944
    42
#define AKEYCODE_BUTTON_2 189
gabomdq@7944
    43
#define AKEYCODE_BUTTON_3 190
gabomdq@7944
    44
#define AKEYCODE_BUTTON_4 191
gabomdq@7944
    45
#define AKEYCODE_BUTTON_5 192
gabomdq@7944
    46
#define AKEYCODE_BUTTON_6 193
gabomdq@7944
    47
#define AKEYCODE_BUTTON_7 194
gabomdq@7944
    48
#define AKEYCODE_BUTTON_8 195
gabomdq@7944
    49
#define AKEYCODE_BUTTON_9 196
gabomdq@7944
    50
#define AKEYCODE_BUTTON_10 197
gabomdq@7944
    51
#define AKEYCODE_BUTTON_11 198
gabomdq@7944
    52
#define AKEYCODE_BUTTON_12 199
gabomdq@7944
    53
#define AKEYCODE_BUTTON_13 200
gabomdq@7944
    54
#define AKEYCODE_BUTTON_14 201
gabomdq@7944
    55
#define AKEYCODE_BUTTON_15 202
gabomdq@7944
    56
#define AKEYCODE_BUTTON_16 203
gabomdq@7944
    57
#endif
gabomdq@7041
    58
gabomdq@7907
    59
#define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
gabomdq@7907
    60
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
gabomdq@7944
    61
#define ANDROID_MAX_NBUTTONS 36
gabomdq@7907
    62
gabomdq@7907
    63
static SDL_Joystick **SYS_Joysticks;
gabomdq@7907
    64
static char **SYS_JoystickNames;
gabomdq@7907
    65
static int SYS_numjoysticks;
gabomdq@7907
    66
static SDL_bool SYS_accelAsJoy;
gabomdq@7907
    67
gabomdq@7907
    68
/* Function to convert Android keyCodes into SDL ones.
gabomdq@7907
    69
 * This code manipulation is done to get a sequential list of codes.
gabomdq@7944
    70
 * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
gabomdq@7907
    71
 */
philipp@7929
    72
static int
gabomdq@7907
    73
keycode_to_SDL(int keycode)
gabomdq@7907
    74
{
gabomdq@7944
    75
    /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
gabomdq@7944
    76
    int button = 0;
gabomdq@7944
    77
    switch(keycode) 
gabomdq@7944
    78
    {
gabomdq@7944
    79
        /* D-Pad key codes (API 1), these get mapped to 0...4 */
gabomdq@7944
    80
        case AKEYCODE_DPAD_UP:
gabomdq@7944
    81
        case AKEYCODE_DPAD_DOWN:
gabomdq@7944
    82
        case AKEYCODE_DPAD_LEFT:
gabomdq@7944
    83
        case AKEYCODE_DPAD_RIGHT:
gabomdq@7944
    84
        case AKEYCODE_DPAD_CENTER:
gabomdq@7944
    85
            button = keycode - AKEYCODE_DPAD_UP;
gabomdq@7944
    86
            break;
gabomdq@7944
    87
        
gabomdq@7944
    88
        /* Some gamepad buttons (API 9), these get mapped to 5...19*/
gabomdq@7944
    89
        case AKEYCODE_BUTTON_A:
gabomdq@7944
    90
        case AKEYCODE_BUTTON_B:
gabomdq@7944
    91
        case AKEYCODE_BUTTON_C:
gabomdq@7944
    92
        case AKEYCODE_BUTTON_X:
gabomdq@7944
    93
        case AKEYCODE_BUTTON_Y:
gabomdq@7944
    94
        case AKEYCODE_BUTTON_Z:
gabomdq@7944
    95
        case AKEYCODE_BUTTON_L1:
gabomdq@7944
    96
        case AKEYCODE_BUTTON_L2:
gabomdq@7944
    97
        case AKEYCODE_BUTTON_R1:
gabomdq@7944
    98
        case AKEYCODE_BUTTON_R2:
gabomdq@7944
    99
        case AKEYCODE_BUTTON_THUMBL:
gabomdq@7944
   100
        case AKEYCODE_BUTTON_THUMBR:
gabomdq@7944
   101
        case AKEYCODE_BUTTON_START:
gabomdq@7944
   102
        case AKEYCODE_BUTTON_SELECT:
gabomdq@7944
   103
        case AKEYCODE_BUTTON_MODE:
gabomdq@7944
   104
            button = keycode - AKEYCODE_BUTTON_A + 5;
gabomdq@7944
   105
            break;
gabomdq@7944
   106
            
gabomdq@7944
   107
        
gabomdq@7944
   108
        /* More gamepad buttons (API 12), these get mapped to 20...35*/
gabomdq@7944
   109
        case AKEYCODE_BUTTON_1:
gabomdq@7944
   110
        case AKEYCODE_BUTTON_2:
gabomdq@7944
   111
        case AKEYCODE_BUTTON_3:
gabomdq@7944
   112
        case AKEYCODE_BUTTON_4:
gabomdq@7944
   113
        case AKEYCODE_BUTTON_5:
gabomdq@7944
   114
        case AKEYCODE_BUTTON_6:
gabomdq@7944
   115
        case AKEYCODE_BUTTON_7:
gabomdq@7944
   116
        case AKEYCODE_BUTTON_8:
gabomdq@7944
   117
        case AKEYCODE_BUTTON_9:
gabomdq@7944
   118
        case AKEYCODE_BUTTON_10:
gabomdq@7944
   119
        case AKEYCODE_BUTTON_11:
gabomdq@7944
   120
        case AKEYCODE_BUTTON_12:
gabomdq@7944
   121
        case AKEYCODE_BUTTON_13:
gabomdq@7944
   122
        case AKEYCODE_BUTTON_14:
gabomdq@7944
   123
        case AKEYCODE_BUTTON_15:
gabomdq@7944
   124
        case AKEYCODE_BUTTON_16:
gabomdq@7944
   125
            button = keycode - AKEYCODE_BUTTON_1 + 20;
gabomdq@7944
   126
            break;
gabomdq@7944
   127
            
gabomdq@7944
   128
        default:
gabomdq@7944
   129
            SDL_Log("The button you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> Android KeyCode %d", keycode);
gabomdq@7944
   130
            return -1;
gabomdq@7944
   131
            break;
gabomdq@7944
   132
    }
gabomdq@7944
   133
    
gabomdq@7944
   134
    /* This is here in case future generations, probably with six fingers per hand, 
gabomdq@7944
   135
     * happily add new cases up above and forget to update the max number of buttons. 
gabomdq@7907
   136
     */
gabomdq@7944
   137
    SDL_assert(button < ANDROID_MAX_NBUTTONS);
gabomdq@7944
   138
    return button;
gabomdq@7944
   139
    
gabomdq@7907
   140
}
gabomdq@7041
   141
gabomdq@7041
   142
/* Function to scan the system for joysticks.
gabomdq@7041
   143
 * This function should set SDL_numjoysticks to the number of available
gabomdq@7041
   144
 * joysticks.  Joystick 0 should be the system default joystick.
gabomdq@7041
   145
 * It should return 0, or -1 on an unrecoverable fatal error.
gabomdq@7041
   146
 */
gabomdq@7041
   147
int
gabomdq@7041
   148
SDL_SYS_JoystickInit(void)
gabomdq@7041
   149
{
gabomdq@7907
   150
    int i = 0;
gabomdq@7907
   151
    const char *env;
gabomdq@7907
   152
    
gabomdq@7907
   153
    env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
gabomdq@7907
   154
    if (env && !SDL_atoi(env))
gabomdq@7907
   155
        SYS_accelAsJoy = SDL_FALSE;
gabomdq@7907
   156
    else
gabomdq@7907
   157
        SYS_accelAsJoy = SDL_TRUE; /* Default behavior */
gabomdq@7907
   158
    
gabomdq@7907
   159
    SYS_numjoysticks = Android_JNI_GetNumJoysticks();
gabomdq@7907
   160
    if (SYS_accelAsJoy) {
gabomdq@7907
   161
        SYS_numjoysticks++;
gabomdq@7907
   162
    }
gabomdq@7907
   163
    SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *));
gabomdq@7907
   164
    if (SYS_Joysticks == NULL)
gabomdq@7907
   165
    {
gabomdq@7907
   166
        return SDL_OutOfMemory();
gabomdq@7907
   167
    }
gabomdq@7907
   168
    SYS_JoystickNames = (char **)SDL_malloc(SYS_numjoysticks*sizeof(char *));
gabomdq@7907
   169
    if (SYS_JoystickNames == NULL)
gabomdq@7907
   170
    {
gabomdq@7907
   171
        SDL_free(SYS_Joysticks);
gabomdq@7907
   172
        SYS_Joysticks = NULL;
gabomdq@7907
   173
        return SDL_OutOfMemory();
gabomdq@7907
   174
    }
gabomdq@7907
   175
    SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *)));
gabomdq@7907
   176
    SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *)));
gabomdq@7907
   177
    
gabomdq@7907
   178
    for (i = 0; i < SYS_numjoysticks; i++)
gabomdq@7907
   179
    {
gabomdq@7907
   180
        if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
gabomdq@7907
   181
            SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
gabomdq@7907
   182
        } else {
gabomdq@7907
   183
            SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
gabomdq@7907
   184
        }
gabomdq@7907
   185
    }
gabomdq@7907
   186
   
gabomdq@7907
   187
    return (SYS_numjoysticks);
gabomdq@7041
   188
}
gabomdq@7041
   189
gabomdq@7041
   190
int SDL_SYS_NumJoysticks()
gabomdq@7041
   191
{
gabomdq@7907
   192
    return SYS_numjoysticks;
gabomdq@7041
   193
}
gabomdq@7041
   194
gabomdq@7041
   195
void SDL_SYS_JoystickDetect()
gabomdq@7041
   196
{
gabomdq@7041
   197
}
gabomdq@7041
   198
gabomdq@7907
   199
/* TODO: Hotplugging support */
gabomdq@7041
   200
SDL_bool SDL_SYS_JoystickNeedsPolling()
gabomdq@7041
   201
{
gabomdq@7041
   202
    return SDL_FALSE;
gabomdq@7041
   203
}
gabomdq@7041
   204
gabomdq@7041
   205
/* Function to get the device-dependent name of a joystick */
gabomdq@7041
   206
const char *
gabomdq@7041
   207
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
gabomdq@7041
   208
{
gabomdq@7907
   209
    return SYS_JoystickNames[device_index];
gabomdq@7041
   210
}
gabomdq@7041
   211
gabomdq@7041
   212
/* Function to perform the mapping from device index to the instance id for this index */
gabomdq@7041
   213
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
gabomdq@7041
   214
{
gabomdq@7041
   215
    return device_index;
gabomdq@7041
   216
}
gabomdq@7041
   217
gabomdq@7041
   218
/* Function to open a joystick for use.
gabomdq@7041
   219
   The joystick to open is specified by the index field of the joystick.
gabomdq@7041
   220
   This should fill the nbuttons and naxes fields of the joystick structure.
gabomdq@7041
   221
   It returns 0, or -1 if there is an error.
gabomdq@7041
   222
 */
gabomdq@7041
   223
int
gabomdq@7041
   224
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
gabomdq@7041
   225
{
gabomdq@7907
   226
    if (device_index < SYS_numjoysticks) {
gabomdq@7041
   227
        joystick->nhats = 0;
gabomdq@7041
   228
        joystick->nballs = 0;
gabomdq@7907
   229
        if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
gabomdq@7907
   230
            joystick->nbuttons = 0;
gabomdq@7907
   231
            joystick->naxes = 3;
gabomdq@7907
   232
        } else {
gabomdq@7944
   233
            /* FIXME: Get the real number of buttons in the device? */
gabomdq@7944
   234
            joystick->nbuttons = ANDROID_MAX_NBUTTONS;
gabomdq@7907
   235
            joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
gabomdq@7907
   236
        }
gabomdq@7907
   237
        
gabomdq@7907
   238
        SYS_Joysticks[device_index] = joystick;
gabomdq@7041
   239
        return 0;
slouken@7191
   240
    } else {
philipp@7930
   241
        return SDL_SetError("No joystick available with that index");
slouken@7191
   242
    }
gabomdq@7041
   243
}
gabomdq@7041
   244
gabomdq@7041
   245
/* Function to determine is this joystick is attached to the system right now */
gabomdq@7041
   246
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
gabomdq@7041
   247
{
gabomdq@7041
   248
    return SDL_TRUE;
gabomdq@7041
   249
}
gabomdq@7041
   250
gabomdq@7041
   251
/* Function to update the state of a joystick - called as a device poll.
gabomdq@7041
   252
 * This function shouldn't update the joystick structure directly,
gabomdq@7041
   253
 * but instead should call SDL_PrivateJoystick*() to deliver events
gabomdq@7041
   254
 * and update joystick device state.
gabomdq@7041
   255
 */
gabomdq@7041
   256
void
gabomdq@7041
   257
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
gabomdq@7041
   258
{
gabomdq@7041
   259
    int i;
gabomdq@7041
   260
    Sint16 value;
gabomdq@7041
   261
    float values[3];
gabomdq@7041
   262
gabomdq@7907
   263
    if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
gabomdq@7907
   264
        joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
gabomdq@7041
   265
        for ( i = 0; i < 3; i++ ) {
gabomdq@7041
   266
            value = (Sint16)(values[i] * 32767.0f);
gabomdq@7041
   267
            SDL_PrivateJoystickAxis(joystick, i, value);
gabomdq@7041
   268
        }
gabomdq@7041
   269
    }
gabomdq@7041
   270
}
gabomdq@7041
   271
gabomdq@7041
   272
/* Function to close a joystick after use */
gabomdq@7041
   273
void
gabomdq@7041
   274
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
gabomdq@7041
   275
{
gabomdq@7041
   276
}
gabomdq@7041
   277
gabomdq@7041
   278
/* Function to perform any system-specific joystick related cleanup */
gabomdq@7041
   279
void
gabomdq@7041
   280
SDL_SYS_JoystickQuit(void)
gabomdq@7041
   281
{
gabomdq@7907
   282
    SDL_free(SYS_JoystickNames);
gabomdq@7907
   283
    SDL_free(SYS_Joysticks);
gabomdq@7907
   284
    SYS_JoystickNames = NULL;
gabomdq@7907
   285
    SYS_Joysticks = NULL;
gabomdq@7041
   286
}
gabomdq@7041
   287
gabomdq@7041
   288
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
gabomdq@7041
   289
{
gabomdq@7041
   290
    SDL_JoystickGUID guid;
slouken@7191
   291
    /* the GUID is just the first 16 chars of the name for now */
gabomdq@7041
   292
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
gabomdq@7041
   293
    SDL_zero( guid );
gabomdq@7041
   294
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
gabomdq@7041
   295
    return guid;
gabomdq@7041
   296
}
gabomdq@7041
   297
gabomdq@7041
   298
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
gabomdq@7041
   299
{
gabomdq@7041
   300
    SDL_JoystickGUID guid;
slouken@7191
   301
    /* the GUID is just the first 16 chars of the name for now */
gabomdq@7041
   302
    const char *name = joystick->name;
gabomdq@7041
   303
    SDL_zero( guid );
gabomdq@7041
   304
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
gabomdq@7041
   305
    return guid;
gabomdq@7041
   306
}
gabomdq@7041
   307
gabomdq@7907
   308
int
gabomdq@7907
   309
Android_OnPadDown(int padId, int keycode)
gabomdq@7907
   310
{
gabomdq@7907
   311
    SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_PRESSED);
gabomdq@7907
   312
    
gabomdq@7907
   313
    return 0;
gabomdq@7907
   314
}
gabomdq@7907
   315
gabomdq@7907
   316
int
gabomdq@7907
   317
Android_OnPadUp(int padId, int keycode)
gabomdq@7907
   318
{
gabomdq@7907
   319
    SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_RELEASED);
gabomdq@7907
   320
    
gabomdq@7907
   321
    return 0;
gabomdq@7907
   322
}
gabomdq@7907
   323
gabomdq@7907
   324
int
gabomdq@7907
   325
Android_OnJoy(int joyId, int axis, float value)
gabomdq@7907
   326
{
gabomdq@7907
   327
    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
gabomdq@7907
   328
    /* TODO: Are the reported values right? */
gabomdq@7907
   329
    SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
gabomdq@7907
   330
    
gabomdq@7907
   331
    return 0;
gabomdq@7907
   332
}
gabomdq@7907
   333
gabomdq@7041
   334
#endif /* SDL_JOYSTICK_ANDROID */
gabomdq@7041
   335
gabomdq@7041
   336
/* vi: set ts=4 sw=4 expandtab: */