break out ARDOUR::PluginType
[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 using namespace std;
59
60 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
61 //       consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
62
63 /* prototypes */
64 #ifdef WINDOWS_VST_SUPPORT
65 #include <fst.h>
66 static bool
67 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
68 #endif
69
70 #ifdef LXVST_SUPPORT
71 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
72 #endif
73
74 static int vstfx_current_loading_id = 0;
75
76 static char *
77 read_string (FILE *fp)
78 {
79         char buf[MAX_STRING_LEN];
80
81         if (!fgets (buf, MAX_STRING_LEN, fp)) {
82                 return 0;
83         }
84
85         if (strlen(buf) < MAX_STRING_LEN) {
86                 if (strlen (buf)) {
87                         buf[strlen(buf)-1] = 0;
88                 }
89                 return strdup (buf);
90         } else {
91                 return 0;
92         }
93 }
94
95 /** Read an integer value from a line in fp into n,
96  *  @return true on failure, false on success.
97  */
98 static bool
99 read_int (FILE* fp, int* n)
100 {
101         char buf[MAX_STRING_LEN];
102
103         char* p = fgets (buf, MAX_STRING_LEN, fp);
104         if (p == 0) {
105                 return true;
106         }
107
108         return (sscanf (p, "%d", n) != 1);
109 }
110
111 static void
112 vstfx_free_info (VSTInfo *info)
113 {
114         for (int i = 0; i < info->numParams; i++) {
115                 free (info->ParamNames[i]);
116                 free (info->ParamLabels[i]);
117         }
118
119         free (info->name);
120         free (info->creator);
121         free (info->Category);
122         free (info->ParamNames);
123         free (info->ParamLabels);
124         free (info);
125 }
126
127 static void
128 vstfx_clear_info_list (vector<VSTInfo *> *infos)
129 {
130         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
131                 vstfx_free_info(*i);
132         }
133         infos->clear();
134 }
135
136 static bool
137 vstfx_load_info_block(FILE* fp, VSTInfo *info)
138 {
139         if ((info->name = read_string(fp)) == 0) return false;
140         if ((info->creator = read_string(fp)) == 0) return false;
141         if (read_int (fp, &info->UniqueID)) return false;
142         if ((info->Category = read_string(fp)) == 0) return false;
143         if (read_int (fp, &info->numInputs)) return false;
144         if (read_int (fp, &info->numOutputs)) return false;
145         if (read_int (fp, &info->numParams)) return false;
146         if (read_int (fp, &info->wantMidi)) return false;
147         if (read_int (fp, &info->hasEditor)) return false;
148         if (read_int (fp, &info->canProcessReplacing)) return false;
149
150         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
151                 return false;
152         }
153
154         for (int i = 0; i < info->numParams; ++i) {
155                 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
156         }
157
158         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
159                 return false;
160         }
161
162         for (int i = 0; i < info->numParams; ++i) {
163                 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
164                         return false;
165                 }
166         }
167         return true;
168 }
169
170 static bool
171 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
172 {
173         VSTInfo *info;
174         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
175                 return false;
176         }
177         if (vstfx_load_info_block(fp, info)) {
178                 if (strncmp (info->Category, "Shell", 5)) {
179                         infos->push_back(info);
180                 } else {
181                         int plugin_cnt = 0;
182                         vstfx_free_info(info);
183                         if (read_int (fp, &plugin_cnt)) {
184                                 for (int i = 0; i < plugin_cnt; i++) {
185                                         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
186                                                 vstfx_clear_info_list(infos);
187                                                 return false;
188                                         }
189                                         if (vstfx_load_info_block(fp, info)) {
190                                                 infos->push_back(info);
191                                         } else {
192                                                 vstfx_free_info(info);
193                                                 vstfx_clear_info_list(infos);
194                                                 return false;
195                                         }
196                                 }
197                         } else {
198                                 return false; /* Bad file */
199                         }
200                 }
201                 return true;
202         }
203         vstfx_free_info(info);
204         vstfx_clear_info_list(infos);
205         return false;
206 }
207
208 static void
209 vstfx_write_info_block (FILE* fp, VSTInfo *info)
210 {
211         assert (info);
212         assert (fp);
213
214         fprintf (fp, "%s\n", info->name);
215         fprintf (fp, "%s\n", info->creator);
216         fprintf (fp, "%d\n", info->UniqueID);
217         fprintf (fp, "%s\n", info->Category);
218         fprintf (fp, "%d\n", info->numInputs);
219         fprintf (fp, "%d\n", info->numOutputs);
220         fprintf (fp, "%d\n", info->numParams);
221         fprintf (fp, "%d\n", info->wantMidi);
222         fprintf (fp, "%d\n", info->hasEditor);
223         fprintf (fp, "%d\n", info->canProcessReplacing);
224
225         for (int i = 0; i < info->numParams; i++) {
226                 fprintf (fp, "%s\n", info->ParamNames[i]);
227         }
228
229         for (int i = 0; i < info->numParams; i++) {
230                 fprintf (fp, "%s\n", info->ParamLabels[i]);
231         }
232 }
233
234 static void
235 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
236 {
237         assert(infos);
238         assert(fp);
239
240         if (infos->size() > 1) {
241                 vector<VSTInfo *>::iterator x = infos->begin();
242                 /* write out the shell info first along with count of the number of
243                  * plugins contained in this shell
244                  */
245                 vstfx_write_info_block(fp, *x);
246                 fprintf( fp, "%d\n", (int)infos->size() - 1 );
247                 ++x;
248                 /* Now write out the info for each plugin */
249                 for (; x != infos->end(); ++x) {
250                         vstfx_write_info_block(fp, *x);
251                 }
252         } else if (infos->size() == 1) {
253                 vstfx_write_info_block(fp, infos->front());
254         } else {
255                 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
256         }
257 }
258
259 static string
260 vstfx_blacklist_path (const char* dllpath, int personal)
261 {
262         string dir;
263         if (personal) {
264                 dir = get_personal_vst_blacklist_dir();
265         } else {
266                 dir = Glib::path_get_dirname (std::string(dllpath));
267         }
268
269         stringstream s;
270         s << "." << Glib::path_get_basename (dllpath) << ".fsb";
271         return Glib::build_filename (dir, s.str ());
272 }
273
274 /* return true if plugin is blacklisted or has an invalid file extension */
275 static bool
276 vstfx_blacklist_stat (const char *dllpath, int personal)
277 {
278         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
279                 return true;
280         }
281         string const path = vstfx_blacklist_path (dllpath, personal);
282
283         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
284                 struct stat dllstat;
285                 struct stat fsbstat;
286
287                 if (stat (dllpath, &dllstat) == 0 && stat (path.c_str(), &fsbstat) == 0) {
288                         if (dllstat.st_mtime > fsbstat.st_mtime) {
289                                 /* plugin is newer than blacklist file */
290                                 return true;
291                         }
292                 }
293                 /* stat failed or plugin is older than blacklist file */
294                 return true;
295         }
296         /* blacklist file does not exist */
297         return false;
298 }
299
300 static bool
301 vstfx_check_blacklist (const char *dllpath)
302 {
303         if (vstfx_blacklist_stat(dllpath, 0)) return true;
304         if (vstfx_blacklist_stat(dllpath, 1)) return true;
305         return false;
306 }
307
308 static FILE *
309 vstfx_blacklist_file (const char *dllpath)
310 {
311         FILE *f;
312         if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "w"))) {
313                 return f;
314         }
315         return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "w");
316 }
317
318 static bool
319 vstfx_blacklist (const char *dllpath)
320 {
321         FILE *f = vstfx_blacklist_file(dllpath);
322         if (f) {
323                 fclose(f);
324                 return true;
325         }
326         return false;
327 }
328
329 static void
330 vstfx_un_blacklist (const char *dllpath)
331 {
332         ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
333         ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
334 }
335
336 static string
337 vstfx_infofile_path (const char* dllpath, int personal)
338 {
339         string dir;
340         if (personal) {
341                 dir = get_personal_vst_info_cache_dir();
342         } else {
343                 dir = Glib::path_get_dirname (std::string(dllpath));
344         }
345
346         stringstream s;
347         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
348         return Glib::build_filename (dir, s.str ());
349 }
350
351 static void
352 vstfx_remove_infofile (const char *dllpath)
353 {
354         ::g_unlink(vstfx_infofile_path (dllpath, 0).c_str());
355         ::g_unlink(vstfx_infofile_path (dllpath, 1).c_str());
356 }
357
358 static char *
359 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
360 {
361         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
362                 return 0;
363         }
364
365         string const path = vstfx_infofile_path (dllpath, personal);
366
367         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
368
369                 struct stat dllstat;
370
371                 if (stat (dllpath, &dllstat) == 0) {
372                         if (stat (path.c_str(), statbuf) == 0) {
373                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
374                                         /* plugin is older than info file */
375                                         return strdup (path.c_str ());
376                                 }
377                         }
378                 }
379         }
380
381         return 0;
382 }
383
384
385 static FILE *
386 vstfx_infofile_for_read (const char* dllpath)
387 {
388         struct stat own_statbuf;
389         struct stat sys_statbuf;
390         FILE *rv = NULL;
391
392         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
393         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
394
395         if (own_info) {
396                 if (sys_info) {
397                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
398                                 /* system info file is newer, use it */
399                                 rv = g_fopen (sys_info, "rb");
400                         }
401                 } else {
402                         rv = g_fopen (own_info, "rb");
403                 }
404         } else if (sys_info) {
405                 rv = g_fopen (sys_info, "rb");
406         }
407         free(own_info);
408         free(sys_info);
409
410         return rv;
411 }
412
413 static FILE *
414 vstfx_infofile_create (const char* dllpath, int personal)
415 {
416         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
417                 return 0;
418         }
419
420         string const path = vstfx_infofile_path (dllpath, personal);
421         return fopen (path.c_str(), "w");
422 }
423
424 static FILE *
425 vstfx_infofile_for_write (const char* dllpath)
426 {
427         FILE* f;
428
429         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
430                 f = vstfx_infofile_create (dllpath, 1);
431         }
432
433         return f;
434 }
435
436 static
437 int vstfx_can_midi (VSTState* vstfx)
438 {
439         AEffect* plugin = vstfx->plugin;
440
441         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
442
443         if (vst_version >= 2) {
444                 /* should we send it VST events (i.e. MIDI) */
445
446                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
447                         return -1;
448                 }
449         }
450
451         return false;
452 }
453
454 static VSTInfo*
455 vstfx_parse_vst_state (VSTState* vstfx)
456 {
457         assert (vstfx);
458
459         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
460         if (!info) {
461                 return 0;
462         }
463
464         /*We need to init the creator because some plugins
465           fail to implement getVendorString, and so won't stuff the
466           string with any name*/
467
468         char creator[65] = "Unknown\0";
469
470         AEffect* plugin = vstfx->plugin;
471
472         info->name = strdup (vstfx->handle->name);
473
474         /*If the plugin doesn't bother to implement GetVendorString we will
475           have pre-stuffed the string with 'Unkown' */
476
477         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
478
479         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
480           so if its just a zero length string we replace it with 'Unknown' */
481
482         if (strlen(creator) == 0) {
483                 info->creator = strdup ("Unknown");
484         } else {
485                 info->creator = strdup (creator);
486         }
487
488
489         switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
490         {
491                 case kPlugCategEffect:         info->Category = strdup ("Effect"); break;
492                 case kPlugCategSynth:          info->Category = strdup ("Synth"); break;
493                 case kPlugCategAnalysis:       info->Category = strdup ("Anaylsis"); break;
494                 case kPlugCategMastering:      info->Category = strdup ("Mastering"); break;
495                 case kPlugCategSpacializer:    info->Category = strdup ("Spacializer"); break;
496                 case kPlugCategRoomFx:         info->Category = strdup ("RoomFx"); break;
497                 case kPlugSurroundFx:          info->Category = strdup ("SurroundFx"); break;
498                 case kPlugCategRestoration:    info->Category = strdup ("Restoration"); break;
499                 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
500                 case kPlugCategShell:          info->Category = strdup ("Shell"); break;
501                 case kPlugCategGenerator:      info->Category = strdup ("Generator"); break;
502                 default:                       info->Category = strdup ("Unknown"); break;
503         }
504
505         info->UniqueID = plugin->uniqueID;
506
507         info->numInputs = plugin->numInputs;
508         info->numOutputs = plugin->numOutputs;
509         info->numParams = plugin->numParams;
510         info->wantMidi = vstfx_can_midi(vstfx);
511         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
512         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
513         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
514         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
515
516         for (int i = 0; i < info->numParams; ++i) {
517                 char name[64];
518                 char label[64];
519
520                 /* Not all plugins give parameters labels as well as names */
521
522                 strcpy (name, "No Name");
523                 strcpy (label, "No Label");
524
525                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
526                 info->ParamNames[i] = strdup(name);
527
528                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
529                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
530                 info->ParamLabels[i] = strdup(label);
531         }
532         return info;
533 }
534
535 static void
536 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, enum ARDOUR::PluginType type)
537 {
538         assert(vstfx);
539         VSTInfo *info;
540
541         if ((info = vstfx_parse_vst_state(vstfx))) {
542                 infos->push_back(info);
543 #if 1
544                 /* If this plugin is a Shell and we are not already inside a shell plugin
545                  * read the info for all of the plugins contained in this shell.
546                  */
547                 if (!strncmp (info->Category, "Shell", 5)
548                     && vstfx->handle->plugincnt == 1) {
549                         int id;
550                         vector< pair<int, string> > ids;
551                         AEffect *plugin = vstfx->plugin;
552                         string path = vstfx->handle->path;
553
554                         do {
555                                 char name[65] = "Unknown\0";
556                                 id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
557                                 ids.push_back(std::make_pair(id, name));
558                         } while ( id != 0 );
559
560                         switch(type) {
561 #ifdef WINDOWS_VST_SUPPORT
562                                 case 1: fst_close(vstfx); break;
563 #endif
564 #ifdef LXVST_SUPPORT
565                                 case 2: vstfx_close (vstfx); break;
566 #endif
567                                 default: assert(0); break;
568                         }
569
570                         for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
571                                 id = (*x).first;
572                                 if (id == 0) continue;
573                                 /* recurse vstfx_get_info() */
574
575                                 bool ok;
576                                 switch (type) { // TODO use lib ardour's type
577 #ifdef WINDOWS_VST_SUPPORT
578                                         case ARDOUR::Windows_VST:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
579 #endif
580 #ifdef LXVST_SUPPORT
581                                         case ARDOUR::LXVST:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
582 #endif
583                                         default: ok = false;
584                                 }
585                                 if (ok) {
586                                         // One shell (some?, all?) does not report the actual plugin name
587                                         // even after the shelled plugin has been instantiated.
588                                         // Replace the name of the shell with the real name.
589                                         info = infos->back();
590                                         free (info->name);
591
592                                         if ((*x).second.length() == 0) {
593                                                 info->name = strdup("Unknown");
594                                         }
595                                         else {
596                                                 info->name = strdup ((*x).second.c_str());
597                                         }
598                                 }
599                         }
600                 }
601 #endif
602         }
603 }
604
605 /* A simple 'dummy' audiomaster callback which should be ok,
606    we will only be instantiating the plugin in order to get its info
607 */
608
609 static intptr_t
610 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
611 {
612         const char* vstfx_can_do_strings[] = {
613                 "supportShell",
614                 "shellCategory"
615         };
616         const int vstfx_can_do_string_count = 2;
617
618         if (opcode == audioMasterVersion) {
619                 return 2400;
620         }
621         else if (opcode == audioMasterCanDo) {
622                 for (int i = 0; i < vstfx_can_do_string_count; i++) {
623                         if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
624                                 return 1;
625                         }
626                 }
627                 return 0;
628         }
629         else if (opcode == audioMasterCurrentId) {
630                 return vstfx_current_loading_id;
631         }
632         else {
633                 return 0;
634         }
635 }
636
637 static bool
638 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
639 {
640         FILE* infofile;
641         bool rv = false;
642         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
643                 rv = vstfx_load_info_file(infofile, infos);
644                 fclose (infofile);
645                 if (!rv) {
646                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
647                 }
648         }
649         return rv;
650 }
651
652 #ifdef LXVST_SUPPORT
653 static bool
654 vstfx_instantiate_and_get_info_lx (
655                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
656 {
657         VSTHandle* h;
658         VSTState* vstfx;
659         if (!(h = vstfx_load(dllpath))) {
660                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
661                 return false;
662         }
663
664         vstfx_current_loading_id = uniqueID;
665
666         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
667                 vstfx_unload(h);
668                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
669                 return false;
670         }
671
672         vstfx_current_loading_id = 0;
673
674         vstfx_info_from_plugin(dllpath, vstfx, infos, ARDOUR::LXVST);
675
676         vstfx_close (vstfx);
677         vstfx_unload (h);
678         return true;
679 }
680 #endif
681
682 #ifdef WINDOWS_VST_SUPPORT
683 static bool
684 vstfx_instantiate_and_get_info_fst (
685                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
686 {
687         VSTHandle* h;
688         VSTState* vstfx;
689         if(!(h = fst_load(dllpath))) {
690                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
691                 return false;
692         }
693
694         vstfx_current_loading_id = uniqueID;
695
696         if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
697                 fst_unload(&h);
698                 vstfx_current_loading_id = 0;
699                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
700                 return false;
701         }
702         vstfx_current_loading_id = 0;
703
704         vstfx_info_from_plugin(dllpath, vstfx, infos, ARDOUR::Windows_VST);
705
706         fst_close(vstfx);
707         //fst_unload(&h); // XXX -> fst_close()
708         return true;
709 }
710 #endif
711
712 #ifndef VST_SCANNER_APP
713 static void parse_scanner_output (std::string msg, size_t /*len*/)
714 {
715         // TODO write to blacklist or error file..?
716         PBD::error << "VST scanner: " << msg;
717 }
718 #endif
719
720 static vector<VSTInfo *> *
721 vstfx_get_info (const char* dllpath, enum ARDOUR::PluginType type, enum VSTScanMode mode)
722 {
723         FILE* infofile;
724         vector<VSTInfo*> *infos = new vector<VSTInfo*>;
725
726         if (vstfx_check_blacklist(dllpath)) {
727                 return infos;
728         }
729
730         if (vstfx_get_info_from_file(dllpath, infos)) {
731                 return infos;
732         }
733
734 #ifndef VST_SCANNER_APP
735         std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
736
737         if (mode == VST_SCAN_CACHE_ONLY) {
738                 /* never scan explicitly, use cache only */
739                 return infos;
740         }
741         else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
742                 /* use external scanner app */
743
744                 char **argp= (char**) calloc(3,sizeof(char*));
745                 argp[0] = strdup(scanner_bin_path.c_str());
746                 argp[1] = strdup(dllpath);
747                 argp[2] = 0;
748
749                 PBD::SystemExec scanner (scanner_bin_path, argp);
750                 PBD::ScopedConnectionList cons;
751                 scanner.ReadStdout.connect_same_thread (cons, boost::bind (&parse_scanner_output, _1 ,_2));
752                 if (scanner.start (2 /* send stderr&stdout via signal */)) {
753                         PBD::error << "Cannot launch VST scanner app '" << scanner_bin_path << "': "<< strerror(errno) << endmsg;
754                         return infos;
755                 } else {
756                         int timeout = PLUGIN_SCAN_TIMEOUT;
757                         while (scanner.is_running() && --timeout) {
758                                 ARDOUR::GUIIdle();
759                                 Glib::usleep (100000);
760
761                                 if (ARDOUR::PluginManager::instance().cancelled()) {
762                                         // remove info file (might be incomplete)
763                                         vstfx_remove_infofile(dllpath);
764                                         // remove temporary blacklist file (scan incomplete)
765                                         vstfx_un_blacklist(dllpath);
766                                         scanner.terminate();
767                                         return infos;
768                                 }
769                         }
770                         scanner.terminate();
771                 }
772                 /* re-read index (generated by external scanner) */
773                 vstfx_clear_info_list(infos);
774                 if (!vstfx_check_blacklist(dllpath)) {
775                         vstfx_get_info_from_file(dllpath, infos);
776                 }
777                 return infos;
778         }
779         /* else .. instantiate and check in in ardour process itself */
780 #else
781         (void) mode; // unused parameter
782 #endif
783
784         bool ok;
785         /* blacklist in case instantiation fails */
786         vstfx_blacklist(dllpath);
787
788         switch (type) {
789 #ifdef WINDOWS_VST_SUPPORT
790                 case ARDOUR::Windows_VST:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
791 #endif
792 #ifdef LXVST_SUPPORT
793                 case ARDOUR::LXVST:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
794 #endif
795                 default: ok = false;
796         }
797
798         if (!ok) {
799                 return infos;
800         }
801
802         /* remove from blacklist */
803         vstfx_un_blacklist(dllpath);
804
805         /* crate cache/whitelist */
806         infofile = vstfx_infofile_for_write (dllpath);
807         if (!infofile) {
808                 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
809                 return infos;
810         } else {
811                 vstfx_write_info_file (infofile, infos);
812                 fclose (infofile);
813         }
814         return infos;
815 }
816
817 /* *** public API *** */
818
819 void
820 vstfx_free_info_list (vector<VSTInfo *> *infos)
821 {
822         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
823                 vstfx_free_info(*i);
824         }
825         delete infos;
826 }
827
828 string
829 get_personal_vst_blacklist_dir() {
830         string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
831         /* if the directory doesn't exist, try to create it */
832         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
833                 if (g_mkdir (dir.c_str (), 0700)) {
834                         PBD::error << "Cannot create VST blacklist folder '" << dir << "'" << endmsg;
835                         //exit(1);
836                 }
837         }
838         return dir;
839 }
840
841 string
842 get_personal_vst_info_cache_dir() {
843         string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
844         /* if the directory doesn't exist, try to create it */
845         if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
846                 if (g_mkdir (dir.c_str (), 0700)) {
847                         PBD::error << "Cannot create VST info folder '" << dir << "'" << endmsg;
848                         //exit(1);
849                 }
850         }
851         return dir;
852 }
853
854 #ifdef LXVST_SUPPORT
855 vector<VSTInfo *> *
856 vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
857 {
858         return vstfx_get_info(dllpath, ARDOUR::LXVST, mode);
859 }
860 #endif
861
862 #ifdef WINDOWS_VST_SUPPORT
863 vector<VSTInfo *> *
864 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
865 {
866         return vstfx_get_info(dllpath, ARDOUR::Windows_VST, mode);
867 }
868 #endif