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