#include <sigc++/sigc++.h>
#include <boost/shared_array.hpp>
+#include "pbd/failed_constructor.h"
+
#include "midi++/types.h"
-#include "midi++/port.h"
-#include "midi++/manager.h"
+#include "midi++/ipmidi_port.h"
+#include "ardour/async_midi_port.h"
#include "ardour/debug.h"
#include "ardour/rc_configuration.h"
+#include "ardour/session.h"
+#include "ardour/audioengine.h"
+#include "ardour/async_midi_port.h"
+#include "ardour/midiport_manager.h"
#include "controls.h"
-#include "mackie_control_exception.h"
+#include "mackie_control_protocol.h"
#include "surface.h"
#include "surface_port.h"
-
-#include "i18n.h"
+#include "pbd/i18n.h"
using namespace std;
-using namespace Mackie;
using namespace PBD;
+using namespace ARDOUR;
+using namespace ArdourSurface;
+using namespace Mackie;
-/** @param input_port Input MIDI::Port; this object takes responsibility for removing it from
- * the MIDI::Manager and destroying it.
- * @param output_port Output MIDI::Port; responsibility similarly taken.
- */
-SurfacePort::SurfacePort (Surface& s, MIDI::Port & input_port, MIDI::Port & output_port)
+SurfacePort::SurfacePort (Surface& s)
: _surface (&s)
- , _input_port (&input_port)
- , _output_port (&output_port)
- , _active (false)
{
+ if (_surface->mcp().device_info().uses_ipmidi()) {
+ _input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
+ _output_port = _input_port;
+
+ } else {
+
+ string in_name;
+ string out_name;
+
+ if (_surface->mcp().device_info().extenders() > 0) {
+ if (_surface->number() == _surface->mcp().device_info().master_position()) {
+ in_name = X_("mackie control in");
+ out_name = X_("mackie control out");
+ } else {
+ in_name = string_compose (X_("mackie control in ext %1"), (_surface->number() + 1));
+ out_name = string_compose (X_("mackie control out ext %1"), _surface->number() + 1);
+ }
+ } else {
+ in_name = X_("mackie control in");
+ out_name = X_("mackie control out");
+ }
+
+ _async_in = AudioEngine::instance()->register_input_port (DataType::MIDI, in_name, true);
+ _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, out_name, true);
+
+ if (_async_in == 0 || _async_out == 0) {
+ throw failed_constructor();
+ }
+
+ _input_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_in).get();
+ _output_port = boost::dynamic_pointer_cast<AsyncMIDIPort>(_async_out).get();
+ }
}
SurfacePort::~SurfacePort()
{
- close ();
-
- MIDI::Manager* mm = MIDI::Manager::instance ();
-
- if (_input_port) {
- mm->remove_port (_input_port);
+ if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
delete _input_port;
- }
+ _input_port = 0;
+ } else {
+ if (_async_in) {
+ DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering input port %1\n", _async_in->name()));
+ AudioEngine::instance()->unregister_port (_async_in);
+ _async_in.reset ((ARDOUR::Port*) 0);
+ }
- if (_output_port) {
- mm->remove_port (_output_port);
- delete _output_port;
+ if (_async_out) {
+ _output_port->drain (10000, 250000);
+ DEBUG_TRACE (DEBUG::MackieControl, string_compose ("unregistering output port %1\n", _async_out->name()));
+ AudioEngine::instance()->unregister_port (_async_out);
+ _async_out.reset ((ARDOUR::Port*) 0);
+ }
}
}
-// wrapper for one day when strerror_r is working properly
-string fetch_errmsg (int error_number)
-{
- char * msg = strerror (error_number);
- return msg;
-}
-
-MidiByteArray SurfacePort::read()
+XMLNode&
+SurfacePort::get_state ()
{
- const int max_buf_size = 512;
- MIDI::byte buf[max_buf_size];
- MidiByteArray retval;
-
- // check active. Mainly so that the destructor
- // doesn't destroy the mutex while it's still locked
- if (!active()) {
- return retval;
- }
-
- // return nothing read if the lock isn't acquired
-
- // read port and copy to return value
- int nread = input_port().read (buf, sizeof (buf));
-
- if (nread >= 0) {
- retval.copy (nread, buf);
- if ((size_t) nread == sizeof (buf)) {
-#ifdef PORT_DEBUG
- cout << "SurfacePort::read recursive" << endl;
-#endif
- retval << read();
- }
- } else {
- if (errno != EAGAIN) {
- ostringstream os;
- os << "Surface: error reading from port: " << input_port().name();
- os << ": " << errno << fetch_errmsg (errno);
+ XMLNode* node = new XMLNode (X_("Port"));
- cout << os.str() << endl;
- inactive_event();
- throw MackieControlException (os.str());
- }
+ if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
+ /* no state required for IPMidi ports */
+ return *node;
}
-#ifdef PORT_DEBUG
- cout << "SurfacePort::read: " << retval << endl;
-#endif
- return retval;
+
+ XMLNode* child;
+
+ child = new XMLNode (X_("Input"));
+ child->add_child_nocopy (_async_in->get_state());
+ node->add_child_nocopy (*child);
+
+
+ child = new XMLNode (X_("Output"));
+ child->add_child_nocopy (_async_out->get_state());
+ node->add_child_nocopy (*child);
+
+ return *node;
}
-void SurfacePort::write (const MidiByteArray & mba)
+int
+SurfacePort::set_state (const XMLNode& node, int version)
{
- if (mba.empty()) {
- return;
+ if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
+ return 0;
}
-#ifdef PORT_DEBUG
- cout << "SurfacePort::write: " << mba << " to " << output_port().name() << endl;
-#endif
-
- // check active before and after lock - to make sure
- // that the destructor doesn't destroy the mutex while
- // it's still in use
- if (!active()) return;
-
- int count = output_port().write (mba.bytes().get(), mba.size(), 0);
- if (count != (int)mba.size()) {
- if (errno == 0) {
- cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
- } else if (errno != EAGAIN) {
- ostringstream os;
- os << "Surface: couldn't write to port " << output_port().name();
- os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
-
- cout << os.str() << endl;
- inactive_event();
+ XMLNode* child;
+
+ if ((child = node.child (X_("Input"))) != 0) {
+ XMLNode* portnode = child->child (Port::state_node_name.c_str());
+ if (portnode) {
+ _async_in->set_state (*portnode, version);
}
}
-#ifdef PORT_DEBUG
- cout << "SurfacePort::wrote " << count << endl;
-#endif
-}
+ if ((child = node.child (X_("Output"))) != 0) {
+ XMLNode* portnode = child->child (Port::state_node_name.c_str());
+ if (portnode) {
+ _async_out->set_state (*portnode, version);
+ }
+ }
-void SurfacePort::open()
-{
- DEBUG_TRACE (DEBUG::MackieControl, string_compose ("SurfacePort::open %1\n", *this));
- input_port().parser()->sysex.connect_same_thread (sysex_connection, boost::bind (&SurfacePort::handle_midi_sysex, this, _1, _2, _3));
- _active = true;
+ return 0;
}
-void SurfacePort::close()
+void
+SurfacePort::reconnect ()
{
- DEBUG_TRACE (DEBUG::MackieControl, "SurfacePort::close\n");
- sysex_connection.disconnect();
-
- if (_surface) {
- // faders to minimum
- _surface->write_sysex (0x61);
- // All LEDs off
- _surface->write_sysex (0x62);
- // Reset (reboot into offline mode)
- _surface->write_sysex (0x63);
- }
+ _async_out->reconnect ();
+ _async_in->reconnect ();
+}
- _active = false;
+std::string
+SurfacePort::input_name () const
+{
+ return _async_in->name();
}
-void
-SurfacePort::handle_midi_sysex (MIDI::Parser &, MIDI::byte * raw_bytes, size_t count)
+std::string
+SurfacePort::output_name () const
{
- MidiByteArray bytes (count, raw_bytes);
-
- DEBUG_TRACE (DEBUG::MackieControl, string_compose ("handle_midi_sysex: %1\n", bytes));
-
- switch (bytes[5])
- {
- case 0x01:
- _surface->write_sysex (host_connection_query (bytes));
- break;
- case 0x03:
- // not used right now
- _surface->write_sysex (host_connection_confirmation (bytes));
- break;
- case 0x04:
- inactive_event ();
- cout << "host connection error" << bytes << endl;
- break;
- case 0x14:
- // probe_emulation (bytes);
- break;
- default:
- cout << "unknown sysex: " << bytes << endl;
- }
+ return _async_out->name();
}
-MidiByteArray calculate_challenge_response (MidiByteArray::iterator begin, MidiByteArray::iterator end)
+// wrapper for one day when strerror_r is working properly
+string fetch_errmsg (int error_number)
{
- MidiByteArray l;
- back_insert_iterator<MidiByteArray> back (l);
- copy (begin, end, back);
-
- MidiByteArray retval;
-
- // this is how to calculate the response to the challenge.
- // from the Logic docs.
- retval << (0x7f & (l[0] + (l[1] ^ 0xa) - l[3]));
- retval << (0x7f & ( (l[2] >> l[3]) ^ (l[0] + l[3])));
- retval << (0x7f & ((l[3] - (l[2] << 2)) ^ (l[0] | l[1])));
- retval << (0x7f & (l[1] - l[2] + (0xf0 ^ (l[3] << 4))));
-
- return retval;
+ char * msg = strerror (error_number);
+ return msg;
}
-// not used right now
-MidiByteArray SurfacePort::host_connection_query (MidiByteArray & bytes)
+int
+SurfacePort::write (const MidiByteArray & mba)
{
- MidiByteArray response;
-
- // handle host connection query
- DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host connection query: %1\n", bytes));
-
- if (bytes.size() != 18) {
- cerr << "expecting 18 bytes, read " << bytes << " from " << input_port().name() << endl;
- return response;
+ if (mba.empty()) {
+ DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name()));
+ return 0;
}
- // build and send host connection reply
- response << 0x02;
- copy (bytes.begin() + 6, bytes.begin() + 6 + 7, back_inserter (response));
- response << calculate_challenge_response (bytes.begin() + 6 + 7, bytes.begin() + 6 + 7 + 4);
- return response;
-}
+ DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
-// not used right now
-MidiByteArray SurfacePort::host_connection_confirmation (const MidiByteArray & bytes)
-{
- DEBUG_TRACE (DEBUG::MackieControl, string_compose ("host_connection_confirmation: %1\n", bytes));
-
- // decode host connection confirmation
- if (bytes.size() != 14) {
- ostringstream os;
- os << "expecting 14 bytes, read " << bytes << " from " << input_port().name();
- throw MackieControlException (os.str());
+ if (mba[0] != 0xf0 && mba.size() > 3) {
+ std::cerr << "TOO LONG WRITE: " << mba << std::endl;
}
-
- // send version request
- return MidiByteArray (2, 0x13, 0x00);
-}
+ /* this call relies on std::vector<T> using contiguous storage. not
+ * actually guaranteed by the standard, but way, way beyond likely.
+ */
+
+ int count = output_port().write (&mba[0], mba.size(), 0);
+
+ if (count != (int) mba.size()) {
+
+ if (errno == 0) {
-ostream & Mackie::operator << (ostream & os, const SurfacePort & port)
+ cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
+
+ } else if (errno != EAGAIN) {
+ ostringstream os;
+ os << "Surface: couldn't write to port " << output_port().name();
+ os << ", error: " << fetch_errmsg (errno) << "(" << errno << ")";
+ cout << os.str() << endl;
+ }
+
+ return -1;
+ }
+
+ return 0;
+}
+
+ostream &
+Mackie::operator << (ostream & os, const SurfacePort & port)
{
os << "{ ";
os << "name: " << port.input_port().name() << " " << port.output_port().name();
os << " }";
return os;
}
-