more temporary debugging for slv2/calf issue
[ardour.git] / gtk2_ardour / lv2_plugin_ui.cc
1 /*
2     Copyright (C) 2008-2011 Paul Davis
3     Author: David Robillard
4
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.
9
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.
14
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.
18
19 */
20
21 #include "ardour/lv2_plugin.h"
22 #include "ardour/plugin_manager.h"
23 #include "ardour/processor.h"
24
25 #include "ardour_ui.h"
26 #include "gui_thread.h"
27 #include "lv2_plugin_ui.h"
28
29 #include "lv2_ui.h"
30
31 using namespace Gtk;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
36 SuilHost  LV2PluginUI::ui_host  = NULL;
37 SLV2Value LV2PluginUI::ui_GtkUI = NULL;
38 #endif
39
40 void
41 LV2PluginUI::lv2_ui_write(void*       controller,
42                           uint32_t    port_index,
43                           uint32_t    /*buffer_size*/,
44                           uint32_t    /*format*/,
45                           const void* buffer)
46 {
47         LV2PluginUI* me = (LV2PluginUI*)controller;
48         boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
49
50         if (ac) {
51                 ac->set_value(*(float*)buffer);
52         }
53 }
54
55 void
56 LV2PluginUI::on_external_ui_closed(void* controller)
57 {
58         LV2PluginUI* me = (LV2PluginUI*)controller;
59         me->_screen_update_connection.disconnect();
60         me->_external_ui_ptr = NULL;
61 }
62
63 void
64 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
65 {
66         PlugUIBase::parameter_changed(port_index, val);
67
68         if (val != _values[port_index]) {
69                 parameter_update(port_index, val);
70         }
71 }
72
73 void
74 LV2PluginUI::parameter_update(uint32_t port_index, float val)
75 {
76         if (!_inst) {
77                 return;
78         }
79
80 #ifdef HAVE_SUIL
81         suil_instance_port_event(_inst, port_index, 4, 0, &val);
82 #else
83         const LV2UI_Descriptor* ui_desc   = slv2_ui_instance_get_descriptor(_inst);
84         LV2UI_Handle            ui_handle = slv2_ui_instance_get_handle(_inst);
85         if (ui_desc->port_event) {
86                 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
87         }
88 #endif
89         _values[port_index] = val;
90 }
91
92 bool
93 LV2PluginUI::start_updating(GdkEventAny*)
94 {
95         if (!_output_ports.empty()) {
96                 _screen_update_connection.disconnect();
97                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
98                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
99         }
100         return false;
101 }
102
103 bool
104 LV2PluginUI::stop_updating(GdkEventAny*)
105 {
106         //cout << "stop_updating" << endl;
107
108         if ( //!_external_ui_ptr &&
109             !_output_ports.empty()) {
110                 _screen_update_connection.disconnect();
111         }
112         return false;
113 }
114
115 void
116 LV2PluginUI::output_update()
117 {
118         //cout << "output_update" << endl;
119         if (_external_ui_ptr) {
120                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
121         }
122
123         /* FIXME only works with control output ports (which is all we support now anyway) */
124         uint32_t nports = _output_ports.size();
125         for (uint32_t i = 0; i < nports; ++i) {
126                 uint32_t index = _output_ports[i];
127                 parameter_changed(index, _lv2->get_parameter(index));
128         }
129
130 }
131
132 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
133                          boost::shared_ptr<LV2Plugin>    lv2p)
134         : PlugUIBase(pi)
135         , _lv2(lv2p)
136         , _values(NULL)
137         , _external_ui_ptr(NULL)
138         , _inst(NULL)
139 {
140         if (!_lv2->is_external_ui()) {
141                 lv2ui_instantiate("gtk2gui");
142         }
143 }
144
145 void
146 LV2PluginUI::lv2ui_instantiate(const std::string& title)
147 {
148         LV2_Feature** features;
149         LV2_Feature** features_src;
150         LV2_Feature** features_dst;
151         size_t        features_count;
152         bool          is_external_ui;
153
154         is_external_ui = _lv2->is_external_ui();
155
156         if (is_external_ui) {
157                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
158                 _external_ui_host.plugin_human_id = strdup(title.c_str());
159
160                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
161                 _external_ui_feature.data = &_external_ui_host;
162
163                 features_src = features = (LV2_Feature**)_lv2->features();
164                 features_count = 2;
165                 while (*features++) {
166                         features_count++;
167                 }
168                 features_dst = features = (LV2_Feature**)malloc(
169                         sizeof(LV2_Feature*) * features_count);
170                 features_dst[--features_count] = NULL;
171                 features_dst[--features_count] = &_external_ui_feature;
172                 while (features_count--) {
173                         *features++ = *features_src++;
174                 }
175         } else {
176                 features_dst = (LV2_Feature**)_lv2->features();
177         }
178
179 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
180         if (!LV2PluginUI::ui_host) {
181                 LV2PluginUI::ui_GtkUI = slv2_value_new_uri(
182                         ARDOUR::PluginManager::the_manager()->lv2_world()->world,
183                         "http://lv2plug.in/ns/extensions/ui#GtkUI");
184                 LV2PluginUI::ui_host = suil_host_new(
185                 LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
186         }
187         SLV2UI ui = _lv2->slv2_ui();
188
189         cerr << "get ui_GtkUI as URI\n";
190         slv2_value_as_uri (ui_GtkUI);
191         cerr << "get plugin as URI\n";
192         slv2_value_as_uri (slv2_plugin_get_uri(_lv2->slv2_plugin()));
193         cerr << "get ui as URI\n";
194         slv2_value_as_uri (slv2_ui_get_uri(ui));
195         cerr << "get ui_type as URI\n";
196         slv2_value_as_uri (_lv2->ui_type());
197         cerr << "get bundle as URI\n";
198         slv2_value_as_uri (slv2_ui_get_bundle_uri(ui));
199         cerr << "get binary as URI\n";
200         slv2_value_as_uri (slv2_ui_get_binary_uri(ui));
201         cerr << "Now instantiate the GUI...\n";
202         
203         _inst = suil_instance_new(
204                 LV2PluginUI::ui_host,
205                 this,
206                 slv2_value_as_uri(ui_GtkUI),
207                 slv2_value_as_uri(slv2_plugin_get_uri(_lv2->slv2_plugin())),
208                 slv2_value_as_uri(slv2_ui_get_uri(ui)),
209                 slv2_value_as_uri(_lv2->ui_type()),
210                 slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_bundle_uri(ui))),
211                 slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_binary_uri(ui))),
212                 features_dst);
213 #else
214         _inst = slv2_ui_instantiate(
215                 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
216                 features_dst);
217 #endif
218
219         if (is_external_ui) {
220                 free(features_dst);
221         }
222
223 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
224 #define GET_WIDGET(inst) suil_instance_get_widget(inst);
225 #else
226 #define GET_WIDGET(inst) slv2_ui_instance_get_widget(inst);
227 #endif
228
229         uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
230         for (uint32_t i = 0; i < num_ports; ++i) {
231                 if (_lv2->parameter_is_output(i)
232                     && _lv2->parameter_is_control(i)
233                     && is_update_wanted(i)) {
234                         _output_ports.push_back(i);
235                 }
236         }
237
238         _external_ui_ptr = NULL;
239         if (_inst) {
240                 if (!is_external_ui) {
241                         GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
242                         _gui_widget = Glib::wrap(c_widget);
243                         _gui_widget->show_all();
244                         pack_start(*_gui_widget, true, true);
245                 } else {
246                         _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
247                 }
248         }
249
250         _values = new float[num_ports];
251         _controllables.resize(num_ports);
252         for (uint32_t i = 0; i < num_ports; ++i) {
253                 bool     ok;
254                 uint32_t port = _lv2->nth_parameter(i, ok);
255                 if (ok) {
256                         _values[port]        = _lv2->get_parameter(port);
257                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
258                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
259
260                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
261                                 parameter_update(port, _values[port]);
262                         }
263                 }
264         }
265 }
266
267 LV2PluginUI::~LV2PluginUI ()
268 {
269         //cout << "LV2PluginUI destructor called" << endl;
270
271         if (_values) {
272                 delete[] _values;
273         }
274
275         /* Close and delete GUI. */
276 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
277         suil_instance_free(_inst);
278 #else
279         const LV2UI_Descriptor* ui_desc   = slv2_ui_instance_get_descriptor(_inst);
280         LV2UI_Handle            ui_handle = slv2_ui_instance_get_handle(_inst);
281
282         if (ui_desc) {
283                 ui_desc->cleanup(ui_handle);
284         }
285 #endif
286
287         _screen_update_connection.disconnect();
288
289         if (_lv2->is_external_ui()) {
290                 /* External UI is no longer valid.
291                    on_window_hide() will not try to use it if is NULL.
292                 */
293                 _external_ui_ptr = NULL;
294         }
295 }
296
297 int
298 LV2PluginUI::get_preferred_height()
299 {
300         Gtk::Requisition r = size_request();
301         return r.height;
302 }
303
304 int
305 LV2PluginUI::get_preferred_width()
306 {
307         Gtk::Requisition r = size_request();
308         return r.width;
309 }
310
311 int
312 LV2PluginUI::package(Gtk::Window& win)
313 {
314         if (_external_ui_ptr) {
315                 _win_ptr = &win;
316         } else {
317                 /* forward configure events to plugin window */
318                 win.signal_configure_event().connect(
319                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
320                 win.signal_map_event().connect(
321                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
322                 win.signal_unmap_event().connect(
323                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
324         }
325         return 0;
326 }
327
328 bool
329 LV2PluginUI::configure_handler(GdkEventConfigure*)
330 {
331         std::cout << "CONFIGURE" << std::endl;
332         return false;
333 }
334
335 bool
336 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
337 {
338         /* FIXME: use port notification properties
339            and/or new UI extension subscription methods
340         */
341         return true;
342 }
343
344 bool
345 LV2PluginUI::on_window_show(const std::string& title)
346 {
347         //cout << "on_window_show - " << title << endl; flush(cout);
348
349         if (_lv2->is_external_ui()) {
350                 if (_external_ui_ptr) {
351                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
352                         return false;
353                 }
354                 lv2ui_instantiate(title);
355                 if (!_external_ui_ptr) {
356                         return false;
357                 }
358
359                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
360                 _screen_update_connection.disconnect();
361                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
362                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
363                 return false;
364         }
365
366         return true;
367 }
368
369 void
370 LV2PluginUI::on_window_hide()
371 {
372         //cout << "on_window_hide" << endl; flush(cout);
373
374         if (_external_ui_ptr) {
375                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
376                 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
377                 //_external_ui_ptr = NULL;
378                 //_screen_update_connection.disconnect();
379         }
380 }