src/joystick/android/SDL_sysjoystick.c
author Gabriel Jacobo <gabomdq@gmail.com>
Thu, 23 Jan 2014 08:44:25 -0300
changeset 8122 7e01e3908f41
parent 8119 df0bf33be2ca
child 8129 47f67902b179
permissions -rw-r--r--
Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
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
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8075
diff changeset
    22
#include "../../SDL_internal.h"
7041
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"
8060
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
    37
#include "SDL_timer.h"
8074
acf83dce00d1 Fixed implicit declaration of SDL_Log() warning in joystick source for Android.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8060
diff changeset
    38
#include "SDL_log.h"
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    39
#include "SDL_sysjoystick_c.h"
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    40
#include "../SDL_joystick_c.h"
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    41
#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
    42
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    43
#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
    44
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    45
/* 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
    46
#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
    47
#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
    48
#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
    49
#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
    50
#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
    51
#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
    52
#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
    53
#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
    54
#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
    55
#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
    56
#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
    57
#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
    58
#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
    59
#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
    60
#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
    61
#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
    62
#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
    63
#endif
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    64
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    65
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    66
#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
    67
#define ANDROID_MAX_NBUTTONS 36
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    68
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    69
static SDL_joylist_item * JoystickByDeviceId(int device_id);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    70
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    71
static SDL_joylist_item *SDL_joylist = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    72
static SDL_joylist_item *SDL_joylist_tail = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    73
static int numjoysticks = 0;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    74
static int instance_counter = 0;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
    75
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    76
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    77
/* Function to convert Android keyCodes into SDL ones.
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    78
 * 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
    79
 * 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
    80
 */
7929
e3b38a3cdd60 Changed function to be static.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7928
diff changeset
    81
static int
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    82
keycode_to_SDL(int keycode)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    83
{
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    84
    /* 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
    85
    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
    86
    switch(keycode) 
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    87
    {
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    88
        /* Some gamepad buttons (API 9) */
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    89
        case AKEYCODE_BUTTON_A:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    90
            button = SDL_CONTROLLER_BUTTON_A;
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    91
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    92
        case AKEYCODE_BUTTON_B:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    93
            button = SDL_CONTROLLER_BUTTON_B;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    94
            break;
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    95
        case AKEYCODE_BUTTON_X:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    96
            button = SDL_CONTROLLER_BUTTON_X;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    97
            break;
7944
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_Y:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
    99
            button = SDL_CONTROLLER_BUTTON_Y;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   100
            break;
7944
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_L1:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   102
            button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   103
            break;
7944
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:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   105
            button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   106
            break;
7944
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_THUMBL:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   108
            button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   109
            break;
7944
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_THUMBR:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   111
            button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   112
            break;
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   113
        case AKEYCODE_BUTTON_START:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   114
            button = SDL_CONTROLLER_BUTTON_START;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   115
            break;
7944
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_SELECT:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   117
            button = SDL_CONTROLLER_BUTTON_BACK;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   118
            break;
7944
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_MODE:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   120
            button = SDL_CONTROLLER_BUTTON_GUIDE;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   121
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   122
        case AKEYCODE_BUTTON_L2:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   123
            button = SDL_CONTROLLER_BUTTON_MAX; /* Not supported by GameController */
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   124
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   125
        case AKEYCODE_BUTTON_R2:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   126
            button = SDL_CONTROLLER_BUTTON_MAX+1; /* Not supported by GameController */
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   127
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   128
        case AKEYCODE_BUTTON_C:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   129
            button = SDL_CONTROLLER_BUTTON_MAX+2; /* Not supported by GameController */
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   130
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   131
        case AKEYCODE_BUTTON_Z:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   132
            button = SDL_CONTROLLER_BUTTON_MAX+3; /* Not supported by GameController */
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   133
            break;
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   134
                        
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   135
        /* D-Pad key codes (API 1) */
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   136
        case AKEYCODE_DPAD_UP:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   137
            button = SDL_CONTROLLER_BUTTON_DPAD_UP;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   138
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   139
        case AKEYCODE_DPAD_DOWN:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   140
            button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   141
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   142
        case AKEYCODE_DPAD_LEFT:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   143
            button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   144
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   145
        case AKEYCODE_DPAD_RIGHT:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   146
            button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   147
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   148
        case AKEYCODE_DPAD_CENTER:
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   149
            button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   150
            break;
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   151
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   152
        /* 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
   153
        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
   154
        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
   155
        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
   156
        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
   157
        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
   158
        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
   159
        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
   160
        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
   161
        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
   162
        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
   163
        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
   164
        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
   165
        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
   166
        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
   167
        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
   168
        case AKEYCODE_BUTTON_16:
8122
7e01e3908f41 Bug 2358 - [Android] Joystick Button Mappings are strange (fix by David Brady)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8119
diff changeset
   169
            button = keycode - AKEYCODE_BUTTON_1 + SDL_CONTROLLER_BUTTON_MAX + 5;
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   170
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   171
            
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   172
        default:
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   173
            return -1;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   174
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   175
    }
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   176
    
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   177
    /* 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
   178
     * 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
   179
     */
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   180
    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
   181
    return button;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   182
    
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   183
}
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   184
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   185
int
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   186
Android_OnPadDown(int device_id, int keycode)
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
    SDL_joylist_item *item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   189
    int button = keycode_to_SDL(keycode);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   190
    if (button >= 0) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   191
        item = JoystickByDeviceId(device_id);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   192
        if (item && item->joystick) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   193
            SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   194
        }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   195
        return 0;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   196
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   197
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   198
    return -1;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   199
}
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   200
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   201
int
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   202
Android_OnPadUp(int device_id, int keycode)
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   203
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   204
    SDL_joylist_item *item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   205
    int button = keycode_to_SDL(keycode);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   206
    if (button >= 0) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   207
        item = JoystickByDeviceId(device_id);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   208
        if (item && item->joystick) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   209
            SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   210
        }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   211
        return 0;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   212
    }
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
    return -1;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   215
}
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   216
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   217
int
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   218
Android_OnJoy(int device_id, int axis, float value)
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   219
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   220
    /* 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
   221
    SDL_joylist_item *item = JoystickByDeviceId(device_id);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   222
    if (item && item->joystick) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   223
        SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   224
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   225
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   226
    return 0;
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
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   229
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   230
int
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   231
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
   232
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   233
    SDL_JoystickGUID guid;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   234
    SDL_joylist_item *item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   235
#if !SDL_EVENTS_DISABLED
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   236
    SDL_Event event;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   237
#endif
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   238
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   239
    if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   240
        return -1;
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
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   243
    /* 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
   244
    SDL_zero( guid );
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   245
    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
   246
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   247
    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
   248
    if (item == NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   249
        return -1;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   250
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   251
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   252
    SDL_zerop(item);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   253
    item->guid = guid;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   254
    item->device_id = device_id;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   255
    item->name = SDL_strdup(name);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   256
    if ( item->name == NULL ) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   257
         SDL_free(item);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   258
         return -1;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   259
    }
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
    item->is_accelerometer = is_accelerometer;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   262
    if (nbuttons > -1) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   263
        item->nbuttons = nbuttons;
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
    else {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   266
        item->nbuttons = ANDROID_MAX_NBUTTONS;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   267
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   268
    item->naxes = naxes;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   269
    item->nhats = nhats;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   270
    item->nballs = nballs;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   271
    item->device_instance = instance_counter++;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   272
    if (SDL_joylist_tail == NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   273
        SDL_joylist = SDL_joylist_tail = item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   274
    } else {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   275
        SDL_joylist_tail->next = item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   276
        SDL_joylist_tail = item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   277
    }
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
    /* 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
   280
    ++numjoysticks;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   281
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   282
#if !SDL_EVENTS_DISABLED
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   283
    event.type = SDL_JOYDEVICEADDED;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   284
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   285
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   286
        event.jdevice.which = (numjoysticks - 1);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   287
        if ( (SDL_EventOK == NULL) ||
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   288
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   289
            SDL_PushEvent(&event);
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
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   292
#endif /* !SDL_EVENTS_DISABLED */
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
    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
   295
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   296
    return 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
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   299
int 
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   300
Android_RemoveJoystick(int device_id)
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   301
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   302
    SDL_joylist_item *item = SDL_joylist;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   303
    SDL_joylist_item *prev = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   304
#if !SDL_EVENTS_DISABLED
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   305
    SDL_Event event;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   306
#endif
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
    /* 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
   309
    while (item != NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   310
        if (item->device_id == device_id) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   311
            break;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   312
        }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   313
        prev = item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   314
        item = item->next;
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
    if (item == NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   318
        return -1;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   319
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   320
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   321
    const int retval = item->device_instance;
8119
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   322
    if (item->joystick) {
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   323
        item->joystick->hwdata = NULL;
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   324
    }
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   325
        
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   326
    if (prev != NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   327
        prev->next = item->next;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   328
    } else {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   329
        SDL_assert(SDL_joylist == item);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   330
        SDL_joylist = item->next;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   331
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   332
    if (item == SDL_joylist_tail) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   333
        SDL_joylist_tail = prev;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   334
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   335
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   336
    /* 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
   337
    --numjoysticks;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   338
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   339
#if !SDL_EVENTS_DISABLED
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   340
    event.type = SDL_JOYDEVICEREMOVED;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   341
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   342
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   343
        event.jdevice.which = item->device_instance;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   344
        if ( (SDL_EventOK == NULL) ||
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   345
             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   346
            SDL_PushEvent(&event);
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
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   349
#endif /* !SDL_EVENTS_DISABLED */
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   350
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   351
    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
   352
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   353
    SDL_free(item->name);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   354
    SDL_free(item);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   355
    return retval;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   356
}
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
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   359
int
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   360
SDL_SYS_JoystickInit(void)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   361
{
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   362
    const char *env;
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   363
    SDL_SYS_JoystickDetect();
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   364
    
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   365
    env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   366
    if (!env || SDL_atoi(env)) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   367
        /* Default behavior, accelerometer as joystick */
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   368
        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
   369
    }
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   370
   
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   371
    return (numjoysticks);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   372
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   373
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   374
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   375
int SDL_SYS_NumJoysticks()
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   376
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   377
    return numjoysticks;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   378
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   379
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   380
void SDL_SYS_JoystickDetect()
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   381
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   382
    /* Support for device connect/disconnect is API >= 16 only,
8060
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
   383
     * so we poll every three seconds
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   384
     * 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
   385
     */
8060
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
   386
    static Uint32 timeout = 0;
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
   387
    if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
   388
        timeout = SDL_GetTicks() + 3000;
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
   389
        Android_JNI_PollInputDevices();
d1948d163145 [Android] Poll joysticks every three seconds
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8057
diff changeset
   390
    }
8057
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
SDL_bool SDL_SYS_JoystickNeedsPolling()
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   394
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   395
    return SDL_TRUE;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   396
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   397
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   398
static SDL_joylist_item *
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   399
JoystickByDevIndex(int device_index)
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   400
{
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   401
    SDL_joylist_item *item = SDL_joylist;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   402
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   403
    if ((device_index < 0) || (device_index >= numjoysticks)) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   404
        return NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   405
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   406
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   407
    while (device_index > 0) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   408
        SDL_assert(item != NULL);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   409
        device_index--;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   410
        item = item->next;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   411
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   412
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   413
    return item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   414
}
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   415
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   416
static SDL_joylist_item *
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   417
JoystickByDeviceId(int device_id)
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   418
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   419
    SDL_joylist_item *item = SDL_joylist;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   420
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   421
    while (item != NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   422
        if (item->device_id == device_id) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   423
            return item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   424
        }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   425
        item = item->next;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   426
    }
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 not found, try adding it */
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   429
    SDL_SYS_JoystickDetect();
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   430
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   431
    while (item != NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   432
        if (item->device_id == device_id) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   433
            return item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   434
        }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   435
        item = item->next;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   436
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   437
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   438
    return NULL;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   439
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   440
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   441
/* Function to get the device-dependent name of a joystick */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   442
const char *
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   443
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   444
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   445
    return JoystickByDevIndex(device_index)->name;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   446
}
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
/* 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
   449
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   450
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   451
    return JoystickByDevIndex(device_index)->device_instance;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   452
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   453
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   454
/* Function to open a joystick for use.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   455
   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
   456
   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
   457
   It returns 0, or -1 if there is an error.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   458
 */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   459
int
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   460
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   461
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   462
    SDL_joylist_item *item = JoystickByDevIndex(device_index);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   463
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   464
    if (item == NULL ) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   465
        return SDL_SetError("No such device");
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   466
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   467
    
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   468
    if (item->joystick != NULL) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   469
        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
   470
    }
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   471
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   472
    joystick->instance_id = item->device_instance;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   473
    joystick->hwdata = (struct joystick_hwdata *) item;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   474
    item->joystick = joystick;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   475
    joystick->nhats = item->nhats;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   476
    joystick->nballs = item->nballs;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   477
    joystick->nbuttons = item->nbuttons;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   478
    joystick->naxes = item->naxes;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   479
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   480
    return (0);
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   481
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   482
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   483
/* 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
   484
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   485
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   486
    return !joystick->closed && (joystick->hwdata != NULL);
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   487
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   488
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   489
void
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   490
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   491
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   492
    int i;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   493
    Sint16 value;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   494
    float values[3];
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   495
    SDL_joylist_item *item = SDL_joylist;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   496
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   497
    while (item) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   498
        if (item->is_accelerometer) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   499
            if (item->joystick) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   500
                Android_JNI_GetAccelerometerValues(values);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   501
                for ( i = 0; i < 3; i++ ) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   502
                    value = (Sint16)(values[i] * 32767.0f);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   503
                    SDL_PrivateJoystickAxis(item->joystick, i, value);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   504
                }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   505
            }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   506
            break;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   507
        }
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   508
        item = item->next;
7041
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
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   511
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   512
/* Function to close a joystick after use */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   513
void
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   514
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   515
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   516
    if (joystick->hwdata) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   517
        ((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   518
        joystick->hwdata = NULL;
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   519
    }
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   520
    joystick->closed = 1;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   521
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   522
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   523
/* Function to perform any system-specific joystick related cleanup */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   524
void
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   525
SDL_SYS_JoystickQuit(void)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   526
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   527
    SDL_joylist_item *item = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   528
    SDL_joylist_item *next = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   529
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   530
    for (item = SDL_joylist; item; item = next) {
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   531
        next = item->next;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   532
        SDL_free(item->name);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   533
        SDL_free(item);
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   534
    }
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   535
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   536
    SDL_joylist = SDL_joylist_tail = NULL;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   537
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   538
    numjoysticks = 0;
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   539
    instance_counter = 0;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   540
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   541
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   542
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   543
{
8057
801d84e26f91 [Android] Hotplugging support for joysticks
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8025
diff changeset
   544
    return JoystickByDevIndex(device_index)->guid;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   545
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   546
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   547
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   548
{
8119
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   549
    SDL_JoystickGUID guid;
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   550
    
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   551
    if (joystick->hwdata != NULL) {
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   552
        return ((SDL_joylist_item*)joystick->hwdata)->guid;
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   553
    }
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   554
    
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   555
    SDL_zero(guid);
df0bf33be2ca Fixes 2356, [Android] SDL_JoystickGetAttached does not function
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   556
    return guid;
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   557
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   558
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   559
#endif /* SDL_JOYSTICK_ANDROID */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   560
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   561
/* vi: set ts=4 sw=4 expandtab: */