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 Jan 25, 2023
1 parent 0d0e92c commit 8732652
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Emulation fixes:
Misc:
- GB Serialize: Add missing savestate support for MBC6 and NT (newer)
- GBA: Improve detection of valid ELF ROMs
- Qt: Detect failed startup and prompt for safe mode

0.10.1: (2023-01-10)
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 @@ -353,6 +374,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
16 changes: 12 additions & 4 deletions src/platform/qt/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,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 @@ -721,6 +721,7 @@ void Window::showEvent(QShowEvent* event) {
}
reloadDisplayDriver();
setFocus();
m_config->setLoadingComplete();
}

void Window::hideEvent(QHideEvent* event) {
Expand Down Expand Up @@ -2176,3 +2177,10 @@ void WindowBackground::paintEvent(QPaintEvent* event) {
QRect full(clampSize(QSize(m_aspectWidth, m_aspectHeight), size(), true, false));
painter.drawPixmap(full, logo);
}

bool Window::promptForSafeMode() {
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);
return choice == QMessageBox::Yes;
}
2 changes: 2 additions & 0 deletions src/platform/qt/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Q_OBJECT

InputController* inputController() { return &m_inputController; }

static bool promptForSafeMode();

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

if (configController.getQtOption("safeModeWarning").toBool()) {
if (Window::promptForSafeMode()) {
configController.setSafeMode();
} else {
configController.declineSafeMode();
}
}

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

0 comments on commit 8732652

Please sign in to comment.