src/joystick/android/SDL_sysjoystick.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 15 Apr 2015 21:29:55 +0200
changeset 9561 d8ad01792399
parent 9433 bd062398b648
child 9579 9a5b4bf6f4d1
permissions -rw-r--r--
Fixed typo in internal joystick documentation comments.
gabomdq@7041
     1
/*
gabomdq@7041
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
icculus@8093
    22
#include "../../SDL_internal.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@8122
    88
        /* Some gamepad buttons (API 9) */
gabomdq@8122
    89
        case AKEYCODE_BUTTON_A:
gabomdq@8122
    90
            button = SDL_CONTROLLER_BUTTON_A;
gabomdq@8122
    91
            break;
gabomdq@8122
    92
        case AKEYCODE_BUTTON_B:
gabomdq@8122
    93
            button = SDL_CONTROLLER_BUTTON_B;
gabomdq@8122
    94
            break;
gabomdq@8122
    95
        case AKEYCODE_BUTTON_X:
gabomdq@8122
    96
            button = SDL_CONTROLLER_BUTTON_X;
gabomdq@8122
    97
            break;
gabomdq@8122
    98
        case AKEYCODE_BUTTON_Y:
gabomdq@8122
    99
            button = SDL_CONTROLLER_BUTTON_Y;
gabomdq@8122
   100
            break;
gabomdq@8122
   101
        case AKEYCODE_BUTTON_L1:
gabomdq@8122
   102
            button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
gabomdq@8122
   103
            break;
gabomdq@8122
   104
        case AKEYCODE_BUTTON_R1:
gabomdq@8122
   105
            button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
gabomdq@8122
   106
            break;
gabomdq@8122
   107
        case AKEYCODE_BUTTON_THUMBL:
gabomdq@8122
   108
            button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
gabomdq@8122
   109
            break;
gabomdq@8122
   110
        case AKEYCODE_BUTTON_THUMBR:
gabomdq@8122
   111
            button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
gabomdq@8122
   112
            break;
gabomdq@8122
   113
        case AKEYCODE_BUTTON_START:
gabomdq@8122
   114
            button = SDL_CONTROLLER_BUTTON_START;
gabomdq@8122
   115
            break;
gabomdq@8122
   116
        case AKEYCODE_BUTTON_SELECT:
gabomdq@8122
   117
            button = SDL_CONTROLLER_BUTTON_BACK;
gabomdq@8122
   118
            break;
gabomdq@8122
   119
        case AKEYCODE_BUTTON_MODE:
gabomdq@8122
   120
            button = SDL_CONTROLLER_BUTTON_GUIDE;
gabomdq@8122
   121
            break;
gabomdq@8122
   122
        case AKEYCODE_BUTTON_L2:
gabomdq@8122
   123
            button = SDL_CONTROLLER_BUTTON_MAX; /* Not supported by GameController */
gabomdq@8122
   124
            break;
gabomdq@8122
   125
        case AKEYCODE_BUTTON_R2:
gabomdq@8122
   126
            button = SDL_CONTROLLER_BUTTON_MAX+1; /* Not supported by GameController */
gabomdq@8122
   127
            break;
gabomdq@8122
   128
        case AKEYCODE_BUTTON_C:
gabomdq@8122
   129
            button = SDL_CONTROLLER_BUTTON_MAX+2; /* Not supported by GameController */
gabomdq@8122
   130
            break;
gabomdq@8122
   131
        case AKEYCODE_BUTTON_Z:
gabomdq@8122
   132
            button = SDL_CONTROLLER_BUTTON_MAX+3; /* Not supported by GameController */
gabomdq@8122
   133
            break;
gabomdq@8122
   134
                        
gabomdq@8122
   135
        /* D-Pad key codes (API 1) */
gabomdq@7944
   136
        case AKEYCODE_DPAD_UP:
gabomdq@8122
   137
            button = SDL_CONTROLLER_BUTTON_DPAD_UP;
gabomdq@8122
   138
            break;
gabomdq@7944
   139
        case AKEYCODE_DPAD_DOWN:
gabomdq@8122
   140
            button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
gabomdq@8122
   141
            break;
gabomdq@7944
   142
        case AKEYCODE_DPAD_LEFT:
gabomdq@8122
   143
            button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
gabomdq@8122
   144
            break;
gabomdq@7944
   145
        case AKEYCODE_DPAD_RIGHT:
gabomdq@8122
   146
            button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
gabomdq@8122
   147
            break;
gabomdq@7944
   148
        case AKEYCODE_DPAD_CENTER:
gabomdq@8122
   149
            button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
gabomdq@7944
   150
            break;
gabomdq@8122
   151
gabomdq@7944
   152
        /* More gamepad buttons (API 12), these get mapped to 20...35*/
gabomdq@7944
   153
        case AKEYCODE_BUTTON_1:
gabomdq@7944
   154
        case AKEYCODE_BUTTON_2:
gabomdq@7944
   155
        case AKEYCODE_BUTTON_3:
gabomdq@7944
   156
        case AKEYCODE_BUTTON_4:
gabomdq@7944
   157
        case AKEYCODE_BUTTON_5:
gabomdq@7944
   158
        case AKEYCODE_BUTTON_6:
gabomdq@7944
   159
        case AKEYCODE_BUTTON_7:
gabomdq@7944
   160
        case AKEYCODE_BUTTON_8:
gabomdq@7944
   161
        case AKEYCODE_BUTTON_9:
gabomdq@7944
   162
        case AKEYCODE_BUTTON_10:
gabomdq@7944
   163
        case AKEYCODE_BUTTON_11:
gabomdq@7944
   164
        case AKEYCODE_BUTTON_12:
gabomdq@7944
   165
        case AKEYCODE_BUTTON_13:
gabomdq@7944
   166
        case AKEYCODE_BUTTON_14:
gabomdq@7944
   167
        case AKEYCODE_BUTTON_15:
gabomdq@7944
   168
        case AKEYCODE_BUTTON_16:
gabomdq@8122
   169
            button = keycode - AKEYCODE_BUTTON_1 + SDL_CONTROLLER_BUTTON_MAX + 5;
gabomdq@7944
   170
            break;
gabomdq@7944
   171
            
gabomdq@7944
   172
        default:
gabomdq@7944
   173
            return -1;
gabomdq@7944
   174
            break;
gabomdq@7944
   175
    }
gabomdq@7944
   176
    
gabomdq@7944
   177
    /* This is here in case future generations, probably with six fingers per hand, 
gabomdq@7944
   178
     * happily add new cases up above and forget to update the max number of buttons. 
gabomdq@7907
   179
     */
gabomdq@7944
   180
    SDL_assert(button < ANDROID_MAX_NBUTTONS);
gabomdq@7944
   181
    return button;
gabomdq@7944
   182
    
gabomdq@7907
   183
}
gabomdq@7041
   184
gabomdq@8057
   185
int
gabomdq@8057
   186
Android_OnPadDown(int device_id, int keycode)
gabomdq@8057
   187
{
gabomdq@8057
   188
    SDL_joylist_item *item;
gabomdq@8057
   189
    int button = keycode_to_SDL(keycode);
gabomdq@8057
   190
    if (button >= 0) {
gabomdq@8057
   191
        item = JoystickByDeviceId(device_id);
gabomdq@8057
   192
        if (item && item->joystick) {
gabomdq@8057
   193
            SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
gabomdq@8057
   194
        }
gabomdq@8057
   195
        return 0;
gabomdq@8057
   196
    }
gabomdq@8057
   197
    
gabomdq@8057
   198
    return -1;
gabomdq@8057
   199
}
gabomdq@8057
   200
gabomdq@8057
   201
int
gabomdq@8057
   202
Android_OnPadUp(int device_id, int keycode)
gabomdq@8057
   203
{
gabomdq@8057
   204
    SDL_joylist_item *item;
gabomdq@8057
   205
    int button = keycode_to_SDL(keycode);
gabomdq@8057
   206
    if (button >= 0) {
gabomdq@8057
   207
        item = JoystickByDeviceId(device_id);
gabomdq@8057
   208
        if (item && item->joystick) {
gabomdq@8057
   209
            SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
gabomdq@8057
   210
        }
gabomdq@8057
   211
        return 0;
gabomdq@8057
   212
    }
gabomdq@8057
   213
    
gabomdq@8057
   214
    return -1;
gabomdq@8057
   215
}
gabomdq@8057
   216
gabomdq@8057
   217
int
gabomdq@8057
   218
Android_OnJoy(int device_id, int axis, float value)
gabomdq@8057
   219
{
gabomdq@8057
   220
    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
gabomdq@8057
   221
    SDL_joylist_item *item = JoystickByDeviceId(device_id);
gabomdq@8057
   222
    if (item && item->joystick) {
gabomdq@8057
   223
        SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
gabomdq@8057
   224
    }
gabomdq@8057
   225
    
gabomdq@8057
   226
    return 0;
gabomdq@8057
   227
}
gabomdq@8057
   228
dbrady@8140
   229
int
dbrady@8140
   230
Android_OnHat(int device_id, int hat_id, int x, int y)
dbrady@8140
   231
{
dbrady@8140
   232
    const Uint8 position_map[3][3] = {
dbrady@8140
   233
        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
dbrady@8140
   234
        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
dbrady@8140
   235
        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
dbrady@8140
   236
    };
dbrady@8140
   237
dbrady@8140
   238
    if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
dbrady@8140
   239
        SDL_joylist_item *item = JoystickByDeviceId(device_id);
dbrady@8140
   240
        if (item && item->joystick) {
dbrady@8140
   241
            SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );
dbrady@8140
   242
        }
dbrady@8140
   243
        return 0;
dbrady@8140
   244
    }
dbrady@8140
   245
dbrady@8140
   246
    return -1;
dbrady@8140
   247
}
dbrady@8140
   248
gabomdq@8057
   249
gabomdq@8057
   250
int
gabomdq@8057
   251
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
gabomdq@8057
   252
{
gabomdq@8057
   253
    SDL_JoystickGUID guid;
gabomdq@8057
   254
    SDL_joylist_item *item;
gabomdq@8057
   255
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   256
    SDL_Event event;
gabomdq@8057
   257
#endif
gabomdq@8057
   258
    
gabomdq@8057
   259
    if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
gabomdq@8057
   260
        return -1;
gabomdq@8057
   261
    }
gabomdq@8057
   262
    
gabomdq@8057
   263
    /* the GUID is just the first 16 chars of the name for now */
gabomdq@8057
   264
    SDL_zero( guid );
gabomdq@8057
   265
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
gabomdq@8057
   266
gabomdq@8057
   267
    item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
gabomdq@8057
   268
    if (item == NULL) {
gabomdq@8057
   269
        return -1;
gabomdq@8057
   270
    }
gabomdq@8057
   271
gabomdq@8057
   272
    SDL_zerop(item);
gabomdq@8057
   273
    item->guid = guid;
gabomdq@8057
   274
    item->device_id = device_id;
gabomdq@8057
   275
    item->name = SDL_strdup(name);
gabomdq@8057
   276
    if ( item->name == NULL ) {
gabomdq@8057
   277
         SDL_free(item);
gabomdq@8057
   278
         return -1;
gabomdq@8057
   279
    }
gabomdq@8057
   280
    
gabomdq@8057
   281
    item->is_accelerometer = is_accelerometer;
gabomdq@8057
   282
    if (nbuttons > -1) {
gabomdq@8057
   283
        item->nbuttons = nbuttons;
gabomdq@8057
   284
    }
gabomdq@8057
   285
    else {
gabomdq@8057
   286
        item->nbuttons = ANDROID_MAX_NBUTTONS;
gabomdq@8057
   287
    }
gabomdq@8057
   288
    item->naxes = naxes;
gabomdq@8057
   289
    item->nhats = nhats;
gabomdq@8057
   290
    item->nballs = nballs;
gabomdq@8057
   291
    item->device_instance = instance_counter++;
gabomdq@8057
   292
    if (SDL_joylist_tail == NULL) {
gabomdq@8057
   293
        SDL_joylist = SDL_joylist_tail = item;
gabomdq@8057
   294
    } else {
gabomdq@8057
   295
        SDL_joylist_tail->next = item;
gabomdq@8057
   296
        SDL_joylist_tail = item;
gabomdq@8057
   297
    }
gabomdq@8057
   298
gabomdq@8057
   299
    /* Need to increment the joystick count before we post the event */
gabomdq@8057
   300
    ++numjoysticks;
gabomdq@8057
   301
gabomdq@8057
   302
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   303
    event.type = SDL_JOYDEVICEADDED;
gabomdq@8057
   304
gabomdq@8057
   305
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
gabomdq@8057
   306
        event.jdevice.which = (numjoysticks - 1);
gabomdq@8057
   307
        if ( (SDL_EventOK == NULL) ||
gabomdq@8057
   308
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
gabomdq@8057
   309
            SDL_PushEvent(&event);
gabomdq@8057
   310
        }
gabomdq@8057
   311
    }
gabomdq@8057
   312
#endif /* !SDL_EVENTS_DISABLED */
gabomdq@8057
   313
gabomdq@8057
   314
    SDL_Log("Added joystick %s with device_id %d", name, device_id);
gabomdq@8057
   315
gabomdq@8057
   316
    return numjoysticks;
gabomdq@8057
   317
}
gabomdq@8057
   318
gabomdq@8057
   319
int 
gabomdq@8057
   320
Android_RemoveJoystick(int device_id)
gabomdq@8057
   321
{
gabomdq@8057
   322
    SDL_joylist_item *item = SDL_joylist;
gabomdq@8057
   323
    SDL_joylist_item *prev = NULL;
gabomdq@8057
   324
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   325
    SDL_Event event;
gabomdq@8057
   326
#endif
gabomdq@8057
   327
    
gabomdq@8057
   328
    /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
gabomdq@8057
   329
    while (item != NULL) {
gabomdq@8057
   330
        if (item->device_id == device_id) {
gabomdq@8057
   331
            break;
gabomdq@8057
   332
        }
gabomdq@8057
   333
        prev = item;
gabomdq@8057
   334
        item = item->next;
gabomdq@8057
   335
    }
gabomdq@8057
   336
    
gabomdq@8057
   337
    if (item == NULL) {
gabomdq@8057
   338
        return -1;
gabomdq@8057
   339
    }
gabomdq@8057
   340
gabomdq@8057
   341
    const int retval = item->device_instance;
gabomdq@8119
   342
    if (item->joystick) {
gabomdq@8119
   343
        item->joystick->hwdata = NULL;
gabomdq@8119
   344
    }
gabomdq@8119
   345
        
gabomdq@8057
   346
    if (prev != NULL) {
gabomdq@8057
   347
        prev->next = item->next;
gabomdq@8057
   348
    } else {
gabomdq@8057
   349
        SDL_assert(SDL_joylist == item);
gabomdq@8057
   350
        SDL_joylist = item->next;
gabomdq@8057
   351
    }
gabomdq@8057
   352
    if (item == SDL_joylist_tail) {
gabomdq@8057
   353
        SDL_joylist_tail = prev;
gabomdq@8057
   354
    }
gabomdq@8057
   355
gabomdq@8057
   356
    /* Need to decrement the joystick count before we post the event */
gabomdq@8057
   357
    --numjoysticks;
gabomdq@8057
   358
gabomdq@8057
   359
#if !SDL_EVENTS_DISABLED
gabomdq@8057
   360
    event.type = SDL_JOYDEVICEREMOVED;
gabomdq@8057
   361
gabomdq@8057
   362
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
gabomdq@8057
   363
        event.jdevice.which = item->device_instance;
gabomdq@8057
   364
        if ( (SDL_EventOK == NULL) ||
gabomdq@8057
   365
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
gabomdq@8057
   366
            SDL_PushEvent(&event);
gabomdq@8057
   367
        }
gabomdq@8057
   368
    }
gabomdq@8057
   369
#endif /* !SDL_EVENTS_DISABLED */
gabomdq@8057
   370
gabomdq@8057
   371
    SDL_Log("Removed joystick with device_id %d", device_id);
gabomdq@8057
   372
    
gabomdq@8057
   373
    SDL_free(item->name);
gabomdq@8057
   374
    SDL_free(item);
gabomdq@8057
   375
    return retval;
gabomdq@8057
   376
}
gabomdq@8057
   377
gabomdq@8057
   378
gabomdq@7041
   379
int
gabomdq@7041
   380
SDL_SYS_JoystickInit(void)
gabomdq@7041
   381
{
slouken@8274
   382
    const char *hint;
gabomdq@8057
   383
    SDL_SYS_JoystickDetect();
gabomdq@7907
   384
    
slouken@8274
   385
    hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
slouken@8274
   386
    if (!hint || SDL_atoi(hint)) {
gabomdq@8057
   387
        /* Default behavior, accelerometer as joystick */
gabomdq@8057
   388
        Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
gabomdq@7907
   389
    }
gabomdq@7907
   390
   
gabomdq@8057
   391
    return (numjoysticks);
gabomdq@8057
   392
gabomdq@7041
   393
}
gabomdq@7041
   394
gabomdq@7041
   395
int SDL_SYS_NumJoysticks()
gabomdq@7041
   396
{
gabomdq@8057
   397
    return numjoysticks;
gabomdq@7041
   398
}
gabomdq@7041
   399
gabomdq@7041
   400
void SDL_SYS_JoystickDetect()
gabomdq@7041
   401
{
gabomdq@8057
   402
    /* Support for device connect/disconnect is API >= 16 only,
gabomdq@8060
   403
     * so we poll every three seconds
gabomdq@8057
   404
     * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
gabomdq@8057
   405
     */
gabomdq@8060
   406
    static Uint32 timeout = 0;
gabomdq@8060
   407
    if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
gabomdq@8060
   408
        timeout = SDL_GetTicks() + 3000;
gabomdq@8060
   409
        Android_JNI_PollInputDevices();
gabomdq@8060
   410
    }
gabomdq@7041
   411
}
gabomdq@7041
   412
gabomdq@8057
   413
static SDL_joylist_item *
gabomdq@8057
   414
JoystickByDevIndex(int device_index)
gabomdq@8057
   415
{
gabomdq@8057
   416
    SDL_joylist_item *item = SDL_joylist;
gabomdq@8057
   417
gabomdq@8057
   418
    if ((device_index < 0) || (device_index >= numjoysticks)) {
gabomdq@8057
   419
        return NULL;
gabomdq@8057
   420
    }
gabomdq@8057
   421
gabomdq@8057
   422
    while (device_index > 0) {
gabomdq@8057
   423
        SDL_assert(item != NULL);
gabomdq@8057
   424
        device_index--;
gabomdq@8057
   425
        item = item->next;
gabomdq@8057
   426
    }
gabomdq@8057
   427
gabomdq@8057
   428
    return item;
gabomdq@8057
   429
}
gabomdq@8057
   430
gabomdq@8057
   431
static SDL_joylist_item *
gabomdq@8057
   432
JoystickByDeviceId(int device_id)
gabomdq@8057
   433
{
gabomdq@8057
   434
    SDL_joylist_item *item = SDL_joylist;
gabomdq@8057
   435
gabomdq@8057
   436
    while (item != NULL) {
gabomdq@8057
   437
        if (item->device_id == device_id) {
gabomdq@8057
   438
            return item;
gabomdq@8057
   439
        }
gabomdq@8057
   440
        item = item->next;
gabomdq@8057
   441
    }
gabomdq@8057
   442
    
gabomdq@8057
   443
    /* Joystick not found, try adding it */
gabomdq@8057
   444
    SDL_SYS_JoystickDetect();
gabomdq@8057
   445
    
gabomdq@8057
   446
    while (item != NULL) {
gabomdq@8057
   447
        if (item->device_id == device_id) {
gabomdq@8057
   448
            return item;
gabomdq@8057
   449
        }
gabomdq@8057
   450
        item = item->next;
gabomdq@8057
   451
    }
gabomdq@8057
   452
gabomdq@8057
   453
    return NULL;
gabomdq@7041
   454
}
gabomdq@7041
   455
gabomdq@7041
   456
/* Function to get the device-dependent name of a joystick */
gabomdq@7041
   457
const char *
gabomdq@7041
   458
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
gabomdq@7041
   459
{
gabomdq@8057
   460
    return JoystickByDevIndex(device_index)->name;
gabomdq@7041
   461
}
gabomdq@7041
   462
gabomdq@7041
   463
/* Function to perform the mapping from device index to the instance id for this index */
gabomdq@7041
   464
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
gabomdq@7041
   465
{
gabomdq@8057
   466
    return JoystickByDevIndex(device_index)->device_instance;
gabomdq@7041
   467
}
gabomdq@7041
   468
gabomdq@7041
   469
/* Function to open a joystick for use.
philipp@9380
   470
   The joystick to open is specified by the device index.
gabomdq@7041
   471
   This should fill the nbuttons and naxes fields of the joystick structure.
gabomdq@7041
   472
   It returns 0, or -1 if there is an error.
gabomdq@7041
   473
 */
gabomdq@7041
   474
int
gabomdq@7041
   475
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
gabomdq@7041
   476
{
gabomdq@8057
   477
    SDL_joylist_item *item = JoystickByDevIndex(device_index);
gabomdq@8057
   478
gabomdq@8057
   479
    if (item == NULL ) {
gabomdq@8057
   480
        return SDL_SetError("No such device");
slouken@7191
   481
    }
gabomdq@8057
   482
    
gabomdq@8057
   483
    if (item->joystick != NULL) {
gabomdq@8057
   484
        return SDL_SetError("Joystick already opened");
gabomdq@8057
   485
    }
gabomdq@8057
   486
gabomdq@8057
   487
    joystick->instance_id = item->device_instance;
gabomdq@8057
   488
    joystick->hwdata = (struct joystick_hwdata *) item;
gabomdq@8057
   489
    item->joystick = joystick;
gabomdq@8057
   490
    joystick->nhats = item->nhats;
gabomdq@8057
   491
    joystick->nballs = item->nballs;
gabomdq@8057
   492
    joystick->nbuttons = item->nbuttons;
gabomdq@8057
   493
    joystick->naxes = item->naxes;
gabomdq@8057
   494
gabomdq@8057
   495
    return (0);
gabomdq@7041
   496
}
gabomdq@7041
   497
philipp@9561
   498
/* Function to determine if this joystick is attached to the system right now */
gabomdq@7041
   499
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
gabomdq@7041
   500
{
icculus@9433
   501
    return joystick->hwdata != NULL;
gabomdq@7041
   502
}
gabomdq@7041
   503
gabomdq@7041
   504
void
gabomdq@7041
   505
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
gabomdq@7041
   506
{
gabomdq@7041
   507
    int i;
gabomdq@7041
   508
    Sint16 value;
gabomdq@7041
   509
    float values[3];
gabomdq@8057
   510
    SDL_joylist_item *item = SDL_joylist;
gabomdq@7041
   511
gabomdq@8057
   512
    while (item) {
gabomdq@8057
   513
        if (item->is_accelerometer) {
gabomdq@8057
   514
            if (item->joystick) {
gabomdq@8129
   515
                if (Android_JNI_GetAccelerometerValues(values)) {
gabomdq@8129
   516
                    for ( i = 0; i < 3; i++ ) {
gabomdq@8129
   517
                        value = (Sint16)(values[i] * 32767.0f);
gabomdq@8129
   518
                        SDL_PrivateJoystickAxis(item->joystick, i, value);
gabomdq@8129
   519
                    }
gabomdq@8057
   520
                }
gabomdq@8057
   521
            }
gabomdq@8057
   522
            break;
gabomdq@7041
   523
        }
gabomdq@8057
   524
        item = item->next;
gabomdq@7041
   525
    }
gabomdq@7041
   526
}
gabomdq@7041
   527
gabomdq@7041
   528
/* Function to close a joystick after use */
gabomdq@7041
   529
void
gabomdq@7041
   530
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
gabomdq@7041
   531
{
gabomdq@7041
   532
}
gabomdq@7041
   533
gabomdq@7041
   534
/* Function to perform any system-specific joystick related cleanup */
gabomdq@7041
   535
void
gabomdq@7041
   536
SDL_SYS_JoystickQuit(void)
gabomdq@7041
   537
{
gabomdq@8057
   538
    SDL_joylist_item *item = NULL;
gabomdq@8057
   539
    SDL_joylist_item *next = NULL;
gabomdq@8057
   540
gabomdq@8057
   541
    for (item = SDL_joylist; item; item = next) {
gabomdq@8057
   542
        next = item->next;
gabomdq@8057
   543
        SDL_free(item->name);
gabomdq@8057
   544
        SDL_free(item);
gabomdq@8057
   545
    }
gabomdq@8057
   546
gabomdq@8057
   547
    SDL_joylist = SDL_joylist_tail = NULL;
gabomdq@8057
   548
gabomdq@8057
   549
    numjoysticks = 0;
gabomdq@8057
   550
    instance_counter = 0;
gabomdq@7041
   551
}
gabomdq@7041
   552
gabomdq@7041
   553
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
gabomdq@7041
   554
{
gabomdq@8057
   555
    return JoystickByDevIndex(device_index)->guid;
gabomdq@7041
   556
}
gabomdq@7041
   557
gabomdq@7041
   558
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
gabomdq@7041
   559
{
gabomdq@7041
   560
    SDL_JoystickGUID guid;
gabomdq@8119
   561
    
gabomdq@8119
   562
    if (joystick->hwdata != NULL) {
gabomdq@8119
   563
        return ((SDL_joylist_item*)joystick->hwdata)->guid;
gabomdq@7956
   564
    }
gabomdq@7907
   565
    
gabomdq@8119
   566
    SDL_zero(guid);
gabomdq@8119
   567
    return guid;
gabomdq@7907
   568
}
gabomdq@7907
   569
gabomdq@7041
   570
#endif /* SDL_JOYSTICK_ANDROID */
gabomdq@7041
   571
gabomdq@7041
   572
/* vi: set ts=4 sw=4 expandtab: */