patch for handling LV2 presets from colinf (#4698)
[ardour.git] / gtk2_ardour / lv2_plugin_ui.cc
1 /*
2     Copyright (C) 2008-2012 Paul Davis
3     Author: David Robillard
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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "ardour/lv2_plugin.h"
21 #include "ardour/session.h"
22 #include "pbd/error.h"
23
24 #include "ardour_ui.h"
25 #include "lv2_plugin_ui.h"
26
27 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
28
29 #include <lilv/lilv.h>
30 #include <suil/suil.h>
31
32 #include "i18n.h"
33
34 using namespace ARDOUR;
35 using namespace Gtk;
36 using namespace PBD;
37
38 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
39
40 static SuilHost* ui_host = NULL;
41
42 void
43 LV2PluginUI::write_from_ui(void*       controller,
44                            uint32_t    port_index,
45                            uint32_t    buffer_size,
46                            uint32_t    format,
47                            const void* buffer)
48 {
49         LV2PluginUI* me = (LV2PluginUI*)controller;
50         if (format == 0) {
51                 if (port_index >= me->_controllables.size()) {
52                         return;
53                 }
54
55                 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
56                 if (ac) {
57                         ac->set_value(*(float*)buffer);
58                 }
59         } else if (format == me->_lv2->atom_eventTransfer()) {
60                 me->_lv2->write_from_ui(port_index, format, buffer_size, (uint8_t*)buffer);
61         }
62 }
63
64 void
65 LV2PluginUI::write_to_ui(void*       controller,
66                          uint32_t    port_index,
67                          uint32_t    buffer_size,
68                          uint32_t    format,
69                          const void* buffer)
70 {
71         LV2PluginUI* me = (LV2PluginUI*)controller;
72         if (me->_inst) {
73                 suil_instance_port_event((SuilInstance*)me->_inst,
74                                          port_index, buffer_size, format, buffer);
75         }
76 }
77
78 uint32_t
79 LV2PluginUI::port_index(void* controller, const char* symbol)
80 {
81         return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
82 }
83
84 void
85 LV2PluginUI::touch(void*    controller,
86                    uint32_t port_index,
87                    bool     grabbed)
88 {
89         LV2PluginUI* me = (LV2PluginUI*)controller;
90         if (port_index >= me->_controllables.size()) {
91                 return;
92         }
93
94         ControllableRef control = me->_controllables[port_index];
95         if (grabbed) {
96                 control->start_touch(control->session().transport_frame());
97         } else {
98                 control->stop_touch(false, control->session().transport_frame());
99         }
100 }
101
102 void
103 LV2PluginUI::update_timeout()
104 {
105         _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
106 }
107
108 void
109 LV2PluginUI::on_external_ui_closed(void* controller)
110 {
111         LV2PluginUI* me = (LV2PluginUI*)controller;
112         me->_screen_update_connection.disconnect();
113         me->_external_ui_ptr = NULL;
114 }
115
116 void
117 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
118 {
119         PlugUIBase::parameter_changed(port_index, val);
120
121         if (val != _values[port_index]) {
122                 parameter_update(port_index, val);
123         }
124 }
125
126 void
127 LV2PluginUI::parameter_update(uint32_t port_index, float val)
128 {
129         if (!_inst) {
130                 return;
131         }
132
133         suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
134         _values[port_index] = val;
135 }
136
137 bool
138 LV2PluginUI::start_updating(GdkEventAny*)
139 {
140         if (!_output_ports.empty()) {
141                 _screen_update_connection.disconnect();
142                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
143                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
144         }
145         return false;
146 }
147
148 bool
149 LV2PluginUI::stop_updating(GdkEventAny*)
150 {
151         //cout << "stop_updating" << endl;
152
153         if (!_output_ports.empty()) {
154                 _screen_update_connection.disconnect();
155         }
156         return false;
157 }
158
159 void
160 LV2PluginUI::output_update()
161 {
162         //cout << "output_update" << endl;
163         if (_external_ui_ptr) {
164                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
165         }
166
167         /* FIXME only works with control output ports (which is all we support now anyway) */
168         uint32_t nports = _output_ports.size();
169         for (uint32_t i = 0; i < nports; ++i) {
170                 uint32_t index = _output_ports[i];
171                 parameter_changed(index, _lv2->get_parameter(index));
172         }
173
174 }
175
176 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
177                          boost::shared_ptr<LV2Plugin>    lv2p)
178         : PlugUIBase(pi)
179         , _lv2(lv2p)
180         , _gui_widget(NULL)
181         , _ardour_buttons_box(NULL)
182         , _values(NULL)
183         , _external_ui_ptr(NULL)
184         , _inst(NULL)
185 {
186 }
187
188 void
189 LV2PluginUI::lv2ui_instantiate(const std::string& title)
190 {
191         LV2_Feature** features;
192         LV2_Feature** features_src;
193         LV2_Feature** features_dst;
194         size_t        features_count;
195         bool          is_external_ui;
196
197         is_external_ui = _lv2->is_external_ui();
198
199         if (is_external_ui) {
200                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
201                 _external_ui_host.plugin_human_id = strdup(title.c_str());
202
203                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
204                 _external_ui_feature.data = &_external_ui_host;
205
206                 features_src = features = (LV2_Feature**)_lv2->features();
207                 features_count = 2;
208                 while (*features++) {
209                         features_count++;
210                 }
211                 features_dst = features = (LV2_Feature**)malloc(
212                         sizeof(LV2_Feature*) * features_count);
213                 features_dst[--features_count] = NULL;
214                 features_dst[--features_count] = &_external_ui_feature;
215                 while (features_count--) {
216                         *features++ = *features_src++;
217                 }
218         } else {
219                 features_dst = (LV2_Feature**)_lv2->features();
220         }
221
222         if (!ui_host) {
223                 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
224                                         LV2PluginUI::port_index,
225                                         NULL, NULL);
226                 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
227         }
228         const char* container_type = (is_external_ui)
229                 ? NS_UI "external"
230                 : NS_UI "GtkUI";
231
232         LilvUI* ui = (LilvUI*)_lv2->c_ui();
233         _inst = suil_instance_new(
234                 ui_host,
235                 this,
236                 container_type,
237                 _lv2->uri(),
238                 lilv_node_as_uri(lilv_ui_get_uri(ui)),
239                 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
240                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
241                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
242                 features_dst);
243
244         if (is_external_ui) {
245                 free(features_dst);
246         }
247
248 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
249
250         const uint32_t num_ports = _lv2->num_ports();
251         for (uint32_t i = 0; i < num_ports; ++i) {
252                 if (_lv2->parameter_is_output(i)
253                     && _lv2->parameter_is_control(i)
254                     && is_update_wanted(i)) {
255                         _output_ports.push_back(i);
256                 }
257         }
258
259         _external_ui_ptr = NULL;
260         if (_inst) {
261                 if (!is_external_ui) {
262                         _ardour_buttons_box = manage (new Gtk::HBox);
263                         _ardour_buttons_box->set_spacing (6);
264                         _ardour_buttons_box->set_border_width (6);
265                         _ardour_buttons_box->pack_end (focus_button, false, false);
266                         _ardour_buttons_box->pack_end (bypass_button, false, false, 10);
267                         _ardour_buttons_box->pack_end (delete_button, false, false);
268                         _ardour_buttons_box->pack_end (save_button, false, false);
269                         _ardour_buttons_box->pack_end (add_button, false, false);
270                         _ardour_buttons_box->pack_end (_preset_combo, false, false);
271                         _ardour_buttons_box->pack_end (_preset_modified, false, false);
272                         _ardour_buttons_box->show_all();
273                         pack_start(*_ardour_buttons_box, false, false);
274
275                         GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
276                         if (!c_widget) {
277                                 error << _("failed to get LV2 UI widget") << endmsg;
278                                 suil_instance_free((SuilInstance*)_inst);
279                                 _inst = NULL;
280                                 return;
281                         }
282                         _gui_widget = Gtk::manage(Glib::wrap(c_widget));
283                         _gui_widget->show_all();
284                         pack_start(*_gui_widget, true, true);
285                 } else {
286                         _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
287                 }
288         }
289
290         _values = new float[num_ports];
291         _controllables.resize(num_ports);
292         for (uint32_t i = 0; i < num_ports; ++i) {
293                 bool     ok;
294                 uint32_t port = _lv2->nth_parameter(i, ok);
295                 if (ok) {
296                         _values[port]        = _lv2->get_parameter(port);
297                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
298                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
299
300                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
301                                 parameter_update(port, _values[port]);
302                         }
303                 }
304         }
305
306         if (_lv2->has_message_output()) {
307                 _lv2->enable_ui_emmission();
308                 ARDOUR_UI::instance()->RapidScreenUpdate.connect(
309                         sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
310         }
311 }
312
313 void
314 LV2PluginUI::lv2ui_free()
315 {
316         stop_updating (0);
317
318         if (_gui_widget) {
319                 remove (*_gui_widget);
320                 _gui_widget = NULL;
321         }
322
323         if (_inst) {
324                 suil_instance_free((SuilInstance*)_inst);
325                 _inst = NULL;
326         }
327 }
328
329 LV2PluginUI::~LV2PluginUI ()
330 {
331         if (_values) {
332                 delete[] _values;
333         }
334
335         /* Close and delete GUI. */
336         lv2ui_free();
337
338         _screen_update_connection.disconnect();
339
340         if (_lv2->is_external_ui()) {
341                 /* External UI is no longer valid.
342                    on_window_hide() will not try to use it if is NULL.
343                 */
344                 _external_ui_ptr = NULL;
345         }
346 }
347
348 int
349 LV2PluginUI::get_preferred_height()
350 {
351         Gtk::Requisition r = size_request();
352         return r.height;
353 }
354
355 int
356 LV2PluginUI::get_preferred_width()
357 {
358         Gtk::Requisition r = size_request();
359         return r.width;
360 }
361
362 bool
363 LV2PluginUI::resizable()
364 {
365         return _lv2->ui_is_resizable();
366 }
367
368 int
369 LV2PluginUI::package(Gtk::Window& win)
370 {
371         if (_external_ui_ptr) {
372                 _win_ptr = &win;
373         } else {
374                 /* forward configure events to plugin window */
375                 win.signal_configure_event().connect(
376                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
377                 win.signal_map_event().connect(
378                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
379                 win.signal_unmap_event().connect(
380                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
381         }
382         return 0;
383 }
384
385 bool
386 LV2PluginUI::configure_handler(GdkEventConfigure*)
387 {
388         std::cout << "CONFIGURE" << std::endl;
389         return false;
390 }
391
392 bool
393 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
394 {
395         /* FIXME: use port notification properties
396            and/or new UI extension subscription methods
397         */
398         return true;
399 }
400
401 bool
402 LV2PluginUI::on_window_show(const std::string& title)
403 {
404         //cout << "on_window_show - " << title << endl; flush(cout);
405
406         if (_lv2->is_external_ui()) {
407                 if (_external_ui_ptr) {
408                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
409                         return false;
410                 }
411                 lv2ui_instantiate(title);
412                 if (!_external_ui_ptr) {
413                         return false;
414                 }
415
416                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
417                 _screen_update_connection.disconnect();
418                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
419                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
420                 return false;
421         } else {
422                 lv2ui_instantiate("gtk2gui");
423         }
424
425         return true;
426 }
427
428 void
429 LV2PluginUI::on_window_hide()
430 {
431         //cout << "on_window_hide" << endl; flush(cout);
432
433         if (_external_ui_ptr) {
434                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
435                 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
436                 //_external_ui_ptr = NULL;
437                 //_screen_update_connection.disconnect();
438         } else {
439                 lv2ui_free();
440         }
441 }