From 8238ba7d50f8b6911a1e760653506c304c406183 Mon Sep 17 00:00:00 2001 From: Themaister Date: Sun, 22 Sep 2013 12:48:33 +0200 Subject: [PATCH] Allow saving new config files directly from RGUI. The file name is inferred to avoid typing. --- frontend/menu/menu_common.c | 78 +++++++++++++++++++++++++++++++++++++ frontend/menu/menu_common.h | 3 ++ frontend/menu/rgui.c | 6 +++ settings.c | 5 +-- 4 files changed, 89 insertions(+), 3 deletions(-) diff --git a/frontend/menu/menu_common.c b/frontend/menu/menu_common.c index 253b852397..f6acac39f0 100644 --- a/frontend/menu/menu_common.c +++ b/frontend/menu/menu_common.c @@ -944,3 +944,81 @@ bool menu_replace_config(const char *path) return true; } +// Save a new config to a file. Filename is based on heuristics to avoid typing. +bool menu_save_new_config(void) +{ + char config_dir[PATH_MAX]; + *config_dir = '\0'; + + if (*g_settings.rgui_config_directory) + strlcpy(config_dir, g_settings.rgui_config_directory, sizeof(config_dir)); + else if (*g_extern.config_path) // Fallback + fill_pathname_basedir(config_dir, g_extern.config_path, sizeof(config_dir)); + else + { + const char *msg = "Config directory not set. Cannot save new config."; + msg_queue_clear(g_extern.msg_queue); + msg_queue_push(g_extern.msg_queue, msg, 1, 180); + RARCH_ERR("%s\n", msg); + return false; + } + + bool found_path = false; + char config_name[PATH_MAX]; + char config_path[PATH_MAX]; + if (*g_settings.libretro && !path_is_directory(g_settings.libretro) && path_file_exists(g_settings.libretro)) // Infer file name based on libretro core. + { + // In case of collision, find an alternative name. + for (unsigned i = 0; i < 16; i++) + { + fill_pathname_base(config_name, g_settings.libretro, sizeof(config_name)); + path_remove_extension(config_name); + fill_pathname_join(config_path, config_dir, config_name, sizeof(config_path)); + + char tmp[64]; + *tmp = '\0'; + if (i) + snprintf(tmp, sizeof(tmp), "-%u.cfg", i); + else + strlcpy(tmp, ".cfg", sizeof(tmp)); + + strlcat(config_path, tmp, sizeof(config_path)); + + if (!path_file_exists(config_path)) + { + found_path = true; + break; + } + } + } + + // Fallback to system time ... + if (!found_path) + { + RARCH_WARN("Cannot infer new config path. Use current time.\n"); + fill_dated_filename(config_name, "cfg", sizeof(config_name)); + fill_pathname_join(config_path, config_dir, config_name, sizeof(config_path)); + } + + char msg[512]; + bool ret; + if (config_save_file(config_path)) + { + strlcpy(g_extern.config_path, config_path, sizeof(g_extern.config_path)); + snprintf(msg, sizeof(msg), "Saved new config to \"%s\".", config_path); + RARCH_LOG("%s\n", msg); + ret = true; + } + else + { + snprintf(msg, sizeof(msg), "Failed saving config to \"%s\".", config_path); + RARCH_ERR("%s\n", msg); + ret = false; + } + + msg_queue_clear(g_extern.msg_queue); + msg_queue_push(g_extern.msg_queue, msg, 1, 180); + return ret; +} + + diff --git a/frontend/menu/menu_common.h b/frontend/menu/menu_common.h index d5326c6dcc..160151ba80 100644 --- a/frontend/menu/menu_common.h +++ b/frontend/menu/menu_common.h @@ -130,6 +130,7 @@ typedef enum RGUI_SETTINGS_OPEN_HISTORY, RGUI_SETTINGS_CORE, RGUI_SETTINGS_CONFIG, + RGUI_SETTINGS_SAVE_CONFIG, RGUI_SETTINGS_CORE_OPTIONS, RGUI_SETTINGS_AUDIO_OPTIONS, RGUI_SETTINGS_INPUT_OPTIONS, @@ -298,6 +299,8 @@ void menu_rom_history_push_current(void); bool menu_replace_config(const char *path); +bool menu_save_new_config(void); + #ifdef __cplusplus } #endif diff --git a/frontend/menu/rgui.c b/frontend/menu/rgui.c index 15e280d986..d2613bee4e 100644 --- a/frontend/menu/rgui.c +++ b/frontend/menu/rgui.c @@ -761,6 +761,7 @@ static void render_text(rgui_handle_t *rgui) case RGUI_SETTINGS_VIDEO_OPTIONS: case RGUI_SETTINGS_AUDIO_OPTIONS: case RGUI_SETTINGS_DISK_OPTIONS: + case RGUI_SETTINGS_SAVE_CONFIG: #ifdef HAVE_SHADER_MANAGER case RGUI_SETTINGS_SHADER_OPTIONS: case RGUI_SETTINGS_SHADER_PRESET: @@ -1124,6 +1125,10 @@ static int rgui_settings_toggle_setting(rgui_handle_t *rgui, unsigned setting, r return -1; } break; + case RGUI_SETTINGS_SAVE_CONFIG: + if (action == RGUI_ACTION_OK) + menu_save_new_config(); + break; #ifdef HAVE_OVERLAY case RGUI_SETTINGS_OVERLAY_PRESET: switch (action) @@ -1510,6 +1515,7 @@ static void rgui_settings_populate_entries(rgui_handle_t *rgui) rgui_list_push(rgui->selection_buf, "Restart RetroArch", RGUI_SETTINGS_RESTART_EMULATOR, 0); #endif rgui_list_push(rgui->selection_buf, "RetroArch Config", RGUI_SETTINGS_CONFIG, 0); + rgui_list_push(rgui->selection_buf, "Save New Config", RGUI_SETTINGS_SAVE_CONFIG, 0); rgui_list_push(rgui->selection_buf, "Quit RetroArch", RGUI_SETTINGS_QUIT_RARCH, 0); } diff --git a/settings.c b/settings.c index 62c2bdd284..8931790b96 100644 --- a/settings.c +++ b/settings.c @@ -1103,10 +1103,9 @@ bool config_save_file(const char *path) config_set_int(conf, cfg, g_settings.input.libretro_device[i]); } - config_file_write(conf, path); + bool ret = config_file_write(conf, path); config_file_free(conf); - - return true; + return ret; } bool config_save_keybinds(const char *path)