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