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