patch for CUE file formatting from Andreas Ruge
[ardour.git] / libs / ardour / io.cc
index 6aaca08c909aed08b8993821a06423ecb19ef548..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)
 {
@@ -118,22 +145,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 +167,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 +185,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 +223,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 +250,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 +278,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 +313,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;
 
        {
@@ -371,7 +400,7 @@ IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
 {
        assert (!AudioEngine::instance()->process_lock().trylock());
 
-       Port* port = 0;
+       boost::shared_ptr<Port> port;
 
        changed    = false;
 
@@ -385,7 +414,7 @@ IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
 
                        assert(port);
                        _ports.remove(port);
-                       _session.engine().unregister_port (*port);
+                       _session.engine().unregister_port (port);
 
                        changed = true;
                }
@@ -481,7 +510,7 @@ IO::ensure_io (ChanCount count, bool clear, void* src)
 }
 
 XMLNode&
-IO::get_state (void)
+IO::get_state ()
 {
        return state (true);
 }
@@ -575,9 +604,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);
@@ -633,9 +660,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;
 
@@ -905,7 +930,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) {
@@ -1254,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);
@@ -1262,7 +1288,7 @@ IO::enable_connecting ()
 void
 IO::bundle_changed (Bundle::Change /*c*/)
 {
-       //XXX
+       /* XXX */
 //     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
@@ -1348,14 +1374,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);
@@ -1489,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));
+               }
        }
 }
 
@@ -1546,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);
 }
@@ -1610,21 +1647,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
@@ -1640,7 +1675,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);