visibility macros and flush() added to SrcFileSource; merge with master
[ardour.git] / libs / midi++2 / midnam_patch.cc
index bb9e27ebff980cef85a6364bbdab9ad938da746d..252804e4669857bf0b0e90c2d1d7bc7a02851b3f 100644 (file)
@@ -240,13 +240,33 @@ Control::get_state (void)
 }
 
 int
-Control::set_state (const XMLTree&, const XMLNode& node)
+Control::set_state (const XMLTree& tree, const XMLNode& node)
 {
        assert(node.name() == "Control");
-       _type   = node.property("Type")->value();
-       _number = node.property("Number")->value();
+       if (node.property("Type")) {
+               _type = node.property("Type")->value();
+       } else {
+               _type = "7bit";
+       }
+       _number = string_to_int(tree, node.property("Number")->value());
        _name   = node.property("Name")->value();
 
+       for (XMLNodeList::const_iterator i = node.children().begin();
+            i != node.children().end(); ++i) {
+               if ((*i)->name() == "Values") {
+                       // <Values> has Min and Max properties, but we don't care
+                       for (XMLNodeList::const_iterator j = (*i)->children().begin();
+                            j != (*i)->children().end(); ++j) {
+                               if ((*j)->name() == "ValueNameList") {
+                                       _value_name_list = boost::shared_ptr<ValueNameList>(new ValueNameList());
+                                       _value_name_list->set_state(tree, **j);
+                               } else if ((*j)->name() == "UsesValueNameList") {
+                                       _value_name_list_name = (*j)->property("Name")->value();
+                               }
+                       }
+               }
+       }
+
        return 0;
 }
 
@@ -265,18 +285,120 @@ ControlNameList::set_state (const XMLTree& tree, const XMLNode& node)
        assert(node.name() == "ControlNameList");
        _name = node.property("Name")->value();
 
+       _controls.clear();
        for (XMLNodeList::const_iterator i = node.children().begin();
             i != node.children().end(); ++i) {
-               if ((*i)->name() != "comment") {
+               if ((*i)->name() == "Control") {
                        boost::shared_ptr<Control> control(new Control());
                        control->set_state (tree, *(*i));
-                       _controls.push_back(control);
+                       if (_controls.find(control->number()) == _controls.end()) {
+                               _controls.insert(make_pair(control->number(), control));
+                       } else {
+                               PBD::warning << string_compose("%1: Duplicate control %2 ignored",
+                                                              tree.filename(), control->number())
+                                            << endmsg;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+boost::shared_ptr<const Control>
+ControlNameList::control(uint16_t num) const
+{
+       Controls::const_iterator i = _controls.find(num);
+       if (i != _controls.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<const Control>();
+}
+
+XMLNode&
+Value::get_state (void)
+{
+       XMLNode* node = new XMLNode("Value");
+       node->add_property("Number", _number);
+       node->add_property("Name",   _name);
+
+       return *node;
+}
+
+int
+Value::set_state (const XMLTree& tree, const XMLNode& node)
+{
+       assert(node.name() == "Value");
+       _number = string_to_int(tree, node.property("Number")->value());
+       _name   = node.property("Name")->value();
+
+       return 0;
+}
+
+XMLNode&
+ValueNameList::get_state (void)
+{
+       XMLNode* node = new XMLNode("ValueNameList");
+       node->add_property("Name", _name);
+
+       return *node;
+}
+
+int
+ValueNameList::set_state (const XMLTree& tree, const XMLNode& node)
+{
+       assert(node.name() == "ValueNameList");
+       const XMLProperty* name_prop = node.property("Name");
+       if (name_prop) {
+               // May be anonymous if written inline within a single <Control> tag
+               _name = name_prop->value();
+       }
+
+       _values.clear();
+       for (XMLNodeList::const_iterator i = node.children().begin();
+            i != node.children().end(); ++i) {
+               if ((*i)->name() == "Value") {
+                       boost::shared_ptr<Value> value(new Value());
+                       value->set_state (tree, *(*i));
+                       if (_values.find(value->number()) == _values.end()) {
+                               _values.insert(make_pair(value->number(), value));
+                       } else {
+                               PBD::warning << string_compose("%1: Duplicate value %2 ignored",
+                                                              tree.filename(), value->number())
+                                            << endmsg;
+                       }
                }
        }
 
        return 0;
 }
 
+boost::shared_ptr<const Value>
+ValueNameList::value(uint16_t num) const
+{
+       Values::const_iterator i = _values.find(num);
+       if (i != _values.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<const Value>();
+}
+
+boost::shared_ptr<const Value>
+ValueNameList::max_value_below(uint16_t num) const
+{
+       Values::const_iterator i = _values.lower_bound(num);
+       if (i->first == num) {
+               // Exact match
+               return i->second;
+       } else if (i == _values.begin()) {
+               // No value is < num
+               return boost::shared_ptr<const Value>();
+       } else {
+               // Found the smallest element >= num, so the previous one is our result
+               --i;
+               return i->second;
+       }
+}
+
 XMLNode&
 PatchBank::get_state (void)
 {
@@ -350,14 +472,14 @@ operator<< (std::ostream& os, const ChannelNameSet& cns)
           << "List size " << cns._patch_list.size() << endl
           << "Patch list name = [" << cns._patch_list_name << ']' << endl
           << "Available channels : ";
-       for (set<uint8_t>::iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
+       for (set<uint8_t>::const_iterator x = cns._available_for_channels.begin(); x != cns._available_for_channels.end(); ++x) {
                os << (int) (*x) << ' ';
        }
        os << endl;
        
        for (ChannelNameSet::PatchBanks::const_iterator pbi = cns._patch_banks.begin(); pbi != cns._patch_banks.end(); ++pbi) {
                os << "\tPatch Bank " << (*pbi)->name() << " with " << (*pbi)->patch_name_list().size() << " patches\n";
-               for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
+               for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
                        os << "\t\tPatch name " << (*pni)->name() << " prog " << (int) (*pni)->program_number() << " bank " << (*pni)->bank_number() << endl;
                }
        }
@@ -376,7 +498,7 @@ ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
        _available_for_channels.clear ();
        
        for (PatchBanks::const_iterator pbi = _patch_banks.begin(); pbi != _patch_banks.end(); ++pbi) {
-               for (PatchBank::PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
+               for (PatchNameList::const_iterator pni = (*pbi)->patch_name_list().begin(); pni != (*pbi)->patch_name_list().end(); ++pni) {
                        _patch_map[(*pni)->patch_primary_key()] = (*pni);
                        _patch_list.push_back ((*pni)->patch_primary_key());
                }
@@ -388,9 +510,9 @@ ChannelNameSet::set_patch_banks (const ChannelNameSet::PatchBanks& pb)
 }
 
 void
-ChannelNameSet::use_patch_name_list (const PatchBank::PatchNameList& pnl)
+ChannelNameSet::use_patch_name_list (const PatchNameList& pnl)
 {
-       for (PatchBank::PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
+       for (PatchNameList::const_iterator p = pnl.begin(); p != pnl.end(); ++p) {
                _patch_map[(*p)->patch_primary_key()] = (*p);
                _patch_list.push_back ((*p)->patch_primary_key());
        }
@@ -431,7 +553,7 @@ int
 ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
 {
        assert(node.name() == "ChannelNameSet");
-       _name   = node.property("Name")->value();
+       _name = node.property("Name")->value();
 
        const XMLNodeList children = node.children();
        for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
@@ -440,29 +562,27 @@ ChannelNameSet::set_state (const XMLTree& tree, const XMLNode& node)
                if (node->name() == "AvailableForChannels") {
                        boost::shared_ptr<XMLSharedNodeList> channels =
                                tree.find("//AvailableChannel[@Available = 'true']/@Channel", node);
-                       for(XMLSharedNodeList::const_iterator i = channels->begin();
+                       for (XMLSharedNodeList::const_iterator i = channels->begin();
                            i != channels->end();
                            ++i) {
                                _available_for_channels.insert(
                                        string_to_int(tree, (*i)->attribute_value()));
                        }
-               }
-
-               if (node->name() == "PatchBank") {
+               } else if (node->name() == "PatchBank") {
                        boost::shared_ptr<PatchBank> bank (new PatchBank ());
                        bank->set_state(tree, *node);
                        _patch_banks.push_back(bank);
-                       const PatchBank::PatchNameList& patches = bank->patch_name_list();
-                       for (PatchBank::PatchNameList::const_iterator patch = patches.begin();
+                       const PatchNameList& patches = bank->patch_name_list();
+                       for (PatchNameList::const_iterator patch = patches.begin();
                             patch != patches.end();
                             ++patch) {
                                _patch_map[(*patch)->patch_primary_key()] = *patch;
                                _patch_list.push_back((*patch)->patch_primary_key());
                        }
-               }
-
-               if (node->name() == "UsesNoteNameList") {
+               } else if (node->name() == "UsesNoteNameList") {
                        _note_list_name = node->property ("Name")->value();
+               } else if (node->name() == "UsesControlNameList") {
+                       _control_list_name = node->property ("Name")->value();
                }
        }
 
@@ -478,7 +598,7 @@ CustomDeviceMode::set_state(const XMLTree& tree, const XMLNode& a_node)
 
        boost::shared_ptr<XMLSharedNodeList> channel_name_set_assignments =
                tree.find("//ChannelNameSetAssign", const_cast<XMLNode *>(&a_node));
-       for(XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
+       for (XMLSharedNodeList::const_iterator i = channel_name_set_assignments->begin();
            i != channel_name_set_assignments->end();
            ++i) {
                const int     channel  = string_to_int(tree, (*i)->property("Channel")->value());
@@ -506,6 +626,31 @@ CustomDeviceMode::get_state(void)
        return *custom_device_mode;
 }
 
+boost::shared_ptr<const ValueNameList>
+MasterDeviceNames::value_name_list_by_control(const std::string& mode, uint8_t channel, uint8_t number)
+{
+       boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(mode, channel);
+       if (!chan_names) {
+               return boost::shared_ptr<const ValueNameList>();
+       }
+
+       boost::shared_ptr<ControlNameList> control_names = control_name_list(chan_names->control_list_name());
+       if (!control_names) {
+               return boost::shared_ptr<const ValueNameList>();
+       }
+
+       boost::shared_ptr<const Control> control = control_names->control(number);
+       if (!control) {
+               return boost::shared_ptr<const ValueNameList>();
+       }
+
+       if (!control->value_name_list_name().empty()) {
+               return value_name_list(control->value_name_list_name());
+       } else {
+               return control->value_name_list();
+       }
+}
+
 boost::shared_ptr<CustomDeviceMode> 
 MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
 {
@@ -513,7 +658,7 @@ MasterDeviceNames::custom_device_mode_by_name(const std::string& mode_name)
 }
 
 boost::shared_ptr<ChannelNameSet> 
-MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string& mode, uint8_t channel)
+MasterDeviceNames::channel_name_set_by_channel(const std::string& mode, uint8_t channel)
 {
        boost::shared_ptr<CustomDeviceMode> cdm = custom_device_mode_by_name(mode);
        boost::shared_ptr<ChannelNameSet> cns =  _channel_name_sets[cdm->channel_name_set_name_by_channel(channel)];
@@ -523,7 +668,9 @@ MasterDeviceNames::channel_name_set_by_device_mode_and_channel(const std::string
 boost::shared_ptr<Patch> 
 MasterDeviceNames::find_patch(const std::string& mode, uint8_t channel, const PatchPrimaryKey& key) 
 {
-       return channel_name_set_by_device_mode_and_channel(mode, channel)->find_patch(key);
+       boost::shared_ptr<ChannelNameSet> cns = channel_name_set_by_channel(mode, channel);
+       if (!cns) return boost::shared_ptr<Patch>();
+       return cns->find_patch(key);
 }
 
 boost::shared_ptr<ChannelNameSet>
@@ -536,6 +683,26 @@ MasterDeviceNames::channel_name_set(const std::string& name)
        return boost::shared_ptr<ChannelNameSet>();
 }
 
+boost::shared_ptr<ControlNameList>
+MasterDeviceNames::control_name_list(const std::string& name)
+{
+       ControlNameLists::const_iterator i = _control_name_lists.find(name);
+       if (i != _control_name_lists.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<ControlNameList>();
+}
+
+boost::shared_ptr<ValueNameList>
+MasterDeviceNames::value_name_list(const std::string& name)
+{
+       ValueNameLists::const_iterator i = _value_name_lists.find(name);
+       if (i != _value_name_lists.end()) {
+               return i->second;
+       }
+       return boost::shared_ptr<ValueNameList>();
+}
+
 boost::shared_ptr<NoteNameList>
 MasterDeviceNames::note_name_list(const std::string& name)
 {
@@ -567,7 +734,7 @@ MasterDeviceNames::note_name(const std::string& mode_name,
                note_name_list(patch->note_list_name()));
        if (!note_names) {
                /* No note names specific to this patch, check the ChannelNameSet */
-               boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_device_mode_and_channel(
+               boost::shared_ptr<ChannelNameSet> chan_names = channel_name_set_by_channel(
                        mode_name, channel);
                if (chan_names) {
                        note_names = note_name_list(chan_names->note_list_name());
@@ -631,7 +798,7 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
             ++i) {
                boost::shared_ptr<NoteNameList> note_name_list(new NoteNameList());
                note_name_list->set_state (tree, *(*i));
-               _note_name_lists[(*i)->property ("Name")->value()] = note_name_list;
+               _note_name_lists[note_name_list->name()] = note_name_list;
        }
 
        // ControlNameLists
@@ -641,7 +808,17 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
             ++i) {
                boost::shared_ptr<ControlNameList> control_name_list(new ControlNameList());
                control_name_list->set_state (tree, *(*i));
-               _control_name_lists.push_back(control_name_list);
+               _control_name_lists[control_name_list->name()] = control_name_list;
+       }
+
+       // ValueNameLists
+       boost::shared_ptr<XMLSharedNodeList> value_name_lists = tree.find("/child::MIDINameDocument/child::MasterDeviceNames/child::ValueNameList");
+       for (XMLSharedNodeList::iterator i = value_name_lists->begin();
+            i != value_name_lists->end();
+            ++i) {
+               boost::shared_ptr<ValueNameList> value_name_list(new ValueNameList());
+               value_name_list->set_state (tree, *(*i));
+               _value_name_lists[value_name_list->name()] = value_name_list;
        }
 
        // global/post-facto PatchNameLists
@@ -650,7 +827,7 @@ MasterDeviceNames::set_state(const XMLTree& tree, const XMLNode&)
             i != patch_name_lists->end();
             ++i) {
 
-               PatchBank::PatchNameList patch_name_list;
+               PatchNameList patch_name_list;
                const XMLNodeList patches = (*i)->children();
 
                for (XMLNodeList::const_iterator p = patches.begin(); p != patches.end(); ++p) {