+ MIDIControllables::iterator tmp;
+ for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ) {
+ tmp = i;
+ ++tmp;
+ if ((*i)->get_controllable() == c) {
+ delete (*i);
+ controllables.erase (i);
+ }
+ i = tmp;
+ }
+
+ {
+ Glib::Threads::Mutex::Lock lm (pending_lock);
+
+ MIDIPendingControllables::iterator ptmp;
+ for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ) {
+ ptmp = i;
+ ++ptmp;
+ if (((*i)->mc)->get_controllable() == c) {
+ if ((*i)->own_mc) {
+ delete (*i)->mc;
+ }
+ (*i)->connection.disconnect();
+ delete *i;
+ pending_controllables.erase (i);
+ }
+ i = ptmp;
+ }
+ }
+
+ MIDIControllable* mc = 0;
+ bool own_mc = false;
+
+ for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
+ if ((*i)->get_controllable() && ((*i)->get_controllable()->id() == c->id())) {
+ mc = *i;
+ break;
+ }
+ }
+
+ if (!mc) {
+ mc = new MIDIControllable (this, *_input_port->parser(), *c, false);
+ own_mc = true;
+ }
+
+ {
+ Glib::Threads::Mutex::Lock lm (pending_lock);
+
+ MIDIPendingControllable* element = new MIDIPendingControllable (mc, own_mc);
+ c->LearningFinished.connect_same_thread (element->connection, boost::bind (&GenericMidiControlProtocol::learning_stopped, this, mc));
+
+ pending_controllables.push_back (element);
+ }
+ mc->learn_about_external_control ();
+ return true;
+}
+
+void
+GenericMidiControlProtocol::learning_stopped (MIDIControllable* mc)
+{
+ Glib::Threads::Mutex::Lock lm (pending_lock);
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+
+ MIDIPendingControllables::iterator tmp;
+
+ for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ) {
+ tmp = i;
+ ++tmp;
+
+ if ( (*i)->mc == mc) {
+ (*i)->connection.disconnect();
+ delete *i;
+ pending_controllables.erase(i);
+ }
+
+ i = tmp;
+ }
+
+ controllables.push_back (mc);
+}
+
+void
+GenericMidiControlProtocol::stop_learning (Controllable* c)
+{
+ Glib::Threads::Mutex::Lock lm (pending_lock);
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+ MIDIControllable* dptr = 0;
+
+ /* learning timed out, and we've been told to consider this attempt to learn to be cancelled. find the
+ relevant MIDIControllable and remove it from the pending list.
+ */
+
+ for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
+ if (((*i)->mc)->get_controllable() == c) {
+ (*i)->mc->stop_learning ();
+ dptr = (*i)->mc;
+ (*i)->connection.disconnect();
+
+ delete *i;
+ pending_controllables.erase (i);
+ break;
+ }
+ }
+
+ delete dptr;
+}
+
+void
+GenericMidiControlProtocol::delete_binding (PBD::Controllable* control)
+{
+ if (control != 0) {
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+
+ for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end();) {
+ MIDIControllable* existingBinding = (*iter);
+
+ if (control == (existingBinding->get_controllable())) {
+ delete existingBinding;
+ iter = controllables.erase (iter);
+ } else {
+ ++iter;
+ }
+
+ }
+ }
+}
+
+// This next function seems unused
+void
+GenericMidiControlProtocol::create_binding (PBD::Controllable* control, int pos, int control_number)
+{
+ if (control != NULL) {
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+
+ MIDI::channel_t channel = (pos & 0xf);
+ MIDI::byte value = control_number;
+
+ // Create a MIDIControllable
+ MIDIControllable* mc = new MIDIControllable (this, *_input_port->parser(), *control, false);
+
+ // Remove any old binding for this midi channel/type/value pair
+ // Note: can't use delete_binding() here because we don't know the specific controllable we want to remove, only the midi information
+ for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end();) {
+ MIDIControllable* existingBinding = (*iter);
+
+ if ((existingBinding->get_control_channel() & 0xf ) == channel &&
+ existingBinding->get_control_additional() == value &&
+ (existingBinding->get_control_type() & 0xf0 ) == MIDI::controller) {
+
+ delete existingBinding;
+ iter = controllables.erase (iter);
+ } else {
+ ++iter;
+ }
+
+ }
+
+ // Update the MIDI Controllable based on the the pos param
+ // Here is where a table lookup for user mappings could go; for now we'll just wing it...
+ mc->bind_midi(channel, MIDI::controller, value);
+ DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Create binding: Channel: %1 Controller: %2 Value: %3 \n", channel, MIDI::controller, value));
+ controllables.push_back (mc);
+ }
+}
+
+void
+GenericMidiControlProtocol::check_used_event (int pos, int control_number)
+{
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+
+ MIDI::channel_t channel = (pos & 0xf);
+ MIDI::byte value = control_number;
+
+ DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("checking for used event: Channel: %1 Controller: %2 value: %3\n", (int) channel, (pos & 0xf0), (int) value));
+
+ // Remove any old binding for this midi channel/type/value pair
+ // Note: can't use delete_binding() here because we don't know the specific controllable we want to remove, only the midi information
+ for (MIDIControllables::iterator iter = controllables.begin(); iter != controllables.end();) {
+ MIDIControllable* existingBinding = (*iter);
+ if ( (existingBinding->get_control_type() & 0xf0 ) == (pos & 0xf0) && (existingBinding->get_control_channel() & 0xf ) == channel ) {
+ if ( ((int) existingBinding->get_control_additional() == (int) value) || ((pos & 0xf0) == MIDI::pitchbend)) {
+ DEBUG_TRACE (DEBUG::GenericMidi, "checking: found match, delete old binding.\n");
+ delete existingBinding;
+ iter = controllables.erase (iter);
+ } else {
+ ++iter;
+ }
+ } else {
+ ++iter;
+ }
+ }
+
+ for (MIDIFunctions::iterator iter = functions.begin(); iter != functions.end();) {
+ MIDIFunction* existingBinding = (*iter);
+ if ( (existingBinding->get_control_type() & 0xf0 ) == (pos & 0xf0) && (existingBinding->get_control_channel() & 0xf ) == channel ) {
+ if ( ((int) existingBinding->get_control_additional() == (int) value) || ((pos & 0xf0) == MIDI::pitchbend)) {
+ DEBUG_TRACE (DEBUG::GenericMidi, "checking: found match, delete old binding.\n");
+ delete existingBinding;
+ iter = functions.erase (iter);
+ } else {
+ ++iter;
+ }
+ } else {
+ ++iter;
+ }
+ }
+
+ for (MIDIActions::iterator iter = actions.begin(); iter != actions.end();) {
+ MIDIAction* existingBinding = (*iter);
+ if ( (existingBinding->get_control_type() & 0xf0 ) == (pos & 0xf0) && (existingBinding->get_control_channel() & 0xf ) == channel ) {
+ if ( ((int) existingBinding->get_control_additional() == (int) value) || ((pos & 0xf0) == MIDI::pitchbend)) {
+ DEBUG_TRACE (DEBUG::GenericMidi, "checking: found match, delete old binding.\n");
+ delete existingBinding;
+ iter = actions.erase (iter);
+ } else {
+ ++iter;
+ }
+ } else {
+ ++iter;
+ }
+ }
+
+}
+
+XMLNode&
+GenericMidiControlProtocol::get_state ()
+{
+ XMLNode& node (ControlProtocol::get_state());
+ char buf[32];
+
+ snprintf (buf, sizeof (buf), "%" PRIu64, _feedback_interval);
+ node.add_property (X_("feedback_interval"), buf);
+ snprintf (buf, sizeof (buf), "%d", _threshold);
+ node.add_property (X_("threshold"), buf);
+
+ node.add_property (X_("motorized"), _motorised ? "yes" : "no");
+
+ if (!_current_binding.empty()) {
+ node.add_property ("binding", _current_binding);
+ }
+
+ XMLNode* children = new XMLNode (X_("Controls"));
+
+ node.add_child_nocopy (*children);
+
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+ for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
+
+ /* we don't care about bindings that come from a bindings map, because
+ they will all be reset/recreated when we load the relevant bindings
+ file.
+ */
+
+ if ((*i)->get_controllable() && (*i)->learned()) {
+ children->add_child_nocopy ((*i)->get_state());
+ }
+ }
+
+ return node;
+}
+
+int
+GenericMidiControlProtocol::set_state (const XMLNode& node, int version)
+{
+ XMLNodeList nlist;
+ XMLNodeConstIterator niter;
+ const XMLProperty* prop;
+
+ if (ControlProtocol::set_state (node, version)) {
+ return -1;
+ }
+
+ if ((prop = node.property ("feedback_interval")) != 0) {
+ if (sscanf (prop->value().c_str(), "%" PRIu64, &_feedback_interval) != 1) {
+ _feedback_interval = 10000;
+ }
+ } else {
+ _feedback_interval = 10000;
+ }
+
+ if ((prop = node.property ("threshold")) != 0) {
+ if (sscanf (prop->value().c_str(), "%d", &_threshold) != 1) {
+ _threshold = 10;
+ }
+ } else {
+ _threshold = 10;
+ }
+
+ if ((prop = node.property ("motorized")) != 0) {
+ _motorised = string_is_affirmative (prop->value ());
+ } else {
+ _motorised = false;
+ }
+
+ boost::shared_ptr<Controllable> c;
+
+ {
+ Glib::Threads::Mutex::Lock lm (pending_lock);
+ for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
+ delete *i;
+ }
+ pending_controllables.clear ();
+ }
+
+ // midi map has to be loaded first so learned binding can go on top
+ if ((prop = node.property ("binding")) != 0) {
+ for (list<MapInfo>::iterator x = map_info.begin(); x != map_info.end(); ++x) {
+ if (prop->value() == (*x).name) {
+ load_bindings ((*x).path);
+ break;
+ }
+ }
+ }
+
+ /* Load up specific bindings from the
+ * <Controls><MidiControllable>...</MidiControllable><Controls> section
+ */
+
+ {
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+ nlist = node.children(); // "Controls"
+
+ if (!nlist.empty()) {
+ nlist = nlist.front()->children(); // "MIDIControllable" ...
+
+ if (!nlist.empty()) {
+ for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
+
+ if ((prop = (*niter)->property ("id")) != 0) {
+
+ ID id = prop->value ();
+ DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Relearned binding for session: Control ID: %1\n", id.to_s()));
+ Controllable* c = Controllable::by_id (id);
+
+ if (c) {
+ MIDIControllable* mc = new MIDIControllable (this, *_input_port->parser(), *c, false);
+
+ if (mc->set_state (**niter, version) == 0) {
+ controllables.push_back (mc);
+ }
+
+ } else {
+ warning << string_compose (
+ _("Generic MIDI control: controllable %1 not found in session (ignored)"),
+ id.to_s()) << endmsg;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+int
+GenericMidiControlProtocol::set_feedback (bool yn)
+{
+ do_feedback = yn;
+ last_feedback_time = 0;
+ return 0;
+}
+
+bool
+GenericMidiControlProtocol::get_feedback () const
+{
+ return do_feedback;
+}
+
+int
+GenericMidiControlProtocol::load_bindings (const string& xmlpath)
+{
+ DEBUG_TRACE (DEBUG::GenericMidi, "Load bindings: Reading midi map\n");
+ 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, µ);
+ Stateful::loading_state_version = (major * 1000) + minor;
+ }
+
+ const XMLNodeList& children (root->children());
+ XMLNodeConstIterator citer;
+ XMLNodeConstIterator gciter;
+
+ MIDIControllable* mc;
+
+ drop_all ();
+
+ DEBUG_TRACE (DEBUG::GenericMidi, "Loading bindings\n");
+ for (citer = children.begin(); citer != children.end(); ++citer) {
+
+ if ((*citer)->name() == "DeviceInfo") {
+ const XMLProperty* prop;
+
+ if ((prop = (*citer)->property ("bank-size")) != 0) {
+ _bank_size = atoi (prop->value());
+ _current_bank = 0;
+ }
+
+ if ((prop = (*citer)->property ("motorized")) != 0) {
+ _motorised = string_is_affirmative (prop->value ());
+ } else {
+ _motorised = false;
+ }
+
+ if ((prop = (*citer)->property ("threshold")) != 0) {
+ _threshold = atoi (prop->value ());
+ } else {
+ _threshold = 10;
+ }
+
+ }
+
+ if ((*citer)->name() == "Binding") {
+ const XMLNode* child = *citer;
+
+ if (child->property ("uri")) {
+ /* controllable */
+
+ if ((mc = create_binding (*child)) != 0) {
+ Glib::Threads::Mutex::Lock lm2 (controllables_lock);
+ controllables.push_back (mc);
+ }
+
+ } else if (child->property ("function")) {
+
+ /* function */
+ MIDIFunction* mf;
+
+ if ((mf = create_function (*child)) != 0) {
+ functions.push_back (mf);
+ }
+
+ } else if (child->property ("action")) {
+ MIDIAction* ma;
+
+ if ((ma = create_action (*child)) != 0) {
+ actions.push_back (ma);
+ }
+ }