From b1ffe0ae7670ff0a6b5e9c86a18ba2b401b47617 Mon Sep 17 00:00:00 2001 From: rofl0r Date: Thu, 21 Oct 2021 18:41:13 +0000 Subject: [PATCH] GTK2: add crude "import backup" dialog to File menu this is necessary to load saves from other devices or emulators, as desmume uses its own incompatible format. it works for importing .sav files from flashcarts, but only if the file extension is .sav or .SAV - if using .dsv desmume guesses it's of its own type and looks for a specific string, then fails. the right code was taken from windows/importSave.cpp - it might make sense to add the export item at some point too, however that will probably require some more effort. --- desmume/src/frontend/posix/gtk2/main.cpp | 58 ++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/desmume/src/frontend/posix/gtk2/main.cpp b/desmume/src/frontend/posix/gtk2/main.cpp index a86456810..111dc92cd 100644 --- a/desmume/src/frontend/posix/gtk2/main.cpp +++ b/desmume/src/frontend/posix/gtk2/main.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "types.h" #include "firmware.h" @@ -127,6 +128,7 @@ static void DoQuit(); static void RecordMovieDialog(); static void PlayMovieDialog(); static void StopMovie(); +static void ImportBackupDialog(); static void OpenNdsDialog(); static void SaveStateDialog(); static void LoadStateDialog(); @@ -212,6 +214,8 @@ static const char *ui_description = " " #endif " " +" " +" " " " " " " " @@ -400,6 +404,7 @@ static const GtkActionEntry action_entries[] = { { "reset", "gtk-refresh", "Re_set", NULL, NULL, Reset }, { "savestateto", NULL, "Save state _to ...", NULL, NULL, SaveStateDialog }, { "loadstatefrom", NULL, "Load state _from ...", NULL, NULL, LoadStateDialog }, + { "importbackup", NULL, "_Import backup from ...", NULL, NULL, ImportBackupDialog }, { "recordmovie", NULL, "Record movie _to ...", NULL, NULL, RecordMovieDialog }, { "playmovie", NULL, "Play movie _from ...", NULL, NULL, PlayMovieDialog }, { "stopmovie", NULL, "Stop movie", NULL, NULL, StopMovie }, @@ -1138,6 +1143,59 @@ static void PlayMovieDialog() gtk_widget_destroy(pFileSelection); } +static void ImportBackupDialog() +{ + GtkFileFilter *pFilter_dsm, *pFilter_any; + GtkWidget *pFileSelection; + GtkWidget *pParent; + gchar *sPath; + + if (desmume_running()) + Pause(); + + pParent = GTK_WIDGET(pWindow); + + pFilter_dsm = gtk_file_filter_new(); + gtk_file_filter_add_pattern(pFilter_dsm, "*.sav"); + gtk_file_filter_set_name(pFilter_dsm, "Battery save file (.sav)"); + + pFilter_any = gtk_file_filter_new(); + gtk_file_filter_add_pattern(pFilter_any, "*"); + gtk_file_filter_set_name(pFilter_any, "All files"); + + /* Creating the selection window */ + pFileSelection = gtk_file_chooser_dialog_new("Import backup from...", + GTK_WINDOW(pParent), + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_OPEN, GTK_RESPONSE_OK, + NULL); + gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (pFileSelection), TRUE); + + /* Only the dialog window is accepting events: */ + gtk_window_set_modal(GTK_WINDOW(pFileSelection), TRUE); + + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(pFileSelection), pFilter_dsm); + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(pFileSelection), pFilter_any); + + /* Showing the window */ + switch(gtk_dialog_run(GTK_DIALOG(pFileSelection))) { + case GTK_RESPONSE_OK: + sPath = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(pFileSelection)); + struct stat st; int ret; + if(stat(sPath, &st) != -1) { + ret = MMU_new.backupDevice.importData(sPath, st.st_size); + NDS_Reset(); // reboot game + } + g_free(sPath); + break; + default: + break; + } + gtk_widget_destroy(pFileSelection); +} + + static void SaveStateDialog() { GtkFileFilter *pFilter_ds, *pFilter_any;