confirm LV2 GUI changes
[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 #include "pbd/stacktrace.h"
24
25 #include "gui_thread.h"
26 #include "lv2_plugin_ui.h"
27 #include "timers.h"
28
29 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
30
31 #include <lilv/lilv.h>
32 #include <suil/suil.h>
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace Gtk;
38 using namespace PBD;
39
40 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
41
42 static SuilHost* ui_host = NULL;
43
44 void
45 LV2PluginUI::write_from_ui(void*       controller,
46                            uint32_t    port_index,
47                            uint32_t    buffer_size,
48                            uint32_t    format,
49                            const void* buffer)
50 {
51         LV2PluginUI* me = (LV2PluginUI*)controller;
52         if (format == 0) {
53                 if (port_index >= me->_controllables.size()) {
54                         return;
55                 }
56
57                 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
58
59                 me->_updates.insert (port_index);
60
61                 if (ac) {
62                         ac->set_value(*(const float*)buffer);
63                 }
64         } else if (format == URIMap::instance().urids.atom_eventTransfer) {
65
66                 const int cnt = me->_pi->get_count();
67                 for (int i=0; i < cnt; i++ ) {
68                         boost::shared_ptr<LV2Plugin> lv2i = boost::dynamic_pointer_cast<LV2Plugin> (me->_pi->plugin(i));
69                         lv2i->write_from_ui(port_index, format, buffer_size, (const uint8_t*)buffer);
70                 }
71         }
72 }
73
74 void
75 LV2PluginUI::write_to_ui(void*       controller,
76                          uint32_t    port_index,
77                          uint32_t    buffer_size,
78                          uint32_t    format,
79                          const void* buffer)
80 {
81         LV2PluginUI* me = (LV2PluginUI*)controller;
82         if (me->_inst) {
83                 suil_instance_port_event((SuilInstance*)me->_inst,
84                                          port_index, buffer_size, format, buffer);
85         }
86 }
87
88 uint32_t
89 LV2PluginUI::port_index(void* controller, const char* symbol)
90 {
91         return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
92 }
93
94 void
95 LV2PluginUI::touch(void*    controller,
96                    uint32_t port_index,
97                    bool     grabbed)
98 {
99         LV2PluginUI* me = (LV2PluginUI*)controller;
100         if (port_index >= me->_controllables.size()) {
101                 return;
102         }
103
104         ControllableRef control = me->_controllables[port_index];
105         if (grabbed) {
106                 control->start_touch(control->session().transport_frame());
107         } else {
108                 control->stop_touch(false, control->session().transport_frame());
109         }
110 }
111
112 void
113 LV2PluginUI::update_timeout()
114 {
115         _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
116 }
117
118 void
119 LV2PluginUI::on_external_ui_closed(void* controller)
120 {
121         //printf("LV2PluginUI::on_external_ui_closed\n");
122         LV2PluginUI* me = (LV2PluginUI*)controller;
123         me->_screen_update_connection.disconnect();
124         me->_external_ui_ptr = NULL;
125 }
126
127 void
128 LV2PluginUI::control_changed (uint32_t port_index)
129 {
130         /* Must run in GUI thread because we modify _updates with no lock */
131         if (_lv2->get_parameter (port_index) != _values[port_index]) {
132                 /* current plugin parameter does not match last value received
133                    from GUI, so queue an update to push it to the GUI during
134                    our regular timeout.
135                 */
136                 _updates.insert (port_index);
137         }
138 }
139
140 bool
141 LV2PluginUI::start_updating(GdkEventAny*)
142 {
143         if (!_output_ports.empty()) {
144                 _screen_update_connection.disconnect();
145                 _screen_update_connection = Timers::super_rapid_connect
146                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
147         }
148         return false;
149 }
150
151 bool
152 LV2PluginUI::stop_updating(GdkEventAny*)
153 {
154         //cout << "stop_updating" << endl;
155
156         if (!_output_ports.empty()) {
157                 _screen_update_connection.disconnect();
158         }
159         return false;
160 }
161
162 void
163 LV2PluginUI::output_update()
164 {
165         //cout << "output_update" << endl;
166         if (_external_ui_ptr) {
167                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
168                 if (_lv2->is_external_kx() && !_external_ui_ptr) {
169                         // clean up external UI if it closes itself via
170                         // on_external_ui_closed() during run()
171                         //printf("LV2PluginUI::output_update -- UI was closed\n");
172                         //_screen_update_connection.disconnect();
173                         _message_update_connection.disconnect();
174                         if (_inst) {
175                                 suil_instance_free((SuilInstance*)_inst);
176                         }
177                         _inst = NULL;
178                         _external_ui_ptr = NULL;
179                         return;
180                 }
181         }
182
183         if (!_inst) {
184                 return;
185         }
186
187         /* output ports (values set by DSP) need propagating to GUI */
188
189         uint32_t nports = _output_ports.size();
190         for (uint32_t i = 0; i < nports; ++i) {
191                 uint32_t index = _output_ports[i];
192                 float val = _lv2->get_parameter (index);
193
194                 if (val != _values[index]) {
195                         /* Send to GUI */
196                         suil_instance_port_event ((SuilInstance*)_inst, index, 4, 0, &val);
197                         /* Cache current value */
198                         _values[index] = val;
199                 }
200         }
201
202         /* Input ports marked for update because the control value changed
203            since the last redisplay.
204         */
205
206         for (Updates::iterator i = _updates.begin(); i != _updates.end(); ++i) {
207                 float val = _lv2->get_parameter (*i);
208                 /* push current value to the GUI */
209                 suil_instance_port_event ((SuilInstance*)_inst, (*i), 4, 0, &val);
210                 _values[(*i)] = val;
211         }
212
213         _updates.clear ();
214 }
215
216 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
217                          boost::shared_ptr<LV2Plugin>    lv2p)
218         : PlugUIBase(pi)
219         , _pi(pi)
220         , _lv2(lv2p)
221         , _gui_widget(NULL)
222         , _values(NULL)
223         , _external_ui_ptr(NULL)
224         , _inst(NULL)
225 {
226         _ardour_buttons_box.set_spacing (6);
227         _ardour_buttons_box.set_border_width (6);
228         _ardour_buttons_box.pack_end (focus_button, false, false);
229         _ardour_buttons_box.pack_end (bypass_button, false, false, 4);
230         _ardour_buttons_box.pack_end (reset_button, false, false, 4);
231         _ardour_buttons_box.pack_end (delete_button, false, false);
232         _ardour_buttons_box.pack_end (save_button, false, false);
233         _ardour_buttons_box.pack_end (add_button, false, false);
234         _ardour_buttons_box.pack_end (_preset_combo, false, false);
235         _ardour_buttons_box.pack_end (_preset_modified, false, false);
236 }
237
238 void
239 LV2PluginUI::lv2ui_instantiate(const std::string& title)
240 {
241         bool          is_external_ui = _lv2->is_external_ui();
242         LV2_Feature** features_src   = const_cast<LV2_Feature**>(_lv2->features());
243         LV2_Feature** features       = const_cast<LV2_Feature**>(_lv2->features());
244         size_t        features_count = 0;
245         while (*features++) {
246                 features_count++;
247         }
248
249         Gtk::Alignment* container = NULL;
250         if (is_external_ui) {
251                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
252                 _external_ui_host.plugin_human_id = strdup(title.c_str());
253
254                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
255                 _external_ui_feature.data = &_external_ui_host;
256
257                 _external_kxui_feature.URI  = LV2_EXTERNAL_UI_KX__Host;
258                 _external_kxui_feature.data = &_external_ui_host;
259
260                 ++features_count;
261                 features = (LV2_Feature**)malloc(
262                         sizeof(LV2_Feature*) * (features_count + 2));
263                 for (size_t i = 0; i < features_count - 2; ++i) {
264                         features[i] = features_src[i];
265                 }
266                 features[features_count - 2] = &_external_kxui_feature;
267                 features[features_count - 1] = &_external_ui_feature;
268                 features[features_count]     = NULL;
269         } else {
270                 if (_ardour_buttons_box.get_parent()) {
271                         _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
272                 }
273                 pack_start(_ardour_buttons_box, false, false);
274                 _ardour_buttons_box.show_all();
275
276                 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
277                 pack_start(*_gui_widget, true, true);
278                 _gui_widget->show();
279
280                 _parent_feature.URI  = LV2_UI__parent;
281                 _parent_feature.data = _gui_widget->gobj();
282
283                 ++features_count;
284                 features = (LV2_Feature**)malloc(
285                         sizeof(LV2_Feature*) * (features_count + 1));
286                 for (size_t i = 0; i < features_count - 1; ++i) {
287                         features[i] = features_src[i];
288                 }
289                 features[features_count - 1] = &_parent_feature;
290                 features[features_count]     = NULL;
291         }
292
293         if (!ui_host) {
294                 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
295                                         LV2PluginUI::port_index,
296                                         NULL, NULL);
297                 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
298         }
299         const char* container_type = (is_external_ui)
300                 ? NS_UI "external"
301                 : NS_UI "GtkUI";
302
303         if (_lv2->has_message_output()) {
304                 _lv2->enable_ui_emission();
305         }
306
307         const LilvUI*   ui     = (const LilvUI*)_lv2->c_ui();
308         const LilvNode* bundle = lilv_ui_get_bundle_uri(ui);
309         const LilvNode* binary = lilv_ui_get_binary_uri(ui);
310 #ifdef HAVE_LILV_0_21_3
311         char* ui_bundle_path = lilv_file_uri_parse(lilv_node_as_uri(bundle), NULL);
312         char* ui_binary_path = lilv_file_uri_parse(lilv_node_as_uri(binary), NULL);
313 #else
314         char* ui_bundle_path = strdup(lilv_uri_to_path(lilv_node_as_uri(bundle)));
315         char* ui_binary_path = strdup(lilv_uri_to_path(lilv_node_as_uri(binary)));
316 #endif
317         if (!ui_bundle_path || !ui_binary_path) {
318                 error << _("failed to get path for UI bindle or binary") << endmsg;
319                 free(ui_bundle_path);
320                 free(ui_binary_path);
321                 free(features);
322                 return;
323         }
324
325         _inst = suil_instance_new(
326                 ui_host,
327                 this,
328                 container_type,
329                 _lv2->uri(),
330                 lilv_node_as_uri(lilv_ui_get_uri(ui)),
331                 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
332                 ui_bundle_path,
333                 ui_binary_path,
334                 features);
335
336         free(ui_bundle_path);
337         free(ui_binary_path);
338         free(features);
339
340         if (!_inst) {
341                 error << _("failed to instantiate LV2 GUI") << endmsg;
342                 return;
343         }
344
345 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
346
347         const uint32_t num_ports = _lv2->num_ports();
348         for (uint32_t i = 0; i < num_ports; ++i) {
349                 if (_lv2->parameter_is_output(i)
350                     && _lv2->parameter_is_control(i)
351                     && is_update_wanted(i)) {
352                         _output_ports.push_back(i);
353                 }
354         }
355
356         _external_ui_ptr = NULL;
357         if (!is_external_ui) {
358                 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
359                 if (!c_widget) {
360                         error << _("failed to get LV2 UI widget") << endmsg;
361                         suil_instance_free((SuilInstance*)_inst);
362                         _inst = NULL;
363                         return;
364                 }
365                 if (!container->get_child()) {
366                         // Suil didn't add the UI to the container for us, so do it now
367                         container->add(*Gtk::manage(Glib::wrap(c_widget)));
368                 }
369                 container->show_all();
370                 gtk_widget_set_can_focus(c_widget, true);
371                 gtk_widget_grab_focus(c_widget);
372         } else {
373                 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
374         }
375
376         _values = new float[num_ports];
377         _controllables.resize(num_ports);
378
379         for (uint32_t i = 0; i < num_ports; ++i) {
380                 bool     ok;
381                 uint32_t port = _lv2->nth_parameter(i, ok);
382                 if (ok) {
383                         /* Cache initial value of the parameter, regardless of
384                            whether it is input or output
385                         */
386
387                         _values[port]        = _lv2->get_parameter(port);
388                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
389                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
390
391                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
392                                 if (_controllables[port]) {
393                                         _controllables[port]->Changed.connect (control_connections, invalidator (*this), boost::bind (&LV2PluginUI::control_changed, this, port), gui_context());
394                                 }
395                         }
396
397                         /* queue for first update ("push") to GUI */
398                         _updates.insert (port);
399                 }
400         }
401
402         if (_lv2->has_message_output()) {
403                 _message_update_connection = Timers::super_rapid_connect (
404                         sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
405         }
406 }
407
408 void
409 LV2PluginUI::grab_focus()
410 {
411         if (_inst && !_lv2->is_external_ui()) {
412                 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
413                 gtk_widget_grab_focus(c_widget);
414         }
415 }
416
417 void
418 LV2PluginUI::lv2ui_free()
419 {
420         stop_updating (0);
421
422         if (_gui_widget) {
423                 remove (*_gui_widget);
424                 _gui_widget = NULL;
425         }
426
427         if (_inst) {
428                 suil_instance_free((SuilInstance*)_inst);
429                 _inst = NULL;
430         }
431 }
432
433 LV2PluginUI::~LV2PluginUI ()
434 {
435         delete [] _values;
436
437         _message_update_connection.disconnect();
438         _screen_update_connection.disconnect();
439
440         if (_external_ui_ptr && _lv2->is_external_kx()) {
441                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
442         }
443         lv2ui_free();
444         _external_ui_ptr = NULL;
445 }
446
447 int
448 LV2PluginUI::get_preferred_height()
449 {
450         Gtk::Requisition r = size_request();
451         return r.height;
452 }
453
454 int
455 LV2PluginUI::get_preferred_width()
456 {
457         Gtk::Requisition r = size_request();
458         return r.width;
459 }
460
461 bool
462 LV2PluginUI::resizable()
463 {
464         return _lv2->ui_is_resizable();
465 }
466
467 int
468 LV2PluginUI::package(Gtk::Window& win)
469 {
470         if (_external_ui_ptr) {
471                 _win_ptr = &win;
472         } else {
473                 /* forward configure events to plugin window */
474                 win.signal_configure_event().connect(
475                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
476                 win.signal_map_event().connect(
477                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
478                 win.signal_unmap_event().connect(
479                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
480         }
481         return 0;
482 }
483
484 bool
485 LV2PluginUI::configure_handler(GdkEventConfigure*)
486 {
487         std::cout << "CONFIGURE" << std::endl;
488         return false;
489 }
490
491 bool
492 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
493 {
494         /* FIXME: use port notification properties
495            and/or new UI extension subscription methods
496         */
497         return true;
498 }
499
500 bool
501 LV2PluginUI::on_window_show(const std::string& title)
502 {
503         //cout << "on_window_show - " << title << endl; flush(cout);
504
505         if (_lv2->is_external_ui()) {
506                 if (_external_ui_ptr) {
507                         _screen_update_connection.disconnect();
508                         _message_update_connection.disconnect();
509                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
510                         _screen_update_connection = Timers::super_rapid_connect
511                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
512                         if (_lv2->has_message_output()) {
513                                 _message_update_connection = Timers::super_rapid_connect (
514                                         sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
515                         }
516                         return false;
517                 }
518                 lv2ui_instantiate(title);
519                 if (!_external_ui_ptr) {
520                         return false;
521                 }
522
523                 _screen_update_connection.disconnect();
524                 _message_update_connection.disconnect();
525                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
526                 _screen_update_connection = Timers::super_rapid_connect
527                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
528                 if (_lv2->has_message_output()) {
529                         _message_update_connection = Timers::super_rapid_connect (
530                                 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
531                 }
532                 return false;
533         } else {
534                 lv2ui_instantiate("gtk2gui");
535         }
536
537         return _inst ? true : false;
538 }
539
540 void
541 LV2PluginUI::on_window_hide()
542 {
543         //printf("LV2PluginUI::on_window_hide\n");
544
545         if (_lv2->is_external_ui()) {
546                 if (!_external_ui_ptr) { return; }
547                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
548                 if (!_lv2->is_external_kx()) { return ; }
549                 _message_update_connection.disconnect();
550                 _screen_update_connection.disconnect();
551                 _external_ui_ptr = NULL;
552                 suil_instance_free((SuilInstance*)_inst);
553                 _inst = NULL;
554         } else {
555                 lv2ui_free();
556         }
557 }