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