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