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