Fixed error handling to use XSI strerror_r(), not the GNU version.
[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 #define _XOPEN_SOURCE 600  // force XSI for non-GNU strerror_r()
31
32 #include <sstream>
33 #include <cstring>
34 #include <cerrno>
35
36
37 using namespace std;
38 using namespace Mackie;
39
40 SurfacePort::SurfacePort( MIDI::Port & port, int number )
41 : _port( port ), _number( number ), _active( false )
42 {
43 }
44
45 SurfacePort::~SurfacePort()
46 {
47         //cout << "~SurfacePort::SurfacePort()" << endl;
48         // make sure another thread isn't reading or writing as we close the port
49         Glib::RecMutex::Lock lock( _rwlock );
50         _active = false;
51         //cout << "~SurfacePort::SurfacePort() finished" << endl;
52 }
53
54 MidiByteArray SurfacePort::read()
55 {
56         const int max_buf_size = 512;
57         MIDI::byte buf[max_buf_size];
58         MidiByteArray retval;
59
60         // check active. Mainly so that the destructor
61         // doesn't destroy the mutex while it's still locked
62         if ( !active() ) return retval;
63         
64         // return nothing read if the lock isn't acquired
65         Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
66                 
67         if ( !lock.locked() )
68         {
69                 //cout << "SurfacePort::read not locked" << endl;
70                 return retval;
71         }
72         
73         // check active again - destructor sequence
74         if ( !active() ) return retval;
75         
76         // read port and copy to return value
77         int nread = port().read( buf, sizeof (buf) );
78
79         if (nread >= 0) {
80                 retval.copy( nread, buf );
81                 if ((size_t) nread == sizeof (buf))
82                 {
83                         retval << read();
84                 }
85         }
86         else
87         {
88                 if ( errno != EAGAIN )
89                 {
90                         ostringstream os;
91                         os << "Surface: error reading from port: " << port().name() << ": " << errno;
92
93                         char buf[512];
94                         int result = strerror_r( errno, buf, 512 );
95                         if (!result) {
96                                 os << " " << buf;
97                         } 
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() );
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() << ": " << errno;
126                         char buf[512];
127                         int result = strerror_r( errno, buf, 512 );
128
129                         if (!result) {
130                                 os << " " << buf;
131                         }
132
133                         cout << os.str();
134                         inactive_event();
135                         throw MackieControlException( os.str() );
136                 }
137         }
138         //if ( mba[0] == 0xf0 ) cout << "SurfacePort::write " << count << endl;
139 }
140
141 void SurfacePort::write_sysex( const MidiByteArray & mba )
142 {
143         MidiByteArray buf;
144         buf << sysex_hdr() << mba << MIDI::eox;
145         write( buf );
146 }
147
148 void SurfacePort::write_sysex( MIDI::byte msg )
149 {
150         MidiByteArray buf;
151         buf << sysex_hdr() << msg << MIDI::eox;
152         write( buf );
153 }
154
155 // This should be moved to midi++ at some point
156 ostream & operator << ( ostream & os, const MIDI::Port & port )
157 {
158         os << "device: " << port.device();
159         os << "; ";
160         os << "name: " << port.name();
161         os << "; ";
162         os << "type: " << port.type();
163         os << "; ";
164         os << "mode: " << port.mode();
165         os << "; ";
166         os << "ok: " << port.ok();
167         os << "; ";
168         os << "number: " << port.number();
169         os << "; ";
170         return os;
171 }
172
173 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
174 {
175         os << "{ ";
176         os << "device: " << port.port().device();
177         os << "; ";
178         os << "name: " << port.port().name();
179         os << "; ";
180         os << "number: " << port.number();
181         os << " }";
182         return os;
183 }