if STRIP is not set, don't strip included libraries either
[ardour2.git] / gtk2_ardour / lv2_plugin_ui.cc
blobf8924e5bec048c005863b8ec56722fa1217854ea
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/processor.h"
22 #include "ardour/lv2_plugin.h"
24 #include "ardour_ui.h"
25 #include "gui_thread.h"
26 #include "lv2_plugin_ui.h"
28 using namespace Gtk;
29 using namespace ARDOUR;
30 using namespace PBD;
32 void
33 LV2PluginUI::lv2_ui_write(
34 LV2UI_Controller controller,
35 uint32_t port_index,
36 uint32_t /*buffer_size*/,
37 uint32_t /*format*/,
38 const void* buffer)
40 //cout << "lv2_ui_write" << endl;
41 LV2PluginUI* me = (LV2PluginUI*)controller;
42 if (*(float*)buffer != me->_values[port_index]) {
43 //cout << "set_parameter " << port_index << ":" << *(float*)buffer << endl;
44 me->_lv2->set_parameter(port_index, *(float*)buffer);
48 void LV2PluginUI::on_external_ui_closed(LV2UI_Controller controller)
50 LV2PluginUI* me = (LV2PluginUI*)controller;
51 me->_screen_update_connection.disconnect();
52 me->_external_ui_ptr = NULL;
55 void
56 LV2PluginUI::parameter_changed (uint32_t port_index, float val)
58 PlugUIBase::parameter_changed (port_index, val);
60 if (val != _values[port_index]) {
61 parameter_update(port_index, val);
65 void
66 LV2PluginUI::parameter_update (uint32_t port_index, float val)
68 if (!_inst) {
69 return;
72 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
73 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
74 if (ui_desc->port_event)
75 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
76 _values[port_index] = val;
79 bool
80 LV2PluginUI::start_updating(GdkEventAny*)
82 if (!_output_ports.empty()) {
83 _screen_update_connection.disconnect();
84 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
85 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
87 return false;
90 bool
91 LV2PluginUI::stop_updating(GdkEventAny*)
93 //cout << "stop_updating" << endl;
95 if (//!_external_ui_ptr &&
96 !_output_ports.empty()) {
97 _screen_update_connection.disconnect();
99 return false;
102 void
103 LV2PluginUI::output_update()
105 //cout << "output_update" << endl;
106 if (_external_ui_ptr) {
107 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
110 /* FIXME only works with control output ports (which is all we support now anyway) */
111 uint32_t nports = _output_ports.size();
112 for (uint32_t i = 0; i < nports; ++i) {
113 uint32_t index = _output_ports[i];
114 parameter_changed(index, _lv2->get_parameter(index));
119 LV2PluginUI::LV2PluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<LV2Plugin> lv2p)
120 : PlugUIBase (pi)
121 , _lv2(lv2p)
122 , _inst(NULL)
123 , _values(NULL)
124 , _external_ui_ptr(NULL)
126 if (!_lv2->is_external_ui()) {
127 lv2ui_instantiate("gtk2gui");
131 void
132 LV2PluginUI::lv2ui_instantiate(const std::string& title)
134 LV2_Feature** features;
135 LV2_Feature** features_src;
136 LV2_Feature** features_dst;
137 size_t features_count;
138 bool is_external_ui;
140 is_external_ui = _lv2->is_external_ui();
142 if (is_external_ui) {
143 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
144 _external_ui_host.plugin_human_id = strdup(title.c_str());
146 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
147 _external_ui_feature.data = &_external_ui_host;
149 features_src = features = (LV2_Feature**)_lv2->features();
150 features_count = 2;
151 while (*features++) {
152 features_count++;
155 features_dst = features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * features_count);
156 features_dst[--features_count] = NULL;
157 features_dst[--features_count] = &_external_ui_feature;
158 while (features_count--) {
159 *features++ = *features_src++;
161 } else {
162 features_dst = (LV2_Feature**)_lv2->features();
165 _inst = slv2_ui_instantiate(
166 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
167 features_dst);
169 if (is_external_ui) {
170 free(features_dst);
173 uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
174 for (uint32_t i = 0; i < num_ports; ++i) {
175 if (_lv2->parameter_is_output(i) && _lv2->parameter_is_control(i) && is_update_wanted(i)) {
176 _output_ports.push_back(i);
180 _external_ui_ptr = NULL;
181 if (_inst) {
182 if (!is_external_ui) {
183 GtkWidget* c_widget = (GtkWidget*)slv2_ui_instance_get_widget(_inst);
184 _gui_widget = Glib::wrap(c_widget);
185 _gui_widget->show_all();
186 pack_start(*_gui_widget, true, true);
187 } else {
188 _external_ui_ptr = (struct lv2_external_ui *)slv2_ui_instance_get_widget(_inst);
192 _values = new float[num_ports];
193 for (uint32_t i = 0; i < num_ports; ++i) {
194 bool ok;
195 uint32_t port = _lv2->nth_parameter(i, ok);
196 if (ok) {
197 _values[port] = _lv2->get_parameter(port);
198 if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
199 parameter_update(port, _values[port]);
205 LV2PluginUI::~LV2PluginUI ()
207 //cout << "LV2PluginUI destructor called" << endl;
209 if (_values) {
210 delete[] _values;
212 // plugin destructor destroys the GTK GUI
215 const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
216 LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
218 /*Call cleanup to tell the plugin to close its GUI and delete it*/
220 if (ui_desc) {
221 ui_desc->cleanup(ui_handle);
224 _screen_update_connection.disconnect();
226 if (_lv2->is_external_ui()) {
227 /*external UI is no longer valid - on_window_hide() will not try to use it if is NULL*/
228 _external_ui_ptr = NULL;
233 LV2PluginUI::get_preferred_height ()
235 Gtk::Requisition r = size_request();
236 return r.height;
240 LV2PluginUI::get_preferred_width ()
242 Gtk::Requisition r = size_request();
243 return r.width;
247 LV2PluginUI::package (Gtk::Window& win)
249 //cout << "package" << endl;
250 if (_external_ui_ptr) {
251 _win_ptr = &win;
252 } else {
253 /* forward configure events to plugin window */
254 win.signal_configure_event().connect (sigc::mem_fun (*this, &LV2PluginUI::configure_handler));
255 win.signal_map_event().connect (sigc::mem_fun (*this, &LV2PluginUI::start_updating));
256 win.signal_unmap_event().connect (sigc::mem_fun (*this, &LV2PluginUI::stop_updating));
258 return 0;
261 bool
262 LV2PluginUI::configure_handler (GdkEventConfigure*)
264 std::cout << "CONFIGURE" << std::endl;
265 return false;
268 bool
269 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
271 /* FIXME this should check the port notification properties, which nobody sets now anyway :) */
272 return true;
275 bool
276 LV2PluginUI::on_window_show(const std::string& title)
278 //cout << "on_window_show - " << title << endl; flush(cout);
280 if (_lv2->is_external_ui()) {
281 if (_external_ui_ptr) {
282 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
283 return false;
285 lv2ui_instantiate(title);
286 if (!_external_ui_ptr) {
287 return false;
290 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
291 _screen_update_connection.disconnect();
292 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
293 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
294 return false;
297 return true;
300 void
301 LV2PluginUI::on_window_hide()
303 //cout << "on_window_hide" << endl; flush(cout);
305 if (_external_ui_ptr) {
306 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
307 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
308 //_external_ui_ptr = NULL;
309 //_screen_update_connection.disconnect();