d9d473728fc8c9133aee80656ccb84a6ce87e73d
[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         /* Key aliasing */
260
261         rowp = available_action_model->append();
262         parent = *(rowp);
263         parent[available_action_columns.name] = _("Shift");
264         rowp = available_action_model->append();
265         parent = *(rowp);
266         parent[available_action_columns.name] = _("Control");
267         rowp = available_action_model->append();
268         parent = *(rowp);
269         parent[available_action_columns.name] = _("Option");
270         rowp = available_action_model->append();
271         parent = *(rowp);
272         parent[available_action_columns.name] = _("CmdAlt");
273
274         
275         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
276
277                 TreeModel::Row row;
278                 vector<string> parts;
279
280                 parts.clear ();
281
282                 split (*p, parts, '/');
283
284                 if (parts.empty()) {
285                         continue;
286                 }
287
288                 //kinda kludgy way to avoid displaying menu items as mappable
289                 if ( parts[1] == _("Main_menu") )
290                         continue;
291                 if ( parts[1] == _("JACK") )
292                         continue;
293                 if ( parts[1] == _("redirectmenu") )
294                         continue;
295                 if ( parts[1] == _("Editor_menus") )
296                         continue;
297                 if ( parts[1] == _("RegionList") )
298                         continue;
299                 if ( parts[1] == _("ProcessorMenu") )
300                         continue;
301
302                 if ((r = nodes.find (parts[1])) == nodes.end()) {
303
304                         /* top level is missing */
305
306                         TreeIter rowp;
307                         TreeModel::Row parent;
308                         rowp = available_action_model->append();
309                         nodes[parts[1]] = rowp;
310                         parent = *(rowp);
311                         parent[available_action_columns.name] = parts[1];
312
313                         row = *(available_action_model->append (parent.children()));
314
315                 } else {
316
317                         row = *(available_action_model->append ((*r->second)->children()));
318
319                 }
320
321                 /* add this action */
322
323                 if (l->empty ()) {
324                         row[available_action_columns.name] = *t;
325                         action_map[*t] = *p;
326                 } else {
327                         row[available_action_columns.name] = *l;
328                         action_map[*l] = *p;
329                 }
330
331                 row[available_action_columns.path] = (*p);
332         }
333 }
334
335 void
336 MackieControlProtocolGUI::build_function_key_editor ()
337 {
338         function_key_editor.append_column (_("Key"), function_key_columns.name);
339
340         TreeViewColumn* col;
341         CellRendererCombo* renderer;
342
343         renderer = make_action_renderer (available_action_model, function_key_columns.plain);
344         col = manage (new TreeViewColumn (_("Plain"), *renderer));
345         col->add_attribute (renderer->property_text(), function_key_columns.plain);
346         function_key_editor.append_column (*col);
347         
348         renderer = make_action_renderer (available_action_model, function_key_columns.shift);
349         col = manage (new TreeViewColumn (_("Shift"), *renderer));
350         col->add_attribute (renderer->property_text(), function_key_columns.shift);
351         function_key_editor.append_column (*col);
352
353         renderer = make_action_renderer (available_action_model, function_key_columns.control);
354         col = manage (new TreeViewColumn (_("Control"), *renderer));
355         col->add_attribute (renderer->property_text(), function_key_columns.control);
356         function_key_editor.append_column (*col);
357
358         renderer = make_action_renderer (available_action_model, function_key_columns.option);
359         col = manage (new TreeViewColumn (_("Option"), *renderer));
360         col->add_attribute (renderer->property_text(), function_key_columns.option);
361         function_key_editor.append_column (*col);
362
363         renderer = make_action_renderer (available_action_model, function_key_columns.cmdalt);
364         col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
365         col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
366         function_key_editor.append_column (*col);
367
368         renderer = make_action_renderer (available_action_model, function_key_columns.shiftcontrol);
369         col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
370         col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
371         function_key_editor.append_column (*col);
372
373         function_key_model = ListStore::create (function_key_columns);
374         function_key_editor.set_model (function_key_model);
375 }
376
377 void
378 MackieControlProtocolGUI::refresh_function_key_editor ()
379 {
380         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
381         function_key_model->clear ();
382
383         /* now fill with data */
384
385         TreeModel::Row row;
386         DeviceProfile dp (_cp.device_profile());
387         DeviceInfo di;
388
389         for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
390
391                 Mackie::Button::ID bid = (Mackie::Button::ID) n;
392
393                 row = *(function_key_model->append());
394                 if (di.global_buttons().find (bid) == di.global_buttons().end()) {
395                         row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
396                 } else {
397                         row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
398                 }
399                 row[function_key_columns.id] = bid;
400
401                 Glib::RefPtr<Gtk::Action> act;
402                 string action;
403                 const string defstring = "\u2022";
404
405                 action = dp.get_button_action (bid, 0);
406                 if (action.empty()) {
407                         row[function_key_columns.plain] = defstring;
408                 } else {
409                         if (action.find ('/') == string::npos) {
410                                 /* Probably a key alias */
411                                 row[function_key_columns.plain] = action;
412                         } else {
413                                 
414                                 act = ActionManager::get_action (action.c_str());
415                                 if (act) {
416                                         row[function_key_columns.plain] = act->get_label();
417                                 } else {
418                                         row[function_key_columns.plain] = defstring;
419                                 }
420                         }
421                 }
422
423                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
424                 if (action.empty()) {
425                         row[function_key_columns.control] = defstring;
426                 } else {
427                         if (action.find ('/') == string::npos) {
428                                 /* Probably a key alias */
429                                 row[function_key_columns.control] = action;
430                         } else {
431                                 act = ActionManager::get_action (action.c_str());
432                                 if (act) {
433                                         row[function_key_columns.control] = act->get_label();
434                                 } else {
435                                         row[function_key_columns.control] = defstring;
436                                 }
437                         }
438                 }
439
440                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
441                 if (action.empty()) {
442                         row[function_key_columns.shift] = defstring;
443                 } else {
444                         if (action.find ('/') == string::npos) {
445                                 /* Probably a key alias */
446                                 row[function_key_columns.shift] = action;
447                         } else {
448                                 act = ActionManager::get_action (action.c_str());
449                                 if (act) {
450                                         row[function_key_columns.shift] = act->get_label();
451                                 } else {
452                                         row[function_key_columns.shift] = defstring;
453                                 }
454                         }
455                 }
456
457                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_OPTION);
458                 if (action.empty()) {
459                         row[function_key_columns.option] = defstring;
460                 } else {
461                         if (action.find ('/') == string::npos) {
462                                 /* Probably a key alias */
463                                 row[function_key_columns.option] = action;
464                         } else {
465                                 act = ActionManager::get_action (action.c_str());
466                                 if (act) {
467                                         row[function_key_columns.option] = act->get_label();
468                                 } else {
469                                         row[function_key_columns.option] = defstring;
470                                 }
471                         }
472                 }
473
474                 action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CMDALT);
475                 if (action.empty()) {
476                         row[function_key_columns.cmdalt] = defstring;
477                 } else {
478                         if (action.find ('/') == string::npos) {
479                                 /* Probably a key alias */
480                                 row[function_key_columns.cmdalt] = action;
481                         } else {
482                                 act = ActionManager::get_action (action.c_str());
483                                 if (act) {
484                                         row[function_key_columns.cmdalt] = act->get_label();
485                                 } else {
486                                         row[function_key_columns.cmdalt] = defstring;
487                                 }
488                         }
489                 }
490
491                 action = dp.get_button_action (bid, (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL));
492                 if (action.empty()) {
493                         row[function_key_columns.shiftcontrol] = defstring;
494                 } else {
495                         act = ActionManager::get_action (action.c_str());
496                         if (act) {
497                                 row[function_key_columns.shiftcontrol] = act->get_label();
498                         } else {
499                                 row[function_key_columns.shiftcontrol] = defstring;
500                         }
501                 }
502         }
503
504         function_key_editor.set_model (function_key_model);
505 }
506
507 void 
508 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
509 {
510         // Remove Binding is not in the action map but still valid
511         bool remove (false);
512         if ( text == "Remove Binding") {
513                 remove = true;
514         }
515         Gtk::TreePath path(sPath);
516         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
517
518         if (row) {
519
520                 std::map<std::string,std::string>::iterator i = action_map.find (text);
521                 
522                 if (i == action_map.end()) {
523                         if (!remove) {
524                                 return;
525                         }
526                 }
527                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second.c_str());
528
529                 if (act || remove) {
530                         /* update visible text, using string supplied by
531                            available action model so that it matches and is found
532                            within the model.
533                         */
534                         if (remove) {
535                                 Glib::ustring dot = "\u2022";
536                                 (*row).set_value (col.index(), dot);
537                         } else {
538                                 (*row).set_value (col.index(), text);
539                         }
540
541                         /* update the current DeviceProfile, using the full
542                          * path
543                          */
544
545                         int modifier;
546
547                         switch (col.index()) {
548                         case 3:
549                                 modifier = MackieControlProtocol::MODIFIER_SHIFT;
550                                 break;
551                         case 4:
552                                 modifier = MackieControlProtocol::MODIFIER_CONTROL;
553                                 break;
554                         case 5:
555                                 modifier = MackieControlProtocol::MODIFIER_OPTION;
556                                 break;
557                         case 6:
558                                 modifier = MackieControlProtocol::MODIFIER_CMDALT;
559                                 break;
560                         case 7:
561                                 modifier = (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL);
562                                 break;
563                         default:
564                                 modifier = 0;
565                         }
566
567                         if (remove) {
568                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
569                         } else {
570                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
571                         }
572
573                 } else {
574                         std::cerr << "no such action\n";
575                 }
576         }
577 }
578
579 void
580 MackieControlProtocolGUI::surface_combo_changed ()
581 {
582         _cp.not_session_load();
583         _cp.set_device (_surface_combo.get_active_text());
584
585         /* update ipMIDI field */
586
587         ipmidi_base_port_spinner.set_sensitive (_cp.device_info().uses_ipmidi());
588 }
589
590 void
591 MackieControlProtocolGUI::profile_combo_changed ()
592 {
593         string profile = _profile_combo.get_active_text();
594
595         _cp.set_profile (profile);
596
597         refresh_function_key_editor ();
598 }
599
600 void
601 MackieControlProtocolGUI::ipmidi_spinner_changed ()
602 {
603         _cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_spinner.get_value()));
604 }
605
606 void
607 MackieControlProtocolGUI::discover_clicked ()
608 {
609         /* this should help to get things started */
610         _cp.midi_connectivity_established ();
611 }
612
613 void
614 MackieControlProtocolGUI::recalibrate_faders ()
615 {
616         _cp.recalibrate_faders ();
617 }
618
619 void
620 MackieControlProtocolGUI::toggle_backlight ()
621 {
622         _cp.toggle_backlight ();
623 }
624
625 void
626 MackieControlProtocolGUI::touch_sensitive_change ()
627 {
628         int sensitivity = (int) touch_sensitivity_adjustment.get_value ();
629         _cp.set_touch_sensitivity (sensitivity);
630 }