1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
Copyright (C) 1999 Paul Barton-Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id$
*/
#include <fcntl.h>
#include <cerrno>
#include <cstring>
#include <sys/time.h>
#include <../pbd/pbd/error.h>
#include <../pbd/pbd/pathscanner.h>
#include <midi++/types.h>
#include <midi++/fd_midiport.h>
using namespace std;
using namespace MIDI;
string *FD_MidiPort::midi_dirpath = 0;
string *FD_MidiPort::midi_filename_pattern = 0;
FD_MidiPort::FD_MidiPort (PortRequest &req,
const string &dirpath,
const string &pattern)
: Port (req)
{
open (req);
if (_fd < 0) {
switch (errno) {
case EBUSY:
error << "MIDI: port device in use" << endmsg;
req.status = PortRequest::Busy;
break;
case ENOENT:
error << "MIDI: no such port device" << endmsg;
req.status = PortRequest::NoSuchFile;
break;
case EACCES:
error << "MIDI: access to port denied" << endmsg;
req.status = PortRequest::NotAllowed;
break;
default:
req.status = PortRequest::Unknown;
}
} else {
_ok = true;
req.status = PortRequest::OK;
if (midi_dirpath == 0) {
midi_dirpath = new string (dirpath);
midi_filename_pattern = new string (pattern);
}
if ((req.mode & O_NONBLOCK) == 0) {
/* we unconditionally set O_NONBLOCK during
open, but the request didn't ask for it,
so remove it.
*/
int flags = fcntl (_fd, F_GETFL, 0);
fcntl (_fd, F_SETFL, flags & ~(O_NONBLOCK));
}
}
}
void
FD_MidiPort::open (PortRequest &req)
{
int mode = req.mode | O_NONBLOCK;
_fd = ::open (req.devname, mode);
}
vector<string *> *
FD_MidiPort::list_devices ()
{
PathScanner scanner;
return scanner (*midi_dirpath, *midi_filename_pattern, false, true);
}
int
FD_MidiPort::selectable () const
{
long flags;
/* turn on non-blocking mode, since we plan to use select/poll
to tell us when there is data to read.
*/
flags = fcntl (_fd, F_GETFL);
flags |= O_NONBLOCK;
if (fcntl (_fd, F_SETFL, flags)) {
error << "FD_MidiPort: could not turn on non-blocking mode"
<< " (" << strerror (errno)
<< ')'
<< endmsg;
return -1;
}
return _fd;
}
int
FD_MidiPort::do_slow_write (byte *msg, unsigned int msglen)
{
size_t n;
size_t i;
for (n = 0; n < msglen; n++) {
if (::write (_fd, &msg[n], 1) != 1) {
break;
}
bytes_written++;
for (i = 0; i < slowdown * 10000; i++);
}
if (n && output_parser) {
output_parser->raw_preparse (*output_parser, msg, n, get_current_host_time());
for (unsigned int i = 0; i < n; i++) {
output_parser->scanner (msg[i]);
}
output_parser->raw_postparse (*output_parser, msg, n);
}
return n;
}
int
FD_MidiPort::read (byte* buf, size_t max)
{
int nread;
if ((_mode & O_ACCMODE) == O_WRONLY) {
return -EACCES;
}
// cerr << "MIDI: read up to " << max << " from " << _fd << " (" << name() << ')' << endl;
if ((nread = ::read (_fd, buf, max)) > 0) {
bytes_read += nread;
// cerr << " read " << nread << endl;
if (input_parser) {
input_parser->raw_preparse
(*input_parser, buf, nread, get_current_host_time());
for (int i = 0; i < nread; i++) {
input_parser->scanner (buf[i]);
}
input_parser->raw_postparse
(*input_parser, buf, nread);
}
}
return nread;
}
timestamp_t FD_MidiPort::get_current_host_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (timestamp_t)(tv.tv_usec * 1e-6) + (timestamp_t)(tv.tv_sec);
}
|