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>
12 #include <midi++/port_request.h>
25 using namespace Mackie;
28 const char * MackieControlProtocol::default_port_name = "mcu";
30 bool MackieControlProtocol::probe()
32 return MIDI::Manager::instance()->port( default_port_name ) != 0;
35 void * MackieControlProtocol::monitor_work()
37 // What does ThreadCreatedWithRequestSize do?
38 PBD::ThreadCreated (pthread_self(), X_("Mackie"));
40 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
41 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
43 // read from midi ports
51 catch ( exception & e ) {
52 cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
53 _ports_changed = true;
57 // poll for automation data from the routes
60 catch ( exception & e )
62 cout << "caught exception in MackieControlProtocol::monitor_work " << e.what() << endl;
66 // TODO ports and pfd and nfds should be in a separate class
74 void MackieControlProtocol::update_ports()
78 Glib::Mutex::Lock ul( update_mutex );
79 // yes, this is a double-test locking paradigm, or whatever it's called
80 // because we don't *always* need to acquire the lock for the first test
83 // create new pollfd structures
84 if ( pfd != 0 ) delete[] pfd;
85 // TODO This might be a memory leak. How does thread cancellation cleanup work?
86 pfd = new pollfd[_ports.size()];
89 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
91 //cout << "adding port " << (*it)->port().name() << " to pollfd" << endl;
92 pfd[nfds].fd = (*it)->port().selectable();
93 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
96 _ports_changed = false;
102 void MackieControlProtocol::read_ports()
104 /* now read any data on the ports */
105 Glib::Mutex::Lock lock( update_mutex );
106 for ( int p = 0; p < nfds; ++p )
108 // this will cause handle_midi_any in the MackiePort to be triggered
109 if ( pfd[p].revents & POLLIN > 0 )
111 // avoid deadlocking?
112 // doesn't seem to make a difference
120 bool MackieControlProtocol::poll_ports()
122 int timeout = 10; // milliseconds
123 int no_ports_sleep = 1000; // milliseconds
125 Glib::Mutex::Lock lock( update_mutex );
126 // if there are no ports
130 //cout << "poll_ports no ports" << endl;
131 usleep( no_ports_sleep * 1000 );
135 int retval = poll( pfd, nfds, timeout );
138 // gdb at work, perhaps
139 if ( errno != EINTR )
141 error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
149 void MackieControlProtocol::handle_port_inactive( SurfacePort * port )
151 // port gone away. So stop polling it ASAP
153 // delete the port instance
154 Glib::Mutex::Lock lock( update_mutex );
155 MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
156 if ( it != _ports.end() )
162 _ports_changed = true;
165 // TODO all the rebuilding of surfaces and so on
168 void MackieControlProtocol::handle_port_active( SurfacePort * port )
170 // no need to re-add port because it was already added
171 // during the init phase. So just update the local surface
172 // representation and send the representation to
173 // all existing ports
175 // TODO update bank size
177 // TODO rebuild surface, to have new units
179 // finally update session state to the surface
180 // TODO but this is also done in set_active, and
181 // in fact update_surface won't execute unless
183 //cout << "update_surface in handle_port_active" << endl;
187 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
189 //cout << "MackieControlProtocol::handle_port_init" << endl;
190 _ports_changed = true;