2 Copyright (C) 2008-2011 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.
21 #include "ardour/lv2_plugin.h"
22 #include "ardour/plugin_manager.h"
23 #include "ardour/processor.h"
25 #include "ardour_ui.h"
26 #include "gui_thread.h"
27 #include "lv2_plugin_ui.h"
31 #include <lilv/lilv.h>
32 #include <suil/suil.h>
35 using namespace ARDOUR;
38 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
40 static SuilHost* ui_host = NULL;
43 LV2PluginUI::lv2_ui_write(void* controller,
45 uint32_t /*buffer_size*/,
49 LV2PluginUI* me = (LV2PluginUI*)controller;
51 if (port_index >= me->_controllables.size()) {
55 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
58 ac->set_value(*(float*)buffer);
63 LV2PluginUI::on_external_ui_closed(void* controller)
65 LV2PluginUI* me = (LV2PluginUI*)controller;
66 me->_screen_update_connection.disconnect();
67 me->_external_ui_ptr = NULL;
71 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
73 PlugUIBase::parameter_changed(port_index, val);
75 if (val != _values[port_index]) {
76 parameter_update(port_index, val);
81 LV2PluginUI::parameter_update(uint32_t port_index, float val)
87 suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
88 _values[port_index] = val;
92 LV2PluginUI::start_updating(GdkEventAny*)
94 if (!_output_ports.empty()) {
95 _screen_update_connection.disconnect();
96 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
97 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
103 LV2PluginUI::stop_updating(GdkEventAny*)
105 //cout << "stop_updating" << endl;
107 if (!_output_ports.empty()) {
108 _screen_update_connection.disconnect();
114 LV2PluginUI::output_update()
116 //cout << "output_update" << endl;
117 if (_external_ui_ptr) {
118 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
121 /* FIXME only works with control output ports (which is all we support now anyway) */
122 uint32_t nports = _output_ports.size();
123 for (uint32_t i = 0; i < nports; ++i) {
124 uint32_t index = _output_ports[i];
125 parameter_changed(index, _lv2->get_parameter(index));
130 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
131 boost::shared_ptr<LV2Plugin> lv2p)
136 , _external_ui_ptr(NULL)
142 LV2PluginUI::lv2ui_instantiate(const std::string& title)
144 LV2_Feature** features;
145 LV2_Feature** features_src;
146 LV2_Feature** features_dst;
147 size_t features_count;
150 is_external_ui = _lv2->is_external_ui();
152 if (is_external_ui) {
153 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
154 _external_ui_host.plugin_human_id = strdup(title.c_str());
156 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
157 _external_ui_feature.data = &_external_ui_host;
159 features_src = features = (LV2_Feature**)_lv2->features();
161 while (*features++) {
164 features_dst = features = (LV2_Feature**)malloc(
165 sizeof(LV2_Feature*) * features_count);
166 features_dst[--features_count] = NULL;
167 features_dst[--features_count] = &_external_ui_feature;
168 while (features_count--) {
169 *features++ = *features_src++;
172 features_dst = (LV2_Feature**)_lv2->features();
176 ui_host = suil_host_new(LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
178 const char* container_type = (is_external_ui)
182 LilvUI* ui = (LilvUI*)_lv2->c_ui();
183 _inst = suil_instance_new(
188 lilv_node_as_uri(lilv_ui_get_uri(ui)),
189 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
190 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
191 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
194 if (is_external_ui) {
198 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
200 const uint32_t num_ports = _lv2->num_ports();
201 for (uint32_t i = 0; i < num_ports; ++i) {
202 if (_lv2->parameter_is_output(i)
203 && _lv2->parameter_is_control(i)
204 && is_update_wanted(i)) {
205 _output_ports.push_back(i);
209 _external_ui_ptr = NULL;
211 if (!is_external_ui) {
212 Gtk::HBox* box = manage (new Gtk::HBox);
213 box->set_spacing (6);
214 box->set_border_width (6);
215 box->pack_end (focus_button, false, false);
216 box->pack_end (bypass_button, false, false, 10);
217 box->pack_end (delete_button, false, false);
218 box->pack_end (save_button, false, false);
219 box->pack_end (add_button, false, false);
220 box->pack_end (_preset_combo, false, false);
222 pack_start(*box, false, false);
224 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
225 _gui_widget = Glib::wrap(c_widget);
226 _gui_widget->show_all();
227 pack_start(*_gui_widget, true, true);
229 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
233 _values = new float[num_ports];
234 _controllables.resize(num_ports);
235 for (uint32_t i = 0; i < num_ports; ++i) {
237 uint32_t port = _lv2->nth_parameter(i, ok);
239 _values[port] = _lv2->get_parameter(port);
240 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
241 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
243 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
244 parameter_update(port, _values[port]);
251 LV2PluginUI::lv2ui_free()
256 remove (*_gui_widget);
259 suil_instance_free((SuilInstance*)_inst);
265 LV2PluginUI::~LV2PluginUI ()
271 /* Close and delete GUI. */
274 _screen_update_connection.disconnect();
276 if (_lv2->is_external_ui()) {
277 /* External UI is no longer valid.
278 on_window_hide() will not try to use it if is NULL.
280 _external_ui_ptr = NULL;
285 LV2PluginUI::get_preferred_height()
287 Gtk::Requisition r = size_request();
292 LV2PluginUI::get_preferred_width()
294 Gtk::Requisition r = size_request();
299 LV2PluginUI::package(Gtk::Window& win)
301 if (_external_ui_ptr) {
304 /* forward configure events to plugin window */
305 win.signal_configure_event().connect(
306 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
307 win.signal_map_event().connect(
308 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
309 win.signal_unmap_event().connect(
310 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
316 LV2PluginUI::configure_handler(GdkEventConfigure*)
318 std::cout << "CONFIGURE" << std::endl;
323 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
325 /* FIXME: use port notification properties
326 and/or new UI extension subscription methods
332 LV2PluginUI::on_window_show(const std::string& title)
334 //cout << "on_window_show - " << title << endl; flush(cout);
336 if (_lv2->is_external_ui()) {
337 if (_external_ui_ptr) {
338 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
341 lv2ui_instantiate(title);
342 if (!_external_ui_ptr) {
346 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
347 _screen_update_connection.disconnect();
348 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
349 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
352 lv2ui_instantiate("gtk2gui");
359 LV2PluginUI::on_window_hide()
361 //cout << "on_window_hide" << endl; flush(cout);
363 if (_external_ui_ptr) {
364 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
365 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
366 //_external_ui_ptr = NULL;
367 //_screen_update_connection.disconnect();