fix [some] issues when adding/removing output ports
[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 "ardour/bundle.h"
23 #include "ardour/audioengine.h"
24 #include "ardour/port.h"
25
26 #include "i18n.h"
27
28 using namespace std;
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 /** Construct an audio bundle.
33  *  @param i true if ports are inputs, otherwise false.
34  */
35 Bundle::Bundle (bool i)
36         : _ports_are_inputs (i),
37           _signals_suspended (false),
38           _pending_change (Change (0))
39 {
40
41 }
42
43
44 /** Construct an audio bundle.
45  *  @param n Name.
46  *  @param i true if ports are inputs, otherwise false.
47  */
48 Bundle::Bundle (std::string const & n, bool i)
49         : _name (n),
50           _ports_are_inputs (i),
51           _signals_suspended (false),
52           _pending_change (Change (0))
53 {
54
55 }
56
57 Bundle::Bundle (boost::shared_ptr<Bundle> other)
58         : _channel (other->_channel),
59           _name (other->_name),
60           _ports_are_inputs (other->_ports_are_inputs),
61           _signals_suspended (other->_signals_suspended),
62           _pending_change (other->_pending_change)
63 {
64
65 }
66
67 ChanCount
68 Bundle::nchannels () const
69 {
70         Glib::Threads::Mutex::Lock lm (_channel_mutex);
71
72         ChanCount c;
73         for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
74                 c.set (i->type, c.get (i->type) + 1);
75         }
76
77         return c;
78 }
79
80 Bundle::PortList const &
81 Bundle::channel_ports (uint32_t c) const
82 {
83         assert (c < nchannels().n_total());
84
85         Glib::Threads::Mutex::Lock lm (_channel_mutex);
86         return _channel[c].ports;
87 }
88
89 /** Add an association between one of our channels and a port.
90  *  @param ch Channel index.
91  *  @param portname full port name to associate with (including prefix).
92  */
93 void
94 Bundle::add_port_to_channel (uint32_t ch, string portname)
95 {
96         assert (ch < nchannels().n_total());
97         assert (portname.find_first_of (':') != string::npos);
98
99         {
100                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
101                 _channel[ch].ports.push_back (portname);
102         }
103
104         emit_changed (PortsChanged);
105 }
106
107 /** Disassociate a port from one of our channels.
108  *  @param ch Channel index.
109  *  @param portname port name to disassociate from.
110  */
111 void
112 Bundle::remove_port_from_channel (uint32_t ch, string portname)
113 {
114         assert (ch < nchannels().n_total());
115
116         bool changed = false;
117
118         {
119                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
120                 PortList& pl = _channel[ch].ports;
121                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
122
123                 if (i != pl.end()) {
124                         pl.erase (i);
125                         changed = true;
126                 }
127         }
128
129         if (changed) {
130                 emit_changed (PortsChanged);
131         }
132 }
133
134 /** Set a single port to be associated with a channel, removing any others.
135  *  @param ch Channel.
136  *  @param portname Full port name, including prefix.
137  */
138 void
139 Bundle::set_port (uint32_t ch, string portname)
140 {
141         assert (ch < nchannels().n_total());
142         assert (portname.find_first_of (':') != string::npos);
143
144         {
145                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
146                 _channel[ch].ports.clear ();
147                 _channel[ch].ports.push_back (portname);
148         }
149
150         emit_changed (PortsChanged);
151 }
152
153 /** @param n Channel name */
154 void
155 Bundle::add_channel (std::string const & n, DataType t)
156 {
157         {
158                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
159                 _channel.push_back (Channel (n, t));
160         }
161
162         emit_changed (ConfigurationChanged);
163 }
164
165 /** @param n Channel name */
166 void
167 Bundle::add_channel (std::string const & n, DataType t, PortList p)
168 {
169         {
170                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
171                 _channel.push_back (Channel (n, t, p));
172         }
173
174         emit_changed (ConfigurationChanged);
175 }
176
177 /** @param n Channel name */
178 void
179 Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
180 {
181         {
182                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
183                 _channel.push_back (Channel (n, t, p));
184         }
185
186         emit_changed (ConfigurationChanged);
187 }
188
189 bool
190 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
191 {
192         assert (ch < nchannels().n_total());
193
194         Glib::Threads::Mutex::Lock lm (_channel_mutex);
195         return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
196 }
197
198 /** Remove a channel.
199  *  @param ch Channel.
200  */
201 void
202 Bundle::remove_channel (uint32_t ch)
203 {
204         assert (ch < nchannels().n_total());
205
206         Glib::Threads::Mutex::Lock lm (_channel_mutex);
207         _channel.erase (_channel.begin () + ch);
208
209         emit_changed (ConfigurationChanged);
210 }
211
212 /** Remove all channels */
213 void
214 Bundle::remove_channels ()
215 {
216         Glib::Threads::Mutex::Lock lm (_channel_mutex);
217
218         _channel.clear ();
219
220         emit_changed (ConfigurationChanged);
221 }
222
223 /** @param p Port name.
224  *  @return true if any channel is associated with p.
225  */
226 bool
227 Bundle::offers_port (std::string p) const
228 {
229         Glib::Threads::Mutex::Lock lm (_channel_mutex);
230
231         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
232                 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
233                         if (*j == p) {
234                                 return true;
235                         }
236                 }
237         }
238
239         return false;
240 }
241
242 /** @param p Port name.
243  *  @return true if this bundle offers this port on its own on a channel.
244  */
245 bool
246 Bundle::offers_port_alone (std::string p) const
247 {
248         Glib::Threads::Mutex::Lock lm (_channel_mutex);
249
250         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
251                 if (i->ports.size() == 1 && i->ports[0] == p) {
252                         return true;
253                 }
254         }
255
256         return false;
257 }
258
259
260 /** @param ch Channel.
261  *  @return Channel name.
262  */
263 std::string
264 Bundle::channel_name (uint32_t ch) const
265 {
266         assert (ch < nchannels().n_total());
267
268         Glib::Threads::Mutex::Lock lm (_channel_mutex);
269         return _channel[ch].name;
270 }
271
272 /** Set the name of a channel.
273  *  @param ch Channel.
274  *  @param n New name.
275  */
276 void
277 Bundle::set_channel_name (uint32_t ch, std::string const & n)
278 {
279         assert (ch < nchannels().n_total());
280
281         {
282                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
283                 _channel[ch].name = n;
284         }
285
286         emit_changed (NameChanged);
287 }
288
289 /** Take the channels from another bundle and add them to this bundle,
290  *  so that channels from other are added to this (with their ports)
291  *  and are named "<other_bundle_name> <other_channel_name>".
292  */
293 void
294 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
295 {
296         uint32_t const ch = nchannels().n_total();
297
298         for (uint32_t i = 0; i < other->nchannels().n_total(); ++i) {
299
300                 std::stringstream s;
301                 s << other->name() << " " << other->channel_name(i);
302
303                 add_channel (s.str(), other->channel_type(i));
304
305                 PortList const& pl = other->channel_ports (i);
306                 for (uint32_t j = 0; j < pl.size(); ++j) {
307                         add_port_to_channel (ch + i, pl[j]);
308                 }
309         }
310 }
311
312 /** Connect the ports associated with our channels to the ports associated
313  *  with another bundle's channels.
314  *  @param other Other bundle.
315  *  @param engine AudioEngine to use to make the connections.
316  */
317 void
318 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
319 {
320         uint32_t const N = nchannels().n_total();
321         assert (N == other->nchannels().n_total());
322
323         for (uint32_t i = 0; i < N; ++i) {
324                 Bundle::PortList const & our_ports = channel_ports (i);
325                 Bundle::PortList const & other_ports = other->channel_ports (i);
326
327                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
328                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
329                                 engine.connect (*j, *k);
330                         }
331                 }
332         }
333 }
334
335 void
336 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
337 {
338         uint32_t const N = nchannels().n_total();
339         assert (N == other->nchannels().n_total());
340
341         for (uint32_t i = 0; i < N; ++i) {
342                 Bundle::PortList const & our_ports = channel_ports (i);
343                 Bundle::PortList const & other_ports = other->channel_ports (i);
344
345                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
346                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
347                                 engine.disconnect (*j, *k);
348                         }
349                 }
350         }
351 }
352
353 /** Remove all ports from all channels */
354 void
355 Bundle::remove_ports_from_channels ()
356 {
357         {
358                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
359                 for (uint32_t c = 0; c < _channel.size(); ++c) {
360                         _channel[c].ports.clear ();
361                 }
362
363         }
364
365         emit_changed (PortsChanged);
366 }
367
368 /** Remove all ports from a given channel.
369  *  @param ch Channel.
370  */
371 void
372 Bundle::remove_ports_from_channel (uint32_t ch)
373 {
374         assert (ch < nchannels().n_total());
375
376         {
377                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
378                 _channel[ch].ports.clear ();
379         }
380
381         emit_changed (PortsChanged);
382 }
383
384 void
385 Bundle::suspend_signals ()
386 {
387         _signals_suspended = true;
388 }
389
390 void
391 Bundle::resume_signals ()
392 {
393         if (_pending_change) {
394                 Changed (_pending_change);
395                 _pending_change = Change (0);
396         }
397
398         _signals_suspended = false;
399 }
400
401 void
402 Bundle::emit_changed (Change c)
403 {
404         if (_signals_suspended) {
405                 _pending_change = Change (int (_pending_change) | int (c));
406         } else {
407                 Changed (c);
408         }
409 }
410
411 bool
412 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
413 {
414         if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) {
415                 return false;
416         }
417
418         for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
419                 Bundle::PortList const & A = channel_ports (i);
420                 Bundle::PortList const & B = other->channel_ports (i);
421
422                 for (uint32_t j = 0; j < A.size(); ++j) {
423                         for (uint32_t k = 0; k < B.size(); ++k) {
424
425                                 boost::shared_ptr<Port> p = engine.get_port_by_name (A[j]);
426                                 boost::shared_ptr<Port> q = engine.get_port_by_name (B[k]);
427
428                                 if (!p && !q) {
429                                         return false;
430                                 }
431
432                                 if (p && !p->connected_to (B[k])) {
433                                         return false;
434                                 } else if (q && !q->connected_to (A[j])) {
435                                         return false;
436                                 }
437                         }
438                 }
439         }
440
441         return true;
442 }
443
444 /** This must not be called in code executed as a response to a JACK event,
445  *  as it uses jack_port_get_all_connections().
446  *  @return true if any of this bundle's channels are connected to anything.
447  */
448 bool
449 Bundle::connected_to_anything (AudioEngine& engine)
450 {
451         for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
452                 Bundle::PortList const & ports = channel_ports (i);
453
454                 for (uint32_t j = 0; j < ports.size(); ++j) {
455                         /* ports[j] may not be an Ardour port, so use JACK directly
456                            rather than doing it with Port.
457                         */
458                         jack_port_t* jp = jack_port_by_name (engine.jack(), ports[j].c_str());
459                         if (jp) {
460                                 const char ** c = jack_port_get_all_connections (engine.jack(), jp);
461                                 if (c) {
462                                         jack_free (c);
463                                         return true;
464                                 }
465                         }
466                 }
467         }
468
469         return false;
470 }
471
472 void
473 Bundle::set_ports_are_inputs ()
474 {
475         _ports_are_inputs = true;
476         emit_changed (DirectionChanged);
477 }
478
479 void
480 Bundle::set_ports_are_outputs ()
481 {
482         _ports_are_inputs = false;
483         emit_changed (DirectionChanged);
484 }
485
486 /** Set the name.
487  *  @param n New name.
488  */
489 void
490 Bundle::set_name (string const & n)
491 {
492         _name = n;
493         emit_changed (NameChanged);
494 }
495
496 /** @param b Other bundle.
497  *  @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
498  */
499 bool
500 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
501 {
502         uint32_t const N = nchannels().n_total();
503
504         if (b->nchannels().n_total() != N) {
505                 return false;
506         }
507
508         /* XXX: probably should sort channel port lists before comparing them */
509
510         for (uint32_t i = 0; i < N; ++i) {
511                 if (channel_ports (i) != b->channel_ports (i)) {
512                         return false;
513                 }
514         }
515
516         return true;
517 }
518
519 DataType
520 Bundle::channel_type (uint32_t c) const
521 {
522         assert (c < nchannels().n_total());
523
524         Glib::Threads::Mutex::Lock lm (_channel_mutex);
525         return _channel[c].type;
526 }
527
528 ostream &
529 operator<< (ostream& os, Bundle const & b)
530 {
531         os << "BUNDLE " << b.nchannels() << " channels: ";
532         for (uint32_t i = 0; i < b.nchannels().n_total(); ++i) {
533                 os << "( ";
534                 Bundle::PortList const & pl = b.channel_ports (i);
535                 for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
536                         os << *j << " ";
537                 }
538                 os << ") ";
539         }
540
541         return os;
542 }
543
544 bool
545 Bundle::operator== (Bundle const & other)
546 {
547         return _channel == other._channel;
548 }
549
550 /** Given an index of a channel as the nth channel of a particular type,
551  *  return an index of that channel when considering channels of all types.
552  *
553  *  e.g. given a bundle with channels:
554  *          fred   [audio]
555  *          jim    [audio]
556  *          sheila [midi]
557  *
558  * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
559  * 0th MIDI channel.
560  *
561  * If t == NIL, we just return c.
562  */
563
564 uint32_t
565 Bundle::type_channel_to_overall (DataType t, uint32_t c) const
566 {
567         if (t == DataType::NIL) {
568                 return c;
569         }
570         
571         Glib::Threads::Mutex::Lock lm (_channel_mutex);
572
573         vector<Channel>::const_iterator i = _channel.begin ();
574
575         uint32_t o = 0;
576
577         while (1) {
578
579                 assert (i != _channel.end ());
580
581                 if (i->type != t) {
582                         ++i;
583                 } else {
584                         if (c == 0) {
585                                 return o;
586                         }
587                         --c;
588                 }
589
590                 ++o;
591         }
592
593         /* NOTREACHED */
594         return -1;
595 }
596
597 /** Perform the reverse of type_channel_to_overall */
598 uint32_t
599 Bundle::overall_channel_to_type (DataType t, uint32_t c) const
600 {
601         if (t == DataType::NIL) {
602                 return c;
603         }
604         
605         Glib::Threads::Mutex::Lock lm (_channel_mutex);
606
607         uint32_t s = 0;
608         
609         vector<Channel>::const_iterator i = _channel.begin ();
610         for (uint32_t j = 0; j < c; ++j) {
611                 if (i->type == t) {
612                         ++s;
613                 }
614                 ++i;
615         }
616
617         return s;
618 }