New test.
[ardour.git] / libs / ardour / io.cc
index 0d9933cb0c369e5eebeb63b9b0c3606843c138df..aac2adf7849dbd25813cba9e67b3fb5aa472b33c 100644 (file)
@@ -118,22 +118,15 @@ IO::silence (framecnt_t nframes)
        }
 }
 
-void
-IO::check_bundles_connected ()
-{
-       check_bundles (_bundles_connected, ports());
-}
-
-/** Check the bundles in list to see which are connected to a given PortSet,
- *  and update list with those that are connected such that every port on every
- *  bundle channel x is connected to port x in ports.
+/** Set _bundles_connected to those bundles that are connected such that every
+ *  port on every bundle channel x is connected to port x in _ports.
  */
 void
-IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
+IO::check_bundles_connected ()
 {
        std::vector<UserBundleInfo*> new_list;
 
-       for (std::vector<UserBundleInfo*>::iterator i = list.begin(); i != list.end(); ++i) {
+       for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
 
                uint32_t const N = (*i)->bundle->nchannels().n_total();
 
@@ -147,7 +140,7 @@ IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
                        /* Every port on bundle channel j must be connected to our input j */
                        Bundle::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) {
+                               if (_ports.port(j)->connected_to (pl[k]) == false) {
                                        ok = false;
                                        break;
                                }
@@ -165,12 +158,12 @@ IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
                }
        }
 
-       list = new_list;
+       _bundles_connected = new_list;
 }
 
 
 int
-IO::disconnect (Port* our_port, string other_port, void* src)
+IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
 {
        if (other_port.length() == 0 || our_port == 0) {
                return 0;
@@ -203,7 +196,7 @@ IO::disconnect (Port* our_port, string other_port, void* src)
 }
 
 int
-IO::connect (Port* our_port, string other_port, void* src)
+IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
 {
        if (other_port.length() == 0 || our_port == 0) {
                return 0;
@@ -230,7 +223,7 @@ IO::connect (Port* our_port, string other_port, void* src)
 }
 
 int
-IO::remove_port (Port* port, void* src)
+IO::remove_port (boost::shared_ptr<Port> port, void* src)
 {
        ChanCount before = _ports.count ();
        ChanCount after = before;
@@ -258,7 +251,7 @@ IO::remove_port (Port* port, void* src)
                                        change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
                                }
 
-                               _session.engine().unregister_port (*port);
+                               _session.engine().unregister_port (port);
                                check_bundles_connected ();
                        }
                }
@@ -293,12 +286,21 @@ IO::remove_port (Port* port, void* src)
 int
 IO::add_port (string destination, void* src, DataType type)
 {
-       Port* our_port;
+       boost::shared_ptr<Port> our_port;
 
        if (type == DataType::NIL) {
                type = _default_type;
        }
 
+       ChanCount before = _ports.count ();
+       ChanCount after = before;
+       after.set (type, after.get (type) + 1);
+       
+       bool const r = PortCountChanging (after); /* EMIT SIGNAL */
+       if (r) {
+               return -1;
+       }
+       
        IOChange change;
 
        {
@@ -366,13 +368,14 @@ IO::disconnect (void* src)
 }
 
 /** Caller must hold process lock */
-bool
-IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/)
+int
+IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
 {
        assert (!AudioEngine::instance()->process_lock().trylock());
 
-       Port* port = 0;
-       bool  changed    = false;
+       boost::shared_ptr<Port> port;
+
+       changed    = false;
 
        for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
 
@@ -384,7 +387,7 @@ IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/)
 
                        assert(port);
                        _ports.remove(port);
-                       _session.engine().unregister_port (*port);
+                       _session.engine().unregister_port (port);
 
                        changed = true;
                }
@@ -432,7 +435,7 @@ IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/)
                }
        }
 
-       return changed;
+       return 0;
 }
 
 /** Caller must hold process lock */
@@ -453,7 +456,9 @@ IO::ensure_ports (ChanCount count, bool clear, void* src)
 
        {
                Glib::Mutex::Lock im (io_lock);
-               changed = ensure_ports_locked (count, clear, src);
+               if (ensure_ports_locked (count, clear, changed)) {
+                       return -1;
+               }
        }
 
        if (changed) {
@@ -478,7 +483,7 @@ IO::ensure_io (ChanCount count, bool clear, void* src)
 }
 
 XMLNode&
-IO::get_state (void)
+IO::get_state ()
 {
        return state (true);
 }
@@ -572,9 +577,7 @@ IO::set_state (const XMLNode& node, int version)
                assert(_default_type != DataType::NIL);
        }
 
-       if ((prop = node.property ("id")) != 0) {
-               _id = prop->value ();
-       }
+       set_id (node);
 
        if ((prop = node.property ("direction")) != 0) {
                _direction = (Direction) string_2_enum (prop->value(), _direction);
@@ -630,9 +633,7 @@ IO::set_state_2X (const XMLNode& node, int version, bool in)
                assert(_default_type != DataType::NIL);
        }
 
-       if ((prop = node.property ("id")) != 0) {
-               _id = prop->value ();
-       }
+       set_id (node);
 
        _direction = in ? Input : Output;
 
@@ -902,7 +903,7 @@ IO::make_connections (const XMLNode& node, int version, bool in)
                                continue;
                        }
 
-                       Port* p = port_by_name (prop->value());
+                       boost::shared_ptr<Port> p = port_by_name (prop->value());
 
                        if (p) {
                                for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
@@ -1259,7 +1260,7 @@ IO::enable_connecting ()
 void
 IO::bundle_changed (Bundle::Change /*c*/)
 {
-       //XXX
+       /* XXX */
 //     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
@@ -1298,7 +1299,12 @@ IO::build_legal_port_name (DataType type)
        char buf1[name_size+1];
        char buf2[name_size+1];
 
-       snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.val().c_str(), suffix.c_str());
+       /* colons are illegal in port names, so fix that */
+
+       string nom = _name.val();
+       replace_all (nom, ":", ";");
+
+       snprintf (buf1, name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
 
        int port_number = find_port_hole (buf1);
        snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
@@ -1340,14 +1346,14 @@ IO::find_port_hole (const char* base)
 }
 
 
-AudioPort*
+boost::shared_ptr<AudioPort>
 IO::audio(uint32_t n) const
 {
        return _ports.nth_audio_port (n);
 
 }
 
-MidiPort*
+boost::shared_ptr<MidiPort>
 IO::midi(uint32_t n) const
 {
        return _ports.nth_midi_port (n);
@@ -1481,10 +1487,14 @@ IO::name_from_state (const XMLNode& node)
 void
 IO::set_name_in_state (XMLNode& node, const string& new_name)
 {
-       const XMLProperty* prop;
-
-       if ((prop = node.property ("name")) != 0) {
-               node.add_property ("name", new_name);
+       node.add_property (X_("name"), new_name);
+       XMLNodeList children = node.children ();
+       for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
+               if ((*i)->name() == X_("Port")) {
+                       string const old_name = (*i)->property(X_("name"))->value();
+                       string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
+                       (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
+               }
        }
 }
 
@@ -1538,12 +1548,19 @@ IO::connected_to (const string& str) const
        return false;
 }
 
-/** Caller must hold process lock */
+/** Call a processor's ::run() method, giving it our buffers
+ *  Caller must hold process lock.
+ */
 void
 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
 {
        /* don't read the data into new buffers - just use the port buffers directly */
 
+       if (n_ports().n_total() == 0) {
+               /* We have no ports, so nothing to process */
+               return;
+       }
+
        _buffers.get_jack_port_addresses (_ports, nframes);
        proc->run (_buffers, start_frame, end_frame, nframes, true);
 }
@@ -1602,21 +1619,19 @@ IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt
        }
 }
 
-Port*
+boost::shared_ptr<Port>
 IO::port_by_name (const std::string& str) const
 {
        /* to be called only from ::set_state() - no locking */
 
        for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
 
-               const Port& p(*i);
-
-               if (p.name() == str) {
-                       return const_cast<Port*>(&p);
+               if (i->name() == str) {
+                       return boost::const_pointer_cast<Port> (*i);
                }
        }
 
-       return 0;
+       return boost::shared_ptr<Port> ();
 }
 
 bool
@@ -1632,7 +1647,7 @@ IO::physically_connected () const
 }
 
 bool
-IO::has_port (Port* p) const
+IO::has_port (boost::shared_ptr<Port> p) const
 {
        Glib::Mutex::Lock lm (io_lock);
        return _ports.contains (p);