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