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.
24 #include <pbd/xml++.h>
25 #include <pbd/failed_constructor.h>
27 #include <midi++/types.h>
28 #include <midi++/port.h>
29 #include <midi++/channel.h>
30 #include <midi++/factory.h>
35 size_t Port::nports = 0;
37 Port::Port (const XMLNode& node)
39 Descriptor desc (node);
41 _ok = false; /* derived class must set to true if constructor
51 _devname = desc.device;
55 if (_mode == O_RDONLY || _mode == O_RDWR) {
56 input_parser = new Parser (*this);
61 if (_mode == O_WRONLY || _mode == O_RDWR) {
62 output_parser = new Parser (*this);
67 for (int i = 0; i < 16; i++) {
68 _channel[i] = new Channel (i, *this);
71 _channel[i]->connect_input_signals ();
75 _channel[i]->connect_output_signals ();
84 for (int i = 0; i < 16; i++) {
90 Port::get_state () const
92 XMLNode* node = new XMLNode ("MIDI-port");
93 node->add_property ("tag", _tagname);
94 node->add_property ("device", _devname);
95 node->add_property ("mode", PortFactory::mode_to_string (_mode));
96 node->add_property ("type", get_typestring());
102 Port::set_state (const XMLNode& node)
111 static byte clockmsg = 0xf8;
113 if (_mode != O_RDONLY) {
114 return midimsg (&clockmsg, 1);
122 Port::selector_read_callback (Selectable *s, Select::Condition cond)
126 read (buf, sizeof (buf));
131 Port::xforms_read_callback (int cond, int fd, void *ptr)
136 ((Port *)ptr)->read (buf, sizeof (buf));
140 Port::gtk_read_callback (void *ptr, int fd, int cond)
145 ((Port *)ptr)->read (buf, sizeof (buf));
149 Port::write_callback (byte *msg, unsigned int len, void *ptr)
152 ((Port *)ptr)->write (msg, len);
155 std::ostream & MIDI::operator << ( std::ostream & os, const MIDI::Port & port )
158 os << "MIDI::Port { ";
159 os << "device: " << port.device();
161 os << "name: " << port.name();
163 os << "type: " << port.type();
165 os << "mode: " << port.mode();
167 os << "ok: " << port.ok();
173 Port::Descriptor::Descriptor (const XMLNode& node)
175 const XMLProperty *prop;
176 bool have_tag = false;
177 bool have_device = false;
178 bool have_type = false;
179 bool have_mode = false;
181 if ((prop = node.property ("tag")) != 0) {
186 if ((prop = node.property ("device")) != 0) {
187 device = prop->value();
191 if ((prop = node.property ("type")) != 0) {
192 type = PortFactory::string_to_type (prop->value());
196 if ((prop = node.property ("mode")) != 0) {
197 mode = PortFactory::string_to_mode (prop->value());
201 if (!have_tag || !have_device || !have_type || !have_mode) {
202 throw failed_constructor();