2 Copyright (C) 2002-2009 Paul Davis
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.
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.
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.
21 #include <boost/shared_ptr.hpp>
23 #include "ardour/audio_track.h"
24 #include "ardour/audioengine.h"
25 #include "ardour/bundle.h"
26 #include "ardour/user_bundle.h"
27 #include "ardour/io_processor.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/port.h"
30 #include "ardour/session.h"
31 #include "ardour/auditioner.h"
33 #include "gui_thread.h"
34 #include "port_group.h"
35 #include "port_matrix.h"
36 #include "time_axis_view.h"
37 #include "public_editor.h"
43 using namespace ARDOUR;
45 /** PortGroup constructor.
48 PortGroup::PortGroup (std::string const & n)
54 PortGroup::~PortGroup()
56 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
62 /** Add a bundle to a group.
64 * @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
67 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
69 add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
72 /** Add a bundle to a group.
74 * @param io IO whose ports are in the bundle.
77 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
79 add_bundle_internal (b, io, false, Gdk::Color (), false);
82 /** Add a bundle to a group.
84 * @param c Colour to represent the bundle with.
87 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
89 add_bundle_internal (b, io, true, c, false);
92 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
101 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
107 /* don't add this bundle if we already have one with the same ports */
109 BundleList::iterator i = _bundles.begin ();
110 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
114 if (i != _bundles.end ()) {
119 BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
120 b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
121 _bundles.push_back (br);
127 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
131 BundleList::iterator i = _bundles.begin ();
132 while (i != _bundles.end() && (*i)->bundle != b) {
136 if (i == _bundles.end()) {
147 PortGroup::bundle_changed (Bundle::Change c)
156 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
165 PortGroup::has_port (std::string const& p) const
167 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
168 if ((*i)->bundle->offers_port_alone (p)) {
176 boost::shared_ptr<Bundle>
177 PortGroup::only_bundle ()
179 assert (_bundles.size() == 1);
180 return _bundles.front()->bundle;
185 PortGroup::total_channels () const
188 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
189 n += (*i)->bundle->nchannels ();
195 boost::shared_ptr<IO>
196 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
198 BundleList::const_iterator i = _bundles.begin ();
199 while (i != _bundles.end() && (*i)->bundle != b) {
203 if (i == _bundles.end()) {
204 return boost::shared_ptr<IO> ();
210 /** Remove bundles whose channels are already represented by other, larger bundles */
212 PortGroup::remove_duplicates ()
214 BundleList::iterator i = _bundles.begin();
215 while (i != _bundles.end()) {
217 BundleList::iterator tmp = i;
222 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
224 if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
225 /* this bundle is larger */
228 while (k < (*i)->bundle->nchannels().n_total()) {
229 /* see if this channel on *i has an equivalent on *j */
231 while (l < (*j)->bundle->nchannels().n_total() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
235 if (l == (*j)->bundle->nchannels().n_total()) {
243 if (k == (*i)->bundle->nchannels().n_total()) {
244 /* all channels on *i are represented by the larger bundle *j, so remove *i */
260 /** PortGroupList constructor.
262 PortGroupList::PortGroupList ()
263 : _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
268 PortGroupList::~PortGroupList()
270 /* XXX need to clean up bundles, but ownership shared with PortGroups */
274 PortGroupList::maybe_add_processor_to_list (
275 boost::weak_ptr<Processor> wp, list<boost::shared_ptr<IO> >* route_ios, bool inputs, set<boost::shared_ptr<IO> >& used_io
278 boost::shared_ptr<Processor> p (wp.lock());
284 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
288 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
290 if (io && used_io.find (io) == used_io.end()) {
291 route_ios->push_back (io);
298 RouteIOs (boost::shared_ptr<Route> r, boost::shared_ptr<IO> i) {
303 boost::shared_ptr<Route> route;
304 std::list<boost::shared_ptr<IO> > ios;
307 class RouteIOsComparator {
309 bool operator() (RouteIOs const & a, RouteIOs const & b) {
310 return a.route->order_key (X_("editor")) < b.route->order_key (X_("editor"));
314 /** Gather ports from around the system and put them in this PortGroupList.
315 * @param type Type of ports to collect, or NIL for all types.
318 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups)
326 boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
327 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
328 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
329 boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
330 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
332 /* Find the IOs which have bundles for routes and their processors. We store
333 these IOs in a RouteIOs class so that we can then sort the results by route
337 boost::shared_ptr<RouteList> routes = session->get_routes ();
338 list<RouteIOs> route_ios;
340 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
342 /* keep track of IOs that we have taken bundles from,
343 so that we can avoid taking the same IO from both
344 Route::output() and the main_outs Delivery */
346 set<boost::shared_ptr<IO> > used_io;
347 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
350 RouteIOs rb (*i, io);
351 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
353 route_ios.push_back (rb);
356 /* Sort RouteIOs by the routes' editor order keys */
357 route_ios.sort (RouteIOsComparator ());
359 /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup.
360 Note that if the RouteIO's bundles are multi-type, we may make new Bundles
361 with only the ports of one type.
364 for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
365 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->route);
367 /* Work out which group to put these IOs' bundles in */
368 boost::shared_ptr<PortGroup> g;
369 if (boost::dynamic_pointer_cast<Track> (i->route)) {
375 for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
376 boost::shared_ptr<Bundle> b = bundle_for_type ((*j)->bundle(), type);
379 g->add_bundle (b, *j, tv->color ());
381 g->add_bundle (b, *j);
387 /* Bundles owned by the session; add user bundles first, then normal ones, so
388 that UserBundles that offer the same ports as a normal bundle get priority
391 boost::shared_ptr<BundleList> b = session->bundles ();
393 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
394 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs) {
395 boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
397 system->add_bundle (b, allow_dups);
402 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
403 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs) {
404 boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
406 system->add_bundle (b, allow_dups);
414 boost::shared_ptr<Bundle> b = bundle_for_type (session->the_auditioner()->output()->bundle(), type);
416 ardour->add_bundle (b);
419 b = bundle_for_type (session->click_io()->bundle(), type);
421 ardour->add_bundle (b);
425 /* Now find all other ports that we haven't thought of yet */
427 std::vector<std::string> extra_system[DataType::num_types];
428 std::vector<std::string> extra_other[DataType::num_types];
430 const char ** ports = 0;
431 if (type == DataType::NIL) {
432 ports = session->engine().get_ports ("", "", inputs ? JackPortIsInput : JackPortIsOutput);
434 ports = session->engine().get_ports ("", type.to_jack_type(), inputs ? JackPortIsInput : JackPortIsOutput);
443 std::string const p = ports[n];
445 if (!system->has_port(p) &&
447 !track->has_port(p) &&
448 !ardour->has_port(p) &&
449 !other->has_port(p)) {
451 /* special hack: ignore MIDI ports labelled Midi-Through. these
452 are basically useless and mess things up for default
456 if (p.find ("MIDI-Through") != string::npos) {
461 /* can't use the audio engine for this as we are looking at non-Ardour ports */
463 jack_port_t* jp = jack_port_by_name (session->engine().jack(), p.c_str());
465 DataType t (jack_port_type (jp));
466 if (t != DataType::NIL) {
467 if (port_has_prefix (p, "system:") || port_has_prefix (p, "alsa_pcm") || port_has_prefix (p, "ardour:")) {
468 extra_system[t].push_back (p);
470 extra_other[t].push_back (p);
482 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
483 if (!extra_system[*i].empty()) {
484 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
485 boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
487 system->add_bundle (bt);
492 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
493 if (!extra_other[*i].empty()) {
494 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_other[*i], *i, inputs);
495 boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
497 other->add_bundle (bt);
503 system->remove_duplicates ();
506 add_group_if_not_empty (other);
507 add_group_if_not_empty (bus);
508 add_group_if_not_empty (track);
509 add_group_if_not_empty (ardour);
510 add_group_if_not_empty (system);
515 boost::shared_ptr<Bundle>
516 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs) const
518 boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
520 std::string const pre = common_prefix (p);
522 b->set_name (pre.substr (0, pre.length() - 1));
525 for (uint32_t j = 0; j < p.size(); ++j) {
526 b->add_channel (p[j].substr (pre.length()), type);
527 b->set_port (j, p[j]);
534 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
536 return n.substr (0, p.length()) == p;
540 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
542 /* we must have some strings and the first must contain the separator string */
543 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
547 /* prefix of the first string */
548 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
550 /* see if the other strings also start with fp */
552 while (j < p.size()) {
553 if (p[j].substr (0, fp.length()) != fp) {
568 PortGroupList::common_prefix (std::vector<std::string> const & p) const
570 /* common prefix before '/' ? */
571 std::string cp = common_prefix_before (p, "/");
576 cp = common_prefix_before (p, ":");
585 PortGroupList::clear ()
588 _bundle_changed_connections.drop_connections ();
593 PortGroup::BundleList const &
594 PortGroupList::bundles () const
598 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
599 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
606 PortGroupList::total_channels () const
610 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
611 n += (*i)->total_channels ();
618 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
620 if (!g->bundles().empty ()) {
626 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
628 _groups.push_back (g);
630 g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
631 g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
637 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
639 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
640 (*i)->remove_bundle (b);
647 PortGroupList::emit_changed ()
649 if (_signals_suspended) {
650 _pending_change = true;
657 PortGroupList::emit_bundle_changed (Bundle::Change c)
659 if (_signals_suspended) {
660 _pending_bundle_change = c;
666 PortGroupList::suspend_signals ()
668 _signals_suspended = true;
672 PortGroupList::resume_signals ()
674 if (_pending_change) {
676 _pending_change = false;
679 if (_pending_bundle_change != 0) {
680 BundleChanged (_pending_bundle_change);
681 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
684 _signals_suspended = false;
687 boost::shared_ptr<IO>
688 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
690 List::const_iterator i = _groups.begin ();
691 while (i != _groups.end()) {
692 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
699 return boost::shared_ptr<IO> ();
703 PortGroupList::empty () const
705 List::const_iterator i = _groups.begin ();
706 while (i != _groups.end() && (*i)->total_channels() == ChanCount::ZERO) {
710 return (i == _groups.end());
713 /** Take a bundle, and either return it, if it contains only ports of type \a t,
714 * or return a new bundle with those ports from \a b which are of type \a t.
715 * Note that t == NIL is taken to mean "all types".
717 boost::shared_ptr<Bundle>
718 PortGroupList::bundle_for_type (boost::shared_ptr<Bundle> b, DataType t) const
720 /* We are asked for a bundle with all types, so that's easy */
721 if (t == DataType::NIL) {
725 if (b->nchannels().get(t) == b->nchannels().n_total()) {
726 /* All channels on b are of the correct type, so just return b */
730 /* We must build a new bundle */
731 boost::shared_ptr<Bundle> n;
732 for (uint32_t i = 0; i < b->nchannels().n_total(); ++i) {
733 if (b->channel_type(i) == t) {
734 n->add_channel (b->channel_name (i), t, b->channel_ports (i));