Put the sidechain ports into a dedicated tab in PortMatrix
[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/session.h"
22 #include "pbd/error.h"
23 #include "pbd/stacktrace.h"
24
25 #include "gui_thread.h"
26 #include "lv2_plugin_ui.h"
27 #include "timers.h"
28
29 #include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
30
31 #include <lilv/lilv.h>
32 #include <suil/suil.h>
33
34 #include "pbd/i18n.h"
35
36 using namespace ARDOUR;
37 using namespace Gtk;
38 using namespace PBD;
39
40 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
41
42 static SuilHost* ui_host = NULL;
43
44 void
45 LV2PluginUI::write_from_ui(void*       controller,
46                            uint32_t    port_index,
47                            uint32_t    buffer_size,
48                            uint32_t    format,
49                            const void* buffer)
50 {
51         LV2PluginUI* me = (LV2PluginUI*)controller;
52         if (format == 0) {
53                 if (port_index >= me->_controllables.size()) {
54                         return;
55                 }
56
57                 boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
58
59                 me->_updates.insert (port_index);
60
61                 if (ac) {
62                         ac->set_value(*(const float*)buffer, Controllable::NoGroup);
63                 }
64         } else if (format == URIMap::instance().urids.atom_eventTransfer) {
65
66                 const int cnt = me->_pi->get_count();
67                 for (int i=0; i < cnt; i++ ) {
68                         boost::shared_ptr<LV2Plugin> lv2i = boost::dynamic_pointer_cast<LV2Plugin> (me->_pi->plugin(i));
69                         lv2i->write_from_ui(port_index, format, buffer_size, (const uint8_t*)buffer);
70                 }
71         }
72 }
73
74 void
75 LV2PluginUI::write_to_ui(void*       controller,
76                          uint32_t    port_index,
77                          uint32_t    buffer_size,
78                          uint32_t    format,
79                          const void* buffer)
80 {
81         LV2PluginUI* me = (LV2PluginUI*)controller;
82         if (me->_inst) {
83                 suil_instance_port_event((SuilInstance*)me->_inst,
84                                          port_index, buffer_size, format, buffer);
85         }
86 }
87
88 uint32_t
89 LV2PluginUI::port_index(void* controller, const char* symbol)
90 {
91         return ((LV2PluginUI*)controller)->_lv2->port_index(symbol);
92 }
93
94 void
95 LV2PluginUI::touch(void*    controller,
96                    uint32_t port_index,
97                    bool     grabbed)
98 {
99         LV2PluginUI* me = (LV2PluginUI*)controller;
100         if (port_index >= me->_controllables.size()) {
101                 return;
102         }
103         if (!me->_lv2->parameter_is_control(port_index) || !me->_lv2->parameter_is_input(port_index)) {
104                 return;
105         }
106
107         ControllableRef control = me->_controllables[port_index];
108         if (grabbed) {
109                 control->start_touch(control->session().transport_sample());
110         } else {
111                 control->stop_touch(control->session().transport_sample());
112         }
113 }
114
115 void
116 LV2PluginUI::update_timeout()
117 {
118         _lv2->emit_to_ui(this, &LV2PluginUI::write_to_ui);
119 }
120
121 void
122 LV2PluginUI::on_external_ui_closed(void* controller)
123 {
124         //printf("LV2PluginUI::on_external_ui_closed\n");
125         LV2PluginUI* me = (LV2PluginUI*)controller;
126         me->_screen_update_connection.disconnect();
127         me->_external_ui_ptr = NULL;
128 }
129
130 void
131 LV2PluginUI::control_changed (uint32_t port_index)
132 {
133         /* Must run in GUI thread because we modify _updates with no lock */
134         if (_lv2->get_parameter (port_index) != _values_last_sent_to_ui[port_index]) {
135                 /* current plugin parameter does not match last value received
136                    from GUI, so queue an update to push it to the GUI during
137                    our regular timeout.
138                 */
139                 _updates.insert (port_index);
140         }
141 }
142
143 bool
144 LV2PluginUI::start_updating(GdkEventAny*)
145 {
146         _screen_update_connection.disconnect();
147         _screen_update_connection = Timers::super_rapid_connect
148                 (sigc::mem_fun(*this, &LV2PluginUI::output_update));
149         return false;
150 }
151
152 bool
153 LV2PluginUI::stop_updating(GdkEventAny*)
154 {
155         //cout << "stop_updating" << endl;
156         _screen_update_connection.disconnect();
157         return false;
158 }
159
160 void
161 LV2PluginUI::queue_port_update()
162 {
163         const uint32_t num_ports = _lv2->num_ports();
164         for (uint32_t i = 0; i < num_ports; ++i) {
165                 bool     ok;
166                 uint32_t port = _lv2->nth_parameter(i, ok);
167                 if (ok) {
168                         _updates.insert (port);
169                 }
170         }
171 }
172
173 void
174 LV2PluginUI::output_update()
175 {
176         //cout << "output_update" << endl;
177         if (_external_ui_ptr) {
178                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
179                 if (_lv2->is_external_kx() && !_external_ui_ptr) {
180                         // clean up external UI if it closes itself via
181                         // on_external_ui_closed() during run()
182                         //printf("LV2PluginUI::output_update -- UI was closed\n");
183                         //_screen_update_connection.disconnect();
184                         _message_update_connection.disconnect();
185                         if (_inst) {
186                                 suil_instance_free((SuilInstance*)_inst);
187                         }
188                         _inst = NULL;
189                         _external_ui_ptr = NULL;
190                         return;
191                 }
192         }
193
194         if (!_inst) {
195                 return;
196         }
197
198         /* output ports (values set by DSP) need propagating to GUI */
199
200         uint32_t nports = _output_ports.size();
201         for (uint32_t i = 0; i < nports; ++i) {
202                 uint32_t index = _output_ports[i];
203                 float val = _lv2->get_parameter (index);
204
205                 if (val != _values_last_sent_to_ui[index]) {
206                         /* Send to GUI */
207                         suil_instance_port_event ((SuilInstance*)_inst, index, 4, 0, &val);
208                         /* Cache current value */
209                         _values_last_sent_to_ui[index] = val;
210                 }
211         }
212
213         /* Input ports marked for update because the control value changed
214            since the last redisplay.
215         */
216
217         for (Updates::iterator i = _updates.begin(); i != _updates.end(); ++i) {
218                 float val = _lv2->get_parameter (*i);
219                 /* push current value to the GUI */
220                 suil_instance_port_event ((SuilInstance*)_inst, (*i), 4, 0, &val);
221                 _values_last_sent_to_ui[(*i)] = val;
222         }
223
224         _updates.clear ();
225 }
226
227 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
228                          boost::shared_ptr<LV2Plugin>    lv2p)
229         : PlugUIBase(pi)
230         , _pi(pi)
231         , _lv2(lv2p)
232         , _gui_widget(NULL)
233         , _values_last_sent_to_ui(NULL)
234         , _external_ui_ptr(NULL)
235         , _inst(NULL)
236 {
237         _ardour_buttons_box.set_spacing (6);
238         _ardour_buttons_box.set_border_width (6);
239         _ardour_buttons_box.pack_end (focus_button, false, false);
240         _ardour_buttons_box.pack_end (bypass_button, false, false, 4);
241         if (pi->controls().size() > 0) {
242                 _ardour_buttons_box.pack_end (reset_button, false, false, 4);
243         }
244         _ardour_buttons_box.pack_end (delete_button, false, false);
245         _ardour_buttons_box.pack_end (save_button, false, false);
246         _ardour_buttons_box.pack_end (add_button, false, false);
247         _ardour_buttons_box.pack_end (_preset_combo, false, false);
248         _ardour_buttons_box.pack_end (_preset_modified, false, false);
249         _ardour_buttons_box.pack_end (pin_management_button, false, false);
250
251         plugin->PresetLoaded.connect (*this, invalidator (*this), boost::bind (&LV2PluginUI::queue_port_update, this), gui_context ());
252 }
253
254 void
255 LV2PluginUI::lv2ui_instantiate(const std::string& title)
256 {
257         bool          is_external_ui = _lv2->is_external_ui();
258         LV2_Feature** features_src   = const_cast<LV2_Feature**>(_lv2->features());
259         LV2_Feature** features       = const_cast<LV2_Feature**>(_lv2->features());
260         size_t        features_count = 0;
261         while (*features++) {
262                 features_count++;
263         }
264
265         Gtk::Alignment* container = NULL;
266         if (is_external_ui) {
267                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
268                 _external_ui_host.plugin_human_id = strdup(title.c_str());
269
270                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
271                 _external_ui_feature.data = &_external_ui_host;
272
273                 _external_kxui_feature.URI  = LV2_EXTERNAL_UI_KX__Host;
274                 _external_kxui_feature.data = &_external_ui_host;
275
276                 ++features_count;
277                 features = (LV2_Feature**)malloc(
278                         sizeof(LV2_Feature*) * (features_count + 2));
279                 for (size_t i = 0; i < features_count - 2; ++i) {
280                         features[i] = features_src[i];
281                 }
282                 features[features_count - 2] = &_external_kxui_feature;
283                 features[features_count - 1] = &_external_ui_feature;
284                 features[features_count]     = NULL;
285         } else {
286                 if (_ardour_buttons_box.get_parent()) {
287                         _ardour_buttons_box.get_parent()->remove(_ardour_buttons_box);
288                 }
289                 pack_start(_ardour_buttons_box, false, false);
290                 _ardour_buttons_box.show_all();
291
292                 _gui_widget = Gtk::manage((container = new Gtk::Alignment()));
293                 pack_start(*_gui_widget, true, true);
294                 _gui_widget->show();
295
296                 _parent_feature.URI  = LV2_UI__parent;
297                 _parent_feature.data = _gui_widget->gobj();
298
299                 ++features_count;
300                 features = (LV2_Feature**)malloc(
301                         sizeof(LV2_Feature*) * (features_count + 1));
302                 for (size_t i = 0; i < features_count - 1; ++i) {
303                         features[i] = features_src[i];
304                 }
305                 features[features_count - 1] = &_parent_feature;
306                 features[features_count]     = NULL;
307         }
308
309         if (!ui_host) {
310                 ui_host = suil_host_new(LV2PluginUI::write_from_ui,
311                                         LV2PluginUI::port_index,
312                                         NULL, NULL);
313                 suil_host_set_touch_func(ui_host, LV2PluginUI::touch);
314         }
315         const char* container_type = (is_external_ui)
316                 ? NS_UI "external"
317                 : NS_UI "GtkUI";
318
319         if (_lv2->has_message_output()) {
320                 _lv2->enable_ui_emission();
321         }
322
323         const LilvUI*   ui     = (const LilvUI*)_lv2->c_ui();
324         const LilvNode* bundle = lilv_ui_get_bundle_uri(ui);
325         const LilvNode* binary = lilv_ui_get_binary_uri(ui);
326 #ifdef HAVE_LILV_0_21_3
327         char* ui_bundle_path = lilv_file_uri_parse(lilv_node_as_uri(bundle), NULL);
328         char* ui_binary_path = lilv_file_uri_parse(lilv_node_as_uri(binary), NULL);
329 #else
330         char* ui_bundle_path = strdup(lilv_uri_to_path(lilv_node_as_uri(bundle)));
331         char* ui_binary_path = strdup(lilv_uri_to_path(lilv_node_as_uri(binary)));
332 #endif
333         if (!ui_bundle_path || !ui_binary_path) {
334                 error << _("failed to get path for UI bindle or binary") << endmsg;
335                 free(ui_bundle_path);
336                 free(ui_binary_path);
337                 free(features);
338                 return;
339         }
340
341         _inst = suil_instance_new(
342                 ui_host,
343                 this,
344                 container_type,
345                 _lv2->uri(),
346                 lilv_node_as_uri(lilv_ui_get_uri(ui)),
347                 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
348                 ui_bundle_path,
349                 ui_binary_path,
350                 features);
351
352         free(ui_bundle_path);
353         free(ui_binary_path);
354         free(features);
355
356         if (!_inst) {
357                 error << _("failed to instantiate LV2 GUI") << endmsg;
358                 return;
359         }
360
361 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
362
363         const uint32_t num_ports = _lv2->num_ports();
364         for (uint32_t i = 0; i < num_ports; ++i) {
365                 if (_lv2->parameter_is_output(i)
366                     && _lv2->parameter_is_control(i)
367                     && is_update_wanted(i)) {
368                         _output_ports.push_back(i);
369                 }
370         }
371
372         _external_ui_ptr = NULL;
373         if (!is_external_ui) {
374                 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
375                 if (!c_widget) {
376                         error << _("failed to get LV2 UI widget") << endmsg;
377                         suil_instance_free((SuilInstance*)_inst);
378                         _inst = NULL;
379                         return;
380                 }
381                 if (!container->get_child()) {
382                         // Suil didn't add the UI to the container for us, so do it now
383                         container->add(*Gtk::manage(Glib::wrap(c_widget)));
384                 }
385                 container->show_all();
386                 gtk_widget_set_can_focus(c_widget, true);
387                 gtk_widget_grab_focus(c_widget);
388         } else {
389                 _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
390         }
391
392         _values_last_sent_to_ui = new float[num_ports];
393         _controllables.resize(num_ports);
394
395         for (uint32_t i = 0; i < num_ports; ++i) {
396                 bool     ok;
397                 uint32_t port = _lv2->nth_parameter(i, ok);
398                 if (ok) {
399                         /* Cache initial value of the parameter, regardless of
400                            whether it is input or output
401                         */
402
403                         _values_last_sent_to_ui[port]        = _lv2->get_parameter(port);
404                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
405                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
406
407                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
408                                 if (_controllables[port]) {
409                                         _controllables[port]->Changed.connect (control_connections, invalidator (*this), boost::bind (&LV2PluginUI::control_changed, this, port), gui_context());
410                                 }
411                         }
412
413                         /* queue for first update ("push") to GUI */
414                         _updates.insert (port);
415                 }
416         }
417
418         if (_lv2->has_message_output()) {
419                 _message_update_connection = Timers::super_rapid_connect (
420                         sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
421         }
422 }
423
424 void
425 LV2PluginUI::grab_focus()
426 {
427         if (_inst && !_lv2->is_external_ui()) {
428                 GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
429                 gtk_widget_grab_focus(c_widget);
430         }
431 }
432
433 void
434 LV2PluginUI::lv2ui_free()
435 {
436         stop_updating (0);
437
438         if (_gui_widget) {
439                 remove (*_gui_widget);
440                 _gui_widget = NULL;
441         }
442
443         if (_inst) {
444                 suil_instance_free((SuilInstance*)_inst);
445                 _inst = NULL;
446         }
447 }
448
449 LV2PluginUI::~LV2PluginUI ()
450 {
451         delete [] _values_last_sent_to_ui;
452
453         _message_update_connection.disconnect();
454         _screen_update_connection.disconnect();
455
456         if (_external_ui_ptr && _lv2->is_external_kx()) {
457                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
458         }
459         lv2ui_free();
460         _external_ui_ptr = NULL;
461 }
462
463 int
464 LV2PluginUI::get_preferred_height()
465 {
466         Gtk::Requisition r = size_request();
467         return r.height;
468 }
469
470 int
471 LV2PluginUI::get_preferred_width()
472 {
473         Gtk::Requisition r = size_request();
474         return r.width;
475 }
476
477 bool
478 LV2PluginUI::resizable()
479 {
480         return _lv2->ui_is_resizable();
481 }
482
483 int
484 LV2PluginUI::package(Gtk::Window& win)
485 {
486         if (_external_ui_ptr) {
487                 _win_ptr = &win;
488         } else {
489                 /* forward configure events to plugin window */
490                 win.signal_configure_event().connect(
491                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
492                 win.signal_map_event().connect(
493                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
494                 win.signal_unmap_event().connect(
495                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
496         }
497         return 0;
498 }
499
500 bool
501 LV2PluginUI::configure_handler(GdkEventConfigure*)
502 {
503         std::cout << "CONFIGURE" << std::endl;
504         return false;
505 }
506
507 bool
508 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
509 {
510         /* FIXME: use port notification properties
511            and/or new UI extension subscription methods
512         */
513         return true;
514 }
515
516 bool
517 LV2PluginUI::on_window_show(const std::string& title)
518 {
519         //cout << "on_window_show - " << title << endl; flush(cout);
520
521         if (_lv2->is_external_ui()) {
522                 if (_external_ui_ptr) {
523                         _screen_update_connection.disconnect();
524                         _message_update_connection.disconnect();
525                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
526                         _screen_update_connection = Timers::super_rapid_connect
527                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
528                         if (_lv2->has_message_output()) {
529                                 _message_update_connection = Timers::super_rapid_connect (
530                                         sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
531                         }
532                         return false;
533                 }
534                 lv2ui_instantiate(title);
535                 if (!_external_ui_ptr) {
536                         return false;
537                 }
538
539                 _screen_update_connection.disconnect();
540                 _message_update_connection.disconnect();
541                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
542                 _screen_update_connection = Timers::super_rapid_connect
543                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
544                 if (_lv2->has_message_output()) {
545                         _message_update_connection = Timers::super_rapid_connect (
546                                 sigc::mem_fun(*this, &LV2PluginUI::update_timeout));
547                 }
548                 return false;
549         } else {
550                 lv2ui_instantiate("gtk2gui");
551         }
552
553         return _inst ? true : false;
554 }
555
556 void
557 LV2PluginUI::on_window_hide()
558 {
559         //printf("LV2PluginUI::on_window_hide\n");
560
561         if (_lv2->is_external_ui()) {
562                 if (!_external_ui_ptr) { return; }
563                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
564                 if (!_lv2->is_external_kx()) { return ; }
565                 _message_update_connection.disconnect();
566                 _screen_update_connection.disconnect();
567                 _external_ui_ptr = NULL;
568                 suil_instance_free((SuilInstance*)_inst);
569                 _inst = NULL;
570         } else {
571                 lv2ui_free();
572         }
573 }