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