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