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>
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.
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.
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.
22 #include <gtkmm/stock.h>
24 #include "ardour/lv2_plugin.h"
25 #include "ardour/session.h"
26 #include "pbd/error.h"
27 #include "pbd/stacktrace.h"
29 #include "gui_thread.h"
30 #include "lv2_plugin_ui.h"
33 #include "gtkmm2ext/utils.h"
35 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
37 #include <lilv/lilv.h>
38 #include <suil/suil.h>
42 using namespace ARDOUR;
46 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
48 static SuilHost* ui_host = NULL;
51 LV2PluginUI::write_from_ui(void* controller,
57 LV2PluginUI* me = (LV2PluginUI*)controller;
59 if (port_index >= me->_controllables.size()) {
63 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
65 me->_updates.insert (port_index);
68 ac->set_value(*(const float*)buffer, Controllable::NoGroup);
70 } else if (format == URIMap::instance().urids.atom_eventTransfer) {
72 const int cnt = me->_pi->get_count();
73 for (int i=0; i < cnt; i++ ) {
74 boost::shared_ptr<LV2Plugin> lv2i = boost::dynamic_pointer_cast<LV2Plugin> (me->_pi->plugin(i));
75 lv2i->write_from_ui(port_index, format, buffer_size, (const uint8_t*)buffer);
81 LV2PluginUI::write_to_ui(void* controller,
87 LV2PluginUI* me = (LV2PluginUI*)controller;
89 suil_instance_port_event((SuilInstance*)me->_inst,
90 port_index, buffer_size, format, buffer);
95 LV2PluginUI::port_index(void* controller, const char* symbol)
97 return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
101 LV2PluginUI::touch(void* controller,
105 LV2PluginUI* me = (LV2PluginUI*)controller;
106 if (port_index >= me->_controllables.size()) {
109 if (!me->_lv2->parameter_is_control(port_index) || !me->_lv2->parameter_is_input(port_index)) {
113 ControllableRef control = me->_controllables[port_index];
115 control->start_touch(control->session().transport_sample());
117 control->stop_touch(control->session().transport_sample());
122 LV2PluginUI::set_path_property (int response,
123 const ParameterDescriptor& desc,
124 Gtk::FileChooserDialog* widget)
126 if (response == Gtk::RESPONSE_ACCEPT) {
127 plugin->set_property (desc.key, Variant (Variant::PATH, widget->get_filename()));
131 delete_when_idle (widget);
135 active_parameter_requests.erase (desc.key);
139 LV2PluginUI::request_parameter (void* handle, LV2_URID key)
141 LV2PluginUI* me = (LV2PluginUI*)handle;
143 /* This will return `PropertyDescriptors nothing` when not found */
144 const ParameterDescriptor& desc (me->_lv2->get_property_descriptor(key));
145 if (desc.datatype != Variant::PATH) {
149 if (me->active_parameter_requests.find (key) != me->active_parameter_requests.end()) {
150 return 0; /* already showing dialog */
152 me->active_parameter_requests.insert (key);
154 Gtk::FileChooserDialog* lv2ui_file_dialog = new Gtk::FileChooserDialog(desc.label, FILE_CHOOSER_ACTION_OPEN);
155 Gtkmm2ext::add_volume_shortcuts (*lv2ui_file_dialog);
156 lv2ui_file_dialog->add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
157 lv2ui_file_dialog->add_button (Gtk::Stock::OPEN, Gtk::RESPONSE_ACCEPT);
158 lv2ui_file_dialog->set_default_response(Gtk::RESPONSE_ACCEPT);
160 /* this assumes announce_property_values() was called, or
161 * the plugin has previously sent a patch:Set */
162 const Variant& value = me->_lv2->get_property_value (desc.key);
163 if (value.type() == Variant::PATH) {
164 lv2ui_file_dialog->set_filename (value.get_path());
167 #if 0 // TODO mime-type, file-extension filter, get from LV2 Parameter Property
168 FileFilter file_ext_filter;
169 file_ext_filter.add_pattern ("*.foo");
170 file_ext_filter.set_name ("Foo File");
171 lv2ui_file_dialog.add_filter (file_ext_filter);
174 lv2ui_file_dialog->signal_response().connect (sigc::bind (sigc::mem_fun (*me, &LV2PluginUI::set_path_property), desc, lv2ui_file_dialog));
175 lv2ui_file_dialog->present();
180 LV2PluginUI::update_timeout()
182 _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
186 LV2PluginUI::on_external_ui_closed(void* controller)
188 //printf("LV2PluginUI::on_external_ui_closed\n");
189 LV2PluginUI* me = (LV2PluginUI*)controller;
190 me->_screen_update_connection.disconnect();
191 me->_external_ui_ptr = NULL;
195 LV2PluginUI::control_changed (uint32_t port_index)
197 /* Must run in GUI thread because we modify _updates with no lock */
198 if (_lv2->get_parameter (port_index) != _values_last_sent_to_ui[port_index]) {
199 /* current plugin parameter does not match last value received
200 from GUI, so queue an update to push it to the GUI during
203 _updates.insert (port_index);
208 LV2PluginUI::start_updating(GdkEventAny*)
210 _screen_update_connection.disconnect();
211 _screen_update_connection = Timers::super_rapid_connect
212 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
217 LV2PluginUI::stop_updating(GdkEventAny*)
219 //cout << "stop_updating" << endl;
220 _screen_update_connection.disconnect();
225 LV2PluginUI::queue_port_update()
227 const uint32_t num_ports = _lv2->num_ports();
228 for (uint32_t i = 0; i < num_ports; ++i) {
230 uint32_t port = _lv2->nth_parameter(i, ok);
232 _updates.insert (port);
238 LV2PluginUI::output_update()
240 //cout << "output_update" << endl;
241 if (_external_ui_ptr) {
242 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
243 if (_lv2->is_external_kx() && !_external_ui_ptr) {
244 // clean up external UI if it closes itself via
245 // on_external_ui_closed() during run()
246 //printf("LV2PluginUI::output_update -- UI was closed\n");
247 //_screen_update_connection.disconnect();
248 _message_update_connection.disconnect();
250 suil_instance_free((SuilInstance*)_inst);
253 _external_ui_ptr = NULL;
262 /* output ports (values set by DSP) need propagating to GUI */
264 uint32_t nports = _output_ports.size();
265 for (uint32_t i = 0; i < nports; ++i) {
266 uint32_t index = _output_ports[i];
267 float val = _lv2->get_parameter (index);
269 if (val != _values_last_sent_to_ui[index]) {
271 suil_instance_port_event ((SuilInstance*)_inst, index, 4, 0, &val);
272 /* Cache current value */
273 _values_last_sent_to_ui[index] = val;
277 /* Input ports marked for update because the control value changed
278 since the last redisplay.
281 for (Updates::iterator i = _updates.begin(); i != _updates.end(); ++i) {
282 float val = _lv2->get_parameter (*i);
283 /* push current value to the GUI */
284 suil_instance_port_event ((SuilInstance*)_inst, (*i), 4, 0, &val);
285 _values_last_sent_to_ui[(*i)] = val;
291 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
292 boost::shared_ptr<LV2Plugin> lv2p)
297 , _values_last_sent_to_ui(NULL)
298 , _external_ui_ptr(NULL)
301 _ardour_buttons_box.set_spacing (6);
302 _ardour_buttons_box.set_border_width (6);
303 _ardour_buttons_box.pack_end (focus_button, false, false);
304 _ardour_buttons_box.pack_end (bypass_button, false, false, 4);
305 if (pi->controls().size() > 0) {
306 _ardour_buttons_box.pack_end (reset_button, false, false, 4);
308 if (has_descriptive_presets ()) {
309 _ardour_buttons_box.pack_end (preset_browser_button, false, false);
311 _ardour_buttons_box.pack_end (delete_button, false, false);
312 _ardour_buttons_box.pack_end (save_button, false, false);
313 _ardour_buttons_box.pack_end (add_button, false, false);
314 _ardour_buttons_box.pack_end (_preset_combo, false, false);
315 _ardour_buttons_box.pack_end (_preset_modified, false, false);
316 _ardour_buttons_box.pack_end (pin_management_button, false, false);
318 plugin->PresetLoaded.connect (*this, invalidator (*this), boost::bind (&LV2PluginUI::queue_port_update, this), gui_context ());
322 LV2PluginUI::lv2ui_instantiate(const std::string& title)
324 bool is_external_ui = _lv2->is_external_ui();
325 LV2_Feature** features_src = const_cast<LV2_Feature**>(_lv2->features());
326 LV2_Feature** features = const_cast<LV2_Feature**>(_lv2->features());
327 size_t features_count = 0;
328 while (*features++) {
332 if (is_external_ui) {
333 features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * (features_count + 4));
335 features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * (features_count + 3));
339 for (; fi < features_count; ++fi) {
340 features[fi] = features_src[fi];
343 _lv2ui_request_paramater.handle = this;
344 _lv2ui_request_paramater.request = LV2PluginUI::request_parameter;
345 _lv2ui_request_feature.URI = LV2_UI_PREFIX "requestParameter";
346 _lv2ui_request_feature.data = &_lv2ui_request_paramater;
348 features[fi++] = &_lv2ui_request_feature;
350 Gtk::Alignment* container = NULL;
351 if (is_external_ui) {
352 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
353 _external_ui_host.plugin_human_id = strdup(title.c_str());
355 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
356 _external_ui_feature.data = &_external_ui_host;
358 _external_kxui_feature.URI = LV2_EXTERNAL_UI_KX__Host;
359 _external_kxui_feature.data = &_external_ui_host;
361 features[fi++] = &_external_kxui_feature;
362 features[fi++] = &_external_ui_feature;
364 if (_ardour_buttons_box.get_parent()) {
365 _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
367 pack_start(_ardour_buttons_box, false, false);
368 _ardour_buttons_box.show_all();
370 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
371 pack_start(*_gui_widget, true, true);
374 _parent_feature.URI = LV2_UI__parent;
375 _parent_feature.data = _gui_widget->gobj();
377 features[fi++] = &_parent_feature;
381 assert (fi == features_count + (is_external_ui ? 3 : 2));
384 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
385 LV2PluginUI::port_index,
387 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
389 const char* container_type = (is_external_ui)
393 if (_lv2->has_message_output()) {
394 _lv2->enable_ui_emission();
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);
410 _inst = suil_instance_new(
415 lilv_node_as_uri(lilv_ui_get_uri(ui)),
416 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
421 free(ui_bundle_path);
422 free(ui_binary_path);
426 error << _("failed to instantiate LV2 GUI") << endmsg;
430 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
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);
441 _external_ui_ptr = NULL;
442 if (!is_external_ui) {
443 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
445 error << _("failed to get LV2 UI widget") << endmsg;
446 suil_instance_free((SuilInstance*)_inst);
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)));
454 container->show_all();
455 gtk_widget_set_can_focus(c_widget, true);
456 gtk_widget_grab_focus(c_widget);
458 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
461 _values_last_sent_to_ui = new float[num_ports];
462 _controllables.resize(num_ports);
464 for (uint32_t i = 0; i < num_ports; ++i) {
466 uint32_t port = _lv2->nth_parameter(i, ok);
468 /* Cache initial value of the parameter, regardless of
469 whether it is input or output
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)));
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());
482 /* queue for first update ("push") to GUI */
483 _updates.insert (port);
487 if (_lv2->has_message_output()) {
488 _message_update_connection = Timers::super_rapid_connect (
489 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
494 LV2PluginUI::grab_focus()
496 if (_inst && !_lv2->is_external_ui()) {
497 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
498 gtk_widget_grab_focus(c_widget);
503 LV2PluginUI::lv2ui_free()
508 remove (*_gui_widget);
513 suil_instance_free((SuilInstance*)_inst);
518 LV2PluginUI::~LV2PluginUI ()
520 delete [] _values_last_sent_to_ui;
522 _message_update_connection.disconnect();
523 _screen_update_connection.disconnect();
525 if (_external_ui_ptr && _lv2->is_external_kx()) {
526 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
529 _external_ui_ptr = NULL;
533 LV2PluginUI::get_preferred_height()
535 Gtk::Requisition r = size_request();
540 LV2PluginUI::get_preferred_width()
542 Gtk::Requisition r = size_request();
547 LV2PluginUI::resizable()
549 return _lv2->ui_is_resizable();
553 LV2PluginUI::package(Gtk::Window& win)
555 if (_external_ui_ptr) {
558 /* forward configure events to plugin window */
559 win.signal_configure_event().connect(
560 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
561 win.signal_map_event().connect(
562 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
563 win.signal_unmap_event().connect(
564 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
570 LV2PluginUI::configure_handler(GdkEventConfigure*)
572 std::cout << "CONFIGURE" << std::endl;
577 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
579 /* FIXME: use port notification properties
580 and/or new UI extension subscription methods
586 LV2PluginUI::on_window_show(const std::string& title)
588 //cout << "on_window_show - " << title << endl; flush(cout);
590 if (_lv2->is_external_ui()) {
591 if (_external_ui_ptr) {
592 _screen_update_connection.disconnect();
593 _message_update_connection.disconnect();
594 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
595 _screen_update_connection = Timers::super_rapid_connect
596 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
597 if (_lv2->has_message_output()) {
598 _message_update_connection = Timers::super_rapid_connect (
599 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
603 lv2ui_instantiate(title);
604 if (!_external_ui_ptr) {
608 _screen_update_connection.disconnect();
609 _message_update_connection.disconnect();
610 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
611 _screen_update_connection = Timers::super_rapid_connect
612 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
613 if (_lv2->has_message_output()) {
614 _message_update_connection = Timers::super_rapid_connect (
615 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
619 lv2ui_instantiate("gtk2gui");
622 return _inst ? true : false;
626 LV2PluginUI::on_window_hide()
628 //printf("LV2PluginUI::on_window_hide\n");
630 if (_lv2->is_external_ui()) {
631 if (!_external_ui_ptr) { return; }
632 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
633 if (!_lv2->is_external_kx()) { return ; }
634 _message_update_connection.disconnect();
635 _screen_update_connection.disconnect();
636 _external_ui_ptr = NULL;
637 suil_instance_free((SuilInstance*)_inst);