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