Set mixbufs count in the case of a panner being used, as
[ardour2.git] / gtk2_ardour / lv2_plugin_ui.cc
blob68038ebd2061bc3aff0b1b869737ba16785f1d75
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 #include "lv2_ui.h"
31 /* Note this file is not compiled without either Suil or SLV2 present,
32 and if Suil is present then Lilv is also present.
35 #ifdef HAVE_SUIL
36 #include <lilv/lilv.h>
37 #include <suil/suil.h>
38 #else
39 #include <slv2/slv2.h>
40 #endif
42 using namespace Gtk;
43 using namespace ARDOUR;
44 using namespace PBD;
46 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
48 #ifdef HAVE_SUIL
49 static SuilHost* ui_host = NULL;
50 #endif
52 void
53 LV2PluginUI::lv2_ui_write(void* controller,
54 uint32_t port_index,
55 uint32_t /*buffer_size*/,
56 uint32_t /*format*/,
57 const void* buffer)
59 LV2PluginUI* me = (LV2PluginUI*)controller;
60 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
62 if (ac) {
63 ac->set_value(*(float*)buffer);
67 void
68 LV2PluginUI::on_external_ui_closed(void* controller)
70 LV2PluginUI* me = (LV2PluginUI*)controller;
71 me->_screen_update_connection.disconnect();
72 me->_external_ui_ptr = NULL;
75 void
76 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
78 PlugUIBase::parameter_changed(port_index, val);
80 if (val != _values[port_index]) {
81 parameter_update(port_index, val);
85 void
86 LV2PluginUI::parameter_update(uint32_t port_index, float val)
88 if (!_inst) {
89 return;
92 #ifdef HAVE_SUIL
93 suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
94 #else
95 SLV2UIInstance inst = (SLV2UIInstance)_inst;
96 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(inst);
97 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(inst);
98 if (ui_desc->port_event) {
99 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
101 #endif
102 _values[port_index] = val;
105 bool
106 LV2PluginUI::start_updating(GdkEventAny*)
108 if (!_output_ports.empty()) {
109 _screen_update_connection.disconnect();
110 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
111 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
113 return false;
116 bool
117 LV2PluginUI::stop_updating(GdkEventAny*)
119 //cout << "stop_updating" << endl;
121 if (!_output_ports.empty()) {
122 _screen_update_connection.disconnect();
124 return false;
127 void
128 LV2PluginUI::output_update()
130 //cout << "output_update" << endl;
131 if (_external_ui_ptr) {
132 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
135 /* FIXME only works with control output ports (which is all we support now anyway) */
136 uint32_t nports = _output_ports.size();
137 for (uint32_t i = 0; i < nports; ++i) {
138 uint32_t index = _output_ports[i];
139 parameter_changed(index, _lv2->get_parameter(index));
144 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
145 boost::shared_ptr<LV2Plugin> lv2p)
146 : PlugUIBase(pi)
147 , _lv2(lv2p)
148 , _gui_widget(NULL)
149 , _values(NULL)
150 , _external_ui_ptr(NULL)
151 , _inst(NULL)
155 void
156 LV2PluginUI::lv2ui_instantiate(const std::string& title)
158 LV2_Feature** features;
159 LV2_Feature** features_src;
160 LV2_Feature** features_dst;
161 size_t features_count;
162 bool is_external_ui;
164 is_external_ui = _lv2->is_external_ui();
166 if (is_external_ui) {
167 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
168 _external_ui_host.plugin_human_id = strdup(title.c_str());
170 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
171 _external_ui_feature.data = &_external_ui_host;
173 features_src = features = (LV2_Feature**)_lv2->features();
174 features_count = 2;
175 while (*features++) {
176 features_count++;
178 features_dst = features = (LV2_Feature**)malloc(
179 sizeof(LV2_Feature*) * features_count);
180 features_dst[--features_count] = NULL;
181 features_dst[--features_count] = &_external_ui_feature;
182 while (features_count--) {
183 *features++ = *features_src++;
185 } else {
186 features_dst = (LV2_Feature**)_lv2->features();
189 #ifdef HAVE_SUIL
190 if (!ui_host) {
191 ui_host = suil_host_new(LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
193 const char* container_type = (is_external_ui)
194 ? NS_UI "external"
195 : NS_UI "GtkUI";
197 LilvUI* ui = (LilvUI*)_lv2->c_ui();
198 _inst = suil_instance_new(
199 ui_host,
200 this,
201 container_type,
202 _lv2->uri(),
203 lilv_node_as_uri(lilv_ui_get_uri(ui)),
204 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
205 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
206 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
207 features_dst);
208 #else
209 _inst = slv2_ui_instantiate((SLV2Plugin)_lv2->c_plugin(),
210 (SLV2UI)_lv2->c_ui(),
211 LV2PluginUI::lv2_ui_write,
212 this,
213 features_dst);
214 #endif
216 if (is_external_ui) {
217 free(features_dst);
220 #ifdef HAVE_SUIL
221 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
222 #else
223 #define GET_WIDGET(inst) slv2_ui_instance_get_widget((SLV2UIInstance)inst);
224 #endif
226 const uint32_t num_ports = _lv2->num_ports();
227 for (uint32_t i = 0; i < num_ports; ++i) {
228 if (_lv2->parameter_is_output(i)
229 && _lv2->parameter_is_control(i)
230 && is_update_wanted(i)) {
231 _output_ports.push_back(i);
235 _external_ui_ptr = NULL;
236 if (_inst) {
237 if (!is_external_ui) {
238 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
239 _gui_widget = Glib::wrap(c_widget);
240 _gui_widget->show_all();
241 pack_start(*_gui_widget, true, true);
242 } else {
243 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
247 _values = new float[num_ports];
248 _controllables.resize(num_ports);
249 for (uint32_t i = 0; i < num_ports; ++i) {
250 bool ok;
251 uint32_t port = _lv2->nth_parameter(i, ok);
252 if (ok) {
253 _values[port] = _lv2->get_parameter(port);
254 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
255 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
257 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
258 parameter_update(port, _values[port]);
264 void
265 LV2PluginUI::lv2ui_free()
267 stop_updating (0);
269 if (_gui_widget) {
270 remove (*_gui_widget);
273 #ifdef HAVE_SUIL
274 suil_instance_free((SuilInstance*)_inst);
275 #else
276 SLV2UIInstance inst = (SLV2UIInstance)_inst;
277 if (inst) {
278 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(inst);
279 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(inst);
281 if (ui_desc) {
282 ui_desc->cleanup(ui_handle);
285 #endif
287 _inst = NULL;
288 _gui_widget = NULL;
291 LV2PluginUI::~LV2PluginUI ()
293 if (_values) {
294 delete[] _values;
297 /* Close and delete GUI. */
298 lv2ui_free();
300 _screen_update_connection.disconnect();
302 if (_lv2->is_external_ui()) {
303 /* External UI is no longer valid.
304 on_window_hide() will not try to use it if is NULL.
306 _external_ui_ptr = NULL;
311 LV2PluginUI::get_preferred_height()
313 Gtk::Requisition r = size_request();
314 return r.height;
318 LV2PluginUI::get_preferred_width()
320 Gtk::Requisition r = size_request();
321 return r.width;
325 LV2PluginUI::package(Gtk::Window& win)
327 if (_external_ui_ptr) {
328 _win_ptr = &win;
329 } else {
330 /* forward configure events to plugin window */
331 win.signal_configure_event().connect(
332 sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
333 win.signal_map_event().connect(
334 sigc::mem_fun(*this, &LV2PluginUI::start_updating));
335 win.signal_unmap_event().connect(
336 sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
338 return 0;
341 bool
342 LV2PluginUI::configure_handler(GdkEventConfigure*)
344 std::cout << "CONFIGURE" << std::endl;
345 return false;
348 bool
349 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
351 /* FIXME: use port notification properties
352 and/or new UI extension subscription methods
354 return true;
357 bool
358 LV2PluginUI::on_window_show(const std::string& title)
360 //cout << "on_window_show - " << title << endl; flush(cout);
362 if (_lv2->is_external_ui()) {
363 if (_external_ui_ptr) {
364 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
365 return false;
367 lv2ui_instantiate(title);
368 if (!_external_ui_ptr) {
369 return false;
372 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
373 _screen_update_connection.disconnect();
374 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
375 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
376 return false;
377 } else {
378 lv2ui_instantiate("gtk2gui");
381 return true;
384 void
385 LV2PluginUI::on_window_hide()
387 //cout << "on_window_hide" << endl; flush(cout);
389 if (_external_ui_ptr) {
390 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
391 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
392 //_external_ui_ptr = NULL;
393 //_screen_update_connection.disconnect();
394 } else {
395 lv2ui_free();