src/joystick/android/SDL_sysjoystick.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 23 Nov 2013 18:29:36 +0100
changeset 8025 a867f1891d2a
parent 8024 ffbdb99af7bd
child 8057 801d84e26f91
child 8543 b9dd3cf38585
permissions -rw-r--r--
Removed include of no more needed header.
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
/* This is the system specific header for the SDL joystick API */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    27
#include <stdio.h>              /* For the definition of NULL */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    28
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    29
#include "SDL_error.h"
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    30
#include "SDL_events.h"
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    31
#include "SDL_joystick.h"
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    32
#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
    33
#include "SDL_assert.h"
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    34
#include "../SDL_sysjoystick.h"
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    35
#include "../SDL_joystick_c.h"
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    36
#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
    37
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    38
#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
    39
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    40
/* 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
    41
#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
    42
#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
    43
#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
    44
#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
    45
#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
    46
#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
    47
#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
    48
#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
    49
#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
    50
#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
    51
#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
    52
#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
    53
#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
    54
#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
    55
#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
    56
#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
    57
#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
    58
#endif
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    59
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    60
#define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    61
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    62
#define ANDROID_MAX_NBUTTONS 36
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    63
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    64
static SDL_Joystick **SYS_Joysticks;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    65
static char **SYS_JoystickNames;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    66
static int SYS_numjoysticks;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    67
static SDL_bool SYS_accelAsJoy;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    68
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    69
/* Function to convert Android keyCodes into SDL ones.
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    70
 * 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
    71
 * 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
    72
 */
7929
e3b38a3cdd60 Changed function to be static.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7928
diff changeset
    73
static int
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    74
keycode_to_SDL(int keycode)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
    75
{
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    76
    /* 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
    77
    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
    78
    switch(keycode) 
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    79
    {
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    80
        /* 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
    81
        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
    82
        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
    83
        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
    84
        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
    85
        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
    86
            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
    87
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    88
        
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
    89
        /* 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
    90
        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
    91
        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
    92
        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
    93
        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
    94
        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
    95
        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
    96
        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
    97
        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
    98
        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
    99
        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
   100
        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
   101
        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
   102
        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
   103
        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
   104
        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
   105
            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
   106
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   107
            
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   108
        
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   109
        /* 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
   110
        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
   111
        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
   112
        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
   113
        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
   114
        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
   115
        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
   116
        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
   117
        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
   118
        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
   119
        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
   120
        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
   121
        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
   122
        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
   123
        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
   124
        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
   125
        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
   126
            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
   127
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   128
            
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   129
        default:
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   130
            return -1;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   131
            break;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   132
    }
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   133
    
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   134
    /* 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
   135
     * 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
   136
     */
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   137
    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
   138
    return button;
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   139
    
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   140
}
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   141
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   142
/* Function to scan the system for joysticks.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   143
 * This function should set SDL_numjoysticks to the number of available
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   144
 * joysticks.  Joystick 0 should be the system default joystick.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   145
 * It should return 0, or -1 on an unrecoverable fatal error.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   146
 */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   147
int
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   148
SDL_SYS_JoystickInit(void)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   149
{
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   150
    int i = 0;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   151
    const char *env;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   152
    
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   153
    env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   154
    if (env && !SDL_atoi(env))
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   155
        SYS_accelAsJoy = SDL_FALSE;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   156
    else
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   157
        SYS_accelAsJoy = SDL_TRUE; /* Default behavior */
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   158
    
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   159
    SYS_numjoysticks = Android_JNI_GetNumJoysticks();
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   160
    if (SYS_accelAsJoy) {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   161
        SYS_numjoysticks++;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   162
    }
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   163
    SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, SYS_numjoysticks*sizeof(SDL_Joystick *));
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   164
    if (SYS_Joysticks == NULL)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   165
    {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   166
        return SDL_OutOfMemory();
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   167
    }
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   168
    SYS_JoystickNames = (char **)SDL_calloc(1, SYS_numjoysticks*sizeof(char *));
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   169
    if (SYS_JoystickNames == NULL)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   170
    {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   171
        SDL_free(SYS_Joysticks);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   172
        SYS_Joysticks = NULL;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   173
        return SDL_OutOfMemory();
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   174
    }
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   175
    
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   176
    for (i = 0; i < SYS_numjoysticks; i++)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   177
    {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   178
        if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   179
            SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   180
        } else {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   181
            SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   182
        }
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   183
    }
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   184
   
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   185
    return (SYS_numjoysticks);
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   186
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   187
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   188
int SDL_SYS_NumJoysticks()
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   189
{
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   190
    return SYS_numjoysticks;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   191
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   192
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   193
void SDL_SYS_JoystickDetect()
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   194
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   195
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   196
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   197
/* TODO: Hotplugging support */
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   198
SDL_bool SDL_SYS_JoystickNeedsPolling()
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   199
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   200
    return SDL_FALSE;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   201
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   202
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   203
/* Function to get the device-dependent name of a joystick */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   204
const char *
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   205
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   206
{
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   207
    return SYS_JoystickNames[device_index];
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   208
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   209
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   210
/* 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
   211
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   212
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   213
    return device_index;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   214
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   215
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   216
/* Function to open a joystick for use.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   217
   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
   218
   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
   219
   It returns 0, or -1 if there is an error.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   220
 */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   221
int
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   222
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   223
{
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   224
    if (device_index < SYS_numjoysticks) {
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   225
        joystick->nhats = 0;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   226
        joystick->nballs = 0;
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   227
        if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   228
            joystick->nbuttons = 0;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   229
            joystick->naxes = 3;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   230
        } else {
7944
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   231
            /* FIXME: Get the real number of buttons in the device? */
f19c0aca56aa Fixes bugs #2213 and #2214, improves the Android joystick code button handling
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7930
diff changeset
   232
            joystick->nbuttons = ANDROID_MAX_NBUTTONS;
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   233
            joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   234
        }
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   235
        
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   236
        SYS_Joysticks[device_index] = joystick;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   237
        return 0;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7041
diff changeset
   238
    } else {
7930
5a1a7cdf3fa2 Changed function to return -1 through SDL_Error() instead of plain -1.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7929
diff changeset
   239
        return SDL_SetError("No joystick available with that index");
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7041
diff changeset
   240
    }
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   241
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   242
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   243
/* 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
   244
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   245
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   246
    return SDL_TRUE;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   247
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   248
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   249
/* Function to update the state of a joystick - called as a device poll.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   250
 * This function shouldn't update the joystick structure directly,
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   251
 * but instead should call SDL_PrivateJoystick*() to deliver events
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   252
 * and update joystick device state.
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   253
 */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   254
void
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   255
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   256
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   257
    int i;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   258
    Sint16 value;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   259
    float values[3];
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   260
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   261
    if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   262
        joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   263
        for ( i = 0; i < 3; i++ ) {
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   264
            value = (Sint16)(values[i] * 32767.0f);
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   265
            SDL_PrivateJoystickAxis(joystick, i, value);
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   266
        }
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   267
    }
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   268
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   269
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   270
/* Function to close a joystick after use */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   271
void
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   272
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   273
{
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   274
    int device_index;
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   275
    
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   276
    for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   277
        if ( SYS_Joysticks[device_index] == joystick ) {
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   278
            SYS_Joysticks[device_index] = NULL;
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   279
        }
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   280
    }
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   281
    
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   282
    joystick->closed = 1;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   283
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   284
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   285
/* Function to perform any system-specific joystick related cleanup */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   286
void
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   287
SDL_SYS_JoystickQuit(void)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   288
{
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   289
    SDL_free(SYS_JoystickNames);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   290
    SDL_free(SYS_Joysticks);
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   291
    SYS_JoystickNames = NULL;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   292
    SYS_Joysticks = NULL;
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   293
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   294
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   295
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   296
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   297
    SDL_JoystickGUID guid;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7041
diff changeset
   298
    /* the GUID is just the first 16 chars of the name for now */
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   299
    const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   300
    SDL_zero( guid );
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   301
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   302
    return guid;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   303
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   304
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   305
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   306
{
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   307
    SDL_JoystickGUID guid;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7041
diff changeset
   308
    /* the GUID is just the first 16 chars of the name for now */
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   309
    const char *name = joystick->name;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   310
    SDL_zero( guid );
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   311
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   312
    return guid;
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   313
}
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   314
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   315
int
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   316
Android_OnPadDown(int padId, int keycode)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   317
{
7956
965b7e09c5a8 [Android] Fixes bug 2217, better joystick axes handling on Android.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7944
diff changeset
   318
    int button = keycode_to_SDL(keycode);
965b7e09c5a8 [Android] Fixes bug 2217, better joystick axes handling on Android.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7944
diff changeset
   319
    if (button >= 0) {
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   320
        if (SYS_Joysticks[padId]) {
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   321
            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   322
        }
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8008
diff changeset
   323
        return 0;
7956
965b7e09c5a8 [Android] Fixes bug 2217, better joystick axes handling on Android.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7944
diff changeset
   324
    }
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   325
    
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8008
diff changeset
   326
    return -1;
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   327
}
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
int
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   330
Android_OnPadUp(int padId, int keycode)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   331
{
7956
965b7e09c5a8 [Android] Fixes bug 2217, better joystick axes handling on Android.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7944
diff changeset
   332
    int button = keycode_to_SDL(keycode);
965b7e09c5a8 [Android] Fixes bug 2217, better joystick axes handling on Android.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7944
diff changeset
   333
    if (button >= 0) {
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   334
        if (SYS_Joysticks[padId]) {
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   335
            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   336
        }
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8008
diff changeset
   337
        return 0;
7956
965b7e09c5a8 [Android] Fixes bug 2217, better joystick axes handling on Android.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7944
diff changeset
   338
    }
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   339
    
8013
109299fe73ad [Android] Try to improve handling of DPAD|GAMEPAD + KEYBOARD devices
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8008
diff changeset
   340
    return -1;
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   341
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   342
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   343
int
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   344
Android_OnJoy(int joyId, int axis, float value)
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   345
{
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   346
    /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   347
    /* TODO: Are the reported values right? */
8024
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   348
    if (SYS_Joysticks[joyId]) {
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   349
        SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
ffbdb99af7bd [Android] Fixes #2264, handle joystick open/closed state properly
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8013
diff changeset
   350
    }
7907
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   351
    
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   352
    return 0;
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   353
}
24b4e98c6010 Adds Joystick support for Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7191
diff changeset
   354
7041
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   355
#endif /* SDL_JOYSTICK_ANDROID */
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   356
eb0b541c9722 Backed out changeset: a500a9dbfb41
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   357
/* vi: set ts=4 sw=4 expandtab: */