drumstick  1.0.1
backendmanager.cpp
Go to the documentation of this file.
1 /*
2  Drumstick RT (realtime MIDI In/Out)
3  Copyright (C) 2009-2015 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(ALSA_BACKEND)
28 Q_IMPORT_PLUGIN(ALSAMIDIInput)
29 Q_IMPORT_PLUGIN(ALSAMIDIOutput)
30 #endif
31 
32 #if defined(MAC_BACKEND)
33 Q_IMPORT_PLUGIN(MacMIDIInput)
34 Q_IMPORT_PLUGIN(MacMIDIOutput)
35 #endif
36 
37 #if defined(WIN_BACKEND)
38 Q_IMPORT_PLUGIN(WinMIDIInput)
39 Q_IMPORT_PLUGIN(WinMIDIOutput)
40 #endif
41 
42 #if defined(NET_BACKEND)
43 Q_IMPORT_PLUGIN(NetMIDIInput)
44 Q_IMPORT_PLUGIN(NetMIDIOutput)
45 #endif
46 
47 #if defined(DUMMY_BACKEND)
48 Q_IMPORT_PLUGIN(DummyInput)
49 Q_IMPORT_PLUGIN(DummyOutput)
50 #endif
51 
52 #if defined(SYNTH_BACKEND)
53 Q_IMPORT_PLUGIN(SynthOutput)
54 #endif
55 
56 #if defined(OSS_BACKEND)
57 Q_IMPORT_PLUGIN(OSSInput)
58 Q_IMPORT_PLUGIN(OSSOutput)
59 #endif
60 
66 namespace drumstick {
67 namespace rt {
68 
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  #elif defined(Q_OS_MAC)
137  d->appendDir( appPath + QStringLiteral("../PlugIns/") + QSTR_DRUMSTICK, result );
138  #else // Linux, Unix...
139  QStringList libs;
140  libs << "../lib/" << "../lib32/" << "../lib64/";
141  foreach(const QString& lib, libs) {
142  d->appendDir( appPath + lib + QSTR_DRUMSTICK, result );
143  }
144  #endif
145  d->appendDir( appPath + ".." + QDir::separator() + QSTR_DRUMSTICK, result );
146  QByteArray envdir = qgetenv(QSTR_DRUMSTICKRT.toLatin1());
147  if(!envdir.isEmpty()) {
148  d->appendDir(QString(envdir), result );
149  }
150  d->appendDir( QDir::homePath() + QDir::separator() + QSTR_DRUMSTICK, result );
151  d->appendDir( QLibraryInfo::location(QLibraryInfo::PluginsPath) + QDir::separator() + QSTR_DRUMSTICK, result );
152  foreach(const QString& path, QCoreApplication::libraryPaths()) {
153  d->appendDir( path + QDir::separator() + QSTR_DRUMSTICK, result );
154  }
155  return result;
156  }
157 
163  void BackendManager::refresh(QSettings *settings)
164  {
165  QString name_in;
166  QString name_out;
167  QStringList names;
168  QStringList paths;
169 
170  if (settings != 0) {
171  settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
172  d->appendDir(settings->value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
173  name_in = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
174  name_out = settings->value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
175  names << settings->value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
176  names << name_in;
177  names << name_out;
178  settings->endGroup();
179  }
180  paths << defaultPaths();
181  d->clearLists();
182 
183  // Dynamic backends
184  foreach(const QString& dir, paths) {
185  QDir pluginsDir(dir);
186  foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
187  if (QLibrary::isLibrary(fileName)) {
188  QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
189  QObject *obj = loader.instance();
190  if (obj != 0) {
191  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
192  if (input != 0 && !d->m_inputsList.contains(input)) {
193  input->setPublicName(name_in);
194  input->setExcludedConnections(names);
195  d->m_inputsList << input;
196  } else {
197  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
198  if (output != 0 && !d->m_outputsList.contains(output)) {
199  output->setPublicName(name_out);
200  output->setExcludedConnections(names);
201  d->m_outputsList << output;
202  }
203  }
204  }
205  }
206  }
207  }
208 
209  // Static backends
210  foreach(QObject* obj, QPluginLoader::staticInstances()) {
211  if (obj != 0) {
212  MIDIInput *input = qobject_cast<MIDIInput*>(obj);
213  if (input != 0 && !d->m_inputsList.contains(input)) {
214  input->setPublicName(name_in);
215  input->setExcludedConnections(names);
216  d->m_inputsList << input;
217  } else {
218  MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
219  if (output != 0 && !d->m_outputsList.contains(output)) {
220  output->setPublicName(name_out);
221  output->setExcludedConnections(names);
222  d->m_outputsList << output;
223  }
224  }
225  }
226  }
227  }
228 
230  {
231  return d->m_inputsList;
232  }
233 
234  QList<MIDIOutput*> BackendManager::availableOutputs()
235  {
236  return d->m_outputsList;
237  }
238 
239 }}
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:37
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
BackendManager()
BackendManager constructor.
virtual void setPublicName(QString name)=0
setPublicName
MIDI OUT interface.
Definition: rtmidioutput.h:71