- 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::ThreadCreatedWithRequestSize (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; 4 is the largest possible size for now */
-
- ports.push_back (0);
- ports.push_back (0);
- ports.push_back (0);
- ports.push_back (0);
-
- while (1) {
-
- nfds = 0;
-
- pfd[nfds].fd = midi_request_pipe[0];
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- nfds++;
-
- /* if we are using MMC control, we obviously have to listen
- on the appropriate port.
- */
-
- if (mmc_control && _mmc_port && _mmc_port->selectable() >= 0) {
- pfd[nfds].fd = _mmc_port->selectable();
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- ports[nfds] = _mmc_port;
- 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 || !mmc_control) && _mtc_port->selectable() >= 0) {
- pfd[nfds].fd = _mtc_port->selectable();
- pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
- ports[nfds] = _mtc_port;
- nfds++;
- }
-
- if (_midi_port && (_midi_port != _mmc_port || !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;
- 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;
- restart = false;
-
- /* 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::SendFullMTC:
- // cerr << "send full MTC\n";
- send_full_time_code ();
- // cerr << "... done\n";
- break;
-
- case MIDIRequest::SendMTC:
- // cerr << "send qtr MTC\n";
- send_midi_time_code ();
- // cerr << "... done\n";
- break;
-
- case MIDIRequest::SendMMC:
- // cerr << "send MMC\n";
- deliver_mmc (request->mmc_cmd, request->locate_frame);
- // cerr << "... done\n";
- break;
-
- case MIDIRequest::SendMessage:
- // cerr << "send Message\n";
- deliver_midi_message (request->port, request->ev, request->chan, request->data);
- // cerr << "... done\n";
- break;
-
- case MIDIRequest::Deliver:
- // cerr << "deliver\n";
- deliver_data (_midi_port, request->buf, request->size);
- // cerr << "... done\n";
- break;
-
- 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;
- }