diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 2e2d962c13..ba475e5141 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -58,6 +58,8 @@ MainUI *emugl = nullptr; static float refreshRate = 60.f; static int browseFileEvent = -1; static int browseFolderEvent = -1; +static int inputBoxEvent = -1; + QTCamera *qtcamera = nullptr; MainWindow *g_mainWindow; @@ -273,22 +275,54 @@ void System_Notify(SystemNotification notification) { } } +// TODO: Find a better version to pass parameters to HandleCustomEvent. +static std::string g_param1; +static std::string g_param2; +static int g_requestId; + +bool MainUI::HandleCustomEvent(QEvent *e) { + if (e->type() == browseFileEvent) { + QString fileName = QFileDialog::getOpenFileName(nullptr, "Load ROM", g_Config.currentDirectory.c_str(), "PSP ROMs (*.iso *.cso *.pbp *.elf *.zip *.ppdmp)"); + if (QFile::exists(fileName)) { + QDir newPath; + g_Config.currentDirectory = Path(newPath.filePath(fileName).toStdString()); + g_Config.Save("browseFileEvent"); + + NativeMessageReceived("boot", fileName.toStdString().c_str()); + } + } else if (e->type() == browseFolderEvent) { + auto mm = GetI18NCategory("MainMenu"); + QString fileName = QFileDialog::getExistingDirectory(nullptr, mm->T("Choose folder"), g_Config.currentDirectory.c_str()); + if (QDir(fileName).exists()) { + NativeMessageReceived("browse_folderSelect", fileName.toStdString().c_str()); + } + } else if (e->type() == inputBoxEvent) { + QString title = QString::fromStdString(g_param1); + QString defaultValue = QString::fromStdString(g_param2); + QString text = emugl->InputBoxGetQString(title, defaultValue); + if (text.isEmpty()) { + g_requestManager.PostSystemFailure(g_requestId); + } else { + g_requestManager.PostSystemSuccess(g_requestId, text.toStdString().c_str()); + } + } else { + return false; + } + return true; +} + bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { switch (type) { case SystemRequestType::INPUT_TEXT_MODAL: { - // NOTE: This doesn't actually work, we're on the wrong thread! - // Was broken even previously to the conversion to System_MakeRequest. - QString title = QString::fromStdString(param1); - QString defaultValue = QString::fromStdString(param2); - QString text = emugl->InputBoxGetQString(title, defaultValue); - if (text.isEmpty()) { - g_requestManager.PostSystemFailure(requestId); - } else { - g_requestManager.PostSystemSuccess(requestId, text.toStdString().c_str()); - } - break; + g_requestId = requestId; + g_param1 = param1; + g_param2 = param2; + QCoreApplication::postEvent(emugl, new QEvent((QEvent::Type)inputBoxEvent)); + return true; } + default: + return false; } } @@ -365,6 +399,7 @@ static int mainInternal(QApplication &a) { browseFileEvent = QEvent::registerEventType(); browseFolderEvent = QEvent::registerEventType(); + inputBoxEvent = QEvent::registerEventType(); int retval = a.exec(); delete emugl; @@ -576,23 +611,8 @@ bool MainUI::event(QEvent *e) { break; default: - if (e->type() == browseFileEvent) { - QString fileName = QFileDialog::getOpenFileName(nullptr, "Load ROM", g_Config.currentDirectory.c_str(), "PSP ROMs (*.iso *.cso *.pbp *.elf *.zip *.ppdmp)"); - if (QFile::exists(fileName)) { - QDir newPath; - g_Config.currentDirectory = Path(newPath.filePath(fileName).toStdString()); - g_Config.Save("browseFileEvent"); - - NativeMessageReceived("boot", fileName.toStdString().c_str()); - } - break; - } else if (e->type() == browseFolderEvent) { - auto mm = GetI18NCategory("MainMenu"); - QString fileName = QFileDialog::getExistingDirectory(nullptr, mm->T("Choose folder"), g_Config.currentDirectory.c_str()); - if (QDir(fileName).exists()) { - NativeMessageReceived("browse_folderSelect", fileName.toStdString().c_str()); - } - } else { + // Can't switch on dynamic event types. + if (!HandleCustomEvent(e)) { return QWidget::event(e); } } diff --git a/Qt/QtMain.h b/Qt/QtMain.h index 639806bcf5..39cce11f77 100644 --- a/Qt/QtMain.h +++ b/Qt/QtMain.h @@ -140,6 +140,7 @@ protected: void EmuThreadJoin(); private: + bool HandleCustomEvent(QEvent *e); QtGLGraphicsContext *graphicsContext; float xscale, yscale;