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