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