changes associated with save/restore of AutomationControl id's
[ardour.git] / libs / pbd / controllable.cc
1 /*
2     Copyright (C) 2000-2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/controllable.h"
21 #include "pbd/enumwriter.h"
22 #include "pbd/xml++.h"
23 #include "pbd/error.h"
24
25 #include "i18n.h"
26
27 using namespace PBD;
28 using namespace std;
29
30 PBD::Signal1<void,Controllable*> Controllable::Destroyed;
31 PBD::Signal1<bool,Controllable*> Controllable::StartLearning;
32 PBD::Signal1<void,Controllable*> Controllable::StopLearning;
33 PBD::Signal3<void,Controllable*,int,int> Controllable::CreateBinding;
34 PBD::Signal1<void,Controllable*> Controllable::DeleteBinding;
35
36 Glib::StaticRWLock Controllable::registry_lock = GLIBMM_STATIC_RW_LOCK_INIT;
37 Controllable::Controllables Controllable::registry;
38 PBD::ScopedConnectionList registry_connections;
39 const std::string Controllable::xml_node_name = X_("Controllable");
40
41 Controllable::Controllable (const string& name, Flag f)
42         : _name (name)
43         , _flags (f)
44         , _touching (false)
45 {
46         add (*this);
47 }
48
49 void
50 Controllable::add (Controllable& ctl)
51 {
52         using namespace boost;
53
54         Glib::RWLock::WriterLock lm (registry_lock);
55         registry.insert (&ctl);
56
57         /* Controllable::remove() is static - no need to manage this connection */
58
59         ctl.DropReferences.connect_same_thread (registry_connections, boost::bind (&Controllable::remove, &ctl));
60 }
61
62 void
63 Controllable::remove (Controllable* ctl)
64 {
65         Glib::RWLock::WriterLock lm (registry_lock);
66
67         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
68                 if ((*i) == ctl) {
69                         registry.erase (i);
70                         break;
71                 }
72         }
73 }
74
75 Controllable*
76 Controllable::by_id (const ID& id)
77 {
78         Glib::RWLock::ReaderLock lm (registry_lock);
79
80         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
81                 if ((*i)->id() == id) {
82                         return (*i);
83                 }
84         }
85         return 0;
86 }
87
88 Controllable*
89 Controllable::by_name (const string& str)
90 {
91         Glib::RWLock::ReaderLock lm (registry_lock);
92
93         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
94                 if ((*i)->_name == str) {
95                         return (*i);
96                 }
97         }
98         return 0;
99 }
100
101 XMLNode&
102 Controllable::get_state ()
103 {
104         XMLNode* node = new XMLNode (xml_node_name);
105         char buf[64];
106
107         node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
108         _id.print (buf, sizeof (buf));
109         node->add_property (X_("id"), buf);
110         node->add_property (X_("flags"), enum_2_string (_flags));
111         snprintf (buf, sizeof (buf), "%2.12f", get_value());
112         node->add_property (X_("value"), buf);
113
114         return *node;
115 }
116
117 int
118 Controllable::set_state (const XMLNode& node, int /*version*/)
119 {
120         const XMLProperty* prop;
121
122         if ((prop = node.property (X_("id"))) != 0) {
123                 _id = prop->value();
124                 return 0;
125         } else {
126                 error << _("Controllable state node has no ID property") << endmsg;
127                 return -1;
128         }
129
130         if ((prop = node.property (X_("flags"))) != 0) {
131                 _flags = (Flag) string_2_enum (prop->value(), _flags);
132         }
133
134         if ((prop = node.property (X_("value"))) != 0) {
135                 float val;
136
137                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
138                         set_value (val);
139                 }
140         }
141 }
142
143 void
144 Controllable::set_flags (Flag f)
145 {
146         _flags = f;
147 }