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"
24 #include "ardour_ui.h"
25 #include "lv2_plugin_ui.h"
27 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
29 #include <lilv/lilv.h>
30 #include <suil/suil.h>
34 using namespace ARDOUR;
38 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
40 static SuilHost* ui_host = NULL;
43 LV2PluginUI::write_from_ui(void* controller,
49 LV2PluginUI* me = (LV2PluginUI*)controller;
51 if (port_index >= me->_controllables.size()) {
55 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
57 ac->set_value(*(const float*)buffer);
59 } else if (format == me->_lv2->urids.atom_eventTransfer) {
61 const int cnt = me->_pi->get_count();
62 for (int i=0; i < cnt; i++ ) {
63 boost::shared_ptr<LV2Plugin> lv2i = boost::dynamic_pointer_cast<LV2Plugin> (me->_pi->plugin(i));
64 lv2i->write_from_ui(port_index, format, buffer_size, (const uint8_t*)buffer);
70 LV2PluginUI::write_to_ui(void* controller,
76 LV2PluginUI* me = (LV2PluginUI*)controller;
78 suil_instance_port_event((SuilInstance*)me->_inst,
79 port_index, buffer_size, format, buffer);
84 LV2PluginUI::port_index(void* controller, const char* symbol)
86 return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
90 LV2PluginUI::touch(void* controller,
94 LV2PluginUI* me = (LV2PluginUI*)controller;
95 if (port_index >= me->_controllables.size()) {
99 ControllableRef control = me->_controllables[port_index];
101 control->start_touch(control->session().transport_frame());
103 control->stop_touch(false, control->session().transport_frame());
108 LV2PluginUI::update_timeout()
110 _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
114 LV2PluginUI::on_external_ui_closed(void* controller)
116 LV2PluginUI* me = (LV2PluginUI*)controller;
117 me->_screen_update_connection.disconnect();
118 me->_external_ui_ptr = NULL;
120 suil_instance_free((SuilInstance*)me->_inst);
126 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
128 PlugUIBase::parameter_changed(port_index, val);
130 if (val != _values[port_index]) {
131 parameter_update(port_index, val);
136 LV2PluginUI::parameter_update(uint32_t port_index, float val)
142 suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
143 _values[port_index] = val;
147 LV2PluginUI::start_updating(GdkEventAny*)
149 if (!_output_ports.empty()) {
150 _screen_update_connection.disconnect();
151 _screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect
152 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
158 LV2PluginUI::stop_updating(GdkEventAny*)
160 //cout << "stop_updating" << endl;
162 if (!_output_ports.empty()) {
163 _screen_update_connection.disconnect();
169 LV2PluginUI::output_update()
171 //cout << "output_update" << endl;
172 if (_external_ui_ptr) {
173 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
174 if (!_external_ui_ptr) return; // ui was closed here
177 /* FIXME only works with control output ports (which is all we support now anyway) */
178 uint32_t nports = _output_ports.size();
179 for (uint32_t i = 0; i < nports; ++i) {
180 uint32_t index = _output_ports[i];
181 parameter_changed(index, _lv2->get_parameter(index));
186 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
187 boost::shared_ptr<LV2Plugin> lv2p)
193 , _external_ui_ptr(NULL)
196 _ardour_buttons_box.set_spacing (6);
197 _ardour_buttons_box.set_border_width (6);
198 _ardour_buttons_box.pack_end (focus_button, false, false);
199 _ardour_buttons_box.pack_end (bypass_button, false, false, 10);
200 _ardour_buttons_box.pack_end (delete_button, false, false);
201 _ardour_buttons_box.pack_end (save_button, false, false);
202 _ardour_buttons_box.pack_end (add_button, false, false);
203 _ardour_buttons_box.pack_end (_preset_combo, false, false);
204 _ardour_buttons_box.pack_end (_preset_modified, false, false);
208 LV2PluginUI::lv2ui_instantiate(const std::string& title)
210 bool is_external_ui = _lv2->is_external_ui();
211 LV2_Feature** features_src = const_cast<LV2_Feature**>(_lv2->features());
212 LV2_Feature** features = const_cast<LV2_Feature**>(_lv2->features());
213 size_t features_count = 0;
214 while (*features++) {
218 Gtk::Alignment* container = NULL;
219 if (is_external_ui) {
220 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
221 _external_ui_host.plugin_human_id = strdup(title.c_str());
223 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
224 _external_ui_feature.data = &_external_ui_host;
226 _external_kxui_feature.URI = LV2_EXTERNAL_UI_KX__Host;
227 _external_kxui_feature.data = &_external_ui_host;
230 features = (LV2_Feature**)malloc(
231 sizeof(LV2_Feature*) * (features_count + 2));
232 for (size_t i = 0; i < features_count - 2; ++i) {
233 features[i] = features_src[i];
235 features[features_count - 2] = &_external_kxui_feature;
236 features[features_count - 1] = &_external_ui_feature;
237 features[features_count] = NULL;
239 if (_ardour_buttons_box.get_parent()) {
240 _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
242 pack_start(_ardour_buttons_box, false, false);
243 _ardour_buttons_box.show_all();
245 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
246 pack_start(*_gui_widget, true, true);
249 _parent_feature.URI = LV2_UI__parent;
250 _parent_feature.data = _gui_widget->gobj();
253 features = (LV2_Feature**)malloc(
254 sizeof(LV2_Feature*) * (features_count + 1));
255 for (size_t i = 0; i < features_count - 1; ++i) {
256 features[i] = features_src[i];
258 features[features_count - 1] = &_parent_feature;
259 features[features_count] = NULL;
263 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
264 LV2PluginUI::port_index,
266 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
268 const char* container_type = (is_external_ui)
272 const LilvUI* ui = (const LilvUI*)_lv2->c_ui();
273 _inst = suil_instance_new(
278 lilv_node_as_uri(lilv_ui_get_uri(ui)),
279 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
280 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
281 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
286 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
288 const uint32_t num_ports = _lv2->num_ports();
289 for (uint32_t i = 0; i < num_ports; ++i) {
290 if (_lv2->parameter_is_output(i)
291 && _lv2->parameter_is_control(i)
292 && is_update_wanted(i)) {
293 _output_ports.push_back(i);
297 _external_ui_ptr = NULL;
299 if (!is_external_ui) {
300 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
302 error << _("failed to get LV2 UI widget") << endmsg;
303 suil_instance_free((SuilInstance*)_inst);
307 if (!container->get_child()) {
308 // Suil didn't add the UI to the container for us, so do it now
309 container->add(*Gtk::manage(Glib::wrap(c_widget)));
311 container->show_all();
313 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
317 _values = new float[num_ports];
318 _controllables.resize(num_ports);
319 for (uint32_t i = 0; i < num_ports; ++i) {
321 uint32_t port = _lv2->nth_parameter(i, ok);
323 _values[port] = _lv2->get_parameter(port);
324 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
325 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
327 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
328 parameter_update(port, _values[port]);
333 if (_lv2->has_message_output()) {
334 _lv2->enable_ui_emmission();
335 ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect(
336 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
341 LV2PluginUI::lv2ui_free()
346 remove (*_gui_widget);
351 suil_instance_free((SuilInstance*)_inst);
356 LV2PluginUI::~LV2PluginUI ()
362 /* Close and delete GUI. */
365 _screen_update_connection.disconnect();
367 if (_lv2->is_external_ui()) {
368 /* External UI is no longer valid.
369 on_window_hide() will not try to use it if is NULL.
371 _external_ui_ptr = NULL;
376 LV2PluginUI::get_preferred_height()
378 Gtk::Requisition r = size_request();
383 LV2PluginUI::get_preferred_width()
385 Gtk::Requisition r = size_request();
390 LV2PluginUI::resizable()
392 return _lv2->ui_is_resizable();
396 LV2PluginUI::package(Gtk::Window& win)
398 if (_external_ui_ptr) {
401 /* forward configure events to plugin window */
402 win.signal_configure_event().connect(
403 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
404 win.signal_map_event().connect(
405 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
406 win.signal_unmap_event().connect(
407 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
413 LV2PluginUI::configure_handler(GdkEventConfigure*)
415 std::cout << "CONFIGURE" << std::endl;
420 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
422 /* FIXME: use port notification properties
423 and/or new UI extension subscription methods
429 LV2PluginUI::on_window_show(const std::string& title)
431 //cout << "on_window_show - " << title << endl; flush(cout);
433 if (_lv2->is_external_ui()) {
434 if (_external_ui_ptr) {
435 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
438 lv2ui_instantiate(title);
439 if (!_external_ui_ptr) {
443 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
444 _screen_update_connection.disconnect();
445 _screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect
446 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
449 lv2ui_instantiate("gtk2gui");
456 LV2PluginUI::on_window_hide()
458 //cout << "on_window_hide" << endl; flush(cout);
460 if (_external_ui_ptr) {
461 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
462 _screen_update_connection.disconnect();
463 _external_ui_ptr = NULL;
465 suil_instance_free((SuilInstance*)_inst);