2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #include <gtkmm/table.h>
21 #include <gtkmm/frame.h>
22 #include <gtkmm/box.h>
23 #include <gtkmm/label.h>
25 #include "pbd/replace_all.h"
27 #include "gtkmm2ext/utils.h"
28 #include "gtkmm2ext/rgb_macros.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/plugin.h"
32 #include "ardour/port.h"
33 #include "ardour/session.h"
35 #include "plugin_pin_dialog.h"
36 #include "gui_thread.h"
38 #include "ui_config.h"
42 using namespace ARDOUR;
46 using namespace Gtkmm2ext;
48 PluginPinDialog::PluginPinDialog (boost::shared_ptr<ARDOUR::PluginInsert> pi)
49 : ArdourWindow (string_compose (_("Pin Configuration: %1"), pi->name ()))
50 , _set_config (_("Configure"), ArdourButton::led_default_elements)
51 , _tgl_sidechain (_("Side Chain"), ArdourButton::led_default_elements)
52 , _add_plugin (_("+"))
53 , _del_plugin (_("-"))
54 , _add_output_audio (_("+"))
55 , _del_output_audio (_("-"))
56 , _add_output_midi (_("+"))
57 , _del_output_midi (_("-"))
58 , _add_sc_audio (_("Audio"))
59 , _add_sc_midi (_("MIDI"))
71 , _position_valid (false)
72 , _ignore_updates (false)
73 , _sidechain_selector (0)
76 assert (pi->owner ()); // Route
78 _pi->PluginIoReConfigure.connect (
79 _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
82 _pi->PluginMapChanged.connect (
83 _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
86 _pi->PluginConfigChanged.connect (
87 _plugin_connections, invalidator (*this), boost::bind (&PluginPinDialog::plugin_reconfigured, this), gui_context ()
90 _pin_box_size = 2 * ceil (max (8., 10. * UIConfiguration::instance ().get_ui_scale ()) * .5);
91 _margin_x = 2 * ceil (max (24., 28. * UIConfiguration::instance ().get_ui_scale ()) * .5);
92 _margin_y = 2 * ceil (max (36., 40. * UIConfiguration::instance ().get_ui_scale ()) * .5);
94 _tgl_sidechain.set_name ("pinrouting sidechain");
95 _set_config.set_name ("pinrouting custom");
97 Menu_Helpers::MenuList& citems = reset_menu.items();
98 reset_menu.set_name ("ArdourContextMenu");
100 citems.push_back (Menu_Helpers::MenuElem (_("Reset"), sigc::mem_fun (*this, &PluginPinDialog::reset_mapping)));
102 _pm_size_group = SizeGroup::create (SIZE_GROUP_BOTH);
103 _add_plugin.set_tweaks (ArdourButton::Square);
104 _del_plugin.set_tweaks (ArdourButton::Square);
105 _pm_size_group->add_widget (_add_plugin);
106 _pm_size_group->add_widget (_del_plugin);
107 _pm_size_group->add_widget (_add_output_audio);
108 _pm_size_group->add_widget (_del_output_audio);
109 _pm_size_group->add_widget (_add_output_midi);
110 _pm_size_group->add_widget (_del_output_midi);
115 VBox* tl = manage (new VBox ());
116 tl->set_border_width (2);
119 VBox* tr = manage (new VBox ());
120 tr->set_border_width (2);
124 tl->pack_start (_set_config, false, false);
125 tl->pack_start (*manage (new Label ("")), true, true); // invisible separator
127 box = manage (new HBox ());
128 box->set_border_width (2);
129 box->pack_start (_add_plugin, true, false);
130 box->pack_start (_del_plugin, true, false);
131 f = manage (new Frame());
132 f->set_label (_("Instances"));
134 tl->pack_start (*f, false, false);
136 box = manage (new HBox ());
137 box->set_border_width (2);
138 box->pack_start (_add_output_audio, true, false);
139 box->pack_start (_del_output_audio, true, false);
140 f = manage (new Frame());
141 f->set_label (_("Audio Out"));
143 tl->pack_start (*f, false, false);
145 box = manage (new HBox ());
146 box->set_border_width (2);
147 box->pack_start (_add_output_midi, true, false);
148 box->pack_start (_del_output_midi, true, false);
149 f = manage (new Frame());
150 f->set_label (_("MIDI Out"));
152 tl->pack_start (*f, false, false);
156 _sidechain_tbl = manage (new Gtk::Table ());
157 _sidechain_tbl->set_spacings (2);
159 tr->pack_start (_tgl_sidechain, false, false);
160 tr->pack_start (*_sidechain_tbl, true, true);
162 box = manage (new VBox ());
163 box->set_border_width (2);
164 box->set_spacing (2);
165 box->pack_start (_add_sc_audio, false, false);
166 box->pack_start (_add_sc_midi , false, false);
167 f = manage (new Frame());
168 f->set_label (_("Add Port"));
171 tr->pack_start (*f, false, false);
174 HBox* hbox = manage (new HBox ());
175 hbox->set_spacing (4);
176 hbox->pack_start (*tl, false, false);
177 hbox->pack_start (darea, true, true);
178 hbox->pack_start (*tr, false, false);
180 VBox* vbox = manage (new VBox ());
181 vbox->pack_start (*hbox, true, true);
182 set_border_width (4);
186 plugin_reconfigured ();
188 darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::POINTER_MOTION_MASK);
189 darea.signal_size_request ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_request));
190 darea.signal_size_allocate ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_size_allocate));
191 darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_expose_event));
192 darea.signal_button_press_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_press_event));
193 darea.signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_button_release_event));
194 darea.signal_motion_notify_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::darea_motion_notify_event));
196 _tgl_sidechain.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::toggle_sidechain));
198 _set_config.signal_clicked.connect (sigc::mem_fun (*this, &PluginPinDialog::reset_configuration));
199 _add_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), true));
200 _del_plugin.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_plugin_clicked), false));
202 _add_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::AUDIO));
203 _del_output_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::AUDIO));
204 _add_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), true, DataType::MIDI));
205 _del_output_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_remove_port_clicked), false, DataType::MIDI));
206 _add_sc_audio.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::AUDIO));
207 _add_sc_midi.signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::add_sidechain_port), DataType::MIDI));
209 AudioEngine::instance()->PortConnectedOrDisconnected.connect (
210 _io_connection, invalidator (*this), boost::bind (&PluginPinDialog::port_connected_or_disconnected, this, _1, _3), gui_context ()
214 PluginPinDialog::~PluginPinDialog ()
216 delete _sidechain_selector;
220 PluginPinDialog::plugin_reconfigured ()
222 ENSURE_GUI_THREAD (*this, &PluginPinDialog::plugin_reconfigured);
223 if (_ignore_updates) {
226 _n_plugins = _pi->get_count ();
227 _pi->configured_io (_in, _out);
228 _ins = _pi->internal_streams (); // with sidechain
229 _sinks = _pi->natural_input_streams ();
230 _sources = _pi->natural_output_streams ();
232 _tgl_sidechain.set_active (_pi->has_sidechain ());
233 _add_sc_audio.set_sensitive (_pi->has_sidechain ());
234 _add_sc_midi.set_sensitive (_pi->has_sidechain ());
236 if (_pi->custom_cfg ()) {
237 _set_config.set_active (true);
238 _add_plugin.set_sensitive (true);
239 _add_output_audio.set_sensitive (true);
240 _add_output_midi.set_sensitive (true);
241 _del_plugin.set_sensitive (_n_plugins > 1);
242 _del_output_audio.set_sensitive (_out.n_audio () > 0 && _out.n_total () > 1);
243 _del_output_midi.set_sensitive (_out.n_midi () > 0 && _out.n_total () > 1);
245 _set_config.set_active (false);
246 _add_plugin.set_sensitive (false);
247 _add_output_audio.set_sensitive (false);
248 _add_output_midi.set_sensitive (false);
249 _del_plugin.set_sensitive (false);
250 _del_output_audio.set_sensitive (false);
251 _del_output_midi.set_sensitive (false);
254 if (!_pi->has_sidechain () && _sidechain_selector) {
255 delete _sidechain_selector;
256 _sidechain_selector = 0;
259 refill_sidechain_table ();
261 /* update elements */
269 _n_inputs = _n_sidechains = 0;
271 for (uint32_t i = 0; i < _ins.n_total (); ++i) {
272 DataType dt = i < _ins.n_midi () ? DataType::MIDI : DataType::AUDIO;
273 uint32_t id = dt == DataType::MIDI ? i : i - _ins.n_midi ();
274 bool sidechain = id >= _in.get (dt) ? true : false;
281 CtrlWidget cw (CtrlWidget (Input, dt, id, 0, sidechain));
282 _elements.push_back (cw);
285 for (uint32_t i = 0; i < _out.n_total (); ++i) {
286 int id = (i < _out.n_midi ()) ? i : i - _out.n_midi ();
287 _elements.push_back (CtrlWidget (Output, (i < _out.n_midi () ? DataType::MIDI : DataType::AUDIO), id));
290 for (uint32_t n = 0; n < _n_plugins; ++n) {
291 boost::shared_ptr<Plugin> plugin = _pi->plugin (n);
292 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
293 DataType dt (_sinks.n_midi () ? DataType::MIDI : DataType::AUDIO);
294 int idx = (i < _sinks.n_midi ()) ? i : i - _sinks.n_midi ();
295 const Plugin::IOPortDescription& iod (plugin->describe_io_port (dt, true, idx));
296 CtrlWidget cw (CtrlWidget (Sink, dt, i, n, iod.is_sidechain));
297 _elements.push_back (cw);
299 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
300 _elements.push_back (CtrlWidget (Source, (i < _sources.n_midi () ? DataType::MIDI : DataType::AUDIO), i, n));
304 /* calc minimum size */
305 const uint32_t max_ports = std::max (_ins.n_total (), _out.n_total ());
306 const uint32_t max_pins = std::max ((_sinks * _n_plugins).n_total (), (_sources * _n_plugins).n_total ());
307 uint32_t min_width = std::max (25 * max_ports, (uint32_t)(20 + _pin_box_size) * max_pins);
308 min_width = std::max (min_width, (uint32_t)ceilf (_margin_y * .45 * _n_plugins * 16. / 9.)); // 16 : 9 aspect
309 min_width = std::max ((uint32_t)300, min_width);
311 min_width = 50 + 10 * ceilf (min_width / 10.f);
313 uint32_t min_height = 3.5 * _margin_y + 2 * (_n_sidechains + 1) * _pin_box_size;
314 min_height = std::max ((uint32_t)200, min_height);
315 min_height = 4 * ceilf (min_height / 4.f);
317 if (min_width != _min_width || min_height != _min_height) {
318 _min_width = min_width;
319 _min_height = min_height;
320 darea.queue_resize ();
323 _position_valid = false;
328 PluginPinDialog::refill_sidechain_table ()
330 Table_Helpers::TableList& kids = _sidechain_tbl->children ();
331 for (Table_Helpers::TableList::iterator i = kids.begin (); i != kids.end ();) {
334 _sidechain_tbl->resize (1, 1);
335 if (!_pi->has_sidechain () && _sidechain_selector) {
338 boost::shared_ptr<IO> io = _pi->sidechain_input ();
344 PortSet& p (io->ports ());
345 bool can_remove = p.num_ports () > 1;
346 for (PortSet::iterator i = p.begin (DataType::MIDI); i != p.end (DataType::MIDI); ++i, ++r) {
347 add_port_to_table (*i, r, can_remove);
349 for (PortSet::iterator i = p.begin (DataType::AUDIO); i != p.end (DataType::AUDIO); ++i, ++r) {
350 add_port_to_table (*i, r, can_remove);
352 _sidechain_tbl->show_all ();
356 PluginPinDialog::add_port_to_table (boost::shared_ptr<Port> p, uint32_t r, bool can_remove)
359 std::string tip = p->name ();
360 std::vector<std::string> cns;
361 p->get_connections (cns);
363 // TODO proper labels, see MixerStrip::update_io_button()
364 if (cns.size () == 0) {
366 } else if (cns.size () > 1) {
372 if (lbl.find ("system:") == 0) {
373 lbl = AudioEngine::instance ()->get_pretty_name_by_name (lbl);
375 lbl = cns[0].substr (7);
379 for (std::vector<std::string>::const_iterator i = cns.begin(); i != cns.end(); ++i) {
384 ArdourButton *pb = manage (new ArdourButton (lbl));
385 pb->set_text_ellipsize (Pango::ELLIPSIZE_MIDDLE);
386 pb->set_layout_ellipsize_width (56 * PANGO_SCALE);
387 ARDOUR_UI_UTILS::set_tooltip (*pb, tip);
388 _sidechain_tbl->attach (*pb, 0, 1, r, r +1 , EXPAND|FILL, SHRINK);
390 pb->signal_button_press_event ().connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::sc_input_press), boost::weak_ptr<Port> (p)), false);
391 pb->signal_button_release_event ().connect (sigc::mem_fun (*this, &PluginPinDialog::sc_input_release), false);
393 pb = manage (new ArdourButton ("-"));
394 _sidechain_tbl->attach (*pb, 1, 2, r, r +1 , FILL, SHRINK);
396 pb->signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::remove_port), boost::weak_ptr<Port> (p)));
398 pb->set_sensitive (false);
403 PluginPinDialog::update_element_pos ()
406 _innerwidth = _width - 2. * _margin_x;
408 const double yc = rint (_height * .5);
409 const double bxh2 = rint (_margin_y * .45); // TODO grow?
410 const double bxw = rint ((_innerwidth * .95) / ((_n_plugins) + .2 * (_n_plugins - 1)));
411 const double bxw2 = rint (bxw * .5);
412 const double y_in = _margin_y;
413 const double y_out = _height - _margin_y;
418 const double dx = _pin_box_size * .5;
421 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
425 i->x = _innerwidth + _margin_x - dx;
426 i->y = y_in + (sc_cnt + .5) * _pin_box_size;
427 i->h = _pin_box_size;
428 i->w = 1.5 * _pin_box_size;
431 uint32_t idx = i->e->id;
432 if (i->e->dt == DataType::AUDIO) { idx += _in.n_midi (); }
433 i->x = rint ((idx + 1) * _width / (1. + _n_inputs)) - 0.5 - dx;
434 i->w = _pin_box_size;
435 i->h = 1.5 * _pin_box_size;
441 uint32_t idx = i->e->id;
442 if (i->e->dt == DataType::AUDIO) { idx += _out.n_midi (); }
443 i->x = rint ((idx + 1) * _width / (1. + _out.n_total ())) - 0.5 - dx;
445 i->w = _pin_box_size;
446 i->h = 1.5 * _pin_box_size;
451 const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
452 i->x = _margin_x + rint (x0 + (i->e->id + 1) * bxw / (1. + _sinks.n_total ())) - .5 - dx;
453 i->y = yc - bxh2 - dx;
454 i->w = _pin_box_size;
455 i->h = _pin_box_size;
460 const double x0 = rint ((i->e->ip + .5) * _innerwidth / (double)(_n_plugins)) - .5 - bxw2;
461 i->x = _margin_x + rint (x0 + (i->e->id + 1) * bxw / (1. + _sources.n_total ())) - .5 - dx;
462 i->y = yc + bxh2 - dx;
463 i->w = _pin_box_size;
464 i->h = _pin_box_size;
472 PluginPinDialog::set_color (cairo_t* cr, bool midi)
474 // see also gtk2_ardour/processor_box.cc
475 static const uint32_t audio_port_color = 0x4A8A0EFF; // Green
476 static const uint32_t midi_port_color = 0x960909FF; //Red
479 cairo_set_source_rgb (cr,
480 UINT_RGBA_R_FLT (midi_port_color),
481 UINT_RGBA_G_FLT (midi_port_color),
482 UINT_RGBA_B_FLT (midi_port_color));
484 cairo_set_source_rgb (cr,
485 UINT_RGBA_R_FLT (audio_port_color),
486 UINT_RGBA_G_FLT (audio_port_color),
487 UINT_RGBA_B_FLT (audio_port_color));
492 PluginPinDialog::draw_io_pin (cairo_t* cr, const CtrlWidget& w)
496 const double dy = w.h * .5;
497 const double dx = w.w - dy;
498 cairo_move_to (cr, w.x, w.y + dy);
499 cairo_rel_line_to (cr, dy, -dy);
500 cairo_rel_line_to (cr, dx, 0);
501 cairo_rel_line_to (cr, 0, w.h);
502 cairo_rel_line_to (cr, -dx, 0);
504 const double dir = (w.e->ct == Input) ? 1 : -1;
505 const double dx = w.w * .5;
506 const double dy = w.h - dx;
508 cairo_move_to (cr, w.x + dx, w.y + ((w.e->ct == Input) ? w.h : 0));
509 cairo_rel_line_to (cr, -dx, -dx * dir);
510 cairo_rel_line_to (cr, 0., -dy * dir);
511 cairo_rel_line_to (cr, 2. * dx, 0.);
512 cairo_rel_line_to (cr, 0., dy * dir);
514 cairo_close_path (cr);
516 cairo_set_line_width (cr, 1.0);
517 cairo_set_source_rgb (cr, 0, 0, 0);
518 cairo_stroke_preserve (cr);
520 set_color (cr, w.e->dt == DataType::MIDI);
523 assert (w.e->ct == Input);
524 cairo_fill_preserve (cr);
525 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
528 if (w.e == _selection || w.e == _actor) {
529 cairo_fill_preserve (cr);
530 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
531 } else if (w.prelight) {
532 cairo_fill_preserve (cr);
533 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
539 PluginPinDialog::draw_plugin_pin (cairo_t* cr, const CtrlWidget& w)
541 const double dx = w.w * .5;
542 const double dy = w.h * .5;
544 cairo_move_to (cr, w.x + dx, w.y);
545 cairo_rel_line_to (cr, -dx, dy);
546 cairo_rel_line_to (cr, dx, dy);
547 cairo_rel_line_to (cr, dx, -dy);
548 cairo_close_path (cr);
550 cairo_set_line_width (cr, 1.0);
551 cairo_set_source_rgb (cr, 0, 0, 0);
552 cairo_stroke_preserve (cr);
554 set_color (cr, w.e->dt == DataType::MIDI);
557 assert (w.e->ct == Sink);
558 cairo_fill_preserve (cr);
559 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.4);
562 if (w.e == _selection || w.e == _actor) {
563 cairo_fill_preserve (cr);
564 cairo_set_source_rgba (cr, 0.9, 0.9, 1.0, 0.6);
565 } else if (w.prelight) {
566 cairo_fill_preserve (cr);
567 cairo_set_source_rgba (cr, 0.9, 0.9, 0.9, 0.3);
573 PluginPinDialog::pin_x_pos (uint32_t i, double x0, double width, uint32_t n_total, uint32_t n_midi, bool midi)
575 if (!midi) { i += n_midi; }
576 return rint (x0 + (i + 1) * width / (1. + n_total)) - .5;
579 const PluginPinDialog::CtrlWidget&
580 PluginPinDialog::get_io_ctrl (CtrlType ct, DataType dt, uint32_t id, uint32_t ip) const
582 for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
583 if (i->e->ct == ct && i->e->dt == dt && i->e->id == id && i->e->ip == ip) {
587 fatal << string_compose (_("programming error: %1"),
588 X_("Invalid Plugin I/O Port."))
590 abort (); /*NOTREACHED*/
591 static CtrlWidget screw_old_compilers (Input, DataType::NIL, 0);
592 return screw_old_compilers;
596 PluginPinDialog::edge_coordinates (const CtrlWidget& w, double &x, double &y)
624 PluginPinDialog::draw_connection (cairo_t* cr, double x0, double x1, double y0, double y1, bool midi, bool horiz, bool dashed)
626 const double bz = 2 * _pin_box_size;
627 const double bc = (dashed && x0 == x1) ? 1.25 * _pin_box_size : 0;
629 cairo_move_to (cr, x0, y0);
631 cairo_curve_to (cr, x0 - bz, y0 + bc, x1 - bc, y1 - bz, x1, y1);
633 cairo_curve_to (cr, x0 - bc, y0 + bz, x1 - bc, y1 - bz, x1, y1);
635 cairo_set_line_width (cr, 3.0);
636 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
637 cairo_set_source_rgb (cr, 1, 0, 0);
639 const double dashes[] = { 5, 7 };
640 cairo_set_dash (cr, dashes, 2, 0);
642 set_color (cr, midi);
645 cairo_set_dash (cr, 0, 0, 0);
650 PluginPinDialog::draw_connection (cairo_t* cr, const CtrlWidget& w0, const CtrlWidget& w1, bool dashed)
652 double x0, x1, y0, y1;
653 edge_coordinates (w0, x0, y0);
654 edge_coordinates (w1, x1, y1);
655 assert (w0.e->dt == w1.e->dt);
656 draw_connection (cr, x0, x1, y0, y1, w0.e->dt == DataType::MIDI, w0.e->sc, dashed);
661 PluginPinDialog::darea_expose_event (GdkEventExpose* ev)
663 Gtk::Allocation a = darea.get_allocation ();
664 double const width = a.get_width ();
665 double const height = a.get_height ();
667 if (!_position_valid) {
670 update_element_pos ();
671 _position_valid = true;
674 cairo_t* cr = gdk_cairo_create (darea.get_window ()->gobj ());
675 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
678 Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
679 cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
680 cairo_rectangle (cr, 0, 0, width, height);
683 const double yc = rint (_height * .5);
686 rounded_rectangle (cr, _margin_x, _margin_y - _pin_box_size * .5, _innerwidth, _height - 2 * _margin_y + _pin_box_size, 7);
687 cairo_set_line_width (cr, 1.0);
688 cairo_set_source_rgb (cr, .1, .1, .3);
689 cairo_stroke_preserve (cr);
690 cairo_set_source_rgb (cr, .3, .3, .3);
693 /* draw midi-bypass (behind) */
694 if (_pi->has_midi_bypass ()) {
695 const CtrlWidget& cw0 = get_io_ctrl (Input, DataType::MIDI, 0);
696 const CtrlWidget& cw1 = get_io_ctrl (Output, DataType::MIDI, 0);
697 draw_connection (cr, cw0, cw1, true);
701 Glib::RefPtr<Pango::Layout> layout;
702 layout = Pango::Layout::create (get_pango_context ());
704 layout->set_ellipsize (Pango::ELLIPSIZE_MIDDLE);
705 layout->set_width (_height * PANGO_SCALE);
710 layout->set_text (_route ()->name ());
711 layout->get_pixel_size (text_width, text_height);
713 cairo_move_to (cr, .5 * (_margin_x - text_height), .5 * (_height + text_width));
714 cairo_rotate (cr, M_PI * -.5);
715 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
716 pango_cairo_show_layout (cr, layout->gobj ());
720 layout->set_width ((_innerwidth - 2 * _pin_box_size) * PANGO_SCALE);
721 layout->set_text (_pi->name ());
722 layout->get_pixel_size (text_width, text_height);
723 cairo_move_to (cr, _margin_x + _innerwidth - text_width - _pin_box_size * .5, _height - _margin_y - text_height);
724 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
725 pango_cairo_show_layout (cr, layout->gobj ());
727 if (_pi->strict_io ()) {
728 layout->set_text (_("Strict I/O"));
729 layout->get_pixel_size (text_width, text_height);
730 const double sx0 = _margin_x + .5 * (_innerwidth - text_width);
731 const double sy0 = _height - 3 - text_height;
733 rounded_rectangle (cr, sx0 - 2, sy0 - 1, text_width + 4, text_height + 2, 7);
734 cairo_set_source_rgba (cr, .4, .3, .1, 1.);
737 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
738 cairo_move_to (cr, sx0, sy0);
739 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
740 pango_cairo_show_layout (cr, layout->gobj ());
744 /* plugins & connection wires */
745 for (uint32_t i = 0; i < _n_plugins; ++i) {
746 double x0 = _margin_x + rint ((i + .5) * _innerwidth / (double)(_n_plugins)) - .5;
749 cairo_set_source_rgb (cr, .5, .5, .5);
750 rounded_rectangle (cr, x0 - _bxw2, yc - _bxh2, 2 * _bxw2, 2 * _bxh2, 7);
753 const ChanMapping::Mappings in_map = _pi->input_map (i).mappings ();
754 const ChanMapping::Mappings out_map = _pi->output_map (i).mappings ();
756 for (ChanMapping::Mappings::const_iterator t = in_map.begin (); t != in_map.end (); ++t) {
757 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
758 const CtrlWidget& cw0 = get_io_ctrl (Input, t->first, c->second);
759 const CtrlWidget& cw1 = get_io_ctrl (Sink, t->first, c->first, i);
760 draw_connection (cr, cw0, cw1);
764 for (ChanMapping::Mappings::const_iterator t = out_map.begin (); t != out_map.end (); ++t) {
765 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
766 const CtrlWidget& cw0 = get_io_ctrl (Source, t->first, c->first, i);
767 const CtrlWidget& cw1 = get_io_ctrl (Output, t->first, c->second);
768 draw_connection (cr, cw0, cw1);
774 for (CtrlElemList::const_iterator i = _elements.begin (); i != _elements.end (); ++i) {
778 draw_io_pin (cr, *i);
782 draw_plugin_pin (cr, *i);
787 CtrlWidget *drag_src = NULL;
789 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
790 if (i->e == _selection ) {
797 if (_selection->ct == Input || _selection->ct == Source) {
798 edge_coordinates (*drag_src, x0, y0);
799 draw_connection (cr, x0, _drag_x, y0, _drag_y,
800 _selection->dt == DataType::MIDI, _selection->sc);
802 edge_coordinates (*drag_src, x0, y0);
803 draw_connection (cr, _drag_x, x0, _drag_y, y0,
804 _selection->dt == DataType::MIDI, _selection->sc);
813 PluginPinDialog::darea_size_request (Gtk::Requisition* req)
815 req->width = _min_width;
816 req->height = _min_height;
820 PluginPinDialog::darea_size_allocate (Gtk::Allocation&)
822 _position_valid = false;
826 PluginPinDialog::drag_type_matches (CtrlType ct) {
827 if (!_dragging || !_selection) {
830 if (_selection->ct == Input && ct == Sink) { return true; }
831 if (_selection->ct == Sink && ct == Input) { return true; }
832 if (_selection->ct == Output && ct == Source) { return true; }
833 if (_selection->ct == Source && ct == Output) { return true; }
838 PluginPinDialog::darea_motion_notify_event (GdkEventMotion* ev)
840 bool changed = false;
842 for (CtrlElemList::iterator i = _elements.begin (); i != _elements.end (); ++i) {
843 if (ev->x >= i->x && ev->x <= i->x + i->w
844 && ev->y >= i->y && ev->y <= i->y + i->h && drag_type_matches (i->e->ct))
846 if (!i->prelight) changed = true;
850 if (i->prelight) changed = true;
858 if (changed || _dragging) {
865 PluginPinDialog::darea_button_press_event (GdkEventButton* ev)
867 if (ev->type != GDK_BUTTON_PRESS) {
871 switch (ev->button) {
873 if (!_selection || (_selection && !_hover)) {
882 } else if (_selection && _hover && _selection != _hover) {
883 if (_selection->dt != _hover->dt) { _actor.reset (); }
884 else if (_selection->ct == Input && _hover->ct == Sink) { _actor = _hover; }
885 else if (_selection->ct == Sink && _hover->ct == Input) { _actor = _hover; }
886 else if (_selection->ct == Output && _hover->ct == Source) { _actor = _hover; }
887 else if (_selection->ct == Source && _hover->ct == Output) { _actor = _hover; }
904 if (_selection != _hover) {
918 PluginPinDialog::darea_button_release_event (GdkEventButton* ev)
920 if (_dragging && _hover && _hover != _selection) {
923 if (_hover == _actor && _actor && ev->button == 1) {
925 assert (_selection->dt == _actor->dt);
926 if (_selection->ct == Input && _actor->ct == Sink) {
927 handle_input_action (_actor, _selection);
929 else if (_selection->ct == Sink && _actor->ct == Input) {
930 handle_input_action (_selection, _actor);
932 else if (_selection->ct == Output && _actor->ct == Source) {
933 handle_output_action (_actor, _selection);
935 else if (_selection->ct == Source && _actor->ct == Output) {
936 handle_output_action (_selection, _actor);
939 } else if (_hover == _selection && _selection && ev->button == 3) {
940 handle_disconnect (_selection);
941 } else if (!_hover && ev->button == 3) {
942 reset_menu.popup (1, ev->time);
951 PluginPinDialog::handle_input_action (const CtrlElem &s, const CtrlElem &i)
953 const int pc = s->ip;
955 ChanMapping in_map (_pi->input_map (pc));
956 uint32_t idx = in_map.get (s->dt, s->id, &valid);
958 if (valid && idx == i->id) {
960 in_map.unset (s->dt, s->id);
961 _pi->set_input_map (pc, in_map);
965 in_map.set (s->dt, s->id, i->id);
966 _pi->set_input_map (pc, in_map);
970 in_map.unset (s->dt, s->id);
971 in_map.set (s->dt, s->id, i->id);
972 _pi->set_input_map (pc, in_map);
977 PluginPinDialog::handle_output_action (const CtrlElem &s, const CtrlElem &o)
979 const uint32_t pc = s->ip;
981 ChanMapping out_map (_pi->output_map (pc));
982 uint32_t idx = out_map.get (s->dt, s->id, &valid);
984 if (valid && idx == o->id) {
986 out_map.unset (s->dt, s->id);
987 _pi->set_output_map (pc, out_map);
992 out_map.unset (s->dt, s->id);
994 // disconnect other outputs
995 _ignore_updates = true;
996 for (uint32_t n = 0; n < _n_plugins; ++n) {
1000 ChanMapping n_out_map (_pi->output_map (n));
1001 idx = n_out_map.get_src (s->dt, o->id, &valid);
1003 n_out_map.unset (s->dt, idx);
1004 _pi->set_output_map (n, n_out_map);
1007 _ignore_updates = false;
1008 idx = out_map.get_src (s->dt, o->id, &valid);
1010 out_map.unset (s->dt, idx);
1013 out_map.set (s->dt, s->id, o->id);
1014 _pi->set_output_map (pc, out_map);
1019 PluginPinDialog::handle_disconnect (const CtrlElem &e)
1021 _ignore_updates = true;
1022 bool changed = false;
1027 for (uint32_t n = 0; n < _n_plugins; ++n) {
1028 ChanMapping map (_pi->input_map (n));
1029 for (uint32_t i = 0; i < _sinks.n_total (); ++i) {
1030 uint32_t idx = map.get (e->dt, i, &valid);
1031 if (valid && idx == e->id) {
1032 map.unset (e->dt, i);
1036 _pi->set_input_map (n, map);
1041 ChanMapping map (_pi->input_map (e->ip));
1042 map.get (e->dt, e->id, &valid);
1044 map.unset (e->dt, e->id);
1045 _pi->set_input_map (e->ip, map);
1052 ChanMapping map (_pi->output_map (e->ip));
1053 map.get (e->dt, e->id, &valid);
1055 map.unset (e->dt, e->id);
1056 _pi->set_output_map (e->ip, map);
1062 for (uint32_t n = 0; n < _n_plugins; ++n) {
1063 ChanMapping map (_pi->output_map (n));
1064 for (uint32_t i = 0; i < _sources.n_total (); ++i) {
1065 uint32_t idx = map.get (e->dt, i, &valid);
1066 if (valid && idx == e->id) {
1067 map.unset (e->dt, i);
1071 _pi->set_output_map (n, map);
1075 _ignore_updates = false;
1077 plugin_reconfigured ();
1082 PluginPinDialog::toggle_sidechain ()
1084 if (_session && _session->actively_recording()) { return; }
1085 _route ()->add_remove_sidechain (_pi, !_pi->has_sidechain ());
1089 PluginPinDialog::connect_sidechain ()
1091 if (!_session) { return; }
1093 if (_sidechain_selector == 0) {
1094 _sidechain_selector = new IOSelectorWindow (_session, _pi->sidechain_input ());
1097 if (_sidechain_selector->is_visible ()) {
1098 _sidechain_selector->get_toplevel ()->get_window ()->raise ();
1100 _sidechain_selector->present ();
1105 PluginPinDialog::reset_configuration ()
1107 if (_set_config.get_active ()) {
1108 _route ()->reset_plugin_insert (_pi);
1110 _route ()->customize_plugin_insert (_pi, _n_plugins, _out);
1115 PluginPinDialog::reset_mapping ()
1121 PluginPinDialog::add_remove_plugin_clicked (bool add)
1123 if (_session && _session->actively_recording()) { return; }
1124 ChanCount out = _out;
1125 assert (add || _n_plugins > 0);
1126 _route ()->customize_plugin_insert (_pi, _n_plugins + (add ? 1 : -1), out);
1130 PluginPinDialog::add_remove_port_clicked (bool add, ARDOUR::DataType dt)
1132 if (_session && _session->actively_recording()) { return; }
1133 ChanCount out = _out;
1134 assert (add || out.get (dt) > 0);
1135 out.set (dt, out.get (dt) + (add ? 1 : -1));
1136 _route ()->customize_plugin_insert (_pi, _n_plugins, out);
1140 PluginPinDialog::add_sidechain_port (DataType dt)
1142 if (_session && _session->actively_recording()) { return; }
1143 boost::shared_ptr<IO> io = _pi->sidechain_input ();
1147 io->add_port ("", this, dt);
1151 PluginPinDialog::remove_port (boost::weak_ptr<ARDOUR::Port> wp)
1153 if (_session && _session->actively_recording()) { return; }
1154 boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1155 boost::shared_ptr<IO> io = _pi->sidechain_input ();
1159 io->remove_port (p, this);
1163 PluginPinDialog::disconnect_port (boost::weak_ptr<ARDOUR::Port> wp)
1165 if (_session && _session->actively_recording()) { return; }
1166 boost::shared_ptr<ARDOUR::Port> p = wp.lock ();
1167 boost::shared_ptr<IO> io = _pi->sidechain_input ();
1171 p->disconnect_all ();
1175 PluginPinDialog::connect_port (boost::weak_ptr<ARDOUR::Port> wp0, boost::weak_ptr<ARDOUR::Port> wp1)
1177 if (_session && _session->actively_recording()) { return; }
1178 boost::shared_ptr<ARDOUR::Port> p0 = wp0.lock ();
1179 boost::shared_ptr<ARDOUR::Port> p1 = wp1.lock ();
1180 boost::shared_ptr<IO> io = _pi->sidechain_input ();
1181 if (!io || !p0 || !p1) {
1184 _ignore_updates = true;
1185 p0->disconnect_all ();
1186 _ignore_updates = false;
1187 p0->connect (p1->name ());
1191 PluginPinDialog::sc_input_release (GdkEventButton *ev)
1193 if (_session && _session->actively_recording()) { return false; }
1194 if (ev->button == 3) {
1195 connect_sidechain ();
1200 struct RouteCompareByName {
1201 bool operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b) {
1202 return a->name().compare (b->name()) < 0;
1207 PluginPinDialog::sc_input_press (GdkEventButton *ev, boost::weak_ptr<ARDOUR::Port> wp)
1209 using namespace Menu_Helpers;
1210 if (!_session || _session->actively_recording()) { return false; }
1211 if (!_session->engine().connected()) { return false; }
1213 if (ev->button == 1) {
1214 MenuList& citems = input_menu.items();
1215 input_menu.set_name ("ArdourContextMenu");
1218 boost::shared_ptr<Port> p = wp.lock ();
1219 if (p && p->connected ()) {
1220 citems.push_back (MenuElem (_("Disconnect"), sigc::bind (sigc::mem_fun (*this, &PluginPinDialog::disconnect_port), wp)));
1221 citems.push_back (SeparatorElem());
1225 // TODO add system inputs, too ?!
1226 boost::shared_ptr<ARDOUR::BundleList> b = _session->bundles ();
1227 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1228 for (uint32_t j = 0; j < i->nchannels ().n_total (); ++j) {
1230 //maybe_add_bundle_to_input_menu (*i, current);
1234 boost::shared_ptr<ARDOUR::RouteList> routes = _session->get_routes ();
1235 RouteList copy = *routes;
1236 copy.sort (RouteCompareByName ());
1238 for (ARDOUR::RouteList::const_iterator i = copy.begin(); i != copy.end(); ++i) {
1239 added += maybe_add_route_to_input_menu (*i, p->type (), wp);
1243 citems.push_back (SeparatorElem());
1245 citems.push_back (MenuElem (_("Routing Grid"), sigc::mem_fun (*this, &PluginPinDialog::connect_sidechain)));
1246 input_menu.popup (1, ev->time);
1252 PluginPinDialog::maybe_add_route_to_input_menu (boost::shared_ptr<Route> r, DataType dt, boost::weak_ptr<Port> wp)
1255 using namespace Menu_Helpers;
1256 if (r->output () == _route()->output()) {
1260 if (_route ()->feeds_according_to_graph (r)) {
1264 MenuList& citems = input_menu.items();
1265 const IOVector& iov (r->all_outputs());
1267 for (IOVector::const_iterator o = iov.begin(); o != iov.end(); ++o) {
1268 boost::shared_ptr<IO> op = o->lock();
1272 PortSet& p (op->ports ());
1273 for (PortSet::iterator i = p.begin (dt); i != p.end (dt); ++i) {
1274 std::string n = i->name ();
1275 replace_all (n, "_", " ");
1276 citems.push_back (MenuElem (n, sigc::bind (sigc::mem_fun(*this, &PluginPinDialog::connect_port), wp, boost::weak_ptr<Port> (*i))));
1284 PluginPinDialog::port_connected_or_disconnected (boost::weak_ptr<ARDOUR::Port> w0, boost::weak_ptr<ARDOUR::Port> w1)
1286 boost::shared_ptr<Port> p0 = w0.lock ();
1287 boost::shared_ptr<Port> p1 = w1.lock ();
1289 boost::shared_ptr<IO> io = _pi->sidechain_input ();
1290 if (!io) { return; }
1292 if (p0 && io->has_port (p0)) {
1293 plugin_reconfigured ();
1295 else if (p1 && io->has_port (p1)) {
1296 plugin_reconfigured ();