Skip to content

Commit

Permalink
Added controller mapping for Android TV remotes
Browse files Browse the repository at this point in the history
Also fixed the back button on the remote exiting the application
  • Loading branch information
slouken committed Nov 1, 2017
1 parent 4478707 commit a90be44
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Expand Up @@ -88,6 +88,8 @@ public static boolean isDeviceSDLJoystick(int deviceId) {
}
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.");
}
Expand All @@ -97,6 +99,7 @@ public static boolean isDeviceSDLJoystick(int deviceId) {
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) ||
Expand Down
9 changes: 9 additions & 0 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/joystick/SDL_sysjoystick.h
Expand Up @@ -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: */
13 changes: 12 additions & 1 deletion src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -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*/
Expand Down Expand Up @@ -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: */

0 comments on commit a90be44

Please sign in to comment.