implement backlight, fader touch sensitivity and recalibrate fader functions for...
[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/strsplit.h"
32
33 #include "gtkmm2ext/utils.h"
34 #include "gtkmm2ext/actions.h"
35
36 #include "ardour/rc_configuration.h"
37
38 #include "mackie_control_protocol.h"
39 #include "device_info.h"
40 #include "gui.h"
41
42 #include "i18n.h"
43
44 using namespace std;
45 using namespace Gtk;
46 using namespace ArdourSurface;
47 using namespace Mackie;
48
49 void*
50 MackieControlProtocol::get_gui () const
51 {
52         if (!_gui) {
53                 const_cast<MackieControlProtocol*>(this)->build_gui ();
54         }
55         static_cast<Gtk::Notebook*>(_gui)->show_all();
56         return _gui;
57 }
58
59 void
60 MackieControlProtocol::tear_down_gui ()
61 {
62         if (_gui) {
63                 Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
64                 if (w) {
65                         w->hide();
66                         delete w;
67                 }
68         }
69         delete (MackieControlProtocolGUI*) _gui;
70         _gui = 0;
71 }
72
73 void
74 MackieControlProtocol::build_gui ()
75 {
76         _gui = (void *) new MackieControlProtocolGUI (*this);
77 }
78
79 MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p)
80         : _cp (p)
81         , touch_sensitivity_adjustment (0, 0, 9, 1, 4)
82         , touch_sensitivity_scale (touch_sensitivity_adjustment)
83         , recalibrate_fader_button (_("Recalibrate Faders"))
84         , ipmidi_base_port_adjustment (_cp.ipmidi_base(), 0, 32767, 1, 1000)
85         , ipmidi_base_port_spinner (ipmidi_base_port_adjustment)
86         , discover_button (_("Discover Mackie Devices"))
87 {
88         Gtk::Label* l;
89         Gtk::Alignment* align;
90
91         set_border_width (12);
92
93         Gtk::Table* table = Gtk::manage (new Gtk::Table (2, 9));
94         table->set_row_spacings (4);
95         table->set_col_spacings (6);
96         table->set_border_width (12);
97         l = manage (new Gtk::Label (_("Device Type:")));
98         l->set_alignment (1.0, 0.5);
99         table->attach (*l, 0, 1, 0, 1, AttachOptions(FILL|EXPAND), AttachOptions(0));
100         table->attach (_surface_combo, 1, 2, 0, 1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 20);
101
102         vector<string> surfaces;
103         
104         for (std::map<std::string,DeviceInfo>::iterator i = DeviceInfo::device_info.begin(); i != DeviceInfo::device_info.end(); ++i) {
105                 surfaces.push_back (i->first);
106         }
107         Gtkmm2ext::set_popdown_strings (_surface_combo, surfaces);
108         _surface_combo.set_active_text (p.device_info().name());
109         _surface_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::surface_combo_changed));
110
111         RadioButtonGroup rb_group = absolute_touch_mode_button.get_group();
112         touch_move_mode_button.set_group (rb_group);
113
114         recalibrate_fader_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::recalibrate_faders));
115         backlight_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::toggle_backlight));
116
117         touch_sensitivity_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::touch_sensitive_change));
118         touch_sensitivity_scale.set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
119         
120         l = manage (new Gtk::Label (_("Button click")));
121         l->set_alignment (1.0, 0.5);
122         table->attach (*l, 0, 1, 1, 2, AttachOptions(FILL|EXPAND), AttachOptions (0));
123         align = manage (new Alignment);
124         align->set (0.0, 0.5);
125         align->add (relay_click_button);
126         table->attach (*align, 1, 2, 1, 2, AttachOptions(FILL|EXPAND), AttachOptions (0));
127         l = manage (new Gtk::Label (_("Backlight")));
128         l->set_alignment (1.0, 0.5);
129         table->attach (*l, 0, 1, 2, 3, AttachOptions(FILL|EXPAND), AttachOptions (0));
130         align = manage (new Alignment);
131         align->set (0.0, 0.5);
132         align->add (backlight_button);
133         table->attach (*align, 1, 2, 2, 3, AttachOptions(FILL|EXPAND), AttachOptions (0));
134         l = manage (new Gtk::Label (_("Send Fader Position Only When Touched")));
135         l->set_alignment (1.0, 0.5);
136         table->attach (*l, 0, 1, 3, 4, AttachOptions(FILL|EXPAND), AttachOptions (0));
137         align = manage (new Alignment);
138         align->set (0.0, 0.5);
139         align->add (absolute_touch_mode_button);
140         table->attach (*align, 1, 2, 3, 4, AttachOptions(FILL|EXPAND), AttachOptions (0));
141         l = manage (new Gtk::Label (_("Send Fader Position When Moved")));
142         l->set_alignment (1.0, 0.5);
143         table->attach (*l, 0, 1, 4, 5, AttachOptions(FILL|EXPAND), AttachOptions (0));
144         align = manage (new Alignment);
145         align->set (0.0, 0.5);
146         align->add (touch_move_mode_button);
147         table->attach (*align, 1, 2, 4, 5, AttachOptions(FILL|EXPAND), AttachOptions (0));
148         l = manage (new Gtk::Label (_("Fader Touch Sense Sensitivity")));
149         l->set_alignment (1.0, 0.5);
150         table->attach (*l, 0, 1, 5, 6, AttachOptions(FILL|EXPAND), AttachOptions (0));
151         touch_sensitivity_scale.property_digits() = 0;
152         touch_sensitivity_scale.property_draw_value() = false;
153         table->attach (touch_sensitivity_scale, 1, 2, 5, 6, AttachOptions(FILL|EXPAND), AttachOptions (0));
154         table->attach (recalibrate_fader_button, 1, 2, 6, 7, AttachOptions(FILL|EXPAND), AttachOptions (0));
155
156         l = manage (new Gtk::Label (_("ipMIDI Port (lowest)")));
157         l->set_alignment (1.0, 0.5);
158         table->attach (*l, 0, 1, 7, 8, AttachOptions(FILL|EXPAND), AttachOptions (0));
159         table->attach (ipmidi_base_port_spinner, 1, 2, 7, 8, AttachOptions(FILL|EXPAND), AttachOptions (0));
160
161         ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
162         ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
163         
164         table->attach (discover_button, 1, 2, 8, 9, AttachOptions(FILL|EXPAND), AttachOptions (0));
165         discover_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::discover_clicked));
166
167         vector<string> profiles;
168         
169         profiles.push_back ("default");
170
171         for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
172                 profiles.push_back (i->first);
173         }
174         Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
175         _profile_combo.set_active_text (p.device_profile().name());
176         _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::profile_combo_changed));
177
178         append_page (*table, _("Device Setup"));
179         table->show_all();
180
181         /* function key editor */
182
183         VBox* fkey_packer = manage (new VBox);
184         HBox* profile_packer = manage (new HBox);
185         HBox* observation_packer = manage (new HBox);
186         
187         l = manage (new Gtk::Label (_("Profile/Settings:")));
188         profile_packer->pack_start (*l, false, false);
189         profile_packer->pack_start (_profile_combo, true, true);
190         profile_packer->set_spacing (12);
191         profile_packer->set_border_width (12);
192         
193         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.")));
194         observation_packer->pack_start (*l, false, false);
195
196         fkey_packer->pack_start (*profile_packer, false, false);
197         fkey_packer->pack_start (function_key_scroller, true, true);
198         fkey_packer->pack_start (*observation_packer, false, false);
199         fkey_packer->set_spacing (12);
200         function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
201         function_key_scroller.add (function_key_editor);
202         append_page (*fkey_packer, _("Function Keys"));
203
204         build_available_action_menu ();
205         build_function_key_editor ();
206         refresh_function_key_editor ();
207         fkey_packer->show_all();
208 }
209
210 CellRendererCombo*
211 MackieControlProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
212 {
213         CellRendererCombo* renderer = manage (new CellRendererCombo);
214         renderer->property_model() = model;
215         renderer->property_editable() = true;
216         renderer->property_text_column() = 0;
217         renderer->property_has_entry() = false;
218         renderer->signal_edited().connect (sigc::bind (sigc::mem_fun(*this, &MackieControlProtocolGUI::action_changed), column));
219
220         return renderer;
221 }
222
223 void
224 MackieControlProtocolGUI::build_available_action_menu ()
225 {
226         /* build a model of all available actions (needs to be tree structured
227          * more) 
228          */
229
230         available_action_model = TreeStore::create (available_action_columns);
231
232         vector<string> paths;
233         vector<string> labels;
234         vector<string> tooltips;
235         vector<string> keys;
236         vector<AccelKey> bindings;
237         typedef std::map<string,TreeIter> NodeMap;
238         NodeMap nodes;
239         NodeMap::iterator r;
240
241         ActionManager::get_all_actions (labels, paths, tooltips, keys, bindings);
242
243         vector<string>::iterator k;
244         vector<string>::iterator p;
245         vector<string>::iterator t;
246         vector<string>::iterator l;
247
248         available_action_model->clear ();
249
250         /* Because there are button bindings built in that are not
251         in the key binding map, there needs to be a way to undo
252         a profile edit. */
253         TreeIter rowp;
254         TreeModel::Row parent;
255         rowp = available_action_model->append();
256         parent = *(rowp);
257         parent[available_action_columns.name] = _("Remove Binding");
258
259         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
260
261                 TreeModel::Row row;
262                 vector<string> parts;
263
264                 parts.clear ();
265
266                 split (*p, parts, '/');
267
268                 if (parts.empty()) {
269                         continue;
270                 }
271
272                 //kinda kludgy way to avoid displaying menu items as mappable
273                 if ( parts[1] == _("Main_menu") )
274                         continue;
275                 if ( parts[1] == _("JACK") )
276                         continue;
277                 if ( parts[1] == _("redirectmenu") )
278                         continue;
279                 if ( parts[1] == _("Editor_menus") )
280                         continue;
281                 if ( parts[1] == _("RegionList") )
282                         continue;
283                 if ( parts[1] == _("ProcessorMenu") )
284                         continue;
285
286                 if ((r = nodes.find (parts[1])) == nodes.end()) {
287
288                         /* top level is missing */
289
290                         TreeIter rowp;
291                         TreeModel::Row parent;
292                         rowp = available_action_model->append();
293                         nodes[parts[1]] = rowp;
294                         parent = *(rowp);
295                         parent[available_action_columns.name] = parts[1];
296
297                         row = *(available_action_model->append (parent.children()));
298
299                 } else {
300
301                         row = *(available_action_model->append ((*r->second)->children()));
302
303                 }
304
305                 /* add this action */
306
307                 if (l->empty ()) {
308                         row[available_action_columns.name] = *t;
309                         action_map[*t] = *p;
310                 } else {
311                         row[available_action_columns.name] = *l;
312                         action_map[*l] = *p;
313                 }
314
315                 row[available_action_columns.path] = (*p);
316         }
317 }
318
319 void
320 MackieControlProtocolGUI::build_function_key_editor ()
321 {
322         function_key_editor.append_column (_("Key"), function_key_columns.name);
323
324         TreeViewColumn* col;
325         CellRendererCombo* renderer;
326
327         renderer = make_action_renderer (available_action_model, function_key_columns.plain);
328         col = manage (new TreeViewColumn (_("Plain"), *renderer));
329         col->add_attribute (renderer->property_text(), function_key_columns.plain);
330         function_key_editor.append_column (*col);
331         
332         renderer = make_action_renderer (available_action_model, function_key_columns.shift);
333         col = manage (new TreeViewColumn (_("Shift"), *renderer));
334         col->add_attribute (renderer->property_text(), function_key_columns.shift);
335         function_key_editor.append_column (*col);
336
337         renderer = make_action_renderer (available_action_model, function_key_columns.control);
338         col = manage (new TreeViewColumn (_("Control"), *renderer));
339         col->add_attribute (renderer->property_text(), function_key_columns.control);
340         function_key_editor.append_column (*col);
341
342         renderer = make_action_renderer (available_action_model, function_key_columns.option);
343         col = manage (new TreeViewColumn (_("Option"), *renderer));
344         col->add_attribute (renderer->property_text(), function_key_columns.option);
345         function_key_editor.append_column (*col);
346
347         renderer = make_action_renderer (available_action_model, function_key_columns.cmdalt);
348         col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
349         col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
350         function_key_editor.append_column (*col);
351
352         renderer = make_action_renderer (available_action_model, function_key_columns.shiftcontrol);
353         col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
354         col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
355         function_key_editor.append_column (*col);
356
357         function_key_model = ListStore::create (function_key_columns);
358         function_key_editor.set_model (function_key_model);
359 }
360
361 void
362 MackieControlProtocolGUI::refresh_function_key_editor ()
363 {
364         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
365         function_key_model->clear ();
366
367         /* now fill with data */
368
369         TreeModel::Row row;
370         DeviceProfile dp (_cp.device_profile());
371         DeviceInfo di;
372
373         for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
374
375                 Mackie::Button::ID bid = (Mackie::Button::ID) n;
376
377                 row = *(function_key_model->append());
378                 if (di.global_buttons().find (bid) == di.global_buttons().end()) {
379                         row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
380                 } else {
381                         row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
382                 }
383                 row[function_key_columns.id] = bid;
384
385                 Glib::RefPtr<Gtk::Action> act;
386                 string action;
387                 const string defstring = "\u2022";
388
389                 action = dp.get_button_action (bid, 0);
390                 if (action.empty()) {
391                         row[function_key_columns.plain] = defstring;
392                 } else {
393                         act = ActionManager::get_action (action.c_str());
394                         if (act) {
395                                 row[function_key_columns.plain] = act->get_label();
396                         } else {
397                                 row[function_key_columns.plain] = defstring;
398                         }
399                 }
400
401                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
402                 if (action.empty()) {
403                         row[function_key_columns.control] = defstring;
404                 } else {
405                         act = ActionManager::get_action (action.c_str());
406                         if (act) {
407                                 row[function_key_columns.control] = act->get_label();
408                         } else {
409                                 row[function_key_columns.control] = defstring;
410                         }
411                 }
412
413                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
414                 if (action.empty()) {
415                         row[function_key_columns.shift] = defstring;
416                 } else {
417                         act = ActionManager::get_action (action.c_str());
418                         if (act) {
419                                 row[function_key_columns.shift] = act->get_label();
420                         } else {
421                                 row[function_key_columns.shift] = defstring;
422                         }
423                 }
424
425                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_OPTION);
426                 if (action.empty()) {
427                         row[function_key_columns.option] = defstring;
428                 } else {
429                         act = ActionManager::get_action (action.c_str());
430                         if (act) {
431                                 row[function_key_columns.option] = act->get_label();
432                         } else {
433                                 row[function_key_columns.option] = defstring;
434                         }
435                 }
436
437                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CMDALT);
438                 if (action.empty()) {
439                         row[function_key_columns.cmdalt] = defstring;
440                 } else {
441                         act = ActionManager::get_action (action.c_str());
442                         if (act) {
443                                 row[function_key_columns.cmdalt] = act->get_label();
444                         } else {
445                                 row[function_key_columns.cmdalt] = defstring;
446                         }
447                 }
448
449                 action = dp.get_button_action (bid, (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL));
450                 if (action.empty()) {
451                         row[function_key_columns.shiftcontrol] = defstring;
452                 } else {
453                         act = ActionManager::get_action (action.c_str());
454                         if (act) {
455                                 row[function_key_columns.shiftcontrol] = act->get_label();
456                         } else {
457                                 row[function_key_columns.shiftcontrol] = defstring;
458                         }
459                 }
460         }
461
462         function_key_editor.set_model (function_key_model);
463 }
464
465 void 
466 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
467 {
468         // Remove Binding is not in the action map but still valid
469         bool remove (false);
470         if ( text == "Remove Binding") {
471                 remove = true;
472         }
473         Gtk::TreePath path(sPath);
474         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
475
476         if (row) {
477
478                 std::map<std::string,std::string>::iterator i = action_map.find (text);
479                 
480                 if (i == action_map.end()) {
481                         if (!remove) {
482                                 return;
483                         }
484                 }
485                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
486
487                 if (act || remove) {
488                         /* update visible text, using string supplied by
489                            available action model so that it matches and is found
490                            within the model.
491                         */
492                         if (remove) {
493                                 Glib::ustring dot = "\u2022";
494                                 (*row).set_value (col.index(), dot);
495                         } else {
496                                 (*row).set_value (col.index(), text);
497                         }
498
499                         /* update the current DeviceProfile, using the full
500                          * path
501                          */
502
503                         int modifier;
504
505                         switch (col.index()) {
506                         case 3:
507                                 modifier = MackieControlProtocol::MODIFIER_SHIFT;
508                                 break;
509                         case 4:
510                                 modifier = MackieControlProtocol::MODIFIER_CONTROL;
511                                 break;
512                         case 5:
513                                 modifier = MackieControlProtocol::MODIFIER_OPTION;
514                                 break;
515                         case 6:
516                                 modifier = MackieControlProtocol::MODIFIER_CMDALT;
517                                 break;
518                         case 7:
519                                 modifier = (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL);
520                                 break;
521                         default:
522                                 modifier = 0;
523                         }
524
525                         if (remove) {
526                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
527                         } else {
528                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
529                         }
530
531                 } else {
532                         std::cerr << "no such action\n";
533                 }
534         }
535 }
536
537 void
538 MackieControlProtocolGUI::surface_combo_changed ()
539 {
540         _cp.not_session_load();
541         _cp.set_device (_surface_combo.get_active_text());
542
543         /* update ipMIDI field */
544
545         ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
546 }
547
548 void
549 MackieControlProtocolGUI::profile_combo_changed ()
550 {
551         string profile = _profile_combo.get_active_text();
552
553         _cp.set_profile (profile);
554
555         refresh_function_key_editor ();
556 }
557
558 void
559 MackieControlProtocolGUI::ipmidi_spinner_changed ()
560 {
561         _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_spinner.get_value()));
562 }
563
564 void
565 MackieControlProtocolGUI::discover_clicked ()
566 {
567         /* this should help to get things started */
568         _cp.midi_connectivity_established ();
569 }
570
571 void
572 MackieControlProtocolGUI::recalibrate_faders ()
573 {
574         _cp.recalibrate_faders ();
575 }
576
577 void
578 MackieControlProtocolGUI::toggle_backlight ()
579 {
580         _cp.toggle_backlight ();
581 }
582
583 void
584 MackieControlProtocolGUI::touch_sensitive_change ()
585 {
586         int sensitivity = (int) touch_sensitivity_adjustment.get_value ();
587         _cp.set_touch_sensitivity (sensitivity);
588 }