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