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