From a90be440e8bc743a24ffc9e5d246d30bfb9e9bdc Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 1 Nov 2017 10:06:58 -0700 Subject: [PATCH] Added controller mapping for Android TV remotes Also fixed the back button on the remote exiting the application --- .../java/org/libsdl/app/SDLControllerManager.java | 3 +++ src/joystick/SDL_gamecontroller.c | 9 +++++++++ src/joystick/SDL_sysjoystick.h | 5 +++++ src/joystick/android/SDL_sysjoystick.c | 13 ++++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java index 4b1473fb0..7b82c0e20 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLControllerManager.java @@ -88,6 +88,8 @@ public class SDLControllerManager } int sources = device.getSources(); + /* This is called for every button press, so let's not spam the logs */ + /** if ((sources & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK) { Log.v(TAG, "Input device " + device.getName() + " is a joystick."); } @@ -97,6 +99,7 @@ public class SDLControllerManager if ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) { Log.v(TAG, "Input device " + device.getName() + " is a gamepad."); } + **/ return (((sources & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK) || ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) || diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c index 13953de51..6f16a26dd 100644 --- a/src/joystick/SDL_gamecontroller.c +++ b/src/joystick/SDL_gamecontroller.c @@ -919,6 +919,15 @@ static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index) mapping = s_pXInputMapping; } #endif +#if defined(__ANDROID__) + if (!mapping && SDL_SYS_IsDPAD_DeviceIndex(device_index)) { + SDL_bool existing; + char mapping_string[1024]; + SDL_snprintf(mapping_string, sizeof(mapping_string), "none,%s,a:b0,b:b1,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,", name); + mapping = SDL_PrivateAddMappingForGUID(guid, mapping_string, + &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT); +#endif /* __ANDROID__ */ + } SDL_UnlockJoysticks(); return mapping; } diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h index e761fd256..be136cbd5 100644 --- a/src/joystick/SDL_sysjoystick.h +++ b/src/joystick/SDL_sysjoystick.h @@ -126,6 +126,11 @@ extern SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick); extern SDL_bool SDL_SYS_IsXInputGamepad_DeviceIndex(int device_index); #endif +#if defined(__ANDROID__) +/* Function returns SDL_TRUE if this device is a DPAD (maybe a TV remote) */ +extern SDL_bool SDL_SYS_IsDPAD_DeviceIndex(int device_index); +#endif + #endif /* SDL_sysjoystick_h_ */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c index 05f1bcde1..45552737d 100644 --- a/src/joystick/android/SDL_sysjoystick.c +++ b/src/joystick/android/SDL_sysjoystick.c @@ -143,7 +143,13 @@ keycode_to_SDL(int keycode) button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT; break; case AKEYCODE_DPAD_CENTER: - button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */ + /* This is handled better by applications as the A button */ + /*button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */ + button = SDL_CONTROLLER_BUTTON_A; + break; + + case AKEYCODE_BACK: + button = SDL_CONTROLLER_BUTTON_B; break; /* More gamepad buttons (API 12), these get mapped to 20...35*/ @@ -640,6 +646,11 @@ SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick) return guid; } +SDL_bool SDL_SYS_IsDPAD_DeviceIndex(int device_index) +{ + return JoystickByDevIndex(device_index)->naxes == 0; +} + #endif /* SDL_JOYSTICK_ANDROID */ /* vi: set ts=4 sw=4 expandtab: */