scan VSTs in background, allow to cancel & timeout
[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 #ifndef VST_SCANNER_APP
46 #include "pbd/system_exec.h"
47 #include "ardour/plugin_manager.h" // scanner_bin_path
48 #endif
49
50 #include "ardour/filesystem_paths.h"
51 #include "ardour/linux_vst_support.h"
52 #include "ardour/vst_info_file.h"
53
54 #define MAX_STRING_LEN 256
55 #define PLUGIN_SCAN_TIMEOUT (600) // in deciseconds
56
57 using namespace std;
58
59 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
60 //       consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
61
62 /* prototypes */
63 #ifdef WINDOWS_VST_SUPPORT
64 #include <fst.h>
65 static bool
66 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
67 #endif
68
69 #ifdef LXVST_SUPPORT
70 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
71 #endif
72
73 static int vstfx_current_loading_id = 0;
74
75 static char *
76 read_string (FILE *fp)
77 {
78         char buf[MAX_STRING_LEN];
79
80         if (!fgets (buf, MAX_STRING_LEN, fp)) {
81                 return 0;
82         }
83
84         if (strlen(buf) < MAX_STRING_LEN) {
85                 if (strlen (buf)) {
86                         buf[strlen(buf)-1] = 0;
87                 }
88                 return strdup (buf);
89         } else {
90                 return 0;
91         }
92 }
93
94 /** Read an integer value from a line in fp into n,
95  *  @return true on failure, false on success.
96  */
97 static bool
98 read_int (FILE* fp, int* n)
99 {
100         char buf[MAX_STRING_LEN];
101
102         char* p = fgets (buf, MAX_STRING_LEN, fp);
103         if (p == 0) {
104                 return true;
105         }
106
107         return (sscanf (p, "%d", n) != 1);
108 }
109
110 static void
111 vstfx_free_info (VSTInfo *info)
112 {
113         for (int i = 0; i < info->numParams; i++) {
114                 free (info->ParamNames[i]);
115                 free (info->ParamLabels[i]);
116         }
117
118         free (info->name);
119         free (info->creator);
120         free (info->Category);
121         free (info->ParamNames);
122         free (info->ParamLabels);
123         free (info);
124 }
125
126 static void
127 vstfx_clear_info_list (vector<VSTInfo *> *infos)
128 {
129         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
130                 vstfx_free_info(*i);
131         }
132         infos->clear();
133 }
134
135 static bool
136 vstfx_load_info_block(FILE* fp, VSTInfo *info)
137 {
138         if ((info->name = read_string(fp)) == 0) return false;
139         if ((info->creator = read_string(fp)) == 0) return false;
140         if (read_int (fp, &info->UniqueID)) return false;
141         if ((info->Category = read_string(fp)) == 0) return false;
142         if (read_int (fp, &info->numInputs)) return false;
143         if (read_int (fp, &info->numOutputs)) return false;
144         if (read_int (fp, &info->numParams)) return false;
145         if (read_int (fp, &info->wantMidi)) return false;
146         if (read_int (fp, &info->hasEditor)) return false;
147         if (read_int (fp, &info->canProcessReplacing)) return false;
148
149         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
150                 return false;
151         }
152
153         for (int i = 0; i < info->numParams; ++i) {
154                 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
155         }
156
157         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
158                 return false;
159         }
160
161         for (int i = 0; i < info->numParams; ++i) {
162                 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
163                         return false;
164                 }
165         }
166         return true;
167 }
168
169 static bool
170 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
171 {
172         VSTInfo *info;
173         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
174                 return false;
175         }
176         if (vstfx_load_info_block(fp, info)) {
177                 if (strncmp (info->Category, "Shell", 5)) {
178                         infos->push_back(info);
179                 } else {
180                         int plugin_cnt = 0;
181                         vstfx_free_info(info);
182                         if (read_int (fp, &plugin_cnt)) {
183                                 for (int i = 0; i < plugin_cnt; i++) {
184                                         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
185                                                 vstfx_clear_info_list(infos);
186                                                 return false;
187                                         }
188                                         if (vstfx_load_info_block(fp, info)) {
189                                                 infos->push_back(info);
190                                         } else {
191                                                 vstfx_free_info(info);
192                                                 vstfx_clear_info_list(infos);
193                                                 return false;
194                                         }
195                                 }
196                         } else {
197                                 return false; /* Bad file */
198                         }
199                 }
200                 return true;
201         }
202         vstfx_free_info(info);
203         vstfx_clear_info_list(infos);
204         return false;
205 }
206
207 static void
208 vstfx_write_info_block (FILE* fp, VSTInfo *info)
209 {
210         assert (info);
211         assert (fp);
212
213         fprintf (fp, "%s\n", info->name);
214         fprintf (fp, "%s\n", info->creator);
215         fprintf (fp, "%d\n", info->UniqueID);
216         fprintf (fp, "%s\n", info->Category);
217         fprintf (fp, "%d\n", info->numInputs);
218         fprintf (fp, "%d\n", info->numOutputs);
219         fprintf (fp, "%d\n", info->numParams);
220         fprintf (fp, "%d\n", info->wantMidi);
221         fprintf (fp, "%d\n", info->hasEditor);
222         fprintf (fp, "%d\n", info->canProcessReplacing);
223
224         for (int i = 0; i < info->numParams; i++) {
225                 fprintf (fp, "%s\n", info->ParamNames[i]);
226         }
227
228         for (int i = 0; i < info->numParams; i++) {
229                 fprintf (fp, "%s\n", info->ParamLabels[i]);
230         }
231 }
232
233 static void
234 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
235 {
236         assert(infos);
237         assert(fp);
238
239         if (infos->size() > 1) {
240                 vector<VSTInfo *>::iterator x = infos->begin();
241                 /* write out the shell info first along with count of the number of
242                  * plugins contained in this shell
243                  */
244                 vstfx_write_info_block(fp, *x);
245                 fprintf( fp, "%d\n", (int)infos->size() - 1 );
246                 ++x;
247                 /* Now write out the info for each plugin */
248                 for (; x != infos->end(); ++x) {
249                         vstfx_write_info_block(fp, *x);
250                 }
251         } else if (infos->size() == 1) {
252                 vstfx_write_info_block(fp, infos->front());
253         } else {
254                 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
255         }
256 }
257
258 static string
259 vstfx_blacklist_path (const char* dllpath, int personal)
260 {
261         string dir;
262         if (personal) {
263                 dir = get_personal_vst_blacklist_dir();
264         } else {
265                 dir = Glib::path_get_dirname (std::string(dllpath));
266         }
267
268         stringstream s;
269         s << "." << Glib::path_get_basename (dllpath) << ".fsb";
270         return Glib::build_filename (dir, s.str ());
271 }
272
273 /* return true if plugin is blacklisted or has an invalid file extension */
274 static bool
275 vstfx_blacklist_stat (const char *dllpath, int personal)
276 {
277         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
278                 return true;
279         }
280         string const path = vstfx_blacklist_path (dllpath, personal);
281
282         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
283                 struct stat dllstat;
284                 struct stat fsbstat;
285
286                 if (stat (dllpath, &dllstat) == 0 && stat (path.c_str(), &fsbstat) == 0) {
287                         if (dllstat.st_mtime > fsbstat.st_mtime) {
288                                 /* plugin is newer than blacklist file */
289                                 return true;
290                         }
291                 }
292                 /* stat failed or plugin is older than blacklist file */
293                 return true;
294         }
295         /* blacklist file does not exist */
296         return false;
297 }
298
299 static bool
300 vstfx_check_blacklist (const char *dllpath)
301 {
302         if (vstfx_blacklist_stat(dllpath, 0)) return true;
303         if (vstfx_blacklist_stat(dllpath, 1)) return true;
304         return false;
305 }
306
307 static FILE *
308 vstfx_blacklist_file (const char *dllpath)
309 {
310         FILE *f;
311         if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "w"))) {
312                 return f;
313         }
314         return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "w");
315 }
316
317 static bool
318 vstfx_blacklist (const char *dllpath)
319 {
320         FILE *f = vstfx_blacklist_file(dllpath);
321         if (f) {
322                 fclose(f);
323                 return true;
324         }
325         return false;
326 }
327
328 static void
329 vstfx_un_blacklist (const char *dllpath)
330 {
331         ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
332         ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
333 }
334
335 static string
336 vstfx_infofile_path (const char* dllpath, int personal)
337 {
338         string dir;
339         if (personal) {
340                 dir = get_personal_vst_info_cache_dir();
341         } else {
342                 dir = Glib::path_get_dirname (std::string(dllpath));
343         }
344
345         stringstream s;
346         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
347         return Glib::build_filename (dir, s.str ());
348 }
349
350 static char *
351 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
352 {
353         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
354                 return 0;
355         }
356
357         string const path = vstfx_infofile_path (dllpath, personal);
358
359         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
360
361                 struct stat dllstat;
362
363                 if (stat (dllpath, &dllstat) == 0) {
364                         if (stat (path.c_str(), statbuf) == 0) {
365                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
366                                         /* plugin is older than info file */
367                                         return strdup (path.c_str ());
368                                 }
369                         }
370                 }
371         }
372
373         return 0;
374 }
375
376
377 static FILE *
378 vstfx_infofile_for_read (const char* dllpath)
379 {
380         struct stat own_statbuf;
381         struct stat sys_statbuf;
382         FILE *rv = NULL;
383
384         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
385         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
386
387         if (own_info) {
388                 if (sys_info) {
389                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
390                                 /* system info file is newer, use it */
391                                 rv = g_fopen (sys_info, "rb");
392                         }
393                 } else {
394                         rv = g_fopen (own_info, "rb");
395                 }
396         } else if (sys_info) {
397                 rv = g_fopen (sys_info, "rb");
398         }
399         free(own_info);
400         free(sys_info);
401
402         return rv;
403 }
404
405 static FILE *
406 vstfx_infofile_create (const char* dllpath, int personal)
407 {
408         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
409                 return 0;
410         }
411
412         string const path = vstfx_infofile_path (dllpath, personal);
413         return fopen (path.c_str(), "w");
414 }
415
416 static FILE *
417 vstfx_infofile_for_write (const char* dllpath)
418 {
419         FILE* f;
420
421         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
422                 f = vstfx_infofile_create (dllpath, 1);
423         }
424
425         return f;
426 }
427
428 static
429 int vstfx_can_midi (VSTState* vstfx)
430 {
431         AEffect* plugin = vstfx->plugin;
432
433         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
434
435         if (vst_version >= 2) {
436                 /* should we send it VST events (i.e. MIDI) */
437
438                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
439                         return -1;
440                 }
441         }
442
443         return false;
444 }
445
446 static VSTInfo*
447 vstfx_parse_vst_state (VSTState* vstfx)
448 {
449         assert (vstfx);
450
451         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
452         if (!info) {
453                 return 0;
454         }
455
456         /*We need to init the creator because some plugins
457           fail to implement getVendorString, and so won't stuff the
458           string with any name*/
459
460         char creator[65] = "Unknown\0";
461
462         AEffect* plugin = vstfx->plugin;
463
464         info->name = strdup (vstfx->handle->name);
465
466         /*If the plugin doesn't bother to implement GetVendorString we will
467           have pre-stuffed the string with 'Unkown' */
468
469         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
470
471         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
472           so if its just a zero length string we replace it with 'Unknown' */
473
474         if (strlen(creator) == 0) {
475                 info->creator = strdup ("Unknown");
476         } else {
477                 info->creator = strdup (creator);
478         }
479
480
481         switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
482         {
483                 case kPlugCategEffect:         info->Category = strdup ("Effect"); break;
484                 case kPlugCategSynth:          info->Category = strdup ("Synth"); break;
485                 case kPlugCategAnalysis:       info->Category = strdup ("Anaylsis"); break;
486                 case kPlugCategMastering:      info->Category = strdup ("Mastering"); break;
487                 case kPlugCategSpacializer:    info->Category = strdup ("Spacializer"); break;
488                 case kPlugCategRoomFx:         info->Category = strdup ("RoomFx"); break;
489                 case kPlugSurroundFx:          info->Category = strdup ("SurroundFx"); break;
490                 case kPlugCategRestoration:    info->Category = strdup ("Restoration"); break;
491                 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
492                 case kPlugCategShell:          info->Category = strdup ("Shell"); break;
493                 case kPlugCategGenerator:      info->Category = strdup ("Generator"); break;
494                 default:                       info->Category = strdup ("Unknown"); break;
495         }
496
497         info->UniqueID = plugin->uniqueID;
498
499         info->numInputs = plugin->numInputs;
500         info->numOutputs = plugin->numOutputs;
501         info->numParams = plugin->numParams;
502         info->wantMidi = vstfx_can_midi(vstfx);
503         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
504         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
505         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
506         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
507
508         for (int i = 0; i < info->numParams; ++i) {
509                 char name[64];
510                 char label[64];
511
512                 /* Not all plugins give parameters labels as well as names */
513
514                 strcpy (name, "No Name");
515                 strcpy (label, "No Label");
516
517                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
518                 info->ParamNames[i] = strdup(name);
519
520                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
521                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
522                 info->ParamLabels[i] = strdup(label);
523         }
524         return info;
525 }
526
527 static void
528 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
529 {
530         assert(vstfx);
531         VSTInfo *info;
532
533         if ((info = vstfx_parse_vst_state(vstfx))) {
534                 infos->push_back(info);
535 #if 1
536                 /* If this plugin is a Shell and we are not already inside a shell plugin
537                  * read the info for all of the plugins contained in this shell.
538                  */
539                 if (!strncmp (info->Category, "Shell", 5)
540                     && vstfx->handle->plugincnt == 1) {
541                         int id;
542                         vector< pair<int, string> > ids;
543                         AEffect *plugin = vstfx->plugin;
544                         string path = vstfx->handle->path;
545
546                         do {
547                                 char name[65] = "Unknown\0";
548                                 id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
549                                 ids.push_back(std::make_pair(id, name));
550                         } while ( id != 0 );
551
552                         switch(type) {
553 #ifdef WINDOWS_VST_SUPPORT
554                                 case 1: fst_close(vstfx); break;
555 #endif
556 #ifdef LXVST_SUPPORT
557                                 case 2: vstfx_close (vstfx); break;
558 #endif
559                                 default: assert(0); break;
560                         }
561
562                         for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
563                                 id = (*x).first;
564                                 if (id == 0) continue;
565                                 /* recurse vstfx_get_info() */
566
567                                 bool ok;
568                                 switch (type) { // TODO use lib ardour's type
569 #ifdef WINDOWS_VST_SUPPORT
570                                         case 1:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
571 #endif
572 #ifdef LXVST_SUPPORT
573                                         case 2:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
574 #endif
575                                         default: ok = false;
576                                 }
577                                 if (ok) {
578                                         // One shell (some?, all?) does not report the actual plugin name
579                                         // even after the shelled plugin has been instantiated.
580                                         // Replace the name of the shell with the real name.
581                                         info = infos->back();
582                                         free (info->name);
583
584                                         if ((*x).second.length() == 0) {
585                                                 info->name = strdup("Unknown");
586                                         }
587                                         else {
588                                                 info->name = strdup ((*x).second.c_str());
589                                         }
590                                 }
591                         }
592                 }
593 #endif
594         }
595 }
596
597 /* A simple 'dummy' audiomaster callback which should be ok,
598    we will only be instantiating the plugin in order to get its info
599 */
600
601 static intptr_t
602 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
603 {
604         const char* vstfx_can_do_strings[] = {
605                 "supportShell",
606                 "shellCategory"
607         };
608         const int vstfx_can_do_string_count = 2;
609
610         if (opcode == audioMasterVersion) {
611                 return 2400;
612         }
613         else if (opcode == audioMasterCanDo) {
614                 for (int i = 0; i < vstfx_can_do_string_count; i++) {
615                         if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
616                                 return 1;
617                         }
618                 }
619                 return 0;
620         }
621         else if (opcode == audioMasterCurrentId) {
622                 return vstfx_current_loading_id;
623         }
624         else {
625                 return 0;
626         }
627 }
628
629 static bool
630 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
631 {
632         FILE* infofile;
633         bool rv = false;
634         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
635                 rv = vstfx_load_info_file(infofile, infos);
636                 fclose (infofile);
637                 if (!rv) {
638                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
639                 }
640         }
641         return rv;
642 }
643
644 #ifdef LXVST_SUPPORT
645 static bool
646 vstfx_instantiate_and_get_info_lx (
647                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
648 {
649         VSTHandle* h;
650         VSTState* vstfx;
651         if (!(h = vstfx_load(dllpath))) {
652                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
653                 return false;
654         }
655
656         vstfx_current_loading_id = uniqueID;
657
658         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
659                 vstfx_unload(h);
660                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
661                 return false;
662         }
663
664         vstfx_current_loading_id = 0;
665
666         vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
667
668         vstfx_close (vstfx);
669         vstfx_unload (h);
670         return true;
671 }
672 #endif
673
674 #ifdef WINDOWS_VST_SUPPORT
675 static bool
676 vstfx_instantiate_and_get_info_fst (
677                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
678 {
679         VSTHandle* h;
680         VSTState* vstfx;
681         if(!(h = fst_load(dllpath))) {
682                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
683                 return false;
684         }
685
686         vstfx_current_loading_id = uniqueID;
687
688         if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
689                 fst_unload(&h);
690                 vstfx_current_loading_id = 0;
691                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
692                 return false;
693         }
694         vstfx_current_loading_id = 0;
695
696         vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
697
698         fst_close(vstfx);
699         //fst_unload(&h); // XXX -> fst_close()
700         return true;
701 }
702 #endif
703
704 #ifndef VST_SCANNER_APP
705 static void parse_scanner_output (std::string msg, size_t /*len*/)
706 {
707         // TODO write to blacklist or error file..?
708         PBD::error << "VST scanner: " << msg;
709 }
710 #endif
711
712 static vector<VSTInfo *> *
713 vstfx_get_info (const char* dllpath, int type, enum VSTScanMode mode)
714 {
715         FILE* infofile;
716         vector<VSTInfo*> *infos = new vector<VSTInfo*>;
717
718         if (vstfx_check_blacklist(dllpath)) {
719                 return infos;
720         }
721
722         if (vstfx_get_info_from_file(dllpath, infos)) {
723                 return infos;
724         }
725
726 #ifndef VST_SCANNER_APP
727         std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
728
729         if (mode == VST_SCAN_CACHE_ONLY) {
730                 /* never scan explicitly, use cache only */
731                 return infos;
732         }
733         else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
734                 /* use external scanner app */
735
736                 char **argp= (char**) calloc(3,sizeof(char*));
737                 argp[0] = strdup(scanner_bin_path.c_str());
738                 argp[1] = strdup(dllpath);
739                 argp[2] = 0;
740
741                 PBD::SystemExec scanner (scanner_bin_path, argp);
742                 PBD::ScopedConnectionList cons;
743                 // TODO timeout.., and honor user-terminate
744                 //scanner->Terminated.connect_same_thread (cons, boost::bind (&scanner_terminated))
745                 scanner.ReadStdout.connect_same_thread (cons, boost::bind (&parse_scanner_output, _1 ,_2));
746                 if (scanner.start (2 /* send stderr&stdout via signal */)) {
747                         PBD::error << "Cannot launch VST scanner app '" << scanner_bin_path << "': "<< strerror(errno) << endmsg;
748                         return infos;
749                 } else {
750                         int timeout = PLUGIN_SCAN_TIMEOUT;
751                         while (scanner.is_running() && --timeout) {
752                                 ARDOUR::GUIIdle();
753                                 Glib::usleep (100000);
754                                 if (ARDOUR::PluginManager::instance().cancelled()) break;
755                         }
756                         scanner.terminate();
757                 }
758                 /* re-read index */
759                 vstfx_clear_info_list(infos);
760                 if (!vstfx_check_blacklist(dllpath)) {
761                         vstfx_get_info_from_file(dllpath, infos);
762                 }
763                 return infos;
764         }
765         /* else .. instantiate and check in in ardour process itself */
766 #else
767         (void) mode; // unused parameter
768 #endif
769
770         bool ok;
771         /* blacklist in case instantiation fails */
772         vstfx_blacklist(dllpath);
773
774         switch (type) { // TODO use lib ardour's type
775 #ifdef WINDOWS_VST_SUPPORT
776                 case 1:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
777 #endif
778 #ifdef LXVST_SUPPORT
779                 case 2:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
780 #endif
781                 default: ok = false;
782         }
783
784         if (!ok) {
785                 return infos;
786         }
787
788         /* remove from blacklist */
789         vstfx_un_blacklist(dllpath);
790
791         /* crate cache/whitelist */
792         infofile = vstfx_infofile_for_write (dllpath);
793         if (!infofile) {
794                 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
795                 return infos;
796         } else {
797                 vstfx_write_info_file (infofile, infos);
798                 fclose (infofile);
799         }
800         return infos;
801 }
802
803 /* *** public API *** */
804
805 void
806 vstfx_free_info_list (vector<VSTInfo *> *infos)
807 {
808         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
809                 vstfx_free_info(*i);
810         }
811         delete infos;
812 }
813
814 string
815 get_personal_vst_blacklist_dir() {
816         string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
817         /* if the directory doesn't exist, try to create it */
818         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
819                 if (g_mkdir (dir.c_str (), 0700)) {
820                         PBD::error << "Cannt create VST cache folder '" << dir << "'" << endmsg;
821                         //exit(1);
822                 }
823         }
824         return dir;
825 }
826
827 string
828 get_personal_vst_info_cache_dir() {
829         string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
830         /* if the directory doesn't exist, try to create it */
831         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
832                 if (g_mkdir (dir.c_str (), 0700)) {
833                         PBD::error << "Cannt create VST info folder '" << dir << "'" << endmsg;
834                         //exit(1);
835                 }
836         }
837         return dir;
838 }
839
840 #ifdef LXVST_SUPPORT
841 vector<VSTInfo *> *
842 vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
843 {
844         return vstfx_get_info(dllpath, 2, mode);
845 }
846 #endif
847
848 #ifdef WINDOWS_VST_SUPPORT
849 vector<VSTInfo *> *
850 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
851 {
852         return vstfx_get_info(dllpath, 1, mode);
853 }
854 #endif