Fixes bugs #2213 and #2214, improves the Android joystick code button handling
authorGabriel Jacobo <gabomdq@gmail.com>
Sun, 10 Nov 2013 20:13:27 -0300
changeset 7944f19c0aca56aa
parent 7943 713c6a333c33
child 7945 764129077d18
Fixes bugs #2213 and #2214, improves the Android joystick code button handling
README-android.txt
src/joystick/android/SDL_sysjoystick.c
     1.1 --- a/README-android.txt	Sun Nov 10 14:48:44 2013 -0800
     1.2 +++ b/README-android.txt	Sun Nov 10 20:13:27 2013 -0300
     1.3 @@ -433,4 +433,6 @@
     1.4   Known issues
     1.5  ================================================================================
     1.6  
     1.7 -- TODO. I'm sure there's a bunch more stuff I haven't thought of 
     1.8 +- The number of buttons reported for each joystick is hardcoded to be 36, which
     1.9 +is the current maximum number of buttons Android can report.
    1.10 +
     2.1 --- a/src/joystick/android/SDL_sysjoystick.c	Sun Nov 10 14:48:44 2013 -0800
     2.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Sun Nov 10 20:13:27 2013 -0300
     2.3 @@ -30,12 +30,35 @@
     2.4  #include "SDL_events.h"
     2.5  #include "SDL_joystick.h"
     2.6  #include "SDL_hints.h"
     2.7 +#include "SDL_assert.h"
     2.8  #include "../SDL_sysjoystick.h"
     2.9  #include "../SDL_joystick_c.h"
    2.10  #include "../../core/android/SDL_android.h"
    2.11 +#include "android/keycodes.h"
    2.12 +
    2.13 +/* As of platform android-14, android/keycodes.h is missing these defines */
    2.14 +#ifndef AKEYCODE_BUTTON_1
    2.15 +#define AKEYCODE_BUTTON_1 188
    2.16 +#define AKEYCODE_BUTTON_2 189
    2.17 +#define AKEYCODE_BUTTON_3 190
    2.18 +#define AKEYCODE_BUTTON_4 191
    2.19 +#define AKEYCODE_BUTTON_5 192
    2.20 +#define AKEYCODE_BUTTON_6 193
    2.21 +#define AKEYCODE_BUTTON_7 194
    2.22 +#define AKEYCODE_BUTTON_8 195
    2.23 +#define AKEYCODE_BUTTON_9 196
    2.24 +#define AKEYCODE_BUTTON_10 197
    2.25 +#define AKEYCODE_BUTTON_11 198
    2.26 +#define AKEYCODE_BUTTON_12 199
    2.27 +#define AKEYCODE_BUTTON_13 200
    2.28 +#define AKEYCODE_BUTTON_14 201
    2.29 +#define AKEYCODE_BUTTON_15 202
    2.30 +#define AKEYCODE_BUTTON_16 203
    2.31 +#endif
    2.32  
    2.33  #define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
    2.34  #define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
    2.35 +#define ANDROID_MAX_NBUTTONS 36
    2.36  
    2.37  static SDL_Joystick **SYS_Joysticks;
    2.38  static char **SYS_JoystickNames;
    2.39 @@ -44,31 +67,76 @@
    2.40  
    2.41  /* Function to convert Android keyCodes into SDL ones.
    2.42   * This code manipulation is done to get a sequential list of codes.
    2.43 + * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
    2.44   */
    2.45  static int
    2.46  keycode_to_SDL(int keycode)
    2.47  {
    2.48 -    /* D-Pad key codes (API 1):
    2.49 -     * KEYCODE_DPAD_UP=19, KEYCODE_DPAD_DOWN
    2.50 -     * KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_CENTER
    2.51 +    /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
    2.52 +    int button = 0;
    2.53 +    switch(keycode) 
    2.54 +    {
    2.55 +        /* D-Pad key codes (API 1), these get mapped to 0...4 */
    2.56 +        case AKEYCODE_DPAD_UP:
    2.57 +        case AKEYCODE_DPAD_DOWN:
    2.58 +        case AKEYCODE_DPAD_LEFT:
    2.59 +        case AKEYCODE_DPAD_RIGHT:
    2.60 +        case AKEYCODE_DPAD_CENTER:
    2.61 +            button = keycode - AKEYCODE_DPAD_UP;
    2.62 +            break;
    2.63 +        
    2.64 +        /* Some gamepad buttons (API 9), these get mapped to 5...19*/
    2.65 +        case AKEYCODE_BUTTON_A:
    2.66 +        case AKEYCODE_BUTTON_B:
    2.67 +        case AKEYCODE_BUTTON_C:
    2.68 +        case AKEYCODE_BUTTON_X:
    2.69 +        case AKEYCODE_BUTTON_Y:
    2.70 +        case AKEYCODE_BUTTON_Z:
    2.71 +        case AKEYCODE_BUTTON_L1:
    2.72 +        case AKEYCODE_BUTTON_L2:
    2.73 +        case AKEYCODE_BUTTON_R1:
    2.74 +        case AKEYCODE_BUTTON_R2:
    2.75 +        case AKEYCODE_BUTTON_THUMBL:
    2.76 +        case AKEYCODE_BUTTON_THUMBR:
    2.77 +        case AKEYCODE_BUTTON_START:
    2.78 +        case AKEYCODE_BUTTON_SELECT:
    2.79 +        case AKEYCODE_BUTTON_MODE:
    2.80 +            button = keycode - AKEYCODE_BUTTON_A + 5;
    2.81 +            break;
    2.82 +            
    2.83 +        
    2.84 +        /* More gamepad buttons (API 12), these get mapped to 20...35*/
    2.85 +        case AKEYCODE_BUTTON_1:
    2.86 +        case AKEYCODE_BUTTON_2:
    2.87 +        case AKEYCODE_BUTTON_3:
    2.88 +        case AKEYCODE_BUTTON_4:
    2.89 +        case AKEYCODE_BUTTON_5:
    2.90 +        case AKEYCODE_BUTTON_6:
    2.91 +        case AKEYCODE_BUTTON_7:
    2.92 +        case AKEYCODE_BUTTON_8:
    2.93 +        case AKEYCODE_BUTTON_9:
    2.94 +        case AKEYCODE_BUTTON_10:
    2.95 +        case AKEYCODE_BUTTON_11:
    2.96 +        case AKEYCODE_BUTTON_12:
    2.97 +        case AKEYCODE_BUTTON_13:
    2.98 +        case AKEYCODE_BUTTON_14:
    2.99 +        case AKEYCODE_BUTTON_15:
   2.100 +        case AKEYCODE_BUTTON_16:
   2.101 +            button = keycode - AKEYCODE_BUTTON_1 + 20;
   2.102 +            break;
   2.103 +            
   2.104 +        default:
   2.105 +            SDL_Log("The button you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> Android KeyCode %d", keycode);
   2.106 +            return -1;
   2.107 +            break;
   2.108 +    }
   2.109 +    
   2.110 +    /* This is here in case future generations, probably with six fingers per hand, 
   2.111 +     * happily add new cases up above and forget to update the max number of buttons. 
   2.112       */
   2.113 -    if(keycode < 96)
   2.114 -        return keycode-19;
   2.115 -    /* Some gamepad buttons (API 9):
   2.116 -     * KEYCODE_BUTTON_A=96, KEYCODE_BUTTON_B, KEYCODE_BUTTON_C,
   2.117 -     * KEYCODE_BUTTON_X, KEYCODE_BUTTON_Y, KEYCODE_BUTTON_Z,
   2.118 -     * KEYCODE_BUTTON_L1, KEYCODE_BUTTON_L2,
   2.119 -     * KEYCODE_BUTTON_R1, KEYCODE_BUTTON_R2,
   2.120 -     * KEYCODE_BUTTON_THUMBL, KEYCODE_BUTTON_THUMBR,
   2.121 -     * KEYCODE_BUTTON_START, KEYCODE_BUTTON_SELECT, KEYCODE_BUTTON_MODE
   2.122 -     */
   2.123 -    else if(keycode < 188)
   2.124 -        return keycode-91;
   2.125 -    /* More gamepad buttons (API 12):
   2.126 -     * KEYCODE_BUTTON_1=188 to KEYCODE_BUTTON_16
   2.127 -     */
   2.128 -    else
   2.129 -        return keycode-168;
   2.130 +    SDL_assert(button < ANDROID_MAX_NBUTTONS);
   2.131 +    return button;
   2.132 +    
   2.133  }
   2.134  
   2.135  /* Function to scan the system for joysticks.
   2.136 @@ -162,8 +230,8 @@
   2.137              joystick->nbuttons = 0;
   2.138              joystick->naxes = 3;
   2.139          } else {
   2.140 -            /* TODO: Get the real number of buttons in the device */
   2.141 -            joystick->nbuttons = 36;
   2.142 +            /* FIXME: Get the real number of buttons in the device? */
   2.143 +            joystick->nbuttons = ANDROID_MAX_NBUTTONS;
   2.144              joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
   2.145          }
   2.146