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