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>
35 using namespace Mackie;
37 SurfacePort::SurfacePort( MIDI::Port & port, int number )
38 : _port( port ), _number( number ), _active( false )
42 SurfacePort::~SurfacePort()
44 //cout << "~SurfacePort::SurfacePort()" << endl;
45 // make sure another thread isn't reading or writing as we close the port
46 Glib::RecMutex::Lock lock( _rwlock );
48 //cout << "~SurfacePort::SurfacePort() finished" << endl;
51 MidiByteArray SurfacePort::read()
53 const int max_buf_size = 512;
54 MIDI::byte buf[max_buf_size];
57 // check active. Mainly so that the destructor
58 // doesn't destroy the mutex while it's still locked
59 if ( !active() ) return retval;
61 // return nothing read if the lock isn't acquired
62 Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
66 //cout << "SurfacePort::read not locked" << endl;
70 // check active again - destructor sequence
71 if ( !active() ) return retval;
73 // read port and copy to return value
74 int nread = port().read( buf, sizeof (buf) );
77 retval.copy( nread, buf );
78 if ((size_t) nread == sizeof (buf))
85 if ( errno != EAGAIN )
88 char * msg = strerror_r( errno, buf, 512 );
91 os << "Surface: error reading from port: " << port().name() << ": " << errno << " " << msg;
92 cout << os.str() << endl;
94 throw MackieControlException( os.str() );
100 void SurfacePort::write( const MidiByteArray & mba )
102 //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
103 //cout << "SurfacePort::write: " << mba << endl;
105 // check active before and after lock - to make sure
106 // that the destructor doesn't destroy the mutex while
108 if ( !active() ) return;
109 Glib::RecMutex::Lock lock( _rwlock );
110 if ( !active() ) return;
112 int count = port().write( mba.bytes().get(), mba.size() );
113 if ( count != (int)mba.size() )
115 if ( errno != EAGAIN )
118 char * msg = strerror_r( errno, buf, 512 );
121 os << "Surface: couldn't write to port " << port().name() << ": " << errno << " " << msg;
124 throw MackieControlException( os.str() );
127 //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
130 void SurfacePort::write_sysex( const MidiByteArray & mba )
133 buf << sysex_hdr() << mba << MIDI::eox;
137 void SurfacePort::write_sysex( MIDI::byte msg )
140 buf << sysex_hdr() << msg << MIDI::eox;
144 // This should be moved to midi++ at some point
145 ostream & operator << ( ostream & os, const MIDI::Port & port )
147 os << "device: " << port.device();
149 os << "name: " << port.name();
151 os << "type: " << port.type();
153 os << "mode: " << port.mode();
155 os << "ok: " << port.ok();
157 os << "number: " << port.number();
162 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
165 os << "device: " << port.port().device();
167 os << "name: " << port.port().name();
169 os << "number: " << port.number();