Make LV2World local to lv2_plugin.cc
[ardour2.git] / gtk2_ardour / lv2_plugin_ui.cc
blob8e2cd34164bba04f47b546ec94043f351916011b
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 using namespace Gtk;
32 using namespace ARDOUR;
33 using namespace PBD;
35 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
37 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
38 SuilHost* LV2PluginUI::ui_host = NULL;
39 #endif
41 void
42 LV2PluginUI::lv2_ui_write(void* controller,
43 uint32_t port_index,
44 uint32_t /*buffer_size*/,
45 uint32_t /*format*/,
46 const void* buffer)
48 LV2PluginUI* me = (LV2PluginUI*)controller;
49 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
51 if (ac) {
52 ac->set_value(*(float*)buffer);
56 void
57 LV2PluginUI::on_external_ui_closed(void* controller)
59 LV2PluginUI* me = (LV2PluginUI*)controller;
60 me->_screen_update_connection.disconnect();
61 me->_external_ui_ptr = NULL;
64 void
65 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
67 PlugUIBase::parameter_changed(port_index, val);
69 if (val != _values[port_index]) {
70 parameter_update(port_index, val);
74 void
75 LV2PluginUI::parameter_update(uint32_t port_index, float val)
77 if (!_inst) {
78 return;
81 #ifdef HAVE_SUIL
82 suil_instance_port_event(_inst, port_index, 4, 0, &val);
83 #else
84 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
85 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
86 if (ui_desc->port_event) {
87 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
89 #endif
90 _values[port_index] = val;
93 bool
94 LV2PluginUI::start_updating(GdkEventAny*)
96 if (!_output_ports.empty()) {
97 _screen_update_connection.disconnect();
98 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
99 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
101 return false;
104 bool
105 LV2PluginUI::stop_updating(GdkEventAny*)
107 //cout << "stop_updating" << endl;
109 if ( //!_external_ui_ptr &&
110 !_output_ports.empty()) {
111 _screen_update_connection.disconnect();
113 return false;
116 void
117 LV2PluginUI::output_update()
119 //cout << "output_update" << endl;
120 if (_external_ui_ptr) {
121 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
124 /* FIXME only works with control output ports (which is all we support now anyway) */
125 uint32_t nports = _output_ports.size();
126 for (uint32_t i = 0; i < nports; ++i) {
127 uint32_t index = _output_ports[i];
128 parameter_changed(index, _lv2->get_parameter(index));
133 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
134 boost::shared_ptr<LV2Plugin> lv2p)
135 : PlugUIBase(pi)
136 , _lv2(lv2p)
137 , _values(NULL)
138 , _external_ui_ptr(NULL)
139 , _inst(NULL)
141 if (!_lv2->is_external_ui()) {
142 lv2ui_instantiate("gtk2gui");
146 void
147 LV2PluginUI::lv2ui_instantiate(const std::string& title)
149 LV2_Feature** features;
150 LV2_Feature** features_src;
151 LV2_Feature** features_dst;
152 size_t features_count;
153 bool is_external_ui;
155 is_external_ui = _lv2->is_external_ui();
157 if (is_external_ui) {
158 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
159 _external_ui_host.plugin_human_id = strdup(title.c_str());
161 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
162 _external_ui_feature.data = &_external_ui_host;
164 features_src = features = (LV2_Feature**)_lv2->features();
165 features_count = 2;
166 while (*features++) {
167 features_count++;
169 features_dst = features = (LV2_Feature**)malloc(
170 sizeof(LV2_Feature*) * features_count);
171 features_dst[--features_count] = NULL;
172 features_dst[--features_count] = &_external_ui_feature;
173 while (features_count--) {
174 *features++ = *features_src++;
176 } else {
177 features_dst = (LV2_Feature**)_lv2->features();
180 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
181 if (!LV2PluginUI::ui_host) {
182 LV2PluginUI::ui_host = suil_host_new(
183 LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
185 const char* container_type = (is_external_ui)
186 ? NS_UI "external"
187 : NS_UI "GtkUI";
189 SLV2UI ui = _lv2->slv2_ui();
190 _inst = suil_instance_new(
191 LV2PluginUI::ui_host,
192 this,
193 container_type,
194 slv2_value_as_uri(slv2_plugin_get_uri(_lv2->slv2_plugin())),
195 slv2_value_as_uri(slv2_ui_get_uri(ui)),
196 slv2_value_as_uri(_lv2->ui_type()),
197 slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_bundle_uri(ui))),
198 slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_binary_uri(ui))),
199 features_dst);
200 #else
201 _inst = slv2_ui_instantiate(
202 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
203 features_dst);
204 #endif
206 if (is_external_ui) {
207 free(features_dst);
210 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
211 #define GET_WIDGET(inst) suil_instance_get_widget(inst);
212 #else
213 #define GET_WIDGET(inst) slv2_ui_instance_get_widget(inst);
214 #endif
216 uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
217 for (uint32_t i = 0; i < num_ports; ++i) {
218 if (_lv2->parameter_is_output(i)
219 && _lv2->parameter_is_control(i)
220 && is_update_wanted(i)) {
221 _output_ports.push_back(i);
225 _external_ui_ptr = NULL;
226 if (_inst) {
227 if (!is_external_ui) {
228 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
229 _gui_widget = Glib::wrap(c_widget);
230 _gui_widget->show_all();
231 pack_start(*_gui_widget, true, true);
232 } else {
233 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
237 _values = new float[num_ports];
238 _controllables.resize(num_ports);
239 for (uint32_t i = 0; i < num_ports; ++i) {
240 bool ok;
241 uint32_t port = _lv2->nth_parameter(i, ok);
242 if (ok) {
243 _values[port] = _lv2->get_parameter(port);
244 _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
245 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
247 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
248 parameter_update(port, _values[port]);
254 LV2PluginUI::~LV2PluginUI ()
256 //cout << "LV2PluginUI destructor called" << endl;
258 if (_values) {
259 delete[] _values;
262 /* Close and delete GUI. */
263 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
264 suil_instance_free(_inst);
265 #else
266 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
267 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
269 if (ui_desc) {
270 ui_desc->cleanup(ui_handle);
272 #endif
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();
288 return r.height;
292 LV2PluginUI::get_preferred_width()
294 Gtk::Requisition r = size_request();
295 return r.width;
299 LV2PluginUI::package(Gtk::Window& win)
301 if (_external_ui_ptr) {
302 _win_ptr = &win;
303 } else {
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));
312 return 0;
315 bool
316 LV2PluginUI::configure_handler(GdkEventConfigure*)
318 std::cout << "CONFIGURE" << std::endl;
319 return false;
322 bool
323 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
325 /* FIXME: use port notification properties
326 and/or new UI extension subscription methods
328 return true;
331 bool
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);
339 return false;
341 lv2ui_instantiate(title);
342 if (!_external_ui_ptr) {
343 return false;
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));
350 return false;
353 return true;
356 void
357 LV2PluginUI::on_window_hide()
359 //cout << "on_window_hide" << endl; flush(cout);
361 if (_external_ui_ptr) {
362 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
363 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
364 //_external_ui_ptr = NULL;
365 //_screen_update_connection.disconnect();