exec bits, desktop icon props, noexecstack, attach transitions to selected edits...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / pluginlv2config.C
1 #ifdef HAVE_LV2
2
3 /*
4  * CINELERRA
5  * Copyright (C) 2018 GG
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 #include "arraylist.h"
24 #include "cstrdup.h"
25 #include "language.h"
26 #include "pluginlv2.h"
27 #include "pluginlv2config.h"
28
29 #include <ctype.h>
30 #include <string.h>
31
32 PluginLV2UriTable::PluginLV2UriTable()
33  : Mutex("PluginLV2UriTable::PluginLV2UriTable")
34 {
35         set_array_delete();
36 }
37
38 PluginLV2UriTable::~PluginLV2UriTable()
39 {
40         remove_all_objects();
41 }
42
43 LV2_URID PluginLV2UriTable::map(const char *uri)
44 {
45         lock("PluginLV2UriTable::map");
46         int i = 0, n = size();
47         while( i<n && strcmp(uri, get(i)) ) ++i;
48         if( i >= n ) append(cstrdup(uri));
49         unlock();
50         return i+1;
51 }
52
53 const char *PluginLV2UriTable::unmap(LV2_URID urid)
54 {
55         lock("PluginLV2UriTable::unmap");
56         int idx = urid - 1;
57         const char *ret = idx>=0 && idx<size() ? get(idx) : 0;
58         unlock();
59         return ret;
60 }
61
62 PluginLV2Client_OptName:: PluginLV2Client_OptName(PluginLV2Client_Opt *opt)
63 {
64         this->opt = opt;
65         set_text(opt->get_name());
66 }
67
68 PluginLV2Client_OptValue::PluginLV2Client_OptValue(PluginLV2Client_Opt *opt)
69 {
70         this->opt = opt;
71         update();
72 }
73
74 int PluginLV2Client_OptValue::update()
75 {
76         char val[BCSTRLEN];
77         sprintf(val, "%f", opt->get_value());
78         if( !strcmp(val, get_text()) ) return 0;
79         set_text(val);
80         return 1;
81 }
82
83
84 PluginLV2Client_Opt::PluginLV2Client_Opt(PluginLV2ClientConfig *conf, int idx)
85 {
86         this->conf = conf;
87         this->idx = idx;
88         item_name = new PluginLV2Client_OptName(this);
89         item_value = new PluginLV2Client_OptValue(this);
90 }
91
92 PluginLV2Client_Opt::~PluginLV2Client_Opt()
93 {
94         delete item_name;
95         delete item_value;
96 }
97
98 float PluginLV2Client_Opt::get_value()
99 {
100         return conf->ctls[idx];
101 }
102
103 const char *PluginLV2Client_Opt::get_symbol()
104 {
105         return conf->syms[idx];
106 }
107
108 void PluginLV2Client_Opt::set_value(float v)
109 {
110         conf->ctls[idx] = v;
111 }
112
113 int PluginLV2Client_Opt::update(float v)
114 {
115         set_value(v);
116         return item_value->update();
117 }
118
119 const char *PluginLV2Client_Opt::get_name()
120 {
121         return conf->names[idx];
122 }
123
124 PluginLV2ClientConfig::PluginLV2ClientConfig()
125 {
126         names = 0;
127         syms = 0;
128         mins = 0;
129         maxs = 0;
130         ctls = 0;
131         ports = 0;
132         nb_ports = 0;
133 }
134
135 PluginLV2ClientConfig::~PluginLV2ClientConfig()
136 {
137         reset();
138         remove_all_objects();
139 }
140
141 void PluginLV2ClientConfig::reset()
142 {
143         for( int i=0; i<nb_ports; ++i ) {
144                 delete [] names[i];
145                 delete [] syms[i];
146         }
147         delete [] names;  names = 0;
148         delete [] syms;   syms = 0;
149         delete [] mins;   mins = 0;
150         delete [] maxs;   maxs = 0;
151         delete [] ctls;   ctls = 0;
152         delete [] ports;  ports = 0;
153         nb_ports = 0;
154 }
155
156
157 int PluginLV2ClientConfig::equivalent(PluginLV2ClientConfig &that)
158 {
159         PluginLV2ClientConfig &conf = *this;
160         for( int i=0; i<that.size(); ++i ) {
161                 PluginLV2Client_Opt *topt = conf[i], *vopt = that[i];
162                 if( !EQUIV(topt->get_value(), vopt->get_value()) ) return 0;
163         }
164         return 1;
165 }
166
167 void PluginLV2ClientConfig::copy_from(PluginLV2ClientConfig &that)
168 {
169         if( nb_ports != that.nb_ports ) {
170                 reset();
171                 nb_ports = that.nb_ports;
172                 names = new const char *[nb_ports];
173                 syms = new const char *[nb_ports];
174                 for( int i=0; i<nb_ports; ++i ) names[i] = syms[i] = 0;
175                 mins  = new float[nb_ports];
176                 maxs  = new float[nb_ports];
177                 ctls  = new float[nb_ports];
178                 ports = new int[nb_ports];
179         }
180         for( int i=0; i<nb_ports; ++i ) {
181                 delete [] names[i];  names[i] = cstrdup(that.names[i]);
182                 delete [] syms[i];   syms[i] = cstrdup(that.syms[i]);
183                 mins[i] = that.mins[i];
184                 maxs[i] = that.maxs[i];
185                 ctls[i] = that.ctls[i];
186                 ports[i] = ports[i];
187         }
188         remove_all_objects();
189         for( int i=0; i<that.size(); ++i ) {
190                 append(new PluginLV2Client_Opt(this, that[i]->idx));
191         }
192 }
193
194 void PluginLV2ClientConfig::interpolate(PluginLV2ClientConfig &prev, PluginLV2ClientConfig &next,
195         int64_t prev_frame, int64_t next_frame, int64_t current_frame)
196 {
197         copy_from(prev);
198 }
199
200 void PluginLV2ClientConfig::init_lv2(const LilvPlugin *lilv, PluginLV2 *lv2)
201 {
202         reset();
203         nb_ports = lilv_plugin_get_num_ports(lilv);
204         names = new const char *[nb_ports];
205         syms = new const char *[nb_ports];
206         mins  = new float[nb_ports];
207         maxs  = new float[nb_ports];
208         ctls  = new float[nb_ports];
209         ports = new int[nb_ports];
210         lilv_plugin_get_port_ranges_float(lilv, mins, maxs, ctls);
211         for( int i=0; i<nb_ports; ++i ) {
212                 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
213                 LilvNode *pnm = lilv_port_get_name(lilv, lp);
214                 names[i] = cstrdup(lilv_node_as_string(pnm));
215                 lilv_node_free(pnm);
216                 const LilvNode *sym = lilv_port_get_symbol(lilv, lp);
217                 syms[i] = cstrdup(lilv_node_as_string(sym));
218                 int port = 0;
219                 if( lilv_port_is_a(lilv, lp, lv2->lv2_AudioPort) )   port |= PORTS_AUDIO;
220                 if( lilv_port_is_a(lilv, lp, lv2->lv2_ControlPort) ) port |= PORTS_CONTROL;
221                 if( lilv_port_is_a(lilv, lp, lv2->lv2_InputPort) )   port |= PORTS_INPUT;
222                 if( lilv_port_is_a(lilv, lp, lv2->lv2_OutputPort) )  port |= PORTS_OUTPUT;
223                 if( lilv_port_is_a(lilv, lp, lv2->atom_AtomPort) )   port |= PORTS_ATOM;
224                 ports[i] = port;
225         }
226 }
227
228 int PluginLV2ClientConfig::update()
229 {
230         int ret = 0;
231         PluginLV2ClientConfig &conf = *this;
232         for( int i=0; i<size(); ++i ) {
233                 if( conf[i]->item_value->update() ) ++ret;
234         }
235         return ret;
236 }
237
238 void PluginLV2ClientConfig::dump(FILE *fp)
239 {
240         fprintf(fp, "Controls:\n");
241         for( int i=0; i<size(); ++i ) {
242                 fprintf(fp, " %3d. (%3d)  %-24s  %f\n",
243                         i, get(i)->idx, get(i)->get_symbol(), get(i)->get_value());
244         }
245         fprintf(fp, "Ports:\n");
246         for( int i=0; i<nb_ports; ++i ) {
247                 fprintf(fp, " %3d.  %-24s  %f  (%f - %f) %s\n",
248                         i, syms[i], ctls[i], mins[i], maxs[i], names[i]);
249         }
250 }
251
252 #endif /* HAVE_LV2 */