1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
Drumstick RT (realtime MIDI In/Out)
Copyright (C) 2009-2024 Pedro Lopez-Cabanillas <plcl@users.sf.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCoreApplication>
#include <QDir>
#include <QLibraryInfo>
#include <QPluginLoader>
#include <QtGlobal>
#include <drumstick/backendmanager.h>
/**
* @file backendmanager.cpp
* Implementation of a class managing realtime MIDI input/output backends
*/
namespace drumstick { namespace rt {
/**
* @addtogroup RT
* @{
*
* BackendManager provides a mechanism to find and enumerate backends (plugins)
* to manage realtime MIDI input/output
*
* This class and plugins are multiplatform. The backends implement one of these
* interfaces:
*
* MIDIInput: for plugins that can receive MIDI events
*
* MIDIOutput: for plugins that can consume MIDI events
*
* @}
*/
class BackendManager::BackendManagerPrivate {
public:
QList<QPluginLoader *> m_loaders;
QList<MIDIInput *> m_inputsList;
QList<MIDIOutput *> m_outputsList;
QString m_inputBackend{QLatin1String("Network")};
#if defined(Q_OS_LINUX)
QStringList m_outputBackends{QLatin1String("SonivoxEAS"),QLatin1String("FluidSynth"),QLatin1String("ALSA")};
#elif defined(Q_OS_DARWIN)
QStringList m_outputBackends{QLatin1String("DLS Synth"),QLatin1String("FluidSynth"),QLatin1String("CoreMIDI")};
#elif defined(Q_OS_WINDOWS)
QStringList m_outputBackends{QLatin1String("Windows MM"),QLatin1String("FluidSynth")};
#elif defined(Q_OS_UNIX)
QStringList m_outputBackends{QLatin1String("FluidSynth"),QLatin1String("OSS")};
#else
QStringList m_outputBackends{m_inputBackend};
#endif
~BackendManagerPrivate()
{
clearLists();
}
void clearLists()
{
// 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;
}
m_inputsList.clear();
m_outputsList.clear();
m_loaders.clear();
}
void appendDir(const QString &candidate, QStringList &result)
{
QDir checked(candidate.trimmed());
//qDebug() << Q_FUNC_INFO << candidate << "exists:" << checked.exists();
if (checked.exists() && !result.contains(checked.absolutePath())) {
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}
{
//qDebug() << Q_FUNC_INFO;
QVariantMap defaultSettings {
{ QSTR_DRUMSTICKRT_PUBLICNAMEIN, QStringLiteral("MIDI In")},
{ QSTR_DRUMSTICKRT_PUBLICNAMEOUT, QStringLiteral("MIDI Out")}
};
refresh(defaultSettings);
}
/**
* @brief Destructor
*/
BackendManager::~BackendManager()
{
//qDebug() << Q_FUNC_INFO;
}
/**
* @brief returns the paths where backends are searched
* @return list of paths
*/
QStringList BackendManager::defaultPaths()
{
QStringList result;
QString appPath = QCoreApplication::applicationDirPath() + QDir::separator();
#if defined(Q_OS_WIN)
d->appendDir( appPath + QSTR_DRUMSTICK, result );
d->appendDir( appPath + "../lib/" + QSTR_DRUMSTICK, result );
#else
#if defined(Q_OS_MAC)
d->appendDir( appPath + QStringLiteral("../PlugIns/") + QSTR_DRUMSTICK, result );
#endif // Linux, Unix...
QStringList libs;
libs << "../lib/";
#if defined(LIBSUFFIX)
QString libextra(QT_STRINGIFY(LIBSUFFIX));
if (QDir::isAbsolutePath(libextra)) {
d->appendDir( libextra + QDir::separator() + QSTR_DRUMSTICK, result );
} else {
libs << QString("../%1/").arg(libextra);
}
#endif
foreach(const QString& lib, libs) {
d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
}
#endif
d->appendDir( appPath + ".." + QDir::separator() + QSTR_DRUMSTICK, result );
QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
//qDebug() << Q_FUNC_INFO << "envdir:" << envdir;
if(!envdir.isEmpty()) {
d->appendDir(QString(envdir), result );
}
d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
#else
d->appendDir( QLibraryInfo::path(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
#endif
foreach(const QString& path, QCoreApplication::libraryPaths()) {
d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
}
return result;
}
/**
* @brief BackendManager::refresh finds the installed backends applying the provided settings.
* @param settings Program settings
*/
void BackendManager::refresh(QSettings *settings)
{
//qDebug() << Q_FUNC_INFO;
QVariantMap tmpMap;
settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
const QStringList allKeys = settings->allKeys();
//qDebug() << Q_FUNC_INFO << allKeys;
for(const auto &k : allKeys) {
tmpMap.insert(k, settings->value(k));
}
settings->endGroup();
refresh(tmpMap);
}
/**
* @brief BackendManager::refresh finds the installed backends searching the list of paths
* provided by the function defaultPaths() applying the provided settings map as well.
* @param map Program settings relevant section
*/
void BackendManager::refresh(const QVariantMap &map)
{
QString name_in;
QString name_out;
QStringList names;
QStringList paths;
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();
names << map.value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
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 << "paths:" << paths;
// Dynamic backends
foreach(const QString& dir, paths) {
QDir pluginsDir(dir);
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
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<MIDIInput *>(obj);
if (input != nullptr && !d->m_inputsList.contains(input)) {
//qDebug() << "input plugin instantiated:" << name_in;
if (!name_in.isEmpty()) {
input->setPublicName(name_in);
}
input->setExcludedConnections(names);
d->m_inputsList << input;
} else {
MIDIOutput *output = qobject_cast<MIDIOutput *>(obj);
if (output != nullptr && !d->m_outputsList.contains(output)) {
//qDebug() << "output plugin instantiated:" << name_out;
if (!name_out.isEmpty()) {
output->setPublicName(name_out);
}
output->setExcludedConnections(names);
d->m_outputsList << output;
}
}
}
}
}
}
// Static backends
foreach(QObject* obj, QPluginLoader::staticInstances()) {
if (obj != nullptr) {
MIDIInput *input = qobject_cast<MIDIInput*>(obj);
if (input != nullptr && !d->m_inputsList.contains(input)) {
if (!name_in.isEmpty()) {
input->setPublicName(name_in);
}
input->setExcludedConnections(names);
d->m_inputsList << input;
} else {
MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
if (output != nullptr && !d->m_outputsList.contains(output)) {
if (!name_out.isEmpty()) {
output->setPublicName(name_out);
}
output->setExcludedConnections(names);
d->m_outputsList << output;
}
}
}
}
}
QList<MIDIInput*> BackendManager::availableInputs()
{
return d->m_inputsList;
}
QList<MIDIOutput*> BackendManager::availableOutputs()
{
return d->m_outputsList;
}
MIDIInput* BackendManager::inputBackendByName(const QString name)
{
foreach (MIDIInput* i, d->m_inputsList) {
if (i->backendName() == name) {
return i;
}
}
return nullptr;
}
MIDIOutput* BackendManager::outputBackendByName(const QString name)
{
foreach (MIDIOutput* i, d->m_outputsList) {
if (i->backendName() == name) {
return i;
}
}
return nullptr;
}
MIDIInput* BackendManager::findInput(QString name)
{
QStringList names{name};
names << d->m_inputBackend;
names.removeDuplicates();
if (!names.isEmpty()) {
foreach(const QString& n, names) {
foreach(MIDIInput* input, d->m_inputsList) {
if (input->backendName() == n) {
return input;
}
}
}
}
return nullptr;
}
MIDIOutput* BackendManager::findOutput(QString name)
{
QStringList names{name};
names << d->m_outputBackends;
names.removeDuplicates();
if (!names.isEmpty()) {
foreach(const QString& n, names) {
foreach(MIDIOutput* output, d->m_outputsList) {
if (output->backendName() == n) {
return output;
}
}
}
}
return nullptr;
}
const QString BackendManager::QSTR_DRUMSTICK = QStringLiteral("drumstick2");
const QString BackendManager::QSTR_DRUMSTICK_VERSION = QStringLiteral(QT_STRINGIFY(VERSION));
const QString BackendManager::QSTR_DRUMSTICKRT = QStringLiteral("DRUMSTICKRT");
const QString BackendManager::QSTR_DRUMSTICKRT_GROUP = QStringLiteral("DrumstickRT");
const QString BackendManager::QSTR_DRUMSTICKRT_PUBLICNAMEIN = QStringLiteral("PublicNameIN");
const QString BackendManager::QSTR_DRUMSTICKRT_PUBLICNAMEOUT = QStringLiteral("PublicNameOUT");
const QString BackendManager::QSTR_DRUMSTICKRT_EXCLUDED = QStringLiteral("ExcludedNames");
const QString BackendManager::QSTR_DRUMSTICKRT_PATH = QStringLiteral("BackendsPath");
/**
* @brief drumstickLibraryVersion provides the Drumstick version as an edited QString
* @return Drumstick library version
*/
QString drumstickLibraryVersion()
{
return BackendManager::QSTR_DRUMSTICK_VERSION;
}
} // namespace rt
} // namespace drumstick
|