preparations for VST Shell plugins (mostly mixbus code forward port)
[ardour.git] / libs / ardour / vst_info_file.cc
1 /*
2     Copyright (C) 2012-2014 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
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.
23  */
24
25 #include <iostream>
26 #include <cassert>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <libgen.h>
38
39 #include <glib.h>
40 #include <glib/gstdio.h>
41 #include <glibmm.h>
42
43 #include "pbd/error.h"
44
45 #include "ardour/linux_vst_support.h"
46 #include "ardour/vst_info_file.h"
47
48 #define MAX_STRING_LEN 256
49
50 using namespace std;
51
52 static char *
53 read_string (FILE *fp)
54 {
55         char buf[MAX_STRING_LEN];
56
57         if (!fgets (buf, MAX_STRING_LEN, fp)) {
58                 return 0;
59         }
60
61         if (strlen(buf) < MAX_STRING_LEN) {
62                 if (strlen (buf)) {
63                         buf[strlen(buf)-1] = 0;
64                 }
65                 return strdup (buf);
66         } else {
67                 return 0;
68         }
69 }
70
71 /** Read an integer value from a line in fp into n,
72  *  @return true on failure, false on success.
73  */
74 static bool
75 read_int (FILE* fp, int* n)
76 {
77         char buf[MAX_STRING_LEN];
78
79         char* p = fgets (buf, MAX_STRING_LEN, fp);
80         if (p == 0) {
81                 return true;
82         }
83
84         return (sscanf (p, "%d", n) != 1);
85 }
86
87 static void
88 vstfx_clear_info_list (vector<VSTInfo *> *infos)
89 {
90         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
91                 vstfx_free_info(*i);
92         }
93         infos->clear();
94 }
95
96 static bool
97 vstfx_load_info_block(FILE* fp, VSTInfo *info)
98 {
99         if ((info->name = read_string(fp)) == 0) return false;
100         if ((info->creator = read_string(fp)) == 0) return false;
101         if (read_int (fp, &info->UniqueID)) return false;
102         if ((info->Category = read_string(fp)) == 0) return false;
103         if (read_int (fp, &info->numInputs)) return false;
104         if (read_int (fp, &info->numOutputs)) return false;
105         if (read_int (fp, &info->numParams)) return false;
106         if (read_int (fp, &info->wantMidi)) return false;
107         if (read_int (fp, &info->hasEditor)) return false;
108         if (read_int (fp, &info->canProcessReplacing)) return false;
109
110         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
111                 return false;
112         }
113
114         for (int i = 0; i < info->numParams; ++i) {
115                 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
116         }
117
118         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
119                 return false;
120         }
121
122         for (int i = 0; i < info->numParams; ++i) {
123                 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
124                         return false;
125                 }
126         }
127         return true;
128 }
129
130 static bool
131 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
132 {
133         VSTInfo *info;
134         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
135                 return false;
136         }
137         if (vstfx_load_info_block(fp, info)) {
138                 if (strncmp (info->Category, "Shell", 5)) {
139                         infos->push_back(info);
140                 } else {
141                         int plugin_cnt = 0;
142                         vstfx_free_info(info);
143                         if (read_int (fp, &plugin_cnt)) {
144                                 for (int i = 0; i < plugin_cnt; i++) {
145                                         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
146                                                 vstfx_clear_info_list(infos);
147                                                 return false;
148                                         }
149                                         if (vstfx_load_info_block(fp, info)) {
150                                                 infos->push_back(info);
151                                         } else {
152                                                 vstfx_free_info(info);
153                                                 vstfx_clear_info_list(infos);
154                                                 return false;
155                                         }
156                                 }
157                         } else {
158                                 return false; /* Bad file */
159                         }
160                 }
161                 return true;
162         }
163         vstfx_free_info(info);
164         vstfx_clear_info_list(infos);
165         return false;
166 }
167
168 static void
169 vstfx_write_info_block (FILE* fp, VSTInfo *info)
170 {
171         assert (info);
172         assert (fp);
173
174         fprintf (fp, "%s\n", info->name);
175         fprintf (fp, "%s\n", info->creator);
176         fprintf (fp, "%d\n", info->UniqueID);
177         fprintf (fp, "%s\n", info->Category);
178         fprintf (fp, "%d\n", info->numInputs);
179         fprintf (fp, "%d\n", info->numOutputs);
180         fprintf (fp, "%d\n", info->numParams);
181         fprintf (fp, "%d\n", info->wantMidi);
182         fprintf (fp, "%d\n", info->hasEditor);
183         fprintf (fp, "%d\n", info->canProcessReplacing);
184
185         for (int i = 0; i < info->numParams; i++) {
186                 fprintf (fp, "%s\n", info->ParamNames[i]);
187         }
188
189         for (int i = 0; i < info->numParams; i++) {
190                 fprintf (fp, "%s\n", info->ParamLabels[i]);
191         }
192 }
193
194 static void
195 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
196 {
197         assert(infos);
198         assert(fp);
199
200         if (infos->size() > 1) {
201                 vector<VSTInfo *>::iterator x = infos->begin();
202                 /* write out the shell info first along with count of the number of
203                  * plugins contained in this shell
204                  */
205                 vstfx_write_info_block(fp, *x);
206                 fprintf( fp, "%d\n", infos->size() - 1 );
207                 ++x;
208                 /* Now write out the info for each plugin */
209                 for (; x != infos->end(); ++x) {
210                         vstfx_write_info_block(fp, *x);
211                 }
212         } else if (infos->size() == 1) {
213                 vstfx_write_info_block(fp, infos->front());
214         } else {
215                 PBD::warning << "Zero plugins in VST." << endmsg; // XXX here?
216         }
217 }
218
219 static string
220 vstfx_infofile_path (char* dllpath, int personal)
221 {
222         string dir;
223         if (personal) {
224                 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
225
226                 /* If the directory doesn't exist, try to create it */
227                 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
228                         if (g_mkdir (dir.c_str (), 0700)) {
229                                 return 0;
230                         }
231                 }
232
233         } else {
234                 dir = Glib::path_get_dirname (std::string(dllpath));
235         }
236
237         stringstream s;
238         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
239         return Glib::build_filename (dir, s.str ());
240 }
241
242 static char *
243 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
244 {
245         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
246                 return 0;
247         }
248
249         string const path = vstfx_infofile_path (dllpath, personal);
250
251         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
252
253                 /* info file exists in same location as the shared object, so
254                    check if its current and up to date
255                 */
256
257
258                 struct stat dllstat;
259
260                 if (stat (dllpath, &dllstat) == 0) {
261                         if (stat (path.c_str(), statbuf) == 0) {
262                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
263                                         /* plugin is older than info file */
264                                         return strdup (path.c_str ());
265                                 }
266                         }
267                 }
268         }
269
270         return 0;
271 }
272
273
274 static FILE *
275 vstfx_infofile_for_read (char* dllpath)
276 {
277         struct stat own_statbuf;
278         struct stat sys_statbuf;
279         FILE *rv = NULL;
280
281         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
282         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
283
284         if (own_info) {
285                 if (sys_info) {
286                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
287                                 /* system info file is newer, use it */
288                                 rv = g_fopen (sys_info, "rb");
289                         }
290                 } else {
291                         rv = g_fopen (own_info, "rb");
292                 }
293         } else if (sys_info) {
294                 rv = g_fopen (sys_info, "rb");
295         }
296         free(own_info);
297         free(sys_info);
298
299         return rv;
300 }
301
302 static FILE *
303 vstfx_infofile_create (char* dllpath, int personal)
304 {
305         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
306                 return 0;
307         }
308
309         string const path = vstfx_infofile_path (dllpath, personal);
310         return fopen (path.c_str(), "w");
311 }
312
313 static FILE *
314 vstfx_infofile_for_write (char* dllpath)
315 {
316         FILE* f;
317
318         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
319                 f = vstfx_infofile_create (dllpath, 1);
320         }
321
322         return f;
323 }
324
325 static
326 int vstfx_can_midi (VSTState* vstfx)
327 {
328         AEffect* plugin = vstfx->plugin;
329
330         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
331
332         if (vst_version >= 2) {
333                 /* should we send it VST events (i.e. MIDI) */
334
335                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
336                         return -1;
337                 }
338         }
339
340         return false;
341 }
342
343 static VSTInfo *
344 vstfx_info_from_plugin (VSTState* vstfx)
345 {
346         assert (vstfx);
347
348         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
349         if (!info) {
350                 return 0;
351         }
352
353         /*We need to init the creator because some plugins
354           fail to implement getVendorString, and so won't stuff the
355           string with any name*/
356
357         char creator[65] = "Unknown\0";
358
359         AEffect* plugin = vstfx->plugin;
360
361         info->name = strdup (vstfx->handle->name);
362
363         /*If the plugin doesn't bother to implement GetVendorString we will
364           have pre-stuffed the string with 'Unkown' */
365
366         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
367
368         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
369           so if its just a zero length string we replace it with 'Unknown' */
370
371         if (strlen(creator) == 0) {
372                 info->creator = strdup ("Unknown");
373         } else {
374                 info->creator = strdup (creator);
375         }
376
377         info->UniqueID = plugin->uniqueID;
378
379         info->Category = strdup("None"); /* XXX */
380         info->numInputs = plugin->numInputs;
381         info->numOutputs = plugin->numOutputs;
382         info->numParams = plugin->numParams;
383         info->wantMidi = vstfx_can_midi(vstfx);
384         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
385         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
386         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
387         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
388
389         for (int i = 0; i < info->numParams; ++i) {
390                 char name[64];
391                 char label[64];
392
393                 /* Not all plugins give parameters labels as well as names */
394
395                 strcpy (name, "No Name");
396                 strcpy (label, "No Label");
397
398                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
399                 info->ParamNames[i] = strdup(name);
400
401                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
402                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
403                 info->ParamLabels[i] = strdup(label);
404         }
405         return info;
406 }
407
408 /* A simple 'dummy' audiomaster callback which should be ok,
409    we will only be instantiating the plugin in order to get its info
410 */
411
412 static intptr_t
413 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
414 {
415 #if 0
416         static const char* can_do_strings[] = {
417                 X_("supportShell"),
418                 X_("shellCategory")
419         };
420         static const int can_do_string_count = sizeof (can_do_strings) / sizeof (char*);
421         static int current_loading_id = 0;
422 #endif
423
424         if (opcode == audioMasterVersion) {
425                 return 2;
426         }
427 #if 0
428         else if (opcode == audioMasterCanDo) {
429                 for (int i = 0; i < can_do_string_count; i++) {
430                         if (! strcmp(can_do_strings[i], (const char*)ptr)) {
431                                 return 1;
432                         }
433                 }
434                 return 0;
435         }
436         else if (opcode == audioMasterCurrentId) {
437                 return current_loading_id;
438         }
439 #endif
440         else {
441                 return 0;
442         }
443 }
444
445 static bool
446 vstfx_get_info_from_file(char* dllpath, vector<VSTInfo*> *infos)
447 {
448         FILE* infofile;
449         bool rv = false;
450         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
451                 rv = vstfx_load_info_file(infofile, infos);
452                 fclose (infofile);
453                 if (!rv) {
454                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
455                 }
456         }
457         return rv;
458 }
459
460 void
461 vstfx_free_info (VSTInfo *info)
462 {
463         for (int i = 0; i < info->numParams; i++) {
464                 free (info->ParamNames[i]);
465                 free (info->ParamLabels[i]);
466         }
467
468         free (info->name);
469         free (info->creator);
470         free (info->Category);
471         free (info->ParamNames);
472         free (info->ParamLabels);
473         free (info);
474 }
475
476 void
477 vstfx_free_info_list (vector<VSTInfo *> *infos)
478 {
479         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
480                 vstfx_free_info(*i);
481         }
482         delete infos;
483 }
484
485 #ifdef LXVST_SUPPORT
486 /** Try to get plugin info - first by looking for a .fsi cache of the
487     data, and if that doesn't exist, load the plugin, get its data and
488     then cache it for future ref
489 */
490
491 vector<VSTInfo *> *
492 vstfx_get_info_lx (char* dllpath)
493 {
494         FILE* infofile;
495         VSTHandle* h;
496         VSTState* vstfx;
497         vector<VSTInfo*> *infos = new vector<VSTInfo*>;
498
499         // TODO check blacklist
500         // TODO pre-check file extension ?
501
502         if (vstfx_get_info_from_file(dllpath, infos)) {
503                 PBD::info << "using cache for LinuxVST plugin '" << dllpath << "'" << endmsg;
504                 return infos;
505         }
506
507         if (!(h = vstfx_load(dllpath))) {
508                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
509                 return infos;
510         }
511
512         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
513                 vstfx_unload(h);
514                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
515                 return infos;
516         }
517
518         infofile = vstfx_infofile_for_write (dllpath);
519
520         if (!infofile) {
521                 vstfx_close(vstfx);
522                 vstfx_unload(h);
523                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
524                 return 0;
525         }
526
527         VSTInfo* info = vstfx_info_from_plugin (vstfx);
528         infos->push_back(info); //XXX
529
530         vstfx_write_info_file (infofile, infos);
531         fclose (infofile);
532
533         vstfx_close (vstfx);
534         vstfx_unload (h);
535         return infos;
536 }
537 #endif
538
539 #ifdef WINDOWS_VST_SUPPORT
540 #include <fst.h>
541
542 vector<VSTInfo *> *
543 vstfx_get_info_fst (char* dllpath)
544 {
545         FILE* infofile;
546         VSTHandle* h;
547         VSTState* vstfx;
548         vector<VSTInfo*> *infos = new vector<VSTInfo*>;
549
550         // TODO check blacklist
551         // TODO pre-check file extension ?
552
553         if (vstfx_get_info_from_file(dllpath, infos)) {
554                 PBD::info << "using cache for VST plugin '" << dllpath << "'" << endmsg;
555                 return infos;
556         }
557
558         if(!(h = fst_load(dllpath))) {
559                 PBD::warning << "Cannot get VST information from " << dllpath << ": load failed." << endmsg;
560                 return infos;
561         }
562
563         if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
564                 fst_unload(&h);
565                 PBD::warning << "Cannot get VST information from " << dllpath << ": instantiation failed." << endmsg;
566                 return infos;
567         }
568
569         infofile = vstfx_infofile_for_write (dllpath);
570
571         if (!infofile) {
572                 fst_close(vstfx);
573                 //fst_unload(&h); // XXX -> fst_close()
574                 PBD::warning << "Cannot get VST information from " << dllpath << ": cannot create new FST info file." << endmsg;
575                 return 0;
576         }
577
578         VSTInfo* info = vstfx_info_from_plugin(vstfx);
579         infos->push_back(info); //XXX
580
581         vstfx_write_info_file (infofile, infos);
582         fclose (infofile);
583
584         fst_close(vstfx);
585         //fst_unload(&h); // XXX -> fst_close()
586         return infos;
587 }
588 #endif