Repair thinko in previous commit.
[ardour.git] / gtk2_ardour / port_group.cc
index 0af3c24ed9202e5198447b3c35382377963b7a06..a3a7ca8e77cfa2ea64349458847f4bff537e0527 100644 (file)
 
 #include <cstring>
 #include <boost/shared_ptr.hpp>
+#include <boost/algorithm/string.hpp>
+
+#include "midi++/manager.h"
+#include "midi++/mmc.h"
 
 #include "ardour/audio_track.h"
 #include "ardour/audioengine.h"
@@ -29,6 +33,8 @@
 #include "ardour/port.h"
 #include "ardour/session.h"
 #include "ardour/auditioner.h"
+#include "ardour/control_protocol_manager.h"
+#include "control_protocol/control_protocol.h"
 
 #include "gui_thread.h"
 #include "port_group.h"
@@ -103,14 +109,14 @@ PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<I
        assert (b.get());
 
        if (!allow_dups) {
-               
+
                /* don't add this bundle if we already have one with the same ports */
-               
+
                BundleList::iterator i = _bundles.begin ();
                while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
                        ++i;
                }
-               
+
                if (i != _bundles.end ()) {
                        return;
                }
@@ -120,7 +126,7 @@ PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<I
        b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
        _bundles.push_back (br);
 
-       Changed ();     
+       Changed ();
 }
 
 void
@@ -204,7 +210,8 @@ PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
                return boost::shared_ptr<IO> ();
        }
 
-       return (*i)->io;
+       boost::shared_ptr<IO> io ((*i)->io.lock ());
+       return io;
 }
 
 /** Remove bundles whose channels are already represented by other, larger bundles */
@@ -247,11 +254,11 @@ PortGroup::remove_duplicates ()
                                }
                        }
                }
-               
+
                if (remove) {
                        _bundles.erase (i);
                }
-               
+
                i = tmp;
        }
 }
@@ -265,7 +272,7 @@ PortGroupList::PortGroupList ()
 
 }
 
-PortGroupList::~PortGroupList() 
+PortGroupList::~PortGroupList()
 {
        /* XXX need to clean up bundles, but ownership shared with PortGroups */
 }
@@ -299,8 +306,9 @@ struct RouteIOs {
                route = r;
                ios.push_back (i);
        }
-       
+
        boost::shared_ptr<Route> route;
+       /* it's ok to use a shared_ptr here as RouteIOs structs are only used during ::gather () */
        std::list<boost::shared_ptr<IO> > ios;
 };
 
@@ -311,8 +319,8 @@ public:
        }
 };
 
-/** Gather bundles from around the system and put them in this PortGroupList.
- *  @param type Type of bundles to collect, or NIL for all types.
+/** Gather ports from around the system and put them in this PortGroupList.
+ *  @param type Type of ports to collect, or NIL for all types.
  */
 void
 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups)
@@ -323,10 +331,10 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
                return;
        }
 
-       boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
-       boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
-       boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
-       boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
+       boost::shared_ptr<PortGroup> bus (new PortGroup (string_compose (_("%1 Busses"), PROGRAM_NAME)));
+       boost::shared_ptr<PortGroup> track (new PortGroup (string_compose (_("%1 Tracks"), PROGRAM_NAME)));
+       boost::shared_ptr<PortGroup> system (new PortGroup (_("Hardware")));
+       boost::shared_ptr<PortGroup> ardour (new PortGroup (string_compose (_("%1 Misc"), PROGRAM_NAME)));
        boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
 
        /* Find the IOs which have bundles for routes and their processors.  We store
@@ -339,9 +347,16 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
 
        for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
 
+                /* we never show the monitor bus inputs */
+
+                if (inputs && (*i)->is_monitor()) {
+                        continue;
+                }
+
                /* keep track of IOs that we have taken bundles from,
                   so that we can avoid taking the same IO from both
-                  Route::output() and the main_outs Delivery */
+                  Route::output() and the main_outs Delivery
+                */
 
                set<boost::shared_ptr<IO> > used_io;
                boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
@@ -356,7 +371,11 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
        /* Sort RouteIOs by the routes' editor order keys */
        route_ios.sort (RouteIOsComparator ());
 
-       /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup */
+       /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup.
+          Note that if the RouteIO's bundles are multi-type, we may make new Bundles
+          with only the ports of one type.
+       */
+
        for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
                TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->route);
 
@@ -367,7 +386,7 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
                } else {
                        g = bus;
                }
-               
+
                for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
                        if (tv) {
                                g->add_bundle ((*j)->bundle(), *j, tv->color ());
@@ -394,26 +413,83 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
                        system->add_bundle (*i, allow_dups);
                }
        }
-       
+
        /* Ardour stuff */
 
-       if (!inputs && (type == DataType::AUDIO || type == DataType::NIL)) {
+       if (!inputs) {
                ardour->add_bundle (session->the_auditioner()->output()->bundle());
                ardour->add_bundle (session->click_io()->bundle());
        }
 
+       /* Ardour's surfaces */
+
+       ControlProtocolManager& m = ControlProtocolManager::instance ();
+       for (list<ControlProtocolInfo*>::iterator i = m.control_protocol_info.begin(); i != m.control_protocol_info.end(); ++i) {
+               if ((*i)->protocol) {
+                       list<boost::shared_ptr<Bundle> > b = (*i)->protocol->bundles ();
+                       for (list<boost::shared_ptr<Bundle> >::iterator j = b.begin(); j != b.end(); ++j) {
+                               if ((*j)->ports_are_inputs() == inputs) {
+                                       ardour->add_bundle (*j);
+                               }
+                       }
+               }
+       }
+
+       /* Ardour's sync ports */
+
+       MIDI::Manager* midi_manager = MIDI::Manager::instance ();
+       if (midi_manager && (type == DataType::MIDI || type == DataType::NIL)) {
+               boost::shared_ptr<Bundle> sync (new Bundle (_("Sync"), inputs));
+               MIDI::MachineControl* mmc = midi_manager->mmc ();
+               AudioEngine& ae = session->engine ();
+               if (inputs) {
+                       sync->add_channel (
+                               _("MTC in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->mtc_input_port()->name())
+                               );
+                       sync->add_channel (
+                               _("MIDI control in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_input_port()->name())
+                               );
+                       sync->add_channel (
+                               _("MIDI clock in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_clock_input_port()->name())
+                               );
+                       sync->add_channel (
+                               _("MMC in"), DataType::MIDI, ae.make_port_name_non_relative (mmc->input_port()->name())
+                               );
+               } else {
+                       sync->add_channel (
+                               _("MTC out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->mtc_output_port()->name())
+                               );
+                       sync->add_channel (
+                               _("MIDI control out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_output_port()->name())
+                               );
+                       sync->add_channel (
+                               _("MIDI clock out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_clock_output_port()->name())
+                               );
+                       sync->add_channel (
+                               _("MMC out"), DataType::MIDI, ae.make_port_name_non_relative (mmc->output_port()->name())
+                               );
+               }
+
+               ardour->add_bundle (sync);
+       }
+
        /* Now find all other ports that we haven't thought of yet */
 
        std::vector<std::string> extra_system[DataType::num_types];
        std::vector<std::string> extra_other[DataType::num_types];
 
+        string lpn (PROGRAM_NAME);
+        boost::to_lower (lpn);
+        string lpnc = lpn;
+        lpnc += ':';
+
        const char ** ports = 0;
        if (type == DataType::NIL) {
                ports = session->engine().get_ports ("", "", inputs ? JackPortIsInput : JackPortIsOutput);
        } else {
                ports = session->engine().get_ports ("", type.to_jack_type(), inputs ? JackPortIsInput : JackPortIsOutput);
        }
-       
+
        if (ports) {
 
                int n = 0;
@@ -427,13 +503,26 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
                            !track->has_port(p) &&
                            !ardour->has_port(p) &&
                            !other->has_port(p)) {
-                                
+
                                 /* special hack: ignore MIDI ports labelled Midi-Through. these
                                    are basically useless and mess things up for default
                                    connections.
                                 */
 
-                                if (p.find ("MIDI-Through") != string::npos) {
+                                if (p.find ("Midi-Through") != string::npos) {
+                                        ++n;
+                                        continue;
+                                }
+
+                                /* special hack: ignore our monitor inputs (which show up here because
+                                   we excluded them earlier.
+                                */
+
+                                string lp = p;
+                                boost::to_lower (lp);
+
+                                if ((lp.find (N_(":monitor")) != string::npos) &&
+                                    (lp.find (lpn) != string::npos)) {
                                         ++n;
                                         continue;
                                 }
@@ -444,7 +533,9 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
                                if (jp) {
                                        DataType t (jack_port_type (jp));
                                        if (t != DataType::NIL) {
-                                               if (port_has_prefix (p, "system:") || port_has_prefix (p, "alsa_pcm") || port_has_prefix (p, "ardour:")) {
+                                               if (port_has_prefix (p, N_("system:")) ||
+                                                    port_has_prefix (p, N_("alsa_pcm")) ||
+                                                    port_has_prefix (p, lpnc)) {
                                                        extra_system[t].push_back (p);
                                                } else {
                                                        extra_other[t].push_back (p);
@@ -461,13 +552,15 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
 
        for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
                if (!extra_system[*i].empty()) {
-                       system->add_bundle (make_bundle_from_ports (extra_system[*i], *i, inputs));
+                       boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
+                       system->add_bundle (b);
                }
        }
 
        for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
                if (!extra_other[*i].empty()) {
-                       other->add_bundle (make_bundle_from_ports (extra_other[*i], *i, inputs));
+                       boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_other[*i], *i, inputs);
+                       other->add_bundle (b);
                }
        }
 
@@ -476,7 +569,9 @@ PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inp
        }
 
        add_group_if_not_empty (other);
-       add_group_if_not_empty (bus);
+       if (type != DataType::MIDI) {
+               add_group_if_not_empty (bus);
+       }
        add_group_if_not_empty (track);
        add_group_if_not_empty (ardour);
        add_group_if_not_empty (system);
@@ -674,11 +769,6 @@ PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
 bool
 PortGroupList::empty () const
 {
-       List::const_iterator i = _groups.begin ();
-       while (i != _groups.end() && (*i)->total_channels() == ChanCount::ZERO) {
-               ++i;
-       }
-
-       return (i == _groups.end());
+       return _groups.empty ();
 }