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