Skip to content

Commit

Permalink
detect failed startups and prompt for safe mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ahigerd committed Apr 27, 2023
1 parent ba6865b commit 2883b62
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Misc:
- GBA: Improve detection of valid ELF ROMs
- mGUI: Enable auto-softpatching (closes mgba.io/i/2899)
- Scripting: Add `callbacks:oneshot` for single-call callbacks
- Qt: Detect failed startup and prompt for safe mode

0.10.2: (2023-04-23)
Emulation fixes:
Expand Down
35 changes: 35 additions & 0 deletions src/platform/qt/ConfigController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ActionMapper.h"
#include "CoreController.h"
#include "Display.h"

#include <QDir>
#include <QMenu>
Expand Down Expand Up @@ -114,6 +115,16 @@ ConfigController::ConfigController(QObject* parent)
fileName.append("qt.ini");
m_settings = std::make_unique<QSettings>(fileName, QSettings::IniFormat);

if (getQtOption("loadStarted").toBool()) {
// If the loadStarted flag is lingering in qt.ini, then that indicates that
// the frontend crashed before clearing the flag.
// Prompt the user to enter into a safe mode to try to get safely started up.
setQtOption("safeModeWarning", true);
} else {
setQtOption("loadStarted", true);
m_settings->sync();
}

mCoreConfigInit(&m_config, PORT);

m_opts.audioSync = CoreController::AUDIO_SYNC;
Expand Down Expand Up @@ -306,6 +317,16 @@ void ConfigController::setQtOption(const QString& key, const QVariant& value, co
}
}

void ConfigController::removeQtOption(const QString& key, const QString& group) {
if (!group.isNull()) {
m_settings->beginGroup(group);
}
m_settings->remove(key);
if (!group.isNull()) {
m_settings->endGroup();
}
}

QStringList ConfigController::getMRU(ConfigController::MRU mruType) const {
QStringList mru;
m_settings->beginGroup(mruName(mruType));
Expand Down Expand Up @@ -354,6 +375,20 @@ void ConfigController::write() {
mCoreConfigMap(&m_config, &m_opts);
}

void ConfigController::setLoadingComplete() {
removeQtOption("loadStarted");
write();
}

void ConfigController::setSafeMode() {
setQtOption("displayDriver", static_cast<int>(Display::Driver::QT));
removeQtOption("safeModeWarning");
}

void ConfigController::declineSafeMode() {
removeQtOption("safeModeWarning");
}

void ConfigController::makePortable() {
mCoreConfigMakePortable(&m_config);

Expand Down
7 changes: 6 additions & 1 deletion src/platform/qt/ConfigController.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Q_OBJECT
const mGraphicsOpts* graphicsOpts() const { return &m_graphicsOpts; }
void usage(const char* arg0) const;

void setLoadingComplete();
void setSafeMode();
void declineSafeMode();

static const QString& configDir();
static const QString& cacheDir();
static bool isPortable();
Expand All @@ -115,6 +119,7 @@ public slots:
void setOption(const char* key, const char* value);
void setOption(const char* key, const QVariant& value);
void setQtOption(const QString& key, const QVariant& value, const QString& group = QString());
void removeQtOption(const QString& key, const QString& group = QString());

void makePortable();
void write();
Expand All @@ -130,7 +135,7 @@ public slots:
mGraphicsOpts m_graphicsOpts{};
std::array<mSubParser, 2> m_subparsers;
bool m_parsed = false;

QHash<QString, QVariant> m_argvOptions;
QHash<QString, ConfigOption*> m_optionSet;
std::unique_ptr<QSettings> m_settings;
Expand Down
14 changes: 14 additions & 0 deletions src/platform/qt/GBAApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QFileOpenEvent>
#include <QFontDatabase>
#include <QIcon>
#include <QMessageBox>

#include <mgba/core/version.h>
#include <mgba/feature/updater.h>
Expand Down Expand Up @@ -384,6 +385,19 @@ void GBAApp::finishJob(qint64 jobId) {
m_workerJobCallbacks.remove(jobId);
}

void GBAApp::checkSafeMode() {
if (m_configController->getQtOption("safeModeWarning").toBool()) {
int choice = QMessageBox::warning(nullptr, tr("Safe Mode"),
tr("mGBA detected a problem while starting.\n\nWould you like to reset settings to failsafe values?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (choice == QMessageBox::Yes) {
m_configController->setSafeMode();
} else {
m_configController->declineSafeMode();
}
}
}

GBAApp::WorkerJob::WorkerJob(qint64 id, std::function<void ()> job, GBAApp* owner)
: m_id(id)
, m_job(job)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/qt/GBAApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Q_OBJECT
ApplicationUpdater* updater() { return &m_updater; }
QString invokeOnExit() { return m_invokeOnExit; }

void checkSafeMode();

public slots:
void restartForUpdate();
Window* newWindow();
Expand Down
9 changes: 5 additions & 4 deletions src/platform/qt/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ Window::Window(CoreManager* manager, ConfigController* config, int playerId, QWi
if (value.toBool()) {
attachWidget(m_libraryView);
} else {
attachWidget(m_screenWidget);
attachWidget(m_screenWidget);
}
}
}, this);
m_config->updateOption("showLibrary");

ConfigOption* showFilenameInLibrary = m_config->addOption("showFilenameInLibrary");
showFilenameInLibrary->connect([this](const QVariant& value) {
m_libraryView->setShowFilename(value.toBool());
}, this);
m_config->updateOption("showFilenameInLibrary");
m_libraryView->setShowFilename(value.toBool());
}, this);
m_config->updateOption("showFilenameInLibrary");
ConfigOption* libraryStyle = m_config->addOption("libraryStyle");
libraryStyle->connect([this](const QVariant& value) {
m_libraryView->setViewStyle(static_cast<LibraryStyle>(value.toInt()));
Expand Down Expand Up @@ -734,6 +734,7 @@ void Window::showEvent(QShowEvent* event) {
}
reloadDisplayDriver();
setFocus();
m_config->setLoadingComplete();
}

void Window::hideEvent(QHideEvent* event) {
Expand Down
2 changes: 2 additions & 0 deletions src/platform/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ int main(int argc, char* argv[]) {
langTranslator.load(locale, binaryName, "-", ":/translations/");
application.installTranslator(&langTranslator);

application.checkSafeMode();

Window* w = application.newWindow();
w->loadConfig();
w->argumentsPassed();
Expand Down

0 comments on commit 2883b62

Please sign in to comment.