changeset 2018:193b804c9845

Add a UI button to reset config to defaults
author Michael Pavone <pavone@retrodev.com>
date Mon, 09 Nov 2020 00:29:47 -0800
parents 8e7b06ade815
children 81eebbe6b2e3
files config.c config.h controller_info.c controller_info.h nuklear_ui/blastem_nuklear.c render.h render_sdl.c tern.c
diffstat 8 files changed, 69 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/config.c	Sat Nov 07 18:27:34 2020 -0800
+++ b/config.c	Mon Nov 09 00:29:47 2020 -0800
@@ -298,6 +298,21 @@
 	persist_config_at(config, config, "blastem.cfg");
 }
 
+void delete_custom_config_at(char *fname)
+{
+	char *confpath = path_append(get_exe_dir(), fname);
+	delete_file(confpath);
+	free(confpath);
+	confpath = path_append(get_config_dir(), fname);
+	delete_file(confpath);
+	free(confpath);
+}
+
+void delete_custom_config(void)
+{
+	delete_custom_config_at("blastem.cfg");
+}
+
 char **get_extension_list(tern_node *config, uint32_t *num_exts_out)
 {
 	char *ext_filter = strdup(tern_find_path_default(config, "ui\0extensions\0", (tern_val){.ptrval = "bin gen md smd sms gg"}, TVAL_PTR).ptrval);
--- a/config.h	Sat Nov 07 18:27:34 2020 -0800
+++ b/config.h	Mon Nov 09 00:29:47 2020 -0800
@@ -16,6 +16,8 @@
 uint8_t serialize_config_file(tern_node *config, char *path);
 void persist_config_at(tern_node *app_config, tern_node *to_save, char *fname);
 void persist_config(tern_node *config);
+void delete_custom_config_at(char *fname);
+void delete_custom_config(void);
 char **get_extension_list(tern_node *config, uint32_t *num_exts_out);
 uint32_t get_lowpass_cutoff(tern_node *config);
 tern_node *get_systems_config(void);
--- a/controller_info.c	Sat Nov 07 18:27:34 2020 -0800
+++ b/controller_info.c	Mon Nov 09 00:29:47 2020 -0800
@@ -224,6 +224,15 @@
 #endif
 }
 
+void delete_controller_info(void)
+{
+	delete_custom_config_at("controller_types.cfg");
+	loaded = 0;
+	tern_free(info_config);
+	info_config = NULL;
+	render_reset_mappings();
+}
+
 char const *labels_xbox[] = {
 	"A", "B", "X", "Y", "Back", NULL, "Start", "Click", "Click", "White", "Black", "LT", "RT"
 };
--- a/controller_info.h	Sat Nov 07 18:27:34 2020 -0800
+++ b/controller_info.h	Mon Nov 09 00:29:47 2020 -0800
@@ -47,6 +47,7 @@
 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 delete_controller_info(void);
 void controller_add_mappings(void);
 char *make_controller_type_key(controller_info *info);
 char *make_human_readable_type_name(controller_info *info);
--- a/nuklear_ui/blastem_nuklear.c	Sat Nov 07 18:27:34 2020 -0800
+++ b/nuklear_ui/blastem_nuklear.c	Mon Nov 09 00:29:47 2020 -0800
@@ -2023,6 +2023,29 @@
 	}
 }
 
+void view_confirm_reset(struct nk_context *context)
+{
+	if (nk_begin(context, "Reset Confirm", nk_rect(0, 0, render_width(), render_height()), 0)) {
+		uint32_t desired_width = context->style.font->height * 20;
+		nk_layout_row_static(context, context->style.font->height, desired_width, 1);
+		nk_label(context, "This will reset all settings and controller", NK_TEXT_LEFT);
+		nk_label(context, "mappings back to the defaults.", NK_TEXT_LEFT);
+		nk_label(context, "Are you sure you want to proceed?", NK_TEXT_LEFT);
+		nk_layout_row_static(context, context->style.font->height * 1.5, desired_width / 2, 2);
+		if (nk_button_label(context, "Maybe not")) {
+			pop_view();
+		}
+		if (nk_button_label(context, "Yep, delete it all")) {
+			delete_custom_config();
+			config = load_config();
+			delete_controller_info();
+			config_dirty = 1;
+			pop_view();
+		}
+		nk_end(context);
+	}
+}
+
 void view_back(struct nk_context *context)
 {
 	pop_view();
@@ -2038,6 +2061,7 @@
 		{"Video", view_video_settings},
 		{"Audio", view_audio_settings},
 		{"System", view_system_settings},
+		{"Reset to Defaults", view_confirm_reset},
 		{"Back", view_back}
 	};
 	
--- a/render.h	Sat Nov 07 18:27:34 2020 -0800
+++ b/render.h	Mon Nov 09 00:29:47 2020 -0800
@@ -140,6 +140,7 @@
 void render_video_loop(void);
 uint8_t render_should_release_on_exit(void);
 void render_set_external_sync(uint8_t ext_sync_on);
+void render_reset_mappings(void);
 #ifndef IS_LIB
 uint8_t render_create_thread(render_thread *thread, const char *name, render_thread_fun fun, void *data);
 #endif
--- a/render_sdl.c	Sat Nov 07 18:27:34 2020 -0800
+++ b/render_sdl.c	Mon Nov 09 00:29:47 2020 -0800
@@ -1203,6 +1203,19 @@
 
 	atexit(render_quit);
 }
+
+void render_reset_mappings(void)
+{
+	SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
+	SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
+	uint32_t db_size;
+	char *db_data = read_bundled_file("gamecontrollerdb.txt", &db_size);
+	if (db_data) {
+		int added = SDL_GameControllerAddMappingsFromRW(SDL_RWFromMem(db_data, db_size), 1);
+		free(db_data);
+		debug_message("Added %d game controller mappings from gamecontrollerdb.txt\n", added);
+	}
+}
 static int in_toggle;
 
 void render_config_updated(void)
--- a/tern.c	Sat Nov 07 18:27:34 2020 -0800
+++ b/tern.c	Mon Nov 09 00:29:47 2020 -0800
@@ -305,12 +305,11 @@
 
 void tern_free(tern_node *head)
 {
-	if (head->left) {
-		tern_free(head->left);
+	if (!head) {
+		return;
 	}
-	if (head->right) {
-		tern_free(head->right);
-	}
+	tern_free(head->left);
+	tern_free(head->right);
 	if (head->el) {
 		tern_free(head->straight.next);
 	}