add sysex support to MIDI binding maps, and a couple more functions
[ardour.git] / libs / surfaces / generic_midi / generic_midi_control_protocol.cc
index a20835dc051e8ede593f651617a0530a988a96a2..d0d74f4c12196efd5ae3daea0c0d0bc4d75ddbe1 100644 (file)
@@ -20,6 +20,7 @@
 #define __STDC_FORMAT_MACROS 1
 #include <stdint.h>
 
+#include <sstream>
 #include <algorithm>
 
 #include "pbd/error.h"
 
 #include "ardour/session.h"
 #include "ardour/route.h"
+#include "ardour/midi_ui.h"
 
 #include "generic_midi_control_protocol.h"
 #include "midicontrollable.h"
+#include "midifunction.h"
 
 using namespace ARDOUR;
 using namespace PBD;
+using namespace std;
 
 #include "i18n.h"
 
+#define midi_ui_context() MidiControlUI::instance() /* a UICallback-derived object that specifies the event loop for signal handling */
+#define ui_bind(x) boost::protect (boost::bind ((x)))
+
 GenericMidiControlProtocol::GenericMidiControlProtocol (Session& s)
-       : ControlProtocol (s, _("Generic MIDI"))
+       : ControlProtocol (s, _("Generic MIDI"), midi_ui_context())
 {
        MIDI::Manager* mm = MIDI::Manager::instance();
 
@@ -59,21 +66,40 @@ GenericMidiControlProtocol::GenericMidiControlProtocol (Session& s)
        _feedback_interval = 10000; // microseconds
        last_feedback_time = 0;
 
-       auto_binding = FALSE;
+       /* XXX is it right to do all these in the same thread as whatever emits the signal? */
 
-       Controllable::StartLearning.connect (mem_fun (*this, &GenericMidiControlProtocol::start_learning));
-       Controllable::StopLearning.connect (mem_fun (*this, &GenericMidiControlProtocol::stop_learning));
-       Session::SendFeedback.connect (mem_fun (*this, &GenericMidiControlProtocol::send_feedback));
-       
-       Controllable::CreateBinding.connect (mem_fun (*this, &GenericMidiControlProtocol::create_binding));
-       Controllable::DeleteBinding.connect (mem_fun (*this, &GenericMidiControlProtocol::delete_binding));
+       Controllable::StartLearning.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::start_learning, this, _1));
+       Controllable::StopLearning.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::stop_learning, this, _1));
+       Controllable::CreateBinding.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::create_binding, this, _1, _2, _3));
+       Controllable::DeleteBinding.connect_same_thread (*this, boost::bind (&GenericMidiControlProtocol::delete_binding, this, _1));
+
+       Session::SendFeedback.connect (*this, boost::bind (&GenericMidiControlProtocol::send_feedback, this), midi_ui_context());;
 
-       Session::AutoBindingOn.connect (mem_fun (*this, &GenericMidiControlProtocol::auto_binding_on));
-       Session::AutoBindingOff.connect (mem_fun (*this, &GenericMidiControlProtocol::auto_binding_off));
+       std::string xmlpath = "/tmp/midi.map";
+
+       load_bindings (xmlpath);
+       reset_controllables ();
 }
 
 GenericMidiControlProtocol::~GenericMidiControlProtocol ()
 {
+       Glib::Mutex::Lock lm (pending_lock);
+       Glib::Mutex::Lock lm2 (controllables_lock);
+
+       for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
+               delete *i;
+       }
+       controllables.clear ();
+
+       for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
+               delete *i;
+       }
+       pending_controllables.clear ();
+
+       for (MIDIFunctions::iterator i = functions.begin(); i != functions.end(); ++i) {
+               delete *i;
+       }
+       functions.clear ();
 }
 
 int
@@ -135,6 +161,9 @@ GenericMidiControlProtocol::start_learning (Controllable* c)
                return false;
        }
 
+       Glib::Mutex::Lock lm (pending_lock);
+       Glib::Mutex::Lock lm2 (controllables_lock);
+
        MIDIControllables::iterator tmp;
        for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ) {
                tmp = i;
@@ -150,9 +179,10 @@ GenericMidiControlProtocol::start_learning (Controllable* c)
        for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ) {
                ptmp = i;
                ++ptmp;
-               if (((*i).first)->get_controllable() == c) {
-                       (*i).second.disconnect();
-                       delete (*i).first;
+               if (((*i)->first)->get_controllable() == c) {
+                       (*i)->second.disconnect();
+                       delete (*i)->first;
+                       delete *i;
                        pending_controllables.erase (i);
                }
                i = ptmp;
@@ -175,13 +205,11 @@ GenericMidiControlProtocol::start_learning (Controllable* c)
        {
                Glib::Mutex::Lock lm (pending_lock);
 
-#if 0 // BOOST SIGNALS
-               std::pair<MIDIControllable *, boost::signals2::scoped_connection> element;
-               element.first = mc;
-               element.second = c->LearningFinished.connect (bind (mem_fun (*this, &GenericMidiControlProtocol::learning_stopped), mc));
+               MIDIPendingControllable* element = new MIDIPendingControllable;
+               element->first = mc;
+               c->LearningFinished.connect_same_thread (element->second, boost::bind (&GenericMidiControlProtocol::learning_stopped, this, mc));
 
                pending_controllables.push_back (element);
-#endif
        }
 
        mc->learn_about_external_control ();
@@ -200,8 +228,9 @@ GenericMidiControlProtocol::learning_stopped (MIDIControllable* mc)
                tmp = i;
                ++tmp;
 
-               if ( (*i).first == mc) {
-                       (*i).second.disconnect();
+               if ( (*i)->first == mc) {
+                       (*i)->second.disconnect();
+                       delete *i;
                        pending_controllables.erase(i);
                }
 
@@ -223,11 +252,12 @@ GenericMidiControlProtocol::stop_learning (Controllable* c)
        */
 
        for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
-               if (((*i).first)->get_controllable() == c) {
-                       (*i).first->stop_learning ();
-                       dptr = (*i).first;
-                       (*i).second.disconnect();
+               if (((*i)->first)->get_controllable() == c) {
+                       (*i)->first->stop_learning ();
+                       dptr = (*i)->first;
+                       (*i)->second.disconnect();
 
+                       delete *i;
                        pending_controllables.erase (i);
                        break;
                }
@@ -289,18 +319,6 @@ GenericMidiControlProtocol::create_binding (PBD::Controllable* control, int pos,
        }
 }
 
-void
-GenericMidiControlProtocol::auto_binding_on()
-{
-       auto_binding = TRUE;
-}
-
-void
-GenericMidiControlProtocol::auto_binding_off()
-{
-       auto_binding = FALSE;
-}
-
 XMLNode&
 GenericMidiControlProtocol::get_state () 
 {
@@ -345,43 +363,43 @@ GenericMidiControlProtocol::set_state (const XMLNode& node, int version)
                _feedback_interval = 10000;
        }
 
-       if ( !auto_binding ) {
-               
-               boost::shared_ptr<Controllable> c;
-               
-               {
-                       Glib::Mutex::Lock lm (pending_lock);
-                       pending_controllables.clear ();
-               }
-               
-               Glib::Mutex::Lock lm2 (controllables_lock);
-               controllables.clear ();
-               nlist = node.children(); // "controls"
-               
-               if (nlist.empty()) {
-                       return 0;
+       boost::shared_ptr<Controllable> c;
+       
+       {
+               Glib::Mutex::Lock lm (pending_lock);
+               for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
+                       delete *i;
                }
+               pending_controllables.clear ();
+       }
+       
+       Glib::Mutex::Lock lm2 (controllables_lock);
+       controllables.clear ();
+       nlist = node.children(); // "controls"
+       
+       if (nlist.empty()) {
+               return 0;
+       }
+       
+       nlist = nlist.front()->children ();
                
-               nlist = nlist.front()->children ();
+       for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
                
-               for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+               if ((prop = (*niter)->property ("id")) != 0) {
                        
-                       if ((prop = (*niter)->property ("id")) != 0) {
-
-                               ID id = prop->value ();
-                               c = session->controllable_by_id (id);
-                               
-                               if (c) {
-                                       MIDIControllable* mc = new MIDIControllable (*_port, *c);
-                                       if (mc->set_state (**niter, version) == 0) {
-                                               controllables.insert (mc);
-                                       }
-                                       
-                               } else {
-                                       warning << string_compose (
-                                                       _("Generic MIDI control: controllable %1 not found in session (ignored)"),
-                                                       id) << endmsg;
+                       ID id = prop->value ();
+                       c = session->controllable_by_id (id);
+                       
+                       if (c) {
+                               MIDIControllable* mc = new MIDIControllable (*_port, *c);
+                               if (mc->set_state (**niter, version) == 0) {
+                                       controllables.insert (mc);
                                }
+                               
+                       } else {
+                               warning << string_compose (
+                                       _("Generic MIDI control: controllable %1 not found in session (ignored)"),
+                                       id) << endmsg;
                        }
                }
        }
@@ -402,3 +420,216 @@ GenericMidiControlProtocol::get_feedback () const
        return do_feedback;
 }
 
+int
+GenericMidiControlProtocol::load_bindings (const string& xmlpath)
+{
+       XMLTree state_tree;
+
+       if (!state_tree.read (xmlpath.c_str())) {
+               error << string_compose(_("Could not understand MIDI bindings file %1"), xmlpath) << endmsg;
+               return -1;
+       }
+
+       XMLNode* root = state_tree.root();
+
+       if (root->name() != X_("ArdourMIDIBindings")) {
+               error << string_compose (_("MIDI Bindings file %1 is not really a MIDI bindings file"), xmlpath) << endmsg;
+               return -1;
+       }
+
+       const XMLProperty* prop;
+
+       if ((prop = root->property ("version")) == 0) {
+               return -1;
+       } else {
+               int major;
+               int minor;
+               int micro;
+
+               sscanf (prop->value().c_str(), "%d.%d.%d", &major, &minor, &micro);
+               Stateful::loading_state_version = (major * 1000) + minor;
+       }
+       
+       const XMLNodeList& children (root->children());
+       XMLNodeConstIterator citer;
+       XMLNodeConstIterator gciter;
+
+       MIDIControllable* mc;
+
+       for (citer = children.begin(); citer != children.end(); ++citer) {
+               if ((*citer)->name() == "Binding") {
+                       const XMLNode* child = *citer;
+
+                       if (child->property ("uri")) {
+                               /* controllable */
+                               
+                               if ((mc = create_binding (*child)) != 0) {
+                                       Glib::Mutex::Lock lm2 (controllables_lock);
+                                       controllables.insert (mc);
+                               }
+
+                       } else if (child->property ("function")) {
+
+                               cerr << "try to create a function from " << child->property ("function")->value() << endl;
+
+                               /* function */
+                               MIDIFunction* mf;
+
+                               if ((mf = create_function (*child)) != 0) {
+                                       functions.push_back (mf);
+                               }
+                       }
+               }
+       }
+
+       return 0;
+}
+
+MIDIControllable*
+GenericMidiControlProtocol::create_binding (const XMLNode& node)
+{
+       const XMLProperty* prop;
+       int detail;
+       int channel;
+       string uri;
+       MIDI::eventType ev;
+
+       if ((prop = node.property (X_("channel"))) == 0) {
+               return 0;
+       }
+       
+       if (sscanf (prop->value().c_str(), "%d", &channel) != 1) {
+               return 0;
+       }
+
+       if ((prop = node.property (X_("ctl"))) != 0) {
+               ev = MIDI::controller;
+       } else if ((prop = node.property (X_("note"))) != 0) {
+               ev = MIDI::on;
+       } else if ((prop = node.property (X_("pgm"))) != 0) {
+               ev = MIDI::program;
+       } else {
+               return 0;
+       }
+
+       if (sscanf (prop->value().c_str(), "%d", &detail) != 1) {
+               return 0;
+       }
+
+       prop = node.property (X_("uri"));
+       uri = prop->value();
+
+       MIDIControllable* mc = new MIDIControllable (*_port, uri, false);
+       mc->bind_midi (channel, ev, detail);
+
+       cerr << "New MC with URI = " << uri << endl;
+
+       return mc;
+}
+
+void
+GenericMidiControlProtocol::reset_controllables ()
+{
+       Glib::Mutex::Lock lm2 (controllables_lock);
+       
+       for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end(); ++iter) {
+               MIDIControllable* existingBinding = (*iter);
+               
+               boost::shared_ptr<Controllable> c = session->controllable_by_uri (existingBinding->current_uri());
+               existingBinding->set_controllable (c.get());
+       }
+}
+
+MIDIFunction*
+GenericMidiControlProtocol::create_function (const XMLNode& node)
+{
+       const XMLProperty* prop;
+       int intval;
+       MIDI::byte detail = 0;
+       MIDI::channel_t channel = 0;
+       string uri;
+       MIDI::eventType ev;
+       MIDI::byte* sysex = 0;
+       uint32_t sysex_size = 0;
+
+       if ((prop = node.property (X_("ctl"))) != 0) {
+               ev = MIDI::controller;
+       } else if ((prop = node.property (X_("note"))) != 0) {
+               ev = MIDI::on;
+       } else if ((prop = node.property (X_("pgm"))) != 0) {
+               ev = MIDI::program;
+       } else if ((prop = node.property (X_("sysex"))) != 0) {
+
+               ev = MIDI::sysex;
+               int val;
+               uint32_t cnt;
+
+               {
+                       cnt = 0;
+                       stringstream ss (prop->value());
+                       ss << hex;
+                       
+                       while (ss >> val) {
+                               cnt++;
+                       }
+               }
+
+               if (cnt == 0) {
+                       return 0;
+               }
+
+               sysex = new MIDI::byte[cnt];
+               sysex_size = cnt;
+               
+               {
+                       stringstream ss (prop->value());
+                       ss << hex;
+                       cnt = 0;
+                       
+                       while (ss >> val) {
+                               sysex[cnt++] = (MIDI::byte) val;
+                               cerr << hex << (int) sysex[cnt-1] << dec << ' ' << endl;
+                       }
+               }
+               
+       } else {
+               warning << "Binding ignored - unknown type" << endmsg;
+               return 0;
+       }
+
+       if (sysex_size == 0) {
+               if ((prop = node.property (X_("channel"))) == 0) {
+                       return 0;
+               }
+       
+               if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
+                       return 0;
+               }
+               channel = (MIDI::channel_t) intval;
+               /* adjust channel to zero-based counting */
+               if (channel > 0) {
+                       channel -= 1;
+               }
+               
+               if (sscanf (prop->value().c_str(), "%d", &intval) != 1) {
+                       return 0;
+               }
+               
+               detail = (MIDI::byte) intval;
+       }
+
+       prop = node.property (X_("function"));
+       
+       MIDIFunction* mf = new MIDIFunction (*_port);
+       
+       if (mf->init (*this, prop->value(), sysex, sysex_size)) {
+               delete mf;
+               return 0;
+       }
+
+       mf->bind_midi (channel, ev, detail);
+
+       cerr << "New MF with function = " << prop->value() << endl;
+
+       return mf;
+}