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( MIDI::Port & port, int number )
39 : _port( port ), _number( number ), _active( false )
43 SurfacePort::~SurfacePort()
45 //cout << "~SurfacePort::SurfacePort()" << endl;
46 // make sure another thread isn't reading or writing as we close the port
47 Glib::RecMutex::Lock lock( _rwlock );
49 //cout << "~SurfacePort::SurfacePort() finished" << endl;
52 // wrapper for one day when strerror_r is working properly
53 string fetch_errmsg( int error_number )
55 char * msg = strerror( error_number );
59 MidiByteArray SurfacePort::read()
61 const int max_buf_size = 512;
62 MIDI::byte buf[max_buf_size];
65 // check active. Mainly so that the destructor
66 // doesn't destroy the mutex while it's still locked
67 if ( !active() ) return retval;
69 // return nothing read if the lock isn't acquired
71 Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
75 cout << "SurfacePort::read not locked" << endl;
79 // check active again - destructor sequence
80 if ( !active() ) return retval;
83 // read port and copy to return value
84 int nread = port().read( buf, sizeof (buf) );
87 retval.copy( nread, buf );
88 if ((size_t) nread == sizeof (buf))
91 cout << "SurfacePort::read recursive" << endl;
98 if ( errno != EAGAIN )
101 os << "Surface: error reading from port: " << port().name();
102 os << ": " << errno << fetch_errmsg( errno );
104 cout << os.str() << endl;
106 throw MackieControlException( os.str() );
110 cout << "SurfacePort::read: " << retval << endl;
115 void SurfacePort::write( const MidiByteArray & mba )
118 //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
119 cout << "SurfacePort::write: " << mba << endl;
122 // check active before and after lock - to make sure
123 // that the destructor doesn't destroy the mutex while
125 if ( !active() ) return;
126 Glib::RecMutex::Lock lock( _rwlock );
127 if ( !active() ) return;
129 int count = port().write( mba.bytes().get(), mba.size() );
130 if ( count != (int)mba.size() )
132 if ( errno != EAGAIN )
135 os << "Surface: couldn't write to port " << port().name();
136 os << ": " << errno << fetch_errmsg( errno );
140 throw MackieControlException( os.str() );
144 cout << "SurfacePort::wrote " << count << endl;
148 void SurfacePort::write_sysex( const MidiByteArray & mba )
151 buf << sysex_hdr() << mba << MIDI::eox;
155 void SurfacePort::write_sysex( MIDI::byte msg )
158 buf << sysex_hdr() << msg << MIDI::eox;
162 // This should be moved to midi++ at some point
163 ostream & operator << ( ostream & os, const MIDI::Port & port )
165 os << "device: " << port.device();
167 os << "name: " << port.name();
169 os << "type: " << port.type();
171 os << "mode: " << port.mode();
173 os << "ok: " << port.ok();
175 os << "number: " << port.number();
180 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
183 os << "device: " << port.port().device();
185 os << "name: " << port.port().name();
187 os << "number: " << port.number();