Fixed bug 4921 - Do not swap B/X buttons on GameCube controller unless it's requested Ethan Lee Basically replicating the solution of the Switch Controller's button label issue. Physical layout should take priority unless it's explicitly requested by the user or application!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
diff --git a/src/joystick/hidapi/SDL_hidapi_gamecube.c b/src/joystick/hidapi/SDL_hidapi_gamecube.c
index 4800141..6cd6659 100644
--- a/src/joystick/hidapi/SDL_hidapi_gamecube.c
+++ b/src/joystick/hidapi/SDL_hidapi_gamecube.c
@@ -29,6 +29,7 @@
#include "SDL_haptic.h"
#include "SDL_joystick.h"
#include "SDL_gamecontroller.h"
+#include "../../SDL_hints_c.h"
#include "../SDL_sysjoystick.h"
#include "SDL_hidapijoystick_c.h"
#include "SDL_hidapi_rumble.h"
@@ -47,6 +48,7 @@ typedef struct {
Uint8 rumble[1+MAX_CONTROLLERS];
/* Without this variable, hid_write starts to lag a TON */
SDL_bool rumbleUpdate;
+ SDL_bool m_bUseButtonLabels;
} SDL_DriverGameCube_Context;
static SDL_bool
@@ -95,6 +97,27 @@ static float RemapVal(float val, float A, float B, float C, float D)
return C + (D - C) * (val - A) / (B - A);
}
+static void SDLCALL SDL_GameControllerButtonReportingHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
+{
+ SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)userdata;
+ ctx->m_bUseButtonLabels = SDL_GetStringBoolean(hint, SDL_TRUE);
+}
+
+static Uint8 RemapButton(SDL_DriverGameCube_Context *ctx, Uint8 button)
+{
+ if (ctx->m_bUseButtonLabels) {
+ switch (button) {
+ case SDL_CONTROLLER_BUTTON_B:
+ return SDL_CONTROLLER_BUTTON_X;
+ case SDL_CONTROLLER_BUTTON_X:
+ return SDL_CONTROLLER_BUTTON_B;
+ default:
+ break;
+ }
+ }
+ return button;
+}
+
static SDL_bool
HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
{
@@ -164,6 +187,9 @@ HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
}
}
+ SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
+ SDL_GameControllerButtonReportingHintChanged, ctx);
+
return SDL_TRUE;
error:
@@ -244,12 +270,12 @@ HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)
#define READ_BUTTON(off, flag, button) \
SDL_PrivateJoystickButton( \
joystick, \
- button, \
+ RemapButton(ctx, button), \
(curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
);
READ_BUTTON(1, 0x01, 0) /* A */
- READ_BUTTON(1, 0x04, 1) /* B */
- READ_BUTTON(1, 0x02, 2) /* X */
+ READ_BUTTON(1, 0x02, 1) /* B */
+ READ_BUTTON(1, 0x04, 2) /* X */
READ_BUTTON(1, 0x08, 3) /* Y */
READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
@@ -352,9 +378,14 @@ HIDAPI_DriverGameCube_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
static void
HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)
{
+ SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
+
hid_close(device->dev);
device->dev = NULL;
+ SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
+ SDL_GameControllerButtonReportingHintChanged, ctx);
+
SDL_free(device->context);
device->context = NULL;
}