2 Copyright (C) 2008-2012 Paul Davis
3 Author: David Robillard
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.
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.
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.
20 #include "ardour/lv2_plugin.h"
21 #include "ardour/session.h"
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
25 #include "gui_thread.h"
26 #include "lv2_plugin_ui.h"
29 #include "gtkmm2ext/utils.h"
31 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
33 #include <lilv/lilv.h>
34 #include <suil/suil.h>
38 using namespace ARDOUR;
42 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
44 static SuilHost* ui_host = NULL;
47 LV2PluginUI::write_from_ui(void* controller,
53 LV2PluginUI* me = (LV2PluginUI*)controller;
55 if (port_index >= me->_controllables.size()) {
59 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
61 me->_updates.insert (port_index);
64 ac->set_value(*(const float*)buffer, Controllable::NoGroup);
66 } else if (format == URIMap::instance().urids.atom_eventTransfer) {
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);
77 LV2PluginUI::write_to_ui(void* controller,
83 LV2PluginUI* me = (LV2PluginUI*)controller;
85 suil_instance_port_event((SuilInstance*)me->_inst,
86 port_index, buffer_size, format, buffer);
91 LV2PluginUI::port_index(void* controller, const char* symbol)
93 return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
97 LV2PluginUI::touch(void* controller,
101 LV2PluginUI* me = (LV2PluginUI*)controller;
102 if (port_index >= me->_controllables.size()) {
105 if (!me->_lv2->parameter_is_control(port_index) || !me->_lv2->parameter_is_input(port_index)) {
109 ControllableRef control = me->_controllables[port_index];
111 control->start_touch(control->session().transport_sample());
113 control->stop_touch(control->session().transport_sample());
118 LV2PluginUI::request_parameter (void* handle, LV2_URID key)
120 LV2PluginUI* me = (LV2PluginUI*)handle;
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) {
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.
132 Gtk::FileChooserDialog lv2ui_file_dialog (desc.label, FILE_CHOOSER_ACTION_OPEN);
133 Gtkmm2ext::add_volume_shortcuts (lv2ui_file_dialog);
135 /* LV2Plugin does not currently save property values and only
136 * emits a PropertyChanged(urid, Variant) signal
138 //lv2ui_file_dialog.set_current_folder (TODO)
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);
146 int response = lv2ui_file_dialog.run();
147 lv2ui_file_dialog.hide ();
149 if (response == Gtk::RESPONSE_OK) {
150 me->plugin->set_property (desc.key, Variant(Variant::PATH, lv2ui_file_dialog.get_filename()));
157 LV2PluginUI::update_timeout()
159 _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
163 LV2PluginUI::on_external_ui_closed(void* controller)
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;
172 LV2PluginUI::control_changed (uint32_t port_index)
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
180 _updates.insert (port_index);
185 LV2PluginUI::start_updating(GdkEventAny*)
187 _screen_update_connection.disconnect();
188 _screen_update_connection = Timers::super_rapid_connect
189 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
194 LV2PluginUI::stop_updating(GdkEventAny*)
196 //cout << "stop_updating" << endl;
197 _screen_update_connection.disconnect();
202 LV2PluginUI::queue_port_update()
204 const uint32_t num_ports = _lv2->num_ports();
205 for (uint32_t i = 0; i < num_ports; ++i) {
207 uint32_t port = _lv2->nth_parameter(i, ok);
209 _updates.insert (port);
215 LV2PluginUI::output_update()
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();
227 suil_instance_free((SuilInstance*)_inst);
230 _external_ui_ptr = NULL;
239 /* output ports (values set by DSP) need propagating to GUI */
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);
246 if (val != _values_last_sent_to_ui[index]) {
248 suil_instance_port_event ((SuilInstance*)_inst, index, 4, 0, &val);
249 /* Cache current value */
250 _values_last_sent_to_ui[index] = val;
254 /* Input ports marked for update because the control value changed
255 since the last redisplay.
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;
268 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
269 boost::shared_ptr<LV2Plugin> lv2p)
274 , _values_last_sent_to_ui(NULL)
275 , _external_ui_ptr(NULL)
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);
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);
292 plugin->PresetLoaded.connect (*this, invalidator (*this), boost::bind (&LV2PluginUI::queue_port_update, this), gui_context ());
296 LV2PluginUI::lv2ui_instantiate(const std::string& title)
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++) {
306 if (is_external_ui) {
307 features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * (features_count + 4));
309 features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * (features_count + 3));
313 for (; fi < features_count; ++fi) {
314 features[fi] = features_src[fi];
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;
322 features[fi++] = &_lv2ui_request_feature;
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());
329 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
330 _external_ui_feature.data = &_external_ui_host;
332 _external_kxui_feature.URI = LV2_EXTERNAL_UI_KX__Host;
333 _external_kxui_feature.data = &_external_ui_host;
335 features[fi++] = &_external_kxui_feature;
336 features[fi++] = &_external_ui_feature;
338 if (_ardour_buttons_box.get_parent()) {
339 _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
341 pack_start(_ardour_buttons_box, false, false);
342 _ardour_buttons_box.show_all();
344 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
345 pack_start(*_gui_widget, true, true);
348 _parent_feature.URI = LV2_UI__parent;
349 _parent_feature.data = _gui_widget->gobj();
351 features[fi++] = &_parent_feature;
355 assert (fi == features_count + (is_external_ui ? 3 : 2));
358 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
359 LV2PluginUI::port_index,
361 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
363 const char* container_type = (is_external_ui)
367 if (_lv2->has_message_output()) {
368 _lv2->enable_ui_emission();
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);
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)));
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);
389 _inst = suil_instance_new(
394 lilv_node_as_uri(lilv_ui_get_uri(ui)),
395 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
400 free(ui_bundle_path);
401 free(ui_binary_path);
405 error << _("failed to instantiate LV2 GUI") << endmsg;
409 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
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);
420 _external_ui_ptr = NULL;
421 if (!is_external_ui) {
422 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
424 error << _("failed to get LV2 UI widget") << endmsg;
425 suil_instance_free((SuilInstance*)_inst);
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)));
433 container->show_all();
434 gtk_widget_set_can_focus(c_widget, true);
435 gtk_widget_grab_focus(c_widget);
437 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
440 _values_last_sent_to_ui = new float[num_ports];
441 _controllables.resize(num_ports);
443 for (uint32_t i = 0; i < num_ports; ++i) {
445 uint32_t port = _lv2->nth_parameter(i, ok);
447 /* Cache initial value of the parameter, regardless of
448 whether it is input or output
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)));
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());
461 /* queue for first update ("push") to GUI */
462 _updates.insert (port);
466 if (_lv2->has_message_output()) {
467 _message_update_connection = Timers::super_rapid_connect (
468 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
473 LV2PluginUI::grab_focus()
475 if (_inst && !_lv2->is_external_ui()) {
476 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
477 gtk_widget_grab_focus(c_widget);
482 LV2PluginUI::lv2ui_free()
487 remove (*_gui_widget);
492 suil_instance_free((SuilInstance*)_inst);
497 LV2PluginUI::~LV2PluginUI ()
499 delete [] _values_last_sent_to_ui;
501 _message_update_connection.disconnect();
502 _screen_update_connection.disconnect();
504 if (_external_ui_ptr && _lv2->is_external_kx()) {
505 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
508 _external_ui_ptr = NULL;
512 LV2PluginUI::get_preferred_height()
514 Gtk::Requisition r = size_request();
519 LV2PluginUI::get_preferred_width()
521 Gtk::Requisition r = size_request();
526 LV2PluginUI::resizable()
528 return _lv2->ui_is_resizable();
532 LV2PluginUI::package(Gtk::Window& win)
534 if (_external_ui_ptr) {
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));
549 LV2PluginUI::configure_handler(GdkEventConfigure*)
551 std::cout << "CONFIGURE" << std::endl;
556 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
558 /* FIXME: use port notification properties
559 and/or new UI extension subscription methods
565 LV2PluginUI::on_window_show(const std::string& title)
567 //cout << "on_window_show - " << title << endl; flush(cout);
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));
582 lv2ui_instantiate(title);
583 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));
598 lv2ui_instantiate("gtk2gui");
601 return _inst ? true : false;
605 LV2PluginUI::on_window_hide()
607 //printf("LV2PluginUI::on_window_hide\n");
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);