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