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