Create and realize LV2 UI container before instantiating (required for proper embeddi...
[ardour.git] / gtk2_ardour / lv2_plugin_ui.cc
1 /*
2     Copyright (C) 2008-2012 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 #include "ardour/lv2_plugin.h"
21 #include "ardour/session.h"
22 #include "pbd/error.h"
23
24 #include "ardour_ui.h"
25 #include "lv2_plugin_ui.h"
26
27 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
28
29 #include <lilv/lilv.h>
30 #include <suil/suil.h>
31
32 #include "i18n.h"
33
34 using namespace ARDOUR;
35 using namespace Gtk;
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::write_from_ui(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         if (format == 0) {
51                 if (port_index >= me->_controllables.size()) {
52                         return;
53                 }
54
55                 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
56                 if (ac) {
57                         ac->set_value(*(const float*)buffer);
58                 }
59         } else if (format == me->_lv2->atom_eventTransfer()) {
60                 me->_lv2->write_from_ui(port_index, format, buffer_size,
61                                         (const uint8_t*)buffer);
62         }
63 }
64
65 void
66 LV2PluginUI::write_to_ui(void*       controller,
67                          uint32_t    port_index,
68                          uint32_t    buffer_size,
69                          uint32_t    format,
70                          const void* buffer)
71 {
72         LV2PluginUI* me = (LV2PluginUI*)controller;
73         if (me->_inst) {
74                 suil_instance_port_event((SuilInstance*)me->_inst,
75                                          port_index, buffer_size, format, buffer);
76         }
77 }
78
79 uint32_t
80 LV2PluginUI::port_index(void* controller, const char* symbol)
81 {
82         return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
83 }
84
85 void
86 LV2PluginUI::touch(void*    controller,
87                    uint32_t port_index,
88                    bool     grabbed)
89 {
90         LV2PluginUI* me = (LV2PluginUI*)controller;
91         if (port_index >= me->_controllables.size()) {
92                 return;
93         }
94
95         ControllableRef control = me->_controllables[port_index];
96         if (grabbed) {
97                 control->start_touch(control->session().transport_frame());
98         } else {
99                 control->stop_touch(false, control->session().transport_frame());
100         }
101 }
102
103 void
104 LV2PluginUI::update_timeout()
105 {
106         _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
107 }
108
109 void
110 LV2PluginUI::on_external_ui_closed(void* controller)
111 {
112         LV2PluginUI* me = (LV2PluginUI*)controller;
113         me->_screen_update_connection.disconnect();
114         me->_external_ui_ptr = NULL;
115 }
116
117 void
118 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
119 {
120         PlugUIBase::parameter_changed(port_index, val);
121
122         if (val != _values[port_index]) {
123                 parameter_update(port_index, val);
124         }
125 }
126
127 void
128 LV2PluginUI::parameter_update(uint32_t port_index, float val)
129 {
130         if (!_inst) {
131                 return;
132         }
133
134         suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
135         _values[port_index] = val;
136 }
137
138 bool
139 LV2PluginUI::start_updating(GdkEventAny*)
140 {
141         if (!_output_ports.empty()) {
142                 _screen_update_connection.disconnect();
143                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
144                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
145         }
146         return false;
147 }
148
149 bool
150 LV2PluginUI::stop_updating(GdkEventAny*)
151 {
152         //cout << "stop_updating" << endl;
153
154         if (!_output_ports.empty()) {
155                 _screen_update_connection.disconnect();
156         }
157         return false;
158 }
159
160 void
161 LV2PluginUI::output_update()
162 {
163         //cout << "output_update" << endl;
164         if (_external_ui_ptr) {
165                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
166         }
167
168         /* FIXME only works with control output ports (which is all we support now anyway) */
169         uint32_t nports = _output_ports.size();
170         for (uint32_t i = 0; i < nports; ++i) {
171                 uint32_t index = _output_ports[i];
172                 parameter_changed(index, _lv2->get_parameter(index));
173         }
174
175 }
176
177 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
178                          boost::shared_ptr<LV2Plugin>    lv2p)
179         : PlugUIBase(pi)
180         , _lv2(lv2p)
181         , _gui_widget(NULL)
182         , _ardour_buttons_box(NULL)
183         , _values(NULL)
184         , _external_ui_ptr(NULL)
185         , _inst(NULL)
186 {
187 }
188
189 void
190 LV2PluginUI::lv2ui_instantiate(const std::string& title)
191 {
192         bool          is_external_ui = _lv2->is_external_ui();
193         LV2_Feature** features_src   = const_cast<LV2_Feature**>(_lv2->features());
194         LV2_Feature** features       = const_cast<LV2_Feature**>(_lv2->features());
195         size_t        features_count = 0;
196         while (*features++) {
197                 features_count++;
198         }
199
200         Gtk::Alignment* container = NULL;
201         if (is_external_ui) {
202                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
203                 _external_ui_host.plugin_human_id = strdup(title.c_str());
204
205                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
206                 _external_ui_feature.data = &_external_ui_host;
207
208                 ++features_count;
209                 features = (LV2_Feature**)malloc(
210                         sizeof(LV2_Feature*) * (features_count + 1));
211                 for (size_t i = 0; i < features_count - 1; ++i) {
212                         features[i] = features_src[i];
213                 }
214                 features[features_count - 1] = &_external_ui_feature;
215                 features[features_count]     = NULL;
216         } else {
217                 _ardour_buttons_box = manage (new Gtk::HBox);
218                 _ardour_buttons_box->set_spacing (6);
219                 _ardour_buttons_box->set_border_width (6);
220                 _ardour_buttons_box->pack_end (focus_button, false, false);
221                 _ardour_buttons_box->pack_end (bypass_button, false, false, 10);
222                 _ardour_buttons_box->pack_end (delete_button, false, false);
223                 _ardour_buttons_box->pack_end (save_button, false, false);
224                 _ardour_buttons_box->pack_end (add_button, false, false);
225                 _ardour_buttons_box->pack_end (_preset_combo, false, false);
226                 _ardour_buttons_box->pack_end (_preset_modified, false, false);
227                 _ardour_buttons_box->show_all();
228                 pack_start(*_ardour_buttons_box, false, false);
229
230                 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
231                 pack_start(*_gui_widget, true, true);
232                 _gui_widget->show();
233
234                 _parent_feature.URI  = LV2_UI__parent;
235                 _parent_feature.data = _gui_widget->gobj();
236
237                 ++features_count;
238                 features = (LV2_Feature**)malloc(
239                         sizeof(LV2_Feature*) * (features_count + 1));
240                 for (size_t i = 0; i < features_count - 1; ++i) {
241                         features[i] = features_src[i];
242                 }
243                 features[features_count - 1] = &_parent_feature;
244                 features[features_count]     = NULL;
245         }
246
247         if (!ui_host) {
248                 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
249                                         LV2PluginUI::port_index,
250                                         NULL, NULL);
251                 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
252         }
253         const char* container_type = (is_external_ui)
254                 ? NS_UI "external"
255                 : NS_UI "GtkUI";
256
257         const LilvUI* ui = (const LilvUI*)_lv2->c_ui();
258         _inst = suil_instance_new(
259                 ui_host,
260                 this,
261                 container_type,
262                 _lv2->uri(),
263                 lilv_node_as_uri(lilv_ui_get_uri(ui)),
264                 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
265                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
266                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
267                 features);
268
269         free(features);
270
271 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
272
273         const uint32_t num_ports = _lv2->num_ports();
274         for (uint32_t i = 0; i < num_ports; ++i) {
275                 if (_lv2->parameter_is_output(i)
276                     && _lv2->parameter_is_control(i)
277                     && is_update_wanted(i)) {
278                         _output_ports.push_back(i);
279                 }
280         }
281
282         _external_ui_ptr = NULL;
283         if (_inst) {
284                 if (!is_external_ui) {
285                         GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
286                         if (!c_widget) {
287                                 error << _("failed to get LV2 UI widget") << endmsg;
288                                 suil_instance_free((SuilInstance*)_inst);
289                                 _inst = NULL;
290                                 return;
291                         }
292                         if (!container->get_child()) {
293                                 // Suil didn't add the UI to the container for us, so do it now
294                                 container->add(*Gtk::manage(Glib::wrap(c_widget)));
295                         }
296                         container->show_all();
297                 } else {
298                         _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
299                 }
300         }
301
302         _values = new float[num_ports];
303         _controllables.resize(num_ports);
304         for (uint32_t i = 0; i < num_ports; ++i) {
305                 bool     ok;
306                 uint32_t port = _lv2->nth_parameter(i, ok);
307                 if (ok) {
308                         _values[port]        = _lv2->get_parameter(port);
309                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
310                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
311
312                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
313                                 parameter_update(port, _values[port]);
314                         }
315                 }
316         }
317
318         if (_lv2->has_message_output()) {
319                 _lv2->enable_ui_emmission();
320                 ARDOUR_UI::instance()->RapidScreenUpdate.connect(
321                         sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
322         }
323 }
324
325 void
326 LV2PluginUI::lv2ui_free()
327 {
328         stop_updating (0);
329
330         if (_gui_widget) {
331                 remove (*_gui_widget);
332                 _gui_widget = NULL;
333         }
334
335         if (_inst) {
336                 suil_instance_free((SuilInstance*)_inst);
337                 _inst = NULL;
338         }
339 }
340
341 LV2PluginUI::~LV2PluginUI ()
342 {
343         if (_values) {
344                 delete[] _values;
345         }
346
347         /* Close and delete GUI. */
348         lv2ui_free();
349
350         _screen_update_connection.disconnect();
351
352         if (_lv2->is_external_ui()) {
353                 /* External UI is no longer valid.
354                    on_window_hide() will not try to use it if is NULL.
355                 */
356                 _external_ui_ptr = NULL;
357         }
358 }
359
360 int
361 LV2PluginUI::get_preferred_height()
362 {
363         Gtk::Requisition r = size_request();
364         return r.height;
365 }
366
367 int
368 LV2PluginUI::get_preferred_width()
369 {
370         Gtk::Requisition r = size_request();
371         return r.width;
372 }
373
374 bool
375 LV2PluginUI::resizable()
376 {
377         return _lv2->ui_is_resizable();
378 }
379
380 int
381 LV2PluginUI::package(Gtk::Window& win)
382 {
383         if (_external_ui_ptr) {
384                 _win_ptr = &win;
385         } else {
386                 /* forward configure events to plugin window */
387                 win.signal_configure_event().connect(
388                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
389                 win.signal_map_event().connect(
390                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
391                 win.signal_unmap_event().connect(
392                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
393         }
394         return 0;
395 }
396
397 bool
398 LV2PluginUI::configure_handler(GdkEventConfigure*)
399 {
400         std::cout << "CONFIGURE" << std::endl;
401         return false;
402 }
403
404 bool
405 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
406 {
407         /* FIXME: use port notification properties
408            and/or new UI extension subscription methods
409         */
410         return true;
411 }
412
413 bool
414 LV2PluginUI::on_window_show(const std::string& title)
415 {
416         //cout << "on_window_show - " << title << endl; flush(cout);
417
418         if (_lv2->is_external_ui()) {
419                 if (_external_ui_ptr) {
420                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
421                         return false;
422                 }
423                 lv2ui_instantiate(title);
424                 if (!_external_ui_ptr) {
425                         return false;
426                 }
427
428                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
429                 _screen_update_connection.disconnect();
430                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
431                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
432                 return false;
433         } else {
434                 lv2ui_instantiate("gtk2gui");
435         }
436
437         return true;
438 }
439
440 void
441 LV2PluginUI::on_window_hide()
442 {
443         //cout << "on_window_hide" << endl; flush(cout);
444
445         if (_external_ui_ptr) {
446                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
447                 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
448                 //_external_ui_ptr = NULL;
449                 //_screen_update_connection.disconnect();
450         } else {
451                 lv2ui_free();
452         }
453 }