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