drumstick  1.1.1
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 
67 namespace drumstick {
68 namespace rt {
69 
87  class BackendManager::BackendManagerPrivate {
88  public:
89  QList<MIDIInput*> m_inputsList;
90  QList<MIDIOutput*> m_outputsList;
91  ~BackendManagerPrivate()
92  {
93  clearLists();
94  }
95  void clearLists()
96  {
97  m_inputsList.clear();
98  m_outputsList.clear();
99  }
100  void appendDir(const QString& candidate, QStringList& result)
101  {
102  //qDebug() << "testing " << candidate;
103  QDir checked(candidate);
104  if (checked.exists() && !result.contains(checked.absolutePath())) {
105  result << checked.absolutePath();
106  }
107  }
108  };
109 
113  BackendManager::BackendManager(): d(new BackendManagerPrivate)
114  {
115  refresh();
116  }
117 
122  {
123  delete d;
124  }
125 
131  {
132  QStringList result;
133  QString appPath = QCoreApplication::applicationDirPath() + QDir::separator();
134  #if defined(Q_OS_WIN)
135  d->appendDir( appPath + QSTR_DRUMSTICK, result );
136  d->appendDir( appPath + "../lib/" + QSTR_DRUMSTICK, result );
137  #else
138  #if defined(Q_OS_MAC)
139  d->appendDir( appPath + QStringLiteral("../PlugIns/") + QSTR_DRUMSTICK, result );
140  #endif // Linux, Unix...
141  QStringList libs;
142  libs << "../lib/" << "../lib32/" << "../lib64/";
143  foreach(const QString& lib, libs) {
144  d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
145  }
146  #endif
147  d->appendDir( appPath + ".." + QDir::separator() + QSTR_DRUMSTICK, result );
148  QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
149  if(!envdir.isEmpty()) {
150  d->appendDir(QString(envdir), result );
151  }
152  d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
153  d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
154  foreach(const QString& path, QCoreApplication::libraryPaths()) {
155  d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
156  }
157  return result;
158  }
159 
165  void BackendManager::refresh(QSettings *settings)
166  {
167  QString name_in;
168  QString name_out;
169  QStringList names;
170  QStringList paths;
171 
172  if (settings != 0) {
173  settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
174  d->appendDir(settings->value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
175  name_in = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
176  name_out = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
177  names << settings->value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
178  names << (name_in.isEmpty() ? QLatin1String("MIDI In") : name_in);
179  names << (name_out.isEmpty() ? QLatin1String("MIDI Out") : name_out);
180  settings->endGroup();
181  }
182  paths << defaultPaths();
183  d->clearLists();
184 
185  // Dynamic backends
186  foreach(const QString& dir, paths) {
187  QDir pluginsDir(dir);
188  foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
189  if (QLibrary::isLibrary(fileName)) {
190  QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
191  QObject *obj = loader.instance();
192  if (obj != 0) {
193  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
194  if (input != 0 && !d->m_inputsList.contains(input)) {
195  if (!name_in.isEmpty()) {
196  input->setPublicName(name_in);
197  }
198  input->setExcludedConnections(names);
199  d->m_inputsList << input;
200  } else {
201  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
202  if (output != 0 && !d->m_outputsList.contains(output)) {
203  if (!name_out.isEmpty()) {
204  output->setPublicName(name_out);
205  }
206  output->setExcludedConnections(names);
207  d->m_outputsList << output;
208  }
209  }
210  }
211  }
212  }
213  }
214 
215  // Static backends
216  foreach(QObject* obj, QPluginLoader::staticInstances()) {
217  if (obj != 0) {
218  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
219  if (input != 0 && !d->m_inputsList.contains(input)) {
220  input->setPublicName(name_in);
221  input->setExcludedConnections(names);
222  d->m_inputsList << input;
223  } else {
224  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
225  if (output != 0 && !d->m_outputsList.contains(output)) {
226  output->setPublicName(name_out);
227  output->setExcludedConnections(names);
228  d->m_outputsList << output;
229  }
230  }
231  }
232  }
233  }
234 
236  {
237  return d->m_inputsList;
238  }
239 
240  QList<MIDIOutput*> BackendManager::availableOutputs()
241  {
242  return d->m_outputsList;
243  }
244 
246  {
247  foreach (MIDIInput* i, d->m_inputsList) {
248  if (i->backendName() == name) {
249  return i;
250  }
251  }
252  return 0;
253  }
254 
256  {
257  foreach (MIDIOutput* i, d->m_outputsList) {
258  if (i->backendName() == name) {
259  return i;
260  }
261  }
262  return 0;
263  }
264 
265 }}
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