2 Copyright (C) 2006,2007 John Anderson
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include "surface_port.h"
20 #include "mackie_control_exception.h"
23 #include <midi++/types.h>
24 #include <midi++/port.h>
25 #include <sigc++/sigc++.h>
26 #include <boost/shared_array.hpp>
36 using namespace Mackie;
38 SurfacePort::SurfacePort()
39 : _port( 0 ), _number( 0 ), _active( false )
43 SurfacePort::SurfacePort( MIDI::Port & port, int number )
44 : _port( &port ), _number( number ), _active( false )
48 SurfacePort::~SurfacePort()
51 cout << "~SurfacePort::SurfacePort()" << endl;
53 // make sure another thread isn't reading or writing as we close the port
54 Glib::RecMutex::Lock lock( _rwlock );
57 cout << "~SurfacePort::SurfacePort() finished" << endl;
61 // wrapper for one day when strerror_r is working properly
62 string fetch_errmsg( int error_number )
64 char * msg = strerror( error_number );
68 MidiByteArray SurfacePort::read()
70 const int max_buf_size = 512;
71 MIDI::byte buf[max_buf_size];
74 // check active. Mainly so that the destructor
75 // doesn't destroy the mutex while it's still locked
76 if ( !active() ) return retval;
78 // return nothing read if the lock isn't acquired
80 Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
84 cout << "SurfacePort::read not locked" << endl;
88 // check active again - destructor sequence
89 if ( !active() ) return retval;
92 // read port and copy to return value
93 int nread = port().read( buf, sizeof (buf) );
96 retval.copy( nread, buf );
97 if ((size_t) nread == sizeof (buf))
100 cout << "SurfacePort::read recursive" << endl;
107 if ( errno != EAGAIN )
110 os << "Surface: error reading from port: " << port().name();
111 os << ": " << errno << fetch_errmsg( errno );
113 cout << os.str() << endl;
115 throw MackieControlException( os.str() );
119 cout << "SurfacePort::read: " << retval << endl;
124 void SurfacePort::write( const MidiByteArray & mba )
127 cout << "SurfacePort::write: " << mba << endl;
130 // check active before and after lock - to make sure
131 // that the destructor doesn't destroy the mutex while
133 if ( !active() ) return;
134 Glib::RecMutex::Lock lock( _rwlock );
135 if ( !active() ) return;
137 int count = port().write( mba.bytes().get(), mba.size(), 0);
138 if ( count != (int)mba.size() )
142 cout << "port overflow on " << port().name() << ". Did not write all of " << mba << endl;
144 else if ( errno != EAGAIN )
147 os << "Surface: couldn't write to port " << port().name();
148 os << ", error: " << fetch_errmsg( errno ) << "(" << errno << ")";
150 cout << os.str() << endl;
155 cout << "SurfacePort::wrote " << count << endl;
159 void SurfacePort::write_sysex( const MidiByteArray & mba )
162 buf << sysex_hdr() << mba << MIDI::eox;
166 void SurfacePort::write_sysex( MIDI::byte msg )
169 buf << sysex_hdr() << msg << MIDI::eox;
173 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
176 os << "device: " << port.port().device();
178 os << "name: " << port.port().name();