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 "lv2/lv2plug.in/ns/extensions/ui/ui.h"
31 #include <lilv/lilv.h>
32 #include <suil/suil.h>
36 using namespace ARDOUR;
40 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
42 static SuilHost* ui_host = NULL;
45 LV2PluginUI::write_from_ui(void* controller,
51 LV2PluginUI* me = (LV2PluginUI*)controller;
53 if (port_index >= me->_controllables.size()) {
57 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
58 /* Cache our local copy of the last value received from the GUI */
59 me->_values[port_index] = *(const float*) buffer;
60 /* Now update the control itself */
62 ac->set_value(*(const float*)buffer);
64 } else if (format == URIMap::instance().urids.atom_eventTransfer) {
66 const int cnt = me->_pi->get_count();
67 for (int i=0; i < cnt; i++ ) {
68 boost::shared_ptr<LV2Plugin> lv2i = boost::dynamic_pointer_cast<LV2Plugin> (me->_pi->plugin(i));
69 lv2i->write_from_ui(port_index, format, buffer_size, (const uint8_t*)buffer);
75 LV2PluginUI::write_to_ui(void* controller,
81 LV2PluginUI* me = (LV2PluginUI*)controller;
83 suil_instance_port_event((SuilInstance*)me->_inst,
84 port_index, buffer_size, format, buffer);
89 LV2PluginUI::port_index(void* controller, const char* symbol)
91 return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
95 LV2PluginUI::touch(void* controller,
99 LV2PluginUI* me = (LV2PluginUI*)controller;
100 if (port_index >= me->_controllables.size()) {
104 ControllableRef control = me->_controllables[port_index];
106 control->start_touch(control->session().transport_frame());
108 control->stop_touch(false, control->session().transport_frame());
113 LV2PluginUI::update_timeout()
115 _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
119 LV2PluginUI::on_external_ui_closed(void* controller)
121 //printf("LV2PluginUI::on_external_ui_closed\n");
122 LV2PluginUI* me = (LV2PluginUI*)controller;
123 me->_screen_update_connection.disconnect();
124 me->_external_ui_ptr = NULL;
128 LV2PluginUI::control_changed (uint32_t port_index)
130 /* Must run in GUI thread because we modify _updates with no lock */
131 if (_lv2->get_parameter (port_index) != _values[port_index]) {
132 /* current plugin parameter does not match last value received
133 from GUI, so queue an update to push it to the GUI during
136 _updates.insert (port_index);
141 LV2PluginUI::start_updating(GdkEventAny*)
143 if (!_output_ports.empty()) {
144 _screen_update_connection.disconnect();
145 _screen_update_connection = Timers::super_rapid_connect
146 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
152 LV2PluginUI::stop_updating(GdkEventAny*)
154 //cout << "stop_updating" << endl;
156 if (!_output_ports.empty()) {
157 _screen_update_connection.disconnect();
163 LV2PluginUI::output_update()
165 //cout << "output_update" << endl;
166 if (_external_ui_ptr) {
167 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
168 if (_lv2->is_external_kx() && !_external_ui_ptr) {
169 // clean up external UI if it closes itself via
170 // on_external_ui_closed() during run()
171 //printf("LV2PluginUI::output_update -- UI was closed\n");
172 //_screen_update_connection.disconnect();
173 _message_update_connection.disconnect();
175 suil_instance_free((SuilInstance*)_inst);
178 _external_ui_ptr = NULL;
184 for (Updates::iterator i = _updates.begin(); i != _updates.end(); ++i) {
185 float val = _lv2->get_parameter (*i);
186 /* push current value to the GUI */
187 suil_instance_port_event ((SuilInstance*)_inst, (*i), 4, 0, &val);
193 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
194 boost::shared_ptr<LV2Plugin> lv2p)
200 , _external_ui_ptr(NULL)
203 _ardour_buttons_box.set_spacing (6);
204 _ardour_buttons_box.set_border_width (6);
205 _ardour_buttons_box.pack_end (focus_button, false, false);
206 _ardour_buttons_box.pack_end (bypass_button, false, false, 4);
207 _ardour_buttons_box.pack_end (reset_button, false, false, 4);
208 _ardour_buttons_box.pack_end (delete_button, false, false);
209 _ardour_buttons_box.pack_end (save_button, false, false);
210 _ardour_buttons_box.pack_end (add_button, false, false);
211 _ardour_buttons_box.pack_end (_preset_combo, false, false);
212 _ardour_buttons_box.pack_end (_preset_modified, false, false);
216 LV2PluginUI::lv2ui_instantiate(const std::string& title)
218 bool is_external_ui = _lv2->is_external_ui();
219 LV2_Feature** features_src = const_cast<LV2_Feature**>(_lv2->features());
220 LV2_Feature** features = const_cast<LV2_Feature**>(_lv2->features());
221 size_t features_count = 0;
222 while (*features++) {
226 Gtk::Alignment* container = NULL;
227 if (is_external_ui) {
228 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
229 _external_ui_host.plugin_human_id = strdup(title.c_str());
231 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
232 _external_ui_feature.data = &_external_ui_host;
234 _external_kxui_feature.URI = LV2_EXTERNAL_UI_KX__Host;
235 _external_kxui_feature.data = &_external_ui_host;
238 features = (LV2_Feature**)malloc(
239 sizeof(LV2_Feature*) * (features_count + 2));
240 for (size_t i = 0; i < features_count - 2; ++i) {
241 features[i] = features_src[i];
243 features[features_count - 2] = &_external_kxui_feature;
244 features[features_count - 1] = &_external_ui_feature;
245 features[features_count] = NULL;
247 if (_ardour_buttons_box.get_parent()) {
248 _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
250 pack_start(_ardour_buttons_box, false, false);
251 _ardour_buttons_box.show_all();
253 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
254 pack_start(*_gui_widget, true, true);
257 _parent_feature.URI = LV2_UI__parent;
258 _parent_feature.data = _gui_widget->gobj();
261 features = (LV2_Feature**)malloc(
262 sizeof(LV2_Feature*) * (features_count + 1));
263 for (size_t i = 0; i < features_count - 1; ++i) {
264 features[i] = features_src[i];
266 features[features_count - 1] = &_parent_feature;
267 features[features_count] = NULL;
271 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
272 LV2PluginUI::port_index,
274 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
276 const char* container_type = (is_external_ui)
280 if (_lv2->has_message_output()) {
281 _lv2->enable_ui_emission();
284 const LilvUI* ui = (const LilvUI*)_lv2->c_ui();
285 const LilvNode* bundle = lilv_ui_get_bundle_uri(ui);
286 const LilvNode* binary = lilv_ui_get_binary_uri(ui);
287 #ifdef HAVE_LILV_0_21_3
288 char* ui_bundle_path = lilv_file_uri_parse(lilv_node_as_uri(bundle), NULL);
289 char* ui_binary_path = lilv_file_uri_parse(lilv_node_as_uri(binary), NULL);
291 char* ui_bundle_path = strdup(lilv_uri_to_path(lilv_node_as_uri(bundle)));
292 char* ui_binary_path = strdup(lilv_uri_to_path(lilv_node_as_uri(binary)));
294 if (!ui_bundle_path || !ui_binary_path) {
295 error << _("failed to get path for UI bindle or binary") << endmsg;
296 free(ui_bundle_path);
297 free(ui_binary_path);
302 _inst = suil_instance_new(
307 lilv_node_as_uri(lilv_ui_get_uri(ui)),
308 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
313 free(ui_bundle_path);
314 free(ui_binary_path);
318 error << _("failed to instantiate LV2 GUI") << endmsg;
322 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
324 const uint32_t num_ports = _lv2->num_ports();
325 for (uint32_t i = 0; i < num_ports; ++i) {
326 if (_lv2->parameter_is_output(i)
327 && _lv2->parameter_is_control(i)
328 && is_update_wanted(i)) {
329 _output_ports.push_back(i);
333 _external_ui_ptr = NULL;
334 if (!is_external_ui) {
335 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
337 error << _("failed to get LV2 UI widget") << endmsg;
338 suil_instance_free((SuilInstance*)_inst);
342 if (!container->get_child()) {
343 // Suil didn't add the UI to the container for us, so do it now
344 container->add(*Gtk::manage(Glib::wrap(c_widget)));
346 container->show_all();
347 gtk_widget_set_can_focus(c_widget, true);
348 gtk_widget_grab_focus(c_widget);
350 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
353 _values = new float[num_ports];
354 _controllables.resize(num_ports);
355 for (uint32_t i = 0; i < num_ports; ++i) {
357 uint32_t port = _lv2->nth_parameter(i, ok);
359 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
360 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
362 /* FIXME only works with control output ports (which is all we support now anyway) */
363 if (_controllables[port] && _lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
364 _controllables[port]->Changed.connect (control_connections, invalidator (*this), boost::bind (&LV2PluginUI::control_changed, this, port), gui_context());
365 /* queue for first update ("push") to GUI */
366 _updates.insert (port);
371 if (_lv2->has_message_output()) {
372 _message_update_connection = Timers::super_rapid_connect (
373 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
378 LV2PluginUI::grab_focus()
380 if (_inst && !_lv2->is_external_ui()) {
381 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
382 gtk_widget_grab_focus(c_widget);
387 LV2PluginUI::lv2ui_free()
392 remove (*_gui_widget);
397 suil_instance_free((SuilInstance*)_inst);
402 LV2PluginUI::~LV2PluginUI ()
406 _message_update_connection.disconnect();
407 _screen_update_connection.disconnect();
409 if (_external_ui_ptr && _lv2->is_external_kx()) {
410 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
413 _external_ui_ptr = NULL;
417 LV2PluginUI::get_preferred_height()
419 Gtk::Requisition r = size_request();
424 LV2PluginUI::get_preferred_width()
426 Gtk::Requisition r = size_request();
431 LV2PluginUI::resizable()
433 return _lv2->ui_is_resizable();
437 LV2PluginUI::package(Gtk::Window& win)
439 if (_external_ui_ptr) {
442 /* forward configure events to plugin window */
443 win.signal_configure_event().connect(
444 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
445 win.signal_map_event().connect(
446 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
447 win.signal_unmap_event().connect(
448 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
454 LV2PluginUI::configure_handler(GdkEventConfigure*)
456 std::cout << "CONFIGURE" << std::endl;
461 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
463 /* FIXME: use port notification properties
464 and/or new UI extension subscription methods
470 LV2PluginUI::on_window_show(const std::string& title)
472 //cout << "on_window_show - " << title << endl; flush(cout);
474 if (_lv2->is_external_ui()) {
475 if (_external_ui_ptr) {
476 _screen_update_connection.disconnect();
477 _message_update_connection.disconnect();
478 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
479 _screen_update_connection = Timers::super_rapid_connect
480 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
481 if (_lv2->has_message_output()) {
482 _message_update_connection = Timers::super_rapid_connect (
483 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
487 lv2ui_instantiate(title);
488 if (!_external_ui_ptr) {
492 _screen_update_connection.disconnect();
493 _message_update_connection.disconnect();
494 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
495 _screen_update_connection = Timers::super_rapid_connect
496 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
497 if (_lv2->has_message_output()) {
498 _message_update_connection = Timers::super_rapid_connect (
499 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
503 lv2ui_instantiate("gtk2gui");
506 return _inst ? true : false;
510 LV2PluginUI::on_window_hide()
512 //printf("LV2PluginUI::on_window_hide\n");
514 if (_lv2->is_external_ui()) {
515 if (!_external_ui_ptr) { return; }
516 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
517 if (!_lv2->is_external_kx()) { return ; }
518 _message_update_connection.disconnect();
519 _screen_update_connection.disconnect();
520 _external_ui_ptr = NULL;
521 suil_instance_free((SuilInstance*)_inst);