-void *
-Session::_midi_thread_work (void* arg)
-{
- pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
- pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
-
- ((Session *) arg)->midi_thread_work ();
- return 0;
-}
-
-void
-Session::midi_thread_work ()
-{
- MIDIRequest* request;
- struct pollfd pfd[4];
- int nfds = 0;
- int timeout;
- int fds_ready;
- struct sched_param rtparam;
- int x;
- bool restart;
- vector<MIDI::Port*> ports;
-
- PBD::notify_gui_about_thread_creation (pthread_self(), X_("MIDI"), 2048);
-
- memset (&rtparam, 0, sizeof (rtparam));
- rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
-
- if ((x = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
- // do we care? not particularly.
- }
-
- /* set up the port vector; 5 is the largest possible size for now */
-
- ports.assign (5, (MIDI::Port*) 0);
-
- while (1) {
-
- nfds = 0;
-
- pfd[nfds].fd = midi_request_pipe[0];
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- nfds++;
-
- if (Config->get_mmc_control() && _mmc_port && _mmc_port->selectable() >= 0) {
- pfd[nfds].fd = _mmc_port->selectable();
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- ports[nfds] = _mmc_port;
- //cerr << "MIDI port " << nfds << " = MMC @ " << _mmc_port << endl;
- nfds++;
- }
-
- /* if MTC is being handled on a different port from MMC
- or we are not handling MMC at all, poll
- the relevant port.
- */
-
- if (_mtc_port && (_mtc_port != _mmc_port || !Config->get_mmc_control()) && _mtc_port->selectable() >= 0) {
- pfd[nfds].fd = _mtc_port->selectable();
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- ports[nfds] = _mtc_port;
- //cerr << "MIDI port " << nfds << " = MTC @ " << _mtc_port << endl;
- nfds++;
- }
-
- if (_midi_clock_port && (_midi_clock_port != _mmc_port || !Config->get_mmc_control()) && _midi_clock_port->selectable() >= 0) {
- pfd[nfds].fd = _midi_clock_port->selectable();
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- ports[nfds] = _midi_clock_port;
- nfds++;
- }
-
- /* if we are using MMC control, we obviously have to listen
- the relevant port.
- */
-
- if (_midi_port && (_midi_port != _mmc_port || !Config->get_mmc_control()) && (_midi_port != _mtc_port) && _midi_port->selectable() >= 0) {
- pfd[nfds].fd = _midi_port->selectable();
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- ports[nfds] = _midi_port;
- // cerr << "MIDI port " << nfds << " = MIDI @ " << _midi_port << endl;
- nfds++;
- }
-
- if (!midi_timeouts.empty()) {
- timeout = 100; /* 10msecs */
- } else {
- timeout = -1; /* if there is no data, we don't care */
- }
-
- again:
- // cerr << "MIDI poll on " << nfds << " for " << timeout << endl;
- if (poll (pfd, nfds, timeout) < 0) {
- if (errno == EINTR) {
- /* gdb at work, perhaps */
- goto again;
- }
-
- error << string_compose(_("MIDI thread poll failed (%1)"), strerror (errno)) << endmsg;
-
- break;
- }
- // cerr << "MIDI thread wakes at " << get_cycles () << endl;
-
- fds_ready = 0;
-
- /* check the transport request pipe */
-
- if (pfd[0].revents & ~POLLIN) {
- error << _("Error on transport thread request pipe") << endmsg;
- break;
- }
-
- if (pfd[0].revents & POLLIN) {
-
- char foo[16];
-
- // cerr << "MIDI request FIFO ready\n";
- fds_ready++;
-
- /* empty the pipe of all current requests */
-
- while (1) {
- size_t nread = read (midi_request_pipe[0], &foo, sizeof (foo));
-
- if (nread > 0) {
- if ((size_t) nread < sizeof (foo)) {
- break;
- } else {
- continue;
- }
- } else if (nread == 0) {
- break;
- } else if (errno == EAGAIN) {
- break;
- } else {
- fatal << _("Error reading from transport request pipe") << endmsg;
- /*NOTREACHED*/
- }
- }
-
- while (midi_requests.read (&request, 1) == 1) {
-
- switch (request->type) {
- case MIDIRequest::PortChange:
- /* restart poll with new ports */
- // cerr << "rebind\n";
- restart = true;
- break;
-
- case MIDIRequest::Quit:
- delete request;
- pthread_exit_pbd (0);
- /*NOTREACHED*/
- break;
-
- default:
- break;
- }
-
-
- delete request;
- }
-
- }
-
- if (restart) {
- continue;
- }
-
- /* now read the rest of the ports */
-
- for (int p = 1; p < nfds; ++p) {
- if ((pfd[p].revents & ~POLLIN)) {
- // error << string_compose(_("Transport: error polling MIDI port %1 (revents =%2%3%4"), p, &hex, pfd[p].revents, &dec) << endmsg;
- break;
- }
-
- if (pfd[p].revents & POLLIN) {
- fds_ready++;
- ports[p]->parse ();
- }
- }
-
- /* timeout driven */
-
- if (fds_ready < 2 && timeout != -1) {
-
- for (MidiTimeoutList::iterator i = midi_timeouts.begin(); i != midi_timeouts.end(); ) {
-
- MidiTimeoutList::iterator tmp;
- tmp = i;
- ++tmp;
-
- if (!(*i)()) {
- midi_timeouts.erase (i);
- }
-
- i = tmp;
- }
- }
- }
-}