1 /*
2  * Copyright (C) 2006-2007 John Anderson
3  * Copyright (C) 2011 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2012-2019 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015-2017 Len Ovens <len@ovenwerks.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <gtkmm/comboboxtext.h>
24 #include <gtkmm/box.h>
25 #include <gtkmm/spinbutton.h>
26 #include <gtkmm/table.h>
27 #include <gtkmm/treeview.h>
28 #include <gtkmm/liststore.h>
29 #include <gtkmm/treestore.h>
30 #include <gtkmm/notebook.h>
31 #include <gtkmm/cellrenderercombo.h>
32 #include <gtkmm/scale.h>
33 #include <gtkmm/alignment.h>
34 
35 #include "pbd/error.h"
36 #include "pbd/unwind.h"
37 #include "pbd/strsplit.h"
38 
39 #include "gtkmm2ext/actions.h"
40 #include "gtkmm2ext/action_model.h"
41 #include "gtkmm2ext/bindings.h"
42 #include "gtkmm2ext/gui_thread.h"
43 #include "gtkmm2ext/utils.h"
44 
45 #include "ardour/audioengine.h"
46 #include "ardour/port.h"
47 #include "ardour/rc_configuration.h"
48 
49 #include "mackie_control_protocol.h"
50 #include "device_info.h"
51 #include "gui.h"
52 #include "surface.h"
53 #include "surface_port.h"
54 
55 #include "pbd/i18n.h"
56 
57 using namespace std;
58 using namespace Gtk;
59 using namespace ArdourSurface;
60 using namespace Mackie;
61 
62 void*
get_gui() const63 MackieControlProtocol::get_gui () const
64 {
65 	if (!_gui) {
66 		const_cast<MackieControlProtocol*>(this)->build_gui ();
67 	}
68 	static_cast<Gtk::Notebook*>(_gui)->show_all();
69 	return _gui;
70 }
71 
72 void
tear_down_gui()73 MackieControlProtocol::tear_down_gui ()
74 {
75 	if (_gui) {
76 		Gtk::Widget *w = static_cast<Gtk::Widget*>(_gui)->get_parent();
77 		if (w) {
78 			w->hide();
79 			delete w;
80 		}
81 	}
82 	delete (MackieControlProtocolGUI*) _gui;
83 	_gui = 0;
84 }
85 
86 void
build_gui()87 MackieControlProtocol::build_gui ()
88 {
89 	_gui = (void *) new MackieControlProtocolGUI (*this);
90 }
91 
MackieControlProtocolGUI(MackieControlProtocol & p)92 MackieControlProtocolGUI::MackieControlProtocolGUI (MackieControlProtocol& p)
93 	: _cp (p)
94 	, table (2, 9)
95 	, action_model (ActionManager::ActionModel::instance ())
96 	, touch_sensitivity_adjustment (0, 0, 9, 1, 4)
97 	, touch_sensitivity_scale (touch_sensitivity_adjustment)
98 	, recalibrate_fader_button (_("Recalibrate Faders"))
99 	, ipmidi_base_port_adjustment (_cp.ipmidi_base(), 0, 32767, 1, 1000)
100 	, discover_button (_("Discover Mackie Devices"))
101 	, _device_dependent_widget (0)
102 	, _ignore_profile_changed (false)
103 	, ignore_active_change (false)
104 {
105 	Gtk::Label* l;
106 	Gtk::Alignment* align;
107 	int row = 0;
108 
109 	set_border_width (12);
110 
111 	table.set_row_spacings (4);
112 	table.set_col_spacings (6);
113 	table.set_border_width (12);
114 	table.set_homogeneous (false);
115 
116 	l = manage (new Gtk::Label (_("Device Type:")));
117 	l->set_alignment (1.0, 0.5);
118 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
119 	table.attach (_surface_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
120 	row++;
121 
122 	vector<string> surfaces;
123 
124 	for (std::map<std::string,DeviceInfo>::iterator i = DeviceInfo::device_info.begin(); i != DeviceInfo::device_info.end(); ++i) {
125 		surfaces.push_back (i->first);
126 	}
127 	Gtkmm2ext::set_popdown_strings (_surface_combo, surfaces);
128 	_surface_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::surface_combo_changed));
129 
130 	_cp.DeviceChanged.connect (device_change_connection, invalidator (*this), boost::bind (&MackieControlProtocolGUI::device_changed, this), gui_context());
131 
132 	/* catch future changes to connection state */
133 	ARDOUR::AudioEngine::instance()->PortRegisteredOrUnregistered.connect (_port_connections, invalidator (*this), boost::bind (&MackieControlProtocolGUI::connection_handler, this), gui_context());
134 	ARDOUR::AudioEngine::instance()->PortPrettyNameChanged.connect (_port_connections, invalidator (*this), boost::bind (&MackieControlProtocolGUI::connection_handler, this), gui_context());
135 	_cp.ConnectionChange.connect (_port_connections, invalidator (*this), boost::bind (&MackieControlProtocolGUI::connection_handler, this), gui_context());
136 
137 	ipmidi_base_port_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::ipmidi_spinner_changed));
138 
139 	/* device-dependent part */
140 
141 	device_dependent_row = row;
142 
143 	if (_device_dependent_widget) {
144 		table.remove (*_device_dependent_widget);
145 		_device_dependent_widget = 0;
146 	}
147 
148 	_device_dependent_widget = device_dependent_widget ();
149 	table.attach (*_device_dependent_widget, 0, 12, row, row+1, AttachOptions(0), AttachOptions(0), 0, 0);
150 	row++;
151 
152 	/* back to the boilerplate */
153 
154 	RadioButtonGroup rb_group = absolute_touch_mode_button.get_group();
155 	touch_move_mode_button.set_group (rb_group);
156 
157 	recalibrate_fader_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::recalibrate_faders));
158 	backlight_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::toggle_backlight));
159 
160 	touch_sensitivity_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::touch_sensitive_change));
161 	touch_sensitivity_scale.set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
162 
163 	l = manage (new Gtk::Label (_("Button click")));
164 	l->set_alignment (1.0, 0.5);
165 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
166 	align = manage (new Alignment);
167 	align->set (0.0, 0.5);
168 	align->add (relay_click_button);
169 	table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
170 	row++;
171 
172 	l = manage (new Gtk::Label (_("Backlight")));
173 	l->set_alignment (1.0, 0.5);
174 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
175 	align = manage (new Alignment);
176 	align->set (0.0, 0.5);
177 	align->add (backlight_button);
178 	table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
179 	row++;
180 
181 	l = manage (new Gtk::Label (_("Send Fader Position Only When Touched")));
182 	l->set_alignment (1.0, 0.5);
183 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
184 	align = manage (new Alignment);
185 	align->set (0.0, 0.5);
186 	align->add (absolute_touch_mode_button);
187 	table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
188 	row++;
189 
190 	l = manage (new Gtk::Label (_("Send Fader Position When Moved")));
191 	l->set_alignment (1.0, 0.5);
192 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
193 	align = manage (new Alignment);
194 	align->set (0.0, 0.5);
195 	align->add (touch_move_mode_button);
196 	table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
197 	row++;
198 
199 	l = manage (new Gtk::Label (_("Fader Touch Sense Sensitivity")));
200 	l->set_alignment (1.0, 0.5);
201 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
202 	touch_sensitivity_scale.property_digits() = 0;
203 	touch_sensitivity_scale.property_draw_value() = false;
204 	table.attach (touch_sensitivity_scale, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
205 	row++;
206 	table.attach (recalibrate_fader_button, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
207 	row++;
208 
209 
210 	table.attach (discover_button, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
211 	discover_button.signal_clicked().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::discover_clicked));
212 	row++;
213 
214 	vector<string> profiles;
215 
216 	for (std::map<std::string,DeviceProfile>::iterator i = DeviceProfile::device_profiles.begin(); i != DeviceProfile::device_profiles.end(); ++i) {
217 		cerr << "add discovered profile " << i->first << endl;
218 		profiles.push_back (i->first);
219 	}
220 	Gtkmm2ext::set_popdown_strings (_profile_combo, profiles);
221 	cerr << "set active profile from " << p.device_profile().name() << endl;
222 	_profile_combo.set_active_text (p.device_profile().name());
223 	_profile_combo.signal_changed().connect (sigc::mem_fun (*this, &MackieControlProtocolGUI::profile_combo_changed));
224 
225 	append_page (table, _("Device Setup"));
226 	table.show_all();
227 
228 	/* function key editor */
229 
230 	VBox* fkey_packer = manage (new VBox);
231 	HBox* profile_packer = manage (new HBox);
232 	HBox* observation_packer = manage (new HBox);
233 
234 	l = manage (new Gtk::Label (_("Profile/Settings:")));
235 	profile_packer->pack_start (*l, false, false);
236 	profile_packer->pack_start (_profile_combo, true, true);
237 	profile_packer->set_spacing (12);
238 	profile_packer->set_border_width (12);
239 
240 	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.")));
241 	observation_packer->pack_start (*l, false, false);
242 
243 	fkey_packer->pack_start (*profile_packer, false, false);
244 	fkey_packer->pack_start (function_key_scroller, true, true);
245 	fkey_packer->pack_start (*observation_packer, false, false);
246 	fkey_packer->set_spacing (12);
247 	function_key_scroller.property_shadow_type() = Gtk::SHADOW_NONE;
248 	function_key_scroller.add (function_key_editor);
249 	append_page (*fkey_packer, _("Function Keys"));
250 
251 	build_function_key_editor ();
252 	refresh_function_key_editor ();
253 	fkey_packer->show_all();
254 }
255 
256 void
connection_handler()257 MackieControlProtocolGUI::connection_handler ()
258 {
259 	/* ignore all changes to combobox active strings here, because we're
260 	   updating them to match a new ("external") reality - we were called
261 	   because port connections have changed.
262 	*/
263 
264 	PBD::Unwinder<bool> ici (ignore_active_change, true);
265 
266 	vector<Gtk::ComboBox*>::iterator ic;
267 	vector<Gtk::ComboBox*>::iterator oc;
268 
269 	vector<string> midi_inputs;
270 	vector<string> midi_outputs;
271 
272 	ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
273 	ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
274 
275 	for (ic = input_combos.begin(), oc = output_combos.begin(); ic != input_combos.end() && oc != output_combos.end(); ++ic, ++oc) {
276 
277 		boost::shared_ptr<Surface> surface = _cp.get_surface_by_raw_pointer ((*ic)->get_data ("surface"));
278 
279 		if (surface) {
280 			update_port_combos (midi_inputs, midi_outputs, *ic, *oc, surface);
281 		}
282 	}
283 }
284 
285 void
update_port_combos(vector<string> const & midi_inputs,vector<string> const & midi_outputs,Gtk::ComboBox * input_combo,Gtk::ComboBox * output_combo,boost::shared_ptr<Surface> surface)286 MackieControlProtocolGUI::update_port_combos (vector<string> const& midi_inputs, vector<string> const& midi_outputs,
287                                               Gtk::ComboBox* input_combo,
288                                               Gtk::ComboBox* output_combo,
289                                               boost::shared_ptr<Surface> surface)
290 {
291 	Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
292 	Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
293 	bool input_found = false;
294 	bool output_found = false;
295 	int n;
296 
297 	input_combo->set_model (input);
298 	output_combo->set_model (output);
299 
300 	Gtk::TreeModel::Children children = input->children();
301 	Gtk::TreeModel::Children::iterator i;
302 	i = children.begin();
303 	++i; /* skip "Disconnected" */
304 
305 
306 	for (n = 1;  i != children.end(); ++i, ++n) {
307 		string port_name = (*i)[midi_port_columns.full_name];
308 		if (surface->port().input().connected_to (port_name)) {
309 			input_combo->set_active (n);
310 			input_found = true;
311 			break;
312 		}
313 	}
314 
315 	if (!input_found) {
316 		input_combo->set_active (0); /* disconnected */
317 	}
318 
319 	children = output->children();
320 	i = children.begin();
321 	++i; /* skip "Disconnected" */
322 
323 	for (n = 1;  i != children.end(); ++i, ++n) {
324 		string port_name = (*i)[midi_port_columns.full_name];
325 		if (surface->port().output().connected_to (port_name)) {
326 			output_combo->set_active (n);
327 			output_found = true;
328 			break;
329 		}
330 	}
331 
332 	if (!output_found) {
333 		output_combo->set_active (0); /* disconnected */
334 	}
335 }
336 
337 Gtk::Widget*
device_dependent_widget()338 MackieControlProtocolGUI::device_dependent_widget ()
339 {
340 	Gtk::Table* dd_table;
341 	Gtk::Label* l;
342 	int row = 0;
343 
344 	uint32_t n_surfaces = 1 + _cp.device_info().extenders();
345 	uint32_t main_pos = _cp.device_info().master_position();
346 
347 	if (!_cp.device_info().uses_ipmidi()) {
348 		dd_table = Gtk::manage (new Gtk::Table (n_surfaces, 2));
349 	} else {
350 		dd_table = Gtk::manage (new Gtk::Table (1, 2));
351 	}
352 
353 	dd_table = Gtk::manage (new Gtk::Table (2, n_surfaces));
354 	dd_table->set_row_spacings (4);
355 	dd_table->set_col_spacings (6);
356 	dd_table->set_border_width (12);
357 
358 	_surface_combo.set_active_text (_cp.device_info().name());
359 
360 	vector<string> midi_inputs;
361 	vector<string> midi_outputs;
362 
363 	ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsPhysical), midi_inputs);
364 	ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsPhysical), midi_outputs);
365 
366 	input_combos.clear ();
367 	output_combos.clear ();
368 
369 	if (!_cp.device_info().uses_ipmidi()) {
370 
371 		for (uint32_t n = 0; n < n_surfaces; ++n) {
372 
373 			boost::shared_ptr<Surface> surface = _cp.nth_surface (n);
374 
375 			if (!surface) {
376 				PBD::fatal << string_compose (_("programming error: %1\n"), string_compose ("n=%1 surface not found!", n)) << endmsg;
377 				abort (); /*NOTREACHED*/
378 			}
379 
380 			Gtk::ComboBox* input_combo = manage (new Gtk::ComboBox);
381 			Gtk::ComboBox* output_combo = manage (new Gtk::ComboBox);
382 
383 			update_port_combos (midi_inputs, midi_outputs, input_combo, output_combo, surface);
384 
385 			input_combo->pack_start (midi_port_columns.short_name);
386 			input_combo->set_data ("surface", surface.get());
387 			input_combos.push_back (input_combo);
388 			output_combo->pack_start (midi_port_columns.short_name);
389 			output_combo->set_data ("surface", surface.get());
390 			output_combos.push_back (output_combo);
391 
392 			boost::weak_ptr<Surface> ws (surface);
393 			input_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &MackieControlProtocolGUI::active_port_changed), input_combo, ws, true));
394 			output_combo->signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &MackieControlProtocolGUI::active_port_changed), output_combo, ws, false));
395 
396 			string send_string;
397 			string receive_string;
398 
399 			if (n_surfaces > 1) {
400 				if (n == main_pos) {
401 					send_string = string_compose(_("Main surface at position %1 sends via:"), n + 1);
402 					receive_string = string_compose(_("Main surface at position %1 receives via:"), n + 1);
403 				} else {
404 					send_string = string_compose (_("Extender at position %1 sends via:"), n + 1);
405 					receive_string = string_compose (_("Extender at position %1 receives via:"), n + 1);
406 				}
407 			} else {
408 				send_string = _("Surface sends via:");
409 				receive_string = _("Surface receives via:");
410 			}
411 
412 			l = manage (new Gtk::Label (send_string));
413 			l->set_alignment (1.0, 0.5);
414 			dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
415 			dd_table->attach (*input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
416 			row++;
417 
418 			l = manage (new Gtk::Label (receive_string));
419 			l->set_alignment (1.0, 0.5);
420 			dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
421 			dd_table->attach (*output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
422 			row++;
423 		}
424 
425 	} else {
426 
427 		l = manage (new Gtk::Label (_("ipMIDI Port (lowest)")));
428 		l->set_alignment (1.0, 0.5);
429 
430 		Gtk::SpinButton*  ipmidi_base_port_spinner = manage (new Gtk::SpinButton (ipmidi_base_port_adjustment));
431 		dd_table->attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
432 		dd_table->attach (*ipmidi_base_port_spinner, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
433 		row++;
434 	}
435 
436 	return dd_table;
437 }
438 
439 CellRendererCombo*
make_action_renderer(Glib::RefPtr<TreeStore> model,Gtk::TreeModelColumnBase column)440 MackieControlProtocolGUI::make_action_renderer (Glib::RefPtr<TreeStore> model, Gtk::TreeModelColumnBase column)
441 {
442 	CellRendererCombo* renderer = manage (new CellRendererCombo);
443 	renderer->property_model() = model;
444 	renderer->property_editable() = true;
445 	renderer->property_text_column() = 0;
446 	renderer->property_has_entry() = false;
447 	renderer->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &MackieControlProtocolGUI::action_changed), column));
448 
449 	return renderer;
450 }
451 
452 void
build_function_key_editor()453 MackieControlProtocolGUI::build_function_key_editor ()
454 {
455 	function_key_editor.append_column (_("Key"), function_key_columns.name);
456 
457 	TreeViewColumn* col;
458 	CellRendererCombo* renderer;
459 
460 	renderer = make_action_renderer (action_model.model(), function_key_columns.plain);
461 	col = manage (new TreeViewColumn (_("Plain"), *renderer));
462 	col->add_attribute (renderer->property_text(), function_key_columns.plain);
463 	function_key_editor.append_column (*col);
464 
465 	renderer = make_action_renderer (action_model.model(), function_key_columns.shift);
466 	col = manage (new TreeViewColumn (_("Shift"), *renderer));
467 	col->add_attribute (renderer->property_text(), function_key_columns.shift);
468 	function_key_editor.append_column (*col);
469 
470 	renderer = make_action_renderer (action_model.model(), function_key_columns.control);
471 	col = manage (new TreeViewColumn (_("Control"), *renderer));
472 	col->add_attribute (renderer->property_text(), function_key_columns.control);
473 	function_key_editor.append_column (*col);
474 
475 	renderer = make_action_renderer (action_model.model(), function_key_columns.option);
476 	col = manage (new TreeViewColumn (_("Option"), *renderer));
477 	col->add_attribute (renderer->property_text(), function_key_columns.option);
478 	function_key_editor.append_column (*col);
479 
480 	renderer = make_action_renderer (action_model.model(), function_key_columns.cmdalt);
481 	col = manage (new TreeViewColumn (_("Cmd/Alt"), *renderer));
482 	col->add_attribute (renderer->property_text(), function_key_columns.cmdalt);
483 	function_key_editor.append_column (*col);
484 
485 	renderer = make_action_renderer (action_model.model(), function_key_columns.shiftcontrol);
486 	col = manage (new TreeViewColumn (_("Shift+Control"), *renderer));
487 	col->add_attribute (renderer->property_text(), function_key_columns.shiftcontrol);
488 	function_key_editor.append_column (*col);
489 
490 	function_key_model = ListStore::create (function_key_columns);
491 	function_key_editor.set_model (function_key_model);
492 }
493 
494 void
refresh_function_key_editor()495 MackieControlProtocolGUI::refresh_function_key_editor ()
496 {
497 	function_key_editor.set_model (Glib::RefPtr<TreeModel>());
498 	function_key_model->clear ();
499 
500 	/* now fill with data */
501 
502 	TreeModel::Row row;
503 	DeviceProfile dp (_cp.device_profile());
504 	DeviceInfo di;
505 
506 	for (int n = 0; n < Mackie::Button::FinalGlobalButton; ++n) {
507 
508 		Mackie::Button::ID bid = (Mackie::Button::ID) n;
509 
510 		row = *(function_key_model->append());
511 		if (di.global_buttons().find (bid) == di.global_buttons().end()) {
512 			row[function_key_columns.name] = Mackie::Button::id_to_name (bid);
513 		} else {
514 			row[function_key_columns.name] = di.get_global_button_name (bid) + "*";
515 		}
516 		row[function_key_columns.id] = bid;
517 
518 		Glib::RefPtr<Gtk::Action> act;
519 		string action;
520 		const string defstring = "\u2022";
521 
522 		/* We only allow plain bindings for Fn keys. All others are
523 		 * reserved for hard-coded actions.
524 		 */
525 
526 		if (bid >= Mackie::Button::F1 && bid <= Mackie::Button::F8) {
527 
528 			action = dp.get_button_action (bid, 0);
529 			if (action.empty()) {
530 				row[function_key_columns.plain] = defstring;
531 			} else {
532 				if (action.find ('/') == string::npos) {
533 					/* Probably a key alias */
534 					row[function_key_columns.plain] = action;
535 				} else {
536 
537 					act = ActionManager::get_action (action, false);
538 					if (act) {
539 						row[function_key_columns.plain] = act->get_label();
540 					} else {
541 						row[function_key_columns.plain] = defstring;
542 					}
543 				}
544 			}
545 		}
546 
547 		/* We only allow plain bindings for Fn keys. All others are
548 		 * reserved for hard-coded actions.
549 		 */
550 
551 		if (bid >= Mackie::Button::F1 && bid <= Mackie::Button::F8) {
552 
553 			action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_SHIFT);
554 			if (action.empty()) {
555 				row[function_key_columns.shift] = defstring;
556 			} else {
557 				if (action.find ('/') == string::npos) {
558 					/* Probably a key alias */
559 					row[function_key_columns.shift] = action;
560 				} else {
561 					act = ActionManager::get_action (action, false);
562 					if (act) {
563 						row[function_key_columns.shift] = act->get_label();
564 					} else {
565 						row[function_key_columns.shift] = defstring;
566 					}
567 				}
568 			}
569 		}
570 
571 		action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CONTROL);
572 		if (action.empty()) {
573 			row[function_key_columns.control] = defstring;
574 		} else {
575 			if (action.find ('/') == string::npos) {
576 				/* Probably a key alias */
577 				row[function_key_columns.control] = action;
578 			} else {
579 				act = ActionManager::get_action (action, false);
580 				if (act) {
581 					row[function_key_columns.control] = act->get_label();
582 				} else {
583 					row[function_key_columns.control] = defstring;
584 				}
585 			}
586 		}
587 
588 		action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_OPTION);
589 		if (action.empty()) {
590 			row[function_key_columns.option] = defstring;
591 		} else {
592 			if (action.find ('/') == string::npos) {
593 				/* Probably a key alias */
594 				row[function_key_columns.option] = action;
595 			} else {
596 				act = ActionManager::get_action (action, false);
597 				if (act) {
598 					row[function_key_columns.option] = act->get_label();
599 				} else {
600 					row[function_key_columns.option] = defstring;
601 				}
602 			}
603 		}
604 
605 		action = dp.get_button_action (bid, MackieControlProtocol::MODIFIER_CMDALT);
606 		if (action.empty()) {
607 			row[function_key_columns.cmdalt] = defstring;
608 		} else {
609 			if (action.find ('/') == string::npos) {
610 				/* Probably a key alias */
611 				row[function_key_columns.cmdalt] = action;
612 			} else {
613 				act = ActionManager::get_action (action, false);
614 				if (act) {
615 					row[function_key_columns.cmdalt] = act->get_label();
616 				} else {
617 					row[function_key_columns.cmdalt] = defstring;
618 				}
619 			}
620 		}
621 
622 		action = dp.get_button_action (bid, (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL));
623 		if (action.empty()) {
624 			row[function_key_columns.shiftcontrol] = defstring;
625 		} else {
626 			act = ActionManager::get_action (action, false);
627 			if (act) {
628 				row[function_key_columns.shiftcontrol] = act->get_label();
629 			} else {
630 				row[function_key_columns.shiftcontrol] = defstring;
631 			}
632 		}
633 	}
634 
635 	function_key_editor.set_model (function_key_model);
636 }
637 
638 void
action_changed(const Glib::ustring & sPath,const TreeModel::iterator & iter,TreeModelColumnBase col)639 MackieControlProtocolGUI::action_changed (const Glib::ustring &sPath, const TreeModel::iterator & iter, TreeModelColumnBase col)
640 {
641 	string action_path = (*iter)[action_model.columns().path];
642 
643 	// Remove Binding is not in the action map but still valid
644 
645 	bool remove = false;
646 
647 	if (action_path == "Remove Binding") {
648 		remove = true;
649 	}
650 
651 	Gtk::TreePath path(sPath);
652 	Gtk::TreeModel::iterator row = function_key_model->get_iter(path);
653 
654 	if (row) {
655 
656 		Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (action_path, false);
657 
658 		if (!act) {
659 			cerr << action_path << " not found in action map\n";
660 			if (!remove) {
661 				return;
662 			}
663 		}
664 
665 		if (act || remove) {
666 			/* update visible text, using string supplied by
667 			   available action model so that it matches and is found
668 			   within the model.
669 			*/
670 			if (remove) {
671 				Glib::ustring dot = "\u2022";
672 				(*row).set_value (col.index(), dot);
673 			} else {
674 				(*row).set_value (col.index(), act->get_label());
675 			}
676 
677 			/* update the current DeviceProfile, using the full
678 			 * path
679 			 */
680 
681 			int modifier;
682 
683 			switch (col.index()) {
684 			case 3:
685 				modifier = MackieControlProtocol::MODIFIER_SHIFT;
686 				break;
687 			case 4:
688 				modifier = MackieControlProtocol::MODIFIER_CONTROL;
689 				break;
690 			case 5:
691 				modifier = MackieControlProtocol::MODIFIER_OPTION;
692 				break;
693 			case 6:
694 				modifier = MackieControlProtocol::MODIFIER_CMDALT;
695 				break;
696 			case 7:
697 				modifier = (MackieControlProtocol::MODIFIER_SHIFT|MackieControlProtocol::MODIFIER_CONTROL);
698 				break;
699 			default:
700 				modifier = 0;
701 			}
702 
703 			if (remove) {
704 				_cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, "");
705 			} else {
706 				_cp.device_profile().set_button_action ((*row)[function_key_columns.id], modifier, action_path);
707 			}
708 
709 			_ignore_profile_changed = true;
710 			_profile_combo.set_active_text ( _cp.device_profile().name() );
711 			_ignore_profile_changed = false;
712 
713 		} else {
714 			std::cerr << "no such action\n";
715 		}
716 	}
717 }
718 
719 void
surface_combo_changed()720 MackieControlProtocolGUI::surface_combo_changed ()
721 {
722 	_cp.set_device (_surface_combo.get_active_text(), false);
723 }
724 
725 void
device_changed()726 MackieControlProtocolGUI::device_changed ()
727 {
728 	if (_device_dependent_widget) {
729 		table.remove (*_device_dependent_widget);
730 		_device_dependent_widget = 0;
731 	}
732 
733 	_device_dependent_widget = device_dependent_widget ();
734 	_device_dependent_widget->show_all ();
735 
736 	table.attach (*_device_dependent_widget, 0, 12, device_dependent_row, device_dependent_row+1, AttachOptions(0), AttachOptions(0), 0, 0);
737 }
738 
739 void
profile_combo_changed()740 MackieControlProtocolGUI::profile_combo_changed ()
741 {
742 	if (!_ignore_profile_changed) {
743 		string profile = _profile_combo.get_active_text();
744 
745 		_cp.set_profile (profile);
746 
747 		refresh_function_key_editor ();
748 	}
749 }
750 
751 void
ipmidi_spinner_changed()752 MackieControlProtocolGUI::ipmidi_spinner_changed ()
753 {
754 	_cp.set_ipmidi_base ((int16_t) lrintf (ipmidi_base_port_adjustment.get_value()));
755 }
756 
757 void
discover_clicked()758 MackieControlProtocolGUI::discover_clicked ()
759 {
760 	/* this should help to get things started */
761 	_cp.ping_devices ();
762 }
763 
764 void
recalibrate_faders()765 MackieControlProtocolGUI::recalibrate_faders ()
766 {
767 	_cp.recalibrate_faders ();
768 }
769 
770 void
toggle_backlight()771 MackieControlProtocolGUI::toggle_backlight ()
772 {
773 	_cp.toggle_backlight ();
774 }
775 
776 void
touch_sensitive_change()777 MackieControlProtocolGUI::touch_sensitive_change ()
778 {
779 	int sensitivity = (int) touch_sensitivity_adjustment.get_value ();
780 	_cp.set_touch_sensitivity (sensitivity);
781 }
782 
783 Glib::RefPtr<Gtk::ListStore>
build_midi_port_list(vector<string> const & ports,bool for_input)784 MackieControlProtocolGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
785 {
786 	Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
787 	TreeModel::Row row;
788 
789 	row = *store->append ();
790 	row[midi_port_columns.full_name] = string();
791 	row[midi_port_columns.short_name] = _("Disconnected");
792 
793 	for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
794 		row = *store->append ();
795 		row[midi_port_columns.full_name] = *p;
796 		std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
797 		if (pn.empty ()) {
798 			pn = (*p).substr ((*p).find (':') + 1);
799 		}
800 		row[midi_port_columns.short_name] = pn;
801 	}
802 
803 	return store;
804 }
805 
806 void
active_port_changed(Gtk::ComboBox * combo,boost::weak_ptr<Surface> ws,bool for_input)807 MackieControlProtocolGUI::active_port_changed (Gtk::ComboBox* combo, boost::weak_ptr<Surface> ws, bool for_input)
808 {
809 	if (ignore_active_change) {
810 		return;
811 	}
812 
813 	boost::shared_ptr<Surface> surface = ws.lock();
814 
815 	if (!surface) {
816 		return;
817 	}
818 
819 	TreeModel::iterator active = combo->get_active ();
820 	string new_port = (*active)[midi_port_columns.full_name];
821 
822 	if (new_port.empty()) {
823 		if (for_input) {
824 			surface->port().input().disconnect_all ();
825 		} else {
826 			surface->port().output().disconnect_all ();
827 		}
828 
829 		return;
830 	}
831 
832 	if (for_input) {
833 		if (!surface->port().input().connected_to (new_port)) {
834 			surface->port().input().disconnect_all ();
835 			surface->port().input().connect (new_port);
836 		}
837 	} else {
838 		if (!surface->port().output().connected_to (new_port)) {
839 			surface->port().output().disconnect_all ();
840 			surface->port().output().connect (new_port);
841 		}
842 	}
843 }
844