fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / vca_manager.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 "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/replace_all.h"
23
24 #include "ardour/boost_debug.h"
25 #include "ardour/session.h"
26 #include "ardour/slavable.h"
27 #include "ardour/vca.h"
28 #include "ardour/vca_manager.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace ARDOUR;
33 using namespace Glib::Threads;
34 using namespace PBD;
35 using std::string;
36
37 string VCAManager::xml_node_name (X_("VCAManager"));
38
39 VCAManager::VCAManager (Session& s)
40         : SessionHandleRef (s)
41         , _vcas_loaded (false)
42 {
43 }
44
45 VCAManager::~VCAManager ()
46 {
47         clear ();
48 }
49
50 void
51 VCAManager::clear ()
52 {
53         Mutex::Lock lm (lock);
54         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
55                 (*i)->DropReferences ();
56         }
57         _vcas.clear ();
58 }
59
60 VCAList
61 VCAManager::vcas () const
62 {
63         Mutex::Lock lm (lock);
64         return _vcas;
65 }
66
67 int
68 VCAManager::create_vca (uint32_t howmany, std::string const & name_template)
69 {
70         VCAList vcal;
71
72         {
73                 Mutex::Lock lm (lock);
74
75                 for (uint32_t n = 0; n < howmany; ++n) {
76
77                         int num = VCA::next_vca_number ();
78                         string name = name_template;
79
80                         if (name.find ("%n")) {
81                                 string sn = PBD::to_string (num, std::dec);
82                                 replace_all (name, "%n", sn);
83                         }
84
85                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
86                         BOOST_MARK_VCA (vca);
87
88                         vca->init ();
89
90                         _vcas.push_back (vca);
91                         vcal.push_back (vca);
92                 }
93         }
94
95         VCAAdded (vcal); /* EMIT SIGNAL */
96
97         _session.set_dirty ();
98
99         return 0;
100 }
101
102
103 void
104 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
105 {
106         {
107                 Mutex::Lock lm (lock);
108                 _vcas.remove (vca);
109         }
110
111         /* this should cause deassignment and deletion */
112
113         vca->DropReferences ();
114
115         _session.set_dirty ();
116 }
117
118 boost::shared_ptr<VCA>
119 VCAManager::vca_by_number (int32_t n) const
120 {
121         Mutex::Lock lm (lock);
122
123         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
124                 if ((*i)->number() == n) {
125                         return *i;
126                 }
127         }
128
129         return boost::shared_ptr<VCA>();
130 }
131
132 XMLNode&
133 VCAManager::get_state ()
134 {
135         XMLNode* node = new XMLNode (xml_node_name);
136
137         {
138                 Mutex::Lock lm (lock);
139
140                 for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
141                         node->add_child_nocopy ((*i)->get_state());
142                 }
143         }
144
145         return *node;
146 }
147
148 int
149 VCAManager::set_state (XMLNode const& node, int version)
150 {
151         if (node.name() != xml_node_name) {
152                 return -1;
153         }
154
155         XMLNodeList const & children = node.children();
156         VCAList vcal;
157
158         _vcas_loaded = false;
159
160         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
161                 if ((*i)->name() == VCA::xml_node_name) {
162                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
163                         BOOST_MARK_VCA (vca);
164
165                         if (vca->init() || vca->set_state (**i, version)) {
166                                 error << _("Cannot set state of a VCA") << endmsg;
167                                 return -1;
168                         }
169
170                         /* can't hold the lock for the entire loop,
171                          * because the new VCA maybe slaved and needs
172                          * to call back into us to set up its own
173                          * slave/master relationship
174                          */
175
176                         {
177                                 Mutex::Lock lm (lock);
178                                 _vcas.push_back (vca);
179                                 vcal.push_back (vca);
180                         }
181                 }
182         }
183
184         _vcas_loaded = true;
185
186         VCAAdded (vcal); /* EMIT SIGNAL */
187
188         return 0;
189 }
190
191 void
192 VCAManager::clear_all_solo_state ()
193 {
194         Mutex::Lock lm (lock);
195
196         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
197                 (*i)->clear_all_solo_state ();
198         }
199 }