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