Fixes to bundle manager to make it vaguely usable.
[ardour.git] / gtk2_ardour / io_selector.cc
index b8598659d7c6ba7614093c33bfdfd9336175aaa2..76892dcd3ddc48cbd8f26a9d25391ee97e0d97b5 100644 (file)
 
 */
 
+#include <gtkmm/messagedialog.h>
 #include <glibmm/objectbase.h>
+
 #include <gtkmm2ext/doi.h>
+
 #include <ardour/port_insert.h>
 #include "ardour/session.h"
 #include "ardour/io.h"
 #include "ardour/audio_track.h"
 #include "ardour/midi_track.h"
 #include "ardour/data_type.h"
+#include "ardour/port.h"
+#include "ardour/bundle.h"
+
 #include "io_selector.h"
 #include "utils.h"
 #include "gui_thread.h"
 #include "i18n.h"
 
-IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool offer_inputs)
-       : PortMatrix (
-               session, io->default_type(), offer_inputs,
-               PortGroupList::Mask (PortGroupList::BUSS | PortGroupList::SYSTEM | PortGroupList::OTHER)
-               ),
-         _io (io)
+using namespace ARDOUR;
+using namespace Gtk;
+
+IOSelector::IOSelector (ARDOUR::Session& session, boost::shared_ptr<ARDOUR::IO> io, bool in)
+       : PortMatrix (session, io->default_type())
+       , _io (io)
+       , _find_inputs_for_io_outputs (in)
 {
-       /* Listen for ports changing on the IO */
-       if (!offer_inputs) {
-               _io->input_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
+       /* signal flow from 0 to 1 */
+       if (_find_inputs_for_io_outputs) {
+               _other = 1;
+               _ours = 0;
        } else {
-               _io->output_changed.connect (mem_fun(*this, &IOSelector::ports_changed));
+               _other = 0;
+               _ours = 1;
        }
-}
 
+       _port_group = boost::shared_ptr<PortGroup> (new PortGroup (""));
+       _ports[_ours].add_group (_port_group);
+       
+       setup_all_ports ();
+}
 
 void
-IOSelector::ports_changed (ARDOUR::IOChange change, void *src)
+IOSelector::setup_ports (int dim)
 {
-       ENSURE_GUI_THREAD (bind (mem_fun (*this, &IOSelector::ports_changed), change, src));
+       _ports[dim].suspend_signals ();
 
-       redisplay ();
-}
+       if (dim == _other) {
 
+               _ports[_other].gather (_session, _find_inputs_for_io_outputs);
+               
+       } else {
 
+               _port_group->clear ();
+               _port_group->add_bundle (
+                       _find_inputs_for_io_outputs ? _io->bundle_for_outputs() : _io->bundle_for_inputs()
+                       );
+       }
+
+       _ports[dim].resume_signals ();
+}
 
 void
-IOSelector::set_state (int r, std::string const & p, bool s)
+IOSelector::set_state (ARDOUR::BundleChannel c[2], bool s)
 {
-       if (s) {
-               if (!_offer_inputs) {
-                       _io->connect_input (_io->input(r), p, 0);
-               } else {
-                       _io->connect_output (_io->output(r), p, 0);
-               }
-       } else {
-               if (!_offer_inputs) {
-                       _io->disconnect_input (_io->input(r), p, 0);
-               } else {
-                       _io->disconnect_output (_io->output(r), p, 0);
-               }
-       }
+       ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
+       ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
+
+       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);
+                       if (!f) {
+                               return;
+                       }
+
+                       if (s) {
+                               if (!_find_inputs_for_io_outputs) {
+                                       _io->connect_input (f, *j, 0);
+                               } else {
+                                       _io->connect_output (f, *j, 0);
+                               }
+                       } else {
+                               if (!_find_inputs_for_io_outputs) {
+                                       _io->disconnect_input (f, *j, 0);
+                               } else {
+                                       _io->disconnect_output (f, *j, 0);
+                               }
+                       }
+               }
+       }
 }
 
-bool
-IOSelector::get_state (int r, std::string const & p) const
+PortMatrix::State
+IOSelector::get_state (ARDOUR::BundleChannel c[2]) const
 {
-       const char **connections = _offer_inputs ? _io->output(r)->get_connections() : _io->input(r)->get_connections();
-
-       int k = 0;
-       while (connections && connections[k]) {
-               if (std::string (connections[k]) == p) {
-                       return true;
+       ARDOUR::Bundle::PortList const & our_ports = c[_ours].bundle->channel_ports (c[_ours].channel);
+       ARDOUR::Bundle::PortList const & other_ports = c[_other].bundle->channel_ports (c[_other].channel);
+
+       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);
+
+                       /* 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 NOT_ASSOCIATED;
+                       }
                }
-
-               ++k;
        }
 
-       return false;
+       return ASSOCIATED;
 }
 
 uint32_t
-IOSelector::n_rows () const
+IOSelector::n_io_ports () const
 {
-       if (!_offer_inputs) {
+       if (!_find_inputs_for_io_outputs) {
                return _io->inputs().num_ports (_io->default_type());
        } else {
                return _io->outputs().num_ports (_io->default_type());
@@ -104,9 +148,9 @@ IOSelector::n_rows () const
 }
 
 uint32_t
-IOSelector::maximum_rows () const
+IOSelector::maximum_io_ports () const
 {
-       if (!_offer_inputs) {
+       if (!_find_inputs_for_io_outputs) {
                return _io->input_maximum ().get (_io->default_type());
        } else {
                return _io->output_maximum ().get (_io->default_type());
@@ -115,40 +159,31 @@ IOSelector::maximum_rows () const
 
 
 uint32_t
-IOSelector::minimum_rows () const
+IOSelector::minimum_io_ports () const
 {
-       if (!_offer_inputs) {
+       if (!_find_inputs_for_io_outputs) {
                return _io->input_minimum ().get (_io->default_type());
        } else {
                return _io->output_minimum ().get (_io->default_type());
        }
 }
 
-std::string
-IOSelector::row_name (int r) const
-{
-       if (!_offer_inputs) {
-               return _io->input(r)->name();
-       } else {
-               return _io->output(r)->name();
-       }
-               
-}
-
 void
-IOSelector::add_row ()
+IOSelector::add_channel (boost::shared_ptr<ARDOUR::Bundle> b)
 {
+       /* 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 (!_offer_inputs) {
+       if (!_find_inputs_for_io_outputs) {
 
                try {
                        _io->add_input_port ("", this);
                }
 
-               catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
-                       Gtk::MessageDialog msg (0,  _("There are no more JACK ports available."));
+               catch (AudioEngine::PortRegistrationFailure& err) {
+                       MessageDialog msg (_("There are no more JACK ports available."));
                        msg.run ();
                }
 
@@ -158,98 +193,117 @@ IOSelector::add_row ()
                        _io->add_output_port ("", this);
                }
 
-               catch (ARDOUR::AudioEngine::PortRegistrationFailure& err) {
-                       Gtk::MessageDialog msg (0, _("There are no more JACK ports available."));
+               catch (AudioEngine::PortRegistrationFailure& err) {
+                       MessageDialog msg (_("There are no more JACK ports available."));
                        msg.run ();
                }
        }
 }
 
-
 void
-IOSelector::remove_row (int r)
+IOSelector::remove_channel (ARDOUR::BundleChannel bc)
 {
-       // The IO selector only works for single typed IOs
-       const ARDOUR::DataType t = _io->default_type ();
+       Port* f = _session.engine().get_port_by_name (bc.bundle->channel_ports(bc.channel)[0]);
+       if (!f) {
+               return;
+       }
        
-       if (!_offer_inputs) {
-               _io->remove_input_port (_io->input (r), this);
+       if (_find_inputs_for_io_outputs) {
+               _io->remove_output_port (f, this);
        } else {
-               _io->remove_output_port (_io->output (r), this);
+               _io->remove_input_port (f, this);
        }
 }
 
-std::string
-IOSelector::row_descriptor () const
+bool
+IOSelector::list_is_global (int dim) const
 {
-       return _("port");
+       return (dim == _other);
 }
 
-
-
-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),
-         ok_button (can_cancel ? _("OK"): _("Close")),
-         cancel_button (_("Cancel")),
-         rescan_button (_("Rescan"))
+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"))
 
 {
+       /* XXX: what's this for? */
        add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
+       
        set_name ("IOSelectorWindow2");
 
-       string title;
-       if (for_input) {
-               title = string_compose(_("%1 input"), io->name());
-       } else {
-               title = string_compose(_("%1 output"), io->name());
-       }
-
-       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");
+       /* 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 */
+       if (_selector.maximum_io_ports() > _selector.n_io_ports()) {
+               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);
 
-       ok_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::accept));
-       cancel_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::cancel));
-       rescan_button.signal_clicked().connect (mem_fun(*this, &IOSelectorWindow::rescan));
+       get_vbox()->pack_start (_selector, true, true);
 
-       set_title (title);
        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), reinterpret_cast<Window *> (this)));
+       signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), this));
 }
 
-IOSelectorWindow::~IOSelectorWindow()
+void
+IOSelectorWindow::ports_changed ()
 {
-       
+       if (_selector.maximum_io_ports() > _selector.n_io_ports()) {
+               add_button.set_sensitive (true);
+       } else {
+               add_button.set_sensitive (false);
+       }
 }
 
 void
 IOSelectorWindow::rescan ()
 {
-       _selector.redisplay ();
+       _selector.setup_ports (_selector.other());
 }
 
 void
@@ -269,26 +323,39 @@ IOSelectorWindow::accept ()
 void
 IOSelectorWindow::on_map ()
 {
-       _selector.redisplay ();
+       _selector.setup_all_ports ();
        Window::on_map ();
 }
 
+void
+IOSelectorWindow::io_name_changed (void* src)
+{
+       ENSURE_GUI_THREAD(bind (mem_fun(*this, &IOSelectorWindow::io_name_changed), src));
+       
+       string title;
+
+       if (!_selector.find_inputs_for_io_outputs()) {
+               title = string_compose(_("%1 input"), _selector.io()->name());
+       } else {
+               title = string_compose(_("%1 output"), _selector.io()->name());
+       }
+
+       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)
 {
-       hbox.pack_start (output_selector, true, true);
-       hbox.pack_start (input_selector, true, true);
-
-       pack_start (hbox);
+       pack_start (output_selector, true, true);
+       pack_start (input_selector, true, true);
 }
 
 void
 PortInsertUI::redisplay ()
 {
-       input_selector.redisplay();
-       output_selector.redisplay();
+       input_selector.setup_ports (input_selector.other());
+       output_selector.setup_ports (output_selector.other());
 }
 
 void
@@ -333,7 +400,6 @@ PortInsertWindow::PortInsertWindow (ARDOUR::Session& sess, boost::shared_ptr<ARD
 
        ok_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::accept));
        cancel_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::cancel));
-       rescan_button.signal_clicked().connect (mem_fun (*this, &PortInsertWindow::rescan));
 
        signal_delete_event().connect (bind (sigc::ptr_fun (just_hide_it), reinterpret_cast<Window *> (this))); 
 
@@ -357,12 +423,6 @@ PortInsertWindow::on_map ()
 }
 
 
-void
-PortInsertWindow::rescan ()
-{
-       _portinsertui.redisplay ();
-}
-
 void
 PortInsertWindow::cancel ()
 {
@@ -376,3 +436,4 @@ PortInsertWindow::accept ()
        _portinsertui.finished (IOSelector::Accepted);
        hide ();
 }
+