src/joystick/android/SDL_sysjoystick.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 10 Dec 2013 16:24:11 -0300
changeset 8057 801d84e26f91
parent 8025 a867f1891d2a
child 8060 d1948d163145
permissions -rw-r--r--
[Android] Hotplugging support for joysticks
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
#include <stdio.h>              /* For the definition of NULL */
gabomdq@7041
    27
#include "SDL_error.h"
gabomdq@7041
    28
#include "SDL_events.h"
gabomdq@8057
    29
gabomdq@8057
    30
#if !SDL_EVENTS_DISABLED
gabomdq@8057
    31
#include "../../events/SDL_events_c.h"
gabomdq@8057
    32
#endif
gabomdq@8057
    33
gabomdq@7041
    34
#include "SDL_joystick.h"
gabomdq@7907
    35
#include "SDL_hints.h"
gabomdq@7944
    36
#include "SDL_assert.h"
gabomdq@8057
    37
#include "SDL_sysjoystick_c.h"
gabomdq@7041
    38
#include "../SDL_joystick_c.h"
gabomdq@7041
    39
#include "../../core/android/SDL_android.h"
philipp@8008
    40
gabomdq@7944
    41
#include "android/keycodes.h"
gabomdq@7944
    42
gabomdq@7944
    43
/* As of platform android-14, android/keycodes.h is missing these defines */
gabomdq@7944
    44
#ifndef AKEYCODE_BUTTON_1
gabomdq@7944
    45
#define AKEYCODE_BUTTON_1 188
gabomdq@7944
    46
#define AKEYCODE_BUTTON_2 189
gabomdq@7944
    47
#define AKEYCODE_BUTTON_3 190
gabomdq@7944
    48
#define AKEYCODE_BUTTON_4 191
gabomdq@7944
    49
#define AKEYCODE_BUTTON_5 192
gabomdq@7944
    50
#define AKEYCODE_BUTTON_6 193
gabomdq@7944
    51
#define AKEYCODE_BUTTON_7 194
gabomdq@7944
    52
#define AKEYCODE_BUTTON_8 195
gabomdq@7944
    53
#define AKEYCODE_BUTTON_9 196
gabomdq@7944
    54
#define AKEYCODE_BUTTON_10 197
gabomdq@7944
    55
#define AKEYCODE_BUTTON_11 198
gabomdq@7944
    56
#define AKEYCODE_BUTTON_12 199
gabomdq@7944
    57
#define AKEYCODE_BUTTON_13 200
gabomdq@7944
    58
#define AKEYCODE_BUTTON_14 201
gabomdq@7944
    59
#define AKEYCODE_BUTTON_15 202
gabomdq@7944
    60
#define AKEYCODE_BUTTON_16 203
gabomdq@7944
    61
#endif
gabomdq@7041
    62
gabomdq@7907
    63
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
gabomdq@8057
    64
#define ANDROID_ACCELEROMETER_DEVICE_ID INT_MIN
gabomdq@7944
    65
#define ANDROID_MAX_NBUTTONS 36
gabomdq@7907
    66
gabomdq@8057
    67
static SDL_joylist_item * JoystickByDeviceId(int device_id);
gabomdq@8057
    68
gabomdq@8057
    69
static SDL_joylist_item *SDL_joylist = NULL;
gabomdq@8057
    70
static SDL_joylist_item *SDL_joylist_tail = NULL;
gabomdq@8057
    71
static int numjoysticks = 0;
gabomdq@8057
    72
static int instance_counter = 0;
gabomdq@8057
    73
gabomdq@7907
    74
gabomdq@7907
    75
/* Function to convert Android keyCodes into SDL ones.
gabomdq@7907
    76
 * This code manipulation is done to get a sequential list of codes.
gabomdq@7944
    77
 * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
gabomdq@7907
    78
 */
philipp@7929
    79
static int
gabomdq@7907
    80
keycode_to_SDL(int keycode)
gabomdq@7907
    81
{
gabomdq@7944
    82
    /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
gabomdq@7944
    83
    int button = 0;
gabomdq@7944
    84
    switch(keycode) 
gabomdq@7944
    85
    {
gabomdq@7944
    86
        /* D-Pad key codes (API 1), these get mapped to 0...4 */
gabomdq@7944
    87
        case AKEYCODE_DPAD_UP:
gabomdq@7944
    88
        case AKEYCODE_DPAD_DOWN:
gabomdq@7944
    89
        case AKEYCODE_DPAD_LEFT:
gabomdq@7944
    90
        case AKEYCODE_DPAD_RIGHT:
gabomdq@7944
    91
        case AKEYCODE_DPAD_CENTER:
gabomdq@7944
    92
            button = keycode - AKEYCODE_DPAD_UP;
gabomdq@7944
    93
            break;
gabomdq@7944
    94
        
gabomdq@7944
    95
        /* Some gamepad buttons (API 9), these get mapped to 5...19*/
gabomdq@7944
    96
        case AKEYCODE_BUTTON_A:
gabomdq@7944
    97
        case AKEYCODE_BUTTON_B:
gabomdq@7944
    98
        case AKEYCODE_BUTTON_C:
gabomdq@7944
    99
        case AKEYCODE_BUTTON_X:
gabomdq@7944
   100
        case AKEYCODE_BUTTON_Y:
gabomdq@7944
   101
        case AKEYCODE_BUTTON_Z:
gabomdq@7944
   102
        case AKEYCODE_BUTTON_L1:
gabomdq@7944
   103
        case AKEYCODE_BUTTON_L2:
gabomdq@7944
   104
        case AKEYCODE_BUTTON_R1:
gabomdq@7944
   105
        case AKEYCODE_BUTTON_R2:
gabomdq@7944
   106
        case AKEYCODE_BUTTON_THUMBL:
gabomdq@7944
   107
        case AKEYCODE_BUTTON_THUMBR:
gabomdq@7944
   108
        case AKEYCODE_BUTTON_START:
gabomdq@7944
   109
        case AKEYCODE_BUTTON_SELECT:
gabomdq@7944
   110
        case AKEYCODE_BUTTON_MODE:
gabomdq@7944
   111
            button = keycode - AKEYCODE_BUTTON_A + 5;
gabomdq@7944
   112
            break;
gabomdq@7944
   113
            
gabomdq@7944
   114
        
gabomdq@7944
   115
        /* More gamepad buttons (API 12), these get mapped to 20...35*/
gabomdq@7944
   116
        case AKEYCODE_BUTTON_1:
gabomdq@7944
   117
        case AKEYCODE_BUTTON_2:
gabomdq@7944
   118
        case AKEYCODE_BUTTON_3:
gabomdq@7944
   119
        case AKEYCODE_BUTTON_4:
gabomdq@7944
   120
        case AKEYCODE_BUTTON_5:
gabomdq@7944
   121
        case AKEYCODE_BUTTON_6:
gabomdq@7944
   122
        case AKEYCODE_BUTTON_7:
gabomdq@7944
   123
        case AKEYCODE_BUTTON_8:
gabomdq@7944
   124
        case AKEYCODE_BUTTON_9:
gabomdq@7944
   125
        case AKEYCODE_BUTTON_10:
gabomdq@7944
   126
        case AKEYCODE_BUTTON_11:
gabomdq@7944
   127
        case AKEYCODE_BUTTON_12:
gabomdq@7944
   128
        case AKEYCODE_BUTTON_13:
gabomdq@7944
   129
        case AKEYCODE_BUTTON_14:
gabomdq@7944
   130
        case AKEYCODE_BUTTON_15:
gabomdq@7944
   131
        case AKEYCODE_BUTTON_16:
gabomdq@7944
   132
            button = keycode - AKEYCODE_BUTTON_1 + 20;
gabomdq@7944
   133
            break;
gabomdq@7944
   134
            
gabomdq@7944
   135
        default:
gabomdq@7944
   136
            return -1;
gabomdq@7944
   137
            break;
gabomdq@7944
   138
    }
gabomdq@7944
   139
    
gabomdq@7944
   140
    /* This is here in case future generations, probably with six fingers per hand, 
gabomdq@7944
   141
     * happily add new cases up above and forget to update the max number of buttons. 
gabomdq@7907
   142
     */
gabomdq@7944
   143
    SDL_assert(button < ANDROID_MAX_NBUTTONS);
gabomdq@7944
   144
    return button;
gabomdq@7944
   145
    
gabomdq@7907
   146
}
gabomdq@7041
   147
gabomdq@8057
   148
int
gabomdq@8057
   149
Android_OnPadDown(int device_id, int keycode)
gabomdq@8057
   150
{
gabomdq@8057
   151
    SDL_joylist_item *item;
gabomdq@8057
   152
    int button = keycode_to_SDL(keycode);
gabomdq@8057
   153
    if (button >= 0) {
gabomdq@8057
   154
        item = JoystickByDeviceId(device_id);
gabomdq@8057
   155
        if (item && item->joystick) {
gabomdq@8057
   156
            SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
gabomdq@8057
   157
        }
gabomdq@8057
   158
        return 0;
gabomdq@8057
   159
    }
gabomdq@8057
   160
    
gabomdq@8057
   161
    return -1;
gabomdq@8057
   162
}
gabomdq@8057
   163
gabomdq@8057
   164
int
gabomdq@8057
   165
Android_OnPadUp(int device_id, int keycode)
gabomdq@8057
   166
{
gabomdq@8057
   167
    SDL_joylist_item *item;
gabomdq@8057
   168
    int button = keycode_to_SDL(keycode);
gabomdq@8057
   169
    if (button >= 0) {
gabomdq@8057
   170
        item = JoystickByDeviceId(device_id);
gabomdq@8057
   171
        if (item && item->joystick) {
gabomdq@8057
   172
            SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
gabomdq@8057
   173
        }
gabomdq@8057
   174
        return 0;
gabomdq@8057
   175
    }
gabomdq@8057
   176
    
gabomdq@8057
   177
    return -1;
gabomdq@8057
   178
}
gabomdq@8057
   179
gabomdq@8057
   180
int
gabomdq@8057
   181
Android_OnJoy(int device_id, int axis, float value)
gabomdq@8057
   182
{
gabomdq@8057
   183
    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
gabomdq@8057
   184
    SDL_joylist_item *item = JoystickByDeviceId(device_id);
gabomdq@8057
   185
    if (item && item->joystick) {
gabomdq@8057
   186
        SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
gabomdq@8057
   187
    }
gabomdq@8057
   188
    
gabomdq@8057
   189
    return 0;
gabomdq@8057
   190
}
gabomdq@8057
   191
gabomdq@8057
   192
gabomdq@8057
   193
int
gabomdq@8057
   194
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
gabomdq@8057
   195
{
gabomdq@8057
   196
    SDL_JoystickGUID guid;
gabomdq@8057
   197
    SDL_joylist_item *item;
gabomdq@8057
   198
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   199
    SDL_Event event;
gabomdq@8057
   200
#endif
gabomdq@8057
   201
    
gabomdq@8057
   202
    if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
gabomdq@8057
   203
        return -1;
gabomdq@8057
   204
    }
gabomdq@8057
   205
    
gabomdq@8057
   206
    /* the GUID is just the first 16 chars of the name for now */
gabomdq@8057
   207
    SDL_zero( guid );
gabomdq@8057
   208
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
gabomdq@8057
   209
gabomdq@8057
   210
    item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
gabomdq@8057
   211
    if (item == NULL) {
gabomdq@8057
   212
        return -1;
gabomdq@8057
   213
    }
gabomdq@8057
   214
gabomdq@8057
   215
    SDL_zerop(item);
gabomdq@8057
   216
    item->guid = guid;
gabomdq@8057
   217
    item->device_id = device_id;
gabomdq@8057
   218
    item->name = SDL_strdup(name);
gabomdq@8057
   219
    if ( item->name == NULL ) {
gabomdq@8057
   220
         SDL_free(item);
gabomdq@8057
   221
         return -1;
gabomdq@8057
   222
    }
gabomdq@8057
   223
    
gabomdq@8057
   224
    item->is_accelerometer = is_accelerometer;
gabomdq@8057
   225
    if (nbuttons > -1) {
gabomdq@8057
   226
        item->nbuttons = nbuttons;
gabomdq@8057
   227
    }
gabomdq@8057
   228
    else {
gabomdq@8057
   229
        item->nbuttons = ANDROID_MAX_NBUTTONS;
gabomdq@8057
   230
    }
gabomdq@8057
   231
    item->naxes = naxes;
gabomdq@8057
   232
    item->nhats = nhats;
gabomdq@8057
   233
    item->nballs = nballs;
gabomdq@8057
   234
    item->device_instance = instance_counter++;
gabomdq@8057
   235
    if (SDL_joylist_tail == NULL) {
gabomdq@8057
   236
        SDL_joylist = SDL_joylist_tail = item;
gabomdq@8057
   237
    } else {
gabomdq@8057
   238
        SDL_joylist_tail->next = item;
gabomdq@8057
   239
        SDL_joylist_tail = item;
gabomdq@8057
   240
    }
gabomdq@8057
   241
gabomdq@8057
   242
    /* Need to increment the joystick count before we post the event */
gabomdq@8057
   243
    ++numjoysticks;
gabomdq@8057
   244
gabomdq@8057
   245
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   246
    event.type = SDL_JOYDEVICEADDED;
gabomdq@8057
   247
gabomdq@8057
   248
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
gabomdq@8057
   249
        event.jdevice.which = (numjoysticks - 1);
gabomdq@8057
   250
        if ( (SDL_EventOK == NULL) ||
gabomdq@8057
   251
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
gabomdq@8057
   252
            SDL_PushEvent(&event);
gabomdq@8057
   253
        }
gabomdq@8057
   254
    }
gabomdq@8057
   255
#endif /* !SDL_EVENTS_DISABLED */
gabomdq@8057
   256
gabomdq@8057
   257
    SDL_Log("Added joystick %s with device_id %d", name, device_id);
gabomdq@8057
   258
gabomdq@8057
   259
    return numjoysticks;
gabomdq@8057
   260
}
gabomdq@8057
   261
gabomdq@8057
   262
int 
gabomdq@8057
   263
Android_RemoveJoystick(int device_id)
gabomdq@8057
   264
{
gabomdq@8057
   265
    SDL_joylist_item *item = SDL_joylist;
gabomdq@8057
   266
    SDL_joylist_item *prev = NULL;
gabomdq@8057
   267
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   268
    SDL_Event event;
gabomdq@8057
   269
#endif
gabomdq@8057
   270
    
gabomdq@8057
   271
    /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
gabomdq@8057
   272
    while (item != NULL) {
gabomdq@8057
   273
        if (item->device_id == device_id) {
gabomdq@8057
   274
            break;
gabomdq@8057
   275
        }
gabomdq@8057
   276
        prev = item;
gabomdq@8057
   277
        item = item->next;
gabomdq@8057
   278
    }
gabomdq@8057
   279
    
gabomdq@8057
   280
    if (item == NULL) {
gabomdq@8057
   281
        return -1;
gabomdq@8057
   282
    }
gabomdq@8057
   283
gabomdq@8057
   284
    const int retval = item->device_instance;
gabomdq@8057
   285
    if (prev != NULL) {
gabomdq@8057
   286
        prev->next = item->next;
gabomdq@8057
   287
    } else {
gabomdq@8057
   288
        SDL_assert(SDL_joylist == item);
gabomdq@8057
   289
        SDL_joylist = item->next;
gabomdq@8057
   290
    }
gabomdq@8057
   291
    if (item == SDL_joylist_tail) {
gabomdq@8057
   292
        SDL_joylist_tail = prev;
gabomdq@8057
   293
    }
gabomdq@8057
   294
gabomdq@8057
   295
    /* Need to decrement the joystick count before we post the event */
gabomdq@8057
   296
    --numjoysticks;
gabomdq@8057
   297
gabomdq@8057
   298
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   299
    event.type = SDL_JOYDEVICEREMOVED;
gabomdq@8057
   300
gabomdq@8057
   301
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
gabomdq@8057
   302
        event.jdevice.which = item->device_instance;
gabomdq@8057
   303
        if ( (SDL_EventOK == NULL) ||
gabomdq@8057
   304
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
gabomdq@8057
   305
            SDL_PushEvent(&event);
gabomdq@8057
   306
        }
gabomdq@8057
   307
    }
gabomdq@8057
   308
#endif /* !SDL_EVENTS_DISABLED */
gabomdq@8057
   309
gabomdq@8057
   310
    SDL_Log("Removed joystick with device_id %d", device_id);
gabomdq@8057
   311
    
gabomdq@8057
   312
    SDL_free(item->name);
gabomdq@8057
   313
    SDL_free(item);
gabomdq@8057
   314
    return retval;
gabomdq@8057
   315
}
gabomdq@8057
   316
gabomdq@8057
   317
gabomdq@7041
   318
int
gabomdq@7041
   319
SDL_SYS_JoystickInit(void)
gabomdq@7041
   320
{
gabomdq@7907
   321
    const char *env;
gabomdq@8057
   322
    SDL_SYS_JoystickDetect();
gabomdq@7907
   323
    
gabomdq@7907
   324
    env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
gabomdq@8057
   325
    if (!env || SDL_atoi(env)) {
gabomdq@8057
   326
        /* Default behavior, accelerometer as joystick */
gabomdq@8057
   327
        Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
gabomdq@7907
   328
    }
gabomdq@7907
   329
   
gabomdq@8057
   330
    return (numjoysticks);
gabomdq@8057
   331
gabomdq@7041
   332
}
gabomdq@7041
   333
gabomdq@7041
   334
int SDL_SYS_NumJoysticks()
gabomdq@7041
   335
{
gabomdq@8057
   336
    return numjoysticks;
gabomdq@7041
   337
}
gabomdq@7041
   338
gabomdq@7041
   339
void SDL_SYS_JoystickDetect()
gabomdq@7041
   340
{
gabomdq@8057
   341
    /* Support for device connect/disconnect is API >= 16 only,
gabomdq@8057
   342
     * so we have to poll ever few seconds.
gabomdq@8057
   343
     * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
gabomdq@8057
   344
     */
gabomdq@8057
   345
    Android_JNI_PollInputDevices();   
gabomdq@8057
   346
}
gabomdq@8057
   347
gabomdq@8057
   348
SDL_bool SDL_SYS_JoystickNeedsPolling()
gabomdq@8057
   349
{
gabomdq@8057
   350
    return SDL_TRUE;
gabomdq@7041
   351
}
gabomdq@7041
   352
gabomdq@8057
   353
static SDL_joylist_item *
gabomdq@8057
   354
JoystickByDevIndex(int device_index)
gabomdq@8057
   355
{
gabomdq@8057
   356
    SDL_joylist_item *item = SDL_joylist;
gabomdq@8057
   357
gabomdq@8057
   358
    if ((device_index < 0) || (device_index >= numjoysticks)) {
gabomdq@8057
   359
        return NULL;
gabomdq@8057
   360
    }
gabomdq@8057
   361
gabomdq@8057
   362
    while (device_index > 0) {
gabomdq@8057
   363
        SDL_assert(item != NULL);
gabomdq@8057
   364
        device_index--;
gabomdq@8057
   365
        item = item->next;
gabomdq@8057
   366
    }
gabomdq@8057
   367
gabomdq@8057
   368
    return item;
gabomdq@8057
   369
}
gabomdq@8057
   370
gabomdq@8057
   371
static SDL_joylist_item *
gabomdq@8057
   372
JoystickByDeviceId(int device_id)
gabomdq@7041
   373
{
gabomdq@8057
   374
    SDL_joylist_item *item = SDL_joylist;
gabomdq@8057
   375
gabomdq@8057
   376
    while (item != NULL) {
gabomdq@8057
   377
        if (item->device_id == device_id) {
gabomdq@8057
   378
            return item;
gabomdq@8057
   379
        }
gabomdq@8057
   380
        item = item->next;
gabomdq@8057
   381
    }
gabomdq@8057
   382
    
gabomdq@8057
   383
    /* Joystick not found, try adding it */
gabomdq@8057
   384
    SDL_SYS_JoystickDetect();
gabomdq@8057
   385
    
gabomdq@8057
   386
    while (item != NULL) {
gabomdq@8057
   387
        if (item->device_id == device_id) {
gabomdq@8057
   388
            return item;
gabomdq@8057
   389
        }
gabomdq@8057
   390
        item = item->next;
gabomdq@8057
   391
    }
gabomdq@8057
   392
gabomdq@8057
   393
    return NULL;
gabomdq@7041
   394
}
gabomdq@7041
   395
gabomdq@7041
   396
/* Function to get the device-dependent name of a joystick */
gabomdq@7041
   397
const char *
gabomdq@7041
   398
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
gabomdq@7041
   399
{
gabomdq@8057
   400
    return JoystickByDevIndex(device_index)->name;
gabomdq@7041
   401
}
gabomdq@7041
   402
gabomdq@7041
   403
/* Function to perform the mapping from device index to the instance id for this index */
gabomdq@7041
   404
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
gabomdq@7041
   405
{
gabomdq@8057
   406
    return JoystickByDevIndex(device_index)->device_instance;
gabomdq@7041
   407
}
gabomdq@7041
   408
gabomdq@7041
   409
/* Function to open a joystick for use.
gabomdq@7041
   410
   The joystick to open is specified by the index field of the joystick.
gabomdq@7041
   411
   This should fill the nbuttons and naxes fields of the joystick structure.
gabomdq@7041
   412
   It returns 0, or -1 if there is an error.
gabomdq@7041
   413
 */
gabomdq@7041
   414
int
gabomdq@7041
   415
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
gabomdq@7041
   416
{
gabomdq@8057
   417
    SDL_joylist_item *item = JoystickByDevIndex(device_index);
gabomdq@8057
   418
    char *fname = NULL;
gabomdq@8057
   419
gabomdq@8057
   420
    if (item == NULL ) {
gabomdq@8057
   421
        return SDL_SetError("No such device");
gabomdq@8057
   422
    }
gabomdq@8057
   423
    
gabomdq@8057
   424
    if (item->joystick != NULL) {
gabomdq@8057
   425
        return SDL_SetError("Joystick already opened");
slouken@7191
   426
    }
gabomdq@8057
   427
gabomdq@8057
   428
    joystick->instance_id = item->device_instance;
gabomdq@8057
   429
    joystick->hwdata = (struct joystick_hwdata *) item;
gabomdq@8057
   430
    item->joystick = joystick;
gabomdq@8057
   431
    joystick->nhats = item->nhats;
gabomdq@8057
   432
    joystick->nballs = item->nballs;
gabomdq@8057
   433
    joystick->nbuttons = item->nbuttons;
gabomdq@8057
   434
    joystick->naxes = item->naxes;
gabomdq@8057
   435
gabomdq@8057
   436
    return (0);
gabomdq@7041
   437
}
gabomdq@7041
   438
gabomdq@7041
   439
/* Function to determine is this joystick is attached to the system right now */
gabomdq@7041
   440
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
gabomdq@7041
   441
{
gabomdq@8057
   442
    return !joystick->closed && (joystick->hwdata != NULL);
gabomdq@7041
   443
}
gabomdq@7041
   444
gabomdq@7041
   445
void
gabomdq@7041
   446
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
gabomdq@7041
   447
{
gabomdq@7041
   448
    int i;
gabomdq@7041
   449
    Sint16 value;
gabomdq@7041
   450
    float values[3];
gabomdq@8057
   451
    SDL_joylist_item *item = SDL_joylist;
gabomdq@7041
   452
gabomdq@8057
   453
    while (item) {
gabomdq@8057
   454
        if (item->is_accelerometer) {
gabomdq@8057
   455
            if (item->joystick) {
gabomdq@8057
   456
                Android_JNI_GetAccelerometerValues(values);
gabomdq@8057
   457
                for ( i = 0; i < 3; i++ ) {
gabomdq@8057
   458
                    value = (Sint16)(values[i] * 32767.0f);
gabomdq@8057
   459
                    SDL_PrivateJoystickAxis(item->joystick, i, value);
gabomdq@8057
   460
                }
gabomdq@8057
   461
            }
gabomdq@8057
   462
            break;
gabomdq@7041
   463
        }
gabomdq@8057
   464
        item = item->next;
gabomdq@7041
   465
    }
gabomdq@7041
   466
}
gabomdq@7041
   467
gabomdq@7041
   468
/* Function to close a joystick after use */
gabomdq@7041
   469
void
gabomdq@7041
   470
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
gabomdq@7041
   471
{
gabomdq@8057
   472
    if (joystick->hwdata) {
gabomdq@8057
   473
        ((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
gabomdq@8057
   474
        joystick->hwdata = NULL;
gabomdq@8024
   475
    }
gabomdq@8024
   476
    joystick->closed = 1;
gabomdq@7041
   477
}
gabomdq@7041
   478
gabomdq@7041
   479
/* Function to perform any system-specific joystick related cleanup */
gabomdq@7041
   480
void
gabomdq@7041
   481
SDL_SYS_JoystickQuit(void)
gabomdq@7041
   482
{
gabomdq@8057
   483
    SDL_joylist_item *item = NULL;
gabomdq@8057
   484
    SDL_joylist_item *next = NULL;
gabomdq@8057
   485
gabomdq@8057
   486
    for (item = SDL_joylist; item; item = next) {
gabomdq@8057
   487
        next = item->next;
gabomdq@8057
   488
        SDL_free(item->name);
gabomdq@8057
   489
        SDL_free(item);
gabomdq@8057
   490
    }
gabomdq@8057
   491
gabomdq@8057
   492
    SDL_joylist = SDL_joylist_tail = NULL;
gabomdq@8057
   493
gabomdq@8057
   494
    numjoysticks = 0;
gabomdq@8057
   495
    instance_counter = 0;
gabomdq@7041
   496
}
gabomdq@7041
   497
gabomdq@7041
   498
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
gabomdq@7041
   499
{
gabomdq@8057
   500
    return JoystickByDevIndex(device_index)->guid;
gabomdq@7041
   501
}
gabomdq@7041
   502
gabomdq@7041
   503
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
gabomdq@7041
   504
{
gabomdq@8057
   505
    return ((SDL_joylist_item*)joystick->hwdata)->guid;
gabomdq@7907
   506
}
gabomdq@7907
   507
gabomdq@7041
   508
#endif /* SDL_JOYSTICK_ANDROID */
gabomdq@7041
   509
gabomdq@7041
   510
/* vi: set ts=4 sw=4 expandtab: */