Added controller mapping for Android TV remotes
authorSam Lantinga <slouken@libsdl.org>
Wed, 01 Nov 2017 10:06:58 -0700
changeset 116630d96acbd34f0
parent 11662 a996f135cc81
child 11664 c677bf234773
Added controller mapping for Android TV remotes
Also fixed the back button on the remote exiting the application
android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java
src/joystick/SDL_gamecontroller.c
src/joystick/SDL_sysjoystick.h
src/joystick/android/SDL_sysjoystick.c
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java	Tue Oct 31 13:49:59 2017 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java	Wed Nov 01 10:06:58 2017 -0700
     1.3 @@ -88,6 +88,8 @@
     1.4          }
     1.5          int sources = device.getSources();
     1.6  
     1.7 +        /* This is called for every button press, so let's not spam the logs */
     1.8 +        /**
     1.9          if ((sources & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK) {
    1.10              Log.v(TAG, "Input device " + device.getName() + " is a joystick.");
    1.11          }
    1.12 @@ -97,6 +99,7 @@
    1.13          if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
    1.14              Log.v(TAG, "Input device " + device.getName() + " is a gamepad.");
    1.15          }
    1.16 +        **/
    1.17  
    1.18          return (((sources & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK) ||
    1.19                  ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) ||
     2.1 --- a/src/joystick/SDL_gamecontroller.c	Tue Oct 31 13:49:59 2017 -0700
     2.2 +++ b/src/joystick/SDL_gamecontroller.c	Wed Nov 01 10:06:58 2017 -0700
     2.3 @@ -919,6 +919,15 @@
     2.4          mapping = s_pXInputMapping;
     2.5      }
     2.6  #endif
     2.7 +#if defined(__ANDROID__)
     2.8 +    if (!mapping && SDL_SYS_IsDPAD_DeviceIndex(device_index)) {
     2.9 +        SDL_bool existing;
    2.10 +        char mapping_string[1024];
    2.11 +        SDL_snprintf(mapping_string, sizeof(mapping_string), "none,%s,a:b0,b:b1,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,", name);
    2.12 +        mapping = SDL_PrivateAddMappingForGUID(guid, mapping_string,
    2.13 +                          &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
    2.14 +#endif /* __ANDROID__ */
    2.15 +    }
    2.16      SDL_UnlockJoysticks();
    2.17      return mapping;
    2.18  }
     3.1 --- a/src/joystick/SDL_sysjoystick.h	Tue Oct 31 13:49:59 2017 -0700
     3.2 +++ b/src/joystick/SDL_sysjoystick.h	Wed Nov 01 10:06:58 2017 -0700
     3.3 @@ -126,6 +126,11 @@
     3.4  extern SDL_bool SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index);
     3.5  #endif
     3.6  
     3.7 +#if defined(__ANDROID__)
     3.8 +/* Function returns SDL_TRUE if this device is a DPAD (maybe a TV remote) */
     3.9 +extern SDL_bool SDL_SYS_IsDPAD_DeviceIndex(int device_index);
    3.10 +#endif
    3.11 +
    3.12  #endif /* SDL_sysjoystick_h_ */
    3.13  
    3.14  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/joystick/android/SDL_sysjoystick.c	Tue Oct 31 13:49:59 2017 -0700
     4.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Wed Nov 01 10:06:58 2017 -0700
     4.3 @@ -143,7 +143,13 @@
     4.4              button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
     4.5              break;
     4.6          case AKEYCODE_DPAD_CENTER:
     4.7 -            button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
     4.8 +            /* This is handled better by applications as the A button */
     4.9 +            /*button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
    4.10 +            button = SDL_CONTROLLER_BUTTON_A;
    4.11 +            break;
    4.12 +
    4.13 +        case AKEYCODE_BACK:
    4.14 +            button = SDL_CONTROLLER_BUTTON_B;
    4.15              break;
    4.16  
    4.17          /* More gamepad buttons (API 12), these get mapped to 20...35*/
    4.18 @@ -640,6 +646,11 @@
    4.19      return guid;
    4.20  }
    4.21  
    4.22 +SDL_bool SDL_SYS_IsDPAD_DeviceIndex(int device_index)
    4.23 +{
    4.24 +    return JoystickByDevIndex(device_index)->naxes == 0;
    4.25 +}
    4.26 +
    4.27  #endif /* SDL_JOYSTICK_ANDROID */
    4.28  
    4.29  /* vi: set ts=4 sw=4 expandtab: */