diff --git a/library/rt/backendmanager.cpp b/library/rt/backendmanager.cpp index d6afdde..370e5ff 100644 --- a/library/rt/backendmanager.cpp +++ b/library/rt/backendmanager.cpp @@ -49,42 +49,44 @@ namespace drumstick { namespace rt { class BackendManager::BackendManagerPrivate { public: - QList m_inputsList; - QList m_outputsList; - QList m_loaders; + QList m_loaders; + QList m_inputsList; + QList m_outputsList; QString m_inputBackend{QLatin1String("Network")}; - #if defined(Q_OS_LINUX) +#if defined(Q_OS_LINUX) QStringList m_outputBackends{QLatin1String("SonivoxEAS"),QLatin1String("FluidSynth"),QLatin1String("ALSA")}; - #elif defined(Q_OS_DARWIN) +#elif defined(Q_OS_DARWIN) QStringList m_outputBackends{QLatin1String("DLS Synth"),QLatin1String("FluidSynth"),QLatin1String("CoreMIDI")}; - #elif defined(Q_OS_WINDOWS) +#elif defined(Q_OS_WINDOWS) QStringList m_outputBackends{QLatin1String("Windows MM"),QLatin1String("FluidSynth")}; - #elif defined(Q_OS_UNIX) +#elif defined(Q_OS_UNIX) QStringList m_outputBackends{QLatin1String("FluidSynth"),QLatin1String("OSS")}; - #else +#else QStringList m_outputBackends{m_inputBackend}; - #endif +#endif ~BackendManagerPrivate() { clearLists(); } + void clearLists() { - qDebug() << Q_FUNC_INFO << "loaders:" << m_loaders.count(); - while(!m_loaders.empty()) { + qDebug() << Q_FUNC_INFO << "loaders:" << m_loaders.count() + << "inputs:" << m_inputsList.count() << "outputs:" << m_outputsList.count(); + while (!m_loaders.empty()) { QPluginLoader* pluginLoader = m_loaders.takeFirst(); qDebug() << "unloading:" << pluginLoader->fileName(); pluginLoader->unload(); delete pluginLoader; } - qDebug() << "inputs:" << m_inputsList.count() << "outputs:" << m_outputsList.count(); m_inputsList.clear(); m_outputsList.clear(); m_loaders.clear(); } - void appendDir(const QString& candidate, QStringList& result) + + void appendDir(const QString &candidate, QStringList &result) { QDir checked(candidate.trimmed()); //qDebug() << Q_FUNC_INFO << candidate << "exists:" << checked.exists(); @@ -92,13 +94,25 @@ namespace drumstick { namespace rt { result << checked.absolutePath(); } } + + bool isLoaderNeeded(const QString &fileName) + { + auto it = std::find_if(m_loaders.constBegin(), + m_loaders.constEnd(), + [=](QPluginLoader *loader) { + return loader->fileName() == fileName; + }); + return it == m_loaders.constEnd(); + } }; /** * @brief Constructor */ - BackendManager::BackendManager(): d(new BackendManagerPrivate) + BackendManager::BackendManager() + : d{new BackendManagerPrivate} { + qDebug() << Q_FUNC_INFO; QVariantMap defaultSettings { { QSTR_DRUMSTICKRT_PUBLICNAMEIN, QStringLiteral("MIDI In")}, { QSTR_DRUMSTICKRT_PUBLICNAMEOUT, QStringLiteral("MIDI Out")} @@ -110,7 +124,9 @@ namespace drumstick { namespace rt { * @brief Destructor */ BackendManager::~BackendManager() - { } + { + qDebug() << Q_FUNC_INFO; + } /** * @brief returns the paths where backends are searched @@ -165,6 +181,7 @@ namespace drumstick { namespace rt { */ void BackendManager::refresh(QSettings *settings) { + qDebug() << Q_FUNC_INFO; QVariantMap tmpMap; settings->beginGroup(QSTR_DRUMSTICKRT_GROUP); const QStringList allKeys = settings->allKeys(); @@ -188,8 +205,6 @@ namespace drumstick { namespace rt { QStringList names; QStringList paths; - qDebug() << Q_FUNC_INFO; - d->appendDir(map.value(QSTR_DRUMSTICKRT_PATH).toString(), paths); name_in = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString(); name_out = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString(); @@ -197,22 +212,21 @@ namespace drumstick { namespace rt { names << (name_in.isEmpty() ? QStringLiteral("MIDI In") : name_in); names << (name_out.isEmpty() ? QStringLiteral("MIDI Out") : name_out); paths << defaultPaths(); - //qDebug() << Q_FUNC_INFO << "names:" << names; - //qDebug() << Q_FUNC_INFO << "paths:" << paths; - d->clearLists(); + qDebug() << Q_FUNC_INFO << "names:" << names << "paths:" << paths; // Dynamic backends foreach(const QString& dir, paths) { QDir pluginsDir(dir); foreach (QString fileName, pluginsDir.entryList(QDir::Files)) { - if (QLibrary::isLibrary(fileName)) { - QPluginLoader *loader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName)); - qDebug() << "plugin loader created for" << fileName; + auto absolutePath = pluginsDir.absoluteFilePath(fileName); + if (QLibrary::isLibrary(absolutePath) && d->isLoaderNeeded(absolutePath)) { + QPluginLoader *loader = new QPluginLoader(absolutePath); + qDebug() << "plugin loader created:" << loader->fileName(); d->m_loaders << loader; QObject *obj = loader->instance(); if (obj != nullptr) { - MIDIInput *input = qobject_cast(obj); + MIDIInput *input = qobject_cast(obj); if (input != nullptr && !d->m_inputsList.contains(input)) { qDebug() << "input plugin instantiated:" << name_in; if (!name_in.isEmpty()) { @@ -221,7 +235,7 @@ namespace drumstick { namespace rt { input->setExcludedConnections(names); d->m_inputsList << input; } else { - MIDIOutput *output = qobject_cast(obj); + MIDIOutput *output = qobject_cast(obj); if (output != nullptr && !d->m_outputsList.contains(output)) { qDebug() << "output plugin instantiated:" << name_out; if (!name_out.isEmpty()) { diff --git a/utils/vpiano/vpiano.cpp b/utils/vpiano/vpiano.cpp index 77386df..b7dd6f4 100644 --- a/utils/vpiano/vpiano.cpp +++ b/utils/vpiano/vpiano.cpp @@ -26,22 +26,24 @@ #if defined(Q_OS_MACOS) #include #endif + +#include +#include + #include "connections.h" #include "preferences.h" #include "vpiano.h" #include "vpianoabout.h" #include "vpianosettings.h" -#include -#include -#include using namespace drumstick::rt; using namespace drumstick::widgets; -VPiano::VPiano( QWidget * parent, Qt::WindowFlags flags) - : QMainWindow(parent, flags), - m_midiIn(nullptr), - m_midiOut(nullptr) +VPiano::VPiano(QWidget *parent, Qt::WindowFlags flags) + : QMainWindow(parent, flags) + , m_manager{new BackendManager} + , m_midiIn{nullptr} + , m_midiOut{nullptr} { ui.setupUi(this); @@ -108,23 +110,25 @@ VPiano::~VPiano() qDebug() << Q_FUNC_INFO; m_midiIn->close(); m_midiOut->close(); + delete m_manager; } void VPiano::initialize() { + qDebug() << Q_FUNC_INFO; + readSettings(); - BackendManager man; - man.refresh(VPianoSettings::instance()->settingsMap()); - m_inputs = man.availableInputs(); - m_outputs = man.availableOutputs(); + m_manager->refresh(VPianoSettings::instance()->settingsMap()); + m_inputs = m_manager->availableInputs(); + m_outputs = m_manager->availableOutputs(); - m_midiIn = man.findInput(VPianoSettings::instance()->lastInputBackend()); + m_midiIn = m_manager->findInput(VPianoSettings::instance()->lastInputBackend()); if (m_midiIn == nullptr) { qFatal("Unable to find a suitable input backend."); } - m_midiOut = man.findOutput(VPianoSettings::instance()->lastOutputBackend()); + m_midiOut = m_manager->findOutput(VPianoSettings::instance()->lastOutputBackend()); if (m_midiOut == nullptr) { qFatal("Unable to find a suitable output backend. You may need to set the DRUMSTICKRT environment variable."); } diff --git a/utils/vpiano/vpiano.h b/utils/vpiano/vpiano.h index 21c309a..146f36a 100644 --- a/utils/vpiano/vpiano.h +++ b/utils/vpiano/vpiano.h @@ -19,10 +19,13 @@ #ifndef VPIANO_H #define VPIANO_H -#include #include +#include + +#include #include #include + #include "ui_vpiano.h" class VPiano : public QMainWindow @@ -67,6 +70,7 @@ public Q_SLOTS: void initialize(); void useCustomNoteNames(); + drumstick::rt::BackendManager *m_manager; QList m_inputs; QList m_outputs; drumstick::rt::MIDIInput * m_midiIn;