From 2983f8de97412b4887394747abed59fa11a11cff Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 11 Dec 2009 02:00:22 +0000 Subject: [PATCH] (MAY BREAK COMPILATION - unfinished work, being transferred between machines via svn) ... start to make MidiControllable bind to a URI, not an object git-svn-id: svn://localhost/ardour2/branches/3.0@6346 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/pbd/controllable.cc | 82 ++++++++++++++++--- libs/pbd/pbd/controllable.h | 13 ++- .../surfaces/generic_midi/midicontrollable.cc | 79 +++++++++++++----- libs/surfaces/generic_midi/midicontrollable.h | 11 ++- 4 files changed, 147 insertions(+), 38 deletions(-) diff --git a/libs/pbd/controllable.cc b/libs/pbd/controllable.cc index a1cd650540..fbbb4d6f25 100644 --- a/libs/pbd/controllable.cc +++ b/libs/pbd/controllable.cc @@ -31,44 +31,82 @@ sigc::signal Controllable::StopLearning; sigc::signal Controllable::CreateBinding; sigc::signal Controllable::DeleteBinding; -Glib::Mutex* Controllable::registry_lock = 0; +Glib::StaticRWLock Controllable::registry_lock = GLIBMM_STATIC_RW_LOCK_INIT; Controllable::Controllables Controllable::registry; +Controllable::ControllablesByURI Controllable::registry_by_uri; -Controllable::Controllable (std::string name) +Controllable::Controllable (const std::string& name, const std::string& uri) : _name (name) + , _uri (uri) , _touching (false) { - if (registry_lock == 0) { - registry_lock = new Glib::Mutex; - } - add (); } void Controllable::add () { - Glib::Mutex::Lock lm (*registry_lock); + Glib::RWLock::WriterLock lm (*registry_lock); registry.insert (this); + + if (!_uri.empty()) { + pair newpair; + newpair.first = _uri; + newpair.second = this; + registry_by_uri.insert (newpair); + } + this->GoingAway.connect (mem_fun (this, &Controllable::remove)); } void Controllable::remove () { - Glib::Mutex::Lock lm (*registry_lock); + Glib::RWLock::WriterLock lm (*registry_lock); + for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) { if ((*i) == this) { registry.erase (i); break; } } + + if (_uri) { + ControllablesByURI::iterator i = registry_by_uri.find (_uri); + if (i != registry_by_uri.end()) { + registry_by_uri.erase (i); + } + } + + +} + +void +Controllable::set_uri (const std::string& new_uri) +{ + Glib::RWLock::WriterLock lm (*registry_lock); + + if (_uri) { + ControllablesByURI::iterator i = registry_by_uri.find (_uri); + if (i != registry_by_uri.end()) { + registry_by_uri.erase (i); + } + } + + _uri = new_uri; + + if (!_uri.empty()) { + pair newpair; + newpair.first = _uri; + newpair.second = this; + registry_by_uri.insert (newpair); + } } Controllable* Controllable::by_id (const ID& id) { - Glib::Mutex::Lock lm (*registry_lock); + Glib::RWLock::ReaderLock lm (*registry_lock); for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) { if ((*i)->id() == id) { @@ -78,11 +116,22 @@ Controllable::by_id (const ID& id) return 0; } +Controllable* +Controllable::by_uri (const string& uri) +{ + Glib::RWLock::ReaderLock lm (*registry_lock); + ControllablesByURI::iterator i; + + if ((i = registry_by_ui.find (uri)) != registry_by_uri.end()) { + return i->second; + } + return 0; +} Controllable* Controllable::by_name (const std::string& str) { - Glib::Mutex::Lock lm (*registry_lock); + Glib::RWLock::ReaderLock lm (*registry_lock); for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) { if ((*i)->_name == str) { @@ -101,19 +150,28 @@ Controllable::get_state () node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at _id.print (buf, sizeof (buf)); node->add_property (X_("id"), buf); + + if (!_uri.empty()) { + node->add_property (X_("uri"), _uri); + } + return *node; } int Controllable::set_state (const XMLNode& node, int /*version*/) { - const XMLProperty* prop = node.property (X_("id")); + const XMLProperty* prop; - if (prop) { + if ((prop = node.property (X_("id"))) != 0) { _id = prop->value(); return 0; } else { error << _("Controllable state node has no ID property") << endmsg; return -1; } + + if ((prop = node.property (X_("uri"))) != 0) { + set_uri (prop->value()); + } } diff --git a/libs/pbd/pbd/controllable.h b/libs/pbd/pbd/controllable.h index 1ab649f8b9..76202d7bdb 100644 --- a/libs/pbd/pbd/controllable.h +++ b/libs/pbd/pbd/controllable.h @@ -2,7 +2,7 @@ Copyright (C) 2000-2007 Paul Davis This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by +v it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. @@ -34,9 +34,12 @@ namespace PBD { class Controllable : public PBD::StatefulDestructible { public: - Controllable (std::string name); + Controllable (std::string name, const std::string& uri); virtual ~Controllable() { Destroyed (this); } + void set_uri (const std::string&); + const std::string& uri() const { return _uri; } + virtual void set_value (float) = 0; virtual float get_value (void) const = 0; @@ -61,17 +64,21 @@ class Controllable : public PBD::StatefulDestructible { static Controllable* by_id (const PBD::ID&); static Controllable* by_name (const std::string&); + static Controllable* by_uri (const std::string&); private: std::string _name; + std::string _uri; bool _touching; void add (); void remove (); typedef std::set Controllables; - static Glib::Mutex* registry_lock; + typedef std::map ControllablesByURI; + static Glib::StaticRWLock registry_lock; static Controllables registry; + static ControllablesByURI registry_by_uri; }; /* a utility class for the occasions when you need but do not have diff --git a/libs/surfaces/generic_midi/midicontrollable.cc b/libs/surfaces/generic_midi/midicontrollable.cc index 92000a6dee..8b2077fb5b 100644 --- a/libs/surfaces/generic_midi/midicontrollable.cc +++ b/libs/surfaces/generic_midi/midicontrollable.cc @@ -32,8 +32,25 @@ using namespace MIDI; using namespace PBD; using namespace ARDOUR; +MIDIControllable::MIDIControllable (Port& p, const string& c, bool is_bistate) + : controllable (0), _port (p), bistate (is_bistate) +{ + init (); +} + MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate) - : controllable (c), _port (p), bistate (is_bistate) + : controllable (&c), _current_uri (c.uri()), _port (p), bistate (is_bistate) +{ + init (); +} + +MIDIControllable::~MIDIControllable () +{ + drop_external_control (); +} + +void +MIDIControllable::init () { setting = false; last_value = 0; // got a better idea ? @@ -48,11 +65,6 @@ MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate) midi_rebind (0); } -MIDIControllable::~MIDIControllable () -{ - drop_external_control (); -} - void MIDIControllable::midi_forget () { @@ -73,6 +85,12 @@ MIDIControllable::midi_forget () } +void +MIDIControllable::reacquire_controllable () +{ + _controllable = Controllable::controllable_by_uri (current_uri); +} + void MIDIControllable::midi_rebind (channel_t c) { @@ -162,8 +180,12 @@ MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb) void MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on) { + if (!controllable) { + return; + } + if (!bistate) { - controllable.set_value (msg->note_number/127.0); + controllable->set_value (msg->note_number/127.0); } else { /* Note: parser handles the use of zero velocity to @@ -176,51 +198,62 @@ MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on) } } - last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights + last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights } void MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg) { - if (controllable.touching()) { + if (!controllable) { + return; + } + + if (controllable->touching()) { return; // to prevent feedback fights when e.g. dragging a UI slider } if (control_additional == msg->controller_number) { if (!bistate) { - controllable.set_value (midi_to_control(msg->value)); + controllable->set_value (midi_to_control(msg->value)); } else { if (msg->value > 64.0) { - controllable.set_value (1); + controllable->set_value (1); } else { - controllable.set_value (0); + controllable->set_value (0); } } - last_value = (MIDI::byte) (control_to_midi(controllable.get_value())); // to prevent feedback fights + last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights } } void MIDIControllable::midi_sense_program_change (Parser &, byte msg) { + if (!controllable) { + return; + } /* XXX program change messages make no sense for bistates */ if (!bistate) { - controllable.set_value (msg/127.0); - last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights + controllable->set_value (msg/127.0); + last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights } } void MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb) { + if (!controllable) { + return; + } + /* pitchbend messages make no sense for bistates */ /* XXX gack - get rid of assumption about typeof pitchbend_t */ - controllable.set_value ((pb/(float) SHRT_MAX)); - last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights + controllable->set_value ((pb/(float) SHRT_MAX)); + last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights } void @@ -240,7 +273,7 @@ MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/) bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]); - controllable.LearningFinished (); + controllable->LearningFinished (); } void @@ -336,7 +369,7 @@ MIDIControllable::send_feedback () msg[0] = (control_type & 0xF0) | (control_channel & 0xF); msg[1] = control_additional; - msg[2] = (byte) (control_to_midi(controllable.get_value())); + msg[2] = (byte) (control_to_midi(controllable->get_value())); _port.write (msg, 3, 0); } @@ -346,7 +379,7 @@ MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*forc { if (control_type != none && feedback && bufsize > 2) { - MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable.get_value())); + MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable->get_value())); if (gm != last_value) { *buf++ = (0xF0 & control_type) | (0xF & control_channel); @@ -401,8 +434,12 @@ MIDIControllable::set_state (const XMLNode& node, int /*version*/) XMLNode& MIDIControllable::get_state () { + if (!controllable) { + return XXX !what!; + } + char buf[32]; - XMLNode& node (controllable.get_state ()); + XMLNode& node (controllable->get_state ()); snprintf (buf, sizeof(buf), "0x%x", (int) control_type); node.add_property ("event", buf); diff --git a/libs/surfaces/generic_midi/midicontrollable.h b/libs/surfaces/generic_midi/midicontrollable.h index 4aabd696b0..7b84c9b60a 100644 --- a/libs/surfaces/generic_midi/midicontrollable.h +++ b/libs/surfaces/generic_midi/midicontrollable.h @@ -41,8 +41,11 @@ class MIDIControllable : public PBD::Stateful { public: MIDIControllable (MIDI::Port&, PBD::Controllable&, bool bistate = false); + MIDIControllable (MIDI::Port&, const std::string& uri, bool bistate = false); virtual ~MIDIControllable (); + bool ok() const { return !_current_uri.empty(); } + void send_feedback (); MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force = false); @@ -59,7 +62,7 @@ class MIDIControllable : public PBD::Stateful float midi_to_control(float val); MIDI::Port& get_port() const { return _port; } - PBD::Controllable& get_controllable() const { return controllable; } + PBD::Controllable* get_controllable() const { return controllable; } std::string control_description() const { return _control_description; } @@ -70,8 +73,10 @@ class MIDIControllable : public PBD::Stateful MIDI::channel_t get_control_channel () { return control_channel; } MIDI::eventType get_control_type () { return control_type; } MIDI::byte get_control_additional () { return control_additional; } + private: - PBD::Controllable& controllable; + PBD::Controllable* controllable; + std::string _current_uri; MIDI::Port& _port; bool setting; MIDI::byte last_value; @@ -85,6 +90,8 @@ class MIDIControllable : public PBD::Stateful MIDI::channel_t control_channel; std::string _control_description; bool feedback; + + void reacquire_controllable (); void midi_receiver (MIDI::Parser &p, MIDI::byte *, size_t); void midi_sense_note (MIDI::Parser &, MIDI::EventTwoBytes *, bool is_on);