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"
30 using namespace ARDOUR;
34 LV2PluginUI::lv2_ui_write(
35 LV2UI_Controller controller,
37 uint32_t /*buffer_size*/,
41 //cout << "lv2_ui_write" << endl;
42 LV2PluginUI* me = (LV2PluginUI *) controller;
43 me->_controllables[port_index]->set_value (*(float *) buffer);
46 void LV2PluginUI::on_external_ui_closed(LV2UI_Controller controller)
48 LV2PluginUI* me = (LV2PluginUI*)controller;
49 me->_screen_update_connection.disconnect();
50 me->_external_ui_ptr = NULL;
54 LV2PluginUI::parameter_changed (uint32_t port_index, float val)
56 PlugUIBase::parameter_changed (port_index, val);
58 if (val != _values[port_index]) {
59 parameter_update(port_index, val);
64 LV2PluginUI::parameter_update (uint32_t port_index, float val)
71 slv2_ui_instance_port_event(_inst, port_index, 4, 0, &val);
73 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
74 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
75 if (ui_desc->port_event) {
76 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
79 _values[port_index] = val;
83 LV2PluginUI::start_updating(GdkEventAny*)
85 if (!_output_ports.empty()) {
86 _screen_update_connection.disconnect();
87 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
88 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
94 LV2PluginUI::stop_updating(GdkEventAny*)
96 //cout << "stop_updating" << endl;
98 if (//!_external_ui_ptr &&
99 !_output_ports.empty()) {
100 _screen_update_connection.disconnect();
106 LV2PluginUI::output_update()
108 //cout << "output_update" << endl;
109 if (_external_ui_ptr) {
110 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
113 /* FIXME only works with control output ports (which is all we support now anyway) */
114 uint32_t nports = _output_ports.size();
115 for (uint32_t i = 0; i < nports; ++i) {
116 uint32_t index = _output_ports[i];
117 parameter_changed(index, _lv2->get_parameter(index));
122 LV2PluginUI::LV2PluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<LV2Plugin> lv2p)
127 , _external_ui_ptr(NULL)
129 if (!_lv2->is_external_ui()) {
130 lv2ui_instantiate("gtk2gui");
135 LV2PluginUI::lv2ui_instantiate(const std::string& title)
137 LV2_Feature** features;
138 LV2_Feature** features_src;
139 LV2_Feature** features_dst;
140 size_t features_count;
143 is_external_ui = _lv2->is_external_ui();
145 if (is_external_ui) {
146 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
147 _external_ui_host.plugin_human_id = strdup(title.c_str());
149 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
150 _external_ui_feature.data = &_external_ui_host;
152 features_src = features = (LV2_Feature**)_lv2->features();
154 while (*features++) {
158 features_dst = features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * features_count);
159 features_dst[--features_count] = NULL;
160 features_dst[--features_count] = &_external_ui_feature;
161 while (features_count--) {
162 *features++ = *features_src++;
165 features_dst = (LV2_Feature**)_lv2->features();
169 SLV2UIHost ui_host = slv2_ui_host_new(
170 this, LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
171 SLV2Value gtk_ui = slv2_value_new_uri(
172 ARDOUR::PluginManager::the_manager()->lv2_world()->world,
173 "http://lv2plug.in/ns/extensions/ui#GtkUI");
174 _inst = slv2_ui_instance_new(
175 _lv2->slv2_plugin(), _lv2->slv2_ui(), gtk_ui, ui_host, features_dst);
176 slv2_value_free(gtk_ui);
177 slv2_ui_host_free(ui_host);
179 _inst = slv2_ui_instantiate(
180 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
184 if (is_external_ui) {
188 uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
189 for (uint32_t i = 0; i < num_ports; ++i) {
190 if (_lv2->parameter_is_output(i) && _lv2->parameter_is_control(i) && is_update_wanted(i)) {
191 _output_ports.push_back(i);
195 _external_ui_ptr = NULL;
197 if (!is_external_ui) {
198 GtkWidget* c_widget = (GtkWidget*)slv2_ui_instance_get_widget(_inst);
199 _gui_widget = Glib::wrap(c_widget);
200 _gui_widget->show_all();
201 pack_start(*_gui_widget, true, true);
203 _external_ui_ptr = (struct lv2_external_ui *)slv2_ui_instance_get_widget(_inst);
207 _values = new float[num_ports];
208 _controllables.resize (num_ports);
209 for (uint32_t i = 0; i < num_ports; ++i) {
211 uint32_t port = _lv2->nth_parameter(i, ok);
213 _values[port] = _lv2->get_parameter(port);
214 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
215 insert->control (Evoral::Parameter (PluginAutomation, 0, port))
218 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
219 parameter_update(port, _values[port]);
225 LV2PluginUI::~LV2PluginUI ()
227 //cout << "LV2PluginUI destructor called" << endl;
233 /* Close and delete GUI. */
235 slv2_ui_instance_free(_inst);
237 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
238 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
241 ui_desc->cleanup(ui_handle);
245 _screen_update_connection.disconnect();
247 if (_lv2->is_external_ui()) {
248 /* External UI is no longer valid.
249 on_window_hide() will not try to use it if is NULL.
251 _external_ui_ptr = NULL;
256 LV2PluginUI::get_preferred_height ()
258 Gtk::Requisition r = size_request();
263 LV2PluginUI::get_preferred_width ()
265 Gtk::Requisition r = size_request();
270 LV2PluginUI::package (Gtk::Window& win)
272 //cout << "package" << endl;
273 if (_external_ui_ptr) {
276 /* forward configure events to plugin window */
277 win.signal_configure_event().connect (sigc::mem_fun (*this, &LV2PluginUI::configure_handler));
278 win.signal_map_event().connect (sigc::mem_fun (*this, &LV2PluginUI::start_updating));
279 win.signal_unmap_event().connect (sigc::mem_fun (*this, &LV2PluginUI::stop_updating));
285 LV2PluginUI::configure_handler (GdkEventConfigure*)
287 std::cout << "CONFIGURE" << std::endl;
292 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
294 /* FIXME this should check the port notification properties, which nobody sets now anyway :) */
299 LV2PluginUI::on_window_show(const std::string& title)
301 //cout << "on_window_show - " << title << endl; flush(cout);
303 if (_lv2->is_external_ui()) {
304 if (_external_ui_ptr) {
305 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
308 lv2ui_instantiate(title);
309 if (!_external_ui_ptr) {
313 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
314 _screen_update_connection.disconnect();
315 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
316 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
324 LV2PluginUI::on_window_hide()
326 //cout << "on_window_hide" << endl; flush(cout);
328 if (_external_ui_ptr) {
329 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
330 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
331 //_external_ui_ptr = NULL;
332 //_screen_update_connection.disconnect();