src/joystick/android/SDL_sysjoystick.c
author David Ludwig <dludwig@pobox.com>
Tue, 24 Dec 2013 21:28:31 -0500
changeset 8552 abd934eac415
parent 8543 b9dd3cf38585
child 8583 fb2933ca805f
permissions -rw-r--r--
WinRT: moved ill-performing XInput device-detection calls to a separate thread
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"
philipp@8008
    37
gabomdq@7944
    38
#include "android/keycodes.h"
gabomdq@7944
    39
gabomdq@7944
    40
/* As of platform android-14, android/keycodes.h is missing these defines */
gabomdq@7944
    41
#ifndef AKEYCODE_BUTTON_1
gabomdq@7944
    42
#define AKEYCODE_BUTTON_1 188
gabomdq@7944
    43
#define AKEYCODE_BUTTON_2 189
gabomdq@7944
    44
#define AKEYCODE_BUTTON_3 190
gabomdq@7944
    45
#define AKEYCODE_BUTTON_4 191
gabomdq@7944
    46
#define AKEYCODE_BUTTON_5 192
gabomdq@7944
    47
#define AKEYCODE_BUTTON_6 193
gabomdq@7944
    48
#define AKEYCODE_BUTTON_7 194
gabomdq@7944
    49
#define AKEYCODE_BUTTON_8 195
gabomdq@7944
    50
#define AKEYCODE_BUTTON_9 196
gabomdq@7944
    51
#define AKEYCODE_BUTTON_10 197
gabomdq@7944
    52
#define AKEYCODE_BUTTON_11 198
gabomdq@7944
    53
#define AKEYCODE_BUTTON_12 199
gabomdq@7944
    54
#define AKEYCODE_BUTTON_13 200
gabomdq@7944
    55
#define AKEYCODE_BUTTON_14 201
gabomdq@7944
    56
#define AKEYCODE_BUTTON_15 202
gabomdq@7944
    57
#define AKEYCODE_BUTTON_16 203
gabomdq@7944
    58
#endif
gabomdq@7041
    59
gabomdq@7907
    60
#define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
gabomdq@7907
    61
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
gabomdq@7944
    62
#define ANDROID_MAX_NBUTTONS 36
gabomdq@7907
    63
gabomdq@7907
    64
static SDL_Joystick **SYS_Joysticks;
gabomdq@7907
    65
static char **SYS_JoystickNames;
gabomdq@7907
    66
static int SYS_numjoysticks;
gabomdq@7907
    67
static SDL_bool SYS_accelAsJoy;
gabomdq@7907
    68
gabomdq@7907
    69
/* Function to convert Android keyCodes into SDL ones.
gabomdq@7907
    70
 * This code manipulation is done to get a sequential list of codes.
gabomdq@7944
    71
 * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
gabomdq@7907
    72
 */
philipp@7929
    73
static int
gabomdq@7907
    74
keycode_to_SDL(int keycode)
gabomdq@7907
    75
{
gabomdq@7944
    76
    /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
gabomdq@7944
    77
    int button = 0;
gabomdq@7944
    78
    switch(keycode) 
gabomdq@7944
    79
    {
gabomdq@7944
    80
        /* D-Pad key codes (API 1), these get mapped to 0...4 */
gabomdq@7944
    81
        case AKEYCODE_DPAD_UP:
gabomdq@7944
    82
        case AKEYCODE_DPAD_DOWN:
gabomdq@7944
    83
        case AKEYCODE_DPAD_LEFT:
gabomdq@7944
    84
        case AKEYCODE_DPAD_RIGHT:
gabomdq@7944
    85
        case AKEYCODE_DPAD_CENTER:
gabomdq@7944
    86
            button = keycode - AKEYCODE_DPAD_UP;
gabomdq@7944
    87
            break;
gabomdq@7944
    88
        
gabomdq@7944
    89
        /* Some gamepad buttons (API 9), these get mapped to 5...19*/
gabomdq@7944
    90
        case AKEYCODE_BUTTON_A:
gabomdq@7944
    91
        case AKEYCODE_BUTTON_B:
gabomdq@7944
    92
        case AKEYCODE_BUTTON_C:
gabomdq@7944
    93
        case AKEYCODE_BUTTON_X:
gabomdq@7944
    94
        case AKEYCODE_BUTTON_Y:
gabomdq@7944
    95
        case AKEYCODE_BUTTON_Z:
gabomdq@7944
    96
        case AKEYCODE_BUTTON_L1:
gabomdq@7944
    97
        case AKEYCODE_BUTTON_L2:
gabomdq@7944
    98
        case AKEYCODE_BUTTON_R1:
gabomdq@7944
    99
        case AKEYCODE_BUTTON_R2:
gabomdq@7944
   100
        case AKEYCODE_BUTTON_THUMBL:
gabomdq@7944
   101
        case AKEYCODE_BUTTON_THUMBR:
gabomdq@7944
   102
        case AKEYCODE_BUTTON_START:
gabomdq@7944
   103
        case AKEYCODE_BUTTON_SELECT:
gabomdq@7944
   104
        case AKEYCODE_BUTTON_MODE:
gabomdq@7944
   105
            button = keycode - AKEYCODE_BUTTON_A + 5;
gabomdq@7944
   106
            break;
gabomdq@7944
   107
            
gabomdq@7944
   108
        
gabomdq@7944
   109
        /* More gamepad buttons (API 12), these get mapped to 20...35*/
gabomdq@7944
   110
        case AKEYCODE_BUTTON_1:
gabomdq@7944
   111
        case AKEYCODE_BUTTON_2:
gabomdq@7944
   112
        case AKEYCODE_BUTTON_3:
gabomdq@7944
   113
        case AKEYCODE_BUTTON_4:
gabomdq@7944
   114
        case AKEYCODE_BUTTON_5:
gabomdq@7944
   115
        case AKEYCODE_BUTTON_6:
gabomdq@7944
   116
        case AKEYCODE_BUTTON_7:
gabomdq@7944
   117
        case AKEYCODE_BUTTON_8:
gabomdq@7944
   118
        case AKEYCODE_BUTTON_9:
gabomdq@7944
   119
        case AKEYCODE_BUTTON_10:
gabomdq@7944
   120
        case AKEYCODE_BUTTON_11:
gabomdq@7944
   121
        case AKEYCODE_BUTTON_12:
gabomdq@7944
   122
        case AKEYCODE_BUTTON_13:
gabomdq@7944
   123
        case AKEYCODE_BUTTON_14:
gabomdq@7944
   124
        case AKEYCODE_BUTTON_15:
gabomdq@7944
   125
        case AKEYCODE_BUTTON_16:
gabomdq@7944
   126
            button = keycode - AKEYCODE_BUTTON_1 + 20;
gabomdq@7944
   127
            break;
gabomdq@7944
   128
            
gabomdq@7944
   129
        default:
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@8024
   163
    SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, 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@8024
   168
    SYS_JoystickNames = (char **)SDL_calloc(1, 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
    
gabomdq@7907
   176
    for (i = 0; i < SYS_numjoysticks; i++)
gabomdq@7907
   177
    {
gabomdq@7907
   178
        if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
gabomdq@7907
   179
            SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
gabomdq@7907
   180
        } else {
gabomdq@7907
   181
            SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
gabomdq@7907
   182
        }
gabomdq@7907
   183
    }
gabomdq@7907
   184
   
gabomdq@7907
   185
    return (SYS_numjoysticks);
gabomdq@7041
   186
}
gabomdq@7041
   187
gabomdq@7041
   188
int SDL_SYS_NumJoysticks()
gabomdq@7041
   189
{
gabomdq@7907
   190
    return SYS_numjoysticks;
gabomdq@7041
   191
}
gabomdq@7041
   192
gabomdq@7041
   193
void SDL_SYS_JoystickDetect()
gabomdq@7041
   194
{
gabomdq@7041
   195
}
gabomdq@7041
   196
gabomdq@7907
   197
/* TODO: Hotplugging support */
gabomdq@7041
   198
SDL_bool SDL_SYS_JoystickNeedsPolling()
gabomdq@7041
   199
{
gabomdq@7041
   200
    return SDL_FALSE;
gabomdq@7041
   201
}
gabomdq@7041
   202
gabomdq@7041
   203
/* Function to get the device-dependent name of a joystick */
gabomdq@7041
   204
const char *
gabomdq@7041
   205
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
gabomdq@7041
   206
{
gabomdq@7907
   207
    return SYS_JoystickNames[device_index];
gabomdq@7041
   208
}
gabomdq@7041
   209
gabomdq@7041
   210
/* Function to perform the mapping from device index to the instance id for this index */
gabomdq@7041
   211
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
gabomdq@7041
   212
{
gabomdq@7041
   213
    return device_index;
gabomdq@7041
   214
}
gabomdq@7041
   215
gabomdq@7041
   216
/* Function to open a joystick for use.
gabomdq@7041
   217
   The joystick to open is specified by the index field of the joystick.
gabomdq@7041
   218
   This should fill the nbuttons and naxes fields of the joystick structure.
gabomdq@7041
   219
   It returns 0, or -1 if there is an error.
gabomdq@7041
   220
 */
gabomdq@7041
   221
int
gabomdq@7041
   222
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
gabomdq@7041
   223
{
gabomdq@7907
   224
    if (device_index < SYS_numjoysticks) {
gabomdq@7041
   225
        joystick->nhats = 0;
gabomdq@7041
   226
        joystick->nballs = 0;
gabomdq@7907
   227
        if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
gabomdq@7907
   228
            joystick->nbuttons = 0;
gabomdq@7907
   229
            joystick->naxes = 3;
gabomdq@7907
   230
        } else {
gabomdq@7944
   231
            /* FIXME: Get the real number of buttons in the device? */
gabomdq@7944
   232
            joystick->nbuttons = ANDROID_MAX_NBUTTONS;
gabomdq@7907
   233
            joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
gabomdq@7907
   234
        }
gabomdq@7907
   235
        
gabomdq@7907
   236
        SYS_Joysticks[device_index] = joystick;
gabomdq@7041
   237
        return 0;
slouken@7191
   238
    } else {
philipp@7930
   239
        return SDL_SetError("No joystick available with that index");
slouken@7191
   240
    }
gabomdq@7041
   241
}
gabomdq@7041
   242
gabomdq@7041
   243
/* Function to determine is this joystick is attached to the system right now */
gabomdq@7041
   244
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
gabomdq@7041
   245
{
gabomdq@7041
   246
    return SDL_TRUE;
gabomdq@7041
   247
}
gabomdq@7041
   248
gabomdq@7041
   249
/* Function to update the state of a joystick - called as a device poll.
gabomdq@7041
   250
 * This function shouldn't update the joystick structure directly,
gabomdq@7041
   251
 * but instead should call SDL_PrivateJoystick*() to deliver events
gabomdq@7041
   252
 * and update joystick device state.
gabomdq@7041
   253
 */
gabomdq@7041
   254
void
gabomdq@7041
   255
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
gabomdq@7041
   256
{
gabomdq@7041
   257
    int i;
gabomdq@7041
   258
    Sint16 value;
gabomdq@7041
   259
    float values[3];
gabomdq@7041
   260
gabomdq@7907
   261
    if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
gabomdq@7907
   262
        joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
gabomdq@7041
   263
        for ( i = 0; i < 3; i++ ) {
gabomdq@7041
   264
            value = (Sint16)(values[i] * 32767.0f);
gabomdq@7041
   265
            SDL_PrivateJoystickAxis(joystick, i, value);
gabomdq@7041
   266
        }
gabomdq@7041
   267
    }
gabomdq@7041
   268
}
gabomdq@7041
   269
gabomdq@7041
   270
/* Function to close a joystick after use */
gabomdq@7041
   271
void
gabomdq@7041
   272
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
gabomdq@7041
   273
{
gabomdq@8024
   274
    int device_index;
gabomdq@8024
   275
    
gabomdq@8024
   276
    for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
gabomdq@8024
   277
        if ( SYS_Joysticks[device_index] == joystick ) {
gabomdq@8024
   278
            SYS_Joysticks[device_index] = NULL;
gabomdq@8024
   279
        }
gabomdq@8024
   280
    }
gabomdq@8024
   281
    
gabomdq@8024
   282
    joystick->closed = 1;
gabomdq@7041
   283
}
gabomdq@7041
   284
gabomdq@7041
   285
/* Function to perform any system-specific joystick related cleanup */
gabomdq@7041
   286
void
gabomdq@7041
   287
SDL_SYS_JoystickQuit(void)
gabomdq@7041
   288
{
gabomdq@7907
   289
    SDL_free(SYS_JoystickNames);
gabomdq@7907
   290
    SDL_free(SYS_Joysticks);
gabomdq@7907
   291
    SYS_JoystickNames = NULL;
gabomdq@7907
   292
    SYS_Joysticks = NULL;
gabomdq@7041
   293
}
gabomdq@7041
   294
gabomdq@7041
   295
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
gabomdq@7041
   296
{
gabomdq@7041
   297
    SDL_JoystickGUID guid;
slouken@7191
   298
    /* the GUID is just the first 16 chars of the name for now */
gabomdq@7041
   299
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
gabomdq@7041
   300
    SDL_zero( guid );
gabomdq@7041
   301
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
gabomdq@7041
   302
    return guid;
gabomdq@7041
   303
}
gabomdq@7041
   304
gabomdq@7041
   305
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
gabomdq@7041
   306
{
gabomdq@7041
   307
    SDL_JoystickGUID guid;
slouken@7191
   308
    /* the GUID is just the first 16 chars of the name for now */
gabomdq@7041
   309
    const char *name = joystick->name;
gabomdq@7041
   310
    SDL_zero( guid );
gabomdq@7041
   311
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
gabomdq@7041
   312
    return guid;
gabomdq@7041
   313
}
gabomdq@7041
   314
gabomdq@7907
   315
int
gabomdq@7907
   316
Android_OnPadDown(int padId, int keycode)
gabomdq@7907
   317
{
gabomdq@7956
   318
    int button = keycode_to_SDL(keycode);
gabomdq@7956
   319
    if (button >= 0) {
gabomdq@8024
   320
        if (SYS_Joysticks[padId]) {
gabomdq@8024
   321
            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
gabomdq@8024
   322
        }
gabomdq@8013
   323
        return 0;
gabomdq@7956
   324
    }
gabomdq@7907
   325
    
gabomdq@8013
   326
    return -1;
gabomdq@7907
   327
}
gabomdq@7907
   328
gabomdq@7907
   329
int
gabomdq@7907
   330
Android_OnPadUp(int padId, int keycode)
gabomdq@7907
   331
{
gabomdq@7956
   332
    int button = keycode_to_SDL(keycode);
gabomdq@7956
   333
    if (button >= 0) {
gabomdq@8024
   334
        if (SYS_Joysticks[padId]) {
gabomdq@8024
   335
            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
gabomdq@8024
   336
        }
gabomdq@8013
   337
        return 0;
gabomdq@7956
   338
    }
gabomdq@7907
   339
    
gabomdq@8013
   340
    return -1;
gabomdq@7907
   341
}
gabomdq@7907
   342
gabomdq@7907
   343
int
gabomdq@7907
   344
Android_OnJoy(int joyId, int axis, float value)
gabomdq@7907
   345
{
gabomdq@7907
   346
    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
gabomdq@7907
   347
    /* TODO: Are the reported values right? */
gabomdq@8024
   348
    if (SYS_Joysticks[joyId]) {
gabomdq@8024
   349
        SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
gabomdq@8024
   350
    }
gabomdq@7907
   351
    
gabomdq@7907
   352
    return 0;
gabomdq@7907
   353
}
gabomdq@7907
   354
gabomdq@7041
   355
#endif /* SDL_JOYSTICK_ANDROID */
gabomdq@7041
   356
gabomdq@7041
   357
/* vi: set ts=4 sw=4 expandtab: */