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