2 Copyright (C) 1998 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/xml++.h"
26 #include "pbd/error.h"
27 #include "pbd/failed_constructor.h"
29 #include "midi++/types.h"
30 #include "midi++/port.h"
31 #include "midi++/channel.h"
32 #include "midi++/factory.h"
38 size_t Port::nports = 0;
40 Port::Port (const XMLNode& node)
41 : _currently_in_cycle(false)
42 , _nframes_this_cycle(0)
44 Descriptor desc (node);
46 _ok = false; /* derived class must set to true if constructor
56 _devname = desc.device;
60 if (_mode == O_RDONLY || _mode == O_RDWR) {
61 input_parser = new Parser (*this);
66 if (_mode == O_WRONLY || _mode == O_RDWR) {
67 output_parser = new Parser (*this);
72 for (int i = 0; i < 16; i++) {
73 _channel[i] = new Channel (i, *this);
76 _channel[i]->connect_input_signals ();
80 _channel[i]->connect_output_signals ();
88 for (int i = 0; i < 16; i++) {
94 Port::parse (nframes_t timestamp)
98 /* NOTE: parsing is done (if at all) by initiating a read from
99 the port. Each port implementation calls on the parser
100 once it has data ready.
104 input_parser->set_timestamp (timestamp);
109 // cerr << "+++ READ ON " << name() << endl;
111 int nread = read (buf, sizeof (buf));
113 // cerr << "-- READ (" << nread << " ON " << name() << endl;
116 if ((size_t) nread < sizeof (buf)) {
121 } else if (nread == 0) {
123 } else if (errno == EAGAIN) {
126 fatal << "Error reading from MIDI port " << name() << endmsg;
132 /** Send a clock tick message.
133 * \return true on success.
136 Port::clock (timestamp_t timestamp)
138 static byte clockmsg = 0xf8;
140 if (_mode != O_RDONLY) {
141 return midimsg (&clockmsg, 1, timestamp);
148 Port::cycle_start (nframes_t nframes)
150 _currently_in_cycle = true;
151 _nframes_this_cycle = nframes;
157 _currently_in_cycle = false;
158 _nframes_this_cycle = 0;
162 Port::get_state () const
164 XMLNode* node = new XMLNode ("MIDI-port");
165 node->add_property ("tag", _tagname);
166 node->add_property ("device", _devname);
167 node->add_property ("mode", PortFactory::mode_to_string (_mode));
168 node->add_property ("type", get_typestring());
171 byte device_inquiry[6];
173 device_inquiry[0] = 0xf0;
174 device_inquiry[0] = 0x7e;
175 device_inquiry[0] = 0x7f;
176 device_inquiry[0] = 0x06;
177 device_inquiry[0] = 0x02;
178 device_inquiry[0] = 0xf7;
180 write (device_inquiry, sizeof (device_inquiry), 0);
187 Port::set_state (const XMLNode& /*node*/)
193 Port::gtk_read_callback (void *ptr, int /*fd*/, int /*cond*/)
196 ((Port *)ptr)->read (buf, sizeof (buf));
200 Port::write_callback (byte *msg, unsigned int len, void *ptr)
202 ((Port *)ptr)->write (msg, len, 0);
205 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
208 os << "MIDI::Port { ";
209 os << "device: " << port.device();
211 os << "name: " << port.name();
213 os << "type: " << port.type();
215 os << "mode: " << port.mode();
217 os << "ok: " << port.ok();
223 Port::Descriptor::Descriptor (const XMLNode& node)
225 const XMLProperty *prop;
226 bool have_tag = false;
227 bool have_device = false;
228 bool have_type = false;
229 bool have_mode = false;
231 if ((prop = node.property ("tag")) != 0) {
236 if ((prop = node.property ("device")) != 0) {
237 device = prop->value();
241 if ((prop = node.property ("type")) != 0) {
242 type = PortFactory::string_to_type (prop->value());
246 if ((prop = node.property ("mode")) != 0) {
247 mode = PortFactory::string_to_mode (prop->value());
251 if (!have_tag || !have_device || !have_type || !have_mode) {
252 throw failed_constructor();