0f0893c85ff4849d1c2a4f70a8b9005d42e2112d
[ardour.git] / libs / surfaces / us2400 / gui.cc
1 /*
2         Copyright (C) 2010 Paul Davis
3         Copyright (C) 2017 Ben Loftis
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 #include <gtkmm/comboboxtext.h>
21 #include <gtkmm/box.h>
22 #include <gtkmm/spinbutton.h>
23 #include <gtkmm/table.h>
24 #include <gtkmm/treeview.h>
25 #include <gtkmm/liststore.h>
26 #include <gtkmm/treestore.h>
27 #include <gtkmm/notebook.h>
28 #include <gtkmm/cellrenderercombo.h>
29 #include <gtkmm/scale.h>
30 #include <gtkmm/alignment.h>
31
32 #include "pbd/error.h"
33 #include "pbd/unwind.h"
34 #include "pbd/strsplit.h"
35 #include "pbd/stacktrace.h"
36
37 #include "gtkmm2ext/actions.h"
38 #include "gtkmm2ext/bindings.h"
39 #include "gtkmm2ext/gui_thread.h"
40 #include "gtkmm2ext/utils.h"
41
42 #include "ardour/audioengine.h"
43 #include "ardour/port.h"
44 #include "ardour/rc_configuration.h"
45
46 #include "us2400_control_protocol.h"
47 #include "device_info.h"
48 #include "gui.h"
49 #include "surface.h"
50 #include "surface_port.h"
51
52 #include "pbd/i18n.h"
53
54 using namespace std;
55 using namespace Gtk;
56 using namespace ArdourSurface;
57 using namespace US2400;
58
59 void*
60 US2400Protocol::get_gui () const
61 {
62         if (!_gui) {
63                 const_cast<US2400Protocol*>(this)->build_gui ();
64         }
65         static_cast<Gtk::Notebook*>(_gui)->show_all();
66         return _gui;
67 }
68
69 void
70 US2400Protocol::tear_down_gui ()
71 {
72         if (_gui) {
73                 Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
74                 if (w) {
75                         w->hide();
76                         delete w;
77                 }
78         }
79         delete (US2400ProtocolGUI*) _gui;
80         _gui = 0;
81 }
82
83 void
84 US2400Protocol::build_gui ()
85 {
86         _gui = (void *) new US2400ProtocolGUI (*this);
87 }
88
89 US2400ProtocolGUI::US2400ProtocolGUI (US2400Protocol& p)
90         : _cp (p)
91         , table (2, 9)
92         , _device_dependent_widget (0)
93         , _ignore_profile_changed (false)
94         , ignore_active_change (false)
95 {
96         Gtk::Label* l;
97         Gtk::Alignment* align;
98         int row = 0;
99
100         set_border_width (12);
101
102         table.set_row_spacings (4);
103         table.set_col_spacings (6);
104         table.set_border_width (12);
105         table.set_homogeneous (false);
106
107         _cp.DeviceChanged.connect (device_change_connection, invalidator (*this), boost::bind (&US2400ProtocolGUI::device_changed, this), gui_context());
108         _cp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&US2400ProtocolGUI::connection_handler, this), gui_context());
109
110         /* device-dependent part */
111
112         device_dependent_row = row;
113
114         if (_device_dependent_widget) {
115                 table.remove (*_device_dependent_widget);
116                 _device_dependent_widget = 0;
117         }
118
119         _device_dependent_widget = device_dependent_widget ();
120         table.attach (*_device_dependent_widget, 0, 12, row, row+1, AttachOptions(0), AttachOptions(0), 0, 0);
121         row++;
122
123         /* back to the boilerplate */
124
125         vector<string> profiles;
126
127         for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
128                 cerr << "add discovered profile " << i->first << endl;
129                 profiles.push_back (i->first);
130         }
131         Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
132         cerr << "set active profile from " << p.device_profile().name() << endl;
133         _profile_combo.set_active_text (p.device_profile().name());
134         _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &US2400ProtocolGUI::profile_combo_changed));
135
136         append_page (table, _("Device Setup"));
137         table.show_all();
138
139         /* function key editor */
140
141         VBox* fkey_packer = manage (new VBox);
142         HBox* profile_packer = manage (new HBox);
143         HBox* observation_packer = manage (new HBox);
144
145         l = manage (new Gtk::Label (_("Profile/Settings:")));
146         profile_packer->pack_start (*l, false, false);
147         profile_packer->pack_start (_profile_combo, true, true);
148         profile_packer->set_spacing (12);
149         profile_packer->set_border_width (12);
150
151         fkey_packer->pack_start (*profile_packer, false, false);
152         fkey_packer->pack_start (function_key_scroller, true, true);
153         fkey_packer->pack_start (*observation_packer, false, false);
154         fkey_packer->set_spacing (12);
155         function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
156         function_key_scroller.add (function_key_editor);
157         append_page (*fkey_packer, _("Function Keys"));
158
159         build_available_action_menu ();
160         build_function_key_editor ();
161         refresh_function_key_editor ();
162         fkey_packer->show_all();
163 }
164
165 void
166 US2400ProtocolGUI::connection_handler ()
167 {
168         /* ignore all changes to combobox active strings here, because we're
169            updating them to match a new ("external") reality - we were called
170            because port connections have changed.
171         */
172
173         PBD::Unwinder<bool> ici (ignore_active_change, true);
174
175         vector<Gtk::ComboBox*>::iterator ic;
176         vector<Gtk::ComboBox*>::iterator oc;
177
178         vector<string> midi_inputs;
179         vector<string> midi_outputs;
180
181         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
182         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
183
184         for (ic = input_combos.begin(), oc = output_combos.begin(); ic != input_combos.end() && oc != output_combos.end(); ++ic, ++oc) {
185
186                 boost::shared_ptr<Surface> surface = _cp.get_surface_by_raw_pointer ((*ic)->get_data ("surface"));
187
188                 if (surface) {
189                         update_port_combos (midi_inputs, midi_outputs, *ic, *oc, surface);
190                 }
191         }
192 }
193
194 void
195 US2400ProtocolGUI::update_port_combos (vector<string> const& midi_inputs, vector<string> const& midi_outputs,
196                                               Gtk::ComboBox* input_combo,
197                                               Gtk::ComboBox* output_combo,
198                                               boost::shared_ptr<Surface> surface)
199 {
200         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
201         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
202         bool input_found = false;
203         bool output_found = false;
204         int n;
205
206         input_combo->set_model (input);
207         output_combo->set_model (output);
208
209         Gtk::TreeModel::Children children = input->children();
210         Gtk::TreeModel::Children::iterator i;
211         i = children.begin();
212         ++i; /* skip "Disconnected" */
213
214
215         for (n = 1;  i != children.end(); ++i, ++n) {
216                 string port_name = (*i)[midi_port_columns.full_name];
217                 if (surface->port().input().connected_to (port_name)) {
218                         input_combo->set_active (n);
219                         input_found = true;
220                         break;
221                 }
222         }
223
224         if (!input_found) {
225                 input_combo->set_active (0); /* disconnected */
226         }
227
228         children = output->children();
229         i = children.begin();
230         ++i; /* skip "Disconnected" */
231
232         for (n = 1;  i != children.end(); ++i, ++n) {
233                 string port_name = (*i)[midi_port_columns.full_name];
234                 if (surface->port().output().connected_to (port_name)) {
235                         output_combo->set_active (n);
236                         output_found = true;
237                         break;
238                 }
239         }
240
241         if (!output_found) {
242                 output_combo->set_active (0); /* disconnected */
243         }
244 }
245
246 Gtk::Widget*
247 US2400ProtocolGUI::device_dependent_widget ()
248 {
249         Gtk::Table* dd_table;
250         Gtk::Label* l;
251         int row = 0;
252
253         uint32_t n_surfaces = 1 + _cp.device_info().extenders();
254         uint32_t main_pos = _cp.device_info().master_position();
255
256         dd_table = Gtk::manage (new Gtk::Table (2, n_surfaces));
257         dd_table->set_row_spacings (4);
258         dd_table->set_col_spacings (6);
259         dd_table->set_border_width (12);
260
261         vector<string> midi_inputs;
262         vector<string> midi_outputs;
263
264         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsPhysical), midi_inputs);
265         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsPhysical), midi_outputs);
266
267         input_combos.clear ();
268         output_combos.clear ();
269
270         int portcount = n_surfaces;
271
272         for (uint32_t n = 0; n < portcount; ++n) {
273
274                 boost::shared_ptr<Surface> surface = _cp.nth_surface (n);
275
276                 if (!surface) {
277                         PBD::fatal << string_compose (_("programming error: %1\n"), string_compose ("n=%1 surface not found!", n)) << endmsg;
278                         /*NOTREACHED*/
279                 }
280
281                 Gtk::ComboBox* input_combo = manage (new Gtk::ComboBox);
282                 Gtk::ComboBox* output_combo = manage (new Gtk::ComboBox);
283
284                 update_port_combos (midi_inputs, midi_outputs, input_combo, output_combo, surface);
285
286                 input_combo->pack_start (midi_port_columns.short_name);
287                 input_combo->set_data ("surface", surface.get());
288                 input_combos.push_back (input_combo);
289                 output_combo->pack_start (midi_port_columns.short_name);
290                 output_combo->set_data ("surface", surface.get());
291                 output_combos.push_back (output_combo);
292
293                 boost::weak_ptr<Surface> ws (surface);
294                 input_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &US2400ProtocolGUI::active_port_changed), input_combo, ws, true));
295                 output_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &US2400ProtocolGUI::active_port_changed), output_combo, ws, false));
296
297                 string send_string;
298                 string receive_string;
299                 
300                 //port 1,2,3 are faders & pan knobs. like mackie MCU
301                 //port 4 is the joystick
302                 //port 5 sends "chan" knobs (24 of them )
303                 //port 6 --- ???  ( could be send to knobs... ? )
304
305                 send_string = string_compose(_("US-2400 send port #%1 (faders %2 to %3):"), n + 1, n*8+1, n*8+8);
306                 receive_string = string_compose(_("US-2400 receive port #%1 (faders %2 to %3):"), n + 1, n*8+1, n*8+8);
307                 if (n==3) {
308                         send_string = string_compose(_("US-2400 send port #%1 (joystick):"), n + 1);
309                         receive_string = string_compose(_("US-2400 receive port #%1 (joystick):"), n + 1);
310                 }
311                 
312                 l = manage (new Gtk::Label (send_string));
313                 l->set_alignment (1.0, 0.5);
314                 dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
315                 dd_table->attach (*input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
316                 row++;
317
318                 l = manage (new Gtk::Label (receive_string));
319                 l->set_alignment (1.0, 0.5);
320                 dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
321                 dd_table->attach (*output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
322                 row++;
323         }
324
325         row++;
326         
327         l = manage (new Gtk::Label ("US-2400 Port #5 is reserved for use as a generic USB device. (click the CHAN button to activate)"));
328         l->set_alignment (1.0, 0.5);
329         dd_table->attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
330         row++;
331         row++;
332
333         l = manage (new Gtk::Label ("US-2400 Port #6 is unused."));
334         l->set_alignment (1.0, 0.5);
335         dd_table->attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
336         row++;
337         row++;
338
339         l = manage (new Gtk::Label ("NOTE:  you must select mode 4 on the US-2400 unit."));
340         l->set_alignment (1.0, 0.5);
341         dd_table->attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
342         row++;
343
344
345         return dd_table;
346 }
347
348 CellRendererCombo*
349 US2400ProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
350 {
351         CellRendererCombo* renderer = manage (new CellRendererCombo);
352         renderer->property_model() = model;
353         renderer->property_editable() = true;
354         renderer->property_text_column() = 0;
355         renderer->property_has_entry() = false;
356         renderer->signal_edited().connect (sigc::bind (sigc::mem_fun(*this, &US2400ProtocolGUI::action_changed), column));
357
358         return renderer;
359 }
360
361 void
362 US2400ProtocolGUI::build_available_action_menu ()
363 {
364         /* build a model of all available actions (needs to be tree structured
365          * more)
366          */
367
368         available_action_model = TreeStore::create (available_action_columns);
369
370         vector<string> paths;
371         vector<string> labels;
372         vector<string> tooltips;
373         vector<string> keys;
374         vector<Glib::RefPtr<Gtk::Action> > actions;
375
376         typedef std::map<string,TreeIter> NodeMap;
377         NodeMap nodes;
378         NodeMap::iterator r;
379
380         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
381
382         vector<string>::iterator k;
383         vector<string>::iterator p;
384         vector<string>::iterator t;
385         vector<string>::iterator l;
386
387         available_action_model->clear ();
388
389         /* Because there are button bindings built in that are not
390            in the key binding map, there needs to be a way to undo
391            a profile edit.
392         */
393         TreeIter rowp;
394         TreeModel::Row parent;
395         rowp = available_action_model->append();
396         parent = *(rowp);
397         parent[available_action_columns.name] = _("Remove Binding");
398
399         /* Key aliasing */
400
401         rowp = available_action_model->append();
402         parent = *(rowp);
403         parent[available_action_columns.name] = _("Shift");
404         rowp = available_action_model->append();
405         parent = *(rowp);
406         parent[available_action_columns.name] = _("Control");
407         rowp = available_action_model->append();
408         parent = *(rowp);
409         parent[available_action_columns.name] = _("Option");
410         rowp = available_action_model->append();
411         parent = *(rowp);
412         parent[available_action_columns.name] = _("CmdAlt");
413
414         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
415
416                 TreeModel::Row row;
417                 vector<string> parts;
418
419                 parts.clear ();
420
421                 split (*p, parts, '/');
422
423                 if (parts.empty()) {
424                         continue;
425                 }
426
427                 //kinda kludgy way to avoid displaying menu items as mappable
428                 if ( parts[1] == _("Main_menu") )
429                         continue;
430                 if ( parts[1] == _("JACK") )
431                         continue;
432                 if ( parts[1] == _("redirectmenu") )
433                         continue;
434                 if ( parts[1] == _("Editor_menus") )
435                         continue;
436                 if ( parts[1] == _("RegionList") )
437                         continue;
438                 if ( parts[1] == _("ProcessorMenu") )
439                         continue;
440
441                 if ((r = nodes.find (parts[1])) == nodes.end()) {
442
443                         /* top level is missing */
444
445                         TreeIter rowp;
446                         TreeModel::Row parent;
447                         rowp = available_action_model->append();
448                         nodes[parts[1]] = rowp;
449                         parent = *(rowp);
450                         parent[available_action_columns.name] = parts[1];
451
452                         row = *(available_action_model->append (parent.children()));
453
454                 } else {
455
456                         row = *(available_action_model->append ((*r->second)->children()));
457
458                 }
459
460                 /* add this action */
461
462                 if (l->empty ()) {
463                         row[available_action_columns.name] = *t;
464                         action_map[*t] = *p;
465                 } else {
466                         row[available_action_columns.name] = *l;
467                         action_map[*l] = *p;
468                 }
469
470                 row[available_action_columns.path] = (*p);
471         }
472 }
473
474 void
475 US2400ProtocolGUI::build_function_key_editor ()
476 {
477         function_key_editor.append_column (_("Key"), function_key_columns.name);
478
479         TreeViewColumn* col;
480         CellRendererCombo* renderer;
481
482         renderer = make_action_renderer (available_action_model, function_key_columns.plain);
483         col = manage (new TreeViewColumn (_("Plain"), *renderer));
484         col->add_attribute (renderer->property_text(), function_key_columns.plain);
485         function_key_editor.append_column (*col);
486
487         renderer = make_action_renderer (available_action_model, function_key_columns.shift);
488         col = manage (new TreeViewColumn (_("Shift"), *renderer));
489         col->add_attribute (renderer->property_text(), function_key_columns.shift);
490         function_key_editor.append_column (*col);
491
492 /*
493  *      renderer = make_action_renderer (available_action_model, function_key_columns.control);
494         col = manage (new TreeViewColumn (_("Control"), *renderer));
495         col->add_attribute (renderer->property_text(), function_key_columns.control);
496         function_key_editor.append_column (*col);
497
498         renderer = make_action_renderer (available_action_model, function_key_columns.option);
499         col = manage (new TreeViewColumn (_("Option"), *renderer));
500         col->add_attribute (renderer->property_text(), function_key_columns.option);
501         function_key_editor.append_column (*col);
502
503         renderer = make_action_renderer (available_action_model, function_key_columns.cmdalt);
504         col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
505         col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
506         function_key_editor.append_column (*col);
507
508         renderer = make_action_renderer (available_action_model, function_key_columns.shiftcontrol);
509         col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
510         col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
511         function_key_editor.append_column (*col);
512 */
513
514         function_key_model = ListStore::create (function_key_columns);
515         function_key_editor.set_model (function_key_model);
516 }
517
518 void
519 US2400ProtocolGUI::refresh_function_key_editor ()
520 {
521         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
522         function_key_model->clear ();
523
524         /* now fill with data */
525
526         TreeModel::Row row;
527         DeviceProfile dp (_cp.device_profile());
528         DeviceInfo di;
529
530         for (int n = 0; n < US2400::Button::FinalGlobalButton; ++n) {
531
532                 US2400::Button::ID bid = (US2400::Button::ID) n;
533
534                 row = *(function_key_model->append());
535                 if (di.global_buttons().find (bid) == di.global_buttons().end()) {
536                         row[function_key_columns.name] = US2400::Button::id_to_name (bid);
537                 } else {
538                         row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
539                 }
540                 row[function_key_columns.id] = bid;
541
542                 Glib::RefPtr<Gtk::Action> act;
543                 string action;
544                 const string defstring = "\u2022";
545
546                 /* We only allow plain bindings for Fn keys. All others are
547                  * reserved for hard-coded actions.
548                  */
549
550                 if (bid >= US2400::Button::F1 && bid <= US2400::Button::F6) {
551
552                         action = dp.get_button_action (bid, 0);
553                         if (action.empty()) {
554                                 row[function_key_columns.plain] = defstring;
555                         } else {
556                                 if (action.find ('/') == string::npos) {
557                                         /* Probably a key alias */
558                                         row[function_key_columns.plain] = action;
559                                 } else {
560
561                                         act = ActionManager::get_action (action.c_str());
562                                         if (act) {
563                                                 row[function_key_columns.plain] = act->get_label();
564                                         } else {
565                                                 row[function_key_columns.plain] = defstring;
566                                         }
567                                 }
568                         }
569                 }
570
571                 //~ /* We only allow plain bindings for Fn keys. All others are
572                  //~ * reserved for hard-coded actions.
573                  //~ */
574 //~ 
575                 //~ if (bid >= US2400::Button::F1 && bid <= US2400::Button::F8) {
576 //~ 
577                         //~ action = dp.get_button_action (bid, US2400Protocol::MODIFIER_SHIFT);
578                         //~ if (action.empty()) {
579                                 //~ row[function_key_columns.shift] = defstring;
580                         //~ } else {
581                                 //~ if (action.find ('/') == string::npos) {
582                                         //~ /* Probably a key alias */
583                                         //~ row[function_key_columns.shift] = action;
584                                 //~ } else {
585                                         //~ act = ActionManager::get_action (action.c_str());
586                                         //~ if (act) {
587                                                 //~ row[function_key_columns.shift] = act->get_label();
588                                         //~ } else {
589                                                 //~ row[function_key_columns.shift] = defstring;
590                                         //~ }
591                                 //~ }
592                         //~ }
593                 //~ }
594
595                 //~ action = dp.get_button_action (bid, US2400Protocol::MODIFIER_CONTROL);
596                 //~ if (action.empty()) {
597                         //~ row[function_key_columns.control] = defstring;
598                 //~ } else {
599                         //~ if (action.find ('/') == string::npos) {
600                                 //~ /* Probably a key alias */
601                                 //~ row[function_key_columns.control] = action;
602                         //~ } else {
603                                 //~ act = ActionManager::get_action (action.c_str());
604                                 //~ if (act) {
605                                         //~ row[function_key_columns.control] = act->get_label();
606                                 //~ } else {
607                                         //~ row[function_key_columns.control] = defstring;
608                                 //~ }
609                         //~ }
610                 //~ }
611 //~ 
612                 //~ action = dp.get_button_action (bid, US2400Protocol::MODIFIER_OPTION);
613                 //~ if (action.empty()) {
614                         //~ row[function_key_columns.option] = defstring;
615                 //~ } else {
616                         //~ if (action.find ('/') == string::npos) {
617                                 //~ /* Probably a key alias */
618                                 //~ row[function_key_columns.option] = action;
619                         //~ } else {
620                                 //~ act = ActionManager::get_action (action.c_str());
621                                 //~ if (act) {
622                                         //~ row[function_key_columns.option] = act->get_label();
623                                 //~ } else {
624                                         //~ row[function_key_columns.option] = defstring;
625                                 //~ }
626                         //~ }
627                 //~ }
628 //~ 
629                 //~ action = dp.get_button_action (bid, US2400Protocol::MODIFIER_CMDALT);
630                 //~ if (action.empty()) {
631                         //~ row[function_key_columns.cmdalt] = defstring;
632                 //~ } else {
633                         //~ if (action.find ('/') == string::npos) {
634                                 //~ /* Probably a key alias */
635                                 //~ row[function_key_columns.cmdalt] = action;
636                         //~ } else {
637                                 //~ act = ActionManager::get_action (action.c_str());
638                                 //~ if (act) {
639                                         //~ row[function_key_columns.cmdalt] = act->get_label();
640                                 //~ } else {
641                                         //~ row[function_key_columns.cmdalt] = defstring;
642                                 //~ }
643                         //~ }
644                 //~ }
645 //~ 
646                 //~ action = dp.get_button_action (bid, (US2400Protocol::MODIFIER_SHIFT|US2400Protocol::MODIFIER_CONTROL));
647                 //~ if (action.empty()) {
648                         //~ row[function_key_columns.shiftcontrol] = defstring;
649                 //~ } else {
650                         //~ act = ActionManager::get_action (action.c_str());
651                         //~ if (act) {
652                                 //~ row[function_key_columns.shiftcontrol] = act->get_label();
653                         //~ } else {
654                                 //~ row[function_key_columns.shiftcontrol] = defstring;
655                         //~ }
656                 //~ }
657         }
658
659         function_key_editor.set_model (function_key_model);
660 }
661
662 void
663 US2400ProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
664 {
665         // Remove Binding is not in the action map but still valid
666         bool remove (false);
667         if ( text == "Remove Binding") {
668                 remove = true;
669         }
670         Gtk::TreePath path(sPath);
671         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
672
673         if (row) {
674
675                 std::map<std::string,std::string>::iterator i = action_map.find (text);
676
677                 if (i == action_map.end()) {
678                         if (!remove) {
679                                 return;
680                         }
681                 }
682                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
683
684                 if (act || remove) {
685                         /* update visible text, using string supplied by
686                            available action model so that it matches and is found
687                            within the model.
688                         */
689                         if (remove) {
690                                 Glib::ustring dot = "\u2022";
691                                 (*row).set_value (col.index(), dot);
692                         } else {
693                                 (*row).set_value (col.index(), text);
694                         }
695
696                         /* update the current DeviceProfile, using the full
697                          * path
698                          */
699
700                         int modifier;
701
702                         switch (col.index()) {
703                         case 3:
704                                 modifier = US2400Protocol::MODIFIER_SHIFT;
705                                 break;
706                         case 4:
707                                 modifier = US2400Protocol::MODIFIER_CONTROL;
708                                 break;
709                         case 5:
710                                 modifier = US2400Protocol::MODIFIER_OPTION;
711                                 break;
712                         case 6:
713                                 modifier = US2400Protocol::MODIFIER_CMDALT;
714                                 break;
715                         case 7:
716                                 modifier = (US2400Protocol::MODIFIER_SHIFT|US2400Protocol::MODIFIER_CONTROL);
717                                 break;
718                         default:
719                                 modifier = 0;
720                         }
721
722                         if (remove) {
723                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
724                         } else {
725                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
726                         }
727
728                         _ignore_profile_changed = true;
729                         _profile_combo.set_active_text ( _cp.device_profile().name() );
730                         _ignore_profile_changed = false;
731
732                 } else {
733                         std::cerr << "no such action\n";
734                 }
735         }
736 }
737
738 void
739 US2400ProtocolGUI::device_changed ()
740 {
741         if (_device_dependent_widget) {
742                 table.remove (*_device_dependent_widget);
743                 _device_dependent_widget = 0;
744         }
745
746         _device_dependent_widget = device_dependent_widget ();
747         _device_dependent_widget->show_all ();
748
749         table.attach (*_device_dependent_widget, 0, 12, device_dependent_row, device_dependent_row+1, AttachOptions(0), AttachOptions(0), 0, 0);
750 }
751
752 void
753 US2400ProtocolGUI::profile_combo_changed ()
754 {
755         if (!_ignore_profile_changed) {
756                 string profile = _profile_combo.get_active_text();
757
758                 _cp.set_profile (profile);
759
760                 refresh_function_key_editor ();
761         }
762 }
763
764 Glib::RefPtr<Gtk::ListStore>
765 US2400ProtocolGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
766 {
767         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
768         TreeModel::Row row;
769
770         row = *store->append ();
771         row[midi_port_columns.full_name] = string();
772         row[midi_port_columns.short_name] = _("Disconnected");
773
774         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
775                 row = *store->append ();
776                 row[midi_port_columns.full_name] = *p;
777                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
778                 if (pn.empty ()) {
779                         pn = (*p).substr ((*p).find (':') + 1);
780                 }
781                 row[midi_port_columns.short_name] = pn;
782         }
783
784         return store;
785 }
786
787 void
788 US2400ProtocolGUI::active_port_changed (Gtk::ComboBox* combo, boost::weak_ptr<Surface> ws, bool for_input)
789 {
790         if (ignore_active_change) {
791                 return;
792         }
793
794         boost::shared_ptr<Surface> surface = ws.lock();
795
796         if (!surface) {
797                 return;
798         }
799
800         TreeModel::iterator active = combo->get_active ();
801         string new_port = (*active)[midi_port_columns.full_name];
802
803         if (new_port.empty()) {
804                 if (for_input) {
805                         surface->port().input().disconnect_all ();
806                 } else {
807                         surface->port().output().disconnect_all ();
808                 }
809
810                 return;
811         }
812
813         if (for_input) {
814                 if (!surface->port().input().connected_to (new_port)) {
815                         surface->port().input().disconnect_all ();
816                         surface->port().input().connect (new_port);
817                 }
818         } else {
819                 if (!surface->port().output().connected_to (new_port)) {
820                         surface->port().output().disconnect_all ();
821                         surface->port().output().connect (new_port);
822                 }
823         }
824 }