assert() to help find some possible causes of #2991. Fix some confusion with GTK...
[ardour.git] / libs / ardour / midi_ui.cc
index 5a0640b3f678d408144afab9eb2353578154d453..fb85309ee1a7e9203d288ec97b9a2cb7919354c7 100644 (file)
@@ -18,6 +18,8 @@
 */
 #include <cstdlib>
 
+#include <sigc++/signal.h>
+
 #include "pbd/pthread_utils.h"
 
 #include "midi++/manager.h"
@@ -37,6 +39,7 @@ 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 */
 
@@ -44,12 +47,14 @@ MidiControlUI::MidiControlUI (Session& s)
        : AbstractUI<MidiUIRequest> (_("midiui"))
        , _session (s) 
 {
-       MIDI::Manager::instance()->PortsChanged.connect (mem_fun (*this, &MidiControlUI::change_midi_ports));
+       MIDI::Manager::instance()->PortsChanged.connect_same_thread (rebind_connection, boost::bind (&MidiControlUI::change_midi_ports, this));
+       _instance = this;
 }
 
 MidiControlUI::~MidiControlUI ()
 {
        clear_ports ();
+       _instance = 0;
 }
 
 void
@@ -63,7 +68,17 @@ MidiControlUI::do_request (MidiUIRequest* req)
 
        } 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 ();
        }
 }
 
@@ -80,6 +95,8 @@ MidiControlUI::change_midi_ports ()
 bool
 MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
 {
+       DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on  %1\n", port->name()));
+
        if (ioc & ~IO_IN) {
                return false;
        }
@@ -102,8 +119,8 @@ void
 MidiControlUI::clear_ports ()
 {
        for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
-               /* remove existing sources from the event loop */
-               (*i)->destroy ();
+               g_source_destroy (*i);
+               g_source_unref (*i);
        }
 
        port_sources.clear ();
@@ -118,15 +135,18 @@ MidiControlUI::reset_ports ()
 
        for (MIDI::Manager::PortList::iterator i = plist.begin(); i != plist.end(); ++i) {
                int fd;
+
                if ((fd = (*i)->selectable ()) >= 0) {
                        Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
-                       psrc->connect (bind (mem_fun (*this, &MidiControlUI::midi_input_handler), (*i)));
-                       port_sources.push_back (psrc);
-               } 
-       }
 
-       for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
-               (*i)->attach (_main_loop->get_context());
+                       psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *i));
+                       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());
+               } 
        }
 }
 
@@ -135,6 +155,12 @@ MidiControlUI::thread_init ()
 {      
        struct sched_param rtparam;
 
+       char* c = new char[7];
+       strcpy (c, X_("midiUI"));
+       pthread_set_name (c);
+
+       cerr << "MIDI UI running\n";
+
        PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
        SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);