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