Show generic plugin UI toolbar (for presets etc.) on LV2 UIs (except external).
[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 <lilv/lilv.h>
32 #include <suil/suil.h>
33
34 using namespace Gtk;
35 using namespace ARDOUR;
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::lv2_ui_write(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         boost::shared_ptr<AutomationControl> ac = me->_controllables[port_index];
51
52         if (ac) {
53                 ac->set_value(*(float*)buffer);
54         }
55 }
56
57 void
58 LV2PluginUI::on_external_ui_closed(void* controller)
59 {
60         LV2PluginUI* me = (LV2PluginUI*)controller;
61         me->_screen_update_connection.disconnect();
62         me->_external_ui_ptr = NULL;
63 }
64
65 void
66 LV2PluginUI::parameter_changed(uint32_t port_index, float val)
67 {
68         PlugUIBase::parameter_changed(port_index, val);
69
70         if (val != _values[port_index]) {
71                 parameter_update(port_index, val);
72         }
73 }
74
75 void
76 LV2PluginUI::parameter_update(uint32_t port_index, float val)
77 {
78         if (!_inst) {
79                 return;
80         }
81
82         suil_instance_port_event((SuilInstance*)_inst, port_index, 4, 0, &val);
83         _values[port_index] = val;
84 }
85
86 bool
87 LV2PluginUI::start_updating(GdkEventAny*)
88 {
89         if (!_output_ports.empty()) {
90                 _screen_update_connection.disconnect();
91                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
92                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
93         }
94         return false;
95 }
96
97 bool
98 LV2PluginUI::stop_updating(GdkEventAny*)
99 {
100         //cout << "stop_updating" << endl;
101
102         if (!_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         , _gui_widget(NULL)
130         , _values(NULL)
131         , _external_ui_ptr(NULL)
132         , _inst(NULL)
133 {
134 }
135
136 void
137 LV2PluginUI::lv2ui_instantiate(const std::string& title)
138 {
139         LV2_Feature** features;
140         LV2_Feature** features_src;
141         LV2_Feature** features_dst;
142         size_t        features_count;
143         bool          is_external_ui;
144
145         is_external_ui = _lv2->is_external_ui();
146
147         if (is_external_ui) {
148                 _external_ui_host.ui_closed       = LV2PluginUI::on_external_ui_closed;
149                 _external_ui_host.plugin_human_id = strdup(title.c_str());
150
151                 _external_ui_feature.URI  = LV2_EXTERNAL_UI_URI;
152                 _external_ui_feature.data = &_external_ui_host;
153
154                 features_src = features = (LV2_Feature**)_lv2->features();
155                 features_count = 2;
156                 while (*features++) {
157                         features_count++;
158                 }
159                 features_dst = features = (LV2_Feature**)malloc(
160                         sizeof(LV2_Feature*) * features_count);
161                 features_dst[--features_count] = NULL;
162                 features_dst[--features_count] = &_external_ui_feature;
163                 while (features_count--) {
164                         *features++ = *features_src++;
165                 }
166         } else {
167                 features_dst = (LV2_Feature**)_lv2->features();
168         }
169
170         if (!ui_host) {
171                 ui_host = suil_host_new(LV2PluginUI::lv2_ui_write, NULL, NULL, NULL);
172         }
173         const char* container_type = (is_external_ui)
174                 ? NS_UI "external"
175                 : NS_UI "GtkUI";
176
177         LilvUI* ui = (LilvUI*)_lv2->c_ui();
178         _inst = suil_instance_new(
179                 ui_host,
180                 this,
181                 container_type,
182                 _lv2->uri(),
183                 lilv_node_as_uri(lilv_ui_get_uri(ui)),
184                 lilv_node_as_uri((const LilvNode*)_lv2->c_ui_type()),
185                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui))),
186                 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui))),
187                 features_dst);
188
189         if (is_external_ui) {
190                 free(features_dst);
191         }
192
193 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
194
195         const uint32_t num_ports = _lv2->num_ports();
196         for (uint32_t i = 0; i < num_ports; ++i) {
197                 if (_lv2->parameter_is_output(i)
198                     && _lv2->parameter_is_control(i)
199                     && is_update_wanted(i)) {
200                         _output_ports.push_back(i);
201                 }
202         }
203
204         _external_ui_ptr = NULL;
205         if (_inst) {
206                 if (!is_external_ui) {
207                         Gtk::HBox* box = manage (new Gtk::HBox);
208                         box->set_spacing (6);
209                         box->set_border_width (6);
210                         box->pack_end (focus_button, false, false);
211                         box->pack_end (bypass_button, false, false, 10);
212                         box->pack_end (delete_button, false, false);
213                         box->pack_end (save_button, false, false);
214                         box->pack_end (add_button, false, false);
215                         box->pack_end (_preset_combo, false, false);
216                         box->show_all();
217                         pack_start(*box, false, false);
218
219                         GtkWidget* c_widget = (GtkWidget*)GET_WIDGET(_inst);
220                         _gui_widget = Glib::wrap(c_widget);
221                         _gui_widget->show_all();
222                         pack_start(*_gui_widget, true, true);
223                 } else {
224                         _external_ui_ptr = (struct lv2_external_ui*)GET_WIDGET(_inst);
225                 }
226         }
227
228         _values = new float[num_ports];
229         _controllables.resize(num_ports);
230         for (uint32_t i = 0; i < num_ports; ++i) {
231                 bool     ok;
232                 uint32_t port = _lv2->nth_parameter(i, ok);
233                 if (ok) {
234                         _values[port]        = _lv2->get_parameter(port);
235                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
236                                 insert->control(Evoral::Parameter(PluginAutomation, 0, port)));
237
238                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
239                                 parameter_update(port, _values[port]);
240                         }
241                 }
242         }
243 }
244
245 void
246 LV2PluginUI::lv2ui_free()
247 {
248         stop_updating (0);
249
250         if (_gui_widget) {
251                 remove (*_gui_widget);
252         }
253
254         suil_instance_free((SuilInstance*)_inst);
255
256         _inst = NULL;
257         _gui_widget = NULL;
258 }
259
260 LV2PluginUI::~LV2PluginUI ()
261 {
262         if (_values) {
263                 delete[] _values;
264         }
265
266         /* Close and delete GUI. */
267         lv2ui_free();
268
269         _screen_update_connection.disconnect();
270
271         if (_lv2->is_external_ui()) {
272                 /* External UI is no longer valid.
273                    on_window_hide() will not try to use it if is NULL.
274                 */
275                 _external_ui_ptr = NULL;
276         }
277 }
278
279 int
280 LV2PluginUI::get_preferred_height()
281 {
282         Gtk::Requisition r = size_request();
283         return r.height;
284 }
285
286 int
287 LV2PluginUI::get_preferred_width()
288 {
289         Gtk::Requisition r = size_request();
290         return r.width;
291 }
292
293 int
294 LV2PluginUI::package(Gtk::Window& win)
295 {
296         if (_external_ui_ptr) {
297                 _win_ptr = &win;
298         } else {
299                 /* forward configure events to plugin window */
300                 win.signal_configure_event().connect(
301                         sigc::mem_fun(*this, &LV2PluginUI::configure_handler));
302                 win.signal_map_event().connect(
303                         sigc::mem_fun(*this, &LV2PluginUI::start_updating));
304                 win.signal_unmap_event().connect(
305                         sigc::mem_fun(*this, &LV2PluginUI::stop_updating));
306         }
307         return 0;
308 }
309
310 bool
311 LV2PluginUI::configure_handler(GdkEventConfigure*)
312 {
313         std::cout << "CONFIGURE" << std::endl;
314         return false;
315 }
316
317 bool
318 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
319 {
320         /* FIXME: use port notification properties
321            and/or new UI extension subscription methods
322         */
323         return true;
324 }
325
326 bool
327 LV2PluginUI::on_window_show(const std::string& title)
328 {
329         //cout << "on_window_show - " << title << endl; flush(cout);
330
331         if (_lv2->is_external_ui()) {
332                 if (_external_ui_ptr) {
333                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
334                         return false;
335                 }
336                 lv2ui_instantiate(title);
337                 if (!_external_ui_ptr) {
338                         return false;
339                 }
340
341                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
342                 _screen_update_connection.disconnect();
343                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
344                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
345                 return false;
346         } else {
347                 lv2ui_instantiate("gtk2gui");
348         }
349
350         return true;
351 }
352
353 void
354 LV2PluginUI::on_window_hide()
355 {
356         //cout << "on_window_hide" << endl; flush(cout);
357
358         if (_external_ui_ptr) {
359                 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr);
360                 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
361                 //_external_ui_ptr = NULL;
362                 //_screen_update_connection.disconnect();
363         } else {
364                 lv2ui_free();
365         }
366 }