Sort the raw input axes by usage, so X comes before Y, etc.
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
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 1162a37..0069860 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -616,7 +616,7 @@ static ControllerMapping_t *SDL_CreateMappingForRAWINPUTController(SDL_JoystickG
char mapping_string[1024];
SDL_strlcpy(mapping_string, "none,*,", sizeof(mapping_string));
- SDL_strlcat(mapping_string, "a:b0,b:b1,x:b2,y:b3,back:b6,guide:b10,start:b7,leftstick:b8,rightstick:b9,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a1,lefty:a0,rightx:a3,righty:a2,lefttrigger:a4,righttrigger:a5,", sizeof(mapping_string));
+ SDL_strlcat(mapping_string, "a:b0,b:b1,x:b2,y:b3,back:b6,guide:b10,start:b7,leftstick:b8,rightstick:b9,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,", sizeof(mapping_string));
return SDL_PrivateAddMappingForGUID(guid, mapping_string,
&existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index 2d10f2c..2f97826 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -1033,6 +1033,16 @@ RAWINPUT_JoystickGetDeviceInstanceID(int device_index)
}
static int
+RAWINPUT_SortValueCaps(const void *A, const void *B)
+{
+ HIDP_VALUE_CAPS *capsA = (HIDP_VALUE_CAPS *)A;
+ HIDP_VALUE_CAPS *capsB = (HIDP_VALUE_CAPS *)B;
+
+ /* Sort by Usage for single values, or UsageMax for range of values */
+ return (int)capsA->NotRange.Usage - capsB->NotRange.Usage;
+}
+
+static int
RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
SDL_RAWINPUT_Device *device = RAWINPUT_GetDeviceByIndex(device_index);
@@ -1092,6 +1102,9 @@ RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
return SDL_SetError("Couldn't get device value capabilities");
}
+ /* Sort the axes by usage, so X comes before Y, etc. */
+ SDL_qsort(value_caps, caps.NumberInputValueCaps, sizeof(*value_caps), RAWINPUT_SortValueCaps);
+
for (i = 0; i < caps.NumberInputButtonCaps; ++i) {
HIDP_BUTTON_CAPS *cap = &button_caps[i];
@@ -1360,20 +1373,12 @@ RAWINPUT_HandleStatePacket(SDL_Joystick *joystick, Uint8 *data, int size)
(1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT),
(1 << SDL_CONTROLLER_BUTTON_DPAD_UP) | (1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT),
};
-#ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
- static const int axis_map[] = {
- SDL_CONTROLLER_AXIS_LEFTY,
- SDL_CONTROLLER_AXIS_LEFTX,
- SDL_CONTROLLER_AXIS_RIGHTY,
- SDL_CONTROLLER_AXIS_RIGHTX
- };
-#endif
Uint32 match_state = ctx->match_state;
/* Update match_state with button bit, then fall through */
#define SDL_PrivateJoystickButton(joystick, button, state) if (button < SDL_arraysize(button_map)) { if (state) match_state |= 1 << button_map[button]; else match_state &= ~(1 << button_map[button]); } SDL_PrivateJoystickButton(joystick, button, state)
#ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
/* Grab high 4 bits of value, then fall through */
-#define SDL_PrivateJoystickAxis(joystick, axis, value) if (axis < SDL_arraysize(axis_map)) match_state = (match_state & ~(0xF << (4 * axis_map[axis] + 16))) | ((value) & 0xF000) << (4 * axis_map[axis] + 4); SDL_PrivateJoystickAxis(joystick, axis, value)
+#define SDL_PrivateJoystickAxis(joystick, axis, value) if (axis < 4) match_state = (match_state & ~(0xF << (4 * axis + 16))) | ((value) & 0xF000) << (4 * axis + 4); SDL_PrivateJoystickAxis(joystick, axis, value)
#endif
#endif /* SDL_JOYSTICK_RAWINPUT_MATCHING */