Merge branch 'master' into windows
[ardour.git] / libs / ardour / midi_ui.cc
index 5a0640b3f678d408144afab9eb2353578154d453..e6d49175bd143ac4b9f4154732f170b1ad73f3fa 100644 (file)
 */
 #include <cstdlib>
 
-#include "pbd/pthread_utils.h"
+#include <sigc++/signal.h>
 
-#include "midi++/manager.h"
-#include "midi++/port.h"
+#include "pbd/pthread_utils.h"
 
+#include "ardour/async_midi_port.h"
 #include "ardour/debug.h"
 #include "ardour/audioengine.h"
+#include "ardour/midi_port.h"
+#include "ardour/midiport_manager.h"
 #include "ardour/midi_ui.h"
 #include "ardour/session.h"
 #include "ardour/session_event.h"
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 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 */
 
 MidiControlUI::MidiControlUI (Session& s)
-       : AbstractUI<MidiUIRequest> (_("midiui"))
-       , _session (s) 
+       : AbstractUI<MidiUIRequest> (X_("midiui"))
+       , _session (s)
 {
-       MIDI::Manager::instance()->PortsChanged.connect (mem_fun (*this, &MidiControlUI::change_midi_ports));
+       _instance = this;
 }
 
 MidiControlUI::~MidiControlUI ()
 {
        clear_ports ();
+       _instance = 0;
 }
 
 void
@@ -63,35 +68,37 @@ 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 ();
-       }
-}
 
-void
-MidiControlUI::change_midi_ports ()
-{
-       MidiUIRequest* req = get_request (PortChange);
-       if (req == 0) {
-               return;
+       } else if (req->type == Quit) {
+
+               BaseUI::quit ();
        }
-       send_request (req);
 }
 
 bool
-MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
+MidiControlUI::midi_input_handler (IOCondition ioc, AsyncMIDIPort* port)
 {
+       DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on  %1\n", ((ARDOUR::Port*)port)->name()));
+
        if (ioc & ~IO_IN) {
                return false;
        }
 
        if (ioc & IO_IN) {
 
-               if (port->must_drain_selectable()) {
-                       CrossThreadChannel::drain (port->selectable());
-               }
+#ifndef PLATFORM_WINDOWS
+               CrossThreadChannel::drain (port->selectable());
+#endif
 
-               DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
-               nframes64_t now = _session.engine().frame_time();
+               DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", ((ARDOUR::Port*)port)->name()));
+               framepos_t now = _session.engine().sample_time();
                port->parse (now);
        }
 
@@ -102,8 +109,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 ();
@@ -112,29 +119,36 @@ MidiControlUI::clear_ports ()
 void
 MidiControlUI::reset_ports ()
 {
-       clear_ports ();
+       if (port_sources.empty()) {
+               AsyncMIDIPort* async = dynamic_cast<AsyncMIDIPort*> (_session.midi_input_port());
 
-       MIDI::Manager::PortList plist = MIDI::Manager::instance()->get_midi_ports ();
+               if (!async) {
+                       return;
+               }
 
-       for (MIDI::Manager::PortList::iterator i = plist.begin(); i != plist.end(); ++i) {
                int fd;
-               if ((fd = (*i)->selectable ()) >= 0) {
+
+               if ((fd = async->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);
-               } 
-       }
+                       
+                       psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), async));
+                       psrc->attach (_main_loop->get_context());
 
-       for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
-               (*i)->attach (_main_loop->get_context());
+                       // glibmm hack: for now, store only the GSource*
+
+                       port_sources.push_back (psrc->gobj());
+                       g_source_ref (psrc->gobj());
+               }
        }
 }
 
 void
 MidiControlUI::thread_init ()
-{      
+{
        struct sched_param rtparam;
 
+       pthread_set_name (X_("midiUI"));
+
        PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self(), X_("MIDI"), 2048);
        SessionEvent::create_per_thread_pool (X_("MIDI I/O"), 128);