2008-06-02 17:41:35 -04:00
|
|
|
#include "mackie_control_protocol.h"
|
|
|
|
|
|
|
|
#include "midi_byte_array.h"
|
|
|
|
#include "surface_port.h"
|
|
|
|
|
2009-10-30 11:30:22 -04:00
|
|
|
#include "pbd/pthread_utils.h"
|
|
|
|
#include "pbd/error.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2009-10-30 11:30:22 -04:00
|
|
|
#include "midi++/types.h"
|
|
|
|
#include "midi++/port.h"
|
|
|
|
#include "midi++/manager.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
2009-10-30 11:30:22 -04:00
|
|
|
#include <string.h>
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Mackie;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
const char * MackieControlProtocol::default_port_name = "mcu";
|
|
|
|
|
|
|
|
bool MackieControlProtocol::probe()
|
|
|
|
{
|
2008-12-12 17:55:03 -05:00
|
|
|
if ( MIDI::Manager::instance()->port( default_port_name ) == 0 )
|
|
|
|
{
|
2009-10-22 16:05:40 -04:00
|
|
|
info << "Mackie: No MIDI port called " << default_port_name << endmsg;
|
2008-12-12 17:55:03 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void * MackieControlProtocol::monitor_work()
|
|
|
|
{
|
2009-12-04 14:24:09 -05:00
|
|
|
register_thread (X_("MCU"));
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
|
|
|
|
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
|
|
|
|
|
|
|
// read from midi ports
|
|
|
|
while ( _polling )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( poll_ports() )
|
|
|
|
{
|
|
|
|
try { read_ports(); }
|
|
|
|
catch ( exception & e ) {
|
|
|
|
cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
|
|
|
|
_ports_changed = true;
|
|
|
|
update_ports();
|
|
|
|
}
|
|
|
|
}
|
2008-12-12 17:55:03 -05:00
|
|
|
// poll for session data that needs to go to the unit
|
|
|
|
poll_session_data();
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
catch ( exception & e )
|
|
|
|
{
|
|
|
|
cout << "caught exception in MackieControlProtocol::monitor_work " << e.what() << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO ports and pfd and nfds should be in a separate class
|
|
|
|
delete[] pfd;
|
|
|
|
pfd = 0;
|
|
|
|
nfds = 0;
|
|
|
|
|
|
|
|
return (void*) 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MackieControlProtocol::update_ports()
|
|
|
|
{
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "MackieControlProtocol::update_ports" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
if ( _ports_changed )
|
|
|
|
{
|
|
|
|
Glib::Mutex::Lock ul( update_mutex );
|
|
|
|
// yes, this is a double-test locking paradigm, or whatever it's called
|
|
|
|
// because we don't *always* need to acquire the lock for the first test
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "MackieControlProtocol::update_ports lock acquired" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
if ( _ports_changed )
|
|
|
|
{
|
|
|
|
// create new pollfd structures
|
2008-12-18 14:31:00 -05:00
|
|
|
delete[] pfd;
|
2008-06-02 17:41:35 -04:00
|
|
|
pfd = new pollfd[_ports.size()];
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "pfd: " << pfd << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
nfds = 0;
|
|
|
|
for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
|
|
|
|
{
|
2008-12-12 17:55:03 -05:00
|
|
|
// add the port any handler
|
|
|
|
(*it)->connect_any();
|
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "adding pollfd for port " << (*it)->port().name() << " to pollfd " << nfds << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
pfd[nfds].fd = (*it)->port().selectable();
|
|
|
|
pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
|
|
|
|
++nfds;
|
|
|
|
}
|
|
|
|
_ports_changed = false;
|
|
|
|
}
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "MackieControlProtocol::update_ports signal" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
update_cond.signal();
|
|
|
|
}
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "MackieControlProtocol::update_ports finish" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MackieControlProtocol::read_ports()
|
|
|
|
{
|
|
|
|
/* now read any data on the ports */
|
|
|
|
Glib::Mutex::Lock lock( update_mutex );
|
|
|
|
for ( int p = 0; p < nfds; ++p )
|
|
|
|
{
|
|
|
|
// this will cause handle_midi_any in the MackiePort to be triggered
|
2008-12-12 09:43:24 -05:00
|
|
|
// for alsa/raw ports
|
|
|
|
// alsa/sequencer ports trigger the midi parser off poll
|
2008-09-20 15:02:58 -04:00
|
|
|
if ( (pfd[p].revents & POLLIN) > 0 )
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
// avoid deadlocking?
|
|
|
|
// doesn't seem to make a difference
|
|
|
|
//lock.release();
|
|
|
|
_ports[p]->read();
|
|
|
|
//lock.acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MackieControlProtocol::poll_ports()
|
|
|
|
{
|
|
|
|
int timeout = 10; // milliseconds
|
|
|
|
int no_ports_sleep = 1000; // milliseconds
|
|
|
|
|
|
|
|
Glib::Mutex::Lock lock( update_mutex );
|
|
|
|
// if there are no ports
|
|
|
|
if ( nfds < 1 )
|
|
|
|
{
|
|
|
|
lock.release();
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "poll_ports no ports" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
usleep( no_ports_sleep * 1000 );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-12 17:55:03 -05:00
|
|
|
int retval = ::poll( pfd, nfds, timeout );
|
2008-06-02 17:41:35 -04:00
|
|
|
if ( retval < 0 )
|
|
|
|
{
|
|
|
|
// gdb at work, perhaps
|
|
|
|
if ( errno != EINTR )
|
|
|
|
{
|
|
|
|
error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MackieControlProtocol::handle_port_inactive( SurfacePort * port )
|
|
|
|
{
|
|
|
|
// port gone away. So stop polling it ASAP
|
|
|
|
{
|
|
|
|
// delete the port instance
|
|
|
|
Glib::Mutex::Lock lock( update_mutex );
|
|
|
|
MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
|
|
|
|
if ( it != _ports.end() )
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
_ports.erase( it );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ports_changed = true;
|
|
|
|
update_ports();
|
|
|
|
|
|
|
|
// TODO all the rebuilding of surfaces and so on
|
|
|
|
}
|
|
|
|
|
2009-07-28 07:49:14 -04:00
|
|
|
void MackieControlProtocol::handle_port_active (SurfacePort *)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
|
|
|
// no need to re-add port because it was already added
|
|
|
|
// during the init phase. So just update the local surface
|
|
|
|
// representation and send the representation to
|
|
|
|
// all existing ports
|
|
|
|
|
|
|
|
// TODO update bank size
|
|
|
|
|
|
|
|
// TODO rebuild surface, to have new units
|
|
|
|
|
|
|
|
// finally update session state to the surface
|
|
|
|
// TODO but this is also done in set_active, and
|
|
|
|
// in fact update_surface won't execute unless
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "update_surface in handle_port_active" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
// _active == true
|
|
|
|
update_surface();
|
|
|
|
}
|
|
|
|
|
2009-07-28 07:49:14 -04:00
|
|
|
void MackieControlProtocol::handle_port_init (Mackie::SurfacePort *)
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "MackieControlProtocol::handle_port_init" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
_ports_changed = true;
|
|
|
|
update_ports();
|
2008-12-12 17:55:03 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
cout << "MackieControlProtocol::handle_port_init finish" << endl;
|
|
|
|
#endif
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|