hide strict-i/o UI for Mixbus
[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/box.h>
25 #include <gtkmm/label.h>
26
27 #include "pbd/replace_all.h"
28
29 #include "gtkmm2ext/utils.h"
30 #include "gtkmm2ext/rgb_macros.h"
31
32 #include "ardour/audioengine.h"
33 #include "ardour/plugin.h"
34 #include "ardour/port.h"
35 #include "ardour/profile.h"
36 #include "ardour/session.h"
37
38 #include "plugin_pin_dialog.h"
39 #include "gui_thread.h"
40 #include "tooltips.h"
41 #include "ui_config.h"
42
43 #include "i18n.h"
44
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace std;
48 using namespace Gtk;
49 using namespace Gtkmm2ext;
50
51 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
52         : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
53         , _set_config (_("Configure"), ArdourButton::led_default_elements)
54         , _tgl_sidechain (_("Side Chain"), ArdourButton::led_default_elements)
55         , _add_plugin (_("+"))
56         , _del_plugin (_("-"))
57         , _add_output_audio (_("+"))
58         , _del_output_audio (_("-"))
59         , _add_output_midi (_("+"))
60         , _del_output_midi (_("-"))
61         , _add_sc_audio (_("Audio"))
62         , _add_sc_midi (_("MIDI"))
63         , _pi (pi)
64         , _pin_box_size (10)
65         , _width (0)
66         , _height (0)
67         , _innerwidth (0)
68         , _margin_x (28)
69         , _margin_y (40)
70         , _min_width (300)
71         , _min_height (200)
72         , _n_inputs (0)
73         , _n_sidechains (0)
74         , _position_valid (false)
75         , _ignore_updates (false)
76         , _sidechain_selector (0)
77         , _dragging (false)
78 {
79         assert (pi->owner ()); // Route
80
81         _pi->PluginIoReConfigure.connect (
82                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
83                         );
84
85         _pi->PluginMapChanged.connect (
86                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
87                         );
88
89         _pi->PluginConfigChanged.connect (
90                         _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
91                         );
92
93         _pin_box_size = 2 * ceil (max (8., 10. * UIConfiguration::instance ().get_ui_scale ()) * .5);
94         _margin_x = 2 * ceil (max (24., 28. * UIConfiguration::instance ().get_ui_scale ()) * .5);
95         _margin_y = 2 * ceil (max (36., 40. * UIConfiguration::instance ().get_ui_scale ()) * .5);
96
97         _tgl_sidechain.set_name ("pinrouting sidechain");
98         _set_config.set_name ("pinrouting custom");
99
100         Menu_Helpers::MenuList& citems = reset_menu.items();
101         reset_menu.set_name ("ArdourContextMenu");
102         citems.clear();
103         citems.push_back (Menu_Helpers::MenuElem (_("Reset"), sigc::mem_fun (*this, &PluginPinDialog::reset_mapping)));
104
105         _pm_size_group  = SizeGroup::create (SIZE_GROUP_BOTH);
106         _add_plugin.set_tweaks (ArdourButton::Square);
107         _del_plugin.set_tweaks (ArdourButton::Square);
108         _pm_size_group->add_widget (_add_plugin);
109         _pm_size_group->add_widget (_del_plugin);
110         _pm_size_group->add_widget (_add_output_audio);
111         _pm_size_group->add_widget (_del_output_audio);
112         _pm_size_group->add_widget (_add_output_midi);
113         _pm_size_group->add_widget (_del_output_midi);
114
115         Box* box;
116         Frame *f;
117
118         VBox* tl = manage (new VBox ());
119         tl->set_border_width (2);
120         tl->set_spacing (2);
121
122         VBox* tr = manage (new VBox ());
123         tr->set_border_width (2);
124         tr->set_spacing (2);
125
126         /* left side */
127         tl->pack_start (_set_config, false, false);
128         tl->pack_start (*manage (new Label ("")), true, true); // invisible separator
129
130         box = manage (new HBox ());
131         box->set_border_width (2);
132         box->pack_start (_add_plugin, true, false);
133         box->pack_start (_del_plugin, true, false);
134         f = manage (new Frame());
135         f->set_label (_("Instances"));
136         f->add (*box);
137         tl->pack_start (*f, false, false);
138
139         box = manage (new HBox ());
140         box->set_border_width (2);
141         box->pack_start (_add_output_audio, true, false);
142         box->pack_start (_del_output_audio, true, false);
143         f = manage (new Frame());
144         f->set_label (_("Audio Out"));
145         f->add (*box);
146         tl->pack_start (*f, false, false);
147
148         box = manage (new HBox ());
149         box->set_border_width (2);
150         box->pack_start (_add_output_midi, true, false);
151         box->pack_start (_del_output_midi, true, false);
152         f = manage (new Frame());
153         f->set_label (_("MIDI Out"));
154         f->add (*box);
155         tl->pack_start (*f, false, false);
156
157
158         /* right side */
159         _sidechain_tbl = manage (new Gtk::Table ());
160         _sidechain_tbl->set_spacings (2);
161
162         tr->pack_start (_tgl_sidechain, false, false);
163         tr->pack_start (*_sidechain_tbl, true, true);
164
165         box = manage (new VBox ());
166         box->set_border_width (2);
167         box->set_spacing (2);
168         box->pack_start (_add_sc_audio, false, false);
169         box->pack_start (_add_sc_midi , false, false);
170         f = manage (new Frame());
171         f->set_label (_("Add Sidechain Input"));
172         f->add (*box);
173
174         tr->pack_start (*f, false, false);
175
176         /* global packing */
177         HBox* hbox = manage (new HBox ());
178         hbox->set_spacing (4);
179         hbox->pack_start (*tl, false, false);
180         hbox->pack_start (darea, true, true);
181         hbox->pack_start (*tr, false, false);
182
183         VBox* vbox = manage (new VBox ());
184         vbox->pack_start (*hbox, true, true);
185         set_border_width (4);
186         add (*vbox);
187         vbox->show_all ();
188
189         plugin_reconfigured ();
190
191         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
192         darea.signal_size_request ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_request));
193         darea.signal_size_allocate ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_allocate));
194         darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
195         darea.signal_button_press_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_press_event));
196         darea.signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_release_event));
197         darea.signal_motion_notify_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_motion_notify_event));
198
199         _tgl_sidechain.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::toggle_sidechain));
200
201         _set_config.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_configuration));
202         _add_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), true));
203         _del_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), false));
204
205         _add_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::AUDIO));
206         _del_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::AUDIO));
207         _add_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::MIDI));
208         _del_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::MIDI));
209         _add_sc_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::AUDIO));
210         _add_sc_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::MIDI));
211
212         AudioEngine::instance()->PortConnectedOrDisconnected.connect (
213                         _io_connection, invalidator (*this), boost::bind (&PluginPinDialog::port_connected_or_disconnected, this, _1, _3), gui_context ()
214                         );
215 }
216
217 PluginPinDialog::~PluginPinDialog ()
218 {
219         delete _sidechain_selector;
220 }
221
222 void
223 PluginPinDialog::plugin_reconfigured ()
224 {
225         ENSURE_GUI_THREAD (*this, &PluginPinDialog::plugin_reconfigured);
226         if (_ignore_updates) {
227                 return;
228         }
229         _n_plugins = _pi->get_count ();
230         _pi->configured_io (_in, _out);
231         _ins = _pi->internal_streams (); // with sidechain
232         _sinks = _pi->natural_input_streams ();
233         _sources = _pi->natural_output_streams ();
234
235         _tgl_sidechain.set_active (_pi->has_sidechain ());
236         _add_sc_audio.set_sensitive (_pi->has_sidechain ());
237         _add_sc_midi.set_sensitive (_pi->has_sidechain ());
238
239         if (_pi->custom_cfg ()) {
240                 _set_config.set_active (true);
241                 _add_plugin.set_sensitive (true);
242                 _add_output_audio.set_sensitive (true);
243                 _add_output_midi.set_sensitive (true);
244                 _del_plugin.set_sensitive (_n_plugins > 1);
245                 _del_output_audio.set_sensitive (_out.n_audio () > 0 && _out.n_total () > 1);
246                 _del_output_midi.set_sensitive (_out.n_midi () > 0 && _out.n_total () > 1);
247         } else {
248                 _set_config.set_active (false);
249                 _add_plugin.set_sensitive (false);
250                 _add_output_audio.set_sensitive (false);
251                 _add_output_midi.set_sensitive (false);
252                 _del_plugin.set_sensitive (false);
253                 _del_output_audio.set_sensitive (false);
254                 _del_output_midi.set_sensitive (false);
255         }
256
257         if (!_pi->has_sidechain () && _sidechain_selector) {
258                 delete _sidechain_selector;
259                 _sidechain_selector = 0;
260         }
261
262         refill_sidechain_table ();
263
264         /* update elements */
265
266         _elements.clear ();
267         _hover.reset ();
268         _actor.reset ();
269         _selection.reset ();
270         _drag_dst.reset();
271         _dragging = false;
272
273         _n_inputs = _n_sidechains = 0;
274
275         for (uint32_t i = 0; i < _ins.n_total (); ++i) {
276                 DataType dt = i < _ins.n_midi () ? DataType::MIDI : DataType::AUDIO;
277                 uint32_t id = dt == DataType::MIDI ? i : i - _ins.n_midi ();
278                 bool sidechain = id >= _in.get (dt) ? true : false;
279                 if (sidechain) {
280                         ++_n_sidechains;
281                 } else {
282                         ++_n_inputs;
283                 }
284
285                 CtrlWidget cw (CtrlWidget ("", Input, dt, id, 0, sidechain));
286                 _elements.push_back (cw);
287         }
288
289         for (uint32_t i = 0; i < _out.n_total (); ++i) {
290                 int id = (i < _out.n_midi ()) ? i : i - _out.n_midi ();
291                 _elements.push_back (CtrlWidget ("", Output, (i < _out.n_midi () ? DataType::MIDI : DataType::AUDIO), id));
292         }
293
294         for (uint32_t n = 0; n < _n_plugins; ++n) {
295                 boost::shared_ptr<Plugin> plugin = _pi->plugin (n);
296                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
297                         DataType dt (i < _sinks.n_midi () ? DataType::MIDI : DataType::AUDIO);
298                         int idx = (dt == DataType::MIDI) ? i : i - _sinks.n_midi ();
299                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, true, idx));
300                         CtrlWidget cw (CtrlWidget (iod.name, Sink, dt, idx, n, iod.is_sidechain));
301                         _elements.push_back (cw);
302                 }
303                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
304                         DataType dt (i < _sources.n_midi () ? DataType::MIDI : DataType::AUDIO);
305                         int idx = (dt == DataType::MIDI) ? i : i - _sources.n_midi ();
306                         const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, false, idx));
307                         _elements.push_back (CtrlWidget (iod.name, Source, dt, idx, n));
308                 }
309         }
310
311         /* calc minimum size */
312         const uint32_t max_ports = std::max (_ins.n_total (), _out.n_total ());
313         const uint32_t max_pins = std::max ((_sinks * _n_plugins).n_total (), (_sources * _n_plugins).n_total ());
314         uint32_t min_width = std::max (25 * max_ports, (uint32_t)(20 + _pin_box_size) * max_pins);
315         min_width = std::max (min_width, (uint32_t)ceilf (_margin_y * .45 * _n_plugins * 16. / 9.)); // 16 : 9 aspect
316         min_width = std::max ((uint32_t)300, min_width);
317
318         min_width = 50 + 10 * ceilf (min_width / 10.f);
319
320         uint32_t min_height = 3.5 * _margin_y + 2 * (_n_sidechains + 1) * _pin_box_size;
321         min_height = std::max ((uint32_t)200, min_height);
322         min_height = 4 * ceilf (min_height / 4.f);
323
324         if (min_width != _min_width || min_height != _min_height) {
325                 _min_width = min_width;
326                 _min_height = min_height;
327                 darea.queue_resize ();
328         }
329
330         _position_valid = false;
331         darea.queue_draw ();
332 }
333
334 void
335 PluginPinDialog::refill_sidechain_table ()
336 {
337         Table_Helpers::TableList& kids = _sidechain_tbl->children ();
338         for (Table_Helpers::TableList::iterator i = kids.begin (); i != kids.end ();) {
339                 i = kids.erase (i);
340         }
341         _sidechain_tbl->resize (1, 1);
342         if (!_pi->has_sidechain () && _sidechain_selector) {
343                 return;
344         }
345         boost::shared_ptr<IO> io = _pi->sidechain_input ();
346         if (!io) {
347                 return;
348         }
349
350         uint32_t r = 0;
351         PortSet& p (io->ports ());
352         bool can_remove = p.num_ports () > 1;
353         for (PortSet::iterator i = p.begin (DataType::MIDI); i != p.end (DataType::MIDI); ++i, ++r) {
354                 add_port_to_table (*i, r, can_remove);
355         }
356         for (PortSet::iterator i = p.begin (DataType::AUDIO); i != p.end (DataType::AUDIO); ++i, ++r) {
357                 add_port_to_table (*i, r, can_remove);
358         }
359         _sidechain_tbl->show_all ();
360 }
361
362 void
363 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
364 {
365         std::string lbl;
366         std::string tip = p->name ();
367         std::vector<std::string> cns;
368         p->get_connections (cns);
369
370         // TODO proper labels, see MixerStrip::update_io_button()
371         if (cns.size () == 0) {
372                 lbl = "-";
373         } else if (cns.size () > 1) {
374                 lbl = "...";
375                 tip += " &lt;- ";
376         } else {
377                 string lpn (PROGRAM_NAME);
378                 boost::to_lower (lpn);
379                 std::string program_port_prefix = lpn + ":"; // e.g. "ardour:"
380
381                 lbl = cns[0];
382                 tip += " &lt;- ";
383                 if (lbl.find ("system:capture_") == 0) {
384                         lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
385                         if (lbl.empty ()) {
386                                 lbl = cns[0].substr (15);
387                         }
388                 } else if (lbl.find("system:midi_capture_") == 0) {
389                         lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
390                         if (lbl.empty ()) {
391                                 // "system:midi_capture_123" -> "123"
392                                 lbl = "M " + cns[0].substr (20);
393                         }
394                 } else if (lbl.find (program_port_prefix) == 0) {
395                         lbl = lbl.substr (program_port_prefix.size());
396                 }
397         }
398         for (std::vector<std::string>::const_iterator i = cns.begin(); i != cns.end(); ++i) {
399                 tip += *i;
400                 tip += " ";
401         }
402         replace_all (lbl, "_", " ");
403
404         ArdourButton *pb = manage (new ArdourButton (lbl));
405         pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
406         pb->set_layout_ellipsize_width (108 * PANGO_SCALE);
407         ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
408         _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
409
410         pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
411         pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
412
413         pb = manage (new ArdourButton ("-"));
414         _sidechain_tbl->attach (*pb, 1, 2, r, r +1 , FILL, SHRINK);
415         if (can_remove) {
416                 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
417         } else {
418                 pb->set_sensitive (false);
419         }
420 }
421
422 void
423 PluginPinDialog::update_element_pos ()
424 {
425         /* layout sizes */
426         _innerwidth = _width - 2. * _margin_x;
427
428         const double yc   = rint (_height * .5);
429         const double bxh2 = rint (_margin_y * .45); // TODO grow?
430         const double bxw  = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
431         const double bxw2 = rint (bxw * .5);
432         const double y_in = _margin_y;
433         const double y_out = _height - _margin_y;
434
435         _bxw2 = bxw2;
436         _bxh2 = bxh2;
437
438         const double dx = _pin_box_size * .5;
439
440         uint32_t sc_cnt = 0;
441         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
442                 switch (i->e->ct) {
443                         case Input:
444                                 if (i->e->sc) {
445                                         i->x = _innerwidth + _margin_x - dx;
446                                         i->y = y_in + (sc_cnt + .5) * _pin_box_size;
447                                         i->h = _pin_box_size;
448                                         i->w = 1.5 * _pin_box_size;
449                                         ++ sc_cnt;
450                                 } else {
451                                         uint32_t idx = i->e->id;
452                                         if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
453                                         i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
454                                         i->w = _pin_box_size;
455                                         i->h = 1.5 * _pin_box_size;
456                                         i->y = y_in - i->h;
457                                 }
458                                 break;
459                         case Output:
460                                 {
461                                         uint32_t idx = i->e->id;
462                                         if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
463                                         i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
464                                         i->y = y_out;
465                                         i->w = _pin_box_size;
466                                         i->h = 1.5 * _pin_box_size;
467                                 }
468                                 break;
469                         case Sink:
470                                 {
471                                         uint32_t idx = i->e->id;
472                                         if (i->e->dt == DataType::AUDIO) { idx += _sinks.n_midi (); }
473                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
474                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
475                                         i->y = yc - bxh2 - dx;
476                                         i->w = _pin_box_size;
477                                         i->h = _pin_box_size;
478                                 }
479                                 break;
480                         case Source:
481                                 {
482                                         uint32_t idx = i->e->id;
483                                         if (i->e->dt == DataType::AUDIO) { idx += _sources.n_midi (); }
484                                         const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
485                                         i->x = _margin_x + rint (x0 + (idx + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
486                                         i->y = yc + bxh2 - dx;
487                                         i->w = _pin_box_size;
488                                         i->h = _pin_box_size;
489                                 }
490                                 break;
491                 }
492         }
493 }
494
495 void
496 PluginPinDialog::set_color (cairo_t* cr, bool midi)
497 {
498         // see also gtk2_ardour/processor_box.cc
499         static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
500         static const uint32_t midi_port_color = 0x960909FF; //Red
501
502         if (midi) {
503                 cairo_set_source_rgb (cr,
504                                 UINT_RGBA_R_FLT (midi_port_color),
505                                 UINT_RGBA_G_FLT (midi_port_color),
506                                 UINT_RGBA_B_FLT (midi_port_color));
507         } else {
508                 cairo_set_source_rgb (cr,
509                                 UINT_RGBA_R_FLT (audio_port_color),
510                                 UINT_RGBA_G_FLT (audio_port_color),
511                                 UINT_RGBA_B_FLT (audio_port_color));
512         }
513 }
514
515 void
516 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
517 {
518         if (w.e->sc) {
519                 const double dy = w.h * .5;
520                 const double dx = w.w - dy;
521                 cairo_move_to (cr, w.x, w.y + dy);
522                 cairo_rel_line_to (cr,  dy, -dy);
523                 cairo_rel_line_to (cr,  dx,  0);
524                 cairo_rel_line_to (cr,   0,  w.h);
525                 cairo_rel_line_to (cr, -dx,  0);
526         } else {
527                 const double dir = (w.e->ct == Input) ? 1 : -1;
528                 const double dx = w.w * .5;
529                 const double dy = w.h - dx;
530
531                 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
532                 cairo_rel_line_to (cr,     -dx, -dx * dir);
533                 cairo_rel_line_to (cr,      0., -dy * dir);
534                 cairo_rel_line_to (cr, 2. * dx,        0.);
535                 cairo_rel_line_to (cr,      0.,  dy * dir);
536         }
537         cairo_close_path  (cr);
538
539         cairo_set_line_width (cr, 1.0);
540         cairo_set_source_rgb (cr, 0, 0, 0);
541         cairo_stroke_preserve (cr);
542
543         set_color (cr, w.e->dt == DataType::MIDI);
544
545         if (w.e->sc) {
546                 assert (w.e->ct == Input);
547                 cairo_fill_preserve (cr);
548                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
549         }
550
551         if (w.e == _selection || w.e == _actor) {
552                 cairo_fill_preserve (cr);
553                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
554         } else if (w.prelight) {
555                 cairo_fill_preserve (cr);
556                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
557         }
558         cairo_fill (cr);
559 }
560
561 void
562 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
563 {
564         const double dx = w.w * .5;
565         const double dy = w.h * .5;
566
567         cairo_move_to (cr, w.x + dx, w.y);
568         cairo_rel_line_to (cr, -dx,  dy);
569         cairo_rel_line_to (cr,  dx,  dy);
570         cairo_rel_line_to (cr,  dx, -dy);
571         cairo_close_path  (cr);
572
573         cairo_set_line_width (cr, 1.0);
574         cairo_set_source_rgb (cr, 0, 0, 0);
575         cairo_stroke_preserve (cr);
576
577         set_color (cr, w.e->dt == DataType::MIDI);
578
579         if (w.e->sc) {
580                 assert (w.e->ct == Sink);
581                 cairo_fill_preserve (cr);
582                 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
583         }
584
585         if (w.e == _selection || w.e == _actor) {
586                 cairo_fill_preserve (cr);
587                 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
588         } else if (w.prelight) {
589                 cairo_fill_preserve (cr);
590                 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
591         }
592         cairo_fill (cr);
593
594         if ((w.prelight || w.e == _selection) && !w.name.empty()) {
595                 int text_width;
596                 int text_height;
597                 Glib::RefPtr<Pango::Layout> layout;
598                 layout = Pango::Layout::create (get_pango_context ());
599                 layout->set_text (w.name);
600                 layout->get_pixel_size (text_width, text_height);
601
602                 rounded_rectangle (cr, w.x + dx - .5 * text_width - 2, w.y - text_height - 2,  text_width + 4, text_height + 2, 7);
603                 cairo_set_source_rgba (cr, 0, 0, 0, .5);
604                 cairo_fill (cr);
605
606                 cairo_move_to (cr, w.x + dx - .5 * text_width, w.y - text_height - 1);
607                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
608                 pango_cairo_show_layout (cr, layout->gobj ());
609         }
610 }
611
612 double
613 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
614 {
615         if (!midi) { i += n_midi; }
616         return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
617 }
618
619 const PluginPinDialog::CtrlWidget&
620 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
621 {
622         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
623                 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
624                         return *i;
625                 }
626         }
627         assert (0);
628         fatal << string_compose (_("programming error: %1"),
629                         X_("Invalid Plugin I/O Port."))
630                 << endmsg;
631         abort (); /*NOTREACHED*/
632         static CtrlWidget screw_old_compilers ("", Input, DataType::NIL, 0);
633         return screw_old_compilers;
634 }
635
636 void
637 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
638 {
639         switch (w.e->ct) {
640                 case Input:
641                         if (w.e->sc) {
642                                 x = w.x;
643                                 y = w.y + w.h * .5;
644                         } else {
645                                 x = w.x + w.w * .5;
646                                 y = w.y + w.h;
647                         }
648                         break;
649                 case Output:
650                         x = w.x + w.w * .5;
651                         y = w.y;
652                         break;
653                 case Sink:
654                         x = w.x + w.w * .5;
655                         y = w.y;
656                         break;
657                 case Source:
658                         x = w.x + w.w * .5;
659                         y = w.y + w.h;
660                         break;
661         }
662 }
663
664 void
665 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
666 {
667         const double bz = 2 * _pin_box_size;
668         const double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
669
670         cairo_move_to (cr, x0, y0);
671         if (horiz) {
672                 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
673         } else {
674                 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
675         }
676         cairo_set_line_width (cr, 3.0);
677         cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
678         cairo_set_source_rgb (cr, 1, 0, 0);
679         if (dashed) {
680                 const double dashes[] = { 5, 7 };
681                 cairo_set_dash (cr, dashes, 2, 0);
682         }
683         set_color (cr, midi);
684         cairo_stroke (cr);
685         if (dashed) {
686                 cairo_set_dash (cr, 0, 0, 0);
687         }
688 }
689
690 void
691 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
692 {
693         double x0, x1, y0, y1;
694         edge_coordinates (w0, x0, y0);
695         edge_coordinates (w1, x1, y1);
696         assert (w0.e->dt == w1.e->dt);
697         draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
698 }
699
700
701 bool
702 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
703 {
704         Gtk::Allocation a = darea.get_allocation ();
705         double const width = a.get_width ();
706         double const height = a.get_height ();
707
708         if (!_position_valid) {
709                 _width = width;
710                 _height = height;
711                 update_element_pos ();
712                 _position_valid = true;
713         }
714
715         cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
716         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
717         cairo_clip (cr);
718
719         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
720         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
721         cairo_rectangle (cr, 0, 0, width, height);
722         cairo_fill (cr);
723
724         const double yc = rint (_height * .5);
725
726         /* processor box */
727         rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
728         cairo_set_line_width (cr, 1.0);
729         cairo_set_source_rgb (cr, .1, .1, .3);
730         cairo_stroke_preserve (cr);
731         cairo_set_source_rgb (cr, .3, .3, .3);
732         cairo_fill (cr);
733
734         /* draw midi-bypass (behind) */
735         if (_pi->has_midi_bypass ()) {
736                 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
737                 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
738                 draw_connection (cr, cw0, cw1, true);
739         }
740
741         /* thru connections */
742         const ChanMapping::Mappings thru_map = _pi->thru_map ().mappings ();
743         for (ChanMapping::Mappings::const_iterator t = thru_map.begin (); t != thru_map.end (); ++t) {
744                 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
745                         const CtrlWidget& cw0 = get_io_ctrl (Output, t->first, c->first);
746                         const CtrlWidget& cw1 = get_io_ctrl (Input, t->first, c->second);
747                         if (!(_dragging && cw1.e == _selection && cw0.e == _drag_dst)) {
748                                 draw_connection (cr, cw1, cw0, true);
749                         }
750                 }
751         }
752
753         /* labels */
754         Glib::RefPtr<Pango::Layout> layout;
755         layout = Pango::Layout::create (get_pango_context ());
756
757         layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
758         layout->set_width (_height * PANGO_SCALE);
759
760         int text_width;
761         int text_height;
762
763         layout->set_text (_route ()->name ());
764         layout->get_pixel_size (text_width, text_height);
765         cairo_save (cr);
766         cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
767         cairo_rotate (cr, M_PI * -.5);
768         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
769         pango_cairo_show_layout (cr, layout->gobj ());
770         cairo_new_path (cr);
771         cairo_restore (cr);
772
773         layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
774         layout->set_text (_pi->name ());
775         layout->get_pixel_size (text_width, text_height);
776         cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
777         cairo_set_source_rgba (cr, 1., 1., 1., 1.);
778         pango_cairo_show_layout (cr, layout->gobj ());
779
780         if (_pi->strict_io () && !Profile->get_mixbus ()) {
781                 layout->set_text (_("Strict I/O"));
782                 layout->get_pixel_size (text_width, text_height);
783                 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
784                 const double sy0 = _height - 3 - text_height;
785
786                 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
787                 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
788                 cairo_fill (cr);
789
790                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
791                 cairo_move_to (cr, sx0, sy0);
792                 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
793                 pango_cairo_show_layout (cr, layout->gobj ());
794         }
795
796
797         /* plugins & connection wires */
798         for (uint32_t i = 0; i < _n_plugins; ++i) {
799                 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
800
801                 /* plugin box */
802                 cairo_set_source_rgb (cr, .5, .5, .5);
803                 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
804                 cairo_fill (cr);
805
806                 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
807                 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
808
809                 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
810                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
811                                 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
812                                 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
813                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
814                                         draw_connection (cr, cw0, cw1);
815                                 }
816                         }
817                 }
818
819                 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
820                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
821                                 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
822                                 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
823                                 if (!(_dragging && cw0.e == _selection && cw1.e == _drag_dst)) {
824                                         draw_connection (cr, cw0, cw1);
825                                 }
826                         }
827                 }
828         }
829
830         /* pins and ports */
831         for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
832                 switch (i->e->ct) {
833                         case Input:
834                         case Output:
835                                 draw_io_pin (cr, *i);
836                                 break;
837                         case Sink:
838                         case Source:
839                                 draw_plugin_pin (cr, *i);
840                                 break;
841                 }
842         }
843
844         /* DnD wire */
845         CtrlWidget *drag_src = NULL;
846         if (_dragging) {
847                 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
848                         if (i->e  == _selection ) {
849                                 drag_src = &(*i);
850                         }
851                 }
852         }
853
854         if (drag_src) {
855                 double x0, y0;
856                 if (_selection->ct == Input || _selection->ct == Source) {
857                         edge_coordinates (*drag_src, x0, y0);
858                         draw_connection (cr, x0, _drag_x, y0, _drag_y,
859                                         _selection->dt == DataType::MIDI, _selection->sc);
860                 } else {
861                         edge_coordinates (*drag_src, x0, y0);
862                         draw_connection (cr, _drag_x, x0, _drag_y, y0,
863                                         _selection->dt == DataType::MIDI, _selection->sc);
864                 }
865         }
866
867         cairo_destroy (cr);
868         return true;
869 }
870
871 void
872 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
873 {
874         req->width = _min_width;
875         req->height = _min_height;
876 }
877
878 void
879 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
880 {
881         _position_valid = false;
882 }
883
884 bool
885 PluginPinDialog::drag_type_matches (const CtrlElem& e)
886 {
887         if (!_dragging || !_selection) {
888                 return true;
889         }
890         if (_selection->dt != e->dt) {
891                 return false;
892         }
893         if (_selection->ct == Input  && e->ct == Sink)   { return true; }
894         if (_selection->ct == Sink   && e->ct == Input)  { return true; }
895         if (_selection->ct == Output && e->ct == Source) { return true; }
896         if (_selection->ct == Source && e->ct == Output) { return true; }
897         if (_selection->ct == Input  && e->ct == Output) { return true; }
898         if (_selection->ct == Output && e->ct == Input)  { return true; }
899         return false;
900 }
901
902 void
903 PluginPinDialog::start_drag (const CtrlElem& e, double x, double y)
904 {
905         assert (_selection == e);
906         _drag_dst.reset ();
907         if (e->ct == Sink) {
908                 bool valid;
909                 const ChanMapping& map (_pi->input_map (e->ip));
910                 uint32_t idx = map.get (e->dt, e->id, &valid);
911                 if (valid) {
912                         const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
913                         _drag_dst = e;
914                         _selection = cw.e;
915                 }
916         }
917         else if (e->ct == Output) {
918                 for (uint32_t i = 0; i < _n_plugins; ++i) {
919                         bool valid;
920                         const ChanMapping& map (_pi->output_map (i));
921                         uint32_t idx = map.get_src (e->dt, e->id, &valid);
922                         if (valid) {
923                                 const CtrlWidget& cw = get_io_ctrl (Source, e->dt, idx, i);
924                                 _drag_dst = e;
925                                 _selection = cw.e;
926                                 break;
927                         }
928                 }
929                 if (!_drag_dst) {
930                         bool valid;
931                         const ChanMapping& map (_pi->thru_map ());
932                         uint32_t idx = map.get (e->dt, e->id, &valid);
933                         if (valid) {
934                                 const CtrlWidget& cw = get_io_ctrl (Input, e->dt, idx, 0);
935                                 _drag_dst = e;
936                                 _selection = cw.e;
937                         }
938                 }
939         }
940         _dragging = true;
941         _drag_x = x;
942         _drag_y = y;
943 }
944
945 bool
946 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
947 {
948         bool changed = false;
949         _hover.reset ();
950         for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
951                 if (ev->x >= i->x && ev->x <= i->x + i->w
952                                 && ev->y >= i->y && ev->y <= i->y + i->h
953                                 && drag_type_matches (i->e))
954                 {
955                         if (!i->prelight) changed = true;
956                         i->prelight = true;
957                         _hover = i->e;
958                 } else {
959                         if (i->prelight) changed = true;
960                         i->prelight = false;
961                 }
962         }
963         if (_dragging) {
964                 _drag_x = ev->x;
965                 _drag_y = ev->y;
966         }
967         if (changed || _dragging) {
968                 darea.queue_draw ();
969         }
970         return true;
971 }
972
973 bool
974 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
975 {
976         if (ev->type != GDK_BUTTON_PRESS) {
977                 return false;
978         }
979
980         switch (ev->button) {
981                 case 1:
982                         _drag_dst.reset ();
983                         if (!_selection || (_selection && !_hover)) {
984                                 _selection = _hover;
985                                 _actor.reset ();
986                                 if (_selection) {
987                                         start_drag (_selection, ev->x, ev->y);
988                                 }
989                                 darea.queue_draw ();
990                         } else if (_selection && _hover && _selection != _hover) {
991                                 if (_selection->dt != _hover->dt) { _actor.reset (); }
992                                 else if (_selection->ct == Input  && _hover->ct == Sink)   { _actor = _hover; }
993                                 else if (_selection->ct == Sink   && _hover->ct == Input)  { _actor = _hover; }
994                                 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
995                                 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
996                                 else if (_selection->ct == Input  && _hover->ct == Output) { _actor = _hover; }
997                                 else if (_selection->ct == Output && _hover->ct == Input)  { _actor = _hover; }
998                                 if (!_actor) {
999                                         _selection = _hover;
1000                                         start_drag (_selection, ev->x, ev->y);
1001                                 }
1002                                 darea.queue_draw ();
1003                         } else if (_hover) {
1004                                 _selection = _hover;
1005                                 _actor.reset ();
1006                                 start_drag (_selection, ev->x, ev->y);
1007                         }
1008                         break;
1009                 case 3:
1010                         _drag_dst.reset ();
1011                         if (_selection != _hover) {
1012                                 _selection = _hover;
1013                                 darea.queue_draw ();
1014                         }
1015                         _actor.reset ();
1016                         break;
1017                 default:
1018                         break;
1019         }
1020
1021         return true;
1022 }
1023
1024 bool
1025 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
1026 {
1027         if (_dragging && _selection && _drag_dst && _drag_dst == _hover) {
1028                 // select click. (or re-connect same)
1029                 assert (_selection != _hover);
1030                 _actor.reset ();
1031                 _dragging = false;
1032                 _drag_dst.reset ();
1033                 _selection =_hover;
1034                 darea.queue_draw ();
1035                 return true;
1036         }
1037
1038         if (_dragging && _hover && _hover != _selection) {
1039                 _actor = _hover;
1040         }
1041
1042         if (_hover == _actor && _actor && ev->button == 1) {
1043                 assert (_selection);
1044                 assert (_selection->dt == _actor->dt);
1045                 if (_drag_dst) {
1046                         assert (_dragging && _selection != _drag_dst);
1047                         handle_disconnect (_drag_dst, true);
1048                 }
1049                 if      (_selection->ct == Input && _actor->ct == Sink) {
1050                         handle_input_action (_actor, _selection);
1051                 }
1052                 else if (_selection->ct == Sink && _actor->ct == Input) {
1053                         handle_input_action (_selection, _actor);
1054                 }
1055                 else if (_selection->ct == Output && _actor->ct == Source) {
1056                         handle_output_action (_actor, _selection);
1057                 }
1058                 else if (_selection->ct == Source && _actor->ct == Output) {
1059                         handle_output_action (_selection, _actor);
1060                 }
1061                 else if (_selection->ct == Input && _actor->ct == Output) {
1062                         handle_thru_action (_actor, _selection);
1063                 }
1064                 else if (_selection->ct == Output && _actor->ct == Input) {
1065                         handle_thru_action (_selection, _actor);
1066                 }
1067                 _selection.reset ();
1068         } else if (_hover == _selection && _selection && ev->button == 3) {
1069                 handle_disconnect (_selection);
1070         } else if (!_hover && ev->button == 3) {
1071                 reset_menu.popup (1, ev->time);
1072         }
1073
1074         if (_dragging && _hover != _selection) {
1075                 _selection.reset ();
1076         }
1077         _actor.reset ();
1078         _dragging = false;
1079         _drag_dst.reset ();
1080         darea.queue_draw ();
1081         return true;
1082 }
1083
1084 void
1085 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
1086 {
1087         const int pc = s->ip;
1088         bool valid;
1089         ChanMapping in_map (_pi->input_map (pc));
1090         uint32_t idx = in_map.get (s->dt, s->id, &valid);
1091
1092         if (valid && idx == i->id) {
1093                 // disconnect
1094                 if (!_dragging) {
1095                         in_map.unset (s->dt, s->id);
1096                         _pi->set_input_map (pc, in_map);
1097                 } else {
1098                         plugin_reconfigured ();
1099                 }
1100         }
1101         else if (!valid) {
1102                 // connect
1103                 in_map.set (s->dt, s->id, i->id);
1104                 _pi->set_input_map (pc, in_map);
1105         }
1106         else {
1107                 // reconnect
1108                 in_map.unset (s->dt, s->id);
1109                 in_map.set (s->dt, s->id, i->id);
1110                 _pi->set_input_map (pc, in_map);
1111         }
1112 }
1113
1114 void
1115 PluginPinDialog::disconnect_other_outputs (uint32_t skip_pc, DataType dt, uint32_t id)
1116 {
1117         _ignore_updates = true;
1118         for (uint32_t n = 0; n < _n_plugins; ++n) {
1119                 if (n == skip_pc) {
1120                         continue;
1121                 }
1122                 bool valid;
1123                 ChanMapping n_out_map (_pi->output_map (n));
1124                 uint32_t idx = n_out_map.get_src (dt, id, &valid);
1125                 if (valid) {
1126                         n_out_map.unset (dt, idx);
1127                         _pi->set_output_map (n, n_out_map);
1128                 }
1129         }
1130         _ignore_updates = false;
1131 }
1132
1133 void
1134 PluginPinDialog::disconnect_other_thru (DataType dt, uint32_t id)
1135 {
1136         _ignore_updates = true;
1137         bool valid;
1138         ChanMapping n_thru_map (_pi->thru_map ());
1139         n_thru_map.get (dt, id, &valid);
1140         if (valid) {
1141                 n_thru_map.unset (dt, id);
1142                 _pi->set_thru_map (n_thru_map);
1143         }
1144         _ignore_updates = false;
1145 }
1146
1147 void
1148 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
1149 {
1150         const uint32_t pc = s->ip;
1151         bool valid;
1152         ChanMapping out_map (_pi->output_map (pc));
1153         uint32_t idx = out_map.get (s->dt, s->id, &valid);
1154
1155         if (valid && idx == o->id) {
1156                 // disconnect
1157                 if (!_dragging) {
1158                         out_map.unset (s->dt, s->id);
1159                         _pi->set_output_map (pc, out_map);
1160                 } else {
1161                         plugin_reconfigured ();
1162                 }
1163         }
1164         else {
1165                 // disconnect source
1166                 disconnect_other_outputs (pc, s->dt, o->id);
1167                 disconnect_other_thru (s->dt, o->id);
1168                 out_map = _pi->output_map (pc); // re-read map
1169                 if (valid) {
1170                         out_map.unset (s->dt, s->id);
1171                 }
1172                 idx = out_map.get_src (s->dt, o->id, &valid);
1173                 if (valid) {
1174                         out_map.unset (s->dt, idx);
1175                 }
1176                 // connect
1177                 out_map.set (s->dt, s->id, o->id);
1178                 _pi->set_output_map (pc, out_map);
1179         }
1180 }
1181
1182 void
1183 PluginPinDialog::handle_thru_action (const CtrlElem &o, const CtrlElem &i)
1184 {
1185         bool valid;
1186         ChanMapping thru_map (_pi->thru_map ());
1187         uint32_t idx = thru_map.get (o->dt, o->id, &valid);
1188
1189         if (valid && idx == i->id) {
1190                 if (!_dragging) {
1191                         thru_map.unset (o->dt, o->id);
1192                 }
1193         } else {
1194                 // disconnect other outputs first
1195                 disconnect_other_outputs (UINT32_MAX, o->dt, o->id);
1196                 disconnect_other_thru (o->dt, o->id);
1197                 thru_map = _pi->thru_map (); // re-read map
1198
1199                 thru_map.set (o->dt, o->id, i->id);
1200         }
1201         _pi->set_thru_map (thru_map);
1202 }
1203
1204 bool
1205 PluginPinDialog::handle_disconnect (const CtrlElem &e, bool no_signal)
1206 {
1207         _ignore_updates = true;
1208         bool changed = false;
1209         bool valid;
1210
1211         switch (e->ct) {
1212                 case Input:
1213                         {
1214                                 ChanMapping n_thru_map (_pi->thru_map ());
1215                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1216                                         uint32_t idx = n_thru_map.get (e->dt, i, &valid);
1217                                         if (valid && idx == e->id) {
1218                                                 n_thru_map.unset (e->dt, i);
1219                                                 changed = true;
1220                                         }
1221                                 }
1222                                 if (changed) {
1223                                         _pi->set_thru_map (n_thru_map);
1224                                 }
1225                         }
1226                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1227                                 ChanMapping map (_pi->input_map (n));
1228                                 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
1229                                         uint32_t idx = map.get (e->dt, i, &valid);
1230                                         if (valid && idx == e->id) {
1231                                                 map.unset (e->dt, i);
1232                                                 changed = true;
1233                                         }
1234                                 }
1235                                 _pi->set_input_map (n, map);
1236                         }
1237                         break;
1238                 case Sink:
1239                         {
1240                                 ChanMapping map (_pi->input_map (e->ip));
1241                                 map.get (e->dt, e->id, &valid);
1242                                 if (valid) {
1243                                         map.unset (e->dt, e->id);
1244                                         _pi->set_input_map (e->ip, map);
1245                                         changed = true;
1246                                 }
1247                         }
1248                         break;
1249                 case Source:
1250                         {
1251                                 ChanMapping map (_pi->output_map (e->ip));
1252                                 map.get (e->dt, e->id, &valid);
1253                                 if (valid) {
1254                                         map.unset (e->dt, e->id);
1255                                         _pi->set_output_map (e->ip, map);
1256                                         changed = true;
1257                                 }
1258                         }
1259                         break;
1260                 case Output:
1261                         for (uint32_t n = 0; n < _n_plugins; ++n) {
1262                                 ChanMapping map (_pi->output_map (n));
1263                                 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1264                                         uint32_t idx = map.get (e->dt, i, &valid);
1265                                         if (valid && idx == e->id) {
1266                                                 map.unset (e->dt, i);
1267                                                 changed = true;
1268                                         }
1269                                 }
1270                                 if (changed) {
1271                                         _pi->set_output_map (n, map);
1272                                 }
1273                         }
1274                         {
1275                                 ChanMapping n_thru_map (_pi->thru_map ());
1276                                 n_thru_map.get (e->dt, e->id, &valid);
1277                                 if (valid) {
1278                                         n_thru_map.unset (e->dt, e->id);
1279                                         changed = true;
1280                                         _pi->set_thru_map (n_thru_map);
1281                                 }
1282                         }
1283                         break;
1284         }
1285         _ignore_updates = false;
1286         if (changed && !no_signal) {
1287                 plugin_reconfigured ();
1288         }
1289         return changed;
1290 }
1291
1292 void
1293 PluginPinDialog::toggle_sidechain ()
1294 {
1295         if (_session && _session->actively_recording()) { return; }
1296         _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1297 }
1298
1299 void
1300 PluginPinDialog::connect_sidechain ()
1301 {
1302         if (!_session) { return; }
1303
1304         if (_sidechain_selector == 0) {
1305                 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1306         }
1307
1308         if (_sidechain_selector->is_visible ()) {
1309                 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1310         } else {
1311                 _sidechain_selector->present ();
1312         }
1313 }
1314
1315 void
1316 PluginPinDialog::reset_configuration ()
1317 {
1318         if (_set_config.get_active ()) {
1319                 _route ()->reset_plugin_insert (_pi);
1320         } else {
1321                 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1322         }
1323 }
1324
1325 void
1326 PluginPinDialog::reset_mapping ()
1327 {
1328         _pi->reset_map ();
1329 }
1330
1331 void
1332 PluginPinDialog::add_remove_plugin_clicked (bool add)
1333 {
1334         if (_session && _session->actively_recording()) { return; }
1335         ChanCount out = _out;
1336         assert (add || _n_plugins > 0);
1337         _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1),  out);
1338 }
1339
1340 void
1341 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1342 {
1343         if (_session && _session->actively_recording()) { return; }
1344         ChanCount out = _out;
1345         assert (add || out.get (dt) > 0);
1346         out.set (dt, out.get (dt) + (add ? 1 : -1));
1347         _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1348 }
1349
1350 void
1351 PluginPinDialog::add_sidechain_port (DataType dt)
1352 {
1353         if (_session && _session->actively_recording()) { return; }
1354         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1355         if (!io) {
1356                 return;
1357         }
1358         io->add_port ("", this, dt);
1359 }
1360
1361 void
1362 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1363 {
1364         if (_session && _session->actively_recording()) { return; }
1365         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1366         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1367         if (!io || !p) {
1368                 return;
1369         }
1370         io->remove_port (p, this);
1371 }
1372
1373 void
1374 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1375 {
1376         if (_session && _session->actively_recording()) { return; }
1377         boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1378         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1379         if (!io || !p) {
1380                 return;
1381         }
1382         p->disconnect_all ();
1383 }
1384
1385 void
1386 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1387 {
1388         if (_session && _session->actively_recording()) { return; }
1389         boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1390         boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1391         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1392         if (!io || !p0 || !p1) {
1393                 return;
1394         }
1395         _ignore_updates = true;
1396         p0->disconnect_all ();
1397         _ignore_updates = false;
1398         p0->connect (p1->name ());
1399 }
1400
1401 bool
1402 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1403 {
1404         if (_session && _session->actively_recording()) { return false; }
1405         if (ev->button == 3) {
1406                 connect_sidechain ();
1407         }
1408         return false;
1409 }
1410
1411 struct RouteCompareByName {
1412         bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1413                 return a->name().compare (b->name()) < 0;
1414         }
1415 };
1416
1417 bool
1418 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1419 {
1420         using namespace Menu_Helpers;
1421         if (!_session || _session->actively_recording()) { return false; }
1422         if (!_session->engine().connected()) { return false; }
1423
1424         if (ev->button == 1) {
1425                 MenuList& citems = input_menu.items();
1426                 input_menu.set_name ("ArdourContextMenu");
1427                 citems.clear();
1428
1429                 boost::shared_ptr<Port> p = wp.lock ();
1430                 if (p && p->connected ()) {
1431                         citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1432                         citems.push_back (SeparatorElem());
1433                 }
1434
1435 #if 0
1436                 // TODO add system inputs, too ?!
1437                 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1438                 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1439                         for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1440                         }
1441                         //maybe_add_bundle_to_input_menu (*i, current);
1442                 }
1443 #endif
1444
1445                 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1446                 RouteList copy = *routes;
1447                 copy.sort (RouteCompareByName ());
1448                 uint32_t added = 0;
1449                 for (ARDOUR::RouteList::const_iterator i = copy.begin(); i != copy.end(); ++i) {
1450                         added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1451                 }
1452
1453                 if (added > 0) {
1454                         citems.push_back (SeparatorElem());
1455                 }
1456                 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1457                 input_menu.popup (1, ev->time);
1458         }
1459         return false;
1460 }
1461
1462 uint32_t
1463 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1464 {
1465         uint32_t added = 0;
1466         using namespace Menu_Helpers;
1467         if (r->output () == _route()->output()) {
1468                 return added;
1469         }
1470
1471         if (_route ()->feeds_according_to_graph (r)) {
1472                 return added;
1473         }
1474
1475         MenuList& citems = input_menu.items();
1476         const IOVector& iov (r->all_outputs());
1477
1478         for (IOVector::const_iterator o = iov.begin(); o != iov.end(); ++o) {
1479                 boost::shared_ptr<IO> op = o->lock();
1480                 if (!op) {
1481                         continue;
1482                 }
1483                 PortSet& p (op->ports ());
1484                 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1485                         std::string n = i->name ();
1486                         replace_all (n, "_", " ");
1487                         citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun(*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1488                         ++added;
1489                 }
1490         }
1491         return added;
1492 }
1493
1494 void
1495 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1496 {
1497         boost::shared_ptr<Port> p0 = w0.lock ();
1498         boost::shared_ptr<Port> p1 = w1.lock ();
1499
1500         boost::shared_ptr<IO> io = _pi->sidechain_input ();
1501         if (!io) { return; }
1502
1503         if (p0 && io->has_port (p0)) {
1504                 plugin_reconfigured ();
1505         }
1506         else if (p1 && io->has_port (p1)) {
1507                 plugin_reconfigured ();
1508         }
1509 }