369e2bd5451d13677b8b7720738f45636ec29520
[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/xml++.h"
22 #include "pbd/error.h"
23
24 #include "i18n.h"
25
26 using namespace PBD;
27 using namespace std;
28
29 boost::signals2::signal<void(Controllable*)> Controllable::Destroyed;
30 boost::signals2::signal<bool(Controllable*)> Controllable::StartLearning;
31 boost::signals2::signal<void(Controllable*)> Controllable::StopLearning;
32 boost::signals2::signal<void(Controllable*,int,int)> Controllable::CreateBinding;
33 boost::signals2::signal<void(Controllable*)> Controllable::DeleteBinding;
34
35 Glib::StaticRWLock Controllable::registry_lock = GLIBMM_STATIC_RW_LOCK_INIT;
36 Controllable::Controllables Controllable::registry;
37 Controllable::ControllablesByURI Controllable::registry_by_uri;
38
39 Controllable::Controllable (const string& name, const string& uri)
40         : _name (name)
41         , _uri (uri)
42         , _touching (false)
43 {
44         add (*this);
45 }
46
47 void
48 Controllable::add (Controllable& ctl)
49 {
50         using namespace boost;
51
52         Glib::RWLock::WriterLock lm (registry_lock);
53         registry.insert (&ctl);
54
55         if (!ctl.uri().empty()) {
56                 pair<string,Controllable*> newpair;
57                 newpair.first = ctl.uri();
58                 newpair.second = &ctl;
59                 registry_by_uri.insert (newpair);
60         }
61
62         /* Controllable::remove() is static - no need to manage this connection */
63
64         ctl.GoingAway.connect (boost::bind (&Controllable::remove, ref (ctl)));
65 }
66
67 void
68 Controllable::remove (Controllable& ctl)
69 {
70         Glib::RWLock::WriterLock lm (registry_lock);
71
72         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
73                 if ((*i) == &ctl) {
74                         registry.erase (i);
75                         break;
76                 }
77         }
78
79         if (!ctl.uri().empty()) {
80                 ControllablesByURI::iterator i = registry_by_uri.find (ctl.uri());
81                 if (i != registry_by_uri.end()) {
82                         registry_by_uri.erase (i);
83                 }
84         }
85 }
86
87 void
88 Controllable::set_uri (const string& new_uri)
89 {
90         Glib::RWLock::WriterLock lm (registry_lock);
91
92         if (!_uri.empty()) {
93                 ControllablesByURI::iterator i = registry_by_uri.find (_uri);
94                 if (i != registry_by_uri.end()) {
95                         registry_by_uri.erase (i);
96                 }
97         }
98
99         _uri = new_uri;
100
101         if (!_uri.empty()) {
102                 pair<string,Controllable*> newpair;
103                 newpair.first = _uri;
104                 newpair.second = this;
105                 registry_by_uri.insert (newpair);
106         }
107 }
108
109 Controllable*
110 Controllable::by_id (const ID& id)
111 {
112         Glib::RWLock::ReaderLock lm (registry_lock);
113
114         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
115                 if ((*i)->id() == id) {
116                         return (*i);
117                 }
118         }
119         return 0;
120 }
121
122 Controllable*
123 Controllable::by_uri (const string& uri)
124 {
125         Glib::RWLock::ReaderLock lm (registry_lock);
126         ControllablesByURI::iterator i;
127
128         if ((i = registry_by_uri.find (uri)) != registry_by_uri.end()) {
129                 return i->second;
130         }
131         return 0;
132 }
133
134 Controllable*
135 Controllable::by_name (const string& str)
136 {
137         Glib::RWLock::ReaderLock lm (registry_lock);
138
139         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
140                 if ((*i)->_name == str) {
141                         return (*i);
142                 }
143         }
144         return 0;
145 }
146
147 XMLNode&
148 Controllable::get_state ()
149 {
150         XMLNode* node = new XMLNode (X_("Controllable"));
151         char buf[64];
152
153         node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
154         _id.print (buf, sizeof (buf));
155         node->add_property (X_("id"), buf);
156
157         if (!_uri.empty()) {
158                 node->add_property (X_("uri"), _uri);
159         }
160                 
161         return *node;
162 }
163
164 int
165 Controllable::set_state (const XMLNode& node, int /*version*/)
166 {
167         const XMLProperty* prop;
168
169         if ((prop = node.property (X_("id"))) != 0) {
170                 _id = prop->value();
171                 return 0;
172         } else {
173                 error << _("Controllable state node has no ID property") << endmsg;
174                 return -1;
175         }
176
177         if ((prop = node.property (X_("uri"))) != 0) {
178                 set_uri (prop->value());
179         }
180 }