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