use temporary sends for sidechain inputs
[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 label (portname);
508         if (label.find ("system:capture_") == 0) {
509                 label = AudioEngine::instance ()->get_pretty_name_by_name (label);
510                 if (label.empty ()) {
511                         label = portname.substr (15);
512                 }
513         } else if (label.find ("system:midi_capture_") == 0) {
514                 label = AudioEngine::instance ()->get_pretty_name_by_name (label);
515                 if (label.empty ()) {
516                         // "system:midi_capture_123" -> "123"
517                         label = "M " + portname.substr (20);
518                 }
519         } else if (label.find (program_port_prefix) == 0) {
520                 label = label.substr (program_port_prefix.size ());
521                 if (strip) {
522                         string::size_type slash = label.find ("/");
523                         if (slash != string::npos) {
524                                 label = label.substr (0, slash);
525                         }
526                 }
527         }
528         return label;
529 }
530
531 uint32_t
532 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
533 {
534         std::string lbl;
535         std::string tip = p->name ();
536         std::vector<std::string> cns;
537         bool single_source = true;
538         p->get_connections (cns);
539
540         for (std::vector<std::string>::const_iterator i = cns.begin (); i != cns.end (); ++i) {
541                 if (lbl.empty ()) {
542                         lbl = port_label (*i, true);
543                         continue;
544                 }
545                 if (port_label (*i, true) != lbl) {
546                         lbl = "...";
547                         single_source = false;
548                         break;
549                 }
550         }
551
552         if (cns.size () == 0) {
553                 lbl = "-";
554                 single_source = false;
555         } else if (cns.size () == 1) {
556                 tip += " &lt;- ";
557                 lbl = port_label (cns[0], false);
558         } else {
559                 tip += " &lt;- ";
560         }
561         replace_all (lbl, "_", " ");
562
563         for (std::vector<std::string>::const_iterator i = cns.begin (); i != cns.end (); ++i) {
564                 tip += *i;
565                 tip += " ";
566         }
567
568         ArdourButton *pb = manage (new ArdourButton (lbl));
569         pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
570         pb->set_layout_ellipsize_width (108 * PANGO_SCALE);
571         ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
572         _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
573
574         pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
575         pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
576
577         pb = manage (new ArdourButton ("-"));
578         _sidechain_tbl->attach (*pb, 1, 2, r, r + 1, FILL, SHRINK);
579         if (can_remove) {
580                 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
581         } else {
582                 pb->set_sensitive (false);
583         }
584
585         uint32_t rv = 1;
586
587         if (single_source && _session) {
588                 /* check if it's an Ardour Send feeding.. */
589                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
590                 for (ARDOUR::RouteList::const_iterator i = routes->begin (); i != routes->end (); ++i) {
591                         uint32_t nth = 0;
592                         boost::shared_ptr<Processor> proc;
593                         /* nth_send () takes a processor read-lock */
594                         while ((proc = (*i)->nth_send (nth))) {
595                                 boost::shared_ptr<IOProcessor> send = boost::dynamic_pointer_cast<IOProcessor> (proc);
596                                 if (!send || !send->output ()) {
597                                         ++nth;
598                                         continue;
599                                 }
600                                 if (!send->output ()->connected_to (p->name ())) {
601                                         ++nth;
602                                         continue;
603                                 }
604                                 /* if processor goes away, we're notified by the port disconnect,
605                                  * there should be no need to explicily connect to proc->DropReferences
606                                  */
607                                 set<Evoral::Parameter> p = proc->what_can_be_automated ();
608                                 for (set<Evoral::Parameter>::iterator i = p.begin (); i != p.end (); ++i) {
609                                         Control* c = new Control (proc->automation_control (*i), _("Send"));
610                                         _controls.push_back (c);
611                                         ++r; ++rv;
612                                         _sidechain_tbl->attach (c->box, 0, 2, r, r + 1, EXPAND|FILL, SHRINK);
613                                 }
614                                 break;
615                         }
616                 }
617         }
618         return rv;
619 }
620
621 void
622 PluginPinDialog::update_element_pos ()
623 {
624         /* layout sizes */
625         _innerwidth = _width - 2. * _margin_x;
626
627         const double yc   = rint (_height * .5);
628         const double bxh2 = rint (_margin_y * .45); // TODO grow?
629         const double bxw  = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
630         const double bxw2 = rint (bxw * .5);
631         const double y_in = _margin_y;
632         const double y_out = _height - _margin_y;
633
634         _bxw2 = bxw2;
635         _bxh2 = bxh2;
636
637         const double dx = _pin_box_size * .5;
638
639         uint32_t sc_cnt = 0;
640         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
641                 switch (i->e->ct) {
642                         case Input:
643                                 if (i->e->sc) {
644                                         i->x = _innerwidth + _margin_x - dx;
645                                         i->y = y_in + (sc_cnt + .5) * _pin_box_size;
646                                         i->h = _pin_box_size;
647                                         i->w = 1.5 * _pin_box_size;
648                                         ++ sc_cnt;
649                                 } else {
650                                         uint32_t idx = i->e->id;
651                                         if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
652                                         i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
653                                         i->w = _pin_box_size;
654                                         i->h = 1.5 * _pin_box_size;
655                                         i->y = y_in - i->h;
656                                 }
657                                 break;
658                         case Output:
659                                 {
660                                         uint32_t idx = i->e->id;
661                                         if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
662                                         i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
663                                         i->y = y_out;
664                                         i->w = _pin_box_size;
665                                         i->h = 1.5 * _pin_box_size;
666                                 }
667                                 break;
668                         case Sink:
669                                 {
670                                         uint32_t idx = i->e->id;
671                                         if (i->e->dt == DataType::AUDIO) { idx += _sinks.n_midi (); }
672                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
673                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
674                                         i->y = yc - bxh2 - dx;
675                                         i->w = _pin_box_size;
676                                         i->h = _pin_box_size;
677                                 }
678                                 break;
679                         case Source:
680                                 {
681                                         uint32_t idx = i->e->id;
682                                         if (i->e->dt == DataType::AUDIO) { idx += _sources.n_midi (); }
683                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
684                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
685                                         i->y = yc + bxh2 - dx;
686                                         i->w = _pin_box_size;
687                                         i->h = _pin_box_size;
688                                 }
689                                 break;
690                 }
691         }
692 }
693
694 void
695 PluginPinDialog::set_color (cairo_t* cr, bool midi)
696 {
697         // see also gtk2_ardour/processor_box.cc
698         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
699         static const uint32_t midi_port_color = 0x960909FF; //Red
700
701         if (midi) {
702                 cairo_set_source_rgb (cr,
703                                 UINT_RGBA_R_FLT (midi_port_color),
704                                 UINT_RGBA_G_FLT (midi_port_color),
705                                 UINT_RGBA_B_FLT (midi_port_color));
706         } else {
707                 cairo_set_source_rgb (cr,
708                                 UINT_RGBA_R_FLT (audio_port_color),
709                                 UINT_RGBA_G_FLT (audio_port_color),
710                                 UINT_RGBA_B_FLT (audio_port_color));
711         }
712 }
713
714 void
715 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
716 {
717         if (w.e->sc) {
718                 const double dy = w.h * .5;
719                 const double dx = w.w - dy;
720                 cairo_move_to (cr, w.x, w.y + dy);
721                 cairo_rel_line_to (cr,  dy, -dy);
722                 cairo_rel_line_to (cr,  dx,  0);
723                 cairo_rel_line_to (cr,   0,  w.h);
724                 cairo_rel_line_to (cr, -dx,  0);
725         } else {
726                 const double dir = (w.e->ct == Input) ? 1 : -1;
727                 const double dx = w.w * .5;
728                 const double dy = w.h - dx;
729
730                 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
731                 cairo_rel_line_to (cr,     -dx, -dx * dir);
732                 cairo_rel_line_to (cr,      0., -dy * dir);
733                 cairo_rel_line_to (cr, 2. * dx,        0.);
734                 cairo_rel_line_to (cr,      0.,  dy * dir);
735         }
736         cairo_close_path  (cr);
737
738         cairo_set_line_width (cr, 1.0);
739         cairo_set_source_rgb (cr, 0, 0, 0);
740         cairo_stroke_preserve (cr);
741
742         set_color (cr, w.e->dt == DataType::MIDI);
743
744         if (w.e->sc) {
745                 assert (w.e->ct == Input);
746                 cairo_fill_preserve (cr);
747                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
748         }
749
750         if (w.e == _selection || w.e == _actor) {
751                 cairo_fill_preserve (cr);
752                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
753         } else if (w.prelight) {
754                 cairo_fill_preserve (cr);
755                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
756         }
757         cairo_fill (cr);
758 }
759
760 void
761 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
762 {
763         const double dx = w.w * .5;
764         const double dy = w.h * .5;
765
766         cairo_move_to (cr, w.x + dx, w.y);
767         cairo_rel_line_to (cr, -dx,  dy);
768         cairo_rel_line_to (cr,  dx,  dy);
769         cairo_rel_line_to (cr,  dx, -dy);
770         cairo_close_path  (cr);
771
772         cairo_set_line_width (cr, 1.0);
773         cairo_set_source_rgb (cr, 0, 0, 0);
774         cairo_stroke_preserve (cr);
775
776         set_color (cr, w.e->dt == DataType::MIDI);
777
778         if (w.e->sc) {
779                 assert (w.e->ct == Sink);
780                 cairo_fill_preserve (cr);
781                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
782         }
783
784         if (w.e == _selection || w.e == _actor) {
785                 cairo_fill_preserve (cr);
786                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
787         } else if (w.prelight) {
788                 cairo_fill_preserve (cr);
789                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
790         }
791         cairo_fill (cr);
792
793         if ((w.prelight || w.e == _selection) && !w.name.empty ()) {
794                 int text_width;
795                 int text_height;
796                 Glib::RefPtr<Pango::Layout> layout;
797                 layout = Pango::Layout::create (get_pango_context ());
798                 layout->set_text (w.name);
799                 layout->get_pixel_size (text_width, text_height);
800
801                 rounded_rectangle (cr, w.x + dx - .5 * text_width - 2, w.y - text_height - 2,  text_width + 4, text_height + 2, 7);
802                 cairo_set_source_rgba (cr, 0, 0, 0, .5);
803                 cairo_fill (cr);
804
805                 cairo_move_to (cr, w.x + dx - .5 * text_width, w.y - text_height - 1);
806                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
807                 pango_cairo_show_layout (cr, layout->gobj ());
808         }
809 }
810
811 double
812 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
813 {
814         if (!midi) { i += n_midi; }
815         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
816 }
817
818 const PluginPinDialog::CtrlWidget&
819 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
820 {
821         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
822                 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
823                         return *i;
824                 }
825         }
826         assert (0);
827         fatal << string_compose (_("programming error: %1"),
828                         X_("Invalid Plugin I/O Port."))
829                 << endmsg;
830         abort (); /*NOTREACHED*/
831         static CtrlWidget screw_old_compilers ("", Input, DataType::NIL, 0);
832         return screw_old_compilers;
833 }
834
835 void
836 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
837 {
838         switch (w.e->ct) {
839                 case Input:
840                         if (w.e->sc) {
841                                 x = w.x;
842                                 y = w.y + w.h * .5;
843                         } else {
844                                 x = w.x + w.w * .5;
845                                 y = w.y + w.h;
846                         }
847                         break;
848                 case Output:
849                         x = w.x + w.w * .5;
850                         y = w.y;
851                         break;
852                 case Sink:
853                         x = w.x + w.w * .5;
854                         y = w.y;
855                         break;
856                 case Source:
857                         x = w.x + w.w * .5;
858                         y = w.y + w.h;
859                         break;
860         }
861 }
862
863 void
864 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
865 {
866         const double bz = 2 * _pin_box_size;
867         double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
868         if (x0 > _width * .5) { bc *= -1; }
869
870         cairo_move_to (cr, x0, y0);
871         if (horiz) {
872                 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
873         } else {
874                 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
875         }
876         cairo_set_line_width (cr, 3.0);
877         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
878         cairo_set_source_rgb (cr, 1, 0, 0);
879         if (dashed) {
880                 const double dashes[] = { 5, 7 };
881                 cairo_set_dash (cr, dashes, 2, 0);
882         }
883         set_color (cr, midi);
884         cairo_stroke (cr);
885         if (dashed) {
886                 cairo_set_dash (cr, 0, 0, 0);
887         }
888 }
889
890 void
891 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
892 {
893         double x0, x1, y0, y1;
894         edge_coordinates (w0, x0, y0);
895         edge_coordinates (w1, x1, y1);
896         assert (w0.e->dt == w1.e->dt);
897         draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
898 }
899
900
901 bool
902 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
903 {
904         Gtk::Allocation a = darea.get_allocation ();
905         double const width = a.get_width ();
906         double const height = a.get_height ();
907
908         if (!_position_valid) {
909                 _width = width;
910                 _height = height;
911                 update_element_pos ();
912                 _position_valid = true;
913         }
914
915         cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
916         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
917         cairo_clip (cr);
918
919         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
920         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
921         cairo_rectangle (cr, 0, 0, width, height);
922         cairo_fill (cr);
923
924         const double yc = rint (_height * .5);
925
926         /* processor box */
927         rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
928         cairo_set_line_width (cr, 1.0);
929         cairo_set_source_rgb (cr, .1, .1, .3);
930         cairo_stroke_preserve (cr);
931         cairo_set_source_rgb (cr, .3, .3, .3);
932         cairo_fill (cr);
933
934         /* labels */
935         Glib::RefPtr<Pango::Layout> layout;
936         layout = Pango::Layout::create (get_pango_context ());
937
938         layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
939         layout->set_width (_height * PANGO_SCALE);
940
941         int text_width;
942         int text_height;
943
944         layout->set_text (_route ()->name ());
945         layout->get_pixel_size (text_width, text_height);
946         cairo_save (cr);
947         cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
948         cairo_rotate (cr, M_PI * -.5);
949         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
950         pango_cairo_show_layout (cr, layout->gobj ());
951         cairo_new_path (cr);
952         cairo_restore (cr);
953
954         layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
955         layout->set_text (_pi->name ());
956         layout->get_pixel_size (text_width, text_height);
957         cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
958         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
959         pango_cairo_show_layout (cr, layout->gobj ());
960
961         if (_pi->signal_latency () > 0) {
962                 // TODO: this needs a better location also format to msec (and cache)
963                 layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
964                 layout->set_text (string_compose (_("Latency %1 spl"), _pi->signal_latency ()));
965                 layout->get_pixel_size (text_width, text_height);
966                 cairo_move_to (cr, _margin_x + _pin_box_size * .5, _margin_y + 2);
967                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
968                 pango_cairo_show_layout (cr, layout->gobj ());
969         }
970
971         if (_pi->strict_io () && !Profile->get_mixbus ()) {
972                 layout->set_text (_("Strict I/O"));
973                 layout->get_pixel_size (text_width, text_height);
974                 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
975                 const double sy0 = _height - 3 - text_height;
976
977                 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
978                 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
979                 cairo_fill (cr);
980
981                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
982                 cairo_move_to (cr, sx0, sy0);
983                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
984                 pango_cairo_show_layout (cr, layout->gobj ());
985         }
986
987         /* draw midi-bypass (behind) */
988         if (_pi->has_midi_bypass ()) {
989                 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
990                 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
991                 draw_connection (cr, cw0, cw1, true);
992         }
993
994         /* thru connections */
995         const ChanMapping::Mappings thru_map = _pi->thru_map ().mappings ();
996         for (ChanMapping::Mappings::const_iterator t = thru_map.begin (); t != thru_map.end (); ++t) {
997                 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
998                         const CtrlWidget& cw0 = get_io_ctrl (Output, t->first, c->first);
999                         const CtrlWidget& cw1 = get_io_ctrl (Input, t->first, c->second);
1000                         if (!(_dragging && cw1.e == _selection && cw0.e == _drag_dst)) {
1001                                 draw_connection (cr, cw1, cw0, true);
1002                         }
1003                 }
1004         }
1005
1006
1007         /* plugins & connection wires */
1008         for (uint32_t i = 0; i < _n_plugins; ++i) {
1009                 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
1010
1011                 /* plugin box */
1012                 cairo_set_source_rgb (cr, .5, .5, .5);
1013                 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
1014                 cairo_fill (cr);
1015
1016                 layout->set_width (1.9 * _bxw2 * PANGO_SCALE);
1017                 layout->set_text (string_compose (_("Instance #%1"), i + 1));
1018                 layout->get_pixel_size (text_width, text_height);
1019                 cairo_move_to (cr, x0 - text_width * .5, yc - text_height * .5);
1020                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
1021                 pango_cairo_show_layout (cr, layout->gobj ());
1022
1023                 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
1024                 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
1025
1026                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
1027                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1028                                 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
1029                                 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
1030                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
1031                                         draw_connection (cr, cw0, cw1);
1032                                 }
1033                         }
1034                 }
1035
1036                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
1037                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1038                                 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
1039                                 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
1040                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
1041                                         draw_connection (cr, cw0, cw1);
1042                                 }
1043                         }
1044                 }
1045         }
1046
1047         /* pins and ports */
1048         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
1049                 switch (i->e->ct) {
1050                         case Input:
1051                         case Output:
1052                                 draw_io_pin (cr, *i);
1053                                 break;
1054                         case Sink:
1055                         case Source:
1056                                 draw_plugin_pin (cr, *i);
1057                                 break;
1058                 }
1059         }
1060
1061         /* DnD wire */
1062         CtrlWidget *drag_src = NULL;
1063         if (_dragging) {
1064                 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
1065                         if (i->e  == _selection ) {
1066                                 drag_src = &(*i);
1067                         }
1068                 }
1069         }
1070
1071         if (drag_src) {
1072                 double x0, y0;
1073                 if (_selection->ct == Input || _selection->ct == Source) {
1074                         edge_coordinates (*drag_src, x0, y0);
1075                         draw_connection (cr, x0, _drag_x, y0, _drag_y,
1076                                         _selection->dt == DataType::MIDI, _selection->sc);
1077                 } else {
1078                         edge_coordinates (*drag_src, x0, y0);
1079                         draw_connection (cr, _drag_x, x0, _drag_y, y0,
1080                                         _selection->dt == DataType::MIDI, _selection->sc);
1081                 }
1082         }
1083
1084         cairo_destroy (cr);
1085         return true;
1086 }
1087
1088 void
1089 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
1090 {
1091         req->width = _min_width;
1092         req->height = _min_height;
1093 }
1094
1095 void
1096 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
1097 {
1098         _position_valid = false;
1099 }
1100
1101 bool
1102 PluginPinDialog::drag_type_matches (const CtrlElem& e)
1103 {
1104         if (!_dragging || !_selection) {
1105                 return true;
1106         }
1107         if (_selection->dt != e->dt) {
1108                 return false;
1109         }
1110         if (_selection->ct == Input  && e->ct == Sink)   { return true; }
1111         if (_selection->ct == Sink   && e->ct == Input)  { return true; }
1112         if (_selection->ct == Output && e->ct == Source) { return true; }
1113         if (_selection->ct == Source && e->ct == Output) { return true; }
1114         if (_selection->ct == Input  && e->ct == Output) { return true; }
1115         if (_selection->ct == Output && e->ct == Input)  { return true; }
1116         return false;
1117 }
1118
1119 void
1120 PluginPinDialog::start_drag (const CtrlElem& e, double x, double y)
1121 {
1122         assert (_selection == e);
1123         _drag_dst.reset ();
1124         if (e->ct == Sink) {
1125                 bool valid;
1126                 const ChanMapping& map (_pi->input_map (e->ip));
1127                 uint32_t idx = map.get (e->dt, e->id, &valid);
1128                 if (valid) {
1129                         const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
1130                         _drag_dst = e;
1131                         _selection = cw.e;
1132                 }
1133         }
1134         else if (e->ct == Output) {
1135                 for (uint32_t i = 0; i < _n_plugins; ++i) {
1136                         bool valid;
1137                         const ChanMapping& map (_pi->output_map (i));
1138                         uint32_t idx = map.get_src (e->dt, e->id, &valid);
1139                         if (valid) {
1140                                 const CtrlWidget& cw = get_io_ctrl (Source, e->dt, idx, i);
1141                                 _drag_dst = e;
1142                                 _selection = cw.e;
1143                                 break;
1144                         }
1145                 }
1146                 if (!_drag_dst) {
1147                         bool valid;
1148                         const ChanMapping& map (_pi->thru_map ());
1149                         uint32_t idx = map.get (e->dt, e->id, &valid);
1150                         if (valid) {
1151                                 const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
1152                                 _drag_dst = e;
1153                                 _selection = cw.e;
1154                         }
1155                 }
1156         }
1157         _dragging = true;
1158         _drag_x = x;
1159         _drag_y = y;
1160 }
1161
1162 bool
1163 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
1164 {
1165         bool changed = false;
1166         _hover.reset ();
1167         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
1168                 if (ev->x >= i->x && ev->x <= i->x + i->w
1169                                 && ev->y >= i->y && ev->y <= i->y + i->h
1170                                 && drag_type_matches (i->e))
1171                 {
1172                         if (!i->prelight) changed = true;
1173                         i->prelight = true;
1174                         _hover = i->e;
1175                 } else {
1176                         if (i->prelight) changed = true;
1177                         i->prelight = false;
1178                 }
1179         }
1180         if (_dragging) {
1181                 _drag_x = ev->x;
1182                 _drag_y = ev->y;
1183         }
1184         if (changed || _dragging) {
1185                 darea.queue_draw ();
1186         }
1187         return true;
1188 }
1189
1190 bool
1191 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
1192 {
1193         if (ev->type != GDK_BUTTON_PRESS) {
1194                 return false;
1195         }
1196
1197         switch (ev->button) {
1198                 case 1:
1199                         _drag_dst.reset ();
1200                         if (!_selection || (_selection && !_hover)) {
1201                                 _selection = _hover;
1202                                 _actor.reset ();
1203                                 if (_selection) {
1204                                         start_drag (_selection, ev->x, ev->y);
1205                                 } else {
1206                                         darea.queue_draw ();
1207                                 }
1208                         } else if (_selection && _hover && _selection != _hover) {
1209                                 if (_selection->dt != _hover->dt) { _actor.reset (); }
1210                                 else if (_selection->ct == Input  && _hover->ct == Sink)   { _actor = _hover; }
1211                                 else if (_selection->ct == Sink   && _hover->ct == Input)  { _actor = _hover; }
1212                                 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
1213                                 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
1214                                 else if (_selection->ct == Input  && _hover->ct == Output) { _actor = _hover; }
1215                                 else if (_selection->ct == Output && _hover->ct == Input)  { _actor = _hover; }
1216                                 if (!_actor) {
1217                                         _selection = _hover;
1218                                         start_drag (_selection, ev->x, ev->y);
1219                                 } else {
1220                                         darea.queue_draw ();
1221                                 }
1222                         } else if (_hover) {
1223                                 _selection = _hover;
1224                                 _actor.reset ();
1225                                 start_drag (_selection, ev->x, ev->y);
1226                         }
1227                         break;
1228                 case 3:
1229                         _drag_dst.reset ();
1230                         if (_selection != _hover) {
1231                                 _selection = _hover;
1232                                 darea.queue_draw ();
1233                         }
1234                         _actor.reset ();
1235                         break;
1236                 default:
1237                         break;
1238         }
1239
1240         return true;
1241 }
1242
1243 bool
1244 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
1245 {
1246         if (_dragging && _selection && _drag_dst && _drag_dst == _hover) {
1247                 // select click. (or re-connect same)
1248                 assert (_selection != _hover);
1249                 _actor.reset ();
1250                 _dragging = false;
1251                 _drag_dst.reset ();
1252                 _selection =_hover;
1253                 darea.queue_draw ();
1254                 return true;
1255         }
1256
1257         if (_dragging && _hover && _hover != _selection) {
1258                 _actor = _hover;
1259         }
1260
1261         if (_hover == _actor && _actor && ev->button == 1) {
1262                 assert (_selection);
1263                 assert (_selection->dt == _actor->dt);
1264                 if (_drag_dst) {
1265                         assert (_dragging && _selection != _drag_dst);
1266                         handle_disconnect (_drag_dst, true);
1267                 }
1268                 if      (_selection->ct == Input && _actor->ct == Sink) {
1269                         handle_input_action (_actor, _selection);
1270                 }
1271                 else if (_selection->ct == Sink && _actor->ct == Input) {
1272                         handle_input_action (_selection, _actor);
1273                 }
1274                 else if (_selection->ct == Output && _actor->ct == Source) {
1275                         handle_output_action (_actor, _selection);
1276                 }
1277                 else if (_selection->ct == Source && _actor->ct == Output) {
1278                         handle_output_action (_selection, _actor);
1279                 }
1280                 else if (_selection->ct == Input && _actor->ct == Output) {
1281                         handle_thru_action (_actor, _selection);
1282                 }
1283                 else if (_selection->ct == Output && _actor->ct == Input) {
1284                         handle_thru_action (_selection, _actor);
1285                 }
1286                 _selection.reset ();
1287         } else if (_hover == _selection && _selection && ev->button == 3) {
1288                 handle_disconnect (_selection);
1289         } else if (!_hover && ev->button == 3) {
1290                 reset_menu.popup (1, ev->time);
1291         }
1292
1293         if (_dragging && _hover != _selection) {
1294                 _selection.reset ();
1295         }
1296         _actor.reset ();
1297         _dragging = false;
1298         _drag_dst.reset ();
1299         darea.queue_draw ();
1300         return true;
1301 }
1302
1303 void
1304 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
1305 {
1306         const int pc = s->ip;
1307         bool valid;
1308         ChanMapping in_map (_pi->input_map (pc));
1309         uint32_t idx = in_map.get (s->dt, s->id, &valid);
1310
1311         if (valid && idx == i->id) {
1312                 // disconnect
1313                 if (!_dragging) {
1314                         in_map.unset (s->dt, s->id);
1315                         _pi->set_input_map (pc, in_map);
1316                 } else {
1317                         plugin_reconfigured ();
1318                 }
1319         }
1320         else if (!valid) {
1321                 // connect
1322                 in_map.set (s->dt, s->id, i->id);
1323                 _pi->set_input_map (pc, in_map);
1324         }
1325         else {
1326                 // reconnect
1327                 in_map.unset (s->dt, s->id);
1328                 in_map.set (s->dt, s->id, i->id);
1329                 _pi->set_input_map (pc, in_map);
1330         }
1331 }
1332
1333 void
1334 PluginPinDialog::disconnect_other_outputs (uint32_t skip_pc, DataType dt, uint32_t id)
1335 {
1336         _ignore_updates = true;
1337         for (uint32_t n = 0; n < _n_plugins; ++n) {
1338                 if (n == skip_pc) {
1339                         continue;
1340                 }
1341                 bool valid;
1342                 ChanMapping n_out_map (_pi->output_map (n));
1343                 uint32_t idx = n_out_map.get_src (dt, id, &valid);
1344                 if (valid) {
1345                         n_out_map.unset (dt, idx);
1346                         _pi->set_output_map (n, n_out_map);
1347                 }
1348         }
1349         _ignore_updates = false;
1350 }
1351
1352 void
1353 PluginPinDialog::disconnect_other_thru (DataType dt, uint32_t id)
1354 {
1355         _ignore_updates = true;
1356         bool valid;
1357         ChanMapping n_thru_map (_pi->thru_map ());
1358         n_thru_map.get (dt, id, &valid);
1359         if (valid) {
1360                 n_thru_map.unset (dt, id);
1361                 _pi->set_thru_map (n_thru_map);
1362         }
1363         _ignore_updates = false;
1364 }
1365
1366 void
1367 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
1368 {
1369         const uint32_t pc = s->ip;
1370         bool valid;
1371         ChanMapping out_map (_pi->output_map (pc));
1372         uint32_t idx = out_map.get (s->dt, s->id, &valid);
1373
1374         if (valid && idx == o->id) {
1375                 // disconnect
1376                 if (!_dragging) {
1377                         out_map.unset (s->dt, s->id);
1378                         _pi->set_output_map (pc, out_map);
1379                 } else {
1380                         plugin_reconfigured ();
1381                 }
1382         }
1383         else {
1384                 // disconnect source
1385                 disconnect_other_outputs (pc, s->dt, o->id);
1386                 disconnect_other_thru (s->dt, o->id);
1387                 out_map = _pi->output_map (pc); // re-read map
1388                 if (valid) {
1389                         out_map.unset (s->dt, s->id);
1390                 }
1391                 idx = out_map.get_src (s->dt, o->id, &valid);
1392                 if (valid) {
1393                         out_map.unset (s->dt, idx);
1394                 }
1395                 // connect
1396                 out_map.set (s->dt, s->id, o->id);
1397                 _pi->set_output_map (pc, out_map);
1398         }
1399 }
1400
1401 void
1402 PluginPinDialog::handle_thru_action (const CtrlElem &o, const CtrlElem &i)
1403 {
1404         bool valid;
1405         ChanMapping thru_map (_pi->thru_map ());
1406         uint32_t idx = thru_map.get (o->dt, o->id, &valid);
1407
1408         if (valid && idx == i->id) {
1409                 if (!_dragging) {
1410                         thru_map.unset (o->dt, o->id);
1411                 }
1412         } else {
1413                 // disconnect other outputs first
1414                 disconnect_other_outputs (UINT32_MAX, o->dt, o->id);
1415                 disconnect_other_thru (o->dt, o->id);
1416                 thru_map = _pi->thru_map (); // re-read map
1417
1418                 thru_map.set (o->dt, o->id, i->id);
1419         }
1420         _pi->set_thru_map (thru_map);
1421 }
1422
1423 bool
1424 PluginPinDialog::handle_disconnect (const CtrlElem &e, bool no_signal)
1425 {
1426         _ignore_updates = true;
1427         bool changed = false;
1428         bool valid;
1429
1430         switch (e->ct) {
1431                 case Input:
1432                         {
1433                                 ChanMapping n_thru_map (_pi->thru_map ());
1434                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1435                                         uint32_t idx = n_thru_map.get (e->dt, i, &valid);
1436                                         if (valid && idx == e->id) {
1437                                                 n_thru_map.unset (e->dt, i);
1438                                                 changed = true;
1439                                         }
1440                                 }
1441                                 if (changed) {
1442                                         _pi->set_thru_map (n_thru_map);
1443                                 }
1444                         }
1445                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1446                                 ChanMapping map (_pi->input_map (n));
1447                                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
1448                                         uint32_t idx = map.get (e->dt, i, &valid);
1449                                         if (valid && idx == e->id) {
1450                                                 map.unset (e->dt, i);
1451                                                 changed = true;
1452                                         }
1453                                 }
1454                                 _pi->set_input_map (n, map);
1455                         }
1456                         break;
1457                 case Sink:
1458                         {
1459                                 ChanMapping map (_pi->input_map (e->ip));
1460                                 map.get (e->dt, e->id, &valid);
1461                                 if (valid) {
1462                                         map.unset (e->dt, e->id);
1463                                         _pi->set_input_map (e->ip, map);
1464                                         changed = true;
1465                                 }
1466                         }
1467                         break;
1468                 case Source:
1469                         {
1470                                 ChanMapping map (_pi->output_map (e->ip));
1471                                 map.get (e->dt, e->id, &valid);
1472                                 if (valid) {
1473                                         map.unset (e->dt, e->id);
1474                                         _pi->set_output_map (e->ip, map);
1475                                         changed = true;
1476                                 }
1477                         }
1478                         break;
1479                 case Output:
1480                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1481                                 ChanMapping map (_pi->output_map (n));
1482                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1483                                         uint32_t idx = map.get (e->dt, i, &valid);
1484                                         if (valid && idx == e->id) {
1485                                                 map.unset (e->dt, i);
1486                                                 changed = true;
1487                                         }
1488                                 }
1489                                 if (changed) {
1490                                         _pi->set_output_map (n, map);
1491                                 }
1492                         }
1493                         {
1494                                 ChanMapping n_thru_map (_pi->thru_map ());
1495                                 n_thru_map.get (e->dt, e->id, &valid);
1496                                 if (valid) {
1497                                         n_thru_map.unset (e->dt, e->id);
1498                                         changed = true;
1499                                         _pi->set_thru_map (n_thru_map);
1500                                 }
1501                         }
1502                         break;
1503         }
1504         _ignore_updates = false;
1505         if (changed && !no_signal) {
1506                 plugin_reconfigured ();
1507         }
1508         return changed;
1509 }
1510
1511 void
1512 PluginPinDialog::toggle_sidechain ()
1513 {
1514         if (_session && _session->actively_recording ()) { return; }
1515         _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1516 }
1517
1518 void
1519 PluginPinDialog::connect_sidechain ()
1520 {
1521         if (!_session) { return; }
1522
1523         if (_sidechain_selector == 0) {
1524                 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1525         }
1526
1527         if (_sidechain_selector->is_visible ()) {
1528                 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1529         } else {
1530                 _sidechain_selector->present ();
1531         }
1532 }
1533
1534 void
1535 PluginPinDialog::reset_configuration ()
1536 {
1537         if (_set_config.get_active ()) {
1538                 _route ()->reset_plugin_insert (_pi);
1539         } else {
1540                 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1541         }
1542 }
1543
1544 void
1545 PluginPinDialog::reset_mapping ()
1546 {
1547         _pi->reset_map ();
1548 }
1549
1550 void
1551 PluginPinDialog::select_output_preset (uint32_t n_audio)
1552 {
1553         if (_session && _session->actively_recording ()) { return; }
1554         ChanCount out (DataType::AUDIO, n_audio);
1555         _route ()->plugin_preset_output (_pi, out);
1556 }
1557
1558 void
1559 PluginPinDialog::add_remove_plugin_clicked (bool add)
1560 {
1561         if (_session && _session->actively_recording ()) { return; }
1562         ChanCount out = _out;
1563         assert (add || _n_plugins > 0);
1564         _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1),  out);
1565 }
1566
1567 void
1568 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1569 {
1570         if (_session && _session->actively_recording ()) { return; }
1571         ChanCount out = _out;
1572         assert (add || out.get (dt) > 0);
1573         out.set (dt, out.get (dt) + (add ? 1 : -1));
1574         _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1575 }
1576
1577 void
1578 PluginPinDialog::add_sidechain_port (DataType dt)
1579 {
1580         if (_session && _session->actively_recording ()) { return; }
1581         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1582         if (!io) {
1583                 return;
1584         }
1585
1586         // this triggers a PluginIoReConfigure with process and processor write lock held
1587         // from /this/ thread.
1588         io->add_port ("", this, dt);
1589 }
1590
1591 void
1592 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1593 {
1594         if (_session && _session->actively_recording ()) { return; }
1595         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1596         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1597         if (!io || !p) {
1598                 return;
1599         }
1600         io->remove_port (p, this);
1601 }
1602
1603 void
1604 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1605 {
1606         if (_session && _session->actively_recording ()) { return; }
1607         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1608         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1609         if (!io || !p) {
1610                 return;
1611         }
1612         p->disconnect_all ();
1613 }
1614
1615 void
1616 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1617 {
1618         if (_session && _session->actively_recording ()) { return; }
1619         boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1620         boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1621         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1622         if (!io || !p0 || !p1) {
1623                 return;
1624         }
1625         _ignore_updates = true;
1626         p0->disconnect_all ();
1627         _ignore_updates = false;
1628         p0->connect (p1->name ());
1629 }
1630
1631 void
1632 PluginPinDialog::add_send_from (boost::weak_ptr<ARDOUR::Port> wp, boost::weak_ptr<ARDOUR::Route> wr)
1633 {
1634         if (_session && _session->actively_recording ()) { return; }
1635         boost::shared_ptr<Port> p = wp.lock ();
1636         boost::shared_ptr<Route> r = wr.lock ();
1637         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1638         if (!p || !r || !io || !_session) {
1639                 return;
1640         }
1641
1642         boost::shared_ptr<Pannable> sendpan (new Pannable (*_session));
1643         boost::shared_ptr<Send> send (new Send (*_session, sendpan, r->mute_master ()));
1644         const ChanCount& outs (r->amp ()->input_streams ());
1645         try {
1646                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance ()->process_lock ());
1647                 send->output()->ensure_io (outs, false, this);
1648         } catch (AudioEngine::PortRegistrationFailure& err) {
1649                 error << string_compose (_("Cannot set up new send: %1"), err.what ()) << endmsg;
1650                 return;
1651         }
1652
1653         _ignore_updates = true;
1654
1655         p->disconnect_all ();
1656
1657         DataType dt = p->type ();
1658         PortSet& ps (send->output ()->ports ());
1659         for (PortSet::iterator i = ps.begin (dt); i != ps.end (dt); ++i) {
1660                 p->connect (&(**i));
1661         }
1662
1663         send->set_remove_on_disconnect (true);
1664         r->add_processor (send, PreFader);
1665         _ignore_updates = false;
1666         queue_idle_update ();
1667 }
1668
1669 bool
1670 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1671 {
1672         if (_session && _session->actively_recording ()) { return false; }
1673         if (ev->button == 3) {
1674                 connect_sidechain ();
1675         }
1676         return false;
1677 }
1678
1679 struct RouteCompareByName {
1680         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1681                 return a->name ().compare (b->name ()) < 0;
1682         }
1683 };
1684
1685 bool
1686 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1687 {
1688         using namespace Menu_Helpers;
1689         if (!_session || _session->actively_recording ()) { return false; }
1690         if (!_session->engine ().connected ()) { return false; }
1691
1692         if (ev->button == 1) {
1693                 MenuList& citems = input_menu.items ();
1694                 input_menu.set_name ("ArdourContextMenu");
1695                 citems.clear ();
1696
1697                 boost::shared_ptr<Port> p = wp.lock ();
1698                 if (p && p->connected ()) {
1699                         citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1700                         citems.push_back (SeparatorElem ());
1701                 }
1702
1703 #if 0
1704                 // TODO add system inputs, too ?!
1705                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1706                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1707                         for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1708                         }
1709                         //maybe_add_bundle_to_input_menu (*i, current);
1710                 }
1711 #endif
1712
1713                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1714                 RouteList copy = *routes;
1715                 copy.sort (RouteCompareByName ());
1716                 uint32_t added = 0;
1717                 for (ARDOUR::RouteList::const_iterator i = copy.begin (); i != copy.end (); ++i) {
1718                         added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1719                 }
1720
1721                 if (added > 0) {
1722                         citems.push_back (SeparatorElem ());
1723                 }
1724                 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1725                 input_menu.popup (1, ev->time);
1726         }
1727         return false;
1728 }
1729
1730 uint32_t
1731 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1732 {
1733         uint32_t added = 0;
1734         using namespace Menu_Helpers;
1735         if (r->output () == _route ()->output ()) {
1736                 return added;
1737         }
1738
1739         if (_route ()->feeds_according_to_graph (r)) {
1740                 return added;
1741         }
1742
1743         MenuList& citems = input_menu.items ();
1744
1745         /*check if there's already a send.. */
1746         bool already_present = false;
1747         uint32_t nth = 0;
1748         boost::shared_ptr<Processor> proc;
1749         /* Note: nth_send () takes a processor read-lock */
1750         while ((proc = r->nth_send (nth))) {
1751                 boost::shared_ptr<IOProcessor> send = boost::dynamic_pointer_cast<IOProcessor> (proc);
1752                 if (!send || !send->output ()) {
1753                         ++nth;
1754                         continue;
1755                 }
1756                 if (send->output ()->connected_to (_pi->sidechain_input ())) {
1757                         // only if (send->remove_on_disconnect ()) ??
1758                         already_present = true;
1759                         ++nth;
1760                         continue;
1761                 }
1762 #if 1 // add existing sends that are not connected
1763                 PortSet& p (send->output ()->ports ());
1764                 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1765                         std::string n = i->name ();
1766                         replace_all (n, "_", " ");
1767                         citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1768                         ++added;
1769                 }
1770 #endif
1771                 ++nth;
1772         }
1773         /* we're going to create the new send pre-fader, so check the route amp's data type.  */
1774         const ChanCount& rc (r->amp ()->input_streams ());
1775         if (!already_present && rc.get (dt) > 0) {
1776                 citems.push_back (MenuElem (r->name (), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_send_from), wp, boost::weak_ptr<Route> (r))));
1777                 ++added;
1778         }
1779         return added;
1780 }
1781
1782 void
1783 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1784 {
1785         boost::shared_ptr<Port> p0 = w0.lock ();
1786         boost::shared_ptr<Port> p1 = w1.lock ();
1787
1788         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1789         if (!io) { return; }
1790
1791         if (p0 && io->has_port (p0)) {
1792                 queue_idle_update ();
1793         }
1794         else if (p1 && io->has_port (p1)) {
1795                 queue_idle_update ();
1796         }
1797 }
1798
1799 /* lifted from ProcessorEntry::Control */
1800 PluginPinDialog::Control::Control (boost::shared_ptr<AutomationControl> c, string const & n)
1801         : _control (c)
1802         , _adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain ()), 0, 1, 0.01, 0.1)
1803         , _slider (&_adjustment, boost::shared_ptr<PBD::Controllable> (), 0, max (13.f, rintf (13.f * UIConfiguration::instance ().get_ui_scale ())))
1804         , _slider_persistant_tooltip (&_slider)
1805         , _ignore_ui_adjustment (false)
1806         , _name (n)
1807 {
1808         _slider.set_controllable (c);
1809         box.set_padding (0, 0, 4, 4);
1810
1811         _slider.set_name ("ProcessorControlSlider");
1812         _slider.set_text (_name);
1813
1814         box.add (_slider);
1815         _slider.show ();
1816
1817         const ARDOUR::ParameterDescriptor& desc = c->desc ();
1818         double const lo = c->internal_to_interface (desc.lower);
1819         double const up = c->internal_to_interface (desc.upper);
1820         double const normal = c->internal_to_interface (desc.normal);
1821         double smallstep = desc.smallstep;
1822         double largestep = desc.largestep;
1823
1824         if (smallstep == 0.0) {
1825                 smallstep = up / 1000.;
1826         } else {
1827                 smallstep = c->internal_to_interface (desc.lower + smallstep);
1828         }
1829
1830         if (largestep == 0.0) {
1831                 largestep = up / 40.;
1832         } else {
1833                 largestep = c->internal_to_interface (desc.lower + largestep);
1834         }
1835
1836         _adjustment.set_lower (lo);
1837         _adjustment.set_upper (up);
1838         _adjustment.set_step_increment (smallstep);
1839         _adjustment.set_page_increment (largestep);
1840         _slider.set_default_value (normal);
1841
1842         _adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &Control::slider_adjusted));
1843         // dup. currently timers are used :(
1844         //c->Changed.connect (_connection, MISSING_INVALIDATOR, boost::bind (&Control::control_changed, this), gui_context ());
1845
1846         // yuck, do we really need to do this?
1847         // according to c404374 this is only needed for send automation
1848         timer_connection = Timers::rapid_connect (sigc::mem_fun (*this, &Control::control_changed));
1849
1850         control_changed ();
1851         set_tooltip ();
1852
1853         /* We're providing our own PersistentTooltip */
1854         set_no_tooltip_whatsoever (_slider);
1855 }
1856
1857 PluginPinDialog::Control::~Control ()
1858 {
1859         timer_connection.disconnect ();
1860 }
1861
1862 void
1863 PluginPinDialog::Control::set_tooltip ()
1864 {
1865         boost::shared_ptr<AutomationControl> c = _control.lock ();
1866         if (!c) {
1867                 return;
1868         }
1869         char tmp[256];
1870         snprintf (tmp, sizeof (tmp), "%s: %.2f", _name.c_str (), c->internal_to_user (c->get_value ()));
1871
1872         string sm = Gtkmm2ext::markup_escape_text (tmp);
1873         _slider_persistant_tooltip.set_tip (sm);
1874 }
1875
1876 void
1877 PluginPinDialog::Control::slider_adjusted ()
1878 {
1879         if (_ignore_ui_adjustment) {
1880                 return;
1881         }
1882         boost::shared_ptr<AutomationControl> c = _control.lock ();
1883         if (!c) {
1884                 return;
1885         }
1886         c->set_value ( c->interface_to_internal (_adjustment.get_value ()) , Controllable::NoGroup);
1887         set_tooltip ();
1888 }
1889
1890
1891 void
1892 PluginPinDialog::Control::control_changed ()
1893 {
1894         boost::shared_ptr<AutomationControl> c = _control.lock ();
1895         if (!c) {
1896                 return;
1897         }
1898
1899         _ignore_ui_adjustment = true;
1900
1901         // as long as rapid timers are used, only update the tooltip
1902         // if the value has changed.
1903         const double nval = c->internal_to_interface (c->get_value ());
1904         if (_adjustment.get_value () != nval) {
1905                 _adjustment.set_value (nval);
1906                 set_tooltip ();
1907         }
1908
1909         _ignore_ui_adjustment = false;
1910 }