Add Launch Control XL control surface support
[ardour.git] / libs / surfaces / launch_control_xl / gui.cc
1 /*
2     Copyright (C) 2015 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
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
36 #include "launch_control_xl.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 LaunchControlXL::get_gui () const
50 {
51         if (!gui) {
52                 const_cast<LaunchControlXL*>(this)->build_gui ();
53         }
54         static_cast<Gtk::VBox*>(gui)->show_all();
55         return gui;
56 }
57
58 void
59 LaunchControlXL::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 gui;
69         gui = 0;
70 }
71
72 void
73 LaunchControlXL::build_gui ()
74 {
75         gui = new LCXLGUI (*this);
76 }
77
78 /*--------------------*/
79
80 LCXLGUI::LCXLGUI (LaunchControlXL& p)
81         : lcxl (p)
82         , table (2, 5)
83         , action_table (5, 4)
84         , ignore_active_change (false)
85 {
86         set_border_width (12);
87
88         table.set_row_spacings (4);
89         table.set_col_spacings (6);
90         table.set_border_width (12);
91         table.set_homogeneous (false);
92
93         std::string data_file_path;
94         string name = "push2-small.png";
95         Searchpath spath(ARDOUR::ardour_data_search_path());
96         spath.add_subdirectory_to_paths ("icons");
97         find_file (spath, name, data_file_path);
98         if (!data_file_path.empty()) {
99                 image.set (data_file_path);
100                 hpacker.pack_start (image, false, false);
101         }
102
103         Gtk::Label* l;
104         int row = 0;
105
106         input_combo.pack_start (midi_port_columns.short_name);
107         output_combo.pack_start (midi_port_columns.short_name);
108
109         input_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &LCXLGUI::active_port_changed), &input_combo, true));
110         output_combo.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &LCXLGUI::active_port_changed), &output_combo, false));
111
112         l = manage (new Gtk::Label);
113         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Incoming MIDI on:")));
114         l->set_alignment (1.0, 0.5);
115         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
116         table.attach (input_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
117         row++;
118
119         l = manage (new Gtk::Label);
120         l->set_markup (string_compose ("<span weight=\"bold\">%1</span>", _("Outgoing MIDI on:")));
121         l->set_alignment (1.0, 0.5);
122         table.attach (*l, 0, 1, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0));
123         table.attach (output_combo, 1, 2, row, row+1, AttachOptions(FILL|EXPAND), AttachOptions(0), 0, 0);
124         row++;
125
126         hpacker.pack_start (table, true, true);
127
128         set_spacing (12);
129
130         pack_start (hpacker, false, false);
131
132         /* update the port connection combos */
133
134         update_port_combos ();
135
136         /* catch future changes to connection state */
137
138         ARDOUR::AudioEngine::instance()->PortRegisteredOrUnregistered.connect (port_reg_connection, invalidator (*this), boost::bind (&LCXLGUI::connection_handler, this), gui_context());
139         lcxl.ConnectionChange.connect (connection_change_connection, invalidator (*this), boost::bind (&LCXLGUI::connection_handler, this), gui_context());
140 }
141
142 LCXLGUI::~LCXLGUI ()
143 {
144 }
145
146 void
147 LCXLGUI::connection_handler ()
148 {
149         /* ignore all changes to combobox active strings here, because we're
150            updating them to match a new ("external") reality - we were called
151            because port connections have changed.
152         */
153
154         PBD::Unwinder<bool> ici (ignore_active_change, true);
155
156         update_port_combos ();
157 }
158
159 void
160 LCXLGUI::update_port_combos ()
161 {
162         vector<string> midi_inputs;
163         vector<string> midi_outputs;
164
165         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsOutput|ARDOUR::IsTerminal), midi_inputs);
166         ARDOUR::AudioEngine::instance()->get_ports ("", ARDOUR::DataType::MIDI, ARDOUR::PortFlags (ARDOUR::IsInput|ARDOUR::IsTerminal), midi_outputs);
167
168         Glib::RefPtr<Gtk::ListStore> input = build_midi_port_list (midi_inputs, true);
169         Glib::RefPtr<Gtk::ListStore> output = build_midi_port_list (midi_outputs, false);
170         bool input_found = false;
171         bool output_found = false;
172         int n;
173
174         input_combo.set_model (input);
175         output_combo.set_model (output);
176
177         Gtk::TreeModel::Children children = input->children();
178         Gtk::TreeModel::Children::iterator i;
179         i = children.begin();
180         ++i; /* skip "Disconnected" */
181
182
183         for (n = 1;  i != children.end(); ++i, ++n) {
184                 string port_name = (*i)[midi_port_columns.full_name];
185                 if (lcxl.input_port()->connected_to (port_name)) {
186                         input_combo.set_active (n);
187                         input_found = true;
188                         break;
189                 }
190         }
191
192         if (!input_found) {
193                 input_combo.set_active (0); /* disconnected */
194         }
195
196         children = output->children();
197         i = children.begin();
198         ++i; /* skip "Disconnected" */
199
200         for (n = 1;  i != children.end(); ++i, ++n) {
201                 string port_name = (*i)[midi_port_columns.full_name];
202                 if (lcxl.output_port()->connected_to (port_name)) {
203                         output_combo.set_active (n);
204                         output_found = true;
205                         break;
206                 }
207         }
208
209         if (!output_found) {
210                 output_combo.set_active (0); /* disconnected */
211         }
212 }
213
214 Glib::RefPtr<Gtk::ListStore>
215 LCXLGUI::build_midi_port_list (vector<string> const & ports, bool for_input)
216 {
217         Glib::RefPtr<Gtk::ListStore> store = ListStore::create (midi_port_columns);
218         TreeModel::Row row;
219
220         row = *store->append ();
221         row[midi_port_columns.full_name] = string();
222         row[midi_port_columns.short_name] = _("Disconnected");
223
224         for (vector<string>::const_iterator p = ports.begin(); p != ports.end(); ++p) {
225                 row = *store->append ();
226                 row[midi_port_columns.full_name] = *p;
227                 std::string pn = ARDOUR::AudioEngine::instance()->get_pretty_name_by_name (*p);
228                 if (pn.empty ()) {
229                         pn = (*p).substr ((*p).find (':') + 1);
230                 }
231                 row[midi_port_columns.short_name] = pn;
232         }
233
234         return store;
235 }
236
237 void
238 LCXLGUI::active_port_changed (Gtk::ComboBox* combo, bool for_input)
239 {
240         if (ignore_active_change) {
241                 return;
242         }
243
244         TreeModel::iterator active = combo->get_active ();
245         string new_port = (*active)[midi_port_columns.full_name];
246
247         if (new_port.empty()) {
248                 if (for_input) {
249                         lcxl.input_port()->disconnect_all ();
250                 } else {
251                         lcxl.output_port()->disconnect_all ();
252                 }
253
254                 return;
255         }
256
257         if (for_input) {
258                 if (!lcxl.input_port()->connected_to (new_port)) {
259                         lcxl.input_port()->disconnect_all ();
260                         lcxl.input_port()->connect (new_port);
261                 }
262         } else {
263                 if (!lcxl.output_port()->connected_to (new_port)) {
264                         lcxl.output_port()->disconnect_all ();
265                         lcxl.output_port()->connect (new_port);
266                 }
267         }
268 }