Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux: Improve gamepad mapping heuristic to accept Android conventions #7797

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 80 additions & 17 deletions src/joystick/linux/SDL_sysjoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,11 +1608,24 @@ static void LINUX_JoystickQuit(void)
/*
This is based on the Linux Gamepad Specification
available at: https://www.kernel.org/doc/html/v4.15/input/gamepad.html
and the Android gamepad documentation,
https://developer.android.com/develop/ui/views/touch-and-input/game-controllers/controller-input
*/
static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
{
SDL_Joystick *joystick;
SDL_joylist_item *item = GetJoystickByDevIndex(device_index);
enum {
MAPPED_TRIGGER_LEFT = 0x1,
MAPPED_TRIGGER_RIGHT = 0x2,
MAPPED_TRIGGER_BOTH = 0x3,

MAPPED_DPAD_UP = 0x1,
MAPPED_DPAD_DOWN = 0x2,
MAPPED_DPAD_LEFT = 0x4,
MAPPED_DPAD_RIGHT = 0x8,
MAPPED_DPAD_ALL = 0xF,
};
unsigned int mapped;

SDL_AssertJoysticksLocked();
Expand Down Expand Up @@ -1817,63 +1830,94 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
/* Prefer analog triggers, but settle for digital hat or buttons. */
mapped = 0;

/* Unfortunately there are several conventions for how analog triggers
* are represented as absolute axes:
*
* - Linux Gamepad Specification:
* LT = ABS_HAT2Y, RT = ABS_HAT2X
* - Android (and therefore many Bluetooth controllers):
* LT = ABS_BRAKE, RT = ABS_GAS
* - De facto standard for older Xbox and Playstation controllers:
* LT = ABS_Z, RT = ABS_RZ
*
* We try each one in turn. */
if (joystick->hwdata->has_abs[ABS_HAT2Y]) {
/* Linux Gamepad Specification */
out->lefttrigger.kind = EMappingKind_Axis;
out->lefttrigger.target = joystick->hwdata->abs_map[ABS_HAT2Y];
mapped |= 0x1;
mapped |= MAPPED_TRIGGER_LEFT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_HAT2Y)", out->lefttrigger.target);
#endif
} else if (joystick->hwdata->has_abs[ABS_BRAKE]) {
/* Android convention */
out->lefttrigger.kind = EMappingKind_Axis;
out->lefttrigger.target = joystick->hwdata->abs_map[ABS_BRAKE];
mapped |= MAPPED_TRIGGER_LEFT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_BRAKE)", out->lefttrigger.target);
#endif
} else if (joystick->hwdata->has_abs[ABS_Z]) {
/* De facto standard for Xbox 360 and Playstation gamepads */
out->lefttrigger.kind = EMappingKind_Axis;
out->lefttrigger.target = joystick->hwdata->abs_map[ABS_Z];
mapped |= 0x1;
mapped |= MAPPED_TRIGGER_LEFT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped LEFTTRIGGER to axis %d (ABS_Z)", out->lefttrigger.target);
#endif
}

if (joystick->hwdata->has_abs[ABS_HAT2X]) {
/* Linux Gamepad Specification */
out->righttrigger.kind = EMappingKind_Axis;
out->righttrigger.target = joystick->hwdata->abs_map[ABS_HAT2X];
mapped |= 0x2;
mapped |= MAPPED_TRIGGER_RIGHT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_HAT2X)", out->righttrigger.target);
#endif
} else if (joystick->hwdata->has_abs[ABS_GAS]) {
/* Android convention */
out->righttrigger.kind = EMappingKind_Axis;
out->righttrigger.target = joystick->hwdata->abs_map[ABS_GAS];
mapped |= MAPPED_TRIGGER_RIGHT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_GAS)", out->righttrigger.target);
#endif
} else if (joystick->hwdata->has_abs[ABS_RZ]) {
/* De facto standard for Xbox 360 and Playstation gamepads */
out->righttrigger.kind = EMappingKind_Axis;
out->righttrigger.target = joystick->hwdata->abs_map[ABS_RZ];
mapped |= 0x2;
mapped |= MAPPED_TRIGGER_RIGHT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped RIGHTTRIGGER to axis %d (ABS_RZ)", out->righttrigger.target);
#endif
}

if (mapped != 0x3 && joystick->hwdata->has_hat[2]) {
if (mapped != MAPPED_TRIGGER_BOTH && joystick->hwdata->has_hat[2]) {
int hat = joystick->hwdata->hats_indices[2] << 4;
out->lefttrigger.kind = EMappingKind_Hat;
out->righttrigger.kind = EMappingKind_Hat;
out->lefttrigger.target = hat | 0x4;
out->righttrigger.target = hat | 0x2;
mapped |= 0x3;
mapped |= MAPPED_TRIGGER_BOTH;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped LEFT+RIGHTTRIGGER to hat 2 (ABS_HAT2X, ABS_HAT2Y)");
#endif
}

if (!(mapped & 0x1) && joystick->hwdata->has_key[BTN_TL2]) {
if (!(mapped & MAPPED_TRIGGER_LEFT) && joystick->hwdata->has_key[BTN_TL2]) {
out->lefttrigger.kind = EMappingKind_Button;
out->lefttrigger.target = joystick->hwdata->key_map[BTN_TL2];
mapped |= 0x1;
mapped |= MAPPED_TRIGGER_LEFT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped LEFTTRIGGER to button %d (BTN_TL2)", out->lefttrigger.target);
#endif
}

if (!(mapped & 0x2) && joystick->hwdata->has_key[BTN_TR2]) {
if (!(mapped & MAPPED_TRIGGER_LEFT) && joystick->hwdata->has_key[BTN_TR2]) {
out->righttrigger.kind = EMappingKind_Button;
out->righttrigger.target = joystick->hwdata->key_map[BTN_TR2];
mapped |= 0x2;
mapped |= MAPPED_TRIGGER_RIGHT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped RIGHTTRIGGER to button %d (BTN_TR2)", out->righttrigger.target);
#endif
Expand All @@ -1885,7 +1929,7 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
if (joystick->hwdata->has_key[BTN_DPAD_UP]) {
out->dpup.kind = EMappingKind_Button;
out->dpup.target = joystick->hwdata->key_map[BTN_DPAD_UP];
mapped |= 0x1;
mapped |= MAPPED_DPAD_UP;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped DPUP to button %d (BTN_DPAD_UP)", out->dpup.target);
#endif
Expand All @@ -1894,7 +1938,7 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
if (joystick->hwdata->has_key[BTN_DPAD_DOWN]) {
out->dpdown.kind = EMappingKind_Button;
out->dpdown.target = joystick->hwdata->key_map[BTN_DPAD_DOWN];
mapped |= 0x2;
mapped |= MAPPED_DPAD_DOWN;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped DPDOWN to button %d (BTN_DPAD_DOWN)", out->dpdown.target);
#endif
Expand All @@ -1903,7 +1947,7 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
if (joystick->hwdata->has_key[BTN_DPAD_LEFT]) {
out->dpleft.kind = EMappingKind_Button;
out->dpleft.target = joystick->hwdata->key_map[BTN_DPAD_LEFT];
mapped |= 0x4;
mapped |= MAPPED_DPAD_LEFT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped DPLEFT to button %d (BTN_DPAD_LEFT)", out->dpleft.target);
#endif
Expand All @@ -1912,13 +1956,13 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
if (joystick->hwdata->has_key[BTN_DPAD_RIGHT]) {
out->dpright.kind = EMappingKind_Button;
out->dpright.target = joystick->hwdata->key_map[BTN_DPAD_RIGHT];
mapped |= 0x8;
mapped |= MAPPED_DPAD_RIGHT;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped DPRIGHT to button %d (BTN_DPAD_RIGHT)", out->dpright.target);
#endif
}

if (mapped != 0xF) {
if (mapped != MAPPED_DPAD_ALL) {
if (joystick->hwdata->has_hat[0]) {
int hat = joystick->hwdata->hats_indices[0] << 4;
out->dpleft.kind = EMappingKind_Hat;
Expand All @@ -1929,7 +1973,7 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
out->dpright.target = hat | 0x2;
out->dpup.target = hat | 0x1;
out->dpdown.target = hat | 0x4;
mapped |= 0xF;
mapped |= MAPPED_DPAD_ALL;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped DPUP+DOWN+LEFT+RIGHT to hat 0 (ABS_HAT0X, ABS_HAT0Y)");
#endif
Expand All @@ -1942,7 +1986,7 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
out->dpright.target = joystick->hwdata->abs_map[ABS_HAT0X];
out->dpup.target = joystick->hwdata->abs_map[ABS_HAT0Y];
out->dpdown.target = joystick->hwdata->abs_map[ABS_HAT0Y];
mapped |= 0xF;
mapped |= MAPPED_DPAD_ALL;
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped DPUP+DOWN to axis %d (ABS_HAT0Y)", out->dpup.target);
SDL_Log("Mapped DPLEFT+RIGHT to axis %d (ABS_HAT0X)", out->dpleft.target);
Expand All @@ -1961,14 +2005,33 @@ static SDL_bool LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMap
#endif
}

/* The Linux Gamepad Specification uses the RX and RY axes,
* originally intended to represent X and Y rotation, as a second
* joystick. This is common for USB gamepads, and also many Bluetooth
* gamepads, particularly older ones.
*
* The Android mapping convention used by many Bluetooth controllers
* instead uses the Z axis as a secondary X axis, and the RZ axis as
* a secondary Y axis. */
if (joystick->hwdata->has_abs[ABS_RX] && joystick->hwdata->has_abs[ABS_RY]) {
/* Linux Gamepad Specification, Xbox 360, Playstation etc. */
out->rightx.kind = EMappingKind_Axis;
out->righty.kind = EMappingKind_Axis;
out->rightx.target = joystick->hwdata->abs_map[ABS_RX];
out->righty.target = joystick->hwdata->abs_map[ABS_RY];
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped RIGHTX to axis %d (ABS_RX)", out->rightx.target);
SDL_Log("Mapped RIGHTY to axis %d (ABS_RY)", out->righty.target);
#endif
} else if (joystick->hwdata->has_abs[ABS_Z] && joystick->hwdata->has_abs[ABS_RZ]) {
/* Android convention */
out->rightx.kind = EMappingKind_Axis;
out->righty.kind = EMappingKind_Axis;
out->rightx.target = joystick->hwdata->abs_map[ABS_Z];
out->righty.target = joystick->hwdata->abs_map[ABS_RZ];
#ifdef DEBUG_GAMEPAD_MAPPING
SDL_Log("Mapped RIGHTX to axis %d (ABS_Z)", out->rightx.target);
SDL_Log("Mapped RIGHTY to axis %d (ABS_RZ)", out->righty.target);
#endif
}

Expand Down