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_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
353 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
357 string const path = vstfx_infofile_path (dllpath, personal);
359 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
363 if (stat (dllpath, &dllstat) == 0) {
364 if (stat (path.c_str(), statbuf) == 0) {
365 if (dllstat.st_mtime <= statbuf->st_mtime) {
366 /* plugin is older than info file */
367 return strdup (path.c_str ());
378 vstfx_infofile_for_read (const char* dllpath)
380 struct stat own_statbuf;
381 struct stat sys_statbuf;
384 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
385 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
389 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
390 /* system info file is newer, use it */
391 rv = g_fopen (sys_info, "rb");
394 rv = g_fopen (own_info, "rb");
396 } else if (sys_info) {
397 rv = g_fopen (sys_info, "rb");
406 vstfx_infofile_create (const char* dllpath, int personal)
408 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
412 string const path = vstfx_infofile_path (dllpath, personal);
413 return fopen (path.c_str(), "w");
417 vstfx_infofile_for_write (const char* dllpath)
421 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
422 f = vstfx_infofile_create (dllpath, 1);
429 int vstfx_can_midi (VSTState* vstfx)
431 AEffect* plugin = vstfx->plugin;
433 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
435 if (vst_version >= 2) {
436 /* should we send it VST events (i.e. MIDI) */
438 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
447 vstfx_parse_vst_state (VSTState* vstfx)
451 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
456 /*We need to init the creator because some plugins
457 fail to implement getVendorString, and so won't stuff the
458 string with any name*/
460 char creator[65] = "Unknown\0";
462 AEffect* plugin = vstfx->plugin;
464 info->name = strdup (vstfx->handle->name);
466 /*If the plugin doesn't bother to implement GetVendorString we will
467 have pre-stuffed the string with 'Unkown' */
469 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
471 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
472 so if its just a zero length string we replace it with 'Unknown' */
474 if (strlen(creator) == 0) {
475 info->creator = strdup ("Unknown");
477 info->creator = strdup (creator);
481 switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
483 case kPlugCategEffect: info->Category = strdup ("Effect"); break;
484 case kPlugCategSynth: info->Category = strdup ("Synth"); break;
485 case kPlugCategAnalysis: info->Category = strdup ("Anaylsis"); break;
486 case kPlugCategMastering: info->Category = strdup ("Mastering"); break;
487 case kPlugCategSpacializer: info->Category = strdup ("Spacializer"); break;
488 case kPlugCategRoomFx: info->Category = strdup ("RoomFx"); break;
489 case kPlugSurroundFx: info->Category = strdup ("SurroundFx"); break;
490 case kPlugCategRestoration: info->Category = strdup ("Restoration"); break;
491 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
492 case kPlugCategShell: info->Category = strdup ("Shell"); break;
493 case kPlugCategGenerator: info->Category = strdup ("Generator"); break;
494 default: info->Category = strdup ("Unknown"); break;
497 info->UniqueID = plugin->uniqueID;
499 info->numInputs = plugin->numInputs;
500 info->numOutputs = plugin->numOutputs;
501 info->numParams = plugin->numParams;
502 info->wantMidi = vstfx_can_midi(vstfx);
503 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
504 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
505 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
506 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
508 for (int i = 0; i < info->numParams; ++i) {
512 /* Not all plugins give parameters labels as well as names */
514 strcpy (name, "No Name");
515 strcpy (label, "No Label");
517 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
518 info->ParamNames[i] = strdup(name);
520 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
521 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
522 info->ParamLabels[i] = strdup(label);
528 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
533 if ((info = vstfx_parse_vst_state(vstfx))) {
534 infos->push_back(info);
536 /* If this plugin is a Shell and we are not already inside a shell plugin
537 * read the info for all of the plugins contained in this shell.
539 if (!strncmp (info->Category, "Shell", 5)
540 && vstfx->handle->plugincnt == 1) {
542 vector< pair<int, string> > ids;
543 AEffect *plugin = vstfx->plugin;
544 string path = vstfx->handle->path;
547 char name[65] = "Unknown\0";
548 id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
549 ids.push_back(std::make_pair(id, name));
553 #ifdef WINDOWS_VST_SUPPORT
554 case 1: fst_close(vstfx); break;
557 case 2: vstfx_close (vstfx); break;
559 default: assert(0); break;
562 for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
564 if (id == 0) continue;
565 /* recurse vstfx_get_info() */
568 switch (type) { // TODO use lib ardour's type
569 #ifdef WINDOWS_VST_SUPPORT
570 case 1: ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
573 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
578 // One shell (some?, all?) does not report the actual plugin name
579 // even after the shelled plugin has been instantiated.
580 // Replace the name of the shell with the real name.
581 info = infos->back();
584 if ((*x).second.length() == 0) {
585 info->name = strdup("Unknown");
588 info->name = strdup ((*x).second.c_str());
597 /* A simple 'dummy' audiomaster callback which should be ok,
598 we will only be instantiating the plugin in order to get its info
602 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
604 const char* vstfx_can_do_strings[] = {
608 const int vstfx_can_do_string_count = 2;
610 if (opcode == audioMasterVersion) {
613 else if (opcode == audioMasterCanDo) {
614 for (int i = 0; i < vstfx_can_do_string_count; i++) {
615 if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
621 else if (opcode == audioMasterCurrentId) {
622 return vstfx_current_loading_id;
630 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
634 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
635 rv = vstfx_load_info_file(infofile, infos);
638 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
646 vstfx_instantiate_and_get_info_lx (
647 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
651 if (!(h = vstfx_load(dllpath))) {
652 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
656 vstfx_current_loading_id = uniqueID;
658 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
660 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
664 vstfx_current_loading_id = 0;
666 vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
674 #ifdef WINDOWS_VST_SUPPORT
676 vstfx_instantiate_and_get_info_fst (
677 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
681 if(!(h = fst_load(dllpath))) {
682 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
686 vstfx_current_loading_id = uniqueID;
688 if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
690 vstfx_current_loading_id = 0;
691 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
694 vstfx_current_loading_id = 0;
696 vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
699 //fst_unload(&h); // XXX -> fst_close()
704 #ifndef VST_SCANNER_APP
705 static void parse_scanner_output (std::string msg, size_t /*len*/)
707 // TODO write to blacklist or error file..?
708 PBD::error << "VST scanner: " << msg;
712 static vector<VSTInfo *> *
713 vstfx_get_info (const char* dllpath, int type, enum VSTScanMode mode)
716 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
718 if (vstfx_check_blacklist(dllpath)) {
722 if (vstfx_get_info_from_file(dllpath, infos)) {
726 #ifndef VST_SCANNER_APP
727 std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
729 if (mode == VST_SCAN_CACHE_ONLY) {
730 /* never scan explicitly, use cache only */
733 else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
734 /* use external scanner app */
736 char **argp= (char**) calloc(3,sizeof(char*));
737 argp[0] = strdup(scanner_bin_path.c_str());
738 argp[1] = strdup(dllpath);
741 PBD::SystemExec scanner (scanner_bin_path, argp);
742 PBD::ScopedConnectionList cons;
743 // TODO timeout.., and honor user-terminate
744 //scanner->Terminated.connect_same_thread (cons, boost::bind (&scanner_terminated))
745 scanner.ReadStdout.connect_same_thread (cons, boost::bind (&parse_scanner_output, _1 ,_2));
746 if (scanner.start (2 /* send stderr&stdout via signal */)) {
747 PBD::error << "Cannot launch VST scanner app '" << scanner_bin_path << "': "<< strerror(errno) << endmsg;
750 int timeout = PLUGIN_SCAN_TIMEOUT;
751 while (scanner.is_running() && --timeout) {
753 Glib::usleep (100000);
754 if (ARDOUR::PluginManager::instance().cancelled()) break;
759 vstfx_clear_info_list(infos);
760 if (!vstfx_check_blacklist(dllpath)) {
761 vstfx_get_info_from_file(dllpath, infos);
765 /* else .. instantiate and check in in ardour process itself */
767 (void) mode; // unused parameter
771 /* blacklist in case instantiation fails */
772 vstfx_blacklist(dllpath);
774 switch (type) { // TODO use lib ardour's type
775 #ifdef WINDOWS_VST_SUPPORT
776 case 1: ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
779 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
788 /* remove from blacklist */
789 vstfx_un_blacklist(dllpath);
791 /* crate cache/whitelist */
792 infofile = vstfx_infofile_for_write (dllpath);
794 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
797 vstfx_write_info_file (infofile, infos);
803 /* *** public API *** */
806 vstfx_free_info_list (vector<VSTInfo *> *infos)
808 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
815 get_personal_vst_blacklist_dir() {
816 string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
817 /* if the directory doesn't exist, try to create it */
818 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
819 if (g_mkdir (dir.c_str (), 0700)) {
820 PBD::error << "Cannt create VST cache folder '" << dir << "'" << endmsg;
828 get_personal_vst_info_cache_dir() {
829 string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
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 << "Cannt create VST info folder '" << dir << "'" << endmsg;
842 vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
844 return vstfx_get_info(dllpath, 2, mode);
848 #ifdef WINDOWS_VST_SUPPORT
850 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
852 return vstfx_get_info(dllpath, 1, mode);