major fixes for MIDI patch change and note undo/redo. Patch change handling was compl...
[ardour.git] / libs / ardour / port.cc
index 45874bba8db1ac769982968436f786d9ed4ff272..3473b73617a84226bb442a1bc421bc717fc1ce57 100644 (file)
 #include "libardour-config.h"
 #endif
 
-#include <stdexcept>
-
 #include <jack/weakjack.h> // so that we can test for new functions at runtime
 
-#include "pbd/error.h"
 #include "pbd/compose.h"
+#include "pbd/error.h"
+#include "pbd/failed_constructor.h"
 
+#include "ardour/audioengine.h"
 #include "ardour/debug.h"
 #include "ardour/port.h"
-#include "ardour/audioengine.h"
-#include "pbd/failed_constructor.h"
 
 #include "i18n.h"
 
@@ -40,6 +38,7 @@ using namespace ARDOUR;
 using namespace PBD;
 
 PBD::Signal2<void,boost::shared_ptr<Port>, boost::shared_ptr<Port> > Port::PostDisconnect;
+PBD::Signal0<void> Port::PortDrop;
 
 AudioEngine* Port::_engine = 0;
 bool         Port::_connecting_blocked = false;
@@ -70,16 +69,27 @@ Port::Port (std::string const & n, DataType t, Flags f)
        }
 
        if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
-               cerr << "Failed to register JACK port, reason is unknown from here\n";
+               cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
                throw failed_constructor ();
        }
+
+       PortDrop.connect_same_thread (drop_connection, boost::bind (&Port::drop, this));
 }
 
 /** Port destructor */
 Port::~Port ()
 {
-       if (_engine->jack ()) {
-               jack_port_unregister (_engine->jack (), _jack_port);
+       drop ();
+}
+
+void
+Port::drop ()
+{
+       if (_jack_port) {
+               if (_engine->jack ()) {
+                       jack_port_unregister (_engine->jack (), _jack_port);
+               }
+               _jack_port = 0;
        }
 }
 
@@ -472,9 +482,11 @@ Port::set_name (std::string const & n)
        int const r = jack_port_set_name (_jack_port, n.c_str());
 
        if (r == 0) {
+               _engine->port_renamed (_name, n);
                _name = n;
        }
 
+
        return r;
 }