drumstick  1.0.2
ossinput_p.cpp
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 <QObject>
21 #include <QFile>
22 #include <QDir>
23 #include <QDebug>
24 
25 #include "ossinput_p.h"
26 #include "ossinput.h"
27 
28 namespace drumstick {
29 namespace rt {
30 
31 static QString DEFAULT_PUBLIC_NAME(QLatin1String("MIDI In"));
32 
33 OSSInputPrivate::OSSInputPrivate(QObject *parent) : QObject(parent),
34  m_inp(qobject_cast<OSSInput *>(parent)),
35  m_out(0),
36  m_device(0),
37  m_notifier(0),
38  m_parser(0),
39  m_thruEnabled(false),
40  m_advanced(false),
41  m_publicName(DEFAULT_PUBLIC_NAME)
42 {
43  reloadDeviceList();
44 }
45 
46 void OSSInputPrivate::reloadDeviceList(bool advanced)
47 {
48  QDir dir("/dev");
49  QStringList filters;
50  m_advanced = advanced;
51  filters << "dmmidi*" << "admmidi*";
52  if (advanced) {
53  filters << "midi*" << "amidi*";
54  }
55  dir.setNameFilters(filters);
56  dir.setFilter(QDir::System);
57  dir.setSorting(QDir::Name);
58  m_inputDevices.clear();
59  QFileInfoList listInfo = dir.entryInfoList();
60  foreach(const QFileInfo &info, listInfo) {
61  m_inputDevices << info.absoluteFilePath();
62  }
63 }
64 
65 void OSSInputPrivate::open(QString portName)
66 {
67  QFile *f = new QFile(portName);
68  m_currentInput = portName;
69  m_device = f;
70  m_device->open( QIODevice::ReadOnly | QIODevice::Unbuffered );
71  m_notifier = new QSocketNotifier(f->handle(), QSocketNotifier::Read);
72  m_parser = new MIDIParser(m_inp);
73  m_buffer.clear();
74  connect(m_notifier, SIGNAL(activated(int)), this, SLOT(processIncomingMessages(int)));
75  //qDebug() << Q_FUNC_INFO << portName;
76 }
77 
78 void OSSInputPrivate::close()
79 {
80  if (m_device != 0) {
81  m_device->close();
82  delete m_notifier;
83  delete m_device;
84  delete m_parser;
85  m_device = 0;
86  m_parser = 0;
87  }
88  m_currentInput.clear();
89 }
90 
91 void OSSInputPrivate::setMIDIThruDevice(MIDIOutput* device)
92 {
93  m_out = device;
94  if (m_parser != 0) {
95  m_parser->setMIDIThruDevice(device);
96  }
97 }
98 
99 void OSSInputPrivate::processIncomingMessages(int)
100 {
101  char ch;
102  m_device->getChar(&ch);
103  if (m_parser != 0) {
104  uchar uch = static_cast<unsigned>(ch);
105  m_parser->parse(uch);
106  }
107 }
108 
109 }}
The QObject class is the base class of all Qt objects.