+ _route ()->customize_plugin_insert (_pi, _n_plugins, out, sinks);
+}
+
+void
+PluginPinWidget::add_remove_inpin_clicked (bool add, ARDOUR::DataType dt)
+{
+ if (_session && _session->actively_recording ()) { return; }
+ ChanCount out = _out;
+ ChanCount sinks = _sinks;
+ assert (add || sinks.get (dt) > 0);
+ sinks.set (dt, sinks.get (dt) + (add ? 1 : -1));
+ _route ()->customize_plugin_insert (_pi, _n_plugins, out, sinks);
+}
+
+void
+PluginPinWidget::add_sidechain_port (DataType dt)
+{
+ if (_session && _session->actively_recording ()) { return; }
+ boost::shared_ptr<IO> io = _pi->sidechain_input ();
+ if (!io) {
+ return;
+ }
+
+ // this triggers a PluginIoReConfigure with process and processor write lock held
+ // from /this/ thread.
+ io->add_port ("", this, dt);
+}
+
+void
+PluginPinWidget::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
+{
+ if (_session && _session->actively_recording ()) { return; }
+ boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
+ boost::shared_ptr<IO> io = _pi->sidechain_input ();
+ if (!io || !p) {
+ return;
+ }
+ io->remove_port (p, this);
+}
+
+void
+PluginPinWidget::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
+{
+ if (_session && _session->actively_recording ()) { return; }
+ boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
+ boost::shared_ptr<IO> io = _pi->sidechain_input ();
+ if (!io || !p) {
+ return;
+ }
+ p->disconnect_all ();
+}
+
+void
+PluginPinWidget::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
+{
+ if (_session && _session->actively_recording ()) { return; }
+ boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
+ boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
+ boost::shared_ptr<IO> io = _pi->sidechain_input ();
+ if (!io || !p0 || !p1) {
+ return;
+ }
+ _ignore_updates = true;
+ p0->disconnect_all ();
+ _ignore_updates = false;
+ p0->connect (p1->name ());
+}
+
+void
+PluginPinWidget::add_send_from (boost::weak_ptr<ARDOUR::Port> wp, boost::weak_ptr<ARDOUR::Route> wr)
+{
+ if (_session && _session->actively_recording ()) { return; }
+ boost::shared_ptr<Port> p = wp.lock ();
+ boost::shared_ptr<Route> r = wr.lock ();
+ boost::shared_ptr<IO> io = _pi->sidechain_input ();
+ if (!p || !r || !io || !_session) {
+ return;
+ }
+
+ boost::shared_ptr<Pannable> sendpan (new Pannable (*_session));
+ boost::shared_ptr<Send> send (new Send (*_session, r->pannable (), r->mute_master ()));
+ const ChanCount& outs (r->amp ()->input_streams ());
+ try {
+ Glib::Threads::Mutex::Lock lm (AudioEngine::instance ()->process_lock ());
+ send->output()->ensure_io (outs, false, this);
+ } catch (AudioEngine::PortRegistrationFailure& err) {
+ error << string_compose (_("Cannot set up new send: %1"), err.what ()) << endmsg;
+ return;
+ }
+
+ std::string sendname = send->name ();
+ string::size_type last_letter = sendname.find_last_not_of ("0123456789");
+ if (last_letter != string::npos) {
+ send->output ()->set_pretty_name (string_compose (_("SC %1 (%2)"),
+ r->name (),
+ sendname.substr (last_letter + 1)));
+ }
+
+ _ignore_updates = true;
+ p->disconnect_all ();
+
+ DataType dt = p->type ();
+ PortSet& ps (send->output ()->ports ());
+ for (PortSet::iterator i = ps.begin (dt); i != ps.end (dt); ++i) {
+ p->connect (&(**i));
+ }
+
+ send->set_remove_on_disconnect (true);
+ r->add_processor (send, PreFader);
+ _ignore_updates = false;
+ queue_idle_update ();
+}
+
+bool
+PluginPinWidget::sc_input_release (GdkEventButton *ev)
+{
+ if (_session && _session->actively_recording ()) { return false; }
+ if (ev->button == 3) {
+ connect_sidechain ();
+ }
+ return false;
+}
+
+struct RouteCompareByName {
+ bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
+ return a->name ().compare (b->name ()) < 0;
+ }
+};
+
+bool
+PluginPinWidget::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
+{
+ using namespace Menu_Helpers;
+ if (!_session || _session->actively_recording ()) { return false; }
+ if (!_session->engine ().connected ()) { return false; }
+
+ if (ev->button == 1) {
+ MenuList& citems = input_menu.items ();
+ input_menu.set_name ("ArdourContextMenu");
+ citems.clear ();
+
+ boost::shared_ptr<Port> p = wp.lock ();
+ if (p && p->connected ()) {
+ citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinWidget::disconnect_port), wp)));
+ citems.push_back (SeparatorElem ());
+ }
+
+#if 0
+ // TODO add system inputs, too ?!
+ boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
+ for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
+ for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
+ }
+ //maybe_add_bundle_to_input_menu (*i, current);
+ }
+#endif
+
+ boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
+ RouteList copy = *routes;
+ copy.sort (RouteCompareByName ());
+ uint32_t added = 0;
+ for (ARDOUR::RouteList::const_iterator i = copy.begin (); i != copy.end (); ++i) {
+ added += maybe_add_route_to_input_menu (*i, p->type (), wp);
+ }
+
+ if (added > 0) {
+ citems.push_back (SeparatorElem ());
+ }
+ citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinWidget::connect_sidechain)));
+ input_menu.popup (1, ev->time);
+ }
+ return false;
+}
+
+uint32_t
+PluginPinWidget::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
+{
+ uint32_t added = 0;
+ using namespace Menu_Helpers;
+ if (r->output () == _route ()->output ()) {
+ return added;
+ }
+
+ if (_route ()->feeds_according_to_graph (r)) {
+ return added;
+ }
+
+ MenuList& citems = input_menu.items ();
+
+ /*check if there's already a send.. */
+ bool already_present = false;
+ uint32_t nth = 0;
+ boost::shared_ptr<Processor> proc;
+ /* Note: nth_send () takes a processor read-lock */
+ while ((proc = r->nth_send (nth))) {
+ boost::shared_ptr<IOProcessor> send = boost::dynamic_pointer_cast<IOProcessor> (proc);
+ if (!send || !send->output ()) {
+ ++nth;
+ continue;
+ }
+ if (send->output ()->connected_to (_pi->sidechain_input ())) {
+ // only if (send->remove_on_disconnect ()) ??
+ already_present = true;
+ break;
+ }
+ ++nth;
+ }
+ /* we're going to create the new send pre-fader, so check the route amp's data type. */
+ const ChanCount& rc (r->amp ()->input_streams ());
+ if (!already_present && rc.get (dt) > 0) {
+ citems.push_back (MenuElem (r->name (), sigc::bind (sigc::mem_fun (*this, &PluginPinWidget::add_send_from), wp, boost::weak_ptr<Route> (r))));
+ ++added;
+ }
+ return added;
+}
+
+void
+PluginPinWidget::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
+{
+ boost::shared_ptr<Port> p0 = w0.lock ();
+ boost::shared_ptr<Port> p1 = w1.lock ();
+
+ boost::shared_ptr<IO> io = _pi->sidechain_input ();
+ if (!io) { return; }
+
+ if (p0 && io->has_port (p0)) {
+ queue_idle_update ();
+ }
+ else if (p1 && io->has_port (p1)) {
+ queue_idle_update ();
+ }