Skip to content

Commit

Permalink
avoid spawning message boxes from the emuthread
Browse files Browse the repository at this point in the history
  • Loading branch information
Arisotura committed Nov 18, 2024
1 parent 259eb4b commit 317b915
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 46 deletions.
37 changes: 21 additions & 16 deletions src/frontend/qt_sdl/EmuInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <fstream>

#include <QDateTime>
#include <QMessageBox>

#include <zstd.h>
#ifdef ARCHIVE_SUPPORT_ENABLED
Expand Down Expand Up @@ -1457,16 +1456,22 @@ void EmuInstance::reset()
}


bool EmuInstance::bootToMenu()
bool EmuInstance::bootToMenu(QString& errorstr)
{
// Keep whatever cart is in the console, if any.
if (!updateConsole())
{
// Try to update the console, but keep the existing cart. If that fails...
errorstr = "Failed to boot the firmware.";
return false;
}

// BIOS and firmware files are loaded, patched, and installed in UpdateConsole
if (nds->NeedsDirectBoot())
{
errorstr = "This firmware is not bootable.";
return false;
}

initFirmwareSaveManager();
nds->Reset();
Expand Down Expand Up @@ -1843,7 +1848,7 @@ QString EmuInstance::getSavErrorString(std::string& filepath, bool gba)
return QString::fromStdString(err1);
}

bool EmuInstance::loadROM(QStringList filepath, bool reset)
bool EmuInstance::loadROM(QStringList filepath, bool reset, QString& errorstr)
{
unique_ptr<u8[]> filedata = nullptr;
u32 filelen;
Expand All @@ -1852,7 +1857,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)

if (!loadROMData(filepath, filedata, filelen, basepath, romname))
{
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM.");
errorstr = "Failed to load the DS ROM.";
return false;
}

Expand All @@ -1874,15 +1879,15 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
{
if (!Platform::CheckFileWritable(origsav))
{
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(origsav, false));
errorstr = getSavErrorString(origsav, false);
return false;
}

sav = Platform::OpenFile(origsav, FileMode::Read);
}
else if (!Platform::CheckFileWritable(savname))
{
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(savname, false));
errorstr = getSavErrorString(savname, false);
return false;
}

Expand All @@ -1909,7 +1914,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)
if (!cart)
{
// If we couldn't parse the ROM...
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM.");
errorstr = "Failed to load the DS ROM.";
return false;
}

Expand All @@ -1920,7 +1925,7 @@ bool EmuInstance::loadROM(QStringList filepath, bool reset)

if (!updateConsole())
{
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the DS ROM.");
errorstr = "Failed to load the DS ROM.";
return false;
}

Expand Down Expand Up @@ -1996,11 +2001,11 @@ QString EmuInstance::cartLabel()
}


bool EmuInstance::loadGBAROM(QStringList filepath)
bool EmuInstance::loadGBAROM(QStringList filepath, QString& errorstr)
{
if (consoleType == 1)
{
QMessageBox::critical(mainWindow, "melonDS", "The DSi doesn't have a GBA slot.");
errorstr = "The DSi doesn't have a GBA slot.";
return false;
}

Expand All @@ -2011,7 +2016,7 @@ bool EmuInstance::loadGBAROM(QStringList filepath)

if (!loadROMData(filepath, filedata, filelen, basepath, romname))
{
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the GBA ROM.");
errorstr = "Failed to load the GBA ROM.";
return false;
}

Expand All @@ -2033,15 +2038,15 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
{
if (!Platform::CheckFileWritable(origsav))
{
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(origsav, true));
errorstr = getSavErrorString(origsav, true);
return false;
}

sav = Platform::OpenFile(origsav, FileMode::Read);
}
else if (!Platform::CheckFileWritable(savname))
{
QMessageBox::critical(mainWindow, "melonDS", getSavErrorString(savname, true));
errorstr = getSavErrorString(savname, true);
return false;
}

Expand All @@ -2061,7 +2066,7 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
auto cart = GBACart::ParseROM(std::move(filedata), filelen, std::move(savedata), savelen, this);
if (!cart)
{
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the GBA ROM.");
errorstr = "Failed to load the GBA ROM.";
return false;
}

Expand All @@ -2080,14 +2085,14 @@ bool EmuInstance::loadGBAROM(QStringList filepath)
return true;
}

void EmuInstance::loadGBAAddon(int type)
void EmuInstance::loadGBAAddon(int type, QString& errorstr)
{
if (consoleType == 1) return;

auto cart = GBACart::LoadAddon(type, this);
if (!cart)
{
QMessageBox::critical(mainWindow, "melonDS", "Failed to load the GBA addon.");
errorstr = "Failed to load the GBA addon.";
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/frontend/qt_sdl/EmuInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class EmuInstance
std::optional<melonDS::FATStorage> loadSDCard(const std::string& key) noexcept;
void setBatteryLevels();
void reset();
bool bootToMenu();
bool bootToMenu(QString& errorstr);
melonDS::u32 decompressROM(const melonDS::u8* inContent, const melonDS::u32 inSize, std::unique_ptr<melonDS::u8[]>& outContent);
void clearBackupState();
std::pair<std::unique_ptr<melonDS::Firmware>, std::string> generateDefaultFirmware();
Expand All @@ -191,13 +191,13 @@ class EmuInstance

bool loadROMData(const QStringList& filepath, std::unique_ptr<melonDS::u8[]>& filedata, melonDS::u32& filelen, std::string& basepath, std::string& romname) noexcept;
QString getSavErrorString(std::string& filepath, bool gba);
bool loadROM(QStringList filepath, bool reset);
bool loadROM(QStringList filepath, bool reset, QString& errorstr);
void ejectCart();
bool cartInserted();
QString cartLabel();

bool loadGBAROM(QStringList filepath);
void loadGBAAddon(int type);
bool loadGBAROM(QStringList filepath, QString& errorstr);
void loadGBAAddon(int type, QString& errorstr);
void ejectGBACart();
bool gbaCartInserted();
QString gbaAddonName(int addon);
Expand Down
28 changes: 19 additions & 9 deletions src/frontend/qt_sdl/EmuThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ void EmuThread::handleMessages()

case msg_BootROM:
msgResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), true))
if (!emuInstance->loadROM(msg.param.value<QStringList>(), true, msgError))
break;

assert(emuInstance->nds != nullptr);
Expand All @@ -587,7 +587,7 @@ void EmuThread::handleMessages()

case msg_BootFirmware:
msgResult = 0;
if (!emuInstance->bootToMenu())
if (!emuInstance->bootToMenu(msgError))
break;

assert(emuInstance->nds != nullptr);
Expand All @@ -597,7 +597,7 @@ void EmuThread::handleMessages()

case msg_InsertCart:
msgResult = 0;
if (!emuInstance->loadROM(msg.param.value<QStringList>(), false))
if (!emuInstance->loadROM(msg.param.value<QStringList>(), false, msgError))
break;

msgResult = 1;
Expand All @@ -609,15 +609,15 @@ void EmuThread::handleMessages()

case msg_InsertGBACart:
msgResult = 0;
if (!emuInstance->loadGBAROM(msg.param.value<QStringList>()))
if (!emuInstance->loadGBAROM(msg.param.value<QStringList>(), msgError))
break;

msgResult = 1;
break;

case msg_InsertGBAAddon:
msgResult = 0;
emuInstance->loadGBAAddon(msg.param.value<int>());
emuInstance->loadGBAAddon(msg.param.value<int>(), msgError);
msgResult = 1;
break;

Expand Down Expand Up @@ -753,36 +753,45 @@ bool EmuThread::emuIsActive()
return emuActive;
}

int EmuThread::bootROM(const QStringList& filename)
int EmuThread::bootROM(const QStringList& filename, QString& errorstr)
{
sendMessage({.type = msg_BootROM, .param = filename});
waitMessage();
if (!msgResult)
{
errorstr = msgError;
return msgResult;
}

sendMessage(msg_EmuRun);
waitMessage();
errorstr = "";
return msgResult;
}

int EmuThread::bootFirmware()
int EmuThread::bootFirmware(QString& errorstr)
{
sendMessage(msg_BootFirmware);
waitMessage();
if (!msgResult)
{
errorstr = msgError;
return msgResult;
}

sendMessage(msg_EmuRun);
waitMessage();
errorstr = "";
return msgResult;
}

int EmuThread::insertCart(const QStringList& filename, bool gba)
int EmuThread::insertCart(const QStringList& filename, bool gba, QString& errorstr)
{
MessageType msgtype = gba ? msg_InsertGBACart : msg_InsertCart;

sendMessage({.type = msgtype, .param = filename});
waitMessage();
errorstr = msgResult ? "" : msgError;
return msgResult;
}

Expand All @@ -792,10 +801,11 @@ void EmuThread::ejectCart(bool gba)
waitMessage();
}

int EmuThread::insertGBAAddon(int type)
int EmuThread::insertGBAAddon(int type, QString& errorstr)
{
sendMessage({.type = msg_InsertGBAAddon, .param = type});
waitMessage();
errorstr = msgResult ? "" : msgError;
return msgResult;
}

Expand Down
9 changes: 5 additions & 4 deletions src/frontend/qt_sdl/EmuThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ class EmuThread : public QThread
void emuFrameStep();
void emuReset();

int bootROM(const QStringList& filename);
int bootFirmware();
int insertCart(const QStringList& filename, bool gba);
int bootROM(const QStringList& filename, QString& errorstr);
int bootFirmware(QString& errorstr);
int insertCart(const QStringList& filename, bool gba, QString& errorstr);
void ejectCart(bool gba);
int insertGBAAddon(int type);
int insertGBAAddon(int type, QString& errorstr);

int saveState(const QString& filename);
int loadState(const QString& filename);
Expand Down Expand Up @@ -179,6 +179,7 @@ class EmuThread : public QThread
int emuPauseStack;

int msgResult = 0;
QString msgError;

QMutex msgMutex;
QSemaphore msgSemaphore;
Expand Down
Loading

0 comments on commit 317b915

Please sign in to comment.