Manage attempts to save plugin presets with the same name. Helps with #2662.
[ardour.git] / libs / ardour / plugin.cc
1 /*
2     Copyright (C) 2000-2002 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <vector>
25 #include <string>
26
27 #include <cstdlib>
28 #include <cstdio> // so libraptor doesn't complain
29 #include <cmath>
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <cerrno>
33 #include <utility>
34
35 #include <lrdf.h>
36
37 #include "pbd/compose.h"
38 #include "pbd/error.h"
39 #include "pbd/xml++.h"
40
41 #include "ardour/ardour.h"
42 #include "ardour/session.h"
43 #include "ardour/audioengine.h"
44 #include "ardour/plugin.h"
45 #include "ardour/ladspa_plugin.h"
46 #include "ardour/plugin_manager.h"
47
48 #ifdef HAVE_AUDIOUNITS
49 #include "ardour/audio_unit.h"
50 #endif
51
52 #ifdef HAVE_SLV2
53 #include "ardour/lv2_plugin.h"
54 #endif
55
56 #include "pbd/stl_delete.h"
57
58 #include "i18n.h"
59 #include <locale.h>
60
61 using namespace std;
62 using namespace ARDOUR;
63 using namespace PBD;
64
65 PBD::Signal0<bool> Plugin::PresetFileExists;
66
67 Plugin::Plugin (AudioEngine& e, Session& s)
68         : _engine (e)
69         , _session (s)
70         , _cycles (0)
71 {
72 }
73
74 Plugin::Plugin (const Plugin& other)
75         : StatefulDestructible()
76         , Latent()
77         , _engine (other._engine)
78         , _session (other._session)
79         , _info (other._info)
80         , _cycles (0)
81         , presets (other.presets)
82 {
83 }
84
85 Plugin::~Plugin ()
86 {
87 }
88
89 const Plugin::PresetRecord*
90 Plugin::preset_by_label(const string& label)
91 {
92         // FIXME: O(n)
93         for (map<string,PresetRecord>::const_iterator i = presets.begin(); i != presets.end(); ++i) {
94                 if (i->second.label == label) {
95                         return &i->second;
96                 }
97         }
98         return NULL;
99 }
100
101 const Plugin::PresetRecord*
102 Plugin::preset_by_uri(const string& uri)
103 {
104         map<string,PresetRecord>::const_iterator pr = presets.find(uri);
105         if (pr != presets.end()) {
106                 return &pr->second;
107         } else {
108                 return NULL;
109         }
110 }
111
112 vector<Plugin::PresetRecord>
113 Plugin::get_presets()
114 {
115         vector<PresetRecord> result;
116         uint32_t id;
117         std::string unique (unique_id());
118
119         /* XXX problem: AU plugins don't have numeric ID's.
120            Solution: they have a different method of providing presets.
121            XXX sub-problem: implement it.
122         */
123
124         if (!isdigit (unique[0])) {
125                 return result;
126         }
127
128         id = atol (unique.c_str());
129
130         lrdf_uris* set_uris = lrdf_get_setting_uris(id);
131
132         if (set_uris) {
133                 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
134                         if (char* label = lrdf_get_label(set_uris->items[i])) {
135                                 PresetRecord rec(set_uris->items[i], label);
136                                 result.push_back(rec);
137                                 presets.insert(std::make_pair(set_uris->items[i], rec));
138                         }
139                 }
140                 lrdf_free_uris(set_uris);
141         }
142
143         return result;
144 }
145
146 bool
147 Plugin::load_preset(const string& preset_uri)
148 {
149         lrdf_defaults* defs = lrdf_get_setting_values(preset_uri.c_str());
150
151         if (defs) {
152                 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
153                         // The defs->items[i].pid < defs->count check is to work around
154                         // a bug in liblrdf that saves invalid values into the presets file.
155                         if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
156                                 set_parameter(defs->items[i].pid, defs->items[i].value);
157                         }
158                 }
159                 lrdf_free_setting_values(defs);
160         }
161
162         return true;
163 }
164
165 /* XXX: should be in liblrdf */
166 static void
167 lrdf_remove_preset (const char *source, const char *setting_uri)
168 {
169         lrdf_statement p;
170         lrdf_statement *q;
171         lrdf_statement *i;
172         char setting_uri_copy[64];
173         char buf[64];
174         
175         strncpy(setting_uri_copy, setting_uri, sizeof(setting_uri_copy));
176
177         p.subject = setting_uri_copy;
178         strncpy(buf, LADSPA_BASE "hasPortValue", sizeof(buf));
179         p.predicate = buf;
180         p.object = NULL;
181         q = lrdf_matches(&p);
182
183         p.predicate = NULL;
184         p.object = NULL;
185         for (i = q; i; i = i->next) {
186                 p.subject = i->object;
187                 lrdf_remove_matches(&p);
188         }
189
190         lrdf_free_statements(q);
191
192         p.subject = NULL;
193         strncpy(buf, LADSPA_BASE "hasSetting", sizeof(buf));
194         p.predicate = buf;
195         p.object = setting_uri_copy;
196         lrdf_remove_matches(&p);
197
198         p.subject = setting_uri_copy;
199         p.predicate = NULL;
200         p.object = NULL;
201         lrdf_remove_matches (&p);
202 }
203
204 void
205 Plugin::remove_preset (string name, string domain)
206 {
207         string const envvar = preset_envvar ();
208         if (envvar.empty()) {
209                 warning << _("Could not locate HOME.  Preset not removed.") << endmsg;
210                 return;
211         }
212
213         Plugin::PresetRecord const * p = preset_by_label (name);
214         if (!p) {
215                 return;
216         }
217         
218         string const source = preset_source (envvar, domain);
219         lrdf_remove_preset (source.c_str(), p->uri.c_str ());
220
221         presets.erase (p->uri);
222
223         write_preset_file (envvar, domain);
224 }
225
226 string
227 Plugin::preset_envvar () const
228 {
229         char* envvar;
230         if ((envvar = getenv ("HOME")) == 0) {
231                 return "";
232         }
233         
234         return envvar;
235 }
236
237 string
238 Plugin::preset_source (string envvar, string domain) const
239 {
240         return string_compose ("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain);
241 }
242
243 bool
244 Plugin::write_preset_file (string envvar, string domain)
245 {
246         string path = string_compose("%1/.%2", envvar, domain);
247         if (g_mkdir_with_parents (path.c_str(), 0775)) {
248                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
249                 return false;
250         }
251
252         path += "/rdf";
253         if (g_mkdir_with_parents (path.c_str(), 0775)) {
254                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
255                 return false;
256         }
257
258         string const source = preset_source (envvar, domain);
259
260         if (lrdf_export_by_source (source.c_str(), source.substr(5).c_str())) {
261                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
262                 return false;
263         }
264
265         return true;
266 }
267
268 bool
269 Plugin::save_preset (string name, string domain)
270 {
271         lrdf_portvalue portvalues[parameter_count()];
272         lrdf_defaults defaults;
273         uint32_t id;
274         std::string unique (unique_id());
275
276         /* XXX problem: AU plugins don't have numeric ID's.
277            Solution: they have a different method of providing/saving presets.
278            XXX sub-problem: implement it.
279         */
280
281         if (!isdigit (unique[0])) {
282                 return false;
283         }
284
285         id = atol (unique.c_str());
286
287         defaults.count = parameter_count();
288         defaults.items = portvalues;
289
290         for (uint32_t i = 0; i < parameter_count(); ++i) {
291                 if (parameter_is_input (i)) {
292                         portvalues[i].pid = i;
293                         portvalues[i].value = get_parameter(i);
294                 }
295         }
296
297         string const envvar = preset_envvar ();
298         if (envvar.empty()) {
299                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
300                 return false;
301         }
302
303         string const source = preset_source (envvar, domain);
304
305         char* uri = lrdf_add_preset (source.c_str(), name.c_str(), id, &defaults);
306         
307         /* XXX: why is the uri apparently kept as the key in the `presets' map and also in the PresetRecord? */
308
309         presets.insert (make_pair (uri, PresetRecord (uri, name)));
310         free (uri);
311
312         return write_preset_file (envvar, domain);
313 }
314
315 PluginPtr
316 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
317 {
318         PluginManager *mgr = PluginManager::the_manager();
319         PluginInfoList plugs;
320
321         switch (type) {
322         case ARDOUR::LADSPA:
323                 plugs = mgr->ladspa_plugin_info();
324                 break;
325
326 #ifdef HAVE_SLV2
327         case ARDOUR::LV2:
328                 plugs = mgr->lv2_plugin_info();
329                 break;
330 #endif
331
332 #ifdef VST_SUPPORT
333         case ARDOUR::VST:
334                 plugs = mgr->vst_plugin_info();
335                 break;
336 #endif
337
338 #ifdef HAVE_AUDIOUNITS
339         case ARDOUR::AudioUnit:
340                 plugs = mgr->au_plugin_info();
341                 break;
342 #endif
343
344         default:
345                 return PluginPtr ((Plugin *) 0);
346         }
347
348         PluginInfoList::iterator i;
349
350         for (i = plugs.begin(); i != plugs.end(); ++i) {
351                 if (identifier == (*i)->unique_id){
352                         return (*i)->load (session);
353                 }
354         }
355
356 #ifdef VST_SUPPORT
357         /* hmm, we didn't find it. could be because in older versions of Ardour.
358            we used to store the name of a VST plugin, not its unique ID. so try
359            again.
360         */
361
362         for (i = plugs.begin(); i != plugs.end(); ++i) {
363                 if (identifier == (*i)->name){
364                         return (*i)->load (session);
365                 }
366         }
367 #endif
368
369         return PluginPtr ((Plugin*) 0);
370 }
371
372 ChanCount
373 Plugin::output_streams () const
374 {
375         /* LADSPA & VST should not get here because they do not
376            return "infinite" i/o counts.
377         */
378         return ChanCount::ZERO;
379 }
380
381 ChanCount
382 Plugin::input_streams () const
383 {
384         /* LADSPA & VST should not get here because they do not
385            return "infinite" i/o counts.
386         */
387         return ChanCount::ZERO;
388 }
389
390