Try matching game controller mappings on CRC and version and fall back to no CRC and no version, in that order. We do exact match when adding mappings, but loose matching everywhere else we look up a mapping for a GUID.
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 14fae84..70f3260 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -43,10 +43,16 @@
/* Many controllers turn the center button into an instantaneous button press */
#define SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS 250
-#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
-#define SDL_CONTROLLER_HINT_FIELD "hint:"
-#define SDL_CONTROLLER_SDKGE_FIELD "sdk>=:"
-#define SDL_CONTROLLER_SDKLE_FIELD "sdk<=:"
+#define SDL_CONTROLLER_CRC_FIELD "crc:"
+#define SDL_CONTROLLER_CRC_FIELD_SIZE 4 /* hard-coded for speed */
+#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
+#define SDL_CONTROLLER_PLATFORM_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD)
+#define SDL_CONTROLLER_HINT_FIELD "hint:"
+#define SDL_CONTROLLER_HINT_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_HINT_FIELD)
+#define SDL_CONTROLLER_SDKGE_FIELD "sdk>=:"
+#define SDL_CONTROLLER_SDKGE_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_SDKGE_FIELD)
+#define SDL_CONTROLLER_SDKLE_FIELD "sdk<=:"
+#define SDL_CONTROLLER_SDKLE_FIELD_SIZE SDL_strlen(SDL_CONTROLLER_SDKLE_FIELD)
/* a list of currently opened game controllers */
static SDL_GameController *SDL_gamecontrollers = NULL;
@@ -681,83 +687,101 @@ static ControllerMapping_t *SDL_CreateMappingForWGIController(SDL_JoystickGUID g
/*
* Helper function to scan the mappings database for a controller with the specified GUID
*/
-static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool exact_match)
+static ControllerMapping_t *SDL_PrivateMatchControllerMappingForGUID(SDL_JoystickGUID guid, SDL_bool match_crc, SDL_bool match_version)
{
ControllerMapping_t *mapping;
- Uint16 crc;
+ Uint16 crc = 0;
- SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
- if (crc) {
- /* Clear the CRC from the GUID for matching */
- SDL_SetJoystickGUIDCRC(&guid, 0);
+ if (match_crc) {
+ SDL_GetJoystickGUIDInfo(guid, NULL, NULL, NULL, &crc);
+ }
+
+ /* Clear the CRC from the GUID for matching, the mappings never include it in the GUID */
+ SDL_SetJoystickGUIDCRC(&guid, 0);
+
+ if (!match_version) {
+ SDL_SetJoystickGUIDVersion(&guid, 0);
}
+
for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
- if (SDL_memcmp(&guid, &mapping->guid, sizeof(guid)) == 0) {
- /* Check to see if the CRC matches */
- const char *crc_string = SDL_strstr(mapping->mapping, "crc:");
+ SDL_JoystickGUID mapping_guid;
+
+ SDL_memcpy(&mapping_guid, &mapping->guid, sizeof(mapping_guid));
+ if (!match_version) {
+ SDL_SetJoystickGUIDVersion(&mapping_guid, 0);
+ }
+
+ if (SDL_memcmp(&guid, &mapping_guid, sizeof(guid)) == 0) {
+ Uint16 mapping_crc = 0;
+ const char *crc_string = SDL_strstr(mapping->mapping, SDL_CONTROLLER_CRC_FIELD);
if (crc_string) {
- Uint16 mapping_crc = (Uint16)SDL_strtol(crc_string + 4, NULL, 16);
- if (crc != mapping_crc) {
- continue;
- }
+ mapping_crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16);
+ }
+ if (crc == mapping_crc) {
+ return mapping;
}
- return mapping;
}
}
+ return NULL;
+}
- if (!exact_match) {
- /* Try again, ignoring the version */
- Uint16 vendor, product;
-
- SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
- if (vendor && product) {
- SDL_JoystickGUID match_guid;
-
- SDL_memcpy(&match_guid, &guid, sizeof(guid));
- SDL_SetJoystickGUIDVersion(&match_guid, 0);
+/*
+ * Helper function to scan the mappings database for a controller with the specified GUID
+ */
+static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID guid)
+{
+ ControllerMapping_t *mapping;
+ Uint16 vendor, product, crc;
- for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
- SDL_JoystickGUID mapping_guid;
+ SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, &crc);
+ if (crc) {
+ /* First check for exact CRC matching */
+ mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, SDL_TRUE);
+ if (mapping) {
+ return mapping;
+ }
+ }
- SDL_memcpy(&mapping_guid, &mapping->guid, sizeof(mapping_guid));
- SDL_SetJoystickGUIDVersion(&mapping_guid, 0);
+ /* Now check for a mapping without CRC */
+ mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_TRUE);
+ if (mapping) {
+ return mapping;
+ }
- if (SDL_memcmp(&match_guid, &mapping_guid, sizeof(match_guid)) == 0) {
- /* Check to see if the CRC matches */
- const char *crc_string = SDL_strstr(mapping->mapping, "crc:");
- if (crc_string) {
- Uint16 mapping_crc = (Uint16)SDL_strtol(crc_string + 4, NULL, 16);
- if (crc != mapping_crc) {
- continue;
- }
- }
- return mapping;
- }
+ if (vendor && product) {
+ /* Try again, ignoring the version */
+ if (crc) {
+ mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_TRUE, SDL_FALSE);
+ if (mapping) {
+ return mapping;
}
}
+
+ mapping = SDL_PrivateMatchControllerMappingForGUID(guid, SDL_FALSE, SDL_FALSE);
+ if (mapping) {
+ return mapping;
+ }
}
- if (!exact_match) {
#if SDL_JOYSTICK_XINPUT
- if (SDL_IsJoystickXInput(guid)) {
- /* This is an XInput device */
- return s_pXInputMapping;
- }
+ if (SDL_IsJoystickXInput(guid)) {
+ /* This is an XInput device */
+ return s_pXInputMapping;
+ }
#endif
- if (!mapping) {
- if (SDL_IsJoystickHIDAPI(guid)) {
- mapping = SDL_CreateMappingForHIDAPIController(guid);
- } else if (SDL_IsJoystickRAWINPUT(guid)) {
- mapping = SDL_CreateMappingForRAWINPUTController(guid);
- } else if (SDL_IsJoystickWGI(guid)) {
- mapping = SDL_CreateMappingForWGIController(guid);
- } else if (SDL_IsJoystickVirtual(guid)) {
- /* We'll pick up a robust mapping in VIRTUAL_JoystickGetGamepadMapping */
+ if (!mapping) {
+ if (SDL_IsJoystickHIDAPI(guid)) {
+ mapping = SDL_CreateMappingForHIDAPIController(guid);
+ } else if (SDL_IsJoystickRAWINPUT(guid)) {
+ mapping = SDL_CreateMappingForRAWINPUTController(guid);
+ } else if (SDL_IsJoystickWGI(guid)) {
+ mapping = SDL_CreateMappingForWGIController(guid);
+ } else if (SDL_IsJoystickVirtual(guid)) {
+ /* We'll pick up a robust mapping in VIRTUAL_JoystickGetGamepadMapping */
#ifdef __ANDROID__
- } else {
- mapping = SDL_CreateMappingForAndroidController(guid);
+ } else {
+ mapping = SDL_CreateMappingForAndroidController(guid);
#endif
- }
}
}
return mapping;
@@ -1179,7 +1203,7 @@ SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString,
/* Make sure the mapping has the CRC */
char *new_mapping;
char *crc_end = "";
- char *crc_string = SDL_strstr(pchMapping, "crc:");
+ char *crc_string = SDL_strstr(pchMapping, SDL_CONTROLLER_CRC_FIELD);
if (crc_string) {
crc_end = SDL_strchr(crc_string, ',');
if (crc_end) {
@@ -1190,22 +1214,22 @@ SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString,
*crc_string = '\0';
}
- if (SDL_asprintf(&new_mapping, "%scrc:%.4x,%s", pchMapping, crc, crc_end) >= 0) {
+ if (SDL_asprintf(&new_mapping, "%s%s%.4x,%s", pchMapping, SDL_CONTROLLER_CRC_FIELD, crc, crc_end) >= 0) {
SDL_free(pchMapping);
pchMapping = new_mapping;
}
} else {
/* Make sure the GUID has the CRC, for matching purposes */
- char *crc_string = SDL_strstr(pchMapping, "crc:");
+ char *crc_string = SDL_strstr(pchMapping, SDL_CONTROLLER_CRC_FIELD);
if (crc_string) {
- crc = (Uint16)SDL_strtol(crc_string + 4, NULL, 16);
+ crc = (Uint16)SDL_strtol(crc_string + SDL_CONTROLLER_CRC_FIELD_SIZE, NULL, 16);
if (crc) {
SDL_SetJoystickGUIDCRC(&jGUID, crc);
}
}
}
- pControllerMapping = SDL_PrivateGetControllerMappingForGUID(jGUID, SDL_TRUE);
+ pControllerMapping = SDL_PrivateMatchControllerMappingForGUID(jGUID, SDL_TRUE, SDL_TRUE);
if (pControllerMapping) {
/* Only overwrite the mapping if the priority is the same or higher. */
if (pControllerMapping->priority <= priority) {
@@ -1265,7 +1289,7 @@ static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const
{
ControllerMapping_t *mapping;
- mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_FALSE);
+ mapping = SDL_PrivateGetControllerMappingForGUID(guid);
#ifdef __LINUX__
if (!mapping && name) {
if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
@@ -1456,7 +1480,7 @@ SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
/* Extract and verify the platform */
tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
if (tmp != NULL) {
- tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
+ tmp += SDL_CONTROLLER_PLATFORM_FIELD_SIZE;
comma = SDL_strchr(tmp, ',');
if (comma != NULL) {
platform_len = comma - tmp + 1;
@@ -1503,7 +1527,7 @@ SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMap
int len;
char hint[128];
- tmp += SDL_strlen(SDL_CONTROLLER_HINT_FIELD);
+ tmp += SDL_CONTROLLER_HINT_FIELD_SIZE;
if (*tmp == '!') {
negate = SDL_TRUE;
@@ -1541,14 +1565,14 @@ SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMap
tmp = SDL_strstr(mappingString, SDL_CONTROLLER_SDKGE_FIELD);
if (tmp != NULL) {
- tmp += SDL_strlen(SDL_CONTROLLER_SDKGE_FIELD);
+ tmp += SDL_CONTROLLER_SDKGE_FIELD_SIZE;
if (!(SDL_GetAndroidSDKVersion() >= SDL_atoi(tmp))) {
return SDL_SetError("SDK version %d < minimum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
}
}
tmp = SDL_strstr(mappingString, SDL_CONTROLLER_SDKLE_FIELD);
if (tmp != NULL) {
- tmp += SDL_strlen(SDL_CONTROLLER_SDKLE_FIELD);
+ tmp += SDL_CONTROLLER_SDKLE_FIELD_SIZE;
if (!(SDL_GetAndroidSDKVersion() <= SDL_atoi(tmp))) {
return SDL_SetError("SDK version %d > maximum version %d", SDL_GetAndroidSDKVersion(), SDL_atoi(tmp));
}
@@ -1633,7 +1657,7 @@ CreateMappingString(ControllerMapping_t *mapping, SDL_JoystickGUID guid)
if (mapping->mapping[SDL_strlen(mapping->mapping) - 1] != ',') {
needed += 1;
}
- needed += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD) + SDL_strlen(platform);
+ needed += SDL_CONTROLLER_PLATFORM_FIELD_SIZE + SDL_strlen(platform);
}
pMappingString = SDL_malloc(needed);
@@ -1690,7 +1714,7 @@ SDL_GameControllerMappingForIndex(int mapping_index)
char *
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
{
- ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid, SDL_FALSE);
+ ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(guid);
if (mapping) {
return CreateMappingString(mapping, guid);
} else {