Skip to content

Commit

Permalink
split ControllerScriptHandler into base and subclasses
Browse files Browse the repository at this point in the history
This allows for a clean separation between new and legacy code.
  • Loading branch information
Be-ing committed Jul 12, 2020
1 parent fb05f25 commit b800259
Show file tree
Hide file tree
Showing 19 changed files with 731 additions and 720 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,11 @@ add_library(mixxx-lib STATIC EXCLUDE_FROM_ALL
src/controllers/dlgprefcontrollerdlg.ui
src/controllers/dlgprefcontrollers.cpp
src/controllers/dlgprefcontrollersdlg.ui
src/controllers/scripting/controllerscripthandler.cpp
src/controllers/scripting/controllerscriptenginebase.cpp
src/controllers/scripting/controllerscriptmoduleengine.cpp
src/controllers/scripting/colormapper.cpp
src/controllers/scripting/colormapperjsproxy.cpp
src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp
src/controllers/scripting/legacy/controllerscriptinterface.cpp
src/controllers/scripting/legacy/scriptconnection.cpp
src/controllers/scripting/legacy/scriptconnectionjsproxy.cpp
Expand Down
4 changes: 3 additions & 1 deletion build/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,11 @@ def sources(self, build):
"src/controllers/delegates/midibytedelegate.cpp",
"src/controllers/delegates/midioptionsdelegate.cpp",
"src/controllers/learningutils.cpp",
"src/controllers/scripting/controllerscripthandler.cpp",
"src/controllers/scripting/controllerscriptenginebase.cpp",
"src/controllers/scripting/controllerscriptmoduleengine.cpp",
"src/controllers/scripting/colormapper.cpp",
"src/controllers/scripting/colormapperjsproxy.cpp",
"src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp",
"src/controllers/scripting/legacy/controllerscriptinterface.cpp",
"src/controllers/scripting/legacy/scriptconnection.cpp",
"src/controllers/scripting/legacy/scriptconnectionjsproxy.cpp",
Expand Down
34 changes: 15 additions & 19 deletions src/controllers/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "util/screensaver.h"

Controller::Controller()
: m_pScriptHandler(nullptr),
: m_pScriptEngineLegacy(nullptr),
m_bIsOutputDevice(false),
m_bIsInputDevice(false),
m_bIsOpen(false),
Expand All @@ -34,22 +34,21 @@ ControllerJSProxy* Controller::jsProxy() {
void Controller::startEngine()
{
controllerDebug(" Starting engine");
if (m_pScriptHandler != NULL) {
if (m_pScriptEngineLegacy != NULL) {
qWarning() << "Controller: Engine already exists! Restarting:";
stopEngine();
}
m_pScriptHandler = new ControllerScriptHandler(this);
m_pScriptEngineLegacy = new ControllerScriptEngineLegacy(this);
}

void Controller::stopEngine() {
controllerDebug(" Shutting down engine");
if (m_pScriptHandler == NULL) {
if (m_pScriptEngineLegacy == nullptr) {
qWarning() << "Controller::stopEngine(): No engine exists!";
return;
}
m_pScriptHandler->gracefulShutdown();
delete m_pScriptHandler;
m_pScriptHandler = NULL;
delete m_pScriptEngineLegacy;
m_pScriptEngineLegacy = nullptr;
}

bool Controller::applyPreset(bool initializeScripts) {
Expand All @@ -58,7 +57,7 @@ bool Controller::applyPreset(bool initializeScripts) {
const ControllerPreset* pPreset = preset();

// Load the script code into the engine
if (m_pScriptHandler == NULL) {
if (m_pScriptEngineLegacy == NULL) {
qWarning() << "Controller::applyPreset(): No engine exists!";
return false;
}
Expand All @@ -69,14 +68,13 @@ bool Controller::applyPreset(bool initializeScripts) {
return true;
}

bool success = m_pScriptHandler->loadScriptFiles(scriptFiles);
if (success && initializeScripts) {
m_pScriptHandler->initializeScripts(scriptFiles);
}
bool success = true;

m_pScriptEngineLegacy->setScriptFiles(scriptFiles);
if (initializeScripts) {
m_pScriptHandler->loadModule(pPreset->moduleFileInfo());
success = m_pScriptEngineLegacy->initialize();
}

return success;
}

Expand Down Expand Up @@ -114,7 +112,7 @@ void Controller::triggerActivity()
}
}
void Controller::receive(const QByteArray data, mixxx::Duration timestamp) {
if (m_pScriptHandler == NULL) {
if (m_pScriptEngineLegacy == NULL) {
//qWarning() << "Controller::receive called with no active engine!";
// Don't complain, since this will always show after closing a device as
// queued signals flush out
Expand All @@ -138,14 +136,12 @@ void Controller::receive(const QByteArray data, mixxx::Duration timestamp) {
controllerDebug(message);
}

foreach (QString function, m_pScriptHandler->getScriptFunctionPrefixes()) {
foreach (QString function, m_pScriptEngineLegacy->getScriptFunctionPrefixes()) {
if (function == "") {
continue;
}
function.append(".incomingData");
QJSValue incomingDataFunction = m_pScriptHandler->wrapFunctionCode(function, 2);
m_pScriptHandler->executeFunction(incomingDataFunction, data);
QJSValue incomingDataFunction = m_pScriptEngineLegacy->wrapFunctionCode(function, 2);
m_pScriptEngineLegacy->executeIncomingDataFunction(incomingDataFunction, data);
}

m_pScriptHandler->handleInput(data, timestamp);
}
8 changes: 4 additions & 4 deletions src/controllers/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "controllers/controllerpresetinfo.h"
#include "controllers/controllerpresetvisitor.h"
#include "controllers/controllervisitor.h"
#include "controllers/scripting/controllerscripthandler.h"
#include "controllers/scripting/legacy/controllerscriptenginelegacy.h"
#include "util/duration.h"

class ControllerJSProxy;
Expand Down Expand Up @@ -116,8 +116,8 @@ class Controller : public QObject, ConstControllerPresetVisitor {
// To be called when receiving events
void triggerActivity();

inline ControllerScriptHandler* getScriptHandler() const {
return m_pScriptHandler;
inline ControllerScriptEngineLegacy* getScriptEngine() const {
return m_pScriptEngineLegacy;
}
inline void setDeviceName(QString deviceName) {
m_sDeviceName = deviceName;
Expand Down Expand Up @@ -154,7 +154,7 @@ class Controller : public QObject, ConstControllerPresetVisitor {
// Returns a pointer to the currently loaded controller preset. For internal
// use only.
virtual ControllerPreset* preset() = 0;
ControllerScriptHandler* m_pScriptHandler;
ControllerScriptEngineLegacy* m_pScriptEngineLegacy;

// Verbose and unique device name suitable for display.
QString m_sDeviceName;
Expand Down
20 changes: 3 additions & 17 deletions src/controllers/midi/midicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,6 @@ void MidiController::commitTemporaryInputMappings() {

void MidiController::receive(unsigned char status, unsigned char control,
unsigned char value, mixxx::Duration timestamp) {
QByteArray byteArray;
byteArray.append(status);
byteArray.append(control);
byteArray.append(value);

ControllerScriptHandler* pEngine = getScriptHandler();
// pEngine is nullptr in tests.
if (pEngine) {
pEngine->handleInput(byteArray, timestamp);
}
// legacy stuff below

// The rest of this function is for legacy mappings
unsigned char channel = MidiUtils::channelFromStatus(status);
unsigned char opCode = MidiUtils::opCodeFromStatus(status);
Expand Down Expand Up @@ -261,7 +249,7 @@ void MidiController::processInputMapping(const MidiInputMapping& mapping,
unsigned char opCode = MidiUtils::opCodeFromStatus(status);

if (mapping.options.script) {
ControllerScriptHandler* pEngine = getScriptHandler();
ControllerScriptEngineLegacy* pEngine = getScriptEngine();
if (pEngine == NULL) {
return;
}
Expand Down Expand Up @@ -482,8 +470,6 @@ double MidiController::computeValue(

void MidiController::receive(QByteArray data, mixxx::Duration timestamp) {
controllerDebug(MidiUtils::formatSysexMessage(getName(), data, timestamp));
getScriptHandler()->handleInput(data, timestamp);
// legacy stuff below

MidiKey mappingKey(data.at(0), 0xFF);

Expand Down Expand Up @@ -514,12 +500,12 @@ void MidiController::processInputMapping(const MidiInputMapping& mapping,
mixxx::Duration timestamp) {
// Custom script handler
if (mapping.options.script) {
ControllerScriptHandler* pEngine = getScriptHandler();
ControllerScriptEngineLegacy* pEngine = getScriptEngine();
if (pEngine == NULL) {
return;
}
QJSValue function = pEngine->wrapFunctionCode(mapping.control.item, 2);
if (!pEngine->executeFunction(function, data)) {
if (!pEngine->executeIncomingDataFunction(function, data)) {
qDebug() << "MidiController: Invalid script function"
<< mapping.control.item;
}
Expand Down
Loading

0 comments on commit b800259

Please sign in to comment.