src/joystick/android/SDL_sysjoystick.c
changeset 7944 f19c0aca56aa
parent 7930 5a1a7cdf3fa2
child 7956 965b7e09c5a8
     1.1 --- a/src/joystick/android/SDL_sysjoystick.c	Sun Nov 10 14:48:44 2013 -0800
     1.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Sun Nov 10 20:13:27 2013 -0300
     1.3 @@ -30,12 +30,35 @@
     1.4  #include "SDL_events.h"
     1.5  #include "SDL_joystick.h"
     1.6  #include "SDL_hints.h"
     1.7 +#include "SDL_assert.h"
     1.8  #include "../SDL_sysjoystick.h"
     1.9  #include "../SDL_joystick_c.h"
    1.10  #include "../../core/android/SDL_android.h"
    1.11 +#include "android/keycodes.h"
    1.12 +
    1.13 +/* As of platform android-14, android/keycodes.h is missing these defines */
    1.14 +#ifndef AKEYCODE_BUTTON_1
    1.15 +#define AKEYCODE_BUTTON_1 188
    1.16 +#define AKEYCODE_BUTTON_2 189
    1.17 +#define AKEYCODE_BUTTON_3 190
    1.18 +#define AKEYCODE_BUTTON_4 191
    1.19 +#define AKEYCODE_BUTTON_5 192
    1.20 +#define AKEYCODE_BUTTON_6 193
    1.21 +#define AKEYCODE_BUTTON_7 194
    1.22 +#define AKEYCODE_BUTTON_8 195
    1.23 +#define AKEYCODE_BUTTON_9 196
    1.24 +#define AKEYCODE_BUTTON_10 197
    1.25 +#define AKEYCODE_BUTTON_11 198
    1.26 +#define AKEYCODE_BUTTON_12 199
    1.27 +#define AKEYCODE_BUTTON_13 200
    1.28 +#define AKEYCODE_BUTTON_14 201
    1.29 +#define AKEYCODE_BUTTON_15 202
    1.30 +#define AKEYCODE_BUTTON_16 203
    1.31 +#endif
    1.32  
    1.33  #define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
    1.34  #define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
    1.35 +#define ANDROID_MAX_NBUTTONS 36
    1.36  
    1.37  static SDL_Joystick **SYS_Joysticks;
    1.38  static char **SYS_JoystickNames;
    1.39 @@ -44,31 +67,76 @@
    1.40  
    1.41  /* Function to convert Android keyCodes into SDL ones.
    1.42   * This code manipulation is done to get a sequential list of codes.
    1.43 + * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
    1.44   */
    1.45  static int
    1.46  keycode_to_SDL(int keycode)
    1.47  {
    1.48 -    /* D-Pad key codes (API 1):
    1.49 -     * KEYCODE_DPAD_UP=19, KEYCODE_DPAD_DOWN
    1.50 -     * KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_CENTER
    1.51 +    /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
    1.52 +    int button = 0;
    1.53 +    switch(keycode) 
    1.54 +    {
    1.55 +        /* D-Pad key codes (API 1), these get mapped to 0...4 */
    1.56 +        case AKEYCODE_DPAD_UP:
    1.57 +        case AKEYCODE_DPAD_DOWN:
    1.58 +        case AKEYCODE_DPAD_LEFT:
    1.59 +        case AKEYCODE_DPAD_RIGHT:
    1.60 +        case AKEYCODE_DPAD_CENTER:
    1.61 +            button = keycode - AKEYCODE_DPAD_UP;
    1.62 +            break;
    1.63 +        
    1.64 +        /* Some gamepad buttons (API 9), these get mapped to 5...19*/
    1.65 +        case AKEYCODE_BUTTON_A:
    1.66 +        case AKEYCODE_BUTTON_B:
    1.67 +        case AKEYCODE_BUTTON_C:
    1.68 +        case AKEYCODE_BUTTON_X:
    1.69 +        case AKEYCODE_BUTTON_Y:
    1.70 +        case AKEYCODE_BUTTON_Z:
    1.71 +        case AKEYCODE_BUTTON_L1:
    1.72 +        case AKEYCODE_BUTTON_L2:
    1.73 +        case AKEYCODE_BUTTON_R1:
    1.74 +        case AKEYCODE_BUTTON_R2:
    1.75 +        case AKEYCODE_BUTTON_THUMBL:
    1.76 +        case AKEYCODE_BUTTON_THUMBR:
    1.77 +        case AKEYCODE_BUTTON_START:
    1.78 +        case AKEYCODE_BUTTON_SELECT:
    1.79 +        case AKEYCODE_BUTTON_MODE:
    1.80 +            button = keycode - AKEYCODE_BUTTON_A + 5;
    1.81 +            break;
    1.82 +            
    1.83 +        
    1.84 +        /* More gamepad buttons (API 12), these get mapped to 20...35*/
    1.85 +        case AKEYCODE_BUTTON_1:
    1.86 +        case AKEYCODE_BUTTON_2:
    1.87 +        case AKEYCODE_BUTTON_3:
    1.88 +        case AKEYCODE_BUTTON_4:
    1.89 +        case AKEYCODE_BUTTON_5:
    1.90 +        case AKEYCODE_BUTTON_6:
    1.91 +        case AKEYCODE_BUTTON_7:
    1.92 +        case AKEYCODE_BUTTON_8:
    1.93 +        case AKEYCODE_BUTTON_9:
    1.94 +        case AKEYCODE_BUTTON_10:
    1.95 +        case AKEYCODE_BUTTON_11:
    1.96 +        case AKEYCODE_BUTTON_12:
    1.97 +        case AKEYCODE_BUTTON_13:
    1.98 +        case AKEYCODE_BUTTON_14:
    1.99 +        case AKEYCODE_BUTTON_15:
   1.100 +        case AKEYCODE_BUTTON_16:
   1.101 +            button = keycode - AKEYCODE_BUTTON_1 + 20;
   1.102 +            break;
   1.103 +            
   1.104 +        default:
   1.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);
   1.106 +            return -1;
   1.107 +            break;
   1.108 +    }
   1.109 +    
   1.110 +    /* This is here in case future generations, probably with six fingers per hand, 
   1.111 +     * happily add new cases up above and forget to update the max number of buttons. 
   1.112       */
   1.113 -    if(keycode < 96)
   1.114 -        return keycode-19;
   1.115 -    /* Some gamepad buttons (API 9):
   1.116 -     * KEYCODE_BUTTON_A=96, KEYCODE_BUTTON_B, KEYCODE_BUTTON_C,
   1.117 -     * KEYCODE_BUTTON_X, KEYCODE_BUTTON_Y, KEYCODE_BUTTON_Z,
   1.118 -     * KEYCODE_BUTTON_L1, KEYCODE_BUTTON_L2,
   1.119 -     * KEYCODE_BUTTON_R1, KEYCODE_BUTTON_R2,
   1.120 -     * KEYCODE_BUTTON_THUMBL, KEYCODE_BUTTON_THUMBR,
   1.121 -     * KEYCODE_BUTTON_START, KEYCODE_BUTTON_SELECT, KEYCODE_BUTTON_MODE
   1.122 -     */
   1.123 -    else if(keycode < 188)
   1.124 -        return keycode-91;
   1.125 -    /* More gamepad buttons (API 12):
   1.126 -     * KEYCODE_BUTTON_1=188 to KEYCODE_BUTTON_16
   1.127 -     */
   1.128 -    else
   1.129 -        return keycode-168;
   1.130 +    SDL_assert(button < ANDROID_MAX_NBUTTONS);
   1.131 +    return button;
   1.132 +    
   1.133  }
   1.134  
   1.135  /* Function to scan the system for joysticks.
   1.136 @@ -162,8 +230,8 @@
   1.137              joystick->nbuttons = 0;
   1.138              joystick->naxes = 3;
   1.139          } else {
   1.140 -            /* TODO: Get the real number of buttons in the device */
   1.141 -            joystick->nbuttons = 36;
   1.142 +            /* FIXME: Get the real number of buttons in the device? */
   1.143 +            joystick->nbuttons = ANDROID_MAX_NBUTTONS;
   1.144              joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
   1.145          }
   1.146