Sort various things to reduce merge hell. No functional changes.
[ardour.git] / libs / ardour / io.cc
index 0c1ba32c74ab39a057fe0a97ac66d16e111ad7d5..babf1e451fac07ca888b42acc07bd01e1bc48b59 100644 (file)
 
 #include <pbd/xml++.h>
 #include <pbd/replace_all.h>
+#include <pbd/unknown_type.h>
 
 #include <ardour/audioengine.h>
 #include <ardour/io.h>
 #include <ardour/port.h>
 #include <ardour/audio_port.h>
 #include <ardour/midi_port.h>
-#include <ardour/bundle.h>
+#include <ardour/auto_bundle.h>
 #include <ardour/session.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/panner.h>
@@ -70,7 +71,7 @@ sigc::signal<void>           IO::Meter;
 sigc::signal<int>            IO::ConnectingLegal;
 sigc::signal<int>            IO::PortsLegal;
 sigc::signal<int>            IO::PannersLegal;
-sigc::signal<void,ChanCount> IO::MoreChannels;
+sigc::signal<void,ChanCount> IO::PortCountChanged;
 sigc::signal<int>            IO::PortsCreated;
 
 Glib::StaticMutex IO::m_meter_signal_lock = GLIBMM_STATIC_MUTEX_INIT;
@@ -99,10 +100,12 @@ static double direct_gain_to_control (gain_t gain) {
  */
 IO::IO (Session& s, const string& name,
        int input_min, int input_max, int output_min, int output_max,
-       DataType default_type)
+       DataType default_type, bool public_ports)
        : Automatable (s, name),
-      _output_buffers (new BufferSet()),
+         _output_buffers (new BufferSet()),
+         _active(true),
          _default_type (default_type),
+         _public_ports (public_ports),
          _input_minimum (ChanCount::ZERO),
          _input_maximum (ChanCount::INFINITE),
          _output_minimum (ChanCount::ZERO),
@@ -148,12 +151,12 @@ IO::IO (Session& s, const string& name,
                m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
        }
        
-       // Connect to our own MoreChannels signal to connect output buffers
-       IO::MoreChannels.connect (mem_fun (*this, &IO::attach_buffers));
+       // Connect to our own PortCountChanged signal to connect output buffers
+       IO::PortCountChanged.connect (mem_fun (*this, &IO::attach_buffers));
 
        _session.add_controllable (_gain_control);
 
-       create_bundles ();
+       create_bundles_for_inputs_and_outputs ();
 }
 
 IO::IO (Session& s, const XMLNode& node, DataType dt)
@@ -162,7 +165,7 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
          _default_type (dt)
 {
        _meter = new PeakMeter (_session);
-
+       _public_ports = true; // XXX get this from node
        _panner = 0;
        deferred_state = 0;
        no_panner_reset = false;
@@ -188,20 +191,21 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
                m_meter_connection = Meter.connect (mem_fun (*this, &IO::meter));
        }
        
-       // Connect to our own MoreChannels signal to connect output buffers
-       IO::MoreChannels.connect (mem_fun (*this, &IO::attach_buffers));
+       // Connect to our own PortCountChanged signal to connect output buffers
+       IO::PortCountChanged.connect (mem_fun (*this, &IO::attach_buffers));
 
        _session.add_controllable (_gain_control);
 
-       create_bundles ();
+       create_bundles_for_inputs_and_outputs ();
 }
 
 IO::~IO ()
 {
        Glib::Mutex::Lock guard (m_meter_signal_lock);
-       
        Glib::Mutex::Lock lm (io_lock);
 
+       BLOCK_PROCESS_CALLBACK ();
+
        for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                _session.engine().unregister_port (*i);
        }
@@ -227,7 +231,7 @@ IO::silence (nframes_t nframes, nframes_t offset)
        }
 }
 
-/** Deliver bufs to the IO's Jack outputs.
+/** Deliver bufs to the IO's output ports
  *
  * This function should automatically do whatever it necessary to correctly deliver bufs
  * to the outputs, eg applying gain or pan or whatever else needs to be done.
@@ -305,12 +309,12 @@ void
 IO::collect_input (BufferSet& outs, nframes_t nframes, nframes_t offset)
 {
        assert(outs.available() >= n_inputs());
-
-       outs.set_count(n_inputs());
        
-       if (outs.count() == ChanCount::ZERO)
+       if (n_inputs() == ChanCount::ZERO)
                return;
 
+       outs.set_count(n_inputs());
+       
        for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
                
                BufferSet::iterator o = outs.begin(*t);
@@ -329,27 +333,65 @@ IO::just_meter_input (nframes_t start_frame, nframes_t end_frame,
 
        collect_input (bufs, nframes, offset);
 
-       _meter->run(bufs, start_frame, end_frame, nframes, offset);
+       _meter->run_in_place(bufs, start_frame, end_frame, nframes, offset);
 }
 
+
 void
-IO::drop_input_bundle ()
+IO::check_bundles_connected_to_inputs ()
 {
-       _input_bundle.reset ();
-       input_bundle_configuration_connection.disconnect();
-       input_bundle_connection_connection.disconnect();
-       _session.set_dirty ();
+       check_bundles (_bundles_connected_to_inputs, inputs());
 }
 
 void
-IO::drop_output_bundle ()
+IO::check_bundles_connected_to_outputs ()
 {
-       _output_bundle.reset ();
-       output_bundle_configuration_connection.disconnect();
-       output_bundle_connection_connection.disconnect();
-       _session.set_dirty ();
+       check_bundles (_bundles_connected_to_outputs, outputs());
+}
+
+void
+IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
+{
+       std::vector<UserBundleInfo> new_list;
+       
+       for (std::vector<UserBundleInfo>::iterator i = list.begin(); i != list.end(); ++i) {
+
+               uint32_t const N = i->bundle->nchannels ();
+
+               if (ports.num_ports() < N) {
+                       continue;
+               }
+
+               bool ok = true;
+               for (uint32_t j = 0; j < N; ++j) {
+                       /* Every port on bundle channel j must be connected to our input j */
+                       PortList const pl = i->bundle->channel_ports (j);
+                       for (uint32_t k = 0; k < pl.size(); ++k) {
+                               if (ports.port(j)->connected_to (pl[k]) == false) {
+                                       ok = false;
+                                       break;
+                               }
+                       }
+
+                       if (ok == false) {
+                               break;
+                       }
+               }
+
+               if (ok) {
+                       new_list.push_back (*i);
+               } else {
+                       i->configuration_will_change.disconnect ();
+                       i->configuration_has_changed.disconnect ();
+                       i->ports_will_change.disconnect ();
+                       i->ports_have_changed.disconnect ();
+               }
+       }
+
+       list = new_list;
 }
 
+
 int
 IO::disconnect_input (Port* our_port, string other_port, void* src)
 {
@@ -376,7 +418,7 @@ IO::disconnect_input (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_input_bundle ();
+                       check_bundles_connected_to_inputs ();
                }
        }
 
@@ -410,8 +452,6 @@ IO::connect_input (Port* our_port, string other_port, void* src)
                        if (_session.engine().connect (other_port, our_port->name())) {
                                return -1;
                        }
-                       
-                       drop_input_bundle ();
                }
        }
 
@@ -446,7 +486,7 @@ IO::disconnect_output (Port* our_port, string other_port, void* src)
                                return -1;
                        }
 
-                       drop_output_bundle ();
+                       check_bundles_connected_to_outputs ();
                }
        }
 
@@ -480,8 +520,6 @@ IO::connect_output (Port* our_port, string other_port, void* src)
                        if (_session.engine().connect (our_port->name(), other_port)) {
                                return -1;
                        }
-
-                       drop_output_bundle ();
                }
        }
 
@@ -542,24 +580,26 @@ IO::remove_output_port (Port* port, void* src)
                                } 
 
                                _session.engine().unregister_port (*port);
-                               drop_output_bundle ();
+                               check_bundles_connected_to_outputs ();
                                
                                setup_peak_meters ();
                                reset_panner ();
                        }
                }
+
+               PortCountChanged (n_outputs()); /* EMIT SIGNAL */
        }
 
        if (change == ConnectionsChanged) {
-               setup_bundles ();
+               setup_bundles_for_inputs_and_outputs ();
        }
 
        if (change != NoChange) {
                output_changed (change, src);
                _session.set_dirty ();
                return 0;
-       } 
-       
+       }
+
        return -1;
 }
 
@@ -573,7 +613,6 @@ int
 IO::add_output_port (string destination, void* src, DataType type)
 {
        Port* our_port;
-       char name[64];
 
        if (type == DataType::NIL)
                type = _default_type;
@@ -591,25 +630,19 @@ IO::add_output_port (string destination, void* src, DataType type)
                
                        /* Create a new output port */
                        
-                       // FIXME: naming scheme for differently typed ports?
-                       if (_output_maximum.get(type) == 1) {
-                               snprintf (name, sizeof (name), _("%s/out"), _name.c_str());
-                       } else {
-                               snprintf (name, sizeof (name), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                       }
+                       string portname = build_legal_port_name (type, false);
                        
-                       if ((our_port = _session.engine().register_output_port (type, name)) == 0) {
-                               error << string_compose(_("IO: cannot register output port %1"), name) << endmsg;
+                       if ((our_port = _session.engine().register_output_port (type, portname, _public_ports)) == 0) {
+                               error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
                                return -1;
                        }
                        
                        _outputs.add (our_port);
-                       drop_output_bundle ();
                        setup_peak_meters ();
                        reset_panner ();
                }
 
-               MoreChannels (n_outputs()); /* EMIT SIGNAL */
+               PortCountChanged (n_outputs()); /* EMIT SIGNAL */
        }
 
        if (destination.length()) {
@@ -620,9 +653,9 @@ IO::add_output_port (string destination, void* src, DataType type)
        
        // pan_changed (src); /* EMIT SIGNAL */
        output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-       setup_bundles ();
+       setup_bundles_for_inputs_and_outputs ();
        _session.set_dirty ();
-       
+
        return 0;
 }
 
@@ -651,16 +684,18 @@ IO::remove_input_port (Port* port, void* src)
                                } 
 
                                _session.engine().unregister_port (*port);
-                               drop_input_bundle ();
+                               check_bundles_connected_to_inputs ();
                                
                                setup_peak_meters ();
                                reset_panner ();
                        }
                }
+               
+               PortCountChanged (n_inputs ()); /* EMIT SIGNAL */
        }
 
        if (change == ConfigurationChanged) {
-               setup_bundles ();
+               setup_bundles_for_inputs_and_outputs ();
        }
 
        if (change != NoChange) {
@@ -675,7 +710,7 @@ IO::remove_input_port (Port* port, void* src)
 
 /** Add an input port.
  *
- * @param type Data type of port.  The appropriate Jack port type, and @ref Port will be created.
+ * @param type Data type of port.  The appropriate port type, and @ref Port will be created.
  * @param destination Name of input port to connect new port to.
  * @param src Source for emitted ConfigurationChanged signal.
  */
@@ -683,7 +718,6 @@ int
 IO::add_input_port (string source, void* src, DataType type)
 {
        Port* our_port;
-       char name[64];
        
        if (type == DataType::NIL)
                type = _default_type;
@@ -700,25 +734,19 @@ IO::add_input_port (string source, void* src, DataType type)
 
                        /* Create a new input port */
                        
-                       // FIXME: naming scheme for differently typed ports?
-                       if (_input_maximum.get(type) == 1) {
-                               snprintf (name, sizeof (name), _("%s/in"), _name.c_str());
-                       } else {
-                               snprintf (name, sizeof (name), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                       }
+                       string portname = build_legal_port_name (type, true);
 
-                       if ((our_port = _session.engine().register_input_port (type, name)) == 0) {
-                               error << string_compose(_("IO: cannot register input port %1"), name) << endmsg;
+                       if ((our_port = _session.engine().register_input_port (type, portname, _public_ports)) == 0) {
+                               error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
                                return -1;
                        }
 
                        _inputs.add (our_port);
-                       drop_input_bundle ();
                        setup_peak_meters ();
                        reset_panner ();
                }
 
-               MoreChannels (n_inputs()); /* EMIT SIGNAL */
+               PortCountChanged (n_inputs()); /* EMIT SIGNAL */
        }
 
        if (source.length()) {
@@ -730,7 +758,7 @@ IO::add_input_port (string source, void* src, DataType type)
 
        // pan_changed (src); /* EMIT SIGNAL */
        input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-       setup_bundles ();
+       setup_bundles_for_inputs_and_outputs ();
        _session.set_dirty ();
        
        return 0;
@@ -749,7 +777,7 @@ IO::disconnect_inputs (void* src)
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_input_bundle ();
+                       check_bundles_connected_to_inputs ();
                }
        }
        
@@ -771,7 +799,7 @@ IO::disconnect_outputs (void* src)
                                _session.engine().disconnect (*i);
                        }
 
-                       drop_output_bundle ();
+                       check_bundles_connected_to_outputs ();
                }
        }
 
@@ -806,18 +834,12 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
                /* create any necessary new ports */
                while (n_inputs().get(*t) < n) {
 
-                       char buf[64];
-
-                       if (_input_maximum.get(*t) == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
-                       } else {
-                               snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                       }
+                       string portname = build_legal_port_name (*t, true);
 
                        try {
 
-                               if ((input_port = _session.engine().register_input_port (*t, buf)) == 0) {
-                                       error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                               if ((input_port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) {
+                                       error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
                                        return -1;
                                }
                        }
@@ -835,10 +857,10 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
        }
        
        if (changed) {
-               drop_input_bundle ();
+               check_bundles_connected_to_inputs ();
                setup_peak_meters ();
                reset_panner ();
-               MoreChannels (n_inputs()); /* EMIT SIGNAL */
+               PortCountChanged (n_inputs()); /* EMIT SIGNAL */
                _session.set_dirty ();
        }
        
@@ -854,7 +876,7 @@ IO::ensure_inputs_locked (ChanCount count, bool clear, void* src)
 
 /** Attach output_buffers to port buffers.
  * 
- * Connected to IO's own MoreChannels signal.
+ * Connected to IO's own PortCountChanged signal.
  */
 void
 IO::attach_buffers(ChanCount ignored)
@@ -921,19 +943,11 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
 
                        while (n_inputs().get(*t) < nin) {
 
-                               char buf[64];
-
-                               /* Create a new input port */
-
-                               if (_input_maximum.get(*t) == 1) {
-                                       snprintf (buf, sizeof (buf), _("%s/in"), _name.c_str());
-                               } else {
-                                       snprintf (buf, sizeof (buf), _("%s/in %u"), _name.c_str(), find_input_port_hole());
-                               }
+                               string portname = build_legal_port_name (*t, true);
 
                                try {
-                                       if ((port = _session.engine().register_input_port (*t, buf)) == 0) {
-                                               error << string_compose(_("IO: cannot register input port %1"), buf) << endmsg;
+                                       if ((port = _session.engine().register_input_port (*t, portname, _public_ports)) == 0) {
+                                               error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
                                                return -1;
                                        }
                                }
@@ -953,19 +967,11 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
 
                        while (n_outputs().get(*t) < nout) {
 
-                               char buf[64];
-
-                               /* Create a new output port */
-
-                               if (_output_maximum.get(*t) == 1) {
-                                       snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
-                               } else {
-                                       snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                               }
-
+                               string portname = build_legal_port_name (*t, false);
+                               
                                try { 
-                                       if ((port = _session.engine().register_output_port (*t, buf)) == 0) {
-                                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                                       if ((port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) {
+                                               error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
                                                return -1;
                                        }
                                }
@@ -1002,18 +1008,18 @@ IO::ensure_io (ChanCount in, ChanCount out, bool clear, void* src)
        }
 
        if (out_changed) {
-               drop_output_bundle ();
+               check_bundles_connected_to_outputs ();
                output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
        
        if (in_changed) {
-               drop_input_bundle ();
+               check_bundles_connected_to_inputs ();
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
        }
 
        if (in_changed || out_changed) {
-               MoreChannels (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */
-               setup_bundles ();
+               PortCountChanged (max (n_outputs(), n_inputs())); /* EMIT SIGNAL */
+               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
 
@@ -1041,7 +1047,7 @@ IO::ensure_inputs (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                input_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-               setup_bundles ();
+               setup_bundles_for_inputs_and_outputs ();
                _session.set_dirty ();
        }
        return 0;
@@ -1076,16 +1082,10 @@ IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
                /* create any necessary new ports */
                while (n_outputs().get(*t) < n) {
 
-                       char buf[64];
-
-                       if (_output_maximum.get(*t) == 1) {
-                               snprintf (buf, sizeof (buf), _("%s/out"), _name.c_str());
-                       } else {
-                               snprintf (buf, sizeof (buf), _("%s/out %u"), _name.c_str(), find_output_port_hole());
-                       }
+                       string portname = build_legal_port_name (*t, false);
 
-                       if ((output_port = _session.engine().register_output_port (*t, buf)) == 0) {
-                               error << string_compose(_("IO: cannot register output port %1"), buf) << endmsg;
+                       if ((output_port = _session.engine().register_output_port (*t, portname, _public_ports)) == 0) {
+                               error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
                                return -1;
                        }
 
@@ -1100,8 +1100,8 @@ IO::ensure_outputs_locked (ChanCount count, bool clear, void* src)
        }
        
        if (changed) {
-               drop_output_bundle ();
-               MoreChannels (n_outputs()); /* EMIT SIGNAL */
+               check_bundles_connected_to_outputs ();
+               PortCountChanged (n_outputs()); /* EMIT SIGNAL */
                _session.set_dirty ();
        }
        
@@ -1139,7 +1139,7 @@ IO::ensure_outputs (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                 output_changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-                setup_bundles ();
+                setup_bundles_for_inputs_and_outputs ();
        }
 
        return 0;
@@ -1203,8 +1203,8 @@ IO::state (bool full_state)
        XMLNode* node = new XMLNode (state_node_name);
        char buf[64];
        string str;
-       bool need_ins = true;
-       bool need_outs = true;
+       vector<string>::iterator ci;
+       int n;
        LocaleGuard lg (X_("POSIX"));
        Glib::Mutex::Lock lm (io_lock);
 
@@ -1212,83 +1212,88 @@ IO::state (bool full_state)
        id().print (buf, sizeof (buf));
        node->add_property("id", buf);
 
-       str = "";
-
-       if (_input_bundle && !_input_bundle->dynamic()) {
-               node->add_property ("input-connection", _input_bundle->name());
-               need_ins = false;
+       for (
+         std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_inputs.begin();
+         i != _bundles_connected_to_inputs.end();
+         ++i
+         )
+       {
+               XMLNode* n = new XMLNode ("InputBundle");
+               n->add_property ("name", i->bundle->name ());
+               node->add_child_nocopy (*n);
        }
 
-       if (_output_bundle && !_output_bundle->dynamic()) {
-               node->add_property ("output-connection", _output_bundle->name());
-               need_outs = false;
+       for (
+         std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_outputs.begin();
+         i != _bundles_connected_to_outputs.end();
+         ++i
+         )
+       {
+               XMLNode* n = new XMLNode ("OutputBundle");
+               n->add_property ("name", i->bundle->name ());
+               node->add_child_nocopy (*n);
        }
+       
+       str = "";
 
-       if (need_ins) {
-               for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
+       for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
                        
-                       const char **connections = i->get_connections();
+               vector<string> connections;
+
+               if (i->get_connections (connections)) {
+
+                       str += '{';
                        
-                       if (connections && connections[0]) {
-                               str += '{';
+                       for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
+                               if (n) {
+                                       str += ',';
+                               }
                                
-                               for (int n = 0; connections && connections[n]; ++n) {
-                                       if (n) {
-                                               str += ',';
-                                       }
-                                       
-                                       /* if its a connection to our own port,
-                                          return only the port name, not the
-                                          whole thing. this allows connections
-                                          to be re-established even when our
-                                          client name is different.
-                                       */
-                                       
-                                       str += _session.engine().make_port_name_relative (connections[n]);
-                               }       
-
-                               str += '}';
+                               /* if its a connection to our own port,
+                                  return only the port name, not the
+                                  whole thing. this allows connections
+                                  to be re-established even when our
+                                  client name is different.
+                               */
                                
-                               free (connections);
-                       }
-                       else {
-                               str += "{}";
-                       }
+                               str += _session.engine().make_port_name_relative (*ci);
+                       }       
+                       
+                       str += '}';
+
+               } else {
+                       str += "{}";
                }
-               
-               node->add_property ("inputs", str);
        }
+       
+       node->add_property ("inputs", str);
 
-       if (need_outs) {
-               str = "";
+       str = "";
+       
+       for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
                
-               for (PortSet::iterator i = _outputs.begin(); i != _outputs.end(); ++i) {
+               vector<string> connections;
+
+               if (i->get_connections (connections)) {
                        
-                       const char **connections = i->get_connections();
+                       str += '{';
                        
-                       if (connections && connections[0]) {
-                               
-                               str += '{';
-                               
-                               for (int n = 0; connections[n]; ++n) {
-                                       if (n) {
-                                               str += ',';
-                                       }
-
-                                       str += _session.engine().make_port_name_relative (connections[n]);
+                       for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
+                               if (n) {
+                                       str += ',';
                                }
-
-                               str += '}';
                                
-                               free (connections);
-                       }
-                       else {
-                               str += "{}";
+                               str += _session.engine().make_port_name_relative (*ci);
                        }
+                       
+                       str += '}';
+
+               } else {
+                       str += "{}";
                }
-               
-               node->add_property ("outputs", str);
        }
+       
+       node->add_property ("outputs", str);
 
        node->add_child_nocopy (_panner->state (full_state));
        node->add_child_nocopy (_gain_control->get_state ());
@@ -1296,13 +1301,14 @@ IO::state (bool full_state)
        snprintf (buf, sizeof(buf), "%2.12f", gain());
        node->add_property ("gain", buf);
 
-       // FIXME: this is NOT sufficient!
-       const int in_min  = (_input_minimum == ChanCount::ZERO) ? -1 : _input_minimum.get(_default_type);
-       const int in_max  = (_input_maximum == ChanCount::INFINITE) ? -1 : _input_maximum.get(_default_type);
-       const int out_min = (_output_minimum == ChanCount::ZERO) ? -1 : _output_minimum.get(_default_type);
-       const int out_max = (_output_maximum == ChanCount::INFINITE) ? -1 : _output_maximum.get(_default_type);
+       /* To make backwards compatibility a bit easier, write ChanCount::INFINITE to the session file
+          as -1.
+       */
 
-       snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d", in_min, in_max, out_min, out_max);
+       int const in_max = _input_maximum == ChanCount::INFINITE ? -1 : _input_maximum.get(_default_type);
+       int const out_max = _output_maximum == ChanCount::INFINITE ? -1 : _output_maximum.get(_default_type);
+
+       snprintf (buf, sizeof(buf)-1, "%d,%d,%d,%d", _input_minimum.get(_default_type), in_max, _output_minimum.get(_default_type), out_max);
 
        node->add_property ("iolimits", buf);
 
@@ -1339,18 +1345,42 @@ IO::set_state (const XMLNode& node)
                _id = prop->value ();
        }
 
-       size_t in_min =  -1;
-       size_t in_max  = -1;
-       size_t out_min = -1;
-       size_t out_max = -1;
+       int in_min = -1;
+       int in_max = -1;
+       int out_min = -1;
+       int out_max = -1;
 
        if ((prop = node.property ("iolimits")) != 0) {
-               sscanf (prop->value().c_str(), "%zd,%zd,%zd,%zd",
+               sscanf (prop->value().c_str(), "%d,%d,%d,%d",
                        &in_min, &in_max, &out_min, &out_max);
-               _input_minimum = ChanCount(_default_type, in_min);
-               _input_maximum = ChanCount(_default_type, in_max);
-               _output_minimum = ChanCount(_default_type, out_min);
-               _output_maximum = ChanCount(_default_type, out_max);
+
+               /* Correct for the difference between the way we write things to session files and the
+                  way things are described by ChanCount; see comments in io.h about what the different
+                  ChanCount values mean. */
+
+               if (in_min < 0) {
+                       _input_minimum = ChanCount::ZERO;
+               } else {
+                       _input_minimum = ChanCount (_default_type, in_min);
+               }
+
+               if (in_max < 0) {
+                       _input_maximum = ChanCount::INFINITE;
+               } else {
+                       _input_maximum = ChanCount (_default_type, in_max);
+               }
+
+               if (out_min < 0) {
+                       _output_minimum = ChanCount::ZERO;
+               } else {
+                       _output_minimum = ChanCount (_default_type, out_min);
+               }
+               
+               if (out_max < 0) {
+                       _output_maximum = ChanCount::INFINITE;
+               } else {
+                       _output_maximum = ChanCount (_default_type, out_max);
+               }
        }
        
        if ((prop = node.property ("gain")) != 0) {
@@ -1544,55 +1574,12 @@ IO::ports_became_legal ()
 int
 IO::create_ports (const XMLNode& node)
 {
-       const XMLProperty* prop;
+       XMLProperty const * prop;
        int num_inputs = 0;
        int num_outputs = 0;
 
-       /* XXX: we could change *-connection to *-bundle, but it seems a bit silly to
-        * break the session file format.
-        */
-       if ((prop = node.property ("input-connection")) != 0) {
-
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
-               
-               if (c == 0) {
-                       error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
-
-                       if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
-                               error << _("No input bundles available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
-               } 
-
-               num_inputs = c->nchannels();
-
-       } else if ((prop = node.property ("inputs")) != 0) {
-
+       if ((prop = node.property ("inputs")) != 0) {
                num_inputs = count (prop->value().begin(), prop->value().end(), '{');
-       }
-       
-       if ((prop = node.property ("output-connection")) != 0) {
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
-
-               if (c == 0) {
-                       error << string_compose(_("Unknown bundle \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
-
-                       if ((c = _session.bundle_by_name (_("out 1"))) == 0) {
-                               error << _("No output bundles available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Bundle %1 was not available - \"out 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
-               } 
-
-               num_outputs = c->nchannels ();
-               
        } else if ((prop = node.property ("outputs")) != 0) {
                num_outputs = count (prop->value().begin(), prop->value().end(), '{');
        }
@@ -1617,57 +1604,48 @@ IO::create_ports (const XMLNode& node)
 int
 IO::make_connections (const XMLNode& node)
 {
-       const XMLProperty* prop;
-
-       if ((prop = node.property ("input-connection")) != 0) {
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
-               
-               if (c == 0) {
-                       error << string_compose(_("Unknown connection \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
-
-                       if ((c = _session.bundle_by_name (_("in 1"))) == 0) {
-                               error << _("No input connections available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       } else {
-                               info << string_compose (_("Bundle %1 was not available - \"in 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
-               } 
-
-               connect_input_ports_to_bundle (c, this);
-
-       } else if ((prop = node.property ("inputs")) != 0) {
+       XMLProperty const * prop;
+       
+       if ((prop = node.property ("inputs")) != 0) {
                if (set_inputs (prop->value())) {
                        error << string_compose(_("improper input channel list in XML node (%1)"), prop->value()) << endmsg;
                        return -1;
                }
        }
-       
-       if ((prop = node.property ("output-bundle")) != 0) {
-               boost::shared_ptr<Bundle> c = _session.bundle_by_name (prop->value());
-               
-               if (c == 0) {
-                       error << string_compose(_("Unknown bundle \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
 
-                       if ((c = _session.bundle_by_name (_("out 1"))) == 0) {
-                               error << _("No output bundles available as a replacement")
-                                     << endmsg;
-                               return -1;
-                       }  else {
-                               info << string_compose (_("Bundle %1 was not available - \"out 1\" used instead"), prop->value())
-                                    << endmsg;
-                       }
-               } 
-
-               connect_output_ports_to_bundle (c, this);
-               
-       } else if ((prop = node.property ("outputs")) != 0) {
+                               
+       if ((prop = node.property ("outputs")) != 0) {
                if (set_outputs (prop->value())) {
                        error << string_compose(_("improper output channel list in XML node (%1)"), prop->value()) << endmsg;
                        return -1;
                }
        }
+
+       for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
+
+               if ((*i)->name() == "InputBundle") {
+                       XMLProperty const * prop = (*i)->property ("name");
+                       if (prop) {
+                               boost::shared_ptr<Bundle> b = _session.bundle_by_name (prop->value());
+                               if (b) {
+                                       connect_input_ports_to_bundle (b, this);
+                               } else {
+                                       error << string_compose(_("Unknown bundle \"%1\" listed for input of %2"), prop->value(), _name) << endmsg;
+                               }
+                       }
+                       
+               } else if ((*i)->name() == "OutputBundle") {
+                       XMLProperty const * prop = (*i)->property ("name");
+                       if (prop) {
+                               boost::shared_ptr<Bundle> b = _session.bundle_by_name (prop->value());
+                               if (b) {
+                                       connect_output_ports_to_bundle (b, this);
+                               } else {
+                                       error << string_compose(_("Unknown bundle \"%1\" listed for output of %2"), prop->value(), _name) << endmsg;
+                               }
+                       }
+               }
+       }
        
        return 0;
 }
@@ -1849,7 +1827,7 @@ IO::set_name (const string& str)
 
        bool const r = SessionObject::set_name(name);
 
-       setup_bundles ();
+       setup_bundles_for_inputs_and_outputs ();
 
        return r;
 }
@@ -1929,61 +1907,21 @@ IO::input_latency () const
 int
 IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 {
-       uint32_t limit;
-
        {
                BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm2 (io_lock);
-               
-               limit = c->nchannels();
-               
-               drop_input_bundle ();
-               
-               // FIXME bundles only work for audio-only
-               if (ensure_inputs (ChanCount(DataType::AUDIO, limit), false, false, src)) {
-                       return -1;
-               }
 
-               /* first pass: check the current state to see what's correctly
-                  connected, and drop anything that we don't want.
-               */
-               
-               for (uint32_t n = 0; n < limit; ++n) {
-                       const Bundle::PortList& pl = c->channel_ports (n);
-                       
-                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                               
-                               if (!_inputs.port(n)->connected_to ((*i))) {
-                                       
-                                       /* clear any existing connections */
-                                       
-                                       _session.engine().disconnect (*_inputs.port(n));
-                                       
-                               } else if (_inputs.port(n)->connected() > 1) {
-                                       
-                                       /* OK, it is connected to the port we want,
-                                          but its also connected to other ports.
-                                          Change that situation.
-                                       */
-                                       
-                                       /* XXX could be optimized to not drop
-                                          the one we want.
-                                       */
-                                       
-                                       _session.engine().disconnect (*_inputs.port(n));
-                                       
-                               }
-                       }
-               }
-               
-               /* second pass: connect all requested ports where necessary */
+               /* Connect to the bundle, not worrying about any connections
+                  that are already made. */
+
+               uint32_t const channels = c->nchannels ();
                
-               for (uint32_t n = 0; n < limit; ++n) {
-                       const Bundle::PortList& pl = c->channel_ports (n);
-                       
-                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                               
-                               if (!_inputs.port(n)->connected_to ((*i))) {
+               for (uint32_t n = 0; n < channels; ++n) {
+                       const PortList& pl = c->channel_ports (n);
+
+                       for (PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
+
+                         if (!_inputs.port(n)->connected_to (*i)) {
                                        
                                        if (_session.engine().connect (*i, _inputs.port(n)->name())) {
                                                return -1;
@@ -1992,13 +1930,23 @@ IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
                                
                        }
                }
-               
-               _input_bundle = c;
-               
-               input_bundle_configuration_connection = c->ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::input_bundle_configuration_changed));
-               input_bundle_connection_connection = c->PortsChanged.connect
-                       (mem_fun (*this, &IO::input_bundle_connection_changed));
+
+               /* If this is a UserBundle, make a note of what we've done */
+
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
+
+                       /* See if we already know about this one */
+                       std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_inputs.begin();
+                       while (i != _bundles_connected_to_inputs.end() && i->bundle != ub) {
+                               ++i;
+                       }
+
+                       if (i == _bundles_connected_to_inputs.end()) {
+                               /* We don't, so make a note */
+                               _bundles_connected_to_inputs.push_back (UserBundleInfo (this, ub));
+                       }
+               }
        }
 
        input_changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
@@ -2008,62 +1956,22 @@ IO::connect_input_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 int
 IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
 {
-       uint32_t limit; 
-
        {
                BLOCK_PROCESS_CALLBACK ();
                Glib::Mutex::Lock lm2 (io_lock);
 
-               limit = c->nchannels();
-                       
-               drop_output_bundle ();
+               /* Connect to the bundle, not worrying about any connections
+                  that are already made. */
 
-               // FIXME: audio-only
-               if (ensure_outputs (ChanCount(DataType::AUDIO, limit), false, false, src)) {
-                       return -1;
-               }
+               uint32_t const channels = c->nchannels ();
 
-               /* first pass: check the current state to see what's correctly
-                  connected, and drop anything that we don't want.
-               */
-                       
-               for (uint32_t n = 0; n < limit; ++n) {
-
-                       const Bundle::PortList& pl = c->channel_ports (n);
-                               
-                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                                       
-                               if (!_outputs.port(n)->connected_to ((*i))) {
-
-                                       /* clear any existing connections */
-
-                                       _session.engine().disconnect (*_outputs.port(n));
-
-                               } else if (_outputs.port(n)->connected() > 1) {
-
-                                       /* OK, it is connected to the port we want,
-                                          but its also connected to other ports.
-                                          Change that situation.
-                                       */
-
-                                       /* XXX could be optimized to not drop
-                                          the one we want.
-                                       */
-                                               
-                                       _session.engine().disconnect (*_outputs.port(n));
-                               }
-                       }
-               }
+               for (uint32_t n = 0; n < channels; ++n) {
 
-               /* second pass: connect all requested ports where necessary */
+                       const PortList& pl = c->channel_ports (n);
 
-               for (uint32_t n = 0; n < limit; ++n) {
+                       for (PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
 
-                       const Bundle::PortList& pl = c->channel_ports (n);
-                               
-                       for (Bundle::PortList::const_iterator i = pl.begin(); i != pl.end(); ++i) {
-                                       
-                               if (!_outputs.port(n)->connected_to ((*i))) {
+                               if (!_outputs.port(n)->connected_to (*i)) {
                                                
                                        if (_session.engine().connect (_outputs.port(n)->name(), *i)) {
                                                return -1;
@@ -2072,12 +1980,22 @@ IO::connect_output_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
                        }
                }
 
-               _output_bundle = c;
+               /* If this is a UserBundle, make a note of what we've done */
+
+               boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
+               if (ub) {
+
+                       /* See if we already know about this one */
+                       std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_outputs.begin();
+                       while (i != _bundles_connected_to_outputs.end() && i->bundle != ub) {
+                               ++i;
+                       }
 
-               output_bundle_configuration_connection = c->ConfigurationChanged.connect
-                       (mem_fun (*this, &IO::output_bundle_configuration_changed));
-               output_bundle_connection_connection = c->PortsChanged.connect
-                       (mem_fun (*this, &IO::output_bundle_connection_changed));
+                       if (i == _bundles_connected_to_outputs.end()) {
+                               /* We don't, so make a note */
+                               _bundles_connected_to_outputs.push_back (UserBundleInfo (this, ub));
+                       }
+               }
        }
 
        output_changed (IOChange (ConnectionsChanged|ConfigurationChanged), src); /* EMIT SIGNAL */
@@ -2128,27 +2046,31 @@ IO::reset_panners ()
 }
 
 void
-IO::input_bundle_connection_changed (int ignored)
+IO::bundle_configuration_will_change ()
 {
-       connect_input_ports_to_bundle (_input_bundle, this);
+       //XXX
+//     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::input_bundle_configuration_changed ()
+IO::bundle_configuration_has_changed ()
 {
-       connect_input_ports_to_bundle (_input_bundle, this);
+       //XXX
+//     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
 void
-IO::output_bundle_connection_changed (int ignored)
+IO::bundle_ports_will_change (int ignored)
 {
-       connect_output_ports_to_bundle (_output_bundle, this);
+//XXX
+//     connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
-IO::output_bundle_configuration_changed ()
+IO::bundle_ports_have_changed (int ignored)
 {
-       connect_output_ports_to_bundle (_output_bundle, this);
+       //XXX
+//     connect_output_ports_to_bundle (_output_bundle, this);
 }
 
 void
@@ -2306,7 +2228,7 @@ IO::automation_snapshot (nframes_t now)
 {
        Automatable::automation_snapshot (now);
 
-       if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _session.automation_interval()) {
+       if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
                _panner->snapshot (now);
        }
 }
@@ -2329,8 +2251,62 @@ IO::transport_stopped (nframes_t frame)
        _panner->transport_stopped (frame);
 }
 
+string
+IO::build_legal_port_name (DataType type, bool in)
+{
+       const int name_size = jack_port_name_size();
+       int limit;
+       string suffix;
+       int maxports;
+
+       if (type == DataType::AUDIO) {
+               suffix = _("audio");
+       } else if (type == DataType::MIDI) {
+               suffix = _("midi");
+       } else {
+               throw unknown_type();
+       }
+       
+       if (in) {
+               suffix += _("_in");
+               maxports = _input_maximum.get(type);
+       } else {
+               suffix += _("_out");
+               maxports = _output_maximum.get(type);
+       }
+       
+       if (maxports == 1) {
+               // allow space for the slash + the suffix
+               limit = name_size - _session.engine().client_name().length() - (suffix.length() + 1);
+               char buf[name_size+1];
+               snprintf (buf, name_size+1, ("%.*s/%s"), limit, _name.c_str(), suffix.c_str());
+               return string (buf);
+       } 
+       
+       // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
+
+       limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5);
+
+       char buf1[name_size+1];
+       char buf2[name_size+1];
+       
+       snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.c_str(), suffix.c_str());
+       
+       int port_number;
+       
+       if (in) {
+               port_number = find_input_port_hole (buf1);
+       } else {
+               port_number = find_output_port_hole (buf1);
+       }
+       
+       snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
+       
+       return string (buf2);
+}
+
 int32_t
-IO::find_input_port_hole ()
+IO::find_input_port_hole (const char* base)
 {
        /* CALLER MUST HOLD IO LOCK */
 
@@ -2340,11 +2316,14 @@ IO::find_input_port_hole ()
                return 1;
        }
 
-       for (n = 1; n < UINT_MAX; ++n) {
+       /* we only allow up to 4 characters for the port number
+        */
+
+       for (n = 1; n < 9999; ++n) {
                char buf[jack_port_name_size()];
                PortSet::iterator i = _inputs.begin();
 
-               snprintf (buf, jack_port_name_size(), _("%s/in %u"), _name.c_str(), n);
+               snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
 
                for ( ; i != _inputs.end(); ++i) {
                        if (i->short_name() == buf) {
@@ -2360,7 +2339,7 @@ IO::find_input_port_hole ()
 }
 
 int32_t
-IO::find_output_port_hole ()
+IO::find_output_port_hole (const char* base)
 {
        /* CALLER MUST HOLD IO LOCK */
 
@@ -2370,11 +2349,14 @@ IO::find_output_port_hole ()
                return 1;
        }
 
-       for (n = 1; n < UINT_MAX; ++n) {
+       /* we only allow up to 4 characters for the port number
+        */
+
+       for (n = 1; n < 9999; ++n) {
                char buf[jack_port_name_size()];
                PortSet::iterator i = _outputs.begin();
 
-               snprintf (buf, jack_port_name_size(), _("%s/out %u"), _name.c_str(), n);
+               snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
 
                for ( ; i != _outputs.end(); ++i) {
                        if (i->short_name() == buf) {
@@ -2390,6 +2372,14 @@ IO::find_output_port_hole ()
        return n;
 }
 
+void
+IO::set_active (bool yn)
+{
+       _active = yn; 
+        active_changed(); /* EMIT SIGNAL */
+}
+
+
 AudioPort*
 IO::audio_input(uint32_t n) const
 {
@@ -2452,27 +2442,25 @@ IO::update_port_total_latencies ()
  */
 
 void
-IO::setup_bundles ()
+IO::setup_bundles_for_inputs_and_outputs ()
 {
         char buf[32];
 
         snprintf(buf, sizeof (buf), _("%s in"), _name.c_str());
-        _bundle_for_inputs->set_name (buf, 0);
-        int const ins = n_inputs().n_total();
-        _bundle_for_inputs->set_nchannels (ins);
-        
-        for (int i = 0; i < ins; ++i) {
-                _bundle_for_inputs->add_port_to_channel (i, _inputs.port(i)->name ());
-        }
+        _bundle_for_inputs->set_name (buf);
+       uint32_t const ni = inputs().num_ports();
+       _bundle_for_inputs->set_channels (ni);
+       for (uint32_t i = 0; i < ni; ++i) {
+               _bundle_for_inputs->set_port (i, inputs().port(i)->name());
+       }
 
         snprintf(buf, sizeof (buf), _("%s out"), _name.c_str());
-        _bundle_for_outputs->set_name (buf, 0);
-        int const outs = n_outputs().n_total();
-        _bundle_for_outputs->set_nchannels (outs);
-        
-        for (int i = 0; i < outs; ++i) {
-                _bundle_for_outputs->add_port_to_channel (i, _outputs.port(i)->name ());
-        }
+        _bundle_for_outputs->set_name (buf);
+       uint32_t const no = outputs().num_ports();
+       _bundle_for_outputs->set_channels (no);
+       for (uint32_t i = 0; i < no; ++i) {
+               _bundle_for_outputs->set_port (i, outputs().port(i)->name());
+       }
 }
 
 
@@ -2481,64 +2469,132 @@ IO::setup_bundles ()
  */
 
 void
-IO::create_bundles ()
+IO::create_bundles_for_inputs_and_outputs ()
 {
-        _bundle_for_inputs = boost::shared_ptr<Bundle> (
-                new InputBundle ("", true)
-                );
-        
-        _bundle_for_outputs = boost::shared_ptr<Bundle> (
-                new OutputBundle ("", true)
-                );
-
-        setup_bundles ();
+       _bundle_for_inputs = boost::shared_ptr<AutoBundle> (new AutoBundle (true));
+        _bundle_for_outputs = boost::shared_ptr<AutoBundle> (new AutoBundle (false));
+        setup_bundles_for_inputs_and_outputs ();
 }
 
-boost::shared_ptr<Bundle>
-IO::input_bundle()
+/** Add a bundle to a list if is connected to our inputs.
+ *  @param b Bundle to check.
+ *  @param bundles List to add to.
+ */
+void
+IO::maybe_add_input_bundle_to_list (boost::shared_ptr<Bundle> b, std::vector<boost::shared_ptr<Bundle> >* bundles)
 {
-       if (_input_bundle) {
-               return _input_bundle;
+       boost::shared_ptr<AutoBundle> ab = boost::dynamic_pointer_cast<AutoBundle, Bundle> (b);
+       if (ab == 0 || ab->ports_are_outputs() == false) {
+               return;
        }
 
-       /* XXX: will only report the first bundle found; should really return a list, I think */
-          
-       /* check that _input_bundle is right wrt the connections that are currently made */
+       if (ab->nchannels () != n_inputs().n_total ()) {
+               return;
+       }
+
+       for (uint32_t i = 0; i < n_inputs().n_total (); ++i) {
+
+               PortList const & pl = b->channel_ports (i);
+
+               if (pl.empty()) {
+                       return;
+               }
 
-       /* make a vector of the first output connected to each of our inputs */
-       std::vector<std::string> connected;
-        for (uint32_t i = 0; i < _inputs.num_ports(); ++i) {
-               const char** c = _inputs.port(i)->get_connections ();
-               if (c) {
-                       connected.push_back (c[0]);
+               if (!input(i)->connected_to (pl[0])) {
+                       return;
                }
        }
 
-       _input_bundle = _session.bundle_by_ports (connected);
-       return _input_bundle;
+       bundles->push_back (b);
 }
 
+/** @return Bundles connected to our inputs */
+std::vector<boost::shared_ptr<Bundle> >
+IO::bundles_connected_to_inputs ()
+{
+       std::vector<boost::shared_ptr<Bundle> > bundles;
+       
+       /* User bundles */
+       for (std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_inputs.begin(); i != _bundles_connected_to_inputs.end(); ++i) {
+               bundles.push_back (i->bundle);
+       }
+
+       /* Auto bundles */
+       _session.foreach_bundle (
+               sigc::bind (sigc::mem_fun (*this, &IO::maybe_add_input_bundle_to_list), &bundles)
+               );
 
-boost::shared_ptr<Bundle>
-IO::output_bundle()
+       return bundles;
+}
+
+
+/** Add a bundle to a list if is connected to our outputs.
+ *  @param b Bundle to check.
+ *  @param bundles List to add to.
+ */
+void
+IO::maybe_add_output_bundle_to_list (boost::shared_ptr<Bundle> b, std::vector<boost::shared_ptr<Bundle> >* bundles)
 {
-       if (_output_bundle) {
-               return _output_bundle;
+       boost::shared_ptr<AutoBundle> ab = boost::dynamic_pointer_cast<AutoBundle, Bundle> (b);
+       if (ab == 0 || ab->ports_are_inputs() == false) {
+               return;
        }
-       
-       /* XXX: will only report the first bundle found; should really return a list, I think */
-          
-       /* check that _output_bundle is right wrt the connections that are currently made */
 
-       /* make a vector of the first input connected to each of our outputs */
-       std::vector<std::string> connected;
-        for (uint32_t i = 0; i < _outputs.num_ports(); ++i) {
-               const char** c = _outputs.port(i)->get_connections ();
-               if (c) {
-                       connected.push_back (c[0]);
+       if (ab->nchannels () != n_outputs().n_total ()) {
+               return;
+       }
+
+       for (uint32_t i = 0; i < n_outputs().n_total (); ++i) {
+
+               PortList const & pl = b->channel_ports (i);
+
+               if (pl.empty()) {
+                       return;
+               }
+
+               if (!output(i)->connected_to (pl[0])) {
+                       return;
                }
        }
 
-       _output_bundle = _session.bundle_by_ports (connected);
-       return _output_bundle;
+       bundles->push_back (b);
+}
+
+
+/* @return Bundles connected to our outputs */
+std::vector<boost::shared_ptr<Bundle> >
+IO::bundles_connected_to_outputs ()
+{
+       std::vector<boost::shared_ptr<Bundle> > bundles;
+
+       /* User bundles */
+       for (std::vector<UserBundleInfo>::iterator i = _bundles_connected_to_outputs.begin(); i != _bundles_connected_to_outputs.end(); ++i) {
+               bundles.push_back (i->bundle);
+       }
+
+       /* Auto bundles */
+       _session.foreach_bundle (
+               sigc::bind (sigc::mem_fun (*this, &IO::maybe_add_output_bundle_to_list), &bundles)
+               );
+
+       return bundles; 
 }
+
+
+IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
+{
+       bundle = b;
+       configuration_will_change = b->ConfigurationWillChange.connect (
+               sigc::mem_fun (*io, &IO::bundle_configuration_will_change)
+               );
+       configuration_has_changed = b->ConfigurationHasChanged.connect (
+               sigc::mem_fun (*io, &IO::bundle_configuration_has_changed)
+               );
+       ports_will_change = b->PortsWillChange.connect (
+               sigc::mem_fun (*io, &IO::bundle_ports_will_change)
+               );
+       ports_have_changed = b->PortsHaveChanged.connect (
+               sigc::mem_fun (*io, &IO::bundle_ports_have_changed)
+               );
+}
+