Fix compilation with old SLV2.
[ardour2.git] / gtk2_ardour / lv2_plugin_ui.cc
blob3a73453dbe5622fa366551f46676d2fafb44c1fe
1 /*
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"
29 using namespace Gtk;
30 using namespace ARDOUR;
31 using namespace PBD;
33 #ifdef HAVE_NEW_SLV2
34 SLV2UIHost LV2PluginUI::ui_host = NULL;
35 #endif
37 void
38 LV2PluginUI::lv2_ui_write(LV2UI_Controller controller,
39 uint32_t port_index,
40 uint32_t /*buffer_size*/,
41 uint32_t /*format*/,
42 const void* buffer)
44 LV2PluginUI* me = (LV2PluginUI*)controller;
45 me->_controllables[port_index]->set_value(*(float*)buffer);
48 void
49 LV2PluginUI::on_external_ui_closed(LV2UI_Controller controller)
51 LV2PluginUI* me = (LV2PluginUI*)controller;
52 me->_screen_update_connection.disconnect();
53 me->_external_ui_ptr = NULL;
56 void
57 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
59 PlugUIBase::parameter_changed(port_index, val);
61 if (val != _values[port_index]) {
62 parameter_update(port_index, val);
66 void
67 LV2PluginUI::parameter_update(uint32_t port_index, float val)
69 if (!_inst) {
70 return;
73 #ifdef HAVE_NEW_SLV2
74 slv2_ui_instance_port_event(_inst, port_index, 4, 0, &val);
75 #else
76 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
77 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
78 if (ui_desc->port_event) {
79 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
81 #endif
82 _values[port_index] = val;
85 bool
86 LV2PluginUI::start_updating(GdkEventAny*)
88 if (!_output_ports.empty()) {
89 _screen_update_connection.disconnect();
90 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
91 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
93 return false;
96 bool
97 LV2PluginUI::stop_updating(GdkEventAny*)
99 //cout << "stop_updating" << endl;
101 if ( //!_external_ui_ptr &&
102 !_output_ports.empty()) {
103 _screen_update_connection.disconnect();
105 return false;
108 void
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)
127 : PlugUIBase(pi)
128 , _lv2(lv2p)
129 , _inst(NULL)
130 , _values(NULL)
131 , _external_ui_ptr(NULL)
133 if (!_lv2->is_external_ui()) {
134 lv2ui_instantiate("gtk2gui");
138 void
139 LV2PluginUI::lv2ui_instantiate(const std::string& title)
141 LV2_Feature** features;
142 LV2_Feature** features_src;
143 LV2_Feature** features_dst;
144 size_t features_count;
145 bool is_external_ui;
147 is_external_ui = _lv2->is_external_ui();
149 if (is_external_ui) {
150 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
151 _external_ui_host.plugin_human_id = strdup(title.c_str());
153 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
154 _external_ui_feature.data = &_external_ui_host;
156 features_src = features = (LV2_Feature**)_lv2->features();
157 features_count = 2;
158 while (*features++) {
159 features_count++;
161 features_dst = features = (LV2_Feature**)malloc(
162 sizeof(LV2_Feature*) * features_count);
163 features_dst[--features_count] = NULL;
164 features_dst[--features_count] = &_external_ui_feature;
165 while (features_count--) {
166 *features++ = *features_src++;
168 } else {
169 features_dst = (LV2_Feature**)_lv2->features();
172 #ifdef HAVE_NEW_SLV2
173 if (!LV2PluginUI::ui_host) {
174 LV2PluginUI::ui_host = slv2_ui_host_new(
175 LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
177 SLV2Value gtk_ui = slv2_value_new_uri(
178 ARDOUR::PluginManager::the_manager()->lv2_world()->world,
179 "http://lv2plug.in/ns/extensions/ui#GtkUI");
180 _inst = slv2_ui_instance_new(
181 _lv2->slv2_plugin(), _lv2->slv2_ui(), gtk_ui, ui_host, this, features_dst);
182 slv2_value_free(gtk_ui);
183 slv2_ui_host_free(ui_host);
184 #else
185 _inst = slv2_ui_instantiate(
186 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
187 features_dst);
188 #endif
190 if (is_external_ui) {
191 free(features_dst);
194 uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
195 for (uint32_t i = 0; i < num_ports; ++i) {
196 if (_lv2->parameter_is_output(i)
197 && _lv2->parameter_is_control(i)
198 && is_update_wanted(i)) {
199 _output_ports.push_back(i);
203 _external_ui_ptr = NULL;
204 if (_inst) {
205 if (!is_external_ui) {
206 GtkWidget* c_widget = (GtkWidget*)slv2_ui_instance_get_widget(_inst);
207 _gui_widget = Glib::wrap(c_widget);
208 _gui_widget->show_all();
209 pack_start(*_gui_widget, true, true);
210 } else {
211 _external_ui_ptr = (struct lv2_external_ui*)slv2_ui_instance_get_widget(_inst);
215 _values = new float[num_ports];
216 _controllables.resize(num_ports);
217 for (uint32_t i = 0; i < num_ports; ++i) {
218 bool ok;
219 uint32_t port = _lv2->nth_parameter(i, ok);
220 if (ok) {
221 _values[port] = _lv2->get_parameter(port);
222 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
223 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
225 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
226 parameter_update(port, _values[port]);
232 LV2PluginUI::~LV2PluginUI ()
234 //cout << "LV2PluginUI destructor called" << endl;
236 if (_values) {
237 delete[] _values;
240 /* Close and delete GUI. */
241 #ifdef HAVE_NEW_SLV2
242 slv2_ui_instance_free(_inst);
243 #else
244 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
245 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
247 if (ui_desc) {
248 ui_desc->cleanup(ui_handle);
250 #endif
252 _screen_update_connection.disconnect();
254 if (_lv2->is_external_ui()) {
255 /* External UI is no longer valid.
256 on_window_hide() will not try to use it if is NULL.
258 _external_ui_ptr = NULL;
263 LV2PluginUI::get_preferred_height()
265 Gtk::Requisition r = size_request();
266 return r.height;
270 LV2PluginUI::get_preferred_width()
272 Gtk::Requisition r = size_request();
273 return r.width;
277 LV2PluginUI::package(Gtk::Window& win)
279 if (_external_ui_ptr) {
280 _win_ptr = &win;
281 } else {
282 /* forward configure events to plugin window */
283 win.signal_configure_event().connect(
284 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
285 win.signal_map_event().connect(
286 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
287 win.signal_unmap_event().connect(
288 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
290 return 0;
293 bool
294 LV2PluginUI::configure_handler(GdkEventConfigure*)
296 std::cout << "CONFIGURE" << std::endl;
297 return false;
300 bool
301 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
303 /* FIXME: use port notification properties
304 and/or new UI extension subscription methods
306 return true;
309 bool
310 LV2PluginUI::on_window_show(const std::string& title)
312 //cout << "on_window_show - " << title << endl; flush(cout);
314 if (_lv2->is_external_ui()) {
315 if (_external_ui_ptr) {
316 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
317 return false;
319 lv2ui_instantiate(title);
320 if (!_external_ui_ptr) {
321 return false;
324 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
325 _screen_update_connection.disconnect();
326 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
327 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
328 return false;
331 return true;
334 void
335 LV2PluginUI::on_window_hide()
337 //cout << "on_window_hide" << endl; flush(cout);
339 if (_external_ui_ptr) {
340 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
341 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
342 //_external_ui_ptr = NULL;
343 //_screen_update_connection.disconnect();