pretty send and sidechain names
[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 <boost/algorithm/string.hpp>
21
22 #include <gtkmm/table.h>
23 #include <gtkmm/frame.h>
24 #include <gtkmm/box.h>
25 #include <gtkmm/label.h>
26
27 #include "pbd/replace_all.h"
28
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31
32 #include "ardour/amp.h"
33 #include "ardour/audioengine.h"
34 #include "ardour/pannable.h"
35 #include "ardour/plugin.h"
36 #include "ardour/port.h"
37 #include "ardour/profile.h"
38 #include "ardour/send.h"
39 #include "ardour/session.h"
40
41 #include "plugin_pin_dialog.h"
42 #include "gui_thread.h"
43 #include "timers.h"
44 #include "tooltips.h"
45 #include "ui_config.h"
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace PBD;
51 using namespace std;
52 using namespace Gtk;
53 using namespace Gtkmm2ext;
54
55 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
56         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
57         , _set_config (_("Manual Config"), ArdourButton::led_default_elements)
58         , _tgl_sidechain (_("Side Chain"), ArdourButton::led_default_elements)
59         , _add_plugin (_("+"))
60         , _del_plugin (_("-"))
61         , _add_output_audio (_("+"))
62         , _del_output_audio (_("-"))
63         , _add_output_midi (_("+"))
64         , _del_output_midi (_("-"))
65         , _add_sc_audio (_("Audio"))
66         , _add_sc_midi (_("MIDI"))
67         , _pi (pi)
68         , _pin_box_size (10)
69         , _width (0)
70         , _height (0)
71         , _innerwidth (0)
72         , _margin_x (28)
73         , _margin_y (40)
74         , _min_width (300)
75         , _min_height (200)
76         , _n_inputs (0)
77         , _n_sidechains (0)
78         , _position_valid (false)
79         , _ignore_updates (false)
80         , _sidechain_selector (0)
81         , _dragging (false)
82 {
83         assert (pi->owner ()); // Route
84
85         _pi->PluginIoReConfigure.connect (
86                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::queue_idle_update, this), gui_context ()
87                         );
88
89         _pi->PluginMapChanged.connect (
90                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::queue_idle_update, this), gui_context ()
91                         );
92
93         _pi->PluginConfigChanged.connect (
94                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::queue_idle_update, this), gui_context ()
95                         );
96
97         _pin_box_size = 2 * ceil (max (8., 10. * UIConfiguration::instance ().get_ui_scale ()) * .5);
98         _margin_x = 2 * ceil (max (24., 28. * UIConfiguration::instance ().get_ui_scale ()) * .5);
99         _margin_y = 2 * ceil (max (36., 40. * UIConfiguration::instance ().get_ui_scale ()) * .5);
100
101         _tgl_sidechain.set_name ("pinrouting sidechain");
102         _set_config.set_name ("pinrouting custom");
103
104         Menu_Helpers::MenuList& citems = reset_menu.items ();
105         reset_menu.set_name ("ArdourContextMenu");
106         citems.clear ();
107         citems.push_back (Menu_Helpers::MenuElem (_("Reset"), sigc::mem_fun (*this, &PluginPinDialog::reset_mapping)));
108
109         _pm_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
110         _add_plugin.set_tweaks (ArdourButton::Square);
111         _del_plugin.set_tweaks (ArdourButton::Square);
112         _pm_size_group->add_widget (_add_plugin);
113         _pm_size_group->add_widget (_del_plugin);
114         _pm_size_group->add_widget (_add_output_audio);
115         _pm_size_group->add_widget (_del_output_audio);
116         _pm_size_group->add_widget (_add_output_midi);
117         _pm_size_group->add_widget (_del_output_midi);
118
119         Box* box;
120         Frame *f;
121
122         VBox* tl = manage (new VBox ());
123         tl->set_border_width (2);
124         tl->set_spacing (2);
125
126         VBox* tr = manage (new VBox ());
127         tr->set_border_width (2);
128         tr->set_spacing (2);
129
130         /* left side */
131         tl->pack_start (_set_config, false, false);
132
133         box = manage (new HBox ());
134         box->set_border_width (2);
135         box->pack_start (_add_plugin, true, false);
136         box->pack_start (_del_plugin, true, false);
137         f = manage (new Frame ());
138         f->set_label (_("Instances"));
139         f->add (*box);
140         tl->pack_start (*f, false, false);
141
142         box = manage (new HBox ());
143         box->set_border_width (2);
144         box->pack_start (_add_output_audio, true, false);
145         box->pack_start (_del_output_audio, true, false);
146         f = manage (new Frame ());
147         f->set_label (_("Audio Out"));
148         f->add (*box);
149         tl->pack_start (*f, false, false);
150
151         box = manage (new HBox ());
152         box->set_border_width (2);
153         box->pack_start (_add_output_midi, true, false);
154         box->pack_start (_del_output_midi, true, false);
155         f = manage (new Frame ());
156         f->set_label (_("MIDI Out"));
157         f->add (*box);
158         tl->pack_start (*f, false, false);
159
160         tl->pack_start (*manage (new Label ("")), true, true); // invisible separator
161         tl->pack_start (*manage (new HSeparator ()), false, false, 4);
162         _out_presets.disable_scrolling ();
163         ARDOUR_UI_UTILS::set_tooltip (_out_presets, _("Output Presets"));
164         tl->pack_start (_out_presets, false, false);
165
166         /* right side */
167         _sidechain_tbl = manage (new Gtk::Table ());
168         _sidechain_tbl->set_spacings (2);
169
170         tr->pack_start (_tgl_sidechain, false, false);
171         tr->pack_start (*_sidechain_tbl, true, true);
172
173         box = manage (new VBox ());
174         box->set_border_width (2);
175         box->set_spacing (2);
176         box->pack_start (_add_sc_audio, false, false);
177         box->pack_start (_add_sc_midi , false, false);
178         f = manage (new Frame ());
179         f->set_label (_("Add Sidechain Input"));
180         f->add (*box);
181
182         tr->pack_start (*f, false, false);
183
184         /* global packing */
185         HBox* hbox = manage (new HBox ());
186         hbox->set_spacing (4);
187         hbox->pack_start (*tl, false, false);
188         hbox->pack_start (darea, true, true);
189         hbox->pack_start (*tr, false, false);
190
191         VBox* vbox = manage (new VBox ());
192         vbox->pack_start (*hbox, true, true);
193         set_border_width (4);
194         add (*vbox);
195         vbox->show_all ();
196
197         plugin_reconfigured ();
198
199         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
200         darea.signal_size_request ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_request));
201         darea.signal_size_allocate ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_allocate));
202         darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
203         darea.signal_button_press_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_press_event));
204         darea.signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_release_event));
205         darea.signal_motion_notify_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_motion_notify_event));
206
207         _tgl_sidechain.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::toggle_sidechain));
208
209         _set_config.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_configuration));
210         _add_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), true));
211         _del_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), false));
212
213         _add_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::AUDIO));
214         _del_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::AUDIO));
215         _add_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::MIDI));
216         _del_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::MIDI));
217         _add_sc_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::AUDIO));
218         _add_sc_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::MIDI));
219
220         AudioEngine::instance ()->PortConnectedOrDisconnected.connect (
221                         _io_connection, invalidator (*this), boost::bind (&PluginPinDialog::port_connected_or_disconnected, this, _1, _3), gui_context ()
222                         );
223 }
224
225 PluginPinDialog::~PluginPinDialog ()
226 {
227         delete _sidechain_selector;
228 }
229
230 void
231 PluginPinDialog::set_session (ARDOUR::Session *s)
232 {
233         SessionHandlePtr::set_session (s);
234         plugin_reconfigured ();
235 }
236
237 void
238 PluginPinDialog::queue_idle_update ()
239 {
240         /* various actions here are directly executed, in the GUI thread,
241          * with the GUI-thread eventually taking the process and processor lock.
242          * "connect gui_context()" will call back immediately and this
243          * signal-handler will run with the locks held.
244          *
245          * This will lead to a crash with calling nth_send() which takes
246          * a processor read-lock while holding a write lock in the same thread.
247          *
248          * decouple update to GUI idle.
249          *
250          * BUT, do delete existing controls here (in case they're affected by
251          * the change and hit by the Timer before idle comes around)
252          */
253         for (list<Control*>::iterator i = _controls.begin (); i != _controls.end (); ++i) {
254                 _sidechain_tbl->remove ((*i)->box);
255                 delete *i;
256         }
257         _controls.clear ();
258         Glib::signal_idle().connect (sigc::mem_fun(*this, &PluginPinDialog::idle_update));
259 }
260
261 bool
262 PluginPinDialog::idle_update ()
263 {
264         plugin_reconfigured ();
265         return false;
266 }
267
268
269 void
270 PluginPinDialog::plugin_reconfigured ()
271 {
272         ENSURE_GUI_THREAD (*this, &PluginPinDialog::plugin_reconfigured);
273         if (_ignore_updates) {
274                 return;
275         }
276         _n_plugins = _pi->get_count ();
277         _pi->configured_io (_in, _out);
278         _ins = _pi->internal_streams (); // with sidechain
279         _sinks = _pi->natural_input_streams ();
280         _sources = _pi->natural_output_streams ();
281
282
283         _tgl_sidechain.set_active (_pi->has_sidechain ());
284         _add_sc_audio.set_sensitive (_pi->has_sidechain ());
285         _add_sc_midi.set_sensitive (_pi->has_sidechain ());
286
287         if (_pi->custom_cfg ()) {
288                 _set_config.set_active (true);
289                 _add_plugin.set_sensitive (true);
290                 _add_output_audio.set_sensitive (true);
291                 _add_output_midi.set_sensitive (true);
292                 _del_plugin.set_sensitive (_n_plugins > 1);
293                 _del_output_audio.set_sensitive (_out.n_audio () > 0 && _out.n_total () > 1);
294                 _del_output_midi.set_sensitive (_out.n_midi () > 0 && _out.n_total () > 1);
295                 _out_presets.set_sensitive (false);
296                 _out_presets.set_text (_("Manual"));
297         } else {
298                 _set_config.set_active (false);
299                 _add_plugin.set_sensitive (false);
300                 _add_output_audio.set_sensitive (false);
301                 _add_output_midi.set_sensitive (false);
302                 _del_plugin.set_sensitive (false);
303                 _del_output_audio.set_sensitive (false);
304                 _del_output_midi.set_sensitive (false);
305                 _out_presets.set_sensitive (true);
306                 refill_output_presets ();
307         }
308
309         if (!_pi->has_sidechain () && _sidechain_selector) {
310                 delete _sidechain_selector;
311                 _sidechain_selector = 0;
312         }
313
314         refill_sidechain_table ();
315
316         /* update elements */
317
318         _elements.clear ();
319         _hover.reset ();
320         _actor.reset ();
321         _selection.reset ();
322         _drag_dst.reset ();
323         _dragging = false;
324
325         _n_inputs = _n_sidechains = 0;
326
327         for (uint32_t i = 0; i < _ins.n_total (); ++i) {
328                 DataType dt = i < _ins.n_midi () ? DataType::MIDI : DataType::AUDIO;
329                 uint32_t id = dt == DataType::MIDI ? i : i - _ins.n_midi ();
330                 bool sidechain = id >= _in.get (dt) ? true : false;
331                 if (sidechain) {
332                         ++_n_sidechains;
333                 } else {
334                         ++_n_inputs;
335                 }
336
337                 CtrlWidget cw (CtrlWidget ("", Input, dt, id, 0, sidechain));
338                 _elements.push_back (cw);
339         }
340
341         for (uint32_t i = 0; i < _out.n_total (); ++i) {
342                 int id = (i < _out.n_midi ()) ? i : i - _out.n_midi ();
343                 _elements.push_back (CtrlWidget ("", Output, (i < _out.n_midi () ? DataType::MIDI : DataType::AUDIO), id));
344         }
345
346         for (uint32_t n = 0; n < _n_plugins; ++n) {
347                 boost::shared_ptr<Plugin> plugin = _pi->plugin (n);
348                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
349                         DataType dt (i < _sinks.n_midi () ? DataType::MIDI : DataType::AUDIO);
350                         int idx = (dt == DataType::MIDI) ? i : i - _sinks.n_midi ();
351                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, true, idx));
352                         CtrlWidget cw (CtrlWidget (iod.name, Sink, dt, idx, n, iod.is_sidechain));
353                         _elements.push_back (cw);
354                 }
355                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
356                         DataType dt (i < _sources.n_midi () ? DataType::MIDI : DataType::AUDIO);
357                         int idx = (dt == DataType::MIDI) ? i : i - _sources.n_midi ();
358                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, false, idx));
359                         _elements.push_back (CtrlWidget (iod.name, Source, dt, idx, n));
360                 }
361         }
362
363         /* calc minimum size */
364         const uint32_t max_ports = std::max (_ins.n_total (), _out.n_total ());
365         const uint32_t max_pins = std::max ((_sinks * _n_plugins).n_total (), (_sources * _n_plugins).n_total ());
366         uint32_t min_width = std::max (25 * max_ports, (uint32_t)(20 + _pin_box_size) * max_pins);
367         min_width = std::max (min_width, (uint32_t)ceilf (_margin_y * .45 * _n_plugins * 16. / 9.)); // 16 : 9 aspect
368         min_width = std::max ((uint32_t)300, min_width);
369
370         min_width = 50 + 10 * ceilf (min_width / 10.f);
371
372         uint32_t min_height = 3.5 * _margin_y + 2 * (_n_sidechains + 1) * _pin_box_size;
373         min_height = std::max ((uint32_t)200, min_height);
374         min_height = 4 * ceilf (min_height / 4.f);
375
376         if (min_width != _min_width || min_height != _min_height) {
377                 _min_width = min_width;
378                 _min_height = min_height;
379                 darea.queue_resize ();
380         }
381
382         _position_valid = false;
383         darea.queue_draw ();
384 }
385
386 void
387 PluginPinDialog::refill_sidechain_table ()
388 {
389         Table_Helpers::TableList& kids = _sidechain_tbl->children ();
390         for (Table_Helpers::TableList::iterator i = kids.begin (); i != kids.end ();) {
391                 i = kids.erase (i);
392         }
393         _sidechain_tbl->resize (1, 1);
394         for (list<Control*>::iterator i = _controls.begin (); i != _controls.end (); ++i) {
395                 delete *i;
396         }
397         _controls.clear ();
398         if (!_pi->has_sidechain () && _sidechain_selector) {
399                 return;
400         }
401         boost::shared_ptr<IO> io = _pi->sidechain_input ();
402         if (!io) {
403                 return;
404         }
405
406         uint32_t r = 0;
407         PortSet& p (io->ports ());
408         bool can_remove = p.num_ports () > 1;
409         for (PortSet::iterator i = p.begin (DataType::MIDI); i != p.end (DataType::MIDI); ++i) {
410                 r += add_port_to_table (*i, r, can_remove);
411         }
412         for (PortSet::iterator i = p.begin (DataType::AUDIO); i != p.end (DataType::AUDIO); ++i) {
413                 r += add_port_to_table (*i, r, can_remove);
414         }
415         _sidechain_tbl->show_all ();
416 }
417
418 void
419 PluginPinDialog::refill_output_presets ()
420 {
421         using namespace Menu_Helpers;
422         _out_presets.clear_items ();
423         PluginOutputConfiguration ppc (_pi->plugin (0)->possible_output ());
424
425         bool need_dropdown = true;
426
427         if (ppc.size () == 0) {
428                 need_dropdown = false;
429         }
430
431         if (!_pi->strict_io () && ppc.size () == 1) {
432                 need_dropdown = false;
433         }
434
435         if (_pi->strict_io () && ppc.size () == 1) {
436                 // "stereo" is currently preferred default for instruments, see PluginInsert
437                 if (ppc.find (2) != ppc.end ()) {
438                         need_dropdown = false;
439                 }
440         }
441
442         if (!_pi->needs_midi_input ()) {
443                 /* loose definition of instruments, maybe impose additional
444                  * || _pi->natural_input_streams ().n_audio () != 0
445                  * and special case variable output plugins
446                  * && !_pi->plugin (0)->info->reconfigurable_io()
447                  */
448                 need_dropdown = false;
449         }
450
451         if (!need_dropdown) {
452                 _out_presets.set_sensitive (false);
453                 _out_presets.set_text (_("Automatic"));
454                 return;
455         }
456
457         _out_presets.AddMenuElem (MenuElem (_("Automatic"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::select_output_preset), 0)));
458
459         const uint32_t n_audio = _pi->preset_out ().n_audio ();
460         if (n_audio == 0) {
461                 _out_presets.set_text (_("Automatic"));
462         }
463
464         if (ppc.find (0) != ppc.end ()) {
465                 // anyting goes
466                 ppc.clear ();
467                 if (n_audio != 0) {
468                         ppc.insert (n_audio);
469                 }
470                 ppc.insert (1);
471                 ppc.insert (2);
472                 ppc.insert (8);
473                 ppc.insert (16);
474                 ppc.insert (24);
475                 ppc.insert (32);
476         }
477
478         for (PluginOutputConfiguration::const_iterator i = ppc.begin () ; i != ppc.end (); ++i) {
479                 assert (*i > 0);
480                 std::string tmp;
481                 switch (*i) {
482                         case 1:
483                                 tmp = _("Mono");
484                                 break;
485                         case 2:
486                                 tmp = _("Stereo");
487                                 break;
488                         default:
489                                 tmp = string_compose (P_("%1 Channel", "%1 Channels", *i), *i);
490                                 break;
491                 }
492                 _out_presets.AddMenuElem (MenuElem (tmp, sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::select_output_preset), *i)));
493                 if (n_audio == *i) {
494                         _out_presets.set_text (tmp);
495                 }
496         }
497 }
498
499 std::string
500 PluginPinDialog::port_label (const std::string& portname, bool strip)
501 {
502         // compare to MixerStrip::update_io_button()
503         string lpn (PROGRAM_NAME);
504         boost::to_lower (lpn);
505         std::string program_port_prefix = lpn + ":"; // e.g. "ardour:"
506
507         std::string pn = AudioEngine::instance ()->get_pretty_name_by_name (portname);
508         if (!pn.empty ()) {
509                 string::size_type slash = pn.find ("/");
510                 if (slash != string::npos) {
511                         pn = pn.substr (0, slash);
512                 }
513                 return pn;
514         }
515         std::string label (portname);
516         if (label.find ("system:capture_") == 0) {
517                 if (label.empty ()) {
518                         label = portname.substr (15);
519                 }
520         } else if (label.find ("system:midi_capture_") == 0) {
521                 if (label.empty ()) {
522                         // "system:midi_capture_123" -> "123"
523                         label = "M " + portname.substr (20);
524                 }
525         } else if (label.find (program_port_prefix) == 0) {
526                 label = label.substr (program_port_prefix.size ());
527                 if (strip) {
528                         string::size_type slash = label.find ("/");
529                         if (slash != string::npos) {
530                                 label = label.substr (0, slash);
531                         }
532                 }
533         }
534         return label;
535 }
536
537 uint32_t
538 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
539 {
540         std::string lbl;
541         std::string tip = p->name ();
542         std::vector<std::string> cns;
543         bool single_source = true;
544         p->get_connections (cns);
545
546         for (std::vector<std::string>::const_iterator i = cns.begin (); i != cns.end (); ++i) {
547                 if (lbl.empty ()) {
548                         lbl = port_label (*i, true);
549                         continue;
550                 }
551                 if (port_label (*i, true) != lbl) {
552                         lbl = "...";
553                         single_source = false;
554                         break;
555                 }
556         }
557
558         if (cns.size () == 0) {
559                 lbl = "-";
560                 single_source = false;
561         } else if (cns.size () == 1) {
562                 tip += " &lt;- ";
563                 lbl = port_label (cns[0], false);
564         } else {
565                 tip += " &lt;- ";
566         }
567         replace_all (lbl, "_", " ");
568
569         for (std::vector<std::string>::const_iterator i = cns.begin (); i != cns.end (); ++i) {
570                 tip += *i;
571                 tip += " ";
572         }
573
574         ArdourButton *pb = manage (new ArdourButton (lbl));
575         pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
576         pb->set_layout_ellipsize_width (108 * PANGO_SCALE);
577         ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
578         _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
579
580         pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
581         pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
582
583         pb = manage (new ArdourButton ("-"));
584         _sidechain_tbl->attach (*pb, 1, 2, r, r + 1, FILL, SHRINK);
585         if (can_remove) {
586                 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
587         } else {
588                 pb->set_sensitive (false);
589         }
590
591         uint32_t rv = 1;
592
593         if (single_source && _session) {
594                 /* check if it's an Ardour Send feeding.. */
595                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
596                 for (ARDOUR::RouteList::const_iterator i = routes->begin (); i != routes->end (); ++i) {
597                         uint32_t nth = 0;
598                         boost::shared_ptr<Processor> proc;
599                         /* nth_send () takes a processor read-lock */
600                         while ((proc = (*i)->nth_send (nth))) {
601                                 boost::shared_ptr<IOProcessor> send = boost::dynamic_pointer_cast<IOProcessor> (proc);
602                                 if (!send || !send->output ()) {
603                                         ++nth;
604                                         continue;
605                                 }
606                                 if (!send->output ()->connected_to (p->name ())) {
607                                         ++nth;
608                                         continue;
609                                 }
610                                 /* if processor goes away, we're notified by the port disconnect,
611                                  * there should be no need to explicily connect to proc->DropReferences
612                                  */
613                                 set<Evoral::Parameter> p = proc->what_can_be_automated ();
614                                 for (set<Evoral::Parameter>::iterator i = p.begin (); i != p.end (); ++i) {
615                                         Control* c = new Control (proc->automation_control (*i), _("Send"));
616                                         _controls.push_back (c);
617                                         ++r; ++rv;
618                                         _sidechain_tbl->attach (c->box, 0, 2, r, r + 1, EXPAND|FILL, SHRINK);
619                                 }
620                                 break;
621                         }
622                 }
623         }
624         return rv;
625 }
626
627 void
628 PluginPinDialog::update_element_pos ()
629 {
630         /* layout sizes */
631         _innerwidth = _width - 2. * _margin_x;
632
633         const double yc   = rint (_height * .5);
634         const double bxh2 = rint (_margin_y * .45); // TODO grow?
635         const double bxw  = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
636         const double bxw2 = rint (bxw * .5);
637         const double y_in = _margin_y;
638         const double y_out = _height - _margin_y;
639
640         _bxw2 = bxw2;
641         _bxh2 = bxh2;
642
643         const double dx = _pin_box_size * .5;
644
645         uint32_t sc_cnt = 0;
646         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
647                 switch (i->e->ct) {
648                         case Input:
649                                 if (i->e->sc) {
650                                         i->x = _innerwidth + _margin_x - dx;
651                                         i->y = y_in + (sc_cnt + .5) * _pin_box_size;
652                                         i->h = _pin_box_size;
653                                         i->w = 1.5 * _pin_box_size;
654                                         ++ sc_cnt;
655                                 } else {
656                                         uint32_t idx = i->e->id;
657                                         if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
658                                         i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
659                                         i->w = _pin_box_size;
660                                         i->h = 1.5 * _pin_box_size;
661                                         i->y = y_in - i->h;
662                                 }
663                                 break;
664                         case Output:
665                                 {
666                                         uint32_t idx = i->e->id;
667                                         if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
668                                         i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
669                                         i->y = y_out;
670                                         i->w = _pin_box_size;
671                                         i->h = 1.5 * _pin_box_size;
672                                 }
673                                 break;
674                         case Sink:
675                                 {
676                                         uint32_t idx = i->e->id;
677                                         if (i->e->dt == DataType::AUDIO) { idx += _sinks.n_midi (); }
678                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
679                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
680                                         i->y = yc - bxh2 - dx;
681                                         i->w = _pin_box_size;
682                                         i->h = _pin_box_size;
683                                 }
684                                 break;
685                         case Source:
686                                 {
687                                         uint32_t idx = i->e->id;
688                                         if (i->e->dt == DataType::AUDIO) { idx += _sources.n_midi (); }
689                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
690                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
691                                         i->y = yc + bxh2 - dx;
692                                         i->w = _pin_box_size;
693                                         i->h = _pin_box_size;
694                                 }
695                                 break;
696                 }
697         }
698 }
699
700 void
701 PluginPinDialog::set_color (cairo_t* cr, bool midi)
702 {
703         // see also gtk2_ardour/processor_box.cc
704         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
705         static const uint32_t midi_port_color = 0x960909FF; //Red
706
707         if (midi) {
708                 cairo_set_source_rgb (cr,
709                                 UINT_RGBA_R_FLT (midi_port_color),
710                                 UINT_RGBA_G_FLT (midi_port_color),
711                                 UINT_RGBA_B_FLT (midi_port_color));
712         } else {
713                 cairo_set_source_rgb (cr,
714                                 UINT_RGBA_R_FLT (audio_port_color),
715                                 UINT_RGBA_G_FLT (audio_port_color),
716                                 UINT_RGBA_B_FLT (audio_port_color));
717         }
718 }
719
720 void
721 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
722 {
723         if (w.e->sc) {
724                 const double dy = w.h * .5;
725                 const double dx = w.w - dy;
726                 cairo_move_to (cr, w.x, w.y + dy);
727                 cairo_rel_line_to (cr,  dy, -dy);
728                 cairo_rel_line_to (cr,  dx,  0);
729                 cairo_rel_line_to (cr,   0,  w.h);
730                 cairo_rel_line_to (cr, -dx,  0);
731         } else {
732                 const double dir = (w.e->ct == Input) ? 1 : -1;
733                 const double dx = w.w * .5;
734                 const double dy = w.h - dx;
735
736                 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
737                 cairo_rel_line_to (cr,     -dx, -dx * dir);
738                 cairo_rel_line_to (cr,      0., -dy * dir);
739                 cairo_rel_line_to (cr, 2. * dx,        0.);
740                 cairo_rel_line_to (cr,      0.,  dy * dir);
741         }
742         cairo_close_path  (cr);
743
744         cairo_set_line_width (cr, 1.0);
745         cairo_set_source_rgb (cr, 0, 0, 0);
746         cairo_stroke_preserve (cr);
747
748         set_color (cr, w.e->dt == DataType::MIDI);
749
750         if (w.e->sc) {
751                 assert (w.e->ct == Input);
752                 cairo_fill_preserve (cr);
753                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
754         }
755
756         if (w.e == _selection || w.e == _actor) {
757                 cairo_fill_preserve (cr);
758                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
759         } else if (w.prelight) {
760                 cairo_fill_preserve (cr);
761                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
762         }
763         cairo_fill (cr);
764 }
765
766 void
767 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
768 {
769         const double dx = w.w * .5;
770         const double dy = w.h * .5;
771
772         cairo_move_to (cr, w.x + dx, w.y);
773         cairo_rel_line_to (cr, -dx,  dy);
774         cairo_rel_line_to (cr,  dx,  dy);
775         cairo_rel_line_to (cr,  dx, -dy);
776         cairo_close_path  (cr);
777
778         cairo_set_line_width (cr, 1.0);
779         cairo_set_source_rgb (cr, 0, 0, 0);
780         cairo_stroke_preserve (cr);
781
782         set_color (cr, w.e->dt == DataType::MIDI);
783
784         if (w.e->sc) {
785                 assert (w.e->ct == Sink);
786                 cairo_fill_preserve (cr);
787                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
788         }
789
790         if (w.e == _selection || w.e == _actor) {
791                 cairo_fill_preserve (cr);
792                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
793         } else if (w.prelight) {
794                 cairo_fill_preserve (cr);
795                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
796         }
797         cairo_fill (cr);
798
799         if ((w.prelight || w.e == _selection) && !w.name.empty ()) {
800                 int text_width;
801                 int text_height;
802                 Glib::RefPtr<Pango::Layout> layout;
803                 layout = Pango::Layout::create (get_pango_context ());
804                 layout->set_text (w.name);
805                 layout->get_pixel_size (text_width, text_height);
806
807                 rounded_rectangle (cr, w.x + dx - .5 * text_width - 2, w.y - text_height - 2,  text_width + 4, text_height + 2, 7);
808                 cairo_set_source_rgba (cr, 0, 0, 0, .5);
809                 cairo_fill (cr);
810
811                 cairo_move_to (cr, w.x + dx - .5 * text_width, w.y - text_height - 1);
812                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
813                 pango_cairo_show_layout (cr, layout->gobj ());
814         }
815 }
816
817 double
818 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
819 {
820         if (!midi) { i += n_midi; }
821         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
822 }
823
824 const PluginPinDialog::CtrlWidget&
825 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
826 {
827         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
828                 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
829                         return *i;
830                 }
831         }
832         assert (0);
833         fatal << string_compose (_("programming error: %1"),
834                         X_("Invalid Plugin I/O Port."))
835                 << endmsg;
836         abort (); /*NOTREACHED*/
837         static CtrlWidget screw_old_compilers ("", Input, DataType::NIL, 0);
838         return screw_old_compilers;
839 }
840
841 void
842 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
843 {
844         switch (w.e->ct) {
845                 case Input:
846                         if (w.e->sc) {
847                                 x = w.x;
848                                 y = w.y + w.h * .5;
849                         } else {
850                                 x = w.x + w.w * .5;
851                                 y = w.y + w.h;
852                         }
853                         break;
854                 case Output:
855                         x = w.x + w.w * .5;
856                         y = w.y;
857                         break;
858                 case Sink:
859                         x = w.x + w.w * .5;
860                         y = w.y;
861                         break;
862                 case Source:
863                         x = w.x + w.w * .5;
864                         y = w.y + w.h;
865                         break;
866         }
867 }
868
869 void
870 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
871 {
872         const double bz = 2 * _pin_box_size;
873         double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
874         if (x0 > _width * .5) { bc *= -1; }
875
876         cairo_move_to (cr, x0, y0);
877         if (horiz) {
878                 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
879         } else {
880                 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
881         }
882         cairo_set_line_width (cr, 3.0);
883         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
884         cairo_set_source_rgb (cr, 1, 0, 0);
885         if (dashed) {
886                 const double dashes[] = { 5, 7 };
887                 cairo_set_dash (cr, dashes, 2, 0);
888         }
889         set_color (cr, midi);
890         cairo_stroke (cr);
891         if (dashed) {
892                 cairo_set_dash (cr, 0, 0, 0);
893         }
894 }
895
896 void
897 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
898 {
899         double x0, x1, y0, y1;
900         edge_coordinates (w0, x0, y0);
901         edge_coordinates (w1, x1, y1);
902         assert (w0.e->dt == w1.e->dt);
903         draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
904 }
905
906
907 bool
908 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
909 {
910         Gtk::Allocation a = darea.get_allocation ();
911         double const width = a.get_width ();
912         double const height = a.get_height ();
913
914         if (!_position_valid) {
915                 _width = width;
916                 _height = height;
917                 update_element_pos ();
918                 _position_valid = true;
919         }
920
921         cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
922         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
923         cairo_clip (cr);
924
925         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
926         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
927         cairo_rectangle (cr, 0, 0, width, height);
928         cairo_fill (cr);
929
930         const double yc = rint (_height * .5);
931
932         /* processor box */
933         rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
934         cairo_set_line_width (cr, 1.0);
935         cairo_set_source_rgb (cr, .1, .1, .3);
936         cairo_stroke_preserve (cr);
937         cairo_set_source_rgb (cr, .3, .3, .3);
938         cairo_fill (cr);
939
940         /* labels */
941         Glib::RefPtr<Pango::Layout> layout;
942         layout = Pango::Layout::create (get_pango_context ());
943
944         layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
945         layout->set_width (_height * PANGO_SCALE);
946
947         int text_width;
948         int text_height;
949
950         layout->set_text (_route ()->name ());
951         layout->get_pixel_size (text_width, text_height);
952         cairo_save (cr);
953         cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
954         cairo_rotate (cr, M_PI * -.5);
955         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
956         pango_cairo_show_layout (cr, layout->gobj ());
957         cairo_new_path (cr);
958         cairo_restore (cr);
959
960         layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
961         layout->set_text (_pi->name ());
962         layout->get_pixel_size (text_width, text_height);
963         cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
964         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
965         pango_cairo_show_layout (cr, layout->gobj ());
966
967         if (_pi->signal_latency () > 0) {
968                 // TODO: this needs a better location also format to msec (and cache)
969                 layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
970                 layout->set_text (string_compose (_("Latency %1 spl"), _pi->signal_latency ()));
971                 layout->get_pixel_size (text_width, text_height);
972                 cairo_move_to (cr, _margin_x + _pin_box_size * .5, _margin_y + 2);
973                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
974                 pango_cairo_show_layout (cr, layout->gobj ());
975         }
976
977         if (_pi->strict_io () && !Profile->get_mixbus ()) {
978                 layout->set_text (_("Strict I/O"));
979                 layout->get_pixel_size (text_width, text_height);
980                 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
981                 const double sy0 = _height - 3 - text_height;
982
983                 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
984                 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
985                 cairo_fill (cr);
986
987                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
988                 cairo_move_to (cr, sx0, sy0);
989                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
990                 pango_cairo_show_layout (cr, layout->gobj ());
991         }
992
993         /* draw midi-bypass (behind) */
994         if (_pi->has_midi_bypass ()) {
995                 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
996                 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
997                 draw_connection (cr, cw0, cw1, true);
998         }
999
1000         /* thru connections */
1001         const ChanMapping::Mappings thru_map = _pi->thru_map ().mappings ();
1002         for (ChanMapping::Mappings::const_iterator t = thru_map.begin (); t != thru_map.end (); ++t) {
1003                 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1004                         const CtrlWidget& cw0 = get_io_ctrl (Output, t->first, c->first);
1005                         const CtrlWidget& cw1 = get_io_ctrl (Input, t->first, c->second);
1006                         if (!(_dragging && cw1.e == _selection && cw0.e == _drag_dst)) {
1007                                 draw_connection (cr, cw1, cw0, true);
1008                         }
1009                 }
1010         }
1011
1012
1013         /* plugins & connection wires */
1014         for (uint32_t i = 0; i < _n_plugins; ++i) {
1015                 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
1016
1017                 /* plugin box */
1018                 cairo_set_source_rgb (cr, .5, .5, .5);
1019                 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
1020                 cairo_fill (cr);
1021
1022                 layout->set_width (1.9 * _bxw2 * PANGO_SCALE);
1023                 layout->set_text (string_compose (_("Instance #%1"), i + 1));
1024                 layout->get_pixel_size (text_width, text_height);
1025                 cairo_move_to (cr, x0 - text_width * .5, yc - text_height * .5);
1026                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
1027                 pango_cairo_show_layout (cr, layout->gobj ());
1028
1029                 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
1030                 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
1031
1032                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
1033                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1034                                 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
1035                                 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
1036                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
1037                                         draw_connection (cr, cw0, cw1);
1038                                 }
1039                         }
1040                 }
1041
1042                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
1043                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1044                                 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
1045                                 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
1046                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
1047                                         draw_connection (cr, cw0, cw1);
1048                                 }
1049                         }
1050                 }
1051         }
1052
1053         /* pins and ports */
1054         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
1055                 switch (i->e->ct) {
1056                         case Input:
1057                         case Output:
1058                                 draw_io_pin (cr, *i);
1059                                 break;
1060                         case Sink:
1061                         case Source:
1062                                 draw_plugin_pin (cr, *i);
1063                                 break;
1064                 }
1065         }
1066
1067         /* DnD wire */
1068         CtrlWidget *drag_src = NULL;
1069         if (_dragging) {
1070                 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
1071                         if (i->e  == _selection ) {
1072                                 drag_src = &(*i);
1073                         }
1074                 }
1075         }
1076
1077         if (drag_src) {
1078                 double x0, y0;
1079                 if (_selection->ct == Input || _selection->ct == Source) {
1080                         edge_coordinates (*drag_src, x0, y0);
1081                         draw_connection (cr, x0, _drag_x, y0, _drag_y,
1082                                         _selection->dt == DataType::MIDI, _selection->sc);
1083                 } else {
1084                         edge_coordinates (*drag_src, x0, y0);
1085                         draw_connection (cr, _drag_x, x0, _drag_y, y0,
1086                                         _selection->dt == DataType::MIDI, _selection->sc);
1087                 }
1088         }
1089
1090         cairo_destroy (cr);
1091         return true;
1092 }
1093
1094 void
1095 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
1096 {
1097         req->width = _min_width;
1098         req->height = _min_height;
1099 }
1100
1101 void
1102 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
1103 {
1104         _position_valid = false;
1105 }
1106
1107 bool
1108 PluginPinDialog::drag_type_matches (const CtrlElem& e)
1109 {
1110         if (!_dragging || !_selection) {
1111                 return true;
1112         }
1113         if (_selection->dt != e->dt) {
1114                 return false;
1115         }
1116         if (_selection->ct == Input  && e->ct == Sink)   { return true; }
1117         if (_selection->ct == Sink   && e->ct == Input)  { return true; }
1118         if (_selection->ct == Output && e->ct == Source) { return true; }
1119         if (_selection->ct == Source && e->ct == Output) { return true; }
1120         if (_selection->ct == Input  && e->ct == Output) { return true; }
1121         if (_selection->ct == Output && e->ct == Input)  { return true; }
1122         return false;
1123 }
1124
1125 void
1126 PluginPinDialog::start_drag (const CtrlElem& e, double x, double y)
1127 {
1128         assert (_selection == e);
1129         _drag_dst.reset ();
1130         if (e->ct == Sink) {
1131                 bool valid;
1132                 const ChanMapping& map (_pi->input_map (e->ip));
1133                 uint32_t idx = map.get (e->dt, e->id, &valid);
1134                 if (valid) {
1135                         const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
1136                         _drag_dst = e;
1137                         _selection = cw.e;
1138                 }
1139         }
1140         else if (e->ct == Output) {
1141                 for (uint32_t i = 0; i < _n_plugins; ++i) {
1142                         bool valid;
1143                         const ChanMapping& map (_pi->output_map (i));
1144                         uint32_t idx = map.get_src (e->dt, e->id, &valid);
1145                         if (valid) {
1146                                 const CtrlWidget& cw = get_io_ctrl (Source, e->dt, idx, i);
1147                                 _drag_dst = e;
1148                                 _selection = cw.e;
1149                                 break;
1150                         }
1151                 }
1152                 if (!_drag_dst) {
1153                         bool valid;
1154                         const ChanMapping& map (_pi->thru_map ());
1155                         uint32_t idx = map.get (e->dt, e->id, &valid);
1156                         if (valid) {
1157                                 const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
1158                                 _drag_dst = e;
1159                                 _selection = cw.e;
1160                         }
1161                 }
1162         }
1163         _dragging = true;
1164         _drag_x = x;
1165         _drag_y = y;
1166 }
1167
1168 bool
1169 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
1170 {
1171         bool changed = false;
1172         _hover.reset ();
1173         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
1174                 if (ev->x >= i->x && ev->x <= i->x + i->w
1175                                 && ev->y >= i->y && ev->y <= i->y + i->h
1176                                 && drag_type_matches (i->e))
1177                 {
1178                         if (!i->prelight) changed = true;
1179                         i->prelight = true;
1180                         _hover = i->e;
1181                 } else {
1182                         if (i->prelight) changed = true;
1183                         i->prelight = false;
1184                 }
1185         }
1186         if (_dragging) {
1187                 _drag_x = ev->x;
1188                 _drag_y = ev->y;
1189         }
1190         if (changed || _dragging) {
1191                 darea.queue_draw ();
1192         }
1193         return true;
1194 }
1195
1196 bool
1197 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
1198 {
1199         if (ev->type != GDK_BUTTON_PRESS) {
1200                 return false;
1201         }
1202
1203         switch (ev->button) {
1204                 case 1:
1205                         _drag_dst.reset ();
1206                         if (!_selection || (_selection && !_hover)) {
1207                                 _selection = _hover;
1208                                 _actor.reset ();
1209                                 if (_selection) {
1210                                         start_drag (_selection, ev->x, ev->y);
1211                                 } else {
1212                                         darea.queue_draw ();
1213                                 }
1214                         } else if (_selection && _hover && _selection != _hover) {
1215                                 if (_selection->dt != _hover->dt) { _actor.reset (); }
1216                                 else if (_selection->ct == Input  && _hover->ct == Sink)   { _actor = _hover; }
1217                                 else if (_selection->ct == Sink   && _hover->ct == Input)  { _actor = _hover; }
1218                                 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
1219                                 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
1220                                 else if (_selection->ct == Input  && _hover->ct == Output) { _actor = _hover; }
1221                                 else if (_selection->ct == Output && _hover->ct == Input)  { _actor = _hover; }
1222                                 if (!_actor) {
1223                                         _selection = _hover;
1224                                         start_drag (_selection, ev->x, ev->y);
1225                                 } else {
1226                                         darea.queue_draw ();
1227                                 }
1228                         } else if (_hover) {
1229                                 _selection = _hover;
1230                                 _actor.reset ();
1231                                 start_drag (_selection, ev->x, ev->y);
1232                         }
1233                         break;
1234                 case 3:
1235                         _drag_dst.reset ();
1236                         if (_selection != _hover) {
1237                                 _selection = _hover;
1238                                 darea.queue_draw ();
1239                         }
1240                         _actor.reset ();
1241                         break;
1242                 default:
1243                         break;
1244         }
1245
1246         return true;
1247 }
1248
1249 bool
1250 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
1251 {
1252         if (_dragging && _selection && _drag_dst && _drag_dst == _hover) {
1253                 // select click. (or re-connect same)
1254                 assert (_selection != _hover);
1255                 _actor.reset ();
1256                 _dragging = false;
1257                 _drag_dst.reset ();
1258                 _selection =_hover;
1259                 darea.queue_draw ();
1260                 return true;
1261         }
1262
1263         if (_dragging && _hover && _hover != _selection) {
1264                 _actor = _hover;
1265         }
1266
1267         if (_hover == _actor && _actor && ev->button == 1) {
1268                 assert (_selection);
1269                 assert (_selection->dt == _actor->dt);
1270                 if (_drag_dst) {
1271                         assert (_dragging && _selection != _drag_dst);
1272                         handle_disconnect (_drag_dst, true);
1273                 }
1274                 if      (_selection->ct == Input && _actor->ct == Sink) {
1275                         handle_input_action (_actor, _selection);
1276                 }
1277                 else if (_selection->ct == Sink && _actor->ct == Input) {
1278                         handle_input_action (_selection, _actor);
1279                 }
1280                 else if (_selection->ct == Output && _actor->ct == Source) {
1281                         handle_output_action (_actor, _selection);
1282                 }
1283                 else if (_selection->ct == Source && _actor->ct == Output) {
1284                         handle_output_action (_selection, _actor);
1285                 }
1286                 else if (_selection->ct == Input && _actor->ct == Output) {
1287                         handle_thru_action (_actor, _selection);
1288                 }
1289                 else if (_selection->ct == Output && _actor->ct == Input) {
1290                         handle_thru_action (_selection, _actor);
1291                 }
1292                 _selection.reset ();
1293         } else if (_hover == _selection && _selection && ev->button == 3) {
1294                 handle_disconnect (_selection);
1295         } else if (!_hover && ev->button == 3) {
1296                 reset_menu.popup (1, ev->time);
1297         }
1298
1299         if (_dragging && _hover != _selection) {
1300                 _selection.reset ();
1301         }
1302         _actor.reset ();
1303         _dragging = false;
1304         _drag_dst.reset ();
1305         darea.queue_draw ();
1306         return true;
1307 }
1308
1309 void
1310 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
1311 {
1312         const int pc = s->ip;
1313         bool valid;
1314         ChanMapping in_map (_pi->input_map (pc));
1315         uint32_t idx = in_map.get (s->dt, s->id, &valid);
1316
1317         if (valid && idx == i->id) {
1318                 // disconnect
1319                 if (!_dragging) {
1320                         in_map.unset (s->dt, s->id);
1321                         _pi->set_input_map (pc, in_map);
1322                 } else {
1323                         plugin_reconfigured ();
1324                 }
1325         }
1326         else if (!valid) {
1327                 // connect
1328                 in_map.set (s->dt, s->id, i->id);
1329                 _pi->set_input_map (pc, in_map);
1330         }
1331         else {
1332                 // reconnect
1333                 in_map.unset (s->dt, s->id);
1334                 in_map.set (s->dt, s->id, i->id);
1335                 _pi->set_input_map (pc, in_map);
1336         }
1337 }
1338
1339 void
1340 PluginPinDialog::disconnect_other_outputs (uint32_t skip_pc, DataType dt, uint32_t id)
1341 {
1342         _ignore_updates = true;
1343         for (uint32_t n = 0; n < _n_plugins; ++n) {
1344                 if (n == skip_pc) {
1345                         continue;
1346                 }
1347                 bool valid;
1348                 ChanMapping n_out_map (_pi->output_map (n));
1349                 uint32_t idx = n_out_map.get_src (dt, id, &valid);
1350                 if (valid) {
1351                         n_out_map.unset (dt, idx);
1352                         _pi->set_output_map (n, n_out_map);
1353                 }
1354         }
1355         _ignore_updates = false;
1356 }
1357
1358 void
1359 PluginPinDialog::disconnect_other_thru (DataType dt, uint32_t id)
1360 {
1361         _ignore_updates = true;
1362         bool valid;
1363         ChanMapping n_thru_map (_pi->thru_map ());
1364         n_thru_map.get (dt, id, &valid);
1365         if (valid) {
1366                 n_thru_map.unset (dt, id);
1367                 _pi->set_thru_map (n_thru_map);
1368         }
1369         _ignore_updates = false;
1370 }
1371
1372 void
1373 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
1374 {
1375         const uint32_t pc = s->ip;
1376         bool valid;
1377         ChanMapping out_map (_pi->output_map (pc));
1378         uint32_t idx = out_map.get (s->dt, s->id, &valid);
1379
1380         if (valid && idx == o->id) {
1381                 // disconnect
1382                 if (!_dragging) {
1383                         out_map.unset (s->dt, s->id);
1384                         _pi->set_output_map (pc, out_map);
1385                 } else {
1386                         plugin_reconfigured ();
1387                 }
1388         }
1389         else {
1390                 // disconnect source
1391                 disconnect_other_outputs (pc, s->dt, o->id);
1392                 disconnect_other_thru (s->dt, o->id);
1393                 out_map = _pi->output_map (pc); // re-read map
1394                 if (valid) {
1395                         out_map.unset (s->dt, s->id);
1396                 }
1397                 idx = out_map.get_src (s->dt, o->id, &valid);
1398                 if (valid) {
1399                         out_map.unset (s->dt, idx);
1400                 }
1401                 // connect
1402                 out_map.set (s->dt, s->id, o->id);
1403                 _pi->set_output_map (pc, out_map);
1404         }
1405 }
1406
1407 void
1408 PluginPinDialog::handle_thru_action (const CtrlElem &o, const CtrlElem &i)
1409 {
1410         bool valid;
1411         ChanMapping thru_map (_pi->thru_map ());
1412         uint32_t idx = thru_map.get (o->dt, o->id, &valid);
1413
1414         if (valid && idx == i->id) {
1415                 if (!_dragging) {
1416                         thru_map.unset (o->dt, o->id);
1417                 }
1418         } else {
1419                 // disconnect other outputs first
1420                 disconnect_other_outputs (UINT32_MAX, o->dt, o->id);
1421                 disconnect_other_thru (o->dt, o->id);
1422                 thru_map = _pi->thru_map (); // re-read map
1423
1424                 thru_map.set (o->dt, o->id, i->id);
1425         }
1426         _pi->set_thru_map (thru_map);
1427 }
1428
1429 bool
1430 PluginPinDialog::handle_disconnect (const CtrlElem &e, bool no_signal)
1431 {
1432         _ignore_updates = true;
1433         bool changed = false;
1434         bool valid;
1435
1436         switch (e->ct) {
1437                 case Input:
1438                         {
1439                                 ChanMapping n_thru_map (_pi->thru_map ());
1440                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1441                                         uint32_t idx = n_thru_map.get (e->dt, i, &valid);
1442                                         if (valid && idx == e->id) {
1443                                                 n_thru_map.unset (e->dt, i);
1444                                                 changed = true;
1445                                         }
1446                                 }
1447                                 if (changed) {
1448                                         _pi->set_thru_map (n_thru_map);
1449                                 }
1450                         }
1451                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1452                                 ChanMapping map (_pi->input_map (n));
1453                                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
1454                                         uint32_t idx = map.get (e->dt, i, &valid);
1455                                         if (valid && idx == e->id) {
1456                                                 map.unset (e->dt, i);
1457                                                 changed = true;
1458                                         }
1459                                 }
1460                                 _pi->set_input_map (n, map);
1461                         }
1462                         break;
1463                 case Sink:
1464                         {
1465                                 ChanMapping map (_pi->input_map (e->ip));
1466                                 map.get (e->dt, e->id, &valid);
1467                                 if (valid) {
1468                                         map.unset (e->dt, e->id);
1469                                         _pi->set_input_map (e->ip, map);
1470                                         changed = true;
1471                                 }
1472                         }
1473                         break;
1474                 case Source:
1475                         {
1476                                 ChanMapping map (_pi->output_map (e->ip));
1477                                 map.get (e->dt, e->id, &valid);
1478                                 if (valid) {
1479                                         map.unset (e->dt, e->id);
1480                                         _pi->set_output_map (e->ip, map);
1481                                         changed = true;
1482                                 }
1483                         }
1484                         break;
1485                 case Output:
1486                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1487                                 ChanMapping map (_pi->output_map (n));
1488                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1489                                         uint32_t idx = map.get (e->dt, i, &valid);
1490                                         if (valid && idx == e->id) {
1491                                                 map.unset (e->dt, i);
1492                                                 changed = true;
1493                                         }
1494                                 }
1495                                 if (changed) {
1496                                         _pi->set_output_map (n, map);
1497                                 }
1498                         }
1499                         {
1500                                 ChanMapping n_thru_map (_pi->thru_map ());
1501                                 n_thru_map.get (e->dt, e->id, &valid);
1502                                 if (valid) {
1503                                         n_thru_map.unset (e->dt, e->id);
1504                                         changed = true;
1505                                         _pi->set_thru_map (n_thru_map);
1506                                 }
1507                         }
1508                         break;
1509         }
1510         _ignore_updates = false;
1511         if (changed && !no_signal) {
1512                 plugin_reconfigured ();
1513         }
1514         return changed;
1515 }
1516
1517 void
1518 PluginPinDialog::toggle_sidechain ()
1519 {
1520         if (_session && _session->actively_recording ()) { return; }
1521         _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1522 }
1523
1524 void
1525 PluginPinDialog::connect_sidechain ()
1526 {
1527         if (!_session) { return; }
1528
1529         if (_sidechain_selector == 0) {
1530                 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1531         }
1532
1533         if (_sidechain_selector->is_visible ()) {
1534                 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1535         } else {
1536                 _sidechain_selector->present ();
1537         }
1538 }
1539
1540 void
1541 PluginPinDialog::reset_configuration ()
1542 {
1543         if (_set_config.get_active ()) {
1544                 _route ()->reset_plugin_insert (_pi);
1545         } else {
1546                 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1547         }
1548 }
1549
1550 void
1551 PluginPinDialog::reset_mapping ()
1552 {
1553         _pi->reset_map ();
1554 }
1555
1556 void
1557 PluginPinDialog::select_output_preset (uint32_t n_audio)
1558 {
1559         if (_session && _session->actively_recording ()) { return; }
1560         ChanCount out (DataType::AUDIO, n_audio);
1561         _route ()->plugin_preset_output (_pi, out);
1562 }
1563
1564 void
1565 PluginPinDialog::add_remove_plugin_clicked (bool add)
1566 {
1567         if (_session && _session->actively_recording ()) { return; }
1568         ChanCount out = _out;
1569         assert (add || _n_plugins > 0);
1570         _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1),  out);
1571 }
1572
1573 void
1574 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1575 {
1576         if (_session && _session->actively_recording ()) { return; }
1577         ChanCount out = _out;
1578         assert (add || out.get (dt) > 0);
1579         out.set (dt, out.get (dt) + (add ? 1 : -1));
1580         _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1581 }
1582
1583 void
1584 PluginPinDialog::add_sidechain_port (DataType dt)
1585 {
1586         if (_session && _session->actively_recording ()) { return; }
1587         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1588         if (!io) {
1589                 return;
1590         }
1591
1592         // this triggers a PluginIoReConfigure with process and processor write lock held
1593         // from /this/ thread.
1594         io->add_port ("", this, dt);
1595 }
1596
1597 void
1598 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1599 {
1600         if (_session && _session->actively_recording ()) { return; }
1601         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1602         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1603         if (!io || !p) {
1604                 return;
1605         }
1606         io->remove_port (p, this);
1607 }
1608
1609 void
1610 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1611 {
1612         if (_session && _session->actively_recording ()) { return; }
1613         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1614         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1615         if (!io || !p) {
1616                 return;
1617         }
1618         p->disconnect_all ();
1619 }
1620
1621 void
1622 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1623 {
1624         if (_session && _session->actively_recording ()) { return; }
1625         boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1626         boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1627         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1628         if (!io || !p0 || !p1) {
1629                 return;
1630         }
1631         _ignore_updates = true;
1632         p0->disconnect_all ();
1633         _ignore_updates = false;
1634         p0->connect (p1->name ());
1635 }
1636
1637 void
1638 PluginPinDialog::add_send_from (boost::weak_ptr<ARDOUR::Port> wp, boost::weak_ptr<ARDOUR::Route> wr)
1639 {
1640         if (_session && _session->actively_recording ()) { return; }
1641         boost::shared_ptr<Port> p = wp.lock ();
1642         boost::shared_ptr<Route> r = wr.lock ();
1643         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1644         if (!p || !r || !io || !_session) {
1645                 return;
1646         }
1647
1648         boost::shared_ptr<Pannable> sendpan (new Pannable (*_session));
1649         boost::shared_ptr<Send> send (new Send (*_session, sendpan, r->mute_master ()));
1650         const ChanCount& outs (r->amp ()->input_streams ());
1651         try {
1652                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance ()->process_lock ());
1653                 send->output()->ensure_io (outs, false, this);
1654         } catch (AudioEngine::PortRegistrationFailure& err) {
1655                 error << string_compose (_("Cannot set up new send: %1"), err.what ()) << endmsg;
1656                 return;
1657         }
1658
1659         std::string sendname = send->name ();
1660         string::size_type last_letter = sendname.find_last_not_of ("0123456789");
1661         if (last_letter != string::npos) {
1662                 send->output ()->set_pretty_name (string_compose (_("SC %1 (%2)"),
1663                                 r->name (),
1664                                 sendname.substr (last_letter + 1)));
1665         }
1666
1667         _ignore_updates = true;
1668         p->disconnect_all ();
1669
1670         DataType dt = p->type ();
1671         PortSet& ps (send->output ()->ports ());
1672         for (PortSet::iterator i = ps.begin (dt); i != ps.end (dt); ++i) {
1673                 p->connect (&(**i));
1674         }
1675
1676         send->set_remove_on_disconnect (true);
1677         r->add_processor (send, PreFader);
1678         _ignore_updates = false;
1679         queue_idle_update ();
1680 }
1681
1682 bool
1683 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1684 {
1685         if (_session && _session->actively_recording ()) { return false; }
1686         if (ev->button == 3) {
1687                 connect_sidechain ();
1688         }
1689         return false;
1690 }
1691
1692 struct RouteCompareByName {
1693         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1694                 return a->name ().compare (b->name ()) < 0;
1695         }
1696 };
1697
1698 bool
1699 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1700 {
1701         using namespace Menu_Helpers;
1702         if (!_session || _session->actively_recording ()) { return false; }
1703         if (!_session->engine ().connected ()) { return false; }
1704
1705         if (ev->button == 1) {
1706                 MenuList& citems = input_menu.items ();
1707                 input_menu.set_name ("ArdourContextMenu");
1708                 citems.clear ();
1709
1710                 boost::shared_ptr<Port> p = wp.lock ();
1711                 if (p && p->connected ()) {
1712                         citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1713                         citems.push_back (SeparatorElem ());
1714                 }
1715
1716 #if 0
1717                 // TODO add system inputs, too ?!
1718                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1719                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1720                         for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1721                         }
1722                         //maybe_add_bundle_to_input_menu (*i, current);
1723                 }
1724 #endif
1725
1726                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1727                 RouteList copy = *routes;
1728                 copy.sort (RouteCompareByName ());
1729                 uint32_t added = 0;
1730                 for (ARDOUR::RouteList::const_iterator i = copy.begin (); i != copy.end (); ++i) {
1731                         added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1732                 }
1733
1734                 if (added > 0) {
1735                         citems.push_back (SeparatorElem ());
1736                 }
1737                 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1738                 input_menu.popup (1, ev->time);
1739         }
1740         return false;
1741 }
1742
1743 uint32_t
1744 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1745 {
1746         uint32_t added = 0;
1747         using namespace Menu_Helpers;
1748         if (r->output () == _route ()->output ()) {
1749                 return added;
1750         }
1751
1752         if (_route ()->feeds_according_to_graph (r)) {
1753                 return added;
1754         }
1755
1756         MenuList& citems = input_menu.items ();
1757
1758         /*check if there's already a send.. */
1759         bool already_present = false;
1760         uint32_t nth = 0;
1761         boost::shared_ptr<Processor> proc;
1762         /* Note: nth_send () takes a processor read-lock */
1763         while ((proc = r->nth_send (nth))) {
1764                 boost::shared_ptr<IOProcessor> send = boost::dynamic_pointer_cast<IOProcessor> (proc);
1765                 if (!send || !send->output ()) {
1766                         ++nth;
1767                         continue;
1768                 }
1769                 if (send->output ()->connected_to (_pi->sidechain_input ())) {
1770                         // only if (send->remove_on_disconnect ()) ??
1771                         already_present = true;
1772                         ++nth;
1773                         continue;
1774                 }
1775 #if 1 // add existing sends that are not connected
1776                 PortSet& p (send->output ()->ports ());
1777                 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1778                         std::string n = i->name ();
1779                         replace_all (n, "_", " ");
1780                         citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1781                         ++added;
1782                 }
1783 #endif
1784                 ++nth;
1785         }
1786         /* we're going to create the new send pre-fader, so check the route amp's data type.  */
1787         const ChanCount& rc (r->amp ()->input_streams ());
1788         if (!already_present && rc.get (dt) > 0) {
1789                 citems.push_back (MenuElem (r->name (), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_send_from), wp, boost::weak_ptr<Route> (r))));
1790                 ++added;
1791         }
1792         return added;
1793 }
1794
1795 void
1796 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1797 {
1798         boost::shared_ptr<Port> p0 = w0.lock ();
1799         boost::shared_ptr<Port> p1 = w1.lock ();
1800
1801         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1802         if (!io) { return; }
1803
1804         if (p0 && io->has_port (p0)) {
1805                 queue_idle_update ();
1806         }
1807         else if (p1 && io->has_port (p1)) {
1808                 queue_idle_update ();
1809         }
1810 }
1811
1812 /* lifted from ProcessorEntry::Control */
1813 PluginPinDialog::Control::Control (boost::shared_ptr<AutomationControl> c, string const & n)
1814         : _control (c)
1815         , _adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain ()), 0, 1, 0.01, 0.1)
1816         , _slider (&_adjustment, boost::shared_ptr<PBD::Controllable> (), 0, max (13.f, rintf (13.f * UIConfiguration::instance ().get_ui_scale ())))
1817         , _slider_persistant_tooltip (&_slider)
1818         , _ignore_ui_adjustment (false)
1819         , _name (n)
1820 {
1821         _slider.set_controllable (c);
1822         box.set_padding (0, 0, 4, 4);
1823
1824         _slider.set_name ("ProcessorControlSlider");
1825         _slider.set_text (_name);
1826
1827         box.add (_slider);
1828         _slider.show ();
1829
1830         const ARDOUR::ParameterDescriptor& desc = c->desc ();
1831         double const lo = c->internal_to_interface (desc.lower);
1832         double const up = c->internal_to_interface (desc.upper);
1833         double const normal = c->internal_to_interface (desc.normal);
1834         double smallstep = desc.smallstep;
1835         double largestep = desc.largestep;
1836
1837         if (smallstep == 0.0) {
1838                 smallstep = up / 1000.;
1839         } else {
1840                 smallstep = c->internal_to_interface (desc.lower + smallstep);
1841         }
1842
1843         if (largestep == 0.0) {
1844                 largestep = up / 40.;
1845         } else {
1846                 largestep = c->internal_to_interface (desc.lower + largestep);
1847         }
1848
1849         _adjustment.set_lower (lo);
1850         _adjustment.set_upper (up);
1851         _adjustment.set_step_increment (smallstep);
1852         _adjustment.set_page_increment (largestep);
1853         _slider.set_default_value (normal);
1854
1855         _adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &Control::slider_adjusted));
1856         // dup. currently timers are used :(
1857         //c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
1858
1859         // yuck, do we really need to do this?
1860         // according to c404374 this is only needed for send automation
1861         timer_connection = Timers::rapid_connect (sigc::mem_fun (*this, &Control::control_changed));
1862
1863         control_changed ();
1864         set_tooltip ();
1865
1866         /* We're providing our own PersistentTooltip */
1867         set_no_tooltip_whatsoever (_slider);
1868 }
1869
1870 PluginPinDialog::Control::~Control ()
1871 {
1872         timer_connection.disconnect ();
1873 }
1874
1875 void
1876 PluginPinDialog::Control::set_tooltip ()
1877 {
1878         boost::shared_ptr<AutomationControl> c = _control.lock ();
1879         if (!c) {
1880                 return;
1881         }
1882         char tmp[256];
1883         snprintf (tmp, sizeof (tmp), "%s: %.2f", _name.c_str (), c->internal_to_user (c->get_value ()));
1884
1885         string sm = Gtkmm2ext::markup_escape_text (tmp);
1886         _slider_persistant_tooltip.set_tip (sm);
1887 }
1888
1889 void
1890 PluginPinDialog::Control::slider_adjusted ()
1891 {
1892         if (_ignore_ui_adjustment) {
1893                 return;
1894         }
1895         boost::shared_ptr<AutomationControl> c = _control.lock ();
1896         if (!c) {
1897                 return;
1898         }
1899         c->set_value ( c->interface_to_internal (_adjustment.get_value ()) , Controllable::NoGroup);
1900         set_tooltip ();
1901 }
1902
1903
1904 void
1905 PluginPinDialog::Control::control_changed ()
1906 {
1907         boost::shared_ptr<AutomationControl> c = _control.lock ();
1908         if (!c) {
1909                 return;
1910         }
1911
1912         _ignore_ui_adjustment = true;
1913
1914         // as long as rapid timers are used, only update the tooltip
1915         // if the value has changed.
1916         const double nval = c->internal_to_interface (c->get_value ());
1917         if (_adjustment.get_value () != nval) {
1918                 _adjustment.set_value (nval);
1919                 set_tooltip ();
1920         }
1921
1922         _ignore_ui_adjustment = false;
1923 }