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