plugin scan progress-display & preferences
[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 // TODO: make static parts of this file re-usable for standalone app
53 // TODO: namespace public API into ARDOUR, ::Session or ::PluginManager
54 //       consolidate vstfx_get_info_lx() and vstfx_get_info_fst()
55
56 /* prototypes */
57 #ifdef WINDOWS_VST_SUPPORT
58 #include <fst.h>
59 static bool
60 vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
61 #endif
62
63 #ifdef LXVST_SUPPORT
64 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
65 #endif
66
67 static int vstfx_current_loading_id = 0;
68
69 static char *
70 read_string (FILE *fp)
71 {
72         char buf[MAX_STRING_LEN];
73
74         if (!fgets (buf, MAX_STRING_LEN, fp)) {
75                 return 0;
76         }
77
78         if (strlen(buf) < MAX_STRING_LEN) {
79                 if (strlen (buf)) {
80                         buf[strlen(buf)-1] = 0;
81                 }
82                 return strdup (buf);
83         } else {
84                 return 0;
85         }
86 }
87
88 /** Read an integer value from a line in fp into n,
89  *  @return true on failure, false on success.
90  */
91 static bool
92 read_int (FILE* fp, int* n)
93 {
94         char buf[MAX_STRING_LEN];
95
96         char* p = fgets (buf, MAX_STRING_LEN, fp);
97         if (p == 0) {
98                 return true;
99         }
100
101         return (sscanf (p, "%d", n) != 1);
102 }
103
104 static void
105 vstfx_free_info (VSTInfo *info)
106 {
107         for (int i = 0; i < info->numParams; i++) {
108                 free (info->ParamNames[i]);
109                 free (info->ParamLabels[i]);
110         }
111
112         free (info->name);
113         free (info->creator);
114         free (info->Category);
115         free (info->ParamNames);
116         free (info->ParamLabels);
117         free (info);
118 }
119
120 static void
121 vstfx_clear_info_list (vector<VSTInfo *> *infos)
122 {
123         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
124                 vstfx_free_info(*i);
125         }
126         infos->clear();
127 }
128
129 static bool
130 vstfx_load_info_block(FILE* fp, VSTInfo *info)
131 {
132         if ((info->name = read_string(fp)) == 0) return false;
133         if ((info->creator = read_string(fp)) == 0) return false;
134         if (read_int (fp, &info->UniqueID)) return false;
135         if ((info->Category = read_string(fp)) == 0) return false;
136         if (read_int (fp, &info->numInputs)) return false;
137         if (read_int (fp, &info->numOutputs)) return false;
138         if (read_int (fp, &info->numParams)) return false;
139         if (read_int (fp, &info->wantMidi)) return false;
140         if (read_int (fp, &info->hasEditor)) return false;
141         if (read_int (fp, &info->canProcessReplacing)) return false;
142
143         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
144                 return false;
145         }
146
147         for (int i = 0; i < info->numParams; ++i) {
148                 if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
149         }
150
151         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
152                 return false;
153         }
154
155         for (int i = 0; i < info->numParams; ++i) {
156                 if ((info->ParamLabels[i] = read_string(fp)) == 0) {
157                         return false;
158                 }
159         }
160         return true;
161 }
162
163 static bool
164 vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
165 {
166         VSTInfo *info;
167         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
168                 return false;
169         }
170         if (vstfx_load_info_block(fp, info)) {
171                 if (strncmp (info->Category, "Shell", 5)) {
172                         infos->push_back(info);
173                 } else {
174                         int plugin_cnt = 0;
175                         vstfx_free_info(info);
176                         if (read_int (fp, &plugin_cnt)) {
177                                 for (int i = 0; i < plugin_cnt; i++) {
178                                         if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
179                                                 vstfx_clear_info_list(infos);
180                                                 return false;
181                                         }
182                                         if (vstfx_load_info_block(fp, info)) {
183                                                 infos->push_back(info);
184                                         } else {
185                                                 vstfx_free_info(info);
186                                                 vstfx_clear_info_list(infos);
187                                                 return false;
188                                         }
189                                 }
190                         } else {
191                                 return false; /* Bad file */
192                         }
193                 }
194                 return true;
195         }
196         vstfx_free_info(info);
197         vstfx_clear_info_list(infos);
198         return false;
199 }
200
201 static void
202 vstfx_write_info_block (FILE* fp, VSTInfo *info)
203 {
204         assert (info);
205         assert (fp);
206
207         fprintf (fp, "%s\n", info->name);
208         fprintf (fp, "%s\n", info->creator);
209         fprintf (fp, "%d\n", info->UniqueID);
210         fprintf (fp, "%s\n", info->Category);
211         fprintf (fp, "%d\n", info->numInputs);
212         fprintf (fp, "%d\n", info->numOutputs);
213         fprintf (fp, "%d\n", info->numParams);
214         fprintf (fp, "%d\n", info->wantMidi);
215         fprintf (fp, "%d\n", info->hasEditor);
216         fprintf (fp, "%d\n", info->canProcessReplacing);
217
218         for (int i = 0; i < info->numParams; i++) {
219                 fprintf (fp, "%s\n", info->ParamNames[i]);
220         }
221
222         for (int i = 0; i < info->numParams; i++) {
223                 fprintf (fp, "%s\n", info->ParamLabels[i]);
224         }
225 }
226
227 static void
228 vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
229 {
230         assert(infos);
231         assert(fp);
232
233         if (infos->size() > 1) {
234                 vector<VSTInfo *>::iterator x = infos->begin();
235                 /* write out the shell info first along with count of the number of
236                  * plugins contained in this shell
237                  */
238                 vstfx_write_info_block(fp, *x);
239                 fprintf( fp, "%d\n", infos->size() - 1 );
240                 ++x;
241                 /* Now write out the info for each plugin */
242                 for (; x != infos->end(); ++x) {
243                         vstfx_write_info_block(fp, *x);
244                 }
245         } else if (infos->size() == 1) {
246                 vstfx_write_info_block(fp, infos->front());
247         } else {
248                 PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
249         }
250 }
251
252 static string
253 vstfx_infofile_path (const char* dllpath, int personal)
254 {
255         string dir;
256         if (personal) {
257                 // TODO use XDG_CACHE_HOME
258                 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
259
260                 /* If the directory doesn't exist, try to create it */
261                 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
262                         if (g_mkdir (dir.c_str (), 0700)) {
263                                 return 0;
264                         }
265                 }
266
267         } else {
268                 dir = Glib::path_get_dirname (std::string(dllpath));
269         }
270
271         stringstream s;
272         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
273         return Glib::build_filename (dir, s.str ());
274 }
275
276 static char *
277 vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
278 {
279         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
280                 return 0;
281         }
282
283         string const path = vstfx_infofile_path (dllpath, personal);
284
285         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
286
287                 /* info file exists in same location as the shared object, so
288                    check if its current and up to date
289                 */
290
291
292                 struct stat dllstat;
293
294                 if (stat (dllpath, &dllstat) == 0) {
295                         if (stat (path.c_str(), statbuf) == 0) {
296                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
297                                         /* plugin is older than info file */
298                                         return strdup (path.c_str ());
299                                 }
300                         }
301                 }
302         }
303
304         return 0;
305 }
306
307
308 static FILE *
309 vstfx_infofile_for_read (const char* dllpath)
310 {
311         struct stat own_statbuf;
312         struct stat sys_statbuf;
313         FILE *rv = NULL;
314
315         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
316         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
317
318         if (own_info) {
319                 if (sys_info) {
320                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
321                                 /* system info file is newer, use it */
322                                 rv = g_fopen (sys_info, "rb");
323                         }
324                 } else {
325                         rv = g_fopen (own_info, "rb");
326                 }
327         } else if (sys_info) {
328                 rv = g_fopen (sys_info, "rb");
329         }
330         free(own_info);
331         free(sys_info);
332
333         return rv;
334 }
335
336 static FILE *
337 vstfx_infofile_create (const char* dllpath, int personal)
338 {
339         if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
340                 return 0;
341         }
342
343         string const path = vstfx_infofile_path (dllpath, personal);
344         return fopen (path.c_str(), "w");
345 }
346
347 static FILE *
348 vstfx_infofile_for_write (const char* dllpath)
349 {
350         FILE* f;
351
352         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
353                 f = vstfx_infofile_create (dllpath, 1);
354         }
355
356         return f;
357 }
358
359 static
360 int vstfx_can_midi (VSTState* vstfx)
361 {
362         AEffect* plugin = vstfx->plugin;
363
364         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
365
366         if (vst_version >= 2) {
367                 /* should we send it VST events (i.e. MIDI) */
368
369                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
370                         return -1;
371                 }
372         }
373
374         return false;
375 }
376
377 static VSTInfo*
378 vstfx_parse_vst_state (VSTState* vstfx)
379 {
380         assert (vstfx);
381
382         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
383         if (!info) {
384                 return 0;
385         }
386
387         /*We need to init the creator because some plugins
388           fail to implement getVendorString, and so won't stuff the
389           string with any name*/
390
391         char creator[65] = "Unknown\0";
392
393         AEffect* plugin = vstfx->plugin;
394
395         info->name = strdup (vstfx->handle->name);
396
397         /*If the plugin doesn't bother to implement GetVendorString we will
398           have pre-stuffed the string with 'Unkown' */
399
400         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
401
402         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
403           so if its just a zero length string we replace it with 'Unknown' */
404
405         if (strlen(creator) == 0) {
406                 info->creator = strdup ("Unknown");
407         } else {
408                 info->creator = strdup (creator);
409         }
410
411
412         switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
413         {
414                 case kPlugCategEffect:         info->Category = strdup ("Effect"); break;
415                 case kPlugCategSynth:          info->Category = strdup ("Synth"); break;
416                 case kPlugCategAnalysis:       info->Category = strdup ("Anaylsis"); break;
417                 case kPlugCategMastering:      info->Category = strdup ("Mastering"); break;
418                 case kPlugCategSpacializer:    info->Category = strdup ("Spacializer"); break;
419                 case kPlugCategRoomFx:         info->Category = strdup ("RoomFx"); break;
420                 case kPlugSurroundFx:          info->Category = strdup ("SurroundFx"); break;
421                 case kPlugCategRestoration:    info->Category = strdup ("Restoration"); break;
422                 case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
423                 case kPlugCategShell:          info->Category = strdup ("Shell"); break;
424                 case kPlugCategGenerator:      info->Category = strdup ("Generator"); break;
425                 default:                       info->Category = strdup ("Unknown"); break;
426         }
427
428         info->UniqueID = plugin->uniqueID;
429
430         info->numInputs = plugin->numInputs;
431         info->numOutputs = plugin->numOutputs;
432         info->numParams = plugin->numParams;
433         info->wantMidi = vstfx_can_midi(vstfx);
434         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
435         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
436         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
437         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
438
439         for (int i = 0; i < info->numParams; ++i) {
440                 char name[64];
441                 char label[64];
442
443                 /* Not all plugins give parameters labels as well as names */
444
445                 strcpy (name, "No Name");
446                 strcpy (label, "No Label");
447
448                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
449                 info->ParamNames[i] = strdup(name);
450
451                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
452                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
453                 info->ParamLabels[i] = strdup(label);
454         }
455         return info;
456 }
457
458 static void
459 vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, int type)
460 {
461         assert(vstfx);
462         VSTInfo *info;
463
464         if ((info = vstfx_parse_vst_state(vstfx))) {
465                 infos->push_back(info);
466 #if 1
467                 /* If this plugin is a Shell and we are not already inside a shell plugin
468                  * read the info for all of the plugins contained in this shell.
469                  */
470                 if (!strncmp (info->Category, "Shell", 5)
471                     && vstfx->handle->plugincnt == 1) {
472                         int id;
473                         vector< pair<int, string> > ids;
474                         AEffect *plugin = vstfx->plugin;
475                         string path = vstfx->handle->path;
476
477                         do {
478                                 char name[65] = "Unknown\0";
479                                 id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
480                                 ids.push_back(std::make_pair(id, name));
481                         } while ( id != 0 );
482
483                         switch(type) {
484 #ifdef WINDOWS_VST_SUPPORT
485                                 case 1: fst_close(vstfx); break;
486 #endif
487 #ifdef LXVST_SUPPORT
488                                 case 2: vstfx_close (vstfx); break;
489 #endif
490                                 default: assert(0); break;
491                         }
492
493                         for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
494                                 id = (*x).first;
495                                 if (id == 0) continue;
496                                 /* recurse vstfx_get_info() */
497
498                                 bool ok;
499                                 switch (type) { // TODO use lib ardour's type
500 #ifdef WINDOWS_VST_SUPPORT
501                                         case 1:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
502 #endif
503 #ifdef LXVST_SUPPORT
504                                         case 2:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
505 #endif
506                                         default: ok = false;
507                                 }
508                                 if (ok) {
509                                         // One shell (some?, all?) does not report the actual plugin name
510                                         // even after the shelled plugin has been instantiated.
511                                         // Replace the name of the shell with the real name.
512                                         info = infos->back();
513                                         free (info->name);
514
515                                         if ((*x).second.length() == 0) {
516                                                 info->name = strdup("Unknown");
517                                         }
518                                         else {
519                                                 info->name = strdup ((*x).second.c_str());
520                                         }
521                                 }
522                         }
523                 }
524 #endif
525         }
526 }
527
528 /* A simple 'dummy' audiomaster callback which should be ok,
529    we will only be instantiating the plugin in order to get its info
530 */
531
532 static intptr_t
533 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
534 {
535         const char* vstfx_can_do_strings[] = {
536                 "supportShell",
537                 "shellCategory"
538         };
539         const int vstfx_can_do_string_count = 2;
540
541         if (opcode == audioMasterVersion) {
542                 return 2400;
543         }
544         else if (opcode == audioMasterCanDo) {
545                 for (int i = 0; i < vstfx_can_do_string_count; i++) {
546                         if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
547                                 return 1;
548                         }
549                 }
550                 return 0;
551         }
552         else if (opcode == audioMasterCurrentId) {
553                 return vstfx_current_loading_id;
554         }
555         else {
556                 return 0;
557         }
558 }
559
560 static bool
561 vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
562 {
563         FILE* infofile;
564         bool rv = false;
565         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
566                 rv = vstfx_load_info_file(infofile, infos);
567                 fclose (infofile);
568                 if (!rv) {
569                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
570                 }
571         }
572         return rv;
573 }
574
575 #ifdef LXVST_SUPPORT
576 static bool
577 vstfx_instantiate_and_get_info_lx (
578                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
579 {
580         VSTHandle* h;
581         VSTState* vstfx;
582         if (!(h = vstfx_load(dllpath))) {
583                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
584                 return false;
585         }
586
587         vstfx_current_loading_id = uniqueID;
588
589         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
590                 vstfx_unload(h);
591                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
592                 return false;
593         }
594
595         vstfx_current_loading_id = 0;
596
597         vstfx_info_from_plugin(dllpath, vstfx, infos, 2);
598
599         vstfx_close (vstfx);
600         vstfx_unload (h);
601         return true;
602 }
603 #endif
604
605 #ifdef WINDOWS_VST_SUPPORT
606 static bool
607 vstfx_instantiate_and_get_info_fst (
608                 const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
609 {
610         VSTHandle* h;
611         VSTState* vstfx;
612         if(!(h = fst_load(dllpath))) {
613                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
614                 return false;
615         }
616
617         vstfx_current_loading_id = uniqueID;
618
619         if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
620                 fst_unload(&h);
621                 vstfx_current_loading_id = 0;
622                 PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
623                 return false;
624         }
625         vstfx_current_loading_id = 0;
626
627         vstfx_info_from_plugin(dllpath, vstfx, infos, 1);
628
629         fst_close(vstfx);
630         //fst_unload(&h); // XXX -> fst_close()
631         return true;
632 }
633 #endif
634
635 static vector<VSTInfo *> *
636 vstfx_get_info (const char* dllpath, int type)
637 {
638         FILE* infofile;
639         vector<VSTInfo*> *infos = new vector<VSTInfo*>;
640
641         // TODO check blacklist
642         // TODO pre-check file extension ?
643
644         if (vstfx_get_info_from_file(dllpath, infos)) {
645                 return infos;
646         }
647
648         bool ok;
649         // TODO blacklist (in case instantiat fails)
650         switch (type) { // TODO use lib ardour's type
651 #ifdef WINDOWS_VST_SUPPORT
652                 case 1:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
653 #endif
654 #ifdef LXVST_SUPPORT
655                 case 2:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
656 #endif
657                 default: ok = false;
658         }
659
660         if (!ok) {
661                 return infos;
662         }
663
664         // TODO un-blacklist
665
666         infofile = vstfx_infofile_for_write (dllpath);
667         if (!infofile) {
668                 PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
669                 return infos;
670         } else {
671                 vstfx_write_info_file (infofile, infos);
672                 fclose (infofile);
673         }
674         return infos;
675 }
676
677 /* *** public API *** */
678
679 void
680 vstfx_free_info_list (vector<VSTInfo *> *infos)
681 {
682         for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
683                 vstfx_free_info(*i);
684         }
685         delete infos;
686 }
687
688 #ifdef LXVST_SUPPORT
689 vector<VSTInfo *> *
690 vstfx_get_info_lx (char* dllpath)
691 {
692         return vstfx_get_info(dllpath, 2);
693 }
694 #endif
695
696 #ifdef WINDOWS_VST_SUPPORT
697 vector<VSTInfo *> *
698 vstfx_get_info_fst (char* dllpath)
699 {
700         return vstfx_get_info(dllpath, 1);
701 }
702 #endif