X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fsurfaces%2Fmackie%2Fsurface_port.cc;h=ddc5a7b037e0fbd0deebbb0f56b33e8578b45047;hb=cf52d6e4b40111eb04b244ec054055a4ec15dbe0;hp=3e10fa7b36b108033eacb5058b22ebb081582a54;hpb=91d512174efb610b004bd4542e05f99080a668d5;p=ardour.git diff --git a/libs/surfaces/mackie/surface_port.cc b/libs/surfaces/mackie/surface_port.cc index 3e10fa7b36..ddc5a7b037 100644 --- a/libs/surfaces/mackie/surface_port.cc +++ b/libs/surfaces/mackie/surface_port.cc @@ -15,206 +15,219 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "surface_port.h" -#include "mackie_control_exception.h" -#include "controls.h" +#include +#include +#include -#include -#include #include #include -#include "i18n.h" +#include "pbd/failed_constructor.h" -#include +#include "midi++/types.h" +#include "midi++/ipmidi_port.h" -#include -#include +#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_protocol.h" +#include "surface.h" +#include "surface_port.h" + +#include "pbd/i18n.h" using namespace std; +using namespace PBD; +using namespace ARDOUR; +using namespace ArdourSurface; using namespace Mackie; -SurfacePort::SurfacePort() - : _input_port (0), _output_port (0), _number (0), _active (false) +SurfacePort::SurfacePort (Surface& s) + : _surface (&s) { -} + 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"); + } -SurfacePort::SurfacePort (MIDI::Port & input_port, MIDI::Port & output_port, int number) - : _input_port (&input_port), _output_port (&output_port), _number (number), _active (false) -{ + _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(_async_in).get(); + _output_port = boost::dynamic_pointer_cast(_async_out).get(); + } } SurfacePort::~SurfacePort() { -#ifdef PORT_DEBUG - cout << "~SurfacePort::SurfacePort()" << endl; -#endif - // make sure another thread isn't reading or writing as we close the port - Glib::RecMutex::Lock lock( _rwlock ); - _active = false; -#ifdef PORT_DEBUG - cout << "~SurfacePort::SurfacePort() finished" << endl; -#endif + if (dynamic_cast(_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 (_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 ) +XMLNode& +SurfacePort::get_state () { - char * msg = strerror( error_number ); - return msg; + XMLNode* node = new XMLNode (X_("Port")); + + if (dynamic_cast(_input_port)) { + /* no state required for IPMidi ports */ + return *node; + } + + 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; } - -MidiByteArray SurfacePort::read() + +int +SurfacePort::set_state (const XMLNode& node, int version) { - 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 -#if 0 - Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK ); - - if ( !lock.locked() ) - { - cout << "SurfacePort::read not locked" << endl; - return retval; + if (dynamic_cast(_input_port)) { + return 0; } - - // check active again - destructor sequence - if ( !active() ) return retval; -#endif - - // 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(); + + 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); } } - else - { - if ( errno != EAGAIN ) - { - ostringstream os; - os << "Surface: error reading from port: " << input_port().name(); - os << ": " << errno << fetch_errmsg( errno ); - cout << os.str() << endl; - inactive_event(); - throw MackieControlException( os.str() ); + 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); } } -#ifdef PORT_DEBUG - cout << "SurfacePort::read: " << retval << endl; -#endif - return retval; + + return 0; } -void SurfacePort::write( const MidiByteArray & mba ) +void +SurfacePort::reconnect () { -#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; - Glib::RecMutex::Lock lock( _rwlock ); - 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(); - } - } -#ifdef PORT_DEBUG - cout << "SurfacePort::wrote " << count << endl; -#endif + _async_out->reconnect (); + _async_in->reconnect (); } -void SurfacePort::write_sysex( const MidiByteArray & mba ) +std::string +SurfacePort::input_name () const { - MidiByteArray buf; - buf << sysex_hdr() << mba << MIDI::eox; - write( buf ); + return _async_in->name(); } -void SurfacePort::write_sysex( MIDI::byte msg ) +std::string +SurfacePort::output_name () const { - MidiByteArray buf; - buf << sysex_hdr() << msg << MIDI::eox; - write( buf ); + return _async_out->name(); } -ostream & Mackie::operator << ( ostream & os, const SurfacePort & port ) +// wrapper for one day when strerror_r is working properly +string fetch_errmsg (int error_number) { - os << "{ "; - os << "name: " << port.input_port().name() << " " << port.output_port().name(); - os << "; "; - os << " }"; - return os; + char * msg = strerror (error_number); + return msg; } -/** Handle timeouts to reset in_use for controls that can't - * do this by themselves (e.g. pots, and faders without touch support). - * @param in_use_control the control whose in_use flag to reset. - * @param touch_control a touch control to emit an event for, or 0. - */ -bool -SurfacePort::control_in_use_timeout (Control* in_use_control, Control* touch_control) +int +SurfacePort::write (const MidiByteArray & mba) { - in_use_control->set_in_use (false); + if (mba.empty()) { + DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 asked to write an empty MBA\n", output_port().name())); + return 0; + } + + DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba)); - if (touch_control) { - // empty control_state - ControlState control_state; - control_event (*this, *touch_control, control_state); + if (mba[0] != 0xf0 && mba.size() > 3) { + std::cerr << "TOO LONG WRITE: " << mba << std::endl; } - - // only call this method once from the timer - return false; + + /* this call relies on std::vector 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) { + + 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; } -/** Add a timeout so that a control's in_use flag will be reset some time in the future. - * @param in_use_control the control whose in_use flag to reset. - * @param touch_control a touch control to emit an event for, or 0. - */ -void -SurfacePort::add_in_use_timeout (Control& in_use_control, Control* touch_control) +ostream & +Mackie::operator << (ostream & os, const SurfacePort & port) { - in_use_control.in_use_connection.disconnect (); - - /* XXX should this use the GUI event loop (default) or the MIDI UI event loop? */ - - /* timeout after 250ms */ - in_use_control.in_use_connection = Glib::signal_timeout().connect ( - sigc::bind (sigc::mem_fun (*this, &SurfacePort::control_in_use_timeout), &in_use_control, touch_control), - 250 - ); - - in_use_control.in_use_touch_control = touch_control; + os << "{ "; + os << "name: " << port.input_port().name() << " " << port.output_port().name(); + os << "; "; + os << " }"; + return os; }