Faderport8 control surface support
[ardour.git] / libs / surfaces / faderport8 / gui.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2015 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 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/gtk_ui.h"
30 #include "gtkmm2ext/gui_thread.h"
31 #include "gtkmm2ext/utils.h"
32
33 #include "ardour/audioengine.h"
34 #include "ardour/filesystem_paths.h"
35
36 #include "faderport8.h"
37 #include "gui.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace PBD;
42 using namespace ARDOUR;
43 using namespace ArdourSurface;
44 using namespace std;
45 using namespace Gtk;
46 using namespace Gtkmm2ext;
47
48 void*
49 FaderPort8::get_gui () const
50 {
51         if (!gui) {
52                 const_cast<FaderPort8*>(this)->build_gui ();
53         }
54         static_cast<Gtk::VBox*>(gui)->show_all();
55         return gui;
56 }
57
58 void
59 FaderPort8::tear_down_gui ()
60 {
61         if (gui) {
62                 Gtk::Widget *w = static_cast<Gtk::VBox*>(gui)->get_parent();
63                 if (w) {
64                         w->hide();
65                         delete w;
66                 }
67         }
68         delete static_cast<FP8GUI*> (gui);
69         gui = 0;
70 }
71
72 void
73 FaderPort8::build_gui ()
74 {
75         gui = (void*) new FP8GUI (*this);
76 }
77
78 /* ****************************************************************************/
79
80 FP8GUI::FP8GUI (FaderPort8& p)
81         : fp (p)
82         , table (2, 3)
83         , ignore_active_change (false)
84 {
85         set_border_width (12);
86
87         table.set_row_spacings (4);
88         table.set_col_spacings (6);
89         table.set_border_width (12);
90         table.set_homogeneous (false);
91
92         Gtk::Label* l;
93         int row = 0;
94
95         input_combo.pack_start (midi_port_columns.short_name);
96         output_combo.pack_start (midi_port_columns.short_name);
97
98         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &input_combo, true));
99         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::active_port_changed), &output_combo, false));
100
101         l = manage (new Gtk::Label);
102         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
103         l->set_alignment (1.0, 0.5);
104         table.attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
105         table.attach (input_combo, 2, 6, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
106         row++;
107
108         l = manage (new Gtk::Label);
109         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
110         l->set_alignment (1.0, 0.5);
111         table.attach (*l, 0, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
112         table.attach (output_combo, 2, 6, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
113         row++;
114
115         pack_start (table);
116
117         /* actions */
118         build_available_action_menu ();
119
120         int action_row = 0;
121         int action_col = 0;
122         Gtk::Alignment* align;
123
124         for (FP8Controls::UserButtonMap::const_iterator i = fp.control().user_buttons ().begin ();
125                         i != fp.control().user_buttons ().end (); ++i) {
126                 Gtk::ComboBox* user_combo = manage (new Gtk::ComboBox);
127                 build_action_combo (*user_combo, i->first);
128                 l = manage (new Gtk::Label);
129                 l->set_markup (string_compose ("<span weight=\"bold\">%1:</span>", i->second));
130                 l->set_alignment (1.0, 0.5);
131                 table.attach (*l, 2 * action_col, 2 * action_col + 1, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
132                 align = manage (new Alignment);
133                 align->set (0.0, 0.5);
134                 align->add (*user_combo);
135                 table.attach (*align, 2 * action_col + 1, 2 * action_col + 2, row + action_row, row + action_row + 1, AttachOptions(FILL|EXPAND), AttachOptions (0));
136
137                 if (++action_row == 4) {
138                         action_row = 0;
139                         ++action_col;
140                 }
141         }
142
143         /* update the port connection combos */
144         update_port_combos ();
145
146         /* catch future changes to connection state */
147         fp.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&FP8GUI::connection_handler, this), gui_context());
148 }
149
150 FP8GUI::~FP8GUI ()
151 {
152 }
153
154 void
155 FP8GUI::connection_handler ()
156 {
157         PBD::Unwinder<bool> ici (ignore_active_change, true);
158         update_port_combos ();
159 }
160
161 void
162 FP8GUI::update_port_combos ()
163 {
164         vector<string> midi_inputs;
165         vector<string> midi_outputs;
166
167         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
168         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
169
170         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
171         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
172         bool input_found = false;
173         bool output_found = false;
174         int n;
175
176         input_combo.set_model (input);
177         output_combo.set_model (output);
178
179         Gtk::TreeModel::Children children = input->children();
180         Gtk::TreeModel::Children::iterator i;
181         i = children.begin();
182         ++i; /* skip "Disconnected" */
183
184         for (n = 1;  i != children.end(); ++i, ++n) {
185                 string port_name = (*i)[midi_port_columns.full_name];
186                 if (fp.input_port()->connected_to (port_name)) {
187                         input_combo.set_active (n);
188                         input_found = true;
189                         break;
190                 }
191         }
192
193         if (!input_found) {
194                 input_combo.set_active (0); /* disconnected */
195         }
196
197         children = output->children();
198         i = children.begin();
199         ++i; /* skip "Disconnected" */
200
201         for (n = 1;  i != children.end(); ++i, ++n) {
202                 string port_name = (*i)[midi_port_columns.full_name];
203                 if (fp.output_port()->connected_to (port_name)) {
204                         output_combo.set_active (n);
205                         output_found = true;
206                         break;
207                 }
208         }
209
210         if (!output_found) {
211                 output_combo.set_active (0); /* disconnected */
212         }
213 }
214
215
216 Glib::RefPtr<Gtk::ListStore>
217 FP8GUI::build_midi_port_list (vector<string> const & ports, bool for_input)
218 {
219         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
220         TreeModel::Row row;
221
222         row = *store->append ();
223         row[midi_port_columns.full_name] = string();
224         row[midi_port_columns.short_name] = _("Disconnected");
225
226         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
227                 row = *store->append ();
228                 row[midi_port_columns.full_name] = *p;
229                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
230                 if (pn.empty ()) {
231                         pn = (*p).substr ((*p).find (':') + 1);
232                 }
233                 row[midi_port_columns.short_name] = pn;
234         }
235
236         return store;
237 }
238
239 void
240 FP8GUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
241 {
242         if (ignore_active_change) {
243                 return;
244         }
245
246         TreeModel::iterator active = combo->get_active ();
247         string new_port = (*active)[midi_port_columns.full_name];
248
249         if (new_port.empty()) {
250                 if (for_input) {
251                         fp.input_port()->disconnect_all ();
252                 } else {
253                         fp.output_port()->disconnect_all ();
254                 }
255
256                 return;
257         }
258
259         if (for_input) {
260                 if (!fp.input_port()->connected_to (new_port)) {
261                         fp.input_port()->disconnect_all ();
262                         fp.input_port()->connect (new_port);
263                 }
264         } else {
265                 if (!fp.output_port()->connected_to (new_port)) {
266                         fp.output_port()->disconnect_all ();
267                         fp.output_port()->connect (new_port);
268                 }
269         }
270 }
271
272
273
274 void
275 FP8GUI::build_available_action_menu ()
276 {
277         /* build a model of all available actions (needs to be tree structured
278          * more)
279          */
280
281         available_action_model = TreeStore::create (action_columns);
282
283         vector<string> paths;
284         vector<string> labels;
285         vector<string> tooltips;
286         vector<string> keys;
287         vector<Glib::RefPtr<Gtk::Action> > actions;
288
289         Gtkmm2ext::ActionMap::get_all_actions (paths, labels, tooltips, keys, actions);
290
291         typedef std::map<string,TreeIter> NodeMap;
292         NodeMap nodes;
293         NodeMap::iterator r;
294
295
296         vector<string>::iterator k;
297         vector<string>::iterator p;
298         vector<string>::iterator t;
299         vector<string>::iterator l;
300
301         available_action_model->clear ();
302
303         TreeIter rowp;
304         TreeModel::Row parent;
305
306         /* Disabled item (row 0) */
307
308         rowp = available_action_model->append();
309         parent = *(rowp);
310         parent[action_columns.name] = _("Disabled");
311
312         for (l = labels.begin(), k = keys.begin(), p = paths.begin(), t = tooltips.begin(); l != labels.end(); ++k, ++p, ++t, ++l) {
313
314                 TreeModel::Row row;
315                 vector<string> parts;
316
317                 parts.clear ();
318
319                 split (*p, parts, '/');
320
321                 if (parts.empty()) {
322                         continue;
323                 }
324
325                 //kinda kludgy way to avoid displaying menu items as mappable
326                 if ( parts[1] == _("Main_menu") )
327                         continue;
328                 if ( parts[1] == _("JACK") )
329                         continue;
330                 if ( parts[1] == _("redirectmenu") )
331                         continue;
332                 if ( parts[1] == _("Editor_menus") )
333                         continue;
334                 if ( parts[1] == _("RegionList") )
335                         continue;
336                 if ( parts[1] == _("ProcessorMenu") )
337                         continue;
338
339                 if ((r = nodes.find (parts[1])) == nodes.end()) {
340
341                         /* top level is missing */
342
343                         TreeIter rowp;
344                         TreeModel::Row parent;
345                         rowp = available_action_model->append();
346                         nodes[parts[1]] = rowp;
347                         parent = *(rowp);
348                         parent[action_columns.name] = parts[1];
349
350                         row = *(available_action_model->append (parent.children()));
351
352                 } else {
353
354                         row = *(available_action_model->append ((*r->second)->children()));
355
356                 }
357
358                 /* add this action */
359
360                 if (l->empty ()) {
361                         row[action_columns.name] = *t;
362                         action_map[*t] = *p;
363                 } else {
364                         row[action_columns.name] = *l;
365                         action_map[*l] = *p;
366                 }
367
368                 string path = (*p);
369                 /* ControlProtocol::access_action() is not interested in the
370                    legacy "<Actions>/" prefix part of a path.
371                 */
372                 path = path.substr (strlen ("<Actions>/"));
373
374                 row[action_columns.path] = path;
375         }
376 }
377
378 bool
379 FP8GUI::find_action_in_model (const TreeModel::iterator& iter, std::string const& action_path, TreeModel::iterator* found)
380 {
381         TreeModel::Row row = *iter;
382         string path = row[action_columns.path];
383
384         if (path == action_path) {
385                 *found = iter;
386                 return true;
387         }
388
389         return false;
390 }
391
392 void
393 FP8GUI::build_action_combo (Gtk::ComboBox& cb, FP8Controls::ButtonId id)
394 {
395         cb.set_model (available_action_model);
396         cb.pack_start (action_columns.name);
397
398         /* set the active "row" to the right value for the current button binding */
399         string current_action = fp.get_button_action (id, false); /* lookup release action */
400
401         if (current_action.empty()) {
402                 cb.set_active (0); /* "disabled" */
403         } else {
404                 TreeModel::iterator iter = available_action_model->children().end();
405
406                 available_action_model->foreach_iter (sigc::bind (sigc::mem_fun (*this, &FP8GUI::find_action_in_model), current_action, &iter));
407
408                 if (iter != available_action_model->children().end()) {
409                         cb.set_active (iter);
410                 } else {
411                         cb.set_active (0);
412                 }
413         }
414         /* bind signal _after_ setting the current value */
415         cb.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &FP8GUI::action_changed), &cb, id));
416 }
417
418 void
419 FP8GUI::action_changed (Gtk::ComboBox* cb, FP8Controls::ButtonId id)
420 {
421         TreeModel::const_iterator row = cb->get_active ();
422         string action_path = (*row)[action_columns.path];
423         fp.set_button_action (id, false, action_path);
424 }