Merge branch 'waveview_hacks' of https://github.com/nmains/ardour into cairocanvas
[ardour.git] / libs / ardour / midi_ui.cc
index 7346c0244ca92287008494bc0b9f886658f90ac7..e00ec587ec4da2f6180b397ee7c3ecb09ce0b92e 100644 (file)
@@ -39,7 +39,6 @@ using namespace Glib;
 
 #include "i18n.h"
 
-BaseUI::RequestType MidiControlUI::PortChange = BaseUI::new_request_type();
 MidiControlUI* MidiControlUI::_instance = 0;
 
 #include "pbd/abstract_ui.cc"  /* instantiate the template */
@@ -53,32 +52,21 @@ MidiControlUI::MidiControlUI (Session& s)
 
 MidiControlUI::~MidiControlUI ()
 {
+       /* stop the thread */
+       quit ();
+       /* drop all ports as GIO::Sources */
        clear_ports ();
+       /* we no longer exist */
        _instance = 0;
 }
 
 void
 MidiControlUI::do_request (MidiUIRequest* req)
 {
-       if (req->type == PortChange) {
-
-               /* restart event loop with new ports */
-               DEBUG_TRACE (DEBUG::MidiIO, "reset ports\n");
-               reset_ports ();
-
+       if (req->type == Quit) {
+               BaseUI::quit ();
        } else if (req->type == CallSlot) {
-
-#ifndef NDEBUG
-               if (getenv ("DEBUG_THREADED_SIGNALS")) {
-                       cerr << "MIDI UI calls a slot\n";
-               }
-#endif
-
                req->the_slot ();
-
-       } else if (req->type == Quit) {
-
-               BaseUI::quit ();
        }
 }
 
@@ -93,7 +81,9 @@ MidiControlUI::midi_input_handler (IOCondition ioc, AsyncMIDIPort* port)
 
        if (ioc & IO_IN) {
 
+#ifndef PLATFORM_WINDOWS
                CrossThreadChannel::drain (port->selectable());
+#endif
 
                DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", ((ARDOUR::Port*)port)->name()));
                framepos_t now = _session.engine().sample_time();
@@ -117,23 +107,41 @@ MidiControlUI::clear_ports ()
 void
 MidiControlUI::reset_ports ()
 {
-       if (port_sources.empty()) {
-               AsyncMIDIPort* async = dynamic_cast<AsyncMIDIPort*> (_session.midi_input_port());
-
-               if (!async) {
-                       return;
-               }
+       if (!port_sources.empty()) {
+               return;
+       }
+       
+       vector<AsyncMIDIPort*> ports;
+       AsyncMIDIPort* p;
+       
+       if ((p = dynamic_cast<AsyncMIDIPort*> (_session.midi_input_port()))) {
+               ports.push_back (p);
+       }
+       
+       
+       if ((p = dynamic_cast<AsyncMIDIPort*> (_session.mmc_input_port()))) {
+               ports.push_back (p);
+       }
 
-               int fd;
+       if ((p = dynamic_cast<AsyncMIDIPort*> (_session.scene_input_port()))) {
+               ports.push_back (p);
+       }
+       
+       if (ports.empty()) {
+               return;
+       }
+       
+       int fd;
+       for (vector<AsyncMIDIPort*>::const_iterator pi = ports.begin(); pi != ports.end(); ++pi) {
 
-               if ((fd = async->selectable ()) >= 0) {
+               if ((fd = (*pi)->selectable ()) >= 0) {
                        Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
                        
-                       psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), async));
+                       psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *pi));
                        psrc->attach (_main_loop->get_context());
-
+                       
                        // glibmm hack: for now, store only the GSource*
-
+                       
                        port_sources.push_back (psrc->gobj());
                        g_source_ref (psrc->gobj());
                }