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++) {
98 /* parsing is done (if at all) by initiating a read from
104 // cerr << "+++ READ ON " << name() << endl;
106 int nread = read (buf, sizeof (buf));
108 // cerr << "-- READ (" << nread << " ON " << name() << endl;
111 if ((size_t) nread < sizeof (buf)) {
116 } else if (nread == 0) {
118 } else if (errno == EAGAIN) {
121 fatal << "Error reading from MIDI port " << name() << endmsg;
127 /** Send a clock tick message.
128 * \return true on success.
131 Port::clock (timestamp_t timestamp)
133 static byte clockmsg = 0xf8;
135 if (_mode != O_RDONLY) {
136 return midimsg (&clockmsg, 1, timestamp);
143 Port::cycle_start (nframes_t nframes)
145 _currently_in_cycle = true;
146 _nframes_this_cycle = nframes;
152 _currently_in_cycle = false;
153 _nframes_this_cycle = 0;
157 Port::get_state () const
159 XMLNode* node = new XMLNode ("MIDI-port");
160 node->add_property ("tag", _tagname);
161 node->add_property ("device", _devname);
162 node->add_property ("mode", PortFactory::mode_to_string (_mode));
163 node->add_property ("type", get_typestring());
169 Port::set_state (const XMLNode& /*node*/)
175 Port::gtk_read_callback (void *ptr, int /*fd*/, int /*cond*/)
178 ((Port *)ptr)->read (buf, sizeof (buf));
182 Port::write_callback (byte *msg, unsigned int len, void *ptr)
184 ((Port *)ptr)->write (msg, len, 0);
187 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
190 os << "MIDI::Port { ";
191 os << "device: " << port.device();
193 os << "name: " << port.name();
195 os << "type: " << port.type();
197 os << "mode: " << port.mode();
199 os << "ok: " << port.ok();
205 Port::Descriptor::Descriptor (const XMLNode& node)
207 const XMLProperty *prop;
208 bool have_tag = false;
209 bool have_device = false;
210 bool have_type = false;
211 bool have_mode = false;
213 if ((prop = node.property ("tag")) != 0) {
218 if ((prop = node.property ("device")) != 0) {
219 device = prop->value();
223 if ((prop = node.property ("type")) != 0) {
224 type = PortFactory::string_to_type (prop->value());
228 if ((prop = node.property ("mode")) != 0) {
229 mode = PortFactory::string_to_mode (prop->value());
233 if (!have_tag || !have_device || !have_type || !have_mode) {
234 throw failed_constructor();