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