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