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