2 Copyright (C) 2008-2011 Paul Davis
3 Author: David Robillard
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.
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.
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.
21 #include "ardour/lv2_plugin.h"
22 #include "ardour/plugin_manager.h"
23 #include "ardour/processor.h"
25 #include "ardour_ui.h"
26 #include "gui_thread.h"
27 #include "lv2_plugin_ui.h"
31 /* Note this file is not compiled without either Suil or SLV2 present,
32 and if Suil is present then Lilv is also present.
36 #include <lilv/lilv.h>
37 #include <suil/suil.h>
39 #include <slv2/slv2.h>
43 using namespace ARDOUR
;
46 #define NS_UI "http://lv2plug.in/ns/extensions/ui#"
49 static SuilHost
* ui_host
= NULL
;
53 LV2PluginUI::lv2_ui_write(void* controller
,
55 uint32_t /*buffer_size*/,
59 LV2PluginUI
* me
= (LV2PluginUI
*)controller
;
60 boost::shared_ptr
<AutomationControl
> ac
= me
->_controllables
[port_index
];
63 ac
->set_value(*(float*)buffer
);
68 LV2PluginUI::on_external_ui_closed(void* controller
)
70 LV2PluginUI
* me
= (LV2PluginUI
*)controller
;
71 me
->_screen_update_connection
.disconnect();
72 me
->_external_ui_ptr
= NULL
;
76 LV2PluginUI::parameter_changed(uint32_t port_index
, float val
)
78 PlugUIBase::parameter_changed(port_index
, val
);
80 if (val
!= _values
[port_index
]) {
81 parameter_update(port_index
, val
);
86 LV2PluginUI::parameter_update(uint32_t port_index
, float val
)
93 suil_instance_port_event((SuilInstance
*)_inst
, port_index
, 4, 0, &val
);
95 SLV2UIInstance inst
= (SLV2UIInstance
)_inst
;
96 const LV2UI_Descriptor
* ui_desc
= slv2_ui_instance_get_descriptor(inst
);
97 LV2UI_Handle ui_handle
= slv2_ui_instance_get_handle(inst
);
98 if (ui_desc
->port_event
) {
99 ui_desc
->port_event(ui_handle
, port_index
, 4, 0, &val
);
102 _values
[port_index
] = val
;
106 LV2PluginUI::start_updating(GdkEventAny
*)
108 if (!_output_ports
.empty()) {
109 _screen_update_connection
.disconnect();
110 _screen_update_connection
= ARDOUR_UI::instance()->RapidScreenUpdate
.connect
111 (sigc::mem_fun(*this, &LV2PluginUI::output_update
));
117 LV2PluginUI::stop_updating(GdkEventAny
*)
119 //cout << "stop_updating" << endl;
121 if (!_output_ports
.empty()) {
122 _screen_update_connection
.disconnect();
128 LV2PluginUI::output_update()
130 //cout << "output_update" << endl;
131 if (_external_ui_ptr
) {
132 LV2_EXTERNAL_UI_RUN(_external_ui_ptr
);
135 /* FIXME only works with control output ports (which is all we support now anyway) */
136 uint32_t nports
= _output_ports
.size();
137 for (uint32_t i
= 0; i
< nports
; ++i
) {
138 uint32_t index
= _output_ports
[i
];
139 parameter_changed(index
, _lv2
->get_parameter(index
));
144 LV2PluginUI::LV2PluginUI(boost::shared_ptr
<PluginInsert
> pi
,
145 boost::shared_ptr
<LV2Plugin
> lv2p
)
150 , _external_ui_ptr(NULL
)
156 LV2PluginUI::lv2ui_instantiate(const std::string
& title
)
158 LV2_Feature
** features
;
159 LV2_Feature
** features_src
;
160 LV2_Feature
** features_dst
;
161 size_t features_count
;
164 is_external_ui
= _lv2
->is_external_ui();
166 if (is_external_ui
) {
167 _external_ui_host
.ui_closed
= LV2PluginUI::on_external_ui_closed
;
168 _external_ui_host
.plugin_human_id
= strdup(title
.c_str());
170 _external_ui_feature
.URI
= LV2_EXTERNAL_UI_URI
;
171 _external_ui_feature
.data
= &_external_ui_host
;
173 features_src
= features
= (LV2_Feature
**)_lv2
->features();
175 while (*features
++) {
178 features_dst
= features
= (LV2_Feature
**)malloc(
179 sizeof(LV2_Feature
*) * features_count
);
180 features_dst
[--features_count
] = NULL
;
181 features_dst
[--features_count
] = &_external_ui_feature
;
182 while (features_count
--) {
183 *features
++ = *features_src
++;
186 features_dst
= (LV2_Feature
**)_lv2
->features();
191 ui_host
= suil_host_new(LV2PluginUI::lv2_ui_write
, NULL
, NULL
, NULL
);
193 const char* container_type
= (is_external_ui
)
197 LilvUI
* ui
= (LilvUI
*)_lv2
->c_ui();
198 _inst
= suil_instance_new(
203 lilv_node_as_uri(lilv_ui_get_uri(ui
)),
204 lilv_node_as_uri((const LilvNode
*)_lv2
->c_ui_type()),
205 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_bundle_uri(ui
))),
206 lilv_uri_to_path(lilv_node_as_uri(lilv_ui_get_binary_uri(ui
))),
209 _inst
= slv2_ui_instantiate((SLV2Plugin
)_lv2
->c_plugin(),
210 (SLV2UI
)_lv2
->c_ui(),
211 LV2PluginUI::lv2_ui_write
,
216 if (is_external_ui
) {
221 #define GET_WIDGET(inst) suil_instance_get_widget((SuilInstance*)inst);
223 #define GET_WIDGET(inst) slv2_ui_instance_get_widget((SLV2UIInstance)inst);
226 const uint32_t num_ports
= _lv2
->num_ports();
227 for (uint32_t i
= 0; i
< num_ports
; ++i
) {
228 if (_lv2
->parameter_is_output(i
)
229 && _lv2
->parameter_is_control(i
)
230 && is_update_wanted(i
)) {
231 _output_ports
.push_back(i
);
235 _external_ui_ptr
= NULL
;
237 if (!is_external_ui
) {
238 GtkWidget
* c_widget
= (GtkWidget
*)GET_WIDGET(_inst
);
239 _gui_widget
= Glib::wrap(c_widget
);
240 _gui_widget
->show_all();
241 pack_start(*_gui_widget
, true, true);
243 _external_ui_ptr
= (struct lv2_external_ui
*)GET_WIDGET(_inst
);
247 _values
= new float[num_ports
];
248 _controllables
.resize(num_ports
);
249 for (uint32_t i
= 0; i
< num_ports
; ++i
) {
251 uint32_t port
= _lv2
->nth_parameter(i
, ok
);
253 _values
[port
] = _lv2
->get_parameter(port
);
254 _controllables
[port
] = boost::dynamic_pointer_cast
<ARDOUR::AutomationControl
> (
255 insert
->control(Evoral::Parameter(PluginAutomation
, 0, port
)));
257 if (_lv2
->parameter_is_control(port
) && _lv2
->parameter_is_input(port
)) {
258 parameter_update(port
, _values
[port
]);
265 LV2PluginUI::lv2ui_free()
270 remove (*_gui_widget
);
274 suil_instance_free((SuilInstance
*)_inst
);
276 SLV2UIInstance inst
= (SLV2UIInstance
)_inst
;
278 const LV2UI_Descriptor
* ui_desc
= slv2_ui_instance_get_descriptor(inst
);
279 LV2UI_Handle ui_handle
= slv2_ui_instance_get_handle(inst
);
282 ui_desc
->cleanup(ui_handle
);
291 LV2PluginUI::~LV2PluginUI ()
297 /* Close and delete GUI. */
300 _screen_update_connection
.disconnect();
302 if (_lv2
->is_external_ui()) {
303 /* External UI is no longer valid.
304 on_window_hide() will not try to use it if is NULL.
306 _external_ui_ptr
= NULL
;
311 LV2PluginUI::get_preferred_height()
313 Gtk::Requisition r
= size_request();
318 LV2PluginUI::get_preferred_width()
320 Gtk::Requisition r
= size_request();
325 LV2PluginUI::package(Gtk::Window
& win
)
327 if (_external_ui_ptr
) {
330 /* forward configure events to plugin window */
331 win
.signal_configure_event().connect(
332 sigc::mem_fun(*this, &LV2PluginUI::configure_handler
));
333 win
.signal_map_event().connect(
334 sigc::mem_fun(*this, &LV2PluginUI::start_updating
));
335 win
.signal_unmap_event().connect(
336 sigc::mem_fun(*this, &LV2PluginUI::stop_updating
));
342 LV2PluginUI::configure_handler(GdkEventConfigure
*)
344 std::cout
<< "CONFIGURE" << std::endl
;
349 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
351 /* FIXME: use port notification properties
352 and/or new UI extension subscription methods
358 LV2PluginUI::on_window_show(const std::string
& title
)
360 //cout << "on_window_show - " << title << endl; flush(cout);
362 if (_lv2
->is_external_ui()) {
363 if (_external_ui_ptr
) {
364 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr
);
367 lv2ui_instantiate(title
);
368 if (!_external_ui_ptr
) {
372 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr
);
373 _screen_update_connection
.disconnect();
374 _screen_update_connection
= ARDOUR_UI::instance()->RapidScreenUpdate
.connect
375 (sigc::mem_fun(*this, &LV2PluginUI::output_update
));
378 lv2ui_instantiate("gtk2gui");
385 LV2PluginUI::on_window_hide()
387 //cout << "on_window_hide" << endl; flush(cout);
389 if (_external_ui_ptr
) {
390 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr
);
391 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
392 //_external_ui_ptr = NULL;
393 //_screen_update_connection.disconnect();