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 #include "ardour/filesystem_paths.h"
46 #include "ardour/linux_vst_support.h"
47 #include "ardour/vst_info_file.h"
49 #define MAX_STRING_LEN 256
53 // TODO: make static parts of this file re-usable for standalone app
54 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
55 // consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
58 #ifdef WINDOWS_VST_SUPPORT
61 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
65 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
68 static int vstfx_current_loading_id = 0;
71 read_string (FILE *fp)
73 char buf[MAX_STRING_LEN];
75 if (!fgets (buf, MAX_STRING_LEN, fp)) {
79 if (strlen(buf) < MAX_STRING_LEN) {
81 buf[strlen(buf)-1] = 0;
89 /** Read an integer value from a line in fp into n,
90 * @return true on failure, false on success.
93 read_int (FILE* fp, int* n)
95 char buf[MAX_STRING_LEN];
97 char* p = fgets (buf, MAX_STRING_LEN, fp);
102 return (sscanf (p, "%d", n) != 1);
106 vstfx_free_info (VSTInfo *info)
108 for (int i = 0; i < info->numParams; i++) {
109 free (info->ParamNames[i]);
110 free (info->ParamLabels[i]);
114 free (info->creator);
115 free (info->Category);
116 free (info->ParamNames);
117 free (info->ParamLabels);
122 vstfx_clear_info_list (vector<VSTInfo *> *infos)
124 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
131 vstfx_load_info_block(FILE* fp, VSTInfo *info)
133 if ((info->name = read_string(fp)) == 0) return false;
134 if ((info->creator = read_string(fp)) == 0) return false;
135 if (read_int (fp, &info->UniqueID)) return false;
136 if ((info->Category = read_string(fp)) == 0) return false;
137 if (read_int (fp, &info->numInputs)) return false;
138 if (read_int (fp, &info->numOutputs)) return false;
139 if (read_int (fp, &info->numParams)) return false;
140 if (read_int (fp, &info->wantMidi)) return false;
141 if (read_int (fp, &info->hasEditor)) return false;
142 if (read_int (fp, &info->canProcessReplacing)) return false;
144 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
148 for (int i = 0; i < info->numParams; ++i) {
149 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
152 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
156 for (int i = 0; i < info->numParams; ++i) {
157 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
165 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
168 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
171 if (vstfx_load_info_block(fp, info)) {
172 if (strncmp (info->Category, "Shell", 5)) {
173 infos->push_back(info);
176 vstfx_free_info(info);
177 if (read_int (fp, &plugin_cnt)) {
178 for (int i = 0; i < plugin_cnt; i++) {
179 if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
180 vstfx_clear_info_list(infos);
183 if (vstfx_load_info_block(fp, info)) {
184 infos->push_back(info);
186 vstfx_free_info(info);
187 vstfx_clear_info_list(infos);
192 return false; /* Bad file */
197 vstfx_free_info(info);
198 vstfx_clear_info_list(infos);
203 vstfx_write_info_block (FILE* fp, VSTInfo *info)
208 fprintf (fp, "%s\n", info->name);
209 fprintf (fp, "%s\n", info->creator);
210 fprintf (fp, "%d\n", info->UniqueID);
211 fprintf (fp, "%s\n", info->Category);
212 fprintf (fp, "%d\n", info->numInputs);
213 fprintf (fp, "%d\n", info->numOutputs);
214 fprintf (fp, "%d\n", info->numParams);
215 fprintf (fp, "%d\n", info->wantMidi);
216 fprintf (fp, "%d\n", info->hasEditor);
217 fprintf (fp, "%d\n", info->canProcessReplacing);
219 for (int i = 0; i < info->numParams; i++) {
220 fprintf (fp, "%s\n", info->ParamNames[i]);
223 for (int i = 0; i < info->numParams; i++) {
224 fprintf (fp, "%s\n", info->ParamLabels[i]);
229 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
234 if (infos->size() > 1) {
235 vector<VSTInfo *>::iterator x = infos->begin();
236 /* write out the shell info first along with count of the number of
237 * plugins contained in this shell
239 vstfx_write_info_block(fp, *x);
240 fprintf( fp, "%d\n", infos->size() - 1 );
242 /* Now write out the info for each plugin */
243 for (; x != infos->end(); ++x) {
244 vstfx_write_info_block(fp, *x);
246 } else if (infos->size() == 1) {
247 vstfx_write_info_block(fp, infos->front());
249 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
254 vstfx_blacklist_path (const char* dllpath, int personal)
258 dir = get_personal_vst_blacklist_dir();
260 dir = Glib::path_get_dirname (std::string(dllpath));
264 s << "." << Glib::path_get_basename (dllpath) << ".fsb";
265 return Glib::build_filename (dir, s.str ());
268 /* return true if plugin is blacklisted or has an invalid file extension */
270 vstfx_blacklist_stat (const char *dllpath, int personal)
272 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
275 string const path = vstfx_blacklist_path (dllpath, personal);
277 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
281 if (stat (dllpath, &dllstat) == 0 && stat (path.c_str(), &fsbstat) == 0) {
282 if (dllstat.st_mtime > fsbstat.st_mtime) {
283 /* plugin is newer than blacklist file */
287 /* stat failed or plugin is older than blacklist file */
290 /* blacklist file does not exist */
295 vstfx_check_blacklist (const char *dllpath)
297 if (vstfx_blacklist_stat(dllpath, 0)) return true;
298 if (vstfx_blacklist_stat(dllpath, 1)) return true;
303 vstfx_blacklist_file (const char *dllpath)
306 if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "w"))) {
309 return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "w");
313 vstfx_blacklist (const char *dllpath)
315 FILE *f = vstfx_blacklist_file(dllpath);
324 vstfx_un_blacklist (const char *dllpath)
326 ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
327 ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
331 vstfx_infofile_path (const char* dllpath, int personal)
335 dir = get_personal_vst_info_cache_dir();
337 dir = Glib::path_get_dirname (std::string(dllpath));
341 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
342 return Glib::build_filename (dir, s.str ());
346 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
348 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
352 string const path = vstfx_infofile_path (dllpath, personal);
354 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
358 if (stat (dllpath, &dllstat) == 0) {
359 if (stat (path.c_str(), statbuf) == 0) {
360 if (dllstat.st_mtime <= statbuf->st_mtime) {
361 /* plugin is older than info file */
362 return strdup (path.c_str ());
373 vstfx_infofile_for_read (const char* dllpath)
375 struct stat own_statbuf;
376 struct stat sys_statbuf;
379 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
380 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
384 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
385 /* system info file is newer, use it */
386 rv = g_fopen (sys_info, "rb");
389 rv = g_fopen (own_info, "rb");
391 } else if (sys_info) {
392 rv = g_fopen (sys_info, "rb");
401 vstfx_infofile_create (const char* dllpath, int personal)
403 if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
407 string const path = vstfx_infofile_path (dllpath, personal);
408 return fopen (path.c_str(), "w");
412 vstfx_infofile_for_write (const char* dllpath)
416 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
417 f = vstfx_infofile_create (dllpath, 1);
424 int vstfx_can_midi (VSTState* vstfx)
426 AEffect* plugin = vstfx->plugin;
428 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
430 if (vst_version >= 2) {
431 /* should we send it VST events (i.e. MIDI) */
433 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
442 vstfx_parse_vst_state (VSTState* vstfx)
446 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
451 /*We need to init the creator because some plugins
452 fail to implement getVendorString, and so won't stuff the
453 string with any name*/
455 char creator[65] = "Unknown\0";
457 AEffect* plugin = vstfx->plugin;
459 info->name = strdup (vstfx->handle->name);
461 /*If the plugin doesn't bother to implement GetVendorString we will
462 have pre-stuffed the string with 'Unkown' */
464 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
466 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
467 so if its just a zero length string we replace it with 'Unknown' */
469 if (strlen(creator) == 0) {
470 info->creator = strdup ("Unknown");
472 info->creator = strdup (creator);
476 switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
478 case kPlugCategEffect: info->Category = strdup ("Effect"); break;
479 case kPlugCategSynth: info->Category = strdup ("Synth"); break;
480 case kPlugCategAnalysis: info->Category = strdup ("Anaylsis"); break;
481 case kPlugCategMastering: info->Category = strdup ("Mastering"); break;
482 case kPlugCategSpacializer: info->Category = strdup ("Spacializer"); break;
483 case kPlugCategRoomFx: info->Category = strdup ("RoomFx"); break;
484 case kPlugSurroundFx: info->Category = strdup ("SurroundFx"); break;
485 case kPlugCategRestoration: info->Category = strdup ("Restoration"); break;
486 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
487 case kPlugCategShell: info->Category = strdup ("Shell"); break;
488 case kPlugCategGenerator: info->Category = strdup ("Generator"); break;
489 default: info->Category = strdup ("Unknown"); break;
492 info->UniqueID = plugin->uniqueID;
494 info->numInputs = plugin->numInputs;
495 info->numOutputs = plugin->numOutputs;
496 info->numParams = plugin->numParams;
497 info->wantMidi = vstfx_can_midi(vstfx);
498 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
499 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
500 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
501 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
503 for (int i = 0; i < info->numParams; ++i) {
507 /* Not all plugins give parameters labels as well as names */
509 strcpy (name, "No Name");
510 strcpy (label, "No Label");
512 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
513 info->ParamNames[i] = strdup(name);
515 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
516 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
517 info->ParamLabels[i] = strdup(label);
523 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
528 if ((info = vstfx_parse_vst_state(vstfx))) {
529 infos->push_back(info);
531 /* If this plugin is a Shell and we are not already inside a shell plugin
532 * read the info for all of the plugins contained in this shell.
534 if (!strncmp (info->Category, "Shell", 5)
535 && vstfx->handle->plugincnt == 1) {
537 vector< pair<int, string> > ids;
538 AEffect *plugin = vstfx->plugin;
539 string path = vstfx->handle->path;
542 char name[65] = "Unknown\0";
543 id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
544 ids.push_back(std::make_pair(id, name));
548 #ifdef WINDOWS_VST_SUPPORT
549 case 1: fst_close(vstfx); break;
552 case 2: vstfx_close (vstfx); break;
554 default: assert(0); break;
557 for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
559 if (id == 0) continue;
560 /* recurse vstfx_get_info() */
563 switch (type) { // TODO use lib ardour's type
564 #ifdef WINDOWS_VST_SUPPORT
565 case 1: ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
568 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
573 // One shell (some?, all?) does not report the actual plugin name
574 // even after the shelled plugin has been instantiated.
575 // Replace the name of the shell with the real name.
576 info = infos->back();
579 if ((*x).second.length() == 0) {
580 info->name = strdup("Unknown");
583 info->name = strdup ((*x).second.c_str());
592 /* A simple 'dummy' audiomaster callback which should be ok,
593 we will only be instantiating the plugin in order to get its info
597 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
599 const char* vstfx_can_do_strings[] = {
603 const int vstfx_can_do_string_count = 2;
605 if (opcode == audioMasterVersion) {
608 else if (opcode == audioMasterCanDo) {
609 for (int i = 0; i < vstfx_can_do_string_count; i++) {
610 if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
616 else if (opcode == audioMasterCurrentId) {
617 return vstfx_current_loading_id;
625 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
629 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
630 rv = vstfx_load_info_file(infofile, infos);
633 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
641 vstfx_instantiate_and_get_info_lx (
642 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
646 if (!(h = vstfx_load(dllpath))) {
647 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
651 vstfx_current_loading_id = uniqueID;
653 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
655 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
659 vstfx_current_loading_id = 0;
661 vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
669 #ifdef WINDOWS_VST_SUPPORT
671 vstfx_instantiate_and_get_info_fst (
672 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
676 if(!(h = fst_load(dllpath))) {
677 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
681 vstfx_current_loading_id = uniqueID;
683 if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
685 vstfx_current_loading_id = 0;
686 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
689 vstfx_current_loading_id = 0;
691 vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
694 //fst_unload(&h); // XXX -> fst_close()
699 static vector<VSTInfo *> *
700 vstfx_get_info (const char* dllpath, int type)
703 vector<VSTInfo*> *infos = new vector<VSTInfo*>;
705 if (vstfx_check_blacklist(dllpath)) {
709 if (vstfx_get_info_from_file(dllpath, infos)) {
714 /* blacklist in case instantiation fails */
715 vstfx_blacklist_file(dllpath);
717 switch (type) { // TODO use lib ardour's type
718 #ifdef WINDOWS_VST_SUPPORT
719 case 1: ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
722 case 2: ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
731 /* remove from blacklist */
732 vstfx_un_blacklist(dllpath);
734 /* crate cache/whitelist */
735 infofile = vstfx_infofile_for_write (dllpath);
737 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
740 vstfx_write_info_file (infofile, infos);
746 /* *** public API *** */
749 vstfx_free_info_list (vector<VSTInfo *> *infos)
751 for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
758 get_personal_vst_blacklist_dir() {
759 string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
760 /* if the directory doesn't exist, try to create it */
761 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
762 if (g_mkdir (dir.c_str (), 0700)) {
763 PBD::error << "Cannt create VST cache folder '" << dir << "'" << endmsg;
771 get_personal_vst_info_cache_dir() {
772 string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
773 /* if the directory doesn't exist, try to create it */
774 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
775 if (g_mkdir (dir.c_str (), 0700)) {
776 PBD::error << "Cannt create VST info folder '" << dir << "'" << endmsg;
785 vstfx_get_info_lx (char* dllpath)
787 return vstfx_get_info(dllpath, 2);
791 #ifdef WINDOWS_VST_SUPPORT
793 vstfx_get_info_fst (char* dllpath)
795 return vstfx_get_info(dllpath, 1);