Merge branch 'windows' of git.ardour.org:ardour/ardour into windows
[ardour.git] / libs / ardour / linux_vst_info_file.cc
1 /*
2     Copyright (C) 2012 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
47 #define MAX_STRING_LEN 256
48
49 using namespace std;
50
51 static char *
52 read_string (FILE *fp)
53 {
54         char buf[MAX_STRING_LEN];
55
56         if (!fgets (buf, MAX_STRING_LEN, fp)) {
57                 return 0;
58         }
59         
60         if (strlen(buf) < MAX_STRING_LEN) {
61                 if (strlen (buf)) {
62                         buf[strlen(buf)-1] = 0;
63                 }
64                 return strdup (buf);
65         } else {
66                 return 0;
67         }
68 }
69
70 /** Read an integer value from a line in fp into n,
71  *  @return true on failure, false on success.
72  */
73 static bool
74 read_int (FILE* fp, int* n)
75 {
76         char buf[MAX_STRING_LEN];
77
78         char* p = fgets (buf, MAX_STRING_LEN, fp);
79         if (p == 0) {
80                 return true;
81         }
82
83         return (sscanf (p, "%d", n) != 1);
84 }
85
86 static VSTInfo *
87 load_vstfx_info_file (FILE* fp)
88 {
89         VSTInfo *info;
90         
91         if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
92                 return 0;
93         }
94
95         if ((info->name = read_string(fp)) == 0) goto error;
96         if ((info->creator = read_string(fp)) == 0) goto error;
97         if (read_int (fp, &info->UniqueID)) goto error;
98         if ((info->Category = read_string(fp)) == 0) goto error;
99         if (read_int (fp, &info->numInputs)) goto error;
100         if (read_int (fp, &info->numOutputs)) goto error;
101         if (read_int (fp, &info->numParams)) goto error;
102         if (read_int (fp, &info->wantMidi)) goto error;
103         if (read_int (fp, &info->hasEditor)) goto error;
104         if (read_int (fp, &info->canProcessReplacing)) goto error;
105
106         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
107                 goto error;
108         }
109
110         for (int i = 0; i < info->numParams; ++i) {
111                 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
112         }
113
114         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
115                 goto error;
116         }
117         
118         for (int i = 0; i < info->numParams; ++i) {
119                 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
120         }
121         
122         return info;
123         
124   error:
125         free (info);
126         return 0;
127 }
128
129 static int
130 save_vstfx_info_file (VSTInfo *info, FILE* fp)
131 {
132         assert (info);
133         assert (fp);
134     
135         fprintf (fp, "%s\n", info->name);
136         fprintf (fp, "%s\n", info->creator);
137         fprintf (fp, "%d\n", info->UniqueID);
138         fprintf (fp, "%s\n", info->Category);
139         fprintf (fp, "%d\n", info->numInputs);
140         fprintf (fp, "%d\n", info->numOutputs);
141         fprintf (fp, "%d\n", info->numParams);
142         fprintf (fp, "%d\n", info->wantMidi);
143         fprintf (fp, "%d\n", info->hasEditor);
144         fprintf (fp, "%d\n", info->canProcessReplacing);
145
146         for (int i = 0; i < info->numParams; i++) {
147                 fprintf (fp, "%s\n", info->ParamNames[i]);
148         }
149         
150         for (int i = 0; i < info->numParams; i++) {
151                 fprintf (fp, "%s\n", info->ParamLabels[i]);
152         }
153         
154     return 0;
155 }
156
157 static string
158 vstfx_infofile_path (char* dllpath, int personal)
159 {
160         string dir;
161         if (personal) {
162                 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
163
164                 /* If the directory doesn't exist, try to create it */
165                 if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
166                         if (g_mkdir (dir.c_str (), 0700)) {
167                                 return 0;
168                         }
169                 }
170                 
171         } else {
172                 dir = Glib::path_get_dirname (std::string(dllpath));
173         }
174
175         stringstream s;
176         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
177         return Glib::build_filename (dir, s.str ());
178 }
179
180 static char *
181 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
182 {
183         if (strstr (dllpath, ".so" ) == 0) {
184                 return 0;
185         }
186
187         string const path = vstfx_infofile_path (dllpath, personal);
188
189         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
190
191                 /* info file exists in same location as the shared object, so
192                    check if its current and up to date
193                 */
194
195
196                 struct stat dllstat;
197                 
198                 if (stat (dllpath, &dllstat) == 0) {
199                         if (stat (path.c_str(), statbuf) == 0) {
200                                 if (dllstat.st_mtime <= statbuf->st_mtime) {
201                                         /* plugin is older than info file */
202                                         return strdup (path.c_str ());
203                                 }
204                         }
205                 } 
206         }
207
208         return 0;
209 }
210
211
212 static FILE *
213 vstfx_infofile_for_read (char* dllpath)
214 {
215         struct stat own_statbuf;
216         struct stat sys_statbuf;
217         FILE *rv = NULL;
218         
219         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
220         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
221
222         if (own_info) {
223                 if (sys_info) {
224                         if (own_statbuf.st_mtime <= sys_statbuf.st_mtime) {
225                                 /* system info file is newer, use it */
226                                 rv = g_fopen (sys_info, "rb");
227                         }
228                 } else {
229                         rv = g_fopen (own_info, "rb");
230                 }
231         }
232         free(own_info);
233         free(sys_info);
234
235         return rv;
236 }
237
238 static FILE *
239 vstfx_infofile_create (char* dllpath, int personal)
240 {
241         if (strstr (dllpath, ".so" ) == 0) {
242                 return 0;
243         }
244
245         string const path = vstfx_infofile_path (dllpath, personal);
246         return fopen (path.c_str(), "w");
247 }
248
249 static FILE *
250 vstfx_infofile_for_write (char* dllpath)
251 {
252         FILE* f;
253
254         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
255                 f = vstfx_infofile_create (dllpath, 1);
256         }
257         
258         return f;
259 }
260
261 static
262 int vstfx_can_midi (VSTState* vstfx)
263 {
264         AEffect* plugin = vstfx->plugin;
265         
266         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
267
268         if (vst_version >= 2) {
269                 /* should we send it VST events (i.e. MIDI) */
270                 
271                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
272                         return -1;
273                 }
274         }
275         
276         return false;
277 }
278
279 static VSTInfo *
280 vstfx_info_from_plugin (VSTState* vstfx)
281 {
282         assert (vstfx);
283         
284         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
285         if (!info) {
286                 return 0;
287         }
288         
289         /*We need to init the creator because some plugins
290           fail to implement getVendorString, and so won't stuff the
291           string with any name*/
292         
293         char creator[65] = "Unknown\0";
294         
295         AEffect* plugin = vstfx->plugin;
296         
297         info->name = strdup (vstfx->handle->name); 
298         
299         /*If the plugin doesn't bother to implement GetVendorString we will
300           have pre-stuffed the string with 'Unkown' */
301         
302         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
303         
304         /*Some plugins DO implement GetVendorString, but DON'T put a name in it
305           so if its just a zero length string we replace it with 'Unknown' */
306         
307         if (strlen(creator) == 0) {
308                 info->creator = strdup ("Unknown");
309         } else {
310                 info->creator = strdup (creator);
311         }
312         
313         info->UniqueID = plugin->uniqueID;
314         
315         info->Category = strdup("None"); /* XXX */
316         info->numInputs = plugin->numInputs;
317         info->numOutputs = plugin->numOutputs;
318         info->numParams = plugin->numParams;
319         info->wantMidi = vstfx_can_midi(vstfx); 
320         info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
321         info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
322         info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
323         info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams);
324
325         for (int i = 0; i < info->numParams; ++i) {
326                 char name[64];
327                 char label[64];
328                 
329                 /* Not all plugins give parameters labels as well as names */
330                 
331                 strcpy (name, "No Name");
332                 strcpy (label, "No Label");
333                 
334                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
335                 info->ParamNames[i] = strdup(name);
336                 
337                 //NOTE: 'effGetParamLabel' is no longer defined in vestige headers
338                 //plugin->dispatcher (plugin, effGetParamLabel, i, 0, label, 0);
339                 info->ParamLabels[i] = strdup(label);
340         }
341         return info;
342 }
343
344 /* A simple 'dummy' audiomaster callback which should be ok,
345    we will only be instantiating the plugin in order to get its info
346 */
347
348 static intptr_t
349 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
350 {
351         if (opcode == audioMasterVersion) {
352                 return 2;
353         } else {
354                 return 0;
355         }
356 }
357
358 /** Try to get plugin info - first by looking for a .fsi cache of the
359     data, and if that doesn't exist, load the plugin, get its data and
360     then cache it for future ref
361 */
362
363 VSTInfo *
364 vstfx_get_info (char* dllpath)
365 {
366         FILE* infofile;
367         VSTHandle* h;
368         VSTState* vstfx;
369
370         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
371                 VSTInfo *info;
372                 info = load_vstfx_info_file (infofile);
373                 fclose (infofile);
374                 if (info == 0) {
375                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
376                 }
377                 return info;
378         } 
379         
380         if (!(h = vstfx_load(dllpath))) {
381                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
382                 return 0;
383         }
384         
385         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
386                 vstfx_unload(h);
387                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
388                 return 0;
389         }
390         
391         infofile = vstfx_infofile_for_write (dllpath);
392         
393         if (!infofile) {
394                 vstfx_close(vstfx);
395                 vstfx_unload(h);
396                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
397                 return 0;
398         }
399         
400         VSTInfo* info = vstfx_info_from_plugin (vstfx);
401         
402         save_vstfx_info_file (info, infofile);
403         fclose (infofile);
404         
405         vstfx_close (vstfx);
406         vstfx_unload (h);
407         
408         return info;
409 }
410
411 void
412 vstfx_free_info (VSTInfo *info)
413 {
414         for (int i = 0; i < info->numParams; i++) {
415                 free (info->ParamNames[i]);
416                 free (info->ParamLabels[i]);
417         }
418         
419         free (info->name);
420         free (info->creator);
421         free (info->Category);
422         free (info->ParamNames);
423         free (info->ParamLabels);
424         free (info);
425 }
426
427