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