provide mechanism for "drop all slaves" for VCA master
[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 "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->add_property (X_("number"), to_string (*i, std::dec));
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                         XMLProperty const* prop = (*i)->property (X_("number"));
76                         if (prop) {
77                                 uint32_t n = atoi (prop->value());
78                                 _masters.insert (n);
79                         }
80                 }
81         }
82
83         return 0;
84 }
85
86 int
87 Slavable::do_assign (VCAManager* manager)
88 {
89         std::vector<boost::shared_ptr<VCA> > vcas;
90
91         {
92                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
93
94                 for (std::set<uint32_t>::const_iterator i = _masters.begin(); i != _masters.end(); ++i) {
95                         boost::shared_ptr<VCA> v = manager->vca_by_number (*i);
96                         if (v) {
97                                 vcas.push_back (v);
98                         } else {
99                                 warning << string_compose (_("Master #%1 not found, assignment lost"), *i) << endmsg;
100                         }
101                 }
102         }
103
104         /* now that we've released the lock, we can do the assignments */
105
106         for (std::vector<boost::shared_ptr<VCA> >::iterator v = vcas.begin(); v != vcas.end(); ++v) {
107                 assign (*v);
108         }
109
110         assign_connection.disconnect ();
111
112         return 0;
113 }
114
115 void
116 Slavable::assign (boost::shared_ptr<VCA> v)
117 {
118         assert (v);
119         Glib::Threads::RWLock::WriterLock lm (master_lock);
120         if (assign_controls (v) == 0) {
121                 _masters.insert (v->number());
122         }
123
124         v->Drop.connect_same_thread (unassign_connections, boost::bind (&Slavable::unassign, this, v));
125 }
126
127 void
128 Slavable::unassign (boost::shared_ptr<VCA> v)
129 {
130         Glib::Threads::RWLock::WriterLock lm (master_lock);
131         (void) unassign_controls (v);
132         if (v) {
133                 _masters.erase (v->number());
134         } else {
135                 _masters.clear ();
136         }
137 }
138
139 int
140 Slavable::assign_controls (boost::shared_ptr<VCA> vca)
141 {
142         boost::shared_ptr<SlavableAutomationControl> slave;
143         boost::shared_ptr<AutomationControl> master;
144         AutomationType types[] = {
145                 GainAutomation,
146                 SoloAutomation,
147                 MuteAutomation,
148                 RecEnableAutomation,
149                 MonitoringAutomation,
150                 NullAutomation
151         };
152
153         for (uint32_t n = 0; types[n] != NullAutomation; ++n) {
154
155                 slave = boost::dynamic_pointer_cast<SlavableAutomationControl> (automation_control (types[n]));
156                 master = vca->automation_control (types[n]);
157
158                 if (slave && master) {
159                         slave->add_master (master);
160                 }
161         }
162
163         return 0;
164 }
165
166 int
167 Slavable::unassign_controls (boost::shared_ptr<VCA> vca)
168 {
169         boost::shared_ptr<SlavableAutomationControl> slave;
170         boost::shared_ptr<AutomationControl> master;
171         AutomationType types[] = {
172                 GainAutomation,
173                 SoloAutomation,
174                 MuteAutomation,
175                 RecEnableAutomation,
176                 MonitoringAutomation,
177                 NullAutomation
178         };
179
180         for (uint32_t n = 0; types[n] != NullAutomation; ++n) {
181
182                 slave = boost::dynamic_pointer_cast<SlavableAutomationControl> (automation_control (types[n]));
183
184                 if (!vca) {
185                         /* unassign from all */
186                         if (slave) {
187                                 slave->clear_masters ();
188                         }
189                 } else {
190                         master = vca->automation_control (types[n]);
191                         if (slave && master) {
192                                 slave->remove_master (master);
193                         }
194                 }
195         }
196
197         return 0;
198 }