changeset 1603:c0727712d529

Read extral SDL2 mappings on startup from controller_types.cfg
author Michael Pavone <pavone@retrodev.com>
date Tue, 31 Jul 2018 23:08:52 -0700
parents b452887f85b4
children 68b05322d971
files controller_info.c controller_info.h render_sdl.c
diffstat 3 files changed, 28 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/controller_info.c	Tue Jul 31 22:19:56 2018 -0700
+++ b/controller_info.c	Tue Jul 31 23:08:52 2018 -0700
@@ -45,12 +45,18 @@
 	"6b bumpers",
 	"6b right"
 };
-controller_info get_controller_info(int joystick)
+
+static void load_ctype_config(void)
 {
 	if (!loaded) {
 		info_config = load_overrideable_config("controller_types.cfg", "controller_types.cfg");
 		loaded = 1;
 	}
+}
+
+controller_info get_controller_info(int joystick)
+{
+	load_ctype_config();
 	char guid_string[33];
 	SDL_Joystick *stick = render_get_joystick(joystick);
 	SDL_GameController *control = render_get_controller(joystick);
@@ -137,6 +143,23 @@
 	};
 }
 
+static void mappings_iter(char *key, tern_val val, uint8_t valtype, void *data)
+{
+	if (valtype != TVAL_NODE) {
+		return;
+	}
+	char *mapping = tern_find_ptr(val.ptrval, "mapping");
+	if (mapping) {
+		SDL_GameControllerAddMapping(mapping);
+	}
+}
+
+void controller_add_mappings(void)
+{
+	load_ctype_config();
+	tern_foreach(info_config, mappings_iter, NULL);
+}
+
 void save_controller_info(int joystick, controller_info *info)
 {
 	char guid_string[33];
--- a/controller_info.h	Tue Jul 31 22:19:56 2018 -0700
+++ b/controller_info.h	Tue Jul 31 23:08:52 2018 -0700
@@ -45,5 +45,6 @@
 const char *get_axis_label(controller_info *info, int axis);
 void save_controller_info(int joystick, controller_info *info);
 void save_controller_mapping(int joystick, char *mapping_string);
+void controller_add_mappings(void);
 
 #endif //CONTROLLER_INFO_H_
\ No newline at end of file
--- a/render_sdl.c	Tue Jul 31 22:19:56 2018 -0700
+++ b/render_sdl.c	Tue Jul 31 23:08:52 2018 -0700
@@ -16,6 +16,7 @@
 #include "ppm.h"
 #include "png.h"
 #include "config.h"
+#include "controller_info.h"
 
 #ifndef DISABLE_OPENGL
 #include <GL/glew.h>
@@ -1141,6 +1142,8 @@
 		printf("Added %d game controller mappings from gamecontrollerdb.txt\n", added);
 	}
 	
+	controller_add_mappings();
+	
 	SDL_JoystickEventState(SDL_ENABLE);
 	
 	render_set_video_standard(VID_NTSC);