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 cout << "MackieControlProtocol::monitor_work" << endl;
38 // What does ThreadCreatedWithRequestSize do?
39 PBD::ThreadCreated (pthread_self(), X_("Mackie"));
42 // it seems to do the "block" on poll less often
43 // with this code disabled
44 struct sched_param rtparam;
45 memset (&rtparam, 0, sizeof (rtparam));
46 rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
49 if ((err = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
50 // do we care? not particularly.
51 PBD::info << string_compose (_("%1: thread not running with realtime scheduling (%2)"), name(), strerror( errno )) << endmsg;
55 pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
56 pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
58 // read from midi ports
59 cout << "start poll cycle" << endl;
66 catch ( exception & e ) {
67 cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
70 // provide a cancellation point
74 // these never get called
75 cout << "MackieControlProtocol::poll_ports exiting" << endl;
82 void MackieControlProtocol::update_ports()
84 // create pollfd structures if necessary
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
92 cout << "MackieControlProtocol::update_ports updating" << endl;
93 if ( pfd != 0 ) delete[] pfd;
94 // TODO This might be a memory leak. How does thread cancellation cleanup work?
95 pfd = new pollfd[_ports.size()];
98 for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
100 cout << "adding port " << (*it)->port().name() << " to pollfd" << endl;
101 pfd[nfds].fd = (*it)->port().selectable();
102 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
105 _ports_changed = false;
107 cout << "MackieControlProtocol::update_ports signalling" << endl;
108 update_cond.signal();
109 cout << "MackieControlProtocol::update_ports finished" << endl;
113 void MackieControlProtocol::read_ports()
115 /* now read any data on the ports */
116 Glib::Mutex::Lock lock( update_mutex );
117 for ( int p = 0; p < nfds; ++p )
119 // this will cause handle_midi_any in the MackiePort to be triggered
120 if ( pfd[p].revents & POLLIN > 0 )
129 bool MackieControlProtocol::poll_ports()
131 int timeout = 10; // milliseconds
132 int no_ports_sleep = 10; // milliseconds
134 Glib::Mutex::Lock lock( update_mutex );
135 // if there are no ports
139 cout << "poll_ports no ports" << endl;
140 usleep( no_ports_sleep * 1000 );
144 int retval = poll( pfd, nfds, timeout );
147 // gdb at work, perhaps
148 if ( errno != EINTR )
150 error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
158 void MackieControlProtocol::handle_port_changed( SurfacePort * port, bool active )
160 cout << "MackieControlProtocol::handle_port_changed port: " << *port << " active: " << active << endl;
161 if ( active == false )
163 // port gone away. So stop polling it ASAP
165 // delete the port instance
166 Glib::Mutex::Lock lock( update_mutex );
167 MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
168 if ( it != _ports.end() )
174 _ports_changed = true;
179 _ports_changed = true;
184 // TODO update bank size
190 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
192 cout << "MackieControlProtocol::handle_port_init" << endl;
193 _ports_changed = true;
195 cout << "MackieControlProtocol::handle_port_init finished" << endl;