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