better handling of the inverse-push of solo-by-upstream. still not quite right, but...
[ardour.git] / libs / ardour / bundle.cc
index bdad9d364da0141eaa0faeefd215ae63d6feef01..f409e0beee4eb1493e9696a3f772a4564213539f 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002 Paul Davis 
+    Copyright (C) 2002 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 #include <algorithm>
 
-#include <pbd/failed_constructor.h>
-#include <ardour/ardour.h>
-#include <ardour/bundle.h>
-#include <pbd/xml++.h>
+#include "pbd/failed_constructor.h"
+#include "ardour/ardour.h"
+#include "ardour/bundle.h"
+#include "ardour/audioengine.h"
+#include "ardour/port.h"
+#include "pbd/xml++.h"
 
 #include "i18n.h"
 
+using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
+/** Construct an audio bundle.
+ *  @param i true if ports are inputs, otherwise false.
+ */
+Bundle::Bundle (bool i)
+       : _type (DataType::AUDIO),
+         _ports_are_inputs (i),
+         _signals_suspended (false),
+         _pending_change (Change (0))
+{
+
+}
+
+
+/** Construct an audio bundle.
+ *  @param n Name.
+ *  @param i true if ports are inputs, otherwise false.
+ */
+Bundle::Bundle (std::string const & n, bool i)
+       : _name (n),
+         _type (DataType::AUDIO),
+         _ports_are_inputs (i),
+         _signals_suspended (false),
+         _pending_change (Change (0))
+{
+
+}
+
+
+/** Construct a bundle.
+ *  @param n Name.
+ *  @param t Type.
+ *  @param i true if ports are inputs, otherwise false.
+ */
+Bundle::Bundle (std::string const & n, DataType t, bool i)
+       : _name (n),
+         _type (t),
+         _ports_are_inputs (i),
+         _signals_suspended (false),
+         _pending_change (Change (0))
+{
+
+}
+
+
+Bundle::Bundle (boost::shared_ptr<Bundle> other)
+       : _channel (other->_channel),
+         _name (other->_name),
+         _type (other->_type),
+         _ports_are_inputs (other->_ports_are_inputs),
+         _signals_suspended (other->_signals_suspended),
+         _pending_change (other->_pending_change)
+{
+
+}
+
 uint32_t
 Bundle::nchannels () const
 {
-       Glib::Mutex::Lock lm (_ports_mutex);
-       return _ports.size ();
+       Glib::Mutex::Lock lm (_channel_mutex);
+       return _channel.size ();
 }
 
 Bundle::PortList const &
@@ -41,25 +99,26 @@ Bundle::channel_ports (uint32_t c) const
 {
        assert (c < nchannels());
 
-       Glib::Mutex::Lock lm (_ports_mutex);
-       return _ports[c];
+       Glib::Mutex::Lock lm (_channel_mutex);
+       return _channel[c].ports;
 }
 
 /** Add an association between one of our channels and a port.
  *  @param ch Channel index.
- *  @param portname port name to associate with.
+ *  @param portname full port name to associate with (including prefix).
  */
 void
 Bundle::add_port_to_channel (uint32_t ch, string portname)
 {
        assert (ch < nchannels());
+       assert (portname.find_first_of (':') != string::npos);
 
        {
-               Glib::Mutex::Lock lm (_ports_mutex);
-               _ports[ch].push_back (portname);
+               Glib::Mutex::Lock lm (_channel_mutex);
+               _channel[ch].ports.push_back (portname);
        }
-       
-       PortsChanged (ch); /* EMIT SIGNAL */
+
+       emit_changed (PortsChanged);
 }
 
 /** Disassociate a port from one of our channels.
@@ -74,10 +133,10 @@ Bundle::remove_port_from_channel (uint32_t ch, string portname)
        bool changed = false;
 
        {
-               Glib::Mutex::Lock lm (_ports_mutex);
-               PortList& pl = _ports[ch];
+               Glib::Mutex::Lock lm (_channel_mutex);
+               PortList& pl = _channel[ch].ports;
                PortList::iterator i = find (pl.begin(), pl.end(), portname);
-               
+
                if (i != pl.end()) {
                        pl.erase (i);
                        changed = true;
@@ -85,77 +144,348 @@ Bundle::remove_port_from_channel (uint32_t ch, string portname)
        }
 
        if (changed) {
-                PortsChanged (ch); /* EMIT SIGNAL */
+               emit_changed (PortsChanged);
        }
 }
 
-/** operator== for Bundles; they are equal if their channels are the same.
- * @param other Bundle to compare with this one.
+/** Set a single port to be associated with a channel, removing any others.
+ *  @param ch Channel.
+ *  @param portname Full port name, including prefix.
  */
+void
+Bundle::set_port (uint32_t ch, string portname)
+{
+       assert (ch < nchannels());
+       assert (portname.find_first_of (':') != string::npos);
+
+       {
+               Glib::Mutex::Lock lm (_channel_mutex);
+               _channel[ch].ports.clear ();
+               _channel[ch].ports.push_back (portname);
+       }
+
+       emit_changed (PortsChanged);
+}
+
+/** @param n Channel name */
+void
+Bundle::add_channel (std::string const & n)
+{
+       {
+               Glib::Mutex::Lock lm (_channel_mutex);
+               _channel.push_back (Channel (n));
+       }
+
+       emit_changed (ConfigurationChanged);
+}
+
 bool
-Bundle::operator== (const Bundle& other) const
+Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
 {
-       return other._ports == _ports;
+       assert (ch < nchannels());
+
+       Glib::Mutex::Lock lm (_channel_mutex);
+       return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
+}
+
+/** Remove a channel.
+ *  @param ch Channel.
+ */
+void
+Bundle::remove_channel (uint32_t ch)
+{
+       assert (ch < nchannels ());
+
+       Glib::Mutex::Lock lm (_channel_mutex);
+       _channel.erase (_channel.begin () + ch);
+}
+
+/** Remove all channels */
+void
+Bundle::remove_channels ()
+{
+       Glib::Mutex::Lock lm (_channel_mutex);
+
+       _channel.clear ();
+}
+
+/** @param p Port name.
+ *  @return true if any channel is associated with p.
+ */
+bool
+Bundle::uses_port (std::string p) const
+{
+       Glib::Mutex::Lock lm (_channel_mutex);
+
+       for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
+               for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
+                       if (*j == p) {
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
+
+/** @param p Port name.
+ *  @return true if this bundle offers this port on its own on a channel.
+ */
+bool
+Bundle::offers_port_alone (std::string p) const
+{
+       Glib::Mutex::Lock lm (_channel_mutex);
+
+       for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
+               if (i->ports.size() == 1 && i->ports[0] == p) {
+                       return true;
+               }
+       }
+
+       return false;
 }
 
 
-/** Set the number of channels.
- * @param n New number of channels.
+/** @param ch Channel.
+ *  @return Channel name.
  */
+std::string
+Bundle::channel_name (uint32_t ch) const
+{
+       assert (ch < nchannels());
 
+       Glib::Mutex::Lock lm (_channel_mutex);
+       return _channel[ch].name;
+}
+
+/** Set the name of a channel.
+ *  @param ch Channel.
+ *  @param n New name.
+ */
 void
-Bundle::set_nchannels (uint32_t n)
+Bundle::set_channel_name (uint32_t ch, std::string const & n)
 {
+       assert (ch < nchannels());
+
        {
-               Glib::Mutex::Lock lm (_ports_mutex);
-               _ports.clear ();
-               for (uint32_t i = 0; i < n; ++i) {
-                       _ports.push_back (PortList());
+               Glib::Mutex::Lock lm (_channel_mutex);
+               _channel[ch].name = n;
+       }
+
+       emit_changed (NameChanged);
+}
+
+/** Take the channels from another bundle and add them to this bundle,
+ *  so that channels from other are added to this (with their ports)
+ *  and are named "<other_bundle_name> <other_channel_name>".
+ */
+void
+Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
+{
+       uint32_t const ch = nchannels ();
+
+       for (uint32_t i = 0; i < other->nchannels(); ++i) {
+
+               std::stringstream s;
+               s << other->name() << " " << other->channel_name(i);
+
+               add_channel (s.str());
+
+               PortList const& pl = other->channel_ports (i);
+               for (uint32_t j = 0; j < pl.size(); ++j) {
+                       add_port_to_channel (ch + i, pl[j]);
                }
        }
+}
 
-       ConfigurationChanged (); /* EMIT SIGNAL */
+/** Connect the ports associated with our channels to the ports associated
+ *  with another bundle's channels.
+ *  @param other Other bundle.
+ *  @param engine AudioEngine to use to make the connections.
+ */
+void
+Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
+{
+       uint32_t const N = nchannels ();
+       assert (N == other->nchannels ());
+
+       for (uint32_t i = 0; i < N; ++i) {
+               Bundle::PortList const & our_ports = channel_ports (i);
+               Bundle::PortList const & other_ports = other->channel_ports (i);
+
+               for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
+                       for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
+                               engine.connect (*j, *k);
+                       }
+               }
+       }
 }
 
 void
-Bundle::set_port (uint32_t ch, string portname)
+Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
 {
-       assert (ch < nchannels());
+       uint32_t const N = nchannels ();
+       assert (N == other->nchannels ());
+
+       for (uint32_t i = 0; i < N; ++i) {
+               Bundle::PortList const & our_ports = channel_ports (i);
+               Bundle::PortList const & other_ports = other->channel_ports (i);
+
+               for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
+                       for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
+                               engine.disconnect (*j, *k);
+                       }
+               }
+       }
+}
 
+/** Remove all ports from all channels */
+void
+Bundle::remove_ports_from_channels ()
+{
        {
-               Glib::Mutex::Lock lm (_ports_mutex);
-               _ports[ch].clear ();
-               _ports[ch].push_back (portname);
+               Glib::Mutex::Lock lm (_channel_mutex);
+               for (uint32_t c = 0; c < _channel.size(); ++c) {
+                       _channel[c].ports.clear ();
+               }
+
        }
 
-       PortsChanged (ch); /* EMIT SIGNAL */
+       emit_changed (PortsChanged);
 }
 
+/** Remove all ports from a given channel.
+ *  @param ch Channel.
+ */
 void
-Bundle::add_channel ()
+Bundle::remove_ports_from_channel (uint32_t ch)
 {
+       assert (ch < nchannels ());
+
        {
-               Glib::Mutex::Lock lm (_ports_mutex);
-               _ports.push_back (PortList ());
+               Glib::Mutex::Lock lm (_channel_mutex);
+               _channel[ch].ports.clear ();
        }
 
-       ConfigurationChanged (); /* EMIT SIGNAL */
+       emit_changed (PortsChanged);
+}
+
+void
+Bundle::suspend_signals ()
+{
+       _signals_suspended = true;
+}
+
+void
+Bundle::resume_signals ()
+{
+       if (_pending_change) {
+               Changed (_pending_change);
+               _pending_change = Change (0);
+       }
+
+       _signals_suspended = false;
+}
+
+void
+Bundle::emit_changed (Change c)
+{
+       if (_signals_suspended) {
+               _pending_change = Change (int (_pending_change) | int (c));
+       } else {
+               Changed (c);
+       }
 }
 
 bool
-Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
+Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
 {
-       assert (ch < nchannels());
-       
-       Glib::Mutex::Lock lm (_ports_mutex);
-       return (std::find (_ports[ch].begin (), _ports[ch].end (), portname) != _ports[ch].end ());
+       if (_ports_are_inputs == other->_ports_are_inputs ||
+           _type != other->_type ||
+           nchannels() != other->nchannels ()) {
+
+               return false;
+       }
+
+       for (uint32_t i = 0; i < nchannels(); ++i) {
+               Bundle::PortList const & A = channel_ports (i);
+               Bundle::PortList const & B = other->channel_ports (i);
+
+               for (uint32_t j = 0; j < A.size(); ++j) {
+                       for (uint32_t k = 0; k < B.size(); ++k) {
+
+                               Port* p = engine.get_port_by_name (A[j]);
+                               Port* q = engine.get_port_by_name (B[k]);
+
+                               if (!p && !q) {
+                                       return false;
+                               }
+
+                               if (p && !p->connected_to (B[k])) {
+                                       return false;
+                               } else if (q && !q->connected_to (A[j])) {
+                                       return false;
+                               }
+                       }
+               }
+       }
+
+       return true;
 }
 
+/** Set the type of the ports in this Bundle.
+ *  @param t New type.
+ */
 void
-Bundle::remove_channel (uint32_t ch)
+Bundle::set_type (DataType t)
 {
-       assert (ch < nchannels ());
+       _type = t;
+       emit_changed (TypeChanged);
+}
+
+void
+Bundle::set_ports_are_inputs ()
+{
+       _ports_are_inputs = true;
+       emit_changed (DirectionChanged);
+}
+
+void
+Bundle::set_ports_are_outputs ()
+{
+       _ports_are_inputs = false;
+       emit_changed (DirectionChanged);
+}
+
+/** Set the name.
+ *  @param n New name.
+ */
+void
+Bundle::set_name (string const & n)
+{
+       _name = n;
+       emit_changed (NameChanged);
+}
+
+/** @param b Other bundle.
+ *  @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
+ */
+bool
+Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
+{
+       uint32_t const N = nchannels ();
+
+       if (b->nchannels() != N) {
+               return false;
+       }
+
+       /* XXX: probably should sort channel port lists before comparing them */
+
+       for (uint32_t i = 0; i < N; ++i) {
+               if (channel_ports (i) != b->channel_ports (i)) {
+                       return false;
+               }
+       }
 
-       Glib::Mutex::Lock lm (_ports_mutex);
-       _ports.erase (_ports.begin () + ch);
+       return true;
 }