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