Make LV2World local to lv2_plugin.cc
[ardour.git] / gtk2_ardour / lv2_plugin_ui.cc
1 /*
2     Copyright (C) 2008-2011 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
21 #include "ardour/lv2_plugin.h"
22 #include "ardour/plugin_manager.h"
23 #include "ardour/processor.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 using namespace Gtk;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
36
37 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
38 SuilHost* LV2PluginUI::ui_host = NULL;
39 #endif
40
41 void
42 LV2PluginUI::lv2_ui_write(void*       controller,
43                           uint32_t    port_index,
44                           uint32_t    /*buffer_size*/,
45                           uint32_t    /*format*/,
46                           const void* buffer)
47 {
48         LV2PluginUI* me = (LV2PluginUI*)controller;
49         boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
50
51         if (ac) {
52                 ac->set_value(*(float*)buffer);
53         }
54 }
55
56 void
57 LV2PluginUI::on_external_ui_closed(void* controller)
58 {
59         LV2PluginUI* me = (LV2PluginUI*)controller;
60         me->_screen_update_connection.disconnect();
61         me->_external_ui_ptr = NULL;
62 }
63
64 void
65 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
66 {
67         PlugUIBase::parameter_changed(port_index, val);
68
69         if (val != _values[port_index]) {
70                 parameter_update(port_index, val);
71         }
72 }
73
74 void
75 LV2PluginUI::parameter_update(uint32_t port_index, float val)
76 {
77         if (!_inst) {
78                 return;
79         }
80
81 #ifdef HAVE_SUIL
82         suil_instance_port_event(_inst, port_index, 4, 0, &val);
83 #else
84         const LV2UI_Descriptor* ui_desc   = slv2_ui_instance_get_descriptor(_inst);
85         LV2UI_Handle            ui_handle = slv2_ui_instance_get_handle(_inst);
86         if (ui_desc->port_event) {
87                 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
88         }
89 #endif
90         _values[port_index] = val;
91 }
92
93 bool
94 LV2PluginUI::start_updating(GdkEventAny*)
95 {
96         if (!_output_ports.empty()) {
97                 _screen_update_connection.disconnect();
98                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
99                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
100         }
101         return false;
102 }
103
104 bool
105 LV2PluginUI::stop_updating(GdkEventAny*)
106 {
107         //cout << "stop_updating" << endl;
108
109         if ( //!_external_ui_ptr &&
110             !_output_ports.empty()) {
111                 _screen_update_connection.disconnect();
112         }
113         return false;
114 }
115
116 void
117 LV2PluginUI::output_update()
118 {
119         //cout << "output_update" << endl;
120         if (_external_ui_ptr) {
121                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
122         }
123
124         /* FIXME only works with control output ports (which is all we support now anyway) */
125         uint32_t nports = _output_ports.size();
126         for (uint32_t i = 0; i < nports; ++i) {
127                 uint32_t index = _output_ports[i];
128                 parameter_changed(index, _lv2->get_parameter(index));
129         }
130
131 }
132
133 LV2PluginUI::LV2PluginUI(boost::shared_ptr<PluginInsert> pi,
134                          boost::shared_ptr<LV2Plugin>    lv2p)
135         : PlugUIBase(pi)
136         , _lv2(lv2p)
137         , _values(NULL)
138         , _external_ui_ptr(NULL)
139         , _inst(NULL)
140 {
141         if (!_lv2->is_external_ui()) {
142                 lv2ui_instantiate("gtk2gui");
143         }
144 }
145
146 void
147 LV2PluginUI::lv2ui_instantiate(const std::string& title)
148 {
149         LV2_Feature** features;
150         LV2_Feature** features_src;
151         LV2_Feature** features_dst;
152         size_t        features_count;
153         bool          is_external_ui;
154
155         is_external_ui = _lv2->is_external_ui();
156
157         if (is_external_ui) {
158                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
159                 _external_ui_host.plugin_human_id = strdup(title.c_str());
160
161                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
162                 _external_ui_feature.data = &_external_ui_host;
163
164                 features_src = features = (LV2_Feature**)_lv2->features();
165                 features_count = 2;
166                 while (*features++) {
167                         features_count++;
168                 }
169                 features_dst = features = (LV2_Feature**)malloc(
170                         sizeof(LV2_Feature*) * features_count);
171                 features_dst[--features_count] = NULL;
172                 features_dst[--features_count] = &_external_ui_feature;
173                 while (features_count--) {
174                         *features++ = *features_src++;
175                 }
176         } else {
177                 features_dst = (LV2_Feature**)_lv2->features();
178         }
179
180 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
181         if (!LV2PluginUI::ui_host) {
182                 LV2PluginUI::ui_host = suil_host_new(
183                 LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
184         }
185         const char* container_type = (is_external_ui)
186                 ? NS_UI "external"
187                 : NS_UI "GtkUI";
188
189         SLV2UI ui = _lv2->slv2_ui();
190         _inst = suil_instance_new(
191                 LV2PluginUI::ui_host,
192                 this,
193                 container_type,
194                 slv2_value_as_uri(slv2_plugin_get_uri(_lv2->slv2_plugin())),
195                 slv2_value_as_uri(slv2_ui_get_uri(ui)),
196                 slv2_value_as_uri(_lv2->ui_type()),
197                 slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_bundle_uri(ui))),
198                 slv2_uri_to_path(slv2_value_as_uri(slv2_ui_get_binary_uri(ui))),
199                 features_dst);
200 #else
201         _inst = slv2_ui_instantiate(
202                 _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
203                 features_dst);
204 #endif
205
206         if (is_external_ui) {
207                 free(features_dst);
208         }
209
210 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
211 #define GET_WIDGET(inst) suil_instance_get_widget(inst);
212 #else
213 #define GET_WIDGET(inst) slv2_ui_instance_get_widget(inst);
214 #endif
215
216         uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
217         for (uint32_t i = 0; i < num_ports; ++i) {
218                 if (_lv2->parameter_is_output(i)
219                     && _lv2->parameter_is_control(i)
220                     && is_update_wanted(i)) {
221                         _output_ports.push_back(i);
222                 }
223         }
224
225         _external_ui_ptr = NULL;
226         if (_inst) {
227                 if (!is_external_ui) {
228                         GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
229                         _gui_widget = Glib::wrap(c_widget);
230                         _gui_widget->show_all();
231                         pack_start(*_gui_widget, true, true);
232                 } else {
233                         _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
234                 }
235         }
236
237         _values = new float[num_ports];
238         _controllables.resize(num_ports);
239         for (uint32_t i = 0; i < num_ports; ++i) {
240                 bool     ok;
241                 uint32_t port = _lv2->nth_parameter(i, ok);
242                 if (ok) {
243                         _values[port]        = _lv2->get_parameter(port);
244                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
245                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
246
247                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
248                                 parameter_update(port, _values[port]);
249                         }
250                 }
251         }
252 }
253
254 LV2PluginUI::~LV2PluginUI ()
255 {
256         //cout << "LV2PluginUI destructor called" << endl;
257
258         if (_values) {
259                 delete[] _values;
260         }
261
262         /* Close and delete GUI. */
263 #if defined(HAVE_NEW_SLV2) && defined(HAVE_SUIL)
264         suil_instance_free(_inst);
265 #else
266         const LV2UI_Descriptor* ui_desc   = slv2_ui_instance_get_descriptor(_inst);
267         LV2UI_Handle            ui_handle = slv2_ui_instance_get_handle(_inst);
268
269         if (ui_desc) {
270                 ui_desc->cleanup(ui_handle);
271         }
272 #endif
273
274         _screen_update_connection.disconnect();
275
276         if (_lv2->is_external_ui()) {
277                 /* External UI is no longer valid.
278                    on_window_hide() will not try to use it if is NULL.
279                 */
280                 _external_ui_ptr = NULL;
281         }
282 }
283
284 int
285 LV2PluginUI::get_preferred_height()
286 {
287         Gtk::Requisition r = size_request();
288         return r.height;
289 }
290
291 int
292 LV2PluginUI::get_preferred_width()
293 {
294         Gtk::Requisition r = size_request();
295         return r.width;
296 }
297
298 int
299 LV2PluginUI::package(Gtk::Window& win)
300 {
301         if (_external_ui_ptr) {
302                 _win_ptr = &win;
303         } else {
304                 /* forward configure events to plugin window */
305                 win.signal_configure_event().connect(
306                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
307                 win.signal_map_event().connect(
308                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
309                 win.signal_unmap_event().connect(
310                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
311         }
312         return 0;
313 }
314
315 bool
316 LV2PluginUI::configure_handler(GdkEventConfigure*)
317 {
318         std::cout << "CONFIGURE" << std::endl;
319         return false;
320 }
321
322 bool
323 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
324 {
325         /* FIXME: use port notification properties
326            and/or new UI extension subscription methods
327         */
328         return true;
329 }
330
331 bool
332 LV2PluginUI::on_window_show(const std::string& title)
333 {
334         //cout << "on_window_show - " << title << endl; flush(cout);
335
336         if (_lv2->is_external_ui()) {
337                 if (_external_ui_ptr) {
338                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
339                         return false;
340                 }
341                 lv2ui_instantiate(title);
342                 if (!_external_ui_ptr) {
343                         return false;
344                 }
345
346                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
347                 _screen_update_connection.disconnect();
348                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
349                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
350                 return false;
351         }
352
353         return true;
354 }
355
356 void
357 LV2PluginUI::on_window_hide()
358 {
359         //cout << "on_window_hide" << endl; flush(cout);
360
361         if (_external_ui_ptr) {
362                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
363                 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
364                 //_external_ui_ptr = NULL;
365                 //_screen_update_connection.disconnect();
366         }
367 }