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 if ( MIDI::Manager::instance()->port( default_port_name ) == 0 )
33 error << "No port called mcu. Add it to ardour.rc." << endmsg;
42 void * MackieControlProtocol::monitor_work()
44 PBD::notify_gui_about_thread_creation (pthread_self(), X_("Mackie"));
46 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
47 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
49 // read from midi ports
57 catch ( exception & e ) {
58 cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
59 _ports_changed = true;
63 // poll for session data that needs to go to the unit
66 catch ( exception & e )
68 cout << "caught exception in MackieControlProtocol::monitor_work " << e.what() << endl;
72 // TODO ports and pfd and nfds should be in a separate class
80 void MackieControlProtocol::update_ports()
83 cout << "MackieControlProtocol::update_ports" << endl;
87 Glib::Mutex::Lock ul( update_mutex );
88 // yes, this is a double-test locking paradigm, or whatever it's called
89 // because we don't *always* need to acquire the lock for the first test
91 cout << "MackieControlProtocol::update_ports lock acquired" << endl;
95 // create new pollfd structures
97 pfd = new pollfd[_ports.size()];
99 cout << "pfd: " << pfd << endl;
102 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
104 // add the port any handler
105 (*it)->connect_any();
107 cout << "adding pollfd for port " << (*it)->port().name() << " to pollfd " << nfds << endl;
109 pfd[nfds].fd = (*it)->port().selectable();
110 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
113 _ports_changed = false;
116 cout << "MackieControlProtocol::update_ports signal" << endl;
118 update_cond.signal();
121 cout << "MackieControlProtocol::update_ports finish" << endl;
125 void MackieControlProtocol::read_ports()
127 /* now read any data on the ports */
128 Glib::Mutex::Lock lock( update_mutex );
129 for ( int p = 0; p < nfds; ++p )
131 // this will cause handle_midi_any in the MackiePort to be triggered
132 // for alsa/raw ports
133 // alsa/sequencer ports trigger the midi parser off poll
134 if ( (pfd[p].revents & POLLIN) > 0 )
136 // avoid deadlocking?
137 // doesn't seem to make a difference
145 bool MackieControlProtocol::poll_ports()
147 int timeout = 10; // milliseconds
148 int no_ports_sleep = 1000; // milliseconds
150 Glib::Mutex::Lock lock( update_mutex );
151 // if there are no ports
156 cout << "poll_ports no ports" << endl;
158 usleep( no_ports_sleep * 1000 );
162 int retval = ::poll( pfd, nfds, timeout );
165 // gdb at work, perhaps
166 if ( errno != EINTR )
168 error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
176 void MackieControlProtocol::handle_port_inactive( SurfacePort * port )
178 // port gone away. So stop polling it ASAP
180 // delete the port instance
181 Glib::Mutex::Lock lock( update_mutex );
182 MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
183 if ( it != _ports.end() )
189 _ports_changed = true;
192 // TODO all the rebuilding of surfaces and so on
195 void MackieControlProtocol::handle_port_active( SurfacePort * port )
197 // no need to re-add port because it was already added
198 // during the init phase. So just update the local surface
199 // representation and send the representation to
200 // all existing ports
202 // TODO update bank size
204 // TODO rebuild surface, to have new units
206 // finally update session state to the surface
207 // TODO but this is also done in set_active, and
208 // in fact update_surface won't execute unless
210 cout << "update_surface in handle_port_active" << endl;
216 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
219 cout << "MackieControlProtocol::handle_port_init" << endl;
221 _ports_changed = true;
224 cout << "MackieControlProtocol::handle_port_init finish" << endl;