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;
50 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
53 ac->set_value(*(float*)buffer);
58 LV2PluginUI::on_external_ui_closed(void* controller)
60 LV2PluginUI* me = (LV2PluginUI*)controller;
61 me->_screen_update_connection.disconnect();
62 me->_external_ui_ptr = NULL;
66 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
68 PlugUIBase::parameter_changed(port_index, val);
70 if (val != _values[port_index]) {
71 parameter_update(port_index, val);
76 LV2PluginUI::parameter_update(uint32_t port_index, float val)
82 suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
83 _values[port_index] = val;
87 LV2PluginUI::start_updating(GdkEventAny*)
89 if (!_output_ports.empty()) {
90 _screen_update_connection.disconnect();
91 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
92 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
98 LV2PluginUI::stop_updating(GdkEventAny*)
100 //cout << "stop_updating" << endl;
102 if (!_output_ports.empty()) {
103 _screen_update_connection.disconnect();
109 LV2PluginUI::output_update()
111 //cout << "output_update" << endl;
112 if (_external_ui_ptr) {
113 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
116 /* FIXME only works with control output ports (which is all we support now anyway) */
117 uint32_t nports = _output_ports.size();
118 for (uint32_t i = 0; i < nports; ++i) {
119 uint32_t index = _output_ports[i];
120 parameter_changed(index, _lv2->get_parameter(index));
125 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
126 boost::shared_ptr<LV2Plugin> lv2p)
131 , _external_ui_ptr(NULL)
137 LV2PluginUI::lv2ui_instantiate(const std::string& title)
139 LV2_Feature** features;
140 LV2_Feature** features_src;
141 LV2_Feature** features_dst;
142 size_t features_count;
145 is_external_ui = _lv2->is_external_ui();
147 if (is_external_ui) {
148 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
149 _external_ui_host.plugin_human_id = strdup(title.c_str());
151 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
152 _external_ui_feature.data = &_external_ui_host;
154 features_src = features = (LV2_Feature**)_lv2->features();
156 while (*features++) {
159 features_dst = features = (LV2_Feature**)malloc(
160 sizeof(LV2_Feature*) * features_count);
161 features_dst[--features_count] = NULL;
162 features_dst[--features_count] = &_external_ui_feature;
163 while (features_count--) {
164 *features++ = *features_src++;
167 features_dst = (LV2_Feature**)_lv2->features();
171 ui_host = suil_host_new(LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
173 const char* container_type = (is_external_ui)
177 LilvUI* ui = (LilvUI*)_lv2->c_ui();
178 _inst = suil_instance_new(
183 lilv_node_as_uri(lilv_ui_get_uri(ui)),
184 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
185 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
186 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
189 if (is_external_ui) {
193 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
195 const uint32_t num_ports = _lv2->num_ports();
196 for (uint32_t i = 0; i < num_ports; ++i) {
197 if (_lv2->parameter_is_output(i)
198 && _lv2->parameter_is_control(i)
199 && is_update_wanted(i)) {
200 _output_ports.push_back(i);
204 _external_ui_ptr = NULL;
206 if (!is_external_ui) {
207 Gtk::HBox* box = manage (new Gtk::HBox);
208 box->set_spacing (6);
209 box->set_border_width (6);
210 box->pack_end (focus_button, false, false);
211 box->pack_end (bypass_button, false, false, 10);
212 box->pack_end (delete_button, false, false);
213 box->pack_end (save_button, false, false);
214 box->pack_end (add_button, false, false);
215 box->pack_end (_preset_combo, false, false);
217 pack_start(*box, false, false);
219 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
220 _gui_widget = Glib::wrap(c_widget);
221 _gui_widget->show_all();
222 pack_start(*_gui_widget, true, true);
224 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
228 _values = new float[num_ports];
229 _controllables.resize(num_ports);
230 for (uint32_t i = 0; i < num_ports; ++i) {
232 uint32_t port = _lv2->nth_parameter(i, ok);
234 _values[port] = _lv2->get_parameter(port);
235 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
236 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
238 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
239 parameter_update(port, _values[port]);
246 LV2PluginUI::lv2ui_free()
251 remove (*_gui_widget);
254 suil_instance_free((SuilInstance*)_inst);
260 LV2PluginUI::~LV2PluginUI ()
266 /* Close and delete GUI. */
269 _screen_update_connection.disconnect();
271 if (_lv2->is_external_ui()) {
272 /* External UI is no longer valid.
273 on_window_hide() will not try to use it if is NULL.
275 _external_ui_ptr = NULL;
280 LV2PluginUI::get_preferred_height()
282 Gtk::Requisition r = size_request();
287 LV2PluginUI::get_preferred_width()
289 Gtk::Requisition r = size_request();
294 LV2PluginUI::package(Gtk::Window& win)
296 if (_external_ui_ptr) {
299 /* forward configure events to plugin window */
300 win.signal_configure_event().connect(
301 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
302 win.signal_map_event().connect(
303 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
304 win.signal_unmap_event().connect(
305 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
311 LV2PluginUI::configure_handler(GdkEventConfigure*)
313 std::cout << "CONFIGURE" << std::endl;
318 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
320 /* FIXME: use port notification properties
321 and/or new UI extension subscription methods
327 LV2PluginUI::on_window_show(const std::string& title)
329 //cout << "on_window_show - " << title << endl; flush(cout);
331 if (_lv2->is_external_ui()) {
332 if (_external_ui_ptr) {
333 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
336 lv2ui_instantiate(title);
337 if (!_external_ui_ptr) {
341 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
342 _screen_update_connection.disconnect();
343 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
344 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
347 lv2ui_instantiate("gtk2gui");
354 LV2PluginUI::on_window_hide()
356 //cout << "on_window_hide" << endl; flush(cout);
358 if (_external_ui_ptr) {
359 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
360 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
361 //_external_ui_ptr = NULL;
362 //_screen_update_connection.disconnect();