drumstick  1.1.2
backendmanager.cpp
Go to the documentation of this file.
1 /*
2  Drumstick RT (realtime MIDI In/Out)
3  Copyright (C) 2009-2018 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>
24 #include "backendmanager.h"
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(x) #x
63 #define MKSTR(x) _MKSTR(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  //qDebug() << "testing " << candidate;
106  QDir checked(candidate);
107  if (checked.exists() && !result.contains(checked.absolutePath())) {
108  result << checked.absolutePath();
109  }
110  }
111  };
112 
116  BackendManager::BackendManager(): d(new BackendManagerPrivate)
117  {
118  refresh();
119  }
120 
125  {
126  delete d;
127  }
128 
134  {
135  QStringList result;
136  QString appPath = QCoreApplication::applicationDirPath() + QDir::separator();
137  #if defined(Q_OS_WIN)
138  d->appendDir( appPath + QSTR_DRUMSTICK, result );
139  d->appendDir( appPath + "../lib/" + QSTR_DRUMSTICK, result );
140  #else
141  #if defined(Q_OS_MAC)
142  d->appendDir( appPath + QStringLiteral("../PlugIns/") + QSTR_DRUMSTICK, result );
143  #endif // Linux, Unix...
144  QStringList libs;
145  libs << "../lib/";
146  #if defined(LIBSUFFIX)
147  libs << QString("../lib%1/").arg(MKSTR(LIBSUFFIX));
148  #endif
149  foreach(const QString& lib, libs) {
150  d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
151  }
152  #endif
153  d->appendDir( appPath + ".." + QDir::separator() + QSTR_DRUMSTICK, result );
154  QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
155  if(!envdir.isEmpty()) {
156  d->appendDir(QString(envdir), result );
157  }
158  d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
159  d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
160  foreach(const QString& path, QCoreApplication::libraryPaths()) {
161  d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
162  }
163  return result;
164  }
165 
171  void BackendManager::refresh(QSettings *settings)
172  {
173  QString name_in;
174  QString name_out;
175  QStringList names;
176  QStringList paths;
177 
178  if (settings != 0) {
179  settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
180  d->appendDir(settings->value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
181  name_in = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
182  name_out = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
183  names << settings->value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
184  names << (name_in.isEmpty() ? QLatin1String("MIDI In") : name_in);
185  names << (name_out.isEmpty() ? QLatin1String("MIDI Out") : name_out);
186  settings->endGroup();
187  }
188  paths << defaultPaths();
189  d->clearLists();
190 
191  // Dynamic backends
192  foreach(const QString& dir, paths) {
193  QDir pluginsDir(dir);
194  foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
195  if (QLibrary::isLibrary(fileName)) {
196  QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
197  QObject *obj = loader.instance();
198  if (obj != 0) {
199  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
200  if (input != 0 && !d->m_inputsList.contains(input)) {
201  if (!name_in.isEmpty()) {
202  input->setPublicName(name_in);
203  }
204  input->setExcludedConnections(names);
205  d->m_inputsList << input;
206  } else {
207  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
208  if (output != 0 && !d->m_outputsList.contains(output)) {
209  if (!name_out.isEmpty()) {
210  output->setPublicName(name_out);
211  }
212  output->setExcludedConnections(names);
213  d->m_outputsList << output;
214  }
215  }
216  }
217  }
218  }
219  }
220 
221  // Static backends
222  foreach(QObject* obj, QPluginLoader::staticInstances()) {
223  if (obj != 0) {
224  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
225  if (input != 0 && !d->m_inputsList.contains(input)) {
226  input->setPublicName(name_in);
227  input->setExcludedConnections(names);
228  d->m_inputsList << input;
229  } else {
230  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
231  if (output != 0 && !d->m_outputsList.contains(output)) {
232  output->setPublicName(name_out);
233  output->setExcludedConnections(names);
234  d->m_outputsList << output;
235  }
236  }
237  }
238  }
239  }
240 
242  {
243  return d->m_inputsList;
244  }
245 
246  QList<MIDIOutput*> BackendManager::availableOutputs()
247  {
248  return d->m_outputsList;
249  }
250 
252  {
253  foreach (MIDIInput* i, d->m_inputsList) {
254  if (i->backendName() == name) {
255  return i;
256  }
257  }
258  return 0;
259  }
260 
262  {
263  foreach (MIDIOutput* i, d->m_outputsList) {
264  if (i->backendName() == name) {
265  return i;
266  }
267  }
268  return 0;
269  }
270 
271 }}
virtual void setPublicName(QString name)=0
setPublicName
QList< MIDIOutput * > availableOutputs()
availableOutputs
QStringList defaultPaths()
defaultPaths
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
QList< MIDIInput * > availableInputs()
availableInputs
MIDI IN interface.
Definition: rtmidiinput.h:43
The QObject class is the base class of all Qt objects.
Realtime MIDI input/output multiplatform classes.
virtual void setExcludedConnections(QStringList conns)=0
setExcludedConnections
virtual ~BackendManager()
~BackendManager destructor
void refresh(QSettings *settings=0)
refresh the list of backends
virtual QString backendName()=0
backendName
virtual QString backendName()=0
backendName
BackendManager()
BackendManager constructor.
MIDIOutput * outputBackendByName(const QString name)
outputBackendByName
MIDIInput * inputBackendByName(const QString name)
inputBackendByName
virtual void setPublicName(QString name)=0
setPublicName
MIDI OUT interface.
Definition: rtmidioutput.h:77