From 2ebb21ce3bcf1a445dce7b13f64e44d6098a3acd Mon Sep 17 00:00:00 2001
From: Arisotura <thetotalworm@gmail.com>
Date: Wed, 20 May 2020 22:22:22 +0200
Subject: hook up pause and reset, w/ relevant hotkeys

---
 src/frontend/FrontendUtil.h  |  3 ++
 src/frontend/Util_ROM.cpp    | 47 +++++++++++++++++++++-
 src/frontend/qt_sdl/main.cpp | 93 +++++++++++++++++++++++---------------------
 src/frontend/qt_sdl/main.h   | 10 +++--
 4 files changed, 104 insertions(+), 49 deletions(-)

(limited to 'src')

diff --git a/src/frontend/FrontendUtil.h b/src/frontend/FrontendUtil.h
index f4f6850..d838b63 100644
--- a/src/frontend/FrontendUtil.h
+++ b/src/frontend/FrontendUtil.h
@@ -65,6 +65,9 @@ int LoadBIOS();
 // note: loading a ROM to the NDS slot resets emulation
 int LoadROM(const char* file, int slot);
 
+// reset execution of the current ROM
+int Reset();
+
 // get the filename associated with the given savestate slot (1-8)
 void GetSavestateName(int slot, char* filename, int len);
 
diff --git a/src/frontend/Util_ROM.cpp b/src/frontend/Util_ROM.cpp
index 3200de4..e13eb2c 100644
--- a/src/frontend/Util_ROM.cpp
+++ b/src/frontend/Util_ROM.cpp
@@ -50,6 +50,11 @@ void Init_ROM()
     memset(PrevSRAMPath[ROMSlot_GBA], 0, 1024);
 }
 
+// TODO: currently, when failing to load a ROM for whatever reason, we attempt
+// to revert to the previous state and resume execution; this may not be a very
+// good thing, depending on what state the core was left in.
+// should we do a better state revert (via the savestate system)? completely stop?
+
 void SetupSRAMPath(int slot)
 {
     strncpy(SRAMPath[slot], ROMPath[slot], 1023);
@@ -184,7 +189,7 @@ int LoadROM(const char* file, int slot)
     }
     else if (slot == ROMSlot_GBA && NDS::LoadGBAROM(ROMPath[slot], SRAMPath[slot]))
     {
-        SavestateLoaded = false;
+        SavestateLoaded = false; // checkme??
 
         strncpy(PrevSRAMPath[slot], SRAMPath[slot], 1024); // safety
         return Load_OK;
@@ -197,6 +202,46 @@ int LoadROM(const char* file, int slot)
     }
 }
 
+int Reset()
+{
+    int res;
+    bool directboot = Config::DirectBoot != 0;
+
+    res = VerifyDSBIOS();
+    if (res != Load_OK) return res;
+
+    res = VerifyDSFirmware();
+    if (res != Load_OK)
+    {
+        if (res == Load_FirmwareNotBootable)
+            directboot = true;
+        else
+            return res;
+    }
+
+    SavestateLoaded = false;
+
+    if (ROMPath[ROMSlot_NDS][0] == '\0')
+    {
+        NDS::LoadBIOS();
+    }
+    else
+    {
+        SetupSRAMPath(0);
+        if (!NDS::LoadROM(ROMPath[ROMSlot_NDS], SRAMPath[ROMSlot_NDS], directboot))
+            return Load_ROMLoadError;
+    }
+
+    if (ROMPath[ROMSlot_GBA][0] != '\0')
+    {
+        SetupSRAMPath(1);
+        if (!NDS::LoadGBAROM(ROMPath[ROMSlot_GBA], SRAMPath[ROMSlot_GBA]))
+            return Load_ROMLoadError;
+    }
+
+    return Load_OK;
+}
+
 
 // SAVESTATE TODO
 // * configurable paths. not everyone wants their ROM directory to be polluted, I guess.
diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp
index 35eac70..a73362d 100644
--- a/src/frontend/qt_sdl/main.cpp
+++ b/src/frontend/qt_sdl/main.cpp
@@ -113,6 +113,8 @@ EmuThread::EmuThread(QObject* parent) : QThread(parent)
     connect(this, SIGNAL(windowTitleChange(QString)), mainWindow, SLOT(onTitleUpdate(QString)));
     connect(this, SIGNAL(windowEmuStart()), mainWindow, SLOT(onEmuStart()));
     connect(this, SIGNAL(windowEmuStop()), mainWindow, SLOT(onEmuStop()));
+    connect(this, SIGNAL(windowEmuPause()), mainWindow->actPause, SLOT(trigger()));
+    connect(this, SIGNAL(windowEmuReset()), mainWindow->actReset, SLOT(trigger()));
 
     emit windowEmuStop();
 }
@@ -152,14 +154,10 @@ void EmuThread::run()
     {
         Input::Process();
 
-        /*if (Input::HotkeyPressed(HK_FastForwardToggle))
-        {
-            Config::LimitFPS = !Config::LimitFPS;
-            // TODO: reflect in UI!
-        }*/
+        if (Input::HotkeyPressed(HK_FastForwardToggle)) emit windowLimitFPSChange();
 
-        //if (Input::HotkeyPressed(HK_Pause)) uiQueueMain(TogglePause, NULL);
-        //if (Input::HotkeyPressed(HK_Reset)) uiQueueMain(Reset, NULL);
+        if (Input::HotkeyPressed(HK_Pause)) emit windowEmuPause();
+        if (Input::HotkeyPressed(HK_Reset)) emit windowEmuReset();
 
         if (GBACart::CartInserted && GBACart::HasSolarSensor)
         {
@@ -318,18 +316,15 @@ void EmuThread::run()
             lastmeasuretick = lasttick;
             fpslimitcount = 0;
 
-            if (EmuRunning == 2)
+            /*if (Screen_UseGL)
             {
-                /*if (Screen_UseGL)
-                {
-                    uiGLBegin(GLContext);
-                    uiGLMakeContextCurrent(GLContext);
-                    GLScreen_DrawScreen();
-                    uiGLEnd(GLContext);
-                }
-                uiAreaQueueRedrawAll(MainDrawArea);*/
-                mainWindow->update();
+                uiGLBegin(GLContext);
+                uiGLMakeContextCurrent(GLContext);
+                GLScreen_DrawScreen();
+                uiGLEnd(GLContext);
             }
+            uiAreaQueueRedrawAll(MainDrawArea);*/
+            mainWindow->update();
 
             //if (Screen_UseGL) uiGLMakeContextCurrent(NULL);
 
@@ -338,7 +333,7 @@ void EmuThread::run()
             sprintf(melontitle, "melonDS " MELONDS_VERSION);
             changeWindowTitle(melontitle);
 
-            SDL_Delay(100);
+            SDL_Delay(75);
         }
     }
 
@@ -375,12 +370,11 @@ void EmuThread::emuRun()
     if (audioDevice) SDL_PauseAudioDevice(audioDevice, 0);
 }
 
-void EmuThread::emuPause(bool refresh)
+void EmuThread::emuPause()
 {
-    int status = refresh ? 2:3;
     PrevEmuStatus = EmuRunning;
-    EmuRunning = status;
-    while (EmuStatus != status);
+    EmuRunning = 2;
+    while (EmuStatus != 2);
 
     //emit windowEmuPause();
     if (audioDevice) SDL_PauseAudioDevice(audioDevice, 1);
@@ -774,7 +768,7 @@ void MainWindow::dropEvent(QDropEvent* event)
     QList<QUrl> urls = event->mimeData()->urls();
     if (urls.count() > 1) return; // not handling more than one file at once
 
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     QString filename = urls.at(0).toLocalFile();
     QString ext = filename.right(3);
@@ -836,7 +830,7 @@ QString MainWindow::loadErrorStr(int error)
 
 void MainWindow::onOpenFile()
 {
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     QString filename = QFileDialog::getOpenFileName(this,
                                                     "Open ROM",
@@ -897,7 +891,7 @@ void MainWindow::onBootFirmware()
 {
     // TODO: check the whole GBA cart shito
 
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     int res = Frontend::LoadBIOS();
     if (res != Frontend::Load_OK)
@@ -917,7 +911,7 @@ void MainWindow::onSaveState()
 {
     int slot = ((QAction*)sender())->data().toInt();
 
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     char filename[1024];
     if (slot > 0)
@@ -958,7 +952,7 @@ void MainWindow::onLoadState()
 {
     int slot = ((QAction*)sender())->data().toInt();
 
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     char filename[1024];
     if (slot > 0)
@@ -1006,7 +1000,7 @@ void MainWindow::onLoadState()
 
 void MainWindow::onUndoStateLoad()
 {
-    emuThread->emuPause(true);
+    emuThread->emuPause();
     Frontend::UndoStateLoad();
     emuThread->emuUnpause();
 }
@@ -1019,9 +1013,11 @@ void MainWindow::onQuit()
 
 void MainWindow::onPause(bool checked)
 {
-    if (emuThread->emuIsRunning())
+    if (!RunningSomething) return;
+
+    if (checked)
     {
-        emuThread->emuPause(true);
+        emuThread->emuPause();
     }
     else
     {
@@ -1031,7 +1027,26 @@ void MainWindow::onPause(bool checked)
 
 void MainWindow::onReset()
 {
-    //
+    if (!RunningSomething) return;
+
+    emuThread->emuPause();
+
+    actUndoStateLoad->setEnabled(false);
+
+    int res = Frontend::Reset();
+    if (res != Frontend::Load_OK)
+    {
+        QMessageBox::critical(this,
+                              "melonDS",
+                              loadErrorStr(res));
+        emuThread->emuUnpause();
+    }
+    else
+    {
+        // OSD::AddMessage(0, "Reset");
+
+        emuThread->emuRun();
+    }
 }
 
 void MainWindow::onStop()
@@ -1047,7 +1062,7 @@ void MainWindow::onOpenEmuSettings()
 
 void MainWindow::onOpenInputConfig()
 {
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     InputConfigDialog* dlg = InputConfigDialog::openDlg(this);
     connect(dlg, &InputConfigDialog::finished, this, &MainWindow::onInputConfigFinished);
@@ -1143,7 +1158,7 @@ void MainWindow::onEmuStart()
     }
     actSaveState[0]->setEnabled(true);
     actLoadState[0]->setEnabled(true);
-    actUndoStateLoad->setEnabled(true);
+    actUndoStateLoad->setEnabled(false);
 
     actPause->setEnabled(true);
     actPause->setChecked(false);
@@ -1165,16 +1180,6 @@ void MainWindow::onEmuStop()
     actStop->setEnabled(false);
 }
 
-void MainWindow::onEmuPause()
-{
-    //
-}
-
-void MainWindow::onEmuUnpause()
-{
-    //
-}
-
 
 
 int main(int argc, char** argv)
@@ -1281,7 +1286,7 @@ int main(int argc, char** argv)
 
     emuThread = new EmuThread();
     emuThread->start();
-    emuThread->emuPause(true);
+    emuThread->emuPause();
 
     if (argc > 1)
     {
diff --git a/src/frontend/qt_sdl/main.h b/src/frontend/qt_sdl/main.h
index b478f4e..22f045f 100644
--- a/src/frontend/qt_sdl/main.h
+++ b/src/frontend/qt_sdl/main.h
@@ -38,7 +38,7 @@ public:
 
     // to be called from the UI thread
     void emuRun();
-    void emuPause(bool refresh);
+    void emuPause();
     void emuUnpause();
     void emuStop();
 
@@ -49,7 +49,10 @@ signals:
 
     void windowEmuStart();
     void windowEmuStop();
-    void windowPauseToggle();
+    void windowEmuPause();
+    void windowEmuReset();
+
+    void windowLimitFPSChange();
 
 private:
     volatile int EmuStatus;
@@ -130,14 +133,13 @@ private slots:
 
     void onEmuStart();
     void onEmuStop();
-    void onEmuPause();
-    void onEmuUnpause();
 
 private:
     QString loadErrorStr(int error);
 
     MainWindowPanel* panel;
 
+public:
     QAction* actOpenROM;
     QAction* actBootFirmware;
     QAction* actSaveState[9];
-- 
cgit v1.2.3