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