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