comparison controller_info.c @ 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 7f39c40b4b25
children 68b05322d971
comparison
equal deleted inserted replaced
1602:b452887f85b4 1603:c0727712d529
43 static const char *variant_names[] = { 43 static const char *variant_names[] = {
44 "normal", 44 "normal",
45 "6b bumpers", 45 "6b bumpers",
46 "6b right" 46 "6b right"
47 }; 47 };
48 controller_info get_controller_info(int joystick) 48
49 static void load_ctype_config(void)
49 { 50 {
50 if (!loaded) { 51 if (!loaded) {
51 info_config = load_overrideable_config("controller_types.cfg", "controller_types.cfg"); 52 info_config = load_overrideable_config("controller_types.cfg", "controller_types.cfg");
52 loaded = 1; 53 loaded = 1;
53 } 54 }
55 }
56
57 controller_info get_controller_info(int joystick)
58 {
59 load_ctype_config();
54 char guid_string[33]; 60 char guid_string[33];
55 SDL_Joystick *stick = render_get_joystick(joystick); 61 SDL_Joystick *stick = render_get_joystick(joystick);
56 SDL_GameController *control = render_get_controller(joystick); 62 SDL_GameController *control = render_get_controller(joystick);
57 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(stick), guid_string, sizeof(guid_string)); 63 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(stick), guid_string, sizeof(guid_string));
58 tern_node *info = tern_find_node(info_config, guid_string); 64 tern_node *info = tern_find_node(info_config, guid_string);
135 .variant = VARIANT_NORMAL, 141 .variant = VARIANT_NORMAL,
136 .name = name 142 .name = name
137 }; 143 };
138 } 144 }
139 145
146 static void mappings_iter(char *key, tern_val val, uint8_t valtype, void *data)
147 {
148 if (valtype != TVAL_NODE) {
149 return;
150 }
151 char *mapping = tern_find_ptr(val.ptrval, "mapping");
152 if (mapping) {
153 SDL_GameControllerAddMapping(mapping);
154 }
155 }
156
157 void controller_add_mappings(void)
158 {
159 load_ctype_config();
160 tern_foreach(info_config, mappings_iter, NULL);
161 }
162
140 void save_controller_info(int joystick, controller_info *info) 163 void save_controller_info(int joystick, controller_info *info)
141 { 164 {
142 char guid_string[33]; 165 char guid_string[33];
143 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(render_get_joystick(joystick)), guid_string, sizeof(guid_string)); 166 SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(render_get_joystick(joystick)), guid_string, sizeof(guid_string));
144 tern_node *existing = tern_find_node(info_config, guid_string); 167 tern_node *existing = tern_find_node(info_config, guid_string);