mostly restore VCA state on session loading.
[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 #include "pbd/locale_guard.h"
25 #include "pbd/stacktrace.h"
26
27 #include "i18n.h"
28
29 using namespace PBD;
30 using namespace std;
31
32 PBD::Signal1<void,Controllable*> Controllable::Destroyed;
33 PBD::Signal1<bool,Controllable*> Controllable::StartLearning;
34 PBD::Signal1<void,Controllable*> Controllable::StopLearning;
35 PBD::Signal3<void,Controllable*,int,int> Controllable::CreateBinding;
36 PBD::Signal1<void,Controllable*> Controllable::DeleteBinding;
37
38 Glib::Threads::RWLock Controllable::registry_lock;
39 Controllable::Controllables Controllable::registry;
40 PBD::ScopedConnectionList* registry_connections = 0;
41 const std::string Controllable::xml_node_name = X_("Controllable");
42
43 Controllable::Controllable (const string& name, Flag f)
44         : _name (name)
45         , _flags (f)
46         , _touching (false)
47 {
48         add (*this);
49 }
50
51 void
52 Controllable::add (Controllable& ctl)
53 {
54         using namespace boost;
55
56         Glib::Threads::RWLock::WriterLock lm (registry_lock);
57         registry.insert (&ctl);
58
59         if (!registry_connections) {
60                 registry_connections = new ScopedConnectionList;
61         }
62
63         /* Controllable::remove() is static - no need to manage this connection */
64
65         ctl.DropReferences.connect_same_thread (*registry_connections, boost::bind (&Controllable::remove, &ctl));
66 }
67
68 void
69 Controllable::remove (Controllable* ctl)
70 {
71         Glib::Threads::RWLock::WriterLock lm (registry_lock);
72
73         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
74                 if ((*i) == ctl) {
75                         registry.erase (i);
76                         break;
77                 }
78         }
79 }
80
81 Controllable*
82 Controllable::by_id (const ID& id)
83 {
84         Glib::Threads::RWLock::ReaderLock lm (registry_lock);
85
86         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
87                 if ((*i)->id() == id) {
88                         return (*i);
89                 }
90         }
91         return 0;
92 }
93
94 Controllable*
95 Controllable::by_name (const string& str)
96 {
97         Glib::Threads::RWLock::ReaderLock lm (registry_lock);
98
99         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
100                 if ((*i)->_name == str) {
101                         return (*i);
102                 }
103         }
104         return 0;
105 }
106
107 XMLNode&
108 Controllable::get_state ()
109 {
110         XMLNode* node = new XMLNode (xml_node_name);
111         LocaleGuard lg;
112         char buf[64];
113
114         /* Waves' "Pressure3" has a parameter called "µ-iness"
115          * which causes a  parser error : Input is not proper UTF-8, indicate encoding !
116          *  Bytes: 0xB5 0x2D 0x69 0x6E
117          *          <Controllable name="�-iness" id="2391" flags="" value="0.000000000000" p
118          */
119
120         // this is not reloaded from XML, but it must be present because it is
121         // used to find and identify XML nodes by various Controllable-derived objects
122
123         node->add_property (X_("name"), _name);
124
125         if (_name == "gaincontrol") {
126                 PBD::stacktrace (cerr, 20);
127         }
128
129         id().print (buf, sizeof (buf));
130         node->add_property (X_("id"), buf);
131         node->add_property (X_("flags"), enum_2_string (_flags));
132         snprintf (buf, sizeof (buf), "%2.12f", get_value());
133         node->add_property (X_("value"), buf);
134
135         if (_extra_xml) {
136                 node->add_child_copy (*_extra_xml);
137         }
138
139         return *node;
140 }
141
142
143 int
144 Controllable::set_state (const XMLNode& node, int /*version*/)
145 {
146         LocaleGuard lg;
147         const XMLProperty* prop;
148
149         Stateful::save_extra_xml (node);
150
151         set_id (node);
152
153         if ((prop = node.property (X_("flags"))) != 0) {
154                 _flags = (Flag) string_2_enum (prop->value(), _flags);
155         }
156
157         if ((prop = node.property (X_("value"))) != 0) {
158                 float val;
159
160                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
161                         set_value (val, NoGroup);
162                 }
163         }
164
165         return 0;
166 }
167
168 void
169 Controllable::set_flags (Flag f)
170 {
171         _flags = f;
172 }