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>
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.
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.
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.
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>
33 #include "pbd/error.h"
34 #include "pbd/unwind.h"
35 #include "pbd/strsplit.h"
36 #include "pbd/stacktrace.h"
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"
44 #include "ardour/audioengine.h"
45 #include "ardour/port.h"
46 #include "ardour/rc_configuration.h"
48 #include "us2400_control_protocol.h"
49 #include "device_info.h"
52 #include "surface_port.h"
58 using namespace ArdourSurface;
59 using namespace US2400;
62 US2400Protocol::get_gui () const
65 const_cast<US2400Protocol*>(this)->build_gui ();
67 static_cast<Gtk::Notebook*>(_gui)->show_all();
72 US2400Protocol::tear_down_gui ()
75 Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
81 delete (US2400ProtocolGUI*) _gui;
86 US2400Protocol::build_gui ()
88 _gui = (void *) new US2400ProtocolGUI (*this);
91 US2400ProtocolGUI::US2400ProtocolGUI (US2400Protocol& p)
94 , action_model (ActionManager::ActionModel::instance ())
95 , _device_dependent_widget (0)
96 , _ignore_profile_changed (false)
97 , ignore_active_change (false)
102 set_border_width (12);
104 table.set_row_spacings (4);
105 table.set_col_spacings (6);
106 table.set_border_width (12);
107 table.set_homogeneous (false);
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());
112 /* device-dependent part */
114 device_dependent_row = row;
116 if (_device_dependent_widget) {
117 table.remove (*_device_dependent_widget);
118 _device_dependent_widget = 0;
121 _device_dependent_widget = device_dependent_widget ();
122 table.attach (*_device_dependent_widget, 0, 12, row, row+1, AttachOptions(0), AttachOptions(0), 0, 0);
125 /* back to the boilerplate */
127 vector<string> profiles;
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);
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));
138 append_page (table, _("Device Setup"));
141 /* function key editor */
143 VBox* fkey_packer = manage (new VBox);
144 HBox* profile_packer = manage (new HBox);
145 HBox* observation_packer = manage (new HBox);
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);
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"));
161 build_function_key_editor ();
162 refresh_function_key_editor ();
163 fkey_packer->show_all();
167 US2400ProtocolGUI::connection_handler ()
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.
174 PBD::Unwinder<bool> ici (ignore_active_change, true);
176 vector<Gtk::ComboBox*>::iterator ic;
177 vector<Gtk::ComboBox*>::iterator oc;
179 vector<string> midi_inputs;
180 vector<string> midi_outputs;
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);
185 for (ic = input_combos.begin(), oc = output_combos.begin(); ic != input_combos.end() && oc != output_combos.end(); ++ic, ++oc) {
187 boost::shared_ptr<Surface> surface = _cp.get_surface_by_raw_pointer ((*ic)->get_data ("surface"));
190 update_port_combos (midi_inputs, midi_outputs, *ic, *oc, surface);
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)
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;
207 input_combo->set_model (input);
208 output_combo->set_model (output);
210 Gtk::TreeModel::Children children = input->children();
211 Gtk::TreeModel::Children::iterator i;
212 i = children.begin();
213 ++i; /* skip "Disconnected" */
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);
226 input_combo->set_active (0); /* disconnected */
229 children = output->children();
230 i = children.begin();
231 ++i; /* skip "Disconnected" */
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);
243 output_combo->set_active (0); /* disconnected */
248 US2400ProtocolGUI::device_dependent_widget ()
250 Gtk::Table* dd_table;
254 uint32_t n_surfaces = 1 + _cp.device_info().extenders();
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);
261 vector<string> midi_inputs;
262 vector<string> midi_outputs;
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);
267 input_combos.clear ();
268 output_combos.clear ();
270 int portcount = n_surfaces;
272 for (int32_t n = 0; n < portcount; ++n) {
274 boost::shared_ptr<Surface> surface = _cp.nth_surface (n);
277 PBD::fatal << string_compose (_("programming error: %1\n"), string_compose ("n=%1 surface not found!", n)) << endmsg;
281 Gtk::ComboBox* input_combo = manage (new Gtk::ComboBox);
282 Gtk::ComboBox* output_combo = manage (new Gtk::ComboBox);
284 update_port_combos (midi_inputs, midi_outputs, input_combo, output_combo, surface);
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);
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));
298 string receive_string;
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... ?)
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);
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);
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);
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);
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));
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));
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));
349 US2400ProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
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));
362 US2400ProtocolGUI::build_function_key_editor ()
364 function_key_editor.append_column (_("Key"), function_key_columns.name);
367 CellRendererCombo* renderer;
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);
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);
379 function_key_model = ListStore::create (function_key_columns);
380 function_key_editor.set_model (function_key_model);
384 US2400ProtocolGUI::refresh_function_key_editor ()
386 function_key_editor.set_model (Glib::RefPtr<TreeModel>());
387 function_key_model->clear ();
389 /* now fill with data */
392 DeviceProfile dp (_cp.device_profile());
395 for (int n = 0; n < US2400::Button::FinalGlobalButton; ++n) {
397 US2400::Button::ID bid = (US2400::Button::ID) n;
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);
403 row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
405 row[function_key_columns.id] = bid;
407 Glib::RefPtr<Gtk::Action> act;
409 const string defstring = "\u2022";
411 /* We only allow plain bindings for Fn keys. All others are
412 * reserved for hard-coded actions. */
414 if (bid >= US2400::Button::F1 && bid <= US2400::Button::F6) {
416 action = dp.get_button_action (bid, 0);
417 if (action.empty()) {
418 row[function_key_columns.plain] = defstring;
420 if (action.find ('/') == string::npos) {
421 /* Probably a key alias */
422 row[function_key_columns.plain] = action;
425 act = ActionManager::get_action (action, false);
427 row[function_key_columns.plain] = act->get_label();
429 row[function_key_columns.plain] = defstring;
436 function_key_editor.set_model (function_key_model);
440 US2400ProtocolGUI::action_changed (const Glib::ustring &sPath, const Glib::ustring &text, TreeModelColumnBase col)
442 // Remove Binding is not in the action map but still valid
444 if (text == "Remove Binding") {
447 Gtk::TreePath path(sPath);
448 Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
452 std::map<std::string,std::string>::iterator i = action_map.find (text);
454 if (i == action_map.end()) {
459 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (i->second, false);
462 /* update visible text, using string supplied by
463 * available action model so that it matches and is found
467 Glib::ustring dot = "\u2022";
468 (*row).set_value (col.index(), dot);
470 (*row).set_value (col.index(), text);
473 /* update the current DeviceProfile, using the full
479 switch (col.index()) {
481 modifier = US2400Protocol::MODIFIER_SHIFT;
484 modifier = US2400Protocol::MODIFIER_CONTROL;
487 modifier = US2400Protocol::MODIFIER_OPTION;
490 modifier = US2400Protocol::MODIFIER_CMDALT;
493 modifier = (US2400Protocol::MODIFIER_SHIFT|US2400Protocol::MODIFIER_CONTROL);
500 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
502 _cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, i->second);
505 _ignore_profile_changed = true;
506 _profile_combo.set_active_text (_cp.device_profile().name());
507 _ignore_profile_changed = false;
510 std::cerr << "no such action\n";
516 US2400ProtocolGUI::device_changed ()
518 if (_device_dependent_widget) {
519 table.remove (*_device_dependent_widget);
520 _device_dependent_widget = 0;
523 _device_dependent_widget = device_dependent_widget ();
524 _device_dependent_widget->show_all ();
526 table.attach (*_device_dependent_widget, 0, 12, device_dependent_row, device_dependent_row+1, AttachOptions(0), AttachOptions(0), 0, 0);
530 US2400ProtocolGUI::profile_combo_changed ()
532 if (!_ignore_profile_changed) {
533 string profile = _profile_combo.get_active_text();
535 _cp.set_profile (profile);
537 refresh_function_key_editor ();
541 Glib::RefPtr<Gtk::ListStore>
542 US2400ProtocolGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
544 Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
547 row = *store->append ();
548 row[midi_port_columns.full_name] = string();
549 row[midi_port_columns.short_name] = _("Disconnected");
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);
556 pn = (*p).substr ((*p).find (':') + 1);
558 row[midi_port_columns.short_name] = pn;
565 US2400ProtocolGUI::active_port_changed (Gtk::ComboBox* combo, boost::weak_ptr<Surface> ws, bool for_input)
567 if (ignore_active_change) {
571 boost::shared_ptr<Surface> surface = ws.lock();
577 TreeModel::iterator active = combo->get_active ();
578 string new_port = (*active)[midi_port_columns.full_name];
580 if (new_port.empty()) {
582 surface->port().input().disconnect_all ();
584 surface->port().output().disconnect_all ();
591 if (!surface->port().input().connected_to (new_port)) {
592 surface->port().input().disconnect_all ();
593 surface->port().input().connect (new_port);
596 if (!surface->port().output().connected_to (new_port)) {
597 surface->port().output().disconnect_all ();
598 surface->port().output().connect (new_port);