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