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