fix import/embed with "sequence files" option
[ardour2.git] / gtk2_ardour / lv2_plugin_ui.cc
blob7fb2c87b055b1befd6b05ad942237d2f83391fa9
1 /*
2 Copyright (C) 2008 Paul Davis
3 Author: Dave 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/insert.h>
22 #include <ardour/lv2_plugin.h>
24 #include "ardour_ui.h"
25 #include "lv2_plugin_ui.h"
27 using namespace Gtk;
28 using namespace ARDOUR;
29 using namespace PBD;
31 std::vector<struct lv2_external_ui*> g_external_uis;
33 void close_external_ui_windows()
35 struct lv2_external_ui* external_ui_ptr;
37 //cout << "close_external_ui_windows" << endl;
39 while (!g_external_uis.empty()) {
40 //cout << "pop" << endl;
41 external_ui_ptr = g_external_uis.back();
42 LV2_EXTERNAL_UI_HIDE(external_ui_ptr);
43 g_external_uis.pop_back();
47 void
48 LV2PluginUI::lv2_ui_write(LV2UI_Controller controller,
49 uint32_t port_index,
50 uint32_t buffer_size,
51 uint32_t format,
52 const void* buffer)
54 //cout << "lv2_ui_write" << endl;
55 LV2PluginUI* me = (LV2PluginUI*)controller;
56 if (*(float*)buffer != me->_values[port_index]) {
57 //cout << "set_parameter " << port_index << ":" << *(float*)buffer << endl;
58 me->_lv2->set_parameter(port_index, *(float*)buffer);
62 void LV2PluginUI::on_external_ui_closed(LV2UI_Controller controller)
64 //cout << "on_external_ui_closed" << endl;
66 LV2PluginUI* me = (LV2PluginUI*)controller;
67 me->_screen_update_connection.disconnect();
68 //me->insert->set_gui(0);
70 for (vector<struct lv2_external_ui*>::iterator it = g_external_uis.begin() ; it < g_external_uis.end(); it++) {
71 if (*it == me->_external_ui_ptr) {
72 g_external_uis.erase(it);
76 //slv2_ui_instance_get_descriptor(me->_inst)->cleanup(me->_inst);
77 me->_external_ui_ptr = NULL;
80 void
81 LV2PluginUI::parameter_changed (uint32_t port_index, float val)
83 //cout << "parameter_changed" << endl;
84 if (val != _values[port_index]) {
85 parameter_update(port_index, val);
89 void
90 LV2PluginUI::parameter_update (uint32_t port_index, float val)
92 if (!_inst) {
93 return;
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 //cout << "port_event " << port_index << ":" << val << endl;
100 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
102 _values[port_index] = val;
105 bool
106 LV2PluginUI::start_updating(GdkEventAny* event)
108 //cout << "start_updating" << endl;
110 if (!_output_ports.empty()) {
111 _screen_update_connection.disconnect();
112 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
113 (mem_fun(*this, &LV2PluginUI::output_update));
115 return false;
118 bool
119 LV2PluginUI::stop_updating(GdkEventAny* event)
121 //cout << "stop_updating" << endl;
123 if (//!_external_ui_ptr &&
124 !_output_ports.empty()) {
125 _screen_update_connection.disconnect();
127 return false;
130 void
131 LV2PluginUI::output_update()
133 //cout << "output_update" << endl;
134 if (_external_ui_ptr) {
135 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
138 /* FIXME only works with control output ports (which is all we support now anyway) */
139 uint32_t nports = _output_ports.size();
140 for (uint32_t i = 0; i < nports; ++i) {
141 uint32_t index = _output_ports[i];
142 parameter_changed(index, _lv2->get_parameter(index));
147 LV2PluginUI::LV2PluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<LV2Plugin> lv2p)
148 : PlugUIBase (pi)
149 , _lv2(lv2p)
150 , _inst(NULL)
151 , _values(NULL)
152 , _external_ui_ptr(NULL)
154 if (!_lv2->is_external_ui()) {
155 lv2ui_instantiate("gtk2gui");
159 void
160 LV2PluginUI::lv2ui_instantiate(const Glib::ustring& title)
162 LV2_Feature** features;
163 LV2_Feature** features_src;
164 LV2_Feature** features_dst;
165 size_t features_count;
166 bool is_external_ui;
168 is_external_ui = _lv2->is_external_ui();
170 if (is_external_ui) {
171 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
172 _external_ui_host.plugin_human_id = strdup(title.c_str());
174 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
175 _external_ui_feature.data = &_external_ui_host;
177 features_src = features = (LV2_Feature**)_lv2->features();
178 features_count = 2;
179 while (*features++) {
180 features_count++;
183 features_dst = features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * features_count);
184 features_dst[--features_count] = NULL;
185 features_dst[--features_count] = &_external_ui_feature;
186 while (features_count--) {
187 *features++ = *features_src++;
189 } else {
190 features_dst = (LV2_Feature**)_lv2->features();
193 _inst = slv2_ui_instantiate(
194 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
195 features_dst);
197 if (is_external_ui) {
198 free(features_dst);
201 uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
202 for (uint32_t i = 0; i < num_ports; ++i) {
203 if (_lv2->parameter_is_output(i) && _lv2->parameter_is_control(i) && is_update_wanted(i)) {
204 _output_ports.push_back(i);
208 _external_ui_ptr = NULL;
209 if (_inst) {
210 if (!is_external_ui) {
211 GtkWidget* c_widget = (GtkWidget*)slv2_ui_instance_get_widget(_inst);
212 _gui_widget = Glib::wrap(c_widget);
213 _gui_widget->show_all();
214 pack_start(*_gui_widget, true, true);
215 } else {
216 _external_ui_ptr = (struct lv2_external_ui *)slv2_ui_instance_get_widget(_inst);
217 g_external_uis.push_back(_external_ui_ptr);
221 _values = new float[num_ports];
222 for (uint32_t i = 0; i < num_ports; ++i) {
223 bool ok;
224 uint32_t port = _lv2->nth_parameter(i, ok);
225 if (ok) {
226 _values[port] = _lv2->get_parameter(port);
227 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
228 parameter_update(port, _values[port]);
233 _lv2->ParameterChanged.connect(mem_fun(*this, &LV2PluginUI::parameter_changed));
236 LV2PluginUI::~LV2PluginUI ()
238 //cout << "LV2PluginUI destructor called" << endl;
240 if (_values) {
241 delete[] _values;
243 // plugin destructor destroys the GTK GUI
247 LV2PluginUI::get_preferred_height ()
249 Gtk::Requisition r = size_request();
250 return r.height;
254 LV2PluginUI::get_preferred_width ()
256 Gtk::Requisition r = size_request();
257 return r.width;
261 LV2PluginUI::package (Gtk::Window& win)
263 //cout << "package" << endl;
264 if (_external_ui_ptr) {
265 _win_ptr = &win;
266 } else {
267 /* forward configure events to plugin window */
268 win.signal_configure_event().connect (mem_fun (*this, &LV2PluginUI::configure_handler));
269 win.signal_map_event().connect (mem_fun (*this, &LV2PluginUI::start_updating));
270 win.signal_unmap_event().connect (mem_fun (*this, &LV2PluginUI::stop_updating));
272 return 0;
275 bool
276 LV2PluginUI::configure_handler (GdkEventConfigure* ev)
278 cout << "CONFIGURE" << endl;
279 return false;
282 bool
283 LV2PluginUI::is_update_wanted(uint32_t index)
285 /* FIXME this should check the port notification properties, which nobody sets now anyway :) */
286 return true;
289 bool
290 LV2PluginUI::on_window_show(const Glib::ustring& title)
292 //cout << "on_window_show - " << title << endl; flush(cout);
294 if (_lv2->is_external_ui()) {
295 if (_external_ui_ptr) {
296 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
297 return false;
299 lv2ui_instantiate(title);
300 if (!_external_ui_ptr) {
301 return false;
304 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
305 _screen_update_connection.disconnect();
306 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
307 (mem_fun(*this, &LV2PluginUI::output_update));
308 return false;
311 return true;
314 void
315 LV2PluginUI::on_window_hide()
317 //cout << "on_window_hide" << endl; flush(cout);
319 if (_external_ui_ptr) {
320 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
321 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
322 //_external_ui_ptr = NULL;
323 //_screen_update_connection.disconnect();