drumstick 2.0.0
backendmanager.cpp
Go to the documentation of this file.
1/*
2 Drumstick RT (realtime MIDI In/Out)
3 Copyright (C) 2009-2020 Pedro Lopez-Cabanillas <plcl@users.sf.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <QCoreApplication>
20//#include <QDebug>
21#include <QDir>
22#include <QLibraryInfo>
23#include <QPluginLoader>
24#include <QtGlobal>
26
31
32namespace drumstick { namespace rt {
33
50
51 class BackendManager::BackendManagerPrivate {
52 public:
53 QList<MIDIInput*> m_inputsList;
54 QList<MIDIOutput*> m_outputsList;
55 ~BackendManagerPrivate()
56 {
57 clearLists();
58 }
59 void clearLists()
60 {
61 m_inputsList.clear();
62 m_outputsList.clear();
63 }
64 void appendDir(const QString& candidate, QStringList& result)
65 {
66 QDir checked(candidate.trimmed());
67 //qDebug() << Q_FUNC_INFO << candidate << "exists:" << checked.exists();
68 if (checked.exists() && !result.contains(checked.absolutePath())) {
69 result << checked.absolutePath();
70 }
71 }
72 };
73
77 BackendManager::BackendManager(): d(new BackendManagerPrivate)
78 {
79 QVariantMap defaultSettings {
80 { QSTR_DRUMSTICKRT_PUBLICNAMEIN, QStringLiteral("MIDI In")},
81 { QSTR_DRUMSTICKRT_PUBLICNAMEOUT, QStringLiteral("MIDI Out")}
82 };
83 refresh(defaultSettings);
84 }
85
91
97 {
98 QStringList result;
99 QString appPath = QCoreApplication::applicationDirPath() + QDir::separator();
100 #if defined(Q_OS_WIN)
101 d->appendDir( appPath + QSTR_DRUMSTICK, result );
102 d->appendDir( appPath + "../lib/" + QSTR_DRUMSTICK, result );
103 #else
104 #if defined(Q_OS_MAC)
105 d->appendDir( appPath + QStringLiteral("../PlugIns/") + QSTR_DRUMSTICK, result );
106 #endif // Linux, Unix...
107 QStringList libs;
108 libs << "../lib/";
109 #if defined(LIBSUFFIX)
110 libs << QString("../%1/").arg(QT_STRINGIFY(LIBSUFFIX));
111 #endif
112 foreach(const QString& lib, libs) {
113 d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
114 }
115 #endif
116 d->appendDir( appPath + ".." + QDir::separator() + QSTR_DRUMSTICK, result );
117 QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
118 //qDebug() << Q_FUNC_INFO << "envdir:" << envdir;
119 if(!envdir.isEmpty()) {
120 d->appendDir(QString(envdir), result );
121 }
122 d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
123 d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
124 foreach(const QString& path, QCoreApplication::libraryPaths()) {
125 d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
126 }
127 return result;
128 }
129
135 {
136 QVariantMap tmpMap;
137 settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
138 const QStringList allKeys = settings->allKeys();
139 //qDebug() << Q_FUNC_INFO << allKeys;
140 for(const auto &k : allKeys) {
141 tmpMap.insert(k, settings->value(k));
142 }
143 settings->endGroup();
144 refresh(tmpMap);
145 }
146
152 void BackendManager::refresh(const QVariantMap &map)
153 {
154 QString name_in;
155 QString name_out;
156 QStringList names;
157 QStringList paths;
158
159 d->appendDir(map.value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
160 name_in = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
161 name_out = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
162 names << map.value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
163 names << (name_in.isEmpty() ? QStringLiteral("MIDI In") : name_in);
164 names << (name_out.isEmpty() ? QStringLiteral("MIDI Out") : name_out);
165 paths << defaultPaths();
166 //qDebug() << Q_FUNC_INFO << "names:" << names;
167 //qDebug() << Q_FUNC_INFO << "paths:" << paths;
168
169 d->clearLists();
170
171 // Dynamic backends
172 foreach(const QString& dir, paths) {
173 QDir pluginsDir(dir);
174 foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
175 if (QLibrary::isLibrary(fileName)) {
176 QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
177 QObject *obj = loader.instance();
178 if (obj != nullptr) {
179 MIDIInput *input = qobject_cast<MIDIInput*>(obj);
180 if (input != nullptr && !d->m_inputsList.contains(input)) {
181 if (!name_in.isEmpty()) {
182 input->setPublicName(name_in);
183 }
184 input->setExcludedConnections(names);
185 d->m_inputsList << input;
186 } else {
187 MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
188 if (output != nullptr && !d->m_outputsList.contains(output)) {
189 if (!name_out.isEmpty()) {
190 output->setPublicName(name_out);
191 }
192 output->setExcludedConnections(names);
193 d->m_outputsList << output;
194 }
195 }
196 }
197 }
198 }
199 }
200
201 // Static backends
202 foreach(QObject* obj, QPluginLoader::staticInstances()) {
203 if (obj != nullptr) {
204 MIDIInput *input = qobject_cast<MIDIInput*>(obj);
205 if (input != nullptr && !d->m_inputsList.contains(input)) {
206 if (!name_in.isEmpty()) {
207 input->setPublicName(name_in);
208 }
209 input->setExcludedConnections(names);
210 d->m_inputsList << input;
211 } else {
212 MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
213 if (output != nullptr && !d->m_outputsList.contains(output)) {
214 if (!name_out.isEmpty()) {
215 output->setPublicName(name_out);
216 }
217 output->setExcludedConnections(names);
218 d->m_outputsList << output;
219 }
220 }
221 }
222 }
223 }
224
226 {
227 return d->m_inputsList;
228 }
229
231 {
232 return d->m_outputsList;
233 }
234
236 {
237 foreach (MIDIInput* i, d->m_inputsList) {
238 if (i->backendName() == name) {
239 return i;
240 }
241 }
242 return nullptr;
243 }
244
246 {
247 foreach (MIDIOutput* i, d->m_outputsList) {
248 if (i->backendName() == name) {
249 return i;
250 }
251 }
252 return nullptr;
253 }
254
255 const QString BackendManager::QSTR_DRUMSTICK = QStringLiteral("drumstick2");
256 const QString BackendManager::QSTR_DRUMSTICK_VERSION = QStringLiteral(QT_STRINGIFY(VERSION));
257 const QString BackendManager::QSTR_DRUMSTICKRT = QStringLiteral("DRUMSTICKRT");
258 const QString BackendManager::QSTR_DRUMSTICKRT_GROUP = QStringLiteral("DrumstickRT");
259 const QString BackendManager::QSTR_DRUMSTICKRT_PUBLICNAMEIN = QStringLiteral("PublicNameIN");
260 const QString BackendManager::QSTR_DRUMSTICKRT_PUBLICNAMEOUT = QStringLiteral("PublicNameOUT");
261 const QString BackendManager::QSTR_DRUMSTICKRT_EXCLUDED = QStringLiteral("ExcludedNames");
262 const QString BackendManager::QSTR_DRUMSTICKRT_PATH = QStringLiteral("BackendsPath");
263
264} // namespace rt
265} // namespace drumstick
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
QStringList defaultPaths()
defaultPaths
MIDIInput * inputBackendByName(const QString name)
inputBackendByName
MIDI IN interface.
Definition rtmidiinput.h:46
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
virtual QString backendName()=0
backendName
virtual void setPublicName(QString name)=0
setPublicName
MIDI OUT interface.
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
virtual QString backendName()=0
backendName
virtual void setPublicName(QString name)=0
setPublicName
Drumstick Real-Time library.
Drumstick common.