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