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