add missing files
[ardour.git] / libs / ardour / linux_vst_info_file.cc
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.
4  */
5
6 #include <iostream>
7 #include <cassert>
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <errno.h>
13
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <libgen.h>
19
20 #include <glib.h>
21 #include <glib/gstdio.h>
22 #include <glibmm.h>
23
24 #include "pbd/error.h"
25
26 #include "ardour/linux_vst_support.h"
27
28 #define MAX_STRING_LEN 256
29
30 using namespace std;
31
32 static char *
33 read_string (FILE *fp)
34 {
35         char buf[MAX_STRING_LEN];
36
37         if (!fgets (buf, MAX_STRING_LEN, fp)) {
38                 return 0;
39         }
40         
41         if (strlen(buf) < MAX_STRING_LEN) {
42                 if (strlen (buf)) {
43                         buf[strlen(buf)-1] = 0;
44                 }
45                 return strdup (buf);
46         } else {
47                 return 0;
48         }
49 }
50
51 /** Read an integer value from a line in fp into n,
52  *  @return true on failure, false on success.
53  */
54 static bool
55 read_int (FILE* fp, int* n)
56 {
57         char buf[MAX_STRING_LEN];
58
59         char* p = fgets (buf, MAX_STRING_LEN, fp);
60         if (p == 0) {
61                 return true;
62         }
63
64         return (sscanf (p, "%d", n) != 1);
65 }
66
67 static VSTInfo *
68 load_vstfx_info_file (FILE* fp)
69 {
70         VSTInfo *info;
71         
72         if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
73                 return 0;
74         }
75
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;
86
87         if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
88                 goto error;
89         }
90
91         for (int i = 0; i < info->numParams; ++i) {
92                 if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
93         }
94
95         if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
96                 goto error;
97         }
98         
99         for (int i = 0; i < info->numParams; ++i) {
100                 if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
101         }
102         
103         return info;
104         
105   error:
106         free (info);
107         return 0;
108 }
109
110 static int
111 save_vstfx_info_file (VSTInfo *info, FILE* fp)
112 {
113         assert (info);
114         assert (fp);
115     
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);
126
127         for (int i = 0; i < info->numParams; i++) {
128                 fprintf (fp, "%s\n", info->ParamNames[i]);
129         }
130         
131         for (int i = 0; i < info->numParams; i++) {
132                 fprintf (fp, "%s\n", info->ParamLabels[i]);
133         }
134         
135     return 0;
136 }
137
138 static string
139 vstfx_infofile_path (char* dllpath, int personal)
140 {
141         string dir;
142         if (personal) {
143                 dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
144
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)) {
148                                 return 0;
149                         }
150                 }
151                 
152         } else {
153                 dir = Glib::path_get_dirname (dllpath);
154         }
155
156         stringstream s;
157         s << "." << Glib::path_get_basename (dllpath) << ".fsi";
158         return Glib::build_filename (dir, s.str ());
159 }
160
161 static char *
162 vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
163 {
164         if (strstr (dllpath, ".so" ) == 0) {
165                 return 0;
166         }
167
168         string const path = vstfx_infofile_path (dllpath, personal);
169
170         if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
171
172                 /* info file exists in same location as the shared object, so
173                    check if its current and up to date
174                 */
175
176
177                 struct stat dllstat;
178                 
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 ());
184                                 }
185                         }
186                 } 
187         }
188
189         return 0;
190 }
191
192
193 static FILE *
194 vstfx_infofile_for_read (char* dllpath)
195 {
196         struct stat own_statbuf;
197         struct stat sys_statbuf;
198         
199         char* own_info = vstfx_infofile_stat (dllpath, &own_statbuf, 1);
200         char* sys_info = vstfx_infofile_stat (dllpath, &sys_statbuf, 0);
201
202         if (own_info) {
203                 if (sys_info) {
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");
207                         }
208                 } else {
209                         return g_fopen (own_info, "rb");
210                 }
211         }
212
213         return 0;
214 }
215
216 static FILE *
217 vstfx_infofile_create (char* dllpath, int personal)
218 {
219         if (strstr (dllpath, ".so" ) == 0) {
220                 return 0;
221         }
222
223         string const path = vstfx_infofile_path (dllpath, personal);
224         return fopen (path.c_str(), "w");
225 }
226
227 static FILE *
228 vstfx_infofile_for_write (char* dllpath)
229 {
230         FILE* f;
231
232         if ((f = vstfx_infofile_create (dllpath, 0)) == 0) {
233                 f = vstfx_infofile_create (dllpath, 1);
234         }
235         
236         return f;
237 }
238
239 static
240 int vstfx_can_midi (VSTState* vstfx)
241 {
242         AEffect* plugin = vstfx->plugin;
243         
244         int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
245
246         if (vst_version >= 2) {
247                 /* should we send it VST events (i.e. MIDI) */
248                 
249                 if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
250                         return -1;
251                 }
252         }
253         
254         return false;
255 }
256
257 static VSTInfo *
258 vstfx_info_from_plugin (VSTState* vstfx)
259 {
260         assert (vstfx);
261         
262         VSTInfo* info = (VSTInfo*) malloc (sizeof (VSTInfo));
263         if (!info) {
264                 return 0;
265         }
266         
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*/
270         
271         char creator[65] = "Unknown\0";
272         
273         AEffect* plugin = vstfx->plugin;
274         
275         info->name = strdup (vstfx->handle->name); 
276         
277         /*If the plugin doesn't bother to implement GetVendorString we will
278           have pre-stuffed the string with 'Unkown' */
279         
280         plugin->dispatcher (plugin, effGetVendorString, 0, 0, creator, 0);
281         
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' */
284         
285         if (strlen(creator) == 0) {
286                 info->creator = strdup ("Unknown");
287         } else {
288                 info->creator = strdup (creator);
289         }
290         
291         info->UniqueID = plugin->uniqueID;
292         
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);
302
303         for (int i = 0; i < info->numParams; ++i) {
304                 char name[64];
305                 char label[64];
306                 
307                 /* Not all plugins give parameters labels as well as names */
308                 
309                 strcpy (name, "No Name");
310                 strcpy (label, "No Label");
311                 
312                 plugin->dispatcher (plugin, effGetParamName, i, 0, name, 0);
313                 info->ParamNames[i] = strdup(name);
314                 
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);
318         }
319         return info;
320 }
321
322 /* A simple 'dummy' audiomaster callback which should be ok,
323    we will only be instantiating the plugin in order to get its info
324 */
325
326 static intptr_t
327 simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
328 {
329         if (opcode == audioMasterVersion) {
330                 return 2;
331         } else {
332                 return 0;
333         }
334 }
335
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
339 */
340
341 VSTInfo *
342 vstfx_get_info (char* dllpath)
343 {
344         FILE* infofile;
345         VSTHandle* h;
346         VSTState* vstfx;
347
348         if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
349                 VSTInfo *info;
350                 info = load_vstfx_info_file (infofile);
351                 fclose (infofile);
352                 if (info == 0) {
353                         PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
354                 }
355                 return info;
356         } 
357         
358         if (!(h = vstfx_load(dllpath))) {
359                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
360                 return 0;
361         }
362         
363         if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
364                 vstfx_unload(h);
365                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
366                 return 0;
367         }
368         
369         infofile = vstfx_infofile_for_write (dllpath);
370         
371         if (!infofile) {
372                 vstfx_close(vstfx);
373                 vstfx_unload(h);
374                 PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
375                 return 0;
376         }
377         
378         VSTInfo* info = vstfx_info_from_plugin (vstfx);
379         
380         save_vstfx_info_file (info, infofile);
381         fclose (infofile);
382         
383         vstfx_close (vstfx);
384         vstfx_unload (h);
385         
386         return info;
387 }
388
389 void
390 vstfx_free_info (VSTInfo *info)
391 {
392         for (int i = 0; i < info->numParams; i++) {
393                 free (info->ParamNames[i]);
394                 free (info->ParamLabels[i]);
395         }
396         
397         free (info->name);
398         free (info->creator);
399         free (info->Category);
400         free (info);
401 }
402
403