From 99c2676efa07755067952b1ac31363b2e6d34510 Mon Sep 17 00:00:00 2001 From: Eric Warmenhoven Date: Sun, 8 Dec 2024 02:58:33 -0500 Subject: [PATCH] Add option to load overlay based on system name (#17233) --- configuration.c | 9 +++ input/input_driver.c | 103 ++++++++++++++++++++++++++++++++++- intl/msg_hash_us.h | 4 ++ menu/cbs/menu_cbs_ok.c | 1 + menu/cbs/menu_cbs_sublabel.c | 4 ++ menu/menu_displaylist.c | 1 + retroarch.c | 10 +++- retroarch.h | 3 +- retroarch_types.h | 1 + 9 files changed, 131 insertions(+), 5 deletions(-) diff --git a/configuration.c b/configuration.c index 2e8afffc8f..b8f947273e 100644 --- a/configuration.c +++ b/configuration.c @@ -3679,6 +3679,10 @@ static bool config_load_file(global_t *global, * variable. */ char tmp_append_path[PATH_MAX_LENGTH]; const char *extra_path = NULL; +#ifdef HAVE_OVERLAY + char old_overlay_path[PATH_MAX_LENGTH], new_overlay_path[PATH_MAX_LENGTH]; + config_get_path(conf, "input_overlay", old_overlay_path, sizeof(old_overlay_path)); +#endif strlcpy(tmp_append_path, path_get(RARCH_PATH_CONFIG_OVERRIDE), sizeof(tmp_append_path)); extra_path = strtok_r(tmp_append_path, "|", &save); @@ -3699,6 +3703,11 @@ static bool config_load_file(global_t *global, /* Re-check verbosity settings */ check_verbosity_settings(conf, settings); +#ifdef HAVE_OVERLAY + config_get_path(conf, "input_overlay", new_overlay_path, sizeof(new_overlay_path)); + if (!string_is_equal(old_overlay_path, new_overlay_path)) + retroarch_override_setting_set(RARCH_OVERRIDE_SETTING_OVERLAY_PRESET, NULL); +#endif } #if 0 diff --git a/input/input_driver.c b/input/input_driver.c index c0581b3530..8be3dcd294 100644 --- a/input/input_driver.c +++ b/input/input_driver.c @@ -48,9 +48,11 @@ #include "../command.h" #include "../config.def.keybinds.h" #include "../configuration.h" +#include "../core_info.h" #include "../driver.h" #include "../frontend/frontend_driver.h" #include "../list_special.h" +#include "../paths.h" #include "../performance_counters.h" #include "../retroarch.h" #ifdef HAVE_BSV_MOVIE @@ -5235,6 +5237,103 @@ static void input_overlay_loaded(retro_task_t *task, #endif } +static const char *input_overlay_path(bool want_osk) +{ + static char system_overlay_path[PATH_MAX_LENGTH] = {0}; + char overlay_directory[PATH_MAX_LENGTH]; + settings_t *settings = config_get_ptr(); + playlist_t *playlist = playlist_get_cached(); + core_info_t *core_info = NULL; + const char *content_path = path_get(RARCH_PATH_CONTENT); + + if (want_osk) + return settings->paths.path_osk_overlay; + /* if the option is set to turn this off, just return default */ + if (!settings->bools.input_overlay_enable_autopreferred) + return settings->paths.path_overlay; + /* if there's an override, use it */ + if (retroarch_override_setting_is_set(RARCH_OVERRIDE_SETTING_OVERLAY_PRESET, NULL)) + return settings->paths.path_overlay; + + /* let's go hunting */ + fill_pathname_expand_special(overlay_directory, + settings->paths.directory_overlay, + sizeof(overlay_directory)); + +#define SYSTEM_OVERLAY_DIR "gamepads/Named_Overlays" + + /* try based on the playlist entry first */ + if (playlist) + { +#ifdef HAVE_MENU + menu_handle_t *menu = menu_state_get_ptr()->driver_data; + if (menu) + { + const char *playlist_db_name = NULL; + playlist_get_db_name(playlist, menu->rpl_entry_selection_ptr, &playlist_db_name); + if (playlist_db_name) + { + fill_pathname_join_special_ext(system_overlay_path, + overlay_directory, SYSTEM_OVERLAY_DIR, playlist_db_name, "", + sizeof(system_overlay_path)); + strlcpy(path_get_extension_mutable(system_overlay_path), ".cfg", 5); + if (path_is_valid(system_overlay_path)) + return system_overlay_path; + } + } +#endif + if (!string_is_empty(content_path)) + { + const struct playlist_entry *entry; + playlist_get_index_by_path(playlist, content_path, &entry); + if (entry && entry->db_name) + { + fill_pathname_join_special_ext(system_overlay_path, + overlay_directory, SYSTEM_OVERLAY_DIR, entry->db_name, "", + sizeof(system_overlay_path)); + strlcpy(path_get_extension_mutable(system_overlay_path), ".cfg", 5); + if (path_is_valid(system_overlay_path)) + return system_overlay_path; + } + } + } + + /* maybe the core info will have some clues */ + core_info_get_current_core(&core_info); + if (core_info) + { + if (core_info->databases_list && core_info->databases_list->size == 1) + { + fill_pathname_join_special_ext(system_overlay_path, + overlay_directory, SYSTEM_OVERLAY_DIR, core_info->databases_list->elems[0].data, ".cfg", + sizeof(system_overlay_path)); + if (path_is_valid(system_overlay_path)) + return system_overlay_path; + } + + fill_pathname_join_special_ext(system_overlay_path, + overlay_directory, SYSTEM_OVERLAY_DIR, core_info->display_name, ".cfg", + sizeof(system_overlay_path)); + if (path_is_valid(system_overlay_path)) + return system_overlay_path; + } + + /* maybe based on the content's directory name */ + if (!string_is_empty(content_path)) + { + char dirname[PATH_MAX_LENGTH]; + fill_pathname_parent_dir_name(dirname, content_path, sizeof(dirname)); + fill_pathname_join_special_ext(system_overlay_path, + overlay_directory, SYSTEM_OVERLAY_DIR, dirname, ".cfg", + sizeof(system_overlay_path)); + if (path_is_valid(system_overlay_path)) + return system_overlay_path; + } + + /* I give up */ + return settings->paths.path_overlay; +} + void input_overlay_init(void) { settings_t *settings = config_get_ptr(); @@ -5244,9 +5343,7 @@ void input_overlay_init(void) bool want_osk = (input_st->flags & INP_FLAG_KB_LINEFEED_ENABLE) && !string_is_empty(settings->paths.path_osk_overlay); - const char *path_overlay = want_osk - ? settings->paths.path_osk_overlay - : settings->paths.path_overlay; + const char *path_overlay = input_overlay_path(want_osk); bool want_hidden = input_overlay_want_hidden(); bool overlay_shown = ol && (ol->flags & INPUT_OVERLAY_ENABLE) diff --git a/intl/msg_hash_us.h b/intl/msg_hash_us.h index c2f8cbb748..ff909feb12 100644 --- a/intl/msg_hash_us.h +++ b/intl/msg_hash_us.h @@ -5426,6 +5426,10 @@ MSG_HASH( MENU_ENUM_LABEL_VALUE_OVERLAY_AUTOLOAD_PREFERRED, "Autoload Preferred Overlay" ) +MSG_HASH( + MENU_ENUM_SUBLABEL_OVERLAY_AUTOLOAD_PREFERRED, + "Prefer loading overlays based on system name before falling back to the default preset. Will be ignored if an override is set for the overlay preset." + ) MSG_HASH( MENU_ENUM_LABEL_VALUE_OVERLAY_OPACITY, "Overlay Opacity" diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c index c9a9c9440a..869400fb55 100644 --- a/menu/cbs/menu_cbs_ok.c +++ b/menu/cbs/menu_cbs_ok.c @@ -2438,6 +2438,7 @@ static int generic_action_ok(const char *path, break; case ACTION_OK_SET_PATH_OVERLAY: flush_char = msg_hash_to_str(MENU_ENUM_LABEL_DEFERRED_ONSCREEN_OVERLAY_SETTINGS_LIST); + retroarch_override_setting_set(RARCH_OVERRIDE_SETTING_OVERLAY_PRESET, NULL); ret = set_path_generic(menu_label, action_path); break; case ACTION_OK_SET_PATH_OSK_OVERLAY: diff --git a/menu/cbs/menu_cbs_sublabel.c b/menu/cbs/menu_cbs_sublabel.c index 9a465b1e37..356bbeee5d 100644 --- a/menu/cbs/menu_cbs_sublabel.c +++ b/menu/cbs/menu_cbs_sublabel.c @@ -883,6 +883,7 @@ DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_overlay_y_separation_portrait, MEN DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_overlay_x_offset_portrait, MENU_ENUM_SUBLABEL_OVERLAY_X_OFFSET_PORTRAIT) DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_overlay_y_offset_portrait, MENU_ENUM_SUBLABEL_OVERLAY_Y_OFFSET_PORTRAIT) DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_overlay_enable, MENU_ENUM_SUBLABEL_INPUT_OVERLAY_ENABLE) +DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_overlay_autoload_preferred, MENU_ENUM_SUBLABEL_OVERLAY_AUTOLOAD_PREFERRED) DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_overlay_preset, MENU_ENUM_SUBLABEL_OVERLAY_PRESET) DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_osk_overlay_preset, MENU_ENUM_SUBLABEL_OSK_OVERLAY_PRESET) DEFAULT_SUBLABEL_MACRO(action_bind_sublabel_input_overlay_pointer_enable, MENU_ENUM_SUBLABEL_INPUT_OVERLAY_POINTER_ENABLE) @@ -3744,6 +3745,9 @@ int menu_cbs_init_bind_sublabel(menu_file_list_cbs_t *cbs, case MENU_ENUM_LABEL_NETPLAY_IP_ADDRESS: BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_netplay_ip_address); break; + case MENU_ENUM_LABEL_OVERLAY_AUTOLOAD_PREFERRED: + BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_overlay_autoload_preferred); + break; case MENU_ENUM_LABEL_OVERLAY_PRESET: BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_overlay_preset); break; diff --git a/menu/menu_displaylist.c b/menu/menu_displaylist.c index 352c60d431..b5a26096ba 100644 --- a/menu/menu_displaylist.c +++ b/menu/menu_displaylist.c @@ -10112,6 +10112,7 @@ unsigned menu_displaylist_build_list( menu_displaylist_build_info_selective_t build_list[] = { {MENU_ENUM_LABEL_INPUT_OVERLAY_ENABLE, PARSE_ONLY_BOOL, true }, + {MENU_ENUM_LABEL_OVERLAY_AUTOLOAD_PREFERRED, PARSE_ONLY_BOOL, true }, {MENU_ENUM_LABEL_OVERLAY_PRESET, PARSE_ONLY_PATH, false }, {MENU_ENUM_LABEL_OVERLAY_OPACITY, PARSE_ONLY_FLOAT, false }, {MENU_ENUM_LABEL_INPUT_OVERLAY_BEHIND_MENU, PARSE_ONLY_BOOL, false }, diff --git a/retroarch.c b/retroarch.c index f5bd2da2b5..9646e8c6b0 100644 --- a/retroarch.c +++ b/retroarch.c @@ -310,7 +310,7 @@ struct rarch_state sthread_tls_t rarch_tls; /* unsigned alignment */ #endif unsigned perf_ptr_rarch; - uint16_t flags; + uint32_t flags; char launch_arguments[4096]; char path_default_shader_preset[PATH_MAX_LENGTH]; @@ -5555,6 +5555,9 @@ void retroarch_override_setting_set( case RARCH_OVERRIDE_SETTING_DATABASE_SCAN: p_rarch->flags |= RARCH_FLAGS_CLI_DATABASE_SCAN; break; + case RARCH_OVERRIDE_SETTING_OVERLAY_PRESET: + p_rarch->flags |= RARCH_FLAGS_HAS_SET_OVERLAY_PRESET; + break; case RARCH_OVERRIDE_SETTING_NONE: default: break; @@ -5637,6 +5640,9 @@ void retroarch_override_setting_unset( case RARCH_OVERRIDE_SETTING_DATABASE_SCAN: p_rarch->flags &= ~RARCH_FLAGS_CLI_DATABASE_SCAN; break; + case RARCH_OVERRIDE_SETTING_OVERLAY_PRESET: + p_rarch->flags &= ~RARCH_FLAGS_HAS_SET_OVERLAY_PRESET; + break; case RARCH_OVERRIDE_SETTING_NONE: default: break; @@ -8112,6 +8118,8 @@ bool retroarch_override_setting_is_set( return ((p_rarch->flags & RARCH_FLAGS_HAS_SET_LOG_TO_FILE) > 0); case RARCH_OVERRIDE_SETTING_DATABASE_SCAN: return ((p_rarch->flags & RARCH_FLAGS_CLI_DATABASE_SCAN) > 0); + case RARCH_OVERRIDE_SETTING_OVERLAY_PRESET: + return ((p_rarch->flags & RARCH_FLAGS_HAS_SET_OVERLAY_PRESET) > 0); case RARCH_OVERRIDE_SETTING_NONE: default: break; diff --git a/retroarch.h b/retroarch.h index 2d1900766c..b57e0d3f6d 100644 --- a/retroarch.h +++ b/retroarch.h @@ -106,7 +106,8 @@ enum rarch_state_flags RARCH_FLAGS_BLOCK_CONFIG_READ = (1 << 13), RARCH_FLAGS_CLI_DATABASE_SCAN = (1 << 14), RARCH_FLAGS_HAS_SET_XDELTA_PREF = (1 << 15), - RARCH_FLAGS_XDELTA_PREF = (1 << 16) + RARCH_FLAGS_XDELTA_PREF = (1 << 16), + RARCH_FLAGS_HAS_SET_OVERLAY_PRESET = (1 << 17) }; bool retroarch_ctl(enum rarch_ctl_state state, void *data); diff --git a/retroarch_types.h b/retroarch_types.h index d3f363ff73..f50d7b51e2 100644 --- a/retroarch_types.h +++ b/retroarch_types.h @@ -128,6 +128,7 @@ enum rarch_override_setting RARCH_OVERRIDE_SETTING_LIBRETRO_DEVICE, RARCH_OVERRIDE_SETTING_LOG_TO_FILE, RARCH_OVERRIDE_SETTING_DATABASE_SCAN, + RARCH_OVERRIDE_SETTING_OVERLAY_PRESET, RARCH_OVERRIDE_SETTING_LAST };