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
55 #define PLUGIN_SCAN_TIMEOUT (600) // in deciseconds
59 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
60 // consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
63 #ifdef WINDOWS_VST_SUPPORT
66 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
70 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
73 static int vstfx_current_loading_id = 0;
76 read_string (FILE *fp)
78 char buf[MAX_STRING_LEN];
80 if (!fgets (buf, MAX_STRING_LEN, fp)) {
84 if (strlen(buf) < MAX_STRING_LEN) {
86 buf[strlen(buf)-1] = 0;
94 /** Read an integer value from a line in fp into n,
95 * @return true on failure, false on success.
98 read_int (FILE* fp, int* n)
100 char buf[MAX_STRING_LEN];
102 char* p = fgets (buf, MAX_STRING_LEN, fp);
107 return (sscanf (p, "%d", n) != 1);
111 vstfx_free_info (VSTInfo *info)
113 for (int i = 0; i < info->numParams; i++) {
114 free (info->ParamNames[i]);
115 free (info->ParamLabels[i]);
119 free (info->creator);
120 free (info->Category);
121 free (info->ParamNames);
122 free (info->ParamLabels);
127 vstfx_clear_info_list (vector<VSTInfo *> *infos)
129 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
136 vstfx_load_info_block(FILE* fp, VSTInfo *info)
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;
149 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
153 for (int i = 0; i < info->numParams; ++i) {
154 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
157 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
161 for (int i = 0; i < info->numParams; ++i) {
162 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
170 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
173 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
176 if (vstfx_load_info_block(fp, info)) {
177 if (strncmp (info->Category, "Shell", 5)) {
178 infos->push_back(info);
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);
188 if (vstfx_load_info_block(fp, info)) {
189 infos->push_back(info);
191 vstfx_free_info(info);
192 vstfx_clear_info_list(infos);
197 return false; /* Bad file */
202 vstfx_free_info(info);
203 vstfx_clear_info_list(infos);
208 vstfx_write_info_block (FILE* fp, VSTInfo *info)
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);
224 for (int i = 0; i < info->numParams; i++) {
225 fprintf (fp, "%s\n", info->ParamNames[i]);
228 for (int i = 0; i < info->numParams; i++) {
229 fprintf (fp, "%s\n", info->ParamLabels[i]);
234 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
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
244 vstfx_write_info_block(fp, *x);
245 fprintf( fp, "%d\n", (int)infos->size() - 1 );
247 /* Now write out the info for each plugin */
248 for (; x != infos->end(); ++x) {
249 vstfx_write_info_block(fp, *x);
251 } else if (infos->size() == 1) {
252 vstfx_write_info_block(fp, infos->front());
254 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
259 vstfx_blacklist_path (const char* dllpath, int personal)
263 dir = get_personal_vst_blacklist_dir();
265 dir = Glib::path_get_dirname (std::string(dllpath));
269 s << "." << Glib::path_get_basename (dllpath) << ".fsb";
270 return Glib::build_filename (dir, s.str ());
273 /* return true if plugin is blacklisted or has an invalid file extension */
275 vstfx_blacklist_stat (const char *dllpath, int personal)
277 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
280 string const path = vstfx_blacklist_path (dllpath, personal);
282 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
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 */
292 /* stat failed or plugin is older than blacklist file */
295 /* blacklist file does not exist */
300 vstfx_check_blacklist (const char *dllpath)
302 if (vstfx_blacklist_stat(dllpath, 0)) return true;
303 if (vstfx_blacklist_stat(dllpath, 1)) return true;
308 vstfx_blacklist_file (const char *dllpath)
311 if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "w"))) {
314 return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "w");
318 vstfx_blacklist (const char *dllpath)
320 FILE *f = vstfx_blacklist_file(dllpath);
329 vstfx_un_blacklist (const char *dllpath)
331 ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
332 ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
336 vstfx_infofile_path (const char* dllpath, int personal)
340 dir = get_personal_vst_info_cache_dir();
342 dir = Glib::path_get_dirname (std::string(dllpath));
346 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
347 return Glib::build_filename (dir, s.str ());
351 vstfx_remove_infofile (const char *dllpath)
353 ::g_unlink(vstfx_infofile_path (dllpath, 0).c_str());
354 ::g_unlink(vstfx_infofile_path (dllpath, 1).c_str());
358 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
360 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
364 string const path = vstfx_infofile_path (dllpath, personal);
366 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
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 ());
385 vstfx_infofile_for_read (const char* dllpath)
387 struct stat own_statbuf;
388 struct stat sys_statbuf;
391 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
392 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
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");
401 rv = g_fopen (own_info, "rb");
403 } else if (sys_info) {
404 rv = g_fopen (sys_info, "rb");
413 vstfx_infofile_create (const char* dllpath, int personal)
415 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
419 string const path = vstfx_infofile_path (dllpath, personal);
420 return fopen (path.c_str(), "w");
424 vstfx_infofile_for_write (const char* dllpath)
428 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
429 f = vstfx_infofile_create (dllpath, 1);
436 int vstfx_can_midi (VSTState* vstfx)
438 AEffect* plugin = vstfx->plugin;
440 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
442 if (vst_version >= 2) {
443 /* should we send it VST events (i.e. MIDI) */
445 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
454 vstfx_parse_vst_state (VSTState* vstfx)
458 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
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*/
467 char creator[65] = "Unknown\0";
469 AEffect* plugin = vstfx->plugin;
471 info->name = strdup (vstfx->handle->name);
473 /*If the plugin doesn't bother to implement GetVendorString we will
474 have pre-stuffed the string with 'Unkown' */
476 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
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' */
481 if (strlen(creator) == 0) {
482 info->creator = strdup ("Unknown");
484 info->creator = strdup (creator);
488 switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
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;
504 info->UniqueID = plugin->uniqueID;
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);
515 for (int i = 0; i < info->numParams; ++i) {
519 /* Not all plugins give parameters labels as well as names */
521 strcpy (name, "No Name");
522 strcpy (label, "No Label");
524 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
525 info->ParamNames[i] = strdup(name);
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);
535 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
540 if ((info = vstfx_parse_vst_state(vstfx))) {
541 infos->push_back(info);
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.
546 if (!strncmp (info->Category, "Shell", 5)
547 && vstfx->handle->plugincnt == 1) {
549 vector< pair<int, string> > ids;
550 AEffect *plugin = vstfx->plugin;
551 string path = vstfx->handle->path;
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));
560 #ifdef WINDOWS_VST_SUPPORT
561 case 1: fst_close(vstfx); break;
564 case 2: vstfx_close (vstfx); break;
566 default: assert(0); break;
569 for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
571 if (id == 0) continue;
572 /* recurse vstfx_get_info() */
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;
580 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
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();
591 if ((*x).second.length() == 0) {
592 info->name = strdup("Unknown");
595 info->name = strdup ((*x).second.c_str());
604 /* A simple 'dummy' audiomaster callback which should be ok,
605 we will only be instantiating the plugin in order to get its info
609 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
611 const char* vstfx_can_do_strings[] = {
615 const int vstfx_can_do_string_count = 2;
617 if (opcode == audioMasterVersion) {
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)) {
628 else if (opcode == audioMasterCurrentId) {
629 return vstfx_current_loading_id;
637 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
641 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
642 rv = vstfx_load_info_file(infofile, infos);
645 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
653 vstfx_instantiate_and_get_info_lx (
654 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
658 if (!(h = vstfx_load(dllpath))) {
659 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
663 vstfx_current_loading_id = uniqueID;
665 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
667 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
671 vstfx_current_loading_id = 0;
673 vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
681 #ifdef WINDOWS_VST_SUPPORT
683 vstfx_instantiate_and_get_info_fst (
684 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
688 if(!(h = fst_load(dllpath))) {
689 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
693 vstfx_current_loading_id = uniqueID;
695 if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
697 vstfx_current_loading_id = 0;
698 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
701 vstfx_current_loading_id = 0;
703 vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
706 //fst_unload(&h); // XXX -> fst_close()
711 #ifndef VST_SCANNER_APP
712 static void parse_scanner_output (std::string msg, size_t /*len*/)
714 // TODO write to blacklist or error file..?
715 PBD::error << "VST scanner: " << msg;
719 static vector<VSTInfo *> *
720 vstfx_get_info (const char* dllpath, int type, enum VSTScanMode mode)
723 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
725 if (vstfx_check_blacklist(dllpath)) {
729 if (vstfx_get_info_from_file(dllpath, infos)) {
733 #ifndef VST_SCANNER_APP
734 std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
736 if (mode == VST_SCAN_CACHE_ONLY) {
737 /* never scan explicitly, use cache only */
740 else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
741 /* use external scanner app */
743 char **argp= (char**) calloc(3,sizeof(char*));
744 argp[0] = strdup(scanner_bin_path.c_str());
745 argp[1] = strdup(dllpath);
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;
755 int timeout = PLUGIN_SCAN_TIMEOUT;
756 while (scanner.is_running() && --timeout) {
758 Glib::usleep (100000);
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);
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);
778 /* else .. instantiate and check in in ardour process itself */
780 (void) mode; // unused parameter
784 /* blacklist in case instantiation fails */
785 vstfx_blacklist(dllpath);
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;
792 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
801 /* remove from blacklist */
802 vstfx_un_blacklist(dllpath);
804 /* crate cache/whitelist */
805 infofile = vstfx_infofile_for_write (dllpath);
807 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
810 vstfx_write_info_file (infofile, infos);
816 /* *** public API *** */
819 vstfx_free_info_list (vector<VSTInfo *> *infos)
821 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
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;
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;
855 vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
857 return vstfx_get_info(dllpath, 2, mode);
861 #ifdef WINDOWS_VST_SUPPORT
863 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
865 return vstfx_get_info(dllpath, 1, mode);