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