From b67b481f1c61df9e1e6f2601030f3573746bafd5 Mon Sep 17 00:00:00 2001 From: Twinaphex Date: Sun, 27 Jul 2014 08:17:38 +0200 Subject: [PATCH] (Frontend/Menu) start refactoring load_menu_game --- frontend/frontend.c | 5 ++- frontend/frontend.h | 1 + frontend/menu/menu_common.c | 64 ++++++++++++++++++++----------------- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/frontend/frontend.c b/frontend/frontend.c index ba81b218c7..b73579f4b8 100644 --- a/frontend/frontend.c +++ b/frontend/frontend.c @@ -272,7 +272,7 @@ void main_exit(args_type() args) driver.frontend_ctx->shutdown(false); } -static void free_args(struct rarch_main_wrap *wrap_args, char **argv_copy, unsigned argv_size) +void free_args(struct rarch_main_wrap *wrap_args, char **argv_copy, unsigned argv_size) { unsigned i; if (!wrap_args->touched) @@ -346,6 +346,9 @@ returntype main_entry(signature()) } } + if (g_extern.main_is_init) + rarch_main_deinit(); + if ((ret = rarch_main_init(*rarch_argc_ptr, rarch_argv_ptr))) { free_args(wrap_args, argv_copy, ARRAY_SIZE(argv_copy)); diff --git a/frontend/frontend.h b/frontend/frontend.h index a5911f7035..2760f23063 100644 --- a/frontend/frontend.h +++ b/frontend/frontend.h @@ -38,6 +38,7 @@ extern "C" { int main_entry_iterate(signature(), args_type() args); void main_exit(args_type() args); returntype main_entry(signature()); +void free_args(struct rarch_main_wrap *wrap_args, char **argv_copy, unsigned argv_size); #ifdef __cplusplus } diff --git a/frontend/menu/menu_common.c b/frontend/menu/menu_common.c index 57ce55b19c..ef1e744004 100644 --- a/frontend/menu/menu_common.c +++ b/frontend/menu/menu_common.c @@ -180,55 +180,61 @@ static void menu_update_libretro_info(menu_handle_t *menu) bool load_menu_game(void) { - int rarch_argc, ret; + int *rarch_argc_ptr; + char **rarch_argv_ptr; + struct rarch_main_wrap *wrap_args; unsigned i; - struct rarch_main_wrap args = {0}; - char *argv_copy[MAX_ARGS]; + int ret, rarch_argc = 0; char *rarch_argv[MAX_ARGS] = {NULL}; + char *argv_copy[MAX_ARGS] = {NULL}; - if (!driver.menu) - return false; + wrap_args = (struct rarch_main_wrap*)calloc(1, sizeof(*wrap_args)); + rarch_assert(wrap_args); - args.no_rom = driver.menu->load_no_rom; - args.verbose = g_extern.verbosity; - args.config_path = *g_extern.config_path ? g_extern.config_path : NULL; - args.sram_path = *g_extern.savefile_dir ? g_extern.savefile_dir : NULL; - args.state_path = *g_extern.savestate_dir ? g_extern.savestate_dir : NULL; - args.rom_path = *g_extern.fullpath ? g_extern.fullpath : NULL; - args.libretro_path = *g_settings.libretro ? g_settings.libretro : NULL; + wrap_args->no_rom = driver.menu->load_no_rom; + wrap_args->verbose = g_extern.verbosity; + wrap_args->config_path = *g_extern.config_path ? g_extern.config_path : NULL; + wrap_args->sram_path = *g_extern.savefile_dir ? g_extern.savefile_dir : NULL; + wrap_args->state_path = *g_extern.savestate_dir ? g_extern.savestate_dir : NULL; + wrap_args->rom_path = *g_extern.fullpath ? g_extern.fullpath : NULL; + wrap_args->libretro_path = *g_settings.libretro ? g_settings.libretro : NULL; rarch_argc = 0; ret = 0; - rarch_main_init_wrap(&args, &rarch_argc, rarch_argv); + rarch_main_init_wrap(wrap_args, &rarch_argc, rarch_argv); - if (rarch_argc > 0) + memcpy(argv_copy, rarch_argv, sizeof(rarch_argv)); + rarch_argv_ptr = (char**)rarch_argv; + rarch_argc_ptr = (int*)&rarch_argc; + + if (g_extern.main_is_init) + rarch_main_deinit(); + + if (ret = rarch_main_init(rarch_argc, rarch_argv)) { - // The pointers themselves are not const, and can be messed around with by getopt_long(). - memcpy(argv_copy, rarch_argv, sizeof(rarch_argv)); + free_args(wrap_args, argv_copy, ARRAY_SIZE(argv_copy)); + free(wrap_args); - if (g_extern.main_is_init) - rarch_main_deinit(); - - ret = rarch_main_init(rarch_argc, rarch_argv); - - for (i = 0; i < ARRAY_SIZE(argv_copy); i++) - free(argv_copy[i]); - } - - if (ret != 0) - { char name[PATH_MAX], msg[PATH_MAX]; fill_pathname_base(name, g_extern.fullpath, sizeof(name)); snprintf(msg, sizeof(msg), "Failed to load %s.\n", name); msg_queue_push(g_extern.msg_queue, msg, 1, 90); - driver.menu->msg_force = true; + + if (driver.menu) + driver.menu->msg_force = true; + return false; } + if (wrap_args) + free_args(wrap_args, argv_copy, ARRAY_SIZE(argv_copy)); + free(wrap_args); + // Update menu state which depends on config. - menu_update_libretro_info(driver.menu); + if (driver.menu) + menu_update_libretro_info(driver.menu); if (g_extern.history) content_history_free(g_extern.history);