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