1 /*
2  * Copyright (C) 2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017-2019 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 "pbd/error.h"
21 #include "pbd/replace_all.h"
22 #include "pbd/string_convert.h"
23 
24 #include "ardour/boost_debug.h"
25 #include "ardour/selection.h"
26 #include "ardour/session.h"
27 #include "ardour/slavable.h"
28 #include "ardour/vca.h"
29 #include "ardour/vca_manager.h"
30 
31 #include "pbd/i18n.h"
32 
33 using namespace ARDOUR;
34 using namespace Glib::Threads;
35 using namespace PBD;
36 using std::string;
37 
38 string VCAManager::xml_node_name (X_("VCAManager"));
39 
VCAManager(Session & s)40 VCAManager::VCAManager (Session& s)
41 	: SessionHandleRef (s)
42 	, _vcas_loaded (false)
43 {
44 }
45 
~VCAManager()46 VCAManager::~VCAManager ()
47 {
48 	clear ();
49 }
50 
51 void
clear()52 VCAManager::clear ()
53 {
54 	bool send = false;
55 	{
56 		Mutex::Lock lm (lock);
57 		for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
58 			if ((*i)->is_selected ()) {
59 				_session.selection().remove_stripable_by_id ((*i)->id());
60 				send = true;
61 			}
62 			(*i)->DropReferences ();
63 		}
64 		_vcas.clear ();
65 	}
66 
67 	if (send && !_session.deletion_in_progress ()) {
68 		PropertyChange pc;
69 		pc.add (Properties::selected);
70 		PresentationInfo::Change (pc);
71 	}
72 }
73 
74 VCAList
vcas() const75 VCAManager::vcas () const
76 {
77 	Mutex::Lock lm (lock);
78 	return _vcas;
79 }
80 
81 VCAList
create_vca(uint32_t howmany,std::string const & name_template)82 VCAManager::create_vca (uint32_t howmany, std::string const & name_template)
83 {
84 	VCAList vcal;
85 
86 	uint32_t n_stripables = _session.nstripables ();
87 
88 	{
89 		Mutex::Lock lm (lock);
90 
91 		for (uint32_t n = 0; n < howmany; ++n) {
92 
93 			int num = VCA::next_vca_number ();
94 			string name = name_template;
95 
96 			if (name.find ("%n")) {
97 				string sn = PBD::to_string (num);
98 				replace_all (name, "%n", sn);
99 			}
100 
101 			boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
102 			BOOST_MARK_VCA (vca);
103 
104 			vca->init ();
105 			vca->set_presentation_order (n + n_stripables);
106 
107 			_vcas.push_back (vca);
108 			vcal.push_back (vca);
109 		}
110 	}
111 
112 	VCAAdded (vcal); /* EMIT SIGNAL */
113 
114 	if (!vcal.empty ()) {
115 		VCACreated (); /* EMIT SIGNAL */
116 	}
117 
118 	_session.set_dirty ();
119 
120 	return vcal;
121 }
122 
123 void
remove_vca(boost::shared_ptr<VCA> vca)124 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
125 {
126 	{
127 		Mutex::Lock lm (lock);
128 		_vcas.remove (vca);
129 	}
130 
131 	/* this should cause deassignment and deletion */
132 
133 	vca->DropReferences ();
134 
135 	if (vca->is_selected () && !_session.deletion_in_progress ()) {
136 		_session.selection().remove_stripable_by_id (vca->id());
137 		PropertyChange pc;
138 		pc.add (Properties::selected);
139 		PresentationInfo::Change (pc);
140 	}
141 	_session.set_dirty ();
142 }
143 
144 boost::shared_ptr<VCA>
vca_by_number(int32_t n) const145 VCAManager::vca_by_number (int32_t n) const
146 {
147 	Mutex::Lock lm (lock);
148 
149 	for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
150 		if ((*i)->number() == n) {
151 			return *i;
152 		}
153 	}
154 
155 	return boost::shared_ptr<VCA>();
156 }
157 
158 boost::shared_ptr<VCA>
vca_by_name(std::string const & name) const159 VCAManager::vca_by_name (std::string const& name) const
160 {
161 	Mutex::Lock lm (lock);
162 
163 	for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
164 		if ((*i)->name() == name || (*i)->full_name() == name) {
165 			return *i;
166 		}
167 	}
168 
169 	return boost::shared_ptr<VCA>();
170 }
171 
172 XMLNode&
get_state()173 VCAManager::get_state ()
174 {
175 	XMLNode* node = new XMLNode (xml_node_name);
176 
177 	{
178 		Mutex::Lock lm (lock);
179 
180 		for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
181 			node->add_child_nocopy ((*i)->get_state());
182 		}
183 	}
184 
185 	return *node;
186 }
187 
188 int
set_state(XMLNode const & node,int version)189 VCAManager::set_state (XMLNode const& node, int version)
190 {
191 	if (node.name() != xml_node_name) {
192 		return -1;
193 	}
194 
195 	XMLNodeList const & children = node.children();
196 	VCAList vcal;
197 
198 	_vcas_loaded = false;
199 
200 	for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
201 		if ((*i)->name() == VCA::xml_node_name) {
202 			boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
203 			BOOST_MARK_VCA (vca);
204 
205 			if (vca->init() || vca->set_state (**i, version)) {
206 				error << _("Cannot set state of a VCA") << endmsg;
207 				return -1;
208 			}
209 
210 			/* can't hold the lock for the entire loop,
211 			 * because the new VCA maybe slaved and needs
212 			 * to call back into us to set up its own
213 			 * slave/master relationship
214 			 */
215 
216 			{
217 				Mutex::Lock lm (lock);
218 				_vcas.push_back (vca);
219 				vcal.push_back (vca);
220 			}
221 		}
222 	}
223 
224 	_vcas_loaded = true;
225 
226 	VCAAdded (vcal); /* EMIT SIGNAL */
227 
228 	return 0;
229 }
230 
231 void
clear_all_solo_state()232 VCAManager::clear_all_solo_state ()
233 {
234 	Mutex::Lock lm (lock);
235 
236 	for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
237 		(*i)->clear_all_solo_state ();
238 	}
239 }
240