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