diff --git a/config_spec.yml b/config_spec.yml index cae05c17ec2..61df66ec9a0 100644 --- a/config_spec.yml +++ b/config_spec.yml @@ -14,9 +14,13 @@ general: snapshots: shortcuts: f5: string + f5_present: bool f6: string + f6_present: bool f7: string + f7_present: bool f8: string + f8_present: bool filter_current_game: bool input: diff --git a/ui/xemu-snapshots.c b/ui/xemu-snapshots.c index 25f227e6fb7..fd09f2f4aa8 100644 --- a/ui/xemu-snapshots.c +++ b/ui/xemu-snapshots.c @@ -42,13 +42,6 @@ static XemuSnapshotData *xemu_snapshots_extra_data = NULL; static int xemu_snapshots_len = 0; static bool xemu_snapshots_dirty = true; -const char **g_snapshot_shortcut_index_key_map[] = { - &g_config.general.snapshots.shortcuts.f5, - &g_config.general.snapshots.shortcuts.f6, - &g_config.general.snapshots.shortcuts.f7, - &g_config.general.snapshots.shortcuts.f8, -}; - static void xemu_snapshots_load_data(BlockDriverState *bs_ro, QEMUSnapshotInfo *info, XemuSnapshotData *data, Error **err) diff --git a/ui/xemu-snapshots.h b/ui/xemu-snapshots.h index a9809117a2d..0f19b14843b 100644 --- a/ui/xemu-snapshots.h +++ b/ui/xemu-snapshots.h @@ -34,8 +34,6 @@ extern "C" { #define XEMU_SNAPSHOT_THUMBNAIL_WIDTH 160 #define XEMU_SNAPSHOT_THUMBNAIL_HEIGHT 120 -extern const char **g_snapshot_shortcut_index_key_map[]; - typedef struct XemuSnapshotData { char *disc_path; char *xbe_title_name; diff --git a/ui/xui/actions.cc b/ui/xui/actions.cc index dc2c830c50d..4657b160b24 100644 --- a/ui/xui/actions.cc +++ b/ui/xui/actions.cc @@ -80,9 +80,8 @@ void ActionScreenshot(void) void ActionActivateBoundSnapshot(int slot, bool save) { - assert(slot < 4 && slot >= 0); - const char *snapshot_name = *(g_snapshot_shortcut_index_key_map[slot]); - if (!snapshot_name || !(snapshot_name[0])) { + auto snapshot_name = g_snapshot_mgr.GetSnapshotShortcut(slot); + if (!snapshot_name) { char *msg = g_strdup_printf("F%d is not bound to a snapshot", slot + 5); xemu_queue_notification(msg); g_free(msg); @@ -91,9 +90,9 @@ void ActionActivateBoundSnapshot(int slot, bool save) Error *err = NULL; if (save) { - xemu_snapshots_save(snapshot_name, &err); + xemu_snapshots_save(*snapshot_name, &err); } else { - ActionLoadSnapshotChecked(snapshot_name); + ActionLoadSnapshotChecked(*snapshot_name); } if (err) { diff --git a/ui/xui/main-menu.cc b/ui/xui/main-menu.cc index f29b0139832..05bed8347f0 100644 --- a/ui/xui/main-menu.cc +++ b/ui/xui/main-menu.cc @@ -32,13 +32,14 @@ #include "actions.hh" #include "../xemu-input.h" -#include "../xemu-notifications.h" -#include "../xemu-settings.h" #include "../xemu-monitor.h" -#include "../xemu-version.h" #include "../xemu-net.h" +#include "../xemu-notifications.h" #include "../xemu-os-utils.h" +#include "../xemu-settings.h" +#include "../xemu-version.h" #include "../xemu-xbe.h" +#include MainMenuScene g_main_menu; @@ -815,10 +816,13 @@ void MainMenuSnapshotsView::Draw() &OnSearchTextUpdate, this); bool snapshot_with_create_name_exists = false; - for (int i = 0; i < g_snapshot_mgr.m_snapshots_len; ++i) { - if (g_strcmp0(m_search_buf.c_str(), g_snapshot_mgr.m_snapshots[i].name) == 0) { - snapshot_with_create_name_exists = true; - break; + if (!m_search_buf.empty()) { + for (int i = 0; i < g_snapshot_mgr.m_snapshots_len; ++i) { + if (g_strcmp0(m_search_buf.c_str(), + g_snapshot_mgr.m_snapshots[i].name) == 0) { + snapshot_with_create_name_exists = true; + break; + } } } @@ -873,7 +877,9 @@ void MainMenuSnapshotsView::Draw() int current_snapshot_binding = -1; for (int i = 0; i < 4; ++i) { - if (g_strcmp0(*(g_snapshot_shortcut_index_key_map[i]), snapshot->name) == 0) { + auto shortcut_name = g_snapshot_mgr.GetSnapshotShortcut(i); + if (shortcut_name && + g_strcmp0(*shortcut_name, snapshot->name) == 0) { assert(current_snapshot_binding == -1); current_snapshot_binding = i; } @@ -936,9 +942,10 @@ void MainMenuSnapshotsView::DrawSnapshotContextMenu(QEMUSnapshotInfo *snapshot, if (ImGui::MenuItem(item_name)) { if (current_snapshot_binding >= 0) { - xemu_settings_set_string(g_snapshot_shortcut_index_key_map[current_snapshot_binding], ""); + g_snapshot_mgr.SetSnapshotShortcut(current_snapshot_binding, + std::nullopt); } - xemu_settings_set_string(g_snapshot_shortcut_index_key_map[i], snapshot->name); + g_snapshot_mgr.SetSnapshotShortcut(i, snapshot->name); current_snapshot_binding = i; ImGui::CloseCurrentPopup(); @@ -949,7 +956,8 @@ void MainMenuSnapshotsView::DrawSnapshotContextMenu(QEMUSnapshotInfo *snapshot, if (current_snapshot_binding >= 0) { if (ImGui::MenuItem("Unbind")) { - xemu_settings_set_string(g_snapshot_shortcut_index_key_map[current_snapshot_binding], ""); + g_snapshot_mgr.SetSnapshotShortcut(current_snapshot_binding, + std::nullopt); current_snapshot_binding = -1; } } diff --git a/ui/xui/menubar.cc b/ui/xui/menubar.cc index 2afcc367c52..97befb879e7 100644 --- a/ui/xui/menubar.cc +++ b/ui/xui/menubar.cc @@ -17,16 +17,18 @@ // along with this program. If not, see . // #include "ui/xemu-notifications.h" +#include "ui/xui/snapshot-manager.hh" +#include "menubar.hh" +#include "actions.hh" #include "common.hh" +#include "compat.hh" +#include "debug.hh" #include "main-menu.hh" -#include "menubar.hh" #include "misc.hh" -#include "widgets.hh" #include "monitor.hh" -#include "debug.hh" -#include "actions.hh" -#include "compat.hh" #include "update.hh" +#include "widgets.hh" + #include "../xemu-os-utils.h" extern float g_main_menu_height; // FIXME @@ -101,13 +103,13 @@ void ShowMainMenu() char *load_name; char *save_name; - assert(g_snapshot_shortcut_index_key_map[i]); - bool bound = *(g_snapshot_shortcut_index_key_map[i]) && - (**(g_snapshot_shortcut_index_key_map[i]) != 0); + auto bound_snapshot = g_snapshot_mgr.GetSnapshotShortcut(i); - if (bound) { - load_name = g_strdup_printf("Load '%s'", *(g_snapshot_shortcut_index_key_map[i])); - save_name = g_strdup_printf("Save '%s'", *(g_snapshot_shortcut_index_key_map[i])); + if (bound_snapshot) { + load_name = + g_strdup_printf("Load '%s'", *bound_snapshot); + save_name = + g_strdup_printf("Save '%s'", *bound_snapshot); } else { load_name = g_strdup_printf("Load F%d (Unbound)", i + 5); save_name = g_strdup_printf("Save F%d (Unbound)", i + 5); @@ -115,11 +117,14 @@ void ShowMainMenu() ImGui::Separator(); - if (ImGui::MenuItem(load_name, hotkey + sizeof("Shift+") - 1, false, bound)) { + if (ImGui::MenuItem(load_name, + hotkey + sizeof("Shift+") - 1, false, + bound_snapshot.has_value())) { ActionActivateBoundSnapshot(i, false); } - if (ImGui::MenuItem(save_name, hotkey, false, bound)) { + if (ImGui::MenuItem(save_name, hotkey, false, + bound_snapshot.has_value())) { ActionActivateBoundSnapshot(i, true); } diff --git a/ui/xui/snapshot-manager.cc b/ui/xui/snapshot-manager.cc index 5362e3f00f9..eae0c8e7657 100644 --- a/ui/xui/snapshot-manager.cc +++ b/ui/xui/snapshot-manager.cc @@ -17,9 +17,10 @@ // along with this program. If not, see . // +#include "ui/xemu-settings.h" +#include "snapshot-manager.hh" #include "common.hh" #include "notifications.hh" -#include "snapshot-manager.hh" #include "xemu-hud.h" SnapshotManager g_snapshot_mgr; @@ -106,6 +107,41 @@ void SnapshotManager::LoadSnapshot(const char *name) } } +static const char **snapshot_shortcut_index_key_map[] = { + &g_config.general.snapshots.shortcuts.f5, + &g_config.general.snapshots.shortcuts.f6, + &g_config.general.snapshots.shortcuts.f7, + &g_config.general.snapshots.shortcuts.f8, +}; + +static bool *snapshot_shortcut_index_present_map[] = { + &g_config.general.snapshots.shortcuts.f5_present, + &g_config.general.snapshots.shortcuts.f6_present, + &g_config.general.snapshots.shortcuts.f7_present, + &g_config.general.snapshots.shortcuts.f8_present, +}; + +std::optional SnapshotManager::GetSnapshotShortcut(int index) +{ + assert(index >= 0 && index < 4); + bool bound = *snapshot_shortcut_index_present_map[index] || + **(snapshot_shortcut_index_key_map[index]) != 0; + if (!bound) + return std::nullopt; + + return *(snapshot_shortcut_index_key_map[index]); +} + +void SnapshotManager::SetSnapshotShortcut( + int index, std::optional snapshot_name) +{ + assert(index >= 0 && index < 4); + + *(snapshot_shortcut_index_present_map[index]) = snapshot_name.has_value(); + xemu_settings_set_string(snapshot_shortcut_index_key_map[index], + snapshot_name ? *snapshot_name : ""); +} + void SnapshotManager::Draw() { DrawSnapshotDiscLoadDialog(); diff --git a/ui/xui/snapshot-manager.hh b/ui/xui/snapshot-manager.hh index 6966d85f85d..2074fe52aa9 100644 --- a/ui/xui/snapshot-manager.hh +++ b/ui/xui/snapshot-manager.hh @@ -18,8 +18,9 @@ // #pragma once -#include #include "../xemu-snapshots.h" +#include +#include class SnapshotManager { @@ -40,6 +41,10 @@ public: void LoadSnapshot(const char *name); void LoadSnapshotChecked(const char *name); + std::optional GetSnapshotShortcut(int index); + void SetSnapshotShortcut(int index, + std::optional snapshot_name); + void Draw(); void DrawSnapshotDiscLoadDialog(); };