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