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