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