Merge branch 'windows+cc' into cairocanvas
[ardour.git] / libs / surfaces / mackie / surface_port.cc
index faf47818405668c37db3ea2f8ab019880478dfaa..013a0019c7e797ef9982df56412a07cd8a7fbc94 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 "pbd/failed_constructor.h"
 
-#include <sstream>
+#include "midi++/types.h"
+#include "midi++/ipmidi_port.h"
 
-#include <cstring>
-#include <cerrno>
+#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 "i18n.h"
 
 using namespace std;
 using namespace Mackie;
+using namespace PBD;
+using namespace ARDOUR;
 
-SurfacePort::SurfacePort( MIDI::Port & port, int number )
-: _port( port ), _number( number ), _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 {
+               
+               _async_in  = AudioEngine::instance()->register_input_port (DataType::MIDI, string_compose (_("%1 in"),  _surface->name()), true);
+               _async_out = AudioEngine::instance()->register_output_port (DataType::MIDI, string_compose (_("%1 out"),  _surface->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()
 {
-       //cout << "~SurfacePort::SurfacePort()" << endl;
-       // make sure another thread isn't reading or writing as we close the port
-       Glib::RecMutex::Lock lock( _rwlock );
-       _active = false;
-       //cout << "~SurfacePort::SurfacePort() finished" << endl;
-}
+       if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
+               delete _input_port;
+       } else {
 
-// wrapper for one day when strerror_r is working properly
-string fetch_errmsg( int error_number )
-{
-       char * msg = strerror( error_number );
-       return msg;
+               if (_async_in) {
+                       AudioEngine::instance()->unregister_port (_async_in);
+                       _async_in.reset ((ARDOUR::Port*) 0);
+               }
+               
+               if (_async_out) {
+                       _output_port->drain (10000);
+                       AudioEngine::instance()->unregister_port (_async_out);
+                       _async_out.reset ((ARDOUR::Port*) 0);
+               }
+       }
 }
-       
-MidiByteArray SurfacePort::read()
+
+XMLNode&
+SurfacePort::get_state ()
 {
-       const int max_buf_size = 512;
-       MIDI::byte buf[max_buf_size];
-       MidiByteArray retval;
+       XMLNode* node = new XMLNode (X_("Port"));
 
-       // 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
-       Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
-               
-       if ( !lock.locked() )
-       {
-               //cout << "SurfacePort::read not locked" << endl;
-               return retval;
+       if (dynamic_cast<MIDI::IPMIDIPort*>(_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);
        
-       // check active again - destructor sequence
-       if ( !active() ) return retval;
-       
-       // read port and copy to return value
-       int nread = port().read( buf, sizeof (buf), 0 );
-
-       if (nread >= 0) {
-               retval.copy( nread, buf );
-               if ((size_t) nread == sizeof (buf))
-               {
-                       retval << read();
-               }
+
+       child = new XMLNode (X_("Output"));
+       child->add_child_nocopy (_async_out->get_state());
+       node->add_child_nocopy (*child);
+
+       return *node;
+}
+
+int
+SurfacePort::set_state (const XMLNode& node, int version)
+{
+       if (dynamic_cast<MIDI::IPMIDIPort*>(_input_port)) {
+               return 0;
        }
-       else
-       {
-               if ( errno != EAGAIN )
-               {
-                       ostringstream os;
-                       os << "Surface: error reading from port: " << port().name();
-                       os << ": " << errno << fetch_errmsg( errno );
 
-                       cout << os.str() << endl;
-                       inactive_event();
-                       throw MackieControlException( os.str() );
+       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);
                }
        }
-       return retval;
-}
 
-void SurfacePort::write( const MidiByteArray & mba )
-{
-       //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
-       //cout << "SurfacePort::write: " << mba << endl;
-       
-       // 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 = port().write( mba.bytes().get(), mba.size(), 0 );
-       if ( count != (int)mba.size() )
-       {
-               if ( errno != EAGAIN )
-               {
-                       ostringstream os;
-                       os << "Surface: couldn't write to port " << port().name();
-                       os << ": " << errno << fetch_errmsg( errno );
-                       
-                       cout << os.str();
-                       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);
                }
        }
-       //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
-}
 
-void SurfacePort::write_sysex( const MidiByteArray & mba )
-{
-       MidiByteArray buf;
-       buf << sysex_hdr() << mba << MIDI::eox;
-       write( buf );
+       return 0;
 }
 
-void SurfacePort::write_sysex( MIDI::byte msg )
+// wrapper for one day when strerror_r is working properly
+string fetch_errmsg (int error_number)
 {
-       MidiByteArray buf;
-       buf << sysex_hdr() << msg << MIDI::eox;
-       write( buf );
+       char * msg = strerror (error_number);
+       return msg;
 }
-
-// This should be moved to midi++ at some point
-ostream & operator << ( ostream & os, const MIDI::Port & port )
+       
+int 
+SurfacePort::write (const MidiByteArray & mba)
 {
-       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 << "; ";
-       return os;
+       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 (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 << "; ";
-       os << "name: " << port.port().name();
+       os << "name: " << port.input_port().name() << " " << port.output_port().name();
        os << "; ";
        os << " }";
        return os;
 }
+