ca06887102c1050d03c92da42bfc139dd56ce027
[ardour.git] / libs / surfaces / cc121 / gui.cc
1 /*
2     Copyright (C) 2015 Paul Davis
3     Copyright (C) 2016 W.P. van Paassen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (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., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <gtkmm/alignment.h>
22 #include <gtkmm/label.h>
23 #include <gtkmm/liststore.h>
24
25 #include "pbd/unwind.h"
26 #include "pbd/strsplit.h"
27 #include "pbd/file_utils.h"
28
29 #include "gtkmm2ext/bindings.h"
30 #include "gtkmm2ext/gtk_ui.h"
31 #include "gtkmm2ext/gui_thread.h"
32 #include "gtkmm2ext/utils.h"
33
34 #include "ardour/audioengine.h"
35 #include "ardour/filesystem_paths.h"
36
37 #include "cc121.h"
38 #include "gui.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace PBD;
43 using namespace ARDOUR;
44 using namespace ArdourSurface;
45 using namespace std;
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48
49 void*
50 CC121::get_gui () const
51 {
52         if (!gui) {
53                 const_cast<CC121*>(this)->build_gui ();
54         }
55         static_cast<Gtk::VBox*>(gui)->show_all();
56         return gui;
57 }
58
59 void
60 CC121::tear_down_gui ()
61 {
62         if (gui) {
63                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
64                 if (w) {
65                         w->hide();
66                         delete w;
67                 }
68         }
69         delete static_cast<CC121GUI*> (gui);
70         gui = 0;
71 }
72
73 void
74 CC121::build_gui ()
75 {
76         gui = (void*) new CC121GUI (*this);
77 }
78
79 /*--------------------*/
80
81 CC121GUI::CC121GUI (CC121& p)
82         : fp (p)
83         , table (2, 5)
84         , action_table (5, 4)
85         , ignore_active_change (false)
86 {
87         set_border_width (12);
88
89         table.set_row_spacings (4);
90         table.set_col_spacings (6);
91         table.set_border_width (12);
92         table.set_homogeneous (false);
93
94         std::string data_file_path;
95         string name = "cc121.png";
96         Searchpath spath(ARDOUR::ardour_data_search_path());
97         spath.add_subdirectory_to_paths ("icons");
98         find_file (spath, name, data_file_path);
99         if (!data_file_path.empty()) {
100                 image.set (data_file_path);
101                 hpacker.pack_start (image, false, false);
102         }
103
104         Gtk::Label* l;
105         Gtk::Alignment* align;
106         int row = 0;
107         int action_row = 1;
108
109         input_combo.pack_start (midi_port_columns.short_name);
110         output_combo.pack_start (midi_port_columns.short_name);
111
112         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &CC121GUI::active_port_changed), &input_combo, true));
113         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &CC121GUI::active_port_changed), &output_combo, false));
114
115         l = manage (new Gtk::Label);
116         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
117         l->set_alignment (1.0, 0.5);
118         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
119         table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
120         row++;
121
122         l = manage (new Gtk::Label);
123         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
124         l->set_alignment (1.0, 0.5);
125         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
126         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
127         row++;
128
129         build_available_action_menu ();
130
131         build_user_action_combo (function1_combo, CC121::ButtonState(0), CC121::Function1);
132         build_user_action_combo (function2_combo, CC121::ButtonState(0), CC121::Function2);
133         build_user_action_combo (function3_combo, CC121::ButtonState(0), CC121::Function3);
134         build_user_action_combo (function4_combo, CC121::ButtonState(0), CC121::Function4);
135         build_user_action_combo (value_combo, CC121::ButtonState(0), CC121::Value);
136         build_user_action_combo (lock_combo, CC121::ButtonState(0), CC121::Lock);
137         build_user_action_combo (eq1_combo, CC121::ButtonState(0), CC121::EQ1Enable);
138         build_user_action_combo (eq2_combo, CC121::ButtonState(0), CC121::EQ2Enable);
139         build_user_action_combo (eq3_combo, CC121::ButtonState(0), CC121::EQ3Enable);
140         build_user_action_combo (eq4_combo, CC121::ButtonState(0), CC121::EQ4Enable);
141         build_user_action_combo (eqtype_combo, CC121::ButtonState(0), CC121::EQType);
142         build_user_action_combo (allbypass_combo, CC121::ButtonState(0), CC121::AllBypass);
143         build_foot_action_combo (foot_combo, CC121::ButtonState(0));
144         action_table.set_row_spacings (4);
145         action_table.set_col_spacings (6);
146         action_table.set_border_width (12);
147         action_table.set_homogeneous (false);
148
149         l = manage (new Gtk::Label);
150         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Function 1")));
151         l->set_alignment (1.0, 0.5);
152         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
153         align = manage (new Alignment);
154         align->set (0.0, 0.5);
155         align->add (function1_combo);
156         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
157         action_row++;
158
159         l = manage (new Gtk::Label);
160         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Function 2")));
161         l->set_alignment (1.0, 0.5);
162         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
163         align = manage (new Alignment);
164         align->set (0.0, 0.5);
165         align->add (function2_combo);
166         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
167         action_row++;
168
169         l = manage (new Gtk::Label);
170         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Function 3")));
171         l->set_alignment (1.0, 0.5);
172         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
173         align = manage (new Alignment);
174         align->set (0.0, 0.5);
175         align->add (function3_combo);
176         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
177         action_row++;
178
179         l = manage (new Gtk::Label);
180         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Function 4")));
181         l->set_alignment (1.0, 0.5);
182         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
183         align = manage (new Alignment);
184         align->set (0.0, 0.5);
185         align->add (function4_combo);
186         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
187         action_row++;
188
189         l = manage (new Gtk::Label);
190         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Value")));
191         l->set_alignment (1.0, 0.5);
192         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
193         align = manage (new Alignment);
194         align->set (0.0, 0.5);
195         align->add (value_combo);
196         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
197         action_row++;
198
199         l = manage (new Gtk::Label);
200         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Lock")));
201         l->set_alignment (1.0, 0.5);
202         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
203         align = manage (new Alignment);
204         align->set (0.0, 0.5);
205         align->add (lock_combo);
206         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
207         action_row++;
208
209         l = manage (new Gtk::Label);
210         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("EQ1")));
211         l->set_alignment (1.0, 0.5);
212         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
213         align = manage (new Alignment);
214         align->set (0.0, 0.5);
215         align->add (eq1_combo);
216         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
217         action_row++;
218
219         l = manage (new Gtk::Label);
220         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("EQ2")));
221         l->set_alignment (1.0, 0.5);
222         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
223         align = manage (new Alignment);
224         align->set (0.0, 0.5);
225         align->add (eq2_combo);
226         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
227         action_row++;
228
229         l = manage (new Gtk::Label);
230         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("EQ3")));
231         l->set_alignment (1.0, 0.5);
232         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
233         align = manage (new Alignment);
234         align->set (0.0, 0.5);
235         align->add (eq3_combo);
236         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
237         action_row++;
238
239         l = manage (new Gtk::Label);
240         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("EQ4")));
241         l->set_alignment (1.0, 0.5);
242         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
243         align = manage (new Alignment);
244         align->set (0.0, 0.5);
245         align->add (eq4_combo);
246         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
247         action_row++;
248
249         l = manage (new Gtk::Label);
250         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("EQType")));
251         l->set_alignment (1.0, 0.5);
252         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
253         align = manage (new Alignment);
254         align->set (0.0, 0.5);
255         align->add (eqtype_combo);
256         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
257         action_row++;
258
259         l = manage (new Gtk::Label);
260         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("AllBypass")));
261         l->set_alignment (1.0, 0.5);
262         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
263         align = manage (new Alignment);
264         align->set (0.0, 0.5);
265         align->add (allbypass_combo);
266         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
267         action_row++;
268
269         l = manage (new Gtk::Label);
270         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Footswitch")));
271         l->set_alignment (1.0, 0.5);
272         action_table.attach (*l, 0, 1, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
273         align = manage (new Alignment);
274         align->set (0.0, 0.5);
275         align->add (foot_combo);
276         action_table.attach (*align, 1, 2, action_row, action_row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
277         action_row++;
278
279         table.attach (action_table, 0, 5, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
280         row++;
281
282         hpacker.pack_start (table, true, true);
283         pack_start (hpacker, false, false);
284
285         /* update the port connection combos */
286
287         update_port_combos ();
288
289         /* catch future changes to connection state */
290
291         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&CC121GUI::connection_handler, this), gui_context());
292 }
293
294 CC121GUI::~CC121GUI ()
295 {
296 }
297
298 void
299 CC121GUI::connection_handler ()
300 {
301         /* ignore all changes to combobox active strings here, because we're
302            updating them to match a new ("external") reality - we were called
303            because port connections have changed.
304         */
305
306         PBD::Unwinder<bool> ici (ignore_active_change, true);
307
308         update_port_combos ();
309 }
310
311 void
312 CC121GUI::update_port_combos ()
313 {
314         vector<string> midi_inputs;
315         vector<string> midi_outputs;
316
317         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
318         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
319
320         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
321         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
322         bool input_found = false;
323         bool output_found = false;
324         int n;
325
326         input_combo.set_model (input);
327         output_combo.set_model (output);
328
329         Gtk::TreeModel::Children children = input->children();
330         Gtk::TreeModel::Children::iterator i;
331         i = children.begin();
332         ++i; /* skip "Disconnected" */
333
334
335         for (n = 1;  i != children.end(); ++i, ++n) {
336                 string port_name = (*i)[midi_port_columns.full_name];
337                 if (fp.input_port()->connected_to (port_name)) {
338                         input_combo.set_active (n);
339                         input_found = true;
340                         break;
341                 }
342         }
343
344         if (!input_found) {
345                 input_combo.set_active (0); /* disconnected */
346         }
347
348         children = output->children();
349         i = children.begin();
350         ++i; /* skip "Disconnected" */
351
352         for (n = 1;  i != children.end(); ++i, ++n) {
353                 string port_name = (*i)[midi_port_columns.full_name];
354                 if (fp.output_port()->connected_to (port_name)) {
355                         output_combo.set_active (n);
356                         output_found = true;
357                         break;
358                 }
359         }
360
361         if (!output_found) {
362                 output_combo.set_active (0); /* disconnected */
363         }
364 }
365
366 void
367 CC121GUI::build_available_action_menu ()
368 {
369         /* build a model of all available actions (needs to be tree structured
370          * more)
371          */
372
373         available_action_model = TreeStore::create (action_columns);
374
375         vector<string> paths;
376         vector<string> labels;
377         vector<string> tooltips;
378         vector<string> keys;
379         vector<Glib::RefPtr<Gtk::Action> > actions;
380
381         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
382
383         typedef std::map<string,TreeIter> NodeMap;
384         NodeMap nodes;
385         NodeMap::iterator r;
386
387
388         vector<string>::iterator k;
389         vector<string>::iterator p;
390         vector<string>::iterator t;
391         vector<string>::iterator l;
392
393         available_action_model->clear ();
394
395         TreeIter rowp;
396         TreeModel::Row parent;
397
398         /* Disabled item (row 0) */
399
400         rowp = available_action_model->append();
401         parent = *(rowp);
402         parent[action_columns.name] = _("Disabled");
403
404         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
405
406                 TreeModel::Row row;
407                 vector<string> parts;
408
409                 parts.clear ();
410
411                 split (*p, parts, '/');
412
413                 if (parts.empty()) {
414                         continue;
415                 }
416
417                 //kinda kludgy way to avoid displaying menu items as mappable
418                 if ( parts[1] == _("Main_menu") )
419                         continue;
420                 if ( parts[1] == _("JACK") )
421                         continue;
422                 if ( parts[1] == _("redirectmenu") )
423                         continue;
424                 if ( parts[1] == _("Editor_menus") )
425                         continue;
426                 if ( parts[1] == _("RegionList") )
427                         continue;
428                 if ( parts[1] == _("ProcessorMenu") )
429                         continue;
430
431                 if ((r = nodes.find (parts[1])) == nodes.end()) {
432
433                         /* top level is missing */
434
435                         TreeIter rowp;
436                         TreeModel::Row parent;
437                         rowp = available_action_model->append();
438                         nodes[parts[1]] = rowp;
439                         parent = *(rowp);
440                         parent[action_columns.name] = parts[1];
441
442                         row = *(available_action_model->append (parent.children()));
443
444                 } else {
445
446                         row = *(available_action_model->append ((*r->second)->children()));
447
448                 }
449
450                 /* add this action */
451
452                 if (l->empty ()) {
453                         row[action_columns.name] = *t;
454                         action_map[*t] = *p;
455                 } else {
456                         row[action_columns.name] = *l;
457                         action_map[*l] = *p;
458                 }
459
460                 string path = (*p);
461                 /* ControlProtocol::access_action() is not interested in the
462                    legacy "<Actions>/" prefix part of a path.
463                 */
464                 path = path.substr (strlen ("<Actions>/"));
465
466                 row[action_columns.path] = path;
467         }
468 }
469
470 void
471 CC121GUI::action_changed (Gtk::ComboBox* cb, CC121::ButtonID id, CC121::ButtonState bs)
472 {
473         TreeModel::const_iterator row = cb->get_active ();
474         string action_path = (*row)[action_columns.path];
475
476         /* release binding */
477         fp.set_action (id, action_path, false, bs);
478 }
479
480 void
481 CC121GUI::build_action_combo (Gtk::ComboBox& cb, vector<pair<string,string> > const & actions, CC121::ButtonID id, CC121::ButtonState bs)
482 {
483         Glib::RefPtr<Gtk::ListStore> model (Gtk::ListStore::create (action_columns));
484         TreeIter rowp;
485         TreeModel::Row row;
486         string current_action = fp.get_action (id, false, bs); /* lookup release action */
487         int active_row = -1;
488         int n;
489         vector<pair<string,string> >::const_iterator i;
490
491         rowp = model->append();
492         row = *(rowp);
493         row[action_columns.name] = _("Disabled");
494         row[action_columns.path] = string();
495
496         if (current_action.empty()) {
497                 active_row = 0;
498         }
499
500         for (i = actions.begin(), n = 0; i != actions.end(); ++i, ++n) {
501                 rowp = model->append();
502                 row = *(rowp);
503                 row[action_columns.name] = i->first;
504                 row[action_columns.path] = i->second;
505                 if (current_action == i->second) {
506                         active_row = n+1;
507                 }
508         }
509
510         cb.set_model (model);
511         cb.pack_start (action_columns.name);
512
513         if (active_row >= 0) {
514                 cb.set_active (active_row);
515         }
516
517         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &CC121GUI::action_changed), &cb, id, bs));
518 }
519
520 void
521 CC121GUI::build_foot_action_combo (Gtk::ComboBox& cb, CC121::ButtonState bs)
522 {
523         vector<pair<string,string> > actions;
524
525         actions.push_back (make_pair (string("Toggle Roll"), string(X_("Transport/ToggleRoll"))));
526         actions.push_back (make_pair (string("Toggle Rec-Enable"), string(X_("Transport/Record"))));
527         actions.push_back (make_pair (string("Toggle Roll+Rec"), string(X_("Transport/record-roll"))));
528         actions.push_back (make_pair (string("Toggle Loop"), string(X_("Transport/Loop"))));
529         actions.push_back (make_pair (string("Toggle Click"), string(X_("Transport/ToggleClick"))));
530
531         build_action_combo (cb, actions, CC121::Footswitch, bs);
532 }
533
534 bool
535 CC121GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const & action_path, TreeModel::iterator* found)
536 {
537         TreeModel::Row row = *iter;
538         string path = row[action_columns.path];
539
540         if (path == action_path) {
541                 *found = iter;
542                 return true;
543         }
544
545         return false;
546 }
547
548 void
549 CC121GUI::build_user_action_combo (Gtk::ComboBox& cb, CC121::ButtonState bs, CC121::ButtonID id)
550 {
551         cb.set_model (available_action_model);
552         cb.pack_start (action_columns.name);
553         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &CC121GUI::action_changed), &cb, id, bs));
554
555         /* set the active "row" to the right value for the current button binding */
556
557         string current_action = fp.get_action (id, false, bs); /* lookup release action */
558
559         if (current_action.empty()) {
560                 cb.set_active (0); /* "disabled" */
561                 return;
562         }
563
564         TreeModel::iterator iter = available_action_model->children().end();
565
566         available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &CC121GUI::find_action_in_model), current_action, &iter));
567
568         if (iter != available_action_model->children().end()) {
569                 cb.set_active (iter);
570         } else {
571                 cb.set_active (0);
572         }
573 }
574
575 Glib::RefPtr<Gtk::ListStore>
576 CC121GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
577 {
578         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
579         TreeModel::Row row;
580
581         row = *store->append ();
582         row[midi_port_columns.full_name] = string();
583         row[midi_port_columns.short_name] = _("Disconnected");
584
585         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
586                 row = *store->append ();
587                 row[midi_port_columns.full_name] = *p;
588                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
589                 if (pn.empty ()) {
590                         pn = (*p).substr ((*p).find (':') + 1);
591                 }
592                 row[midi_port_columns.short_name] = pn;
593         }
594
595         return store;
596 }
597
598 void
599 CC121GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
600 {
601         if (ignore_active_change) {
602                 return;
603         }
604
605         TreeModel::iterator active = combo->get_active ();
606         string new_port = (*active)[midi_port_columns.full_name];
607
608         if (new_port.empty()) {
609                 if (for_input) {
610                         fp.input_port()->disconnect_all ();
611                 } else {
612                         fp.output_port()->disconnect_all ();
613                 }
614
615                 return;
616         }
617
618         if (for_input) {
619                 if (!fp.input_port()->connected_to (new_port)) {
620                         fp.input_port()->disconnect_all ();
621                         fp.input_port()->connect (new_port);
622                 }
623         } else {
624                 if (!fp.output_port()->connected_to (new_port)) {
625                         fp.output_port()->disconnect_all ();
626                         fp.output_port()->connect (new_port);
627                 }
628         }
629 }