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