substantial overhaul of MCU code - no more separate thread, just connect to signals...
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol_poll.cc
index f6171c7f0e7d0fcc8d99304800895a2dc52e891e..6392a91c4f6abe36ff7432dfb98186326bd1291f 100644 (file)
@@ -29,151 +29,14 @@ const char * MackieControlProtocol::default_port_name = "mcu";
 
 bool MackieControlProtocol::probe()
 {
-       if ( MIDI::Manager::instance()->port( default_port_name ) == 0 )
-       {
+       if ( MIDI::Manager::instance()->port(default_port_name)  == 0 ) {
                info << "Mackie: No MIDI port called " << default_port_name << endmsg;
                return false;
-       }
-       else
-       {
+       } else {
                return true;
        }
 }
 
-void * MackieControlProtocol::monitor_work()
-{
-       register_thread (X_("MCU"));
-
-       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();
-                               }
-                       }
-                       // poll for session data that needs to go to the unit
-                       poll_session_data();
-               }
-               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()
-{
-#ifdef DEBUG
-       cout << "MackieControlProtocol::update_ports" << endl;
-#endif
-       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
-#ifdef DEBUG
-               cout << "MackieControlProtocol::update_ports lock acquired" << endl;
-#endif
-               if ( _ports_changed )
-               {
-                       // create new pollfd structures
-                       delete[] pfd;
-                       pfd = new pollfd[_ports.size()];
-#ifdef DEBUG
-                       cout << "pfd: " << pfd << endl;
-#endif
-                       nfds = 0;
-                       for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
-                       {
-                               // add the port any handler
-                               (*it)->connect_any();
-#ifdef DEBUG
-                               cout << "adding pollfd for port " << (*it)->port().name() << " to pollfd " << nfds << endl;
-#endif
-                               pfd[nfds].fd = (*it)->port().selectable();
-                               pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
-                               ++nfds;
-                       }
-                       _ports_changed = false;
-               }
-#ifdef DEBUG
-               cout << "MackieControlProtocol::update_ports signal" << endl;
-#endif
-               update_cond.signal();
-       }
-#ifdef DEBUG
-       cout << "MackieControlProtocol::update_ports finish" << endl;
-#endif
-}
-
-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
-               // for alsa/raw ports
-               // alsa/sequencer ports trigger the midi parser off poll
-               if ( (pfd[p].revents & POLLIN) > 0 )
-               {
-                       // 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();
-#ifdef DEBUG
-               cout << "poll_ports no ports" << endl;
-#endif
-               usleep( no_ports_sleep * 1000 );
-               return false;
-       }
-
-       int retval = ::poll( pfd, nfds, timeout );
-       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
@@ -187,9 +50,7 @@ void MackieControlProtocol::handle_port_inactive( SurfacePort * port )
                        _ports.erase( it );
                }
        }
-       _ports_changed = true;
-       update_ports();
-       
+
        // TODO all the rebuilding of surfaces and so on
 }
 
@@ -219,8 +80,6 @@ void MackieControlProtocol::handle_port_init (Mackie::SurfacePort *)
 #ifdef DEBUG
        cout << "MackieControlProtocol::handle_port_init" << endl;
 #endif
-       _ports_changed = true;
-       update_ports();
 #ifdef DEBUG
        cout << "MackieControlProtocol::handle_port_init finish" << endl;
 #endif