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