consolidate lxVST & winVST file-info code into libardour
[ardour.git] / libs / ardour / vst_info_file.cc
1 /*
2     Copyright (C) 2012-2014 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 /** @file libs/ardour/vst_info_file.cc
21  *  @brief Code to manage info files containing cached information about a plugin.
22  *  e.g. its name, creator etc.
23  */
24
25 #include <iostream>
26 #include <cassert>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <libgen.h>
38
39 #include <glib.h>
40 #include <glib/gstdio.h>
41 #include <glibmm.h>
42
43 #include "pbd/error.h"
44
45 #include "ardour/linux_vst_support.h"
46 #include "ardour/vst_info_file.h"
47
48 #define MAX_STRING_LEN 256
49
50 using namespace std;
51
52 static char *
53 read_string (FILE *fp)
54 {
55         char buf[MAX_STRING_LEN];
56
57         if (!fgets (buf, MAX_STRING_LEN, fp)) {
58                 return 0;
59         }
60
61         if (strlen(buf) < MAX_STRING_LEN) {
62                 if (strlen (buf)) {
63                         buf[strlen(buf)-1] = 0;
64                 }
65                 return strdup (buf);
66         } else {
67                 return 0;
68         }
69 }
70
71 /** Read an integer value from a line in fp into n,
72  *  @return true on failure, false on success.
73  */
74 static bool
75 read_int (FILE* fp, int* n)
76 {
77         char buf[MAX_STRING_LEN];
78
79         char* p = fgets (buf, MAX_STRING_LEN, fp);
80         if (p == 0) {
81                 return true;
82         }
83
84         return (sscanf (p, "%d", n) != 1);
85 }
86
87 static VSTInfo *
88 load_vstfx_info_file (FILE* fp)
89 {
90         VSTInfo *info;
91
92         if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
93                 return 0;
94         }
95
96         if ((info->name = read_string(fp)) == 0) goto error;
97         if ((info->creator = read_string(fp)) == 0) goto error;
98         if (read_int (fp, &info->UniqueID)) goto error;
99         if ((info->Category = read_string(fp)) == 0) goto error;
100         if (read_int (fp, &info->numInputs)) goto error;
101         if (read_int (fp, &info->numOutputs)) goto error;
102         if (read_int (fp, &info->numParams)) goto error;
103         if (read_int (fp, &info->wantMidi)) goto error;
104         if (read_int (fp, &info->hasEditor)) goto error;
105         if (read_int (fp, &info->canProcessReplacing)) goto error;
106
107         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
108                 goto error;
109         }
110
111         for (int i = 0; i < info->numParams; ++i) {
112                 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
113         }
114
115         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
116                 goto error;
117         }
118
119         for (int i = 0; i < info->numParams; ++i) {
120                 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
121         }
122
123         return info;
124
125 error:
126         free (info);
127         return 0;
128 }
129
130 static int
131 save_vstfx_info_file (VSTInfo *info, FILE* fp)
132 {
133         assert (info);
134         assert (fp);
135
136         fprintf (fp, "%s\n", info->name);
137         fprintf (fp, "%s\n", info->creator);
138         fprintf (fp, "%d\n", info->UniqueID);
139         fprintf (fp, "%s\n", info->Category);
140         fprintf (fp, "%d\n", info->numInputs);
141         fprintf (fp, "%d\n", info->numOutputs);
142         fprintf (fp, "%d\n", info->numParams);
143         fprintf (fp, "%d\n", info->wantMidi);
144         fprintf (fp, "%d\n", info->hasEditor);
145         fprintf (fp, "%d\n", info->canProcessReplacing);
146
147         for (int i = 0; i < info->numParams; i++) {
148                 fprintf (fp, "%s\n", info->ParamNames[i]);
149         }
150
151         for (int i = 0; i < info->numParams; i++) {
152                 fprintf (fp, "%s\n", info->ParamLabels[i]);
153         }
154
155         return 0;
156 }
157
158 static string
159 vstfx_infofile_path (char* dllpath, int personal)
160 {
161         string dir;
162         if (personal) {
163                 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
164
165                 /* If the directory doesn't exist, try to create it */
166                 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
167                         if (g_mkdir (dir.c_str (), 0700)) {
168                                 return 0;
169                         }
170                 }
171
172         } else {
173                 dir = Glib::path_get_dirname (std::string(dllpath));
174         }
175
176         stringstream s;
177         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
178         return Glib::build_filename (dir, s.str ());
179 }
180
181 static char *
182 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
183 {
184         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
185                 return 0;
186         }
187
188         string const path = vstfx_infofile_path (dllpath, personal);
189
190         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
191
192                 /* info file exists in same location as the shared object, so
193                    check if its current and up to date
194                 */
195
196
197                 struct stat dllstat;
198
199                 if (stat (dllpath, &dllstat) == 0) {
200                         if (stat (path.c_str(), statbuf) == 0) {
201                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
202                                         /* plugin is older than info file */
203                                         return strdup (path.c_str ());
204                                 }
205                         }
206                 }
207         }
208
209         return 0;
210 }
211
212
213 static FILE *
214 vstfx_infofile_for_read (char* dllpath)
215 {
216         struct stat own_statbuf;
217         struct stat sys_statbuf;
218         FILE *rv = NULL;
219
220         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
221         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
222
223         if (own_info) {
224                 if (sys_info) {
225                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
226                                 /* system info file is newer, use it */
227                                 rv = g_fopen (sys_info, "rb");
228                         }
229                 } else {
230                         rv = g_fopen (own_info, "rb");
231                 }
232         } else if (sys_info) {
233                 rv = g_fopen (sys_info, "rb");
234         }
235         free(own_info);
236         free(sys_info);
237
238         return rv;
239 }
240
241 static FILE *
242 vstfx_infofile_create (char* dllpath, int personal)
243 {
244         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
245                 return 0;
246         }
247
248         string const path = vstfx_infofile_path (dllpath, personal);
249         return fopen (path.c_str(), "w");
250 }
251
252 static FILE *
253 vstfx_infofile_for_write (char* dllpath)
254 {
255         FILE* f;
256
257         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
258                 f = vstfx_infofile_create (dllpath, 1);
259         }
260
261         return f;
262 }
263
264 static
265 int vstfx_can_midi (VSTState* vstfx)
266 {
267         AEffect* plugin = vstfx->plugin;
268
269         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
270
271         if (vst_version >= 2) {
272                 /* should we send it VST events (i.e. MIDI) */
273
274                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
275                         return -1;
276                 }
277         }
278
279         return false;
280 }
281
282 static VSTInfo *
283 vstfx_info_from_plugin (VSTState* vstfx)
284 {
285         assert (vstfx);
286
287         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
288         if (!info) {
289                 return 0;
290         }
291
292         /*We need to init the creator because some plugins
293           fail to implement getVendorString, and so won't stuff the
294           string with any name*/
295
296         char creator[65] = "Unknown\0";
297
298         AEffect* plugin = vstfx->plugin;
299
300         info->name = strdup (vstfx->handle->name);
301
302         /*If the plugin doesn't bother to implement GetVendorString we will
303           have pre-stuffed the string with 'Unkown' */
304
305         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
306
307         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
308           so if its just a zero length string we replace it with 'Unknown' */
309
310         if (strlen(creator) == 0) {
311                 info->creator = strdup ("Unknown");
312         } else {
313                 info->creator = strdup (creator);
314         }
315
316         info->UniqueID = plugin->uniqueID;
317
318         info->Category = strdup("None"); /* XXX */
319         info->numInputs = plugin->numInputs;
320         info->numOutputs = plugin->numOutputs;
321         info->numParams = plugin->numParams;
322         info->wantMidi = vstfx_can_midi(vstfx);
323         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
324         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
325         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
326         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
327
328         for (int i = 0; i < info->numParams; ++i) {
329                 char name[64];
330                 char label[64];
331
332                 /* Not all plugins give parameters labels as well as names */
333
334                 strcpy (name, "No Name");
335                 strcpy (label, "No Label");
336
337                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
338                 info->ParamNames[i] = strdup(name);
339
340                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
341                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
342                 info->ParamLabels[i] = strdup(label);
343         }
344         return info;
345 }
346
347 /* A simple 'dummy' audiomaster callback which should be ok,
348    we will only be instantiating the plugin in order to get its info
349 */
350
351 static intptr_t
352 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
353 {
354         if (opcode == audioMasterVersion) {
355                 return 2;
356         } else {
357                 return 0;
358         }
359 }
360
361 static VSTInfo *
362 vstfx_get_info_from_file(char* dllpath, bool &found)
363 {
364         FILE* infofile;
365         VSTInfo *info = 0;
366         found = false;
367         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
368                 found = true;
369                 info = load_vstfx_info_file (infofile);
370                 fclose (infofile);
371                 if (info == 0) {
372                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
373                 }
374         }
375         return info;
376 }
377
378 void
379 vstfx_free_info (VSTInfo *info)
380 {
381         for (int i = 0; i < info->numParams; i++) {
382                 free (info->ParamNames[i]);
383                 free (info->ParamLabels[i]);
384         }
385
386         free (info->name);
387         free (info->creator);
388         free (info->Category);
389         free (info->ParamNames);
390         free (info->ParamLabels);
391         free (info);
392 }
393
394 #ifdef LXVST_SUPPORT
395 /** Try to get plugin info - first by looking for a .fsi cache of the
396     data, and if that doesn't exist, load the plugin, get its data and
397     then cache it for future ref
398 */
399
400 VSTInfo *
401 vstfx_get_info_lx (char* dllpath)
402 {
403         FILE* infofile;
404         VSTHandle* h;
405         VSTState* vstfx;
406
407         bool used_cache;
408         VSTInfo* info = vstfx_get_info_from_file(dllpath, used_cache);
409         if (used_cache) {
410                 PBD::info << "using cache for LinuxVST plugin '" << dllpath << "'" << endmsg;
411                 return info;
412         }
413
414         if (!(h = vstfx_load(dllpath))) {
415                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
416                 return 0;
417         }
418
419         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
420                 vstfx_unload(h);
421                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
422                 return 0;
423         }
424
425         infofile = vstfx_infofile_for_write (dllpath);
426
427         if (!infofile) {
428                 vstfx_close(vstfx);
429                 vstfx_unload(h);
430                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
431                 return 0;
432         }
433
434         info = vstfx_info_from_plugin (vstfx);
435
436         save_vstfx_info_file (info, infofile);
437         fclose (infofile);
438
439         vstfx_close (vstfx);
440         vstfx_unload (h);
441
442         return info;
443 }
444 #endif
445
446 #ifdef WINDOWS_VST_SUPPORT
447 #include <fst.h>
448
449 VSTInfo *
450 vstfx_get_info_fst (char* dllpath)
451 {
452         FILE* infofile;
453         VSTHandle* h;
454         VSTState* vstfx;
455
456         bool used_cache;
457         VSTInfo* info = vstfx_get_info_from_file(dllpath, used_cache);
458         if (used_cache) {
459                 PBD::info << "using cache for VST plugin '" << dllpath << "'" << endmsg;
460                 return info;
461         }
462
463         if(!(h = fst_load(dllpath))) {
464                 PBD::warning << "Cannot get VST information from " << dllpath << ": load failed." << endmsg;
465                 return 0;
466         }
467
468         if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
469                 fst_unload(&h);
470                 PBD::warning << "Cannot get VST information from " << dllpath << ": instantiation failed." << endmsg;
471                 return 0;
472         }
473
474         infofile = vstfx_infofile_for_write (dllpath);
475
476         if (!infofile) {
477                 fst_close(vstfx);
478                 //fst_unload(&h); // XXX -> fst_close()
479                 PBD::warning << "Cannot get VST information from " << dllpath << ": cannot create new FST info file." << endmsg;
480                 return 0;
481         }
482
483         info = vstfx_info_from_plugin(vstfx);
484         save_vstfx_info_file (info, infofile);
485         fclose (infofile);
486
487         fst_close(vstfx);
488         //fst_unload(&h); // XXX -> fst_close()
489
490         return info;
491 }
492 #endif