MCP: build fix
[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 "midi++/manager.h"
26 #include <sigc++/sigc++.h>
27 #include <boost/shared_array.hpp>
28
29 #include "i18n.h"
30
31 #include <sstream>
32
33 #include <cstring>
34 #include <cerrno>
35
36 using namespace std;
37 using namespace Mackie;
38
39 SurfacePort::SurfacePort()
40         : _input_port (0), _output_port (0), _number (0), _active (false)
41 {
42 }
43
44 /** @param input_port Input MIDI::Port; this object takes responsibility for removing it from
45  *  the MIDI::Manager and destroying it.
46  *  @param output_port Output MIDI::Port; responsibility similarly taken.
47  */
48 SurfacePort::SurfacePort (MIDI::Port & input_port, MIDI::Port & output_port, int number)
49         : _input_port (&input_port), _output_port (&output_port), _number (number), _active (false)
50 {
51 }
52
53 SurfacePort::~SurfacePort()
54 {
55 #ifdef PORT_DEBUG
56         cout << "~SurfacePort::SurfacePort()" << endl;
57 #endif
58         // make sure another thread isn't reading or writing as we close the port
59         Glib::RecMutex::Lock lock( _rwlock );
60         _active = false;
61
62         MIDI::Manager* mm = MIDI::Manager::instance ();
63         
64         if (_input_port) {
65                 mm->remove_port (_input_port);
66                 delete _input_port;
67         }
68
69         if (_output_port) {
70                 mm->remove_port (_output_port);
71                 delete _output_port;
72         }
73         
74 #ifdef PORT_DEBUG
75         cout << "~SurfacePort::SurfacePort() finished" << endl;
76 #endif
77 }
78
79 // wrapper for one day when strerror_r is working properly
80 string fetch_errmsg( int error_number )
81 {
82         char * msg = strerror( error_number );
83         return msg;
84 }
85         
86 MidiByteArray SurfacePort::read()
87 {
88         const int max_buf_size = 512;
89         MIDI::byte buf[max_buf_size];
90         MidiByteArray retval;
91
92         // check active. Mainly so that the destructor
93         // doesn't destroy the mutex while it's still locked
94         if ( !active() ) return retval;
95         
96         // return nothing read if the lock isn't acquired
97 #if 0
98         Glib::RecMutex::Lock lock( _rwlock, Glib::TRY_LOCK );
99                 
100         if ( !lock.locked() )
101         {
102                 cout << "SurfacePort::read not locked" << endl;
103                 return retval;
104         }
105         
106         // check active again - destructor sequence
107         if ( !active() ) return retval;
108 #endif
109         
110         // read port and copy to return value
111         int nread = input_port().read( buf, sizeof (buf) );
112
113         if (nread >= 0) {
114                 retval.copy( nread, buf );
115                 if ((size_t) nread == sizeof (buf))
116                 {
117 #ifdef PORT_DEBUG
118                         cout << "SurfacePort::read recursive" << endl;
119 #endif
120                         retval << read();
121                 }
122         }
123         else
124         {
125                 if ( errno != EAGAIN )
126                 {
127                         ostringstream os;
128                         os << "Surface: error reading from port: " << input_port().name();
129                         os << ": " << errno << fetch_errmsg( errno );
130
131                         cout << os.str() << endl;
132                         inactive_event();
133                         throw MackieControlException( os.str() );
134                 }
135         }
136 #ifdef PORT_DEBUG
137         cout << "SurfacePort::read: " << retval << endl;
138 #endif
139         return retval;
140 }
141
142 void SurfacePort::write( const MidiByteArray & mba )
143 {
144 #ifdef PORT_DEBUG
145         cout << "SurfacePort::write: " << mba << " to " << output_port().name() << endl;
146 #endif
147         
148         // check active before and after lock - to make sure
149         // that the destructor doesn't destroy the mutex while
150         // it's still in use
151         if ( !active() ) return;
152         Glib::RecMutex::Lock lock( _rwlock );
153         if ( !active() ) return;
154
155         int count = output_port().write( mba.bytes().get(), mba.size(), 0);
156         if ( count != (int)mba.size() )
157         {
158                 if ( errno == 0 )
159                 {
160                         cout << "port overflow on " << output_port().name() << ". Did not write all of " << mba << endl;
161                 }
162                 else if ( errno != EAGAIN )
163                 {
164                         ostringstream os;
165                         os << "Surface: couldn't write to port " << output_port().name();
166                         os << ", error: " << fetch_errmsg( errno ) << "(" << errno << ")";
167                         
168                         cout << os.str() << endl;
169                         inactive_event();
170                 }
171         }
172 #ifdef PORT_DEBUG
173         cout << "SurfacePort::wrote " << count << endl;
174 #endif
175 }
176
177 void SurfacePort::write_sysex( const MidiByteArray & mba )
178 {
179         MidiByteArray buf;
180         buf << sysex_hdr() << mba << MIDI::eox;
181         write( buf );
182 }
183
184 void SurfacePort::write_sysex( MIDI::byte msg )
185 {
186         MidiByteArray buf;
187         buf << sysex_hdr() << msg << MIDI::eox;
188         write( buf );
189 }
190
191 ostream & Mackie::operator << ( ostream & os, const SurfacePort & port )
192 {
193         os << "{ ";
194         os << "name: " << port.input_port().name() << " " << port.output_port().name();
195         os << "; ";
196         os << " }";
197         return os;
198 }
199
200 /** Handle timeouts to reset in_use for controls that can't
201  *  do this by themselves (e.g. pots, and faders without touch support).
202  *  @param in_use_control the control whose in_use flag to reset.
203  *  @param touch_control a touch control to emit an event for, or 0.
204  */
205 bool
206 SurfacePort::control_in_use_timeout (Control* in_use_control, Control* touch_control)
207 {
208         in_use_control->set_in_use (false);
209
210         if (touch_control) {
211                 // empty control_state
212                 ControlState control_state;
213                 control_event (*this, *touch_control, control_state);
214         }
215         
216         // only call this method once from the timer
217         return false;
218 }
219
220 /** Add a timeout so that a control's in_use flag will be reset some time in the future.
221  *  @param in_use_control the control whose in_use flag to reset.
222  *  @param touch_control a touch control to emit an event for, or 0.
223  */
224 void
225 SurfacePort::add_in_use_timeout (Control& in_use_control, Control* touch_control)
226 {
227         in_use_control.in_use_connection.disconnect ();
228
229         /* XXX should this use the GUI event loop (default) or the MIDI UI event loop? */
230         
231         /* timeout after 250ms */
232         in_use_control.in_use_connection = Glib::signal_timeout().connect (
233                 sigc::bind (sigc::mem_fun (*this, &SurfacePort::control_in_use_timeout), &in_use_control, touch_control),
234                 250
235                 );
236         
237         in_use_control.in_use_touch_control = touch_control;
238 }