1 /*
2 * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
Slavable()42 Slavable::Slavable ()
43 {
44 Assign.connect_same_thread (assign_connection, boost::bind (&Slavable::do_assign, this, _1));
45 }
46
47 XMLNode&
get_state() const48 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 std::vector<boost::shared_ptr<VCA> >
masters(VCAManager * manager) const64 Slavable::masters (VCAManager* manager) const
65 {
66 std::vector<boost::shared_ptr<VCA> > rv;
67 Glib::Threads::RWLock::ReaderLock lm (master_lock);
68 for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
69 rv.push_back (manager->vca_by_number (*i));
70 }
71 return rv;
72 }
73
74 bool
assigned_to(VCAManager * manager,boost::shared_ptr<VCA> mst) const75 Slavable::assigned_to (VCAManager* manager, boost::shared_ptr<VCA> mst) const
76 {
77 if (mst.get () == this) {
78 return true;
79 }
80 std::vector<boost::shared_ptr<VCA> > ml = mst->masters (manager);
81 for (std::vector<boost::shared_ptr<VCA> >::const_iterator i = ml.begin (); i != ml.end(); ++i) {
82 if (assigned_to (manager, *i)) {
83 return true;
84 }
85 }
86 return false;
87 }
88
89 int
set_state(XMLNode const & node,int version)90 Slavable::set_state (XMLNode const& node, int version)
91 {
92 if (node.name() != xml_node_name) {
93 return -1;
94 }
95
96 XMLNodeList const& children (node.children());
97 Glib::Threads::RWLock::WriterLock lm (master_lock);
98
99 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
100 if ((*i)->name() == X_("Master")) {
101 uint32_t n;
102 if ((*i)->get_property (X_("number"), n)) {
103 _masters.insert (n);
104 }
105 }
106 }
107
108 return 0;
109 }
110
111 int
do_assign(VCAManager * manager)112 Slavable::do_assign (VCAManager* manager)
113 {
114 std::vector<boost::shared_ptr<VCA> > vcas;
115
116 {
117 Glib::Threads::RWLock::ReaderLock lm (master_lock);
118
119 for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
120 boost::shared_ptr<VCA> v = manager->vca_by_number (*i);
121 if (v) {
122 vcas.push_back (v);
123 } else {
124 warning << string_compose (_("Master #%1 not found, assignment lost"), *i) << endmsg;
125 }
126 }
127 }
128
129 /* now that we've released the lock, we can do the assignments */
130
131 if (!vcas.empty()) {
132
133 for (std::vector<boost::shared_ptr<VCA> >::iterator v = vcas.begin(); v != vcas.end(); ++v) {
134 assign (*v);
135 }
136
137 SlavableControlList scl = slavables ();
138 for (SlavableControlList::iterator i = scl.begin(); i != scl.end(); ++i) {
139 (*i)->use_saved_master_ratios ();
140 }
141 }
142
143 assign_connection.disconnect ();
144
145 return 0;
146 }
147
148 void
assign(boost::shared_ptr<VCA> v)149 Slavable::assign (boost::shared_ptr<VCA> v)
150 {
151 assert (v);
152 {
153 Glib::Threads::RWLock::WriterLock lm (master_lock);
154 if (assign_controls (v)) {
155 _masters.insert (v->number());
156 }
157
158 /* Do NOT use ::unassign() because it will store a
159 * boost::shared_ptr<VCA> in the functor, leaving a dangling ref to the
160 * VCA.
161 */
162
163
164 v->Drop.connect_same_thread (unassign_connections, boost::bind (&Slavable::weak_unassign, this, boost::weak_ptr<VCA>(v)));
165 v->DropReferences.connect_same_thread (unassign_connections, boost::bind (&Slavable::weak_unassign, this, boost::weak_ptr<VCA>(v)));
166 }
167
168 AssignmentChange (v, true);
169 }
170
171 void
weak_unassign(boost::weak_ptr<VCA> v)172 Slavable::weak_unassign (boost::weak_ptr<VCA> v)
173 {
174 boost::shared_ptr<VCA> sv (v.lock());
175 if (sv) {
176 unassign (sv);
177 }
178 }
179
180 void
unassign(boost::shared_ptr<VCA> v)181 Slavable::unassign (boost::shared_ptr<VCA> v)
182 {
183 {
184 Glib::Threads::RWLock::WriterLock lm (master_lock);
185
186 unassign_controls (v);
187 if (v) {
188 _masters.erase (v->number());
189 } else {
190 _masters.clear ();
191 }
192 }
193 AssignmentChange (v, false);
194 }
195
196 bool
assign_controls(boost::shared_ptr<VCA> vca)197 Slavable::assign_controls (boost::shared_ptr<VCA> vca)
198 {
199 bool rv = false;
200 SlavableControlList scl = slavables ();
201 for (SlavableControlList::iterator i = scl.begin(); i != scl.end(); ++i) {
202 rv |= assign_control (vca, *i);
203 }
204 return rv;
205 }
206
207 void
unassign_controls(boost::shared_ptr<VCA> vca)208 Slavable::unassign_controls (boost::shared_ptr<VCA> vca)
209 {
210 SlavableControlList scl = slavables ();
211 for (SlavableControlList::iterator i = scl.begin(); i != scl.end(); ++i) {
212 unassign_control (vca, *i);
213 }
214 }
215
216 bool
assign_control(boost::shared_ptr<VCA> vca,boost::shared_ptr<SlavableAutomationControl> slave)217 Slavable::assign_control (boost::shared_ptr<VCA> vca, boost::shared_ptr<SlavableAutomationControl> slave)
218 {
219 boost::shared_ptr<AutomationControl> master;
220 master = vca->automation_control (slave->parameter());
221 if (!master) {
222 return false;
223 }
224 slave->add_master (master);
225 return true;
226 }
227
228 void
unassign_control(boost::shared_ptr<VCA> vca,boost::shared_ptr<SlavableAutomationControl> slave)229 Slavable::unassign_control (boost::shared_ptr<VCA> vca, boost::shared_ptr<SlavableAutomationControl> slave)
230 {
231 if (!vca) {
232 /* unassign from all */
233 slave->clear_masters ();
234 } else {
235 boost::shared_ptr<AutomationControl> master;
236 master = vca->automation_control (slave->parameter());
237 if (master) {
238 slave->remove_master (master);
239 }
240 }
241 }
242