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