Automation of LV2 plugin properties.
[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
38 #include <glib.h>
39 #include <glib/gstdio.h>
40 #include <glibmm.h>
41
42 #include "pbd/error.h"
43
44 #ifndef VST_SCANNER_APP
45 #include "ardour/plugin_manager.h" // scanner_bin_path
46 #include "ardour/rc_configuration.h"
47 #include "ardour/system_exec.h"
48 #endif
49
50 #include "ardour/filesystem_paths.h"
51 #include "ardour/linux_vst_support.h"
52 #include "ardour/plugin_types.h"
53 #include "ardour/vst_info_file.h"
54
55 #define MAX_STRING_LEN 256
56 #define PLUGIN_SCAN_TIMEOUT (Config->get_vst_scan_timeout()) // in deciseconds
57
58
59 /* CACHE FILE PATHS */
60 #define EXT_BLACKLIST ".fsb"
61 #define EXT_ERRORFILE ".err"
62 #define EXT_INFOFILE  ".fsi"
63
64 #ifdef PLATFORM_WINDOWS
65 #define PFX_DOTFILE   ""
66 #else
67 #define PFX_DOTFILE   "."
68 #endif
69
70
71 using namespace std;
72 #ifndef VST_SCANNER_APP
73 namespace ARDOUR {
74 #endif
75
76 /* prototypes */
77 #ifdef WINDOWS_VST_SUPPORT
78 #include <fst.h>
79 static bool
80 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
81 #endif
82
83 #ifdef LXVST_SUPPORT
84 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
85 #endif
86
87 /* ID for shell plugins */
88 static int vstfx_current_loading_id = 0;
89
90
91
92 /* *** CACHE FILE PATHS *** */
93
94 static string
95 vstfx_cache_file (const char* dllpath, int personal, const char *ext)
96 {
97         string dir;
98         if (personal) {
99                 dir = get_personal_vst_info_cache_dir();
100         } else {
101                 dir = Glib::path_get_dirname (std::string(dllpath));
102         }
103
104         stringstream s;
105         s << PFX_DOTFILE << Glib::path_get_basename (dllpath) << ext;
106         return Glib::build_filename (dir, s.str ());
107 }
108
109 static string
110 vstfx_blacklist_path (const char* dllpath, int personal)
111 {
112         string dir;
113         if (personal) {
114                 dir = get_personal_vst_blacklist_dir();
115         } else {
116                 dir = Glib::path_get_dirname (std::string(dllpath));
117         }
118
119         stringstream s;
120         s << PFX_DOTFILE << Glib::path_get_basename (dllpath) << EXT_BLACKLIST;
121         return Glib::build_filename (dir, s.str ());
122 }
123
124 static string
125 vstfx_infofile_path (const char* dllpath, int personal)
126 {
127         return vstfx_cache_file(dllpath, personal, EXT_INFOFILE);
128 }
129
130 #ifndef VST_SCANNER_APP
131 static string
132 vstfx_errorfile_path (const char* dllpath, int personal)
133 {
134         return vstfx_cache_file(dllpath, personal, EXT_ERRORFILE);
135 }
136 #endif
137
138
139 /* *** MEMORY MANAGEMENT *** */
140
141 /** cleanup single allocated VSTInfo */
142 static void
143 vstfx_free_info (VSTInfo *info)
144 {
145         for (int i = 0; i < info->numParams; i++) {
146                 free (info->ParamNames[i]);
147                 free (info->ParamLabels[i]);
148         }
149
150         free (info->name);
151         free (info->creator);
152         free (info->Category);
153         free (info->ParamNames);
154         free (info->ParamLabels);
155         free (info);
156 }
157
158 /** reset vector */
159 static void
160 vstfx_clear_info_list (vector<VSTInfo *> *infos)
161 {
162         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
163                 vstfx_free_info(*i);
164         }
165         infos->clear();
166 }
167
168
169
170 /* *** CACHE FILE I/O *** */
171
172 /** Helper function to read a line from the cache file
173  * @return newly allocated string of NULL
174  */
175 static char *
176 read_string (FILE *fp)
177 {
178         char buf[MAX_STRING_LEN];
179
180         if (!fgets (buf, MAX_STRING_LEN, fp)) {
181                 return 0;
182         }
183
184         if (strlen(buf) < MAX_STRING_LEN) {
185                 if (strlen (buf)) {
186                         buf[strlen(buf)-1] = 0;
187                 }
188                 return strdup (buf);
189         } else {
190                 return 0;
191         }
192 }
193
194 /** Read an integer value from a line in fp into n,
195  *  @return true on failure, false on success.
196  */
197 static bool
198 read_int (FILE* fp, int* n)
199 {
200         char buf[MAX_STRING_LEN];
201
202         char* p = fgets (buf, MAX_STRING_LEN, fp);
203         if (p == 0) {
204                 return true;
205         }
206
207         return (sscanf (p, "%d", n) != 1);
208 }
209
210 /** parse a plugin-block from the cache info file */
211 static bool
212 vstfx_load_info_block(FILE* fp, VSTInfo *info)
213 {
214         if ((info->name = read_string(fp)) == 0) return false;
215         if ((info->creator = read_string(fp)) == 0) return false;
216         if (read_int (fp, &info->UniqueID)) return false;
217         if ((info->Category = read_string(fp)) == 0) return false;
218         if (read_int (fp, &info->numInputs)) return false;
219         if (read_int (fp, &info->numOutputs)) return false;
220         if (read_int (fp, &info->numParams)) return false;
221         if (read_int (fp, &info->wantMidi)) return false;
222         if (read_int (fp, &info->hasEditor)) return false;
223         if (read_int (fp, &info->canProcessReplacing)) return false;
224
225         /* backwards compatibility with old .fsi files */
226         if (info->wantMidi == -1) {
227                 info->wantMidi = 1;
228         }
229
230         if ((info->numParams) == 0) {
231                 info->ParamNames = NULL;
232                 info->ParamLabels = NULL;
233                 return true;
234         }
235
236         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
237                 return false;
238         }
239
240         for (int i = 0; i < info->numParams; ++i) {
241                 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
242         }
243
244         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
245                 return false;
246         }
247
248         for (int i = 0; i < info->numParams; ++i) {
249                 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
250                         return false;
251                 }
252         }
253         return true;
254 }
255
256 /** parse all blocks in a cache info file */
257 static bool
258 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
259 {
260         VSTInfo *info;
261         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
262                 return false;
263         }
264         if (vstfx_load_info_block(fp, info)) {
265                 if (strncmp (info->Category, "Shell", 5)) {
266                         infos->push_back(info);
267                 } else {
268                         int plugin_cnt = 0;
269                         vstfx_free_info(info);
270                         if (!read_int (fp, &plugin_cnt)) {
271                                 for (int i = 0; i < plugin_cnt; i++) {
272                                         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
273                                                 vstfx_clear_info_list(infos);
274                                                 return false;
275                                         }
276                                         if (vstfx_load_info_block(fp, info)) {
277                                                 infos->push_back(info);
278                                         } else {
279                                                 vstfx_free_info(info);
280                                                 vstfx_clear_info_list(infos);
281                                                 return false;
282                                         }
283                                 }
284                         } else {
285                                 return false; /* Bad file */
286                         }
287                 }
288                 return true;
289         }
290         vstfx_free_info(info);
291         vstfx_clear_info_list(infos);
292         return false;
293 }
294
295 static void
296 vstfx_write_info_block (FILE* fp, VSTInfo *info)
297 {
298         assert (info);
299         assert (fp);
300
301         fprintf (fp, "%s\n", info->name);
302         fprintf (fp, "%s\n", info->creator);
303         fprintf (fp, "%d\n", info->UniqueID);
304         fprintf (fp, "%s\n", info->Category);
305         fprintf (fp, "%d\n", info->numInputs);
306         fprintf (fp, "%d\n", info->numOutputs);
307         fprintf (fp, "%d\n", info->numParams);
308         fprintf (fp, "%d\n", info->wantMidi);
309         fprintf (fp, "%d\n", info->hasEditor);
310         fprintf (fp, "%d\n", info->canProcessReplacing);
311
312         for (int i = 0; i < info->numParams; i++) {
313                 fprintf (fp, "%s\n", info->ParamNames[i]);
314         }
315
316         for (int i = 0; i < info->numParams; i++) {
317                 fprintf (fp, "%s\n", info->ParamLabels[i]);
318         }
319 }
320
321 static void
322 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
323 {
324         assert(infos);
325         assert(fp);
326
327         if (infos->size() > 1) {
328                 vector<VSTInfo *>::iterator x = infos->begin();
329                 /* write out the shell info first along with count of the number of
330                  * plugins contained in this shell
331                  */
332                 vstfx_write_info_block(fp, *x);
333                 fprintf( fp, "%d\n", (int)infos->size() - 1 );
334                 ++x;
335                 /* Now write out the info for each plugin */
336                 for (; x != infos->end(); ++x) {
337                         vstfx_write_info_block(fp, *x);
338                 }
339         } else if (infos->size() == 1) {
340                 vstfx_write_info_block(fp, infos->front());
341         } else {
342                 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
343         }
344 }
345
346
347 /* *** CACHE AND BLACKLIST MANAGEMENT *** */
348
349 /* return true if plugin is blacklisted or has an invalid file extension */
350 static bool
351 vstfx_blacklist_stat (const char *dllpath, int personal)
352 {
353         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
354                 return true;
355         }
356         string const path = vstfx_blacklist_path (dllpath, personal);
357
358         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
359                 struct stat dllstat;
360                 struct stat fsbstat;
361
362                 if (stat (dllpath, &dllstat) == 0 && stat (path.c_str(), &fsbstat) == 0) {
363                         if (dllstat.st_mtime > fsbstat.st_mtime) {
364                                 /* plugin is newer than blacklist file */
365                                 return true;
366                         }
367                 }
368                 /* stat failed or plugin is older than blacklist file */
369                 return true;
370         }
371         /* blacklist file does not exist */
372         return false;
373 }
374
375 /* return true if plugin is blacklisted, checks both personal
376  * and global folder */
377 static bool
378 vstfx_check_blacklist (const char *dllpath)
379 {
380         if (vstfx_blacklist_stat(dllpath, 0)) return true;
381         if (vstfx_blacklist_stat(dllpath, 1)) return true;
382         return false;
383 }
384
385 /* create blacklist file, preferably in same folder as the
386  * plugin, fall back to personal folder in $HOME
387  */
388 static FILE *
389 vstfx_blacklist_file (const char *dllpath)
390 {
391         FILE *f;
392         if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "wb"))) {
393 #ifndef NDEBUG
394         PBD::info << "Blacklisted VST: '" << vstfx_blacklist_path (dllpath, 0) << "'" << endmsg;
395 #endif
396                 return f;
397         }
398 #ifndef NDEBUG
399         PBD::info << "Blacklisted VST: '" << vstfx_blacklist_path (dllpath, 1) << "'" << endmsg;
400 #endif
401         return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "wb");
402 }
403
404 /** mark plugin as blacklisted */
405 static bool
406 vstfx_blacklist (const char *dllpath)
407 {
408         FILE *f = vstfx_blacklist_file(dllpath);
409         if (f) {
410                 fclose(f);
411                 return true;
412         }
413         return false;
414 }
415
416 /** mark plugin as not blacklisted */
417 static void
418 vstfx_un_blacklist (const char *dllpath)
419 {
420         ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
421         ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
422 }
423
424 /** remove info file from cache */
425 static void
426 vstfx_remove_infofile (const char *dllpath)
427 {
428         ::g_unlink(vstfx_infofile_path (dllpath, 0).c_str());
429         ::g_unlink(vstfx_infofile_path (dllpath, 1).c_str());
430 }
431
432 /** helper function, check if cache is newer than plugin
433  * @return path to cache file */
434 static char *
435 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
436 {
437         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
438                 return 0;
439         }
440
441         string const path = vstfx_infofile_path (dllpath, personal);
442
443         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
444
445                 struct stat dllstat;
446
447                 if (stat (dllpath, &dllstat) == 0) {
448                         if (stat (path.c_str(), statbuf) == 0) {
449                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
450                                         /* plugin is older than info file */
451                                         return strdup (path.c_str ());
452                                 }
453                         }
454                 }
455         }
456
457         return 0;
458 }
459
460 /** cache file for given plugin
461  * @return FILE of the .fsi cache if found and up-to-date*/
462 static FILE *
463 vstfx_infofile_for_read (const char* dllpath)
464 {
465         struct stat own_statbuf;
466         struct stat sys_statbuf;
467         FILE *rv = NULL;
468
469         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
470         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
471
472         if (own_info) {
473                 if (sys_info) {
474                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
475                                 /* system info file is newer, use it */
476                                 rv = g_fopen (sys_info, "rb");
477                         }
478                 } else {
479                         rv = g_fopen (own_info, "rb");
480                 }
481         } else if (sys_info) {
482                 rv = g_fopen (sys_info, "rb");
483         }
484         free(own_info);
485         free(sys_info);
486
487         return rv;
488 }
489
490 /** helper function for \ref vstfx_infofile_for_write
491  * abstract global and personal cache folders
492  */
493 static FILE *
494 vstfx_infofile_create (const char* dllpath, int personal)
495 {
496         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
497                 return 0;
498         }
499
500         string const path = vstfx_infofile_path (dllpath, personal);
501 #ifndef NDEBUG
502         PBD::info << "Creating VST cache file " << path << endmsg;
503 #endif
504         return fopen (path.c_str(), "wb");
505 }
506
507 /** newly created cache file for given plugin
508  * @return FILE for the .fsi cache, NULL if neither personal,
509  * nor global cache folder is writable */
510 static FILE *
511 vstfx_infofile_for_write (const char* dllpath)
512 {
513         FILE* f;
514
515         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
516                 f = vstfx_infofile_create (dllpath, 1);
517         }
518
519         return f;
520 }
521
522 /** check if cache-file exists, is up-to-date and parse cache file
523  * @param infos [return] loaded plugin info
524  * @return true if .fsi cache was read successfully, false otherwise
525  */
526 static bool
527 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
528 {
529         FILE* infofile;
530         bool rv = false;
531         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
532                 rv = vstfx_load_info_file(infofile, infos);
533                 fclose (infofile);
534                 if (!rv) {
535                         PBD::warning << "Cannot get VST information form " << dllpath << ": info file load failed." << endmsg;
536                 }
537         }
538         return rv;
539 }
540
541
542
543 /* *** VST system-under-test methods *** */
544
545 static
546 bool vstfx_midi_input (VSTState* vstfx)
547 {
548         AEffect* plugin = vstfx->plugin;
549
550         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
551
552         if (vst_version >= 2) {
553                 /* should we send it VST events (i.e. MIDI) */
554
555                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
556                         return true;
557                 }
558         }
559
560         return false;
561 }
562
563 static
564 bool vstfx_midi_output (VSTState* vstfx)
565 {
566         AEffect* plugin = vstfx->plugin;
567
568         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
569
570         if (vst_version >= 2) {
571                 /* should we send it VST events (i.e. MIDI) */
572
573                 if (   (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "sendVstEvents", 0.0f) > 0)
574                                 || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "sendVstMidiEvent", 0.0f) > 0)
575                          ) {
576                         return true;
577                 }
578         }
579
580         return false;
581 }
582
583 /** simple 'dummy' audiomaster callback to instantiate the plugin
584  * and query information
585  */
586 static intptr_t
587 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
588 {
589         const char* vstfx_can_do_strings[] = {
590                 "supplyIdle",
591                 "sendVstTimeInfo",
592                 "sendVstEvents",
593                 "sendVstMidiEvent",
594                 "receiveVstEvents",
595                 "receiveVstMidiEvent",
596                 "supportShell",
597                 "shellCategory",
598                 "shellCategorycurID"
599         };
600         const int vstfx_can_do_string_count = 9;
601
602         if (opcode == audioMasterVersion) {
603                 return 2400;
604         }
605         else if (opcode == audioMasterCanDo) {
606                 for (int i = 0; i < vstfx_can_do_string_count; i++) {
607                         if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
608                                 return 1;
609                         }
610                 }
611                 return 0;
612         }
613         else if (opcode == audioMasterCurrentId) {
614                 return vstfx_current_loading_id;
615         }
616         else {
617                 return 0;
618         }
619 }
620
621
622 /** main plugin query and test function */
623 static VSTInfo*
624 vstfx_parse_vst_state (VSTState* vstfx)
625 {
626         assert (vstfx);
627
628         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
629         if (!info) {
630                 return 0;
631         }
632
633         /*We need to init the creator because some plugins
634           fail to implement getVendorString, and so won't stuff the
635           string with any name*/
636
637         char creator[65] = "Unknown";
638         char name[65] = "";
639
640         AEffect* plugin = vstfx->plugin;
641
642
643         plugin->dispatcher (plugin, effGetEffectName, 0, 0, name, 0);
644
645         if (strlen(name) == 0) {
646                 plugin->dispatcher (plugin, effGetProductString, 0, 0, name, 0);
647         }
648
649         if (strlen(name) == 0) {
650                 info->name = strdup (vstfx->handle->name);
651         } else {
652                 info->name = strdup (name);
653         }
654
655         /*If the plugin doesn't bother to implement GetVendorString we will
656           have pre-stuffed the string with 'Unknown' */
657
658         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
659
660         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
661           so if its just a zero length string we replace it with 'Unknown' */
662
663         if (strlen(creator) == 0) {
664                 info->creator = strdup ("Unknown");
665         } else {
666                 info->creator = strdup (creator);
667         }
668
669
670         switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
671         {
672                 case kPlugCategEffect:         info->Category = strdup ("Effect"); break;
673                 case kPlugCategSynth:          info->Category = strdup ("Synth"); break;
674                 case kPlugCategAnalysis:       info->Category = strdup ("Anaylsis"); break;
675                 case kPlugCategMastering:      info->Category = strdup ("Mastering"); break;
676                 case kPlugCategSpacializer:    info->Category = strdup ("Spacializer"); break;
677                 case kPlugCategRoomFx:         info->Category = strdup ("RoomFx"); break;
678                 case kPlugSurroundFx:          info->Category = strdup ("SurroundFx"); break;
679                 case kPlugCategRestoration:    info->Category = strdup ("Restoration"); break;
680                 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
681                 case kPlugCategShell:          info->Category = strdup ("Shell"); break;
682                 case kPlugCategGenerator:      info->Category = strdup ("Generator"); break;
683                 default:                       info->Category = strdup ("Unknown"); break;
684         }
685
686         info->UniqueID = plugin->uniqueID;
687
688         info->numInputs = plugin->numInputs;
689         info->numOutputs = plugin->numOutputs;
690         info->numParams = plugin->numParams;
691         info->wantMidi = (vstfx_midi_input(vstfx) ? 1 : 0) | (vstfx_midi_output(vstfx) ? 2 : 0);
692         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
693         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
694         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
695         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
696
697         for (int i = 0; i < info->numParams; ++i) {
698                 char name[64];
699                 char label[64];
700
701                 /* Not all plugins give parameters labels as well as names */
702
703                 strcpy (name, "No Name");
704                 strcpy (label, "No Label");
705
706                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
707                 info->ParamNames[i] = strdup(name);
708
709                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
710                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
711                 info->ParamLabels[i] = strdup(label);
712         }
713         return info;
714 }
715
716 /** wrapper around \ref vstfx_parse_vst_state,
717  * iterate over plugins in shell, translate VST-info into ardour VSTState
718  */
719 static void
720 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, enum ARDOUR::PluginType type)
721 {
722         assert(vstfx);
723         VSTInfo *info;
724
725         if (!(info = vstfx_parse_vst_state(vstfx))) {
726                 return;
727         }
728
729         infos->push_back(info);
730 #if 1 // shell-plugin support
731         /* If this plugin is a Shell and we are not already inside a shell plugin
732          * read the info for all of the plugins contained in this shell.
733          */
734         if (!strncmp (info->Category, "Shell", 5)
735                         && vstfx->handle->plugincnt == 1) {
736                 int id;
737                 vector< pair<int, string> > ids;
738                 AEffect *plugin = vstfx->plugin;
739
740                 do {
741                         char name[65] = "Unknown";
742                         id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
743                         ids.push_back(std::make_pair(id, name));
744                 } while ( id != 0 );
745
746                 switch(type) {
747 #ifdef WINDOWS_VST_SUPPORT
748                         case ARDOUR::Windows_VST: fst_close(vstfx); break;
749 #endif
750 #ifdef LXVST_SUPPORT
751                         case ARDOUR::LXVST: vstfx_close (vstfx); break;
752 #endif
753                         default: assert(0); break;
754                 }
755
756                 for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
757                         id = (*x).first;
758                         if (id == 0) continue;
759                         /* recurse vstfx_get_info() */
760
761                         bool ok;
762                         switch (type) {
763 #ifdef WINDOWS_VST_SUPPORT
764                                 case ARDOUR::Windows_VST:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
765 #endif
766 #ifdef LXVST_SUPPORT
767                                 case ARDOUR::LXVST:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
768 #endif
769                                 default: ok = false;
770                         }
771                         if (ok) {
772                                 // One shell (some?, all?) does not report the actual plugin name
773                                 // even after the shelled plugin has been instantiated.
774                                 // Replace the name of the shell with the real name.
775                                 info = infos->back();
776                                 free (info->name);
777
778                                 if ((*x).second.length() == 0) {
779                                         info->name = strdup("Unknown");
780                                 }
781                                 else {
782                                         info->name = strdup ((*x).second.c_str());
783                                 }
784                         }
785                 }
786         } else {
787                 switch(type) {
788 #ifdef WINDOWS_VST_SUPPORT
789                         case ARDOUR::Windows_VST: fst_close(vstfx); break;
790 #endif
791 #ifdef LXVST_SUPPORT
792                         case ARDOUR::LXVST: vstfx_close (vstfx); break;
793 #endif
794                         default: assert(0); break;
795                 }
796         }
797 #endif
798 }
799
800
801
802 /* *** TOP-LEVEL PLUGIN INSTANTIATION FUNCTIONS *** */
803
804 #ifdef LXVST_SUPPORT
805 static bool
806 vstfx_instantiate_and_get_info_lx (
807                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
808 {
809         VSTHandle* h;
810         VSTState* vstfx;
811         if (!(h = vstfx_load(dllpath))) {
812                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
813                 return false;
814         }
815
816         vstfx_current_loading_id = uniqueID;
817
818         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
819                 vstfx_unload(h);
820                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
821                 return false;
822         }
823
824         vstfx_current_loading_id = 0;
825
826         vstfx_info_from_plugin(dllpath, vstfx, infos, ARDOUR::LXVST);
827
828         vstfx_unload (h);
829         return true;
830 }
831 #endif
832
833 #ifdef WINDOWS_VST_SUPPORT
834 static bool
835 vstfx_instantiate_and_get_info_fst (
836                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
837 {
838         VSTHandle* h;
839         VSTState* vstfx;
840         if(!(h = fst_load(dllpath))) {
841                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
842                 return false;
843         }
844
845         vstfx_current_loading_id = uniqueID;
846
847         if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
848                 fst_unload(&h);
849                 vstfx_current_loading_id = 0;
850                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
851                 return false;
852         }
853         vstfx_current_loading_id = 0;
854
855         vstfx_info_from_plugin(dllpath, vstfx, infos, ARDOUR::Windows_VST);
856
857         return true;
858 }
859 #endif
860
861
862
863 /* *** ERROR LOGGING *** */
864 #ifndef VST_SCANNER_APP
865
866 static FILE * _errorlog_fd = 0;
867 static char * _errorlog_dll = 0;
868
869 static void parse_scanner_output (std::string msg, size_t /*len*/)
870 {
871         if (!_errorlog_fd && !_errorlog_dll) {
872                 PBD::error << "VST scanner: " << msg;
873                 return;
874         }
875
876         if (!_errorlog_fd) {
877                 if (!(_errorlog_fd = fopen(vstfx_errorfile_path(_errorlog_dll, 0).c_str(), "w"))) {
878                         if (!(_errorlog_fd = fopen(vstfx_errorfile_path(_errorlog_dll, 1).c_str(), "w"))) {
879                                 PBD::error << "Cannot create plugin error-log for plugin " << _errorlog_dll;
880                                 free(_errorlog_dll);
881                                 _errorlog_dll = NULL;
882                         }
883                 }
884         }
885
886         if (_errorlog_fd) {
887                 fprintf (_errorlog_fd, "%s\n", msg.c_str());
888         } else {
889                 PBD::error << "VST scanner: " << msg;
890         }
891 }
892
893 static void
894 set_error_log (const char* dllpath) {
895         assert(!_errorlog_fd);
896         assert(!_errorlog_dll);
897         _errorlog_dll = strdup(dllpath);
898 }
899
900 static void
901 close_error_log () {
902         if (_errorlog_fd) {
903                 fclose(_errorlog_fd);
904                 _errorlog_fd = 0;
905         }
906         free(_errorlog_dll);
907         _errorlog_dll = 0;
908 }
909
910 #endif
911
912
913 /* *** THE MAIN FUNCTION THAT USES ALL OF THE ABOVE :) *** */
914
915 static vector<VSTInfo *> *
916 vstfx_get_info (const char* dllpath, enum ARDOUR::PluginType type, enum VSTScanMode mode)
917 {
918         FILE* infofile;
919         vector<VSTInfo*> *infos = new vector<VSTInfo*>;
920
921         if (vstfx_check_blacklist(dllpath)) {
922                 return infos;
923         }
924
925         if (vstfx_get_info_from_file(dllpath, infos)) {
926                 return infos;
927         }
928
929 #ifndef VST_SCANNER_APP
930         std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
931
932         if (mode == VST_SCAN_CACHE_ONLY) {
933                 /* never scan explicitly, use cache only */
934                 return infos;
935         }
936         else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
937                 /* use external scanner app */
938
939                 char **argp= (char**) calloc(3,sizeof(char*));
940                 argp[0] = strdup(scanner_bin_path.c_str());
941                 argp[1] = strdup(dllpath);
942                 argp[2] = 0;
943
944                 set_error_log(dllpath);
945                 ARDOUR::SystemExec scanner (scanner_bin_path, argp);
946                 PBD::ScopedConnectionList cons;
947                 scanner.ReadStdout.connect_same_thread (cons, boost::bind (&parse_scanner_output, _1 ,_2));
948                 if (scanner.start (2 /* send stderr&stdout via signal */)) {
949                         PBD::error << "Cannot launch VST scanner app '" << scanner_bin_path << "': "<< strerror(errno) << endmsg;
950                         close_error_log();
951                         return infos;
952                 } else {
953                         int timeout = PLUGIN_SCAN_TIMEOUT;
954                         bool no_timeout = (timeout <= 0);
955                         ARDOUR::PluginScanTimeout(timeout);
956                         while (scanner.is_running() && (no_timeout || timeout > 0)) {
957                                 if (!no_timeout && !ARDOUR::PluginManager::instance().no_timeout()) {
958                                         if (timeout%5 == 0) {
959                                                 ARDOUR::PluginScanTimeout(timeout);
960                                         }
961                                         --timeout;
962                                 }
963                                 ARDOUR::GUIIdle();
964                                 Glib::usleep (100000);
965
966                                 if (ARDOUR::PluginManager::instance().cancelled()) {
967                                         // remove info file (might be incomplete)
968                                         vstfx_remove_infofile(dllpath);
969                                         // remove temporary blacklist file (scan incomplete)
970                                         vstfx_un_blacklist(dllpath);
971                                         scanner.terminate();
972                                         close_error_log();
973                                         return infos;
974                                 }
975                         }
976                         scanner.terminate();
977                 }
978                 close_error_log();
979                 /* re-read index (generated by external scanner) */
980                 vstfx_clear_info_list(infos);
981                 if (!vstfx_check_blacklist(dllpath)) {
982                         vstfx_get_info_from_file(dllpath, infos);
983                 }
984                 return infos;
985         }
986         /* else .. instantiate and check in in ardour process itself */
987 #else
988         (void) mode; // unused parameter
989 #endif
990
991         bool ok;
992         /* blacklist in case instantiation fails */
993         vstfx_blacklist(dllpath);
994
995         switch (type) {
996 #ifdef WINDOWS_VST_SUPPORT
997                 case ARDOUR::Windows_VST:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
998 #endif
999 #ifdef LXVST_SUPPORT
1000                 case ARDOUR::LXVST:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
1001 #endif
1002                 default: ok = false;
1003         }
1004
1005         if (!ok) {
1006                 return infos;
1007         }
1008
1009         /* remove from blacklist */
1010         vstfx_un_blacklist(dllpath);
1011
1012         /* crate cache/whitelist */
1013         infofile = vstfx_infofile_for_write (dllpath);
1014         if (!infofile) {
1015                 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
1016                 return infos;
1017         } else {
1018                 vstfx_write_info_file (infofile, infos);
1019                 fclose (infofile);
1020         }
1021         return infos;
1022 }
1023
1024
1025
1026 /* *** public API *** */
1027
1028 void
1029 vstfx_free_info_list (vector<VSTInfo *> *infos)
1030 {
1031         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
1032                 vstfx_free_info(*i);
1033         }
1034         delete infos;
1035 }
1036
1037 string
1038 get_personal_vst_blacklist_dir() {
1039         string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
1040         /* if the directory doesn't exist, try to create it */
1041         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
1042                 if (g_mkdir (dir.c_str (), 0700)) {
1043                         PBD::error << "Cannot create VST blacklist folder '" << dir << "'" << endmsg;
1044                         //exit(1);
1045                 }
1046         }
1047         return dir;
1048 }
1049
1050 string
1051 get_personal_vst_info_cache_dir() {
1052         string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
1053         /* if the directory doesn't exist, try to create it */
1054         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
1055                 if (g_mkdir (dir.c_str (), 0700)) {
1056                         PBD::error << "Cannot create VST info folder '" << dir << "'" << endmsg;
1057                         //exit(1);
1058                 }
1059         }
1060         return dir;
1061 }
1062
1063 #ifdef LXVST_SUPPORT
1064 vector<VSTInfo *> *
1065 vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
1066 {
1067         return vstfx_get_info(dllpath, ARDOUR::LXVST, mode);
1068 }
1069 #endif
1070
1071 #ifdef WINDOWS_VST_SUPPORT
1072 vector<VSTInfo *> *
1073 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
1074 {
1075         return vstfx_get_info(dllpath, ARDOUR::Windows_VST, mode);
1076 }
1077 #endif
1078
1079 #ifndef VST_SCANNER_APP
1080 } // namespace
1081 #endif