Non-numeric Properties are not automatable
[ardour.git] / libs / ardour / slavable.cc
1 /*
2     Copyright (C) 2016 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 <vector>
21
22 #include <glibmm/threads.h>
23
24 #include "pbd/convert.h"
25 #include "pbd/error.h"
26 #include "pbd/xml++.h"
27
28 #include "ardour/slavable.h"
29 #include "ardour/slavable_automation_control.h"
30 #include "ardour/vca.h"
31 #include "ardour/vca_manager.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace PBD;
36 using namespace ARDOUR;
37
38 std::string Slavable::xml_node_name = X_("Slavable");
39 PBD::Signal1<void,VCAManager*> Slavable::Assign; /* signal sent once
40                                                   * assignment is possible */
41
42 Slavable::Slavable ()
43 {
44         Assign.connect_same_thread (assign_connection, boost::bind (&Slavable::do_assign, this, _1));
45 }
46
47 XMLNode&
48 Slavable::get_state () const
49 {
50         XMLNode* node = new XMLNode (xml_node_name);
51         XMLNode* child;
52
53         Glib::Threads::RWLock::ReaderLock lm (master_lock);
54         for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
55                 child = new XMLNode (X_("Master"));
56                 child->set_property (X_("number"), *i);
57                 node->add_child_nocopy (*child);
58         }
59
60         return *node;
61 }
62
63 int
64 Slavable::set_state (XMLNode const& node, int version)
65 {
66         if (node.name() != xml_node_name) {
67                 return -1;
68         }
69
70         XMLNodeList const& children (node.children());
71         Glib::Threads::RWLock::WriterLock lm (master_lock);
72
73         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
74                 if ((*i)->name() == X_("Master")) {
75                         uint32_t n;
76                         if ((*i)->get_property (X_("number"), n)) {
77                                 _masters.insert (n);
78                         }
79                 }
80         }
81
82         return 0;
83 }
84
85 int
86 Slavable::do_assign (VCAManager* manager)
87 {
88         std::vector<boost::shared_ptr<VCA> > vcas;
89
90         {
91                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
92
93                 for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
94                         boost::shared_ptr<VCA> v = manager->vca_by_number (*i);
95                         if (v) {
96                                 vcas.push_back (v);
97                         } else {
98                                 warning << string_compose (_("Master #%1 not found, assignment lost"), *i) << endmsg;
99                         }
100                 }
101         }
102
103         /* now that we've released the lock, we can do the assignments */
104
105         if (!vcas.empty()) {
106
107                 for (std::vector<boost::shared_ptr<VCA> >::iterator v = vcas.begin(); v != vcas.end(); ++v) {
108                         assign (*v);
109                 }
110
111                 SlavableControlList scl = slavables ();
112                 for (SlavableControlList::iterator i = scl.begin(); i != scl.end(); ++i) {
113                                 (*i)->use_saved_master_ratios ();
114                 }
115         }
116
117         assign_connection.disconnect ();
118
119         return 0;
120 }
121
122 void
123 Slavable::assign (boost::shared_ptr<VCA> v)
124 {
125         assert (v);
126         {
127                 Glib::Threads::RWLock::WriterLock lm (master_lock);
128                 if (assign_controls (v)) {
129                         _masters.insert (v->number());
130                 }
131
132                 /* Do NOT use ::unassign() because it will store a
133                  * boost::shared_ptr<VCA> in the functor, leaving a dangling ref to the
134                  * VCA.
135                  */
136
137
138                 v->Drop.connect_same_thread (unassign_connections, boost::bind (&Slavable::weak_unassign, this, boost::weak_ptr<VCA>(v)));
139                 v->DropReferences.connect_same_thread (unassign_connections, boost::bind (&Slavable::weak_unassign, this, boost::weak_ptr<VCA>(v)));
140         }
141
142         AssignmentChange (v, true);
143 }
144
145 void
146 Slavable::weak_unassign (boost::weak_ptr<VCA> v)
147 {
148         boost::shared_ptr<VCA> sv (v.lock());
149         if (sv) {
150                 unassign (sv);
151         }
152 }
153
154 void
155 Slavable::unassign (boost::shared_ptr<VCA> v)
156 {
157         {
158                 Glib::Threads::RWLock::WriterLock lm (master_lock);
159
160                 unassign_controls (v);
161                 if (v) {
162                         _masters.erase (v->number());
163                 } else {
164                         _masters.clear ();
165                 }
166         }
167         AssignmentChange (v, false);
168 }
169
170 bool
171 Slavable::assign_controls (boost::shared_ptr<VCA> vca)
172 {
173         bool rv = false;
174         SlavableControlList scl = slavables ();
175         for (SlavableControlList::iterator i = scl.begin(); i != scl.end(); ++i) {
176                 rv |= assign_control (vca, *i);
177         }
178         return rv;
179 }
180
181 void
182 Slavable::unassign_controls (boost::shared_ptr<VCA> vca)
183 {
184         SlavableControlList scl = slavables ();
185         for (SlavableControlList::iterator i = scl.begin(); i != scl.end(); ++i) {
186                 unassign_control (vca, *i);
187         }
188 }
189
190 bool
191 Slavable::assign_control (boost::shared_ptr<VCA> vca, boost::shared_ptr<SlavableAutomationControl> slave)
192 {
193         boost::shared_ptr<AutomationControl> master;
194         master = vca->automation_control (slave->parameter());
195         if (!master) {
196                 return false;
197         }
198         slave->add_master (master);
199         return true;
200 }
201
202 void
203 Slavable::unassign_control (boost::shared_ptr<VCA> vca, boost::shared_ptr<SlavableAutomationControl> slave)
204 {
205         if (!vca) {
206                 /* unassign from all */
207                 slave->clear_masters ();
208         } else {
209                 boost::shared_ptr<AutomationControl> master;
210                 master = vca->automation_control (slave->parameter());
211                 if (master) {
212                         slave->remove_master (master);
213                 }
214         }
215 }