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