Create and destroy (non-external) LV2 plugin UIs as window is shown/hidden.
[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 (!_output_ports.empty()) {
122                 _screen_update_connection.disconnect();
123         }
124         return false;
125 }
126
127 void
128 LV2PluginUI::output_update()
129 {
130         //cout << "output_update" << endl;
131         if (_external_ui_ptr) {
132                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
133         }
134
135         /* FIXME only works with control output ports (which is all we support now anyway) */
136         uint32_t nports = _output_ports.size();
137         for (uint32_t i = 0; i < nports; ++i) {
138                 uint32_t index = _output_ports[i];
139                 parameter_changed(index, _lv2->get_parameter(index));
140         }
141
142 }
143
144 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
145                          boost::shared_ptr<LV2Plugin>    lv2p)
146         : PlugUIBase(pi)
147         , _lv2(lv2p)
148         , _gui_widget(NULL)
149         , _values(NULL)
150         , _external_ui_ptr(NULL)
151         , _inst(NULL)
152 {
153 }
154
155 void
156 LV2PluginUI::lv2ui_instantiate(const std::string& title)
157 {
158         LV2_Feature** features;
159         LV2_Feature** features_src;
160         LV2_Feature** features_dst;
161         size_t        features_count;
162         bool          is_external_ui;
163
164         is_external_ui = _lv2->is_external_ui();
165
166         if (is_external_ui) {
167                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
168                 _external_ui_host.plugin_human_id = strdup(title.c_str());
169
170                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
171                 _external_ui_feature.data = &_external_ui_host;
172
173                 features_src = features = (LV2_Feature**)_lv2->features();
174                 features_count = 2;
175                 while (*features++) {
176                         features_count++;
177                 }
178                 features_dst = features = (LV2_Feature**)malloc(
179                         sizeof(LV2_Feature*) * features_count);
180                 features_dst[--features_count] = NULL;
181                 features_dst[--features_count] = &_external_ui_feature;
182                 while (features_count--) {
183                         *features++ = *features_src++;
184                 }
185         } else {
186                 features_dst = (LV2_Feature**)_lv2->features();
187         }
188
189 #ifdef HAVE_SUIL
190         if (!ui_host) {
191                 ui_host = suil_host_new(LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
192         }
193         const char* container_type = (is_external_ui)
194                 ? NS_UI "external"
195                 : NS_UI "GtkUI";
196
197         LilvUI* ui = (LilvUI*)_lv2->c_ui();
198         _inst = suil_instance_new(
199                 ui_host,
200                 this,
201                 container_type,
202                 _lv2->uri(),
203                 lilv_node_as_uri(lilv_ui_get_uri(ui)),
204                 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
205                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
206                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
207                 features_dst);
208 #else
209         _inst = slv2_ui_instantiate((SLV2Plugin)_lv2->c_plugin(),
210                                     (SLV2UI)_lv2->c_ui(),
211                                     LV2PluginUI::lv2_ui_write,
212                                     this,
213                                     features_dst);
214 #endif
215
216         if (is_external_ui) {
217                 free(features_dst);
218         }
219
220 #ifdef HAVE_SUIL
221 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
222 #else
223 #define GET_WIDGET(inst) slv2_ui_instance_get_widget((SLV2UIInstance)inst);
224 #endif
225
226         const uint32_t num_ports = _lv2->num_ports();
227         for (uint32_t i = 0; i < num_ports; ++i) {
228                 if (_lv2->parameter_is_output(i)
229                     && _lv2->parameter_is_control(i)
230                     && is_update_wanted(i)) {
231                         _output_ports.push_back(i);
232                 }
233         }
234
235         _external_ui_ptr = NULL;
236         if (_inst) {
237                 if (!is_external_ui) {
238                         GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
239                         _gui_widget = Glib::wrap(c_widget);
240                         _gui_widget->show_all();
241                         pack_start(*_gui_widget, true, true);
242                 } else {
243                         _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
244                 }
245         }
246
247         _values = new float[num_ports];
248         _controllables.resize(num_ports);
249         for (uint32_t i = 0; i < num_ports; ++i) {
250                 bool     ok;
251                 uint32_t port = _lv2->nth_parameter(i, ok);
252                 if (ok) {
253                         _values[port]        = _lv2->get_parameter(port);
254                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
255                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
256
257                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
258                                 parameter_update(port, _values[port]);
259                         }
260                 }
261         }
262 }
263
264 void
265 LV2PluginUI::lv2ui_free()
266 {
267         if (_lv2->is_external_ui() || !_gui_widget) {
268                 return;
269         }
270
271         stop_updating(NULL);
272         remove(*_gui_widget);
273
274 #ifdef HAVE_SUIL
275         suil_instance_free((SuilInstance*)_inst);
276 #else
277         SLV2UIInstance          inst      = (SLV2UIInstance)_inst;
278         const LV2UI_Descriptor* ui_desc   = slv2_ui_instance_get_descriptor(inst);
279         LV2UI_Handle            ui_handle = slv2_ui_instance_get_handle(inst);
280
281         if (ui_desc) {
282                 ui_desc->cleanup(ui_handle);
283         }
284 #endif
285
286         _inst = NULL;
287         _gui_widget = NULL;
288 }
289
290 LV2PluginUI::~LV2PluginUI ()
291 {
292         //cout << "LV2PluginUI destructor called" << endl;
293
294         if (_values) {
295                 delete[] _values;
296         }
297
298         /* Close and delete GUI. */
299         lv2ui_free();
300
301         _screen_update_connection.disconnect();
302
303         if (_lv2->is_external_ui()) {
304                 /* External UI is no longer valid.
305                    on_window_hide() will not try to use it if is NULL.
306                 */
307                 _external_ui_ptr = NULL;
308         }
309 }
310
311 int
312 LV2PluginUI::get_preferred_height()
313 {
314         Gtk::Requisition r = size_request();
315         return r.height;
316 }
317
318 int
319 LV2PluginUI::get_preferred_width()
320 {
321         Gtk::Requisition r = size_request();
322         return r.width;
323 }
324
325 int
326 LV2PluginUI::package(Gtk::Window& win)
327 {
328         if (_external_ui_ptr) {
329                 _win_ptr = &win;
330         } else {
331                 /* forward configure events to plugin window */
332                 win.signal_configure_event().connect(
333                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
334                 win.signal_map_event().connect(
335                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
336                 win.signal_unmap_event().connect(
337                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
338         }
339         return 0;
340 }
341
342 bool
343 LV2PluginUI::configure_handler(GdkEventConfigure*)
344 {
345         std::cout << "CONFIGURE" << std::endl;
346         return false;
347 }
348
349 bool
350 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
351 {
352         /* FIXME: use port notification properties
353            and/or new UI extension subscription methods
354         */
355         return true;
356 }
357
358 bool
359 LV2PluginUI::on_window_show(const std::string& title)
360 {
361         //cout << "on_window_show - " << title << endl; flush(cout);
362
363         if (_lv2->is_external_ui()) {
364                 if (_external_ui_ptr) {
365                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
366                         return false;
367                 }
368                 lv2ui_instantiate(title);
369                 if (!_external_ui_ptr) {
370                         return false;
371                 }
372
373                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
374                 _screen_update_connection.disconnect();
375                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
376                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
377                 return false;
378         } else {
379                 lv2ui_instantiate("gtk2gui");
380         }
381
382         return true;
383 }
384
385 void
386 LV2PluginUI::on_window_hide()
387 {
388         //cout << "on_window_hide" << endl; flush(cout);
389
390         if (_external_ui_ptr) {
391                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
392                 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
393                 //_external_ui_ptr = NULL;
394                 //_screen_update_connection.disconnect();
395         } else {
396                 lv2ui_free();
397         }
398 }