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