2 Copyright (C) 2008 Paul Davis
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/insert.h>
22 #include <ardour/lv2_plugin.h>
24 #include "ardour_ui.h"
25 #include "lv2_plugin_ui.h"
28 using namespace ARDOUR
;
32 LV2PluginUI::lv2_ui_write(LV2UI_Controller controller
,
38 //cout << "lv2_ui_write" << endl;
39 LV2PluginUI
* me
= (LV2PluginUI
*)controller
;
40 if (*(float*)buffer
!= me
->_values
[port_index
]) {
41 //cout << "set_parameter " << port_index << ":" << *(float*)buffer << endl;
42 me
->_lv2
->set_parameter(port_index
, *(float*)buffer
);
46 void LV2PluginUI::on_external_ui_closed(LV2UI_Controller controller
)
48 LV2PluginUI
* me
= (LV2PluginUI
*)controller
;
49 me
->_screen_update_connection
.disconnect();
50 me
->_external_ui_ptr
= NULL
;
54 LV2PluginUI::parameter_changed (uint32_t port_index
, float val
)
56 //cout << "parameter_changed" << endl;
57 if (val
!= _values
[port_index
]) {
58 parameter_update(port_index
, val
);
63 LV2PluginUI::parameter_update (uint32_t port_index
, float val
)
69 const LV2UI_Descriptor
* ui_desc
= slv2_ui_instance_get_descriptor(_inst
);
70 LV2UI_Handle ui_handle
= slv2_ui_instance_get_handle(_inst
);
71 if (ui_desc
->port_event
) {
72 //cout << "port_event " << port_index << ":" << val << endl;
73 ui_desc
->port_event(ui_handle
, port_index
, 4, 0, &val
);
75 _values
[port_index
] = val
;
79 LV2PluginUI::start_updating(GdkEventAny
* event
)
81 //cout << "start_updating" << endl;
83 if (!_output_ports
.empty()) {
84 _screen_update_connection
.disconnect();
85 _screen_update_connection
= ARDOUR_UI::instance()->RapidScreenUpdate
.connect
86 (mem_fun(*this, &LV2PluginUI::output_update
));
92 LV2PluginUI::stop_updating(GdkEventAny
* event
)
94 //cout << "stop_updating" << endl;
96 if (//!_external_ui_ptr &&
97 !_output_ports
.empty()) {
98 _screen_update_connection
.disconnect();
104 LV2PluginUI::output_update()
106 //cout << "output_update" << endl;
107 if (_external_ui_ptr
) {
108 LV2_EXTERNAL_UI_RUN(_external_ui_ptr
);
111 /* FIXME only works with control output ports (which is all we support now anyway) */
112 uint32_t nports
= _output_ports
.size();
113 for (uint32_t i
= 0; i
< nports
; ++i
) {
114 uint32_t index
= _output_ports
[i
];
115 parameter_changed(index
, _lv2
->get_parameter(index
));
120 LV2PluginUI::LV2PluginUI (boost::shared_ptr
<PluginInsert
> pi
, boost::shared_ptr
<LV2Plugin
> lv2p
)
125 , _external_ui_ptr(NULL
)
127 if (!_lv2
->is_external_ui()) {
128 lv2ui_instantiate("gtk2gui");
133 LV2PluginUI::lv2ui_instantiate(const Glib::ustring
& title
)
135 LV2_Feature
** features
;
136 LV2_Feature
** features_src
;
137 LV2_Feature
** features_dst
;
138 size_t features_count
;
141 is_external_ui
= _lv2
->is_external_ui();
143 if (is_external_ui
) {
144 _external_ui_host
.ui_closed
= LV2PluginUI::on_external_ui_closed
;
145 _external_ui_host
.plugin_human_id
= strdup(title
.c_str());
147 _external_ui_feature
.URI
= LV2_EXTERNAL_UI_URI
;
148 _external_ui_feature
.data
= &_external_ui_host
;
150 features_src
= features
= (LV2_Feature
**)_lv2
->features();
152 while (*features
++) {
156 features_dst
= features
= (LV2_Feature
**)malloc(sizeof(LV2_Feature
*) * features_count
);
157 features_dst
[--features_count
] = NULL
;
158 features_dst
[--features_count
] = &_external_ui_feature
;
159 while (features_count
--) {
160 *features
++ = *features_src
++;
163 features_dst
= (LV2_Feature
**)_lv2
->features();
166 _inst
= slv2_ui_instantiate(
167 _lv2
->slv2_plugin(), _lv2
->slv2_ui(), LV2PluginUI::lv2_ui_write
, this,
170 if (is_external_ui
) {
174 uint32_t num_ports
= slv2_plugin_get_num_ports(_lv2
->slv2_plugin());
175 for (uint32_t i
= 0; i
< num_ports
; ++i
) {
176 if (_lv2
->parameter_is_output(i
) && _lv2
->parameter_is_control(i
) && is_update_wanted(i
)) {
177 _output_ports
.push_back(i
);
181 _external_ui_ptr
= NULL
;
183 if (!is_external_ui
) {
184 GtkWidget
* c_widget
= (GtkWidget
*)slv2_ui_instance_get_widget(_inst
);
185 _gui_widget
= Glib::wrap(c_widget
);
186 _gui_widget
->show_all();
187 pack_start(*_gui_widget
, true, true);
189 _external_ui_ptr
= (struct lv2_external_ui
*)slv2_ui_instance_get_widget(_inst
);
193 _values
= new float[num_ports
];
194 for (uint32_t i
= 0; i
< num_ports
; ++i
) {
196 uint32_t port
= _lv2
->nth_parameter(i
, ok
);
198 _values
[port
] = _lv2
->get_parameter(port
);
199 if (_lv2
->parameter_is_control(port
) && _lv2
->parameter_is_input(port
)) {
200 parameter_update(port
, _values
[port
]);
205 _lv2
->ParameterChanged
.connect(mem_fun(*this, &LV2PluginUI::parameter_changed
));
208 LV2PluginUI::~LV2PluginUI ()
210 //cout << "LV2PluginUI destructor called" << endl;
217 const LV2UI_Descriptor
* ui_desc
= slv2_ui_instance_get_descriptor(_inst
);
218 LV2UI_Handle ui_handle
= slv2_ui_instance_get_handle(_inst
);
220 /*Call cleanup to tell the plugin to close its GUI and delete it*/
223 ui_desc
->cleanup(ui_handle
);
226 _screen_update_connection
.disconnect();
228 if (_lv2
->is_external_ui()) {
229 /*external UI is no longer valid - on_window_hide() will not try to use it if is NULL*/
230 _external_ui_ptr
= NULL
;
235 LV2PluginUI::get_preferred_height ()
237 Gtk::Requisition r
= size_request();
242 LV2PluginUI::get_preferred_width ()
244 Gtk::Requisition r
= size_request();
249 LV2PluginUI::package (Gtk::Window
& win
)
251 //cout << "package" << endl;
252 if (_external_ui_ptr
) {
255 /* forward configure events to plugin window */
256 win
.signal_configure_event().connect (mem_fun (*this, &LV2PluginUI::configure_handler
));
257 win
.signal_map_event().connect (mem_fun (*this, &LV2PluginUI::start_updating
));
258 win
.signal_unmap_event().connect (mem_fun (*this, &LV2PluginUI::stop_updating
));
264 LV2PluginUI::configure_handler (GdkEventConfigure
* ev
)
266 cout
<< "CONFIGURE" << endl
;
271 LV2PluginUI::is_update_wanted(uint32_t index
)
273 /* FIXME this should check the port notification properties, which nobody sets now anyway :) */
278 LV2PluginUI::on_window_show(const Glib::ustring
& title
)
280 //cout << "on_window_show - " << title << endl; flush(cout);
282 if (_lv2
->is_external_ui()) {
283 if (_external_ui_ptr
) {
284 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr
);
287 lv2ui_instantiate(title
);
288 if (!_external_ui_ptr
) {
292 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr
);
293 _screen_update_connection
.disconnect();
294 _screen_update_connection
= ARDOUR_UI::instance()->RapidScreenUpdate
.connect
295 (mem_fun(*this, &LV2PluginUI::output_update
));
303 LV2PluginUI::on_window_hide()
305 //cout << "on_window_hide" << endl; flush(cout);
307 if (_external_ui_ptr
) {
308 LV2_EXTERNAL_UI_HIDE(_external_ui_ptr
);
309 //slv2_ui_instance_get_descriptor(_inst)->cleanup(_inst);
310 //_external_ui_ptr = NULL;
311 //_screen_update_connection.disconnect();