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