Skip to content

Commit d819106

Browse files
committed
Try matching gamepad mappings with CRC first
Fixes #13874
1 parent 98944ec commit d819106

File tree

2 files changed

+112
-18
lines changed

2 files changed

+112
-18
lines changed

src/joystick/SDL_gamepad.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,8 @@ static GamepadMapping_t *SDL_PrivateGetGamepadMappingForGUID(SDL_GUID guid, bool
13721372
{
13731373
GamepadMapping_t *mapping;
13741374

1375-
mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, true, adding_mapping);
1375+
// Try first with an exact match on version and CRC
1376+
mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, true, true);
13761377
if (mapping) {
13771378
return mapping;
13781379
}
@@ -1382,10 +1383,19 @@ static GamepadMapping_t *SDL_PrivateGetGamepadMappingForGUID(SDL_GUID guid, bool
13821383
return NULL;
13831384
}
13841385

1385-
// Try harder to get the best match, or create a mapping
1386+
// Try without CRC match
1387+
mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, true, false);
1388+
if (mapping) {
1389+
return mapping;
1390+
}
13861391

1392+
// Try without version match
13871393
if (SDL_JoystickGUIDUsesVersion(guid)) {
1388-
// Try again, ignoring the version
1394+
mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, false, true);
1395+
if (mapping) {
1396+
return mapping;
1397+
}
1398+
13891399
mapping = SDL_PrivateMatchGamepadMappingForGUID(guid, false, false);
13901400
if (mapping) {
13911401
return mapping;

test/testautomation_joystick.c

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,44 @@
99

1010
/* ================= Test Case Implementation ================== */
1111

12+
/* Fixture */
13+
14+
/* Create a 32-bit writable surface for blitting tests */
15+
static void SDLCALL joystickSetUp(void **arg)
16+
{
17+
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
18+
19+
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
20+
}
21+
22+
static void SDLCALL joystickTearDown(void *arg)
23+
{
24+
SDL_ResetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
25+
26+
SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
27+
}
28+
29+
1230
/* Test case functions */
1331

1432
/**
1533
* Check virtual joystick creation
1634
*
1735
* \sa SDL_AttachVirtualJoystick
1836
*/
19-
static int SDLCALL TestVirtualJoystick(void *arg)
37+
static int SDLCALL joystick_testVirtual(void *arg)
2038
{
2139
SDL_VirtualJoystickDesc desc;
2240
SDL_Joystick *joystick = NULL;
2341
SDL_Gamepad *gamepad = NULL;
2442
SDL_JoystickID device_id;
2543

26-
SDLTest_AssertCheck(SDL_InitSubSystem(SDL_INIT_GAMEPAD), "SDL_InitSubSystem(SDL_INIT_GAMEPAD)");
27-
28-
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
29-
3044
SDL_INIT_INTERFACE(&desc);
3145
desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
3246
desc.naxes = SDL_GAMEPAD_AXIS_COUNT;
3347
desc.nbuttons = SDL_GAMEPAD_BUTTON_COUNT;
3448
desc.vendor_id = USB_VENDOR_NVIDIA;
35-
desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
49+
desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103;
3650
desc.name = "Virtual NVIDIA SHIELD Controller";
3751
device_id = SDL_AttachVirtualJoystick(&desc);
3852
SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystick() -> %" SDL_PRIs32 " (expected > 0)", device_id);
@@ -111,7 +125,7 @@ static int SDLCALL TestVirtualJoystick(void *arg)
111125
}
112126

113127
/* Set an explicit mapping with a different name */
114-
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Gamepad,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
128+
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff008316550900001072000000007601,Virtual Gamepad,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
115129
{
116130
const char *name = SDL_GetGamepadName(gamepad);
117131
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad");
@@ -133,7 +147,7 @@ static int SDLCALL TestVirtualJoystick(void *arg)
133147
SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
134148

135149
/* Set an explicit mapping with legacy GameCube style buttons */
136-
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Nintendo GameCube,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,hint:SDL_GAMECONTROLLER_USE_GAMECUBE_LABELS:=1,");
150+
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff008316550900001072000000007601,Virtual Nintendo GameCube,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,hint:SDL_GAMECONTROLLER_USE_GAMECUBE_LABELS:=1,");
137151
{
138152
const char *name = SDL_GetGamepadName(gamepad);
139153
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Nintendo GameCube") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual Nintendo GameCube");
@@ -150,7 +164,7 @@ static int SDLCALL TestVirtualJoystick(void *arg)
150164
SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_WEST) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_WEST) == false");
151165

152166
/* Set an explicit mapping with legacy Nintendo style buttons */
153-
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual Nintendo Gamepad,a:b1,b:b0,x:b3,y:b2,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1,");
167+
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff008316550900001072000000007601,Virtual Nintendo Gamepad,a:b1,b:b0,x:b3,y:b2,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,hint:SDL_GAMECONTROLLER_USE_BUTTON_LABELS:=1,");
154168
{
155169
const char *name = SDL_GetGamepadName(gamepad);
156170
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Nintendo Gamepad") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual Nintendo Gamepad");
@@ -167,7 +181,7 @@ static int SDLCALL TestVirtualJoystick(void *arg)
167181
SDLTest_AssertCheck(SDL_GetGamepadButton(gamepad, SDL_GAMEPAD_BUTTON_SOUTH) == false, "SDL_GetGamepadButton(SDL_GAMEPAD_BUTTON_SOUTH) == false");
168182

169183
/* Set an explicit mapping with PS4 style buttons */
170-
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff0013db5669727475616c2043007601,Virtual PS4 Gamepad,type:ps4,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
184+
SDL_SetGamepadMapping(SDL_GetJoystickID(joystick), "ff008316550900001072000000007601,Virtual PS4 Gamepad,type:ps4,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
171185
{
172186
const char *name = SDL_GetGamepadName(gamepad);
173187
SDLTest_AssertCheck(SDL_strcmp(name, "Virtual PS4 Gamepad") == 0, "SDL_GetGamepadName() -> \"%s\" (expected \"%s\")", name, "Virtual PS4 Gamepad");
@@ -192,9 +206,75 @@ static int SDLCALL TestVirtualJoystick(void *arg)
192206
}
193207
SDLTest_AssertCheck(!SDL_IsJoystickVirtual(device_id), "!SDL_IsJoystickVirtual()");
194208

195-
SDL_ResetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS);
209+
return TEST_COMPLETED;
210+
}
196211

197-
SDL_QuitSubSystem(SDL_INIT_GAMEPAD);
212+
/**
213+
* Check gamepad mappings
214+
*/
215+
static int SDLCALL joystick_testMappings(void *arg)
216+
{
217+
SDL_VirtualJoystickDesc desc;
218+
SDL_Gamepad *gamepad = NULL;
219+
SDL_JoystickID device_id;
220+
221+
/* Add a mapping for the virtual controller in advance */
222+
SDL_AddGamepadMapping("ff000000550900001472000000007601,Virtual Gamepad,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
223+
224+
SDL_INIT_INTERFACE(&desc);
225+
desc.type = SDL_JOYSTICK_TYPE_GAMEPAD;
226+
desc.naxes = SDL_GAMEPAD_AXIS_COUNT;
227+
desc.nbuttons = SDL_GAMEPAD_BUTTON_COUNT;
228+
desc.vendor_id = USB_VENDOR_NVIDIA;
229+
desc.product_id = USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104;
230+
desc.name = "Virtual NVIDIA SHIELD Controller";
231+
device_id = SDL_AttachVirtualJoystick(&desc);
232+
SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystick() -> %" SDL_PRIs32 " (expected > 0)", device_id);
233+
SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_id), "SDL_IsJoystickVirtual()");
234+
235+
gamepad = SDL_OpenGamepad(device_id);
236+
SDLTest_AssertCheck(gamepad != NULL, "SDL_OpenGamepad() succeeded");
237+
if (gamepad) {
238+
/* Verify that the gamepad picked up the predefined mapping */
239+
{
240+
const char *name = SDL_GetGamepadName(gamepad);
241+
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad");
242+
}
243+
244+
/* Verify that the gamepad picks up a new mapping with no CRC */
245+
SDL_AddGamepadMapping("ff000000550900001472000000007601,Virtual Gamepad V2,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
246+
{
247+
const char *name = SDL_GetGamepadName(gamepad);
248+
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad V2") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad V2");
249+
}
250+
251+
/* Verify that the gamepad picks up a new mapping with valid CRC */
252+
SDL_AddGamepadMapping("ff008316550900001472000000007601,Virtual Gamepad V3,a:b0,b:b1,x:b2,y:b3,back:b4,guide:b5,start:b6,leftstick:b7,rightstick:b8,leftshoulder:b9,rightshoulder:b10,dpup:b11,dpdown:b12,dpleft:b13,dpright:b14,misc1:b15,paddle1:b16,paddle2:b17,paddle3:b18,paddle4:b19,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,");
253+
{
254+
const char *name = SDL_GetGamepadName(gamepad);
255+
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad V3") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad V3");
256+
}
257+
258+
SDL_CloseGamepad(gamepad);
259+
}
260+
SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id), "SDL_DetachVirtualJoystick()");
261+
262+
/* Try matching mappings with a new CRC */
263+
desc.name = "Virtual NVIDIA SHIELD Controller V2";
264+
device_id = SDL_AttachVirtualJoystick(&desc);
265+
SDLTest_AssertCheck(device_id > 0, "SDL_AttachVirtualJoystick() -> %" SDL_PRIs32 " (expected > 0)", device_id);
266+
SDLTest_AssertCheck(SDL_IsJoystickVirtual(device_id), "SDL_IsJoystickVirtual()");
267+
268+
gamepad = SDL_OpenGamepad(device_id);
269+
SDLTest_AssertCheck(gamepad != NULL, "SDL_OpenGamepad() succeeded");
270+
if (gamepad) {
271+
{
272+
const char *name = SDL_GetGamepadName(gamepad);
273+
SDLTest_AssertCheck(name && SDL_strcmp(name, "Virtual Gamepad V2") == 0, "SDL_GetGamepadName() ->\"%s\" (expected \"%s\")", name, "Virtual Gamepad V2");
274+
}
275+
SDL_CloseGamepad(gamepad);
276+
}
277+
SDLTest_AssertCheck(SDL_DetachVirtualJoystick(device_id), "SDL_DetachVirtualJoystick()");
198278

199279
return TEST_COMPLETED;
200280
}
@@ -203,19 +283,23 @@ static int SDLCALL TestVirtualJoystick(void *arg)
203283

204284
/* Joystick routine test cases */
205285
static const SDLTest_TestCaseReference joystickTest1 = {
206-
TestVirtualJoystick, "TestVirtualJoystick", "Test virtual joystick functionality", TEST_ENABLED
286+
joystick_testVirtual, "joystick_testVirtual", "Test virtual joystick functionality", TEST_ENABLED
287+
};
288+
static const SDLTest_TestCaseReference joystickTest2 = {
289+
joystick_testMappings, "joystick_testMappings", "Test gamepad mapping functionality", TEST_ENABLED
207290
};
208291

209292
/* Sequence of Joystick routine test cases */
210293
static const SDLTest_TestCaseReference *joystickTests[] = {
211294
&joystickTest1,
295+
&joystickTest2,
212296
NULL
213297
};
214298

215299
/* Joystick routine test suite (global) */
216300
SDLTest_TestSuiteReference joystickTestSuite = {
217301
"Joystick",
218-
NULL,
302+
joystickSetUp,
219303
joystickTests,
220-
NULL
304+
joystickTearDown
221305
};

0 commit comments

Comments
 (0)