Merged with trunk R1612.
[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 #include <cstring>
33 #include <cerrno>
34
35 using namespace std;
36 using namespace Mackie;
37
38 SurfacePort::SurfacePort( MIDI::Port & port, int number )
39 : _port( port ), _number( number ), _active( false )
40 {
41 }
42
43 SurfacePort::~SurfacePort()
44 {
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 );
48         _active = false;
49         //cout << "~SurfacePort::SurfacePort() finished" << endl;
50 }
51
52 // wrapper for one day when strerror_r is working properly
53 string fetch_errmsg( int error_number )
54 {
55         char * msg = strerror( error_number );
56         return msg;
57 }
58         
59 MidiByteArray SurfacePort::read()
60 {
61         const int max_buf_size = 512;
62         MIDI::byte buf[max_buf_size];
63         MidiByteArray retval;
64
65         // check active. Mainly so that the destructor
66         // doesn't destroy the mutex while it's still locked
67         if ( !active() ) return retval;
68         
69         // return nothing read if the lock isn't acquired
70         Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
71                 
72         if ( !lock.locked() )
73         {
74                 //cout << "SurfacePort::read not locked" << endl;
75                 return retval;
76         }
77         
78         // check active again - destructor sequence
79         if ( !active() ) return retval;
80         
81         // read port and copy to return value
82         int nread = port().read( buf, sizeof (buf), 0 );
83
84         if (nread >= 0) {
85                 retval.copy( nread, buf );
86                 if ((size_t) nread == sizeof (buf))
87                 {
88                         retval << read();
89                 }
90         }
91         else
92         {
93                 if ( errno != EAGAIN )
94                 {
95                         ostringstream os;
96                         os << "Surface: error reading from port: " << port().name();
97                         os << ": " << errno << fetch_errmsg( errno );
98
99                         cout << os.str() << endl;
100                         inactive_event();
101                         throw MackieControlException( os.str() );
102                 }
103         }
104         return retval;
105 }
106
107 void SurfacePort::write( const MidiByteArray & mba )
108 {
109         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write: " << mba << endl;
110         //cout << "SurfacePort::write: " << mba << endl;
111         
112         // check active before and after lock - to make sure
113         // that the destructor doesn't destroy the mutex while
114         // it's still in use
115         if ( !active() ) return;
116         Glib::RecMutex::Lock lock( _rwlock );
117         if ( !active() ) return;
118
119         int count = port().write( mba.bytes().get(), mba.size(), 0 );
120         if ( count != (int)mba.size() )
121         {
122                 if ( errno != EAGAIN )
123                 {
124                         ostringstream os;
125                         os << "Surface: couldn't write to port " << port().name();
126                         os << ": " << errno << fetch_errmsg( errno );
127                         
128                         cout << os.str();
129                         inactive_event();
130                         throw MackieControlException( os.str() );
131                 }
132         }
133         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
134 }
135
136 void SurfacePort::write_sysex( const MidiByteArray & mba )
137 {
138         MidiByteArray buf;
139         buf << sysex_hdr() << mba << MIDI::eox;
140         write( buf );
141 }
142
143 void SurfacePort::write_sysex( MIDI::byte msg )
144 {
145         MidiByteArray buf;
146         buf << sysex_hdr() << msg << MIDI::eox;
147         write( buf );
148 }
149
150 // This should be moved to midi++ at some point
151 ostream & operator << ( ostream & os, const MIDI::Port & port )
152 {
153         os << "device: " << port.device();
154         os << "; ";
155         os << "name: " << port.name();
156         os << "; ";
157         os << "type: " << port.type();
158         os << "; ";
159         os << "mode: " << port.mode();
160         os << "; ";
161         os << "ok: " << port.ok();
162         os << "; ";
163         os << "number: " << port.number();
164         os << "; ";
165         return os;
166 }
167
168 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
169 {
170         os << "{ ";
171         os << "device: " << port.port().device();
172         os << "; ";
173         os << "name: " << port.port().name();
174         os << "; ";
175         os << "number: " << port.number();
176         os << " }";
177         return os;
178 }