only the last step-edited note remains selected after each note addition; waf install...
[ardour.git] / gtk2_ardour / io_selector.cc
index fb7d946f7d75f6df25967b6fbbf70acb296d18a0..9399a6aaa7c5000c166b0e0219ba970c7baaf218 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002-2007 Paul Davis 
+    Copyright (C) 2002-2007 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
@@ -29,6 +29,7 @@
 #include "ardour/track.h"
 #include "ardour/audio_track.h"
 #include "ardour/midi_track.h"
+#include "ardour/mtdm.h"
 #include "ardour/data_type.h"
 #include "ardour/port.h"
 #include "ardour/bundle.h"
 using namespace ARDOUR;
 using namespace Gtk;
 
-IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool in)
-       : PortMatrix (session, io->default_type())
+IOSelector::IOSelector (Gtk::Window* p, ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io)
+       : PortMatrix (p, session, DataType::NIL)
        , _io (io)
-       , _find_inputs_for_io_outputs (in)
 {
+       setup_type ();
+       
        /* signal flow from 0 to 1 */
+
+       _find_inputs_for_io_outputs = (_io->direction() == IO::Output);
+
        if (_find_inputs_for_io_outputs) {
                _other = 1;
                _ours = 0;
@@ -55,27 +60,71 @@ IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO>
                _ours = 1;
        }
 
-       _port_group.reset (new PortGroup (""));
+       _port_group.reset (new PortGroup (io->name()));
        _ports[_ours].add_group (_port_group);
+
+       io->changed.connect (_io_connection, invalidator (*this), boost::bind (&IOSelector::io_changed_proxy, this), gui_context ());
+
+       setup_all_ports ();
+       init ();
+}
+
+void
+IOSelector::setup_type ()
+{
+       /* set type according to what's in the IO */
+
+       int N = 0;
+       DataType type_with_ports = DataType::NIL;
+       for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
+               if (_io->ports().num_ports (*i)) {
+                       type_with_ports = *i;
+                       ++N;
+               }
+       }
+
+       if (N <= 1) {
+               set_type (type_with_ports);
+       } else {
+               set_type (DataType::NIL);
+       }
+}
+
+void
+IOSelector::io_changed_proxy ()
+{
+       /* The IO's changed signal is emitted from code that holds its route's processor lock,
+          so we can't call setup_all_ports (which results in a call to Route::foreach_processor)
+          without a deadlock unless we break things up with this idle handler.
+       */
        
+       Glib::signal_idle().connect_once (sigc::mem_fun (*this, &IOSelector::io_changed));
+}
+
+void
+IOSelector::io_changed ()
+{
+       setup_type ();
        setup_all_ports ();
 }
 
 void
 IOSelector::setup_ports (int dim)
 {
+       if (!_session) {
+               return;
+       }
+
        _ports[dim].suspend_signals ();
 
        if (dim == _other) {
 
-               _ports[_other].gather (_session, _find_inputs_for_io_outputs);
-               
+               _ports[_other].gather (_session, type(), _find_inputs_for_io_outputs, false);
+
        } else {
 
                _port_group->clear ();
-               _port_group->add_bundle (
-                       _find_inputs_for_io_outputs ? _io->bundle_for_outputs() : _io->bundle_for_inputs()
-                       );
+               _port_group->add_bundle (_io->bundle (), _io);
        }
 
        _ports[dim].resume_signals ();
@@ -90,23 +139,15 @@ IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
        for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
                for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
 
-                       Port* f = _session.engine().get_port_by_name (*i);
+                       Port* f = _session->engine().get_port_by_name (*i);
                        if (!f) {
                                return;
                        }
 
                        if (s) {
-                               if (!_find_inputs_for_io_outputs) {
-                                       _io->connect_input (f, *j, 0);
-                               } else {
-                                       _io->connect_output (f, *j, 0);
-                               }
+                               _io->connect (f, *j, 0);
                        } else {
-                               if (!_find_inputs_for_io_outputs) {
-                                       _io->disconnect_input (f, *j, 0);
-                               } else {
-                                       _io->disconnect_output (f, *j, 0);
-                               }
+                               _io->disconnect (f, *j, 0);
                        }
                }
        }
@@ -121,18 +162,18 @@ IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
        if (our_ports.empty() || other_ports.empty()) {
                /* we're looking at a bundle with no parts associated with this channel,
                   so nothing to connect */
-               return PortMatrixNode::UNKNOWN;
+               return PortMatrixNode::NOT_ASSOCIATED;
        }
 
        for (ARDOUR::Bundle::PortList::const_iterator i = our_ports.begin(); i != our_ports.end(); ++i) {
                for (ARDOUR::Bundle::PortList::const_iterator j = other_ports.begin(); j != other_ports.end(); ++j) {
 
-                       Port* f = _session.engine().get_port_by_name (*i);
+                       Port* f = _session->engine().get_port_by_name (*i);
 
                        /* since we are talking about an IO, our ports should all have an associated Port *,
                           so the above call should never fail */
                        assert (f);
-                       
+
                        if (!f->connected_to (*j)) {
                                /* if any one thing is not connected, all bets are off */
                                return PortMatrixNode::NOT_ASSOCIATED;
@@ -147,162 +188,56 @@ uint32_t
 IOSelector::n_io_ports () const
 {
        if (!_find_inputs_for_io_outputs) {
-               return _io->inputs().num_ports (_io->default_type());
+               return _io->n_ports().get (_io->default_type());
        } else {
-               return _io->outputs().num_ports (_io->default_type());
+               return _io->n_ports().get (_io->default_type());
        }
 }
 
-void
-IOSelector::add_channel (boost::shared_ptr<ARDOUR::Bundle> b)
+bool
+IOSelector::list_is_global (int dim) const
 {
-       /* we ignore the bundle parameter, as we know what it is that we're adding to */
-       
-       // The IO selector only works for single typed IOs
-       const ARDOUR::DataType t = _io->default_type ();
-
-       if (!_find_inputs_for_io_outputs) {
-
-               try {
-                       _io->add_input_port ("", this);
-               }
-
-               catch (AudioEngine::PortRegistrationFailure& err) {
-                       MessageDialog msg (_("There are no more JACK ports available."));
-                       msg.run ();
-               }
-
-       } else {
-
-               try {
-                       _io->add_output_port ("", this);
-               }
-
-               catch (AudioEngine::PortRegistrationFailure& err) {
-                       MessageDialog msg (_("There are no more JACK ports available."));
-                       msg.run ();
-               }
-       }
+       return (dim == _other);
 }
 
-void
-IOSelector::remove_channel (ARDOUR::BundleChannel bc)
+string
+IOSelector::disassociation_verb () const
 {
-       Port* f = _session.engine().get_port_by_name (bc.bundle->channel_ports(bc.channel)[0]);
-       if (!f) {
-               return;
-       }
-       
-       if (_find_inputs_for_io_outputs) {
-               _io->remove_output_port (f, this);
-       } else {
-               _io->remove_input_port (f, this);
-       }
+       return _("Disconnect");
 }
 
-bool
-IOSelector::list_is_global (int dim) const
+string
+IOSelector::channel_noun () const
 {
-       return (dim == _other);
+       return _("port");
 }
 
-IOSelectorWindow::IOSelectorWindow (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool for_input, bool can_cancel)
-       : ArdourDialog ("I/O selector")
-       , _selector (session, io, !for_input)
-       , add_button (_("Add Port"))
-       , disconnect_button (_("Disconnect All"))
-       , ok_button (can_cancel ? _("OK"): _("Close"))
-       , cancel_button (_("Cancel"))
-       , rescan_button (_("Rescan"))
-
+IOSelectorWindow::IOSelectorWindow (ARDOUR::Session* session, boost::shared_ptr<ARDOUR::IO> io, bool /*can_cancel*/)
+       : _selector (this, session, io)
 {
-       /* XXX: what's this for? */
-       add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
-       
        set_name ("IOSelectorWindow2");
+       set_title (_("I/O selector"));
 
-       /* Disconnect All button */
-       disconnect_button.set_name ("IOSelectorButton");
-       disconnect_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::DISCONNECT, Gtk::ICON_SIZE_BUTTON)));
-       disconnect_button.signal_clicked().connect (sigc::mem_fun (_selector, &IOSelector::disassociate_all));
-       get_action_area()->pack_start (disconnect_button, false, false);
-
-       /* Add Port button */
-       add_button.set_name ("IOSelectorButton");
-       add_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::ADD, Gtk::ICON_SIZE_BUTTON)));
-       get_action_area()->pack_start (add_button, false, false);
-       add_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (_selector, &IOSelector::add_channel), boost::shared_ptr<Bundle> ()));
-
-       /* Rescan button */
-       rescan_button.set_name ("IOSelectorButton");
-       rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
-       rescan_button.signal_clicked().connect (sigc::mem_fun (*this, &IOSelectorWindow::rescan));
-       get_action_area()->pack_start (rescan_button, false, false);
-
-       io->PortCountChanged.connect (sigc::hide (mem_fun (*this, &IOSelectorWindow::ports_changed)));
-
-       /* Cancel button */
-       if (can_cancel) {
-               cancel_button.set_name ("IOSelectorButton");
-               cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
-               get_action_area()->pack_start (cancel_button, false, false);
-       } else {
-               cancel_button.hide();
-       }
-       cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
-
-       /* OK button */
-       ok_button.set_name ("IOSelectorButton");
-       if (!can_cancel) {
-               ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
-       }
-       ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
-       get_action_area()->pack_start (ok_button, false, false);
-
-       get_vbox()->set_spacing (8);
-
-       get_vbox()->pack_start (_selector, true, true);
+       add (_selector);
 
        set_position (Gtk::WIN_POS_MOUSE);
 
        io_name_changed (this);
-       ports_changed ();
 
        show_all ();
 
-       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), this));
-}
-
-void
-IOSelectorWindow::ports_changed ()
-{
-       /* XXX make this insensitive based on port connectivity, not
-          port counts.
-       */
-
-       add_button.set_sensitive (true);
+       signal_delete_event().connect (sigc::mem_fun (*this, &IOSelectorWindow::wm_delete));
 }
 
-void
-IOSelectorWindow::rescan ()
-{
-       _selector.setup_ports (_selector.other());
-}
-
-void
-IOSelectorWindow::cancel ()
-{
-       _selector.Finished (IOSelector::Cancelled);
-       hide ();
-}
-
-void
-IOSelectorWindow::accept ()
+bool
+IOSelectorWindow::wm_delete (GdkEventAny* /*event*/)
 {
        _selector.Finished (IOSelector::Accepted);
        hide ();
+       return true;
 }
 
+
 void
 IOSelectorWindow::on_map ()
 {
@@ -310,11 +245,19 @@ IOSelectorWindow::on_map ()
        Window::on_map ();
 }
 
+void
+IOSelectorWindow::on_show ()
+{
+       Gtk::Window::on_show ();
+       pair<uint32_t, uint32_t> const pm_max = _selector.max_size ();
+       resize_window_to_proportion_of_monitor (this, pm_max.first, pm_max.second);
+}
+
 void
 IOSelectorWindow::io_name_changed (void* src)
 {
-       ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelectorWindow::io_name_changed), src));
-       
+       ENSURE_GUI_THREAD (*this, &IOSelectorWindow::io_name_changed, src)
+
        string title;
 
        if (!_selector.find_inputs_for_io_outputs()) {
@@ -326,17 +269,89 @@ IOSelectorWindow::io_name_changed (void* src)
        set_title (title);
 }
 
-PortInsertUI::PortInsertUI (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
-       : input_selector (sess, pi->io(), true),
-         output_selector (sess, pi->io(), false)
+PortInsertUI::PortInsertUI (Gtk::Window* parent, ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi)
+        : _pi (pi)
+        , latency_button (_("Measure Latency"))
+        , input_selector (parent, sess, pi->input())
+        , output_selector (parent, sess, pi->output())
 {
+        latency_hbox.pack_start (latency_button, false, false);
+        latency_hbox.pack_start (latency_display, false, false);
+        latency_frame.add (latency_hbox);
+        
        output_selector.set_min_height_divisor (2);
        input_selector.set_min_height_divisor (2);
-       
+        
+        pack_start (latency_frame);
        pack_start (output_selector, true, true);
        pack_start (input_selector, true, true);
+
+        latency_button.signal_toggled().connect (mem_fun (*this, &PortInsertUI::latency_button_toggled));
 }
 
+bool
+PortInsertUI::check_latency_measurement ()
+{
+        MTDM* mtdm = _pi->mtdm ();
+
+        if (mtdm->resolve () < 0) {
+                latency_display.set_text (_("No signal detected"));
+                return true;
+        }
+
+        if (mtdm->err () > 0.3) {
+                mtdm->invert ();
+                mtdm->resolve ();
+        }
+
+        char buf[64];
+        nframes_t sample_rate = AudioEngine::instance()->frame_rate();
+
+        if (sample_rate == 0) {
+                latency_display.set_text (_("Disconnected from audio engine"));
+                _pi->stop_latency_detection ();
+                return false;
+        }
+
+        snprintf (buf, sizeof (buf), "%10.3lf frames %10.3lf ms", mtdm->del (), mtdm->del () * 1000.0f/sample_rate);
+
+        bool solid = true;
+
+        if (mtdm->err () > 0.2) {
+                strcat (buf, " ??");
+                solid = false;
+        }
+
+        if (mtdm->inv ()) {
+                strcat (buf, " (Inv)");
+                solid = false;
+        }
+
+        if (solid) {
+                _pi->set_measured_latency ((nframes_t) rint (mtdm->del()));
+                strcat (buf, " (set)");
+        }
+
+        latency_display.set_text (buf);
+        return true;
+}
+
+void
+PortInsertUI::latency_button_toggled ()
+{
+        if (latency_button.get_active ()) {
+
+                _pi->start_latency_detection ();
+                latency_display.set_text (_("Detecting ..."));
+                latency_timeout = Glib::signal_timeout().connect (mem_fun (*this, &PortInsertUI::check_latency_measurement), 250);
+
+        } else {
+                _pi->stop_latency_detection ();
+                latency_timeout.disconnect ();
+        }
+}
+
+
 void
 PortInsertUI::redisplay ()
 {
@@ -352,28 +367,24 @@ PortInsertUI::finished (IOSelector::Result r)
 }
 
 
-PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
+PortInsertWindow::PortInsertWindow (ARDOUR::Session* sess, boost::shared_ptr<ARDOUR::PortInsert> pi, bool can_cancel)
        : ArdourDialog ("port insert dialog"),
-         _portinsertui (sess, pi),
+         _portinsertui (this, sess, pi),
          ok_button (can_cancel ? _("OK"): _("Close")),
-         cancel_button (_("Cancel")),
-         rescan_button (_("Rescan"))
+         cancel_button (_("Cancel"))
 {
 
        set_name ("IOSelectorWindow");
-       string title = _("ardour: ");
+       string title = _("Port Insert ");
        title += pi->name();
        set_title (title);
-       
+
        ok_button.set_name ("IOSelectorButton");
        if (!can_cancel) {
                ok_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CLOSE, Gtk::ICON_SIZE_BUTTON)));
        }
        cancel_button.set_name ("IOSelectorButton");
-       rescan_button.set_name ("IOSelectorButton");
-       rescan_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::REFRESH, Gtk::ICON_SIZE_BUTTON)));
 
-       get_action_area()->pack_start (rescan_button, false, false);
        if (can_cancel) {
                cancel_button.set_image (*Gtk::manage (new Gtk::Image (Gtk::Stock::CANCEL, Gtk::ICON_SIZE_BUTTON)));
                get_action_area()->pack_start (cancel_button, false, false);
@@ -384,19 +395,26 @@ PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARD
 
        get_vbox()->pack_start (_portinsertui);
 
-       ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept));
-       cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel));
+       ok_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::accept));
+       cancel_button.signal_clicked().connect (sigc::mem_fun (*this, &PortInsertWindow::cancel));
 
-       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
+       signal_delete_event().connect (sigc::mem_fun (*this, &PortInsertWindow::wm_delete), false);
+
+       pi->DropReferences.connect (going_away_connection, invalidator (*this), boost::bind (&PortInsertWindow::plugin_going_away, this), gui_context());
+}
 
-       going_away_connection = pi->GoingAway.connect (mem_fun (*this, &PortInsertWindow::plugin_going_away));
+bool
+PortInsertWindow::wm_delete (GdkEventAny* /*event*/)
+{
+       accept ();
+       return true;
 }
 
 void
 PortInsertWindow::plugin_going_away ()
 {
-       ENSURE_GUI_THREAD (mem_fun (*this, &PortInsertWindow::plugin_going_away));
-       
+       ENSURE_GUI_THREAD (*this, &PortInsertWindow::plugin_going_away)
+
        going_away_connection.disconnect ();
        delete_when_idle (this);
 }