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