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