1 /*
2  * Copyright (C) 2018 Jan Lentfer <jan.lentfer@web.de>
3  * Copyright (C) 2018 Térence Clastres <t.clastres@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <gtkmm/alignment.h>
21 #include <gtkmm/label.h>
22 #include <gtkmm/liststore.h>
23 
24 #include "pbd/unwind.h"
25 #include "pbd/strsplit.h"
26 #include "pbd/file_utils.h"
27 
28 #include "gtkmm2ext/bindings.h"
29 #include "gtkmm2ext/gui_thread.h"
30 #include "gtkmm2ext/utils.h"
31 
32 #include "ardour/audioengine.h"
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/parameter_descriptor.h"
35 #include "ardour/debug.h"
36 
37 #include "launch_control_xl.h"
38 #include "gui.h"
39 
40 #include "pbd/i18n.h"
41 
42 using namespace PBD;
43 using namespace ARDOUR;
44 using namespace ArdourSurface;
45 using namespace std;
46 using namespace Gtk;
47 using namespace Gtkmm2ext;
48 
49 void*
get_gui() const50 LaunchControlXL::get_gui () const
51 {
52 	if (!gui) {
53 		const_cast<LaunchControlXL*>(this)->build_gui ();
54 	}
55 	static_cast<Gtk::VBox*>(gui)->show_all();
56 	return gui;
57 }
58 
59 void
tear_down_gui()60 LaunchControlXL::tear_down_gui ()
61 {
62 	if (gui) {
63 		Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
64 		if (w) {
65 			w->hide();
66 			delete w;
67 		}
68 	}
69 	delete gui;
70 	gui = 0;
71 }
72 
73 void
build_gui()74 LaunchControlXL::build_gui ()
75 {
76 	gui = new LCXLGUI (*this);
77 }
78 
79 /*--------------------*/
80 
LCXLGUI(LaunchControlXL & p)81 LCXLGUI::LCXLGUI (LaunchControlXL& p)
82 	: lcxl (p)
83 	, table (2, 5)
84 	, action_table (5, 4)
85 	, ignore_active_change (false)
86 {
87 	set_border_width (12);
88 
89 	table.set_row_spacings (4);
90 	table.set_col_spacings (6);
91 	table.set_border_width (12);
92 	table.set_homogeneous (false);
93 
94 	std::string data_file_path;
95 	string name = "launch_control_xl.png";
96 	Searchpath spath(ARDOUR::ardour_data_search_path());
97 	spath.add_subdirectory_to_paths ("icons");
98 	find_file (spath, name, data_file_path);
99 	if (!data_file_path.empty()) {
100 		image.set (data_file_path);
101 		hpacker.pack_start (image, false, false);
102 	}
103 
104 	Gtk::Label* l;
105 	Gtk::Alignment* align;
106 	int row = 0;
107 
108 	input_combo.pack_start (midi_port_columns.short_name);
109 	output_combo.pack_start (midi_port_columns.short_name);
110 
111 	input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &LCXLGUI::active_port_changed), &input_combo, true));
112 	output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &LCXLGUI::active_port_changed), &output_combo, false));
113 
114 	l = manage (new Gtk::Label);
115 	l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
116 	l->set_alignment (1.0, 0.5);
117 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
118 	table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
119 	row++;
120 
121 	l = manage (new Gtk::Label);
122 	l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
123 	l->set_alignment (1.0, 0.5);
124 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
125 	table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
126 	row++;
127 
128 	/* User Settings */
129 #ifdef MIXBUS32C
130 	l = manage (new Gtk::Label (_("Control sends 7-12 in Mixer Mode")));
131 	l->set_alignment (1.0, 0.5);
132 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
133 	align = manage (new Alignment);
134 	align->set (0.0, 0.5);
135 	align->add (ctrllowersends_button);
136 	table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0),0,0);
137 	ctrllowersends_button.set_active (lcxl.ctrllowersends());
138 	ctrllowersends_button.signal_toggled().connect (sigc::mem_fun (*this, &LCXLGUI::toggle_ctrllowersends));
139 	row++;
140 #endif
141 
142 	l = manage (new Gtk::Label (_("Fader 8 Master")));
143 	l->set_alignment (1.0, 0.5);
144 	table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0));
145 	align = manage (new Alignment);
146 	align->set (0.0, 0.5);
147 	align->add (fader8master_button);
148 	table.attach (*align, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions (0),0,0);
149 	fader8master_button.set_active (lcxl.fader8master());
150 	fader8master_button.signal_toggled().connect (sigc::mem_fun (*this, &LCXLGUI::toggle_fader8master));
151 	row++;
152 
153 	hpacker.pack_start (table, true, true);
154 
155 	set_spacing (12);
156 
157 	pack_start (hpacker, false, false);
158 
159 	/* update the port connection combos */
160 
161 	update_port_combos ();
162 
163 	/* catch future changes to connection state */
164 
165 	ARDOUR::AudioEngine::instance()->PortRegisteredOrUnregistered.connect (_port_connections, invalidator (*this), boost::bind (&LCXLGUI::connection_handler, this), gui_context());
166 	ARDOUR::AudioEngine::instance()->PortPrettyNameChanged.connect (_port_connections, invalidator (*this), boost::bind (&LCXLGUI::connection_handler, this), gui_context());
167 	lcxl.ConnectionChange.connect (_port_connections, invalidator (*this), boost::bind (&LCXLGUI::connection_handler, this), gui_context());
168 }
169 
~LCXLGUI()170 LCXLGUI::~LCXLGUI ()
171 {
172 }
173 
174 void
connection_handler()175 LCXLGUI::connection_handler ()
176 {
177 	/* ignore all changes to combobox active strings here, because we're
178 	   updating them to match a new ("external") reality - we were called
179 	   because port connections have changed.
180 	*/
181 
182 	PBD::Unwinder<bool> ici (ignore_active_change, true);
183 
184 	update_port_combos ();
185 }
186 
187 void
update_port_combos()188 LCXLGUI::update_port_combos ()
189 {
190 	vector<string> midi_inputs;
191 	vector<string> midi_outputs;
192 
193 	ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
194 	ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
195 
196 	Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
197 	Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
198 	bool input_found = false;
199 	bool output_found = false;
200 	int n;
201 
202 	input_combo.set_model (input);
203 	output_combo.set_model (output);
204 
205 	Gtk::TreeModel::Children children = input->children();
206 	Gtk::TreeModel::Children::iterator i;
207 	i = children.begin();
208 	++i; /* skip "Disconnected" */
209 
210 
211 	for (n = 1;  i != children.end(); ++i, ++n) {
212 		string port_name = (*i)[midi_port_columns.full_name];
213 		if (lcxl.input_port()->connected_to (port_name)) {
214 			input_combo.set_active (n);
215 			input_found = true;
216 			break;
217 		}
218 	}
219 
220 	if (!input_found) {
221 		input_combo.set_active (0); /* disconnected */
222 	}
223 
224 	children = output->children();
225 	i = children.begin();
226 	++i; /* skip "Disconnected" */
227 
228 	for (n = 1;  i != children.end(); ++i, ++n) {
229 		string port_name = (*i)[midi_port_columns.full_name];
230 		if (lcxl.output_port()->connected_to (port_name)) {
231 			output_combo.set_active (n);
232 			output_found = true;
233 			break;
234 		}
235 	}
236 
237 	if (!output_found) {
238 		output_combo.set_active (0); /* disconnected */
239 	}
240 }
241 
242 Glib::RefPtr<Gtk::ListStore>
build_midi_port_list(vector<string> const & ports,bool for_input)243 LCXLGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
244 {
245 	Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
246 	TreeModel::Row row;
247 
248 	row = *store->append ();
249 	row[midi_port_columns.full_name] = string();
250 	row[midi_port_columns.short_name] = _("Disconnected");
251 
252 	for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
253 		row = *store->append ();
254 		row[midi_port_columns.full_name] = *p;
255 		std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
256 		if (pn.empty ()) {
257 			pn = (*p).substr ((*p).find (':') + 1);
258 		}
259 		row[midi_port_columns.short_name] = pn;
260 	}
261 
262 	return store;
263 }
264 
265 void
active_port_changed(Gtk::ComboBox * combo,bool for_input)266 LCXLGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
267 {
268 	if (ignore_active_change) {
269 		return;
270 	}
271 
272 	TreeModel::iterator active = combo->get_active ();
273 	string new_port = (*active)[midi_port_columns.full_name];
274 
275 	if (new_port.empty()) {
276 		if (for_input) {
277 			lcxl.input_port()->disconnect_all ();
278 		} else {
279 			lcxl.output_port()->disconnect_all ();
280 		}
281 
282 		return;
283 	}
284 
285 	if (for_input) {
286 		if (!lcxl.input_port()->connected_to (new_port)) {
287 			lcxl.input_port()->disconnect_all ();
288 			lcxl.input_port()->connect (new_port);
289 		}
290 	} else {
291 		if (!lcxl.output_port()->connected_to (new_port)) {
292 			lcxl.output_port()->disconnect_all ();
293 			lcxl.output_port()->connect (new_port);
294 		}
295 	}
296 }
297 
298 void
toggle_fader8master()299 LCXLGUI::toggle_fader8master ()
300 {
301 	DEBUG_TRACE(DEBUG::LaunchControlXL, string_compose("use_fader8master WAS: %1\n", lcxl.fader8master()));
302 	lcxl.set_fader8master (!lcxl.fader8master());
303 	DEBUG_TRACE(DEBUG::LaunchControlXL, string_compose("use_fader8master IS: %1\n", lcxl.fader8master()));
304 }
305 
306 #ifdef MIXBUS32C
307 void
toggle_ctrllowersends()308 LCXLGUI::toggle_ctrllowersends ()
309 {
310 	DEBUG_TRACE(DEBUG::LaunchControlXL, string_compose("ctrllowersends WAS: %1\n", lcxl.ctrllowersends()));
311 	lcxl.set_ctrllowersends (!lcxl.ctrllowersends());
312 	DEBUG_TRACE(DEBUG::LaunchControlXL, string_compose("ctrllowersends IS: %1\n", lcxl.ctrllowersends()));
313 }
314 #endif
315