Prefix all env variable with "ARDOUR_"
[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/types_convert.h"
25 #include "pbd/string_convert.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace PBD;
30 using namespace std;
31
32 PBD::Signal1<bool, boost::weak_ptr<PBD::Controllable> > Controllable::StartLearning;
33 PBD::Signal1<void, boost::weak_ptr<PBD::Controllable> > Controllable::StopLearning;
34 PBD::Signal1<void, boost::weak_ptr<PBD::Controllable> > Controllable::GUIFocusChanged;
35
36 Glib::Threads::RWLock Controllable::registry_lock;
37 Controllable::Controllables Controllable::registry;
38 PBD::ScopedConnectionList Controllable::registry_connections;
39
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 XMLNode&
51 Controllable::get_state ()
52 {
53         XMLNode* node = new XMLNode (xml_node_name);
54
55         /* Waves' "Pressure3" has a parameter called "µ-iness"
56          * which causes a  parser error : Input is not proper UTF-8, indicate encoding !
57          *  Bytes: 0xB5 0x2D 0x69 0x6E
58          *          <Controllable name="�-iness" id="2391" flags="" value="0.000000000000" p
59          */
60
61         // this is not reloaded from XML, but it must be present because it is
62         // used to find and identify XML nodes by various Controllable-derived objects
63
64         node->set_property (X_("name"), _name);
65         node->set_property (X_("id"), id());
66         node->set_property (X_("flags"), _flags);
67         node->set_property (X_("value"), get_save_value());
68
69         if (_extra_xml) {
70                 node->add_child_copy (*_extra_xml);
71         }
72
73         return *node;
74 }
75
76 int
77 Controllable::set_state (const XMLNode& node, int /*version*/)
78 {
79         Stateful::save_extra_xml (node);
80
81         set_id (node);
82
83         if (node.get_property (X_("flags"), _flags)) {
84                 _flags = Flag(_flags | (_flags & Controllable::RealTime));
85         }
86
87         float val;
88         if (node.get_property (X_("value"), val)) {
89                         set_value (val, NoGroup);
90         }
91         return 0;
92 }
93
94 void
95 Controllable::set_flags (Flag f)
96 {
97         _flags = f;
98 }
99
100 void
101 Controllable::add (Controllable& ctl)
102 {
103         Glib::Threads::RWLock::WriterLock lm (registry_lock);
104         registry.insert (&ctl);
105         ctl.DropReferences.connect_same_thread (registry_connections, boost::bind (&Controllable::remove, &ctl));
106         ctl.Destroyed.connect_same_thread (registry_connections, boost::bind (&Controllable::remove, &ctl));
107 }
108
109 void
110 Controllable::remove (Controllable* ctl)
111 {
112         Glib::Threads::RWLock::WriterLock lm (registry_lock);
113         Controllables::iterator i = std::find (registry.begin(), registry.end(), ctl);
114         if (i != registry.end()) {
115                 registry.erase (i);
116         }
117 }
118
119 boost::shared_ptr<Controllable>
120 Controllable::by_id (const ID& id)
121 {
122         Glib::Threads::RWLock::ReaderLock lm (registry_lock);
123
124         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
125                 if ((*i)->id() == id) {
126                         return (*i)->shared_from_this ();
127                 }
128         }
129         return boost::shared_ptr<Controllable>();
130 }
131
132 void
133 Controllable::dump_registry ()
134 {
135         Glib::Threads::RWLock::ReaderLock lm (registry_lock);
136         unsigned int cnt = 0;
137         cout << "-- List Of Registered Controllables\n";
138         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i, ++cnt) {
139                 cout << "CTRL: " << (*i)->name() << "\n";
140         }
141         cout << "Total number of registered sontrollables: " << cnt << "\n";
142 }