Remove LocaleGuard from ARDOUR::Pannable class
[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/error.h"
21 #include "pbd/replace_all.h"
22 #include "pbd/string_convert.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         uint32_t n_stripables = _session.nstripables ();
73
74         {
75                 Mutex::Lock lm (lock);
76
77                 for (uint32_t n = 0; n < howmany; ++n) {
78
79                         int num = VCA::next_vca_number ();
80                         string name = name_template;
81
82                         if (name.find ("%n")) {
83                                 string sn = PBD::to_string (num);
84                                 replace_all (name, "%n", sn);
85                         }
86
87                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
88                         BOOST_MARK_VCA (vca);
89
90                         vca->init ();
91                         vca->set_presentation_order (n + n_stripables);
92
93                         _vcas.push_back (vca);
94                         vcal.push_back (vca);
95                 }
96         }
97
98         VCAAdded (vcal); /* EMIT SIGNAL */
99
100         _session.set_dirty ();
101
102         return 0;
103 }
104
105
106 void
107 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
108 {
109         {
110                 Mutex::Lock lm (lock);
111                 _vcas.remove (vca);
112         }
113
114         /* this should cause deassignment and deletion */
115
116         vca->DropReferences ();
117
118         _session.set_dirty ();
119 }
120
121 boost::shared_ptr<VCA>
122 VCAManager::vca_by_number (int32_t n) const
123 {
124         Mutex::Lock lm (lock);
125
126         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
127                 if ((*i)->number() == n) {
128                         return *i;
129                 }
130         }
131
132         return boost::shared_ptr<VCA>();
133 }
134
135 XMLNode&
136 VCAManager::get_state ()
137 {
138         XMLNode* node = new XMLNode (xml_node_name);
139
140         {
141                 Mutex::Lock lm (lock);
142
143                 for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
144                         node->add_child_nocopy ((*i)->get_state());
145                 }
146         }
147
148         return *node;
149 }
150
151 int
152 VCAManager::set_state (XMLNode const& node, int version)
153 {
154         if (node.name() != xml_node_name) {
155                 return -1;
156         }
157
158         XMLNodeList const & children = node.children();
159         VCAList vcal;
160
161         _vcas_loaded = false;
162
163         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
164                 if ((*i)->name() == VCA::xml_node_name) {
165                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
166                         BOOST_MARK_VCA (vca);
167
168                         if (vca->init() || vca->set_state (**i, version)) {
169                                 error << _("Cannot set state of a VCA") << endmsg;
170                                 return -1;
171                         }
172
173                         /* can't hold the lock for the entire loop,
174                          * because the new VCA maybe slaved and needs
175                          * to call back into us to set up its own
176                          * slave/master relationship
177                          */
178
179                         {
180                                 Mutex::Lock lm (lock);
181                                 _vcas.push_back (vca);
182                                 vcal.push_back (vca);
183                         }
184                 }
185         }
186
187         _vcas_loaded = true;
188
189         VCAAdded (vcal); /* EMIT SIGNAL */
190
191         return 0;
192 }
193
194 void
195 VCAManager::clear_all_solo_state ()
196 {
197         Mutex::Lock lm (lock);
198
199         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
200                 (*i)->clear_all_solo_state ();
201         }
202 }