replace Pin Config "reset" button with "custom" toggle
[ardour.git] / gtk2_ardour / plugin_pin_dialog.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2011 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <gtkmm/table.h>
21 #include <gtkmm/box.h>
22 #include <gtkmm/label.h>
23
24 #include "pbd/replace_all.h"
25
26 #include "gtkmm2ext/utils.h"
27 #include "gtkmm2ext/rgb_macros.h"
28
29 #include "ardour/audioengine.h"
30 #include "ardour/plugin.h"
31 #include "ardour/port.h"
32 #include "ardour/session.h"
33
34 #include "plugin_pin_dialog.h"
35 #include "gui_thread.h"
36 #include "tooltips.h"
37 #include "ui_config.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace std;
44 using namespace Gtk;
45 using namespace Gtkmm2ext;
46
47 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
48         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
49         , _set_config (_("Custom"), ArdourButton::led_default_elements)
50         , _rst_mapping (_("Reset"))
51         , _tgl_sidechain (_("Side Chain"))
52         , _add_plugin (_("+"))
53         , _del_plugin (_("-"))
54         , _add_output_audio (_("+"))
55         , _del_output_audio (_("-"))
56         , _add_output_midi (_("+"))
57         , _del_output_midi (_("-"))
58         , _add_sc_audio (_("A+"))
59         , _add_sc_midi (_("M+"))
60         , _pi (pi)
61         , _pin_box_size (10)
62         , _width (0)
63         , _height (0)
64         , _innerwidth (0)
65         , _margin_x (28)
66         , _margin_y (40)
67         , _min_width (300)
68         , _min_height (200)
69         , _n_inputs (0)
70         , _n_sidechains (0)
71         , _position_valid (false)
72         , _ignore_updates (false)
73         , _sidechain_selector (0)
74 {
75         assert (pi->owner ()); // Route
76
77         _pi->PluginIoReConfigure.connect (
78                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
79                         );
80
81         _pi->PluginMapChanged.connect (
82                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
83                         );
84
85         _pi->PluginConfigChanged.connect (
86                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
87                         );
88
89         _pin_box_size = 2 * ceil (max (8., 10. * UIConfiguration::instance ().get_ui_scale ()) * .5);
90         _margin_x = 2 * ceil (max (24., 28. * UIConfiguration::instance ().get_ui_scale ()) * .5);
91         _margin_y = 2 * ceil (max (36., 40. * UIConfiguration::instance ().get_ui_scale ()) * .5);
92
93         _tgl_sidechain.set_name ("pinrouting sidechain");
94
95         _pm_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
96         _add_plugin.set_tweaks (ArdourButton::Square);
97         _del_plugin.set_tweaks (ArdourButton::Square);
98         _pm_size_group->add_widget (_add_plugin);
99         _pm_size_group->add_widget (_del_plugin);
100         _pm_size_group->add_widget (_add_output_audio);
101         _pm_size_group->add_widget (_del_output_audio);
102         _pm_size_group->add_widget (_add_output_midi);
103         _pm_size_group->add_widget (_del_output_midi);
104
105         _sc_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
106         _sc_size_group->add_widget (_add_sc_audio);
107         _sc_size_group->add_widget (_add_sc_midi);
108
109         Label* l;
110         Gtk::Separator *sep;
111         int r = 0;
112         Table* tl = manage (new Table (9, 2));
113         tl->set_border_width (0);
114         tl->set_spacings (2);
115
116         Table* tr = manage (new Table (4, 3));
117         tr->set_border_width (0);
118         tr->set_spacings (2);
119
120         /* left side table */
121         l = manage (new Label (_("<b>Config</b>"), ALIGN_CENTER));
122         l->set_use_markup ();
123         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
124         ++r;
125         tl->attach (_set_config, 0, 2, r, r + 1, FILL, SHRINK);
126         ++r;
127
128         sep = manage (new HSeparator ());
129         tl->attach (*sep, 0, 2, r, r + 1, FILL|EXPAND, FILL|EXPAND, 0, 4);
130         ++r;
131
132         l = manage (new Label (_("Instances"), ALIGN_CENTER));
133         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
134         ++r;
135         tl->attach (_add_plugin, 0, 1, r, r + 1, SHRINK, SHRINK);
136         tl->attach (_del_plugin, 1, 2, r, r + 1, SHRINK, SHRINK);
137         ++r;
138
139         l = manage (new Label (_("Audio Out"), ALIGN_CENTER));
140         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
141         ++r;
142         tl->attach (_add_output_audio, 0, 1, r, r + 1, SHRINK, SHRINK);
143         tl->attach (_del_output_audio, 1, 2, r, r + 1, SHRINK, SHRINK);
144         ++r;
145
146         l = manage (new Label (_("Midi Out"), ALIGN_CENTER));
147         tl->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
148         ++r;
149         tl->attach (_add_output_midi, 0, 1, r, r + 1, SHRINK, SHRINK);
150         tl->attach (_del_output_midi, 1, 2, r, r + 1, SHRINK, SHRINK);
151         ++r;
152
153         /* right side table */
154         r = 0;
155         l = manage (new Label (_("<b>Connections</b>"), ALIGN_CENTER));
156         l->set_use_markup ();
157         tr->attach (*l, 0, 2, r, r + 1, FILL, SHRINK);
158         ++r;
159         tr->attach (_rst_mapping, 0, 2, r, r + 1, FILL, SHRINK);
160         ++r;
161
162         sep = manage (new HSeparator ());
163         tr->attach (*sep, 0, 2, r, r + 1, FILL|EXPAND, SHRINK, 0, 4);
164         ++r;
165
166         tr->attach (_tgl_sidechain, 0, 2, r, r + 1, FILL, SHRINK);
167         ++r;
168
169         _sidechain_tbl = manage (new Gtk::Table ());
170         _sidechain_tbl->set_spacings (2);
171         tr->attach (*_sidechain_tbl, 0, 2, r, r + 1, EXPAND|FILL, EXPAND|FILL, 0, 2);
172         ++r;
173
174         tr->attach (_add_sc_audio, 0, 1, r, r + 1, FILL, SHRINK);
175         tr->attach (_add_sc_midi, 1, 2, r, r + 1, FILL, SHRINK);
176         ++r;
177
178         HBox* hbox = manage (new HBox);
179         hbox->set_spacing (4);
180         hbox->pack_start (*tl, false, false);
181         hbox->pack_start (darea, true, true);
182         hbox->pack_start (*tr, false, false);
183
184         VBox* vbox = manage (new VBox);
185         vbox->pack_start (*hbox, true, true);
186         set_border_width (4);
187         add (*vbox);
188         vbox->show_all ();
189
190         plugin_reconfigured ();
191
192         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
193         darea.signal_size_request ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_request));
194         darea.signal_size_allocate ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_allocate));
195         darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
196         darea.signal_button_press_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_press_event));
197         darea.signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_release_event));
198         darea.signal_motion_notify_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_motion_notify_event));
199
200         _tgl_sidechain.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::toggle_sidechain));
201
202         _rst_mapping.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_mapping));
203         _set_config.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_configuration));
204         _add_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), true));
205         _del_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), false));
206
207         _add_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::AUDIO));
208         _del_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::AUDIO));
209         _add_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::MIDI));
210         _del_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::MIDI));
211         _add_sc_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::AUDIO));
212         _add_sc_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::MIDI));
213
214         AudioEngine::instance()->PortConnectedOrDisconnected.connect (
215                         _io_connection, invalidator (*this), boost::bind (&PluginPinDialog::port_connected_or_disconnected, this, _1, _3), gui_context ()
216                         );
217 }
218
219 PluginPinDialog::~PluginPinDialog ()
220 {
221         delete _sidechain_selector;
222 }
223
224 void
225 PluginPinDialog::plugin_reconfigured ()
226 {
227         ENSURE_GUI_THREAD (*this, &PluginPinDialog::plugin_reconfigured);
228         if (_ignore_updates) {
229                 return;
230         }
231         _n_plugins = _pi->get_count ();
232         _pi->configured_io (_in, _out);
233         _ins = _pi->internal_streams (); // with sidechain
234         _sinks = _pi->natural_input_streams ();
235         _sources = _pi->natural_output_streams ();
236
237         _tgl_sidechain.set_active (_pi->has_sidechain ());
238         _add_sc_audio.set_sensitive (_pi->has_sidechain ());
239         _add_sc_midi.set_sensitive (_pi->has_sidechain ());
240
241         if (_pi->custom_cfg ()) {
242                 _set_config.set_active (true);
243                 _add_plugin.set_sensitive (true);
244                 _add_output_audio.set_sensitive (true);
245                 _add_output_midi.set_sensitive (true);
246                 _del_plugin.set_sensitive (_n_plugins > 1);
247                 _del_output_audio.set_sensitive (_out.n_audio () > 0 && _out.n_total () > 1);
248                 _del_output_midi.set_sensitive (_out.n_midi () > 0 && _out.n_total () > 1);
249         } else {
250                 _set_config.set_active (false);
251                 _add_plugin.set_sensitive (false);
252                 _add_output_audio.set_sensitive (false);
253                 _add_output_midi.set_sensitive (false);
254                 _del_plugin.set_sensitive (false);
255                 _del_output_audio.set_sensitive (false);
256                 _del_output_midi.set_sensitive (false);
257         }
258
259         if (!_pi->has_sidechain () && _sidechain_selector) {
260                 delete _sidechain_selector;
261                 _sidechain_selector = 0;
262         }
263
264         refill_sidechain_table ();
265
266         /* update elements */
267
268         _elements.clear ();
269         _hover.reset ();
270         _actor.reset ();
271         _selection.reset ();
272
273         _n_inputs = _n_sidechains = 0;
274
275         for (uint32_t i = 0; i < _ins.n_total (); ++i) {
276                 DataType dt = i < _ins.n_midi () ? DataType::MIDI : DataType::AUDIO;
277                 uint32_t id = dt == DataType::MIDI ? i : i - _ins.n_midi ();
278                 bool sidechain = id >= _in.get (dt) ? true : false;
279                 if (sidechain) {
280                         ++_n_sidechains;
281                 } else {
282                         ++_n_inputs;
283                 }
284
285                 CtrlWidget cw (CtrlWidget (Input, dt, id, 0, sidechain));
286                 _elements.push_back (cw);
287         }
288
289         for (uint32_t i = 0; i < _out.n_total (); ++i) {
290                 int id = (i < _out.n_midi ()) ? i : i - _out.n_midi ();
291                 _elements.push_back (CtrlWidget (Output, (i < _out.n_midi () ? DataType::MIDI : DataType::AUDIO), id));
292         }
293
294         for (uint32_t n = 0; n < _n_plugins; ++n) {
295                 boost::shared_ptr<Plugin> plugin = _pi->plugin (n);
296                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
297                         DataType dt (_sinks.n_midi () ? DataType::MIDI : DataType::AUDIO);
298                         int idx = (i < _sinks.n_midi ()) ? i : i - _sinks.n_midi ();
299                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, true, idx));
300                         CtrlWidget cw (CtrlWidget (Sink, dt, i, n, iod.is_sidechain));
301                         _elements.push_back (cw);
302                 }
303                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
304                         _elements.push_back (CtrlWidget (Source, (i < _sources.n_midi () ? DataType::MIDI : DataType::AUDIO), i, n));
305                 }
306         }
307
308         /* calc minimum size */
309         const uint32_t max_ports = std::max (_ins.n_total (), _out.n_total ());
310         const uint32_t max_pins = std::max ((_sinks * _n_plugins).n_total (), (_sources * _n_plugins).n_total ());
311         uint32_t min_width = std::max (25 * max_ports, (uint32_t)(20 + _pin_box_size) * max_pins);
312         min_width = std::max (min_width, (uint32_t)ceilf (_margin_y * .45 * _n_plugins * 16. / 9.)); // 16 : 9 aspect
313         min_width = std::max ((uint32_t)300, min_width);
314
315         min_width = 50 + 10 * ceilf (min_width / 10.f);
316
317         uint32_t min_height = 3.5 * _margin_y + 2 * (_n_sidechains + 1) * _pin_box_size;
318         min_height = std::max ((uint32_t)200, min_height);
319         min_height = 4 * ceilf (min_height / 4.f);
320
321         if (min_width != _min_width || min_height != _min_height) {
322                 _min_width = min_width;
323                 _min_height = min_height;
324                 darea.queue_resize ();
325         }
326
327         _position_valid = false;
328         darea.queue_draw ();
329 }
330
331 void
332 PluginPinDialog::refill_sidechain_table ()
333 {
334         Table_Helpers::TableList& kids = _sidechain_tbl->children ();
335         for (Table_Helpers::TableList::iterator i = kids.begin (); i != kids.end ();) {
336                 i = kids.erase (i);
337         }
338         _sidechain_tbl->resize (1, 1);
339         if (!_pi->has_sidechain () && _sidechain_selector) {
340                 return;
341         }
342         boost::shared_ptr<IO> io = _pi->sidechain_input ();
343         if (!io) {
344                 return;
345         }
346
347         uint32_t r = 0;
348         PortSet& p (io->ports ());
349         bool can_remove = p.num_ports () > 1;
350         for (PortSet::iterator i = p.begin (DataType::MIDI); i != p.end (DataType::MIDI); ++i, ++r) {
351                 add_port_to_table (*i, r, can_remove);
352         }
353         for (PortSet::iterator i = p.begin (DataType::AUDIO); i != p.end (DataType::AUDIO); ++i, ++r) {
354                 add_port_to_table (*i, r, can_remove);
355         }
356         _sidechain_tbl->show_all ();
357 }
358
359 void
360 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
361 {
362         std::string lbl;
363         std::string tip = p->name ();
364         std::vector<std::string> cns;
365         p->get_connections (cns);
366
367         // TODO proper labels, see MixerStrip::update_io_button()
368         if (cns.size () == 0) {
369                 lbl = "-";
370         } else if (cns.size () > 1) {
371                 lbl = "...";
372                 tip += " &lt;- ";
373         } else {
374                 lbl = cns[0];
375                 tip += " &lt;- ";
376                 if (lbl.find ("system:") == 0) {
377                         lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
378                         if (lbl.empty ()) {
379                                 lbl = cns[0].substr (7);
380                         }
381                 }
382         }
383         for (std::vector<std::string>::const_iterator i = cns.begin(); i != cns.end(); ++i) {
384                 tip += *i;
385                 tip += " ";
386         }
387
388         ArdourButton *pb = manage (new ArdourButton (lbl));
389         pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
390         pb->set_layout_ellipsize_width (56 * PANGO_SCALE);
391         ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
392         _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
393
394         pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
395         pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
396
397         pb = manage (new ArdourButton ("-"));
398         _sidechain_tbl->attach (*pb, 1, 2, r, r +1 , FILL, SHRINK);
399         if (can_remove) {
400                 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
401         } else {
402                 pb->set_sensitive (false);
403         }
404 }
405
406 void
407 PluginPinDialog::update_element_pos ()
408 {
409         /* layout sizes */
410         _innerwidth = _width - 2. * _margin_x;
411
412         const double yc   = rint (_height * .5);
413         const double bxh2 = rint (_margin_y * .45); // TODO grow?
414         const double bxw  = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
415         const double bxw2 = rint (bxw * .5);
416         const double y_in = _margin_y;
417         const double y_out = _height - _margin_y;
418
419         _bxw2 = bxw2;
420         _bxh2 = bxh2;
421
422         const double dx = _pin_box_size * .5;
423
424         uint32_t sc_cnt = 0;
425         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
426                 switch (i->e->ct) {
427                         case Input:
428                                 if (i->e->sc) {
429                                         i->x = _innerwidth + _margin_x - dx;
430                                         i->y = y_in + (sc_cnt + .5) * _pin_box_size;
431                                         i->h = _pin_box_size;
432                                         i->w = 1.5 * _pin_box_size;
433                                         ++ sc_cnt;
434                                 } else {
435                                         uint32_t idx = i->e->id;
436                                         if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
437                                         i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
438                                         i->w = _pin_box_size;
439                                         i->h = 1.5 * _pin_box_size;
440                                         i->y = y_in - i->h;
441                                 }
442                                 break;
443                         case Output:
444                                 {
445                                         uint32_t idx = i->e->id;
446                                         if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
447                                         i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
448                                         i->y = y_out;
449                                         i->w = _pin_box_size;
450                                         i->h = 1.5 * _pin_box_size;
451                                 }
452                                 break;
453                         case Sink:
454                                 {
455                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
456                                         i->x = _margin_x + rint (x0 + (i->e->id + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
457                                         i->y = yc - bxh2 - dx;
458                                         i->w = _pin_box_size;
459                                         i->h = _pin_box_size;
460                                 }
461                                 break;
462                         case Source:
463                                 {
464                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
465                                         i->x = _margin_x + rint (x0 + (i->e->id + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
466                                         i->y = yc + bxh2 - dx;
467                                         i->w = _pin_box_size;
468                                         i->h = _pin_box_size;
469                                 }
470                                 break;
471                 }
472         }
473 }
474
475 void
476 PluginPinDialog::set_color (cairo_t* cr, bool midi)
477 {
478         // see also gtk2_ardour/processor_box.cc
479         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
480         static const uint32_t midi_port_color = 0x960909FF; //Red
481
482         if (midi) {
483                 cairo_set_source_rgb (cr,
484                                 UINT_RGBA_R_FLT (midi_port_color),
485                                 UINT_RGBA_G_FLT (midi_port_color),
486                                 UINT_RGBA_B_FLT (midi_port_color));
487         } else {
488                 cairo_set_source_rgb (cr,
489                                 UINT_RGBA_R_FLT (audio_port_color),
490                                 UINT_RGBA_G_FLT (audio_port_color),
491                                 UINT_RGBA_B_FLT (audio_port_color));
492         }
493 }
494
495 void
496 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
497 {
498
499         if (w.e->sc) {
500                 const double dy = w.h * .5;
501                 const double dx = w.w - dy;
502                 cairo_move_to (cr, w.x, w.y + dy);
503                 cairo_rel_line_to (cr,  dy, -dy);
504                 cairo_rel_line_to (cr,  dx,  0);
505                 cairo_rel_line_to (cr,   0,  w.h);
506                 cairo_rel_line_to (cr, -dx,  0);
507         } else {
508                 const double dir = (w.e->ct == Input) ? 1 : -1;
509                 const double dx = w.w * .5;
510                 const double dy = w.h - dx;
511
512                 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
513                 cairo_rel_line_to (cr,     -dx, -dx * dir);
514                 cairo_rel_line_to (cr,      0., -dy * dir);
515                 cairo_rel_line_to (cr, 2. * dx,        0.);
516                 cairo_rel_line_to (cr,      0.,  dy * dir);
517         }
518         cairo_close_path  (cr);
519
520         cairo_set_line_width (cr, 1.0);
521         cairo_set_source_rgb (cr, 0, 0, 0);
522         cairo_stroke_preserve (cr);
523
524         set_color (cr, w.e->dt == DataType::MIDI);
525
526         if (w.e->sc) {
527                 assert (w.e->ct == Input);
528                 cairo_fill_preserve (cr);
529                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
530         }
531
532         if (w.e == _selection || w.e == _actor) {
533                 cairo_fill_preserve (cr);
534                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
535         } else if (w.prelight) {
536                 cairo_fill_preserve (cr);
537                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
538         }
539         cairo_fill (cr);
540 }
541
542 void
543 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
544 {
545         const double dx = w.w * .5;
546         const double dy = w.h * .5;
547
548         cairo_move_to (cr, w.x + dx, w.y);
549         cairo_rel_line_to (cr, -dx,  dy);
550         cairo_rel_line_to (cr,  dx,  dy);
551         cairo_rel_line_to (cr,  dx, -dy);
552         cairo_close_path  (cr);
553
554         cairo_set_line_width (cr, 1.0);
555         cairo_set_source_rgb (cr, 0, 0, 0);
556         cairo_stroke_preserve (cr);
557
558         set_color (cr, w.e->dt == DataType::MIDI);
559
560         if (w.e->sc) {
561                 assert (w.e->ct == Sink);
562                 cairo_fill_preserve (cr);
563                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
564         }
565
566         if (w.e == _selection || w.e == _actor) {
567                 cairo_fill_preserve (cr);
568                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
569         } else if (w.prelight) {
570                 cairo_fill_preserve (cr);
571                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
572         }
573         cairo_fill (cr);
574 }
575
576 double
577 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
578 {
579         if (!midi) { i += n_midi; }
580         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
581 }
582
583 const PluginPinDialog::CtrlWidget&
584 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
585 {
586         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
587                 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
588                         return *i;
589                 }
590         }
591         fatal << string_compose (_("programming error: %1"),
592                         X_("Invalid Plugin I/O Port."))
593                 << endmsg;
594         abort (); /*NOTREACHED*/
595         static CtrlWidget screw_old_compilers (Input, DataType::NIL, 0);
596         return screw_old_compilers;
597 }
598
599 void
600 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
601 {
602         switch (w.e->ct) {
603                 case Input:
604                         if (w.e->sc) {
605                                 x = w.x;
606                                 y = w.y + w.h * .5;
607                         } else {
608                                 x = w.x + w.w * .5;
609                                 y = w.y + w.h;
610                         }
611                         break;
612                 case Output:
613                         x = w.x + w.w * .5;
614                         y = w.y;
615                         break;
616                 case Sink:
617                         x = w.x + w.w * .5;
618                         y = w.y;
619                         break;
620                 case Source:
621                         x = w.x + w.w * .5;
622                         y = w.y + w.h;
623                         break;
624         }
625 }
626
627 void
628 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
629 {
630         const double bz = 2 * _pin_box_size;
631         const double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
632
633         cairo_move_to (cr, x0, y0);
634         if (horiz) {
635                 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
636         } else {
637                 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
638         }
639         cairo_set_line_width (cr, 3.0);
640         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
641         cairo_set_source_rgb (cr, 1, 0, 0);
642         if (dashed) {
643                 const double dashes[] = { 5, 7 };
644                 cairo_set_dash (cr, dashes, 2, 0);
645         }
646         set_color (cr, midi);
647         cairo_stroke (cr);
648         if (dashed) {
649                 cairo_set_dash (cr, 0, 0, 0);
650         }
651 }
652
653 void
654 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
655 {
656         double x0, x1, y0, y1;
657         edge_coordinates (w0, x0, y0);
658         edge_coordinates (w1, x1, y1);
659         assert (w0.e->dt == w1.e->dt);
660         draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
661 }
662
663
664 bool
665 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
666 {
667         Gtk::Allocation a = darea.get_allocation ();
668         double const width = a.get_width ();
669         double const height = a.get_height ();
670
671         if (!_position_valid) {
672                 _width = width;
673                 _height = height;
674                 update_element_pos ();
675                 _position_valid = true;
676         }
677
678         cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
679         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
680         cairo_clip (cr);
681
682         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
683         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
684         cairo_rectangle (cr, 0, 0, width, height);
685         cairo_fill (cr);
686
687         const double yc = rint (_height * .5);
688
689         /* processor box */
690         rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
691         cairo_set_line_width (cr, 1.0);
692         cairo_set_source_rgb (cr, .1, .1, .3);
693         cairo_stroke_preserve (cr);
694         cairo_set_source_rgb (cr, .3, .3, .3);
695         cairo_fill (cr);
696
697         /* draw midi-bypass (behind) */
698         if (_pi->has_midi_bypass ()) {
699                 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
700                 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
701                 draw_connection (cr, cw0, cw1, true);
702         }
703
704         /* labels */
705         Glib::RefPtr<Pango::Layout> layout;
706         layout = Pango::Layout::create (get_pango_context ());
707
708         layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
709         layout->set_width (_height * PANGO_SCALE);
710
711         int text_width;
712         int text_height;
713
714         layout->set_text (_route ()->name ());
715         layout->get_pixel_size (text_width, text_height);
716         cairo_save (cr);
717         cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
718         cairo_rotate (cr, M_PI * -.5);
719         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
720         pango_cairo_show_layout (cr, layout->gobj ());
721         cairo_new_path (cr);
722         cairo_restore (cr);
723
724         layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
725         layout->set_text (_pi->name ());
726         layout->get_pixel_size (text_width, text_height);
727         cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
728         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
729         pango_cairo_show_layout (cr, layout->gobj ());
730
731         if (_pi->strict_io ()) {
732                 layout->set_text (_("Strict I/O"));
733                 layout->get_pixel_size (text_width, text_height);
734                 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
735                 const double sy0 = _height - 3 - text_height;
736
737                 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
738                 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
739                 cairo_fill (cr);
740
741                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
742                 cairo_move_to (cr, sx0, sy0);
743                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
744                 pango_cairo_show_layout (cr, layout->gobj ());
745         }
746
747
748         /* plugins & connection wires */
749         for (uint32_t i = 0; i < _n_plugins; ++i) {
750                 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
751
752                 /* plugin box */
753                 cairo_set_source_rgb (cr, .5, .5, .5);
754                 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
755                 cairo_fill (cr);
756
757                 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
758                 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
759
760                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
761                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
762                                 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
763                                 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
764                                 draw_connection (cr, cw0, cw1);
765                         }
766                 }
767
768                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
769                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
770                                 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
771                                 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
772                                 draw_connection (cr, cw0, cw1);
773                         }
774                 }
775         }
776
777         /* pins and ports */
778         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
779                 switch (i->e->ct) {
780                         case Input:
781                         case Output:
782                                 draw_io_pin (cr, *i);
783                                 break;
784                         case Sink:
785                         case Source:
786                                 draw_plugin_pin (cr, *i);
787                                 break;
788                 }
789         }
790
791         cairo_destroy (cr);
792         return true;
793 }
794
795 void
796 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
797 {
798         req->width = _min_width;
799         req->height = _min_height;
800 }
801
802 void
803 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
804 {
805         _position_valid = false;
806 }
807
808 bool
809 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
810 {
811         bool changed = false;
812         _hover.reset ();
813         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
814                 if (ev->x >= i->x && ev->x <= i->x + i->w
815                                 && ev->y >= i->y && ev->y <= i->y + i->h)
816                 {
817                         if (!i->prelight) changed = true;
818                         i->prelight = true;
819                         _hover = i->e;
820                 } else {
821                         if (i->prelight) changed = true;
822                         i->prelight = false;
823                 }
824         }
825         if (changed) {
826                 darea.queue_draw ();
827         }
828         return true;
829 }
830
831 bool
832 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
833 {
834         if (ev->type != GDK_BUTTON_PRESS) {
835                 return false;
836         }
837
838         switch (ev->button) {
839                 case 1:
840                         if (!_selection || (_selection && !_hover)) {
841                                 _selection = _hover;
842                                 _actor.reset ();
843                                 darea.queue_draw ();
844                         } else if (_selection && _hover && _selection != _hover) {
845                                 if (_selection->dt != _hover->dt) { _actor.reset (); }
846                                 else if (_selection->ct == Input && _hover->ct == Sink) { _actor = _hover; }
847                                 else if (_selection->ct == Sink && _hover->ct == Input) { _actor = _hover; }
848                                 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
849                                 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
850                                 if (!_actor) {
851                                 _selection = _hover;
852                                 }
853                                 darea.queue_draw ();
854                         }
855                         break;
856                 case 3:
857                         if (_selection != _hover) {
858                                 _selection = _hover;
859                                 darea.queue_draw ();
860                         }
861                         _actor.reset ();
862                         break;
863                 default:
864                         break;
865         }
866
867         return true;
868 }
869
870 bool
871 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
872 {
873         if (_hover == _actor && _actor && ev->button == 1) {
874                 assert (_selection);
875                 assert (_selection->dt == _actor->dt);
876                 if      (_selection->ct == Input && _actor->ct == Sink) {
877                         handle_input_action (_actor, _selection);
878                 }
879                 else if (_selection->ct == Sink && _actor->ct == Input) {
880                         handle_input_action (_selection, _actor);
881                 }
882                 else if (_selection->ct == Output && _actor->ct == Source) {
883                         handle_output_action (_actor, _selection);
884                 }
885                 else if (_selection->ct == Source && _actor->ct == Output) {
886                         handle_output_action (_selection, _actor);
887                 }
888                 _selection.reset ();
889         } else if (_hover == _selection && _selection && ev->button == 3) {
890                 handle_disconnect (_selection);
891         }
892         _actor.reset ();
893         darea.queue_draw ();
894         return true;
895 }
896
897 void
898 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
899 {
900         const int pc = s->ip;
901         bool valid;
902         ChanMapping in_map (_pi->input_map (pc));
903         uint32_t idx = in_map.get (s->dt, s->id, &valid);
904
905         if (valid && idx == i->id) {
906                 // disconnect
907                 in_map.unset (s->dt, s->id);
908                 _pi->set_input_map (pc, in_map);
909         }
910         else if (!valid) {
911                 // connect
912                 in_map.set (s->dt, s->id, i->id);
913                 _pi->set_input_map (pc, in_map);
914         }
915         else {
916                 // reconnect
917                 in_map.unset (s->dt, s->id);
918                 in_map.set (s->dt, s->id, i->id);
919                 _pi->set_input_map (pc, in_map);
920         }
921 }
922
923 void
924 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
925 {
926         const uint32_t pc = s->ip;
927         bool valid;
928         ChanMapping out_map (_pi->output_map (pc));
929         uint32_t idx = out_map.get (s->dt, s->id, &valid);
930
931         if (valid && idx == o->id) {
932                 // disconnect
933                 out_map.unset (s->dt, s->id);
934                 _pi->set_output_map (pc, out_map);
935         }
936         else {
937                 // disconnect source
938                 if (valid) {
939                         out_map.unset (s->dt, s->id);
940                 }
941                 // disconnect other outputs
942                 _ignore_updates = true;
943                 for (uint32_t n = 0; n < _n_plugins; ++n) {
944                         if (n == pc) {
945                                 continue;
946                         }
947                         ChanMapping n_out_map (_pi->output_map (n));
948                         idx = n_out_map.get_src (s->dt, o->id, &valid);
949                         if (valid) {
950                                 n_out_map.unset (s->dt, idx);
951                                 _pi->set_output_map (n, n_out_map);
952                         }
953                 }
954                 _ignore_updates = false;
955                 idx = out_map.get_src (s->dt, o->id, &valid);
956                 if (valid) {
957                         out_map.unset (s->dt, idx);
958                 }
959                 // connect
960                 out_map.set (s->dt, s->id, o->id);
961                 _pi->set_output_map (pc, out_map);
962         }
963 }
964
965 void
966 PluginPinDialog::handle_disconnect (const CtrlElem &e)
967 {
968         _ignore_updates = true;
969         bool changed = false;
970         bool valid;
971
972         switch (e->ct) {
973                 case Input:
974                         for (uint32_t n = 0; n < _n_plugins; ++n) {
975                                 ChanMapping map (_pi->input_map (n));
976                                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
977                                         uint32_t idx = map.get (e->dt, i, &valid);
978                                         if (valid && idx == e->id) {
979                                                 map.unset (e->dt, i);
980                                                 changed = true;
981                                         }
982                                 }
983                                 _pi->set_input_map (n, map);
984                         }
985                         break;
986                 case Sink:
987                         {
988                                 ChanMapping map (_pi->input_map (e->ip));
989                                 map.get (e->dt, e->id, &valid);
990                                 if (valid) {
991                                         map.unset (e->dt, e->id);
992                                         _pi->set_input_map (e->ip, map);
993                                         changed = true;
994                                 }
995                         }
996                         break;
997                 case Source:
998                         {
999                                 ChanMapping map (_pi->output_map (e->ip));
1000                                 map.get (e->dt, e->id, &valid);
1001                                 if (valid) {
1002                                         map.unset (e->dt, e->id);
1003                                         _pi->set_output_map (e->ip, map);
1004                                         changed = true;
1005                                 }
1006                         }
1007                         break;
1008                 case Output:
1009                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1010                                 ChanMapping map (_pi->output_map (n));
1011                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1012                                         uint32_t idx = map.get (e->dt, i, &valid);
1013                                         if (valid && idx == e->id) {
1014                                                 map.unset (e->dt, i);
1015                                                 changed = true;
1016                                         }
1017                                 }
1018                                 _pi->set_output_map (n, map);
1019                         }
1020                         break;
1021         }
1022         _ignore_updates = false;
1023         if (changed) {
1024                 plugin_reconfigured ();
1025         }
1026 }
1027
1028 void
1029 PluginPinDialog::toggle_sidechain ()
1030 {
1031         if (_session && _session->actively_recording()) { return; }
1032         _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1033 }
1034
1035 void
1036 PluginPinDialog::connect_sidechain ()
1037 {
1038         if (!_session) { return; }
1039
1040         if (_sidechain_selector == 0) {
1041                 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1042         }
1043
1044         if (_sidechain_selector->is_visible ()) {
1045                 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1046         } else {
1047                 _sidechain_selector->present ();
1048         }
1049 }
1050
1051 void
1052 PluginPinDialog::reset_configuration ()
1053 {
1054         if (_set_config.get_active ()) {
1055                 _route ()->reset_plugin_insert (_pi);
1056         } else {
1057                 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1058         }
1059 }
1060
1061 void
1062 PluginPinDialog::reset_mapping ()
1063 {
1064         _pi->reset_map ();
1065 }
1066
1067 void
1068 PluginPinDialog::add_remove_plugin_clicked (bool add)
1069 {
1070         if (_session && _session->actively_recording()) { return; }
1071         ChanCount out = _out;
1072         assert (add || _n_plugins > 0);
1073         _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1),  out);
1074 }
1075
1076 void
1077 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1078 {
1079         if (_session && _session->actively_recording()) { return; }
1080         ChanCount out = _out;
1081         assert (add || out.get (dt) > 0);
1082         out.set (dt, out.get (dt) + (add ? 1 : -1));
1083         _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1084 }
1085
1086 void
1087 PluginPinDialog::add_sidechain_port (DataType dt)
1088 {
1089         if (_session && _session->actively_recording()) { return; }
1090         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1091         if (!io) {
1092                 return;
1093         }
1094         io->add_port ("", this, dt);
1095 }
1096
1097 void
1098 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1099 {
1100         if (_session && _session->actively_recording()) { return; }
1101         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1102         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1103         if (!io || !p) {
1104                 return;
1105         }
1106         io->remove_port (p, this);
1107 }
1108
1109 void
1110 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1111 {
1112         if (_session && _session->actively_recording()) { return; }
1113         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1114         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1115         if (!io || !p) {
1116                 return;
1117         }
1118         p->disconnect_all ();
1119 }
1120
1121 void
1122 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1123 {
1124         if (_session && _session->actively_recording()) { return; }
1125         boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1126         boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1127         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1128         if (!io || !p0 || !p1) {
1129                 return;
1130         }
1131         p0->connect (p1->name ());
1132 }
1133
1134 bool
1135 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1136 {
1137         if (_session && _session->actively_recording()) { return false; }
1138         if (ev->button == 3) {
1139                 connect_sidechain ();
1140         }
1141         return false;
1142 }
1143
1144 struct RouteCompareByName {
1145         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1146                 return a->name().compare (b->name()) < 0;
1147         }
1148 };
1149
1150 bool
1151 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1152 {
1153         using namespace Menu_Helpers;
1154         if (!_session || _session->actively_recording()) { return false; }
1155         if (!_session->engine().connected()) { return false; }
1156
1157         if (ev->button == 1) {
1158                 MenuList& citems = input_menu.items();
1159                 input_menu.set_name ("ArdourContextMenu");
1160                 citems.clear();
1161
1162                 boost::shared_ptr<Port> p = wp.lock ();
1163                 if (p && p->connected ()) {
1164                         citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1165                         citems.push_back (SeparatorElem());
1166                 }
1167
1168 #if 0
1169                 // TODO add system inputs, too ?!
1170                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1171                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1172                         for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1173                         }
1174                         //maybe_add_bundle_to_input_menu (*i, current);
1175                 }
1176 #endif
1177
1178                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1179                 RouteList copy = *routes;
1180                 copy.sort (RouteCompareByName ());
1181                 uint32_t added = 0;
1182                 for (ARDOUR::RouteList::const_iterator i = copy.begin(); i != copy.end(); ++i) {
1183                         added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1184                 }
1185
1186                 if (added > 0) {
1187                         citems.push_back (SeparatorElem());
1188                 }
1189                 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1190                 input_menu.popup (1, ev->time);
1191         }
1192         return false;
1193 }
1194
1195 uint32_t
1196 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1197 {
1198         uint32_t added = 0;
1199         using namespace Menu_Helpers;
1200         if (r->output () == _route()->output()) {
1201                 return added;
1202         }
1203
1204         if (_route ()->feeds_according_to_graph (r)) {
1205                 return added;
1206         }
1207
1208         MenuList& citems = input_menu.items();
1209         const IOVector& iov (r->all_outputs());
1210
1211         for (IOVector::const_iterator o = iov.begin(); o != iov.end(); ++o) {
1212                 boost::shared_ptr<IO> op = o->lock();
1213                 if (!op) {
1214                         continue;
1215                 }
1216                 PortSet& p (op->ports ());
1217                 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1218                         std::string n = i->name ();
1219                         replace_all (n, "_", " ");
1220                         citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun(*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1221                         ++added;
1222                 }
1223         }
1224         return added;
1225 }
1226
1227 void
1228 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1229 {
1230         boost::shared_ptr<Port> p0 = w0.lock ();
1231         boost::shared_ptr<Port> p1 = w1.lock ();
1232
1233         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1234         if (!io) { return; }
1235
1236         if (p0 && io->has_port (p0)) {
1237                 plugin_reconfigured ();
1238         }
1239         else if (p1 && io->has_port (p1)) {
1240                 plugin_reconfigured ();
1241         }
1242 }