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