From e51a748a05f6ed6bdac5aab99151b6401a3ecd63 Mon Sep 17 00:00:00 2001 From: harry Date: Mon, 15 Jan 2024 05:34:13 -0500 Subject: [PATCH] Qt JS engine in work. --- src/drivers/Qt/ConsoleWindow.cpp | 6 ++--- src/drivers/Qt/QtScriptManager.cpp | 41 +++++++++++++++++++++++++++++- src/drivers/Qt/QtScriptManager.h | 5 ++++ src/drivers/Qt/fceuWrapper.cpp | 4 +-- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/drivers/Qt/ConsoleWindow.cpp b/src/drivers/Qt/ConsoleWindow.cpp index b7030da2..e656939b 100644 --- a/src/drivers/Qt/ConsoleWindow.cpp +++ b/src/drivers/Qt/ConsoleWindow.cpp @@ -4597,9 +4597,9 @@ void consoleWin_t::emuFrameFinish(void) //printf("EMU Frame Finish\n"); -#ifdef __FCEU_QSCRIPT_ENABLE__ - QtScriptManager::getInstance()->frameFinishedUpdate(); -#endif +//#ifdef __FCEU_QSCRIPT_ENABLE__ +// QtScriptManager::getInstance()->frameFinishedUpdate(); +//#endif transferVideoBuffer(); } diff --git a/src/drivers/Qt/QtScriptManager.cpp b/src/drivers/Qt/QtScriptManager.cpp index bf053827..fd084f6f 100644 --- a/src/drivers/Qt/QtScriptManager.cpp +++ b/src/drivers/Qt/QtScriptManager.cpp @@ -240,6 +240,8 @@ int QtScriptInstance::configEngine() engine->globalObject().setProperty("gui", guiObject); + QtScriptManager::getInstance()->removeFrameFinishedConnection(this); + onFrameFinishCallback = QJSValue(); onScriptStopCallback = QJSValue(); @@ -277,6 +279,10 @@ int QtScriptInstance::loadScriptFile( QString filepath ) onFrameFinishCallback = engine->globalObject().property("onFrameFinish"); onScriptStopCallback = engine->globalObject().property("onScriptStop"); + if (onFrameFinishCallback.isCallable()) + { + QtScriptManager::getInstance()->addFrameFinishedConnection(this); + } return 0; } //---------------------------------------------------- @@ -513,12 +519,45 @@ void QtScriptManager::removeScriptInstance(QtScriptInstance* script) it++; } } + + removeFrameFinishedConnection(script); +} +//---------------------------------------------------- +void QtScriptManager::addFrameFinishedConnection(QtScriptInstance* script) +{ + if (frameFinishConnectList.size() == 0) + { + connect(consoleWindow->emulatorThread, SIGNAL(frameFinished(void)), this, SLOT(frameFinishedUpdate(void)), Qt::BlockingQueuedConnection); + } + frameFinishConnectList.push_back(script); +} +//---------------------------------------------------- +void QtScriptManager::removeFrameFinishedConnection(QtScriptInstance* script) +{ + auto it = frameFinishConnectList.begin(); + + while (it != frameFinishConnectList.end()) + { + if (*it == script) + { + it = frameFinishConnectList.erase(it); + } + else + { + it++; + } + } + + if (frameFinishConnectList.size() == 0) + { + consoleWindow->emulatorThread->disconnect( SIGNAL(frameFinished(void)), this, SLOT(frameFinishedUpdate(void))); + } } //---------------------------------------------------- void QtScriptManager::frameFinishedUpdate() { FCEU_WRAPPER_LOCK(); - for (auto script : scriptList) + for (auto script : frameFinishConnectList) { script->onFrameFinish(); } diff --git a/src/drivers/Qt/QtScriptManager.h b/src/drivers/Qt/QtScriptManager.h index 34cf34c8..6e8690e3 100644 --- a/src/drivers/Qt/QtScriptManager.h +++ b/src/drivers/Qt/QtScriptManager.h @@ -113,12 +113,17 @@ public: void addScriptInstance(QtScriptInstance* script); void removeScriptInstance(QtScriptInstance* script); + void addFrameFinishedConnection(QtScriptInstance* script); + void removeFrameFinishedConnection(QtScriptInstance* script); private: static QtScriptManager* _instance; QList scriptList; + QList frameFinishConnectList; FCEU::timeStampRecord lastFrameUpdate; + int frameFinishedConnectCount = 0; + public slots: void frameFinishedUpdate(); }; diff --git a/src/drivers/Qt/fceuWrapper.cpp b/src/drivers/Qt/fceuWrapper.cpp index c907b104..331c1ece 100644 --- a/src/drivers/Qt/fceuWrapper.cpp +++ b/src/drivers/Qt/fceuWrapper.cpp @@ -1481,12 +1481,12 @@ int fceuWrapperUpdate( void ) hexEditorUpdateMemoryValues(); + fceuWrapperUnLock(); + if ( consoleWindow ) { consoleWindow->emulatorThread->signalFrameFinished(); } - fceuWrapperUnLock(); - emulatorHasMutex = 0; #ifdef __FCEU_PROFILER_ENABLE__