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