3ef7923286b0313a9ddf5424135a91347992c8d2
[ardour.git] / libs / surfaces / us2400 / gui.cc
1 /*
2  * Copyright (C) 2017-2018 Ben Loftis <ben@harrisonconsoles.com>
3  * Copyright (C) 2018-2019 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <gtkmm/comboboxtext.h>
22 #include <gtkmm/box.h>
23 #include <gtkmm/spinbutton.h>
24 #include <gtkmm/table.h>
25 #include <gtkmm/treeview.h>
26 #include <gtkmm/liststore.h>
27 #include <gtkmm/treestore.h>
28 #include <gtkmm/notebook.h>
29 #include <gtkmm/cellrenderercombo.h>
30 #include <gtkmm/scale.h>
31 #include <gtkmm/alignment.h>
32
33 #include "pbd/error.h"
34 #include "pbd/unwind.h"
35 #include "pbd/strsplit.h"
36 #include "pbd/stacktrace.h"
37
38 #include "gtkmm2ext/actions.h"
39 #include "gtkmm2ext/action_model.h"
40 #include "gtkmm2ext/bindings.h"
41 #include "gtkmm2ext/gui_thread.h"
42 #include "gtkmm2ext/utils.h"
43
44 #include "ardour/audioengine.h"
45 #include "ardour/port.h"
46 #include "ardour/rc_configuration.h"
47
48 #include "us2400_control_protocol.h"
49 #include "device_info.h"
50 #include "gui.h"
51 #include "surface.h"
52 #include "surface_port.h"
53
54 #include "pbd/i18n.h"
55
56 using namespace std;
57 using namespace Gtk;
58 using namespace ArdourSurface;
59 using namespace US2400;
60
61 void*
62 US2400Protocol::get_gui () const
63 {
64         if (!_gui) {
65                 const_cast<US2400Protocol*>(this)->build_gui ();
66         }
67         static_cast<Gtk::Notebook*>(_gui)->show_all();
68         return _gui;
69 }
70
71 void
72 US2400Protocol::tear_down_gui ()
73 {
74         if (_gui) {
75                 Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
76                 if (w) {
77                         w->hide();
78                         delete w;
79                 }
80         }
81         delete (US2400ProtocolGUI*) _gui;
82         _gui = 0;
83 }
84
85 void
86 US2400Protocol::build_gui ()
87 {
88         _gui = (void *) new US2400ProtocolGUI (*this);
89 }
90
91 US2400ProtocolGUI::US2400ProtocolGUI (US2400Protocol& p)
92         : _cp (p)
93         , table (2, 9)
94         , action_model (ActionManager::ActionModel::instance ())
95         , _device_dependent_widget (0)
96         , _ignore_profile_changed (false)
97         , ignore_active_change (false)
98 {
99         Gtk::Label* l;
100         int row = 0;
101
102         set_border_width (12);
103
104         table.set_row_spacings (4);
105         table.set_col_spacings (6);
106         table.set_border_width (12);
107         table.set_homogeneous (false);
108
109         _cp.DeviceChanged.connect (device_change_connection, invalidator (*this), boost::bind (&US2400ProtocolGUI::device_changed, this), gui_context());
110         _cp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&US2400ProtocolGUI::connection_handler, this), gui_context());
111
112         /* device-dependent part */
113
114         device_dependent_row = row;
115
116         if (_device_dependent_widget) {
117                 table.remove (*_device_dependent_widget);
118                 _device_dependent_widget = 0;
119         }
120
121         _device_dependent_widget = device_dependent_widget ();
122         table.attach (*_device_dependent_widget, 0, 12, row, row+1, AttachOptions(0), AttachOptions(0), 0, 0);
123         row++;
124
125         /* back to the boilerplate */
126
127         vector<string> profiles;
128
129         for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
130                 cerr << "add discovered profile " << i->first << endl;
131                 profiles.push_back (i->first);
132         }
133         Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
134         cerr << "set active profile from " << p.device_profile().name() << endl;
135         _profile_combo.set_active_text (p.device_profile().name());
136         _profile_combo.signal_changed().connect (sigc::mem_fun (*this, &US2400ProtocolGUI::profile_combo_changed));
137
138         append_page (table, _("Device Setup"));
139         table.show_all();
140
141         /* function key editor */
142
143         VBox* fkey_packer = manage (new VBox);
144         HBox* profile_packer = manage (new HBox);
145         HBox* observation_packer = manage (new HBox);
146
147         l = manage (new Gtk::Label (_("Profile/Settings:")));
148         profile_packer->pack_start (*l, false, false);
149         profile_packer->pack_start (_profile_combo, true, true);
150         profile_packer->set_spacing (12);
151         profile_packer->set_border_width (12);
152
153         fkey_packer->pack_start (*profile_packer, false, false);
154         fkey_packer->pack_start (function_key_scroller, true, true);
155         fkey_packer->pack_start (*observation_packer, false, false);
156         fkey_packer->set_spacing (12);
157         function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
158         function_key_scroller.add (function_key_editor);
159         append_page (*fkey_packer, _("Function Keys"));
160
161         build_function_key_editor ();
162         refresh_function_key_editor ();
163         fkey_packer->show_all();
164 }
165
166 void
167 US2400ProtocolGUI::connection_handler ()
168 {
169         /* ignore all changes to combobox active strings here, because we're
170            updating them to match a new ("external") reality - we were called
171            because port connections have changed.
172         */
173
174         PBD::Unwinder<bool> ici (ignore_active_change, true);
175
176         vector<Gtk::ComboBox*>::iterator ic;
177         vector<Gtk::ComboBox*>::iterator oc;
178
179         vector<string> midi_inputs;
180         vector<string> midi_outputs;
181
182         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
183         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
184
185         for (ic = input_combos.begin(), oc = output_combos.begin(); ic != input_combos.end() && oc != output_combos.end(); ++ic, ++oc) {
186
187                 boost::shared_ptr<Surface> surface = _cp.get_surface_by_raw_pointer ((*ic)->get_data ("surface"));
188
189                 if (surface) {
190                         update_port_combos (midi_inputs, midi_outputs, *ic, *oc, surface);
191                 }
192         }
193 }
194
195 void
196 US2400ProtocolGUI::update_port_combos (vector<string> const& midi_inputs, vector<string> const& midi_outputs,
197                                        Gtk::ComboBox* input_combo,
198                                        Gtk::ComboBox* output_combo,
199                                        boost::shared_ptr<Surface> surface)
200 {
201         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
202         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
203         bool input_found = false;
204         bool output_found = false;
205         int n;
206
207         input_combo->set_model (input);
208         output_combo->set_model (output);
209
210         Gtk::TreeModel::Children children = input->children();
211         Gtk::TreeModel::Children::iterator i;
212         i = children.begin();
213         ++i; /* skip "Disconnected" */
214
215
216         for (n = 1;  i != children.end(); ++i, ++n) {
217                 string port_name = (*i)[midi_port_columns.full_name];
218                 if (surface->port().input().connected_to (port_name)) {
219                         input_combo->set_active (n);
220                         input_found = true;
221                         break;
222                 }
223         }
224
225         if (!input_found) {
226                 input_combo->set_active (0); /* disconnected */
227         }
228
229         children = output->children();
230         i = children.begin();
231         ++i; /* skip "Disconnected" */
232
233         for (n = 1;  i != children.end(); ++i, ++n) {
234                 string port_name = (*i)[midi_port_columns.full_name];
235                 if (surface->port().output().connected_to (port_name)) {
236                         output_combo->set_active (n);
237                         output_found = true;
238                         break;
239                 }
240         }
241
242         if (!output_found) {
243                 output_combo->set_active (0); /* disconnected */
244         }
245 }
246
247 Gtk::Widget*
248 US2400ProtocolGUI::device_dependent_widget ()
249 {
250         Gtk::Table* dd_table;
251         Gtk::Label* l;
252         int row = 0;
253
254         uint32_t n_surfaces = 1 + _cp.device_info().extenders();
255
256         dd_table = Gtk::manage (new Gtk::Table (2, n_surfaces));
257         dd_table->set_row_spacings (4);
258         dd_table->set_col_spacings (6);
259         dd_table->set_border_width (12);
260
261         vector<string> midi_inputs;
262         vector<string> midi_outputs;
263
264         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsPhysical), midi_inputs);
265         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsPhysical), midi_outputs);
266
267         input_combos.clear ();
268         output_combos.clear ();
269
270         int portcount = n_surfaces;
271
272         for (int32_t n = 0; n < portcount; ++n) {
273
274                 boost::shared_ptr<Surface> surface = _cp.nth_surface (n);
275
276                 if (!surface) {
277                         PBD::fatal << string_compose (_("programming error: %1\n"), string_compose ("n=%1 surface not found!", n)) << endmsg;
278                         /*NOTREACHED*/
279                 }
280
281                 Gtk::ComboBox* input_combo = manage (new Gtk::ComboBox);
282                 Gtk::ComboBox* output_combo = manage (new Gtk::ComboBox);
283
284                 update_port_combos (midi_inputs, midi_outputs, input_combo, output_combo, surface);
285
286                 input_combo->pack_start (midi_port_columns.short_name);
287                 input_combo->set_data ("surface", surface.get());
288                 input_combos.push_back (input_combo);
289                 output_combo->pack_start (midi_port_columns.short_name);
290                 output_combo->set_data ("surface", surface.get());
291                 output_combos.push_back (output_combo);
292
293                 boost::weak_ptr<Surface> ws (surface);
294                 input_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &US2400ProtocolGUI::active_port_changed), input_combo, ws, true));
295                 output_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &US2400ProtocolGUI::active_port_changed), output_combo, ws, false));
296
297                 string send_string;
298                 string receive_string;
299
300                 //port 1,2,3 are faders & pan knobs. like mackie MCU
301                 //port 4 is the joystick
302                 //port 5 sends "chan" knobs (24 of them)
303                 //port 6 --- ???  (could be send to knobs... ?)
304
305                 send_string = string_compose(_("US-2400 send port #%1 (faders %2 to %3):"), n + 1, n*8+1, n*8+8);
306                 receive_string = string_compose(_("US-2400 receive port #%1 (faders %2 to %3):"), n + 1, n*8+1, n*8+8);
307                 if (n==3) {
308                         send_string = string_compose(_("US-2400 send port #%1 (joystick):"), n + 1);
309                         receive_string = string_compose(_("US-2400 receive port #%1 (joystick):"), n + 1);
310                 }
311
312                 l = manage (new Gtk::Label (send_string));
313                 l->set_alignment (1.0, 0.5);
314                 dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
315                 dd_table->attach (*input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
316                 row++;
317
318                 l = manage (new Gtk::Label (receive_string));
319                 l->set_alignment (1.0, 0.5);
320                 dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
321                 dd_table->attach (*output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
322                 row++;
323         }
324
325         row++;
326
327         l = manage (new Gtk::Label ("US-2400 Port #5 is reserved for use as a generic USB device. (click the CHAN button to activate)"));
328         l->set_alignment (1.0, 0.5);
329         dd_table->attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
330         row++;
331         row++;
332
333         l = manage (new Gtk::Label ("US-2400 Port #6 is unused."));
334         l->set_alignment (1.0, 0.5);
335         dd_table->attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
336         row++;
337         row++;
338
339         l = manage (new Gtk::Label ("NOTE:  you must select mode 4 on the US-2400 unit."));
340         l->set_alignment (1.0, 0.5);
341         dd_table->attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
342         row++;
343
344
345         return dd_table;
346 }
347
348 CellRendererCombo*
349 US2400ProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
350 {
351         CellRendererCombo* renderer = manage (new CellRendererCombo);
352         renderer->property_model() = model;
353         renderer->property_editable() = true;
354         renderer->property_text_column() = 0;
355         renderer->property_has_entry() = false;
356         renderer->signal_edited().connect (sigc::bind (sigc::mem_fun(*this, &US2400ProtocolGUI::action_changed), column));
357
358         return renderer;
359 }
360
361 void
362 US2400ProtocolGUI::build_function_key_editor ()
363 {
364         function_key_editor.append_column (_("Key"), function_key_columns.name);
365
366         TreeViewColumn* col;
367         CellRendererCombo* renderer;
368
369         renderer = make_action_renderer (action_model.model(), function_key_columns.plain);
370         col = manage (new TreeViewColumn (_("Plain"), *renderer));
371         col->add_attribute (renderer->property_text(), function_key_columns.plain);
372         function_key_editor.append_column (*col);
373
374         renderer = make_action_renderer (action_model.model(), function_key_columns.shift);
375         col = manage (new TreeViewColumn (_("Shift"), *renderer));
376         col->add_attribute (renderer->property_text(), function_key_columns.shift);
377         function_key_editor.append_column (*col);
378
379         function_key_model = ListStore::create (function_key_columns);
380         function_key_editor.set_model (function_key_model);
381 }
382
383 void
384 US2400ProtocolGUI::refresh_function_key_editor ()
385 {
386         function_key_editor.set_model (Glib::RefPtr<TreeModel>());
387         function_key_model->clear ();
388
389         /* now fill with data */
390
391         TreeModel::Row row;
392         DeviceProfile dp (_cp.device_profile());
393         DeviceInfo di;
394
395         for (int n = 0; n < US2400::Button::FinalGlobalButton; ++n) {
396
397                 US2400::Button::ID bid = (US2400::Button::ID) n;
398
399                 row = *(function_key_model->append());
400                 if (di.global_buttons().find (bid) == di.global_buttons().end()) {
401                         row[function_key_columns.name] = US2400::Button::id_to_name (bid);
402                 } else {
403                         row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
404                 }
405                 row[function_key_columns.id] = bid;
406
407                 Glib::RefPtr<Gtk::Action> act;
408                 string action;
409                 const string defstring = "\u2022";
410
411                 /* We only allow plain bindings for Fn keys. All others are
412                  * reserved for hard-coded actions. */
413
414                 if (bid >= US2400::Button::F1 && bid <= US2400::Button::F6) {
415
416                         action = dp.get_button_action (bid, 0);
417                         if (action.empty()) {
418                                 row[function_key_columns.plain] = defstring;
419                         } else {
420                                 if (action.find ('/') == string::npos) {
421                                         /* Probably a key alias */
422                                         row[function_key_columns.plain] = action;
423                                 } else {
424
425                                         act = ActionManager::get_action (action, false);
426                                         if (act) {
427                                                 row[function_key_columns.plain] = act->get_label();
428                                         } else {
429                                                 row[function_key_columns.plain] = defstring;
430                                         }
431                                 }
432                         }
433                 }
434         }
435
436         function_key_editor.set_model (function_key_model);
437 }
438
439 void
440 US2400ProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
441 {
442         // Remove Binding is not in the action map but still valid
443         bool remove (false);
444         if (text == "Remove Binding") {
445                 remove = true;
446         }
447         Gtk::TreePath path(sPath);
448         Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
449
450         if (row) {
451
452                 std::map<std::string,std::string>::iterator i = action_map.find (text);
453
454                 if (i == action_map.end()) {
455                         if (!remove) {
456                                 return;
457                         }
458                 }
459                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second, false);
460
461                 if (act || remove) {
462                         /* update visible text, using string supplied by
463                          * available action model so that it matches and is found
464                          * within the model.
465                          */
466                         if (remove) {
467                                 Glib::ustring dot = "\u2022";
468                                 (*row).set_value (col.index(), dot);
469                         } else {
470                                 (*row).set_value (col.index(), text);
471                         }
472
473                         /* update the current DeviceProfile, using the full
474                          * path
475                          */
476
477                         int modifier;
478
479                         switch (col.index()) {
480                         case 3:
481                                 modifier = US2400Protocol::MODIFIER_SHIFT;
482                                 break;
483                         case 4:
484                                 modifier = US2400Protocol::MODIFIER_CONTROL;
485                                 break;
486                         case 5:
487                                 modifier = US2400Protocol::MODIFIER_OPTION;
488                                 break;
489                         case 6:
490                                 modifier = US2400Protocol::MODIFIER_CMDALT;
491                                 break;
492                         case 7:
493                                 modifier = (US2400Protocol::MODIFIER_SHIFT|US2400Protocol::MODIFIER_CONTROL);
494                                 break;
495                         default:
496                                 modifier = 0;
497                         }
498
499                         if (remove) {
500                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
501                         } else {
502                                 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
503                         }
504
505                         _ignore_profile_changed = true;
506                         _profile_combo.set_active_text (_cp.device_profile().name());
507                         _ignore_profile_changed = false;
508
509                 } else {
510                         std::cerr << "no such action\n";
511                 }
512         }
513 }
514
515 void
516 US2400ProtocolGUI::device_changed ()
517 {
518         if (_device_dependent_widget) {
519                 table.remove (*_device_dependent_widget);
520                 _device_dependent_widget = 0;
521         }
522
523         _device_dependent_widget = device_dependent_widget ();
524         _device_dependent_widget->show_all ();
525
526         table.attach (*_device_dependent_widget, 0, 12, device_dependent_row, device_dependent_row+1, AttachOptions(0), AttachOptions(0), 0, 0);
527 }
528
529 void
530 US2400ProtocolGUI::profile_combo_changed ()
531 {
532         if (!_ignore_profile_changed) {
533                 string profile = _profile_combo.get_active_text();
534
535                 _cp.set_profile (profile);
536
537                 refresh_function_key_editor ();
538         }
539 }
540
541 Glib::RefPtr<Gtk::ListStore>
542 US2400ProtocolGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
543 {
544         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
545         TreeModel::Row row;
546
547         row = *store->append ();
548         row[midi_port_columns.full_name] = string();
549         row[midi_port_columns.short_name] = _("Disconnected");
550
551         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
552                 row = *store->append ();
553                 row[midi_port_columns.full_name] = *p;
554                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
555                 if (pn.empty ()) {
556                         pn = (*p).substr ((*p).find (':') + 1);
557                 }
558                 row[midi_port_columns.short_name] = pn;
559         }
560
561         return store;
562 }
563
564 void
565 US2400ProtocolGUI::active_port_changed (Gtk::ComboBox* combo, boost::weak_ptr<Surface> ws, bool for_input)
566 {
567         if (ignore_active_change) {
568                 return;
569         }
570
571         boost::shared_ptr<Surface> surface = ws.lock();
572
573         if (!surface) {
574                 return;
575         }
576
577         TreeModel::iterator active = combo->get_active ();
578         string new_port = (*active)[midi_port_columns.full_name];
579
580         if (new_port.empty()) {
581                 if (for_input) {
582                         surface->port().input().disconnect_all ();
583                 } else {
584                         surface->port().output().disconnect_all ();
585                 }
586
587                 return;
588         }
589
590         if (for_input) {
591                 if (!surface->port().input().connected_to (new_port)) {
592                         surface->port().input().disconnect_all ();
593                         surface->port().input().connect (new_port);
594                 }
595         } else {
596                 if (!surface->port().output().connected_to (new_port)) {
597                         surface->port().output().disconnect_all ();
598                         surface->port().output().connect (new_port);
599                 }
600         }
601 }