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