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