Call PBD::EnumWriter::destroy in ARDOUR::cleanup (from
[ardour.git] / libs / surfaces / mackie / surface_port.cc
index a6f10f92433bd2b007e37bda5310915157c43cad..9d0296a9a4d5355eadf463fb16ddb1c6970f6593 100644 (file)
        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 <sstream>
+#include <cstring>
+#include <cerrno>
 
-#include <midi++/types.h>
-#include <midi++/port.h>
 #include <sigc++/sigc++.h>
 #include <boost/shared_array.hpp>
 
-#include "i18n.h"
+#include "midi++/types.h"
+#include "midi++/port.h"
+#include "midi++/jack_midi_port.h"
+#include "midi++/ipmidi_port.h"
+#include "midi++/manager.h"
 
-#include <sstream>
+#include "ardour/debug.h"
+#include "ardour/rc_configuration.h"
+#include "ardour/session.h"
+#include "ardour/audioengine.h"
+
+#include "controls.h"
+#include "mackie_control_protocol.h"
+#include "surface.h"
+#include "surface_port.h"
+
+
+#include "i18n.h"
 
 using namespace std;
 using namespace Mackie;
-
-SurfacePort::SurfacePort( MIDI::Port & port, int number )
-: _port( port ), _number( number ), _active( false )
+using namespace PBD;
+
+/** @param input_port Input MIDI::Port; this object takes responsibility for
+ *  adding & removing it from the MIDI::Manager and destroying it.  @param
+ *  output_port Output MIDI::Port; responsibility similarly taken.
+ */
+SurfacePort::SurfacePort (Surface& s)
+       : _surface (&s)
+       , _input_port (0)
+       , _output_port (0)
 {
+       if (_surface->mcp().device_info().uses_ipmidi()) {
+               _input_port = new MIDI::IPMIDIPort (_surface->mcp().ipmidi_base() +_surface->number());
+               _output_port = _input_port;
+       } else {
+               jack_client_t* jack = MackieControlProtocol::instance()->get_session().engine().jack();
+               
+               _input_port = new MIDI::JackMIDIPort (string_compose (_("%1 in"),  _surface->name()), MIDI::Port::IsInput, jack);
+               _output_port =new MIDI::JackMIDIPort (string_compose (_("%1 out"), _surface->name()), MIDI::Port::IsOutput, jack);
+               
+               /* MackieControl has its own thread for handling input from the input
+                * port, and we don't want anything handling output from the output
+                * port. This stops the Generic MIDI UI event loop in ardour from
+                * attempting to handle these ports.
+                */
+               
+               _input_port->set_centrally_parsed (false);
+               _output_port->set_centrally_parsed (false);
+               
+               MIDI::Manager * mm = MIDI::Manager::instance();
+               
+               mm->add_port (_input_port);
+               mm->add_port (_output_port);
+       }
 }
 
-MidiByteArray SurfacePort::read()
+SurfacePort::~SurfacePort()
 {
-       const int max_buf_size = 512;
-       MIDI::byte buf[max_buf_size];
-       MidiByteArray retval;
-
-       int nread = port().read( buf, sizeof (buf) );
-
-       if (nread >= 0) {
-               retval.copy( nread, buf );
-               if ((size_t) nread == sizeof (buf))
-               {
-                       retval << read();
+       if (_surface->mcp().device_info().uses_ipmidi()) {
+               delete _input_port;
+       } else {
+
+               MIDI::Manager* mm = MIDI::Manager::instance ();
+               
+               if (_input_port) {
+                       mm->remove_port (_input_port);
+                       delete _input_port;
+               }
+               
+               if (_output_port) {
+                       _output_port->drain (10000);
+                       mm->remove_port (_output_port);
+                       delete _output_port;
                }
        }
-       else
-       {
-               ostringstream os;
-               os << "error reading from port: " << port().name() << " nread: " << nread;
-               cout << os.str() << endl;
-               inactive_event();
-               throw MackieControlException( os.str() );
-       }
-       return retval;
 }
 
-void SurfacePort::write( const MidiByteArray & mba )
+// wrapper for one day when strerror_r is working properly
+string fetch_errmsg (int error_number)
 {
-       if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
-       //cout << "SurfacePort::write: " << mba << endl;
-       int count = port().write( mba.bytes().get(), mba.size() );
-       if ( count != (int)mba.size() )
-       {
-               inactive_event();
-               ostringstream os;
-               os << _("Surface: couldn't write to port ") << port().name();
-               throw MackieControlException( os.str() );
-       }
-       //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
+       char * msg = strerror (error_number);
+       return msg;
 }
-
-void SurfacePort::write_sysex( const MidiByteArray & mba )
+       
+int 
+SurfacePort::write (const MidiByteArray & mba)
 {
-       MidiByteArray buf;
-       buf << sysex_hdr() << mba << MIDI::eox;
-       write( buf );
-}
+       if (mba.empty()) {
+               return 0;
+       }
 
-void SurfacePort::write_sysex( MIDI::byte msg )
-{
-       MidiByteArray buf;
-       buf << sysex_hdr() << msg << MIDI::eox;
-       write( buf );
-}
+       DEBUG_TRACE (DEBUG::MackieControl, string_compose ("port %1 write %2\n", output_port().name(), mba));
 
-// This should be moved to midi++ at some point
-ostream & operator << ( ostream & os, const MIDI::Port & port )
-{
-       os << "device: " << port.device();
-       os << "; ";
-       os << "name: " << port.name();
-       os << "; ";
-       os << "type: " << port.type();
-       os << "; ";
-       os << "mode: " << port.mode();
-       os << "; ";
-       os << "ok: " << port.ok();
-       os << "; ";
-       os << "number: " << port.number();
-       os << "; ";
-       return os;
+       if (mba[0] != 0xf0 && mba.size() > 3) {
+               std::cerr << "TOO LONG WRITE: " << mba << std::endl;
+       }
+               
+       /* 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) {
+
+                       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 )
+ostream & 
+Mackie::operator <<  (ostream & os, const SurfacePort & port)
 {
        os << "{ ";
-       os << "device: " << port.port().device();
+       os << "name: " << port.input_port().name() << " " << port.output_port().name();
        os << "; ";
-       os << "name: " << port.port().name();
-       os << "; ";
-       os << "number: " << port.number();
        os << " }";
        return os;
 }
+