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