Skip to content

Commit

Permalink
ui: Handle snapshots with empty names in snapshot menu
Browse files Browse the repository at this point in the history
  • Loading branch information
antangelo committed Jun 11, 2024
1 parent 2be220d commit 58e4d91
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 47 deletions.
4 changes: 4 additions & 0 deletions config_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions ui/xemu-snapshots.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions ui/xemu-snapshots.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions ui/xui/actions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
37 changes: 19 additions & 18 deletions ui/xui/main-menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <optional>

#include "../thirdparty/fatx/fatx.h"

Expand Down Expand Up @@ -1023,11 +1024,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;
}
}
}

Expand Down Expand Up @@ -1092,8 +1095,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;
}
Expand Down Expand Up @@ -1159,12 +1163,10 @@ void MainMenuSnapshotsView::DrawSnapshotContextMenu(

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();
Expand All @@ -1175,9 +1177,8 @@ void MainMenuSnapshotsView::DrawSnapshotContextMenu(

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;
}
}
Expand Down
31 changes: 18 additions & 13 deletions ui/xui/menubar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#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
Expand Down Expand Up @@ -101,25 +103,28 @@ 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);
}

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);
}

Expand Down
38 changes: 37 additions & 1 deletion ui/xui/snapshot-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

#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;
Expand Down Expand Up @@ -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<const char *> 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<const char *> 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();
Expand Down
7 changes: 6 additions & 1 deletion ui/xui/snapshot-manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
//

#pragma once
#include <string>
#include "../xemu-snapshots.h"
#include <optional>
#include <string>

class SnapshotManager
{
Expand All @@ -40,6 +41,10 @@ public:
void LoadSnapshot(const char *name);
void LoadSnapshotChecked(const char *name);

std::optional<const char *> GetSnapshotShortcut(int index);
void SetSnapshotShortcut(int index,
std::optional<const char *> snapshot_name);

void Draw();
void DrawSnapshotDiscLoadDialog();
};
Expand Down

0 comments on commit 58e4d91

Please sign in to comment.