remove PBD::Connection (replace use with PBD::ScopedConnection); remove limitation...
[ardour.git] / libs / surfaces / mackie / surface_port.cc
index 8f55754f49aa633fa587f54d121a27c2470e4d5a..8aaea1d5ba9ac1a7db3d5ffac04b70694cdf5fda 100644 (file)
 
 #include "i18n.h"
 
-#define _XOPEN_SOURCE 600  // force XSI for non-GNU strerror_r()
-
 #include <sstream>
+
 #include <cstring>
 #include <cerrno>
 
-
 using namespace std;
 using namespace Mackie;
 
+SurfacePort::SurfacePort()
+: _port( 0 ), _number( 0 ), _active( false )
+{
+}
+
 SurfacePort::SurfacePort( MIDI::Port & port, int number )
-: _port( port ), _number( number ), _active( false )
+: _port( &port ), _number( number ), _active( false )
 {
 }
 
 SurfacePort::~SurfacePort()
 {
-       //cout << "~SurfacePort::SurfacePort()" << endl;
+#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;
-       //cout << "~SurfacePort::SurfacePort() finished" << endl;
+#ifdef PORT_DEBUG
+       cout << "~SurfacePort::SurfacePort() finished" << endl;
+#endif
 }
 
+// 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()
 {
        const int max_buf_size = 512;
@@ -62,16 +76,18 @@ MidiByteArray SurfacePort::read()
        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;
+               cout << "SurfacePort::read not locked" << endl;
                return retval;
        }
        
        // check active again - destructor sequence
        if ( !active() ) return retval;
+#endif
        
        // read port and copy to return value
        int nread = port().read( buf, sizeof (buf) );
@@ -80,6 +96,9 @@ MidiByteArray SurfacePort::read()
                retval.copy( nread, buf );
                if ((size_t) nread == sizeof (buf))
                {
+#ifdef PORT_DEBUG
+                       cout << "SurfacePort::read recursive" << endl;
+#endif
                        retval << read();
                }
        }
@@ -88,26 +107,25 @@ MidiByteArray SurfacePort::read()
                if ( errno != EAGAIN )
                {
                        ostringstream os;
-                       os << "Surface: error reading from port: " << port().name() << ": " << errno;
+                       os << "Surface: error reading from port: " << port().name();
+                       os << ": " << errno << fetch_errmsg( errno );
 
-                       char buf[512];
-                       int result = strerror_r( errno, buf, 512 );
-                       if (!result) {
-                               os << " " << buf;
-                       } 
-                       
                        cout << os.str() << endl;
                        inactive_event();
                        throw MackieControlException( os.str() );
                }
        }
+#ifdef PORT_DEBUG
+       cout << "SurfacePort::read: " << retval << endl;
+#endif
        return retval;
 }
 
 void SurfacePort::write( const MidiByteArray & mba )
 {
-       //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
-       //cout << "SurfacePort::write: " << mba << endl;
+#ifdef PORT_DEBUG
+       cout << "SurfacePort::write: " << mba << endl;
+#endif
        
        // check active before and after lock - to make sure
        // that the destructor doesn't destroy the mutex while
@@ -116,26 +134,26 @@ void SurfacePort::write( const MidiByteArray & mba )
        Glib::RecMutex::Lock lock( _rwlock );
        if ( !active() ) return;
 
-       int count = port().write( mba.bytes().get(), mba.size() );
+       int count = port().write( mba.bytes().get(), mba.size(), 0);
        if ( count != (int)mba.size() )
        {
-               if ( errno != EAGAIN )
+               if ( errno == 0 )
+               {
+                       cout << "port overflow on " << port().name() << ". Did not write all of " << mba << endl;
+               }
+               else if ( errno != EAGAIN )
                {
                        ostringstream os;
-                       os << "Surface: couldn't write to port " << port().name() << ": " << errno;
-                       char buf[512];
-                       int result = strerror_r( errno, buf, 512 );
-
-                       if (!result) {
-                               os << " " << buf;
-                       }
-
-                       cout << os.str();
+                       os << "Surface: couldn't write to port " << port().name();
+                       os << ", error: " << fetch_errmsg( errno ) << "(" << errno << ")";
+                       
+                       cout << os.str() << endl;
                        inactive_event();
-                       throw MackieControlException( os.str() );
                }
        }
-       //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
+#ifdef PORT_DEBUG
+       cout << "SurfacePort::wrote " << count << endl;
+#endif
 }
 
 void SurfacePort::write_sysex( const MidiByteArray & mba )
@@ -152,24 +170,6 @@ void SurfacePort::write_sysex( MIDI::byte msg )
        write( buf );
 }
 
-// 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;
-}
-
 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
 {
        os << "{ ";
@@ -177,7 +177,6 @@ ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
        os << "; ";
        os << "name: " << port.port().name();
        os << "; ";
-       os << "number: " << port.number();
        os << " }";
        return os;
 }