1 #include "mackie_control_protocol.h"
3 #include "midi_byte_array.h"
4 #include "surface_port.h"
6 #include <pbd/pthread_utils.h>
9 #include <midi++/types.h>
10 #include <midi++/port.h>
11 #include <midi++/manager.h>
24 using namespace Mackie;
27 const char * MackieControlProtocol::default_port_name = "mcu";
29 bool MackieControlProtocol::probe()
31 return MIDI::Manager::instance()->port( default_port_name ) != 0;
34 void * MackieControlProtocol::monitor_work()
36 // What does ThreadCreatedWithRequestSize do?
37 PBD::ThreadCreated (pthread_self(), X_("Mackie"));
39 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
40 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
42 // read from midi ports
50 catch ( exception & e ) {
51 cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
52 _ports_changed = true;
56 // poll for automation data from the routes
59 catch ( exception & e )
61 cout << "caught exception in MackieControlProtocol::monitor_work " << e.what() << endl;
65 // TODO ports and pfd and nfds should be in a separate class
73 void MackieControlProtocol::update_ports()
77 Glib::Mutex::Lock ul( update_mutex );
78 // yes, this is a double-test locking paradigm, or whatever it's called
79 // because we don't *always* need to acquire the lock for the first test
82 // create new pollfd structures
83 if ( pfd != 0 ) delete[] pfd;
84 // TODO This might be a memory leak. How does thread cancellation cleanup work?
85 pfd = new pollfd[_ports.size()];
88 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
90 //cout << "adding port " << (*it)->port().name() << " to pollfd" << endl;
91 pfd[nfds].fd = (*it)->port().selectable();
92 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
95 _ports_changed = false;
101 void MackieControlProtocol::read_ports()
103 /* now read any data on the ports */
104 Glib::Mutex::Lock lock( update_mutex );
105 for ( int p = 0; p < nfds; ++p )
107 // this will cause handle_midi_any in the MackiePort to be triggered
108 if ( (pfd[p].revents & POLLIN) > 0 )
110 // avoid deadlocking?
111 // doesn't seem to make a difference
119 bool MackieControlProtocol::poll_ports()
121 int timeout = 10; // milliseconds
122 int no_ports_sleep = 1000; // milliseconds
124 Glib::Mutex::Lock lock( update_mutex );
125 // if there are no ports
129 //cout << "poll_ports no ports" << endl;
130 usleep( no_ports_sleep * 1000 );
134 int retval = poll( pfd, nfds, timeout );
137 // gdb at work, perhaps
138 if ( errno != EINTR )
140 error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
148 void MackieControlProtocol::handle_port_inactive( SurfacePort * port )
150 // port gone away. So stop polling it ASAP
152 // delete the port instance
153 Glib::Mutex::Lock lock( update_mutex );
154 MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
155 if ( it != _ports.end() )
161 _ports_changed = true;
164 // TODO all the rebuilding of surfaces and so on
167 void MackieControlProtocol::handle_port_active( SurfacePort * port )
169 // no need to re-add port because it was already added
170 // during the init phase. So just update the local surface
171 // representation and send the representation to
172 // all existing ports
174 // TODO update bank size
176 // TODO rebuild surface, to have new units
178 // finally update session state to the surface
179 // TODO but this is also done in set_active, and
180 // in fact update_surface won't execute unless
182 //cout << "update_surface in handle_port_active" << endl;
186 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
188 //cout << "MackieControlProtocol::handle_port_init" << endl;
189 _ports_changed = true;