pay attention to MIDI channel selector when creating notes with GUI (fixes #3233)
[ardour.git] / gtk2_ardour / port_group.cc
index 7a7fb4ad252b2f610e7d0f82e3d90ca0ec6cc05d..a010b3c184b4be91743ca6820d020e178142c003 100644 (file)
@@ -30,6 +30,7 @@
 #include "ardour/session.h"
 #include "ardour/auditioner.h"
 
+#include "gui_thread.h"
 #include "port_group.h"
 #include "port_matrix.h"
 #include "time_axis_view.h"
@@ -50,6 +51,14 @@ PortGroup::PortGroup (std::string const & n)
 
 }
 
+PortGroup::~PortGroup()
+{
+       for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
+               delete *i;
+       }
+       _bundles.clear ();
+}
+
 /** Add a bundle to a group.
  *  @param b Bundle.
  *  @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
@@ -80,6 +89,14 @@ PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gd
        add_bundle_internal (b, io, true, c, false);
 }
 
+PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
+       : bundle (b)
+       , io (iop)
+       , colour (c)
+       , has_colour (has_c)
+{
+}
+
 void
 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
 {
@@ -90,7 +107,7 @@ PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<I
                /* 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) {
+               while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
                        ++i;
                }
                
@@ -99,14 +116,9 @@ PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<I
                }
        }
 
-       BundleRecord r;
-       r.bundle = b;
-       r.io = io;
-       r.colour = colour;
-       r.has_colour = has_colour;
-       r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
-
-       _bundles.push_back (r);
+       BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
+       b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
+       _bundles.push_back (br);
 
        Changed ();     
 }
@@ -117,7 +129,7 @@ PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
        assert (b.get());
 
        BundleList::iterator i = _bundles.begin ();
-       while (i != _bundles.end() && i->bundle != b) {
+       while (i != _bundles.end() && (*i)->bundle != b) {
                ++i;
        }
 
@@ -125,7 +137,7 @@ PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
                return;
        }
 
-       i->changed_connection.disconnect ();
+       delete *i;
        _bundles.erase (i);
 
        Changed ();
@@ -142,7 +154,7 @@ void
 PortGroup::clear ()
 {
        for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
-               i->changed_connection.disconnect ();
+               delete *i;
        }
 
        _bundles.clear ();
@@ -153,7 +165,7 @@ bool
 PortGroup::has_port (std::string const& p) const
 {
        for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
-               if (i->bundle->offers_port_alone (p)) {
+               if ((*i)->bundle->offers_port_alone (p)) {
                        return true;
                }
        }
@@ -165,7 +177,7 @@ boost::shared_ptr<Bundle>
 PortGroup::only_bundle ()
 {
        assert (_bundles.size() == 1);
-       return _bundles.front().bundle;
+       return _bundles.front()->bundle;
 }
 
 
@@ -174,7 +186,7 @@ PortGroup::total_channels () const
 {
        uint32_t n = 0;
        for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
-               n += i->bundle->nchannels ();
+               n += (*i)->bundle->nchannels ();
        }
 
        return n;
@@ -184,7 +196,7 @@ boost::shared_ptr<IO>
 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
 {
        BundleList::const_iterator i = _bundles.begin ();
-       while (i != _bundles.end() && i->bundle != b) {
+       while (i != _bundles.end() && (*i)->bundle != b) {
                ++i;
        }
 
@@ -192,7 +204,56 @@ PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
                return boost::shared_ptr<IO> ();
        }
 
-       return i->io;
+       return (*i)->io;
+}
+
+/** Remove bundles whose channels are already represented by other, larger bundles */
+void
+PortGroup::remove_duplicates ()
+{
+       BundleList::iterator i = _bundles.begin();
+       while (i != _bundles.end()) {
+
+               BundleList::iterator tmp = i;
+               ++tmp;
+
+               bool remove = false;
+
+               for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
+
+                       if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
+                               /* this bundle is larger */
+
+                               uint32_t k = 0;
+                               while (k < (*i)->bundle->nchannels()) {
+                                       /* see if this channel on *i has an equivalent on *j */
+                                       uint32_t l = 0;
+                                       while (l < (*j)->bundle->nchannels() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
+                                               ++l;
+                                       }
+
+                                       if (l == (*j)->bundle->nchannels()) {
+                                               /* it does not */
+                                               break;
+                                       }
+
+                                       ++k;
+                               }
+
+                               if (k == (*i)->bundle->nchannels ()) {
+                                       /* all channels on *i are represented by the larger bundle *j, so remove *i */
+                                       remove = true;
+                                       break;
+                               }
+                       }
+               }
+               
+               if (remove) {
+                       _bundles.erase (i);
+               }
+               
+               i = tmp;
+       }
 }
 
 
@@ -204,6 +265,11 @@ PortGroupList::PortGroupList ()
 
 }
 
+PortGroupList::~PortGroupList() 
+{
+       /* XXX need to clean up bundles, but ownership shared with PortGroups */
+}
+
 void
 PortGroupList::set_type (DataType t)
 {
@@ -238,15 +304,17 @@ PortGroupList::maybe_add_processor_to_list (
 
 /** Gather bundles from around the system and put them in this PortGroupList */
 void
-PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
+PortGroupList::gather (ARDOUR::Session* session, bool inputs, bool allow_dups)
 {
        clear ();
 
+       if (session == 0) {
+               return;
+       }
+
        boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
        boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
-       boost::shared_ptr<PortGroup> system_mono (new PortGroup (_("System (mono)")));
-       boost::shared_ptr<PortGroup> system_stereo (new PortGroup (_("System (stereo)")));
-       boost::shared_ptr<PortGroup> system_other (new PortGroup (_("System (other)")));
+       boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
        boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
        boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
 
@@ -254,7 +322,7 @@ PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
           the route's input/output and processor bundles together so that they
           are presented as one bundle in the matrix. */
 
-       boost::shared_ptr<RouteList> routes = session.get_routes ();
+       boost::shared_ptr<RouteList> routes = session->get_routes ();
 
        for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
 
@@ -270,7 +338,7 @@ PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
 
                route_bundles.push_back (io->bundle ());
 
-               (*i)->foreach_processor (bind (mem_fun (*this, &PortGroupList::maybe_add_processor_to_list), &route_bundles, inputs, used_io));
+               (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &route_bundles, inputs, used_io));
 
                /* Work out which group to put these bundles in */
                boost::shared_ptr<PortGroup> g;
@@ -294,7 +362,7 @@ PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
 
                if (g) {
 
-                       TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->get());
+                       TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (*i);
                        for (list<boost::shared_ptr<Bundle> >::iterator i = route_bundles.begin(); i != route_bundles.end(); ++i) {
                                if (tv) {
                                        g->add_bundle (*i, io, tv->color ());
@@ -309,37 +377,25 @@ PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
           that UserBundles that offer the same ports as a normal bundle get priority
        */
 
-       boost::shared_ptr<BundleList> b = session.bundles ();
+       boost::shared_ptr<BundleList> b = session->bundles ();
 
        for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
                if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
-                       if ((*i)->nchannels() == 1) {
-                               system_mono->add_bundle (*i, allow_dups);
-                       } else if ((*i)->nchannels() == 2) {
-                               system_stereo->add_bundle (*i, allow_dups);
-                       } else {
-                               system_other->add_bundle (*i, allow_dups);
-                       }
+                       system->add_bundle (*i, allow_dups);
                }
        }
 
        for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
                if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
-                       if ((*i)->nchannels() == 1) {
-                               system_mono->add_bundle (*i, allow_dups);
-                       } else if ((*i)->nchannels() == 2) {
-                               system_stereo->add_bundle (*i, allow_dups);
-                       } else {
-                               system_other->add_bundle (*i, allow_dups);
-                       }
+                       system->add_bundle (*i, allow_dups);
                }
        }
        
        /* Ardour stuff */
 
        if (!inputs && _type == DataType::AUDIO) {
-               ardour->add_bundle (session.the_auditioner()->output()->bundle());
-               ardour->add_bundle (session.click_io()->bundle());
+               ardour->add_bundle (session->the_auditioner()->output()->bundle());
+               ardour->add_bundle (session->click_io()->bundle());
        }
 
        /* Now find all other ports that we haven't thought of yet */
@@ -347,27 +403,35 @@ PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
        std::vector<std::string> extra_system;
        std::vector<std::string> extra_other;
 
-       const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ?
+       const char **ports = session->engine().get_ports ("", _type.to_jack_type(), inputs ?
                                                         JackPortIsInput : JackPortIsOutput);
        if (ports) {
 
                int n = 0;
                string client_matching_string;
 
-               client_matching_string = session.engine().client_name();
+               client_matching_string = session->engine().client_name();
                client_matching_string += ':';
 
                while (ports[n]) {
 
                        std::string const p = ports[n];
 
-                       if (!system_mono->has_port(p) &&
-                           !system_stereo->has_port(p) &&
-                           !system_other->has_port(p) &&
+                       if (!system->has_port(p) &&
                            !bus->has_port(p) &&
                            !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) {
+                                        ++n;
+                                        continue;
+                                }
 
                                if (port_has_prefix (p, "system:") ||
                                    port_has_prefix (p, "alsa_pcm") ||
@@ -386,26 +450,22 @@ PortGroupList::gather (ARDOUR::Session& session, bool inputs, bool allow_dups)
 
        if (!extra_system.empty()) {
                boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system, inputs);
-               if (b->nchannels() == 1) {
-                       system_mono->add_bundle (b);
-               } else if (b->nchannels() == 2) {
-                       system_stereo->add_bundle (b);
-               } else {
-                       system_other->add_bundle (b);
-               }
+               system->add_bundle (b);
        }
 
        if (!extra_other.empty()) {
                other->add_bundle (make_bundle_from_ports (extra_other, inputs));
        }
 
-       add_group_if_not_empty (system_mono);
-       add_group_if_not_empty (system_stereo);
-       add_group_if_not_empty (system_other);
+       if (!allow_dups) {
+               system->remove_duplicates ();
+       }
+
+       add_group_if_not_empty (other);
        add_group_if_not_empty (bus);
        add_group_if_not_empty (track);
        add_group_if_not_empty (ardour);
-       add_group_if_not_empty (other);
+       add_group_if_not_empty (system);
 
        emit_changed ();
 }
@@ -483,13 +543,7 @@ void
 PortGroupList::clear ()
 {
        _groups.clear ();
-
-       for (std::vector<sigc::connection>::iterator i = _bundle_changed_connections.begin(); i != _bundle_changed_connections.end(); ++i) {
-               i->disconnect ();
-       }
-
-       _bundle_changed_connections.clear ();
-
+       _bundle_changed_connections.drop_connections ();
        emit_changed ();
 }
 
@@ -531,11 +585,8 @@ PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
 {
        _groups.push_back (g);
 
-       g->Changed.connect (sigc::mem_fun (*this, &PortGroupList::emit_changed));
-
-       _bundle_changed_connections.push_back (
-               g->BundleChanged.connect (sigc::mem_fun (*this, &PortGroupList::emit_bundle_changed))
-               );
+       g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
+       g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
 
        emit_changed ();
 }