2 Copyright (C) 2012-2014 Paul Davis
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.
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.
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.
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.
28 #include <sys/types.h>
40 #include <glib/gstdio.h>
43 #include "pbd/error.h"
45 #ifndef VST_SCANNER_APP
46 #include "pbd/system_exec.h"
47 #include "ardour/plugin_manager.h" // scanner_bin_path
50 #include "ardour/filesystem_paths.h"
51 #include "ardour/linux_vst_support.h"
52 #include "ardour/vst_info_file.h"
54 #define MAX_STRING_LEN 256
58 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
59 // consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
62 #ifdef WINDOWS_VST_SUPPORT
65 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
69 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
72 static int vstfx_current_loading_id = 0;
75 read_string (FILE *fp)
77 char buf[MAX_STRING_LEN];
79 if (!fgets (buf, MAX_STRING_LEN, fp)) {
83 if (strlen(buf) < MAX_STRING_LEN) {
85 buf[strlen(buf)-1] = 0;
93 /** Read an integer value from a line in fp into n,
94 * @return true on failure, false on success.
97 read_int (FILE* fp, int* n)
99 char buf[MAX_STRING_LEN];
101 char* p = fgets (buf, MAX_STRING_LEN, fp);
106 return (sscanf (p, "%d", n) != 1);
110 vstfx_free_info (VSTInfo *info)
112 for (int i = 0; i < info->numParams; i++) {
113 free (info->ParamNames[i]);
114 free (info->ParamLabels[i]);
118 free (info->creator);
119 free (info->Category);
120 free (info->ParamNames);
121 free (info->ParamLabels);
126 vstfx_clear_info_list (vector<VSTInfo *> *infos)
128 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
135 vstfx_load_info_block(FILE* fp, VSTInfo *info)
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;
148 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
152 for (int i = 0; i < info->numParams; ++i) {
153 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
156 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
160 for (int i = 0; i < info->numParams; ++i) {
161 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
169 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
172 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
175 if (vstfx_load_info_block(fp, info)) {
176 if (strncmp (info->Category, "Shell", 5)) {
177 infos->push_back(info);
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);
187 if (vstfx_load_info_block(fp, info)) {
188 infos->push_back(info);
190 vstfx_free_info(info);
191 vstfx_clear_info_list(infos);
196 return false; /* Bad file */
201 vstfx_free_info(info);
202 vstfx_clear_info_list(infos);
207 vstfx_write_info_block (FILE* fp, VSTInfo *info)
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);
223 for (int i = 0; i < info->numParams; i++) {
224 fprintf (fp, "%s\n", info->ParamNames[i]);
227 for (int i = 0; i < info->numParams; i++) {
228 fprintf (fp, "%s\n", info->ParamLabels[i]);
233 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
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
243 vstfx_write_info_block(fp, *x);
244 fprintf( fp, "%d\n", (int)infos->size() - 1 );
246 /* Now write out the info for each plugin */
247 for (; x != infos->end(); ++x) {
248 vstfx_write_info_block(fp, *x);
250 } else if (infos->size() == 1) {
251 vstfx_write_info_block(fp, infos->front());
253 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
258 vstfx_blacklist_path (const char* dllpath, int personal)
262 dir = get_personal_vst_blacklist_dir();
264 dir = Glib::path_get_dirname (std::string(dllpath));
268 s << "." << Glib::path_get_basename (dllpath) << ".fsb";
269 return Glib::build_filename (dir, s.str ());
272 /* return true if plugin is blacklisted or has an invalid file extension */
274 vstfx_blacklist_stat (const char *dllpath, int personal)
276 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
279 string const path = vstfx_blacklist_path (dllpath, personal);
281 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
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 */
291 /* stat failed or plugin is older than blacklist file */
294 /* blacklist file does not exist */
299 vstfx_check_blacklist (const char *dllpath)
301 if (vstfx_blacklist_stat(dllpath, 0)) return true;
302 if (vstfx_blacklist_stat(dllpath, 1)) return true;
307 vstfx_blacklist_file (const char *dllpath)
310 if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "w"))) {
313 return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "w");
317 vstfx_blacklist (const char *dllpath)
319 FILE *f = vstfx_blacklist_file(dllpath);
328 vstfx_un_blacklist (const char *dllpath)
330 ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
331 ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
335 vstfx_infofile_path (const char* dllpath, int personal)
339 dir = get_personal_vst_info_cache_dir();
341 dir = Glib::path_get_dirname (std::string(dllpath));
345 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
346 return Glib::build_filename (dir, s.str ());
350 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
352 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
356 string const path = vstfx_infofile_path (dllpath, personal);
358 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
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 ());
377 vstfx_infofile_for_read (const char* dllpath)
379 struct stat own_statbuf;
380 struct stat sys_statbuf;
383 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
384 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
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");
393 rv = g_fopen (own_info, "rb");
395 } else if (sys_info) {
396 rv = g_fopen (sys_info, "rb");
405 vstfx_infofile_create (const char* dllpath, int personal)
407 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
411 string const path = vstfx_infofile_path (dllpath, personal);
412 return fopen (path.c_str(), "w");
416 vstfx_infofile_for_write (const char* dllpath)
420 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
421 f = vstfx_infofile_create (dllpath, 1);
428 int vstfx_can_midi (VSTState* vstfx)
430 AEffect* plugin = vstfx->plugin;
432 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
434 if (vst_version >= 2) {
435 /* should we send it VST events (i.e. MIDI) */
437 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
446 vstfx_parse_vst_state (VSTState* vstfx)
450 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
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*/
459 char creator[65] = "Unknown\0";
461 AEffect* plugin = vstfx->plugin;
463 info->name = strdup (vstfx->handle->name);
465 /*If the plugin doesn't bother to implement GetVendorString we will
466 have pre-stuffed the string with 'Unkown' */
468 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
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' */
473 if (strlen(creator) == 0) {
474 info->creator = strdup ("Unknown");
476 info->creator = strdup (creator);
480 switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
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;
496 info->UniqueID = plugin->uniqueID;
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);
507 for (int i = 0; i < info->numParams; ++i) {
511 /* Not all plugins give parameters labels as well as names */
513 strcpy (name, "No Name");
514 strcpy (label, "No Label");
516 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
517 info->ParamNames[i] = strdup(name);
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);
527 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
532 if ((info = vstfx_parse_vst_state(vstfx))) {
533 infos->push_back(info);
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.
538 if (!strncmp (info->Category, "Shell", 5)
539 && vstfx->handle->plugincnt == 1) {
541 vector< pair<int, string> > ids;
542 AEffect *plugin = vstfx->plugin;
543 string path = vstfx->handle->path;
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));
552 #ifdef WINDOWS_VST_SUPPORT
553 case 1: fst_close(vstfx); break;
556 case 2: vstfx_close (vstfx); break;
558 default: assert(0); break;
561 for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
563 if (id == 0) continue;
564 /* recurse vstfx_get_info() */
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;
572 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
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();
583 if ((*x).second.length() == 0) {
584 info->name = strdup("Unknown");
587 info->name = strdup ((*x).second.c_str());
596 /* A simple 'dummy' audiomaster callback which should be ok,
597 we will only be instantiating the plugin in order to get its info
601 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
603 const char* vstfx_can_do_strings[] = {
607 const int vstfx_can_do_string_count = 2;
609 if (opcode == audioMasterVersion) {
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)) {
620 else if (opcode == audioMasterCurrentId) {
621 return vstfx_current_loading_id;
629 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
633 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
634 rv = vstfx_load_info_file(infofile, infos);
637 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
645 vstfx_instantiate_and_get_info_lx (
646 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
650 if (!(h = vstfx_load(dllpath))) {
651 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
655 vstfx_current_loading_id = uniqueID;
657 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
659 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
663 vstfx_current_loading_id = 0;
665 vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
673 #ifdef WINDOWS_VST_SUPPORT
675 vstfx_instantiate_and_get_info_fst (
676 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
680 if(!(h = fst_load(dllpath))) {
681 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
685 vstfx_current_loading_id = uniqueID;
687 if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
689 vstfx_current_loading_id = 0;
690 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
693 vstfx_current_loading_id = 0;
695 vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
698 //fst_unload(&h); // XXX -> fst_close()
703 #ifndef VST_SCANNER_APP
704 static void parse_scanner_output (std::string msg, size_t /*len*/)
706 // TODO write to blacklist or error file..?
707 PBD::error << "VST scanner: " << msg;
711 static vector<VSTInfo *> *
712 vstfx_get_info (const char* dllpath, int type, enum VSTScanMode mode)
715 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
717 if (vstfx_check_blacklist(dllpath)) {
721 if (vstfx_get_info_from_file(dllpath, infos)) {
725 #ifndef VST_SCANNER_APP
726 std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
728 if (mode == VST_SCAN_CACHE_ONLY) {
729 /* never scan explicitly, use cache only */
732 else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
733 /* use external scanner app */
735 char **argp= (char**) calloc(3,sizeof(char*));
736 argp[0] = strdup(scanner_bin_path.c_str());
737 argp[1] = strdup(dllpath);
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;
749 // TODO idle loop (emit signal to GUI to call gtk_main_iteration()) check cancel.
753 vstfx_clear_info_list(infos);
754 if (!vstfx_check_blacklist(dllpath)) {
755 vstfx_get_info_from_file(dllpath, infos);
759 /* else .. instantiate and check in in ardour process itself */
761 (void) mode; // unused parameter
765 /* blacklist in case instantiation fails */
766 vstfx_blacklist(dllpath);
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;
773 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
782 /* remove from blacklist */
783 vstfx_un_blacklist(dllpath);
785 /* crate cache/whitelist */
786 infofile = vstfx_infofile_for_write (dllpath);
788 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
791 vstfx_write_info_file (infofile, infos);
797 /* *** public API *** */
800 vstfx_free_info_list (vector<VSTInfo *> *infos)
802 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
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;
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;
836 vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
838 return vstfx_get_info(dllpath, 2, mode);
842 #ifdef WINDOWS_VST_SUPPORT
844 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
846 return vstfx_get_info(dllpath, 1, mode);