more or less complete restoration of Controllable::_id from XML, with all that implie...
[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
26 #include "i18n.h"
27
28 using namespace PBD;
29 using namespace std;
30
31 PBD::Signal1<void,Controllable*> Controllable::Destroyed;
32 PBD::Signal1<bool,Controllable*> Controllable::StartLearning;
33 PBD::Signal1<void,Controllable*> Controllable::StopLearning;
34 PBD::Signal3<void,Controllable*,int,int> Controllable::CreateBinding;
35 PBD::Signal1<void,Controllable*> Controllable::DeleteBinding;
36
37 Glib::StaticRWLock Controllable::registry_lock = GLIBMM_STATIC_RW_LOCK_INIT;
38 Controllable::Controllables Controllable::registry;
39 PBD::ScopedConnectionList registry_connections;
40 const std::string Controllable::xml_node_name = X_("Controllable");
41
42 Controllable::Controllable (const string& name, Flag f)
43         : _name (name)
44         , _flags (f)
45         , _touching (false)
46 {
47         add (*this);
48 }
49
50 void
51 Controllable::add (Controllable& ctl)
52 {
53         using namespace boost;
54
55         Glib::RWLock::WriterLock lm (registry_lock);
56         registry.insert (&ctl);
57
58         /* Controllable::remove() is static - no need to manage this connection */
59
60         ctl.DropReferences.connect_same_thread (registry_connections, boost::bind (&Controllable::remove, &ctl));
61 }
62
63 void
64 Controllable::remove (Controllable* ctl)
65 {
66         Glib::RWLock::WriterLock lm (registry_lock);
67
68         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
69                 if ((*i) == ctl) {
70                         registry.erase (i);
71                         break;
72                 }
73         }
74 }
75
76 Controllable*
77 Controllable::by_id (const ID& id)
78 {
79         Glib::RWLock::ReaderLock lm (registry_lock);
80
81         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
82                 if ((*i)->id() == id) {
83                         return (*i);
84                 }
85         }
86         return 0;
87 }
88
89 Controllable*
90 Controllable::by_name (const string& str)
91 {
92         Glib::RWLock::ReaderLock lm (registry_lock);
93
94         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
95                 if ((*i)->_name == str) {
96                         return (*i);
97                 }
98         }
99         return 0;
100 }
101
102 XMLNode&
103 Controllable::get_state ()
104 {
105         XMLNode* node = new XMLNode (xml_node_name);
106         LocaleGuard lg (X_("POSIX"));
107         char buf[64];
108
109         node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
110         _id.print (buf, sizeof (buf));
111         node->add_property (X_("id"), buf);
112         node->add_property (X_("flags"), enum_2_string (_flags));
113         snprintf (buf, sizeof (buf), "%2.12f", get_value());
114         node->add_property (X_("value"), buf);
115
116         return *node;
117 }
118
119
120 int
121 Controllable::set_state (const XMLNode& node, int /*version*/)
122 {
123         LocaleGuard lg (X_("POSIX"));
124         const XMLProperty* prop;
125
126         if ((prop = node.property (X_("id"))) != 0) {
127                 _id = prop->value();
128         } else {
129                 error << _("Controllable state node has no ID property") << endmsg;
130                 return -1;
131         }
132
133         if ((prop = node.property (X_("flags"))) != 0) {
134                 _flags = (Flag) string_2_enum (prop->value(), _flags);
135         }
136
137         if ((prop = node.property (X_("value"))) != 0) {
138                 float val;
139
140                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
141                         set_value (val);
142                 } 
143         }
144
145         return 0;
146 }
147
148 void
149 Controllable::set_flags (Flag f)
150 {
151         _flags = f;
152 }