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