drumstick  1.1.3
backendmanager.cpp
Go to the documentation of this file.
1 /*
2  Drumstick RT (realtime MIDI In/Out)
3  Copyright (C) 2009-2019 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 2 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 <QtGlobal>
20 #include <QDir>
21 #include <QPluginLoader>
22 #include <QCoreApplication>
23 #include <QLibraryInfo>
25 
26 #if defined(LINUX_BACKEND)
27 Q_IMPORT_PLUGIN(ALSAMIDIInput)
28 Q_IMPORT_PLUGIN(ALSAMIDIOutput)
29 Q_IMPORT_PLUGIN(SynthController)
30 #endif
31 
32 #if defined(MAC_BACKEND)
33 Q_IMPORT_PLUGIN(MacMIDIInput)
34 Q_IMPORT_PLUGIN(MacMIDIOutput)
35 Q_IMPORT_PLUGIN(MacSynthOutput)
36 #endif
37 
38 #if defined(WIN_BACKEND)
39 Q_IMPORT_PLUGIN(WinMIDIInput)
40 Q_IMPORT_PLUGIN(WinMIDIOutput)
41 #endif
42 
43 #if defined(NET_BACKEND)
44 Q_IMPORT_PLUGIN(NetMIDIInput)
45 Q_IMPORT_PLUGIN(NetMIDIOutput)
46 #endif
47 
48 #if defined(DUMMY_BACKEND)
49 Q_IMPORT_PLUGIN(DummyInput)
50 Q_IMPORT_PLUGIN(DummyOutput)
51 #endif
52 
53 #if defined(SYNTH_BACKEND)
54 Q_IMPORT_PLUGIN(SynthOutput)
55 #endif
56 
57 #if defined(OSS_BACKEND)
58 Q_IMPORT_PLUGIN(OSSInput)
59 Q_IMPORT_PLUGIN(OSSOutput)
60 #endif
61 
62 #define MKSTR_A(x) #x
63 #define MKSTR(x) MKSTR_A(x)
64 
70 namespace drumstick {
71 namespace rt {
72 
90  class BackendManager::BackendManagerPrivate {
91  public:
92  QList<MIDIInput*> m_inputsList;
93  QList<MIDIOutput*> m_outputsList;
94  ~BackendManagerPrivate()
95  {
96  clearLists();
97  }
98  void clearLists()
99  {
100  m_inputsList.clear();
101  m_outputsList.clear();
102  }
103  void appendDir(const QString& candidate, QStringList& result)
104  {
105  QDir checked(candidate);
106  if (checked.exists() && !result.contains(checked.absolutePath())) {
107  result << checked.absolutePath();
108  }
109  }
110  };
111 
115  BackendManager::BackendManager(): d(new BackendManagerPrivate)
116  {
117  refresh();
118  }
119 
124  {
125  delete d;
126  }
127 
133  {
134  QStringList result;
135  QString appPath = QCoreApplication::applicationDirPath() + QDir::separator();
136  #if defined(Q_OS_WIN)
137  d->appendDir( appPath + QSTR_DRUMSTICK, result );
138  d->appendDir( appPath + "../lib/" + QSTR_DRUMSTICK, result );
139  #else
140  #if defined(Q_OS_MAC)
141  d->appendDir( appPath + QStringLiteral("../PlugIns/") + QSTR_DRUMSTICK, result );
142  #endif // Linux, Unix...
143  QStringList libs;
144  libs << "../lib/";
145  #if defined(LIBSUFFIX)
146  libs << QString("../%1/").arg(MKSTR(LIBSUFFIX));
147  #endif
148  foreach(const QString& lib, libs) {
149  d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
150  }
151  #endif
152  d->appendDir( appPath + ".." + QDir::separator() + QSTR_DRUMSTICK, result );
153  QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
154  if(!envdir.isEmpty()) {
155  d->appendDir(QString(envdir), result );
156  }
157  d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
158  d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
159  foreach(const QString& path, QCoreApplication::libraryPaths()) {
160  d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
161  }
162  return result;
163  }
164 
170  void BackendManager::refresh(QSettings *settings)
171  {
172  QString name_in;
173  QString name_out;
174  QStringList names;
175  QStringList paths;
176 
177  if (settings != nullptr) {
178  settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
179  d->appendDir(settings->value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
180  name_in = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
181  name_out = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
182  names << settings->value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
183  names << (name_in.isEmpty() ? QLatin1String("MIDI In") : name_in);
184  names << (name_out.isEmpty() ? QLatin1String("MIDI Out") : name_out);
185  settings->endGroup();
186  }
187  paths << defaultPaths();
188  d->clearLists();
189 
190  // Dynamic backends
191  foreach(const QString& dir, paths) {
192  QDir pluginsDir(dir);
193  foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
194  if (QLibrary::isLibrary(fileName)) {
195  QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
196  QObject *obj = loader.instance();
197  if (obj != nullptr) {
198  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
199  if (input != nullptr && !d->m_inputsList.contains(input)) {
200  if (!name_in.isEmpty()) {
201  input->setPublicName(name_in);
202  }
203  input->setExcludedConnections(names);
204  d->m_inputsList << input;
205  } else {
206  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
207  if (output != nullptr && !d->m_outputsList.contains(output)) {
208  if (!name_out.isEmpty()) {
209  output->setPublicName(name_out);
210  }
211  output->setExcludedConnections(names);
212  d->m_outputsList << output;
213  }
214  }
215  }
216  }
217  }
218  }
219 
220  // Static backends
221  foreach(QObject* obj, QPluginLoader::staticInstances()) {
222  if (obj != nullptr) {
223  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
224  if (input != nullptr && !d->m_inputsList.contains(input)) {
225  input->setPublicName(name_in);
226  input->setExcludedConnections(names);
227  d->m_inputsList << input;
228  } else {
229  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
230  if (output != nullptr && !d->m_outputsList.contains(output)) {
231  output->setPublicName(name_out);
232  output->setExcludedConnections(names);
233  d->m_outputsList << output;
234  }
235  }
236  }
237  }
238  }
239 
241  {
242  return d->m_inputsList;
243  }
244 
245  QList<MIDIOutput*> BackendManager::availableOutputs()
246  {
247  return d->m_outputsList;
248  }
249 
251  {
252  foreach (MIDIInput* i, d->m_inputsList) {
253  if (i->backendName() == name) {
254  return i;
255  }
256  }
257  return nullptr;
258  }
259 
261  {
262  foreach (MIDIOutput* i, d->m_outputsList) {
263  if (i->backendName() == name) {
264  return i;
265  }
266  }
267  return nullptr;
268  }
269 
270 }}
drumstick::rt::BackendManager::inputBackendByName
MIDIInput * inputBackendByName(const QString name)
inputBackendByName
Definition: backendmanager.cpp:250
QObject
drumstick::rt::MIDIOutput
MIDI OUT interface.
Definition: rtmidioutput.h:80
drumstick::rt::BackendManager::outputBackendByName
MIDIOutput * outputBackendByName(const QString name)
outputBackendByName
Definition: backendmanager.cpp:260
drumstick::rt::MIDIInput::backendName
virtual QString backendName()=0
backendName
drumstick::rt::BackendManager::BackendManager
BackendManager()
BackendManager constructor.
Definition: backendmanager.cpp:115
drumstick::rt::MIDIInput
MIDI IN interface.
Definition: rtmidiinput.h:43
drumstick::rt::MIDIInput::setPublicName
virtual void setPublicName(QString name)=0
setPublicName
backendmanager.h
drumstick::rt::MIDIOutput::setExcludedConnections
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
drumstick::rt::BackendManager::availableOutputs
QList< MIDIOutput * > availableOutputs()
availableOutputs
Definition: backendmanager.cpp:245
drumstick::rt::BackendManager::availableInputs
QList< MIDIInput * > availableInputs()
availableInputs
Definition: backendmanager.cpp:240
drumstick::rt::MIDIInput::setExcludedConnections
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
drumstick::rt::MIDIOutput::backendName
virtual QString backendName()=0
backendName
drumstick::rt::BackendManager::~BackendManager
virtual ~BackendManager()
~BackendManager destructor
Definition: backendmanager.cpp:123
drumstick::rt::BackendManager::refresh
void refresh(QSettings *settings=0)
refresh the list of backends
Definition: backendmanager.cpp:170
drumstick::rt::BackendManager::defaultPaths
QStringList defaultPaths()
defaultPaths
Definition: backendmanager.cpp:132
drumstick::rt::MIDIOutput::setPublicName
virtual void setPublicName(QString name)=0
setPublicName