Fixes to bundle manager to make it vaguely usable.
[ardour.git] / libs / ardour / bundle.cc
1 /*
2     Copyright (C) 2002 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 <algorithm>
21
22 #include <pbd/failed_constructor.h>
23 #include <ardour/ardour.h>
24 #include <ardour/bundle.h>
25 #include <ardour/audioengine.h>
26 #include <pbd/xml++.h>
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 /** Construct an audio bundle.
34  *  @param i true if ports are inputs, otherwise false.
35  */
36 Bundle::Bundle (bool i)
37         : _type (DataType::AUDIO),
38           _ports_are_inputs (i),
39           _signals_suspended (false),
40           _pending_change (Change (0))
41 {
42
43 }
44
45
46 /** Construct an audio bundle.
47  *  @param n Name.
48  *  @param i true if ports are inputs, otherwise false.
49  */
50 Bundle::Bundle (std::string const & n, bool i)
51         : _name (n),
52           _type (DataType::AUDIO),
53           _ports_are_inputs (i),
54           _signals_suspended (false),
55           _pending_change (Change (0))
56 {
57
58 }
59
60
61 /** Construct a bundle.
62  *  @param n Name.
63  *  @param t Type.
64  *  @param i true if ports are inputs, otherwise false.
65  */
66 Bundle::Bundle (std::string const & n, DataType t, bool i)
67         : _name (n),
68           _type (t),
69           _ports_are_inputs (i),
70           _signals_suspended (false),
71           _pending_change (Change (0))
72 {
73
74 }
75
76
77 Bundle::Bundle (boost::shared_ptr<Bundle> other)
78         : _channel (other->_channel),
79           _name (other->_name),
80           _type (other->_type),
81           _ports_are_inputs (other->_ports_are_inputs),
82           _signals_suspended (other->_signals_suspended),
83           _pending_change (other->_pending_change)
84 {
85         
86 }
87
88 uint32_t
89 Bundle::nchannels () const
90 {
91         Glib::Mutex::Lock lm (_channel_mutex);
92         return _channel.size ();
93 }
94
95 Bundle::PortList const &
96 Bundle::channel_ports (uint32_t c) const
97 {
98         assert (c < nchannels());
99
100         Glib::Mutex::Lock lm (_channel_mutex);
101         return _channel[c].ports;
102 }
103
104 /** Add an association between one of our channels and a port.
105  *  @param ch Channel index.
106  *  @param portname full port name to associate with (including prefix).
107  */
108 void
109 Bundle::add_port_to_channel (uint32_t ch, string portname)
110 {
111         assert (ch < nchannels());
112         assert (portname.find_first_of (':') != string::npos);
113
114         {
115                 Glib::Mutex::Lock lm (_channel_mutex);
116                 _channel[ch].ports.push_back (portname);
117         }
118
119         emit_changed (PortsChanged);
120 }
121
122 /** Disassociate a port from one of our channels.
123  *  @param ch Channel index.
124  *  @param portname port name to disassociate from.
125  */
126 void
127 Bundle::remove_port_from_channel (uint32_t ch, string portname)
128 {
129         assert (ch < nchannels());
130
131         bool changed = false;
132
133         {
134                 Glib::Mutex::Lock lm (_channel_mutex);
135                 PortList& pl = _channel[ch].ports;
136                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
137                 
138                 if (i != pl.end()) {
139                         pl.erase (i);
140                         changed = true;
141                 }
142         }
143
144         if (changed) {
145                 emit_changed (PortsChanged);
146         }
147 }
148
149 /** Set a single port to be associated with a channel, removing any others.
150  *  @param ch Channel.
151  *  @param portname Full port name, including prefix.
152  */
153 void
154 Bundle::set_port (uint32_t ch, string portname)
155 {
156         assert (ch < nchannels());
157         assert (portname.find_first_of (':') != string::npos);
158
159         {
160                 Glib::Mutex::Lock lm (_channel_mutex);
161                 _channel[ch].ports.clear ();
162                 _channel[ch].ports.push_back (portname);
163         }
164
165         emit_changed (PortsChanged);
166 }
167
168 /** @param n Channel name */
169 void
170 Bundle::add_channel (std::string const & n)
171 {
172         {
173                 Glib::Mutex::Lock lm (_channel_mutex);
174                 _channel.push_back (Channel (n));
175         }
176
177         emit_changed (ConfigurationChanged);
178 }
179
180 bool
181 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
182 {
183         assert (ch < nchannels());
184         
185         Glib::Mutex::Lock lm (_channel_mutex);
186         return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
187 }
188
189 /** Remove a channel.
190  *  @param ch Channel.
191  */
192 void
193 Bundle::remove_channel (uint32_t ch)
194 {
195         assert (ch < nchannels ());
196
197         Glib::Mutex::Lock lm (_channel_mutex);
198         _channel.erase (_channel.begin () + ch);
199 }
200
201 /** Remove all channels */
202 void
203 Bundle::remove_channels ()
204 {
205         Glib::Mutex::Lock lm (_channel_mutex);
206
207         _channel.clear ();
208 }
209
210 /** @param p Port name.
211  *  @return true if any channel is associated with p.
212  */
213 bool
214 Bundle::uses_port (std::string p) const
215 {
216         Glib::Mutex::Lock lm (_channel_mutex);
217
218         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
219                 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
220                         if (*j == p) {
221                                 return true;
222                         }
223                 }
224         }
225
226         return false;
227 }
228
229 /** @param p Port name.
230  *  @return true if this bundle offers this port on its own on a channel.
231  */
232 bool
233 Bundle::offers_port_alone (std::string p) const
234 {
235         Glib::Mutex::Lock lm (_channel_mutex);
236
237         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
238                 if (i->ports.size() == 1 && i->ports[0] == p) {
239                         return true;
240                 }
241         }
242
243         return false;
244 }
245
246
247 /** @param ch Channel.
248  *  @return Channel name.
249  */
250 std::string
251 Bundle::channel_name (uint32_t ch) const
252 {
253         assert (ch < nchannels());
254
255         Glib::Mutex::Lock lm (_channel_mutex);
256         return _channel[ch].name;
257 }
258
259 /** Set the name of a channel.
260  *  @param ch Channel.
261  *  @param n New name.
262  */
263 void
264 Bundle::set_channel_name (uint32_t ch, std::string const & n)
265 {
266         assert (ch < nchannels());
267
268         {
269                 Glib::Mutex::Lock lm (_channel_mutex);
270                 _channel[ch].name = n;
271         }
272
273         emit_changed (NameChanged);
274 }
275
276 /** Take the channels from another bundle and add them to this bundle,
277  *  so that channels from other are added to this (with their ports)
278  *  and are named "<other_bundle_name> <other_channel_name>".
279  */
280 void
281 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
282 {
283         uint32_t const ch = nchannels ();
284         
285         for (uint32_t i = 0; i < other->nchannels(); ++i) {
286
287                 std::stringstream s;
288                 s << other->name() << " " << other->channel_name(i);
289
290                 add_channel (s.str());
291
292                 PortList const& pl = other->channel_ports (i);
293                 for (uint32_t j = 0; j < pl.size(); ++j) {
294                         add_port_to_channel (ch + i, pl[j]);
295                 }
296         }
297 }
298
299 /** Connect the ports associated with our channels to the ports associated
300  *  with another bundle's channels.
301  *  @param other Other bundle.
302  *  @param engine AudioEngine to use to make the connections.
303  */
304 void
305 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
306 {
307         uint32_t const N = nchannels ();
308         assert (N == other->nchannels ());
309
310         for (uint32_t i = 0; i < N; ++i) {
311                 Bundle::PortList const & our_ports = channel_ports (i);
312                 Bundle::PortList const & other_ports = other->channel_ports (i);
313
314                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
315                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
316                                 engine.connect (*j, *k);
317                         }
318                 }
319         }
320 }
321
322 void
323 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
324 {
325         uint32_t const N = nchannels ();
326         assert (N == other->nchannels ());
327
328         for (uint32_t i = 0; i < N; ++i) {
329                 Bundle::PortList const & our_ports = channel_ports (i);
330                 Bundle::PortList const & other_ports = other->channel_ports (i);
331
332                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
333                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
334                                 engine.disconnect (*j, *k);
335                         }
336                 }
337         }
338 }
339
340 /** Remove all ports from all channels */
341 void
342 Bundle::remove_ports_from_channels ()
343 {
344         {
345                 Glib::Mutex::Lock lm (_channel_mutex);
346                 for (uint32_t c = 0; c < _channel.size(); ++c) {
347                         _channel[c].ports.clear ();
348                 }
349
350         }
351
352         emit_changed (PortsChanged);
353 }
354
355 /** Remove all ports from a given channel.
356  *  @param ch Channel.
357  */
358 void
359 Bundle::remove_ports_from_channel (uint32_t ch)
360 {
361         assert (ch < nchannels ());
362         
363         {
364                 Glib::Mutex::Lock lm (_channel_mutex);
365                 _channel[ch].ports.clear ();
366         }
367
368         emit_changed (PortsChanged);
369 }
370
371 void
372 Bundle::suspend_signals ()
373 {
374         _signals_suspended = true;
375 }
376
377 void
378 Bundle::resume_signals ()
379 {
380         if (_pending_change) {
381                 Changed (_pending_change);
382                 _pending_change = Change (0);
383         }
384
385         _signals_suspended = false;
386 }
387
388 void
389 Bundle::emit_changed (Change c)
390 {
391         if (_signals_suspended) {
392                 _pending_change = Change (int (_pending_change) | int (c));
393         } else {
394                 Changed (c);
395         }
396 }
397