enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / midi_ui.cc
index 0d9ac1760150c54b7bc5e612dd34c7216d76d17a..eaacde2969f3cb5cc077f8d5ee383610154f8570 100644 (file)
 
 #include "pbd/pthread_utils.h"
 
-#include "midi++/manager.h"
-#include "midi++/port.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"
@@ -37,66 +37,59 @@ using namespace ARDOUR;
 using namespace PBD;
 using namespace Glib;
 
-#include "i18n.h"
+#include "pbd/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> (X_("midiui"))
+       : AbstractUI<MidiUIRequest> (X_("midiUI"))
        , _session (s)
 {
-       MIDI::Manager::instance()->PortsChanged.connect_same_thread (rebind_connection, boost::bind (&MidiControlUI::change_midi_ports, this));
        _instance = this;
 }
 
 MidiControlUI::~MidiControlUI ()
 {
+       /* stop the thread */
+       quit ();
+       /* drop all ports as GIO::Sources */
        clear_ports ();
+       /* we no longer exist */
        _instance = 0;
 }
 
+void*
+MidiControlUI::request_factory (uint32_t num_requests)
+{
+       /* AbstractUI<T>::request_buffer_factory() is a template method only
+          instantiated in this source module. To provide something visible for
+          use when registering the factory, we have this static method that is
+          template-free.
+       */
+       return request_buffer_factory (num_requests);
+}
+
 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 ();
        }
 }
 
-void
-MidiControlUI::change_midi_ports ()
+bool
+MidiControlUI::midi_input_handler (IOCondition ioc, boost::weak_ptr<AsyncMIDIPort> wport)
 {
-       MidiUIRequest* req = get_request (PortChange);
-       if (req == 0) {
-               return;
+       boost::shared_ptr<AsyncMIDIPort> port = wport.lock ();
+       if (!port) {
+               return false;
        }
-       send_request (req);
-}
 
-bool
-MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
-{
-       DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on  %1\n", port->name()));
+       DEBUG_TRACE (DEBUG::MidiIO, string_compose ("something happend on  %1\n", boost::shared_ptr<ARDOUR::Port> (port)->name()));
 
        if (ioc & ~IO_IN) {
                return false;
@@ -104,9 +97,8 @@ MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
 
        if (ioc & IO_IN) {
 
-               CrossThreadChannel::drain (port->selectable());
-
-               DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", port->name()));
+               port->clear ();
+               DEBUG_TRACE (DEBUG::MidiIO, string_compose ("data available on %1\n", boost::shared_ptr<ARDOUR::Port>(port)->name()));
                framepos_t now = _session.engine().sample_time();
                port->parse (now);
        }
@@ -117,40 +109,35 @@ MidiControlUI::midi_input_handler (IOCondition ioc, MIDI::Port* port)
 void
 MidiControlUI::clear_ports ()
 {
-       for (PortSources::iterator i = port_sources.begin(); i != port_sources.end(); ++i) {
-               g_source_destroy (*i);
-               g_source_unref (*i);
-       }
-
-       port_sources.clear ();
 }
 
 void
 MidiControlUI::reset_ports ()
 {
-       clear_ports ();
+       vector<boost::shared_ptr<AsyncMIDIPort> > ports;
+       boost::shared_ptr<AsyncMIDIPort> p;
 
-       boost::shared_ptr<const MIDI::Manager::PortList> plist = MIDI::Manager::instance()->get_midi_ports ();
-
-       for (MIDI::Manager::PortList::const_iterator i = plist->begin(); i != plist->end(); ++i) {
-
-               if (!(*i)->centrally_parsed()) {
-                       continue;
-               }
+       if ((p = boost::dynamic_pointer_cast<AsyncMIDIPort> (_session.midi_input_port()))) {
+               ports.push_back (p);
+       }
 
-               int fd;
 
-               if ((fd = (*i)->selectable ()) >= 0) {
-                       Glib::RefPtr<IOSource> psrc = IOSource::create (fd, IO_IN|IO_HUP|IO_ERR);
+       if ((p = boost::dynamic_pointer_cast<AsyncMIDIPort> (_session.mmc_input_port()))) {
+               ports.push_back (p);
+       }
 
-                       psrc->connect (sigc::bind (sigc::mem_fun (this, &MidiControlUI::midi_input_handler), *i));
-                       psrc->attach (_main_loop->get_context());
+       if ((p = boost::dynamic_pointer_cast<AsyncMIDIPort> (_session.scene_input_port()))) {
+               ports.push_back (p);
+       }
 
-                       // glibmm hack: for now, store only the GSource*
+       if (ports.empty()) {
+               return;
+       }
 
-                       port_sources.push_back (psrc->gobj());
-                       g_source_ref (psrc->gobj());
-               }
+       for (vector<boost::shared_ptr<AsyncMIDIPort> >::const_iterator pi = ports.begin(); pi != ports.end(); ++pi) {
+               (*pi)->xthread().set_receive_handler (sigc::bind (
+                                       sigc::mem_fun (this, &MidiControlUI::midi_input_handler), boost::weak_ptr<AsyncMIDIPort>(*pi)));
+               (*pi)->xthread().attach (_main_loop->get_context());
        }
 }
 
@@ -161,8 +148,8 @@ MidiControlUI::thread_init ()
 
        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);
+       PBD::notify_event_loops_about_thread_creation (pthread_self(), X_("midiUI"), 2048);
+       SessionEvent::create_per_thread_pool (X_("midiUI"), 128);
 
        memset (&rtparam, 0, sizeof (rtparam));
        rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */