2 Copyright (C) 1999 Paul Barton-Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "pbd/error.h"
26 #include "pbd/pathscanner.h"
28 #include "midi++/types.h"
29 #include "midi++/fd_midiport.h"
35 string *FD_MidiPort::midi_dirpath = 0;
36 string *FD_MidiPort::midi_filename_pattern = 0;
38 FD_MidiPort::FD_MidiPort (const XMLNode& node,
39 const string &dirpath,
40 const string &pattern)
43 Descriptor desc (node);
50 error << "MIDI: port device in use" << endmsg;
53 error << "MIDI: no such port device" << endmsg;
56 error << "MIDI: access to port denied" << endmsg;
64 if (midi_dirpath == 0) {
65 midi_dirpath = new string (dirpath);
66 midi_filename_pattern = new string (pattern);
69 if ((desc.mode & O_NONBLOCK) == 0) {
70 /* we unconditionally set O_NONBLOCK during
71 open, but the request didn't ask for it,
75 int flags = fcntl (_fd, F_GETFL, 0);
76 fcntl (_fd, F_SETFL, flags & ~(O_NONBLOCK));
82 FD_MidiPort::open (const Descriptor& desc)
85 int mode = desc.mode | O_NONBLOCK;
86 _fd = ::open (desc.device.c_str(), mode);
90 FD_MidiPort::list_devices ()
95 return scanner (*midi_dirpath, *midi_filename_pattern, false, true, false);
99 FD_MidiPort::selectable () const
104 /* turn on non-blocking mode, since we plan to use select/poll
105 to tell us when there is data to read.
108 flags = fcntl (_fd, F_GETFL);
111 if (fcntl (_fd, F_SETFL, flags)) {
112 error << "FD_MidiPort: could not turn on non-blocking mode"
113 << " (" << strerror (errno)
125 FD_MidiPort::do_slow_write (byte *msg, unsigned int msglen)
131 for (n = 0; n < msglen; n++) {
133 if (::write (_fd, &msg[n], 1) != 1) {
138 for (i = 0; i < slowdown * 10000; i++) {}
142 if (n && output_parser) {
143 output_parser->raw_preparse (*output_parser, msg, n);
144 for (unsigned int i = 0; i < n; i++) {
145 output_parser->scanner (msg[i]);
147 output_parser->raw_postparse (*output_parser, msg, n);
154 FD_MidiPort::read (byte* buf, size_t max)
158 if ((_mode & O_ACCMODE) == O_WRONLY) {
162 // cerr << "MIDI: read up to " << max << " from " << _fd << " (" << name() << ')' << endl;
164 if ((nread = ::read (_fd, buf, max)) > 0) {
167 // cerr << " read " << nread << endl;
170 input_parser->raw_preparse
171 (*input_parser, buf, nread);
172 for (int i = 0; i < nread; i++) {
173 input_parser->scanner (buf[i]);
175 input_parser->raw_postparse
176 (*input_parser, buf, nread);