2 Copyright (C) 2006,2007 John Anderson
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.
23 #include <sigc++/sigc++.h>
24 #include <boost/shared_array.hpp>
26 #include "pbd/failed_constructor.h"
28 #include "midi++/types.h"
29 #include "midi++/ipmidi_port.h"
31 #include "ardour/async_midi_port.h"
32 #include "ardour/debug.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/session.h"
35 #include "ardour/audioengine.h"
38 #include "mackie_control_protocol.h"
40 #include "surface_port.h"
46 using namespace ARDOUR;
47 using namespace ArdourSurface;
48 using namespace Mackie;
50 SurfacePort::SurfacePort (Surface& s)
53 if (_surface->mcp().device_info().uses_ipmidi()) {
54 _input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
55 _output_port = _input_port;
62 if (_surface->mcp().device_info().extenders() > 0) {
63 if (_surface->number() == _surface->mcp().device_info().master_position()) {
64 in_name = X_("mackie control in");
65 out_name = X_("mackie control out");
67 in_name = string_compose (X_("mackie control in ext %1"), (_surface->number() + 1));
68 out_name = string_compose (X_("mackie control out ext %1"), _surface->number() + 1);
71 in_name = X_("mackie control in");
72 out_name = X_("mackie control out");
75 _async_in = AudioEngine::instance()->register_input_port (DataType::MIDI, in_name, true);
76 _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, out_name, true);
78 if (_async_in == 0 || _async_out == 0) {
79 throw failed_constructor();
82 _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
83 _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
87 SurfacePort::~SurfacePort()
89 if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
94 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering input port %1\n", _async_in->name()));
95 AudioEngine::instance()->unregister_port (_async_in);
96 _async_in.reset ((ARDOUR::Port*) 0);
100 _output_port->drain (10000, 250000);
101 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering output port %1\n", _async_out->name()));
102 AudioEngine::instance()->unregister_port (_async_out);
103 _async_out.reset ((ARDOUR::Port*) 0);
109 SurfacePort::get_state ()
111 XMLNode* node = new XMLNode (X_("Port"));
113 if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
114 /* no state required for IPMidi ports */
120 child = new XMLNode (X_("Input"));
121 child->add_child_nocopy (_async_in->get_state());
122 node->add_child_nocopy (*child);
125 child = new XMLNode (X_("Output"));
126 child->add_child_nocopy (_async_out->get_state());
127 node->add_child_nocopy (*child);
133 SurfacePort::set_state (const XMLNode& node, int version)
135 if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
141 if ((child = node.child (X_("Input"))) != 0) {
142 XMLNode* portnode = child->child (Port::state_node_name.c_str());
144 _async_in->set_state (*portnode, version);
148 if ((child = node.child (X_("Output"))) != 0) {
149 XMLNode* portnode = child->child (Port::state_node_name.c_str());
151 _async_out->set_state (*portnode, version);
159 SurfacePort::reconnect ()
161 _async_out->reconnect ();
162 _async_in->reconnect ();
166 SurfacePort::input_name () const
168 return _async_in->name();
172 SurfacePort::output_name () const
174 return _async_out->name();
177 // wrapper for one day when strerror_r is working properly
178 string fetch_errmsg (int error_number)
180 char * msg = strerror (error_number);
185 SurfacePort::write (const MidiByteArray & mba)
188 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
192 DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
194 if (mba[0] != 0xf0 && mba.size() > 3) {
195 std::cerr << "TOO LONG WRITE: " << mba << std::endl;
198 /* this call relies on std::vector<T> using contiguous storage. not
199 * actually guaranteed by the standard, but way, way beyond likely.
202 int count = output_port().write (&mba[0], mba.size(), 0);
204 if (count != (int) mba.size()) {
208 cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
210 } else if (errno != EAGAIN) {
212 os << "Surface: couldn't write to port " << output_port().name();
213 os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
214 cout << os.str() << endl;
224 Mackie::operator << (ostream & os, const SurfacePort & port)
227 os << "name: " << port.input_port().name() << " " << port.output_port().name();