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