drumstick  1.0.2
ossinput_p.cpp
00001 /*
00002     Drumstick RT (realtime MIDI In/Out)
00003     Copyright (C) 2009-2015 Pedro Lopez-Cabanillas <plcl@users.sf.net>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License along
00016     with this program; if not, write to the Free Software Foundation, Inc.,
00017     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 */
00019 
00020 #include <QObject>
00021 #include <QFile>
00022 #include <QDir>
00023 #include <QDebug>
00024 
00025 #include "ossinput_p.h"
00026 #include "ossinput.h"
00027 
00028 namespace drumstick {
00029 namespace rt {
00030 
00031 static QString DEFAULT_PUBLIC_NAME(QLatin1String("MIDI In"));
00032 
00033 OSSInputPrivate::OSSInputPrivate(QObject *parent) : QObject(parent),
00034     m_inp(qobject_cast<OSSInput *>(parent)),
00035     m_out(0),
00036     m_device(0),
00037     m_notifier(0),
00038     m_parser(0),
00039     m_thruEnabled(false),
00040     m_advanced(false),
00041     m_publicName(DEFAULT_PUBLIC_NAME)
00042 {
00043     reloadDeviceList();
00044 }
00045 
00046 void OSSInputPrivate::reloadDeviceList(bool advanced)
00047 {
00048     QDir dir("/dev");
00049     QStringList filters;
00050     m_advanced = advanced;
00051     filters << "dmmidi*" << "admmidi*";
00052     if (advanced) {
00053         filters << "midi*" << "amidi*";
00054     }
00055     dir.setNameFilters(filters);
00056     dir.setFilter(QDir::System);
00057     dir.setSorting(QDir::Name);
00058     m_inputDevices.clear();
00059     QFileInfoList listInfo = dir.entryInfoList();
00060     foreach(const QFileInfo &info, listInfo) {
00061         m_inputDevices << info.absoluteFilePath();
00062     }
00063 }
00064 
00065 void OSSInputPrivate::open(QString portName)
00066 {
00067     QFile *f = new QFile(portName);
00068     m_currentInput = portName;
00069     m_device = f;
00070     m_device->open( QIODevice::ReadOnly | QIODevice::Unbuffered );
00071     m_notifier = new QSocketNotifier(f->handle(), QSocketNotifier::Read);
00072     m_parser = new MIDIParser(m_inp);
00073     m_buffer.clear();
00074     connect(m_notifier, SIGNAL(activated(int)), this, SLOT(processIncomingMessages(int)));
00075     //qDebug() << Q_FUNC_INFO << portName;
00076 }
00077 
00078 void OSSInputPrivate::close()
00079 {
00080     if (m_device != 0) {
00081         m_device->close();
00082         delete m_notifier;
00083         delete m_device;
00084         delete m_parser;
00085         m_device = 0;
00086         m_parser = 0;
00087     }
00088     m_currentInput.clear();
00089 }
00090 
00091 void OSSInputPrivate::setMIDIThruDevice(MIDIOutput* device)
00092 {
00093     m_out = device;
00094     if (m_parser != 0) {
00095         m_parser->setMIDIThruDevice(device);
00096     }
00097 }
00098 
00099 void OSSInputPrivate::processIncomingMessages(int)
00100 {
00101     char ch;
00102     m_device->getChar(&ch);
00103     if (m_parser != 0) {
00104         uchar uch = static_cast<unsigned>(ch);
00105         m_parser->parse(uch);
00106     }
00107 }
00108 
00109 }}