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 <gtkmm/stock.h>
22 #include "ardour/lv2_plugin.h"
23 #include "ardour/session.h"
24 #include "pbd/error.h"
25 #include "pbd/stacktrace.h"
27 #include "gui_thread.h"
28 #include "lv2_plugin_ui.h"
31 #include "gtkmm2ext/utils.h"
33 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
35 #include <lilv/lilv.h>
36 #include <suil/suil.h>
40 using namespace ARDOUR;
44 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
46 static SuilHost* ui_host = NULL;
49 LV2PluginUI::write_from_ui(void* controller,
55 LV2PluginUI* me = (LV2PluginUI*)controller;
57 if (port_index >= me->_controllables.size()) {
61 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
63 me->_updates.insert (port_index);
66 ac->set_value(*(const float*)buffer, Controllable::NoGroup);
68 } else if (format == URIMap::instance().urids.atom_eventTransfer) {
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);
79 LV2PluginUI::write_to_ui(void* controller,
85 LV2PluginUI* me = (LV2PluginUI*)controller;
87 suil_instance_port_event((SuilInstance*)me->_inst,
88 port_index, buffer_size, format, buffer);
93 LV2PluginUI::port_index(void* controller, const char* symbol)
95 return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
99 LV2PluginUI::touch(void* controller,
103 LV2PluginUI* me = (LV2PluginUI*)controller;
104 if (port_index >= me->_controllables.size()) {
107 if (!me->_lv2->parameter_is_control(port_index) || !me->_lv2->parameter_is_input(port_index)) {
111 ControllableRef control = me->_controllables[port_index];
113 control->start_touch(control->session().transport_sample());
115 control->stop_touch(control->session().transport_sample());
120 LV2PluginUI::set_path_property (int response,
121 const ParameterDescriptor& desc,
122 Gtk::FileChooserDialog* widget)
124 if (response == Gtk::RESPONSE_ACCEPT) {
125 plugin->set_property (desc.key, Variant (Variant::PATH, widget->get_filename()));
129 delete_when_idle (widget);
133 active_parameter_requests.erase (desc.key);
137 LV2PluginUI::request_parameter (void* handle, LV2_URID key)
139 LV2PluginUI* me = (LV2PluginUI*)handle;
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) {
147 if (me->active_parameter_requests.find (key) != me->active_parameter_requests.end()) {
148 return 0; /* already showing dialog */
150 me->active_parameter_requests.insert (key);
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);
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());
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);
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();
178 LV2PluginUI::update_timeout()
180 _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
184 LV2PluginUI::on_external_ui_closed(void* controller)
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;
193 LV2PluginUI::control_changed (uint32_t port_index)
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
201 _updates.insert (port_index);
206 LV2PluginUI::start_updating(GdkEventAny*)
208 _screen_update_connection.disconnect();
209 _screen_update_connection = Timers::super_rapid_connect
210 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
215 LV2PluginUI::stop_updating(GdkEventAny*)
217 //cout << "stop_updating" << endl;
218 _screen_update_connection.disconnect();
223 LV2PluginUI::queue_port_update()
225 const uint32_t num_ports = _lv2->num_ports();
226 for (uint32_t i = 0; i < num_ports; ++i) {
228 uint32_t port = _lv2->nth_parameter(i, ok);
230 _updates.insert (port);
236 LV2PluginUI::output_update()
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();
248 suil_instance_free((SuilInstance*)_inst);
251 _external_ui_ptr = NULL;
260 /* output ports (values set by DSP) need propagating to GUI */
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);
267 if (val != _values_last_sent_to_ui[index]) {
269 suil_instance_port_event ((SuilInstance*)_inst, index, 4, 0, &val);
270 /* Cache current value */
271 _values_last_sent_to_ui[index] = val;
275 /* Input ports marked for update because the control value changed
276 since the last redisplay.
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;
289 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
290 boost::shared_ptr<LV2Plugin> lv2p)
295 , _values_last_sent_to_ui(NULL)
296 , _external_ui_ptr(NULL)
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);
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);
313 plugin->PresetLoaded.connect (*this, invalidator (*this), boost::bind (&LV2PluginUI::queue_port_update, this), gui_context ());
317 LV2PluginUI::lv2ui_instantiate(const std::string& title)
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++) {
327 if (is_external_ui) {
328 features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * (features_count + 4));
330 features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * (features_count + 3));
334 for (; fi < features_count; ++fi) {
335 features[fi] = features_src[fi];
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;
343 features[fi++] = &_lv2ui_request_feature;
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());
350 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
351 _external_ui_feature.data = &_external_ui_host;
353 _external_kxui_feature.URI = LV2_EXTERNAL_UI_KX__Host;
354 _external_kxui_feature.data = &_external_ui_host;
356 features[fi++] = &_external_kxui_feature;
357 features[fi++] = &_external_ui_feature;
359 if (_ardour_buttons_box.get_parent()) {
360 _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
362 pack_start(_ardour_buttons_box, false, false);
363 _ardour_buttons_box.show_all();
365 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
366 pack_start(*_gui_widget, true, true);
369 _parent_feature.URI = LV2_UI__parent;
370 _parent_feature.data = _gui_widget->gobj();
372 features[fi++] = &_parent_feature;
376 assert (fi == features_count + (is_external_ui ? 3 : 2));
379 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
380 LV2PluginUI::port_index,
382 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
384 const char* container_type = (is_external_ui)
388 if (_lv2->has_message_output()) {
389 _lv2->enable_ui_emission();
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);
405 _inst = suil_instance_new(
410 lilv_node_as_uri(lilv_ui_get_uri(ui)),
411 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
416 free(ui_bundle_path);
417 free(ui_binary_path);
421 error << _("failed to instantiate LV2 GUI") << endmsg;
425 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
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);
436 _external_ui_ptr = NULL;
437 if (!is_external_ui) {
438 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
440 error << _("failed to get LV2 UI widget") << endmsg;
441 suil_instance_free((SuilInstance*)_inst);
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)));
449 container->show_all();
450 gtk_widget_set_can_focus(c_widget, true);
451 gtk_widget_grab_focus(c_widget);
453 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
456 _values_last_sent_to_ui = new float[num_ports];
457 _controllables.resize(num_ports);
459 for (uint32_t i = 0; i < num_ports; ++i) {
461 uint32_t port = _lv2->nth_parameter(i, ok);
463 /* Cache initial value of the parameter, regardless of
464 whether it is input or output
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)));
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());
477 /* queue for first update ("push") to GUI */
478 _updates.insert (port);
482 if (_lv2->has_message_output()) {
483 _message_update_connection = Timers::super_rapid_connect (
484 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
489 LV2PluginUI::grab_focus()
491 if (_inst && !_lv2->is_external_ui()) {
492 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
493 gtk_widget_grab_focus(c_widget);
498 LV2PluginUI::lv2ui_free()
503 remove (*_gui_widget);
508 suil_instance_free((SuilInstance*)_inst);
513 LV2PluginUI::~LV2PluginUI ()
515 delete [] _values_last_sent_to_ui;
517 _message_update_connection.disconnect();
518 _screen_update_connection.disconnect();
520 if (_external_ui_ptr && _lv2->is_external_kx()) {
521 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
524 _external_ui_ptr = NULL;
528 LV2PluginUI::get_preferred_height()
530 Gtk::Requisition r = size_request();
535 LV2PluginUI::get_preferred_width()
537 Gtk::Requisition r = size_request();
542 LV2PluginUI::resizable()
544 return _lv2->ui_is_resizable();
548 LV2PluginUI::package(Gtk::Window& win)
550 if (_external_ui_ptr) {
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));
565 LV2PluginUI::configure_handler(GdkEventConfigure*)
567 std::cout << "CONFIGURE" << std::endl;
572 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
574 /* FIXME: use port notification properties
575 and/or new UI extension subscription methods
581 LV2PluginUI::on_window_show(const std::string& title)
583 //cout << "on_window_show - " << title << endl; flush(cout);
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));
598 lv2ui_instantiate(title);
599 if (!_external_ui_ptr) {
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));
614 lv2ui_instantiate("gtk2gui");
617 return _inst ? true : false;
621 LV2PluginUI::on_window_hide()
623 //printf("LV2PluginUI::on_window_hide\n");
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);