19#include <QCoreApplication>
21#include <QLibraryInfo>
22#include <QPluginLoader>
50 class BackendManager::BackendManagerPrivate {
52 QList<QPluginLoader *> m_loaders;
53 QList<MIDIInput *> m_inputsList;
54 QList<MIDIOutput *> m_outputsList;
56 QString m_inputBackend{QLatin1String(
"Network")};
57#if defined(Q_OS_LINUX)
58 QStringList m_outputBackends{QLatin1String(
"SonivoxEAS"),QLatin1String(
"FluidSynth"),QLatin1String(
"ALSA")};
59#elif defined(Q_OS_DARWIN)
60 QStringList m_outputBackends{QLatin1String(
"DLS Synth"),QLatin1String(
"FluidSynth"),QLatin1String(
"CoreMIDI")};
61#elif defined(Q_OS_WINDOWS)
62 QStringList m_outputBackends{QLatin1String(
"Windows MM"),QLatin1String(
"FluidSynth")};
63#elif defined(Q_OS_UNIX)
64 QStringList m_outputBackends{QLatin1String(
"FluidSynth"),QLatin1String(
"OSS")};
66 QStringList m_outputBackends{m_inputBackend};
69 ~BackendManagerPrivate()
78 while (!m_loaders.empty()) {
79 QPluginLoader* pluginLoader = m_loaders.takeFirst();
81 pluginLoader->unload();
85 m_outputsList.clear();
89 void appendDir(
const QString &candidate, QStringList &result)
91 QDir checked(candidate.trimmed());
93 if (checked.exists() && !result.contains(checked.absolutePath())) {
94 result << checked.absolutePath();
98 bool isLoaderNeeded(
const QString &fileName)
100 auto it = std::find_if(m_loaders.constBegin(),
101 m_loaders.constEnd(),
102 [=](QPluginLoader *loader) {
103 return loader->fileName() == fileName;
105 return it == m_loaders.constEnd();
113 : d{new BackendManagerPrivate}
116 QVariantMap defaultSettings {
117 { QSTR_DRUMSTICKRT_PUBLICNAMEIN, QStringLiteral(
"MIDI In")},
118 { QSTR_DRUMSTICKRT_PUBLICNAMEOUT, QStringLiteral(
"MIDI Out")}
138 QString appPath = QCoreApplication::applicationDirPath() + QDir::separator();
139 #if defined(Q_OS_WIN)
140 d->appendDir( appPath + QSTR_DRUMSTICK, result );
141 d->appendDir( appPath +
"../lib/" + QSTR_DRUMSTICK, result );
143 #if defined(Q_OS_MAC)
144 d->appendDir( appPath + QStringLiteral(
"../PlugIns/") + QSTR_DRUMSTICK, result );
148 #if defined(LIBSUFFIX)
149 QString libextra(QT_STRINGIFY(LIBSUFFIX));
150 if (QDir::isAbsolutePath(libextra)) {
151 d->appendDir( libextra + QDir::separator() + QSTR_DRUMSTICK, result );
153 libs << QString(
"../%1/").arg(libextra);
156 foreach(
const QString& lib, libs) {
157 d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
160 d->appendDir( appPath +
".." + QDir::separator() + QSTR_DRUMSTICK, result );
161 QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
163 if(!envdir.isEmpty()) {
164 d->appendDir(QString(envdir), result );
166 d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
167#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
168 d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
170 d->appendDir( QLibraryInfo::path(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
172 foreach(
const QString& path, QCoreApplication::libraryPaths()) {
173 d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
186 settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
187 const QStringList allKeys = settings->allKeys();
189 for(
const auto &k : allKeys) {
190 tmpMap.insert(k, settings->value(k));
192 settings->endGroup();
208 d->appendDir(map.value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
209 name_in = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
210 name_out = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
211 names << map.value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
212 names << (name_in.isEmpty() ? QStringLiteral(
"MIDI In") : name_in);
213 names << (name_out.isEmpty() ? QStringLiteral(
"MIDI Out") : name_out);
219 foreach(
const QString& dir, paths) {
220 QDir pluginsDir(dir);
221 foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
222 auto absolutePath = pluginsDir.absoluteFilePath(fileName);
223 if (QLibrary::isLibrary(absolutePath) && d->isLoaderNeeded(absolutePath)) {
224 QPluginLoader *loader =
new QPluginLoader(absolutePath);
226 d->m_loaders << loader;
227 QObject *obj = loader->instance();
228 if (obj !=
nullptr) {
229 MIDIInput *input = qobject_cast<MIDIInput *>(obj);
230 if (input !=
nullptr && !d->m_inputsList.contains(input)) {
232 if (!name_in.isEmpty()) {
236 d->m_inputsList << input;
238 MIDIOutput *output = qobject_cast<MIDIOutput *>(obj);
239 if (output !=
nullptr && !d->m_outputsList.contains(output)) {
241 if (!name_out.isEmpty()) {
245 d->m_outputsList << output;
254 foreach(
QObject* obj, QPluginLoader::staticInstances()) {
255 if (obj !=
nullptr) {
256 MIDIInput *input = qobject_cast<MIDIInput*>(obj);
257 if (input !=
nullptr && !d->m_inputsList.contains(input)) {
258 if (!name_in.isEmpty()) {
262 d->m_inputsList << input;
264 MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
265 if (output !=
nullptr && !d->m_outputsList.contains(output)) {
266 if (!name_out.isEmpty()) {
270 d->m_outputsList << output;
279 return d->m_inputsList;
284 return d->m_outputsList;
289 foreach (
MIDIInput* i, d->m_inputsList) {
309 QStringList names{name};
310 names << d->m_inputBackend;
311 names.removeDuplicates();
312 if (!names.isEmpty()) {
313 foreach(
const QString& n, names) {
314 foreach(
MIDIInput* input, d->m_inputsList) {
326 QStringList names{name};
327 names << d->m_outputBackends;
328 names.removeDuplicates();
329 if (!names.isEmpty()) {
330 foreach(
const QString& n, names) {
331 foreach(
MIDIOutput* output, d->m_outputsList) {
341 const QString BackendManager::QSTR_DRUMSTICK = QStringLiteral(
"drumstick2");
342 const QString BackendManager::QSTR_DRUMSTICK_VERSION = QStringLiteral(QT_STRINGIFY(VERSION));
343 const QString BackendManager::QSTR_DRUMSTICKRT = QStringLiteral(
"DRUMSTICKRT");
344 const QString BackendManager::QSTR_DRUMSTICKRT_GROUP = QStringLiteral(
"DrumstickRT");
345 const QString BackendManager::QSTR_DRUMSTICKRT_PUBLICNAMEIN = QStringLiteral(
"PublicNameIN");
346 const QString BackendManager::QSTR_DRUMSTICKRT_PUBLICNAMEOUT = QStringLiteral(
"PublicNameOUT");
347 const QString BackendManager::QSTR_DRUMSTICKRT_EXCLUDED = QStringLiteral(
"ExcludedNames");
348 const QString BackendManager::QSTR_DRUMSTICKRT_PATH = QStringLiteral(
"BackendsPath");
356 return BackendManager::QSTR_DRUMSTICK_VERSION;
BackendManager class declaration.
The QObject class is the base class of all Qt objects.
The QSettings class provides persistent platform-independent application settings.
QList< MIDIInput * > availableInputs()
availableInputs
virtual ~BackendManager()
~BackendManager destructor
BackendManager()
BackendManager constructor.
MIDIOutput * outputBackendByName(const QString name)
outputBackendByName
void refresh(QSettings *settings=nullptr)
refresh the list of backends
QList< MIDIOutput * > availableOutputs()
availableOutputs
MIDIOutput * findOutput(QString name)
findOutput returns the backend corresponding to the provided name, or a suitable output instead.
MIDIInput * findInput(QString name)
findInput returns the backend corresponding to the provided name, or a suitable input instead.
QStringList defaultPaths()
defaultPaths
MIDIInput * inputBackendByName(const QString name)
inputBackendByName
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
virtual QString backendName()=0
backendName
virtual void setPublicName(QString name)=0
setPublicName
QString DRUMSTICK_RT_EXPORT drumstickLibraryVersion()
drumstickLibraryVersion provides the Drumstick version as an edited QString