changeset 2561:8dc8eb079584

Workaround for weird controller mapping bug on Windows
author Michael Pavone <pavone@retrodev.com>
date Sat, 25 Jan 2025 19:23:37 -0800
parents 6404643aca38
children 595719fe69f2
files controller_info.c
diffstat 1 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/controller_info.c	Tue Jan 21 21:34:19 2025 -0800
+++ b/controller_info.c	Sat Jan 25 19:23:37 2025 -0800
@@ -196,8 +196,15 @@
 	}
 	char *mapping = tern_find_ptr(val.ptrval, "mapping");
 	if (mapping) {
-		const char *parts[] = {key, ",", mapping};
+		const char *parts[] = {key, ",", mapping, ","};
+#if SDL_VERSION_ATLEAST(2,0,10)
+		//For reasons that are unclear, in some cases the last mapping element
+		//seems to get dropped unless there is a trailing comma
+		char * full = alloc_concat_m(4, parts);
+#else
+		//In SDL 2.0.9 and below, it is not legal to have a trailing comma
 		char * full = alloc_concat_m(3, parts);
+#endif
 		SDL_GameControllerAddMapping(full);
 		free(full);
 	}
@@ -242,8 +249,15 @@
 	existing = tern_insert_ptr(existing, "mapping", strdup(mapping_string));
 	info_config = tern_insert_node(info_config, guid_string, existing);
 	persist_config_at(config, info_config, "controller_types.cfg");
-	const char *parts[] = {guid_string, ",", mapping_string};
+	const char *parts[] = {guid_string, ",", mapping_string, ","};
+#if SDL_VERSION_ATLEAST(2,0,10)
+	//For reasons that are unclear, in some cases the last mapping element
+	//seems to get dropped unless there is a trailing comma
+	char * full = alloc_concat_m(4, parts);
+#else
+	//In SDL 2.0.9 and below, it is not legal to have a trailing comma
 	char * full = alloc_concat_m(3, parts);
+#endif
 	uint8_t gc_events = render_are_gamepad_events_enabled();
 	render_enable_gamepad_events(0);
 	SDL_GameControllerAddMapping(full);