add locks around port read/write. Much more stable on startup now.
[ardour.git] / libs / surfaces / mackie / surface_port.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3
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.
8
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.
13
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.
17 */
18 #include "surface_port.h"
19
20 #include "mackie_control_exception.h"
21 #include "controls.h"
22
23 #include <midi++/types.h>
24 #include <midi++/port.h>
25 #include <sigc++/sigc++.h>
26 #include <boost/shared_array.hpp>
27
28 #include "i18n.h"
29
30 #include <sstream>
31
32 using namespace std;
33 using namespace Mackie;
34
35 SurfacePort::SurfacePort( MIDI::Port & port, int number )
36 : _port( port ), _number( number ), _active( false )
37 {
38 }
39
40 MidiByteArray SurfacePort::read()
41 {
42         const int max_buf_size = 512;
43         MIDI::byte buf[max_buf_size];
44         MidiByteArray retval;
45
46         // return nothing read if the lock isn't acquired
47         Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
48         if ( !lock.locked() ) return retval;
49         
50         // read port and copy to return value
51         int nread = port().read( buf, sizeof (buf) );
52
53         if (nread >= 0) {
54                 retval.copy( nread, buf );
55                 if ((size_t) nread == sizeof (buf))
56                 {
57                         retval << read();
58                 }
59         }
60         else
61         {
62                 ostringstream os;
63                 os << "error reading from port: " << port().name() << " nread: " << nread;
64                 cout << os.str() << endl;
65                 inactive_event();
66                 throw MackieControlException( os.str() );
67         }
68         return retval;
69 }
70
71 void SurfacePort::write( const MidiByteArray & mba )
72 {
73         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
74         //cout << "SurfacePort::write: " << mba << endl;
75         Glib::RecMutex::Lock lock( _rwlock );
76         int count = port().write( mba.bytes().get(), mba.size() );
77         if ( count != (int)mba.size() )
78         {
79                 inactive_event();
80                 ostringstream os;
81                 os << _("Surface: couldn't write to port ") << port().name();
82                 throw MackieControlException( os.str() );
83         }
84         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
85 }
86
87 void SurfacePort::write_sysex( const MidiByteArray & mba )
88 {
89         MidiByteArray buf;
90         buf << sysex_hdr() << mba << MIDI::eox;
91         write( buf );
92 }
93
94 void SurfacePort::write_sysex( MIDI::byte msg )
95 {
96         MidiByteArray buf;
97         buf << sysex_hdr() << msg << MIDI::eox;
98         write( buf );
99 }
100
101 // This should be moved to midi++ at some point
102 ostream & operator << ( ostream & os, const MIDI::Port & port )
103 {
104         os << "device: " << port.device();
105         os << "; ";
106         os << "name: " << port.name();
107         os << "; ";
108         os << "type: " << port.type();
109         os << "; ";
110         os << "mode: " << port.mode();
111         os << "; ";
112         os << "ok: " << port.ok();
113         os << "; ";
114         os << "number: " << port.number();
115         os << "; ";
116         return os;
117 }
118
119 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
120 {
121         os << "{ ";
122         os << "device: " << port.port().device();
123         os << "; ";
124         os << "name: " << port.port().name();
125         os << "; ";
126         os << "number: " << port.number();
127         os << " }";
128         return os;
129 }