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