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