don't show monitor bus inputs in matrix(es); relabel tabs in port matrix; change...
[ardour.git] / gtk2_ardour / port_group.cc
1 /*
2     Copyright (C) 2002-2009 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 <cstring>
21 #include <boost/shared_ptr.hpp>
22
23 #include "midi++/manager.h"
24 #include "midi++/mmc.h"
25
26 #include "ardour/audio_track.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/bundle.h"
29 #include "ardour/user_bundle.h"
30 #include "ardour/io_processor.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/port.h"
33 #include "ardour/session.h"
34 #include "ardour/auditioner.h"
35 #include "ardour/control_protocol_manager.h"
36 #include "control_protocol/control_protocol.h"
37
38 #include "gui_thread.h"
39 #include "port_group.h"
40 #include "port_matrix.h"
41 #include "time_axis_view.h"
42 #include "public_editor.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace Gtk;
48 using namespace ARDOUR;
49
50 /** PortGroup constructor.
51  * @param n Name.
52  */
53 PortGroup::PortGroup (std::string const & n)
54         : name (n)
55 {
56
57 }
58
59 PortGroup::~PortGroup()
60 {
61         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
62                 delete *i;
63         }
64         _bundles.clear ();
65 }
66
67 /** Add a bundle to a group.
68  *  @param b Bundle.
69  *  @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
70  */
71 void
72 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
73 {
74         add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
75 }
76
77 /** Add a bundle to a group.
78  *  @param b Bundle.
79  *  @param io IO whose ports are in the bundle.
80  */
81 void
82 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
83 {
84         add_bundle_internal (b, io, false, Gdk::Color (), false);
85 }
86
87 /** Add a bundle to a group.
88  *  @param b Bundle.
89  *  @param c Colour to represent the bundle with.
90  */
91 void
92 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
93 {
94         add_bundle_internal (b, io, true, c, false);
95 }
96
97 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
98         : bundle (b)
99         , io (iop)
100         , colour (c)
101         , has_colour (has_c)
102 {
103 }
104
105 void
106 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
107 {
108         assert (b.get());
109
110         if (!allow_dups) {
111                 
112                 /* don't add this bundle if we already have one with the same ports */
113                 
114                 BundleList::iterator i = _bundles.begin ();
115                 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
116                         ++i;
117                 }
118                 
119                 if (i != _bundles.end ()) {
120                         return;
121                 }
122         }
123
124         BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
125         b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
126         _bundles.push_back (br);
127
128         Changed ();     
129 }
130
131 void
132 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
133 {
134         assert (b.get());
135
136         BundleList::iterator i = _bundles.begin ();
137         while (i != _bundles.end() && (*i)->bundle != b) {
138                 ++i;
139         }
140
141         if (i == _bundles.end()) {
142                 return;
143         }
144
145         delete *i;
146         _bundles.erase (i);
147
148         Changed ();
149 }
150
151 void
152 PortGroup::bundle_changed (Bundle::Change c)
153 {
154         BundleChanged (c);
155 }
156
157
158 void
159 PortGroup::clear ()
160 {
161         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
162                 delete *i;
163         }
164
165         _bundles.clear ();
166         Changed ();
167 }
168
169 bool
170 PortGroup::has_port (std::string const& p) const
171 {
172         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
173                 if ((*i)->bundle->offers_port_alone (p)) {
174                         return true;
175                 }
176         }
177
178         return false;
179 }
180
181 boost::shared_ptr<Bundle>
182 PortGroup::only_bundle ()
183 {
184         assert (_bundles.size() == 1);
185         return _bundles.front()->bundle;
186 }
187
188
189 ChanCount
190 PortGroup::total_channels () const
191 {
192         ChanCount n;
193         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
194                 n += (*i)->bundle->nchannels ();
195         }
196
197         return n;
198 }
199
200 boost::shared_ptr<IO>
201 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
202 {
203         BundleList::const_iterator i = _bundles.begin ();
204         while (i != _bundles.end() && (*i)->bundle != b) {
205                 ++i;
206         }
207
208         if (i == _bundles.end()) {
209                 return boost::shared_ptr<IO> ();
210         }
211
212         boost::shared_ptr<IO> io ((*i)->io.lock ());
213         return io;
214 }
215
216 /** Remove bundles whose channels are already represented by other, larger bundles */
217 void
218 PortGroup::remove_duplicates ()
219 {
220         BundleList::iterator i = _bundles.begin();
221         while (i != _bundles.end()) {
222
223                 BundleList::iterator tmp = i;
224                 ++tmp;
225
226                 bool remove = false;
227
228                 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
229
230                         if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
231                                 /* this bundle is larger */
232
233                                 uint32_t k = 0;
234                                 while (k < (*i)->bundle->nchannels().n_total()) {
235                                         /* see if this channel on *i has an equivalent on *j */
236                                         uint32_t l = 0;
237                                         while (l < (*j)->bundle->nchannels().n_total() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
238                                                 ++l;
239                                         }
240
241                                         if (l == (*j)->bundle->nchannels().n_total()) {
242                                                 /* it does not */
243                                                 break;
244                                         }
245
246                                         ++k;
247                                 }
248
249                                 if (k == (*i)->bundle->nchannels().n_total()) {
250                                         /* all channels on *i are represented by the larger bundle *j, so remove *i */
251                                         remove = true;
252                                         break;
253                                 }
254                         }
255                 }
256                 
257                 if (remove) {
258                         _bundles.erase (i);
259                 }
260                 
261                 i = tmp;
262         }
263 }
264
265
266 /** PortGroupList constructor.
267  */
268 PortGroupList::PortGroupList ()
269         : _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
270 {
271
272 }
273
274 PortGroupList::~PortGroupList() 
275 {
276         /* XXX need to clean up bundles, but ownership shared with PortGroups */
277 }
278
279 void
280 PortGroupList::maybe_add_processor_to_list (
281         boost::weak_ptr<Processor> wp, list<boost::shared_ptr<IO> >* route_ios, bool inputs, set<boost::shared_ptr<IO> >& used_io
282         )
283 {
284         boost::shared_ptr<Processor> p (wp.lock());
285
286         if (!p) {
287                 return;
288         }
289
290         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
291
292         if (iop) {
293
294                 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
295
296                 if (io && used_io.find (io) == used_io.end()) {
297                         route_ios->push_back (io);
298                         used_io.insert (io);
299                 }
300         }
301 }
302
303 struct RouteIOs {
304         RouteIOs (boost::shared_ptr<Route> r, boost::shared_ptr<IO> i) {
305                 route = r;
306                 ios.push_back (i);
307         }
308         
309         boost::shared_ptr<Route> route;
310         /* it's ok to use a shared_ptr here as RouteIOs structs are only used during ::gather () */
311         std::list<boost::shared_ptr<IO> > ios;
312 };
313
314 class RouteIOsComparator {
315 public:
316         bool operator() (RouteIOs const & a, RouteIOs const & b) {
317                 return a.route->order_key (X_("editor")) < b.route->order_key (X_("editor"));
318         }
319 };
320
321 /** Gather ports from around the system and put them in this PortGroupList.
322  *  @param type Type of ports to collect, or NIL for all types.
323  */
324 void
325 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups)
326 {
327         clear ();
328
329         if (session == 0) {
330                 return;
331         }
332
333         boost::shared_ptr<PortGroup> bus (new PortGroup (string_compose (PROGRAM_NAME " %1", _("Busses"))));
334         boost::shared_ptr<PortGroup> track (new PortGroup (string_compose (PROGRAM_NAME " %1", _("Tracks"))));
335         boost::shared_ptr<PortGroup> system (new PortGroup (_("Hardware")));
336         boost::shared_ptr<PortGroup> ardour (new PortGroup (string_compose (PROGRAM_NAME " %1", _("Misc"))));
337         boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
338
339         /* Find the IOs which have bundles for routes and their processors.  We store
340            these IOs in a RouteIOs class so that we can then sort the results by route
341            order key.
342         */
343
344         boost::shared_ptr<RouteList> routes = session->get_routes ();
345         list<RouteIOs> route_ios;
346
347         for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
348
349                 /* we never show the monitor bus inputs */
350
351                 if (inputs && (*i)->is_monitor()) {
352                         continue;
353                 }
354
355                 /* keep track of IOs that we have taken bundles from,
356                    so that we can avoid taking the same IO from both
357                    Route::output() and the main_outs Delivery 
358                 */
359
360                 set<boost::shared_ptr<IO> > used_io;
361                 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
362                 used_io.insert (io);
363
364                 RouteIOs rb (*i, io);
365                 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
366
367                 route_ios.push_back (rb);
368         }
369
370         /* Sort RouteIOs by the routes' editor order keys */
371         route_ios.sort (RouteIOsComparator ());
372
373         /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup.
374            Note that if the RouteIO's bundles are multi-type, we may make new Bundles
375            with only the ports of one type.
376         */
377         
378         for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
379                 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->route);
380
381                 /* Work out which group to put these IOs' bundles in */
382                 boost::shared_ptr<PortGroup> g;
383                 if (boost::dynamic_pointer_cast<Track> (i->route)) {
384                         g = track;
385                 } else {
386                         g = bus;
387                 }
388
389                 for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
390                         boost::shared_ptr<Bundle> b = bundle_for_type ((*j)->bundle(), type);
391                         if (b->nchannels() != ChanCount::ZERO) {
392                                 if (tv) {
393                                         g->add_bundle (b, *j, tv->color ());
394                                 } else {
395                                         g->add_bundle (b, *j);
396                                 }
397                         }
398                 }
399         }
400
401         /* Bundles owned by the session; add user bundles first, then normal ones, so
402            that UserBundles that offer the same ports as a normal bundle get priority
403         */
404
405         boost::shared_ptr<BundleList> b = session->bundles ();
406
407         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
408                 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs) {
409                         boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
410                         if (b->nchannels() != ChanCount::ZERO) {
411                                 system->add_bundle (b, allow_dups);
412                         }
413                 }
414         }
415
416         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
417                 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs) {
418                         boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
419                         if (b->nchannels() != ChanCount::ZERO) {
420                                 system->add_bundle (b, allow_dups);
421                         }
422                 }
423         }
424         
425         /* Ardour stuff */
426
427         if (!inputs) {
428                 boost::shared_ptr<Bundle> b = bundle_for_type (session->the_auditioner()->output()->bundle(), type);
429                 if (b->nchannels() != ChanCount::ZERO) {
430                         ardour->add_bundle (b);
431                 }
432
433                 b = bundle_for_type (session->click_io()->bundle(), type);
434                 if (b->nchannels() != ChanCount::ZERO) {
435                         ardour->add_bundle (b);
436                 }
437         }
438
439         /* Ardour's surfaces */
440
441         ControlProtocolManager& m = ControlProtocolManager::instance ();
442         for (list<ControlProtocolInfo*>::iterator i = m.control_protocol_info.begin(); i != m.control_protocol_info.end(); ++i) {
443                 if ((*i)->protocol) {
444                         list<boost::shared_ptr<Bundle> > b = (*i)->protocol->bundles ();
445                         for (list<boost::shared_ptr<Bundle> >::iterator j = b.begin(); j != b.end(); ++j) {
446                                 if ((*j)->ports_are_inputs() == inputs) {
447                                         ardour->add_bundle (*j);
448                                 }
449                         }
450                 }
451         }
452
453         /* Ardour's sync ports */
454
455         MIDI::Manager* midi_manager = MIDI::Manager::instance ();
456         if (midi_manager && (type == DataType::MIDI || type == DataType::NIL)) {
457                 boost::shared_ptr<Bundle> sync (new Bundle (_("Sync"), inputs));
458                 MIDI::MachineControl* mmc = midi_manager->mmc ();
459                 AudioEngine& ae = session->engine ();
460                 if (inputs) {
461                         sync->add_channel (
462                                 _("MTC in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->mtc_input_port()->name())
463                                 );
464                         sync->add_channel (
465                                 _("MIDI control in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_input_port()->name())
466                                 );
467                         sync->add_channel (
468                                 _("MIDI clock in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_clock_input_port()->name())
469                                 );
470                         sync->add_channel (
471                                 _("MMC in"), DataType::MIDI, ae.make_port_name_non_relative (mmc->input_port()->name())
472                                 );
473                 } else {
474                         sync->add_channel (
475                                 _("MTC out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->mtc_output_port()->name())
476                                 );
477                         sync->add_channel (
478                                 _("MIDI control out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_output_port()->name())
479                                 );
480                         sync->add_channel (
481                                 _("MIDI clock out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_clock_output_port()->name())
482                                 );
483                         sync->add_channel (
484                                 _("MMC out"), DataType::MIDI, ae.make_port_name_non_relative (mmc->output_port()->name())
485                                 );
486                 }
487                 
488                 ardour->add_bundle (sync);
489         }
490
491         /* Now find all other ports that we haven't thought of yet */
492
493         std::vector<std::string> extra_system[DataType::num_types];
494         std::vector<std::string> extra_other[DataType::num_types];
495
496         const char ** ports = 0;
497         if (type == DataType::NIL) {
498                 ports = session->engine().get_ports ("", "", inputs ? JackPortIsInput : JackPortIsOutput);
499         } else {
500                 ports = session->engine().get_ports ("", type.to_jack_type(), inputs ? JackPortIsInput : JackPortIsOutput);
501         }
502         
503         if (ports) {
504
505                 int n = 0;
506
507                 while (ports[n]) {
508
509                         std::string const p = ports[n];
510
511                         if (!system->has_port(p) &&
512                             !bus->has_port(p) &&
513                             !track->has_port(p) &&
514                             !ardour->has_port(p) &&
515                             !other->has_port(p)) {
516                                 
517                                 /* special hack: ignore MIDI ports labelled Midi-Through. these
518                                    are basically useless and mess things up for default
519                                    connections.
520                                 */
521
522                                 if (p.find ("Midi-Through") != string::npos) {
523                                         ++n;
524                                         continue;
525                                 }
526
527                                 /* can't use the audio engine for this as we are looking at non-Ardour ports */
528
529                                 jack_port_t* jp = jack_port_by_name (session->engine().jack(), p.c_str());
530                                 if (jp) {
531                                         DataType t (jack_port_type (jp));
532                                         if (t != DataType::NIL) {
533                                                 if (port_has_prefix (p, "system:") || port_has_prefix (p, "alsa_pcm") || port_has_prefix (p, "ardour:")) {
534                                                         extra_system[t].push_back (p);
535                                                 } else {
536                                                         extra_other[t].push_back (p);
537                                                 }
538                                         }
539                                 }
540                         }
541
542                         ++n;
543                 }
544
545                 free (ports);
546         }
547
548         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
549                 if (!extra_system[*i].empty()) {
550                         boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
551                         boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
552                         if (bt->nchannels() != ChanCount::ZERO) {
553                                 system->add_bundle (bt);
554                         }
555                 }
556         }
557
558         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
559                 if (!extra_other[*i].empty()) {
560                         boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_other[*i], *i, inputs);
561                         boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
562                         if (bt->nchannels() != ChanCount::ZERO) {
563                                 other->add_bundle (bt);
564                         }
565                 }
566         }
567
568         if (!allow_dups) {
569                 system->remove_duplicates ();
570         }
571
572         add_group_if_not_empty (other);
573         add_group_if_not_empty (bus);
574         add_group_if_not_empty (track);
575         add_group_if_not_empty (ardour);
576         add_group_if_not_empty (system);
577
578         emit_changed ();
579 }
580
581 boost::shared_ptr<Bundle>
582 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs) const
583 {
584         boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
585
586         std::string const pre = common_prefix (p);
587         if (!pre.empty()) {
588                 b->set_name (pre.substr (0, pre.length() - 1));
589         }
590
591         for (uint32_t j = 0; j < p.size(); ++j) {
592                 b->add_channel (p[j].substr (pre.length()), type);
593                 b->set_port (j, p[j]);
594         }
595
596         return b;
597 }
598
599 bool
600 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
601 {
602         return n.substr (0, p.length()) == p;
603 }
604
605 std::string
606 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
607 {
608         /* we must have some strings and the first must contain the separator string */
609         if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
610                 return "";
611         }
612
613         /* prefix of the first string */
614         std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
615
616         /* see if the other strings also start with fp */
617         uint32_t j = 1;
618         while (j < p.size()) {
619                 if (p[j].substr (0, fp.length()) != fp) {
620                         break;
621                 }
622                 ++j;
623         }
624
625         if (j != p.size()) {
626                 return "";
627         }
628
629         return fp;
630 }
631
632
633 std::string
634 PortGroupList::common_prefix (std::vector<std::string> const & p) const
635 {
636         /* common prefix before '/' ? */
637         std::string cp = common_prefix_before (p, "/");
638         if (!cp.empty()) {
639                 return cp;
640         }
641
642         cp = common_prefix_before (p, ":");
643         if (!cp.empty()) {
644                 return cp;
645         }
646
647         return "";
648 }
649
650 void
651 PortGroupList::clear ()
652 {
653         _groups.clear ();
654         _bundle_changed_connections.drop_connections ();
655         emit_changed ();
656 }
657
658
659 PortGroup::BundleList const &
660 PortGroupList::bundles () const
661 {
662         _bundles.clear ();
663
664         for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
665                 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
666         }
667
668         return _bundles;
669 }
670
671 ChanCount
672 PortGroupList::total_channels () const
673 {
674         ChanCount n;
675
676         for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
677                 n += (*i)->total_channels ();
678         }
679
680         return n;
681 }
682
683 void
684 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
685 {
686         if (!g->bundles().empty ()) {
687                 add_group (g);
688         }
689 }
690
691 void
692 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
693 {
694         _groups.push_back (g);
695
696         g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
697         g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
698
699         emit_changed ();
700 }
701
702 void
703 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
704 {
705         for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
706                 (*i)->remove_bundle (b);
707         }
708
709         emit_changed ();
710 }
711
712 void
713 PortGroupList::emit_changed ()
714 {
715         if (_signals_suspended) {
716                 _pending_change = true;
717         } else {
718                 Changed ();
719         }
720 }
721
722 void
723 PortGroupList::emit_bundle_changed (Bundle::Change c)
724 {
725         if (_signals_suspended) {
726                 _pending_bundle_change = c;
727         } else {
728                 BundleChanged (c);
729         }
730 }
731 void
732 PortGroupList::suspend_signals ()
733 {
734         _signals_suspended = true;
735 }
736
737 void
738 PortGroupList::resume_signals ()
739 {
740         if (_pending_change) {
741                 Changed ();
742                 _pending_change = false;
743         }
744
745         if (_pending_bundle_change != 0) {
746                 BundleChanged (_pending_bundle_change);
747                 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
748         }
749
750         _signals_suspended = false;
751 }
752
753 boost::shared_ptr<IO>
754 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
755 {
756         List::const_iterator i = _groups.begin ();
757         while (i != _groups.end()) {
758                 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
759                 if (io) {
760                         return io;
761                 }
762                 ++i;
763         }
764
765         return boost::shared_ptr<IO> ();
766 }
767
768 bool
769 PortGroupList::empty () const
770 {
771         List::const_iterator i = _groups.begin ();
772         while (i != _groups.end() && (*i)->total_channels() == ChanCount::ZERO) {
773                 ++i;
774         }
775
776         return (i == _groups.end());
777 }
778
779 /** Take a bundle, and either return it, if it contains only ports of type \a t,
780  *  or return a new bundle with those ports from \a b which are of type \a t.
781  *  Note that t == NIL is taken to mean "all types".
782  */
783 boost::shared_ptr<Bundle>
784 PortGroupList::bundle_for_type (boost::shared_ptr<Bundle> b, DataType t) const
785 {
786         /* We are asked for a bundle with all types, so that's easy */
787         if (t == DataType::NIL) {
788                 return b;
789         }
790
791         if (b->nchannels().get(t) == b->nchannels().n_total()) {
792                 /* All channels on b are of the correct type, so just return b */
793                 return b;
794         }
795
796         /* We must build a new bundle */
797         boost::shared_ptr<Bundle> n (new ARDOUR::Bundle (b->name(), b->ports_are_inputs()));
798         for (uint32_t i = 0; i < b->nchannels().n_total(); ++i) {
799                 if (b->channel_type(i) == t) {
800                         n->add_channel (b->channel_name (i), t, b->channel_ports (i));
801                 }
802         }
803
804         return n;
805 }