d5b81a73ed0eeebd13453f60eae65131e84f8253
[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
40 Controllable::Controllable (const string& name, Flag f)
41         : _name (name)
42         , _flags (f)
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         /* Controllable::remove() is static - no need to manage this connection */
56
57         ctl.DropReferences.connect_same_thread (registry_connections, boost::bind (&Controllable::remove, &ctl));
58 }
59
60 void
61 Controllable::remove (Controllable* ctl)
62 {
63         Glib::RWLock::WriterLock lm (registry_lock);
64
65         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
66                 if ((*i) == ctl) {
67                         registry.erase (i);
68                         break;
69                 }
70         }
71 }
72
73 Controllable*
74 Controllable::by_id (const ID& id)
75 {
76         Glib::RWLock::ReaderLock lm (registry_lock);
77
78         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
79                 if ((*i)->id() == id) {
80                         return (*i);
81                 }
82         }
83         return 0;
84 }
85
86 Controllable*
87 Controllable::by_name (const string& str)
88 {
89         Glib::RWLock::ReaderLock lm (registry_lock);
90
91         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
92                 if ((*i)->_name == str) {
93                         return (*i);
94                 }
95         }
96         return 0;
97 }
98
99 XMLNode&
100 Controllable::get_state ()
101 {
102         XMLNode* node = new XMLNode (X_("Controllable"));
103         char buf[64];
104
105         node->add_property (X_("name"), _name); // not reloaded from XML state, just there to look at
106         _id.print (buf, sizeof (buf));
107         node->add_property (X_("id"), buf);
108         node->add_property (X_("flags"), enum_2_string (_flags));
109
110         return *node;
111 }
112
113 int
114 Controllable::set_state (const XMLNode& node, int /*version*/)
115 {
116         const XMLProperty* prop;
117
118         if ((prop = node.property (X_("id"))) != 0) {
119                 _id = prop->value();
120                 return 0;
121         } else {
122                 error << _("Controllable state node has no ID property") << endmsg;
123                 return -1;
124         }
125
126         if ((prop = node.property (X_("flags"))) != 0) {
127                 _flags = (Flag) string_2_enum (prop->value(), _flags);
128         }
129 }
130
131 void
132 Controllable::set_flags (Flag f)
133 {
134         _flags = f;
135 }