patch for CUE file formatting from Andreas Ruge
[ardour.git] / libs / ardour / io.cc
index 4f1b03dfdaf8d4684766b525878339db5cb9e5d8..3805d7ac837eac14f16c13e876471e9643b6e49a 100644 (file)
@@ -69,6 +69,7 @@ IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
        , _default_type (default_type)
 {
        _active = true;
+       Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
        pending_state_node = 0;
        setup_bundle ();
 }
@@ -80,6 +81,7 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
 {
        _active = true;
        pending_state_node = 0;
+       Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
 
        set_state (node, Stateful::loading_state_version);
        setup_bundle ();
@@ -96,6 +98,31 @@ IO::~IO ()
        }
 }
 
+void
+IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
+{
+       /* this could be called from within our own ::disconnect() method(s)
+          or from somewhere that operates directly on a port. so, we don't
+          know for sure if we can take this lock or not. if we fail,
+          we assume that its safely locked by our own ::disconnect().
+       */
+
+       Glib::Mutex::Lock tm (io_lock, Glib::TRY_LOCK);
+
+       if (tm.locked()) {
+               /* we took the lock, so we cannot be here from inside
+                * ::disconnect()
+                */
+               if (_ports.contains (a) || _ports.contains (b)) {
+                       changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */              
+               }
+       } else {
+               /* we didn't get the lock, so assume that we're inside
+                * ::disconnect(), and it will call changed() appropriately.
+                */
+       }
+}
+
 void
 IO::increment_port_buffer_offset (pframes_t offset)
 {
@@ -292,6 +319,15 @@ IO::add_port (string destination, void* src, DataType type)
                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;
 
        {
@@ -1243,6 +1279,7 @@ IO::disable_connecting ()
 int
 IO::enable_connecting ()
 {
+       Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
        connecting_legal = true;
        boost::optional<int> r = ConnectingLegal ();
        return r.get_value_or (0);
@@ -1478,10 +1515,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));
+               }
        }
 }
 
@@ -1535,12 +1576,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);
 }