Call PluginControl when a LV2 GUI control changes, rather than calling the plugin...
[ardour.git] / gtk2_ardour / lv2_plugin_ui.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Dave 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/processor.h"
22 #include "ardour/lv2_plugin.h"
23
24 #include "ardour_ui.h"
25 #include "gui_thread.h"
26 #include "lv2_plugin_ui.h"
27
28 using namespace Gtk;
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 void
33 LV2PluginUI::lv2_ui_write(
34                 LV2UI_Controller controller,
35                 uint32_t         port_index,
36                 uint32_t         /*buffer_size*/,
37                 uint32_t         /*format*/,
38                 const void*      buffer)
39 {
40         //cout << "lv2_ui_write" << endl;
41         LV2PluginUI* me = (LV2PluginUI *) controller;
42         me->_controllables[port_index]->set_value (*(float *) buffer);
43 }
44
45 void LV2PluginUI::on_external_ui_closed(LV2UI_Controller controller)
46 {
47         LV2PluginUI* me = (LV2PluginUI*)controller;
48         me->_screen_update_connection.disconnect();
49         me->_external_ui_ptr = NULL;
50 }
51
52 void
53 LV2PluginUI::parameter_changed (uint32_t port_index, float val)
54 {
55         PlugUIBase::parameter_changed (port_index, val);
56         
57         if (val != _values[port_index]) {
58                 parameter_update(port_index, val);
59         }
60 }
61
62 void
63 LV2PluginUI::parameter_update (uint32_t port_index, float val)
64 {
65         if (!_inst) {
66                 return;
67         }
68
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                 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
73         _values[port_index] = val;
74 }
75
76 bool
77 LV2PluginUI::start_updating(GdkEventAny*)
78 {
79         if (!_output_ports.empty()) {
80                 _screen_update_connection.disconnect();
81                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
82                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
83         }
84         return false;
85 }
86
87 bool
88 LV2PluginUI::stop_updating(GdkEventAny*)
89 {
90         //cout << "stop_updating" << endl;
91
92         if (//!_external_ui_ptr &&
93                         !_output_ports.empty()) {
94                 _screen_update_connection.disconnect();
95         }
96         return false;
97 }
98
99 void
100 LV2PluginUI::output_update()
101 {
102         //cout << "output_update" << endl;
103         if (_external_ui_ptr) {
104                 LV2_EXTERNAL_UI_RUN(_external_ui_ptr);
105         }
106
107         /* FIXME only works with control output ports (which is all we support now anyway) */
108         uint32_t nports = _output_ports.size();
109         for (uint32_t i = 0; i < nports; ++i) {
110                 uint32_t index = _output_ports[i];
111                 parameter_changed(index, _lv2->get_parameter(index));
112         }
113
114 }
115
116 LV2PluginUI::LV2PluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<LV2Plugin> lv2p)
117         : PlugUIBase (pi)
118         , _lv2(lv2p)
119         , _inst(NULL)
120         , _values(NULL)
121         , _external_ui_ptr(NULL)
122 {
123         if (!_lv2->is_external_ui()) {
124                 lv2ui_instantiate("gtk2gui");
125         }
126 }
127
128 void
129 LV2PluginUI::lv2ui_instantiate(const std::string& title)
130 {
131         LV2_Feature** features;
132         LV2_Feature** features_src;
133         LV2_Feature** features_dst;
134         size_t features_count;
135         bool is_external_ui;
136
137         is_external_ui = _lv2->is_external_ui();
138
139         if (is_external_ui) {
140                 _external_ui_host.ui_closed = LV2PluginUI::on_external_ui_closed;
141                 _external_ui_host.plugin_human_id = strdup(title.c_str());
142
143                 _external_ui_feature.URI = LV2_EXTERNAL_UI_URI;
144                 _external_ui_feature.data = &_external_ui_host;
145
146                 features_src = features = (LV2_Feature**)_lv2->features();
147                 features_count = 2;
148                 while (*features++) {
149                         features_count++;
150                 }
151
152                 features_dst = features = (LV2_Feature**)malloc(sizeof(LV2_Feature*) * features_count);
153                 features_dst[--features_count] = NULL;
154                 features_dst[--features_count] = &_external_ui_feature;
155                 while (features_count--) {
156                         *features++ = *features_src++;
157                 }
158         } else {
159                 features_dst = (LV2_Feature**)_lv2->features();
160         }
161
162         _inst = slv2_ui_instantiate(
163                         _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
164                         features_dst);
165
166         if (is_external_ui) {
167                 free(features_dst);
168         }
169
170         uint32_t num_ports = slv2_plugin_get_num_ports(_lv2->slv2_plugin());
171         for (uint32_t i = 0; i < num_ports; ++i) {
172                 if (_lv2->parameter_is_output(i) && _lv2->parameter_is_control(i) && is_update_wanted(i)) {
173                         _output_ports.push_back(i);
174                 }
175         }
176
177         _external_ui_ptr = NULL;
178         if (_inst) {
179                 if (!is_external_ui) {
180                         GtkWidget* c_widget = (GtkWidget*)slv2_ui_instance_get_widget(_inst);
181                         _gui_widget = Glib::wrap(c_widget);
182                         _gui_widget->show_all();
183                         pack_start(*_gui_widget, true, true);
184                 } else {
185                         _external_ui_ptr = (struct lv2_external_ui *)slv2_ui_instance_get_widget(_inst);
186                 }
187         }
188
189         _values = new float[num_ports];
190         _controllables.resize (num_ports);
191         for (uint32_t i = 0; i < num_ports; ++i) {
192                 bool ok;
193                 uint32_t port = _lv2->nth_parameter(i, ok);
194                 if (ok) {
195                         _values[port] = _lv2->get_parameter(port);
196                         _controllables[port] = boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (
197                                 insert->control (Evoral::Parameter (PluginAutomation, 0, port))
198                                 );
199                         
200                         if (_lv2->parameter_is_control(port) && _lv2->parameter_is_input(port)) {
201                                 parameter_update(port, _values[port]);
202                         }
203                 }
204         }
205 }
206
207 LV2PluginUI::~LV2PluginUI ()
208 {
209         //cout << "LV2PluginUI destructor called" << endl;
210
211         if (_values) {
212                 delete[] _values;
213         }
214         // plugin destructor destroys the GTK GUI
215         
216         
217         const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
218         LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
219                 
220         /*Call cleanup to tell the plugin to close its GUI and delete it*/
221                 
222         if (ui_desc) {
223                 ui_desc->cleanup(ui_handle);
224         }
225                 
226         _screen_update_connection.disconnect();         
227
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;
231         }
232 }
233
234 int
235 LV2PluginUI::get_preferred_height ()
236 {
237         Gtk::Requisition r = size_request();
238         return r.height;
239 }
240
241 int
242 LV2PluginUI::get_preferred_width ()
243 {
244         Gtk::Requisition r = size_request();
245         return r.width;
246 }
247
248 int
249 LV2PluginUI::package (Gtk::Window& win)
250 {
251         //cout << "package" << endl;
252         if (_external_ui_ptr) {
253                 _win_ptr = &win;
254         } else {
255                 /* forward configure events to plugin window */
256                 win.signal_configure_event().connect (sigc::mem_fun (*this, &LV2PluginUI::configure_handler));
257                 win.signal_map_event().connect (sigc::mem_fun (*this, &LV2PluginUI::start_updating));
258                 win.signal_unmap_event().connect (sigc::mem_fun (*this, &LV2PluginUI::stop_updating));
259         }
260         return 0;
261 }
262
263 bool
264 LV2PluginUI::configure_handler (GdkEventConfigure*)
265 {
266         std::cout << "CONFIGURE" << std::endl;
267         return false;
268 }
269
270 bool
271 LV2PluginUI::is_update_wanted(uint32_t /*index*/)
272 {
273         /* FIXME this should check the port notification properties, which nobody sets now anyway :) */
274         return true;
275 }
276
277 bool
278 LV2PluginUI::on_window_show(const std::string& title)
279 {
280         //cout << "on_window_show - " << title << endl; flush(cout);
281
282         if (_lv2->is_external_ui()) {
283                 if (_external_ui_ptr) {
284                         LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
285                         return false;
286                 }
287                 lv2ui_instantiate(title);
288                 if (!_external_ui_ptr) {
289                         return false;
290                 }
291
292                 LV2_EXTERNAL_UI_SHOW(_external_ui_ptr);
293                 _screen_update_connection.disconnect();
294                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect
295                         (sigc::mem_fun(*this, &LV2PluginUI::output_update));
296                 return false;
297         }
298
299         return true;
300 }
301
302 void
303 LV2PluginUI::on_window_hide()
304 {
305         //cout << "on_window_hide" << endl; flush(cout);
306
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();
312         }
313 }