1 /** @file libs/ardour/vst_info_file.cc
2 * @brief Code to manage info files containing cached information about a plugin.
3 * e.g. its name, creator etc.
21 #include <glib/gstdio.h>
24 #include "pbd/error.h"
26 #include "ardour/linux_vst_support.h"
28 #define MAX_STRING_LEN 256
33 read_string (FILE *fp)
35 char buf[MAX_STRING_LEN];
37 if (!fgets (buf, MAX_STRING_LEN, fp)) {
41 if (strlen(buf) < MAX_STRING_LEN) {
43 buf[strlen(buf)-1] = 0;
51 /** Read an integer value from a line in fp into n,
52 * @return true on failure, false on success.
55 read_int (FILE* fp, int* n)
57 char buf[MAX_STRING_LEN];
59 char* p = fgets (buf, MAX_STRING_LEN, fp);
64 return (sscanf (p, "%d", n) != 1);
68 load_vstfx_info_file (FILE* fp)
72 if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
76 if ((info->name = read_string(fp)) == 0) goto error;
77 if ((info->creator = read_string(fp)) == 0) goto error;
78 if (read_int (fp, &info->UniqueID)) goto error;
79 if ((info->Category = read_string(fp)) == 0) goto error;
80 if (read_int (fp, &info->numInputs)) goto error;
81 if (read_int (fp, &info->numOutputs)) goto error;
82 if (read_int (fp, &info->numParams)) goto error;
83 if (read_int (fp, &info->wantMidi)) goto error;
84 if (read_int (fp, &info->hasEditor)) goto error;
85 if (read_int (fp, &info->canProcessReplacing)) goto error;
87 if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
91 for (int i = 0; i < info->numParams; ++i) {
92 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
95 if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
99 for (int i = 0; i < info->numParams; ++i) {
100 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
111 save_vstfx_info_file (VSTInfo *info, FILE* fp)
116 fprintf (fp, "%s\n", info->name);
117 fprintf (fp, "%s\n", info->creator);
118 fprintf (fp, "%d\n", info->UniqueID);
119 fprintf (fp, "%s\n", info->Category);
120 fprintf (fp, "%d\n", info->numInputs);
121 fprintf (fp, "%d\n", info->numOutputs);
122 fprintf (fp, "%d\n", info->numParams);
123 fprintf (fp, "%d\n", info->wantMidi);
124 fprintf (fp, "%d\n", info->hasEditor);
125 fprintf (fp, "%d\n", info->canProcessReplacing);
127 for (int i = 0; i < info->numParams; i++) {
128 fprintf (fp, "%s\n", info->ParamNames[i]);
131 for (int i = 0; i < info->numParams; i++) {
132 fprintf (fp, "%s\n", info->ParamLabels[i]);
139 vstfx_infofile_path (char* dllpath, int personal)
143 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
145 /* If the directory doesn't exist, try to create it */
146 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
147 if (g_mkdir (dir.c_str (), 0700)) {
153 dir = Glib::path_get_dirname (dllpath);
157 s << "." << Glib::path_get_basename (dllpath) << ".fsi";
158 return Glib::build_filename (dir, s.str ());
162 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
164 if (strstr (dllpath, ".so" ) == 0) {
168 string const path = vstfx_infofile_path (dllpath, personal);
170 if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
172 /* info file exists in same location as the shared object, so
173 check if its current and up to date
179 if (stat (dllpath, &dllstat) == 0) {
180 if (stat (path.c_str(), statbuf) == 0) {
181 if (dllstat.st_mtime <= statbuf->st_mtime) {
182 /* plugin is older than info file */
183 return strdup (path.c_str ());
194 vstfx_infofile_for_read (char* dllpath)
196 struct stat own_statbuf;
197 struct stat sys_statbuf;
199 char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
200 char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
204 if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
205 /* system info file is newer, use it */
206 return g_fopen (sys_info, "rb");
209 return g_fopen (own_info, "rb");
217 vstfx_infofile_create (char* dllpath, int personal)
219 if (strstr (dllpath, ".so" ) == 0) {
223 string const path = vstfx_infofile_path (dllpath, personal);
224 return fopen (path.c_str(), "w");
228 vstfx_infofile_for_write (char* dllpath)
232 if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
233 f = vstfx_infofile_create (dllpath, 1);
240 int vstfx_can_midi (VSTState* vstfx)
242 AEffect* plugin = vstfx->plugin;
244 int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
246 if (vst_version >= 2) {
247 /* should we send it VST events (i.e. MIDI) */
249 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
258 vstfx_info_from_plugin (VSTState* vstfx)
262 VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
267 /*We need to init the creator because some plugins
268 fail to implement getVendorString, and so won't stuff the
269 string with any name*/
271 char creator[65] = "Unknown\0";
273 AEffect* plugin = vstfx->plugin;
275 info->name = strdup (vstfx->handle->name);
277 /*If the plugin doesn't bother to implement GetVendorString we will
278 have pre-stuffed the string with 'Unkown' */
280 plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
282 /*Some plugins DO implement GetVendorString, but DON'T put a name in it
283 so if its just a zero length string we replace it with 'Unknown' */
285 if (strlen(creator) == 0) {
286 info->creator = strdup ("Unknown");
288 info->creator = strdup (creator);
291 info->UniqueID = plugin->uniqueID;
293 info->Category = strdup("None"); /* XXX */
294 info->numInputs = plugin->numInputs;
295 info->numOutputs = plugin->numOutputs;
296 info->numParams = plugin->numParams;
297 info->wantMidi = vstfx_can_midi(vstfx);
298 info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
299 info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
300 info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
301 info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
303 for (int i = 0; i < info->numParams; ++i) {
307 /* Not all plugins give parameters labels as well as names */
309 strcpy (name, "No Name");
310 strcpy (label, "No Label");
312 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
313 info->ParamNames[i] = strdup(name);
315 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
316 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
317 info->ParamLabels[i] = strdup(label);
322 /* A simple 'dummy' audiomaster callback which should be ok,
323 we will only be instantiating the plugin in order to get its info
327 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
329 if (opcode == audioMasterVersion) {
336 /** Try to get plugin info - first by looking for a .fsi cache of the
337 data, and if that doesn't exist, load the plugin, get its data and
338 then cache it for future ref
342 vstfx_get_info (char* dllpath)
348 if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
350 info = load_vstfx_info_file (infofile);
353 PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
358 if (!(h = vstfx_load(dllpath))) {
359 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
363 if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
365 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
369 infofile = vstfx_infofile_for_write (dllpath);
374 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
378 VSTInfo* info = vstfx_info_from_plugin (vstfx);
380 save_vstfx_info_file (info, infofile);
390 vstfx_free_info (VSTInfo *info)
392 for (int i = 0; i < info->numParams; i++) {
393 free (info->ParamNames[i]);
394 free (info->ParamLabels[i]);
398 free (info->creator);
399 free (info->Category);