Changes missing from previous commit
[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 "lv2_plugin_ui.h"
26
27 using namespace Gtk;
28 using namespace ARDOUR;
29 using namespace PBD;
30
31 void
32 LV2PluginUI::lv2_ui_write(
33                 LV2UI_Controller controller,
34                 uint32_t         port_index,
35                 uint32_t         buffer_size,
36                 uint32_t         format,
37                 const void*      buffer)
38 {
39         LV2PluginUI* me = (LV2PluginUI*)controller;
40         if (*(float*)buffer != me->_values[port_index])
41                 me->_lv2->set_parameter(port_index, *(float*)buffer);
42 }
43
44 void
45 LV2PluginUI::parameter_changed (uint32_t port_index, float val)
46 {
47         if (val != _values[port_index]) {
48                 parameter_update(port_index, val);
49         }
50 }
51
52 void
53 LV2PluginUI::parameter_update (uint32_t port_index, float val)
54 {
55         const LV2UI_Descriptor* ui_desc = slv2_ui_instance_get_descriptor(_inst);
56         LV2UI_Handle ui_handle = slv2_ui_instance_get_handle(_inst);
57         if (ui_desc->port_event)
58                 ui_desc->port_event(ui_handle, port_index, 4, 0, &val);
59         _values[port_index] = val;
60 }
61
62 bool
63 LV2PluginUI::start_updating(GdkEventAny* event)
64 {
65         if (!_output_ports.empty()) {
66                 _screen_update_connection.disconnect();
67                 _screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
68                         (mem_fun(*this, &LV2PluginUI::output_update));
69         }
70         return false;
71 }
72
73 bool
74 LV2PluginUI::stop_updating(GdkEventAny* event)
75 {
76         if (!_output_ports.empty()) {
77                 _screen_update_connection.disconnect();
78         }
79         return false;
80 }
81
82 void
83 LV2PluginUI::output_update()
84 {
85         /* FIXME only works with control output ports (which is all we support now anyway) */
86         uint32_t nports = _output_ports.size();
87         for (uint32_t i = 0; i < nports; ++i) {
88                 uint32_t index = _output_ports[i];
89                 parameter_changed(index, _lv2->get_parameter(index));
90         }
91         
92 }
93
94 LV2PluginUI::LV2PluginUI (boost::shared_ptr<PluginInsert> pi, boost::shared_ptr<LV2Plugin> lv2p)
95         : PlugUIBase (pi)
96         , _lv2(lv2p)
97 {
98         _inst = slv2_ui_instantiate(
99                         _lv2->slv2_plugin(), _lv2->slv2_ui(), LV2PluginUI::lv2_ui_write, this,
100                         _lv2->features());
101                         
102         uint32_t num_ports = slv2_plugin_get_num_ports(lv2p->slv2_plugin());
103         for (uint32_t i = 0; i < num_ports; ++i) {
104                 if (lv2p->parameter_is_output(i) && lv2p->parameter_is_control(i) && is_update_wanted(i)) {
105                         _output_ports.push_back(i);
106                 }
107         }
108         
109         GtkWidget* c_widget = (GtkWidget*)slv2_ui_instance_get_widget(_inst);
110         _gui_widget = Glib::wrap(c_widget);
111         _gui_widget->show_all();
112         pack_start(*_gui_widget, true, true);
113         
114         _values = new float[num_ports];
115         for (uint32_t i = 0; i < num_ports; ++i) {
116                 bool ok;
117                 uint32_t port = lv2p->nth_parameter(i, ok);
118                 if (ok) {
119                         _values[port] = lv2p->get_parameter(port);
120                         if (lv2p->parameter_is_control(port) && lv2p->parameter_is_input(port)) {
121                                 parameter_update(port, _values[port]);
122                         }
123                 }
124         }
125                 
126         _lv2->ParameterChanged.connect(mem_fun(*this, &LV2PluginUI::parameter_changed));
127 }
128
129 LV2PluginUI::~LV2PluginUI ()
130 {
131         delete[] _values;
132         // plugin destructor destroys the GUI
133 }
134
135 int
136 LV2PluginUI::get_preferred_height ()
137 {
138         Gtk::Requisition r = size_request();
139         return r.height;
140 }
141
142 int
143 LV2PluginUI::get_preferred_width ()
144 {
145         Gtk::Requisition r = size_request();
146         return r.width;
147 }
148
149 int
150 LV2PluginUI::package (Gtk::Window& win)
151 {
152         /* forward configure events to plugin window */
153         win.signal_configure_event().connect (mem_fun (*this, &LV2PluginUI::configure_handler));
154         win.signal_map_event().connect (mem_fun (*this, &LV2PluginUI::start_updating));
155         win.signal_unmap_event().connect (mem_fun (*this, &LV2PluginUI::stop_updating));
156         return 0;
157 }
158
159 bool
160 LV2PluginUI::configure_handler (GdkEventConfigure* ev)
161 {
162         std::cout << "CONFIGURE" << std::endl;
163         return false;
164 }
165
166 bool
167 LV2PluginUI::is_update_wanted(uint32_t index)
168 {
169         /* FIXME this should check the port notification properties, which nobody sets now anyway :) */
170         return true;
171 }